📄 imgwriterpgx.java
字号:
int c, boolean isSigned) throws IOException{ this(new File(fname),imgSrc,c,isSigned); } /** * Closes the underlying file or netwrok connection to where the data is * written. Any call to other methods of the class become illegal after a * call to this one. * * @exception IOException If an I/O error occurs. * */ public void close() throws IOException { int i; // Finish writing the file, writing 0s at the end if the data at end // has not been written. if(out.length() != w*h*packBytes+offset) { // Goto end of file out.seek(out.length()); // Fill with 0s for(i=offset+w*h*packBytes-(int)out.length(); i>0; i--) { out.writeByte(0); } } out.close(); src = null; out = null; db = null; } /** * Writes all buffered data to the file or resource. * * @exception IOException If an I/O error occurs. * */ public void flush() throws IOException { // No flush is needed since we use RandomAccessFile // Get rid of line buffer (is this a good choice?) buf = null; } /** * Writes the data of the specified area to the file, coordinates are * relative to the current tile of the source. Before writing, the * coefficients are limited to the nominal range and packed into 1,2 or 4 * bytes (according to the bit-depth). * * <p>If the data is unisigned, level shifting is applied adding 2^(bit * depth - 1)</p> * * <p>This method may not be called concurrently from different * threads.</p> * * <p>If the data returned from the BlkImgDataSrc source is progressive, * then it is requested over and over until it is not progressive * anymore.</p> * * @param ulx The horizontal coordinate of the upper-left corner of the * area to write, relative to the current tile. * * @param uly The vertical coordinate of the upper-left corner of the area * to write, relative to the current tile. * * @param width The width of the area to write. * * @param height The height of the area to write. * * @exception IOException If an I/O error occurs. * */ public void write(int ulx, int uly, int w, int h) throws IOException { int k,i,j; int fracbits = fb; // In local variable for faster access int tOffx, tOffy; // Active tile offset in the X and Y direction // Initialize db db.ulx = ulx; db.uly = uly; db.w = w; db.h = h; // Get the current active tile offset tOffx = src.getCompULX(c)- (int)Math.ceil(src.getImgULX()/(double)src.getCompSubsX(c)); tOffy = src.getCompULY(c)- (int)Math.ceil(src.getImgULY()/(double)src.getCompSubsY(c)); // Check the array size if(db.data!=null && db.data.length<w*h) { // A new one will be allocated by getInternCompData() db.data = null; } // Request the data and make sure it is not // progressive do { db = (DataBlkInt) src.getInternCompData(db,c); } while (db.progressive); int tmp; // Check line buffer if(buf==null || buf.length<packBytes*w) { buf = new byte[packBytes*w]; // Expand buffer } switch(packBytes) { case 1: // Samples packed into 1 byte // Write line by line for(i=0; i<h; i++) { // Skip to beggining of line in file out.seek(offset+this.w*(uly+tOffy+i)+ulx+tOffx); // Write all bytes in the line if(fracbits==0) { for(k=db.offset+i*db.scanw+w-1, j=w-1; j>=0; k--) { tmp = db.data[k]+levShift; buf[j--] = (byte)((tmp < minVal) ? minVal : ((tmp>maxVal)? maxVal: tmp)); } } else { for (k=db.offset+i*db.scanw+w-1, j=w-1; j>=0; k--) { tmp = (db.data[k]>>>fracbits)+levShift; buf[j--] = (byte)((tmp < minVal) ? minVal : ((tmp>maxVal)? maxVal: tmp)); } } out.write(buf,0,w); } break; case 2: // Samples packed in to 2 bytes (short) // Write line by line for(i=0; i<h; i++) { // Skip to beggining of line in file out.seek(offset+2*(this.w*(uly+tOffy+i)+ulx+tOffx)); // Write all bytes in the line if(fracbits==0) { for (k=db.offset+i*db.scanw+w-1, j=(w<<1)-1; j>=0; k--) { tmp = db.data[k]+levShift; tmp = (tmp<minVal) ? minVal : ((tmp>maxVal)? maxVal: tmp); buf[j--] = (byte)tmp; // no need for 0xFF mask since // truncation will do it already buf[j--] = (byte)(tmp>>>8); } } else { for (k=db.offset+i*db.scanw+w-1, j=(w<<1)-1; j>=0; k--) { tmp = (db.data[k]>>>fracbits)+levShift; tmp = (tmp<minVal) ? minVal : ((tmp>maxVal)? maxVal: tmp); buf[j--] = (byte)tmp; // no need for 0xFF mask since // truncation will do it already buf[j--] = (byte)(tmp>>>8); } } out.write(buf,0,w<<1); } break; case 4: // Write line by line for(i=0; i<h; i++) { // Skip to beggining of line in file out.seek(offset+4*(this.w*(uly+tOffy+i)+ulx+tOffx)); // Write all bytes in the line if(fracbits==0) { for(k=db.offset+i*db.scanw+w-1, j=(w<<2)-1; j>=0; k--) { tmp = db.data[k]+levShift; tmp = (tmp<minVal) ? minVal : ((tmp>maxVal)? maxVal: tmp); buf[j--] = (byte)tmp; // No need to use 0xFF buf[j--] = (byte)(tmp>>>8); // masks since truncation buf[j--] = (byte)(tmp>>>16); // will have already the buf[j--] = (byte)(tmp>>>24); // same effect } } else { for(k=db.offset+i*db.scanw+w-1, j=(w<<2)-1; j>=0; k--) { tmp = (db.data[k]>>>fracbits)+levShift; tmp = (tmp<minVal) ? minVal : ((tmp>maxVal)? maxVal: tmp); buf[j--] = (byte)tmp; // No need to use 0xFF buf[j--] = (byte)(tmp>>>8); // masks since truncation buf[j--] = (byte)(tmp>>>16); // will have already the buf[j--] = (byte)(tmp>>>24); // same effect } } out.write(buf,0,w<<2); } break; default: throw new IOException("PGX supports only bit-depth between "+ "1 and 31"); } } /** * Writes the source's current tile to the output. The requests of data * issued to the source BlkImgDataSrc object are done by strips, in order * to reduce memory usage. * * <p>If the data returned from the BlkImgDataSrc source is progressive, * then it is requested over and over until it is not progressive * anymore.</p> * * @exception IOException If an I/O error occurs. * * @see DataBlk * */ public void write() throws IOException { int i; int tIdx = src.getTileIdx(); int tw = src.getTileCompWidth(tIdx,c); // Tile width int th = src.getTileCompHeight(tIdx,c); // Tile height // Write in strips for(i=0; i<th ; i+=DEF_STRIP_HEIGHT) { write(0,i,tw,(th-i<DEF_STRIP_HEIGHT) ? th-i : DEF_STRIP_HEIGHT); } } /** * Returns a string of information about the object, more than 1 line * long. The information string includes information from the underlying * RandomAccessFile (its toString() method is called in turn). * * @return A string of information about the object. * */ public String toString() { return "ImgWriterPGX: WxH = " + w + "x" + h + ", Component = "+ c + ", Bit-depth = "+bitDepth + ", signed = "+isSigned + "\nUnderlying RandomAccessFile:\n" + out.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -