pngimagedecoder.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 911 行 · 第 1/3 页

JAVA
911
字号
            case COLOR | PALETTE:            case COLOR | PALETTE | ALPHA:                if (red_map == null) throw new PNGException("palette expected");                if (alpha_map == null)                    cm = new IndexColorModel(bitDepth, red_map.length,                                red_map, green_map, blue_map);                else                    cm = new IndexColorModel(bitDepth, red_map.length,                                red_map, green_map, blue_map, alpha_map);                bPixels = new byte[pixSize];                break;            case GRAY: {                    int llog = logDepth >= 4 ? 3 : logDepth;                    if ((cm = greyModels[llog]) == null) {                        int size = 1 << (1 << llog);                        byte ramp[] = new byte[size];                        for (int i = 0; i < size; i++) ramp[i] = (byte) (255 * i / (size - 1));                        cm = new IndexColorModel(bitDepth, ramp.length, ramp, ramp, ramp);                        greyModels[llog] = cm;                    }                }                bPixels = new byte[pixSize];                break;            case COLOR:            case COLOR | ALPHA:            case GRAY | ALPHA:                cm = ColorModel.getRGBdefault();                wPixels = new int[pixSize];                break;            default:                throw new PNGException("invalid color type");            }            /* this is going to be set in the pixel store             t.setColorModel(cm);             t.setHints(interlaceMethod !=0             ? ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES             : ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES |             ImageConsumer.SINGLEPASS | ImageConsumer.SINGLEFRAME);             */            // code added to make it work with ImageDecoder architecture            setDimensions(width, height);            setColorModel(cm);            // 4679080 - set properties as in JPEG and GIF image architecture.            setProperties(properties);            // end fix - 4679080                        int flags = (interlaceMethod != 0                    ? ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES                    : ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES |                    ImageConsumer.SINGLEPASS | ImageConsumer.SINGLEFRAME);            setHints(flags);            headerComplete();            // end of adding            int samplesPerPixel = ((colorType & PALETTE) != 0 ? 1                    : ((colorType & COLOR) != 0 ? 3 : 1) + ((colorType & ALPHA) != 0 ? 1 : 0));            int bitsPerPixel = samplesPerPixel * bitDepth;            int bytesPerPixel = (bitsPerPixel + 7) >> 3;            int pass, passLimit;            if (interlaceMethod == 0) {                pass = -1;                passLimit = 0;            } else {                pass = 0;                passLimit = 7;            }            // These loops are far from being tuned.  They're this way to make them easy to            // debug.  Tuning comes later.            /* code changed. target not needed here             while(++pass<=passLimit && (t=target)!=null) {             */            while (++pass <= passLimit) {                int row = startingRow[pass];                int rowInc = rowIncrement[pass];                int colInc = colIncrement[pass];                //int bWidth = blockWidth[pass];                //int bHeight = blockHeight[pass];                int sCol = startingCol[pass];                int rowPixelWidth = (width - sCol + (colInc - 1)) / colInc;                int rowByteWidth = ((rowPixelWidth * bitsPerPixel) + 7) >> 3;                if (rowByteWidth == 0) continue;                int rowOffset = rowStride * row;                boolean firstRow = true;                                byte[] rowByteBuffer = new byte[rowByteWidth];                byte[] prevRowByteBuffer = new byte[rowByteWidth];                /* code changed. target not needed here                  while (row < height && (t=target)!=null) {                 */                while (row < height) {                    int rowFilter = is.read();                    for (int rowFillPos = 0; rowFillPos < rowByteWidth;) {                        int n = is.read(rowByteBuffer, rowFillPos, rowByteWidth - rowFillPos);                        if (n <= 0) throw new PNGException("missing data");                        rowFillPos += n;                    }                    filterRow(rowByteBuffer,                              firstRow ? null : prevRowByteBuffer,                              rowFilter, rowByteWidth, bytesPerPixel);                    int col = sCol;                    int spos = 0;                    while (col < width) {                        if (wPixels != null) {                            switch (combinedType) {                            case COLOR | ALPHA | (8 << 3):                                wPixels[col + rowOffset] =                                        ((rowByteBuffer[spos  ] & 0xFF) << 16)                                        | ((rowByteBuffer[spos + 1] & 0xFF) << 8)                                        | ((rowByteBuffer[spos + 2] & 0xFF))                                        | ((rowByteBuffer[spos + 3] & 0xFF) << 24);                                spos += 4;                                break;                            case COLOR | ALPHA | (16 << 3):                                wPixels[col + rowOffset] =                                        ((rowByteBuffer[spos  ] & 0xFF) << 16)                                        | ((rowByteBuffer[spos + 2] & 0xFF) << 8)                                        | ((rowByteBuffer[spos + 4] & 0xFF))                                        | ((rowByteBuffer[spos + 6] & 0xFF) << 24);                                spos += 8;                                break;                            case COLOR | (8 << 3):                                wPixels[col + rowOffset] =                                        ((rowByteBuffer[spos  ] & 0xFF) << 16)                                        | ((rowByteBuffer[spos + 1] & 0xFF) << 8)                                        | ((rowByteBuffer[spos + 2] & 0xFF))                                        | ((0xFF) << 24);                                spos += 3;                                break;                            case COLOR | (16 << 3):                                wPixels[col + rowOffset] =                                        ((rowByteBuffer[spos  ] & 0xFF) << 16)                                        | ((rowByteBuffer[spos + 2] & 0xFF) << 8)                                        | ((rowByteBuffer[spos + 4] & 0xFF))                                        | ((0xFF) << 24);                                spos += 6;                                break;                            case GRAY | ALPHA | (8 << 3): {                                    int tx = rowByteBuffer[spos] & 0xFF;                                    wPixels[col + rowOffset] =                                            (tx << 16) | (tx << 8) | tx                                            | ((rowByteBuffer[spos + 1] & 0xFF) << 24);                                }                                spos += 2;                                break;                            case GRAY | ALPHA | (16 << 3): {                                    int tx = rowByteBuffer[spos] & 0xFF;                                    wPixels[col + rowOffset] =                                            (tx << 16) | (tx << 8) | tx                                            | ((rowByteBuffer[spos + 2] & 0xFF) << 24);                                }                                spos += 4;                                break;                            default:                                throw new PNGException("illegal type/depth");                            }                        } else switch (bitDepth) {                            case 1:                                bPixels[col + rowOffset] =                                        (byte) ((rowByteBuffer[spos >> 3] >> (7 - (spos & 7))) & 1);                                spos++;                                break;                            case 2:                                bPixels[col + rowOffset] =                                        (byte) ((rowByteBuffer[spos >> 2] >> ((3 - (spos & 3)) * 2)) & 3);                                spos++;                                break;                            case 4:                                bPixels[col + rowOffset] =                                        (byte) ((rowByteBuffer[spos >> 1] >> ((1 - (spos & 1)) * 4)) & 7);                                spos++;                                break;                            case 8:                                bPixels[col + rowOffset] = rowByteBuffer[spos++];                                break;                            case 16:                                bPixels[col + rowOffset] = rowByteBuffer[spos];                                spos += 2;                                break;                            default:                                throw new PNGException("illegal type/depth");                            }                            /*visit (row, col,                             min (bHeight, height - row),                             min (bWidth, width - col)); */                        col += colInc;                    }                    if (interlaceMethod == 0)                         if (wPixels != null) {                            /* code changed. target not needed here                             t.setPixels(0,row,width,1,cm,wPixels,0,width);                              */                            // code added to make it work with ImageDecoder arch                            sendPixels(0, row, width, 1, wPixels, 0, width);                             // end of adding                        } else {                            /* code changed. target not needed here                             t.setPixels(0,row,width,1,cm,bPixels,0,width);                             */                            // code added to make it work with ImageDecoder arch                            sendPixels(0, row, width, 1, bPixels, 0, width);                            //end of adding                        }                    row += rowInc;                    rowOffset += rowInc * rowStride;                    byte[] T = rowByteBuffer;                    rowByteBuffer = prevRowByteBuffer;                    prevRowByteBuffer = T;                    firstRow = false;                }                if (interlaceMethod != 0)                    if (wPixels != null) {                        /* code changed. target not needed here                         t.setPixels(0,0,width,height,cm,wPixels,0,width);                          */                        // code added to make it work with ImageDecoder arch                        sendPixels(0, 0, width, height, wPixels, 0, width);                         //end of adding                    } else {                        /* code changed. target not needed here                         t.setPixels(0,0,width,height,cm,bPixels,0,width);                         */                        // code added to make it work with ImageDecoder arch                        sendPixels(0, 0, width, height, bPixels, 0, width);                        //end of adding                    }            }            /* Here, the function "visit(row,column,height,width)" obtains the             next transmitted pixel and paints a rectangle of the specified             height and width, whose upper-left corner is at the specified row             and column, using the color indicated by the pixel.  Note that row             and column are measured from 0,0 at the upper left corner. */  	    /* code not needed, don't deal with target             if((t=target)!=null) {	       if(properties!=null) t.setProperties(properties);                 t.imageComplete(ImageConsumer.STATICIMAGEDONE);		 */            imageComplete(ImageConsumer.STATICIMAGEDONE, true);            /* code not needed }             is.close();             */        } catch (Throwable e) {            /* code not needed             if((t=target)!=null) {             PNGEncoder.prChunk(e.toString(),inbuf,pos,limit-pos,true);             */            property("error", e);            /* code not needed             t.setProperties(properties);             t.imageComplete(ImageConsumer.IMAGEERROR|ImageConsumer.STATICIMAGEDONE);             */            imageComplete(ImageConsumer.IMAGEERROR | ImageConsumer.STATICIMAGEDONE, true);            //   }            e.printStackTrace();        } finally {             try {                close();            } catch (Throwable e) {}            /* code not needed             target = null;             endTurn();             */        }    }    private boolean sendPixels(int x, int y, int w, int h, int[] pixels,                               int offset, int pixlength) {	int count = setPixels(x, y, w, h, cm,			      pixels, offset, pixlength);	if (count <= 0) {	    aborted = true;	}	return !aborted;    }    private boolean sendPixels(int x, int y, int w, int h, byte[] pixels,                               int offset, int pixlength) {	int count = setPixels(x, y, w, h, cm,			      pixels, offset, pixlength);	if (count <= 0) {	    aborted = true;	}	return !aborted;    }    private void filterRow(byte rowByteBuffer[], byte[] prevRow,                           int rowFilter, int rowByteWidth, int bytesPerSample)        throws IOException {        int x = 0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?