PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/SharpImage/Internal/ThirdParty/CSJ2K/j2k/fileformat/writer/FileFormatWriter.cs

https://bitbucket.org/tbohnen/sharpimage
C# | 374 lines | 135 code | 61 blank | 178 comment | 10 complexity | 7c44248e03f65e89f9e8590d03536a71 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * cvs identifier:
  3. *
  4. * $Id: FileFormatWriter.java,v 1.13 2001/02/16 11:53:54 qtxjoas Exp $
  5. *
  6. * Class: FileFormatWriter
  7. *
  8. * Description: Writes the file format
  9. *
  10. *
  11. *
  12. * COPYRIGHT:
  13. *
  14. * This software module was originally developed by Raphaël Grosbois and
  15. * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
  16. * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
  17. * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
  18. * Centre France S.A) in the course of development of the JPEG2000
  19. * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
  20. * software module is an implementation of a part of the JPEG 2000
  21. * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
  22. * Systems AB and Canon Research Centre France S.A (collectively JJ2000
  23. * Partners) agree not to assert against ISO/IEC and users of the JPEG
  24. * 2000 Standard (Users) any of their rights under the copyright, not
  25. * including other intellectual property rights, for this software module
  26. * with respect to the usage by ISO/IEC and Users of this software module
  27. * or modifications thereof for use in hardware or software products
  28. * claiming conformance to the JPEG 2000 Standard. Those intending to use
  29. * this software module in hardware or software products are advised that
  30. * their use may infringe existing patents. The original developers of
  31. * this software module, JJ2000 Partners and ISO/IEC assume no liability
  32. * for use of this software module or modifications thereof. No license
  33. * or right to this software module is granted for non JPEG 2000 Standard
  34. * conforming products. JJ2000 Partners have full right to use this
  35. * software module for his/her own purpose, assign or donate this
  36. * software module to any third party and to inhibit third parties from
  37. * using this software module for non JPEG 2000 Standard conforming
  38. * products. This copyright notice must be included in all copies or
  39. * derivative works of this software module.
  40. *
  41. * Copyright (c) 1999/2000 JJ2000 Partners.
  42. * */
  43. namespace CSJ2K.j2k.fileformat.writer
  44. {
  45. using System;
  46. using CSJ2K.j2k.codestream;
  47. using CSJ2K.j2k.fileformat;
  48. using CSJ2K.j2k.io;
  49. /// <summary> This class writes the file format wrapper that may or may not exist around
  50. /// a valid JPEG 2000 codestream. This class writes the simple possible legal
  51. /// fileformat
  52. ///
  53. /// </summary>
  54. /// <seealso cref="jj2000.j2k.fileformat.reader.FileFormatReader">
  55. ///
  56. /// </seealso>
  57. internal class FileFormatWriter
  58. {
  59. #region Fields
  60. /// <summary>base length of Bits Per Component box </summary>
  61. private const int BPC_LENGTH = 8;
  62. /// <summary>Length of Colour Specification Box </summary>
  63. private const int CSB_LENGTH = 15;
  64. /// <summary>Length of File Type Box </summary>
  65. private const int FTB_LENGTH = 20;
  66. /// <summary>Length of Image Header Box </summary>
  67. private const int IHB_LENGTH = 22;
  68. /// <summary>Bits per component </summary>
  69. private int[] bpc;
  70. /// <summary>Flag indicating whether number of bits per component varies </summary>
  71. private bool bpcVaries;
  72. /// <summary>Length of codestream </summary>
  73. private int clength;
  74. /// <summary>The file from which to read the codestream and write file</summary>
  75. private BEBufferedRandomAccessFile fi;
  76. /// <summary>The name of the file from which to read the codestream and to write
  77. /// the JP2 file
  78. /// </summary>
  79. private System.String filename;
  80. /// <summary>Image height </summary>
  81. private int height;
  82. /// <summary>Number of components </summary>
  83. private int nc;
  84. /// <summary>Image width </summary>
  85. private int width;
  86. #endregion Fields
  87. #region Constructors
  88. /// <summary> The constructor of the FileFormatWriter. It receives all the
  89. /// information necessary about a codestream to generate a legal JP2 file
  90. ///
  91. /// </summary>
  92. /// <param name="filename">The name of the file that is to be made a JP2 file
  93. ///
  94. /// </param>
  95. /// <param name="height">The height of the image
  96. ///
  97. /// </param>
  98. /// <param name="width">The width of the image
  99. ///
  100. /// </param>
  101. /// <param name="nc">The number of components
  102. ///
  103. /// </param>
  104. /// <param name="bpc">The number of bits per component
  105. ///
  106. /// </param>
  107. /// <param name="clength">Length of codestream
  108. ///
  109. /// </param>
  110. public FileFormatWriter(System.String filename, int height, int width, int nc, int[] bpc, int clength)
  111. {
  112. this.height = height;
  113. this.width = width;
  114. this.nc = nc;
  115. this.bpc = bpc;
  116. this.filename = filename;
  117. this.clength = clength;
  118. bpcVaries = false;
  119. int fixbpc = bpc[0];
  120. for (int i = nc - 1; i > 0; i--)
  121. {
  122. if (bpc[i] != fixbpc)
  123. bpcVaries = true;
  124. }
  125. }
  126. #endregion Constructors
  127. #region Methods
  128. #region Public Methods
  129. /// <summary> This method writes the Bits Per Component box
  130. ///
  131. /// </summary>
  132. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  133. ///
  134. /// </exception>
  135. public virtual void writeBitsPerComponentBox()
  136. {
  137. // Write box length (LBox)
  138. fi.writeInt(BPC_LENGTH + nc);
  139. // Write a Bits Per Component box (TBox)
  140. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.BITS_PER_COMPONENT_BOX);
  141. // Write bpc fields
  142. for (int i = 0; i < nc; i++)
  143. {
  144. fi.writeByte(bpc[i] - 1);
  145. }
  146. }
  147. /// <summary> This method writes the Colour Specification box
  148. ///
  149. /// </summary>
  150. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  151. ///
  152. /// </exception>
  153. public virtual void writeColourSpecificationBox()
  154. {
  155. // Write box length (LBox)
  156. fi.writeInt(CSB_LENGTH);
  157. // Write a Bits Per Component box (TBox)
  158. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.COLOUR_SPECIFICATION_BOX);
  159. // Write METH field
  160. fi.writeByte(CSJ2K.j2k.fileformat.FileFormatBoxes.CSB_METH);
  161. // Write PREC field
  162. fi.writeByte(CSJ2K.j2k.fileformat.FileFormatBoxes.CSB_PREC);
  163. // Write APPROX field
  164. fi.writeByte(CSJ2K.j2k.fileformat.FileFormatBoxes.CSB_APPROX);
  165. // Write EnumCS field
  166. if (nc > 1)
  167. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.CSB_ENUM_SRGB);
  168. else
  169. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.CSB_ENUM_GREY);
  170. }
  171. /// <summary> This method writes the Contiguous codestream box
  172. ///
  173. /// </summary>
  174. /// <param name="cs">The contiguous codestream
  175. ///
  176. /// </param>
  177. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  178. ///
  179. /// </exception>
  180. public virtual void writeContiguousCodeStreamBox(byte[] cs)
  181. {
  182. // Write box length (LBox)
  183. // This value is set to 0 since in this implementation, this box is
  184. // always last
  185. fi.writeInt(clength + 8);
  186. // Write contiguous codestream box name (TBox)
  187. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.CONTIGUOUS_CODESTREAM_BOX);
  188. // Write codestream
  189. for (int i = 0; i < clength; i++)
  190. fi.writeByte(cs[i]);
  191. }
  192. /// <summary> This method reads the codestream and writes the file format wrapper and
  193. /// the codestream to the same file
  194. ///
  195. /// </summary>
  196. /// <returns> The number of bytes increases because of the file format
  197. ///
  198. /// </returns>
  199. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  200. ///
  201. /// </exception>
  202. public virtual int writeFileFormat()
  203. {
  204. byte[] codestream;
  205. try
  206. {
  207. // Read and buffer the codestream
  208. fi = new BEBufferedRandomAccessFile(filename, "rw+");
  209. codestream = new byte[clength];
  210. fi.readFully(codestream, 0, clength);
  211. // Write the JP2_SINATURE_BOX
  212. fi.seek(0);
  213. fi.writeInt(0x0000000c);
  214. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_SIGNATURE_BOX);
  215. fi.writeInt(0x0d0a870a);
  216. // Write File Type box
  217. writeFileTypeBox();
  218. // Write JP2 Header box
  219. writeJP2HeaderBox();
  220. // Write the Codestream box
  221. writeContiguousCodeStreamBox(codestream);
  222. fi.close();
  223. }
  224. catch (System.Exception e)
  225. {
  226. throw new System.ApplicationException("Error while writing JP2 file format(2): " + e.Message + "\n" + e.StackTrace);
  227. }
  228. if (bpcVaries)
  229. return 12 + FTB_LENGTH + 8 + IHB_LENGTH + CSB_LENGTH + BPC_LENGTH + nc + 8;
  230. else
  231. return 12 + FTB_LENGTH + 8 + IHB_LENGTH + CSB_LENGTH + 8;
  232. }
  233. /// <summary> This method writes the File Type box
  234. ///
  235. /// </summary>
  236. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  237. ///
  238. /// </exception>
  239. public virtual void writeFileTypeBox()
  240. {
  241. // Write box length (LBox)
  242. // LBox(4) + TBox (4) + BR(4) + MinV(4) + CL(4) = 20
  243. fi.writeInt(FTB_LENGTH);
  244. // Write File Type box (TBox)
  245. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.FILE_TYPE_BOX);
  246. // Write File Type data (DBox)
  247. // Write Brand box (BR)
  248. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.FT_BR);
  249. // Write Minor Version
  250. fi.writeInt(0);
  251. // Write Compatibility list
  252. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.FT_BR);
  253. }
  254. /// <summary> This method writes the Image Header box
  255. ///
  256. /// </summary>
  257. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  258. ///
  259. /// </exception>
  260. public virtual void writeImageHeaderBox()
  261. {
  262. // Write box length
  263. fi.writeInt(IHB_LENGTH);
  264. // Write ihdr box name
  265. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.IMAGE_HEADER_BOX);
  266. // Write HEIGHT field
  267. fi.writeInt(height);
  268. // Write WIDTH field
  269. fi.writeInt(width);
  270. // Write NC field
  271. fi.writeShort(nc);
  272. // Write BPC field
  273. // if the number of bits per component varies write 0xff else write
  274. // number of bits per components
  275. if (bpcVaries)
  276. fi.writeByte(0xff);
  277. else
  278. fi.writeByte(bpc[0] - 1);
  279. // Write C field
  280. fi.writeByte(CSJ2K.j2k.fileformat.FileFormatBoxes.IMB_C);
  281. // Write UnkC field
  282. fi.writeByte(CSJ2K.j2k.fileformat.FileFormatBoxes.IMB_UnkC);
  283. // Write IPR field
  284. fi.writeByte(CSJ2K.j2k.fileformat.FileFormatBoxes.IMB_IPR);
  285. }
  286. /// <summary> This method writes the JP2Header box
  287. ///
  288. /// </summary>
  289. /// <exception cref="java.io.IOException">If an I/O error ocurred.
  290. ///
  291. /// </exception>
  292. public virtual void writeJP2HeaderBox()
  293. {
  294. // Write box length (LBox)
  295. // if the number of bits per components varies, a bpcc box is written
  296. if (bpcVaries)
  297. fi.writeInt(8 + IHB_LENGTH + CSB_LENGTH + BPC_LENGTH + nc);
  298. else
  299. fi.writeInt(8 + IHB_LENGTH + CSB_LENGTH);
  300. // Write a JP2Header (TBox)
  301. fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_HEADER_BOX);
  302. // Write image header box
  303. writeImageHeaderBox();
  304. // Write Colour Bpecification Box
  305. writeColourSpecificationBox();
  306. // if the number of bits per components varies write bpcc box
  307. if (bpcVaries)
  308. writeBitsPerComponentBox();
  309. }
  310. #endregion Public Methods
  311. #endregion Methods
  312. }
  313. }