DEADSOFTWARE

added Vampyre Imaging Library; now textures can be in various formats, including...
[d2df-sdl.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;
1695 for i := 0 to numtoread-1 do
1696 begin
1697 { Read a byte into b[i]. If must suspend, return FALSE. }
1698 { make a byte available.
1699 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1700 but we must reload the local copies after a successful fill. }
1701 if (bytes_in_buffer = 0) then
1702 begin
1703 if (not datasrc^.fill_input_buffer(cinfo)) then
1704 begin
1705 get_interesting_appn := FALSE;
1706 exit;
1707 end;
1708 { Reload the local copies }
1709 next_input_byte := datasrc^.next_input_byte;
1710 bytes_in_buffer := datasrc^.bytes_in_buffer;
1711 end;
1712 Dec( bytes_in_buffer );
1714 b[i] := GETJOCTET(next_input_byte^);
1715 Inc(next_input_byte);
1716 end;
1718 Dec(length, numtoread);
1720 { process it }
1721 case (cinfo^.unread_marker) of
1722 M_APP0:
1723 examine_app0(cinfo, b, numtoread, length);
1724 M_APP14:
1725 examine_app14(cinfo, b, numtoread, length);
1726 else
1727 { can't get here unless jpeg_save_markers chooses wrong processor }
1728 ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, cinfo^.unread_marker);
1729 end;
1731 { skip any remaining data -- could be lots }
1733 { Unload the local copies --- do this only at a restart boundary }
1734 datasrc^.next_input_byte := next_input_byte;
1735 datasrc^.bytes_in_buffer := bytes_in_buffer;
1737 if (length > 0) then
1738 cinfo^.src^.skip_input_data(cinfo, long(length));
1740 get_interesting_appn := TRUE;
1741 end;
1743 {$ifdef SAVE_MARKERS_SUPPORTED}
1745 {METHODDEF}
1746 function save_marker (cinfo : j_decompress_ptr) : boolean;
1747 { Save an APPn or COM marker into the marker list }
1748 var
1749 marker : my_marker_ptr;
1750 cur_marker : jpeg_saved_marker_ptr;
1751 bytes_read, data_length : uint;
1752 data : JOCTET_FIELD_PTR;
1753 length : INT32;
1754 var
1755 datasrc : jpeg_source_mgr_ptr;
1756 next_input_byte : JOCTETptr;
1757 bytes_in_buffer : size_t;
1758 var
1759 limit : uint;
1760 var
1761 prev : jpeg_saved_marker_ptr;
1762 begin
1763 { local copies of input pointer/count }
1764 datasrc := cinfo^.src;
1765 next_input_byte := datasrc^.next_input_byte;
1766 bytes_in_buffer := datasrc^.bytes_in_buffer;
1768 marker := my_marker_ptr(cinfo^.marker);
1769 cur_marker := marker^.cur_marker;
1770 length := 0;
1772 if (cur_marker = NIL) then
1773 begin
1774 { begin reading a marker }
1775 { Read two bytes interpreted as an unsigned 16-bit integer. }
1777 { make a byte available.
1778 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1779 but we must reload the local copies after a successful fill. }
1780 if (bytes_in_buffer = 0) then
1781 begin
1782 if (not datasrc^.fill_input_buffer(cinfo)) then
1783 begin
1784 save_marker := FALSE;
1785 exit;
1786 end;
1787 { Reload the local copies }
1788 next_input_byte := datasrc^.next_input_byte;
1789 bytes_in_buffer := datasrc^.bytes_in_buffer;
1790 end;
1791 Dec( bytes_in_buffer );
1793 length := (uint( GETJOCTET(next_input_byte^)) shl 8);
1794 Inc( next_input_byte );
1796 { make a byte available.
1797 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1798 but we must reload the local copies after a successful fill. }
1799 if (bytes_in_buffer = 0) then
1800 begin
1801 if (not datasrc^.fill_input_buffer(cinfo)) then
1802 begin
1803 save_marker := FALSE;
1804 exit;
1805 end;
1806 { Reload the local copies }
1807 next_input_byte := datasrc^.next_input_byte;
1808 bytes_in_buffer := datasrc^.bytes_in_buffer;
1809 end;
1810 Dec( bytes_in_buffer );
1812 Inc( length, GETJOCTET(next_input_byte^));
1813 Inc( next_input_byte );
1815 Dec(length, 2);
1816 if (length >= 0) then
1817 begin { watch out for bogus length word }
1818 { figure out how much we want to save }
1820 if (cinfo^.unread_marker = int(M_COM)) then
1821 limit := marker^.length_limit_COM
1822 else
1823 limit := marker^.length_limit_APPn[cinfo^.unread_marker - int(M_APP0)];
1824 if (uint(length) < limit) then
1825 limit := uint(length);
1826 { allocate and initialize the marker item }
1827 cur_marker := jpeg_saved_marker_ptr(
1828 cinfo^.mem^.alloc_large (j_common_ptr(cinfo), JPOOL_IMAGE,
1829 SIZEOF(jpeg_marker_struct) + limit) );
1830 cur_marker^.next := NIL;
1831 cur_marker^.marker := UINT8 (cinfo^.unread_marker);
1832 cur_marker^.original_length := uint(length);
1833 cur_marker^.data_length := limit;
1834 { data area is just beyond the jpeg_marker_struct }
1835 cur_marker^.data := JOCTET_FIELD_PTR(cur_marker);
1836 Inc(jpeg_saved_marker_ptr(cur_marker^.data));
1837 data := cur_marker^.data;
1839 marker^.cur_marker := cur_marker;
1840 marker^.bytes_read := 0;
1841 bytes_read := 0;
1842 data_length := limit;
1843 end
1844 else
1845 begin
1846 { deal with bogus length word }
1847 data_length := 0;
1848 bytes_read := 0;
1849 data := NIL;
1850 end
1851 end
1852 else
1853 begin
1854 { resume reading a marker }
1855 bytes_read := marker^.bytes_read;
1856 data_length := cur_marker^.data_length;
1857 data := cur_marker^.data;
1858 Inc(data, bytes_read);
1859 end;
1861 while (bytes_read < data_length) do
1862 begin
1863 { move the restart point to here }
1864 datasrc^.next_input_byte := next_input_byte;
1865 datasrc^.bytes_in_buffer := bytes_in_buffer;
1867 marker^.bytes_read := bytes_read;
1868 { If there's not at least one byte in buffer, suspend }
1869 if (bytes_in_buffer = 0) then
1870 begin
1871 if not datasrc^.fill_input_buffer (cinfo) then
1872 begin
1873 save_marker := FALSE;
1874 exit;
1875 end;
1876 next_input_byte := datasrc^.next_input_byte;
1877 bytes_in_buffer := datasrc^.bytes_in_buffer;
1878 end;
1880 { Copy bytes with reasonable rapidity }
1881 while (bytes_read < data_length) and (bytes_in_buffer > 0) do
1882 begin
1883 JOCTETPTR(data)^ := next_input_byte^;
1884 Inc(JOCTETPTR(data));
1885 Inc(next_input_byte);
1886 Dec(bytes_in_buffer);
1887 Inc(bytes_read);
1888 end;
1889 end;
1891 { Done reading what we want to read }
1892 if (cur_marker <> NIL) then
1893 begin { will be NIL if bogus length word }
1894 { Add new marker to end of list }
1895 if (cinfo^.marker_list = NIL) then
1896 begin
1897 cinfo^.marker_list := cur_marker
1898 end
1899 else
1900 begin
1901 prev := cinfo^.marker_list;
1902 while (prev^.next <> NIL) do
1903 prev := prev^.next;
1904 prev^.next := cur_marker;
1905 end;
1906 { Reset pointer & calc remaining data length }
1907 data := cur_marker^.data;
1908 length := cur_marker^.original_length - data_length;
1909 end;
1910 { Reset to initial state for next marker }
1911 marker^.cur_marker := NIL;
1913 { Process the marker if interesting; else just make a generic trace msg }
1914 case (cinfo^.unread_marker) of
1915 M_APP0:
1916 examine_app0(cinfo, data^, data_length, length);
1917 M_APP14:
1918 examine_app14(cinfo, data^, data_length, length);
1919 else
1920 {$IFDEF DEBUG}
1921 TRACEMS2(j_common_ptr(cinfo), 1, JTRC_MISC_MARKER, cinfo^.unread_marker,
1922 int(data_length + length));
1923 {$ENDIF}
1924 end;
1926 { skip any remaining data -- could be lots }
1927 { do before skip_input_data }
1928 datasrc^.next_input_byte := next_input_byte;
1929 datasrc^.bytes_in_buffer := bytes_in_buffer;
1931 if (length > 0) then
1932 cinfo^.src^.skip_input_data (cinfo, long(length) );
1934 save_marker := TRUE;
1935 end;
1937 {$endif} { SAVE_MARKERS_SUPPORTED }
1940 { Find the next JPEG marker, save it in cinfo^.unread_marker.
1941 Returns FALSE if had to suspend before reaching a marker;
1942 in that case cinfo^.unread_marker is unchanged.
1944 Note that the result might not be a valid marker code,
1945 but it will never be 0 or FF. }
1947 {LOCAL}
1948 function next_marker (cinfo : j_decompress_ptr) : boolean;
1949 var
1950 c : int;
1951 var
1952 datasrc : jpeg_source_mgr_ptr;
1953 next_input_byte : JOCTETptr;
1954 bytes_in_buffer : size_t;
1955 begin
1956 datasrc := cinfo^.src;
1957 next_input_byte := datasrc^.next_input_byte;
1958 bytes_in_buffer := datasrc^.bytes_in_buffer;
1960 {while TRUE do}
1961 repeat
1962 { Read a byte into variable c. If must suspend, return FALSE. }
1963 { make a byte available.
1964 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1965 but we must reload the local copies after a successful fill. }
1966 if (bytes_in_buffer = 0) then
1967 begin
1968 if (not datasrc^.fill_input_buffer(cinfo)) then
1969 begin
1970 next_marker := FALSE;
1971 exit;
1972 end;
1973 { Reload the local copies }
1974 next_input_byte := datasrc^.next_input_byte;
1975 bytes_in_buffer := datasrc^.bytes_in_buffer;
1976 end;
1977 Dec( bytes_in_buffer );
1979 c := GETJOCTET(next_input_byte^);
1980 Inc(next_input_byte);
1982 { Skip any non-FF bytes.
1983 This may look a bit inefficient, but it will not occur in a valid file.
1984 We sync after each discarded byte so that a suspending data source
1985 can discard the byte from its buffer. }
1987 while (c <> $FF) do
1988 begin
1989 Inc(cinfo^.marker^.discarded_bytes);
1990 { Unload the local copies --- do this only at a restart boundary }
1991 datasrc^.next_input_byte := next_input_byte;
1992 datasrc^.bytes_in_buffer := bytes_in_buffer;
1994 { Read a byte into variable c. If must suspend, return FALSE. }
1995 { make a byte available.
1996 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
1997 but we must reload the local copies after a successful fill. }
1998 if (bytes_in_buffer = 0) then
1999 begin
2000 if (not datasrc^.fill_input_buffer(cinfo)) then
2001 begin
2002 next_marker := FALSE;
2003 exit;
2004 end;
2005 { Reload the local copies }
2006 next_input_byte := datasrc^.next_input_byte;
2007 bytes_in_buffer := datasrc^.bytes_in_buffer;
2008 end;
2009 Dec( bytes_in_buffer );
2011 c := GETJOCTET(next_input_byte^);
2012 Inc(next_input_byte);
2014 end;
2015 { This loop swallows any duplicate FF bytes. Extra FFs are legal as
2016 pad bytes, so don't count them in discarded_bytes. We assume there
2017 will not be so many consecutive FF bytes as to overflow a suspending
2018 data source's input buffer. }
2020 repeat
2021 { Read a byte into variable c. If must suspend, return FALSE. }
2022 { make a byte available.
2023 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2024 but we must reload the local copies after a successful fill. }
2025 if (bytes_in_buffer = 0) then
2026 begin
2027 if (not datasrc^.fill_input_buffer(cinfo)) then
2028 begin
2029 next_marker := FALSE;
2030 exit;
2031 end;
2032 { Reload the local copies }
2033 next_input_byte := datasrc^.next_input_byte;
2034 bytes_in_buffer := datasrc^.bytes_in_buffer;
2035 end;
2036 Dec( bytes_in_buffer );
2038 c := GETJOCTET(next_input_byte^);
2039 Inc(next_input_byte);
2040 Until (c <> $FF);
2041 if (c <> 0) then
2042 break; { found a valid marker, exit loop }
2043 { Reach here if we found a stuffed-zero data sequence (FF/00).
2044 Discard it and loop back to try again. }
2046 Inc(cinfo^.marker^.discarded_bytes, 2);
2047 { Unload the local copies --- do this only at a restart boundary }
2048 datasrc^.next_input_byte := next_input_byte;
2049 datasrc^.bytes_in_buffer := bytes_in_buffer;
2050 Until False;
2052 if (cinfo^.marker^.discarded_bytes <> 0) then
2053 begin
2054 WARNMS2(j_common_ptr(cinfo), JWRN_EXTRANEOUS_DATA,
2055 cinfo^.marker^.discarded_bytes, c);
2056 cinfo^.marker^.discarded_bytes := 0;
2057 end;
2059 cinfo^.unread_marker := c;
2061 { Unload the local copies --- do this only at a restart boundary }
2062 datasrc^.next_input_byte := next_input_byte;
2063 datasrc^.bytes_in_buffer := bytes_in_buffer;
2065 next_marker := TRUE;
2066 end; { next_marker }
2069 {LOCAL}
2070 function first_marker (cinfo : j_decompress_ptr) : boolean;
2071 { Like next_marker, but used to obtain the initial SOI marker. }
2072 { For this marker, we do not allow preceding garbage or fill; otherwise,
2073 we might well scan an entire input file before realizing it ain't JPEG.
2074 If an application wants to process non-JFIF files, it must seek to the
2075 SOI before calling the JPEG library. }
2076 var
2077 c, c2 : int;
2078 var
2079 datasrc : jpeg_source_mgr_ptr;
2080 next_input_byte : JOCTETptr;
2081 bytes_in_buffer : size_t;
2082 begin
2083 datasrc := cinfo^.src;
2084 next_input_byte := datasrc^.next_input_byte;
2085 bytes_in_buffer := datasrc^.bytes_in_buffer;
2087 { Read a byte into variable c. If must suspend, return FALSE. }
2088 { make a byte available.
2089 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2090 but we must reload the local copies after a successful fill. }
2091 if (bytes_in_buffer = 0) then
2092 begin
2093 if (not datasrc^.fill_input_buffer(cinfo)) then
2094 begin
2095 first_marker := FALSE;
2096 exit;
2097 end;
2098 { Reload the local copies }
2099 next_input_byte := datasrc^.next_input_byte;
2100 bytes_in_buffer := datasrc^.bytes_in_buffer;
2101 end;
2102 Dec( bytes_in_buffer );
2104 c := GETJOCTET(next_input_byte^);
2105 Inc(next_input_byte);
2107 { Read a byte into variable c2. If must suspend, return FALSE. }
2108 { make a byte available.
2109 Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
2110 but we must reload the local copies after a successful fill. }
2111 if (bytes_in_buffer = 0) then
2112 begin
2113 if (not datasrc^.fill_input_buffer(cinfo)) then
2114 begin
2115 first_marker := FALSE;
2116 exit;
2117 end;
2118 { Reload the local copies }
2119 next_input_byte := datasrc^.next_input_byte;
2120 bytes_in_buffer := datasrc^.bytes_in_buffer;
2121 end;
2122 Dec( bytes_in_buffer );
2124 c2 := GETJOCTET(next_input_byte^);
2125 Inc(next_input_byte);
2127 if (c <> $FF) or (c2 <> int(M_SOI)) then
2128 ERREXIT2(j_common_ptr(cinfo), JERR_NO_SOI, c, c2);
2130 cinfo^.unread_marker := c2;
2132 { Unload the local copies --- do this only at a restart boundary }
2133 datasrc^.next_input_byte := next_input_byte;
2134 datasrc^.bytes_in_buffer := bytes_in_buffer;
2136 first_marker := TRUE;
2137 end; { first_marker }
2140 { Read markers until SOS or EOI.
2142 Returns same codes as are defined for jpeg_consume_input:
2143 JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. }
2145 {METHODDEF}
2146 function read_markers (cinfo : j_decompress_ptr) : int;
2147 begin
2148 { Outer loop repeats once for each marker. }
2149 repeat
2150 { Collect the marker proper, unless we already did. }
2151 { NB: first_marker() enforces the requirement that SOI appear first. }
2152 if (cinfo^.unread_marker = 0) then
2153 begin
2154 if not cinfo^.marker^.saw_SOI then
2155 begin
2156 if not first_marker(cinfo) then
2157 begin
2158 read_markers := JPEG_SUSPENDED;
2159 exit;
2160 end;
2161 end
2162 else
2163 begin
2164 if not next_marker(cinfo) then
2165 begin
2166 read_markers := JPEG_SUSPENDED;
2167 exit;
2168 end;
2169 end;
2170 end;
2171 { At this point cinfo^.unread_marker contains the marker code and the
2172 input point is just past the marker proper, but before any parameters.
2173 A suspension will cause us to return with this state still true. }
2175 case (cinfo^.unread_marker) of
2176 M_SOI:
2177 if not get_soi(cinfo) then
2178 begin
2179 read_markers := JPEG_SUSPENDED;
2180 exit;
2181 end;
2183 M_SOF0, { Baseline }
2184 M_SOF1: { Extended sequential, Huffman }
2185 if not get_sof(cinfo, FALSE, FALSE) then
2186 begin
2187 read_markers := JPEG_SUSPENDED;
2188 exit;
2189 end;
2190 M_SOF2: { Progressive, Huffman }
2191 if not get_sof(cinfo, TRUE, FALSE) then
2192 begin
2193 read_markers := JPEG_SUSPENDED;
2194 exit;
2195 end;
2197 M_SOF9: { Extended sequential, arithmetic }
2198 if not get_sof(cinfo, FALSE, TRUE) then
2199 begin
2200 read_markers := JPEG_SUSPENDED;
2201 exit;
2202 end;
2204 M_SOF10: { Progressive, arithmetic }
2205 if not get_sof(cinfo, TRUE, TRUE) then
2206 begin
2207 read_markers := JPEG_SUSPENDED;
2208 exit;
2209 end;
2211 { Currently unsupported SOFn types }
2212 M_SOF3, { Lossless, Huffman }
2213 M_SOF5, { Differential sequential, Huffman }
2214 M_SOF6, { Differential progressive, Huffman }
2215 M_SOF7, { Differential lossless, Huffman }
2216 M_JPG, { Reserved for JPEG extensions }
2217 M_SOF11, { Lossless, arithmetic }
2218 M_SOF13, { Differential sequential, arithmetic }
2219 M_SOF14, { Differential progressive, arithmetic }
2220 M_SOF15: { Differential lossless, arithmetic }
2221 ERREXIT1(j_common_ptr(cinfo), JERR_SOF_UNSUPPORTED, cinfo^.unread_marker);
2223 M_SOS:
2224 begin
2225 if not get_sos(cinfo) then
2226 begin
2227 read_markers := JPEG_SUSPENDED;
2228 exit;
2229 end;
2230 cinfo^.unread_marker := 0; { processed the marker }
2231 read_markers := JPEG_REACHED_SOS;
2232 exit;
2233 end;
2235 M_EOI:
2236 begin
2237 {$IFDEF DEBUG}
2238 TRACEMS(j_common_ptr(cinfo), 1, JTRC_EOI);
2239 {$ENDIF}
2240 cinfo^.unread_marker := 0; { processed the marker }
2241 read_markers := JPEG_REACHED_EOI;
2242 exit;
2243 end;
2245 M_DAC:
2246 if not get_dac(cinfo) then
2247 begin
2248 read_markers := JPEG_SUSPENDED;
2249 exit;
2250 end;
2252 M_DHT:
2253 if not get_dht(cinfo) then
2254 begin
2255 read_markers := JPEG_SUSPENDED;
2256 exit;
2257 end;
2259 M_DQT:
2260 if not get_dqt(cinfo) then
2261 begin
2262 read_markers := JPEG_SUSPENDED;
2263 exit;
2264 end;
2266 M_DRI:
2267 if not get_dri(cinfo) then
2268 begin
2269 read_markers := JPEG_SUSPENDED;
2270 exit;
2271 end;
2273 M_APP0,
2274 M_APP1,
2275 M_APP2,
2276 M_APP3,
2277 M_APP4,
2278 M_APP5,
2279 M_APP6,
2280 M_APP7,
2281 M_APP8,
2282 M_APP9,
2283 M_APP10,
2284 M_APP11,
2285 M_APP12,
2286 M_APP13,
2287 M_APP14,
2288 M_APP15:
2289 if not my_marker_ptr(cinfo^.marker)^.
2290 process_APPn[cinfo^.unread_marker - int(M_APP0)](cinfo) then
2291 begin
2292 read_markers := JPEG_SUSPENDED;
2293 exit;
2294 end;
2296 M_COM:
2297 if not my_marker_ptr(cinfo^.marker)^.process_COM (cinfo) then
2298 begin
2299 read_markers := JPEG_SUSPENDED;
2300 exit;
2301 end;
2303 M_RST0, { these are all parameterless }
2304 M_RST1,
2305 M_RST2,
2306 M_RST3,
2307 M_RST4,
2308 M_RST5,
2309 M_RST6,
2310 M_RST7,
2311 M_TEM:
2312 {$IFDEF DEBUG}
2313 TRACEMS1(j_common_ptr(cinfo), 1, JTRC_PARMLESS_MARKER,
2314 cinfo^.unread_marker)
2315 {$ENDIF}
2318 M_DNL: { Ignore DNL ... perhaps the wrong thing }
2319 if not skip_variable(cinfo) then
2320 begin
2321 read_markers := JPEG_SUSPENDED;
2322 exit;
2323 end;
2325 else { must be DHP, EXP, JPGn, or RESn }
2326 { For now, we treat the reserved markers as fatal errors since they are
2327 likely to be used to signal incompatible JPEG Part 3 extensions.
2328 Once the JPEG 3 version-number marker is well defined, this code
2329 ought to change! }
2330 ERREXIT1(j_common_ptr(cinfo) , JERR_UNKNOWN_MARKER,
2331 cinfo^.unread_marker);
2332 end; { end of case }
2333 { Successfully processed marker, so reset state variable }
2334 cinfo^.unread_marker := 0;
2335 Until false;
2336 end; { read_markers }
2339 { Read a restart marker, which is expected to appear next in the datastream;
2340 if the marker is not there, take appropriate recovery action.
2341 Returns FALSE if suspension is required.
2343 This is called by the entropy decoder after it has read an appropriate
2344 number of MCUs. cinfo^.unread_marker may be nonzero if the entropy decoder
2345 has already read a marker from the data source. Under normal conditions
2346 cinfo^.unread_marker will be reset to 0 before returning; if not reset,
2347 it holds a marker which the decoder will be unable to read past. }
2349 {METHODDEF}
2350 function read_restart_marker (cinfo : j_decompress_ptr) :boolean;
2351 begin
2352 { Obtain a marker unless we already did. }
2353 { Note that next_marker will complain if it skips any data. }
2354 if (cinfo^.unread_marker = 0) then
2355 begin
2356 if not next_marker(cinfo) then
2357 begin
2358 read_restart_marker := FALSE;
2359 exit;
2360 end;
2361 end;
2363 if (cinfo^.unread_marker = (int(M_RST0) + cinfo^.marker^.next_restart_num)) then
2364 begin
2365 { Normal case --- swallow the marker and let entropy decoder continue }
2366 {$IFDEF DEBUG}
2367 TRACEMS1(j_common_ptr(cinfo), 3, JTRC_RST,
2368 cinfo^.marker^.next_restart_num);
2369 {$ENDIF}
2370 cinfo^.unread_marker := 0;
2371 end
2372 else
2373 begin
2374 { Uh-oh, the restart markers have been messed up. }
2375 { Let the data source manager determine how to resync. }
2376 if not cinfo^.src^.resync_to_restart(cinfo,
2377 cinfo^.marker^.next_restart_num) then
2378 begin
2379 read_restart_marker := FALSE;
2380 exit;
2381 end;
2382 end;
2384 { Update next-restart state }
2385 with cinfo^.marker^ do
2386 next_restart_num := (next_restart_num + 1) and 7;
2388 read_restart_marker := TRUE;
2389 end; { read_restart_marker }
2392 { This is the default resync_to_restart method for data source managers
2393 to use if they don't have any better approach. Some data source managers
2394 may be able to back up, or may have additional knowledge about the data
2395 which permits a more intelligent recovery strategy; such managers would
2396 presumably supply their own resync method.
2398 read_restart_marker calls resync_to_restart if it finds a marker other than
2399 the restart marker it was expecting. (This code is *not* used unless
2400 a nonzero restart interval has been declared.) cinfo^.unread_marker is
2401 the marker code actually found (might be anything, except 0 or FF).
2402 The desired restart marker number (0..7) is passed as a parameter.
2403 This routine is supposed to apply whatever error recovery strategy seems
2404 appropriate in order to position the input stream to the next data segment.
2405 Note that cinfo^.unread_marker is treated as a marker appearing before
2406 the current data-source input point; usually it should be reset to zero
2407 before returning.
2408 Returns FALSE if suspension is required.
2410 This implementation is substantially constrained by wanting to treat the
2411 input as a data stream; this means we can't back up. Therefore, we have
2412 only the following actions to work with:
2413 1. Simply discard the marker and let the entropy decoder resume at next
2414 byte of file.
2415 2. Read forward until we find another marker, discarding intervening
2416 data. (In theory we could look ahead within the current bufferload,
2417 without having to discard data if we don't find the desired marker.
2418 This idea is not implemented here, in part because it makes behavior
2419 dependent on buffer size and chance buffer-boundary positions.)
2420 3. Leave the marker unread (by failing to zero cinfo^.unread_marker).
2421 This will cause the entropy decoder to process an empty data segment,
2422 inserting dummy zeroes, and then we will reprocess the marker.
2424 #2 is appropriate if we think the desired marker lies ahead, while #3 is
2425 appropriate if the found marker is a future restart marker (indicating
2426 that we have missed the desired restart marker, probably because it got
2427 corrupted).
2428 We apply #2 or #3 if the found marker is a restart marker no more than
2429 two counts behind or ahead of the expected one. We also apply #2 if the
2430 found marker is not a legal JPEG marker code (it's certainly bogus data).
2431 If the found marker is a restart marker more than 2 counts away, we do #1
2432 (too much risk that the marker is erroneous; with luck we will be able to
2433 resync at some future point).
2434 For any valid non-restart JPEG marker, we apply #3. This keeps us from
2435 overrunning the end of a scan. An implementation limited to single-scan
2436 files might find it better to apply #2 for markers other than EOI, since
2437 any other marker would have to be bogus data in that case. }
2440 {GLOBAL}
2441 function jpeg_resync_to_restart(cinfo : j_decompress_ptr;
2442 desired : int) : boolean;
2443 var
2444 marker : int;
2445 action : int;
2446 begin
2447 marker := cinfo^.unread_marker;
2448 //action := 1; { never used }
2449 { Always put up a warning. }
2450 WARNMS2(j_common_ptr(cinfo), JWRN_MUST_RESYNC, marker, desired);
2452 { Outer loop handles repeated decision after scanning forward. }
2453 repeat
2454 if (marker < int(M_SOF0)) then
2455 action := 2 { invalid marker }
2456 else
2457 if (marker < int(M_RST0)) or (marker > int(M_RST7)) then
2458 action := 3 { valid non-restart marker }
2459 else
2460 begin
2461 if (marker = (int(M_RST0) + ((desired+1) and 7))) or
2462 (marker = (int(M_RST0) + ((desired+2) and 7))) then
2463 action := 3 { one of the next two expected restarts }
2464 else
2465 if (marker = (int(M_RST0) + ((desired-1) and 7))) or
2466 (marker = (int(M_RST0) + ((desired-2) and 7))) then
2467 action := 2 { a prior restart, so advance }
2468 else
2469 action := 1; { desired restart or too far away }
2470 end;
2472 {$IFDEF DEBUG}
2473 TRACEMS2(j_common_ptr(cinfo), 4, JTRC_RECOVERY_ACTION, marker, action);
2474 {$ENDIF}
2475 case action of
2476 1:
2477 { Discard marker and let entropy decoder resume processing. }
2478 begin
2479 cinfo^.unread_marker := 0;
2480 jpeg_resync_to_restart := TRUE;
2481 exit;
2482 end;
2483 2:
2484 { Scan to the next marker, and repeat the decision loop. }
2485 begin
2486 if not next_marker(cinfo) then
2487 begin
2488 jpeg_resync_to_restart := FALSE;
2489 exit;
2490 end;
2491 marker := cinfo^.unread_marker;
2492 end;
2493 3:
2494 { Return without advancing past this marker. }
2495 { Entropy decoder will be forced to process an empty segment. }
2496 begin
2497 jpeg_resync_to_restart := TRUE;
2498 exit;
2499 end;
2500 end; { case }
2501 Until false; { end loop }
2502 end; { jpeg_resync_to_restart }
2505 { Reset marker processing state to begin a fresh datastream. }
2507 {METHODDEF}
2508 procedure reset_marker_reader (cinfo : j_decompress_ptr);
2509 var
2510 marker : my_marker_ptr;
2511 begin
2512 marker := my_marker_ptr (cinfo^.marker);
2513 with cinfo^ do
2514 begin
2515 comp_info := NIL; { until allocated by get_sof }
2516 input_scan_number := 0; { no SOS seen yet }
2517 unread_marker := 0; { no pending marker }
2518 end;
2519 marker^.pub.saw_SOI := FALSE; { set internal state too }
2520 marker^.pub.saw_SOF := FALSE;
2521 marker^.pub.discarded_bytes := 0;
2522 marker^.cur_marker := NIL;
2523 end; { reset_marker_reader }
2526 { Initialize the marker reader module.
2527 This is called only once, when the decompression object is created. }
2529 {GLOBAL}
2530 procedure jinit_marker_reader (cinfo : j_decompress_ptr);
2531 var
2532 marker : my_marker_ptr;
2533 i : int;
2534 begin
2535 { Create subobject in permanent pool }
2536 marker := my_marker_ptr(
2537 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
2538 SIZEOF(my_marker_reader))
2539 );
2540 cinfo^.marker := jpeg_marker_reader_ptr(marker);
2541 { Initialize method pointers }
2542 marker^.pub.reset_marker_reader := reset_marker_reader;
2543 marker^.pub.read_markers := read_markers;
2544 marker^.pub.read_restart_marker := read_restart_marker;
2545 { Initialize COM/APPn processing.
2546 By default, we examine and then discard APP0 and APP14,
2547 but simply discard COM and all other APPn. }
2549 marker^.process_COM := skip_variable;
2550 marker^.length_limit_COM := 0;
2551 for i := 0 to 16-1 do
2552 begin
2553 marker^.process_APPn[i] := skip_variable;
2554 marker^.length_limit_APPn[i] := 0;
2555 end;
2556 marker^.process_APPn[0] := get_interesting_appn;
2557 marker^.process_APPn[14] := get_interesting_appn;
2558 { Reset marker processing state }
2559 reset_marker_reader(cinfo);
2560 end; { jinit_marker_reader }
2563 { Control saving of COM and APPn markers into marker_list. }
2566 {$ifdef SAVE_MARKERS_SUPPORTED}
2568 {GLOBAL}
2569 procedure jpeg_save_markers (cinfo : j_decompress_ptr;
2570 marker_code : int;
2571 length_limit : uint);
2572 var
2573 marker : my_marker_ptr;
2574 maxlength : long;
2575 processor : jpeg_marker_parser_method;
2576 begin
2577 marker := my_marker_ptr (cinfo^.marker);
2579 { Length limit mustn't be larger than what we can allocate
2580 (should only be a concern in a 16-bit environment). }
2582 maxlength := cinfo^.mem^.max_alloc_chunk - SIZEOF(jpeg_marker_struct);
2583 if (long(length_limit) > maxlength) then
2584 length_limit := uint(maxlength);
2586 { Choose processor routine to use.
2587 APP0/APP14 have special requirements. }
2589 if (length_limit <> 0) then
2590 begin
2591 processor := save_marker;
2592 { If saving APP0/APP14, save at least enough for our internal use. }
2593 if (marker_code = int(M_APP0)) and (length_limit < APP0_DATA_LEN) then
2594 length_limit := APP0_DATA_LEN
2595 else
2596 if (marker_code = int(M_APP14)) and (length_limit < APP14_DATA_LEN) then
2597 length_limit := APP14_DATA_LEN;
2598 end
2599 else
2600 begin
2601 processor := skip_variable;
2602 { If discarding APP0/APP14, use our regular on-the-fly processor. }
2603 if (marker_code = int(M_APP0)) or (marker_code = int(M_APP14)) then
2604 processor := get_interesting_appn;
2605 end;
2607 if (marker_code = int(M_COM)) then
2608 begin
2609 marker^.process_COM := processor;
2610 marker^.length_limit_COM := length_limit;
2611 end
2612 else
2613 if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
2614 begin
2615 marker^.process_APPn[marker_code - int(M_APP0)] := processor;
2616 marker^.length_limit_APPn[marker_code - int(M_APP0)] := length_limit;
2617 end
2618 else
2619 ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
2620 end;
2622 {$endif} { SAVE_MARKERS_SUPPORTED }
2624 { Install a special processing method for COM or APPn markers. }
2626 {GLOBAL}
2628 procedure jpeg_set_marker_processor (cinfo : j_decompress_ptr;
2629 marker_code : int;
2630 routine : jpeg_marker_parser_method);
2631 var
2632 marker : my_marker_ptr;
2633 begin
2634 marker := my_marker_ptr (cinfo^.marker);
2635 if (marker_code = int(M_COM)) then
2636 marker^.process_COM := routine
2637 else
2638 if (marker_code >= int(M_APP0)) and (marker_code <= int(M_APP15)) then
2639 marker^.process_APPn[marker_code - int(M_APP0)] := routine
2640 else
2641 ERREXIT1(j_common_ptr(cinfo), JERR_UNKNOWN_MARKER, marker_code);
2642 end;
2644 end.