tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / vcl / source / filter / GraphicNativeTransform.cxx
blob88078b2499cb85a8ffcb529ffe572ca97d62e749
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/beans/PropertyValue.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <tools/stream.hxx>
27 #include <comphelper/propertyvalue.hxx>
29 #include "jpeg/Exif.hxx"
30 #include "jpeg/JpegTransform.hxx"
32 GraphicNativeTransform::GraphicNativeTransform(Graphic& rGraphic)
33 : mrGraphic(rGraphic)
37 void GraphicNativeTransform::rotate(Degree10 aInputRotation)
39 // Rotation can be between 0 and 3600
40 Degree10 aRotation = aInputRotation % 3600_deg10;
42 if (aRotation == 0_deg10)
44 return; // No rotation is needed
46 else if (aRotation != 900_deg10 && aRotation != 1800_deg10 && aRotation != 2700_deg10)
48 return;
51 GfxLink aLink = mrGraphic.GetGfxLink();
52 if (aLink.GetType() == GfxLinkType::NativeJpg)
54 rotateJPEG(aRotation);
56 else if (aLink.GetType() == GfxLinkType::NativePng)
58 rotateGeneric(aRotation, u"png");
60 else if (aLink.GetType() == GfxLinkType::NativeGif)
62 rotateGeneric(aRotation, u"gif");
64 else if (aLink.GetType() == GfxLinkType::NONE)
66 rotateBitmapOnly(aRotation);
70 bool GraphicNativeTransform::rotateBitmapOnly(Degree10 aRotation)
72 if (mrGraphic.IsAnimated())
74 return false;
77 BitmapEx aBitmap = mrGraphic.GetBitmapEx();
78 aBitmap.Rotate(aRotation, COL_BLACK);
79 mrGraphic = aBitmap;
81 return true;
84 bool GraphicNativeTransform::rotateGeneric(Degree10 aRotation, std::u16string_view aType)
86 // Can't rotate animations yet
87 if (mrGraphic.IsAnimated())
89 return false;
92 SvMemoryStream aStream;
94 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
96 css::uno::Sequence<css::beans::PropertyValue> aFilterData{
97 comphelper::makePropertyValue(u"Interlaced"_ustr, sal_Int32(0)),
98 comphelper::makePropertyValue(u"Compression"_ustr, sal_Int32(9)),
99 comphelper::makePropertyValue(u"Quality"_ustr, sal_Int32(90))
102 sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName(aType);
104 BitmapEx aBitmap = mrGraphic.GetBitmapEx();
105 aBitmap.Rotate(aRotation, COL_BLACK);
106 rFilter.ExportGraphic(aBitmap, u"none", aStream, nFilterFormat, &aFilterData);
108 aStream.Seek(STREAM_SEEK_TO_BEGIN);
110 Graphic aGraphic;
111 rFilter.ImportGraphic(aGraphic, u"import", aStream);
113 mrGraphic = std::move(aGraphic);
114 return true;
117 void GraphicNativeTransform::rotateJPEG(Degree10 aRotation)
119 BitmapEx aBitmap = mrGraphic.GetBitmapEx();
121 if (aBitmap.GetSizePixel().Width() % 16 != 0 || aBitmap.GetSizePixel().Height() % 16 != 0)
123 rotateGeneric(aRotation, u"png");
125 else
127 GfxLink aLink = mrGraphic.GetGfxLink();
129 SvMemoryStream aSourceStream;
130 aSourceStream.WriteBytes(aLink.GetData(), aLink.GetDataSize());
131 aSourceStream.Seek(STREAM_SEEK_TO_BEGIN);
133 exif::Orientation aOrientation = exif::TOP_LEFT;
135 Exif exif;
136 if (exif.read(aSourceStream))
138 aOrientation = exif.getOrientation();
141 SvMemoryStream aTargetStream;
142 JpegTransform transform(aSourceStream, aTargetStream);
143 transform.setRotate(aRotation);
144 transform.perform();
146 aTargetStream.Seek(STREAM_SEEK_TO_BEGIN);
148 // Reset orientation in exif if needed
149 if (exif.hasExif() && aOrientation != exif::TOP_LEFT)
151 exif.setOrientation(exif::TOP_LEFT);
152 exif.write(aTargetStream);
155 aTargetStream.Seek(STREAM_SEEK_TO_BEGIN);
157 Graphic aGraphic;
158 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
159 rFilter.ImportGraphic(aGraphic, u"import", aTargetStream);
160 mrGraphic = std::move(aGraphic);
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */