File: libmng_jpeg.c

package info (click to toggle)
libmng 1.0.10+dfsg-3.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 6,472 kB
  • ctags: 7,999
  • sloc: ansic: 78,361; pascal: 3,559; cpp: 2,624; sh: 937; makefile: 158
file content (1088 lines) | stat: -rw-r--r-- 44,453 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
/* ************************************************************************** */
/* *             For conditions of distribution and use,                    * */
/* *                see copyright notice in libmng.h                        * */
/* ************************************************************************** */
/* *                                                                        * */
/* * project   : libmng                                                     * */
/* * file      : libmng_jpeg.c             copyright (c) 2000-2004 G.Juyn   * */
/* * version   : 1.0.9                                                      * */
/* *                                                                        * */
/* * purpose   : JPEG library interface (implementation)                    * */
/* *                                                                        * */
/* * author    : G.Juyn                                                     * */
/* *                                                                        * */
/* * comment   : implementation of the JPEG library interface               * */
/* *                                                                        * */
/* * changes   : 0.5.1 - 05/08/2000 - G.Juyn                                * */
/* *             - changed strict-ANSI stuff                                * */
/* *                                                                        * */
/* *             0.5.2 - 05/22/2000 - G.Juyn                                * */
/* *             - implemented all the JNG routines                         * */
/* *                                                                        * */
/* *             0.5.3 - 06/17/2000 - G.Juyn                                * */
/* *             - added tracing of JPEG calls                              * */
/* *             0.5.3 - 06/24/2000 - G.Juyn                                * */
/* *             - fixed inclusion of IJG read/write code                   * */
/* *             0.5.3 - 06/29/2000 - G.Juyn                                * */
/* *             - fixed some 64-bit warnings                               * */
/* *                                                                        * */
/* *             0.9.2 - 08/05/2000 - G.Juyn                                * */
/* *             - changed file-prefixes                                    * */
/* *                                                                        * */
/* *             0.9.3 - 10/16/2000 - G.Juyn                                * */
/* *             - added support for JDAA                                   * */
/* *                                                                        * */
/* *             1.0.1 - 04/19/2001 - G.Juyn                                * */
/* *             - added export of JPEG functions for DLL                   * */
/* *             1.0.1 - 04/22/2001 - G.Juyn                                * */
/* *             - fixed memory-leaks (Thanks Gregg!)                       * */
/* *                                                                        * */
/* *             1.0.4 - 06/22/2002 - G.Juyn                                * */
/* *             - B526138 - returned IJGSRC6B calling convention to        * */
/* *               default for MSVC                                         * */
/* *                                                                        * */
/* *             1.0.5 - 24/02/2003 - G.Juyn                                * */
/* *             - B683152 - libjpeg suspension not always honored correctly* */
/* *                                                                        * */
/* *             1.0.6 - 03/04/2003 - G.Juyn                                * */
/* *             - fixed some compiler-warnings                             * */
/* *                                                                        * */
/* *             1.0.8 - 08/01/2004 - G.Juyn                                * */
/* *             - added support for 3+byte pixelsize for JPEG's            * */
/* *                                                                        * */
/* *             1.0.9 - 12/20/2004 - G.Juyn                                * */
/* *             - cleaned up macro-invocations (thanks to D. Airlie)       * */
/* *                                                                        * */
/* ************************************************************************** */

#include "libmng.h"
#include "libmng_data.h"
#include "libmng_error.h"
#include "libmng_trace.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "libmng_memory.h"
#include "libmng_pixels.h"
#include "libmng_jpeg.h"

#if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
#pragma option -A                      /* force ANSI-C */
#endif

/* ************************************************************************** */

#if defined(MNG_INCLUDE_JNG) && defined(MNG_INCLUDE_DISPLAY_PROCS)

/* ************************************************************************** */
/* *                                                                        * */
/* * Local IJG callback routines (source-manager, error-manager and such)   * */
/* *                                                                        * */
/* ************************************************************************** */

#ifdef MNG_INCLUDE_IJG6B

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
#ifdef MNG_DEFINE_JPEG_STDCALL
void MNG_DECL mng_init_source (j_decompress_ptr cinfo)
#else
void mng_init_source (j_decompress_ptr cinfo)
#endif
{
  return;                              /* nothing needed */
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
#ifdef MNG_DEFINE_JPEG_STDCALL
boolean MNG_DECL mng_fill_input_buffer (j_decompress_ptr cinfo)
#else
boolean mng_fill_input_buffer (j_decompress_ptr cinfo)
#endif
{
  return FALSE;                        /* force IJG routine to return to caller */
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
#ifdef MNG_DEFINE_JPEG_STDCALL
void MNG_DECL mng_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
#else
void mng_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
#endif
{
  if (num_bytes > 0)                   /* ignore fony calls */
  {                                    /* address my generic structure */
    mng_datap pData = (mng_datap)cinfo->client_data;
                                       /* address source manager */
    mngjpeg_sourcep pSrc = pData->pJPEGdinfo->src;
                                       /* problem scenario ? */
    if (pSrc->bytes_in_buffer < (size_t)num_bytes)
    {                                  /* tell the boss we need to skip some data! */
      pData->iJPEGtoskip = (mng_uint32)((size_t)num_bytes - pSrc->bytes_in_buffer);

      pSrc->bytes_in_buffer = 0;       /* let the JPEG lib suspend */
      pSrc->next_input_byte = MNG_NULL;
    }
    else
    {                                  /* simply advance in the buffer */
      pSrc->bytes_in_buffer -= num_bytes;
      pSrc->next_input_byte += num_bytes;
    }
  }

  return;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
#ifdef MNG_DEFINE_JPEG_STDCALL
void MNG_DECL mng_skip_input_data2 (j_decompress_ptr cinfo, long num_bytes)
#else
void mng_skip_input_data2 (j_decompress_ptr cinfo, long num_bytes)
#endif
{
  if (num_bytes > 0)                   /* ignore fony calls */
  {                                    /* address my generic structure */
    mng_datap pData = (mng_datap)cinfo->client_data;
                                       /* address source manager */
    mngjpeg_sourcep pSrc = pData->pJPEGdinfo2->src;
                                       /* problem scenario ? */
    if (pSrc->bytes_in_buffer < (size_t)num_bytes)
    {                                  /* tell the boss we need to skip some data! */
      pData->iJPEGtoskip2 = (mng_uint32)((size_t)num_bytes - pSrc->bytes_in_buffer);

      pSrc->bytes_in_buffer = 0;       /* let the JPEG lib suspend */
      pSrc->next_input_byte = MNG_NULL;
    }
    else
    {                                  /* simply advance in the buffer */
      pSrc->bytes_in_buffer -= num_bytes;
      pSrc->next_input_byte += num_bytes;
    }
  }

  return;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
#ifdef MNG_DEFINE_JPEG_STDCALL
void MNG_DECL mng_term_source (j_decompress_ptr cinfo)
#else
void mng_term_source (j_decompress_ptr cinfo)
#endif
{
  return;                              /* nothing needed */
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_USE_SETJMP
#ifdef MNG_DEFINE_JPEG_STDCALL
void MNG_DECL mng_error_exit (j_common_ptr cinfo)
#else
void mng_error_exit (j_common_ptr cinfo)
#endif
{                                      /* address my generic structure */
  mng_datap pData = (mng_datap)cinfo->client_data;

#ifdef MNG_ERROR_TELLTALE              /* fill the message text ??? */
  (*cinfo->err->output_message) (cinfo);
#endif
                                       /* return to the point of no return... */
  longjmp (pData->sErrorbuf, cinfo->err->msg_code);
}
#endif /* MNG_USE_SETJMP */

/* ************************************************************************** */

#ifdef MNG_USE_SETJMP
#ifdef MNG_DEFINE_JPEG_STDCALL
void MNG_DECL mng_output_message (j_common_ptr cinfo)
#else
void mng_output_message (j_common_ptr cinfo)
#endif
{
  return;                              /* just do nothing ! */
}
#endif /* MNG_USE_SETJMP */

/* ************************************************************************** */

#endif /* MNG_INCLUDE_IJG6B */

/* ************************************************************************** */
/* *                                                                        * */
/* * Global JPEG routines                                                   * */
/* *                                                                        * */
/* ************************************************************************** */

mng_retcode mngjpeg_initialize (mng_datap pData)
{
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_INITIALIZE, MNG_LC_START);
#endif
                                       /* allocate space for JPEG structures if necessary */
#ifdef MNG_INCLUDE_JNG_READ
  if (pData->pJPEGderr   == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGderr,   sizeof (mngjpeg_error ));
  if (pData->pJPEGdsrc   == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGdsrc,   sizeof (mngjpeg_source));
  if (pData->pJPEGdinfo  == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGdinfo,  sizeof (mngjpeg_decomp));
                                       /* enable reverse addressing */
  pData->pJPEGdinfo->client_data  = pData;

  if (pData->pJPEGderr2  == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGderr2,  sizeof (mngjpeg_error ));
  if (pData->pJPEGdsrc2  == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGdsrc2,  sizeof (mngjpeg_source));
  if (pData->pJPEGdinfo2 == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGdinfo2, sizeof (mngjpeg_decomp));
                                       /* enable reverse addressing */
  pData->pJPEGdinfo2->client_data = pData;
#endif

#ifdef MNG_INCLUDE_JNG_WRITE
  if (pData->pJPEGcerr  == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGcerr,  sizeof (mngjpeg_error ));
  if (pData->pJPEGcinfo == MNG_NULL)
    MNG_ALLOC (pData, pData->pJPEGcinfo, sizeof (mngjpeg_comp  ));
                                       /* enable reverse addressing */
  pData->pJPEGcinfo->client_data = pData;
#endif

  if (pData->pJPEGbuf   == MNG_NULL)   /* initialize temporary buffers */
  {
    pData->iJPEGbufmax     = MNG_JPEG_MAXBUF;
    MNG_ALLOC (pData, pData->pJPEGbuf, pData->iJPEGbufmax);
  }

  if (pData->pJPEGbuf2  == MNG_NULL) 
  {
    pData->iJPEGbufmax2    = MNG_JPEG_MAXBUF;
    MNG_ALLOC (pData, pData->pJPEGbuf2, pData->iJPEGbufmax2);
  }

  pData->pJPEGcurrent      = pData->pJPEGbuf;
  pData->iJPEGbufremain    = 0;
  pData->pJPEGrow          = MNG_NULL;
  pData->iJPEGrowlen       = 0;
  pData->iJPEGtoskip       = 0;

  pData->pJPEGcurrent2     = pData->pJPEGbuf2;
  pData->iJPEGbufremain2   = 0;
  pData->pJPEGrow2         = MNG_NULL;
  pData->iJPEGrowlen2      = 0;
  pData->iJPEGtoskip2      = 0;
                                      /* not doing anything yet ! */
  pData->bJPEGcompress     = MNG_FALSE;
  
  pData->bJPEGdecompress   = MNG_FALSE;
  pData->bJPEGhasheader    = MNG_FALSE;
  pData->bJPEGdecostarted  = MNG_FALSE;
  pData->bJPEGscanstarted  = MNG_FALSE;
  pData->bJPEGscanending   = MNG_FALSE;

  pData->bJPEGdecompress2  = MNG_FALSE;
  pData->bJPEGhasheader2   = MNG_FALSE;
  pData->bJPEGdecostarted2 = MNG_FALSE;
  pData->bJPEGscanstarted2 = MNG_FALSE;

  pData->iJPEGrow          = 0;        /* zero input/output lines */
  pData->iJPEGalpharow     = 0;
  pData->iJPEGrgbrow       = 0;
  pData->iJPEGdisprow      = 0;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_INITIALIZE, MNG_LC_END);
#endif

  return MNG_NOERROR;
}

/* ************************************************************************** */

mng_retcode mngjpeg_cleanup (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  mng_retcode iRetcode;
#endif

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_CLEANUP, MNG_LC_START);
#endif

#ifdef MNG_INCLUDE_IJG6B
#ifdef MNG_USE_SETJMP
  iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif

#ifdef MNG_INCLUDE_JNG_READ            /* still decompressing something ? */
  if (pData->bJPEGdecompress)
    jpeg_destroy_decompress (pData->pJPEGdinfo);
  if (pData->bJPEGdecompress2)
    jpeg_destroy_decompress (pData->pJPEGdinfo2);
#endif

#ifdef MNG_INCLUDE_JNG_WRITE
  if (pData->bJPEGcompress)            /* still compressing something ? */
    jpeg_destroy_compress (pData->pJPEGcinfo);
#endif

#endif /* MNG_INCLUDE_IJG6B */
                                       /* cleanup temporary buffers */
  MNG_FREE (pData, pData->pJPEGbuf2, pData->iJPEGbufmax2);
  MNG_FREE (pData, pData->pJPEGbuf,  pData->iJPEGbufmax);
                                       /* cleanup space for JPEG structures */
#ifdef MNG_INCLUDE_JNG_WRITE
  MNG_FREE (pData, pData->pJPEGcinfo,  sizeof (mngjpeg_comp  ));
  MNG_FREE (pData, pData->pJPEGcerr,   sizeof (mngjpeg_error ));
#endif

#ifdef MNG_INCLUDE_JNG_READ
  MNG_FREE (pData, pData->pJPEGdinfo,  sizeof (mngjpeg_decomp));
  MNG_FREE (pData, pData->pJPEGdsrc,   sizeof (mngjpeg_source));
  MNG_FREE (pData, pData->pJPEGderr,   sizeof (mngjpeg_error ));
  MNG_FREE (pData, pData->pJPEGdinfo2, sizeof (mngjpeg_decomp));
  MNG_FREE (pData, pData->pJPEGdsrc2,  sizeof (mngjpeg_source));
  MNG_FREE (pData, pData->pJPEGderr2,  sizeof (mngjpeg_error ));
#endif

  MNG_FREE (pData, pData->pJPEGrow2, pData->iJPEGrowlen2);
  MNG_FREE (pData, pData->pJPEGrow,  pData->iJPEGrowlen);
                                       /* whatever we were doing ... */
                                       /* we don't anymore ... */
  pData->bJPEGcompress     = MNG_FALSE;

  pData->bJPEGdecompress   = MNG_FALSE;
  pData->bJPEGhasheader    = MNG_FALSE;
  pData->bJPEGdecostarted  = MNG_FALSE;
  pData->bJPEGscanstarted  = MNG_FALSE;
  pData->bJPEGscanending   = MNG_FALSE;

  pData->bJPEGdecompress2  = MNG_FALSE;
  pData->bJPEGhasheader2   = MNG_FALSE;
  pData->bJPEGdecostarted2 = MNG_FALSE;
  pData->bJPEGscanstarted2 = MNG_FALSE;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_CLEANUP, MNG_LC_END);
#endif

  return MNG_NOERROR;
}

/* ************************************************************************** */
/* *                                                                        * */
/* * JPEG decompression routines (JDAT)                                     * */
/* *                                                                        * */
/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressinit (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  mng_retcode iRetcode;
#endif

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_START);
#endif

#ifdef MNG_INCLUDE_IJG6B
  /* allocate and initialize a JPEG decompression object */
  pData->pJPEGdinfo->err = jpeg_std_error (pData->pJPEGderr);

#ifdef MNG_USE_SETJMP                  /* setup local JPEG error-routines */
  pData->pJPEGderr->error_exit     = mng_error_exit;
  pData->pJPEGderr->output_message = mng_output_message;

  iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif /* MNG_USE_SETJMP */

  /* allocate and initialize a JPEG decompression object (continued) */
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_JPEG_CREATE_DECOMPRESS)
#endif
  jpeg_create_decompress (pData->pJPEGdinfo);

  pData->bJPEGdecompress = MNG_TRUE;   /* indicate it's initialized */

  /* specify the source of the compressed data (eg, a file) */
                                       /* no, not a file; we have buffered input */
  pData->pJPEGdinfo->src = pData->pJPEGdsrc;
                                       /* use the default handler */
  pData->pJPEGdinfo->src->resync_to_restart = jpeg_resync_to_restart;
                                       /* setup local source routine & parms */
  pData->pJPEGdinfo->src->init_source       = mng_init_source;
  pData->pJPEGdinfo->src->fill_input_buffer = mng_fill_input_buffer;
  pData->pJPEGdinfo->src->skip_input_data   = mng_skip_input_data;
  pData->pJPEGdinfo->src->term_source       = mng_term_source;
  pData->pJPEGdinfo->src->next_input_byte   = pData->pJPEGcurrent;
  pData->pJPEGdinfo->src->bytes_in_buffer   = pData->iJPEGbufremain;

