/// <summary> This method performs the actual manipulation of the codestream which is /// the reparsing for tile parts and packed packet headers /// /// </summary> /// <returns> The number of bytes that the file has increased by /// /// </returns> /// <exception cref="IOException">If an I/O error ocurred. /// /// </exception> public virtual int doCodestreamManipulation() { BEBufferedRandomAccessFile fi; int addedHeaderBytes = 0; ppt = new int[nt]; tileParts = new byte[nt][][]; tileHeaders = new byte[nt][]; packetHeaders = new byte[nt][][]; packetData = new byte[nt][][]; sopMarkSeg = new byte[nt][][]; // If neither packed packet header nor tile parts are used, return 0 if (ppmUsed == false && pptUsed == false && pptp == 0) return 0; // Open file for reading and writing fi = new BEBufferedRandomAccessFile(stream, false); addedHeaderBytes -= fi.length(); // Parse the codestream for SOT, SOP and EPH markers parseAndFind(fi); // Read and buffer the tile headers, packet headers and packet data readAndBuffer(fi); // Rewind and overwrite with new contents fi.seek(0); // Create tile-parts createTileParts(); // Write new codestream writeNewCodestream(fi); // Close file fi.flush(); addedHeaderBytes += fi.length(); fi.close(); return addedHeaderBytes; }
/// <summary> This method reads the codestream and writes the file format wrapper and /// the codestream to the same file /// /// </summary> /// <returns> The number of bytes increases because of the file format /// /// </returns> /// <exception cref="java.io.IOException">If an I/O error ocurred. /// /// </exception> public virtual int writeFileFormat() { byte[] codestream; try { // Read and buffer the codestream fi = new BEBufferedRandomAccessFile(filename, "rw+"); codestream = new byte[clength]; fi.readFully(codestream, 0, clength); // Write the JP2_SINATURE_BOX fi.seek(0); fi.writeInt(0x0000000c); fi.writeInt(CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_SIGNATURE_BOX); fi.writeInt(0x0d0a870a); // Write File Type box writeFileTypeBox(); // Write JP2 Header box writeJP2HeaderBox(); // Write the Codestream box writeContiguousCodeStreamBox(codestream); fi.close(); } catch (System.Exception e) { throw new System.ApplicationException("Error while writing JP2 file format(2): " + e.Message + "\n" + e.StackTrace); } if (bpcVaries) return 12 + FTB_LENGTH + 8 + IHB_LENGTH + CSB_LENGTH + BPC_LENGTH + nc + 8; else return 12 + FTB_LENGTH + 8 + IHB_LENGTH + CSB_LENGTH + 8; }