LibreOffice Module vcl (master)  1
GraphicNativeTransform.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
21 
22 #include <vcl/gfxlink.hxx>
23 #include <vcl/graphicfilter.hxx>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <tools/stream.hxx>
27 
28 #include "jpeg/Exif.hxx"
29 #include "jpeg/JpegTransform.hxx"
30 
32  : mrGraphic(rGraphic)
33 {
34 }
35 
37 
39 {
40  // Rotation can be between 0 and 3600
41  Degree10 aRotation = aInputRotation % Degree10(3600);
42 
43  if (aRotation == Degree10(0))
44  {
45  return; // No rotation is needed
46  }
47  else if (aRotation != Degree10(900) && aRotation != Degree10(1800)
48  && aRotation != Degree10(2700))
49  {
50  return;
51  }
52 
53  GfxLink aLink = mrGraphic.GetGfxLink();
54  if (aLink.GetType() == GfxLinkType::NativeJpg)
55  {
56  rotateJPEG(aRotation);
57  }
58  else if (aLink.GetType() == GfxLinkType::NativePng)
59  {
60  rotateGeneric(aRotation, u"png");
61  }
62  else if (aLink.GetType() == GfxLinkType::NativeGif)
63  {
64  rotateGeneric(aRotation, u"gif");
65  }
66  else if (aLink.GetType() == GfxLinkType::NONE)
67  {
68  rotateBitmapOnly(aRotation);
69  }
70 }
71 
73 {
74  if (mrGraphic.IsAnimated())
75  {
76  return false;
77  }
78 
79  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
80  aBitmap.Rotate(aRotation, COL_BLACK);
81  mrGraphic = aBitmap;
82 
83  return true;
84 }
85 
86 bool GraphicNativeTransform::rotateGeneric(Degree10 aRotation, std::u16string_view aType)
87 {
88  // Can't rotate animations yet
89  if (mrGraphic.IsAnimated())
90  {
91  return false;
92  }
93 
94  SvMemoryStream aStream;
95 
97 
98  css::uno::Sequence<css::beans::PropertyValue> aFilterData(3);
99  aFilterData[0].Name = "Interlaced";
100  aFilterData[0].Value <<= sal_Int32(0);
101  aFilterData[1].Name = "Compression";
102  aFilterData[1].Value <<= sal_Int32(9);
103  aFilterData[2].Name = "Quality";
104  aFilterData[2].Value <<= sal_Int32(90);
105 
106  sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName(aType);
107 
108  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
109  aBitmap.Rotate(aRotation, COL_BLACK);
110  rFilter.ExportGraphic(aBitmap, "none", aStream, nFilterFormat, &aFilterData);
111 
112  aStream.Seek(STREAM_SEEK_TO_BEGIN);
113 
114  Graphic aGraphic;
115  rFilter.ImportGraphic(aGraphic, "import", aStream);
116 
117  mrGraphic = aGraphic;
118  return true;
119 }
120 
122 {
123  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
124 
125  if (aBitmap.GetSizePixel().Width() % 16 != 0 || aBitmap.GetSizePixel().Height() % 16 != 0)
126  {
127  rotateGeneric(aRotation, u"png");
128  }
129  else
130  {
131  GfxLink aLink = mrGraphic.GetGfxLink();
132 
133  SvMemoryStream aSourceStream;
134  aSourceStream.WriteBytes(aLink.GetData(), aLink.GetDataSize());
135  aSourceStream.Seek(STREAM_SEEK_TO_BEGIN);
136 
137  exif::Orientation aOrientation = exif::TOP_LEFT;
138 
139  Exif exif;
140  if (exif.read(aSourceStream))
141  {
142  aOrientation = exif.getOrientation();
143  }
144 
145  SvMemoryStream aTargetStream;
146  JpegTransform transform(aSourceStream, aTargetStream);
147  transform.setRotate(aRotation);
148  transform.perform();
149 
150  aTargetStream.Seek(STREAM_SEEK_TO_BEGIN);
151 
152  // Reset orientation in exif if needed
153  if (exif.hasExif() && aOrientation != exif::TOP_LEFT)
154  {
156  exif.write(aTargetStream);
157  }
158 
159  aTargetStream.Seek(STREAM_SEEK_TO_BEGIN);
160 
161  Graphic aGraphic;
163  rFilter.ImportGraphic(aGraphic, "import", aTargetStream);
164  mrGraphic = aGraphic;
165  }
166 }
167 
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
Definition: Exif.hxx:25
void write(SvStream &rStream)
Definition: Exif.cxx:78
bool read(SvStream &rStream)
Definition: Exif.cxx:69
sal_uInt64 Seek(sal_uInt64 nPos)
Class to import and export graphic formats.
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
bool rotateGeneric(Degree10 aRotation, std::u16string_view aType)
bool IsAnimated() const
Definition: graph.cxx:315
void setOrientation(exif::Orientation orientation)
Definition: Exif.cxx:33
GraphicNativeTransform(Graphic &rGraphic)
Orientation
Definition: Exif.hxx:27
#define STREAM_SEEK_TO_BEGIN
tools::Long Width() const
std::size_t WriteBytes(const void *pData, std::size_t nSize)
float u
bool rotateBitmapOnly(Degree10 aRotation)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Definition: graph.cxx:325
GfxLink GetGfxLink() const
Definition: graph.cxx:503
bool Rotate(Degree10 nAngle10, const Color &rFillColor)
Rotate bitmap by the specified angle.
Definition: bitmapex.cxx:386
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
exif::Orientation getOrientation() const
Definition: Exif.hxx:74
tools::Long Height() const
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
Definition: Exif.hxx:43
static GraphicFilter & GetGraphicFilter()
void rotate(Degree10 aRotation)
void setRotate(Degree10 aRotate)
const Size & GetSizePixel() const
Definition: bitmapex.hxx:84
void rotateJPEG(Degree10 aRotation)
bool hasExif() const
Definition: Exif.hxx:72