LibreOffice Module vcl (master)  1
JpegWriter.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 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include "jpeg.h"
24 #include <jpeglib.h>
25 #include <jerror.h>
26 
27 #include "JpegWriter.hxx"
28 #include <vcl/bitmapaccess.hxx>
29 #include <vcl/FilterConfigItem.hxx>
30 #include <tools/helpers.hxx>
31 #include <tools/stream.hxx>
32 
33 #define BUFFER_SIZE 4096
34 
36 {
37  jpeg_destination_mgr pub; /* public fields */
38  SvStream* stream; /* target stream */
39  JOCTET * buffer; /* start of buffer */
40 };
41 
42 extern "C" {
43 
44 static void init_destination (j_compress_ptr cinfo)
45 {
46  DestinationManagerStruct * destination = reinterpret_cast<DestinationManagerStruct *>(cinfo->dest);
47 
48  /* Allocate the output buffer -- it will be released when done with image */
49  destination->buffer = static_cast<JOCTET *>(
50  (*cinfo->mem->alloc_small) (reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE, BUFFER_SIZE * sizeof(JOCTET)));
51 
52  destination->pub.next_output_byte = destination->buffer;
53  destination->pub.free_in_buffer = BUFFER_SIZE;
54 }
55 
56 static boolean empty_output_buffer (j_compress_ptr cinfo)
57 {
58  DestinationManagerStruct * destination = reinterpret_cast<DestinationManagerStruct *>(cinfo->dest);
59 
60  if (destination->stream->WriteBytes(destination->buffer, BUFFER_SIZE) != BUFFER_SIZE)
61  {
62  ERREXIT(cinfo, JERR_FILE_WRITE);
63  }
64 
65  destination->pub.next_output_byte = destination->buffer;
66  destination->pub.free_in_buffer = BUFFER_SIZE;
67 
68  return TRUE;
69 }
70 
71 static void term_destination (j_compress_ptr cinfo)
72 {
73  DestinationManagerStruct * destination = reinterpret_cast<DestinationManagerStruct *>(cinfo->dest);
74  size_t datacount = BUFFER_SIZE - destination->pub.free_in_buffer;
75 
76  /* Write any data remaining in the buffer */
77  if (datacount > 0)
78  {
79  if (destination->stream->WriteBytes(destination->buffer, datacount) != datacount)
80  {
81  ERREXIT(cinfo, JERR_FILE_WRITE);
82  }
83  }
84 }
85 
86 }
87 
88 void jpeg_svstream_dest (j_compress_ptr cinfo, void* output)
89 {
90  SvStream* stream = static_cast<SvStream*>(output);
91  DestinationManagerStruct * destination;
92 
93  /* The destination object is made permanent so that multiple JPEG images
94  * can be written to the same file without re-executing jpeg_svstream_dest.
95  * This makes it dangerous to use this manager and a different destination
96  * manager serially with the same JPEG object, because their private object
97  * sizes may be different. Caveat programmer.
98  */
99  if (cinfo->dest == nullptr)
100  { /* first time for this JPEG object? */
101  cinfo->dest = static_cast<jpeg_destination_mgr*>(
102  (*cinfo->mem->alloc_small) (reinterpret_cast<j_common_ptr>(cinfo), JPOOL_PERMANENT, sizeof(DestinationManagerStruct)));
103  }
104 
105  destination = reinterpret_cast<DestinationManagerStruct *>(cinfo->dest);
106  destination->pub.init_destination = init_destination;
107  destination->pub.empty_output_buffer = empty_output_buffer;
108  destination->pub.term_destination = term_destination;
109  destination->stream = stream;
110 }
111 
112 JPEGWriter::JPEGWriter( SvStream& rStream, const css::uno::Sequence< css::beans::PropertyValue >* pFilterData, bool* pExportWasGrey ) :
113  mrStream ( rStream ),
114  mpBuffer ( nullptr ),
115  mbNative ( false ),
116  mpExpWasGrey ( pExportWasGrey )
117 {
118  FilterConfigItem aConfigItem( pFilterData );
119  mbGreys = aConfigItem.ReadInt32( "ColorMode", 0 ) != 0;
120  mnQuality = aConfigItem.ReadInt32( "Quality", 75 );
121  maChromaSubsampling = aConfigItem.ReadInt32( "ChromaSubsamplingMode", 0 );
122 
123  if ( pFilterData )
124  {
125  for( const auto& rValue : *pFilterData )
126  {
127  if ( rValue.Name == "StatusIndicator" )
128  {
129  rValue.Value >>= mxStatusIndicator;
130  }
131  }
132  }
133 }
134 
135 void* JPEGWriter::GetScanline( long nY )
136 {
137  void* pScanline = nullptr;
138 
139  if( mpReadAccess )
140  {
141  if( mbNative )
142  {
143  pScanline = mpReadAccess->GetScanline( nY );
144  }
145  else if( mpBuffer )
146  {
147  BitmapColor aColor;
148  long nWidth = mpReadAccess->Width();
149  sal_uInt8* pTmp = mpBuffer;
150 
151  if( mpReadAccess->HasPalette() )
152  {
153  Scanline pScanlineRead = mpReadAccess->GetScanline( nY );
154  for( long nX = 0; nX < nWidth; nX++ )
155  {
156  aColor = mpReadAccess->GetPaletteColor( mpReadAccess->GetIndexFromData( pScanlineRead, nX ) );
157  *pTmp++ = aColor.GetRed();
158  if ( !mbGreys )
159  {
160  *pTmp++ = aColor.GetGreen();
161  *pTmp++ = aColor.GetBlue();
162  }
163  }
164  }
165  else
166  {
167  Scanline pScanlineRead = mpReadAccess->GetScanline( nY );
168  for( long nX = 0; nX < nWidth; nX++ )
169  {
170  aColor = mpReadAccess->GetPixelFromData( pScanlineRead, nX );
171  *pTmp++ = aColor.GetRed();
172  if ( !mbGreys )
173  {
174  *pTmp++ = aColor.GetGreen();
175  *pTmp++ = aColor.GetBlue();
176  }
177  }
178  }
179 
180  pScanline = mpBuffer;
181  }
182  }
183 
184  return pScanline;
185 }
186 
187 bool JPEGWriter::Write( const Graphic& rGraphic )
188 {
189  bool bRet = false;
190 
191  if ( mxStatusIndicator.is() )
192  {
193  mxStatusIndicator->start( OUString(), 100 );
194  }
195 
196  Bitmap aGraphicBmp( rGraphic.GetBitmapEx().GetBitmap() );
197 
198  if ( mbGreys )
199  {
200  if ( !aGraphicBmp.Convert( BmpConversion::N8BitGreys ) )
201  aGraphicBmp = rGraphic.GetBitmapEx().GetBitmap();
202  }
203 
204  mpReadAccess = Bitmap::ScopedReadAccess(aGraphicBmp);
205  if( mpReadAccess )
206  {
207  if ( !mbGreys ) // bitmap was not explicitly converted into greyscale,
208  { // check if source is greyscale only
209  bool bIsGrey = true;
210 
211  long nWidth = mpReadAccess->Width();
212  for ( long nY = 0; bIsGrey && ( nY < mpReadAccess->Height() ); nY++ )
213  {
214  BitmapColor aColor;
215  Scanline pScanlineRead = mpReadAccess->GetScanline( nY );
216  for( long nX = 0; bIsGrey && ( nX < nWidth ); nX++ )
217  {
218  aColor = mpReadAccess->HasPalette() ? mpReadAccess->GetPaletteColor( mpReadAccess->GetIndexFromData( pScanlineRead, nX ) )
219  : mpReadAccess->GetPixelFromData( pScanlineRead, nX );
220  bIsGrey = ( aColor.GetRed() == aColor.GetGreen() ) && ( aColor.GetRed() == aColor.GetBlue() );
221  }
222  }
223  if ( bIsGrey )
224  mbGreys = true;
225  }
226  if( mpExpWasGrey )
228 
229  if ( mbGreys )
230  mbNative = ( mpReadAccess->GetScanlineFormat() == ScanlineFormat::N8BitPal && aGraphicBmp.HasGreyPalette());
231  else
233 
234  if( !mbNative )
236 
237  SAL_INFO("vcl", "\nJPEG Export - DPI X: " << rGraphic.GetPPI().getX() << "\nJPEG Export - DPI Y: " << rGraphic.GetPPI().getY());
238 
239  bRet = WriteJPEG( this, &mrStream, mpReadAccess->Width(),
240  mpReadAccess->Height(), rGraphic.GetPPI(), mbGreys,
242 
243  delete[] mpBuffer;
244  mpBuffer = nullptr;
245 
247  }
248  if ( mxStatusIndicator.is() )
249  mxStatusIndicator->end();
250 
251  return bRet;
252 }
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetRed() const
bool WriteJPEG(JPEGWriter *pJPEGWriter, void *pOutputStream, long nWidth, long nHeight, basegfx::B2DSize const &aPPI, bool bGreyScale, long nQualityPercent, long aChromaSubsampling, css::uno::Reference< css::task::XStatusIndicator > const &status)
Definition: jpegc.cxx:358
sal_Int32 mnQuality
Definition: JpegWriter.hxx:37
sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
Scanline GetScanline(long nY) const
JPEGWriter(SvStream &rStream, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData, bool *pExportWasGrey)
Definition: JpegWriter.cxx:112
bool * mpExpWasGrey
Definition: JpegWriter.hxx:40
double getX() const
jpeg_destination_mgr pub
Definition: JpegWriter.cxx:37
double getY() const
void * GetScanline(long nY)
Definition: JpegWriter.cxx:135
#define BUFFER_SIZE
Definition: JpegWriter.cxx:33
long Width() const
tools::SvRef< SotStorageStream > stream
sal_Int32 ReadInt32(const OUString &rKey, sal_Int32 nDefault)
static boolean empty_output_buffer(j_compress_ptr cinfo)
Definition: JpegWriter.cxx:56
bool mbNative
Definition: JpegWriter.hxx:35
sal_Int32 maChromaSubsampling
Definition: JpegWriter.hxx:38
sal_uInt8 * mpBuffer
Definition: JpegWriter.hxx:34
sal_uInt8 GetBlue() const
vcl::ScopedBitmapAccess< BitmapReadAccess, Bitmap,&Bitmap::AcquireReadAccess > ScopedReadAccess
Definition: bitmap.hxx:530
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
#define TRUE
bool HasPalette() const
ScanlineFormat GetScanlineFormat() const
static void term_destination(j_compress_ptr cinfo)
Definition: JpegWriter.cxx:71
std::size_t WriteBytes(const void *pData, std::size_t nSize)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Definition: graph.cxx:349
void jpeg_svstream_dest(j_compress_ptr cinfo, void *output)
Definition: JpegWriter.cxx:88
bool mbGreys
Definition: JpegWriter.hxx:36
basegfx::B2DSize GetPPI() const
Definition: graph.cxx:405
sal_uInt8 GetGreen() const
long Height() const
unsigned char sal_uInt8
#define SAL_INFO(area, stream)
SvStream & mrStream
Definition: JpegWriter.hxx:32
BitmapColor GetPixelFromData(const sal_uInt8 *pData, long nX) const
Bitmap::ScopedReadAccess mpReadAccess
Definition: JpegWriter.hxx:33
static void init_destination(j_compress_ptr cinfo)
Definition: JpegWriter.cxx:44
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
css::uno::Reference< css::task::XStatusIndicator > mxStatusIndicator
Definition: JpegWriter.hxx:42
bool Write(const Graphic &rGraphic)
Definition: JpegWriter.cxx:187
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, long nX) const