summaryrefslogtreecommitdiff
path: root/packages/pasjpeg/src/jdapistd.pas
blob: b816ff830819086a756c698f11ae95a03ab492d2 (plain)
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
Unit JdAPIstd;

{ Original : jdapistd.c ;  Copyright (C) 1994-1996, Thomas G. Lane. }

{  This file is part of the Independent JPEG Group's software.
  For conditions of distribution and use, see the accompanying README file.

  This file contains application interface code for the decompression half
  of the JPEG library.  These are the "standard" API routines that are
  used in the normal full-decompression case.  They are not used by a
  transcoding-only application.  Note that if an application links in
  jpeg_start_decompress, it will end up linking in the entire decompressor.
  We thus must separate this file from jdapimin.c to avoid linking the
  whole decompression library into a transcoder. }

interface

{$I jconfig.inc}

uses
  jmorecfg,
  jinclude,
  jdeferr,
  jerror,
  jpeglib,
  jdmaster;

{ Read some scanlines of data from the JPEG decompressor.

  The return value will be the number of lines actually read.
  This may be less than the number requested in several cases,
  including bottom of image, data source suspension, and operating
  modes that emit multiple scanlines at a time.

  Note: we warn about excess calls to jpeg_read_scanlines() since
  this likely signals an application programmer error.  However,
  an oversize buffer (max_lines > scanlines remaining) is not an error. }

{GLOBAL}
function jpeg_read_scanlines (cinfo : j_decompress_ptr;
                              scanlines : JSAMPARRAY;
                              max_lines : JDIMENSION) : JDIMENSION;


{ Alternate entry point to read raw data.
  Processes exactly one iMCU row per call, unless suspended. }

{GLOBAL}
function jpeg_read_raw_data (cinfo : j_decompress_ptr;
                             data : JSAMPIMAGE;
                             max_lines : JDIMENSION) : JDIMENSION;

{$ifdef D_MULTISCAN_FILES_SUPPORTED}

{ Initialize for an output pass in buffered-image mode. }

{GLOBAL}
function jpeg_start_output (cinfo : j_decompress_ptr;
                            scan_number : int) : boolean;

{ Finish up after an output pass in buffered-image mode.

  Returns FALSE if suspended.  The return value need be inspected only if
  a suspending data source is used. }

{GLOBAL}
function jpeg_finish_output (cinfo : j_decompress_ptr) : boolean;

{$endif} { D_MULTISCAN_FILES_SUPPORTED }

{ Decompression initialization.
  jpeg_read_header must be completed before calling this.

  If a multipass operating mode was selected, this will do all but the
  last pass, and thus may take a great deal of time.

  Returns FALSE if suspended.  The return value need be inspected only if
  a suspending data source is used. }

{GLOBAL}
function jpeg_start_decompress (cinfo : j_decompress_ptr) : boolean;


implementation

{ Forward declarations }
{LOCAL}
function output_pass_setup (cinfo : j_decompress_ptr) : boolean; forward;

{ Decompression initialization.
  jpeg_read_header must be completed before calling this.

  If a multipass operating mode was selected, this will do all but the
  last pass, and thus may take a great deal of time.

  Returns FALSE if suspended.  The return value need be inspected only if
  a suspending data source is used. }

{GLOBAL}
function jpeg_start_decompress (cinfo : j_decompress_ptr) : boolean;
var
  retcode : int;
begin
  if (cinfo^.global_state = DSTATE_READY) then
  begin
    { First call: initialize master control, select active modules }
    jinit_master_decompress(cinfo);
    if (cinfo^.buffered_image) then
    begin
      { No more work here; expecting jpeg_start_output next }
      cinfo^.global_state := DSTATE_BUFIMAGE;
      jpeg_start_decompress := TRUE;
      exit;
    end;
    cinfo^.global_state := DSTATE_PRELOAD;
  end;
  if (cinfo^.global_state = DSTATE_PRELOAD) then
  begin
    { If file has multiple scans, absorb them all into the coef buffer }
    if (cinfo^.inputctl^.has_multiple_scans) then
    begin
{$ifdef D_MULTISCAN_FILES_SUPPORTED}
      while TRUE do
      begin

        { Call progress monitor hook if present }
        if (cinfo^.progress <> NIL) then
          cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
        { Absorb some more input }
        retcode := cinfo^.inputctl^.consume_input (cinfo);
        if (retcode = JPEG_SUSPENDED) then
        begin
          jpeg_start_decompress := FALSE;
          exit;
        end;
        if (retcode = JPEG_REACHED_EOI) then
          break;
        { Advance progress counter if appropriate }
        if (cinfo^.progress <> NIL) and
           ((retcode = JPEG_ROW_COMPLETED) or (retcode = JPEG_REACHED_SOS)) then
        begin
          Inc(cinfo^.progress^.pass_counter);
          if (cinfo^.progress^.pass_counter >= cinfo^.progress^.pass_limit) then
          begin
            { jdmaster underestimated number of scans; ratchet up one scan }
            Inc(cinfo^.progress^.pass_limit, long(cinfo^.total_iMCU_rows));
          end;
        end;
      end;
{$else}
      ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
{$endif} { D_MULTISCAN_FILES_SUPPORTED }
    end;
    cinfo^.output_scan_number := cinfo^.input_scan_number;
  end
  else
    if (cinfo^.global_state <> DSTATE_PRESCAN) then
      ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  { Perform any dummy output passes, and set up for the final pass }
  jpeg_start_decompress := output_pass_setup(cinfo);
