Update ooo320-m1
[ooovba.git] / sdext / source / pdfimport / xpdfwrapper / pnghelper.cxx
blobe0574c70e7d0c7500f5cb7c1da1d220617c215d7
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * The Contents of this file are made available subject to
6 * the terms of GNU General Public License Version 2.
9 * GNU General Public License, version 2
10 * =============================================
11 * Copyright 2005 by Sun Microsystems, Inc.
12 * 901 San Antonio Road, Palo Alto, CA 94303, USA
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the Free
26 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 * Boston, MA 02110-1301, USA.
29 ************************************************************************/
31 #include "pnghelper.hxx"
33 #ifdef SYSTEM_ZLIB
34 #include "zlib.h"
35 #else
36 #include <zlib/zlib.h>
37 #endif
39 using namespace pdfi;
41 // checksum helpers, courtesy of libpng.org
43 /* Table of CRCs of all 8-bit messages. */
44 sal_uInt32 PngHelper::crc_table[256];
46 /* Flag: has the table been computed? Initially false. */
47 bool PngHelper::bCRCTableInit = true;
49 /* Make the table for a fast CRC. */
50 void PngHelper::initCRCTable()
52 for (sal_uInt32 n = 0; n < 256; n++)
54 sal_uInt32 c = n;
55 for (int k = 0; k < 8; k++)
57 if (c & 1)
58 c = 0xedb88320L ^ (c >> 1);
59 else
60 c = c >> 1;
62 crc_table[n] = c;
64 bCRCTableInit = false;
67 /* Update a running CRC with the bytes buf[0..len-1]--the CRC
68 should be initialized to all 1's, and the transmitted value
69 is the 1's complement of the final running CRC (see the
70 crc() routine below)). */
72 void PngHelper::updateCRC( sal_uInt32& io_rCRC, const sal_uInt8* i_pBuf, size_t i_nLen )
74 if( bCRCTableInit )
75 initCRCTable();
77 sal_uInt32 nCRC = io_rCRC;
78 for( size_t n = 0; n < i_nLen; n++ )
79 nCRC = crc_table[(nCRC ^ i_pBuf[n]) & 0xff] ^ (nCRC >> 8);
80 io_rCRC = nCRC;
83 sal_uInt32 PngHelper::getCRC( const sal_uInt8* i_pBuf, size_t i_nLen )
85 sal_uInt32 nCRC = 0xffffffff;
86 updateCRC( nCRC, i_pBuf, i_nLen );
87 return nCRC ^ 0xffffffff;
90 sal_uInt32 PngHelper::deflateBuffer( const Output_t* i_pBuf, size_t i_nLen, OutputBuffer& o_rOut )
92 size_t nOrigSize = o_rOut.size();
94 // prepare z stream
95 z_stream aStream;
96 aStream.zalloc = Z_NULL;
97 aStream.zfree = Z_NULL;
98 aStream.opaque = Z_NULL;
99 deflateInit( &aStream, Z_BEST_COMPRESSION );
100 aStream.avail_in = uInt(i_nLen);
101 aStream.next_in = (Bytef*)i_pBuf;
103 sal_uInt8 aOutBuf[ 32768 ];
106 aStream.avail_out = sizeof( aOutBuf );
107 aStream.next_out = aOutBuf;
109 if( deflate( &aStream, Z_FINISH ) == Z_STREAM_ERROR )
111 deflateEnd( &aStream );
112 // scrao the data of this broken stream
113 o_rOut.resize( nOrigSize );
114 return 0;
117 // append compressed bytes
118 sal_uInt32 nCompressedBytes = sizeof( aOutBuf ) - aStream.avail_out;
119 if( nCompressedBytes )
120 o_rOut.insert( o_rOut.end(), aOutBuf, aOutBuf+nCompressedBytes );
122 } while( aStream.avail_out == 0 );
124 // cleanup
125 deflateEnd( &aStream );
127 return sal_uInt32( o_rOut.size() - nOrigSize );
130 void PngHelper::appendFileHeader( OutputBuffer& o_rOutputBuf )
132 static const Output_t aHeader[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
134 o_rOutputBuf.insert( o_rOutputBuf.end(), aHeader, aHeader + sizeof(aHeader)/sizeof(aHeader[0]) );
137 size_t PngHelper::startChunk( const char* pChunkName, OutputBuffer& o_rOutputBuf )
139 size_t nIndex = sal_uInt32( o_rOutputBuf.size() );
140 o_rOutputBuf.insert( o_rOutputBuf.end(), 4, (Output_t)0 );
141 o_rOutputBuf.push_back( pChunkName[0] );
142 o_rOutputBuf.push_back( pChunkName[1] );
143 o_rOutputBuf.push_back( pChunkName[2] );
144 o_rOutputBuf.push_back( pChunkName[3] );
145 return nIndex;
148 void PngHelper::set( sal_uInt32 i_nValue, OutputBuffer& o_rOutputBuf, size_t i_nIndex )
150 o_rOutputBuf[ i_nIndex ] = (i_nValue & 0xff000000) >> 24;
151 o_rOutputBuf[ i_nIndex+1 ] = (i_nValue & 0x00ff0000) >> 16;
152 o_rOutputBuf[ i_nIndex+2 ] = (i_nValue & 0x0000ff00) >> 8;
153 o_rOutputBuf[ i_nIndex+3 ] = (i_nValue & 0x000000ff);
156 void PngHelper::endChunk( size_t nStart, OutputBuffer& o_rOutputBuf )
158 if( nStart+8 > o_rOutputBuf.size() )
159 return; // something broken is going on
161 // update chunk length
162 size_t nLen = o_rOutputBuf.size() - nStart;
163 sal_uInt32 nDataLen = sal_uInt32(nLen)-8;
164 set( nDataLen, o_rOutputBuf, nStart );
166 // append chunk crc
167 sal_uInt32 nChunkCRC = getCRC( (sal_uInt8*)&o_rOutputBuf[nStart+4], nLen-4 );
168 append( nChunkCRC, o_rOutputBuf );
171 void PngHelper::appendIHDR( OutputBuffer& o_rOutputBuf, int width, int height, int depth, int colortype )
173 size_t nStart = startChunk( "IHDR", o_rOutputBuf );
174 append( width, o_rOutputBuf );
175 append( height, o_rOutputBuf );
176 o_rOutputBuf.push_back( Output_t(depth) );
177 o_rOutputBuf.push_back( Output_t(colortype) );
178 o_rOutputBuf.push_back( 0 ); // compression method deflate
179 o_rOutputBuf.push_back( 0 ); // filtering method 0 (default)
180 o_rOutputBuf.push_back( 0 ); // no interlacing
181 endChunk( nStart, o_rOutputBuf );
184 void PngHelper::appendIEND( OutputBuffer& o_rOutputBuf )
186 size_t nStart = startChunk( "IEND", o_rOutputBuf );
187 endChunk( nStart, o_rOutputBuf );
190 void PngHelper::createPng( OutputBuffer& o_rOutputBuf,
191 Stream* str,
192 int width,
193 int height,
194 GfxRGB& zeroColor,
195 GfxRGB& oneColor,
196 bool bIsMask
199 appendFileHeader( o_rOutputBuf );
200 appendIHDR( o_rOutputBuf, width, height, 1, 3 );
202 // write palette
203 size_t nIdx = startChunk( "PLTE", o_rOutputBuf );
204 // write colors 0 and 1
205 o_rOutputBuf.push_back(colToByte(zeroColor.r));
206 o_rOutputBuf.push_back(colToByte(zeroColor.g));
207 o_rOutputBuf.push_back(colToByte(zeroColor.b));
208 o_rOutputBuf.push_back(colToByte(oneColor.r));
209 o_rOutputBuf.push_back(colToByte(oneColor.g));
210 o_rOutputBuf.push_back(colToByte(oneColor.b));
211 // end PLTE chunk
212 endChunk( nIdx, o_rOutputBuf );
214 if( bIsMask )
216 // write tRNS chunk
217 nIdx = startChunk( "tRNS", o_rOutputBuf );
218 o_rOutputBuf.push_back( 0xff );
219 o_rOutputBuf.push_back( 0 );
220 // end tRNS chunk
221 endChunk( nIdx, o_rOutputBuf );
224 // create scan line data buffer
225 OutputBuffer aScanlines;
226 int nLineSize = (width + 7)/8;
227 aScanlines.reserve( nLineSize * height + height );
229 str->reset();
230 for( int y = 0; y < height; y++ )
232 // determine filter type (none) for this scanline
233 aScanlines.push_back( 0 );
234 for( int x = 0; x < nLineSize; x++ )
235 aScanlines.push_back( str->getChar() );
238 // begin IDAT chunk for scanline data
239 nIdx = startChunk( "IDAT", o_rOutputBuf );
240 // compress scanlines
241 deflateBuffer( &aScanlines[0], aScanlines.size(), o_rOutputBuf );
242 // end IDAT chunk
243 endChunk( nIdx, o_rOutputBuf );
245 // output IEND
246 appendIEND( o_rOutputBuf );
249 void PngHelper::createPng( OutputBuffer& o_rOutputBuf,
250 Stream* str,
251 int width, int height, GfxImageColorMap* colorMap,
252 Stream* maskStr,
253 int maskWidth, int maskHeight, GfxImageColorMap* maskColorMap )
255 appendFileHeader( o_rOutputBuf );
256 appendIHDR( o_rOutputBuf, width, height, 8, 6 ); // RGBA image
258 // initialize stream
259 Guchar *p, *pm;
260 GfxRGB rgb;
261 GfxGray alpha;
262 ImageStream* imgStr =
263 new ImageStream(str,
264 width,
265 colorMap->getNumPixelComps(),
266 colorMap->getBits());
267 imgStr->reset();
269 // create scan line data buffer
270 OutputBuffer aScanlines;
271 aScanlines.reserve( width*height*4 + height );
273 for( int y=0; y<height; ++y)
275 aScanlines.push_back( 0 );
276 p = imgStr->getLine();
277 for( int x=0; x<width; ++x)
279 colorMap->getRGB(p, &rgb);
280 aScanlines.push_back(colToByte(rgb.r));
281 aScanlines.push_back(colToByte(rgb.g));
282 aScanlines.push_back(colToByte(rgb.b));
283 aScanlines.push_back( 0xff );
285 p +=colorMap->getNumPixelComps();
290 // now fill in the mask data
292 // CAUTION: originally this was done in one single loop
293 // it caused merry chaos; the reason is that maskStr and str are
294 // not independent streams, it happens that reading one advances
295 // the other, too. Hence the two passes are imperative !
297 // initialize mask stream
298 ImageStream* imgStrMask =
299 new ImageStream(maskStr,
300 maskWidth,
301 maskColorMap->getNumPixelComps(),
302 maskColorMap->getBits());
304 imgStrMask->reset();
305 for( int y = 0; y < maskHeight; ++y )
307 pm = imgStrMask->getLine();
308 for( int x = 0; x < maskWidth; ++x )
310 maskColorMap->getGray(pm,&alpha);
311 pm += maskColorMap->getNumPixelComps();
312 int nIndex = (y*height/maskHeight) * (width*4+1) + // mapped line
313 (x*width/maskWidth)*4 + 1 + 3 // mapped column
315 aScanlines[ nIndex ] = colToByte(alpha);
319 delete imgStr;
320 delete imgStrMask;
322 // begind IDAT chunk for scanline data
323 size_t nIdx = startChunk( "IDAT", o_rOutputBuf );
324 // compress scanlines
325 deflateBuffer( &aScanlines[0], aScanlines.size(), o_rOutputBuf );
326 // end IDAT chunk
327 endChunk( nIdx, o_rOutputBuf );
328 // output IEND
329 appendIEND( o_rOutputBuf );
332 // one bit mask; 0 bits opaque
333 void PngHelper::createPng( OutputBuffer& o_rOutputBuf,
334 Stream* str,
335 int width, int height, GfxImageColorMap* colorMap,
336 Stream* maskStr,
337 int maskWidth, int maskHeight,
338 bool maskInvert
341 appendFileHeader( o_rOutputBuf );
342 appendIHDR( o_rOutputBuf, width, height, 8, 6 ); // RGBA image
344 // initialize stream
345 Guchar *p;
346 GfxRGB rgb;
347 ImageStream* imgStr =
348 new ImageStream(str,
349 width,
350 colorMap->getNumPixelComps(),
351 colorMap->getBits());
352 imgStr->reset();
354 // create scan line data buffer
355 OutputBuffer aScanlines;
356 aScanlines.reserve( width*height*4 + height );
358 for( int y=0; y<height; ++y)
360 aScanlines.push_back( 0 );
361 p = imgStr->getLine();
362 for( int x=0; x<width; ++x)
364 colorMap->getRGB(p, &rgb);
365 aScanlines.push_back(colToByte(rgb.r));
366 aScanlines.push_back(colToByte(rgb.g));
367 aScanlines.push_back(colToByte(rgb.b));
368 aScanlines.push_back( 0xff );
370 p +=colorMap->getNumPixelComps();
375 // now fill in the mask data
377 // CAUTION: originally this was done in one single loop
378 // it caused merry chaos; the reason is that maskStr and str are
379 // not independent streams, it happens that reading one advances
380 // the other, too. Hence the two passes are imperative !
382 // initialize mask stream
383 ImageStream* imgStrMask =
384 new ImageStream(maskStr, maskWidth, 1, 1);
386 imgStrMask->reset();
387 for( int y = 0; y < maskHeight; ++y )
389 for( int x = 0; x < maskWidth; ++x )
391 Guchar aPixel = 0;
392 imgStrMask->getPixel( &aPixel );
393 int nIndex = (y*height/maskHeight) * (width*4+1) + // mapped line
394 (x*width/maskWidth)*4 + 1 + 3 // mapped column
396 if( maskInvert )
397 aScanlines[ nIndex ] = aPixel ? 0xff : 0x00;
398 else
399 aScanlines[ nIndex ] = aPixel ? 0x00 : 0xff;
403 delete imgStr;
404 delete imgStrMask;
406 // begind IDAT chunk for scanline data
407 size_t nIdx = startChunk( "IDAT", o_rOutputBuf );
408 // compress scanlines
409 deflateBuffer( &aScanlines[0], aScanlines.size(), o_rOutputBuf );
410 // end IDAT chunk
411 endChunk( nIdx, o_rOutputBuf );
412 // output IEND
413 appendIEND( o_rOutputBuf );