/decoders/image/jpeg.d

http://github.com/wilkie/djehuty · D · 1567 lines · 1002 code · 400 blank · 165 comment · 198 complexity · e4752d3267babf5e0ea4721ad2410ce7 MD5 · raw file

  1. /*
  2. * jpeg.d
  3. *
  4. * This module implements the JPEG image standard.
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module decoders.image.jpeg;
  10. import graphics.bitmap;
  11. import core.string;
  12. import core.stream;
  13. import core.endian;
  14. import core.definitions;
  15. import decoders.image.decoder;
  16. import decoders.decoder;
  17. private {
  18. // Decoder States
  19. enum {
  20. JPEG_STATE_INIT_PROGRESS,
  21. JPEG_STATE_READ_HEADER,
  22. JPEG_STATE_READ_CHUNK_TYPE,
  23. JPEG_STATE_READ_CHUNK_SIZE,
  24. JPEG_STATE_INTERPRET_CHUNK,
  25. JPEG_STATE_SKIP_CHUNK,
  26. JPEG_STATE_START_OF_IMAGE,
  27. JPEG_STATE_CHUNK_SOF0,
  28. JPEG_STATE_CHUNK_SOF2,
  29. JPEG_STATE_CHUNK_APP0,
  30. JPEG_STATE_CHUNK_COM,
  31. JPEG_STATE_CHUNK_DNL,
  32. JPEG_STATE_CHUNK_DRI,
  33. JPEG_STATE_CHUNK_DQT,
  34. JPEG_STATE_CHUNK_DQT_READ_TABLE,
  35. JPEG_STATE_CHUNK_DHT,
  36. JPEG_STATE_CHUNK_DHT_READ_LENGTHS,
  37. JPEG_STATE_CHUNK_DHT_READ_TABLE,
  38. JPEG_STATE_CHUNK_SOF_READ_COMPONENTS,
  39. JPEG_STATE_CHUNK_APP0_UNKNOWN,
  40. JPEG_STATE_CHUNK_APP0_JFIF,
  41. JPEG_STATE_CHUNK_SOS,
  42. JPEG_STATE_CHUNK_SOS_READ_COMPONENTS,
  43. JPEG_STATE_CHUNK_SOS_READ_SELECTOR,
  44. JPEG_STATE_DECODE_INIT,
  45. JPEG_STATE_DECODE_HUFFMAN_INIT,
  46. JPEG_STATE_DECODE_HUFFMAN_DC,
  47. JPEG_STATE_DECODE_HUFFMAN_DC_READ,
  48. JPEG_STATE_DECODE_HUFFMAN_AC,
  49. JPEG_STATE_DECODE_HUFFMAN_AC_READ,
  50. JPEG_STATE_DECODE_IDCT,
  51. JPEG_STATE_RENDER_MCU,
  52. JPEG_STATE_READ_BYTE,
  53. JPEG_STATE_READ_BYTE_FF,
  54. JPEG_STATE_READ_BITS,
  55. }
  56. struct HUFFMAN_TABLE {
  57. ubyte[16] lengths;
  58. ubyte[][16] data;
  59. ubyte[16] data_pos;
  60. ushort[16] minor_code;
  61. ushort[16] major_code;
  62. }
  63. struct SCAN_COMPONENT_SELECTOR {
  64. ubyte Cs;
  65. ubyte DC_index; //dc huffman table index
  66. ubyte AC_index; //ac huffman table index
  67. ubyte C; //component identifier
  68. ubyte H; //horizontal sampling factor
  69. ubyte V; //vertical sampling factor
  70. ubyte Tq; //quantization table index
  71. ushort HxV; //number of data sections
  72. short[] data; //data section, allocate by (scs.H * scs.V) * 64;
  73. short lastDC;
  74. }
  75. struct JPEG_RENDER_INFO {
  76. HUFFMAN_TABLE[4] HT_DC;
  77. HUFFMAN_TABLE[4] HT_AC;
  78. ushort[64][4] quantization_table;
  79. ubyte Ns;
  80. ubyte sample_precision;
  81. ushort num_lines;
  82. ushort num_samples_per_line;
  83. ushort actual_image_width;
  84. ushort actual_image_height;
  85. ubyte num_image_components;
  86. ubyte Hmajor; ubyte Vmajor;
  87. SCAN_COMPONENT_SELECTOR * scan_components;
  88. ubyte * cb_upsample_lookup;
  89. ubyte * cr_upsample_lookup;
  90. uint component_counter;
  91. uint component_sample_counter;
  92. }
  93. align(1) struct JFIF_HEADER {
  94. ubyte version_major;
  95. ubyte version_minor;
  96. ubyte density_unit;
  97. ushort density_x;
  98. ushort density_y;
  99. ubyte thumb_w;
  100. ubyte thumb_h;
  101. }
  102. align(1) struct JPEG_SOF {
  103. ubyte sample_percision; // sample percision
  104. ushort num_lines; // number of lines
  105. ushort num_samples_per_line; // number of samples per line
  106. ubyte num_image_components; // number of image components per frame
  107. }
  108. align(1) struct JPEG_SOF_COMPONENTS {
  109. ubyte[255] component_identifier; // C(i)
  110. ubyte[255] sampling_factor; // H(i), V(i) - hi 4 bits: horizontal, else: vertical
  111. ubyte[255] quantization_table_destination; // Tq(i)
  112. ubyte[255] H;
  113. ubyte[255] V;
  114. ubyte[255] HxV;
  115. }
  116. align(1) struct JPEG_SOS {
  117. ubyte num_image_components;
  118. }
  119. align(1) struct JPEG_SOS_COMPONENTS {
  120. ubyte[255] scan_components; // C(j)
  121. ubyte[255] entropy_table_selector; // Td(j), Ta(j) - hi 4 bits: DC entropy table, else: AC
  122. }
  123. align(1) struct JPEG_SOS_SELECTOR {
  124. ubyte start_spectral_selector;
  125. ubyte end_spectral_selector;
  126. ubyte successive_approximation; // Ah, Al - hi 4 bits: high, else: low
  127. }
  128. align(1) struct JFIF_EXT {
  129. ubyte ext_code;
  130. }
  131. //static const ushort bit_mask[] = [1, 2, 4, 8, 16, 32, 64, 128]; //, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
  132. static const ushort bit_mask[] = [128, 64, 32, 16, 8, 4, 2, 1]; //, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
  133. static const ubyte zig_zag_reference[] = [0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63];
  134. static const float cb_b_uncode[] = [
  135. -226.816f, -225.044f, -223.272f, -221.5f, -219.728f, -217.956f, -216.184f, -214.412f, -212.64f, -210.868f, -209.096f,
  136. -207.324f, -205.552f, -203.78f, -202.008f, -200.236f, -198.464f, -196.692f, -194.92f, -193.148f, -191.376f,
  137. -189.604f, -187.832f, -186.06f, -184.288f, -182.516f, -180.744f, -178.972f, -177.2f, -175.428f, -173.656f,
  138. -171.884f, -170.112f, -168.34f, -166.568f, -164.796f, -163.024f, -161.252f, -159.48f, -157.708f, -155.936f,
  139. -154.164f, -152.392f, -150.62f, -148.848f, -147.076f, -145.304f, -143.532f, -141.76f, -139.988f, -138.216f,
  140. -136.444f, -134.672f, -132.9f, -131.128f, -129.356f, -127.584f, -125.812f, -124.04f, -122.268f, -120.496f,
  141. -118.724f, -116.952f, -115.18f, -113.408f, -111.636f, -109.864f, -108.092f, -106.32f, -104.548f, -102.776f,
  142. -101.004f, -99.232f, -97.46f, -95.688f, -93.916f, -92.144f, -90.372f, -88.6f, -86.828f, -85.056f,
  143. -83.284f, -81.512f, -79.74f, -77.968f, -76.196f, -74.424f, -72.652f, -70.88f, -69.108f, -67.336f,
  144. -65.564f, -63.792f, -62.02f, -60.248f, -58.476f, -56.704f, -54.932f, -53.16f, -51.388f, -49.616f,
  145. -47.844f, -46.072f, -44.3f, -42.528f, -40.756f, -38.984f, -37.212f, -35.44f, -33.668f, -31.896f,
  146. -30.124f, -28.352f, -26.58f, -24.808f, -23.036f, -21.264f, -19.492f, -17.72f, -15.948f, -14.176f,
  147. -12.404f, -10.632f, -8.86f, -7.088f, -5.316f, -3.544f, -1.772f, 0, 1.772f, 3.544f,
  148. 5.316f, 7.088f, 8.86f, 10.632f, 12.404f, 14.176f, 15.948f, 17.72f, 19.492f, 21.264f,
  149. 23.036f, 24.808f, 26.58f, 28.352f, 30.124f, 31.896f, 33.668f, 35.44f, 37.212f, 38.984f,
  150. 40.756f, 42.528f, 44.3f, 46.072f, 47.844f, 49.616f, 51.388f, 53.16f, 54.932f, 56.704f,
  151. 58.476f, 60.248f, 62.02f, 63.792f, 65.564f, 67.336f, 69.108f, 70.88f, 72.652f, 74.424f,
  152. 76.196f, 77.968f, 79.74f, 81.512f, 83.284f, 85.056f, 86.828f, 88.6f, 90.372f, 92.144f,
  153. 93.916f, 95.688f, 97.46f, 99.232f, 101.004f, 102.776f, 104.548f, 106.32f, 108.092f, 109.864f,
  154. 111.636f, 113.408f, 115.18f, 116.952f, 118.724f, 120.496f, 122.268f, 124.04f, 125.812f, 127.584f,
  155. 129.356f, 131.128f, 132.9f, 134.672f, 136.444f, 138.216f, 139.988f, 141.76f, 143.532f, 145.304f,
  156. 147.076f, 148.848f, 150.62f, 152.392f, 154.164f, 155.936f, 157.708f, 159.48f, 161.252f, 163.024f,
  157. 164.796f, 166.568f, 168.34f, 170.112f, 171.884f, 173.656f, 175.428f, 177.2f, 178.972f, 180.744f,
  158. 182.516f, 184.288f, 186.06f, 187.832f, 189.604f, 191.376f, 193.148f, 194.92f, 196.692f, 198.464f,
  159. 200.236f, 202.008f, 203.78f, 205.552f, 207.324f, 209.096f, 210.868f, 212.64f, 214.412f, 216.184f,
  160. 217.956f, 219.728f, 221.5f, 223.272f, 225.044f
  161. ];
  162. static const float cb_g_uncode[] = [
  163. 44.04992f, 43.70578f, 43.36164f, 43.0175f, 42.67336f, 42.32922f, 41.98508f, 41.64094f, 41.2968f, 40.95266f, 40.60852f,
  164. 40.26438f, 39.92024f, 39.5761f, 39.23196f, 38.88782f, 38.54368f, 38.19954f, 37.8554f, 37.51126f, 37.16712f,
  165. 36.82298f, 36.47884f, 36.1347f, 35.79056f, 35.44642f, 35.10228f, 34.75814f, 34.414f, 34.06986f, 33.72572f,
  166. 33.38158f, 33.03744f, 32.6933f, 32.34916f, 32.00502f, 31.66088f, 31.31674f, 30.9726f, 30.62846f, 30.28432f,
  167. 29.94018f, 29.59604f, 29.2519f, 28.90776f, 28.56362f, 28.21948f, 27.87534f, 27.5312f, 27.18706f, 26.84292f,
  168. 26.49878f, 26.15464f, 25.8105f, 25.46636f, 25.12222f, 24.77808f, 24.43394f, 24.0898f, 23.74566f, 23.40152f,
  169. 23.05738f, 22.71324f, 22.3691f, 22.02496f, 21.68082f, 21.33668f, 20.99254f, 20.6484f, 20.30426f, 19.96012f,
  170. 19.61598f, 19.27184f, 18.9277f, 18.58356f, 18.23942f, 17.89528f, 17.55114f, 17.207f, 16.86286f, 16.51872f,
  171. 16.17458f, 15.83044f, 15.4863f, 15.14216f, 14.79802f, 14.45388f, 14.10974f, 13.7656f, 13.42146f, 13.07732f,
  172. 12.73318f, 12.38904f, 12.0449f, 11.70076f, 11.35662f, 11.01248f, 10.66834f, 10.3242f, 9.98006f, 9.63592f,
  173. 9.29178f, 8.94764f, 8.6035f, 8.25936f, 7.91522f, 7.57108f, 7.22694f, 6.8828f, 6.53866f, 6.19452f,
  174. 5.85038f, 5.50624f, 5.1621f, 4.81796f, 4.47382f, 4.12968f, 3.78554f, 3.4414f, 3.09726f, 2.75312f,
  175. 2.40898f, 2.06484f, 1.7207f, 1.37656f, 1.03242f, 0.68828f, 0.34414f, 0, -0.34414f, -0.68828f,
  176. -1.03242f, -1.37656f, -1.7207f, -2.06484f, -2.40898f, -2.75312f, -3.09726f, -3.4414f, -3.78554f, -4.12968f,
  177. -4.47382f, -4.81796f, -5.1621f, -5.50624f, -5.85038f, -6.19452f, -6.53866f, -6.8828f, -7.22694f, -7.57108f,
  178. -7.91522f, -8.25936f, -8.6035f, -8.94764f, -9.29178f, -9.63592f, -9.98006f, -10.3242f, -10.66834f, -11.01248f,
  179. -11.35662f, -11.70076f, -12.0449f, -12.38904f, -12.73318f, -13.07732f, -13.42146f, -13.7656f, -14.10974f, -14.45388f,
  180. -14.79802f, -15.14216f, -15.4863f, -15.83044f, -16.17458f, -16.51872f, -16.86286f, -17.207f, -17.55114f, -17.89528f,
  181. -18.23942f, -18.58356f, -18.9277f, -19.27184f, -19.61598f, -19.96012f, -20.30426f, -20.6484f, -20.99254f, -21.33668f,
  182. -21.68082f, -22.02496f, -22.3691f, -22.71324f, -23.05738f, -23.40152f, -23.74566f, -24.0898f, -24.43394f, -24.77808f,
  183. -25.12222f, -25.46636f, -25.8105f, -26.15464f, -26.49878f, -26.84292f, -27.18706f, -27.5312f, -27.87534f, -28.21948f,
  184. -28.56362f, -28.90776f, -29.2519f, -29.59604f, -29.94018f, -30.28432f, -30.62846f, -30.9726f, -31.31674f, -31.66088f,
  185. -32.00502f, -32.34916f, -32.6933f, -33.03744f, -33.38158f, -33.72572f, -34.06986f, -34.414f, -34.75814f, -35.10228f,
  186. -35.44642f, -35.79056f, -36.1347f, -36.47884f, -36.82298f, -37.16712f, -37.51126f, -37.8554f, -38.19954f, -38.54368f,
  187. -38.88782f, -39.23196f, -39.5761f, -39.92024f, -40.26438f, -40.60852f, -40.95266f, -41.2968f, -41.64094f, -41.98508f,
  188. -42.32922f, -42.67336f, -43.0175f, -43.36164f, -43.70578f
  189. ];
  190. static const float cr_g_uncode[] = [
  191. -91.40992f, -90.69578f, -89.98164f, -89.2675f, -88.55336f, -87.83922f, -87.12508f, -86.41094f, -85.6968f, -84.98266f, -84.26852f,
  192. -83.55438f, -82.84024f, -82.1261f, -81.41196f, -80.69782f, -79.98368f, -79.26954f, -78.5554f, -77.84126f, -77.12712f,
  193. -76.41298f, -75.69884f, -74.9847f, -74.27056f, -73.55642f, -72.84228f, -72.12814f, -71.414f, -70.69986f, -69.98572f,
  194. -69.27158f, -68.55744f, -67.8433f, -67.12916f, -66.41502f, -65.70088f, -64.98674f, -64.2726f, -63.55846f, -62.84432f,
  195. -62.13018f, -61.41604f, -60.7019f, -59.98776f, -59.27362f, -58.55948f, -57.84534f, -57.1312f, -56.41706f, -55.70292f,
  196. -54.98878f, -54.27464f, -53.5605f, -52.84636f, -52.13222f, -51.41808f, -50.70394f, -49.9898f, -49.27566f, -48.56152f,
  197. -47.84738f, -47.13324f, -46.4191f, -45.70496f, -44.99082f, -44.27668f, -43.56254f, -42.8484f, -42.13426f, -41.42012f,
  198. -40.70598f, -39.99184f, -39.2777f, -38.56356f, -37.84942f, -37.13528f, -36.42114f, -35.707f, -34.99286f, -34.27872f,
  199. -33.56458f, -32.85044f, -32.1363f, -31.42216f, -30.70802f, -29.99388f, -29.27974f, -28.5656f, -27.85146f, -27.13732f,
  200. -26.42318f, -25.70904f, -24.9949f, -24.28076f, -23.56662f, -22.85248f, -22.13834f, -21.4242f, -20.71006f, -19.99592f,
  201. -19.28178f, -18.56764f, -17.8535f, -17.13936f, -16.42522f, -15.71108f, -14.99694f, -14.2828f, -13.56866f, -12.85452f,
  202. -12.14038f, -11.42624f, -10.7121f, -9.99796f, -9.28382f, -8.56968f, -7.85554f, -7.1414f, -6.42726f, -5.71312f,
  203. -4.99898f, -4.28484f, -3.5707f, -2.85656f, -2.14242f, -1.42828f, -0.71414f, 0, 0.71414f, 1.42828f,
  204. 2.14242f, 2.85656f, 3.5707f, 4.28484f, 4.99898f, 5.71312f, 6.42726f, 7.1414f, 7.85554f, 8.56968f,
  205. 9.28382f, 9.99796f, 10.7121f, 11.42624f, 12.14038f, 12.85452f, 13.56866f, 14.2828f, 14.99694f, 15.71108f,
  206. 16.42522f, 17.13936f, 17.8535f, 18.56764f, 19.28178f, 19.99592f, 20.71006f, 21.4242f, 22.13834f, 22.85248f,
  207. 23.56662f, 24.28076f, 24.9949f, 25.70904f, 26.42318f, 27.13732f, 27.85146f, 28.5656f, 29.27974f, 29.99388f,
  208. 30.70802f, 31.42216f, 32.1363f, 32.85044f, 33.56458f, 34.27872f, 34.99286f, 35.707f, 36.42114f, 37.13528f,
  209. 37.84942f, 38.56356f, 39.2777f, 39.99184f, 40.70598f, 41.42012f, 42.13426f, 42.8484f, 43.56254f, 44.27668f,
  210. 44.99082f, 45.70496f, 46.4191f, 47.13324f, 47.84738f, 48.56152f, 49.27566f, 49.9898f, 50.70394f, 51.41808f,
  211. 52.13222f, 52.84636f, 53.5605f, 54.27464f, 54.98878f, 55.70292f, 56.41706f, 57.1312f, 57.84534f, 58.55948f,
  212. 59.27362f, 59.98776f, 60.7019f, 61.41604f, 62.13018f, 62.84432f, 63.55846f, 64.2726f, 64.98674f, 65.70088f,
  213. 66.41502f, 67.12916f, 67.8433f, 68.55744f, 69.27158f, 69.98572f, 70.69986f, 71.414f, 72.12814f, 72.84228f,
  214. 73.55642f, 74.27056f, 74.9847f, 75.69884f, 76.41298f, 77.12712f, 77.84126f, 78.5554f, 79.26954f, 79.98368f,
  215. 80.69782f, 81.41196f, 82.1261f, 82.84024f, 83.55438f, 84.26852f, 84.98266f, 85.6968f, 86.41094f, 87.12508f,
  216. 87.83922f, 88.55336f, 89.2675f, 89.98164f, 90.69578f
  217. ];
  218. static const float cr_r_uncode[] = [
  219. -179.456f, -178.054f, -176.652f, -175.25f, -173.848f, -172.446f, -171.044f, -169.642f, -168.24f, -166.838f, -165.436f,
  220. -164.034f, -162.632f, -161.23f, -159.828f, -158.426f, -157.024f, -155.622f, -154.22f, -152.818f, -151.416f,
  221. -150.014f, -148.612f, -147.21f, -145.808f, -144.406f, -143.004f, -141.602f, -140.2f, -138.798f, -137.396f,
  222. -135.994f, -134.592f, -133.19f, -131.788f, -130.386f, -128.984f, -127.582f, -126.18f, -124.778f, -123.376f,
  223. -121.974f, -120.572f, -119.17f, -117.768f, -116.366f, -114.964f, -113.562f, -112.16f, -110.758f, -109.356f,
  224. -107.954f, -106.552f, -105.15f, -103.748f, -102.346f, -100.944f, -99.542f, -98.14f, -96.738f, -95.336f,
  225. -93.934f, -92.532f, -91.13f, -89.728f, -88.326f, -86.924f, -85.522f, -84.12f, -82.718f, -81.316f,
  226. -79.914f, -78.512f, -77.11f, -75.708f, -74.306f, -72.904f, -71.502f, -70.1f, -68.698f, -67.296f,
  227. -65.894f, -64.492f, -63.09f, -61.688f, -60.286f, -58.884f, -57.482f, -56.08f, -54.678f, -53.276f,
  228. -51.874f, -50.472f, -49.07f, -47.668f, -46.266f, -44.864f, -43.462f, -42.06f, -40.658f, -39.256f,
  229. -37.854f, -36.452f, -35.05f, -33.648f, -32.246f, -30.844f, -29.442f, -28.04f, -26.638f, -25.236f,
  230. -23.834f, -22.432f, -21.03f, -19.628f, -18.226f, -16.824f, -15.422f, -14.02f, -12.618f, -11.216f,
  231. -9.814f, -8.412f, -7.01f, -5.608f, -4.206f, -2.804f, -1.402f, 0, 1.402f, 2.804f,
  232. 4.206f, 5.608f, 7.01f, 8.412f, 9.814f, 11.216f, 12.618f, 14.02f, 15.422f, 16.824f,
  233. 18.226f, 19.628f, 21.03f, 22.432f, 23.834f, 25.236f, 26.638f, 28.04f, 29.442f, 30.844f,
  234. 32.246f, 33.648f, 35.05f, 36.452f, 37.854f, 39.256f, 40.658f, 42.06f, 43.462f, 44.864f,
  235. 46.266f, 47.668f, 49.07f, 50.472f, 51.874f, 53.276f, 54.678f, 56.08f, 57.482f, 58.884f,
  236. 60.286f, 61.688f, 63.09f, 64.492f, 65.894f, 67.296f, 68.698f, 70.1f, 71.502f, 72.904f,
  237. 74.306f, 75.708f, 77.11f, 78.512f, 79.914f, 81.316f, 82.718f, 84.12f, 85.522f, 86.924f,
  238. 88.326f, 89.728f, 91.13f, 92.532f, 93.934f, 95.336f, 96.738f, 98.14f, 99.542f, 100.944f,
  239. 102.346f, 103.748f, 105.15f, 106.552f, 107.954f, 109.356f, 110.758f, 112.16f, 113.562f, 114.964f,
  240. 116.366f, 117.768f, 119.17f, 120.572f, 121.974f, 123.376f, 124.778f, 126.18f, 127.582f, 128.984f,
  241. 130.386f, 131.788f, 133.19f, 134.592f, 135.994f, 137.396f, 138.798f, 140.2f, 141.602f, 143.004f,
  242. 144.406f, 145.808f, 147.21f, 148.612f, 150.014f, 151.416f, 152.818f, 154.22f, 155.622f, 157.024f,
  243. 158.426f, 159.828f, 161.23f, 162.632f, 164.034f, 165.436f, 166.838f, 168.24f, 169.642f, 171.044f,
  244. 172.446f, 173.848f, 175.25f, 176.652f, 178.054f
  245. ];
  246. }
  247. // Section: Codecs/Image
  248. // Description: The JPEG Codec
  249. class JPEGDecoder : ImageDecoder {
  250. override string name() {
  251. return "Joint Picture Experts Group";
  252. }
  253. StreamData decode(Stream stream, ref Bitmap view) {
  254. ImageFrameDescription imageDesc;
  255. bool hasMultipleFrames;
  256. hasMultipleFrames = false;
  257. ushort header;
  258. ubyte byteCheck;
  259. for (;;) {
  260. switch(decoderState) {
  261. case JPEG_STATE_READ_BYTE:
  262. //writefln("jpeg - decode - read byte");
  263. if (!stream.read(cur_byte)) {
  264. return StreamData.Required;
  265. }
  266. //cur_byte = file[file_idx];
  267. //file_idx++;
  268. cur_bit_pos = 0;
  269. // check for a FF block
  270. // if this is a 0xFF and the next block is a 0x00,
  271. // then we use this block, but skip the next byte
  272. if (cur_byte != 0xFF) {
  273. decoderState = decoderNextState;
  274. continue;
  275. }
  276. /* follow through */
  277. case JPEG_STATE_READ_BYTE_FF:
  278. ubyte test_byte;
  279. if (!stream.read(test_byte)) {
  280. return StreamData.Required;
  281. }
  282. //test_byte = file[file_idx];
  283. //file_idx++;
  284. // if this is not a 0x00, then check for the block type
  285. // else, this just effectively skips the 0x00
  286. if (test_byte == 0xD9) {
  287. // EOI (End Of Image)
  288. return StreamData.Complete;
  289. }
  290. decoderState = decoderNextState;
  291. continue;
  292. // state to read a certain amount of bits (bits_to_read)
  293. // and places them in bits_read
  294. case JPEG_STATE_READ_BITS:
  295. // cur_bit_pos tells us how many bits remain in cur_byte
  296. // read what we can until aligned, and then read a byte
  297. for (; cur_bit_pos < 8 && bits_to_read > 0; cur_bit_pos++, bits_to_read--) {
  298. if (first_bit == 0) {
  299. if (cur_byte & bit_mask[cur_bit_pos]) {
  300. bits_read = 0;
  301. first_bit = 0x11;
  302. }
  303. else {
  304. bits_read = 0xFFFF;
  305. first_bit = 0x10;
  306. }
  307. }
  308. bits_read <<= 1;
  309. if (cur_byte & bit_mask[cur_bit_pos]) {
  310. bits_read |= 1;
  311. }
  312. }
  313. if (cur_bit_pos == 8) {
  314. decoderState = JPEG_STATE_READ_BYTE;
  315. decoderNextState = JPEG_STATE_READ_BITS;
  316. continue;
  317. }
  318. if (bits_to_read == 0) {
  319. decoderState = decoderNextSubState;
  320. // is it a negative?
  321. if (first_bit == 0x10) {
  322. bits_read += 1; //-bits_read;
  323. }
  324. }
  325. continue;
  326. case JPEG_STATE_INIT_PROGRESS:
  327. decoderState = JPEG_STATE_READ_HEADER;
  328. /* fall through */
  329. case JPEG_STATE_READ_HEADER:
  330. if (!stream.read(header)) {
  331. return StreamData.Required;
  332. }
  333. if (header == FromBigEndian16(0xFFD8)) {
  334. // SOI (Start of Image)
  335. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  336. }
  337. else {
  338. //header not found
  339. return StreamData.Invalid;
  340. }
  341. /* follow through */
  342. case JPEG_STATE_READ_CHUNK_TYPE:
  343. //writeln("jpeg - reading chunk type");
  344. // get the the block type
  345. if (!stream.read(chunkType)) {
  346. return StreamData.Required;
  347. }
  348. chunkType = FromBigEndian16(chunkType);
  349. //grabbing info from block headers \ initing huffman tables
  350. //determine the block size
  351. decoderState = JPEG_STATE_READ_CHUNK_SIZE;
  352. /* follow through */
  353. case JPEG_STATE_READ_CHUNK_SIZE:
  354. // get chunk size
  355. if (!stream.read(chunkLength)) {
  356. return StreamData.Required;
  357. }
  358. chunkLength = FromBigEndian16(chunkLength);
  359. chunkLength -= 2; // supplement for the length identifier (short)
  360. // interpret chunk type
  361. decoderState = JPEG_STATE_INTERPRET_CHUNK;
  362. /* follow through */
  363. case JPEG_STATE_INTERPRET_CHUNK:
  364. switch(chunkType) {
  365. case 0xFFC0: // SOF0 (start of frame 0 - Baseline DCT)
  366. decoderState = JPEG_STATE_CHUNK_SOF0;
  367. break;
  368. case 0xFFC2: // SOF2 (start of frame 2 - Progressive DCT)
  369. decoderState = JPEG_STATE_CHUNK_SOF2;
  370. break;
  371. case 0xFFC4: // DHT (define huffman tables)
  372. decoderState = JPEG_STATE_CHUNK_DHT;
  373. break;
  374. case 0xFFCC: // DAC (define arithmetic coding conditionings)
  375. break;
  376. case 0xFFE0: // APP0 (signifies the JFIF spec is being utilized
  377. decoderState = JPEG_STATE_CHUNK_APP0;
  378. break;
  379. case 0xFFFE: // COM (comment)
  380. decoderState = JPEG_STATE_CHUNK_COM;
  381. break;
  382. case 0xFFD8: // SOI (start of image)
  383. break;
  384. case 0xFFD9: // EOI (end of image)
  385. break;
  386. case 0xFFDA: // SOS (start of scan)
  387. decoderState = JPEG_STATE_CHUNK_SOS;
  388. break;
  389. case 0xFFDB: // DQT (define quantization table)
  390. decoderState = JPEG_STATE_CHUNK_DQT;
  391. break;
  392. case 0xFFDC: // DNL (define number of lines)
  393. decoderState = JPEG_STATE_CHUNK_DNL;
  394. break;
  395. case 0xFFDD: // DRI (define restart interval)
  396. decoderState = JPEG_STATE_CHUNK_DRI;
  397. break;
  398. case 0xFFDE: // DHP (define hierarchical progression)
  399. break;
  400. case 0xFFDF: // EXP (expand reference components)
  401. break;
  402. default:
  403. // just ignore unknown blocks
  404. // and pass over the section length
  405. if (!stream.skip(chunkLength))
  406. {
  407. return StreamData.Required;
  408. }
  409. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  410. break;
  411. }
  412. continue;
  413. case JPEG_STATE_CHUNK_COM:
  414. case JPEG_STATE_SKIP_CHUNK:
  415. if (!stream.skip(chunkLength)) {
  416. return StreamData.Required;
  417. }
  418. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  419. continue;
  420. // SOF0 - start of frame 0
  421. case JPEG_STATE_CHUNK_SOF0:
  422. // get information about the frame (dimensions)
  423. if (!stream.read(&sof, sof.sizeof)) {
  424. return StreamData.Required;
  425. }
  426. // enforce endian
  427. sof.num_lines = FromBigEndian16(sof.num_lines);
  428. sof.num_samples_per_line = FromBigEndian16(sof.num_samples_per_line);
  429. decoderState = JPEG_STATE_CHUNK_SOF_READ_COMPONENTS;
  430. /* follow through */
  431. case JPEG_STATE_CHUNK_SOF_READ_COMPONENTS:
  432. // read the image components
  433. if (stream.remaining < (sof.num_image_components * 3)) {
  434. return StreamData.Required;
  435. }
  436. sof_comp = new SCAN_COMPONENT_SELECTOR[3]; //sof.num_image_components];
  437. ubyte[] bytesRead = new ubyte[sof.num_image_components*3];
  438. stream.read(bytesRead.ptr, bytesRead.length);
  439. for (int n=0, a=0; a<sof.num_image_components; a++) {
  440. sof_comp[a].C = bytesRead[n];
  441. n++;
  442. sof_comp[a].H = cast(ubyte)((bytesRead[n] & 0xF0) >> 4);
  443. sof_comp[a].V = cast(ubyte)(bytesRead[n] & 0xF);
  444. n++;
  445. sof_comp[a].HxV = cast(ushort)(sof_comp[a].H * sof_comp[a].V);
  446. if (Hmajor < sof_comp[a].H) { Hmajor = sof_comp[a].H; }
  447. if (Vmajor < sof_comp[a].V) { Vmajor = sof_comp[a].V; }
  448. //allocate memory for the MCU data
  449. sof_comp[a].data = new short[sof_comp[a].HxV * 64];
  450. sof_comp[a].Tq = bytesRead[n];
  451. n++;
  452. sof_comp[a].lastDC = 0;
  453. }
  454. if (sof.num_image_components == 1) {
  455. // monochrome
  456. sof_comp[1].C = 2;
  457. sof_comp[1].data = new short[64];
  458. sof_comp[2].C = 3;
  459. sof_comp[2].data = new short[64];
  460. }
  461. //allocate memory for the image
  462. if (sof.num_samples_per_line % (Hmajor * 8) == 0) {
  463. actual_image_width = sof.num_samples_per_line;
  464. }
  465. else {
  466. actual_image_width = sof.num_samples_per_line + ((Hmajor * 8)-(sof.num_samples_per_line % (Hmajor * 8)));
  467. }
  468. if (sof.num_lines % (Vmajor * 8) == 0) {
  469. actual_image_height = sof.num_lines;
  470. }
  471. else {
  472. actual_image_height = sof.num_lines + ((Vmajor * 8)-(sof.num_lines % (Vmajor * 8)));
  473. }
  474. block_width = actual_image_width / (Hmajor * 8);
  475. block_height = actual_image_height / (Vmajor * 8);
  476. imgylinemovement = sof.num_samples_per_line * 4;
  477. imgylinemovement_block = imgylinemovement * 8;
  478. imgxlinemovement_block_start = 32 * Hmajor;
  479. imgylinemovement_block_start = imgylinemovement_block * Vmajor;
  480. view.create(sof.num_samples_per_line, sof.num_lines);
  481. //view.CreateDIB(actual_image_width, actual_image_height);
  482. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  483. continue;
  484. // SOF2 - start of frame 2
  485. case JPEG_STATE_CHUNK_SOF2:
  486. if (!stream.skip(chunkLength)) {
  487. return StreamData.Required;
  488. }
  489. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  490. continue;
  491. // APP0 - JFIF Specifications
  492. case JPEG_STATE_CHUNK_APP0:
  493. // Check for the signature of the APP0 segment
  494. char[5] signature;
  495. if (!stream.read(signature.ptr, signature.length)) {
  496. return StreamData.Required;
  497. }
  498. switch (signature) {
  499. case "JFIF\0":
  500. decoderState = JPEG_STATE_CHUNK_APP0_JFIF;
  501. break;
  502. case "JFXX\0":
  503. break;
  504. default:
  505. decoderState = JPEG_STATE_CHUNK_APP0_UNKNOWN;
  506. break;
  507. }
  508. continue;
  509. case JPEG_STATE_CHUNK_APP0_JFIF:
  510. // skip the unknown app extension
  511. if (!stream.read(&jfif, jfif.sizeof)) {
  512. return StreamData.Required;
  513. }
  514. jfif.density_x = FromBigEndian16(jfif.density_x);
  515. jfif.density_y = FromBigEndian16(jfif.density_y);
  516. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  517. continue;
  518. case JPEG_STATE_CHUNK_APP0_UNKNOWN:
  519. // skip the unknown app extension
  520. if (!stream.skip(chunkLength - 5)) {
  521. return StreamData.Required;
  522. }
  523. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  524. continue;
  525. // DHT - define huffman tables
  526. case JPEG_STATE_CHUNK_DHT:
  527. ubyte table_id;
  528. if (!stream.read(table_id)) {
  529. return StreamData.Required;
  530. }
  531. //find what table it should go into
  532. if (((table_id & 0x10) >> 4) == 0) {
  533. //DC TABLE
  534. cur_ht = &HT_DC[table_id & 0x03];
  535. }
  536. else {
  537. //AC TABLE
  538. cur_ht = &HT_AC[table_id & 0x03];
  539. }
  540. decoderState = JPEG_STATE_CHUNK_DHT_READ_LENGTHS;
  541. /* follow through */
  542. case JPEG_STATE_CHUNK_DHT_READ_LENGTHS:
  543. chunkLength -= 17;
  544. if (!stream.read(cur_ht.lengths.ptr, 16)) {
  545. return StreamData.Required;
  546. }
  547. //load the lengths into the specified table
  548. for (int a=0; a<16; a++) {
  549. int size = cur_ht.lengths[a];
  550. cur_ht.data[a] = new ubyte[size];
  551. cur_ht.data_pos[a] = 0;
  552. }
  553. decoderState = JPEG_STATE_CHUNK_DHT_READ_TABLE;
  554. //create the table
  555. int o=0;
  556. int n=0;
  557. bytesToRead = 0;
  558. for (int a=0; a<chunkLength; a++) {
  559. while (n == cur_ht.lengths[o]) {
  560. o++;
  561. n=0;
  562. if (o == 16) { break; }
  563. }
  564. if (o==16) { break; }
  565. bytesToRead++;
  566. n++;
  567. }
  568. /* follow through */
  569. case JPEG_STATE_CHUNK_DHT_READ_TABLE:
  570. ubyte[] bytesRead = new ubyte[bytesToRead];
  571. if (!stream.read(bytesRead.ptr, bytesToRead)) {
  572. return StreamData.Required;
  573. }
  574. //create the table
  575. int o=0;
  576. int n=0;
  577. int q=0;
  578. for (int a=0; a<chunkLength; a++) {
  579. while (n == cur_ht.lengths[o]) {
  580. o++;
  581. n=0;
  582. if (o == 16) { break; }
  583. }
  584. if (o==16) { break; }
  585. cur_ht.data[o][n] = bytesRead[q];
  586. q++;
  587. n++;
  588. }
  589. //aquire minor and major codes
  590. o=0;
  591. for (int a=0; a<16; a++) {
  592. cur_ht.minor_code[a] = cast(ushort)(o);
  593. o += cur_ht.lengths[a];
  594. cur_ht.major_code[a] = cast(ushort)(o-1);
  595. o = o << 1;
  596. if (cur_ht.lengths[a] == 0) {
  597. cur_ht.minor_code[a] = 0xFFFF;
  598. cur_ht.major_code[a] = 0x0000;
  599. }
  600. }
  601. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  602. continue;
  603. // DQT - define quantization tables
  604. case JPEG_STATE_CHUNK_DQT:
  605. chunkLength -= 65; //supplement for the length identifier and the 1 byte for table id
  606. ubyte quantization_type;
  607. if (!stream.read(quantization_type)) {
  608. return StreamData.Required;
  609. }
  610. //get Pq - which is the precision... 0 for 8 bit values and 1 for 16 bit values
  611. quantization_precision = ((quantization_type & 0x10) >> 4);
  612. if (quantization_precision==1) { chunkLength -= 64; }
  613. //chunkLength should be 0 now
  614. //get Tq - the destination index of the table
  615. quantization_destination = (quantization_type & 0x03);
  616. decoderState = JPEG_STATE_CHUNK_DQT_READ_TABLE;
  617. /* follow through */
  618. case JPEG_STATE_CHUNK_DQT_READ_TABLE:
  619. if (quantization_precision==0) {
  620. ubyte[64] bytesRead;
  621. if (!stream.read(bytesRead.ptr, 64)) {
  622. return StreamData.Required;
  623. }
  624. for (int n = 0; n < 64; n++) {
  625. quantization_table[quantization_destination][n] = bytesRead[n];
  626. }
  627. }
  628. else {
  629. ushort[64] bytesRead;
  630. if (!stream.read(bytesRead.ptr, 128)) {
  631. return StreamData.Required;
  632. }
  633. for (int n = 0; n < 64; n++) {
  634. quantization_table[quantization_destination][n] = FromBigEndian16(bytesRead[n]);
  635. }
  636. }
  637. decoderState = JPEG_STATE_READ_CHUNK_TYPE;
  638. continue;
  639. // SOS - start of scan
  640. case JPEG_STATE_CHUNK_SOS:
  641. // read the number of image components in the scan
  642. // (a subset of the frame image components)
  643. if (!stream.read(&sos, sos.sizeof)) {
  644. return StreamData.Required;
  645. }
  646. decoderState = JPEG_STATE_CHUNK_SOS_READ_COMPONENTS;
  647. /* follow through */
  648. case JPEG_STATE_CHUNK_SOS_READ_COMPONENTS:
  649. if (stream.remaining < (sos.num_image_components * 2)) {
  650. return StreamData.Required;
  651. }
  652. ubyte[] bytesRead = new ubyte[sof.num_image_components*2];
  653. stream.read(bytesRead.ptr, bytesRead.length);
  654. //get the number of source components
  655. //jpeg_vars->Ns = in_bytes[file_counter];
  656. /*
  657. if (jpeg_vars->Ns < 1 || jpeg_vars->Ns > 4) {
  658. break;
  659. }
  660. if (jpeg_vars->scan_components != NULL) {
  661. if (jpeg_vars->num_image_components != jpeg_vars->Ns)
  662. { delete jpeg_vars->scan_components; jpeg_vars->scan_components = new SCAN_COMPONENT_SELECTOR[jpeg_vars->Ns];}
  663. }
  664. else {
  665. jpeg_vars->scan_components = new SCAN_COMPONENT_SELECTOR[jpeg_vars->Ns];
  666. }*/
  667. //get scan component selectors
  668. int n = 0;
  669. for (int a = 0; a < sos.num_image_components; a++) {
  670. sof_comp[a].Cs = bytesRead[n];
  671. n++;
  672. sof_comp[a].DC_index = cast(ubyte)((bytesRead[n] & 0xF0) >> 4);
  673. sof_comp[a].AC_index = cast(ubyte)((bytesRead[n] & 0x0F));
  674. n++;
  675. sof_comp[a].lastDC = 0;
  676. }
  677. decoderState = JPEG_STATE_CHUNK_SOS_READ_SELECTOR;
  678. /* follow through */
  679. case JPEG_STATE_CHUNK_SOS_READ_SELECTOR:
  680. if (!stream.read(&sos_sel, sos_sel.sizeof)) {
  681. return StreamData.Required;
  682. }
  683. decoderState = JPEG_STATE_DECODE_INIT;
  684. /* follow through */
  685. case JPEG_STATE_DECODE_INIT:
  686. // decode init
  687. cb_upsample_lookup = new ubyte[64 * Hmajor * Vmajor];
  688. cr_upsample_lookup = new ubyte[64 * Hmajor * Vmajor];
  689. for (int a=0; a<3;a++) {
  690. sof_comp[a].lastDC = 0;
  691. int d0,d1,d2,d3;
  692. //create sampling lookup tables
  693. if (sof_comp[a].C == 1) {
  694. //Y component
  695. }
  696. else if (sof_comp[a].C == 2) {
  697. //ch
  698. d0 = 0;
  699. d2 = (8*Hmajor);
  700. for (int n=0; n<Hmajor; n++) {
  701. for (int q=0; q<Vmajor; q++) {
  702. //starting coords for the block
  703. d1 = (q*8) + (n*64*Hmajor);
  704. //for every 8x8 block in the MCU
  705. for (int o=0; o<8;o++) {
  706. d3 = d1;
  707. for (int p=0; p<8; p++) {
  708. cb_upsample_lookup[d0] = cast(ubyte)(cast(int)(d3 / (d2 * Vmajor) * 8) + cast(int)((d3 % d2)/Vmajor));
  709. //printf("cb %d, %d\n", cb_upsample_lookup[d0], d0);
  710. d3++;
  711. d0++;
  712. }
  713. d1+=d2;
  714. }
  715. }
  716. }
  717. }
  718. else if (sof_comp[a].C == 3) {
  719. //cr
  720. d0 = 0;
  721. d2 = (8*Hmajor);
  722. for (int n=0; n<Hmajor; n++) {
  723. for (int q=0; q<Vmajor; q++) {
  724. //starting coords for the block
  725. d1 = (q*8) + (n*64*Hmajor);
  726. //for every 8x8 block in the MCU
  727. for (int o=0; o<8;o++) {
  728. d3 = d1;
  729. for (int p=0; p<8; p++) {
  730. cr_upsample_lookup[d0] = cast(ubyte)(cast(int)(d3 / (d2 * Vmajor) * 8) + cast(int)((d3 % d2)/Vmajor));
  731. //printf("cr %d, %d\n", cr_upsample_lookup[d0], d0);
  732. d3++;
  733. d0++;
  734. }
  735. d1+=d2;
  736. }
  737. }
  738. }
  739. }
  740. }
  741. cur_bit_pos=7;
  742. component_counter = 0;
  743. component_sample_counter = 0;
  744. decoderState = JPEG_STATE_READ_BYTE;
  745. decoderNextState = JPEG_STATE_DECODE_HUFFMAN_INIT;
  746. continue;
  747. case JPEG_STATE_DECODE_HUFFMAN_INIT:
  748. data_start_pos = 64 * component_sample_counter;
  749. cur_ht = &HT_DC[sof_comp[component_counter].DC_index];
  750. huffman_code = 0;
  751. huffman_bits = 0;
  752. decoderState = JPEG_STATE_DECODE_HUFFMAN_DC;
  753. /* follow through */
  754. case JPEG_STATE_DECODE_HUFFMAN_DC:
  755. // Get DC Component
  756. if (huffman_bits == 16) {
  757. decoderState = JPEG_STATE_DECODE_HUFFMAN_DC_READ;
  758. huffman_code = 0;
  759. huffman_bits = 0;
  760. }
  761. else {
  762. if (cur_bit_pos == 8) {
  763. decoderNextState = JPEG_STATE_DECODE_HUFFMAN_DC;
  764. decoderState = JPEG_STATE_READ_BYTE;
  765. continue;
  766. }
  767. huffman_code <<= 1;
  768. if (cur_byte & bit_mask[cur_bit_pos]) {
  769. huffman_code |= 1;
  770. }
  771. cur_bit_pos++;
  772. if (huffman_code >= cur_ht.minor_code[huffman_bits] && huffman_code <= cur_ht.major_code[huffman_bits]) {
  773. //valid code
  774. huffman_code -= cur_ht.minor_code[huffman_bits]; //get its position within that range of length!
  775. huffman_code = cur_ht.data[huffman_bits][huffman_code];
  776. //huffman_code == the # of bits following the code to read in
  777. if (huffman_code == 0) {
  778. sof_comp[component_counter].data[data_start_pos] = sof_comp[component_counter].lastDC;
  779. sof_comp[component_counter].data[data_start_pos] *= quantization_table[sof_comp[component_counter].Tq][0];
  780. decoderState = JPEG_STATE_DECODE_HUFFMAN_AC;
  781. // init AC huffman table
  782. cur_ht = &HT_AC[sof_comp[component_counter].AC_index];
  783. cur_ac = 1;
  784. }
  785. else {
  786. bits_read = 0;
  787. bits_to_read = huffman_code;
  788. first_bit = 0;
  789. decoderNextSubState = JPEG_STATE_DECODE_HUFFMAN_DC_READ;
  790. decoderState = JPEG_STATE_READ_BITS;
  791. }
  792. huffman_code = 0;
  793. huffman_bits = 0;
  794. }
  795. else {
  796. huffman_bits++;
  797. }
  798. continue;
  799. }
  800. case JPEG_STATE_DECODE_HUFFMAN_DC_READ:
  801. // read_bits state has stored the read code into bits_read
  802. int d0 = cast(short)bits_read;
  803. //huffman_code is actually component_counter value of the dc coefficent
  804. d0 += sof_comp[component_counter].lastDC;
  805. sof_comp[component_counter].data[data_start_pos] = cast(short)d0;
  806. sof_comp[component_counter].lastDC = sof_comp[component_counter].data[data_start_pos];
  807. d0 *= quantization_table[sof_comp[component_counter].Tq][0];
  808. sof_comp[component_counter].data[data_start_pos] = cast(short)d0;
  809. // init AC huffman table
  810. cur_ht = &HT_AC[sof_comp[component_counter].AC_index];
  811. cur_ac = 1;
  812. huffman_code = 0;
  813. huffman_bits = 0;
  814. decoderState = JPEG_STATE_DECODE_HUFFMAN_AC;
  815. /* follow through */
  816. case JPEG_STATE_DECODE_HUFFMAN_AC:
  817. // Get AC Components
  818. if (cur_ac >= 64) {
  819. // HUFFMAN CODING END
  820. decoderState = JPEG_STATE_DECODE_IDCT;
  821. huffman_code = 0;
  822. huffman_bits = 0;
  823. continue;
  824. }
  825. //from the next bits in the stream...find the first valid huffman code
  826. if (huffman_bits == 16) {
  827. decoderState = JPEG_STATE_DECODE_HUFFMAN_AC_READ;
  828. huffman_code = 0;
  829. huffman_bits = 0;
  830. }
  831. else {
  832. if (cur_bit_pos == 8) {
  833. decoderNextState = JPEG_STATE_DECODE_HUFFMAN_AC;
  834. decoderState = JPEG_STATE_READ_BYTE;
  835. continue;
  836. }
  837. huffman_code <<= 1;
  838. if (cur_byte & bit_mask[cur_bit_pos]) {
  839. huffman_code |= 1;
  840. }
  841. cur_bit_pos++;
  842. if (huffman_code >= cur_ht.minor_code[huffman_bits] && huffman_code <= cur_ht.major_code[huffman_bits]) {
  843. //valid code
  844. huffman_code -= cur_ht.minor_code[huffman_bits]; //get its position within that range of length!
  845. huffman_code = cur_ht.data[huffman_bits][huffman_code];
  846. //code: (o, q);
  847. int q=huffman_code & 0xF; //bit_count
  848. int o=huffman_code >> 4; //zero_count
  849. if (q == 0) {
  850. if (o == 0) {
  851. //code: (0, 0)
  852. //end of data
  853. //the rest are zeroes
  854. o = 64;
  855. while (cur_ac!=o) {
  856. sof_comp[component_counter].data[data_start_pos+zig_zag_reference[cur_ac]] = 0;
  857. cur_ac++;
  858. }
  859. }
  860. else if (o==0xF) {
  861. // code: (15, 0)
  862. //next 16 spaces of data are zeroes!
  863. if (cur_ac+16 > 64) {
  864. o = 64;
  865. }
  866. else {
  867. o = cur_ac+16;
  868. }
  869. while (cur_ac!=o) {
  870. sof_comp[component_counter].data[data_start_pos+zig_zag_reference[cur_ac]] = 0;
  871. cur_ac++;
  872. }
  873. }
  874. }
  875. else {
  876. //set zeroes in all the spaces marked in zero_count
  877. if (cur_ac+o > 64) {
  878. o = 64;
  879. }
  880. else {
  881. o = cur_ac+o;
  882. }
  883. //set the items from p to o
  884. while (cur_ac!=o) {
  885. sof_comp[component_counter].data[data_start_pos+zig_zag_reference[cur_ac]] = 0;
  886. cur_ac++;
  887. }
  888. // grab the next 'q' bits from the stream
  889. bits_read = 0;
  890. bits_to_read = q;
  891. first_bit = 0;
  892. decoderNextSubState = JPEG_STATE_DECODE_HUFFMAN_AC_READ;
  893. decoderState = JPEG_STATE_READ_BITS;
  894. }
  895. huffman_code = 0;
  896. huffman_bits = 0;
  897. }
  898. else {
  899. huffman_bits++;
  900. }
  901. }
  902. continue;
  903. case JPEG_STATE_DECODE_HUFFMAN_AC_READ:
  904. //bits_read is actually the value of the data
  905. if (cur_ac < 64) {
  906. sof_comp[component_counter].data[data_start_pos+zig_zag_reference[cur_ac]] = cast(short)(cast(short)bits_read * quantization_table[sof_comp[component_counter].Tq][cur_ac]);
  907. cur_ac++;
  908. }
  909. decoderState = JPEG_STATE_DECODE_HUFFMAN_AC;
  910. huffman_code = 0;
  911. huffman_bits = 0;
  912. continue;
  913. case JPEG_STATE_DECODE_IDCT:
  914. // integer, fixed point implementation of IDCT
  915. //at this point we should have component_counter 8 x 8 block
  916. //IDCT
  917. short* dataRef = &sof_comp[component_counter].data[data_start_pos];
  918. int d0,d1,d2,d3,d4,d5,d6,d7,tmp;
  919. for (int o=0; o<8; o++) {
  920. d4 = dataRef[7]; d5 = dataRef[5]; d6 = dataRef[3]; d7 = dataRef[1];
  921. d2 = d4 + d6; d3 = d5 + d7;
  922. d1 = (d2 + d3) * 77062; d2 *= -128553; d3 *= -25571; d2 += d1; d3 += d1;
  923. d1 = (d4 + d7) * -58981; d4 *= 19571; d7 *= 98391; d4 += d1; d7 += d1;
  924. d1 = (d5 + d6) * -167963; d5 *= 134553; d6 *= 201373; d5 += d1; d6 += d1;
  925. d4 += d2; d5 += d3; d6 += d2; d7 += d3;
  926. //even
  927. d3 = dataRef[2]; d2 = dataRef[6];
  928. d1 = (d3 + d2) * 35468; d3 *= 50159; d2 *= -121095; d3 += d1; d2 += d1;
  929. d0 = dataRef[0] + dataRef[4]; d1 = dataRef[0] - dataRef[4];
  930. d0 = (d0 << 16) + 4096; d1 = (d1 << 16) + 4096;
  931. tmp = d0 + d3; d3 = d0 - d3; d0 = tmp;
  932. tmp = d1 + d2; d2 = d1 - d2; d1 = tmp;
  933. tmp = d0 + d7; d7 = d0 - d7; d0 = tmp;
  934. dataRef[0] = cast(short)(d0 >> 13); dataRef[7] = cast(short)(d7 >> 13);
  935. d0 = d1 + d6; d6 = d1 - d6; d1 = d0;
  936. dataRef[1] = cast(short)(d1 >> 13); dataRef[6] = cast(short)(d6 >> 13);
  937. d0 = d2 + d5; d5 = d2 - d5; d2 = d0;
  938. dataRef[2] = cast(short)(d2 >> 13); dataRef[5] = cast(short)(d5 >> 13);
  939. d0 = d3 + d4; d4 = d3 - d4; d3 = d0;
  940. dataRef[3] = cast(short)(d3 >> 13); dataRef[4] = cast(short)(d4 >> 13);
  941. dataRef += 8;
  942. }
  943. dataRef = &sof_comp[component_counter].data[data_start_pos];
  944. for (int o=0; o<8; o++) {
  945. // odd
  946. d4 = dataRef[56]; d5 = dataRef[40]; d6 = dataRef[24]; d7 = dataRef[8];
  947. d2 = d4 + d6; d3 = d5 + d7;
  948. d1 = (d2 + d3) * 77062; d2 *= -128553; d3 *= -25571; d2 += d1; d3 += d1;
  949. d1 = (d4 + d7) * -58981; d4 *= 19571; d7 *= 98391; d4 += d1; d7 += d1;
  950. d1 = (d5 + d6) * -167963; d5 *= 134553; d6 *= 201373; d5 += d1; d6 += d1;
  951. d4 += d2; d5 += d3; d6 += d2; d7 += d3;
  952. // even
  953. d3 = dataRef[2*8]; d2 = dataRef[6*8];
  954. d1 = (d3 + d2) * 35468; d3 *= 50159; d2 *= -121095; d3 += d1; d2 += d1;
  955. d0 = dataRef[0] + dataRef[32]; d1 = dataRef[0] - dataRef[32];
  956. d0 = (d0 << 16) + 2097152; d1 = (d1 << 16) + 2097152;
  957. tmp = d0 + d3; d3 = d0 - d3; d0 = tmp;
  958. tmp = d1 + d2; d2 = d1 - d2; d1 = tmp;
  959. tmp = d0 + d7; d7 = d0 - d7; d0 = tmp;
  960. //the 128 is part of the rescaling process of JPEG
  961. //and is not part of the IDCT algorithm
  962. //526870912 is 128 << 22
  963. d0 += (128 << 22); dataRef[0] = cast(short)(d0 >> 22) ;
  964. if (dataRef[0] > 255) { dataRef[0] = 255; } else if (dataRef[0] < 0) { dataRef[0] = 0;}
  965. d7 += (128 << 22); dataRef[56] = cast(short)(d7 >> 22) ;
  966. if (dataRef[56] > 255) { dataRef[56] = 255; } else if (dataRef[56] < 0) { dataRef[56] = 0;}
  967. d0 = d1 + d6; d6 = d1 - d6; d1 = d0;
  968. d1 += (128 << 22); dataRef[8] = cast(short)(d1 >> 22) ;
  969. if (dataRef[8] > 255) { dataRef[8] = 255; } else if (dataRef[8] < 0) { dataRef[8] = 0;}
  970. d6 += (128 << 22); dataRef[48] = cast(short)(d6 >> 22) ;
  971. if (dataRef[48] > 255) { dataRef[48] = 255; } else if (dataRef[48] < 0) { dataRef[48] = 0;}
  972. d0 = d2 + d5; d5 = d2 - d5; d2 = d0;
  973. d2 += (128 << 22);
  974. dataRef[16] = cast(short)(d2 >> 22) ;
  975. if (dataRef[16] > 255) { dataRef[16] = 255; } else if (dataRef[16] < 0) { dataRef[16] = 0;}
  976. d5 += (128 << 22); dataRef[40] = cast(short)(d5 >> 22) ;
  977. if (dataRef[40] > 255) { dataRef[40] = 255; } else if (dataRef[40] < 0) { dataRef[40] = 0;}
  978. d0 = d3 + d4; d4 = d3 - d4; d3 = d0;
  979. d3 += (128 << 22); dataRef[24] = cast(short)(d3 >> 22) ;
  980. if (dataRef[24] > 255) { dataRef[24] = 255; } else if (dataRef[24] < 0) { dataRef[24] = 0;}
  981. d4 += (128 << 22); dataRef[32] = cast(short)(d4 >> 22) ;
  982. if (dataRef[32] > 255) { dataRef[32] = 255; } else if (dataRef[32] < 0) { dataRef[32] = 0;}
  983. dataRef++;
  984. }
  985. //the IDCT has been done...we now have uncompressed data for this 8x8, 8 bit component
  986. //goto the next sample or next component to complete the scan
  987. component_sample_counter++;
  988. if (component_sample_counter == sof_comp[component_counter].HxV) {
  989. component_counter++;
  990. component_sample_counter=0;
  991. }
  992. if (component_counter != sof.num_image_components) {
  993. // continue decoding components
  994. decoderState = JPEG_STATE_DECODE_HUFFMAN_INIT;
  995. continue;
  996. }
  997. decoderState = JPEG_STATE_RENDER_MCU;
  998. /* follow through */
  999. case JPEG_STATE_RENDER_MCU:
  1000. //writefln("jpeg - render - mcu");
  1001. //we have decoded the MCU at this point
  1002. //paint the MCU to the image
  1003. //now, for every 8x8 pixel block, we will take the 8x8 information we have to
  1004. //place the RGB transformed pixels on the image
  1005. // LOCK the buffer, if it hasn't been locked yet
  1006. if (intermediate_imgPos_Start is null) {
  1007. view.lockBuffer(cast(void**)&intermediate_imgPos_Start, image_length);
  1008. }
  1009. intermediate_imgPos_Start_MCU = intermediate_imgPos_Start + image_ptr_offset;
  1010. int d0=0; //Y data index
  1011. ushort* d0_pos;
  1012. ushort* d1_pos;
  1013. ushort* d2_pos;
  1014. int d3=255; //Y scan component index
  1015. int d4=255; //Cb scan component index
  1016. int d5=255; //Cr scan component index
  1017. int a;
  1018. for (a=0; a<3; a++) {
  1019. if (sof_comp[a].C == 1) {
  1020. //Y component
  1021. d0_pos = cast(ushort*)sof_comp[a].data.ptr;
  1022. d3=a;
  1023. }
  1024. else if (sof_comp[a].C == 2) {
  1025. //cb
  1026. d1_pos = cast(ushort*)sof_comp[a].data.ptr;
  1027. d4=a;
  1028. }
  1029. else if (sof_comp[a].C == 3) {
  1030. //cr
  1031. d2_pos = cast(ushort*)sof_comp[a].data.ptr;
  1032. d5=a;
  1033. }
  1034. }
  1035. a=0;
  1036. while (a<Vmajor) {
  1037. for (int q=0;q<Hmajor;q++) {
  1038. //code will be run through for every 8x8 block represented by this MCU
  1039. //in this order:
  1040. // /-------\
  1041. // | 1 | 2 |
  1042. // |-------| a 2x2 MCU (could also be 1x1, 1x2, or 2x1 in common situations)
  1043. // | 3 | 4 |
  1044. // \-------/
  1045. intermediate_imgPos = intermediate_imgPos_Start_MCU;
  1046. float ab;
  1047. if (sof.num_image_components == 1) {
  1048. for (int n=0; n<8; n++) {
  1049. for (int p=0; p<8; p++) {
  1050. ubyte r,g,b;
  1051. //write at the RGBA of intermediate_imgPos;
  1052. r = g = b = (cast(ubyte)d0_pos[d0]);
  1053. *(cast(uint*)intermediate_imgPos) = view.rgbaTouint(r,g,b,0xFF);
  1054. d0++;
  1055. intermediate_imgPos+=4;
  1056. }
  1057. intermediate_imgPos+=imgylinemovement-32;
  1058. }
  1059. }
  1060. else {
  1061. for (int n=0; n<8; n++) {
  1062. for (int p=0; p<8; p++) {
  1063. ubyte r,g,b;
  1064. //write at the RGBA of intermediate_imgPos;
  1065. ab = (cb_b_uncode[d1_pos[cb_upsample_lookup[d0]]] + cast(float)d0_pos[d0]);
  1066. if (ab > 255) { ab = 255; } else if (ab < 0) { ab = 0; }
  1067. b = cast(ubyte)(ab); //BB
  1068. ab = (cb_g_uncode[d1_pos[cb_upsample_lookup[d0]]] - cr_g_uncode[d2_pos[cr_upsample_lookup[d0]]] + cast(float)d0_pos[d0]);
  1069. if (ab > 255) { ab = 255; } else if (ab < 0) { ab = 0; }
  1070. g = cast(ubyte)(ab); //GG
  1071. ab = (cr_r_uncode[d2_pos[cr_upsample_lookup[d0]]] + cast(float)d0_pos[d0]);
  1072. if (ab > 255) { ab = 255; } else if (ab < 0) { ab = 0; }
  1073. r = cast(ubyte)(ab); //RR
  1074. *(cast(uint*)intermediate_imgPos) = view.rgbaTouint(r,g,b,0xFF);
  1075. d0++;
  1076. intermediate_imgPos+=4;
  1077. }
  1078. intermediate_imgPos+=imgylinemovement-32;
  1079. }
  1080. }
  1081. //translate pixels, place on image buffer
  1082. //move over image pointer in the X direction
  1083. intermediate_imgPos_Start_MCU+=32;
  1084. } //end for (q)
  1085. //reset X direction
  1086. //move down image pointer in the Y direction
  1087. a++;
  1088. intermediate_imgPos_Start_MCU = intermediate_imgPos_Start + (image_ptr_offset + (a * imgylinemovement_block));
  1089. } //end for (a)
  1090. //move image buffer pointer to point at coords of next MCU block
  1091. cur_block_x++;
  1092. if (cur_block_x == block_width) {
  1093. cur_block_x = 0;
  1094. cur_block_y++;
  1095. image_ptr_offset = (cur_block_y * imgylinemovement_block_start);
  1096. if(cur_block_y == block_height) {
  1097. //we should be done
  1098. //we have decoded the frame
  1099. if (intermediate_imgPos_Start !is null) {
  1100. intermediate_imgPos_Start = null;
  1101. view.unlockBuffer();
  1102. }
  1103. return StreamData.Complete;
  1104. }
  1105. }
  1106. else {
  1107. image_ptr_offset += imgxlinemovement_block_start;
  1108. }
  1109. component_counter=0;
  1110. decoderState = JPEG_STATE_DECODE_HUFFMAN_INIT;
  1111. continue;
  1112. default:
  1113. break;
  1114. }
  1115. break;
  1116. }
  1117. return StreamData.Invalid;
  1118. }
  1119. protected:
  1120. JPEG_RENDER_INFO jpeg_vars;
  1121. ushort chunkType;
  1122. ushort chunkLength;
  1123. ushort bytesToRead;
  1124. JFIF_HEADER jfif;
  1125. JPEG_SOF sof;
  1126. SCAN_COMPONENT_SELECTOR[] sof_comp;
  1127. JPEG_SOS sos;
  1128. JPEG_SOS_COMPONENTS sos_comp;
  1129. JPEG_SOS_SELECTOR sos_sel;
  1130. HUFFMAN_TABLE HT_DC[4];
  1131. HUFFMAN_TABLE HT_AC[4];
  1132. HUFFMAN_TABLE* cur_ht;
  1133. int quantization_destination;
  1134. int quantization_precision;
  1135. ushort[64][4] quantization_table;
  1136. uint actual_image_width;
  1137. uint actual_image_height;
  1138. uint block_width;
  1139. uint block_height;
  1140. ubyte Hmajor;
  1141. ubyte Vmajor;
  1142. ubyte[] cb_upsample_lookup;
  1143. ubyte[] cr_upsample_lookup;
  1144. uint component_counter;
  1145. uint component_sample_counter;
  1146. int data_start_pos;
  1147. ubyte cur_bit_pos;
  1148. ubyte cur_byte;
  1149. ushort huffman_code;
  1150. uint huffman_bits;
  1151. uint bits_to_read;
  1152. ushort bits_read;
  1153. uint first_bit;
  1154. uint cur_ac;
  1155. ubyte* intermediate_imgPos_Start;
  1156. ubyte* intermediate_imgPos_Start_MCU;
  1157. ubyte* intermediate_imgPos;
  1158. uint image_ptr_offset;
  1159. ulong image_length;
  1160. uint imgylinemovement;
  1161. uint imgylinemovement_block;
  1162. uint imgylinemovement_block_start;
  1163. uint imgxlinemovement_block_start;
  1164. uint cur_block_x;
  1165. uint cur_block_y;
  1166. }