Line data Source code
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 :
20 : #include <vcl/GraphicNativeTransform.hxx>
21 :
22 : #include <vcl/gfxlink.hxx>
23 : #include <vcl/graphicfilter.hxx>
24 : #include <com/sun/star/uno/Sequence.hxx>
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 :
27 : #include "jpeg/Exif.hxx"
28 : #include "jpeg/JpegTransform.hxx"
29 :
30 0 : GraphicNativeTransform::GraphicNativeTransform(Graphic& rGraphic) :
31 0 : mrGraphic(rGraphic)
32 0 : {}
33 :
34 0 : GraphicNativeTransform::~GraphicNativeTransform()
35 0 : {}
36 :
37 0 : bool GraphicNativeTransform::canBeRotated()
38 : {
39 0 : GfxLink aLink = mrGraphic.GetLink();
40 :
41 : // Don't allow rotation on animations for now
42 0 : if (mrGraphic.IsAnimated())
43 : {
44 0 : return false;
45 : }
46 :
47 0 : if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_JPG
48 0 : || aLink.GetType() == GFX_LINK_TYPE_NATIVE_PNG
49 0 : || aLink.GetType() == GFX_LINK_TYPE_NATIVE_GIF
50 0 : || aLink.GetType() == GFX_LINK_TYPE_NONE)
51 : {
52 0 : return true;
53 : }
54 :
55 0 : return false;
56 : }
57 :
58 0 : bool GraphicNativeTransform::rotate(sal_uInt16 aInputRotation)
59 : {
60 : // Rotation can be between 0 and 3600
61 0 : sal_uInt16 aRotation = aInputRotation % 3600;
62 :
63 0 : if (aRotation == 0)
64 : {
65 0 : return true; // No rotation is needed
66 : }
67 0 : else if (aRotation != 900 && aRotation != 1800 && aRotation != 2700)
68 : {
69 0 : return false;
70 : }
71 :
72 0 : GfxLink aLink = mrGraphic.GetLink();
73 0 : if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_JPG )
74 : {
75 0 : return rotateJPEG(aRotation);
76 : }
77 0 : else if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_PNG )
78 : {
79 0 : return rotateGeneric(aRotation, OUString("png"));
80 : }
81 0 : else if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_GIF )
82 : {
83 0 : return rotateGeneric(aRotation, OUString("gif"));
84 : }
85 0 : else if ( aLink.GetType() == GFX_LINK_TYPE_NONE )
86 : {
87 0 : return rotateBitmapOnly(aRotation);
88 : }
89 0 : return false;
90 : }
91 :
92 0 : bool GraphicNativeTransform::rotateBitmapOnly(sal_uInt16 aRotation)
93 : {
94 0 : if (mrGraphic.IsAnimated())
95 : {
96 0 : return false;
97 : }
98 :
99 0 : BitmapEx aBitmap = mrGraphic.GetBitmapEx();
100 0 : aBitmap.Rotate(aRotation, COL_BLACK);
101 0 : mrGraphic = aBitmap;
102 :
103 0 : return true;
104 : }
105 :
106 0 : bool GraphicNativeTransform::rotateGeneric(sal_uInt16 aRotation, const OUString& aType)
107 : {
108 : // Can't rotate animations yet
109 0 : if (mrGraphic.IsAnimated())
110 : {
111 0 : return false;
112 : }
113 :
114 0 : SvMemoryStream aStream;
115 :
116 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
117 :
118 0 : css::uno::Sequence< css::beans::PropertyValue > aFilterData( 3 );
119 0 : aFilterData[ 0 ].Name = "Interlaced";
120 0 : aFilterData[ 0 ].Value <<= (sal_Int32) 0;
121 0 : aFilterData[ 1 ].Name = "Compression";
122 0 : aFilterData[ 1 ].Value <<= (sal_Int32) 9;
123 0 : aFilterData[ 2 ].Name = "Quality";
124 0 : aFilterData[ 2 ].Value <<= (sal_Int32) 90;
125 :
126 0 : sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aType );
127 :
128 0 : BitmapEx aBitmap = mrGraphic.GetBitmapEx();
129 0 : aBitmap.Rotate(aRotation, COL_BLACK);
130 0 : rFilter.ExportGraphic( aBitmap, OUString( "none" ), aStream, nFilterFormat, &aFilterData );
131 :
132 0 : aStream.Seek( STREAM_SEEK_TO_BEGIN );
133 :
134 0 : Graphic aGraphic;
135 0 : rFilter.ImportGraphic( aGraphic, OUString("import"), aStream );
136 :
137 0 : mrGraphic = aGraphic;
138 0 : return true;
139 : }
140 :
141 0 : bool GraphicNativeTransform::rotateJPEG(sal_uInt16 aRotation)
142 : {
143 0 : BitmapEx aBitmap = mrGraphic.GetBitmapEx();
144 :
145 0 : if (aBitmap.GetSizePixel().Width() % 16 != 0 ||
146 0 : aBitmap.GetSizePixel().Height() % 16 != 0 )
147 : {
148 0 : rotateGeneric(aRotation, OUString("png"));
149 : }
150 : else
151 : {
152 0 : GfxLink aLink = mrGraphic.GetLink();
153 :
154 0 : SvMemoryStream aSourceStream;
155 0 : aSourceStream.Write(aLink.GetData(), aLink.GetDataSize());
156 0 : aSourceStream.Seek( STREAM_SEEK_TO_BEGIN );
157 :
158 0 : Orientation aOrientation = TOP_LEFT;
159 :
160 0 : Exif exif;
161 0 : if ( exif.read(aSourceStream) )
162 : {
163 0 : aOrientation = exif.getOrientation();
164 : }
165 :
166 0 : SvMemoryStream aTargetStream;
167 0 : JpegTransform tranform(aSourceStream, aTargetStream);
168 0 : tranform.setRotate(aRotation);
169 0 : tranform.perform();
170 :
171 0 : aTargetStream.Seek( STREAM_SEEK_TO_BEGIN );
172 :
173 : // Reset orientation in exif if needed
174 0 : if ( exif.hasExif() && aOrientation != TOP_LEFT)
175 : {
176 0 : exif.setOrientation(TOP_LEFT);
177 0 : exif.write(aTargetStream);
178 : }
179 :
180 0 : aTargetStream.Seek( STREAM_SEEK_TO_BEGIN );
181 :
182 0 : Graphic aGraphic;
183 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
184 0 : rFilter.ImportGraphic( aGraphic, OUString("import"), aTargetStream );
185 0 : mrGraphic = aGraphic;
186 : }
187 :
188 0 : return true;
189 801 : }
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|