#endif /* MNG_INCLUDE_IJG6B */

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_END);
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressdata (mng_datap  pData,
                                    mng_uint32 iRawsize,
                                    mng_uint8p pRawdata)
{
  mng_retcode iRetcode;
  mng_uint32  iRemain;
  mng_uint8p  pWork;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_START);
#endif

#if defined (MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  iRetcode = setjmp (pData->sErrorbuf);/* initialize local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif

  pWork   = pRawdata;
  iRemain = iRawsize;

  if (pData->iJPEGtoskip)              /* JPEG-lib told us to skip some more data ? */
  {
    if (iRemain > pData->iJPEGtoskip)  /* enough data in this buffer ? */
    {
      iRemain -= pData->iJPEGtoskip;   /* skip enough to access the next byte */
      pWork   += pData->iJPEGtoskip;

      pData->iJPEGtoskip = 0;          /* no more to skip then */
    }
    else
    {
      pData->iJPEGtoskip -= iRemain;   /* skip all data in the buffer */
      iRemain = 0;                     /* and indicate this accordingly */
    }
                                       /* the skip set current-pointer to NULL ! */
    pData->pJPEGcurrent = pData->pJPEGbuf;
  }

  while (iRemain)                      /* repeat until no more input-bytes */
  {                                    /* need to shift anything ? */
    if ((pData->pJPEGcurrent > pData->pJPEGbuf) &&
        (pData->pJPEGcurrent - pData->pJPEGbuf + pData->iJPEGbufremain + iRemain > pData->iJPEGbufmax))
    {
      if (pData->iJPEGbufremain > 0)   /* then do so */
        MNG_COPY (pData->pJPEGbuf, pData->pJPEGcurrent, pData->iJPEGbufremain);

      pData->pJPEGcurrent = pData->pJPEGbuf;
    }
                                       /* does the remaining input fit into the buffer ? */
    if (pData->iJPEGbufremain + iRemain <= pData->iJPEGbufmax)
    {                                  /* move the lot */
      MNG_COPY ((pData->pJPEGcurrent + pData->iJPEGbufremain), pWork, iRemain);

      pData->iJPEGbufremain += iRemain;/* adjust remaining_bytes counter */
      iRemain = 0;                     /* and indicate there's no input left */
    }
    else
    {                                  /* calculate what does fit */
      mng_uint32 iFits = pData->iJPEGbufmax - pData->iJPEGbufremain;

      if (iFits <= 0)                  /* no space is just bugger 'm all */
        MNG_ERROR (pData, MNG_JPEGBUFTOOSMALL);
                                       /* move that */
      MNG_COPY ((pData->pJPEGcurrent + pData->iJPEGbufremain), pWork, iFits);

      pData->iJPEGbufremain += iFits;  /* adjust remain_bytes counter */
      iRemain -= iFits;                /* and the input-parms */
      pWork   += iFits;
    }

#ifdef MNG_INCLUDE_IJG6B
    pData->pJPEGdinfo->src->next_input_byte = pData->pJPEGcurrent;
    pData->pJPEGdinfo->src->bytes_in_buffer = pData->iJPEGbufremain;

    if (!pData->bJPEGhasheader)        /* haven't got the header yet ? */
    {
      /* call jpeg_read_header() to obtain image info */
#ifdef MNG_SUPPORT_TRACE
      MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_READ_HEADER)
#endif
      if (jpeg_read_header (pData->pJPEGdinfo, TRUE) != JPEG_SUSPENDED)
      {                                /* indicate the header's oke */
        pData->bJPEGhasheader = MNG_TRUE;
                                       /* let's do some sanity checks ! */
        if ((pData->pJPEGdinfo->image_width  != pData->iDatawidth ) ||
            (pData->pJPEGdinfo->image_height != pData->iDataheight)    )
          MNG_ERROR (pData, MNG_JPEGPARMSERR);

        if ( ((pData->iJHDRcolortype == MNG_COLORTYPE_JPEGGRAY ) ||
              (pData->iJHDRcolortype == MNG_COLORTYPE_JPEGGRAYA)    ) &&
             (pData->pJPEGdinfo->jpeg_color_space != JCS_GRAYSCALE  )    )
          MNG_ERROR (pData, MNG_JPEGPARMSERR);

        if ( ((pData->iJHDRcolortype == MNG_COLORTYPE_JPEGCOLOR ) ||
              (pData->iJHDRcolortype == MNG_COLORTYPE_JPEGCOLORA)    ) &&
             (pData->pJPEGdinfo->jpeg_color_space != JCS_YCbCr       )    )
          MNG_ERROR (pData, MNG_JPEGPARMSERR);
                                       /* indicate whether or not it's progressive */
        pData->bJPEGprogressive = (mng_bool)jpeg_has_multiple_scans (pData->pJPEGdinfo);
                                       /* progressive+alpha can't display "on-the-fly"!! */
        if ((pData->bJPEGprogressive) &&
            ((pData->iJHDRcolortype == MNG_COLORTYPE_JPEGGRAYA ) ||
             (pData->iJHDRcolortype == MNG_COLORTYPE_JPEGCOLORA)    ))
          pData->fDisplayrow = MNG_NULL;
                                       /* allocate a row of JPEG-samples */
        if (pData->pJPEGdinfo->jpeg_color_space == JCS_YCbCr)
          pData->iJPEGrowlen = pData->pJPEGdinfo->image_width * RGB_PIXELSIZE;
        else
          pData->iJPEGrowlen = pData->pJPEGdinfo->image_width;

        MNG_ALLOC (pData, pData->pJPEGrow, pData->iJPEGrowlen);

        pData->iJPEGrgbrow = 0;        /* quite empty up to now */
      }

      pData->pJPEGcurrent   = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
      pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
    }
                                       /* decompress not started ? */
    if ((pData->bJPEGhasheader) && (!pData->bJPEGdecostarted))
    {
      /* set parameters for decompression */

      if (pData->bJPEGprogressive)     /* progressive display ? */
        pData->pJPEGdinfo->buffered_image = TRUE;

      /* jpeg_start_decompress(...); */
#ifdef MNG_SUPPORT_TRACE
      MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_START_DECOMPRESS)
#endif
      if (jpeg_start_decompress (pData->pJPEGdinfo) == TRUE)
                                       /* indicate it started */
        pData->bJPEGdecostarted = MNG_TRUE;

      pData->pJPEGcurrent   = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
      pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
    }
                                       /* process some scanlines ? */
    if ((pData->bJPEGhasheader) && (pData->bJPEGdecostarted) &&
	    ((!jpeg_input_complete (pData->pJPEGdinfo)) ||
         (pData->pJPEGdinfo->output_scanline < pData->pJPEGdinfo->output_height) ||
         ((pData->bJPEGprogressive) && (pData->bJPEGscanending))))
    {
      mng_int32 iLines = 0;

      /* for (each output pass) */
      do
      {                                /* address the row output buffer */
        JSAMPROW pRow = (JSAMPROW)pData->pJPEGrow;

                                       /* init new pass ? */
        if ((pData->bJPEGprogressive) && (!pData->bJPEGscanstarted))
        {
          pData->bJPEGscanstarted = MNG_TRUE;

          /* adjust output decompression parameters if required */
          /* nop */

          /* start a new output pass */
#ifdef MNG_SUPPORT_TRACE
          MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_START_OUTPUT)
#endif
          jpeg_start_output (pData->pJPEGdinfo, pData->pJPEGdinfo->input_scan_number);

          pData->iJPEGrow = 0;         /* start at row 0 in the image again */
        }

        /* while (scan lines remain to be read) */
        if ((!pData->bJPEGprogressive) || (!pData->bJPEGscanending))
        {
          do
          {
          /*   jpeg_read_scanlines(...); */
#ifdef MNG_SUPPORT_TRACE
            MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_READ_SCANLINES)
#endif
            iLines = jpeg_read_scanlines (pData->pJPEGdinfo, (JSAMPARRAY)&pRow, 1);

            pData->pJPEGcurrent   = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
            pData->iJPEGbufremain = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;

            if (iLines > 0)            /* got something ? */
            {
              if (pData->fStorerow2)   /* store in object ? */
              {
                iRetcode = ((mng_storerow)pData->fStorerow2) (pData);

                if (iRetcode)          /* on error bail out */
                return iRetcode;

              }
            }
          }
          while ((pData->pJPEGdinfo->output_scanline < pData->pJPEGdinfo->output_height) &&
                 (iLines > 0));        /* until end-of-image or not enough input-data */
        }

        /* terminate output pass */
        if ((pData->bJPEGprogressive) &&
            (pData->pJPEGdinfo->output_scanline >= pData->pJPEGdinfo->output_height))
        {
#ifdef MNG_SUPPORT_TRACE
          MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_FINISH_OUTPUT)
#endif
          if (jpeg_finish_output (pData->pJPEGdinfo) != JPEG_SUSPENDED)
          {                            /* this scan has ended */
            pData->bJPEGscanstarted = MNG_FALSE;
            pData->bJPEGscanending  = MNG_FALSE;
          }
          else
          {
            pData->bJPEGscanending  = MNG_TRUE;
          }
        }
      }
      while ((!jpeg_input_complete (pData->pJPEGdinfo)) &&
             (iLines > 0) && (!pData->bJPEGscanending));
    }
                                       /* end of image ? */
    if ((pData->bJPEGhasheader) && (pData->bJPEGdecostarted) &&
        (!pData->bJPEGscanending) && (jpeg_input_complete (pData->pJPEGdinfo)) &&
        (pData->pJPEGdinfo->input_scan_number == pData->pJPEGdinfo->output_scan_number))
    {
      /* jpeg_finish_decompress(...); */
#ifdef MNG_SUPPORT_TRACE
      MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_FINISH_DECOMPRESS)
#endif
      if (jpeg_finish_decompress (pData->pJPEGdinfo) == TRUE)
      {                                /* indicate it's done */
        pData->bJPEGhasheader   = MNG_FALSE;
        pData->bJPEGdecostarted = MNG_FALSE;
        pData->pJPEGcurrent     = (mng_uint8p)pData->pJPEGdinfo->src->next_input_byte;
        pData->iJPEGbufremain   = (mng_uint32)pData->pJPEGdinfo->src->bytes_in_buffer;
                                       /* remaining fluff is an error ! */
        if ((pData->iJPEGbufremain > 0) || (iRemain > 0))
          MNG_ERROR (pData, MNG_TOOMUCHJDAT);
      }
    }
