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"
36 #include <zlib/zlib.h>
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
++)
55 for (int k
= 0; k
< 8; k
++)
58 c
= 0xedb88320L
^ (c
>> 1);
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
)
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);
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();
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
);
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 );
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] );
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
);
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
,
199 appendFileHeader( o_rOutputBuf
);
200 appendIHDR( o_rOutputBuf
, width
, height
, 1, 3 );
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
));
212 endChunk( nIdx
, o_rOutputBuf
);
217 nIdx
= startChunk( "tRNS", o_rOutputBuf
);
218 o_rOutputBuf
.push_back( 0xff );
219 o_rOutputBuf
.push_back( 0 );
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
);
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
);
243 endChunk( nIdx
, o_rOutputBuf
);
246 appendIEND( o_rOutputBuf
);
249 void PngHelper::createPng( OutputBuffer
& o_rOutputBuf
,
251 int width
, int height
, GfxImageColorMap
* colorMap
,
253 int maskWidth
, int maskHeight
, GfxImageColorMap
* maskColorMap
)
255 appendFileHeader( o_rOutputBuf
);
256 appendIHDR( o_rOutputBuf
, width
, height
, 8, 6 ); // RGBA image
262 ImageStream
* imgStr
=
265 colorMap
->getNumPixelComps(),
266 colorMap
->getBits());
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
,
301 maskColorMap
->getNumPixelComps(),
302 maskColorMap
->getBits());
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
);
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
);
327 endChunk( nIdx
, o_rOutputBuf
);
329 appendIEND( o_rOutputBuf
);
332 // one bit mask; 0 bits opaque
333 void PngHelper::createPng( OutputBuffer
& o_rOutputBuf
,
335 int width
, int height
, GfxImageColorMap
* colorMap
,
337 int maskWidth
, int maskHeight
,
341 appendFileHeader( o_rOutputBuf
);
342 appendIHDR( o_rOutputBuf
, width
, height
, 8, 6 ); // RGBA image
347 ImageStream
* imgStr
=
350 colorMap
->getNumPixelComps(),
351 colorMap
->getBits());
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);
387 for( int y
= 0; y
< maskHeight
; ++y
)
389 for( int x
= 0; x
< maskWidth
; ++x
)
392 imgStrMask
->getPixel( &aPixel
);
393 int nIndex
= (y
*height
/maskHeight
) * (width
*4+1) + // mapped line
394 (x
*width
/maskWidth
)*4 + 1 + 3 // mapped column
397 aScanlines
[ nIndex
] = aPixel
? 0xff : 0x00;
399 aScanlines
[ nIndex
] = aPixel
? 0x00 : 0xff;
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
);
411 endChunk( nIdx
, o_rOutputBuf
);
413 appendIEND( o_rOutputBuf
);