Example usage for java.awt AWTException AWTException

List of usage examples for java.awt AWTException AWTException

Introduction

In this page you can find the example usage for java.awt AWTException AWTException.

Prototype

public AWTException(String msg) 

Source Link

Document

Constructs an instance of AWTException with the specified detail message.

Usage

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Constructing a GIF encoder./*from  www  .jav  a  2  s.c  om*/
 *
 * @param image  The image to encode. The image must be
 *               completely loaded.
 * @throws AWTException  If memory is exhausted or image contains
 *                       more than 256 colors.
 */
public GifEncoder(Image image) throws AWTException {
    imageWidth_ = (short) image.getWidth(null);
    imageHeight_ = (short) image.getHeight(null);

    int values[] = new int[imageWidth_ * imageHeight_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, imageWidth_, imageHeight_, values, 0, imageWidth_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException exception) {
    }

    byte[][] r = new byte[imageWidth_][imageHeight_];
    byte[][] g = new byte[imageWidth_][imageHeight_];
    byte[][] b = new byte[imageWidth_][imageHeight_];

    int index = 0;

    for (int y = 0; y < imageHeight_; y++) {
        for (int x = 0; x < imageWidth_; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index] >> 0) & 0xFF);
        }
    }

    toIndexColor(r, g, b);
}

From source file:GIFEncoder.java

/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 * /*from w  ww .  ja va  2s.c o  m*/
 * @param image The image to encode. The image <B>must</B> be
 * completely loaded.
 * @exception AWTException Will be thrown if the pixel grab fails. This
 * can happen if Java runs out of memory. It may also indicate that the image
 * contains more than 256 colors.
 * */
public GIFEncoder(Image image) throws AWTException {
    width_ = (short) image.getWidth(null);
    height_ = (short) image.getHeight(null);

    int values[] = new int[width_ * height_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException e) {
        ;
    }

    byte r[][] = new byte[width_][height_];
    byte g[][] = new byte[width_][height_];
    byte b[][] = new byte[width_][height_];
    int index = 0;
    for (int y = 0; y < height_; ++y)
        for (int x = 0; x < width_; ++x) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index]) & 0xFF);
            ++index;
        }
    ToIndexedColor(r, g, b);
}

From source file:com.greenline.guahao.biz.manager.image.codes.gif.GifEncoder.java

/**
 * Convenience constructor for class <CODE>GIFEncoder</CODE>. The argument
 * will be converted to an indexed color array. <B>This may take some
 * time.</B>/*from w  w  w.  j  av a  2 s  .  c o m*/
 * 
 * @param image The image to encode. The image <B>must</B> be completely
 *            loaded.
 * @exception AWTException Will be thrown if the pixel grab fails. This can
 *                happen if Java runs out of memory. It may also indicate
 *                that the image contains more than 256 colors.
 */
public GifEncoder(Image image) throws AWTException {
    this.imageWidth = (short) image.getWidth(null);
    this.imageHeight = (short) image.getHeight(null);

    int values[] = new int[this.imageWidth * this.imageHeight];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, this.imageWidth, this.imageHeight, values, 0,
            this.imageWidth);

    try {
        if (grabber.grabPixels() != true) {
            log.error("GifEncoder#GifEncoder Grabber returned false: " + grabber.status());
            throw new AWTException("Grabber returned false: " + grabber.status());
        }
    } // ends try
    catch (InterruptedException ie) {
        log.error("GifEncoder#GifEncoder " + ie.getMessage(), ie);
    }

    byte[][] r = new byte[this.imageWidth][this.imageHeight];
    byte[][] g = new byte[this.imageWidth][this.imageHeight];
    byte[][] b = new byte[this.imageWidth][this.imageHeight];
    int index = 0;

    for (int y = 0; y < this.imageHeight; y++) {
        for (int x = 0; x < this.imageWidth; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index]) & 0xFF);
        } // ends for

    } // ends for

    this.toIndexColor(r, g, b);
}

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Converts rgb desrcription of image to colour
 * number description used by GIF./*from  w  ww  .ja v a  2s  .co m*/
 *
 * @param r  Red array of pixels.
 * @param g  Green array of pixels.
 * @param b  Blue array of pixels.
 * @throws   AWTException
 *           Thrown if too many different colours in image.
 */