#endif /* MNG_INCLUDE_IJG6B */
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_END);
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressfree (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  mng_retcode iRetcode;
#endif

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_START);
#endif

#ifdef MNG_INCLUDE_IJG6B
#ifdef MNG_USE_SETJMP
  iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif
                                       /* free the row of JPEG-samples*/
  MNG_FREE (pData, pData->pJPEGrow, pData->iJPEGrowlen);

  /* release the JPEG decompression object */
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_JPEG_DESTROY_DECOMPRESS)
#endif
  jpeg_destroy_decompress (pData->pJPEGdinfo);

  pData->bJPEGdecompress = MNG_FALSE;  /* indicate it's done */

#endif /* MNG_INCLUDE_IJG6B */

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_END);
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */
/* *                                                                        * */
/* * JPEG decompression routines (JDAA)                                     * */
/* *                                                                        * */
/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressinit2 (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  mng_retcode iRetcode;
#endif

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_START);
#endif

#ifdef MNG_INCLUDE_IJG6B
  /* allocate and initialize a JPEG decompression object */
  pData->pJPEGdinfo2->err = jpeg_std_error (pData->pJPEGderr2);

#ifdef MNG_USE_SETJMP                  /* setup local JPEG error-routines */
  pData->pJPEGderr2->error_exit     = mng_error_exit;
  pData->pJPEGderr2->output_message = mng_output_message;

  iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif /* MNG_USE_SETJMP */

  /* allocate and initialize a JPEG decompression object (continued) */
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_JPEG_CREATE_DECOMPRESS)
#endif
  jpeg_create_decompress (pData->pJPEGdinfo2);

  pData->bJPEGdecompress2 = MNG_TRUE;  /* indicate it's initialized */

  /* specify the source of the compressed data (eg, a file) */
                                       /* no, not a file; we have buffered input */
  pData->pJPEGdinfo2->src = pData->pJPEGdsrc2;
                                       /* use the default handler */
  pData->pJPEGdinfo2->src->resync_to_restart = jpeg_resync_to_restart;
                                       /* setup local source routine & parms */
  pData->pJPEGdinfo2->src->init_source       = mng_init_source;
  pData->pJPEGdinfo2->src->fill_input_buffer = mng_fill_input_buffer;
  pData->pJPEGdinfo2->src->skip_input_data   = mng_skip_input_data2;
  pData->pJPEGdinfo2->src->term_source       = mng_term_source;
  pData->pJPEGdinfo2->src->next_input_byte   = pData->pJPEGcurrent2;
  pData->pJPEGdinfo2->src->bytes_in_buffer   = pData->iJPEGbufremain2;

#endif /* MNG_INCLUDE_IJG6B */

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSINIT, MNG_LC_END);
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressdata2 (mng_datap  pData,
                                     mng_uint32 iRawsize,
                                     mng_uint8p pRawdata)
{
  mng_retcode iRetcode;
  mng_uint32  iRemain;
  mng_uint8p  pWork;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_START);
#endif

