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>
28 
29 #include "jpeg/Exif.hxx"
30 #include "jpeg/JpegTransform.hxx"
31 
33  : mrGraphic(rGraphic)
34 {
35 }
36 
38 
40 {
41  // Rotation can be between 0 and 3600
42  Degree10 aRotation = aInputRotation % 3600_deg10;
43 
44  if (aRotation == 0_deg10)
45  {
46  return; // No rotation is needed
47  }
48  else if (aRotation != 900_deg10 && aRotation != 1800_deg10 && aRotation != 2700_deg10)
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{
99  comphelper::makePropertyValue("Interlaced", sal_Int32(0)),
100  comphelper::makePropertyValue("Compression", sal_Int32(9)),
101  comphelper::makePropertyValue("Quality", sal_Int32(90))
102  };
103 
104  sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName(aType);
105 
106  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
107  aBitmap.Rotate(aRotation, COL_BLACK);
108  rFilter.ExportGraphic(aBitmap, "none", aStream, nFilterFormat, &aFilterData);
109 
110  aStream.Seek(STREAM_SEEK_TO_BEGIN);
111 
112  Graphic aGraphic;
113  rFilter.ImportGraphic(aGraphic, "import", aStream);
114 
115  mrGraphic = aGraphic;
116  return true;
117 }
118 
120 {
121  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
122 
123  if (aBitmap.GetSizePixel().Width() % 16 != 0 || aBitmap.GetSizePixel().Height() % 16 != 0)
124  {
125  rotateGeneric(aRotation, u"png");
126  }
127  else
128  {
129  GfxLink aLink = mrGraphic.GetGfxLink();
130 
131  SvMemoryStream aSourceStream;
132  aSourceStream.WriteBytes(aLink.GetData(), aLink.GetDataSize());
133  aSourceStream.Seek(STREAM_SEEK_TO_BEGIN);
134 
135  exif::Orientation aOrientation = exif::TOP_LEFT;
136 
137  Exif exif;
138  if (exif.read(aSourceStream))
139  {
140  aOrientation = exif.getOrientation();
141  }
142 
143  SvMemoryStream aTargetStream;
144  JpegTransform transform(aSourceStream, aTargetStream);
145  transform.setRotate(aRotation);
146  transform.perform();
147 
148  aTargetStream.Seek(STREAM_SEEK_TO_BEGIN);
149 
150  // Reset orientation in exif if needed
151  if (exif.hasExif() && aOrientation != exif::TOP_LEFT)
152  {
154  exif.write(aTargetStream);
155  }
156 
157  aTargetStream.Seek(STREAM_SEEK_TO_BEGIN);
158 
159  Graphic aGraphic;
161  rFilter.ImportGraphic(aGraphic, "import", aTargetStream);
162  mrGraphic = aGraphic;
163  }
164 }
165 
166 /* 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
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
bool read(SvStream &rStream)
Definition: Exif.cxx:69
sal_uInt64 Seek(sal_uInt64 nPos)
Class to import and export graphic formats.
constexpr tools::Long Width() const
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
bool rotateGeneric(Degree10 aRotation, std::u16string_view aType)
bool IsAnimated() const
Definition: graph.cxx:319
void setOrientation(exif::Orientation orientation)
Definition: Exif.cxx:33
GraphicNativeTransform(Graphic &rGraphic)
Orientation
Definition: Exif.hxx:27
#define STREAM_SEEK_TO_BEGIN
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:329
GfxLink GetGfxLink() const
Definition: graph.cxx:509
bool Rotate(Degree10 nAngle10, const Color &rFillColor)
Rotate bitmap by the specified angle.
Definition: BitmapEx.cxx:316
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
constexpr 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:73
void rotateJPEG(Degree10 aRotation)
bool hasExif() const
Definition: Exif.hxx:72