jdmarker.pas 79 KB

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