#if defined (MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  iRetcode = setjmp (pData->sErrorbuf);/* initialize local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif

  pWork   = pRawdata;
  iRemain = iRawsize;

  if (pData->iJPEGtoskip2)             /* JPEG-lib told us to skip some more data ? */
  {
    if (iRemain > pData->iJPEGtoskip2) /* enough data in this buffer ? */
    {
      iRemain -= pData->iJPEGtoskip2;  /* skip enough to access the next byte */
      pWork   += pData->iJPEGtoskip2;

      pData->iJPEGtoskip2 = 0;         /* no more to skip then */
    }
    else
    {
      pData->iJPEGtoskip2 -= iRemain;  /* skip all data in the buffer */
      iRemain = 0;                     /* and indicate this accordingly */
    }
                                       /* the skip set current-pointer to NULL ! */
    pData->pJPEGcurrent2 = pData->pJPEGbuf2;
  }

  while (iRemain)                      /* repeat until no more input-bytes */
  {                                    /* need to shift anything ? */
    if ((pData->pJPEGcurrent2 > pData->pJPEGbuf2) &&
        (pData->pJPEGcurrent2 - pData->pJPEGbuf2 + pData->iJPEGbufremain2 + iRemain > pData->iJPEGbufmax2))
    {
      if (pData->iJPEGbufremain2 > 0)  /* then do so */
        MNG_COPY (pData->pJPEGbuf2, pData->pJPEGcurrent2, pData->iJPEGbufremain2);

      pData->pJPEGcurrent2 = pData->pJPEGbuf2;
    }
                                       /* does the remaining input fit into the buffer ? */
    if (pData->iJPEGbufremain2 + iRemain <= pData->iJPEGbufmax2)
    {                                  /* move the lot */
      MNG_COPY ((pData->pJPEGcurrent2 + pData->iJPEGbufremain2), pWork, iRemain);
                                       /* adjust remaining_bytes counter */
      pData->iJPEGbufremain2 += iRemain;
      iRemain = 0;                     /* and indicate there's no input left */
    }
    else
    {                                  /* calculate what does fit */
      mng_uint32 iFits = pData->iJPEGbufmax2 - pData->iJPEGbufremain2;

      if (iFits <= 0)                  /* no space is just bugger 'm all */
        MNG_ERROR (pData, MNG_JPEGBUFTOOSMALL);
                                       /* move that */
      MNG_COPY ((pData->pJPEGcurrent2 + pData->iJPEGbufremain2), pWork, iFits);

      pData->iJPEGbufremain2 += iFits; /* adjust remain_bytes counter */
      iRemain -= iFits;                /* and the input-parms */
      pWork   += iFits;
    }

#ifdef MNG_INCLUDE_IJG6B
    pData->pJPEGdinfo2->src->next_input_byte = pData->pJPEGcurrent2;
    pData->pJPEGdinfo2->src->bytes_in_buffer = pData->iJPEGbufremain2;

    if (!pData->bJPEGhasheader2)       /* haven't got the header yet ? */
    {
      /* call jpeg_read_header() to obtain image info */
#ifdef MNG_SUPPORT_TRACE
      MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_READ_HEADER)
#endif
      if (jpeg_read_header (pData->pJPEGdinfo2, TRUE) != JPEG_SUSPENDED)
      {                                /* indicate the header's oke */
        pData->bJPEGhasheader2 = MNG_TRUE;
                                       /* let's do some sanity checks ! */
        if ((pData->pJPEGdinfo2->image_width  != pData->iDatawidth ) ||
            (pData->pJPEGdinfo2->image_height != pData->iDataheight)    )
          MNG_ERROR (pData, MNG_JPEGPARMSERR);

        if (pData->pJPEGdinfo2->jpeg_color_space != JCS_GRAYSCALE)
          MNG_ERROR (pData, MNG_JPEGPARMSERR);
                                       /* indicate whether or not it's progressive */
        pData->bJPEGprogressive2 = (mng_bool)jpeg_has_multiple_scans (pData->pJPEGdinfo2);

        if (pData->bJPEGprogressive2)  /* progressive alphachannel not allowed !!! */
          MNG_ERROR (pData, MNG_JPEGPARMSERR);
                                       /* allocate a row of JPEG-samples */
        if (pData->pJPEGdinfo2->jpeg_color_space == JCS_YCbCr)
          pData->iJPEGrowlen2 = pData->pJPEGdinfo2->image_width * RGB_PIXELSIZE;
        else
          pData->iJPEGrowlen2 = pData->pJPEGdinfo2->image_width;

        MNG_ALLOC (pData, pData->pJPEGrow2, pData->iJPEGrowlen2);

        pData->iJPEGalpharow = 0;      /* quite empty up to now */
      }

      pData->pJPEGcurrent2   = (mng_uint8p)pData->pJPEGdinfo2->src->next_input_byte;
      pData->iJPEGbufremain2 = (mng_uint32)pData->pJPEGdinfo2->src->bytes_in_buffer;
    }
                                       /* decompress not started ? */
    if ((pData->bJPEGhasheader2) && (!pData->bJPEGdecostarted2))
    {
      /* set parameters for decompression */

      if (pData->bJPEGprogressive2)    /* progressive display ? */
        pData->pJPEGdinfo2->buffered_image = TRUE;

      /* jpeg_start_decompress(...); */
#ifdef MNG_SUPPORT_TRACE
      MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_START_DECOMPRESS)