private void toIndexColor(byte[][] r, byte[][] g, byte[][] b) throws AWTException {
    pixels_ = new byte[imageWidth_ * imageHeight_];
    colors_ = new byte[256 * 3];
    int colornum = 0;

    for (int x = 0; x < imageWidth_; x++) {
        for (int y = 0; y < imageHeight_; y++) {
            int search;
            for (search = 0; search < colornum; search++) {
                if (colors_[search * 3 + 0] == r[x][y] && colors_[search * 3 + 1] == g[x][y]
                        && colors_[search * 3 + 2] == b[x][y]) {
                    break;
                }
            }

            if (search > 255)
                throw new AWTException("Too many colors.");

            // Row major order y=row x=col
            pixels_[y * imageWidth_ + x] = (byte) search;

            if (search == colornum) {
                colors_[search * 3 + 0] = r[x][y]; // [col][row]
                colors_[search * 3 + 1] = g[x][y];
                colors_[search * 3 + 2] = b[x][y];
                colornum++;
            }
        }
    }

    nColors_ = 1 << bitsNeeded(colornum);
    byte copy[] = new byte[nColors_ * 3];
    System.arraycopy(colors_, 0, copy, 0, nColors_ * 3);

    colors_ = copy;
}

From source file:GIFEncoder.java

void ToIndexedColor(byte r[][], byte g[][], byte b[][]) throws AWTException {
    pixels_ = new byte[width_ * height_];
    colors_ = new byte[256 * 3];
    int colornum = 0;
    for (int x = 0; x < width_; ++x) {
        for (int y = 0; y < height_; ++y) {
            int search;
            for (search = 0; search < colornum; ++search)
                if (colors_[search * 3] == r[x][y] && colors_[search * 3 + 1] == g[x][y]
                        && colors_[search * 3 + 2] == b[x][y])
                    break;

            if (search > 255)
                throw new AWTException("Too many colors.");

            pixels_[y * width_ + x] = (byte) search;

            if (search == colornum) {
                colors_[search * 3] = r[x][y];
                colors_[search * 3 + 1] = g[x][y];
                colors_[search * 3 + 2] = b[x][y];
                ++colornum;// w  w  w  . j a v  a2 s. com
            }
        }
    }
    numColors_ = 1 << BitUtils.BitsNeeded(colornum);
    byte copy[] = new byte[numColors_ * 3];
    System.arraycopy(colors_, 0, copy, 0, numColors_ * 3);
    colors_ = copy;
}

From source file:com.greenline.guahao.biz.manager.image.codes.gif.GifEncoder.java

/**
 * Converts rgb desrcription of image to colour number description used by
 * GIF./*from   w w  w .j  ava  2  s. c o m*/
 * 
 * @param r red array of pixels
 * @param g green array of pixels
 * @param b blue array of pixels
 * @exception AWTException Thrown if too many different colours in image.
 */
void toIndexColor(byte[][] r, byte[][] g, byte[][] b) throws AWTException {
    this.allPixels = new byte[this.imageWidth * this.imageHeight];
    this.allColors = new byte[256 * 3];
    int colornum = 0;
    for (int x = 0; x < this.imageWidth; x++) {
        for (int y = 0; y < this.imageHeight; y++) {
            int search;
            for (search = 0; search < colornum; search++) {
                if (this.allColors[search * 3] == r[x][y] && this.allColors[search * 3 + 1] == g[x][y]
                        && this.allColors[search * 3 + 2] == b[x][y]) {
                    break;
                } // ends if

            } // ends for

            if (search > 255)
                throw new AWTException("Too many colors.");
            // row major order y=row x=col
            this.allPixels[y * this.imageWidth + x] = (byte) search;

            if (search == colornum) {
                this.allColors[search * 3] = r[x][y]; // [col][row]
                this.allColors[search * 3 + 1] = g[x][y];
                this.allColors[search * 3 + 2] = b[x][y];
                colornum++;
            } // ends if

        } // ends for

    } // ends for

    this.numberOfColors = 1 << BitUtils.BitsNeeded(colornum);
    byte copy[] = new byte[this.numberOfColors * 3];
    System.arraycopy(this.allColors, 0, copy, 0, this.numberOfColors * 3);
    this.allColors = copy;
}