end;


{ Set up for an output pass, and perform any dummy pass(es) needed.
  Common subroutine for jpeg_start_decompress and jpeg_start_output.
  Entry: global_state := DSTATE_PRESCAN only if previously suspended.
  Exit: If done, returns TRUE and sets global_state for proper output mode.
        If suspended, returns FALSE and sets global_state := DSTATE_PRESCAN. }

{LOCAL}
function output_pass_setup (cinfo : j_decompress_ptr) : boolean;
var
  last_scanline : JDIMENSION;
begin
  if (cinfo^.global_state <> DSTATE_PRESCAN) then
  begin
    { First call: do pass setup }
    cinfo^.master^.prepare_for_output_pass (cinfo);
    cinfo^.output_scanline := 0;
    cinfo^.global_state := DSTATE_PRESCAN;
  end;
  { Loop over any required dummy passes }
  while (cinfo^.master^.is_dummy_pass) do
  begin
{$ifdef QUANT_2PASS_SUPPORTED}
    { Crank through the dummy pass }
    while (cinfo^.output_scanline < cinfo^.output_height) do
    begin
      { Call progress monitor hook if present }
      if (cinfo^.progress <> NIL) then
      begin
        cinfo^.progress^.pass_counter := long (cinfo^.output_scanline);
        cinfo^.progress^.pass_limit := long (cinfo^.output_height);
        cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
      end;
      { Process some data }
      last_scanline := cinfo^.output_scanline;
      cinfo^.main^.process_data (cinfo, JSAMPARRAY(NIL),
                                 cinfo^.output_scanline, {var}
                                 JDIMENSION(0));
      if (cinfo^.output_scanline = last_scanline) then
      begin
        output_pass_setup := FALSE;     { No progress made, must suspend }
        exit;
      end;
    end;
    { Finish up dummy pass, and set up for another one }
    cinfo^.master^.finish_output_pass (cinfo);
    cinfo^.master^.prepare_for_output_pass (cinfo);
    cinfo^.output_scanline := 0;
{$else}
    ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
{$endif} { QUANT_2PASS_SUPPORTED }
  end;
  { Ready for application to drive output pass through
    jpeg_read_scanlines or jpeg_read_raw_data. }
  if cinfo^.raw_data_out then
    cinfo^.global_state := DSTATE_RAW_OK
   else
     cinfo^.global_state := DSTATE_SCANNING;
  output_pass_setup := TRUE;
end;


{ Read some scanlines of data from the JPEG decompressor.

  The return value will be the number of lines actually read.
  This may be less than the number requested in several cases,
  including bottom of image, data source suspension, and operating
  modes that emit multiple scanlines at a time.

  Note: we warn about excess calls to jpeg_read_scanlines() since
  this likely signals an application programmer error.  However,
  an oversize buffer (max_lines > scanlines remaining) is not an error. }

{GLOBAL}
function jpeg_read_scanlines (cinfo : j_decompress_ptr;
                              scanlines : JSAMPARRAY;
                              max_lines : JDIMENSION) : JDIMENSION;
var
  row_ctr : JDIMENSION;
begin
  if (cinfo^.global_state <> DSTATE_SCANNING) then
    ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  if (cinfo^.output_scanline >= cinfo^.output_height) then
  begin
    WARNMS(j_common_ptr(cinfo), JWRN_TOO_MUCH_DATA);
    jpeg_read_scanlines := 0;
    exit;
  end;

  { Call progress monitor hook if present }
  if (cinfo^.progress <> NIL) then
  begin
    cinfo^.progress^.pass_counter := long (cinfo^.output_scanline);
    cinfo^.progress^.pass_limit := long (cinfo^.output_height);
    cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  end;

  { Process some data }
  row_ctr := 0;
  cinfo^.main^.process_data (cinfo, scanlines, {var}row_ctr, max_lines);
  Inc(cinfo^.output_scanline, row_ctr);
  jpeg_read_scanlines := row_ctr;
