source: trunk/src/3rdparty/libmng/libmng_zlib.c

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 16.7 KB
Line 
1/* ************************************************************************** */
2/* * For conditions of distribution and use, * */
3/* * see copyright notice in libmng.h * */
4/* ************************************************************************** */
5/* * * */
6/* * project : libmng * */
7/* * file : libmng_zlib.c copyright (c) 2000 G.Juyn * */
8/* * version : 1.0.0 * */
9/* * * */
10/* * purpose : ZLIB library interface (implementation) * */
11/* * * */
12/* * author : G.Juyn * */
13/* * web : http://www.3-t.com * */
14/* * email : mailto:info@3-t.com * */
15/* * * */
16/* * comment : implementation of the ZLIB library interface * */
17/* * * */
18/* * changes : 0.5.1 - 05/08/2000 - G.Juyn * */
19/* * - changed strict-ANSI stuff * */
20/* * 0.5.1 - 05/11/2000 - G.Juyn * */
21/* * - filled the deflatedata routine * */
22/* * 0.5.1 - 05/12/2000 - G.Juyn * */
23/* * - changed trace to macro for callback error-reporting * */
24/* * * */
25/* * 0.5.2 - 05/20/2000 - G.Juyn * */
26/* * - fixed for JNG alpha handling * */
27/* * 0.5.2 - 05/24/2000 - G.Juyn * */
28/* * - moved init of default zlib parms from here to * */
29/* * "mng_hlapi.c" * */
30/* * * */
31/* * 0.5.3 - 06/16/2000 - G.Juyn * */
32/* * - changed progressive-display processing * */
33/* * * */
34/* * 0.9.2 - 08/05/2000 - G.Juyn * */
35/* * - changed file-prefixes * */
36/* * * */
37/* * 0.9.3 - 08/08/2000 - G.Juyn * */
38/* * - fixed compiler-warnings from Mozilla * */
39/* * 0.9.3 - 09/07/2000 - G.Juyn * */
40/* * - added support for new filter_types * */
41/* * * */
42/* ************************************************************************** */
43
44#include "libmng.h"
45#include "libmng_data.h"
46#include "libmng_error.h"
47#include "libmng_trace.h"
48#ifdef __BORLANDC__
49#pragma hdrstop
50#endif
51#include "libmng_memory.h"
52#include "libmng_pixels.h"
53#include "libmng_filter.h"
54#include "libmng_zlib.h"
55
56#if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
57#pragma option -A /* force ANSI-C */
58#endif
59
60/* ************************************************************************** */
61
62#ifdef MNG_INCLUDE_ZLIB
63
64/* ************************************************************************** */
65
66voidpf mngzlib_alloc (voidpf pData,
67 uInt iCount,
68 uInt iSize)
69{
70 voidpf pPtr; /* temporary space */
71
72#ifdef MNG_INTERNAL_MEMMNGMT
73 pPtr = calloc (iCount, iSize); /* local allocation */
74#else
75 if (((mng_datap)pData)->fMemalloc) /* callback function set ? */
76 pPtr = ((mng_datap)pData)->fMemalloc (iCount * iSize);
77 else
78 pPtr = Z_NULL; /* can't allocate! */
79#endif
80
81 return pPtr; /* return the result */
82}
83
84/* ************************************************************************** */
85
86void mngzlib_free (voidpf pData,
87 voidpf pAddress)
88{
89#ifdef MNG_INTERNAL_MEMMNGMT
90 free (pAddress); /* free locally */
91#else
92 if (((mng_datap)pData)->fMemfree) /* callback set? */
93 ((mng_datap)pData)->fMemfree (pAddress, 1);
94#endif
95}
96
97/* ************************************************************************** */
98
99mng_retcode mngzlib_initialize (mng_datap pData)
100{
101#ifdef MNG_SUPPORT_TRACE
102 MNG_TRACE (pData, MNG_FN_ZLIB_INITIALIZE, MNG_LC_START)
103#endif
104
105#ifdef MNG_INTERNAL_MEMMNGMT
106 pData->sZlib.zalloc = Z_NULL; /* let zlib figure out memory management */
107 pData->sZlib.zfree = Z_NULL;
108 pData->sZlib.opaque = Z_NULL;
109#else /* use user-provided callbacks */
110 pData->sZlib.zalloc = mngzlib_alloc;
111 pData->sZlib.zfree = mngzlib_free;
112 pData->sZlib.opaque = (voidpf)pData;
113#endif
114
115 pData->bInflating = MNG_FALSE; /* not performing any action yet */
116 pData->bDeflating = MNG_FALSE;
117
118#ifdef MNG_SUPPORT_TRACE
119 MNG_TRACE (pData, MNG_FN_ZLIB_INITIALIZE, MNG_LC_END)
120#endif
121
122 return MNG_NOERROR; /* done */
123}
124
125/* ************************************************************************** */
126
127mng_retcode mngzlib_cleanup (mng_datap pData)
128{
129#ifdef MNG_SUPPORT_TRACE
130 MNG_TRACE (pData, MNG_FN_ZLIB_CLEANUP, MNG_LC_START)
131#endif
132
133 if (pData->bInflating) /* force zlib cleanup */
134 mngzlib_inflatefree (pData);
135 if (pData->bDeflating)
136 mngzlib_deflatefree (pData);
137
138#ifdef MNG_SUPPORT_TRACE
139 MNG_TRACE (pData, MNG_FN_ZLIB_CLEANUP, MNG_LC_END)
140#endif
141
142 return MNG_NOERROR; /* done */
143}
144
145/* ************************************************************************** */
146
147mng_retcode mngzlib_inflateinit (mng_datap pData)
148{
149 int iZrslt;
150
151#ifdef MNG_SUPPORT_TRACE
152 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEINIT, MNG_LC_START)
153#endif
154 /* initialize zlib structures and such */
155 iZrslt = inflateInit (&pData->sZlib);
156
157 if (iZrslt != Z_OK) /* on error bail out */
158 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
159
160 pData->bInflating = MNG_TRUE; /* really inflating something now */
161 pData->sZlib.next_out = 0; /* force JIT initialization */
162
163#ifdef MNG_SUPPORT_TRACE
164 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEINIT, MNG_LC_END)
165#endif
166
167 return MNG_NOERROR; /* done */
168}
169
170/* ************************************************************************** */
171
172#ifdef MNG_SUPPORT_DISPLAY
173mng_retcode mngzlib_inflaterows (mng_datap pData,
174 mng_uint32 iInlen,
175 mng_uint8p pIndata)
176{
177 int iZrslt;
178 mng_retcode iRslt;
179 mng_ptr pSwap;
180
181#ifdef MNG_SUPPORT_TRACE
182 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEROWS, MNG_LC_START)
183#endif
184
185 pData->sZlib.next_in = pIndata; /* let zlib know where to get stuff */
186 pData->sZlib.avail_in = (uInt)iInlen;
187
188 if (pData->sZlib.next_out == 0) /* initialize output variables ? */
189 { /* let zlib know where to store stuff */
190 pData->sZlib.next_out = pData->pWorkrow;
191 pData->sZlib.avail_out = (uInt)(pData->iRowsize + pData->iPixelofs);
192 }
193
194 do
195 { /* now inflate a row */
196 iZrslt = inflate (&pData->sZlib, Z_SYNC_FLUSH);
197 /* produced a full row ? */
198 if (((iZrslt == Z_OK) || (iZrslt == Z_STREAM_END)) &&
199 (pData->sZlib.avail_out == 0))
200 { /* shouldn't we be at the end ? */
201 if (pData->iRow >= (mng_int32)pData->iDataheight)
202/* MNG_ERROR (pData, MNG_TOOMUCHIDAT) */ ; /* TODO: check this!!! */
203 else
204 { /* has leveling info ? */
205/* if (pData->iFilterofs)
206 iRslt = init_rowdiffering (pData);
207 else
208 iRslt = MNG_NOERROR; */
209 /* filter the row if necessary */
210/* if ((!iRslt) && (pData->iFilterofs < pData->iPixelofs ) &&
211 (*(pData->pWorkrow + pData->iFilterofs)) ) */
212 if (*(pData->pWorkrow + pData->iFilterofs))
213 iRslt = filter_a_row (pData);
214 else
215 iRslt = MNG_NOERROR;
216 /* additonal leveling/differing ? */
217 if ((!iRslt) && (pData->fDifferrow))
218 {
219 iRslt = ((mng_differrow)pData->fDifferrow) (pData);
220
221 pSwap = pData->pWorkrow;
222 pData->pWorkrow = pData->pPrevrow;
223 pData->pPrevrow = pSwap; /* make sure we're processing the right data */
224 }
225
226 if (!iRslt)
227 {
228#ifdef MNG_INCLUDE_JNG
229 if (pData->bHasJHDR) /* is JNG alpha-channel ? */
230 { /* just store in object ? */
231 if ((!iRslt) && (pData->fStorerow))
232 iRslt = ((mng_storerow)pData->fStorerow) (pData);
233 }
234 else
235#endif /* MNG_INCLUDE_JNG */
236 { /* process this row */
237 if ((!iRslt) && (pData->fProcessrow))
238 iRslt = ((mng_processrow)pData->fProcessrow) (pData);
239 /* store in object ? */
240 if ((!iRslt) && (pData->fStorerow))
241 iRslt = ((mng_storerow)pData->fStorerow) (pData);
242 /* color correction ? */
243 if ((!iRslt) && (pData->fCorrectrow))
244 iRslt = ((mng_correctrow)pData->fCorrectrow) (pData);
245 /* slap onto canvas ? */
246 if ((!iRslt) && (pData->fDisplayrow))
247 {
248 iRslt = ((mng_displayrow)pData->fDisplayrow) (pData);
249
250 if (!iRslt) /* check progressive display refresh */
251 iRslt = display_progressive_check (pData);
252
253 }
254 }
255 }
256
257 if (iRslt) /* on error bail out */
258 MNG_ERROR (pData, iRslt);
259
260 if (!pData->fDifferrow) /* swap row-pointers */
261 {
262 pSwap = pData->pWorkrow;
263 pData->pWorkrow = pData->pPrevrow;
264 pData->pPrevrow = pSwap; /* so prev points to the processed row! */
265 }
266
267 iRslt = next_row (pData); /* adjust variables for next row */
268
269 if (iRslt) /* on error bail out */
270 MNG_ERROR (pData, iRslt);
271 }
272 /* let zlib know where to store next output */
273 pData->sZlib.next_out = pData->pWorkrow;
274 pData->sZlib.avail_out = (uInt)(pData->iRowsize + pData->iPixelofs);
275 }
276 } /* until some error or EOI */
277 while ((iZrslt == Z_OK) && (pData->sZlib.avail_in > 0));
278 /* on error bail out */
279 if ((iZrslt != Z_OK) && (iZrslt != Z_STREAM_END))
280 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
281
282#ifdef MNG_SUPPORT_TRACE
283 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEROWS, MNG_LC_END)
284#endif
285
286 return MNG_NOERROR;
287}
288#endif /* MNG_SUPPORT_DISPLAY */
289
290/* ************************************************************************** */
291
292mng_retcode mngzlib_inflatedata (mng_datap pData,
293 mng_uint32 iInlen,
294 mng_uint8p pIndata)
295{
296 int iZrslt;
297
298#ifdef MNG_SUPPORT_TRACE
299 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEDATA, MNG_LC_START)
300#endif
301 /* let zlib know where to get stuff */
302 pData->sZlib.next_in = pIndata;
303 pData->sZlib.avail_in = (uInt)iInlen;
304 /* now inflate the data in one go! */
305 iZrslt = inflate (&pData->sZlib, Z_FINISH);
306 /* not enough room in output-buffer ? */
307 if ((iZrslt == Z_BUF_ERROR) || (pData->sZlib.avail_in > 0))
308 return MNG_BUFOVERFLOW;
309 /* on error bail out */
310 if ((iZrslt != Z_OK) && (iZrslt != Z_STREAM_END))
311 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
312
313#ifdef MNG_SUPPORT_TRACE
314 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEDATA, MNG_LC_END)
315#endif
316
317 return MNG_NOERROR;
318}
319
320/* ************************************************************************** */
321
322mng_retcode mngzlib_inflatefree (mng_datap pData)
323{
324 int iZrslt;
325
326#ifdef MNG_SUPPORT_TRACE
327 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEFREE, MNG_LC_START)
328#endif
329
330 pData->bInflating = MNG_FALSE; /* stopped it */
331
332 iZrslt = inflateEnd (&pData->sZlib); /* let zlib cleanup it's own stuff */
333
334 if (iZrslt != Z_OK) /* on error bail out */
335 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
336
337#ifdef MNG_SUPPORT_TRACE
338 MNG_TRACE (pData, MNG_FN_ZLIB_INFLATEFREE, MNG_LC_END)
339#endif
340
341 return MNG_NOERROR; /* done */
342}
343
344/* ************************************************************************** */
345
346mng_retcode mngzlib_deflateinit (mng_datap pData)
347{
348 int iZrslt;
349
350#ifdef MNG_SUPPORT_TRACE
351 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEINIT, MNG_LC_START)
352#endif
353 /* initialize zlib structures and such */
354 iZrslt = deflateInit2 (&pData->sZlib, pData->iZlevel, pData->iZmethod,
355 pData->iZwindowbits, pData->iZmemlevel,
356 pData->iZstrategy);
357
358 if (iZrslt != Z_OK) /* on error bail out */
359 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
360
361 pData->bDeflating = MNG_TRUE; /* really deflating something now */
362
363#ifdef MNG_SUPPORT_TRACE
364 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEINIT, MNG_LC_END)
365#endif
366
367 return MNG_NOERROR; /* done */
368}
369
370/* ************************************************************************** */
371
372mng_retcode mngzlib_deflaterows (mng_datap pData,
373 mng_uint32 iInlen,
374 mng_uint8p pIndata)
375{
376#ifdef MNG_SUPPORT_TRACE
377 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEROWS, MNG_LC_START)
378#endif
379
380
381
382
383#ifdef MNG_SUPPORT_TRACE
384 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEROWS, MNG_LC_END)
385#endif
386
387 return MNG_NOERROR;
388}
389
390/* ************************************************************************** */
391
392mng_retcode mngzlib_deflatedata (mng_datap pData,
393 mng_uint32 iInlen,
394 mng_uint8p pIndata)
395{
396 int iZrslt;
397
398#ifdef MNG_SUPPORT_TRACE
399 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEDATA, MNG_LC_START)
400#endif
401
402 pData->sZlib.next_in = pIndata; /* let zlib know where to get stuff */
403 pData->sZlib.avail_in = (uInt)iInlen;
404 /* now deflate the data in one go! */
405 iZrslt = deflate (&pData->sZlib, Z_FINISH);
406 /* not enough room in output-buffer ? */
407 if ((iZrslt == Z_BUF_ERROR) || (pData->sZlib.avail_in > 0))
408 return MNG_BUFOVERFLOW;
409 /* on error bail out */
410 if ((iZrslt != Z_OK) && (iZrslt != Z_STREAM_END))
411 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
412
413#ifdef MNG_SUPPORT_TRACE
414 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEDATA, MNG_LC_END)
415#endif
416
417 return MNG_NOERROR;
418}
419
420/* ************************************************************************** */
421
422mng_retcode mngzlib_deflatefree (mng_datap pData)
423{
424 int iZrslt;
425
426#ifdef MNG_SUPPORT_TRACE
427 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEFREE, MNG_LC_START)
428#endif
429
430 iZrslt = deflateEnd (&pData->sZlib); /* let zlib cleanup it's own stuff */
431
432 if (iZrslt != Z_OK) /* on error bail out */
433 MNG_ERRORZ (pData, (mng_uint32)iZrslt)
434
435 pData->bDeflating = MNG_FALSE; /* stopped it */
436
437#ifdef MNG_SUPPORT_TRACE
438 MNG_TRACE (pData, MNG_FN_ZLIB_DEFLATEFREE, MNG_LC_END)
439#endif
440
441 return MNG_NOERROR; /* done */
442}
443
444/* ************************************************************************** */
445
446#endif /* MNG_INCLUDE_ZLIB */
447
448/* ************************************************************************** */
449/* * end of file * */
450/* ************************************************************************** */
451
Note: See TracBrowser for help on using the repository browser.