Search in sources :

Example 16 with IndexColorModel

use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.

the class PNGImageWriterSpi method canEncodeImage.

public boolean canEncodeImage(ImageTypeSpecifier type) {
    SampleModel sampleModel = type.getSampleModel();
    ColorModel colorModel = type.getColorModel();
    // Find the maximum bit depth across all channels
    int[] sampleSize = sampleModel.getSampleSize();
    int bitDepth = sampleSize[0];
    for (int i = 1; i < sampleSize.length; i++) {
        if (sampleSize[i] > bitDepth) {
            bitDepth = sampleSize[i];
        }
    }
    // Ensure bitDepth is between 1 and 16
    if (bitDepth < 1 || bitDepth > 16) {
        return false;
    }
    // Check number of bands, alpha
    int numBands = sampleModel.getNumBands();
    if (numBands < 1 || numBands > 4) {
        return false;
    }
    boolean hasAlpha = colorModel.hasAlpha();
    // the check below to fail and return false.
    if (colorModel instanceof IndexColorModel) {
        return true;
    }
    if ((numBands == 1 || numBands == 3) && hasAlpha) {
        return false;
    }
    if ((numBands == 2 || numBands == 4) && !hasAlpha) {
        return false;
    }
    return true;
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 17 with IndexColorModel

use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.

the class PNGMetadata method initialize.

/**
     * Sets the IHDR_bitDepth and IHDR_colorType variables.
     * The <code>numBands</code> parameter is necessary since
     * we may only be writing a subset of the image bands.
     */
public void initialize(ImageTypeSpecifier imageType, int numBands) {
    ColorModel colorModel = imageType.getColorModel();
    SampleModel sampleModel = imageType.getSampleModel();
    // Initialize IHDR_bitDepth
    int[] sampleSize = sampleModel.getSampleSize();
    int bitDepth = sampleSize[0];
    // Fixes bug 4413109
    for (int i = 1; i < sampleSize.length; i++) {
        if (sampleSize[i] > bitDepth) {
            bitDepth = sampleSize[i];
        }
    }
    // Multi-channel images must have a bit depth of 8 or 16
    if (sampleSize.length > 1 && bitDepth < 8) {
        bitDepth = 8;
    }
    // Round bit depth up to a power of 2
    if (bitDepth > 2 && bitDepth < 4) {
        bitDepth = 4;
    } else if (bitDepth > 4 && bitDepth < 8) {
        bitDepth = 8;
    } else if (bitDepth > 8 && bitDepth < 16) {
        bitDepth = 16;
    } else if (bitDepth > 16) {
        throw new RuntimeException("bitDepth > 16!");
    }
    IHDR_bitDepth = bitDepth;
    // Initialize IHDR_colorType
    if (colorModel instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) colorModel;
        int size = icm.getMapSize();
        byte[] reds = new byte[size];
        icm.getReds(reds);
        byte[] greens = new byte[size];
        icm.getGreens(greens);
        byte[] blues = new byte[size];
        icm.getBlues(blues);
        // Determine whether the color tables are actually a gray ramp
        // if the color type has not been set previously
        boolean isGray = false;
        if (!IHDR_present || (IHDR_colorType != PNGImageReader.PNG_COLOR_PALETTE)) {
            isGray = true;
            int scale = 255 / ((1 << IHDR_bitDepth) - 1);
            for (int i = 0; i < size; i++) {
                byte red = reds[i];
                if ((red != (byte) (i * scale)) || (red != greens[i]) || (red != blues[i])) {
                    isGray = false;
                    break;
                }
            }
        }
        // Determine whether transparency exists
        boolean hasAlpha = colorModel.hasAlpha();
        byte[] alpha = null;
        if (hasAlpha) {
            alpha = new byte[size];
            icm.getAlphas(alpha);
        }
        if (isGray && hasAlpha && (bitDepth == 8 || bitDepth == 16)) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
        } else if (isGray && !hasAlpha) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
        } else {
            IHDR_colorType = PNGImageReader.PNG_COLOR_PALETTE;
            PLTE_present = true;
            PLTE_order = null;
            PLTE_red = (byte[]) reds.clone();
            PLTE_green = (byte[]) greens.clone();
            PLTE_blue = (byte[]) blues.clone();
            if (hasAlpha) {
                tRNS_present = true;
                tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE;
                PLTE_order = new int[alpha.length];
                // Reorder the palette so that non-opaque entries
                // come first.  Since the tRNS chunk does not have
                // to store trailing 255's, this can save a
                // considerable amount of space when encoding
                // images with only one transparent pixel value,
                // e.g., images from GIF sources.
                byte[] newAlpha = new byte[alpha.length];
                // Scan for non-opaque entries and assign them
                // positions starting at 0.
                int newIndex = 0;
                for (int i = 0; i < alpha.length; i++) {
                    if (alpha[i] != (byte) 255) {
                        PLTE_order[i] = newIndex;
                        newAlpha[newIndex] = alpha[i];
                        ++newIndex;
                    }
                }
                int numTransparent = newIndex;
                // positions following the non-opaque entries.
                for (int i = 0; i < alpha.length; i++) {
                    if (alpha[i] == (byte) 255) {
                        PLTE_order[i] = newIndex++;
                    }
                }
                // Reorder the palettes
                byte[] oldRed = PLTE_red;
                byte[] oldGreen = PLTE_green;
                byte[] oldBlue = PLTE_blue;
                // All have the same length
                int len = oldRed.length;
                PLTE_red = new byte[len];
                PLTE_green = new byte[len];
                PLTE_blue = new byte[len];
                for (int i = 0; i < len; i++) {
                    PLTE_red[PLTE_order[i]] = oldRed[i];
                    PLTE_green[PLTE_order[i]] = oldGreen[i];
                    PLTE_blue[PLTE_order[i]] = oldBlue[i];
                }
                // Copy only the transparent entries into tRNS_alpha
                tRNS_alpha = new byte[numTransparent];
                System.arraycopy(newAlpha, 0, tRNS_alpha, 0, numTransparent);
            }
        }
    } else {
        if (numBands == 1) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
        } else if (numBands == 2) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
        } else if (numBands == 3) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_RGB;
        } else if (numBands == 4) {
            IHDR_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA;
        } else {
            throw new RuntimeException("Number of bands not 1-4!");
        }
    }
    IHDR_present = true;
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 18 with IndexColorModel

use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.

the class BMPImageWriter method write.

public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    if (stream == null) {
        throw new IllegalStateException(I18N.getString("BMPImageWriter7"));
    }
    if (image == null) {
        throw new IllegalArgumentException(I18N.getString("BMPImageWriter8"));
    }
    clearAbortRequest();
    processImageStarted(0);
    if (param == null)
        param = getDefaultWriteParam();
    BMPImageWriteParam bmpParam = (BMPImageWriteParam) param;
    // Default is using 24 bits per pixel.
    int bitsPerPixel = 24;
    boolean isPalette = false;
    int paletteEntries = 0;
    IndexColorModel icm = null;
    RenderedImage input = null;
    Raster inputRaster = null;
    boolean writeRaster = image.hasRaster();
    Rectangle sourceRegion = param.getSourceRegion();
    SampleModel sampleModel = null;
    ColorModel colorModel = null;
    compImageSize = 0;
    if (writeRaster) {
        inputRaster = image.getRaster();
        sampleModel = inputRaster.getSampleModel();
        colorModel = ImageUtil.createColorModel(null, sampleModel);
        if (sourceRegion == null)
            sourceRegion = inputRaster.getBounds();
        else
            sourceRegion = sourceRegion.intersection(inputRaster.getBounds());
    } else {
        input = image.getRenderedImage();
        sampleModel = input.getSampleModel();
        colorModel = input.getColorModel();
        Rectangle rect = new Rectangle(input.getMinX(), input.getMinY(), input.getWidth(), input.getHeight());
        if (sourceRegion == null)
            sourceRegion = rect;
        else
            sourceRegion = sourceRegion.intersection(rect);
    }
    IIOMetadata imageMetadata = image.getMetadata();
    BMPMetadata bmpImageMetadata = null;
    if (imageMetadata != null && imageMetadata instanceof BMPMetadata) {
        bmpImageMetadata = (BMPMetadata) imageMetadata;
    } else {
        ImageTypeSpecifier imageType = new ImageTypeSpecifier(colorModel, sampleModel);
        bmpImageMetadata = (BMPMetadata) getDefaultImageMetadata(imageType, param);
    }
    if (sourceRegion.isEmpty())
        throw new RuntimeException(I18N.getString("BMPImageWrite0"));
    int scaleX = param.getSourceXSubsampling();
    int scaleY = param.getSourceYSubsampling();
    int xOffset = param.getSubsamplingXOffset();
    int yOffset = param.getSubsamplingYOffset();
    // cache the data type;
    int dataType = sampleModel.getDataType();
    sourceRegion.translate(xOffset, yOffset);
    sourceRegion.width -= xOffset;
    sourceRegion.height -= yOffset;
    int minX = sourceRegion.x / scaleX;
    int minY = sourceRegion.y / scaleY;
    w = (sourceRegion.width + scaleX - 1) / scaleX;
    h = (sourceRegion.height + scaleY - 1) / scaleY;
    xOffset = sourceRegion.x % scaleX;
    yOffset = sourceRegion.y % scaleY;
    Rectangle destinationRegion = new Rectangle(minX, minY, w, h);
    boolean noTransform = destinationRegion.equals(sourceRegion);
    // Raw data can only handle bytes, everything greater must be ASCII.
    int[] sourceBands = param.getSourceBands();
    boolean noSubband = true;
    int numBands = sampleModel.getNumBands();
    if (sourceBands != null) {
        sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
        colorModel = null;
        noSubband = false;
        numBands = sampleModel.getNumBands();
    } else {
        sourceBands = new int[numBands];
        for (int i = 0; i < numBands; i++) sourceBands[i] = i;
    }
    int[] bandOffsets = null;
    boolean bgrOrder = true;
    if (sampleModel instanceof ComponentSampleModel) {
        bandOffsets = ((ComponentSampleModel) sampleModel).getBandOffsets();
        if (sampleModel instanceof BandedSampleModel) {
            // for images with BandedSampleModel we can not work
            //  with raster directly and must use writePixels()
            bgrOrder = false;
        } else {
            // In any other case we must use writePixels()
            for (int i = 0; i < bandOffsets.length; i++) {
                bgrOrder &= (bandOffsets[i] == (bandOffsets.length - i - 1));
            }
        }
    } else {
        if (sampleModel instanceof SinglePixelPackedSampleModel) {
            // BugId 4892214: we can not work with raster directly
            // if image have different color order than RGB.
            // We should use writePixels() for such images.
            int[] bitOffsets = ((SinglePixelPackedSampleModel) sampleModel).getBitOffsets();
            for (int i = 0; i < bitOffsets.length - 1; i++) {
                bgrOrder &= bitOffsets[i] > bitOffsets[i + 1];
            }
        }
    }
    if (bandOffsets == null) {
        // we will use getPixels() to extract pixel data for writePixels()
        // Please note that getPixels() provides rgb bands order.
        bandOffsets = new int[numBands];
        for (int i = 0; i < numBands; i++) bandOffsets[i] = i;
    }
    noTransform &= bgrOrder;
    int[] sampleSize = sampleModel.getSampleSize();
    //XXX: check more
    // Number of bytes that a scanline for the image written out will have.
    int destScanlineBytes = w * numBands;
    switch(bmpParam.getCompressionMode()) {
        case ImageWriteParam.MODE_EXPLICIT:
            compressionType = BMPCompressionTypes.getType(bmpParam.getCompressionType());
            break;
        case ImageWriteParam.MODE_COPY_FROM_METADATA:
            compressionType = bmpImageMetadata.compression;
            break;
        case ImageWriteParam.MODE_DEFAULT:
            compressionType = getPreferredCompressionType(colorModel, sampleModel);
            break;
        default:
            // ImageWriteParam.MODE_DISABLED:
            compressionType = BI_RGB;
    }
    if (!canEncodeImage(compressionType, colorModel, sampleModel)) {
        throw new IOException("Image can not be encoded with compression type " + BMPCompressionTypes.getName(compressionType));
    }
    byte[] r = null, g = null, b = null, a = null;
    if (compressionType == BI_BITFIELDS) {
        bitsPerPixel = DataBuffer.getDataTypeSize(sampleModel.getDataType());
        if (bitsPerPixel != 16 && bitsPerPixel != 32) {
            // we should use 32bpp images in case of BI_BITFIELD
            // compression to avoid color conversion artefacts
            bitsPerPixel = 32;
            // Setting this flag to false ensures that generic
            // writePixels() will be used to store image data
            noTransform = false;
        }
        destScanlineBytes = w * bitsPerPixel + 7 >> 3;
        isPalette = true;
        paletteEntries = 3;
        r = new byte[paletteEntries];
        g = new byte[paletteEntries];
        b = new byte[paletteEntries];
        a = new byte[paletteEntries];
        int rmask = 0x00ff0000;
        int gmask = 0x0000ff00;
        int bmask = 0x000000ff;
        if (bitsPerPixel == 16) {
            /* NB: canEncodeImage() ensures we have image of
                 * either USHORT_565_RGB or USHORT_555_RGB type here.
                 * Technically, it should work for other direct color
                 * model types but it might be non compatible with win98
                 * and friends.
                 */
            if (colorModel instanceof DirectColorModel) {
                DirectColorModel dcm = (DirectColorModel) colorModel;
                rmask = dcm.getRedMask();
                gmask = dcm.getGreenMask();
                bmask = dcm.getBlueMask();
            } else {
                // an exception related to unsupported image format
                throw new IOException("Image can not be encoded with " + "compression type " + BMPCompressionTypes.getName(compressionType));
            }
        }
        writeMaskToPalette(rmask, 0, r, g, b, a);
        writeMaskToPalette(gmask, 1, r, g, b, a);
        writeMaskToPalette(bmask, 2, r, g, b, a);
        if (!noTransform) {
            // prepare info for writePixels procedure
            bitMasks = new int[3];
            bitMasks[0] = rmask;
            bitMasks[1] = gmask;
            bitMasks[2] = bmask;
            bitPos = new int[3];
            bitPos[0] = firstLowBit(rmask);
            bitPos[1] = firstLowBit(gmask);
            bitPos[2] = firstLowBit(bmask);
        }
        if (colorModel instanceof IndexColorModel) {
            icm = (IndexColorModel) colorModel;
        }
    } else {
        // handle BI_RGB compression
        if (colorModel instanceof IndexColorModel) {
            isPalette = true;
            icm = (IndexColorModel) colorModel;
            paletteEntries = icm.getMapSize();
            if (paletteEntries <= 2) {
                bitsPerPixel = 1;
                destScanlineBytes = w + 7 >> 3;
            } else if (paletteEntries <= 16) {
                bitsPerPixel = 4;
                destScanlineBytes = w + 1 >> 1;
            } else if (paletteEntries <= 256) {
                bitsPerPixel = 8;
            } else {
                // Cannot be written as a Palette image. So write out as
                // 24 bit image.
                bitsPerPixel = 24;
                isPalette = false;
                paletteEntries = 0;
                destScanlineBytes = w * 3;
            }
            if (isPalette == true) {
                r = new byte[paletteEntries];
                g = new byte[paletteEntries];
                b = new byte[paletteEntries];
                a = new byte[paletteEntries];
                icm.getAlphas(a);
                icm.getReds(r);
                icm.getGreens(g);
                icm.getBlues(b);
            }
        } else {
            // Grey scale images
            if (numBands == 1) {
                isPalette = true;
                paletteEntries = 256;
                bitsPerPixel = sampleSize[0];
                destScanlineBytes = (w * bitsPerPixel + 7 >> 3);
                r = new byte[256];
                g = new byte[256];
                b = new byte[256];
                a = new byte[256];
                for (int i = 0; i < 256; i++) {
                    r[i] = (byte) i;
                    g[i] = (byte) i;
                    b[i] = (byte) i;
                    a[i] = (byte) 255;
                }
            } else {
                if (sampleModel instanceof SinglePixelPackedSampleModel && noSubband) {
                    /* NB: the actual pixel size can be smaller than
                         * size of used DataBuffer element.
                         * For example: in case of TYPE_INT_RGB actual pixel
                         * size is 24 bits, but size of DataBuffere element
                         * is 32 bits
                         */
                    int[] sample_sizes = sampleModel.getSampleSize();
                    bitsPerPixel = 0;
                    for (int size : sample_sizes) {
                        bitsPerPixel += size;
                    }
                    bitsPerPixel = roundBpp(bitsPerPixel);
                    if (bitsPerPixel != DataBuffer.getDataTypeSize(sampleModel.getDataType())) {
                        noTransform = false;
                    }
                    destScanlineBytes = w * bitsPerPixel + 7 >> 3;
                }
            }
        }
    }
    // actual writing of image data
    int fileSize = 0;
    int offset = 0;
    int headerSize = 0;
    int imageSize = 0;
    int xPelsPerMeter = 0;
    int yPelsPerMeter = 0;
    int colorsUsed = 0;
    int colorsImportant = paletteEntries;
    // Calculate padding for each scanline
    int padding = destScanlineBytes % 4;
    if (padding != 0) {
        padding = 4 - padding;
    }
    // FileHeader is 14 bytes, BitmapHeader is 40 bytes,
    // add palette size and that is where the data will begin
    offset = 54 + paletteEntries * 4;
    imageSize = (destScanlineBytes + padding) * h;
    fileSize = imageSize + offset;
    headerSize = 40;
    long headPos = stream.getStreamPosition();
    writeFileHeader(fileSize, offset);
    /* According to MSDN description, the top-down image layout
         * is allowed only if compression type is BI_RGB or BI_BITFIELDS.
         * Images with any other compression type must be wrote in the
         * bottom-up layout.
         */
    if (compressionType == BI_RGB || compressionType == BI_BITFIELDS) {
        isTopDown = bmpParam.isTopDown();
    } else {
        isTopDown = false;
    }
    writeInfoHeader(headerSize, bitsPerPixel);
    // compression
    stream.writeInt(compressionType);
    // imageSize
    stream.writeInt(imageSize);
    // xPelsPerMeter
    stream.writeInt(xPelsPerMeter);
    // yPelsPerMeter
    stream.writeInt(yPelsPerMeter);
    // Colors Used
    stream.writeInt(colorsUsed);
    // Colors Important
    stream.writeInt(colorsImportant);
    // palette
    if (isPalette == true) {
        // write palette
        if (compressionType == BI_BITFIELDS) {
            // write masks for red, green and blue components.
            for (int i = 0; i < 3; i++) {
                int mask = (a[i] & 0xFF) + ((r[i] & 0xFF) * 0x100) + ((g[i] & 0xFF) * 0x10000) + ((b[i] & 0xFF) * 0x1000000);
                stream.writeInt(mask);
            }
        } else {
            for (int i = 0; i < paletteEntries; i++) {
                stream.writeByte(b[i]);
                stream.writeByte(g[i]);
                stream.writeByte(r[i]);
                stream.writeByte(a[i]);
            }
        }
    }
    // Writing of actual image data
    int scanlineBytes = w * numBands;
    // Buffer for up to 8 rows of pixels
    int[] pixels = new int[scanlineBytes * scaleX];
    // Also create a buffer to hold one line of the data
    // to be written to the file, so we can use array writes.
    bpixels = new byte[destScanlineBytes];
    int l;
    if (compressionType == BI_JPEG || compressionType == BI_PNG) {
        // prepare embedded buffer
        embedded_stream = new ByteArrayOutputStream();
        writeEmbedded(image, bmpParam);
        // update the file/image Size
        embedded_stream.flush();
        imageSize = embedded_stream.size();
        long endPos = stream.getStreamPosition();
        fileSize = (int) (offset + imageSize);
        stream.seek(headPos);
        writeSize(fileSize, 2);
        stream.seek(headPos);
        writeSize(imageSize, 34);
        stream.seek(endPos);
        stream.write(embedded_stream.toByteArray());
        embedded_stream = null;
        if (abortRequested()) {
            processWriteAborted();
        } else {
            processImageComplete();
            stream.flushBefore(stream.getStreamPosition());
        }
        return;
    }
    int maxBandOffset = bandOffsets[0];
    for (int i = 1; i < bandOffsets.length; i++) if (bandOffsets[i] > maxBandOffset)
        maxBandOffset = bandOffsets[i];
    int[] pixel = new int[maxBandOffset + 1];
    int destScanlineLength = destScanlineBytes;
    if (noTransform && noSubband) {
        destScanlineLength = destScanlineBytes / (DataBuffer.getDataTypeSize(dataType) >> 3);
    }
    for (int i = 0; i < h; i++) {
        if (abortRequested()) {
            break;
        }
        int row = minY + i;
        if (!isTopDown)
            row = minY + h - i - 1;
        // Get the pixels
        Raster src = inputRaster;
        Rectangle srcRect = new Rectangle(minX * scaleX + xOffset, row * scaleY + yOffset, (w - 1) * scaleX + 1, 1);
        if (!writeRaster)
            src = input.getData(srcRect);
        if (noTransform && noSubband) {
            SampleModel sm = src.getSampleModel();
            int pos = 0;
            int startX = srcRect.x - src.getSampleModelTranslateX();
            int startY = srcRect.y - src.getSampleModelTranslateY();
            if (sm instanceof ComponentSampleModel) {
                ComponentSampleModel csm = (ComponentSampleModel) sm;
                pos = csm.getOffset(startX, startY, 0);
                for (int nb = 1; nb < csm.getNumBands(); nb++) {
                    if (pos > csm.getOffset(startX, startY, nb)) {
                        pos = csm.getOffset(startX, startY, nb);
                    }
                }
            } else if (sm instanceof MultiPixelPackedSampleModel) {
                MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sm;
                pos = mppsm.getOffset(startX, startY);
            } else if (sm instanceof SinglePixelPackedSampleModel) {
                SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sm;
                pos = sppsm.getOffset(startX, startY);
            }
            if (compressionType == BI_RGB || compressionType == BI_BITFIELDS) {
                switch(dataType) {
                    case DataBuffer.TYPE_BYTE:
                        byte[] bdata = ((DataBufferByte) src.getDataBuffer()).getData();
                        stream.write(bdata, pos, destScanlineLength);
                        break;
                    case DataBuffer.TYPE_SHORT:
                        short[] sdata = ((DataBufferShort) src.getDataBuffer()).getData();
                        stream.writeShorts(sdata, pos, destScanlineLength);
                        break;
                    case DataBuffer.TYPE_USHORT:
                        short[] usdata = ((DataBufferUShort) src.getDataBuffer()).getData();
                        stream.writeShorts(usdata, pos, destScanlineLength);
                        break;
                    case DataBuffer.TYPE_INT:
                        int[] idata = ((DataBufferInt) src.getDataBuffer()).getData();
                        stream.writeInts(idata, pos, destScanlineLength);
                        break;
                }
                for (int k = 0; k < padding; k++) {
                    stream.writeByte(0);
                }
            } else if (compressionType == BI_RLE4) {
                if (bpixels == null || bpixels.length < scanlineBytes)
                    bpixels = new byte[scanlineBytes];
                src.getPixels(srcRect.x, srcRect.y, srcRect.width, srcRect.height, pixels);
                for (int h = 0; h < scanlineBytes; h++) {
                    bpixels[h] = (byte) pixels[h];
                }
                encodeRLE4(bpixels, scanlineBytes);
            } else if (compressionType == BI_RLE8) {
                //System.arraycopy(bdata, pos, bpixels, 0, scanlineBytes);
                if (bpixels == null || bpixels.length < scanlineBytes)
                    bpixels = new byte[scanlineBytes];
                src.getPixels(srcRect.x, srcRect.y, srcRect.width, srcRect.height, pixels);
                for (int h = 0; h < scanlineBytes; h++) {
                    bpixels[h] = (byte) pixels[h];
                }
                encodeRLE8(bpixels, scanlineBytes);
            }
        } else {
            src.getPixels(srcRect.x, srcRect.y, srcRect.width, srcRect.height, pixels);
            if (scaleX != 1 || maxBandOffset != numBands - 1) {
                for (int j = 0, k = 0, n = 0; j < w; j++, k += scaleX * numBands, n += numBands) {
                    System.arraycopy(pixels, k, pixel, 0, pixel.length);
                    for (int m = 0; m < numBands; m++) {
                        // pixel data is provided here in RGB order
                        pixels[n + m] = pixel[sourceBands[m]];
                    }
                }
            }
            writePixels(0, scanlineBytes, bitsPerPixel, pixels, padding, numBands, icm);
        }
        processImageProgress(100.0f * (((float) i) / ((float) h)));
    }
    if (compressionType == BI_RLE4 || compressionType == BI_RLE8) {
        // Write the RLE EOF marker and
        stream.writeByte(0);
        stream.writeByte(1);
        incCompImageSize(2);
        // update the file/image Size
        imageSize = compImageSize;
        fileSize = compImageSize + offset;
        long endPos = stream.getStreamPosition();
        stream.seek(headPos);
        writeSize(fileSize, 2);
        stream.seek(headPos);
        writeSize(imageSize, 34);
        stream.seek(endPos);
    }
    if (abortRequested()) {
        processWriteAborted();
    } else {
        processImageComplete();
        stream.flushBefore(stream.getStreamPosition());
    }
}
Also used : Rectangle(java.awt.Rectangle) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) DataBufferShort(java.awt.image.DataBufferShort) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) BandedSampleModel(java.awt.image.BandedSampleModel) DirectColorModel(java.awt.image.DirectColorModel) DataBufferUShort(java.awt.image.DataBufferUShort) IndexColorModel(java.awt.image.IndexColorModel) Raster(java.awt.image.Raster) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) BMPImageWriteParam(javax.imageio.plugins.bmp.BMPImageWriteParam) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) RenderedImage(java.awt.image.RenderedImage)