#endif
      if (jpeg_start_decompress (pData->pJPEGdinfo2) == TRUE)
                                       /* indicate it started */
        pData->bJPEGdecostarted2 = MNG_TRUE;

      pData->pJPEGcurrent2   = (mng_uint8p)pData->pJPEGdinfo2->src->next_input_byte;
      pData->iJPEGbufremain2 = (mng_uint32)pData->pJPEGdinfo2->src->bytes_in_buffer;
    }
                                       /* process some scanlines ? */
    if ((pData->bJPEGhasheader2) && (pData->bJPEGdecostarted2) &&
	    ((!jpeg_input_complete (pData->pJPEGdinfo2)) ||
         (pData->pJPEGdinfo2->output_scanline < pData->pJPEGdinfo2->output_height)))
    {
      mng_int32 iLines;

      /* for (each output pass) */
      do
      {                                /* address the row output buffer */
        JSAMPROW pRow = (JSAMPROW)pData->pJPEGrow2;

                                       /* init new pass ? */
        if ((pData->bJPEGprogressive2) &&
            ((!pData->bJPEGscanstarted2) ||
             (pData->pJPEGdinfo2->output_scanline >= pData->pJPEGdinfo2->output_height)))
        {
          pData->bJPEGscanstarted2 = MNG_TRUE;

          /* adjust output decompression parameters if required */
          /* nop */

          /* start a new output pass */
#ifdef MNG_SUPPORT_TRACE
          MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_START_OUTPUT)
#endif
          jpeg_start_output (pData->pJPEGdinfo2, pData->pJPEGdinfo2->input_scan_number);

          pData->iJPEGrow = 0;         /* start at row 0 in the image again */
        }

        /* while (scan lines remain to be read) */
        do
        {
          /*   jpeg_read_scanlines(...); */
#ifdef MNG_SUPPORT_TRACE
          MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_READ_SCANLINES)
#endif
          iLines = jpeg_read_scanlines (pData->pJPEGdinfo2, (JSAMPARRAY)&pRow, 1);

          pData->pJPEGcurrent2   = (mng_uint8p)pData->pJPEGdinfo2->src->next_input_byte;
          pData->iJPEGbufremain2 = (mng_uint32)pData->pJPEGdinfo2->src->bytes_in_buffer;

          if (iLines > 0)              /* got something ? */
          {
            if (pData->fStorerow3)     /* store in object ? */
            {
              iRetcode = ((mng_storerow)pData->fStorerow3) (pData);

              if (iRetcode)            /* on error bail out */
                return iRetcode;

            }
          }
        }
        while ((pData->pJPEGdinfo2->output_scanline < pData->pJPEGdinfo2->output_height) &&
               (iLines > 0));          /* until end-of-image or not enough input-data */

        /* terminate output pass */
        if ((pData->bJPEGprogressive2) &&
            (pData->pJPEGdinfo2->output_scanline >= pData->pJPEGdinfo2->output_height))
        {
#ifdef MNG_SUPPORT_TRACE
          MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_FINISH_OUTPUT)
#endif
          if (jpeg_finish_output (pData->pJPEGdinfo2) == JPEG_SUSPENDED)
            jpeg_finish_output (pData->pJPEGdinfo2);
                                       /* this scan has ended */
          pData->bJPEGscanstarted2 = MNG_FALSE;
        }
      }
      while ((!jpeg_input_complete (pData->pJPEGdinfo2)) && (iLines > 0));
    }
                                       /* end of image ? */
    if ((pData->bJPEGhasheader2) && (pData->bJPEGdecostarted2) &&
        (jpeg_input_complete (pData->pJPEGdinfo2)) &&
        (pData->pJPEGdinfo2->input_scan_number == pData->pJPEGdinfo2->output_scan_number))
    {
      /* jpeg_finish_decompress(...); */
#ifdef MNG_SUPPORT_TRACE
      MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_JPEG_FINISH_DECOMPRESS)
#endif
      if (jpeg_finish_decompress (pData->pJPEGdinfo2) == TRUE)
      {                                /* indicate it's done */
        pData->bJPEGhasheader2   = MNG_FALSE;
        pData->bJPEGdecostarted2 = MNG_FALSE;
        pData->pJPEGcurrent2     = (mng_uint8p)pData->pJPEGdinfo2->src->next_input_byte;
        pData->iJPEGbufremain2   = (mng_uint32)pData->pJPEGdinfo2->src->bytes_in_buffer;
                                       /* remaining fluff is an error ! */
        if ((pData->iJPEGbufremain2 > 0) || (iRemain > 0))
          MNG_ERROR (pData, MNG_TOOMUCHJDAT);
      }
    }
#endif /* MNG_INCLUDE_IJG6B */
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSDATA, MNG_LC_END);
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#ifdef MNG_INCLUDE_JNG_READ
mng_retcode mngjpeg_decompressfree2 (mng_datap pData)
{
#if defined(MNG_INCLUDE_IJG6B) && defined(MNG_USE_SETJMP)
  mng_retcode iRetcode;
#endif

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_START);
#endif

#ifdef MNG_INCLUDE_IJG6B
#ifdef MNG_USE_SETJMP
  iRetcode = setjmp (pData->sErrorbuf);/* setup local JPEG error-recovery */
  if (iRetcode != 0)                   /* got here from longjmp ? */
    MNG_ERRORJ (pData, iRetcode);      /* then IJG-lib issued an error */
#endif
                                       /* free the row of JPEG-samples*/
  MNG_FREE (pData, pData->pJPEGrow2, pData->iJPEGrowlen2);

  /* release the JPEG decompression object */
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_JPEG_DESTROY_DECOMPRESS)
#endif
  jpeg_destroy_decompress (pData->pJPEGdinfo2);

  pData->bJPEGdecompress2 = MNG_FALSE; /* indicate it's done */

#endif /* MNG_INCLUDE_IJG6B */

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_JPEG_DECOMPRESSFREE, MNG_LC_END);
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_JNG_READ */

/* ************************************************************************** */

#endif /* MNG_INCLUDE_JNG && MNG_INCLUDE_DISPLAY_PROCS */

/* ************************************************************************** */
/* * end of file                                                            * */
/* ************************************************************************** */