jdmarker.pas 86 KB

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