From source file:Jpeg.java

private void getYCCArray() {
    int values[] = new int[imageWidth * imageHeight];
    int r, g, b, y, x;
    // In order to minimize the chance that grabPixels will throw an exception
    // it may be necessary to grab some pixels every few scanlines and process
    // those before going for more. The time expense may be prohibitive.
    // However, for a situation where memory overhead is a concern, this may be
    // the only choice.
    PixelGrabber grabber = new PixelGrabber(imageobj.getSource(), 0, 0, imageWidth, imageHeight, values, 0,
            imageWidth);/*from   w  ww . j a  v a  2 s .  com*/
    MaxHsampFactor = 1;
    MaxVsampFactor = 1;
    for (y = 0; y < NumberOfComponents; y++) {
        MaxHsampFactor = Math.max(MaxHsampFactor, HsampFactor[y]);
        MaxVsampFactor = Math.max(MaxVsampFactor, VsampFactor[y]);
    }
    for (y = 0; y < NumberOfComponents; y++) {
        compWidth[y] = (((imageWidth % 8 != 0) ? ((int) Math.ceil(imageWidth / 8.0)) * 8 : imageWidth)
                / MaxHsampFactor) * HsampFactor[y];
        if (compWidth[y] != ((imageWidth / MaxHsampFactor) * HsampFactor[y])) {
            lastColumnIsDummy[y] = true;
        }
        // results in a multiple of 8 for compWidth
        // this will make the rest of the program fail for the unlikely
        // event that someone tries to compress an 16 x 16 pixel image
        // which would of course be worse than pointless
        BlockWidth[y] = (int) Math.ceil(compWidth[y] / 8.0);
        compHeight[y] = (((imageHeight % 8 != 0) ? ((int) Math.ceil(imageHeight / 8.0)) * 8 : imageHeight)
                / MaxVsampFactor) * VsampFactor[y];
        if (compHeight[y] != ((imageHeight / MaxVsampFactor) * VsampFactor[y])) {
            lastRowIsDummy[y] = true;
        }
        BlockHeight[y] = (int) Math.ceil(compHeight[y] / 8.0);
    }
    try {
        if (grabber.grabPixels() != true) {
            try {
                throw new AWTException("Grabber returned false: " + grabber.status());
            } catch (Exception e) {
            }
        }
    } catch (InterruptedException e) {
    }
    float Y[][] = new float[compHeight[0]][compWidth[0]];
    float Cr1[][] = new float[compHeight[0]][compWidth[0]];
    float Cb1[][] = new float[compHeight[0]][compWidth[0]];
    // float Cb2[][] = new float[compHeight[1]][compWidth[1]];
    // float Cr2[][] = new float[compHeight[2]][compWidth[2]];
    int index = 0;
    for (y = 0; y < imageHeight; ++y) {
        for (x = 0; x < imageWidth; ++x) {
            r = ((values[index] >> 16) & 0xff);
            g = ((values[index] >> 8) & 0xff);
            b = (values[index] & 0xff);

            // The following three lines are a more correct color conversion but
            // the current conversion technique is sufficient and results in a
            // higher
            // compression rate.
            // Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g +
            // 0.114 * (float)b ));
            // Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 *
            // (float)g + 0.5 * (float)b));
            // Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g
            // - 0.08131 * (float)b));
            Y[y][x] = (float) ((0.299 * r + 0.587 * g + 0.114 * b));
            Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b));
            Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * b));
            index++;
        }
    }

    // Need a way to set the H and V sample factors before allowing
    // downsampling.
    // For now (04/04/98) downsampling must be hard coded.
    // Until a better downsampler is implemented, this will not be done.
    // Downsampling is currently supported. The downsampling method here
    // is a simple box filter.

    Components[0] = Y;
    // Cb2 = DownSample(Cb1, 1);
    Components[1] = Cb1;
    // Cr2 = DownSample(Cr1, 2);
    Components[2] = Cr1;
}