JPEGComponent.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* JPEGComponent.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.imageio.jpeg;
  32. import java.util.ArrayList;
  33. import java.io.IOException;
  34. import java.awt.image.WritableRaster;
  35. import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
  36. /**
  37. * This class holds the methods to decode and write a component information to
  38. * a raster.
  39. */
  40. public class JPEGComponent
  41. {
  42. public byte factorH, factorV, component_id, quant_id;
  43. public int width = 0, height = 0, maxV = 0, maxH = 0;
  44. public HuffmanTable ACTable;
  45. public HuffmanTable DCTable;
  46. public int[] quantizationTable;
  47. public double previousDC = 0;
  48. ArrayList data = new ArrayList();
  49. /**
  50. * Initializes the component
  51. *
  52. * @param id
  53. * @param factorHorizontal
  54. * @param factorVertical
  55. * @param quantizationID
  56. */
  57. public JPEGComponent(byte id, byte factorHorizontal, byte factorVertical,
  58. byte quantizationID)
  59. {
  60. component_id = id;
  61. factorH = factorHorizontal;
  62. factorV = factorVertical;
  63. quant_id = quantizationID;
  64. }
  65. /**
  66. * If a restart marker is found with too little of an MCU count (i.e. our
  67. * Restart Interval is 63 and we have 61 we copy the last MCU until it's
  68. * full)
  69. *
  70. * @param index
  71. * @param length
  72. */
  73. public void padMCU(int index, int length)
  74. {
  75. double[] src = (double[]) data.get(index - 1);
  76. for (int i = 0; i < length; i++)
  77. data.add(index, src);
  78. }
  79. /**
  80. * Reset the interval by setting the previous DC value
  81. */
  82. public void resetInterval()
  83. {
  84. previousDC = 0;
  85. }
  86. /**
  87. * Run the Quantization backward method on all of the block data.
  88. */
  89. public void quantitizeData()
  90. {
  91. for (int i = 0; i < data.size(); i++)
  92. {
  93. double[] mydata = (double[]) data.get(i);
  94. for (int j = 0; j < mydata.length; j++)
  95. mydata[j] *= quantizationTable[j];
  96. }
  97. }
  98. public void setDCTable(JPEGHuffmanTable table)
  99. {
  100. DCTable = new HuffmanTable(table);
  101. }
  102. public void setACTable(JPEGHuffmanTable table)
  103. {
  104. ACTable = new HuffmanTable(table);
  105. }
  106. /**
  107. * Run the Inverse DCT method on all of the block data
  108. */
  109. public void idctData(DCT myDCT)
  110. {
  111. for (int i = 0; i < data.size(); i++)
  112. data.add(i,myDCT.fast_idct(ZigZag.decode8x8_map((double[]) data.remove(i))));
  113. }
  114. /**
  115. * This scales up the component size based on the factor size. This
  116. * calculates everyting up automatically so it's simply ran at the end of
  117. * the frame to normalize the size of all of the components.
  118. */
  119. public void scaleByFactors()
  120. {
  121. int factorUpVertical = maxV / factorV;
  122. int factorUpHorizontal = maxH / factorH;
  123. if (factorUpVertical > 1)
  124. {
  125. for (int i = 0; i < data.size(); i++)
  126. {
  127. double[][] src = (double[][]) data.remove(i);
  128. double[][] dest =
  129. new double[src.length * factorUpVertical][src[0].length];
  130. for (int j = 0; j < src.length; j++)
  131. {
  132. for (int u = 0; u < factorUpVertical; u++)
  133. {
  134. dest[j * factorUpVertical + u] = src[j];
  135. }
  136. }
  137. data.add(i, dest);
  138. }
  139. }
  140. if (factorUpHorizontal > 1)
  141. {
  142. for (int i = 0; i < data.size(); i++)
  143. {
  144. double[][] src = (double[][]) data.remove(i);
  145. double[][] dest =
  146. new double[src.length][src[0].length * factorUpHorizontal];
  147. for (int j = 0; j < src.length; j++)
  148. {
  149. for (int u = 0; u < src[0].length; u++)
  150. {
  151. for (int v = 0; v < factorUpHorizontal; v++)
  152. dest[j][u * factorUpHorizontal + v] = src[j][u];
  153. }
  154. }
  155. data.add(i, dest);
  156. }
  157. }
  158. }
  159. /**
  160. * This write the block of data to the raster throwing out anything that
  161. * spills over the raster width or height.
  162. *
  163. * @param raster
  164. * @param data
  165. * @param compIndex
  166. * @param x
  167. * @param y
  168. */
  169. public void writeBlock(WritableRaster raster, double[][] data,
  170. int compIndex, int x, int y)
  171. {
  172. for (int yIndex = 0; yIndex < data.length; yIndex++)
  173. {
  174. for (int xIndex = 0; xIndex < data[yIndex].length; xIndex++)
  175. {
  176. // The if statement is needed because blocks can spill over the
  177. // frame width because they are padded to make sure we keep the
  178. // height of the block the same as the width of the block
  179. if (x + xIndex < raster.getWidth()
  180. && y + yIndex < raster.getHeight())
  181. raster.setSample(x + xIndex, y + yIndex, compIndex,
  182. data[yIndex][xIndex]);
  183. }
  184. }
  185. }
  186. /**
  187. * This writes data to a raster block, so really it's reading not writing
  188. * but it writes the data to the raster block by factor size in a zig zag
  189. * fashion. This has the helper function writeBlock which does the actual
  190. * writing.
  191. *
  192. * @param raster
  193. * @param componentIndex
  194. */
  195. public void writeData(WritableRaster raster, int componentIndex)
  196. {
  197. int x = 0, y = 0, lastblockheight = 0, incrementblock = 0;
  198. // Keep looping through all of the blocks until there are no more.
  199. while(data.size() > 0)
  200. {
  201. int blockwidth = 0;
  202. int blockheight = 0;
  203. if (x >= raster.getWidth())
  204. {
  205. x = 0;
  206. y += incrementblock;
  207. }
  208. // Loop through the horizontal component blocks of the MCU first
  209. // then for each horizontal line write out all of the vertical
  210. // components
  211. for (int factorVIndex = 0; factorVIndex < factorV; factorVIndex++)
  212. {
  213. blockwidth = 0;
  214. for (int factorHIndex = 0; factorHIndex < factorH; factorHIndex++)
  215. {
  216. // Captures the width of this block so we can increment the
  217. // X coordinate
  218. double[][] blockdata = (double[][]) data.remove(0);
  219. // Writes the data at the specific X and Y coordinate of
  220. // this component
  221. writeBlock(raster, blockdata, componentIndex, x, y);
  222. blockwidth += blockdata[0].length;
  223. x += blockdata[0].length;
  224. blockheight = blockdata.length;
  225. }
  226. y += blockheight;
  227. x -= blockwidth;
  228. lastblockheight += blockheight;
  229. }
  230. y -= lastblockheight;
  231. incrementblock = lastblockheight;
  232. lastblockheight = 0;
  233. x += blockwidth;
  234. }
  235. }
  236. /**
  237. * Set the quantization table for this component.
  238. *
  239. * @param quanttable
  240. */
  241. public void setQuantizationTable(int[] quanttable)
  242. {
  243. quantizationTable = quanttable;
  244. }
  245. /**
  246. * Read in a partial MCU for this component
  247. *
  248. * @param stream TODO
  249. * @throws JPEGException TODO
  250. * @throws IOException TODO
  251. */
  252. public void readComponentMCU(JPEGImageInputStream stream)
  253. throws JPEGException, IOException
  254. {
  255. for (int i = 0; i < factorH * factorV; i++)
  256. {
  257. double dc = decode_dc_coefficient(stream);
  258. double[] datablock = decode_ac_coefficients(stream);
  259. datablock[0] = dc;
  260. data.add(datablock);
  261. }
  262. }
  263. /**
  264. * Generated from text on F-22, F.2.2.1 - Huffman decoding of DC
  265. * coefficients on ISO DIS 10918-1. Requirements and Guidelines.
  266. *
  267. * @param JPEGStream TODO
  268. *
  269. * @return TODO
  270. * @throws JPEGException TODO
  271. * @throws IOException TODO
  272. */
  273. public double decode_dc_coefficient(JPEGImageInputStream JPEGStream)
  274. throws JPEGException, IOException
  275. {
  276. int t = DCTable.decode(JPEGStream);
  277. double diff = JPEGStream.readBits(t);
  278. diff = HuffmanTable.extend((int) diff, t);
  279. diff = (previousDC + diff);
  280. previousDC = diff;
  281. return diff;
  282. }
  283. /**
  284. * Generated from text on F-23, F.13 - Huffman decoded of AC coefficients
  285. * on ISO DIS 10918-1. Requirements and Guidelines.
  286. *
  287. * @param JPEGStream TODO
  288. * @return TODO
  289. *
  290. * @throws JPEGException TODO
  291. * @throws IOException TODO
  292. */
  293. public double[] decode_ac_coefficients(JPEGImageInputStream JPEGStream)
  294. throws JPEGException, IOException
  295. {
  296. double[] zz = new double[64];
  297. for (int k = 1; k < 64; k++)
  298. {
  299. int s = ACTable.decode(JPEGStream);
  300. int r = s >> 4;
  301. s &= 15;
  302. if (s != 0)
  303. {
  304. k += r;
  305. r = (int) JPEGStream.readBits(s);
  306. s = HuffmanTable.extend(r, s);
  307. zz[k] = s;
  308. }
  309. else
  310. {
  311. if (r != 15)
  312. return (zz);
  313. k += 15;
  314. }
  315. }
  316. return zz;
  317. }
  318. }