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