DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / JpegLib / imjdmarker.pas
1 unit imjdmarker;
3 { This file contains routines to decode JPEG datastream markers.
4 Most of the complexity arises from our desire to support input
5 suspension: if not all of the data for a marker is available;
6 we must exit back to the application. On resumption; we reprocess
7 the marker. }
9 { Original: jdmarker.c; Copyright (C) 1991-1998; Thomas G. Lane. }
10 { History
11 9.7.96 Conversion to pascal started jnn
12 22.3.98 updated to 6b jnn }
15 interface
17 {$I imjconfig.inc}
19 uses
20 imjmorecfg,
21 imjinclude,
22 imjdeferr,
23 imjerror,
24 imjcomapi,
25 imjpeglib;
27 const { JPEG marker codes }
28 M_SOF0 = $c0;
29 M_SOF1 = $c1;
30 M_SOF2 = $c2;
31 M_SOF3 = $c3;
33 M_SOF5 = $c5;
34 M_SOF6 = $c6;
35 M_SOF7 = $c7;
37 M_JPG = $c8;
38 M_SOF9 = $c9;
39 M_SOF10 = $ca;
40 M_SOF11 = $cb;
42 M_SOF13 = $cd;
43 M_SOF14 = $ce;
44 M_SOF15 = $cf;
46 M_DHT = $c4;
48 M_DAC = $cc;
50 M_RST0 = $d0;
51 M_RST1 = $d1;
52 M_RST2 = $d2;
53 M_RST3 = $d3;
54 M_RST4 = $d4;
55 M_RST5 = $d5;
56 M_RST6 = $d6;
57 M_RST7 = $d7;
59 M_SOI = $d8;
60 M_EOI = $d9;
61 M_SOS = $da;
62 M_DQT = $db;
63 M_DNL = $dc;
64 M_DRI = $dd;
65 M_DHP = $de;
66 M_EXP = $df;
68 M_APP0 = $e0;
69 M_APP1 = $e1;
70 M_APP2 = $e2;
71 M_APP3 = $e3;
72 M_APP4 = $e4;
73 M_APP5 = $e5;
74 M_APP6 = $e6;
75 M_APP7 = $e7;
76 M_APP8 = $e8;
77 M_APP9 = $e9;
78 M_APP10 = $ea;
79 M_APP11 = $eb;
80 M_APP12 = $ec;
81 M_APP13 = $ed;
82 M_APP14 = $ee;
83 M_APP15 = $ef;
85 M_JPG0 = $f0;
86 M_JPG13 = $fd;
87 M_COM = $fe;
89 M_TEM = $01;
91 M_ERROR = $100;
93 type
94 JPEG_MARKER = uint; { JPEG marker codes }
96 { Private state }
98 type
99 my_marker_ptr = ^my_marker_reader;
100 my_marker_reader = record
101 pub : jpeg_marker_reader; { public fields }
103 { Application-overridable marker processing methods }
104 process_COM : jpeg_marker_parser_method;
105 process_APPn : array[0..16-1] of jpeg_marker_parser_method;
107 { Limit on marker data length to save for each marker type }
108 length_limit_COM : uint;
109 length_limit_APPn : array[0..16-1] of uint;
111 { Status of COM/APPn marker saving }
112 cur_marker : jpeg_saved_marker_ptr; { NIL if not processing a marker }
113 bytes_read : uint; { data bytes read so far in marker }
114 { Note: cur_marker is not linked into marker_list until it's all read. }
115 end;
117 {GLOBAL}
118 function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
119 desired : int) : boolean;
120 {GLOBAL}
121 procedure jinit_marker_reader (cinfo : j_decompress_ptr);
123 {$ifdef SAVE_MARKERS_SUPPORTED}
125 {GLOBAL}
126 procedure jpeg_save_markers (cinfo : j_decompress_ptr;
127 marker_code : int;
128 length_limit : uint);
129 {$ENDIF}
131 {GLOBAL}
132 procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
133 marker_code : int;
134 routine : jpeg_marker_parser_method);
136 implementation
138 uses
139 imjutils;
141 { At all times, cinfo1.src.next_input_byte and .bytes_in_buffer reflect
142 the current restart point; we update them only when we have reached a
143 suitable place to restart if a suspension occurs. }
146 { Routines to process JPEG markers.
148 Entry condition: JPEG marker itself has been read and its code saved
149 in cinfo^.unread_marker; input restart point is just after the marker.
151 Exit: if return TRUE, have read and processed any parameters, and have
152 updated the restart point to point after the parameters.
153 If return FALSE, was forced to suspend before reaching end of
154 marker parameters; restart point has not been moved. Same routine
155 will be called again after application supplies more input data.
157 This approach to suspension assumes that all of a marker's parameters
158 can fit into a single input bufferload. This should hold for "normal"
159 markers. Some COM/APPn markers might have large parameter segments
160 that might not fit. If we are simply dropping such a marker, we use
161 skip_input_data to get past it, and thereby put the problem on the
162 source manager's shoulders. If we are saving the marker's contents
163 into memory, we use a slightly different convention: when forced to
164 suspend, the marker processor updates the restart point to the end of
165 what it's consumed (ie, the end of the buffer) before returning FALSE.
166 On resumption, cinfo->unread_marker still contains the marker code,
167 but the data source will point to the next chunk of marker data.
168 The marker processor must retain internal state to deal with this.
170 Note that we don't bother to avoid duplicate trace messages if a
171 suspension occurs within marker parameters. Other side effects
172 require more care. }
174 {LOCAL}
175 function get_soi (cinfo : j_decompress_ptr) : boolean;
176 { Process an SOI marker }
177 var
178 i : int;
179 begin
180 {$IFDEF DEBUG}
181 TRACEMS(j_common_ptr(cinfo), 1, JTRC_SOI);
182 {$ENDIF}
184 if (cinfo^.marker^.saw_SOI) then
185 ERREXIT(j_common_ptr(cinfo), JERR_SOI_DUPLICATE);
187 { Reset all parameters that are defined to be reset by SOI }
189 for i := 0 to Pred(NUM_ARITH_TBLS) do
190 with cinfo^ do
191 begin
192 arith_dc_L[i] := 0;
193 arith_dc_U[i] := 1;
194 arith_ac_K[i] := 5;
195 end;
196 cinfo^.restart_interval := 0;
198 { Set initial assumptions for colorspace etc }
200 with cinfo^ do
201 begin
202 jpeg_color_space := JCS_UNKNOWN;
203 CCIR601_sampling := FALSE; { Assume non-CCIR sampling??? }
205 saw_JFIF_marker := FALSE;
206 JFIF_major_version := 1; { set default JFIF APP0 values }
207 JFIF_minor_version := 1;
208 density_unit := 0;
209 X_density := 1;
210 Y_density := 1;
211 saw_Adobe_marker := FALSE;
212 Adobe_transform := 0;
214 marker^.saw_SOI := TRUE;
215 end;
216 get_soi := TRUE;
217 end; { get_soi }
220 {LOCAL}
221 function get_sof(cinfo : j_decompress_ptr;
222 is_prog : boolean;
223 is_arith : boolean) : boolean;
224 { Process a SOFn marker }
225 var
226 length : INT32;
227 c, ci : int;
228 compptr : jpeg_component_info_ptr;
229 { Declare and initialize local copies of input pointer/count }
230 var
231 datasrc : jpeg_source_mgr_ptr;
232 next_input_byte : JOCTETptr;
233 bytes_in_buffer : size_t;
234 begin
235 datasrc := cinfo^.src;
236 next_input_byte := datasrc^.next_input_byte;
237 bytes_in_buffer := datasrc^.bytes_in_buffer;
238 {}
239 cinfo^.progressive_mode := is_prog;
240 cinfo^.arith_code := is_arith;
242 { Read two bytes interpreted as an unsigned 16-bit integer.
243 length should be declared unsigned int or perhaps INT32. }
245 { make a byte available.
246 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
247 but we must reload the local copies after a successful fill. }
248 if (bytes_in_buffer = 0) then
249 begin
250 if (not datasrc^.fill_input_buffer(cinfo)) then
251 begin
252 get_sof := FALSE;
253 exit;
254 end;
255 { Reload the local copies }
256 next_input_byte := datasrc^.next_input_byte;
257 bytes_in_buffer := datasrc^.bytes_in_buffer;
258 end;
259 Dec( bytes_in_buffer );
261 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
262 Inc( next_input_byte );
263 { make a byte available.
264 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
265 but we must reload the local copies after a successful fill. }
266 if (bytes_in_buffer = 0) then
267 begin
268 if (not datasrc^.fill_input_buffer(cinfo)) then
269 begin
270 get_sof := FALSE;
271 exit;
272 end;
273 { Reload the local copies }
274 next_input_byte := datasrc^.next_input_byte;
275 bytes_in_buffer := datasrc^.bytes_in_buffer;
276 end;
277 Dec( bytes_in_buffer );
279 Inc( length, GETJOCTET( next_input_byte^));
280 Inc( next_input_byte );
283 { Read a byte into variable cinfo^.data_precision.
284 If must suspend, return FALSE. }
285 { make a byte available.
286 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
287 but we must reload the local copies after a successful fill. }
288 if (bytes_in_buffer = 0) then
289 begin
290 if (not datasrc^.fill_input_buffer(cinfo)) then
291 begin
292 get_sof := FALSE;
293 exit;
294 end;
295 { Reload the local copies }
296 next_input_byte := datasrc^.next_input_byte;
297 bytes_in_buffer := datasrc^.bytes_in_buffer;
298 end;
299 Dec( bytes_in_buffer );
301 cinfo^.data_precision := GETJOCTET(next_input_byte^);
302 Inc(next_input_byte);
304 { Read two bytes interpreted as an unsigned 16-bit integer.
305 cinfo^.image_height should be declared unsigned int or perhaps INT32. }
307 { make a byte available.
308 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
309 but we must reload the local copies after a successful fill. }
310 if (bytes_in_buffer = 0) then
311 begin
312 if (not datasrc^.fill_input_buffer(cinfo)) then
313 begin
314 get_sof := FALSE;
315 exit;
316 end;
317 { Reload the local copies }
318 next_input_byte := datasrc^.next_input_byte;
319 bytes_in_buffer := datasrc^.bytes_in_buffer;
320 end;
321 Dec( bytes_in_buffer );
323 cinfo^.image_height := (uint( GETJOCTET(next_input_byte^)) shl 8);
324 Inc( next_input_byte );
325 { make a byte available.
326 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
327 but we must reload the local copies after a successful fill. }
328 if (bytes_in_buffer = 0) then
329 begin
330 if (not datasrc^.fill_input_buffer(cinfo)) then
331 begin
332 get_sof := FALSE;
333 exit;
334 end;
335 { Reload the local copies }
336 next_input_byte := datasrc^.next_input_byte;
337 bytes_in_buffer := datasrc^.bytes_in_buffer;
338 end;
339 Dec( bytes_in_buffer );
341 Inc( cinfo^.image_height, GETJOCTET( next_input_byte^));
342 Inc( next_input_byte );
344 { Read two bytes interpreted as an unsigned 16-bit integer.
345 cinfo^.image_width should be declared unsigned int or perhaps INT32. }
347 { make a byte available.
348 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
349 but we must reload the local copies after a successful fill. }
350 if (bytes_in_buffer = 0) then
351 begin
352 if (not datasrc^.fill_input_buffer(cinfo)) then
353 begin
354 get_sof := FALSE;
355 exit;
356 end;
357 { Reload the local copies }
358 next_input_byte := datasrc^.next_input_byte;
359 bytes_in_buffer := datasrc^.bytes_in_buffer;
360 end;
361 Dec( bytes_in_buffer );
363 cinfo^.image_width := (uint( GETJOCTET(next_input_byte^)) shl 8);
364 Inc( next_input_byte );
365 { make a byte available.
366 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
367 but we must reload the local copies after a successful fill. }
368 if (bytes_in_buffer = 0) then
369 begin
370 if (not datasrc^.fill_input_buffer(cinfo)) then
371 begin
372 get_sof := FALSE;
373 exit;
374 end;
375 { Reload the local copies }
376 next_input_byte := datasrc^.next_input_byte;
377 bytes_in_buffer := datasrc^.bytes_in_buffer;
378 end;
379 Dec( bytes_in_buffer );
381 Inc( cinfo^.image_width, GETJOCTET( next_input_byte^));
382 Inc( next_input_byte );
384 { Read a byte into variable cinfo^.num_components.
385 If must suspend, return FALSE. }
386 { make a byte available.
387 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
388 but we must reload the local copies after a successful fill. }
389 if (bytes_in_buffer = 0) then
390 begin
391 if (not datasrc^.fill_input_buffer(cinfo)) then
392 begin
393 get_sof := FALSE;
394 exit;
395 end;
396 { Reload the local copies }
397 next_input_byte := datasrc^.next_input_byte;
398 bytes_in_buffer := datasrc^.bytes_in_buffer;
399 end;
400 Dec( bytes_in_buffer );
402 cinfo^.num_components := GETJOCTET(next_input_byte^);
403 Inc(next_input_byte);
405 Dec(length, 8);
407 {$IFDEF DEBUG}
408 TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF, cinfo^.unread_marker,
409 int(cinfo^.image_width), int(cinfo^.image_height),
410 cinfo^.num_components);
411 {$ENDIF}
413 if (cinfo^.marker^.saw_SOF) then
414 ERREXIT(j_common_ptr(cinfo), JERR_SOF_DUPLICATE);
416 { We don't support files in which the image height is initially specified }
417 { as 0 and is later redefined by DNL. As long as we have to check that, }
418 { might as well have a general sanity check. }
419 if (cinfo^.image_height <= 0) or (cinfo^.image_width <= 0)
420 or (cinfo^.num_components <= 0) then
421 ERREXIT(j_common_ptr(cinfo), JERR_EMPTY_IMAGE);
423 if (length <> (cinfo^.num_components * 3)) then
424 ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
426 if (cinfo^.comp_info = NIL) then { do only once, even if suspend }
427 cinfo^.comp_info := jpeg_component_info_list_ptr(
428 cinfo^.mem^.alloc_small(j_common_ptr(cinfo), JPOOL_IMAGE,
429 cinfo^.num_components * SIZEOF(jpeg_component_info)));
431 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
432 for ci := 0 to pred(cinfo^.num_components) do
433 begin
434 compptr^.component_index := ci;
436 { Read a byte into variable compptr^.component_id.
437 If must suspend, return FALSE. }
438 { make a byte available.
439 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
440 but we must reload the local copies after a successful fill. }
441 if (bytes_in_buffer = 0) then
442 begin
443 if (not datasrc^.fill_input_buffer(cinfo)) then
444 begin
445 get_sof := FALSE;
446 exit;
447 end;
448 { Reload the local copies }
449 next_input_byte := datasrc^.next_input_byte;
450 bytes_in_buffer := datasrc^.bytes_in_buffer;
451 end;
452 Dec( bytes_in_buffer );
454 compptr^.component_id := GETJOCTET(next_input_byte^);
455 Inc(next_input_byte);
457 { Read a byte into variable c. If must suspend, return FALSE. }
458 { make a byte available.
459 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
460 but we must reload the local copies after a successful fill. }
461 if (bytes_in_buffer = 0) then
462 begin
463 if (not datasrc^.fill_input_buffer(cinfo)) then
464 begin
465 get_sof := FALSE;
466 exit;
467 end;
468 { Reload the local copies }
469 next_input_byte := datasrc^.next_input_byte;
470 bytes_in_buffer := datasrc^.bytes_in_buffer;
471 end;
472 Dec( bytes_in_buffer );
474 c := GETJOCTET(next_input_byte^);
475 Inc(next_input_byte);
477 compptr^.h_samp_factor := (c shr 4) and 15;
478 compptr^.v_samp_factor := (c ) and 15;
480 { Read a byte into variable compptr^.quant_tbl_no.
481 If must suspend, return FALSE. }
482 { make a byte available.
483 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
484 but we must reload the local copies after a successful fill. }
485 if (bytes_in_buffer = 0) then
486 begin
487 if (not datasrc^.fill_input_buffer(cinfo)) then
488 begin
489 get_sof := FALSE;
490 exit;
491 end;
492 { Reload the local copies }
493 next_input_byte := datasrc^.next_input_byte;
494 bytes_in_buffer := datasrc^.bytes_in_buffer;
495 end;
496 Dec( bytes_in_buffer );
498 compptr^.quant_tbl_no := GETJOCTET(next_input_byte^);
499 Inc(next_input_byte);
501 {$IFDEF DEBUG}
502 TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOF_COMPONENT,
503 compptr^.component_id, compptr^.h_samp_factor,
504 compptr^.v_samp_factor, compptr^.quant_tbl_no);
505 {$ENDIF}
507 Inc(compptr);
508 end;
510 cinfo^.marker^.saw_SOF := TRUE;
512 { Unload the local copies --- do this only at a restart boundary }
513 datasrc^.next_input_byte := next_input_byte;
514 datasrc^.bytes_in_buffer := bytes_in_buffer;
516 get_sof := TRUE;
517 end; { get_sof }
520 {LOCAL}
521 function get_sos (cinfo : j_decompress_ptr) : boolean;
522 { Process a SOS marker }
523 label
524 id_found;
525 var
526 length : INT32;
527 i, ci, n, c, cc : int;
528 compptr : jpeg_component_info_ptr;
529 { Declare and initialize local copies of input pointer/count }
530 var
531 datasrc : jpeg_source_mgr_ptr;
532 next_input_byte : JOCTETptr; { Array[] of JOCTET; }
533 bytes_in_buffer : size_t;
534 begin
535 datasrc := cinfo^.src;
536 next_input_byte := datasrc^.next_input_byte;
537 bytes_in_buffer := datasrc^.bytes_in_buffer;
539 {}
541 if not cinfo^.marker^.saw_SOF then
542 ERREXIT(j_common_ptr(cinfo), JERR_SOS_NO_SOF);
544 { Read two bytes interpreted as an unsigned 16-bit integer.
545 length should be declared unsigned int or perhaps INT32. }
547 { make a byte available.
548 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
549 but we must reload the local copies after a successful fill. }
550 if (bytes_in_buffer = 0) then
551 begin
552 if (not datasrc^.fill_input_buffer(cinfo)) then
553 begin
554 get_sos := FALSE;
555 exit;
556 end;
557 { Reload the local copies }
558 next_input_byte := datasrc^.next_input_byte;
559 bytes_in_buffer := datasrc^.bytes_in_buffer;
560 end;
561 Dec( bytes_in_buffer );
563 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
564 Inc( next_input_byte );
565 { make a byte available.
566 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
567 but we must reload the local copies after a successful fill. }
568 if (bytes_in_buffer = 0) then
569 begin
570 if (not datasrc^.fill_input_buffer(cinfo)) then
571 begin
572 get_sos := FALSE;
573 exit;
574 end;
575 { Reload the local copies }
576 next_input_byte := datasrc^.next_input_byte;
577 bytes_in_buffer := datasrc^.bytes_in_buffer;
578 end;
579 Dec( bytes_in_buffer );
581 Inc( length, GETJOCTET( next_input_byte^));
582 Inc( next_input_byte );
585 { Read a byte into variable n (Number of components).
586 If must suspend, return FALSE. }
587 { make a byte available.
588 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
589 but we must reload the local copies after a successful fill. }
590 if (bytes_in_buffer = 0) then
591 begin
592 if (not datasrc^.fill_input_buffer(cinfo)) then
593 begin
594 get_sos := FALSE;
595 exit;
596 end;
597 { Reload the local copies }
598 next_input_byte := datasrc^.next_input_byte;
599 bytes_in_buffer := datasrc^.bytes_in_buffer;
600 end;
601 Dec( bytes_in_buffer );
603 n := GETJOCTET(next_input_byte^); { Number of components }
604 Inc(next_input_byte);
606 {$IFDEF DEBUG}
607 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_SOS, n);
608 {$ENDIF}
610 if ((length <> (n * 2 + 6)) or (n < 1) or (n > MAX_COMPS_IN_SCAN)) then
611 ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
613 cinfo^.comps_in_scan := n;
615 { Collect the component-spec parameters }
617 for i := 0 to Pred(n) do
618 begin
619 { Read a byte into variable cc. If must suspend, return FALSE. }
620 { make a byte available.
621 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
622 but we must reload the local copies after a successful fill. }
623 if (bytes_in_buffer = 0) then
624 begin
625 if (not datasrc^.fill_input_buffer(cinfo)) then
626 begin
627 get_sos := FALSE;
628 exit;
629 end;
630 { Reload the local copies }
631 next_input_byte := datasrc^.next_input_byte;
632 bytes_in_buffer := datasrc^.bytes_in_buffer;
633 end;
634 Dec( bytes_in_buffer );
636 cc := GETJOCTET(next_input_byte^);
637 Inc(next_input_byte);
639 { Read a byte into variable c. If must suspend, return FALSE. }
640 { make a byte available.
641 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
642 but we must reload the local copies after a successful fill. }
643 if (bytes_in_buffer = 0) then
644 begin
645 if (not datasrc^.fill_input_buffer(cinfo)) then
646 begin
647 get_sos := FALSE;
648 exit;
649 end;
650 { Reload the local copies }
651 next_input_byte := datasrc^.next_input_byte;
652 bytes_in_buffer := datasrc^.bytes_in_buffer;
653 end;
654 Dec( bytes_in_buffer );
656 c := GETJOCTET(next_input_byte^);
657 Inc(next_input_byte);
659 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
660 for ci := 0 to Pred(cinfo^.num_components) do
661 begin
662 if (cc = compptr^.component_id) then
663 goto id_found;
664 Inc(compptr);
665 end;
667 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_COMPONENT_ID, cc);
669 id_found:
671 cinfo^.cur_comp_info[i] := compptr;
672 compptr^.dc_tbl_no := (c shr 4) and 15;
673 compptr^.ac_tbl_no := (c ) and 15;
675 {$IFDEF DEBUG}
676 TRACEMS3(j_common_ptr(cinfo), 1, JTRC_SOS_COMPONENT, cc,
677 compptr^.dc_tbl_no, compptr^.ac_tbl_no);
678 {$ENDIF}
679 end;
681 { Collect the additional scan parameters Ss, Se, Ah/Al. }
682 { Read a byte into variable c. If must suspend, return FALSE. }
683 { make a byte available.
684 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
685 but we must reload the local copies after a successful fill. }
686 if (bytes_in_buffer = 0) then
687 begin
688 if (not datasrc^.fill_input_buffer(cinfo)) then
689 begin
690 get_sos := FALSE;
691 exit;
692 end;
693 { Reload the local copies }
694 next_input_byte := datasrc^.next_input_byte;
695 bytes_in_buffer := datasrc^.bytes_in_buffer;
696 end;
697 Dec( bytes_in_buffer );
699 c := GETJOCTET(next_input_byte^);
700 Inc(next_input_byte);
702 cinfo^.Ss := c;
704 { Read a byte into variable c. If must suspend, return FALSE. }
705 { make a byte available.
706 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
707 but we must reload the local copies after a successful fill. }
708 if (bytes_in_buffer = 0) then
709 begin
710 if (not datasrc^.fill_input_buffer(cinfo)) then
711 begin
712 get_sos := FALSE;
713 exit;
714 end;
715 { Reload the local copies }
716 next_input_byte := datasrc^.next_input_byte;
717 bytes_in_buffer := datasrc^.bytes_in_buffer;
718 end;
719 Dec( bytes_in_buffer );
721 c := GETJOCTET(next_input_byte^);
722 Inc(next_input_byte);
724 cinfo^.Se := c;
726 { Read a byte into variable c. If must suspend, return FALSE. }
727 { make a byte available.
728 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
729 but we must reload the local copies after a successful fill. }
730 if (bytes_in_buffer = 0) then
731 begin
732 if (not datasrc^.fill_input_buffer(cinfo)) then
733 begin
734 get_sos := FALSE;
735 exit;
736 end;
737 { Reload the local copies }
738 next_input_byte := datasrc^.next_input_byte;
739 bytes_in_buffer := datasrc^.bytes_in_buffer;
740 end;
741 Dec( bytes_in_buffer );
743 c := GETJOCTET(next_input_byte^);
744 Inc(next_input_byte);
746 cinfo^.Ah := (c shr 4) and 15;
747 cinfo^.Al := (c ) and 15;
749 {$IFDEF DEBUG}
750 TRACEMS4(j_common_ptr(cinfo), 1, JTRC_SOS_PARAMS, cinfo^.Ss, cinfo^.Se,
751 cinfo^.Ah, cinfo^.Al);
752 {$ENDIF}
754 { Prepare to scan data & restart markers }
755 cinfo^.marker^.next_restart_num := 0;
757 { Count another SOS marker }
758 Inc( cinfo^.input_scan_number );
760 { Unload the local copies --- do this only at a restart boundary }
761 datasrc^.next_input_byte := next_input_byte;
762 datasrc^.bytes_in_buffer := bytes_in_buffer;
764 get_sos := TRUE;
765 end; { get_sos }
768 {METHODDEF}
769 function skip_variable (cinfo : j_decompress_ptr) : boolean;
770 { Skip over an unknown or uninteresting variable-length marker }
771 var
772 length : INT32;
773 var
774 datasrc : jpeg_source_mgr_ptr;
775 next_input_byte : JOCTETptr; { Array[] of JOCTET; }
776 bytes_in_buffer : size_t;
777 begin
778 datasrc := cinfo^.src;
779 next_input_byte := datasrc^.next_input_byte;
780 bytes_in_buffer := datasrc^.bytes_in_buffer;
782 { Read two bytes interpreted as an unsigned 16-bit integer.
783 length should be declared unsigned int or perhaps INT32. }
785 { make a byte available.
786 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
787 but we must reload the local copies after a successful fill. }
788 if (bytes_in_buffer = 0) then
789 begin
790 if (not datasrc^.fill_input_buffer(cinfo)) then
791 begin
792 skip_variable := FALSE;
793 exit;
794 end;
795 { Reload the local copies }
796 next_input_byte := datasrc^.next_input_byte;
797 bytes_in_buffer := datasrc^.bytes_in_buffer;
798 end;
799 Dec( bytes_in_buffer );
801 length := uint(GETJOCTET(next_input_byte^)) shl 8;
802 Inc( next_input_byte );
803 { make a byte available.
804 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
805 but we must reload the local copies after a successful fill. }
806 if (bytes_in_buffer = 0) then
807 begin
808 if (not datasrc^.fill_input_buffer(cinfo)) then
809 begin
810 skip_variable := FALSE;
811 exit;
812 end;
813 { Reload the local copies }
814 next_input_byte := datasrc^.next_input_byte;
815 bytes_in_buffer := datasrc^.bytes_in_buffer;
816 end;
817 Dec( bytes_in_buffer );
819 Inc( length, GETJOCTET(next_input_byte^));
820 Inc( next_input_byte );
822 Dec(length, 2);
824 {$IFDEF DEBUG}
825 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER,
826 cinfo^.unread_marker, int(length));
827 {$ENDIF}
829 { Unload the local copies --- do this only at a restart boundary }
830 { do before skip_input_data }
831 datasrc^.next_input_byte := next_input_byte;
832 datasrc^.bytes_in_buffer := bytes_in_buffer;
834 if (length > 0) then
835 cinfo^.src^.skip_input_data(cinfo, long(length));
837 skip_variable := TRUE;
838 end; { skip_variable }
841 {$IFDEF D_ARITH_CODING_SUPPORTED}
843 {LOCAL}
844 function get_dac (cinfo : j_decompress_ptr) : boolean;
845 { Process a DAC marker }
846 var
847 length : INT32;
848 index, val : int;
849 var
850 datasrc : jpeg_source_mgr_ptr;
851 next_input_byte : JOCTETptr;
852 bytes_in_buffer : size_t;
853 begin
854 datasrc := cinfo^.src;
855 next_input_byte := datasrc^.next_input_byte;
856 bytes_in_buffer := datasrc^.bytes_in_buffer;
858 { Read two bytes interpreted as an unsigned 16-bit integer.
859 length should be declared unsigned int or perhaps INT32. }
861 { make a byte available.
862 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
863 but we must reload the local copies after a successful fill. }
864 if (bytes_in_buffer = 0) then
865 begin
866 if (not datasrc^.fill_input_buffer(cinfo)) then
867 begin
868 get_dac := FALSE;
869 exit;
870 end;
871 { Reload the local copies }
872 next_input_byte := datasrc^.next_input_byte;
873 bytes_in_buffer := datasrc^.bytes_in_buffer;
874 end;
875 Dec( bytes_in_buffer );
877 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
878 Inc( next_input_byte );
879 { make a byte available.
880 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
881 but we must reload the local copies after a successful fill. }
882 if (bytes_in_buffer = 0) then
883 begin
884 if (not datasrc^.fill_input_buffer(cinfo)) then
885 begin
886 get_dac := FALSE;
887 exit;
888 end;
889 { Reload the local copies }
890 next_input_byte := datasrc^.next_input_byte;
891 bytes_in_buffer := datasrc^.bytes_in_buffer;
892 end;
893 Dec( bytes_in_buffer );
895 Inc( length, GETJOCTET( next_input_byte^));
896 Inc( next_input_byte );
898 Dec(length, 2);
900 while (length > 0) do
901 begin
902 { Read a byte into variable index. If must suspend, return FALSE. }
903 { make a byte available.
904 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
905 but we must reload the local copies after a successful fill. }
906 if (bytes_in_buffer = 0) then
907 begin
908 if (not datasrc^.fill_input_buffer(cinfo)) then
909 begin
910 get_dac := FALSE;
911 exit;
912 end;
913 { Reload the local copies }
914 next_input_byte := datasrc^.next_input_byte;
915 bytes_in_buffer := datasrc^.bytes_in_buffer;
916 end;
917 Dec( bytes_in_buffer );
919 index := GETJOCTET(next_input_byte^);
920 Inc(next_input_byte);
922 { Read a byte into variable val. If must suspend, return FALSE. }
923 { make a byte available.
924 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
925 but we must reload the local copies after a successful fill. }
926 if (bytes_in_buffer = 0) then
927 begin
928 if (not datasrc^.fill_input_buffer(cinfo)) then
929 begin
930 get_dac := FALSE;
931 exit;
932 end;
933 { Reload the local copies }
934 next_input_byte := datasrc^.next_input_byte;
935 bytes_in_buffer := datasrc^.bytes_in_buffer;
936 end;
937 Dec( bytes_in_buffer );
939 val := GETJOCTET(next_input_byte^);
940 Inc(next_input_byte);
942 Dec( length, 2);
944 {$IFDEF DEBUG}
945 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_DAC, index, val);
946 {$ENDIF}
948 if (index < 0) or (index >= (2*NUM_ARITH_TBLS)) then
949 ERREXIT1(j_common_ptr(cinfo) , JERR_DAC_INDEX, index);
951 if (index >= NUM_ARITH_TBLS) then
952 begin { define AC table }
953 cinfo^.arith_ac_K[index-NUM_ARITH_TBLS] := UINT8(val);
954 end
955 else
956 begin { define DC table }
957 cinfo^.arith_dc_L[index] := UINT8(val and $0F);
958 cinfo^.arith_dc_U[index] := UINT8(val shr 4);
959 if (cinfo^.arith_dc_L[index] > cinfo^.arith_dc_U[index]) then
960 ERREXIT1(j_common_ptr(cinfo) , JERR_DAC_VALUE, val);
961 end;
962 end;
964 if (length <> 0) then
965 ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
967 { Unload the local copies --- do this only at a restart boundary }
968 datasrc^.next_input_byte := next_input_byte;
969 datasrc^.bytes_in_buffer := bytes_in_buffer;
971 get_dac := TRUE;
972 end; { get_dac }
974 {$ELSE}
976 {LOCAL}
977 function get_dac (cinfo : j_decompress_ptr) : boolean;
978 begin
979 get_dac := skip_variable(cinfo);
980 end;
982 {$ENDIF}
984 {LOCAL}
985 function get_dht (cinfo : j_decompress_ptr) : boolean;
986 { Process a DHT marker }
987 var
988 length : INT32;
989 bits : Array[0..17-1] of UINT8;
990 huffval : Array[0..256-1] of UINT8;
991 i, index, count : int;
992 htblptr : ^JHUFF_TBL_PTR;
993 var
994 datasrc : jpeg_source_mgr_ptr;
995 next_input_byte : JOCTETptr;
996 bytes_in_buffer : size_t;
997 begin
998 datasrc := cinfo^.src;
999 next_input_byte := datasrc^.next_input_byte;
1000 bytes_in_buffer := datasrc^.bytes_in_buffer;
1002 { Read two bytes interpreted as an unsigned 16-bit integer.
1003 length should be declared unsigned int or perhaps INT32. }
1005 { make a byte available.
1006 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1007 but we must reload the local copies after a successful fill. }
1008 if (bytes_in_buffer = 0) then
1009 begin
1010 if (not datasrc^.fill_input_buffer(cinfo)) then
1011 begin
1012 get_dht := FALSE;
1013 exit;
1014 end;
1015 { Reload the local copies }
1016 next_input_byte := datasrc^.next_input_byte;
1017 bytes_in_buffer := datasrc^.bytes_in_buffer;
1018 end;
1019 Dec( bytes_in_buffer );
1021 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
1022 Inc( next_input_byte );
1023 { make a byte available.
1024 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1025 but we must reload the local copies after a successful fill. }
1026 if (bytes_in_buffer = 0) then
1027 begin
1028 if (not datasrc^.fill_input_buffer(cinfo)) then
1029 begin
1030 get_dht := FALSE;
1031 exit;
1032 end;
1033 { Reload the local copies }
1034 next_input_byte := datasrc^.next_input_byte;
1035 bytes_in_buffer := datasrc^.bytes_in_buffer;
1036 end;
1037 Dec( bytes_in_buffer );
1039 Inc( length, GETJOCTET( next_input_byte^));
1040 Inc( next_input_byte );
1042 Dec(length, 2);
1044 while (length > 16) do
1045 begin
1046 { Read a byte into variable index. If must suspend, return FALSE. }
1047 { make a byte available.
1048 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1049 but we must reload the local copies after a successful fill. }
1050 if (bytes_in_buffer = 0) then
1051 begin
1052 if (not datasrc^.fill_input_buffer(cinfo)) then
1053 begin
1054 get_dht := FALSE;
1055 exit;
1056 end;
1057 { Reload the local copies }
1058 next_input_byte := datasrc^.next_input_byte;
1059 bytes_in_buffer := datasrc^.bytes_in_buffer;
1060 end;
1061 Dec( bytes_in_buffer );
1063 index := GETJOCTET(next_input_byte^);
1064 Inc(next_input_byte);
1066 {$IFDEF DEBUG}
1067 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_DHT, index);
1068 {$ENDIF}
1070 bits[0] := 0;
1071 count := 0;
1072 for i := 1 to 16 do
1073 begin
1074 { Read a byte into variable bits[i]. If must suspend, return FALSE. }
1075 { make a byte available.
1076 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1077 but we must reload the local copies after a successful fill. }
1078 if (bytes_in_buffer = 0) then
1079 begin
1080 if (not datasrc^.fill_input_buffer(cinfo)) then
1081 begin
1082 get_dht := FALSE;
1083 exit;
1084 end;
1085 { Reload the local copies }
1086 next_input_byte := datasrc^.next_input_byte;
1087 bytes_in_buffer := datasrc^.bytes_in_buffer;
1088 end;
1089 Dec( bytes_in_buffer );
1091 bits[i] := GETJOCTET(next_input_byte^);
1092 Inc(next_input_byte);
1094 Inc( count, bits[i] );
1095 end;
1097 Dec( length, (1 + 16) );
1099 {$IFDEF DEBUG}
1100 TRACEMS8(j_common_ptr(cinfo), 2, JTRC_HUFFBITS,
1101 bits[1], bits[2], bits[3], bits[4],
1102 bits[5], bits[6], bits[7], bits[8]);
1103 TRACEMS8(j_common_ptr(cinfo), 2, JTRC_HUFFBITS,
1104 bits[9], bits[10], bits[11], bits[12],
1105 bits[13], bits[14], bits[15], bits[16]);
1106 {$ENDIF}
1108 { Here we just do minimal validation of the counts to avoid walking
1109 off the end of our table space. jdhuff.c will check more carefully. }
1111 if (count > 256) or (INT32(count) > length) then
1112 ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
1114 for i := 0 to Pred(count) do
1115 begin
1116 { Read a byte into variable huffval[i]. If must suspend, return FALSE. }
1117 { make a byte available.
1118 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1119 but we must reload the local copies after a successful fill. }
1120 if (bytes_in_buffer = 0) then
1121 begin
1122 if (not datasrc^.fill_input_buffer(cinfo)) then
1123 begin
1124 get_dht := FALSE;
1125 exit;
1126 end;
1127 { Reload the local copies }
1128 next_input_byte := datasrc^.next_input_byte;
1129 bytes_in_buffer := datasrc^.bytes_in_buffer;
1130 end;
1131 Dec( bytes_in_buffer );
1133 huffval[i] := GETJOCTET(next_input_byte^);
1134 Inc(next_input_byte);
1135 end;
1137 Dec( length, count );
1139 if (index and $10)<>0 then
1140 begin { AC table definition }
1141 Dec( index, $10 );
1142 htblptr := @cinfo^.ac_huff_tbl_ptrs[index];
1143 end
1144 else
1145 begin { DC table definition }
1146 htblptr := @cinfo^.dc_huff_tbl_ptrs[index];
1147 end;
1149 if (index < 0) or (index >= NUM_HUFF_TBLS) then
1150 ERREXIT1(j_common_ptr(cinfo), JERR_DHT_INDEX, index);
1152 if (htblptr^ = NIL) then
1153 htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
1155 MEMCOPY(@(htblptr^)^.bits, @bits, SIZEOF((htblptr^)^.bits));
1156 MEMCOPY(@(htblptr^)^.huffval, @huffval, SIZEOF((htblptr^)^.huffval));
1157 end;
1159 if (length <> 0) then
1160 ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
1162 { Unload the local copies --- do this only at a restart boundary }
1163 datasrc^.next_input_byte := next_input_byte;
1164 datasrc^.bytes_in_buffer := bytes_in_buffer;
1166 get_dht := TRUE;
1167 end; { get_dht }
1170 {LOCAL}
1171 function get_dqt (cinfo : j_decompress_ptr) : boolean;
1172 { Process a DQT marker }
1173 var
1174 length : INT32;
1175 n, i, prec : int;
1176 tmp : uint;
1177 quant_ptr : JQUANT_TBL_PTR;
1178 var
1179 datasrc : jpeg_source_mgr_ptr;
1180 next_input_byte : JOCTETptr;
1181 bytes_in_buffer : size_t;
1182 begin
1183 datasrc := cinfo^.src;
1184 next_input_byte := datasrc^.next_input_byte;
1185 bytes_in_buffer := datasrc^.bytes_in_buffer;
1187 { Read two bytes interpreted as an unsigned 16-bit integer.
1188 length should be declared unsigned int or perhaps INT32. }
1190 { make a byte available.
1191 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1192 but we must reload the local copies after a successful fill. }
1193 if (bytes_in_buffer = 0) then
1194 begin
1195 if (not datasrc^.fill_input_buffer(cinfo)) then
1196 begin
1197 get_dqt := FALSE;
1198 exit;
1199 end;
1200 { Reload the local copies }
1201 next_input_byte := datasrc^.next_input_byte;
1202 bytes_in_buffer := datasrc^.bytes_in_buffer;
1203 end;
1204 Dec( bytes_in_buffer );
1206 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
1207 Inc( next_input_byte );
1208 { make a byte available.
1209 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1210 but we must reload the local copies after a successful fill. }
1211 if (bytes_in_buffer = 0) then
1212 begin
1213 if (not datasrc^.fill_input_buffer(cinfo)) then
1214 begin
1215 get_dqt := FALSE;
1216 exit;
1217 end;
1218 { Reload the local copies }
1219 next_input_byte := datasrc^.next_input_byte;
1220 bytes_in_buffer := datasrc^.bytes_in_buffer;
1221 end;
1222 Dec( bytes_in_buffer );
1224 Inc( length, GETJOCTET( next_input_byte^));
1225 Inc( next_input_byte );
1227 Dec( length, 2 );
1229 while (length > 0) do
1230 begin
1231 { Read a byte into variable n. If must suspend, return FALSE. }
1232 { make a byte available.
1233 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1234 but we must reload the local copies after a successful fill. }
1235 if (bytes_in_buffer = 0) then
1236 begin
1237 if (not datasrc^.fill_input_buffer(cinfo)) then
1238 begin
1239 get_dqt := FALSE;
1240 exit;
1241 end;
1242 { Reload the local copies }
1243 next_input_byte := datasrc^.next_input_byte;
1244 bytes_in_buffer := datasrc^.bytes_in_buffer;
1245 end;
1246 Dec( bytes_in_buffer );
1248 n := GETJOCTET(next_input_byte^);
1249 Inc(next_input_byte);
1251 prec := n shr 4;
1252 n := n and $0F;
1254 {$IFDEF DEBUG}
1255 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_DQT, n, prec);
1256 {$ENDIF}
1258 if (n >= NUM_QUANT_TBLS) then
1259 ERREXIT1(j_common_ptr(cinfo) , JERR_DQT_INDEX, n);
1261 if (cinfo^.quant_tbl_ptrs[n] = NIL) then
1262 cinfo^.quant_tbl_ptrs[n] := jpeg_alloc_quant_table(j_common_ptr(cinfo));
1263 quant_ptr := cinfo^.quant_tbl_ptrs[n];
1265 for i := 0 to Pred(DCTSIZE2) do
1266 begin
1267 if (prec <> 0) then
1268 begin
1269 { Read two bytes interpreted as an unsigned 16-bit integer.
1270 tmp should be declared unsigned int or perhaps INT32. }
1272 { make a byte available.
1273 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1274 but we must reload the local copies after a successful fill. }
1275 if (bytes_in_buffer = 0) then
1276 begin
1277 if (not datasrc^.fill_input_buffer(cinfo)) then
1278 begin
1279 get_dqt := FALSE;
1280 exit;
1281 end;
1282 { Reload the local copies }
1283 next_input_byte := datasrc^.next_input_byte;
1284 bytes_in_buffer := datasrc^.bytes_in_buffer;
1285 end;
1286 Dec( bytes_in_buffer );
1288 tmp := (uint( GETJOCTET(next_input_byte^)) shl 8);
1289 Inc( next_input_byte );
1290 { make a byte available.
1291 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1292 but we must reload the local copies after a successful fill. }
1293 if (bytes_in_buffer = 0) then
1294 begin
1295 if (not datasrc^.fill_input_buffer(cinfo)) then
1296 begin
1297 get_dqt := FALSE;
1298 exit;
1299 end;
1300 { Reload the local copies }
1301 next_input_byte := datasrc^.next_input_byte;
1302 bytes_in_buffer := datasrc^.bytes_in_buffer;
1303 end;
1304 Dec( bytes_in_buffer );
1306 Inc( tmp, GETJOCTET( next_input_byte^));
1307 Inc( next_input_byte );
1309 end
1310 else
1311 begin
1312 { Read a byte into variable tmp. If must suspend, return FALSE. }
1313 { make a byte available.
1314 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1315 but we must reload the local copies after a successful fill. }
1316 if (bytes_in_buffer = 0) then
1317 begin
1318 if (not datasrc^.fill_input_buffer(cinfo)) then
1319 begin
1320 get_dqt := FALSE;
1321 exit;
1322 end;
1323 { Reload the local copies }
1324 next_input_byte := datasrc^.next_input_byte;
1325 bytes_in_buffer := datasrc^.bytes_in_buffer;
1326 end;
1327 Dec( bytes_in_buffer );
1329 tmp := GETJOCTET(next_input_byte^);
1330 Inc(next_input_byte);
1331 end;
1333 { We convert the zigzag-order table to natural array order. }
1334 quant_ptr^.quantval[jpeg_natural_order[i]] := UINT16(tmp);
1335 end;
1337 if (cinfo^.err^.trace_level >= 2) then
1338 begin
1339 i := 0;
1340 while i < Pred(DCTSIZE2) do
1341 begin
1342 {$IFDEF DEBUG}
1343 TRACEMS8(j_common_ptr(cinfo), 2, JTRC_QUANTVALS,
1344 quant_ptr^.quantval[i], quant_ptr^.quantval[i+1],
1345 quant_ptr^.quantval[i+2], quant_ptr^.quantval[i+3],
1346 quant_ptr^.quantval[i+4], quant_ptr^.quantval[i+5],
1347 quant_ptr^.quantval[i+6], quant_ptr^.quantval[i+7]);
1348 {$ENDIF}
1349 Inc(i, 8);
1350 end;
1351 end;
1353 Dec( length, DCTSIZE2+1 );
1354 if (prec <> 0) then
1355 Dec( length, DCTSIZE2 );
1356 end;
1358 if (length <> 0) then
1359 ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
1361 { Unload the local copies --- do this only at a restart boundary }
1362 datasrc^.next_input_byte := next_input_byte;
1363 datasrc^.bytes_in_buffer := bytes_in_buffer;
1365 get_dqt := TRUE;
1366 end; { get_dqt }
1369 {LOCAL}
1370 function get_dri (cinfo : j_decompress_ptr) : boolean;
1371 { Process a DRI marker }
1372 var
1373 length : INT32;
1374 tmp : uint;
1375 var
1376 datasrc : jpeg_source_mgr_ptr;
1377 next_input_byte : JOCTETptr;
1378 bytes_in_buffer : size_t;
1379 begin
1380 datasrc := cinfo^.src;
1381 next_input_byte := datasrc^.next_input_byte;
1382 bytes_in_buffer := datasrc^.bytes_in_buffer;
1384 { Read two bytes interpreted as an unsigned 16-bit integer.
1385 length should be declared unsigned int or perhaps INT32. }
1387 { make a byte available.
1388 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1389 but we must reload the local copies after a successful fill. }
1390 if (bytes_in_buffer = 0) then
1391 begin
1392 if (not datasrc^.fill_input_buffer(cinfo)) then
1393 begin
1394 get_dri := FALSE;
1395 exit;
1396 end;
1397 { Reload the local copies }
1398 next_input_byte := datasrc^.next_input_byte;
1399 bytes_in_buffer := datasrc^.bytes_in_buffer;
1400 end;
1401 Dec( bytes_in_buffer );
1403 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
1404 Inc( next_input_byte );
1405 { make a byte available.
1406 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1407 but we must reload the local copies after a successful fill. }
1408 if (bytes_in_buffer = 0) then
1409 begin
1410 if (not datasrc^.fill_input_buffer(cinfo)) then
1411 begin
1412 get_dri := FALSE;
1413 exit;
1414 end;
1415 { Reload the local copies }
1416 next_input_byte := datasrc^.next_input_byte;
1417 bytes_in_buffer := datasrc^.bytes_in_buffer;
1418 end;
1419 Dec( bytes_in_buffer );
1421 Inc( length, GETJOCTET( next_input_byte^));
1422 Inc( next_input_byte );
1424 if (length <> 4) then
1425 ERREXIT(j_common_ptr(cinfo), JERR_BAD_LENGTH);
1427 { Read two bytes interpreted as an unsigned 16-bit integer.
1428 tmp should be declared unsigned int or perhaps INT32. }
1430 { make a byte available.
1431 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1432 but we must reload the local copies after a successful fill. }
1433 if (bytes_in_buffer = 0) then
1434 begin
1435 if (not datasrc^.fill_input_buffer(cinfo)) then
1436 begin
1437 get_dri := FALSE;
1438 exit;
1439 end;
1440 { Reload the local copies }
1441 next_input_byte := datasrc^.next_input_byte;
1442 bytes_in_buffer := datasrc^.bytes_in_buffer;
1443 end;
1444 Dec( bytes_in_buffer );
1446 tmp := (uint( GETJOCTET(next_input_byte^)) shl 8);
1447 Inc( next_input_byte );
1448 { make a byte available.
1449 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1450 but we must reload the local copies after a successful fill. }
1451 if (bytes_in_buffer = 0) then
1452 begin
1453 if (not datasrc^.fill_input_buffer(cinfo)) then
1454 begin
1455 get_dri := FALSE;
1456 exit;
1457 end;
1458 { Reload the local copies }
1459 next_input_byte := datasrc^.next_input_byte;
1460 bytes_in_buffer := datasrc^.bytes_in_buffer;
1461 end;
1462 Dec( bytes_in_buffer );
1464 Inc( tmp, GETJOCTET( next_input_byte^));
1465 Inc( next_input_byte );
1467 {$IFDEF DEBUG}
1468 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_DRI, tmp);
1469 {$ENDIF}
1471 cinfo^.restart_interval := tmp;
1473 { Unload the local copies --- do this only at a restart boundary }
1474 datasrc^.next_input_byte := next_input_byte;
1475 datasrc^.bytes_in_buffer := bytes_in_buffer;
1477 get_dri := TRUE;
1478 end; { get_dri }
1481 { Routines for processing APPn and COM markers.
1482 These are either saved in memory or discarded, per application request.
1483 APP0 and APP14 are specially checked to see if they are
1484 JFIF and Adobe markers, respectively. }
1486 const
1487 APP0_DATA_LEN = 14; { Length of interesting data in APP0 }
1488 APP14_DATA_LEN = 12; { Length of interesting data in APP14 }
1489 APPN_DATA_LEN = 14; { Must be the largest of the above!! }
1492 {LOCAL}
1493 procedure examine_app0 (cinfo : j_decompress_ptr;
1494 var data : array of JOCTET;
1495 datalen : uint;
1496 remaining : INT32);
1498 { Examine first few bytes from an APP0.
1499 Take appropriate action if it is a JFIF marker.
1500 datalen is # of bytes at data[], remaining is length of rest of marker data.
1502 {$IFDEF DEBUG}
1503 var
1504 totallen : INT32;
1505 {$ENDIF}
1506 begin
1507 {$IFDEF DEBUG}
1508 totallen := INT32(datalen) + remaining;
1509 {$ENDIF}
1510 if (datalen >= APP0_DATA_LEN) and
1511 (GETJOCTET(data[0]) = $4A) and
1512 (GETJOCTET(data[1]) = $46) and
1513 (GETJOCTET(data[2]) = $49) and
1514 (GETJOCTET(data[3]) = $46) and
1515 (GETJOCTET(data[4]) = 0) then
1516 begin
1517 { Found JFIF APP0 marker: save info }
1518 cinfo^.saw_JFIF_marker := TRUE;
1519 cinfo^.JFIF_major_version := GETJOCTET(data[5]);
1520 cinfo^.JFIF_minor_version := GETJOCTET(data[6]);
1521 cinfo^.density_unit := GETJOCTET(data[7]);
1522 cinfo^.X_density := (GETJOCTET(data[8]) shl 8) + GETJOCTET(data[9]);
1523 cinfo^.Y_density := (GETJOCTET(data[10]) shl 8) + GETJOCTET(data[11]);
1524 { Check version.
1525 Major version must be 1, anything else signals an incompatible change.
1526 (We used to treat this as an error, but now it's a nonfatal warning,
1527 because some bozo at Hijaak couldn't read the spec.)
1528 Minor version should be 0..2, but process anyway if newer. }
1530 if (cinfo^.JFIF_major_version <> 1) then
1531 WARNMS2(j_common_ptr(cinfo), JWRN_JFIF_MAJOR,
1532 cinfo^.JFIF_major_version, cinfo^.JFIF_minor_version);
1533 { Generate trace messages }
1534 {$IFDEF DEBUG}
1535 TRACEMS5(j_common_ptr(cinfo), 1, JTRC_JFIF,
1536 cinfo^.JFIF_major_version, cinfo^.JFIF_minor_version,
1537 cinfo^.X_density, cinfo^.Y_density, cinfo^.density_unit);
1538 { Validate thumbnail dimensions and issue appropriate messages }
1539 if (GETJOCTET(data[12]) or GETJOCTET(data[13])) <> 0 then
1540 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_THUMBNAIL,
1541 GETJOCTET(data[12]), GETJOCTET(data[13]));
1542 Dec(totallen, APP0_DATA_LEN);
1543 if (totallen <>
1544 ( INT32(GETJOCTET(data[12])) * INT32(GETJOCTET(data[13])) * INT32(3) )) then
1545 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_JFIF_BADTHUMBNAILSIZE, int(totallen));
1546 {$ENDIF}
1547 end
1548 else
1549 if (datalen >= 6) and
1550 (GETJOCTET(data[0]) = $4A) and
1551 (GETJOCTET(data[1]) = $46) and
1552 (GETJOCTET(data[2]) = $58) and
1553 (GETJOCTET(data[3]) = $58) and
1554 (GETJOCTET(data[4]) = 0) then
1555 begin
1556 { Found JFIF "JFXX" extension APP0 marker }
1557 { The library doesn't actually do anything with these,
1558 but we try to produce a helpful trace message. }
1559 {$IFDEF DEBUG}
1560 case (GETJOCTET(data[5])) of
1561 $10:
1562 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_JPEG, int(totallen));
1563 $11:
1564 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_PALETTE, int(totallen));
1565 $13:
1566 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_THUMB_RGB, int(totallen));
1567 else
1568 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_JFIF_EXTENSION,
1569 GETJOCTET(data[5]), int(totallen));
1570 end;
1571 {$ENDIF}
1572 end
1573 else
1574 begin
1575 { Start of APP0 does not match "JFIF" or "JFXX", or too short }
1576 {$IFDEF DEBUG}
1577 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP0, int(totallen));
1578 {$ENDIF}
1579 end;
1580 end;
1583 {LOCAL}
1584 procedure examine_app14 (cinfo : j_decompress_ptr;
1585 var data : array of JOCTET;
1586 datalen : uint;
1587 remaining : INT32);
1588 { Examine first few bytes from an APP14.
1589 Take appropriate action if it is an Adobe marker.
1590 datalen is # of bytes at data[], remaining is length of rest of marker data.
1592 var
1593 {$IFDEF DEBUG}
1594 version, flags0, flags1,
1595 {$ENDIF}
1596 transform : uint;
1597 begin
1598 if (datalen >= APP14_DATA_LEN) and
1599 (GETJOCTET(data[0]) = $41) and
1600 (GETJOCTET(data[1]) = $64) and
1601 (GETJOCTET(data[2]) = $6F) and
1602 (GETJOCTET(data[3]) = $62) and
1603 (GETJOCTET(data[4]) = $65) then
1604 begin
1605 { Found Adobe APP14 marker }
1606 {$IFDEF DEBUG}
1607 version := (GETJOCTET(data[5]) shl 8) + GETJOCTET(data[6]);
1608 flags0 := (GETJOCTET(data[7]) shl 8) + GETJOCTET(data[8]);
1609 flags1 := (GETJOCTET(data[9]) shl 8) + GETJOCTET(data[10]);
1610 {$ENDIF}
1611 transform := GETJOCTET(data[11]);
1612 {$IFDEF DEBUG}
1613 TRACEMS4(j_common_ptr(cinfo), 1, JTRC_ADOBE, version, flags0, flags1, transform);
1614 {$ENDIF}
1615 cinfo^.saw_Adobe_marker := TRUE;
1616 cinfo^.Adobe_transform := UINT8 (transform);
1617 end
1618 else
1619 begin
1620 { Start of APP14 does not match "Adobe", or too short }
1621 {$IFDEF DEBUG}
1622 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_APP14, int (datalen + remaining));
1623 {$ENDIF}
1624 end;
1625 end;
1628 {METHODDEF}
1629 function get_interesting_appn (cinfo : j_decompress_ptr) : boolean;
1630 { Process an APP0 or APP14 marker without saving it }
1631 var
1632 length : INT32;
1633 b : array[0..APPN_DATA_LEN-1] of JOCTET;
1634 i, numtoread: uint;
1635 var
1636 datasrc : jpeg_source_mgr_ptr;
1637 next_input_byte : JOCTETptr;
1638 bytes_in_buffer : size_t;
1639 begin
1640 datasrc := cinfo^.src;
1641 next_input_byte := datasrc^.next_input_byte;
1642 bytes_in_buffer := datasrc^.bytes_in_buffer;
1644 { Read two bytes interpreted as an unsigned 16-bit integer.
1645 length should be declared unsigned int or perhaps INT32. }
1647 { make a byte available.
1648 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1649 but we must reload the local copies after a successful fill. }
1650 if (bytes_in_buffer = 0) then
1651 begin
1652 if (not datasrc^.fill_input_buffer(cinfo)) then
1653 begin
1654 get_interesting_appn := FALSE;
1655 exit;
1656 end;
1657 { Reload the local copies }
1658 next_input_byte := datasrc^.next_input_byte;
1659 bytes_in_buffer := datasrc^.bytes_in_buffer;
1660 end;
1661 Dec( bytes_in_buffer );
1663 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
1664 Inc( next_input_byte );
1666 { make a byte available.
1667 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1668 but we must reload the local copies after a successful fill. }
1669 if (bytes_in_buffer = 0) then
1670 begin
1671 if (not datasrc^.fill_input_buffer(cinfo)) then
1672 begin
1673 get_interesting_appn := FALSE;
1674 exit;
1675 end;
1676 { Reload the local copies }
1677 next_input_byte := datasrc^.next_input_byte;
1678 bytes_in_buffer := datasrc^.bytes_in_buffer;
1679 end;
1680 Dec( bytes_in_buffer );
1682 Inc( length, GETJOCTET(next_input_byte^));
1683 Inc( next_input_byte );
1685 Dec(length, 2);
1687 { get the interesting part of the marker data }
1688 if (length >= APPN_DATA_LEN) then
1689 numtoread := APPN_DATA_LEN
1690 else
1691 if (length > 0) then
1692 numtoread := uint(length)
1693 else
1694 numtoread := 0;
1696 if numtoread > 0 then
1697 begin
1698 for i := 0 to numtoread-1 do
1699 begin
1700 { Read a byte into b[i]. If must suspend, return FALSE. }
1701 { make a byte available.
1702 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1703 but we must reload the local copies after a successful fill. }
1704 if (bytes_in_buffer = 0) then
1705 begin
1706 if (not datasrc^.fill_input_buffer(cinfo)) then
1707 begin
1708 get_interesting_appn := FALSE;
1709 exit;
1710 end;
1711 { Reload the local copies }
1712 next_input_byte := datasrc^.next_input_byte;
1713 bytes_in_buffer := datasrc^.bytes_in_buffer;
1714 end;
1715 Dec( bytes_in_buffer );
1717 b[i] := GETJOCTET(next_input_byte^);
1718 Inc(next_input_byte);
1719 end;
1720 end;
1722 Dec(length, numtoread);
1724 { process it }
1725 case (cinfo^.unread_marker) of
1726 M_APP0:
1727 examine_app0(cinfo, b, numtoread, length);
1728 M_APP14:
1729 examine_app14(cinfo, b, numtoread, length);
1730 else
1731 { can't get here unless jpeg_save_markers chooses wrong processor }
1732 ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, cinfo^.unread_marker);
1733 end;
1735 { skip any remaining data -- could be lots }
1737 { Unload the local copies --- do this only at a restart boundary }
1738 datasrc^.next_input_byte := next_input_byte;
1739 datasrc^.bytes_in_buffer := bytes_in_buffer;
1741 if (length > 0) then
1742 cinfo^.src^.skip_input_data(cinfo, long(length));
1744 get_interesting_appn := TRUE;
1745 end;
1747 {$ifdef SAVE_MARKERS_SUPPORTED}
1749 {METHODDEF}
1750 function save_marker (cinfo : j_decompress_ptr) : boolean;
1751 { Save an APPn or COM marker into the marker list }
1752 var
1753 marker : my_marker_ptr;
1754 cur_marker : jpeg_saved_marker_ptr;
1755 bytes_read, data_length : uint;
1756 data : JOCTET_FIELD_PTR;
1757 length : INT32;
1758 var
1759 datasrc : jpeg_source_mgr_ptr;
1760 next_input_byte : JOCTETptr;
1761 bytes_in_buffer : size_t;
1762 var
1763 limit : uint;
1764 var
1765 prev : jpeg_saved_marker_ptr;
1766 begin
1767 { local copies of input pointer/count }
1768 datasrc := cinfo^.src;
1769 next_input_byte := datasrc^.next_input_byte;
1770 bytes_in_buffer := datasrc^.bytes_in_buffer;
1772 marker := my_marker_ptr(cinfo^.marker);
1773 cur_marker := marker^.cur_marker;
1774 length := 0;
1776 if (cur_marker = NIL) then
1777 begin
1778 { begin reading a marker }
1779 { Read two bytes interpreted as an unsigned 16-bit integer. }
1781 { make a byte available.
1782 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1783 but we must reload the local copies after a successful fill. }
1784 if (bytes_in_buffer = 0) then
1785 begin
1786 if (not datasrc^.fill_input_buffer(cinfo)) then
1787 begin
1788 save_marker := FALSE;
1789 exit;
1790 end;
1791 { Reload the local copies }
1792 next_input_byte := datasrc^.next_input_byte;
1793 bytes_in_buffer := datasrc^.bytes_in_buffer;
1794 end;
1795 Dec( bytes_in_buffer );
1797 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
1798 Inc( next_input_byte );
1800 { make a byte available.
1801 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1802 but we must reload the local copies after a successful fill. }
1803 if (bytes_in_buffer = 0) then
1804 begin
1805 if (not datasrc^.fill_input_buffer(cinfo)) then
1806 begin
1807 save_marker := FALSE;
1808 exit;
1809 end;
1810 { Reload the local copies }
1811 next_input_byte := datasrc^.next_input_byte;
1812 bytes_in_buffer := datasrc^.bytes_in_buffer;
1813 end;
1814 Dec( bytes_in_buffer );
1816 Inc( length, GETJOCTET(next_input_byte^));
1817 Inc( next_input_byte );
1819 Dec(length, 2);
1820 if (length >= 0) then
1821 begin { watch out for bogus length word }
1822 { figure out how much we want to save }
1824 if (cinfo^.unread_marker = int(M_COM)) then
1825 limit := marker^.length_limit_COM
1826 else
1827 limit := marker^.length_limit_APPn[cinfo^.unread_marker - int(M_APP0)];
1828 if (uint(length) < limit) then
1829 limit := uint(length);
1830 { allocate and initialize the marker item }
1831 cur_marker := jpeg_saved_marker_ptr(
1832 cinfo^.mem^.alloc_large (j_common_ptr(cinfo), JPOOL_IMAGE,
1833 SIZEOF(jpeg_marker_struct) + limit) );
1834 cur_marker^.next := NIL;
1835 cur_marker^.marker := UINT8 (cinfo^.unread_marker);
1836 cur_marker^.original_length := uint(length);
1837 cur_marker^.data_length := limit;
1838 { data area is just beyond the jpeg_marker_struct }
1839 cur_marker^.data := JOCTET_FIELD_PTR(cur_marker);
1840 Inc(jpeg_saved_marker_ptr(cur_marker^.data));
1841 data := cur_marker^.data;
1843 marker^.cur_marker := cur_marker;
1844 marker^.bytes_read := 0;
1845 bytes_read := 0;
1846 data_length := limit;
1847 end
1848 else
1849 begin
1850 { deal with bogus length word }
1851 data_length := 0;
1852 bytes_read := 0;
1853 data := NIL;
1854 end
1855 end
1856 else
1857 begin
1858 { resume reading a marker }
1859 bytes_read := marker^.bytes_read;
1860 data_length := cur_marker^.data_length;
1861 data := cur_marker^.data;
1862 Inc(data, bytes_read);
1863 end;
1865 while (bytes_read < data_length) do
1866 begin
1867 { move the restart point to here }
1868 datasrc^.next_input_byte := next_input_byte;
1869 datasrc^.bytes_in_buffer := bytes_in_buffer;
1871 marker^.bytes_read := bytes_read;
1872 { If there's not at least one byte in buffer, suspend }
1873 if (bytes_in_buffer = 0) then
1874 begin
1875 if not datasrc^.fill_input_buffer (cinfo) then
1876 begin
1877 save_marker := FALSE;
1878 exit;
1879 end;
1880 next_input_byte := datasrc^.next_input_byte;
1881 bytes_in_buffer := datasrc^.bytes_in_buffer;
1882 end;
1884 { Copy bytes with reasonable rapidity }
1885 while (bytes_read < data_length) and (bytes_in_buffer > 0) do
1886 begin
1887 JOCTETPTR(data)^ := next_input_byte^;
1888 Inc(JOCTETPTR(data));
1889 Inc(next_input_byte);
1890 Dec(bytes_in_buffer);
1891 Inc(bytes_read);
1892 end;
1893 end;
1895 { Done reading what we want to read }
1896 if (cur_marker <> NIL) then
1897 begin { will be NIL if bogus length word }
1898 { Add new marker to end of list }
1899 if (cinfo^.marker_list = NIL) then
1900 begin
1901 cinfo^.marker_list := cur_marker
1902 end
1903 else
1904 begin
1905 prev := cinfo^.marker_list;
1906 while (prev^.next <> NIL) do
1907 prev := prev^.next;
1908 prev^.next := cur_marker;
1909 end;
1910 { Reset pointer & calc remaining data length }
1911 data := cur_marker^.data;
1912 length := cur_marker^.original_length - data_length;
1913 end;
1914 { Reset to initial state for next marker }
1915 marker^.cur_marker := NIL;
1917 { Process the marker if interesting; else just make a generic trace msg }
1918 case (cinfo^.unread_marker) of
1919 M_APP0:
1920 examine_app0(cinfo, data^, data_length, length);
1921 M_APP14:
1922 examine_app14(cinfo, data^, data_length, length);
1923 else
1924 {$IFDEF DEBUG}
1925 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER, cinfo^.unread_marker,
1926 int(data_length + length));
1927 {$ENDIF}
1928 end;
1930 { skip any remaining data -- could be lots }
1931 { do before skip_input_data }
1932 datasrc^.next_input_byte := next_input_byte;
1933 datasrc^.bytes_in_buffer := bytes_in_buffer;
1935 if (length > 0) then
1936 cinfo^.src^.skip_input_data (cinfo, long(length) );
1938 save_marker := TRUE;
1939 end;
1941 {$endif} { SAVE_MARKERS_SUPPORTED }
1944 { Find the next JPEG marker, save it in cinfo^.unread_marker.
1945 Returns FALSE if had to suspend before reaching a marker;
1946 in that case cinfo^.unread_marker is unchanged.
1948 Note that the result might not be a valid marker code,
1949 but it will never be 0 or FF. }
1951 {LOCAL}
1952 function next_marker (cinfo : j_decompress_ptr) : boolean;
1953 var
1954 c : int;
1955 var
1956 datasrc : jpeg_source_mgr_ptr;
1957 next_input_byte : JOCTETptr;
1958 bytes_in_buffer : size_t;
1959 begin
1960 datasrc := cinfo^.src;
1961 next_input_byte := datasrc^.next_input_byte;
1962 bytes_in_buffer := datasrc^.bytes_in_buffer;
1964 {while TRUE do}
1965 repeat
1966 { Read a byte into variable c. If must suspend, return FALSE. }
1967 { make a byte available.
1968 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1969 but we must reload the local copies after a successful fill. }
1970 if (bytes_in_buffer = 0) then
1971 begin
1972 if (not datasrc^.fill_input_buffer(cinfo)) then
1973 begin
1974 next_marker := FALSE;
1975 exit;
1976 end;
1977 { Reload the local copies }
1978 next_input_byte := datasrc^.next_input_byte;
1979 bytes_in_buffer := datasrc^.bytes_in_buffer;
1980 end;
1981 Dec( bytes_in_buffer );
1983 c := GETJOCTET(next_input_byte^);
1984 Inc(next_input_byte);
1986 { Skip any non-FF bytes.
1987 This may look a bit inefficient, but it will not occur in a valid file.
1988 We sync after each discarded byte so that a suspending data source
1989 can discard the byte from its buffer. }
1991 while (c <> $FF) do
1992 begin
1993 Inc(cinfo^.marker^.discarded_bytes);
1994 { Unload the local copies --- do this only at a restart boundary }
1995 datasrc^.next_input_byte := next_input_byte;
1996 datasrc^.bytes_in_buffer := bytes_in_buffer;
1998 { Read a byte into variable c. If must suspend, return FALSE. }
1999 { make a byte available.
2000 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2001 but we must reload the local copies after a successful fill. }
2002 if (bytes_in_buffer = 0) then
2003 begin
2004 if (not datasrc^.fill_input_buffer(cinfo)) then
2005 begin
2006 next_marker := FALSE;
2007 exit;
2008 end;
2009 { Reload the local copies }
2010 next_input_byte := datasrc^.next_input_byte;
2011 bytes_in_buffer := datasrc^.bytes_in_buffer;
2012 end;
2013 Dec( bytes_in_buffer );
2015 c := GETJOCTET(next_input_byte^);
2016 Inc(next_input_byte);
2018 end;
2019 { This loop swallows any duplicate FF bytes. Extra FFs are legal as
2020 pad bytes, so don't count them in discarded_bytes. We assume there
2021 will not be so many consecutive FF bytes as to overflow a suspending
2022 data source's input buffer. }
2024 repeat
2025 { Read a byte into variable c. If must suspend, return FALSE. }
2026 { make a byte available.
2027 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2028 but we must reload the local copies after a successful fill. }
2029 if (bytes_in_buffer = 0) then
2030 begin
2031 if (not datasrc^.fill_input_buffer(cinfo)) then
2032 begin
2033 next_marker := FALSE;
2034 exit;
2035 end;
2036 { Reload the local copies }
2037 next_input_byte := datasrc^.next_input_byte;
2038 bytes_in_buffer := datasrc^.bytes_in_buffer;
2039 end;
2040 Dec( bytes_in_buffer );
2042 c := GETJOCTET(next_input_byte^);
2043 Inc(next_input_byte);
2044 Until (c <> $FF);
2045 if (c <> 0) then
2046 break; { found a valid marker, exit loop }
2047 { Reach here if we found a stuffed-zero data sequence (FF/00).
2048 Discard it and loop back to try again. }
2050 Inc(cinfo^.marker^.discarded_bytes, 2);
2051 { Unload the local copies --- do this only at a restart boundary }
2052 datasrc^.next_input_byte := next_input_byte;
2053 datasrc^.bytes_in_buffer := bytes_in_buffer;
2054 Until False;
2056 if (cinfo^.marker^.discarded_bytes <> 0) then
2057 begin
2058 WARNMS2(j_common_ptr(cinfo), JWRN_EXTRANEOUS_DATA,
2059 cinfo^.marker^.discarded_bytes, c);
2060 cinfo^.marker^.discarded_bytes := 0;
2061 end;
2063 cinfo^.unread_marker := c;
2065 { Unload the local copies --- do this only at a restart boundary }
2066 datasrc^.next_input_byte := next_input_byte;
2067 datasrc^.bytes_in_buffer := bytes_in_buffer;
2069 next_marker := TRUE;
2070 end; { next_marker }
2073 {LOCAL}
2074 function first_marker (cinfo : j_decompress_ptr) : boolean;
2075 { Like next_marker, but used to obtain the initial SOI marker. }
2076 { For this marker, we do not allow preceding garbage or fill; otherwise,
2077 we might well scan an entire input file before realizing it ain't JPEG.
2078 If an application wants to process non-JFIF files, it must seek to the
2079 SOI before calling the JPEG library. }
2080 var
2081 c, c2 : int;
2082 var
2083 datasrc : jpeg_source_mgr_ptr;
2084 next_input_byte : JOCTETptr;
2085 bytes_in_buffer : size_t;
2086 begin
2087 datasrc := cinfo^.src;
2088 next_input_byte := datasrc^.next_input_byte;
2089 bytes_in_buffer := datasrc^.bytes_in_buffer;
2091 { Read a byte into variable c. If must suspend, return FALSE. }
2092 { make a byte available.
2093 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2094 but we must reload the local copies after a successful fill. }
2095 if (bytes_in_buffer = 0) then
2096 begin
2097 if (not datasrc^.fill_input_buffer(cinfo)) then
2098 begin
2099 first_marker := FALSE;
2100 exit;
2101 end;
2102 { Reload the local copies }
2103 next_input_byte := datasrc^.next_input_byte;
2104 bytes_in_buffer := datasrc^.bytes_in_buffer;
2105 end;
2106 Dec( bytes_in_buffer );
2108 c := GETJOCTET(next_input_byte^);
2109 Inc(next_input_byte);
2111 { Read a byte into variable c2. If must suspend, return FALSE. }
2112 { make a byte available.
2113 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2114 but we must reload the local copies after a successful fill. }
2115 if (bytes_in_buffer = 0) then
2116 begin
2117 if (not datasrc^.fill_input_buffer(cinfo)) then
2118 begin
2119 first_marker := FALSE;
2120 exit;
2121 end;
2122 { Reload the local copies }
2123 next_input_byte := datasrc^.next_input_byte;
2124 bytes_in_buffer := datasrc^.bytes_in_buffer;
2125 end;
2126 Dec( bytes_in_buffer );
2128 c2 := GETJOCTET(next_input_byte^);
2129 Inc(next_input_byte);
2131 if (c <> $FF) or (c2 <> int(M_SOI)) then
2132 ERREXIT2(j_common_ptr(cinfo), JERR_NO_SOI, c, c2);
2134 cinfo^.unread_marker := c2;
2136 { Unload the local copies --- do this only at a restart boundary }
2137 datasrc^.next_input_byte := next_input_byte;
2138 datasrc^.bytes_in_buffer := bytes_in_buffer;
2140 first_marker := TRUE;
2141 end; { first_marker }
2144 { Read markers until SOS or EOI.
2146 Returns same codes as are defined for jpeg_consume_input:
2147 JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. }
2149 {METHODDEF}
2150 function read_markers (cinfo : j_decompress_ptr) : int;
2151 begin
2152 { Outer loop repeats once for each marker. }
2153 repeat
2154 { Collect the marker proper, unless we already did. }
2155 { NB: first_marker() enforces the requirement that SOI appear first. }
2156 if (cinfo^.unread_marker = 0) then
2157 begin
2158 if not cinfo^.marker^.saw_SOI then
2159 begin
2160 if not first_marker(cinfo) then
2161 begin
2162 read_markers := JPEG_SUSPENDED;
2163 exit;
2164 end;
2165 end
2166 else
2167 begin
2168 if not next_marker(cinfo) then
2169 begin
2170 read_markers := JPEG_SUSPENDED;
2171 exit;
2172 end;
2173 end;
2174 end;
2175 { At this point cinfo^.unread_marker contains the marker code and the
2176 input point is just past the marker proper, but before any parameters.
2177 A suspension will cause us to return with this state still true. }
2179 case (cinfo^.unread_marker) of
2180 M_SOI:
2181 if not get_soi(cinfo) then
2182 begin
2183 read_markers := JPEG_SUSPENDED;
2184 exit;
2185 end;
2187 M_SOF0, { Baseline }
2188 M_SOF1: { Extended sequential, Huffman }
2189 if not get_sof(cinfo, FALSE, FALSE) then
2190 begin
2191 read_markers := JPEG_SUSPENDED;
2192 exit;
2193 end;
2194 M_SOF2: { Progressive, Huffman }
2195 if not get_sof(cinfo, TRUE, FALSE) then
2196 begin
2197 read_markers := JPEG_SUSPENDED;
2198 exit;
2199 end;
2201 M_SOF9: { Extended sequential, arithmetic }
2202 if not get_sof(cinfo, FALSE, TRUE) then
2203 begin
2204 read_markers := JPEG_SUSPENDED;
2205 exit;
2206 end;
2208 M_SOF10: { Progressive, arithmetic }
2209 if not get_sof(cinfo, TRUE, TRUE) then
2210 begin
2211 read_markers := JPEG_SUSPENDED;
2212 exit;
2213 end;
2215 { Currently unsupported SOFn types }
2216 M_SOF3, { Lossless, Huffman }
2217 M_SOF5, { Differential sequential, Huffman }
2218 M_SOF6, { Differential progressive, Huffman }
2219 M_SOF7, { Differential lossless, Huffman }
2220 M_JPG, { Reserved for JPEG extensions }
2221 M_SOF11, { Lossless, arithmetic }
2222 M_SOF13, { Differential sequential, arithmetic }
2223 M_SOF14, { Differential progressive, arithmetic }
2224 M_SOF15: { Differential lossless, arithmetic }
2225 ERREXIT1(j_common_ptr(cinfo), JERR_SOF_UNSUPPORTED, cinfo^.unread_marker);
2227 M_SOS:
2228 begin
2229 if not get_sos(cinfo) then
2230 begin
2231 read_markers := JPEG_SUSPENDED;
2232 exit;
2233 end;
2234 cinfo^.unread_marker := 0; { processed the marker }
2235 read_markers := JPEG_REACHED_SOS;
2236 exit;
2237 end;
2239 M_EOI:
2240 begin
2241 {$IFDEF DEBUG}
2242 TRACEMS(j_common_ptr(cinfo), 1, JTRC_EOI);
2243 {$ENDIF}
2244 cinfo^.unread_marker := 0; { processed the marker }
2245 read_markers := JPEG_REACHED_EOI;
2246 exit;
2247 end;
2249 M_DAC:
2250 if not get_dac(cinfo) then
2251 begin
2252 read_markers := JPEG_SUSPENDED;
2253 exit;
2254 end;
2256 M_DHT:
2257 if not get_dht(cinfo) then
2258 begin
2259 read_markers := JPEG_SUSPENDED;
2260 exit;
2261 end;
2263 M_DQT:
2264 if not get_dqt(cinfo) then
2265 begin
2266 read_markers := JPEG_SUSPENDED;
2267 exit;
2268 end;
2270 M_DRI:
2271 if not get_dri(cinfo) then
2272 begin
2273 read_markers := JPEG_SUSPENDED;
2274 exit;
2275 end;
2277 M_APP0,
2278 M_APP1,
2279 M_APP2,
2280 M_APP3,
2281 M_APP4,
2282 M_APP5,
2283 M_APP6,
2284 M_APP7,
2285 M_APP8,
2286 M_APP9,
2287 M_APP10,
2288 M_APP11,
2289 M_APP12,
2290 M_APP13,
2291 M_APP14,
2292 M_APP15:
2293 if not my_marker_ptr(cinfo^.marker)^.
2294 process_APPn[cinfo^.unread_marker - int(M_APP0)](cinfo) then
2295 begin
2296 read_markers := JPEG_SUSPENDED;
2297 exit;
2298 end;
2300 M_COM:
2301 if not my_marker_ptr(cinfo^.marker)^.process_COM (cinfo) then
2302 begin
2303 read_markers := JPEG_SUSPENDED;
2304 exit;
2305 end;
2307 M_RST0, { these are all parameterless }
2308 M_RST1,
2309 M_RST2,
2310 M_RST3,
2311 M_RST4,
2312 M_RST5,
2313 M_RST6,
2314 M_RST7,
2315 M_TEM:
2316 {$IFDEF DEBUG}
2317 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_PARMLESS_MARKER,
2318 cinfo^.unread_marker)
2319 {$ENDIF}
2322 M_DNL: { Ignore DNL ... perhaps the wrong thing }
2323 if not skip_variable(cinfo) then
2324 begin
2325 read_markers := JPEG_SUSPENDED;
2326 exit;
2327 end;
2329 else { must be DHP, EXP, JPGn, or RESn }
2330 { For now, we treat the reserved markers as fatal errors since they are
2331 likely to be used to signal incompatible JPEG Part 3 extensions.
2332 Once the JPEG 3 version-number marker is well defined, this code
2333 ought to change! }
2334 ERREXIT1(j_common_ptr(cinfo) , JERR_UNKNOWN_MARKER,
2335 cinfo^.unread_marker);
2336 end; { end of case }
2337 { Successfully processed marker, so reset state variable }
2338 cinfo^.unread_marker := 0;
2339 Until false;
2340 end; { read_markers }
2343 { Read a restart marker, which is expected to appear next in the datastream;
2344 if the marker is not there, take appropriate recovery action.
2345 Returns FALSE if suspension is required.
2347 This is called by the entropy decoder after it has read an appropriate
2348 number of MCUs. cinfo^.unread_marker may be nonzero if the entropy decoder
2349 has already read a marker from the data source. Under normal conditions
2350 cinfo^.unread_marker will be reset to 0 before returning; if not reset,
2351 it holds a marker which the decoder will be unable to read past. }
2353 {METHODDEF}
2354 function read_restart_marker (cinfo : j_decompress_ptr) :boolean;
2355 begin
2356 { Obtain a marker unless we already did. }
2357 { Note that next_marker will complain if it skips any data. }
2358 if (cinfo^.unread_marker = 0) then
2359 begin
2360 if not next_marker(cinfo) then
2361 begin
2362 read_restart_marker := FALSE;
2363 exit;
2364 end;
2365 end;
2367 if (cinfo^.unread_marker = (int(M_RST0) + cinfo^.marker^.next_restart_num)) then
2368 begin
2369 { Normal case --- swallow the marker and let entropy decoder continue }
2370 {$IFDEF DEBUG}
2371 TRACEMS1(j_common_ptr(cinfo), 3, JTRC_RST,
2372 cinfo^.marker^.next_restart_num);
2373 {$ENDIF}
2374 cinfo^.unread_marker := 0;
2375 end
2376 else
2377 begin
2378 { Uh-oh, the restart markers have been messed up. }
2379 { Let the data source manager determine how to resync. }
2380 if not cinfo^.src^.resync_to_restart(cinfo,
2381 cinfo^.marker^.next_restart_num) then
2382 begin
2383 read_restart_marker := FALSE;
2384 exit;
2385 end;
2386 end;
2388 { Update next-restart state }
2389 with cinfo^.marker^ do
2390 next_restart_num := (next_restart_num + 1) and 7;
2392 read_restart_marker := TRUE;
2393 end; { read_restart_marker }
2396 { This is the default resync_to_restart method for data source managers
2397 to use if they don't have any better approach. Some data source managers
2398 may be able to back up, or may have additional knowledge about the data
2399 which permits a more intelligent recovery strategy; such managers would
2400 presumably supply their own resync method.
2402 read_restart_marker calls resync_to_restart if it finds a marker other than
2403 the restart marker it was expecting. (This code is *not* used unless
2404 a nonzero restart interval has been declared.) cinfo^.unread_marker is
2405 the marker code actually found (might be anything, except 0 or FF).
2406 The desired restart marker number (0..7) is passed as a parameter.
2407 This routine is supposed to apply whatever error recovery strategy seems
2408 appropriate in order to position the input stream to the next data segment.
2409 Note that cinfo^.unread_marker is treated as a marker appearing before
2410 the current data-source input point; usually it should be reset to zero
2411 before returning.
2412 Returns FALSE if suspension is required.
2414 This implementation is substantially constrained by wanting to treat the
2415 input as a data stream; this means we can't back up. Therefore, we have
2416 only the following actions to work with:
2417 1. Simply discard the marker and let the entropy decoder resume at next
2418 byte of file.
2419 2. Read forward until we find another marker, discarding intervening
2420 data. (In theory we could look ahead within the current bufferload,
2421 without having to discard data if we don't find the desired marker.
2422 This idea is not implemented here, in part because it makes behavior
2423 dependent on buffer size and chance buffer-boundary positions.)
2424 3. Leave the marker unread (by failing to zero cinfo^.unread_marker).
2425 This will cause the entropy decoder to process an empty data segment,
2426 inserting dummy zeroes, and then we will reprocess the marker.
2428 #2 is appropriate if we think the desired marker lies ahead, while #3 is
2429 appropriate if the found marker is a future restart marker (indicating
2430 that we have missed the desired restart marker, probably because it got
2431 corrupted).
2432 We apply #2 or #3 if the found marker is a restart marker no more than
2433 two counts behind or ahead of the expected one. We also apply #2 if the
2434 found marker is not a legal JPEG marker code (it's certainly bogus data).
2435 If the found marker is a restart marker more than 2 counts away, we do #1
2436 (too much risk that the marker is erroneous; with luck we will be able to
2437 resync at some future point).
2438 For any valid non-restart JPEG marker, we apply #3. This keeps us from
2439 overrunning the end of a scan. An implementation limited to single-scan
2440 files might find it better to apply #2 for markers other than EOI, since
2441 any other marker would have to be bogus data in that case. }
2444 {GLOBAL}
2445 function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
2446 desired : int) : boolean;
2447 var
2448 marker : int;
2449 action : int;
2450 begin
2451 marker := cinfo^.unread_marker;
2452 //action := 1; { never used }
2453 { Always put up a warning. }
2454 WARNMS2(j_common_ptr(cinfo), JWRN_MUST_RESYNC, marker, desired);
2456 { Outer loop handles repeated decision after scanning forward. }
2457 repeat
2458 if (marker < int(M_SOF0)) then
2459 action := 2 { invalid marker }
2460 else
2461 if (marker < int(M_RST0)) or (marker > int(M_RST7)) then
2462 action := 3 { valid non-restart marker }
2463 else
2464 begin
2465 if (marker = (int(M_RST0) + ((desired+1) and 7))) or
2466 (marker = (int(M_RST0) + ((desired+2) and 7))) then
2467 action := 3 { one of the next two expected restarts }
2468 else
2469 if (marker = (int(M_RST0) + ((desired-1) and 7))) or
2470 (marker = (int(M_RST0) + ((desired-2) and 7))) then
2471 action := 2 { a prior restart, so advance }
2472 else
2473 action := 1; { desired restart or too far away }
2474 end;
2476 {$IFDEF DEBUG}
2477 TRACEMS2(j_common_ptr(cinfo), 4, JTRC_RECOVERY_ACTION, marker, action);
2478 {$ENDIF}
2479 case action of
2480 1:
2481 { Discard marker and let entropy decoder resume processing. }
2482 begin
2483 cinfo^.unread_marker := 0;
2484 jpeg_resync_to_restart := TRUE;
2485 exit;
2486 end;
2487 2:
2488 { Scan to the next marker, and repeat the decision loop. }
2489 begin
2490 if not next_marker(cinfo) then
2491 begin
2492 jpeg_resync_to_restart := FALSE;
2493 exit;
2494 end;
2495 marker := cinfo^.unread_marker;
2496 end;
2497 3:
2498 { Return without advancing past this marker. }
2499 { Entropy decoder will be forced to process an empty segment. }
2500 begin
2501 jpeg_resync_to_restart := TRUE;
2502 exit;
2503 end;
2504 end; { case }
2505 Until false; { end loop }
2506 end; { jpeg_resync_to_restart }
2509 { Reset marker processing state to begin a fresh datastream. }
2511 {METHODDEF}
2512 procedure reset_marker_reader (cinfo : j_decompress_ptr);
2513 var
2514 marker : my_marker_ptr;
2515 begin
2516 marker := my_marker_ptr (cinfo^.marker);
2517 with cinfo^ do
2518 begin
2519 comp_info := NIL; { until allocated by get_sof }
2520 input_scan_number := 0; { no SOS seen yet }
2521 unread_marker := 0; { no pending marker }
2522 end;
2523 marker^.pub.saw_SOI := FALSE; { set internal state too }
2524 marker^.pub.saw_SOF := FALSE;
2525 marker^.pub.discarded_bytes := 0;
2526 marker^.cur_marker := NIL;
2527 end; { reset_marker_reader }
2530 { Initialize the marker reader module.
2531 This is called only once, when the decompression object is created. }
2533 {GLOBAL}
2534 procedure jinit_marker_reader (cinfo : j_decompress_ptr);
2535 var
2536 marker : my_marker_ptr;
2537 i : int;
2538 begin
2539 { Create subobject in permanent pool }
2540 marker := my_marker_ptr(
2541 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
2542 SIZEOF(my_marker_reader))
2543 );
2544 cinfo^.marker := jpeg_marker_reader_ptr(marker);
2545 { Initialize method pointers }
2546 marker^.pub.reset_marker_reader := reset_marker_reader;
2547 marker^.pub.read_markers := read_markers;
2548 marker^.pub.read_restart_marker := read_restart_marker;
2549 { Initialize COM/APPn processing.
2550 By default, we examine and then discard APP0 and APP14,
2551 but simply discard COM and all other APPn. }
2553 marker^.process_COM := skip_variable;
2554 marker^.length_limit_COM := 0;
2555 for i := 0 to 16-1 do
2556 begin
2557 marker^.process_APPn[i] := skip_variable;
2558 marker^.length_limit_APPn[i] := 0;
2559 end;
2560 marker^.process_APPn[0] := get_interesting_appn;
2561 marker^.process_APPn[14] := get_interesting_appn;
2562 { Reset marker processing state }
2563 reset_marker_reader(cinfo);
2564 end; { jinit_marker_reader }
2567 { Control saving of COM and APPn markers into marker_list. }
2570 {$ifdef SAVE_MARKERS_SUPPORTED}
2572 {GLOBAL}
2573 procedure jpeg_save_markers (cinfo : j_decompress_ptr;
2574 marker_code : int;
2575 length_limit : uint);
2576 var
2577 marker : my_marker_ptr;
2578 maxlength : long;
2579 processor : jpeg_marker_parser_method;
2580 begin
2581 marker := my_marker_ptr (cinfo^.marker);
2583 { Length limit mustn't be larger than what we can allocate
2584 (should only be a concern in a 16-bit environment). }
2586 maxlength := cinfo^.mem^.max_alloc_chunk - SIZEOF(jpeg_marker_struct);
2587 if (long(length_limit) > maxlength) then
2588 length_limit := uint(maxlength);
2590 { Choose processor routine to use.
2591 APP0/APP14 have special requirements. }
2593 if (length_limit <> 0) then
2594 begin
2595 processor := save_marker;
2596 { If saving APP0/APP14, save at least enough for our internal use. }
2597 if (marker_code = int(M_APP0)) and (length_limit < APP0_DATA_LEN) then
2598 length_limit := APP0_DATA_LEN
2599 else
2600 if (marker_code = int(M_APP14)) and (length_limit < APP14_DATA_LEN) then
2601 length_limit := APP14_DATA_LEN;
2602 end
2603 else
2604 begin
2605 processor := skip_variable;
2606 { If discarding APP0/APP14, use our regular on-the-fly processor. }
2607 if (marker_code = int(M_APP0)) or (marker_code = int(M_APP14)) then
2608 processor := get_interesting_appn;
2609 end;
2611 if (marker_code = int(M_COM)) then
2612 begin
2613 marker^.process_COM := processor;
2614 marker^.length_limit_COM := length_limit;
2615 end
2616 else
2617 if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
2618 begin
2619 marker^.process_APPn[marker_code - int(M_APP0)] := processor;
2620 marker^.length_limit_APPn[marker_code - int(M_APP0)] := length_limit;
2621 end
2622 else
2623 ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
2624 end;
2626 {$endif} { SAVE_MARKERS_SUPPORTED }
2628 { Install a special processing method for COM or APPn markers. }
2630 {GLOBAL}
2632 procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
2633 marker_code : int;
2634 routine : jpeg_marker_parser_method);
2635 var
2636 marker : my_marker_ptr;
2637 begin
2638 marker := my_marker_ptr (cinfo^.marker);
2639 if (marker_code = int(M_COM)) then
2640 marker^.process_COM := routine
2641 else
2642 if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
2643 marker^.process_APPn[marker_code - int(M_APP0)] := routine
2644 else
2645 ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
2646 end;
2648 end.