end;


{ Alternate entry point to read raw data.
  Processes exactly one iMCU row per call, unless suspended. }

{GLOBAL}
function jpeg_read_raw_data (cinfo : j_decompress_ptr;
                             data : JSAMPIMAGE;
                             max_lines : JDIMENSION) : JDIMENSION;
var
  lines_per_iMCU_row : JDIMENSION;
begin
  if (cinfo^.global_state <> DSTATE_RAW_OK) then
    ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  if (cinfo^.output_scanline >= cinfo^.output_height) then
  begin
    WARNMS(j_common_ptr(cinfo), JWRN_TOO_MUCH_DATA);
    jpeg_read_raw_data := 0;
    exit;
  end;

  { Call progress monitor hook if present }
  if (cinfo^.progress <> NIL) then
  begin
    cinfo^.progress^.pass_counter := long (cinfo^.output_scanline);
    cinfo^.progress^.pass_limit := long (cinfo^.output_height);
    cinfo^.progress^.progress_monitor (j_common_ptr(cinfo));
  end;

  { Verify that at least one iMCU row can be returned. }
  lines_per_iMCU_row := cinfo^.max_v_samp_factor * cinfo^.min_DCT_scaled_size;
  if (max_lines < lines_per_iMCU_row) then
    ERREXIT(j_common_ptr(cinfo), JERR_BUFFER_SIZE);

  { Decompress directly into user's buffer. }
  if (cinfo^.coef^.decompress_data (cinfo, data) = 0) then
  begin
    jpeg_read_raw_data := 0;                    { suspension forced, can do nothing more }
    exit;
  end;

  { OK, we processed one iMCU row. }
  Inc(cinfo^.output_scanline, lines_per_iMCU_row);
  jpeg_read_raw_data := lines_per_iMCU_row;
end;


{ Additional entry points for buffered-image mode. }

{$ifdef D_MULTISCAN_FILES_SUPPORTED}

{ Initialize for an output pass in buffered-image mode. }

{GLOBAL}
function jpeg_start_output (cinfo : j_decompress_ptr;
                            scan_number : int) : boolean;
begin
  if (cinfo^.global_state <> DSTATE_BUFIMAGE) and
     (cinfo^.global_state <> DSTATE_PRESCAN) then
    ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  { Limit scan number to valid range }
  if (scan_number <= 0) then
    scan_number := 1;
  if (cinfo^.inputctl^.eoi_reached) and
     (scan_number > cinfo^.input_scan_number) then
    scan_number := cinfo^.input_scan_number;
  cinfo^.output_scan_number := scan_number;
  { Perform any dummy output passes, and set up for the real pass }
  jpeg_start_output := output_pass_setup(cinfo);
end;


{ Finish up after an output pass in buffered-image mode.

  Returns FALSE if suspended.  The return value need be inspected only if
  a suspending data source is used. }

{GLOBAL}
function jpeg_finish_output (cinfo : j_decompress_ptr) : boolean;
begin
  if ((cinfo^.global_state = DSTATE_SCANNING) or
      (cinfo^.global_state = DSTATE_RAW_OK) and cinfo^.buffered_image) then
  begin
    { Terminate this pass. }
    { We do not require the whole pass to have been completed. }
    cinfo^.master^.finish_output_pass (cinfo);
    cinfo^.global_state := DSTATE_BUFPOST;
  end
  else
    if (cinfo^.global_state <> DSTATE_BUFPOST) then
    begin
      { BUFPOST := repeat call after a suspension, anything else is error }
      ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
    end;
  { Read markers looking for SOS or EOI }
  while (cinfo^.input_scan_number <= cinfo^.output_scan_number) and
        (not cinfo^.inputctl^.eoi_reached) do
  begin
    if (cinfo^.inputctl^.consume_input (cinfo) = JPEG_SUSPENDED) then
    begin
      jpeg_finish_output := FALSE;      { Suspend, come back later }
      exit;
    end;
  end;
  cinfo^.global_state := DSTATE_BUFIMAGE;
  jpeg_finish_output := TRUE;
end;

{$endif} { D_MULTISCAN_FILES_SUPPORTED }

end.