Example 19 with IndexColorModel

use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.

the class ImageUtil method createColorModel.

/* XXX testing only
    public static void main(String[] args) {
        ImageTypeSpecifier bilevel =
            ImageTypeSpecifier.createIndexed(new byte[] {(byte)0, (byte)255},
                                             new byte[] {(byte)0, (byte)255},
                                             new byte[] {(byte)0, (byte)255},
                                             null, 1,
                                             DataBuffer.TYPE_BYTE);
        ImageTypeSpecifier gray =
            ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false);
        ImageTypeSpecifier grayAlpha =
            ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false,
                                               false);
        ImageTypeSpecifier rgb =
            ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                                 new int[] {0, 1, 2},
                                                 DataBuffer.TYPE_BYTE,
                                                 false,
                                                 false);
        ImageTypeSpecifier rgba =
            ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                                 new int[] {0, 1, 2, 3},
                                                 DataBuffer.TYPE_BYTE,
                                                 true,
                                                 false);
        ImageTypeSpecifier packed =
            ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                            0xff000000,
                                            0x00ff0000,
                                            0x0000ff00,
                                            0x000000ff,
                                            DataBuffer.TYPE_BYTE,
                                            false);

        SampleModel bandedSM =
            new java.awt.image.BandedSampleModel(DataBuffer.TYPE_BYTE,
                                                 1, 1, 15);

        System.out.println(createColorModel(bilevel.getSampleModel()));
        System.out.println(createColorModel(gray.getSampleModel()));
        System.out.println(createColorModel(grayAlpha.getSampleModel()));
        System.out.println(createColorModel(rgb.getSampleModel()));
        System.out.println(createColorModel(rgba.getSampleModel()));
        System.out.println(createColorModel(packed.getSampleModel()));
        System.out.println(createColorModel(bandedSM));
    }
    */
/**
     * Creates a <code>ColorModel</code> that may be used with the
     * specified <code>SampleModel</code>.  If a suitable
     * <code>ColorModel</code> cannot be found, this method returns
     * <code>null</code>.
     *
     * <p> Suitable <code>ColorModel</code>s are guaranteed to exist
     * for all instances of <code>ComponentSampleModel</code>.
     * For 1- and 3- banded <code>SampleModel</code>s, the returned
     * <code>ColorModel</code> will be opaque.  For 2- and 4-banded
     * <code>SampleModel</code>s, the output will use alpha transparency
     * which is not premultiplied.  1- and 2-banded data will use a
     * grayscale <code>ColorSpace</code>, and 3- and 4-banded data a sRGB
     * <code>ColorSpace</code>. Data with 5 or more bands will have a
     * <code>BogusColorSpace</code>.</p>
     *
     * <p>An instance of <code>DirectColorModel</code> will be created for
     * instances of <code>SinglePixelPackedSampleModel</code> with no more
     * than 4 bands.</p>
     *
     * <p>An instance of <code>IndexColorModel</code> will be created for
     * instances of <code>MultiPixelPackedSampleModel</code>. The colormap
     * will be a grayscale ramp with <code>1&nbsp;<<&nbsp;numberOfBits</code>
     * entries ranging from zero to at most 255.</p>
     *
     * @return An instance of <code>ColorModel</code> that is suitable for
     *         the supplied <code>SampleModel</code>, or <code>null</code>.
     *
     * @throws IllegalArgumentException  If <code>sampleModel</code> is
     *         <code>null</code>.
     */
public static final ColorModel createColorModel(SampleModel sampleModel) {
    // Check the parameter.
    if (sampleModel == null) {
        throw new IllegalArgumentException("sampleModel == null!");
    }
    // Get the data type.
    int dataType = sampleModel.getDataType();
    // Check the data type
    switch(dataType) {
        case DataBuffer.TYPE_BYTE:
        case DataBuffer.TYPE_USHORT:
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_INT:
        case DataBuffer.TYPE_FLOAT:
        case DataBuffer.TYPE_DOUBLE:
            break;
        default:
            // Return null for other types.
            return null;
    }
    // The return variable.
    ColorModel colorModel = null;
    // Get the sample size.
    int[] sampleSize = sampleModel.getSampleSize();
    // Create a Component ColorModel.
    if (sampleModel instanceof ComponentSampleModel) {
        // Get the number of bands.
        int numBands = sampleModel.getNumBands();
        // Determine the color space.
        ColorSpace colorSpace = null;
        if (numBands <= 2) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        } else if (numBands <= 4) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        } else {
            colorSpace = new BogusColorSpace(numBands);
        }
        boolean hasAlpha = (numBands == 2) || (numBands == 4);
        boolean isAlphaPremultiplied = false;
        int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
        colorModel = new ComponentColorModel(colorSpace, sampleSize, hasAlpha, isAlphaPremultiplied, transparency, dataType);
    } else if (sampleModel.getNumBands() <= 4 && sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        int[] bitMasks = sppsm.getBitMasks();
        int rmask = 0;
        int gmask = 0;
        int bmask = 0;
        int amask = 0;
        int numBands = bitMasks.length;
        if (numBands <= 2) {
            rmask = gmask = bmask = bitMasks[0];
            if (numBands == 2) {
                amask = bitMasks[1];
            }
        } else {
            rmask = bitMasks[0];
            gmask = bitMasks[1];
            bmask = bitMasks[2];
            if (numBands == 4) {
                amask = bitMasks[3];
            }
        }
        int bits = 0;
        for (int i = 0; i < sampleSize.length; i++) {
            bits += sampleSize[i];
        }
        return new DirectColorModel(bits, rmask, gmask, bmask, amask);
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        // Load the colormap with a ramp.
        int bitsPerSample = sampleSize[0];
        int numEntries = 1 << bitsPerSample;
        byte[] map = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            map[i] = (byte) (i * 255 / (numEntries - 1));
        }
        colorModel = new IndexColorModel(bitsPerSample, numEntries, map, map, map);
    }
    return colorModel;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) Point(java.awt.Point) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 20 with IndexColorModel

use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.

the class ImageUtil method createColorModel.

public static ColorModel createColorModel(ColorSpace colorSpace, SampleModel sampleModel) {
    ColorModel colorModel = null;
    if (sampleModel == null) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil1"));
    }
    int numBands = sampleModel.getNumBands();
    if (numBands < 1 || numBands > 4) {
        return null;
    }
    int dataType = sampleModel.getDataType();
    if (sampleModel instanceof ComponentSampleModel) {
        if (dataType < DataBuffer.TYPE_BYTE || //dataType == DataBuffer.TYPE_SHORT ||
        dataType > DataBuffer.TYPE_DOUBLE) {
            return null;
        }
        if (colorSpace == null)
            colorSpace = numBands <= 2 ? ColorSpace.getInstance(ColorSpace.CS_GRAY) : ColorSpace.getInstance(ColorSpace.CS_sRGB);
        boolean useAlpha = (numBands == 2) || (numBands == 4);
        int transparency = useAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
        boolean premultiplied = false;
        int dataTypeSize = DataBuffer.getDataTypeSize(dataType);
        int[] bits = new int[numBands];
        for (int i = 0; i < numBands; i++) {
            bits[i] = dataTypeSize;
        }
        colorModel = new ComponentColorModel(colorSpace, bits, useAlpha, premultiplied, transparency, dataType);
    } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        int[] bitMasks = sppsm.getBitMasks();
        int rmask = 0;
        int gmask = 0;
        int bmask = 0;
        int amask = 0;
        numBands = bitMasks.length;
        if (numBands <= 2) {
            rmask = gmask = bmask = bitMasks[0];
            if (numBands == 2) {
                amask = bitMasks[1];
            }
        } else {
            rmask = bitMasks[0];
            gmask = bitMasks[1];
            bmask = bitMasks[2];
            if (numBands == 4) {
                amask = bitMasks[3];
            }
        }
        int[] sampleSize = sppsm.getSampleSize();
        int bits = 0;
        for (int i = 0; i < sampleSize.length; i++) {
            bits += sampleSize[i];
        }
        if (colorSpace == null)
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        colorModel = new DirectColorModel(colorSpace, bits, rmask, gmask, bmask, amask, false, sampleModel.getDataType());
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        int bits = ((MultiPixelPackedSampleModel) sampleModel).getPixelBitStride();
        int size = 1 << bits;
        byte[] comp = new byte[size];
        for (int i = 0; i < size; i++) comp[i] = (byte) (255 * i / (size - 1));
        colorModel = new IndexColorModel(bits, size, comp, comp, comp);
    }
    return colorModel;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) DirectColorModel(java.awt.image.DirectColorModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) Point(java.awt.Point) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

IndexColorModel (java.awt.image.IndexColorModel)57 ColorModel (java.awt.image.ColorModel)24 BufferedImage (java.awt.image.BufferedImage)19 DirectColorModel (java.awt.image.DirectColorModel)11 SampleModel (java.awt.image.SampleModel)10 Point (java.awt.Point)8 WritableRaster (java.awt.image.WritableRaster)8 Rectangle (java.awt.Rectangle)7 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 Raster (java.awt.image.Raster)7 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 ColorSpace (java.awt.color.ColorSpace)5 ComponentColorModel (java.awt.image.ComponentColorModel)5 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)5 Graphics (java.awt.Graphics)4 Graphics2D (java.awt.Graphics2D)4 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)4 RenderedImage (java.awt.image.RenderedImage)4 ByteComponentRaster (sun.awt.image.ByteComponentRaster)4 DataBufferByte (java.awt.image.DataBufferByte)3