⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmpimagewriter.java

📁 JAVA的一些源码 JAVA2 STANDARD EDITION DEVELOPMENT KIT 5.0
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            processWriteAborted();        } else {            processImageComplete();            stream.flushBefore(stream.getStreamPosition());        }    }    private void writePixels(int l, int scanlineBytes, int bitsPerPixel,                             int pixels[],                             int padding, int numBands,                             IndexColorModel icm) throws IOException {        int pixel = 0;        int k = 0;        switch (bitsPerPixel) {        case 1:            for (int j=0; j<scanlineBytes/8; j++) {                bpixels[k++] = (byte)((pixels[l++]  << 7) |                                      (pixels[l++]  << 6) |                                      (pixels[l++]  << 5) |                                      (pixels[l++]  << 4) |                                      (pixels[l++]  << 3) |                                      (pixels[l++]  << 2) |                                      (pixels[l++]  << 1) |                                      pixels[l++]);            }            // Partially filled last byte, if any            if (scanlineBytes%8 > 0) {                pixel = 0;                for (int j=0; j<scanlineBytes%8; j++) {                    pixel |= (pixels[l++] << (7 - j));                }                bpixels[k++] = (byte)pixel;            }            stream.write(bpixels, 0, (scanlineBytes+7)/8);            break;        case 4:            if (compressionType == BMPConstants.BI_RLE4){                byte[] bipixels = new byte[scanlineBytes];                for (int h=0; h<scanlineBytes; h++) {                    bipixels[h] = (byte)pixels[l++];                }                encodeRLE4(bipixels, scanlineBytes);            }else {                for (int j=0; j<scanlineBytes/2; j++) {                    pixel = (pixels[l++] << 4) | pixels[l++];                    bpixels[k++] = (byte)pixel;                }                // Put the last pixel of odd-length lines in the 4 MSBs                if ((scanlineBytes%2) == 1) {                    pixel = pixels[l] << 4;                    bpixels[k++] = (byte)pixel;                }                stream.write(bpixels, 0, (scanlineBytes+1)/2);            }            break;        case 8:            if(compressionType == BMPConstants.BI_RLE8) {                for (int h=0; h<scanlineBytes; h++) {                    bpixels[h] = (byte)pixels[l++];                }                encodeRLE8(bpixels, scanlineBytes);            }else {                for (int j=0; j<scanlineBytes; j++) {                    bpixels[j] = (byte)pixels[l++];                }                stream.write(bpixels, 0, scanlineBytes);            }            break;        case 16:            if (spixels == null)                spixels = new short[scanlineBytes / numBands];            for (int j = 0, m = 0; j < scanlineBytes; m++) {                spixels[m] = 0;                for(int i = numBands -1 ; i >= 0; i--, j++)                    spixels[m] |= pixels[j] << bitPos[i];            }            stream.writeShorts(spixels, 0, spixels.length);            break;        case 24:            if (numBands == 3) {                for (int j=0; j<scanlineBytes; j+=3) {                    // Since BMP needs BGR format                    bpixels[k++] = (byte)(pixels[l+2]);                    bpixels[k++] = (byte)(pixels[l+1]);                    bpixels[k++] = (byte)(pixels[l]);                    l+=3;                }                stream.write(bpixels, 0, scanlineBytes);            } else {                // Case where IndexColorModel had > 256 colors.                int entries = icm.getMapSize();                byte r[] = new byte[entries];                byte g[] = new byte[entries];                byte b[] = new byte[entries];                icm.getReds(r);                icm.getGreens(g);                icm.getBlues(b);                int index;                for (int j=0; j<scanlineBytes; j++) {                    index = pixels[l];                    bpixels[k++] = b[index];                    bpixels[k++] = g[index];                    bpixels[k++] = b[index];                    l++;                }                stream.write(bpixels, 0, scanlineBytes*3);            }            break;        case 32:            if (ipixels == null)                ipixels = new int[scanlineBytes / numBands];            for (int j = 0, m = 0; j < scanlineBytes; m++) {                ipixels[m] = 0;                for(int i = numBands -1 ; i >= 0; i--, j++)                    ipixels[m] |= pixels[j] << bitPos[i];            }            stream.writeInts(ipixels, 0, ipixels.length);            break;        }        // Write out the padding        if (compressionType == BMPConstants.BI_RGB){            for(k=0; k<padding; k++) {                stream.writeByte(0);            }        }    }    private void encodeRLE8(byte[] bpixels, int scanlineBytes)      throws IOException{        int runCount = 1, absVal = -1, j = -1;        byte runVal = 0, nextVal =0 ;        runVal = bpixels[++j];        byte[] absBuf = new byte[256];        while (j < scanlineBytes-1) {            nextVal = bpixels[++j];            if (nextVal == runVal ){                if(absVal >= 3 ){                    /// Check if there was an existing Absolute Run                    stream.writeByte(0);                    stream.writeByte(absVal);                    incCompImageSize(2);                    for(int a=0; a<absVal;a++){                        stream.writeByte(absBuf[a]);                        incCompImageSize(1);                    }                    if (!isEven(absVal)){                        //Padding                        stream.writeByte(0);                        incCompImageSize(1);                    }                }                else if(absVal > -1){                    /// Absolute Encoding for less than 3                    /// treated as regular encoding                    /// Do not include the last element since it will                    /// be inclued in the next encoding/run                    for (int b=0;b<absVal;b++){                        stream.writeByte(1);                        stream.writeByte(absBuf[b]);                        incCompImageSize(2);                    }                }                absVal = -1;                runCount++;                if (runCount == 256){                    /// Only 255 values permitted                    stream.writeByte(runCount-1);                    stream.writeByte(runVal);                    incCompImageSize(2);                    runCount = 1;                }            }            else {                if (runCount > 1){                    /// If there was an existing run                    stream.writeByte(runCount);                    stream.writeByte(runVal);                    incCompImageSize(2);                } else if (absVal < 0){                    // First time..                    absBuf[++absVal] = runVal;                    absBuf[++absVal] = nextVal;                } else if (absVal < 254){                    //  0-254 only                    absBuf[++absVal] = nextVal;                } else {                    stream.writeByte(0);                    stream.writeByte(absVal+1);                    incCompImageSize(2);                    for(int a=0; a<=absVal;a++){                        stream.writeByte(absBuf[a]);                        incCompImageSize(1);                    }                    // padding since 255 elts is not even                    stream.writeByte(0);                    incCompImageSize(1);                    absVal = -1;                }                runVal = nextVal;                runCount = 1;            }            if (j == scanlineBytes-1){ // EOF scanline                // Write the run                if (absVal == -1){                    stream.writeByte(runCount);                    stream.writeByte(runVal);                    incCompImageSize(2);                    runCount = 1;                }                else {                    // write the Absolute Run                    if(absVal >= 2){                        stream.writeByte(0);                        stream.writeByte(absVal+1);                        incCompImageSize(2);                        for(int a=0; a<=absVal;a++){                            stream.writeByte(absBuf[a]);                            incCompImageSize(1);                        }                        if (!isEven(absVal+1)){                            //Padding                            stream.writeByte(0);                            incCompImageSize(1);                        }                    }                    else if(absVal > -1){                        for (int b=0;b<=absVal;b++){                            stream.writeByte(1);                            stream.writeByte(absBuf[b]);                            incCompImageSize(2);                        }                    }                }                /// EOF scanline                stream.writeByte(0);                stream.writeByte(0);                incCompImageSize(2);            }        }    }    private void encodeRLE4(byte[] bipixels, int scanlineBytes)      throws IOException {        int runCount=2, absVal=-1, j=-1, pixel=0, q=0;        byte runVal1=0, runVal2=0, nextVal1=0, nextVal2=0;        byte[] absBuf = new byte[256];        runVal1 = bipixels[++j];        runVal2 = bipixels[++j];        while (j < scanlineBytes-2){            nextVal1 = bipixels[++j];            nextVal2 = bipixels[++j];            if (nextVal1 == runVal1 ) {                //Check if there was an existing Absolute Run                if(absVal >= 4){                    stream.writeByte(0);                    stream.writeByte(absVal - 1);                    incCompImageSize(2);                    // we need to exclude  last 2 elts, similarity of                    // which caused to enter this part of the code                    for(int a=0; a<absVal-2;a+=2){                        pixel = (absBuf[a] << 4) | absBuf[a+1];                        stream.writeByte((byte)pixel);                        incCompImageSize(1);                    }                    // if # of elts is odd - read the last element                    if(!(isEven(absVal-1))){                        q = absBuf[absVal-2] << 4| 0;                        stream.writeByte(q);                        incCompImageSize(1);                    }                    // Padding to word align absolute encoding                    if ( !isEven((int)Math.ceil((absVal-1)/2)) ) {                        stream.writeByte(0);                        incCompImageSize(1);                    }                } else if (absVal > -1){                    stream.writeByte(2);                    pixel = (absBuf[0] << 4) | absBuf[1];                    stream.writeByte(pixel);                    incCompImageSize(2);                }                absVal = -1;                if (nextVal2 == runVal2){                    // Even runlength                    runCount+=2;                    if(runCount == 256){                        stream.writeByte(runCount-1);                        pixel = ( runVal1 << 4) | runVal2;                        stream.writeByte(pixel);                        incCompImageSize(2);                        runCount =2;                        if(j< scanlineBytes - 1){                            runVal1 = runVal2;                            runVal2 = bipixels[++j];                        } else {                            stream.writeByte(01);                            int r = runVal2 << 4 | 0;                            stream.writeByte(r);                            incCompImageSize(2);                            runCount = -1;/// Only EOF required now                        }                    }

⌨️ 快捷键说明

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