bump product version to 4.1.6.2
[LibreOffice.git] / vcl / source / filter / GraphicNativeTransform.cxx
blob69a10bdc65c2849307eb33f29128970f72f11985
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/GraphicNativeTransform.hxx>
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>
28 #include "jpeg/Exif.hxx"
29 #include "jpeg/JpegTransform.hxx"
31 GraphicNativeTransform::GraphicNativeTransform(Graphic& rGraphic) :
32 mrGraphic(rGraphic)
35 GraphicNativeTransform::~GraphicNativeTransform()
38 bool GraphicNativeTransform::canBeRotated()
40 GfxLink aLink = mrGraphic.GetLink();
42 // Don't allow rotation on animations for now
43 if (mrGraphic.IsAnimated())
45 return false;
48 if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_JPG
49 || aLink.GetType() == GFX_LINK_TYPE_NATIVE_PNG
50 || aLink.GetType() == GFX_LINK_TYPE_NATIVE_GIF
51 || aLink.GetType() == GFX_LINK_TYPE_NONE)
53 return true;
56 return false;
59 bool GraphicNativeTransform::rotate(sal_uInt16 aInputRotation)
61 // Rotation can be between 0 and 3600
62 sal_uInt16 aRotation = aInputRotation % 3600;
64 if (aRotation == 0)
66 return true; // No rotation is needed
68 else if (aRotation != 900 && aRotation != 1800 && aRotation != 2700)
70 return false;
73 GfxLink aLink = mrGraphic.GetLink();
74 if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_JPG )
76 return rotateJPEG(aRotation);
78 else if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_PNG )
80 return rotateGeneric(aRotation, OUString("png"));
82 else if ( aLink.GetType() == GFX_LINK_TYPE_NATIVE_GIF )
84 return rotateGeneric(aRotation, OUString("gif"));
86 else if ( aLink.GetType() == GFX_LINK_TYPE_NONE )
88 return rotateBitmapOnly(aRotation);
90 return false;
93 bool GraphicNativeTransform::rotateBitmapOnly(sal_uInt16 aRotation)
95 if (mrGraphic.IsAnimated())
97 return false;
100 BitmapEx aBitmap = mrGraphic.GetBitmapEx();
101 aBitmap.Rotate(aRotation, COL_BLACK);
102 mrGraphic = aBitmap;
104 return true;
107 bool GraphicNativeTransform::rotateGeneric(sal_uInt16 aRotation, OUString aType)
109 // Can't rotate animations yet
110 if (mrGraphic.IsAnimated())
112 return false;
115 SvMemoryStream aStream;
117 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
119 css::uno::Sequence< css::beans::PropertyValue > aFilterData( 3 );
120 aFilterData[ 0 ].Name = "Interlaced";
121 aFilterData[ 0 ].Value <<= (sal_Int32) 0;
122 aFilterData[ 1 ].Name = "Compression";
123 aFilterData[ 1 ].Value <<= (sal_Int32) 9;
124 aFilterData[ 2 ].Name = "Quality";
125 aFilterData[ 2 ].Value <<= (sal_Int32) 90;
127 sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aType );
129 BitmapEx aBitmap = mrGraphic.GetBitmapEx();
130 aBitmap.Rotate(aRotation, COL_BLACK);
131 rFilter.ExportGraphic( aBitmap, OUString( "none" ), aStream, nFilterFormat, &aFilterData );
133 aStream.Seek( STREAM_SEEK_TO_BEGIN );
135 Graphic aGraphic;
136 rFilter.ImportGraphic( aGraphic, OUString("import"), aStream );
138 mrGraphic = aGraphic;
139 return true;
142 bool GraphicNativeTransform::rotateJPEG(sal_uInt16 aRotation)
144 BitmapEx aBitmap = mrGraphic.GetBitmapEx();
146 if (aBitmap.GetSizePixel().Width() % 16 != 0 ||
147 aBitmap.GetSizePixel().Height() % 16 != 0 )
149 rotateGeneric(aRotation, OUString("png"));
151 else
153 GfxLink aLink = mrGraphic.GetLink();
155 SvMemoryStream aSourceStream;
156 aSourceStream.Write(aLink.GetData(), aLink.GetDataSize());
157 aSourceStream.Seek( STREAM_SEEK_TO_BEGIN );
159 Orientation aOrientation = TOP_LEFT;
161 Exif exif;
162 if ( exif.read(aSourceStream) )
164 aOrientation = exif.getOrientation();
167 SvMemoryStream aTargetStream;
168 JpegTransform tranform(aSourceStream, aTargetStream);
169 tranform.setRotate(aRotation);
170 tranform.perform();
172 aTargetStream.Seek( STREAM_SEEK_TO_BEGIN );
174 // Reset orientation in exif if needed
175 if ( exif.hasExif() && aOrientation != TOP_LEFT)
177 exif.setOrientation(TOP_LEFT);
178 exif.write(aTargetStream);
181 aTargetStream.Seek( STREAM_SEEK_TO_BEGIN );
183 Graphic aGraphic;
184 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
185 rFilter.ImportGraphic( aGraphic, OUString("import"), aTargetStream );
186 mrGraphic = aGraphic;
189 return true;
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */