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 
31 using namespace ::exif;
32 
34  mrGraphic(rGraphic)
35 {}
36 
38 {}
39 
40 void GraphicNativeTransform::rotate(sal_uInt16 aInputRotation)
41 {
42  // Rotation can be between 0 and 3600
43  sal_uInt16 aRotation = aInputRotation % 3600;
44 
45  if (aRotation == 0)
46  {
47  return; // No rotation is needed
48  }
49  else if (aRotation != 900 && aRotation != 1800 && aRotation != 2700)
50  {
51  return;
52  }
53 
54  GfxLink aLink = mrGraphic.GetGfxLink();
55  if ( aLink.GetType() == GfxLinkType::NativeJpg )
56  {
57  rotateJPEG(aRotation);
58  }
59  else if ( aLink.GetType() == GfxLinkType::NativePng )
60  {
61  rotateGeneric(aRotation, "png");
62  }
63  else if ( aLink.GetType() == GfxLinkType::NativeGif )
64  {
65  rotateGeneric(aRotation, "gif");
66  }
67  else if ( aLink.GetType() == GfxLinkType::NONE )
68  {
69  rotateBitmapOnly(aRotation);
70  }
71 }
72 
73 bool GraphicNativeTransform::rotateBitmapOnly(sal_uInt16 aRotation)
74 {
75  if (mrGraphic.IsAnimated())
76  {
77  return false;
78  }
79 
80  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
81  aBitmap.Rotate(aRotation, COL_BLACK);
82  mrGraphic = aBitmap;
83 
84  return true;
85 }
86 
87 bool GraphicNativeTransform::rotateGeneric(sal_uInt16 aRotation, const OUString& aType)
88 {
89  // Can't rotate animations yet
90  if (mrGraphic.IsAnimated())
91  {
92  return false;
93  }
94 
95  SvMemoryStream aStream;
96 
98 
99  css::uno::Sequence< css::beans::PropertyValue > aFilterData( 3 );
100  aFilterData[ 0 ].Name = "Interlaced";
101  aFilterData[ 0 ].Value <<= sal_Int32(0);
102  aFilterData[ 1 ].Name = "Compression";
103  aFilterData[ 1 ].Value <<= sal_Int32(9);
104  aFilterData[ 2 ].Name = "Quality";
105  aFilterData[ 2 ].Value <<= sal_Int32(90);
106 
107  sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aType );
108 
109  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
110  aBitmap.Rotate(aRotation, COL_BLACK);
111  rFilter.ExportGraphic( aBitmap, "none", aStream, nFilterFormat, &aFilterData );
112 
113  aStream.Seek( STREAM_SEEK_TO_BEGIN );
114 
115  Graphic aGraphic;
116  rFilter.ImportGraphic( aGraphic, "import", aStream );
117 
118  mrGraphic = aGraphic;
119  return true;
120 }
121 
122 void GraphicNativeTransform::rotateJPEG(sal_uInt16 aRotation)
123 {
124  BitmapEx aBitmap = mrGraphic.GetBitmapEx();
125 
126  if (aBitmap.GetSizePixel().Width() % 16 != 0 ||
127  aBitmap.GetSizePixel().Height() % 16 != 0 )
128  {
129  rotateGeneric(aRotation, "png");
130  }
131  else
132  {
133  GfxLink aLink = mrGraphic.GetGfxLink();
134 
135  SvMemoryStream aSourceStream;
136  aSourceStream.WriteBytes(aLink.GetData(), aLink.GetDataSize());
137  aSourceStream.Seek( STREAM_SEEK_TO_BEGIN );
138 
139  Orientation aOrientation = TOP_LEFT;
140 
141  Exif exif;
142  if ( exif.read(aSourceStream) )
143  {
144  aOrientation = exif.getOrientation();
145  }
146 
147  SvMemoryStream aTargetStream;
148  JpegTransform transform(aSourceStream, aTargetStream);
149  transform.setRotate(aRotation);
150  transform.perform();
151 
152  aTargetStream.Seek( STREAM_SEEK_TO_BEGIN );
153 
154  // Reset orientation in exif if needed
155  if ( exif.hasExif() && aOrientation != TOP_LEFT)
156  {
157  exif.setOrientation(TOP_LEFT);
158  exif.write(aTargetStream);
159  }
160 
161  aTargetStream.Seek( STREAM_SEEK_TO_BEGIN );
162 
163  Graphic aGraphic;
165  rFilter.ImportGraphic( aGraphic, "import", aTargetStream );
166  mrGraphic = aGraphic;
167  }
168 }
169 
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void rotateJPEG(sal_uInt16 aRotation)
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
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
long Height() const
void write(SvStream &rStream)
Definition: Exif.cxx:82
bool read(SvStream &rStream)
Definition: Exif.cxx:73
sal_uInt64 Seek(sal_uInt64 nPos)
Class to import and export graphic formats.
sal_uInt16 GetExportFormatNumberForShortName(const OUString &rShortName)
bool IsAnimated() const
Definition: graph.cxx:339
void setOrientation(exif::Orientation orientation)
Definition: Exif.cxx:36
GraphicNativeTransform(Graphic &rGraphic)
void setRotate(sal_uInt16 aRotate)
#define STREAM_SEEK_TO_BEGIN
std::size_t WriteBytes(const void *pData, std::size_t nSize)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Definition: graph.cxx:349
GfxLink GetGfxLink() const
Definition: graph.cxx:527
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:73
Orientation
Definition: prntypes.hxx:31
Definition: Exif.hxx:42
void rotate(sal_uInt16 aRotation)
static GraphicFilter & GetGraphicFilter()
const Size & GetSizePixel() const
Definition: bitmapex.hxx:83
bool rotateGeneric(sal_uInt16 aRotation, const OUString &aType)
bool rotateBitmapOnly(sal_uInt16 aRotation)
bool hasExif() const
Definition: Exif.hxx:71
bool Rotate(long nAngle10, const Color &rFillColor)
Rotate bitmap by the specified angle.
Definition: bitmapex.cxx:390