/**
   * Construct a GIFEncoder. The constructor will convert the image to an indexed color array.
   * <B>This may take some time.</B>
   *
   * <p>
   *
   * @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);
  }
Exemple #2
0
  /*
   * This method creates and fills three arrays, Y, Cb, and Cr using the
   * input image.
   */
  private void getYCCArray() {
    int[] values = new int[imageWidth * imageHeight];
    int r;
    int g;
    int b;
    int y;
    int 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);
    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((double) 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((double) compWidth[y] / 8.0);
      compHeight[y] =
          ((((imageHeight % 8) != 0)
                      ? (((int) Math.ceil((double) imageHeight / 8.0)) * 8)
                      : imageHeight)
                  / MaxVsampFactor)
              * VsampFactor[y];

      if (compHeight[y] != ((imageHeight / MaxVsampFactor) * VsampFactor[y])) {
        lastRowIsDummy[y] = true;
      }

      BlockHeight[y] = (int) Math.ceil((double) compHeight[y] / 8.0);
    }

    try {
      if (grabber.grabPixels() != true) {
        try {
          throw new AWTException("Grabber returned false: " + grabber.status());
        } catch (Exception e) {
          String2.log(MustBe.throwableToString(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 * (float) r) + (0.587 * (float) g) + (0.114 * (float) b)));
        Cb1[y][x] =
            128 + (float) (((-0.16874 * (float) r) - (0.33126 * (float) g) + (0.5 * (float) b)));
        Cr1[y][x] =
            128 + (float) (((0.5 * (float) r) - (0.41869 * (float) g) - (0.08131 * (float) 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;
  }