📄 imgreaderppm.java
字号:
(dbi.ulx > blk.ulx) || (dbi.uly > blk.uly) || (dbi.ulx+dbi.w < blk.ulx+blk.w) || (dbi.uly+dbi.h < blk.uly+blk.h)) { int k,j,i,mi; int[] red,green,blue; // Reset data arrays if needed if (barr[c] == null || barr[c].length < blk.w*blk.h) { barr[c] = new int[blk.w*blk.h]; } blk.setData(barr[c]); i = (c+1)%3; if (barr[i] == null || barr[i].length < blk.w*blk.h) { barr[i] = new int[blk.w*blk.h]; } i = (c+2)%3; if (barr[i] == null || barr[i].length < blk.w*blk.h) { barr[i] = new int[blk.w*blk.h]; } // set attributes of the DataBlk used for buffering dbi.ulx = blk.ulx; dbi.uly = blk.uly; dbi.w = blk.w; dbi.h = blk.h; // Check line buffer if (buf == null || buf.length < 3*blk.w) { buf = new byte[3*blk.w]; } red = barr[0]; green = barr[1]; blue = barr[2]; try { // Read line by line mi = blk.uly + blk.h; for (i = blk.uly; i < mi; i++) { // Reposition in input offset takes care of // header offset in.seek(offset+i*3*w+3*blk.ulx); in.read(buf,0,3*blk.w); for (k = (i-blk.uly)*blk.w+blk.w-1, j = 3*blk.w-1; j >= 0; k--) { // Read every third sample blue[k] = (((byte)buf[j--])&0xFF)-DC_OFFSET; green[k] = (((byte)buf[j--])&0xFF)-DC_OFFSET; red[k] = (((byte)buf[j--])&0xFF)-DC_OFFSET; } } } catch (IOException e) { JJ2KExceptionHandler.handleException(e); } barr[0] = red; barr[1] = green; barr[2] = blue; // Set buffer attributes blk.setData(barr[c]); blk.offset = 0; blk.scanw = blk.w; } else { //Asking for the 2nd or 3rd block component blk.setData(barr[c]); blk.offset = (blk.ulx-dbi.ulx)*dbi.w+blk.ulx-dbi.ulx; blk.scanw = dbi.scanw; } // Turn off the progressive attribute blk.progressive = false; return blk; } /** * Returns, in the blk argument, a block of image data containing the * specifed rectangular area, in the specified component. The data is * returned, as a copy of the internal data, therefore the returned data * can be modified "in place". * * <P> After being read the coefficients are level shifted by subtracting * 2^(nominal bit range - 1) * * <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w' * and 'h' members of the 'blk' argument, relative to the current * tile. These members are not modified by this method. The 'offset' of * the returned data is 0, and the 'scanw' is the same as the block's * width. See the 'DataBlk' class. * * <P>If the data array in 'blk' is 'null', then a new one is created. If * the data array is not 'null' then it is reused, and it must be large * enough to contain the block's data. Otherwise an 'ArrayStoreException' * or an 'IndexOutOfBoundsException' is thrown by the Java system. * * <P>The returned data has its 'progressive' attribute unset * (i.e. false). * * <P>When an I/O exception is encountered the JJ2KExceptionHandler is * used. The exception is passed to its handleException method. The action * that is taken depends on the action that has been registered in * JJ2KExceptionHandler. See JJ2KExceptionHandler for details. * * @param blk Its coordinates and dimensions specify the area to * return. If it contains a non-null data array, then it must have the * correct dimensions. If it contains a null data array a new one is * created. The fields in this object are modified to return the data. * * @param c The index of the component from which to get the data. Only * 0,1 and 2 are valid. * * @return The requested DataBlk * * @see #getInternCompData * * @see JJ2KExceptionHandler * */ public final DataBlk getCompData(DataBlk blk, int c) { // NOTE: can not directly call getInterCompData since that returns // internally buffered data. int ulx,uly,w,h; // Check type of block provided as an argument if(blk.getDataType()!=DataBlk.TYPE_INT){ DataBlkInt tmp = new DataBlkInt(blk.ulx,blk.uly,blk.w,blk.h); blk = tmp; } int bakarr[] = (int[])blk.getData(); // Save requested block size ulx = blk.ulx; uly = blk.uly; w = blk.w; h = blk.h; // Force internal data buffer to be different from external blk.setData(null); getInternCompData(blk,c); // Copy the data if (bakarr == null) { bakarr = new int[w*h]; } if (blk.offset == 0 && blk.scanw == w) { // Requested and returned block buffer are the same size System.arraycopy(blk.getData(),0,bakarr,0,w*h); } else { // Requested and returned block are different for (int i=h-1; i>=0; i--) { // copy line by line System.arraycopy(blk.getData(),blk.offset+i*blk.scanw, bakarr,i*w,w); } } blk.setData(bakarr); blk.offset = 0; blk.scanw = blk.w; return blk; } /** * Returns a byte read from the RandomAccessFile. The number of read byted * are counted to keep track of the offset of the pixel data in the PPM * file * * @return One byte read from the header of the PPM file. * */ private byte countedByteRead() throws IOException, EOFException{ offset++; return in.readByte(); } /** * Checks that the file begins with 'P6' * */ private void confirmFileType() throws IOException, EOFException{ byte[] type={80,54}; int i; byte b; for(i=0;i<2;i++){ b = countedByteRead(); if(b!=type[i]){ if(i==1 && b==51) { // i.e 'P3' throw new IllegalArgumentException("JJ2000 does not support"+ " ascii-PPM files. Use "+ " raw-PPM file instead. "); } else { throw new IllegalArgumentException("Not a raw-PPM file"); } } } } /** * Skips any line in the header starting with '#' and any space, tab, line * feed or carriage return. * */ private void skipCommentAndWhiteSpace() throws IOException, EOFException{ boolean done=false; byte b; while(!done){ b=countedByteRead(); if(b==35){ // Comment start while(b!=10 && b!=13){ // While not comment end (end-of-line) b=countedByteRead(); } }else if(!(b==9||b==10||b==13||b==32)){ // If not whitespace done=true; } } // Put back last valid byte offset--; in.seek(offset); } /** * Returns an int read from the header of the PPM file. * * @return One int read from the header of the PPM file. * */ private int readHeaderInt() throws IOException, EOFException{ int res=0; byte b=0; b=countedByteRead(); while(b!=32&&b!=10&&b!=9&&b!=13){ // While not whitespace res=res*10+b-48; // Convert from ASCII to decimal b=countedByteRead(); } return res; } /** * Returns true if the data read was originally signed in the specified * component, false if not. This method always returns false since PPM * data is always unsigned. * * @param c The index of the component, from 0 to N-1. * * @return always false, since PPM data is always unsigned. * */ public boolean isOrigSigned(int c) { // Check component index if (c < 0 || c > 2) throw new IllegalArgumentException(); return false; } /** * 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 "ImgReaderPPM: WxH = " + w + "x" + h + ", Component = 0,1,2" + "\nUnderlying RandomAccessFile:\n" + in.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -