tinyjpeg.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  1. /*
  2. * Small jpeg decoder library
  3. *
  4. * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
  5. * All rights reserved.
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of the author nor the names of its contributors may be
  17. * used to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include "tinyjpeg.h"
  38. #include "tinyjpeg-internal.h"
  39. enum std_markers {
  40. DQT = 0xDB, /* Define Quantization Table */
  41. SOF = 0xC0, /* Start of Frame (size information) */
  42. DHT = 0xC4, /* Huffman Table */
  43. SOI = 0xD8, /* Start of Image */
  44. SOS = 0xDA, /* Start of Scan */
  45. RST = 0xD0, /* Reset Marker d0 -> .. */
  46. RST7 = 0xD7, /* Reset Marker .. -> d7 */
  47. EOI = 0xD9, /* End of Image */
  48. DRI = 0xDD, /* Define Restart Interval */
  49. APP0 = 0xE0,
  50. };
  51. #define cY 0
  52. #define cCb 1
  53. #define cCr 2
  54. #define BLACK_Y 0
  55. #define BLACK_U 127
  56. #define BLACK_V 127
  57. #if DEBUG
  58. #define trace(fmt, args...) do { \
  59. fprintf(stderr, fmt, ## args); \
  60. fflush(stderr); \
  61. } while(0)
  62. #else
  63. #define trace(fmt, ...) do { } while (0)
  64. #endif
  65. #define error(fmt, ...) do { \
  66. sprintf(error_string, fmt, ## __VA_ARGS__); \
  67. return -1; \
  68. } while(0)
  69. #if 0
  70. static char *print_bits(unsigned int value, char *bitstr)
  71. {
  72. int i, j;
  73. i=31;
  74. while (i>0)
  75. {
  76. if (value & (1UL<<i))
  77. break;
  78. i--;
  79. }
  80. j=0;
  81. while (i>=0)
  82. {
  83. bitstr[j++] = (value & (1UL<<i))?'1':'0';
  84. i--;
  85. }
  86. bitstr[j] = 0;
  87. return bitstr;
  88. }
  89. static void print_next_16bytes(int offset, const unsigned char *stream)
  90. {
  91. trace("%4.4x: %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
  92. offset,
  93. stream[0], stream[1], stream[2], stream[3],
  94. stream[4], stream[5], stream[6], stream[7],
  95. stream[8], stream[9], stream[10], stream[11],
  96. stream[12], stream[13], stream[14], stream[15]);
  97. }
  98. #endif
  99. /* Global variable to return the last error found while deconding */
  100. static char error_string[256];
  101. static const unsigned char zigzag[64] =
  102. {
  103. 0, 1, 5, 6, 14, 15, 27, 28,
  104. 2, 4, 7, 13, 16, 26, 29, 42,
  105. 3, 8, 12, 17, 25, 30, 41, 43,
  106. 9, 11, 18, 24, 31, 40, 44, 53,
  107. 10, 19, 23, 32, 39, 45, 52, 54,
  108. 20, 22, 33, 38, 46, 51, 55, 60,
  109. 21, 34, 37, 47, 50, 56, 59, 61,
  110. 35, 36, 48, 49, 57, 58, 62, 63
  111. };
  112. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  113. /* IMPORTANT: these are only valid for 8-bit data precision! */
  114. static const unsigned char bits_dc_luminance[17] =
  115. {
  116. 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
  117. };
  118. static const unsigned char val_dc_luminance[] =
  119. {
  120. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  121. };
  122. static const unsigned char bits_dc_chrominance[17] =
  123. {
  124. 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
  125. };
  126. static const unsigned char val_dc_chrominance[] =
  127. {
  128. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  129. };
  130. static const unsigned char bits_ac_luminance[17] =
  131. {
  132. 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
  133. };
  134. static const unsigned char val_ac_luminance[] =
  135. {
  136. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  137. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  138. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  139. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  140. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  141. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  142. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  143. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  144. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  145. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  146. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  147. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  148. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  149. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  150. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  151. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  152. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  153. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  154. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  155. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  156. 0xf9, 0xfa
  157. };
  158. static const unsigned char bits_ac_chrominance[17] =
  159. {
  160. 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
  161. };
  162. static const unsigned char val_ac_chrominance[] =
  163. {
  164. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  165. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  166. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  167. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  168. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  169. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  170. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  171. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  172. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  173. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  174. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  175. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  176. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  177. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  178. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  179. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  180. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  181. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  182. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  183. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  184. 0xf9, 0xfa
  185. };
  186. /*
  187. * 4 functions to manage the stream
  188. *
  189. * fill_nbits: put at least nbits in the reservoir of bits.
  190. * But convert any 0xff,0x00 into 0xff
  191. * get_nbits: read nbits from the stream, and put it in result,
  192. * bits is removed from the stream and the reservoir is filled
  193. * automaticaly. The result is signed according to the number of
  194. * bits.
  195. * look_nbits: read nbits from the stream without marking as read.
  196. * skip_nbits: read nbits from the stream but do not return the result.
  197. *
  198. * stream: current pointer in the jpeg data (read bytes per bytes)
  199. * nbits_in_reservoir: number of bits filled into the reservoir
  200. * reservoir: register that contains bits information. Only nbits_in_reservoir
  201. * is valid.
  202. * nbits_in_reservoir
  203. * <-- 17 bits -->
  204. * Ex: 0000 0000 1010 0000 1111 0000 <== reservoir
  205. * ^
  206. * bit 1
  207. * To get two bits from this example
  208. * result = (reservoir >> 15) & 3
  209. *
  210. */
  211. #define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
  212. while (nbits_in_reservoir<nbits_wanted) \
  213. { \
  214. unsigned char c; \
  215. if (stream >= priv->stream_end) \
  216. longjmp(priv->jump_state, -EIO); \
  217. c = *stream++; \
  218. reservoir <<= 8; \
  219. if (c == 0xff && *stream == 0x00) \
  220. stream++; \
  221. reservoir |= c; \
  222. nbits_in_reservoir+=8; \
  223. } \
  224. } while(0);
  225. /* Signed version !!!! */
  226. #define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
  227. fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
  228. result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
  229. nbits_in_reservoir -= (nbits_wanted); \
  230. reservoir &= ((1U<<nbits_in_reservoir)-1); \
  231. if ((unsigned int)result < (1UL<<((nbits_wanted)-1))) \
  232. result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \
  233. } while(0);
  234. #define look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
  235. fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
  236. result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
  237. } while(0);
  238. /* To speed up the decoding, we assume that the reservoir have enough bit
  239. * slow version:
  240. * #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
  241. * fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
  242. * nbits_in_reservoir -= (nbits_wanted); \
  243. * reservoir &= ((1U<<nbits_in_reservoir)-1); \
  244. * } while(0);
  245. */
  246. #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
  247. nbits_in_reservoir -= (nbits_wanted); \
  248. reservoir &= ((1U<<nbits_in_reservoir)-1); \
  249. } while(0);
  250. #define be16_to_cpu(x) (((x)[0]<<8)|(x)[1])
  251. static void resync(struct jdec_private *priv);
  252. /**
  253. * Get the next (valid) huffman code in the stream.
  254. *
  255. * To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is
  256. * lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code
  257. * and the value by using two lookup table.
  258. * Else if the value is not found, just search (linear) into an array for each
  259. * bits is the code is present.
  260. *
  261. * If the code is not present for any reason, -1 is return.
  262. */
  263. static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table)
  264. {
  265. int value, hcode;
  266. unsigned int extra_nbits, nbits;
  267. uint16_t *slowtable;
  268. look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
  269. value = huffman_table->lookup[hcode];
  270. if (__likely(value >= 0))
  271. {
  272. unsigned int code_size = huffman_table->code_size[value];
  273. skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
  274. return value;
  275. }
  276. /* Decode more bits each time ... */
  277. for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++)
  278. {
  279. nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
  280. look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode);
  281. slowtable = huffman_table->slowtable[extra_nbits];
  282. /* Search if the code is in this array */
  283. while (slowtable[0]) {
  284. if (slowtable[0] == hcode) {
  285. skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits);
  286. return slowtable[1];
  287. }
  288. slowtable+=2;
  289. }
  290. }
  291. return 0;
  292. }
  293. /**
  294. *
  295. * Decode a single block that contains the DCT coefficients.
  296. * The table coefficients is already dezigzaged at the end of the operation.
  297. *
  298. */
  299. static void process_Huffman_data_unit(struct jdec_private *priv, int component)
  300. {
  301. unsigned char j;
  302. unsigned int huff_code;
  303. unsigned char size_val, count_0;
  304. struct component *c = &priv->component_infos[component];
  305. short int DCT[64];
  306. /* Initialize the DCT coef table */
  307. memset(DCT, 0, sizeof(DCT));
  308. /* DC coefficient decoding */
  309. huff_code = get_next_huffman_code(priv, c->DC_table);
  310. //trace("+ %x\n", huff_code);
  311. if (huff_code) {
  312. get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
  313. DCT[0] += c->previous_DC;
  314. c->previous_DC = DCT[0];
  315. } else {
  316. DCT[0] = c->previous_DC;
  317. }
  318. /* AC coefficient decoding */
  319. j = 1;
  320. while (j<64)
  321. {
  322. huff_code = get_next_huffman_code(priv, c->AC_table);
  323. //trace("- %x\n", huff_code);
  324. size_val = huff_code & 0xF;
  325. count_0 = huff_code >> 4;
  326. if (size_val == 0)
  327. { /* RLE */
  328. if (count_0 == 0)
  329. break; /* EOB found, go out */
  330. else if (count_0 == 0xF)
  331. j += 16; /* skip 16 zeros */
  332. }
  333. else
  334. {
  335. j += count_0; /* skip count_0 zeroes */
  336. if (__unlikely(j >= 64))
  337. {
  338. sprintf(error_string, "Bad huffman data (buffer overflow)");
  339. break;
  340. }
  341. get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
  342. j++;
  343. }
  344. }
  345. for (j = 0; j < 64; j++)
  346. c->DCT[j] = DCT[zigzag[j]];
  347. }
  348. /*
  349. * Takes two array of bits, and build the huffman table for size, and code
  350. *
  351. * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
  352. * code_size will be used to known how many bits this symbol is encoded.
  353. * slowtable will be used when the first lookup didn't give the result.
  354. */
  355. static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
  356. {
  357. unsigned int i, j, code, code_size, val, nbits;
  358. unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz;
  359. unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc;
  360. int next_free_entry;
  361. /*
  362. * Build a temp array
  363. * huffsize[X] => numbers of bits to write vals[X]
  364. */
  365. hz = huffsize;
  366. for (i=1; i<=16; i++)
  367. {
  368. for (j=1; j<=bits[i]; j++)
  369. *hz++ = i;
  370. }
  371. *hz = 0;
  372. memset(table->lookup, 0xff, sizeof(table->lookup));
  373. for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
  374. table->slowtable[i][0] = 0;
  375. /* Build a temp array
  376. * huffcode[X] => code used to write vals[X]
  377. */
  378. code = 0;
  379. hc = huffcode;
  380. hz = huffsize;
  381. nbits = *hz;
  382. while (*hz)
  383. {
  384. while (*hz == nbits)
  385. {
  386. *hc++ = code++;
  387. hz++;
  388. }
  389. code <<= 1;
  390. nbits++;
  391. }
  392. /*
  393. * Build the lookup table, and the slowtable if needed.
  394. */
  395. next_free_entry = -1;
  396. for (i=0; huffsize[i]; i++)
  397. {
  398. val = vals[i];
  399. code = huffcode[i];
  400. code_size = huffsize[i];
  401. trace("val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
  402. table->code_size[val] = code_size;
  403. if (code_size <= HUFFMAN_HASH_NBITS)
  404. {
  405. /*
  406. * Good: val can be put in the lookup table, so fill all value of this
  407. * column with value val
  408. */
  409. int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
  410. code <<= HUFFMAN_HASH_NBITS - code_size;
  411. while ( repeat-- )
  412. table->lookup[code++] = val;
  413. }
  414. else
  415. {
  416. /* Perhaps sorting the array will be an optimization */
  417. uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1];
  418. while(slowtable[0])
  419. slowtable+=2;
  420. slowtable[0] = code;
  421. slowtable[1] = val;
  422. slowtable[2] = 0;
  423. /* TODO: NEED TO CHECK FOR AN OVERFLOW OF THE TABLE */
  424. }
  425. }
  426. }
  427. static void build_default_huffman_tables(struct jdec_private *priv)
  428. {
  429. if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
  430. && priv->default_huffman_table_initialized)
  431. return;
  432. build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]);
  433. build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]);
  434. build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]);
  435. build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]);
  436. priv->default_huffman_table_initialized = 1;
  437. }
  438. /*******************************************************************************
  439. *
  440. * Colorspace conversion routine
  441. *
  442. *
  443. * Note:
  444. * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  445. * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  446. * The conversion equations to be implemented are therefore
  447. * R = Y + 1.40200 * Cr
  448. * G = Y - 0.34414 * Cb - 0.71414 * Cr
  449. * B = Y + 1.77200 * Cb
  450. *
  451. ******************************************************************************/
  452. static unsigned char clamp(int i)
  453. {
  454. if (i<0)
  455. return 0;
  456. else if (i>255)
  457. return 255;
  458. else
  459. return i;
  460. }
  461. /**
  462. * YCrCb -> YUV420P (1x1)
  463. * .---.
  464. * | 1 |
  465. * `---'
  466. */
  467. static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
  468. {
  469. const unsigned char *s, *y;
  470. unsigned char *p;
  471. int i,j;
  472. p = priv->plane[0];
  473. y = priv->Y;
  474. for (i=0; i<8; i++)
  475. {
  476. memcpy(p, y, 8);
  477. p+=priv->width;
  478. y+=8;
  479. }
  480. p = priv->plane[1];
  481. s = priv->Cb;
  482. for (i=0; i<8; i+=2)
  483. {
  484. for (j=0; j<8; j+=2, s+=2)
  485. *p++ = *s;
  486. s += 8; /* Skip one line */
  487. p += priv->width/2 - 4;
  488. }
  489. p = priv->plane[2];
  490. s = priv->Cr;
  491. for (i=0; i<8; i+=2)
  492. {
  493. for (j=0; j<8; j+=2, s+=2)
  494. *p++ = *s;
  495. s += 8; /* Skip one line */
  496. p += priv->width/2 - 4;
  497. }
  498. }
  499. /**
  500. * YCrCb -> YUV420P (2x1)
  501. * .-------.
  502. * | 1 | 2 |
  503. * `-------'
  504. */
  505. static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
  506. {
  507. unsigned char *p;
  508. const unsigned char *s, *y1;
  509. unsigned int i;
  510. p = priv->plane[0];
  511. y1 = priv->Y;
  512. for (i=0; i<8; i++)
  513. {
  514. memcpy(p, y1, 16);
  515. p += priv->width;
  516. y1 += 16;
  517. }
  518. p = priv->plane[1];
  519. s = priv->Cb;
  520. for (i=0; i<8; i+=2)
  521. {
  522. memcpy(p, s, 8);
  523. s += 16; /* Skip one line */
  524. p += priv->width/2;
  525. }
  526. p = priv->plane[2];
  527. s = priv->Cr;
  528. for (i=0; i<8; i+=2)
  529. {
  530. memcpy(p, s, 8);
  531. s += 16; /* Skip one line */
  532. p += priv->width/2;
  533. }
  534. }
  535. /**
  536. * YCrCb -> YUV420P (1x2)
  537. * .---.
  538. * | 1 |
  539. * |---|
  540. * | 2 |
  541. * `---'
  542. */
  543. static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
  544. {
  545. const unsigned char *s, *y;
  546. unsigned char *p;
  547. int i,j;
  548. p = priv->plane[0];
  549. y = priv->Y;
  550. for (i=0; i<16; i++)
  551. {
  552. memcpy(p, y, 8);
  553. p+=priv->width;
  554. y+=8;
  555. }
  556. p = priv->plane[1];
  557. s = priv->Cb;
  558. for (i=0; i<8; i++)
  559. {
  560. for (j=0; j<8; j+=2, s+=2)
  561. *p++ = *s;
  562. p += priv->width/2 - 4;
  563. }
  564. p = priv->plane[2];
  565. s = priv->Cr;
  566. for (i=0; i<8; i++)
  567. {
  568. for (j=0; j<8; j+=2, s+=2)
  569. *p++ = *s;
  570. p += priv->width/2 - 4;
  571. }
  572. }
  573. /**
  574. * YCrCb -> YUV420P (2x2)
  575. * .-------.
  576. * | 1 | 2 |
  577. * |---+---|
  578. * | 3 | 4 |
  579. * `-------'
  580. */
  581. static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
  582. {
  583. unsigned char *p;
  584. const unsigned char *s, *y1;
  585. unsigned int i;
  586. p = priv->plane[0];
  587. y1 = priv->Y;
  588. for (i=0; i<16; i++)
  589. {
  590. memcpy(p, y1, 16);
  591. p += priv->width;
  592. y1 += 16;
  593. }
  594. p = priv->plane[1];
  595. s = priv->Cb;
  596. for (i=0; i<8; i++)
  597. {
  598. memcpy(p, s, 8);
  599. s += 8;
  600. p += priv->width/2;
  601. }
  602. p = priv->plane[2];
  603. s = priv->Cr;
  604. for (i=0; i<8; i++)
  605. {
  606. memcpy(p, s, 8);
  607. s += 8;
  608. p += priv->width/2;
  609. }
  610. }
  611. /**
  612. * YCrCb -> RGB24 (1x1)
  613. * .---.
  614. * | 1 |
  615. * `---'
  616. */
  617. static void YCrCB_to_RGB24_1x1(struct jdec_private *priv)
  618. {
  619. const unsigned char *Y, *Cb, *Cr;
  620. unsigned char *p;
  621. int i,j;
  622. int offset_to_next_row;
  623. #define SCALEBITS 10
  624. #define ONE_HALF (1UL << (SCALEBITS-1))
  625. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  626. p = priv->plane[0];
  627. Y = priv->Y;
  628. Cb = priv->Cb;
  629. Cr = priv->Cr;
  630. offset_to_next_row = priv->width*3 - 8*3;
  631. for (i=0; i<8; i++) {
  632. for (j=0; j<8; j++) {
  633. int y, cb, cr;
  634. int add_r, add_g, add_b;
  635. int r, g , b;
  636. y = (*Y++) << SCALEBITS;
  637. cb = *Cb++ - 128;
  638. cr = *Cr++ - 128;
  639. add_r = FIX(1.40200) * cr + ONE_HALF;
  640. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  641. add_b = FIX(1.77200) * cb + ONE_HALF;
  642. r = (y + add_r) >> SCALEBITS;
  643. g = (y + add_g) >> SCALEBITS;
  644. b = (y + add_b) >> SCALEBITS;
  645. priv->decomp_block[i][j*3+0]=clamp(r);
  646. priv->decomp_block[i][j*3+1]=clamp(g);
  647. priv->decomp_block[i][j*3+2]=clamp(b);
  648. }
  649. // p += offset_to_next_row;
  650. }
  651. #undef SCALEBITS
  652. #undef ONE_HALF
  653. #undef FIX
  654. }
  655. /**
  656. * YCrCb -> BGR24 (1x1)
  657. * .---.
  658. * | 1 |
  659. * `---'
  660. */
  661. static void YCrCB_to_BGR24_1x1(struct jdec_private *priv)
  662. {
  663. const unsigned char *Y, *Cb, *Cr;
  664. unsigned char *p;
  665. int i,j;
  666. int offset_to_next_row;
  667. #define SCALEBITS 10
  668. #define ONE_HALF (1UL << (SCALEBITS-1))
  669. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  670. p = priv->plane[0];
  671. Y = priv->Y;
  672. Cb = priv->Cb;
  673. Cr = priv->Cr;
  674. offset_to_next_row = priv->width*3 - 8*3;
  675. for (i=0; i<8; i++) {
  676. for (j=0; j<8; j++) {
  677. int y, cb, cr;
  678. int add_r, add_g, add_b;
  679. int r, g , b;
  680. y = (*Y++) << SCALEBITS;
  681. cb = *Cb++ - 128;
  682. cr = *Cr++ - 128;
  683. add_r = FIX(1.40200) * cr + ONE_HALF;
  684. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  685. add_b = FIX(1.77200) * cb + ONE_HALF;
  686. b = (y + add_b) >> SCALEBITS;
  687. *p++ = clamp(b);
  688. g = (y + add_g) >> SCALEBITS;
  689. *p++ = clamp(g);
  690. r = (y + add_r) >> SCALEBITS;
  691. *p++ = clamp(r);
  692. }
  693. p += offset_to_next_row;
  694. }
  695. #undef SCALEBITS
  696. #undef ONE_HALF
  697. #undef FIX
  698. }
  699. /**
  700. * YCrCb -> RGB24 (2x1)
  701. * .-------.
  702. * | 1 | 2 |
  703. * `-------'
  704. */
  705. static void YCrCB_to_RGB24_2x1(struct jdec_private *priv)
  706. {
  707. const unsigned char *Y, *Cb, *Cr;
  708. unsigned char *p;
  709. int i,j;
  710. int offset_to_next_row;
  711. #define SCALEBITS 10
  712. #define ONE_HALF (1UL << (SCALEBITS-1))
  713. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  714. p = priv->plane[0];
  715. Y = priv->Y;
  716. Cb = priv->Cb;
  717. Cr = priv->Cr;
  718. offset_to_next_row = priv->width*3 - 16*3;
  719. for (i=0; i<8; i++) {
  720. for (j=0; j<8; j++) {
  721. int y, cb, cr;
  722. int add_r, add_g, add_b;
  723. int r, g , b;
  724. y = (*Y++) << SCALEBITS;
  725. cb = *Cb++ - 128;
  726. cr = *Cr++ - 128;
  727. add_r = FIX(1.40200) * cr + ONE_HALF;
  728. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  729. add_b = FIX(1.77200) * cb + ONE_HALF;
  730. r = (y + add_r) >> SCALEBITS;
  731. g = (y + add_g) >> SCALEBITS;
  732. b = (y + add_b) >> SCALEBITS;
  733. priv->decomp_block[i][j*6+0]=clamp(r);
  734. priv->decomp_block[i][j*6+1]=clamp(g);
  735. priv->decomp_block[i][j*6+2]=clamp(b);
  736. y = (*Y++) << SCALEBITS;
  737. r = (y + add_r) >> SCALEBITS;
  738. g = (y + add_g) >> SCALEBITS;
  739. b = (y + add_b) >> SCALEBITS;
  740. priv->decomp_block[i][j*6+3]=clamp(r);
  741. priv->decomp_block[i][j*6+4]=clamp(g);
  742. priv->decomp_block[i][j*6+5]=clamp(b);
  743. }
  744. p += offset_to_next_row;
  745. }
  746. #undef SCALEBITS
  747. #undef ONE_HALF
  748. #undef FIX
  749. }
  750. /*
  751. * YCrCb -> BGR24 (2x1)
  752. * .-------.
  753. * | 1 | 2 |
  754. * `-------'
  755. */
  756. static void YCrCB_to_BGR24_2x1(struct jdec_private *priv)
  757. {
  758. const unsigned char *Y, *Cb, *Cr;
  759. unsigned char *p;
  760. int i,j;
  761. int offset_to_next_row;
  762. #define SCALEBITS 10
  763. #define ONE_HALF (1UL << (SCALEBITS-1))
  764. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  765. p = priv->plane[0];
  766. Y = priv->Y;
  767. Cb = priv->Cb;
  768. Cr = priv->Cr;
  769. offset_to_next_row = priv->width*3 - 16*3;
  770. for (i=0; i<8; i++) {
  771. for (j=0; j<8; j++) {
  772. int y, cb, cr;
  773. int add_r, add_g, add_b;
  774. int r, g , b;
  775. cb = *Cb++ - 128;
  776. cr = *Cr++ - 128;
  777. add_r = FIX(1.40200) * cr + ONE_HALF;
  778. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  779. add_b = FIX(1.77200) * cb + ONE_HALF;
  780. y = (*Y++) << SCALEBITS;
  781. b = (y + add_b) >> SCALEBITS;
  782. *p++ = clamp(b);
  783. g = (y + add_g) >> SCALEBITS;
  784. *p++ = clamp(g);
  785. r = (y + add_r) >> SCALEBITS;
  786. *p++ = clamp(r);
  787. y = (*Y++) << SCALEBITS;
  788. b = (y + add_b) >> SCALEBITS;
  789. *p++ = clamp(b);
  790. g = (y + add_g) >> SCALEBITS;
  791. *p++ = clamp(g);
  792. r = (y + add_r) >> SCALEBITS;
  793. *p++ = clamp(r);
  794. }
  795. p += offset_to_next_row;
  796. }
  797. #undef SCALEBITS
  798. #undef ONE_HALF
  799. #undef FIX
  800. }
  801. /**
  802. * YCrCb -> RGB24 (1x2)
  803. * .---.
  804. * | 1 |
  805. * |---|
  806. * | 2 |
  807. * `---'
  808. */
  809. static void YCrCB_to_RGB24_1x2(struct jdec_private *priv)
  810. {
  811. const unsigned char *Y, *Cb, *Cr;
  812. unsigned char *p, *p2;
  813. int i,j;
  814. int offset_to_next_row;
  815. #define SCALEBITS 10
  816. #define ONE_HALF (1UL << (SCALEBITS-1))
  817. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  818. p = priv->plane[0];
  819. p2 = priv->plane[0] + priv->width*3;
  820. Y = priv->Y;
  821. Cb = priv->Cb;
  822. Cr = priv->Cr;
  823. offset_to_next_row = 2*priv->width*3 - 8*3;
  824. for (i=0; i<8; i++) {
  825. for (j=0; j<8; j++) {
  826. int y, cb, cr;
  827. int add_r, add_g, add_b;
  828. int r, g , b;
  829. cb = *Cb++ - 128;
  830. cr = *Cr++ - 128;
  831. add_r = FIX(1.40200) * cr + ONE_HALF;
  832. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  833. add_b = FIX(1.77200) * cb + ONE_HALF;
  834. y = (*Y++) << SCALEBITS;
  835. r = (y + add_r) >> SCALEBITS;
  836. g = (y + add_g) >> SCALEBITS;
  837. b = (y + add_b) >> SCALEBITS;
  838. priv->decomp_block[i*2][j*3+0]=clamp(r);
  839. priv->decomp_block[i*2][j*3+1]=clamp(g);
  840. priv->decomp_block[i*2][j*3+2]=clamp(b);
  841. y = (Y[8-1]) << SCALEBITS;
  842. r = (y + add_r) >> SCALEBITS;
  843. g = (y + add_g) >> SCALEBITS;
  844. b = (y + add_b) >> SCALEBITS;
  845. priv->decomp_block[i*2+1][j*3+0]=clamp(r);
  846. priv->decomp_block[i*2+1][j*3+1]=clamp(g);
  847. priv->decomp_block[i*2+1][j*3+2]=clamp(b);
  848. }
  849. Y += 8;
  850. p += offset_to_next_row;
  851. p2 += offset_to_next_row;
  852. }
  853. #undef SCALEBITS
  854. #undef ONE_HALF
  855. #undef FIX
  856. }
  857. /*
  858. * YCrCb -> BGR24 (1x2)
  859. * .---.
  860. * | 1 |
  861. * |---|
  862. * | 2 |
  863. * `---'
  864. */
  865. static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
  866. {
  867. const unsigned char *Y, *Cb, *Cr;
  868. unsigned char *p, *p2;
  869. int i,j;
  870. int offset_to_next_row;
  871. #define SCALEBITS 10
  872. #define ONE_HALF (1UL << (SCALEBITS-1))
  873. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  874. p = priv->plane[0];
  875. p2 = priv->plane[0] + priv->width*3;
  876. Y = priv->Y;
  877. Cb = priv->Cb;
  878. Cr = priv->Cr;
  879. offset_to_next_row = 2*priv->width*3 - 8*3;
  880. for (i=0; i<8; i++) {
  881. for (j=0; j<8; j++) {
  882. int y, cb, cr;
  883. int add_r, add_g, add_b;
  884. int r, g , b;
  885. cb = *Cb++ - 128;
  886. cr = *Cr++ - 128;
  887. add_r = FIX(1.40200) * cr + ONE_HALF;
  888. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  889. add_b = FIX(1.77200) * cb + ONE_HALF;
  890. y = (*Y++) << SCALEBITS;
  891. b = (y + add_b) >> SCALEBITS;
  892. *p++ = clamp(b);
  893. g = (y + add_g) >> SCALEBITS;
  894. *p++ = clamp(g);
  895. r = (y + add_r) >> SCALEBITS;
  896. *p++ = clamp(r);
  897. y = (Y[8-1]) << SCALEBITS;
  898. b = (y + add_b) >> SCALEBITS;
  899. *p2++ = clamp(b);
  900. g = (y + add_g) >> SCALEBITS;
  901. *p2++ = clamp(g);
  902. r = (y + add_r) >> SCALEBITS;
  903. *p2++ = clamp(r);
  904. }
  905. Y += 8;
  906. p += offset_to_next_row;
  907. p2 += offset_to_next_row;
  908. }
  909. #undef SCALEBITS
  910. #undef ONE_HALF
  911. #undef FIX
  912. }
  913. /**
  914. * YCrCb -> RGB24 (2x2)
  915. * .-------.
  916. * | 1 | 2 |
  917. * |---+---|
  918. * | 3 | 4 |
  919. * `-------'
  920. */
  921. static void YCrCB_to_RGB24_2x2(struct jdec_private *priv)
  922. {
  923. const unsigned char *Y, *Cb, *Cr;
  924. unsigned char *p, *p2;
  925. int i,j;
  926. int offset_to_next_row;
  927. #define SCALEBITS 10
  928. #define ONE_HALF (1UL << (SCALEBITS-1))
  929. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  930. p = priv->plane[0];
  931. p2 = priv->plane[0] + priv->width*3;
  932. Y = priv->Y;
  933. Cb = priv->Cb;
  934. Cr = priv->Cr;
  935. offset_to_next_row = (priv->width*3*2) - 16*3;
  936. for (i=0; i<8; i++) {
  937. for (j=0; j<8; j++) {
  938. int y, cb, cr;
  939. int add_r, add_g, add_b;
  940. int r, g , b;
  941. cb = *Cb++ - 128;
  942. cr = *Cr++ - 128;
  943. add_r = FIX(1.40200) * cr + ONE_HALF;
  944. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  945. add_b = FIX(1.77200) * cb + ONE_HALF;
  946. y = (*Y++) << SCALEBITS;
  947. r = (y + add_r) >> SCALEBITS;
  948. g = (y + add_g) >> SCALEBITS;
  949. b = (y + add_b) >> SCALEBITS;
  950. priv->decomp_block[i*2][j*6+0]=clamp(r);
  951. priv->decomp_block[i*2][j*6+1]=clamp(g);
  952. priv->decomp_block[i*2][j*6+2]=clamp(b);
  953. y = (*Y++) << SCALEBITS;
  954. r = (y + add_r) >> SCALEBITS;
  955. g = (y + add_g) >> SCALEBITS;
  956. b = (y + add_b) >> SCALEBITS;
  957. priv->decomp_block[i*2][j*6+3]=clamp(r);
  958. priv->decomp_block[i*2][j*6+4]=clamp(g);
  959. priv->decomp_block[i*2][j*6+5]=clamp(b);
  960. y = (Y[16-2]) << SCALEBITS;
  961. r = (y + add_r) >> SCALEBITS;
  962. g = (y + add_g) >> SCALEBITS;
  963. b = (y + add_b) >> SCALEBITS;
  964. priv->decomp_block[i*2+1][j*6+0]=clamp(r);
  965. priv->decomp_block[i*2+1][j*6+1]=clamp(g);
  966. priv->decomp_block[i*2+1][j*6+2]=clamp(b);
  967. y = (Y[16-1]) << SCALEBITS;
  968. r = (y + add_r) >> SCALEBITS;
  969. g = (y + add_g) >> SCALEBITS;
  970. b = (y + add_b) >> SCALEBITS;
  971. priv->decomp_block[i*2+1][j*6+3]=clamp(r);
  972. priv->decomp_block[i*2+1][j*6+4]=clamp(g);
  973. priv->decomp_block[i*2+1][j*6+5]=clamp(b);
  974. }
  975. Y += 16;
  976. p += offset_to_next_row;
  977. p2 += offset_to_next_row;
  978. }
  979. #undef SCALEBITS
  980. #undef ONE_HALF
  981. #undef FIX
  982. }
  983. /*
  984. * YCrCb -> BGR24 (2x2)
  985. * .-------.
  986. * | 1 | 2 |
  987. * |---+---|
  988. * | 3 | 4 |
  989. * `-------'
  990. */
  991. static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
  992. {
  993. const unsigned char *Y, *Cb, *Cr;
  994. unsigned char *p, *p2;
  995. int i,j;
  996. int offset_to_next_row;
  997. #define SCALEBITS 10
  998. #define ONE_HALF (1UL << (SCALEBITS-1))
  999. #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
  1000. p = priv->plane[0];
  1001. p2 = priv->plane[0] + priv->width*3;
  1002. Y = priv->Y;
  1003. Cb = priv->Cb;
  1004. Cr = priv->Cr;
  1005. offset_to_next_row = (priv->width*3*2) - 16*3;
  1006. for (i=0; i<8; i++) {
  1007. for (j=0; j<8; j++) {
  1008. int y, cb, cr;
  1009. int add_r, add_g, add_b;
  1010. int r, g , b;
  1011. cb = *Cb++ - 128;
  1012. cr = *Cr++ - 128;
  1013. add_r = FIX(1.40200) * cr + ONE_HALF;
  1014. add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
  1015. add_b = FIX(1.77200) * cb + ONE_HALF;
  1016. y = (*Y++) << SCALEBITS;
  1017. b = (y + add_b) >> SCALEBITS;
  1018. *p++ = clamp(b);
  1019. g = (y + add_g) >> SCALEBITS;
  1020. *p++ = clamp(g);
  1021. r = (y + add_r) >> SCALEBITS;
  1022. *p++ = clamp(r);
  1023. y = (*Y++) << SCALEBITS;
  1024. b = (y + add_b) >> SCALEBITS;
  1025. *p++ = clamp(b);
  1026. g = (y + add_g) >> SCALEBITS;
  1027. *p++ = clamp(g);
  1028. r = (y + add_r) >> SCALEBITS;
  1029. *p++ = clamp(r);
  1030. y = (Y[16-2]) << SCALEBITS;
  1031. b = (y + add_b) >> SCALEBITS;
  1032. *p2++ = clamp(b);
  1033. g = (y + add_g) >> SCALEBITS;
  1034. *p2++ = clamp(g);
  1035. r = (y + add_r) >> SCALEBITS;
  1036. *p2++ = clamp(r);
  1037. y = (Y[16-1]) << SCALEBITS;
  1038. b = (y + add_b) >> SCALEBITS;
  1039. *p2++ = clamp(b);
  1040. g = (y + add_g) >> SCALEBITS;
  1041. *p2++ = clamp(g);
  1042. r = (y + add_r) >> SCALEBITS;
  1043. *p2++ = clamp(r);
  1044. }
  1045. Y += 16;
  1046. p += offset_to_next_row;
  1047. p2 += offset_to_next_row;
  1048. }
  1049. #undef SCALEBITS
  1050. #undef ONE_HALF
  1051. #undef FIX
  1052. }
  1053. /**
  1054. * YCrCb -> Grey (1x1)
  1055. * .---.
  1056. * | 1 |
  1057. * `---'
  1058. */
  1059. static void YCrCB_to_Grey_1x1(struct jdec_private *priv)
  1060. {
  1061. const unsigned char *y;
  1062. unsigned char *p;
  1063. unsigned int i;
  1064. int offset_to_next_row;
  1065. p = priv->plane[0];
  1066. y = priv->Y;
  1067. offset_to_next_row = priv->width;
  1068. for (i=0; i<8; i++) {
  1069. memcpy(p, y, 8);
  1070. y+=8;
  1071. p += offset_to_next_row;
  1072. }
  1073. }
  1074. /**
  1075. * YCrCb -> Grey (2x1)
  1076. * .-------.
  1077. * | 1 | 2 |
  1078. * `-------'
  1079. */
  1080. static void YCrCB_to_Grey_2x1(struct jdec_private *priv)
  1081. {
  1082. const unsigned char *y;
  1083. unsigned char *p;
  1084. unsigned int i;
  1085. p = priv->plane[0];
  1086. y = priv->Y;
  1087. for (i=0; i<8; i++) {
  1088. memcpy(p, y, 16);
  1089. y += 16;
  1090. p += priv->width;
  1091. }
  1092. }
  1093. /**
  1094. * YCrCb -> Grey (1x2)
  1095. * .---.
  1096. * | 1 |
  1097. * |---|
  1098. * | 2 |
  1099. * `---'
  1100. */
  1101. static void YCrCB_to_Grey_1x2(struct jdec_private *priv)
  1102. {
  1103. const unsigned char *y;
  1104. unsigned char *p;
  1105. unsigned int i;
  1106. p = priv->plane[0];
  1107. y = priv->Y;
  1108. for (i=0; i<16; i++) {
  1109. memcpy(p, y, 8);
  1110. y += 8;
  1111. p += priv->width;
  1112. }
  1113. }
  1114. /**
  1115. * YCrCb -> Grey (2x2)
  1116. * .-------.
  1117. * | 1 | 2 |
  1118. * |---+---|
  1119. * | 3 | 4 |
  1120. * `-------'
  1121. */
  1122. static void YCrCB_to_Grey_2x2(struct jdec_private *priv)
  1123. {
  1124. const unsigned char *y;
  1125. unsigned char *p;
  1126. unsigned int i;
  1127. p = priv->plane[0];
  1128. y = priv->Y;
  1129. for (i=0; i<16; i++) {
  1130. memcpy(p, y, 16);
  1131. y += 16;
  1132. p += priv->width;
  1133. }
  1134. }
  1135. /*
  1136. * Decode all the 3 components for 1x1
  1137. */
  1138. static void decode_MCU_1x1_3planes(struct jdec_private *priv)
  1139. {
  1140. // Y
  1141. process_Huffman_data_unit(priv, cY);
  1142. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1143. // Cb
  1144. process_Huffman_data_unit(priv, cCb);
  1145. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1146. // Cr
  1147. process_Huffman_data_unit(priv, cCr);
  1148. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1149. }
  1150. /*
  1151. * Decode a 1x1 directly in 1 color
  1152. */
  1153. static void decode_MCU_1x1_1plane(struct jdec_private *priv)
  1154. {
  1155. // Y
  1156. process_Huffman_data_unit(priv, cY);
  1157. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1158. // Cb
  1159. process_Huffman_data_unit(priv, cCb);
  1160. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1161. // Cr
  1162. process_Huffman_data_unit(priv, cCr);
  1163. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1164. }
  1165. /*
  1166. * Decode a 2x1
  1167. * .-------.
  1168. * | 1 | 2 |
  1169. * `-------'
  1170. */
  1171. static void decode_MCU_2x1_3planes(struct jdec_private *priv)
  1172. {
  1173. // Y
  1174. process_Huffman_data_unit(priv, cY);
  1175. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1176. process_Huffman_data_unit(priv, cY);
  1177. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1178. // Cb
  1179. process_Huffman_data_unit(priv, cCb);
  1180. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1181. // Cr
  1182. process_Huffman_data_unit(priv, cCr);
  1183. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1184. }
  1185. /*
  1186. * Decode a 2x1
  1187. * .-------.
  1188. * | 1 | 2 |
  1189. * `-------'
  1190. */
  1191. static void decode_MCU_2x1_1plane(struct jdec_private *priv)
  1192. {
  1193. // Y
  1194. process_Huffman_data_unit(priv, cY);
  1195. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1196. process_Huffman_data_unit(priv, cY);
  1197. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1198. // Cb
  1199. process_Huffman_data_unit(priv, cCb);
  1200. // Cr
  1201. process_Huffman_data_unit(priv, cCr);
  1202. }
  1203. /*
  1204. * Decode a 2x2
  1205. * .-------.
  1206. * | 1 | 2 |
  1207. * |---+---|
  1208. * | 3 | 4 |
  1209. * `-------'
  1210. */
  1211. static void decode_MCU_2x2_3planes(struct jdec_private *priv)
  1212. {
  1213. // Y
  1214. process_Huffman_data_unit(priv, cY);
  1215. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1216. process_Huffman_data_unit(priv, cY);
  1217. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1218. process_Huffman_data_unit(priv, cY);
  1219. IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
  1220. process_Huffman_data_unit(priv, cY);
  1221. IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
  1222. // Cb
  1223. process_Huffman_data_unit(priv, cCb);
  1224. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1225. // Cr
  1226. process_Huffman_data_unit(priv, cCr);
  1227. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1228. }
  1229. /*
  1230. * Decode a 2x2 directly in GREY format (8bits)
  1231. * .-------.
  1232. * | 1 | 2 |
  1233. * |---+---|
  1234. * | 3 | 4 |
  1235. * `-------'
  1236. */
  1237. static void decode_MCU_2x2_1plane(struct jdec_private *priv)
  1238. {
  1239. // Y
  1240. process_Huffman_data_unit(priv, cY);
  1241. IDCT(&priv->component_infos[cY], priv->Y, 16);
  1242. process_Huffman_data_unit(priv, cY);
  1243. IDCT(&priv->component_infos[cY], priv->Y+8, 16);
  1244. process_Huffman_data_unit(priv, cY);
  1245. IDCT(&priv->component_infos[cY], priv->Y+64*2, 16);
  1246. process_Huffman_data_unit(priv, cY);
  1247. IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16);
  1248. // Cb
  1249. process_Huffman_data_unit(priv, cCb);
  1250. // Cr
  1251. process_Huffman_data_unit(priv, cCr);
  1252. }
  1253. /*
  1254. * Decode a 1x2 mcu
  1255. * .---.
  1256. * | 1 |
  1257. * |---|
  1258. * | 2 |
  1259. * `---'
  1260. */
  1261. static void decode_MCU_1x2_3planes(struct jdec_private *priv)
  1262. {
  1263. // Y
  1264. process_Huffman_data_unit(priv, cY);
  1265. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1266. process_Huffman_data_unit(priv, cY);
  1267. IDCT(&priv->component_infos[cY], priv->Y+64, 8);
  1268. // Cb
  1269. process_Huffman_data_unit(priv, cCb);
  1270. IDCT(&priv->component_infos[cCb], priv->Cb, 8);
  1271. // Cr
  1272. process_Huffman_data_unit(priv, cCr);
  1273. IDCT(&priv->component_infos[cCr], priv->Cr, 8);
  1274. }
  1275. /*
  1276. * Decode a 1x2 mcu
  1277. * .---.
  1278. * | 1 |
  1279. * |---|
  1280. * | 2 |
  1281. * `---'
  1282. */
  1283. static void decode_MCU_1x2_1plane(struct jdec_private *priv)
  1284. {
  1285. // Y
  1286. process_Huffman_data_unit(priv, cY);
  1287. IDCT(&priv->component_infos[cY], priv->Y, 8);
  1288. process_Huffman_data_unit(priv, cY);
  1289. IDCT(&priv->component_infos[cY], priv->Y+64, 8);
  1290. // Cb
  1291. process_Huffman_data_unit(priv, cCb);
  1292. // Cr
  1293. process_Huffman_data_unit(priv, cCr);
  1294. }
  1295. static void print_SOF(const unsigned char *stream)
  1296. {
  1297. int width, height, nr_components, precision;
  1298. #if DEBUG
  1299. const char *nr_components_to_string[] = {
  1300. "????",
  1301. "Grayscale",
  1302. "????",
  1303. "YCbCr",
  1304. "CYMK"
  1305. };
  1306. #endif
  1307. precision = stream[2];
  1308. height = be16_to_cpu(stream+3);
  1309. width = be16_to_cpu(stream+5);
  1310. nr_components = stream[7];
  1311. trace("> SOF marker\n");
  1312. trace("Size:%dx%d nr_components:%d (%s) precision:%d\n",
  1313. width, height,
  1314. nr_components, nr_components_to_string[nr_components],
  1315. precision);
  1316. }
  1317. /*******************************************************************************
  1318. *
  1319. * JPEG/JFIF Parsing functions
  1320. *
  1321. * Note: only a small subset of the jpeg file format is supported. No markers,
  1322. * nor progressive stream is supported.
  1323. *
  1324. ******************************************************************************/
  1325. static void build_quantization_table(float *qtable, const unsigned char *ref_table)
  1326. {
  1327. /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
  1328. * For float AA&N IDCT method, divisors are equal to quantization
  1329. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  1330. * scalefactor[0] = 1
  1331. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  1332. * We apply a further scale factor of 8.
  1333. * What's actually stored is 1/divisor so that the inner loop can
  1334. * use a multiplication rather than a division.
  1335. */
  1336. int i, j;
  1337. static const double aanscalefactor[8] = {
  1338. 1.0, 1.387039845, 1.306562965, 1.175875602,
  1339. 1.0, 0.785694958, 0.541196100, 0.275899379
  1340. };
  1341. const unsigned char *zz = zigzag;
  1342. for (i=0; i<8; i++) {
  1343. for (j=0; j<8; j++) {
  1344. *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
  1345. }
  1346. }
  1347. }
  1348. static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
  1349. {
  1350. int qi;
  1351. float *table;
  1352. const unsigned char *dqt_block_end;
  1353. trace("> DQT marker\n");
  1354. dqt_block_end = stream + be16_to_cpu(stream);
  1355. stream += 2; /* Skip length */
  1356. while (stream < dqt_block_end)
  1357. {
  1358. qi = *stream++;
  1359. #if SANITY_CHECK
  1360. if (qi>>4)
  1361. error("16 bits quantization table is not supported\n");
  1362. if (qi>4)
  1363. error("No more 4 quantization table is supported (got %d)\n", qi);
  1364. #endif
  1365. table = priv->Q_tables[qi];
  1366. build_quantization_table(table, stream);
  1367. stream += 64;
  1368. }
  1369. trace("< DQT marker\n");
  1370. return 0;
  1371. }
  1372. static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
  1373. {
  1374. int i, width, height, nr_components, cid, sampling_factor;
  1375. int Q_table;
  1376. struct component *c;
  1377. trace("> SOF marker\n");
  1378. print_SOF(stream);
  1379. height = be16_to_cpu(stream+3);
  1380. width = be16_to_cpu(stream+5);
  1381. nr_components = stream[7];
  1382. #if SANITY_CHECK
  1383. if (stream[2] != 8)
  1384. error("Precision other than 8 is not supported\n");
  1385. if (width>JPEG_MAX_WIDTH || height>JPEG_MAX_HEIGHT)
  1386. error("Width and Height (%dx%d) seems suspicious\n", width, height);
  1387. if (nr_components != 3)
  1388. error("We only support YUV images\n");
  1389. //if (height%16)
  1390. // error("Height need to be a multiple of 16 (current height is %d)\n", height);
  1391. // if (width%16)
  1392. // error("Width need to be a multiple of 16 (current Width is %d)\n", width);
  1393. #endif
  1394. stream += 8;
  1395. for (i=0; i<nr_components; i++) {
  1396. cid = *stream++;
  1397. sampling_factor = *stream++;
  1398. Q_table = *stream++;
  1399. c = &priv->component_infos[i];
  1400. #if SANITY_CHECK
  1401. c->cid = cid;
  1402. if (Q_table >= COMPONENTS)
  1403. error("Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1);
  1404. #endif
  1405. c->Vfactor = sampling_factor&0xf;
  1406. c->Hfactor = sampling_factor>>4;
  1407. c->Q_table = priv->Q_tables[Q_table];
  1408. trace("Component:%d factor:%dx%d Quantization table:%d\n",
  1409. cid, c->Hfactor, c->Hfactor, Q_table );
  1410. }
  1411. priv->width = width;
  1412. priv->height = height;
  1413. trace("< SOF marker\n");
  1414. return 0;
  1415. }
  1416. static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
  1417. {
  1418. unsigned int i, cid, table;
  1419. unsigned int nr_components = stream[2];
  1420. trace("> SOS marker\n");
  1421. #if SANITY_CHECK
  1422. if (nr_components != 3)
  1423. error("We only support YCbCr image\n");
  1424. #endif
  1425. stream += 3;
  1426. for (i=0;i<nr_components;i++) {
  1427. cid = *stream++;
  1428. table = *stream++;
  1429. #if SANITY_CHECK
  1430. if ((table&0xf)>=4)
  1431. error("We do not support more than 2 AC Huffman table\n");
  1432. if ((table>>4)>=4)
  1433. error("We do not support more than 2 DC Huffman table\n");
  1434. if (cid != priv->component_infos[i].cid)
  1435. error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n",
  1436. i, cid, i, priv->component_infos[i].cid);
  1437. trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4);
  1438. #endif
  1439. priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
  1440. priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
  1441. }
  1442. priv->stream = stream+3;
  1443. trace("< SOS marker\n");
  1444. return 0;
  1445. }
  1446. static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
  1447. {
  1448. unsigned int count, i;
  1449. unsigned char huff_bits[17];
  1450. int length, index;
  1451. length = be16_to_cpu(stream) - 2;
  1452. stream += 2; /* Skip length */
  1453. trace("> DHT marker (length=%d)\n", length);
  1454. while (length>0) {
  1455. index = *stream++;
  1456. /* We need to calculate the number of bytes 'vals' will takes */
  1457. huff_bits[0] = 0;
  1458. count = 0;
  1459. for (i=1; i<17; i++) {
  1460. huff_bits[i] = *stream++;
  1461. count += huff_bits[i];
  1462. }
  1463. #if SANITY_CHECK
  1464. if (count >= HUFFMAN_BITS_SIZE)
  1465. error("No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE);
  1466. if ( (index &0xf) >= HUFFMAN_TABLES)
  1467. error("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf);
  1468. trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
  1469. #endif
  1470. if (index & 0xf0 )
  1471. build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]);
  1472. else
  1473. build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]);
  1474. length -= 1;
  1475. length -= 16;
  1476. length -= count;
  1477. stream += count;
  1478. }
  1479. trace("< DHT marker\n");
  1480. return 0;
  1481. }
  1482. static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
  1483. {
  1484. unsigned int length;
  1485. trace("> DRI marker\n");
  1486. length = be16_to_cpu(stream);
  1487. #if SANITY_CHECK
  1488. if (length != 4)
  1489. error("Length of DRI marker need to be 4\n");
  1490. #endif
  1491. priv->restart_interval = be16_to_cpu(stream+2);
  1492. #if DEBUG
  1493. trace("Restart interval = %d\n", priv->restart_interval);
  1494. #endif
  1495. trace("< DRI marker\n");
  1496. return 0;
  1497. }
  1498. static void resync(struct jdec_private *priv)
  1499. {
  1500. int i;
  1501. /* Init DC coefficients */
  1502. for (i=0; i<COMPONENTS; i++)
  1503. priv->component_infos[i].previous_DC = 0;
  1504. priv->reservoir = 0;
  1505. priv->nbits_in_reservoir = 0;
  1506. if (priv->restart_interval > 0)
  1507. priv->restarts_to_go = priv->restart_interval;
  1508. else
  1509. priv->restarts_to_go = -1;
  1510. }
  1511. static int find_next_rst_marker(struct jdec_private *priv)
  1512. {
  1513. int rst_marker_found = 0;
  1514. int marker;
  1515. const unsigned char *stream = priv->stream;
  1516. /* Parse marker */
  1517. while (!rst_marker_found)
  1518. {
  1519. while (*stream++ != 0xff)
  1520. {
  1521. if (stream >= priv->stream_end)
  1522. error("EOF while search for a RST marker.");
  1523. }
  1524. /* Skip any padding ff byte (this is normal) */
  1525. while (*stream == 0xff)
  1526. stream++;
  1527. marker = *stream++;
  1528. if ((RST+priv->last_rst_marker_seen) == marker)
  1529. rst_marker_found = 1;
  1530. else if (marker >= RST && marker <= RST7)
  1531. error("Wrong Reset marker found, abording");
  1532. else if (marker == EOI)
  1533. return 0;
  1534. }
  1535. trace("RST Marker %d found at offset %d\n", priv->last_rst_marker_seen, stream - priv->stream_begin);
  1536. priv->stream = stream;
  1537. priv->last_rst_marker_seen++;
  1538. priv->last_rst_marker_seen &= 7;
  1539. return 0;
  1540. }
  1541. static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
  1542. {
  1543. int chuck_len;
  1544. int marker;
  1545. int sos_marker_found = 0;
  1546. int dht_marker_found = 0;
  1547. const unsigned char *next_chunck;
  1548. /* Parse marker */
  1549. while (!sos_marker_found)
  1550. {
  1551. if (*stream++ != 0xff)
  1552. goto bogus_jpeg_format;
  1553. /* Skip any padding ff byte (this is normal) */
  1554. while (*stream == 0xff)
  1555. stream++;
  1556. marker = *stream++;
  1557. chuck_len = be16_to_cpu(stream);
  1558. next_chunck = stream + chuck_len;
  1559. switch (marker)
  1560. {
  1561. case SOF:
  1562. if (parse_SOF(priv, stream) < 0)
  1563. return -1;
  1564. break;
  1565. case DQT:
  1566. if (parse_DQT(priv, stream) < 0)
  1567. return -1;
  1568. break;
  1569. case SOS:
  1570. if (parse_SOS(priv, stream) < 0)
  1571. return -1;
  1572. sos_marker_found = 1;
  1573. break;
  1574. case DHT:
  1575. if (parse_DHT(priv, stream) < 0)
  1576. return -1;
  1577. dht_marker_found = 1;
  1578. break;
  1579. case DRI:
  1580. if (parse_DRI(priv, stream) < 0)
  1581. return -1;
  1582. break;
  1583. default:
  1584. trace("> Unknown marker %2.2x\n", marker);
  1585. break;
  1586. }
  1587. stream = next_chunck;
  1588. }
  1589. if (!dht_marker_found) {
  1590. trace("No Huffman table loaded, using the default one\n");
  1591. build_default_huffman_tables(priv);
  1592. }
  1593. #ifdef SANITY_CHECK
  1594. if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
  1595. || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
  1596. error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
  1597. if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
  1598. || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
  1599. error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
  1600. if ( (priv->component_infos[cCb].Hfactor!=1)
  1601. || (priv->component_infos[cCr].Hfactor!=1)
  1602. || (priv->component_infos[cCb].Vfactor!=1)
  1603. || (priv->component_infos[cCr].Vfactor!=1))
  1604. error("Sampling other than 1x1 for Cr and Cb is not supported");
  1605. #endif
  1606. return 0;
  1607. bogus_jpeg_format:
  1608. trace("Bogus jpeg format\n");
  1609. return -1;
  1610. }
  1611. /*******************************************************************************
  1612. *
  1613. * Functions exported of the library.
  1614. *
  1615. * Note: Some applications can access directly to internal pointer of the
  1616. * structure. It's is not recommended, but if you have many images to
  1617. * uncompress with the same parameters, some functions can be called to speedup
  1618. * the decoding.
  1619. *
  1620. ******************************************************************************/
  1621. /**
  1622. * Allocate a new tinyjpeg decoder object.
  1623. *
  1624. * Before calling any other functions, an object need to be called.
  1625. */
  1626. struct jdec_private *tinyjpeg_init(void *(*allocate_mem)(unsigned int),void (*free_mem)(void *))
  1627. {
  1628. struct jdec_private *priv;
  1629. unsigned int i;
  1630. priv = (struct jdec_private *)allocate_mem(sizeof(struct jdec_private));
  1631. for(i=0;i<sizeof(struct jdec_private);i++) {
  1632. char *pzero = (char*)priv;
  1633. pzero[i]=0;
  1634. }
  1635. priv->allocate_mem=allocate_mem;
  1636. priv->free_mem=free_mem;
  1637. if (priv == NULL)
  1638. return NULL;
  1639. return priv;
  1640. }
  1641. /**
  1642. * Free a tinyjpeg object.
  1643. *
  1644. * No others function can be called after this one.
  1645. */
  1646. void tinyjpeg_free(struct jdec_private *priv)
  1647. {
  1648. int i;
  1649. for (i=0; i<COMPONENTS; i++) {
  1650. if (priv->components[i]) {
  1651. // priv->free_mem(priv->components[i]);
  1652. priv->components[i] = NULL;
  1653. }
  1654. }
  1655. priv->free_mem(priv);
  1656. }
  1657. /**
  1658. * Initialize the tinyjpeg object and prepare the decoding of the stream.
  1659. *
  1660. * Check if the jpeg can be decoded with this jpeg decoder.
  1661. * Fill some table used for preprocessing.
  1662. */
  1663. int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
  1664. {
  1665. int ret;
  1666. /* Identify the file */
  1667. if ((buf[0] != 0xFF) || (buf[1] != SOI))
  1668. error("Not a JPG file ?\n");
  1669. priv->stream_begin = buf+2;
  1670. priv->stream_length = size-2;
  1671. priv->stream_end = priv->stream_begin + priv->stream_length;
  1672. ret = parse_JFIF(priv, priv->stream_begin);
  1673. return ret;
  1674. }
  1675. static const decode_MCU_fct decode_mcu_3comp_table[4] = {
  1676. decode_MCU_1x1_3planes,
  1677. decode_MCU_1x2_3planes,
  1678. decode_MCU_2x1_3planes,
  1679. decode_MCU_2x2_3planes,
  1680. };
  1681. static const decode_MCU_fct decode_mcu_1comp_table[4] = {
  1682. decode_MCU_1x1_1plane,
  1683. decode_MCU_1x2_1plane,
  1684. decode_MCU_2x1_1plane,
  1685. decode_MCU_2x2_1plane,
  1686. };
  1687. static const convert_colorspace_fct convert_colorspace_yuv420p[4] = {
  1688. YCrCB_to_YUV420P_1x1,
  1689. YCrCB_to_YUV420P_1x2,
  1690. YCrCB_to_YUV420P_2x1,
  1691. YCrCB_to_YUV420P_2x2,
  1692. };
  1693. static const convert_colorspace_fct convert_colorspace_rgb24[4] = {
  1694. YCrCB_to_RGB24_1x1,
  1695. YCrCB_to_RGB24_1x2,
  1696. YCrCB_to_RGB24_2x1,
  1697. YCrCB_to_RGB24_2x2,
  1698. };
  1699. static const convert_colorspace_fct convert_colorspace_bgr24[4] = {
  1700. YCrCB_to_BGR24_1x1,
  1701. YCrCB_to_BGR24_1x2,
  1702. YCrCB_to_BGR24_2x1,
  1703. YCrCB_to_BGR24_2x2,
  1704. };
  1705. static const convert_colorspace_fct convert_colorspace_grey[4] = {
  1706. YCrCB_to_Grey_1x1,
  1707. YCrCB_to_Grey_1x2,
  1708. YCrCB_to_Grey_2x1,
  1709. YCrCB_to_Grey_2x2,
  1710. };
  1711. /**
  1712. * Decode and convert the jpeg image into @pixfmt@ image
  1713. *
  1714. * Note: components will be automaticaly allocated if no memory is attached.
  1715. */
  1716. int tinyjpeg_decode(struct jdec_private *priv, int pixfmt)
  1717. {
  1718. unsigned int x, y, xstride_by_mcu, ystride_by_mcu;
  1719. unsigned int bytes_per_blocklines[3], bytes_per_mcu[3];
  1720. decode_MCU_fct decode_MCU;
  1721. const decode_MCU_fct *decode_mcu_table;
  1722. const convert_colorspace_fct *colorspace_array_conv;
  1723. convert_colorspace_fct convert_to_pixfmt;
  1724. if (pixfmt!=TINYJPEG_FMT_RGB24)
  1725. error("Only TINYJPEG_FMT_RGB24 is supported in this version");
  1726. if (setjmp(priv->jump_state))
  1727. return -1;
  1728. /* To keep gcc happy initialize some array */
  1729. bytes_per_mcu[1] = 0;
  1730. bytes_per_mcu[2] = 0;
  1731. bytes_per_blocklines[1] = 0;
  1732. bytes_per_blocklines[2] = 0;
  1733. decode_mcu_table = decode_mcu_3comp_table;
  1734. switch (pixfmt) {
  1735. case TINYJPEG_FMT_YUV420P:
  1736. colorspace_array_conv = convert_colorspace_yuv420p;
  1737. if (priv->components[0] == NULL)
  1738. priv->components[0] = (uint8_t *)priv->allocate_mem(priv->width * priv->height);
  1739. if (priv->components[1] == NULL)
  1740. priv->components[1] = (uint8_t *)priv->allocate_mem(priv->width * priv->height/4);
  1741. if (priv->components[2] == NULL)
  1742. priv->components[2] = (uint8_t *)priv->allocate_mem(priv->width * priv->height/4);
  1743. bytes_per_blocklines[0] = priv->width;
  1744. bytes_per_blocklines[1] = priv->width/4;
  1745. bytes_per_blocklines[2] = priv->width/4;
  1746. bytes_per_mcu[0] = 8;
  1747. bytes_per_mcu[1] = 4;
  1748. bytes_per_mcu[2] = 4;
  1749. break;
  1750. case TINYJPEG_FMT_RGB24:
  1751. colorspace_array_conv = convert_colorspace_rgb24;
  1752. if (priv->components[0] == NULL)
  1753. priv->components[0] = (uint8_t *)priv->allocate_mem(priv->width * priv->height * 3);
  1754. bytes_per_blocklines[0] = priv->width * 3;
  1755. bytes_per_mcu[0] = 3*8;
  1756. break;
  1757. case TINYJPEG_FMT_BGR24:
  1758. colorspace_array_conv = convert_colorspace_bgr24;
  1759. if (priv->components[0] == NULL)
  1760. priv->components[0] = (uint8_t *)priv->allocate_mem(priv->width * priv->height * 3);
  1761. bytes_per_blocklines[0] = priv->width * 3;
  1762. bytes_per_mcu[0] = 3*8;
  1763. break;
  1764. case TINYJPEG_FMT_GREY:
  1765. decode_mcu_table = decode_mcu_1comp_table;
  1766. colorspace_array_conv = convert_colorspace_grey;
  1767. if (priv->components[0] == NULL)
  1768. priv->components[0] = (uint8_t *)priv->allocate_mem(priv->width * priv->height);
  1769. bytes_per_blocklines[0] = priv->width;
  1770. bytes_per_mcu[0] = 8;
  1771. break;
  1772. default:
  1773. trace("Bad pixel format\n");
  1774. return -1;
  1775. }
  1776. xstride_by_mcu = ystride_by_mcu = 8;
  1777. if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
  1778. decode_MCU = decode_mcu_table[0];
  1779. convert_to_pixfmt = colorspace_array_conv[0];
  1780. trace("Use decode 1x1 sampling\n");
  1781. } else if (priv->component_infos[cY].Hfactor == 1) {
  1782. decode_MCU = decode_mcu_table[1];
  1783. convert_to_pixfmt = colorspace_array_conv[1];
  1784. ystride_by_mcu = 16;
  1785. trace("Use decode 1x2 sampling (not supported)\n");
  1786. } else if (priv->component_infos[cY].Vfactor == 2) {
  1787. decode_MCU = decode_mcu_table[3];
  1788. convert_to_pixfmt = colorspace_array_conv[3];
  1789. xstride_by_mcu = 16;
  1790. ystride_by_mcu = 16;
  1791. trace("Use decode 2x2 sampling\n");
  1792. } else {
  1793. decode_MCU = decode_mcu_table[2];
  1794. convert_to_pixfmt = colorspace_array_conv[2];
  1795. xstride_by_mcu = 16;
  1796. trace("Use decode 2x1 sampling\n");
  1797. }
  1798. resync(priv);
  1799. /* Don't forget to that block can be either 8 or 16 lines */
  1800. bytes_per_blocklines[0] *= ystride_by_mcu;
  1801. bytes_per_blocklines[1] *= ystride_by_mcu;
  1802. bytes_per_blocklines[2] *= ystride_by_mcu;
  1803. bytes_per_mcu[0] *= xstride_by_mcu/8;
  1804. bytes_per_mcu[1] *= xstride_by_mcu/8;
  1805. bytes_per_mcu[2] *= xstride_by_mcu/8;
  1806. /* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */
  1807. for (y=0; y < priv->height; y+=ystride_by_mcu)
  1808. {
  1809. //trace("Decoding row %d\n", y);
  1810. /// priv->plane[0] = priv->components[0] + (y/ystride_by_mcu * bytes_per_blocklines[0]);
  1811. /// priv->plane[1] = priv->components[1] + (y/ystride_by_mcu * bytes_per_blocklines[1]);
  1812. /// priv->plane[2] = priv->components[2] + (y/ystride_by_mcu * bytes_per_blocklines[2]);
  1813. for (x=0; x < priv->width; x+=xstride_by_mcu)
  1814. {
  1815. int i,copy_x,copy_y;
  1816. decode_MCU(priv);
  1817. convert_to_pixfmt(priv);
  1818. //priv->plane[0] += bytes_per_mcu[0];
  1819. //priv->plane[1] += bytes_per_mcu[1];
  1820. //priv->plane[2] += bytes_per_mcu[2];
  1821. copy_x=priv->width-x;
  1822. if (copy_x>xstride_by_mcu)
  1823. copy_x=xstride_by_mcu;
  1824. copy_y=priv->height-y;
  1825. if (copy_y>ystride_by_mcu)
  1826. copy_y=ystride_by_mcu;
  1827. for(i=0;i<copy_y;i++) {
  1828. unsigned char *dst = &priv->components[0][((y+i)*priv->width+x)*3];
  1829. memcpy(dst,&priv->decomp_block[i][0],copy_x*3);
  1830. }
  1831. if (priv->restarts_to_go>0)
  1832. {
  1833. priv->restarts_to_go--;
  1834. if (priv->restarts_to_go == 0)
  1835. {
  1836. priv->stream -= (priv->nbits_in_reservoir/8);
  1837. resync(priv);
  1838. if (find_next_rst_marker(priv) < 0)
  1839. return -1;
  1840. }
  1841. }
  1842. }
  1843. }
  1844. trace("Input file size: %d\n", priv->stream_length+2);
  1845. trace("Input bytes actually read: %d\n", priv->stream - priv->stream_begin + 2);
  1846. return 0;
  1847. }
  1848. const char *tinyjpeg_get_errorstring(struct jdec_private *priv)
  1849. {
  1850. /* FIXME: the error string must be store in the context */
  1851. priv = priv;
  1852. return error_string;
  1853. }
  1854. void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height)
  1855. {
  1856. *width = priv->width;
  1857. *height = priv->height;
  1858. }
  1859. int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components)
  1860. {
  1861. int i;
  1862. for (i=0; priv->components[i] && i<COMPONENTS; i++)
  1863. components[i] = priv->components[i];
  1864. return 0;
  1865. }
  1866. int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents)
  1867. {
  1868. unsigned int i;
  1869. if (ncomponents > COMPONENTS)
  1870. ncomponents = COMPONENTS;
  1871. for (i=0; i<ncomponents; i++)
  1872. priv->components[i] = components[i];
  1873. return 0;
  1874. }
  1875. int tinyjpeg_set_flags(struct jdec_private *priv, int flags)
  1876. {
  1877. int oldflags = priv->flags;
  1878. priv->flags = flags;
  1879. return oldflags;
  1880. }