Package java.awt.image

Examples of java.awt.image.WritableRaster


    public Raster computeTile(int tileX, int tileY) {
        int orgX = tileXToX(tileX);
        int orgY = tileYToY(tileY);

        WritableRaster dst = RasterFactory.createWritableRaster(
            sampleModel, new Point(orgX, orgY));

        Rectangle rect = new Rectangle(orgX, orgY,
                                       sampleModel.getWidth(),
                                       sampleModel.getHeight());
        rect = rect.intersection(getBounds());

        int numBands = sampleModel.getNumBands();
        int p[] = new int[numBands];

        for (int y = rect.y; y < (rect.y + rect.height); y++) {
            for (int x = rect.x; x < (rect.x + rect.width); x++) {
                int value = Math.max(x & 0xFF, y & 0xFF);
                for (int i = 0; i < numBands; i++) {
                    p[i] = value;
                }
                dst.setPixel(x, y, p);
            }
        }
        return dst;
    }
View Full Code Here


    public Raster computeTile(int tileX, int tileY) {
        int orgX = tileXToX(tileX);
        int orgY = tileYToY(tileY);

        WritableRaster dst = RasterFactory.createWritableRaster(
            sampleModel, new Point(orgX, orgY));

        Rectangle rect = new Rectangle(orgX, orgY,
                                       sampleModel.getWidth(),
                                       sampleModel.getHeight());
        rect = rect.intersection(getBounds());

        int numBands = sampleModel.getNumBands();
        int p[] = new int[numBands];

        for (int y = rect.y; y < (rect.y + rect.height); y++) {
            for (int x = rect.x; x < (rect.x + rect.width); x++) {
                for (int i = 0; i < numBands; i++) {
                    switch ( transtype ) {
                    case DataBuffer.TYPE_BYTE:
                    case DataBuffer.TYPE_USHORT:
                    // For unsigned data, limint the random number to [0, 1]
                    // and the result to [0, MAX_VALUE];
                        p[i] = (int)(maxValue * Math.random());
                        break;
                    default:
                    // For signed data, limint the random number to [-1, 1]
                    // and the result to [MIN_VALUE, MAX_VALUE];
                        p[i] = (int)((maxValue+1.0F) *
                                 (Math.random() - 0.5F) * 2.0F);
                    }
                }
                dst.setPixel(x, y, p);
            }
        }
        return dst;
    }
View Full Code Here

            (tileY < 0) || (tileY >= tilesY)) {
            throw new IllegalArgumentException(JaiI18N.getString("TIFFImage12"));
        }

        // The tile to return.
        WritableRaster tile = null;

        // Synchronize the rest of the method in case other TIFFImage
        // instances using the same stream were created by the same
        // TIFFImageDecoder. This fixes 4690773.
        synchronized(this.stream) {

  // Get the data array out of the DataBuffer
  byte bdata[] = null;
  short sdata[] = null;
  int idata[] = null;
  float fdata[] = null;
  DataBuffer buffer = sampleModel.createDataBuffer();

  int dataType = sampleModel.getDataType();
  if (dataType == DataBuffer.TYPE_BYTE) {
      bdata = ((DataBufferByte)buffer).getData();
  } else if (dataType == DataBuffer.TYPE_USHORT) {
      sdata = ((DataBufferUShort)buffer).getData();
  } else if (dataType == DataBuffer.TYPE_SHORT) {
      sdata = ((DataBufferShort)buffer).getData();
  } else if (dataType == DataBuffer.TYPE_INT) {
      idata = ((DataBufferInt)buffer).getData();
  } else if (dataType == DataBuffer.TYPE_FLOAT) {
            if(buffer instanceof DataBufferFloat) {
                fdata = ((DataBufferFloat)buffer).getData();
            } else {
                // This is a hack to make this work with JAI which in some
                // cases downcasts the DataBuffer to a type-specific class.
                // In the case of float data this current means the JAI class
                // com.lightcrafts.mediax.jai.DataBufferFloat.
                try {
                    Method getDataMethod =
                        buffer.getClass().getMethod("getData", null);
                    fdata = (float[])getDataMethod.invoke(buffer, null);
                } catch(Exception e) {
                    String message = JaiI18N.getString("TIFFImage18");
                    ImagingListenerProxy.errorOccurred(message,
                                           new ImagingException(message, e),
                                           this, false);
//                    throw new RuntimeException(JaiI18N.getString("TIFFImage18"));
                }
            }
  }

        tile =
      (WritableRaster)RasterFactory.createWritableRaster(sampleModel,
                                                   buffer,
                                                   new Point(tileXToX(tileX),
                                                             tileYToY(tileY)));

  // Save original file pointer position and seek to tile data location.
  long save_offset = 0;
  try {
      save_offset = stream.getFilePointer();
      stream.seek(tileOffsets[tileY*tilesX + tileX]);
  } catch (IOException ioe) {
            String message = JaiI18N.getString("TIFFImage13");
            ImagingListenerProxy.errorOccurred(message,
                                   new ImagingException(message, ioe),
                                   this, false);
//      throw new RuntimeException(JaiI18N.getString("TIFFImage13"));
  }

  // Number of bytes in this tile (strip) after compression.
  int byteCount = (int)tileByteCounts[tileY*tilesX + tileX];

  // Find out the number of bytes in the current tile. If the image is
        // tiled this may include pixels which are outside of the image bounds
        // if the image width and height are not multiples of the tile width
        // and height respectively.
  Rectangle tileRect = new Rectangle(tileXToX(tileX), tileYToY(tileY),
             tileWidth, tileHeight);
  Rectangle newRect = isTiled ?
            tileRect : tileRect.intersection(getBounds());
        int unitsInThisTile = newRect.width * newRect.height * numBands;

        // Allocate read buffer if needed.
  byte data[] = compression != COMP_NONE || imageType == TYPE_PALETTE ?
            new byte[byteCount] : null;

        // Read the data, uncompressing as needed. There are four cases:
        // bilevel, palette-RGB, 4-bit grayscale, and everything else.
        if(imageType == TYPE_BILEVEL) { // bilevel
      try {
    if (compression == COMP_PACKBITS) {
        stream.readFully(data, 0, byteCount);

        // Since the decompressed data will still be packed
        // 8 pixels into 1 byte, calculate bytesInThisTile
        int bytesInThisTile;
        if ((newRect.width % 8) == 0) {
      bytesInThisTile = (newRect.width/8) * newRect.height;
        } else {
      bytesInThisTile =
                            (newRect.width/8 + 1) * newRect.height;
        }
        decodePackbits(data, bytesInThisTile, bdata);
    } else if (compression == COMP_LZW) {
        stream.readFully(data, 0, byteCount);
        lzwDecoder.decode(data, bdata, newRect.height);
    } else if (compression == COMP_FAX_G3_1D) {
        stream.readFully(data, 0, byteCount);
        decoder.decode1D(bdata, data, 0, newRect.height);
    } else if (compression == COMP_FAX_G3_2D) {
        stream.readFully(data, 0, byteCount);
        decoder.decode2D(bdata, data, 0, newRect.height,
                                     tiffT4Options);
    } else if (compression == COMP_FAX_G4_2D) {
        stream.readFully(data, 0, byteCount);
                    decoder.decodeT6(bdata, data, 0, newRect.height,
                                     tiffT6Options);
    } else if (compression == COMP_DEFLATE) {
                    stream.readFully(data, 0, byteCount);
                    inflate(data, bdata);
    } else if (compression == COMP_NONE) {
        stream.readFully(bdata, 0, byteCount);
    }

    stream.seek(save_offset);
      } catch (IOException ioe) {
                String message = JaiI18N.getString("TIFFImage13");
                ImagingListenerProxy.errorOccurred(message,
                                       new ImagingException(message, ioe),
                                       this, false);
//    throw new RuntimeException(JaiI18N.getString("TIFFImage13"));
      }
        } else if(imageType == TYPE_PALETTE) { // palette-RGB
      if (sampleSize == 16) {

    if (decodePaletteAsShorts) {

        short tempData[]= null;

        // At this point the data is 1 banded and will
        // become 3 banded only after we've done the palette
        // lookup, since unitsInThisTile was calculated with
        // 3 bands, we need to divide this by 3.
        int unitsBeforeLookup = unitsInThisTile / 3;

        // Since unitsBeforeLookup is the number of shorts,
        // but we do our decompression in terms of bytes, we
        // need to multiply it by 2 in order to figure out
        // how many bytes we'll get after decompression.
        int entries = unitsBeforeLookup * 2;

        // Read the data, if compressed, decode it, reset the pointer
        try {

      if (compression == COMP_PACKBITS) {

          stream.readFully(data, 0, byteCount);

          byte byteArray[] = new byte[entries];
          decodePackbits(data, entries, byteArray);
          tempData = new short[unitsBeforeLookup];
          interpretBytesAsShorts(byteArray, tempData,
               unitsBeforeLookup);

      else if (compression == COMP_LZW) {

          // Read in all the compressed data for this tile
          stream.readFully(data, 0, byteCount);

          byte byteArray[] = new byte[entries];
          lzwDecoder.decode(data, byteArray, newRect.height);
          tempData = new short[unitsBeforeLookup];
          interpretBytesAsShorts(byteArray, tempData,
               unitsBeforeLookup);

      else if (compression == COMP_DEFLATE) {

          stream.readFully(data, 0, byteCount);
          byte byteArray[] = new byte[entries];
          inflate(data, byteArray);
          tempData = new short[unitsBeforeLookup];
          interpretBytesAsShorts(byteArray, tempData,
               unitsBeforeLookup);

      } else if (compression == COMP_NONE) {

          // byteCount tells us how many bytes are there
          // in this tile, but we need to read in shorts,
          // which will take half the space, so while
          // allocating we divide byteCount by 2.
          tempData = new short[byteCount/2];
          readShorts(byteCount/2, tempData);
      }

      stream.seek(save_offset);

        } catch (IOException ioe) {
                        String message = JaiI18N.getString("TIFFImage13");
                        ImagingListenerProxy.errorOccurred(message,
                                               new ImagingException(message, ioe),
                                               this, false);
//      throw new RuntimeException(
//          JaiI18N.getString("TIFFImage13"));
        }

        if (dataType == DataBuffer.TYPE_USHORT) {

      // Expand the palette image into an rgb image with ushort
      // data type.
      int cmapValue;
      int count = 0, lookup, len = colormap.length/3;
      int len2 = len * 2;
      for (int i=0; i<unitsBeforeLookup; i++) {
          // Get the index into the colormap
          lookup = tempData[i] & 0xffff;
          // Get the blue value
          cmapValue = colormap[lookup+len2];
          sdata[count++] = (short)(cmapValue & 0xffff);
          // Get the green value
          cmapValue = colormap[lookup+len];
          sdata[count++] = (short)(cmapValue & 0xffff);
          // Get the red value
          cmapValue = colormap[lookup];
          sdata[count++] = (short)(cmapValue & 0xffff);
      }

        } else if (dataType == DataBuffer.TYPE_SHORT) {

      // Expand the palette image into an rgb image with
      // short data type.
      int cmapValue;
      int count = 0, lookup, len = colormap.length/3;
      int len2 = len * 2;
      for (int i=0; i<unitsBeforeLookup; i++) {
          // Get the index into the colormap
          lookup = tempData[i] & 0xffff;
          // Get the blue value
          cmapValue = colormap[lookup+len2];
          sdata[count++] = (short)cmapValue;
          // Get the green value
          cmapValue = colormap[lookup+len];
          sdata[count++] = (short)cmapValue;
          // Get the red value
          cmapValue = colormap[lookup];
          sdata[count++] = (short)cmapValue;
      }
        }

    } else {

        // No lookup being done here, when RGB values are needed,
        // the associated IndexColorModel can be used to get them.

        try {

      if (compression == COMP_PACKBITS) {

          stream.readFully(data, 0, byteCount);

          // Since unitsInThisTile is the number of shorts,
          // but we do our decompression in terms of bytes, we
          // need to multiply unitsInThisTile by 2 in order to
          // figure out how many bytes we'll get after
          // decompression.
          int bytesInThisTile = unitsInThisTile * 2;

          byte byteArray[] = new byte[bytesInThisTile];
          decodePackbits(data, bytesInThisTile, byteArray);
          interpretBytesAsShorts(byteArray, sdata,
               unitsInThisTile);

      } else if (compression == COMP_LZW) {

          stream.readFully(data, 0, byteCount);

          // Since unitsInThisTile is the number of shorts,
          // but we do our decompression in terms of bytes, we
          // need to multiply unitsInThisTile by 2 in order to
          // figure out how many bytes we'll get after
          // decompression.
          byte byteArray[] = new byte[unitsInThisTile * 2];
          lzwDecoder.decode(data, byteArray, newRect.height);
          interpretBytesAsShorts(byteArray, sdata,
               unitsInThisTile);

      else if (compression == COMP_DEFLATE) {

          stream.readFully(data, 0, byteCount);
          byte byteArray[] = new byte[unitsInThisTile * 2];
          inflate(data, byteArray);
          interpretBytesAsShorts(byteArray, sdata,
               unitsInThisTile);

      } else if (compression == COMP_NONE) {

          readShorts(byteCount/2, sdata);
      }

      stream.seek(save_offset);

        } catch (IOException ioe) {
                        String message = JaiI18N.getString("TIFFImage13");
                        ImagingListenerProxy.errorOccurred(message,
                                               new ImagingException(message, ioe),
                                               this, false);
//      throw new RuntimeException(
//          JaiI18N.getString("TIFFImage13"));
        }
    }

      } else if (sampleSize == 8) {

    if (decodePaletteAsShorts) {

        byte tempData[]= null;

        // At this point the data is 1 banded and will
        // become 3 banded only after we've done the palette
        // lookup, since unitsInThisTile was calculated with
        // 3 bands, we need to divide this by 3.
        int unitsBeforeLookup = unitsInThisTile / 3;

        // Read the data, if compressed, decode it, reset the pointer
        try {

      if (compression == COMP_PACKBITS) {

          stream.readFully(data, 0, byteCount);
          tempData = new byte[unitsBeforeLookup];
          decodePackbits(data, unitsBeforeLookup, tempData);

      else if (compression == COMP_LZW) {

          stream.readFully(data, 0, byteCount);
          tempData = new byte[unitsBeforeLookup];
          lzwDecoder.decode(data, tempData, newRect.height);

                        } else if (compression == COMP_JPEG_TTN2) {

                            stream.readFully(data, 0, byteCount);
                            Raster tempTile = decodeJPEG(data,
                                                         decodeParam,
                                                         colorConvertJPEG,
                                                         tile.getMinX(),
                                                         tile.getMinY());
                            int[] tempPixels = new int[unitsBeforeLookup];
                            tempTile.getPixels(tile.getMinX(),
                                               tile.getMinY(),
                                               tile.getWidth(),
                                               tile.getHeight(),
                                               tempPixels);
          tempData = new byte[unitsBeforeLookup];
                            for(int i = 0; i < unitsBeforeLookup; i++) {
                                tempData[i] = (byte)tempPixels[i];
                            }

      }  else if (compression == COMP_DEFLATE) {

          stream.readFully(data, 0, byteCount);
          tempData = new byte[unitsBeforeLookup];
          inflate(data, tempData);

      } else if (compression == COMP_NONE) {

          tempData = new byte[byteCount];
          stream.readFully(tempData, 0, byteCount);
      }

      stream.seek(save_offset);

        } catch (IOException ioe) {
                        String message = JaiI18N.getString("TIFFImage13");
                        ImagingListenerProxy.errorOccurred(message,
                                               new ImagingException(message, ioe),
                                               this, false);
//    throw new RuntimeException(
//          JaiI18N.getString("TIFFImage13"));
        }

        // Expand the palette image into an rgb image with ushort
        // data type.
        int cmapValue;
        int count = 0, lookup, len = colormap.length/3;
        int len2 = len * 2;
        for (int i=0; i<unitsBeforeLookup; i++) {
      // Get the index into the colormap
      lookup = tempData[i] & 0xff;
      // Get the blue value
      cmapValue = colormap[lookup+len2];
      sdata[count++] = (short)(cmapValue & 0xffff);
      // Get the green value
      cmapValue = colormap[lookup+len];
      sdata[count++] = (short)(cmapValue & 0xffff);
      // Get the red value
      cmapValue = colormap[lookup];
      sdata[count++] = (short)(cmapValue & 0xffff);
        }
    } else {

        // No lookup being done here, when RGB values are needed,
        // the associated IndexColorModel can be used to get them.

        try {

      if (compression == COMP_PACKBITS) {

          stream.readFully(data, 0, byteCount);
          decodePackbits(data, unitsInThisTile, bdata);

      } else if (compression == COMP_LZW) {

          stream.readFully(data, 0, byteCount);
          lzwDecoder.decode(data, bdata, newRect.height);

                        } else if (compression == COMP_JPEG_TTN2) {

                            stream.readFully(data, 0, byteCount);
                            tile.setRect(decodeJPEG(data,
                                                    decodeParam,
                                                    colorConvertJPEG,
                                                    tile.getMinX(),
                                                    tile.getMinY()));

      else if (compression == COMP_DEFLATE) {

                            stream.readFully(data, 0, byteCount);
                            inflate(data, bdata);

      } else if (compression == COMP_NONE) {

          stream.readFully(bdata, 0, byteCount);
      }

      stream.seek(save_offset);

        } catch (IOException ioe) {
                        String message = JaiI18N.getString("TIFFImage13");
                        ImagingListenerProxy.errorOccurred(message,
                                               new ImagingException(message, ioe),
                                               this, false);
//    throw new RuntimeException(
//          JaiI18N.getString("TIFFImage13"));
        }
    }

      } else if (sampleSize == 4) {

    int padding = (newRect.width % 2 == 0) ? 0 : 1;
    int bytesPostDecoding = ((newRect.width/2 + padding) *
           newRect.height);

    // Output short images
    if (decodePaletteAsShorts) {

        byte tempData[] = null;

        try {
      stream.readFully(data, 0, byteCount);
      stream.seek(save_offset);
        } catch (IOException ioe) {
                        String message = JaiI18N.getString("TIFFImage13");
                        ImagingListenerProxy.errorOccurred(message,
                                               new ImagingException(message, ioe),
                                               this, false);
//      throw new RuntimeException(
//          JaiI18N.getString("TIFFImage13"));
        }

        // If compressed, decode the data.
        if (compression == COMP_PACKBITS) {

      tempData = new byte[bytesPostDecoding];
      decodePackbits(data, bytesPostDecoding, tempData);

        else if (compression == COMP_LZW) {

      tempData = new byte[bytesPostDecoding];
      lzwDecoder.decode(data, tempData, newRect.height);

                    else if (compression == COMP_DEFLATE) {

      tempData = new byte[bytesPostDecoding];
      inflate(data, tempData);

        } else if (compression == COMP_NONE) {

      tempData = data;
        }

        int bytes = unitsInThisTile / 3;

        // Unpack the 2 pixels packed into each byte.
        data = new byte[bytes];

        int srcCount = 0, dstCount = 0;
        for (int j=0; j<newRect.height; j++) {
      for (int i=0; i<newRect.width/2; i++) {
          data[dstCount++] =
        (byte)((tempData[srcCount] & 0xf0) >> 4);
          data[dstCount++] =
        (byte)(tempData[srcCount++] & 0x0f);
      }

      if (padding == 1) {
          data[dstCount++] =
        (byte)((tempData[srcCount++] & 0xf0) >> 4);
      }
        }

        int len = colormap.length/3;
        int len2 = len*2;
        int cmapValue, lookup;
        int count = 0;
        for (int i=0; i<bytes; i++) {
      lookup = data[i] & 0xff;
      cmapValue = colormap[lookup+len2];
      sdata[count++] = (short)(cmapValue & 0xffff);
      cmapValue = colormap[lookup+len];
      sdata[count++] = (short)(cmapValue & 0xffff);
      cmapValue = colormap[lookup];
      sdata[count++] = (short)(cmapValue & 0xffff);
        }
    } else {

        // Output byte values, use IndexColorModel for unpacking
        try {

      // If compressed, decode the data.
      if (compression == COMP_PACKBITS) {

          stream.readFully(data, 0, byteCount);
          decodePackbits(data, bytesPostDecoding, bdata);

      else if (compression == COMP_LZW) {

          stream.readFully(data, 0, byteCount);
          lzwDecoder.decode(data, bdata, newRect.height);

                        else if (compression == COMP_DEFLATE) {

          stream.readFully(data, 0, byteCount);
          inflate(data, bdata);

      } else if (compression == COMP_NONE) {

          stream.readFully(bdata, 0, byteCount);
      }

      stream.seek(save_offset);

        } catch (IOException ioe) {
                        String message = JaiI18N.getString("TIFFImage13");
                        ImagingListenerProxy.errorOccurred(message,
                                               new ImagingException(message, ioe),
                                               this, false);
//      throw new RuntimeException(
//          JaiI18N.getString("TIFFImage13"));
        }
    }
      }
        } else if(imageType == TYPE_GRAY_4BIT) { // 4-bit gray
            try {
                if (compression == COMP_PACKBITS) {

                    stream.readFully(data, 0, byteCount);

                    // Since the decompressed data will still be packed
                    // 2 pixels into 1 byte, calculate bytesInThisTile
                    int bytesInThisTile;
                    if ((newRect.width % 8) == 0) {
                        bytesInThisTile = (newRect.width/2) * newRect.height;
                    } else {
                        bytesInThisTile = (newRect.width/2 + 1) *
                            newRect.height;
                    }

                    decodePackbits(data, bytesInThisTile, bdata);

                } else if (compression == COMP_LZW) {

                    stream.readFully(data, 0, byteCount);
                    lzwDecoder.decode(data, bdata, newRect.height);

                else if (compression == COMP_DEFLATE) {

                    stream.readFully(data, 0, byteCount);
                    inflate(data, bdata);

                } else {

                    stream.readFully(bdata, 0, byteCount);
                }

                stream.seek(save_offset);
      } catch (IOException ioe) {
                String message = JaiI18N.getString("TIFFImage13");
                ImagingListenerProxy.errorOccurred(message,
                                       new ImagingException(message, ioe),
                                       this, false);
//    throw new RuntimeException(JaiI18N.getString("TIFFImage13"));
      }
        } else { // everything else
      try {

    if (sampleSize == 8) {

        if (compression == COMP_NONE) {

      stream.readFully(bdata, 0, byteCount);

        } else if (compression == COMP_LZW) {

      stream.readFully(data, 0, byteCount);
      lzwDecoder.decode(data, bdata, newRect.height);

        } else if (compression == COMP_PACKBITS) {

      stream.readFully(data, 0, byteCount);
      decodePackbits(data, unitsInThisTile, bdata);

        } else if (compression == COMP_JPEG_TTN2) {

      stream.readFully(data, 0, byteCount);
                        tile.setRect(decodeJPEG(data,
                                                decodeParam,
                                                colorConvertJPEG,
                                                tile.getMinX(),
                                                tile.getMinY()));
        } else if (compression == COMP_DEFLATE) {

      stream.readFully(data, 0, byteCount);
                        inflate(data, bdata);
                    }

    } else if (sampleSize == 16) {

        if (compression == COMP_NONE) {

      readShorts(byteCount/2, sdata);

        } else if (compression == COMP_LZW) {

      stream.readFully(data, 0, byteCount);

      // Since unitsInThisTile is the number of shorts,
      // but we do our decompression in terms of bytes, we
      // need to multiply unitsInThisTile by 2 in order to
      // figure out how many bytes we'll get after
      // decompression.
      byte byteArray[] = new byte[unitsInThisTile * 2];
      lzwDecoder.decode(data, byteArray, newRect.height);
      interpretBytesAsShorts(byteArray, sdata,
                 unitsInThisTile);

        } else if (compression == COMP_PACKBITS) {

      stream.readFully(data, 0, byteCount);

      // Since unitsInThisTile is the number of shorts,
      // but we do our decompression in terms of bytes, we
      // need to multiply unitsInThisTile by 2 in order to
      // figure out how many bytes we'll get after
      // decompression.
      int bytesInThisTile = unitsInThisTile * 2;

      byte byteArray[] = new byte[bytesInThisTile];
      decodePackbits(data, bytesInThisTile, byteArray);
      interpretBytesAsShorts(byteArray, sdata,
                 unitsInThisTile);
        } else if (compression == COMP_DEFLATE) {

      stream.readFully(data, 0, byteCount);
      byte byteArray[] = new byte[unitsInThisTile * 2];
      inflate(data, byteArray);
      interpretBytesAsShorts(byteArray, sdata,
                 unitsInThisTile);

        }
    } else if (sampleSize == 32 &&
                           dataType == DataBuffer.TYPE_INT) { // redundant
        if (compression == COMP_NONE) {

      readInts(byteCount/4, idata);

        } else if (compression == COMP_LZW) {

      stream.readFully(data, 0, byteCount);

      // Since unitsInThisTile is the number of ints,
      // but we do our decompression in terms of bytes, we
      // need to multiply unitsInThisTile by 4 in order to
      // figure out how many bytes we'll get after
      // decompression.
      byte byteArray[] = new byte[unitsInThisTile * 4];
      lzwDecoder.decode(data, byteArray, newRect.height);
      interpretBytesAsInts(byteArray, idata,
                                             unitsInThisTile);

        } else if (compression == COMP_PACKBITS) {

      stream.readFully(data, 0, byteCount);

      // Since unitsInThisTile is the number of ints,
      // but we do our decompression in terms of bytes, we
      // need to multiply unitsInThisTile by 4 in order to
      // figure out how many bytes we'll get after
      // decompression.
      int bytesInThisTile = unitsInThisTile * 4;

      byte byteArray[] = new byte[bytesInThisTile];
      decodePackbits(data, bytesInThisTile, byteArray);
      interpretBytesAsInts(byteArray, idata,
                                             unitsInThisTile);
        } else if (compression == COMP_DEFLATE) {

      stream.readFully(data, 0, byteCount);
      byte byteArray[] = new byte[unitsInThisTile * 4];
      inflate(data, byteArray);
      interpretBytesAsInts(byteArray, idata,
                                             unitsInThisTile);

                    }
    } else if (sampleSize == 32 &&
                           dataType == DataBuffer.TYPE_FLOAT) { // redundant
        if (compression == COMP_NONE) {

      readFloats(byteCount/4, fdata);

        } else if (compression == COMP_LZW) {

      stream.readFully(data, 0, byteCount);

      // Since unitsInThisTile is the number of floats,
      // but we do our decompression in terms of bytes, we
      // need to multiply unitsInThisTile by 4 in order to
      // figure out how many bytes we'll get after
      // decompression.
      byte byteArray[] = new byte[unitsInThisTile * 4];
      lzwDecoder.decode(data, byteArray, newRect.height);
      interpretBytesAsFloats(byteArray, fdata,
                                               unitsInThisTile);

        } else if (compression == COMP_PACKBITS) {

      stream.readFully(data, 0, byteCount);

      // Since unitsInThisTile is the number of floats,
      // but we do our decompression in terms of bytes, we
      // need to multiply unitsInThisTile by 4 in order to
      // figure out how many bytes we'll get after
      // decompression.
      int bytesInThisTile = unitsInThisTile * 4;

      byte byteArray[] = new byte[bytesInThisTile];
      decodePackbits(data, bytesInThisTile, byteArray);
      interpretBytesAsFloats(byteArray, fdata,
                                               unitsInThisTile);
        } else if (compression == COMP_DEFLATE) {

      stream.readFully(data, 0, byteCount);
      byte byteArray[] = new byte[unitsInThisTile * 4];
                        inflate(data, byteArray);
      interpretBytesAsFloats(byteArray, fdata,
                                               unitsInThisTile);

                    }
    }

    stream.seek(save_offset);

      } catch (IOException ioe) {
                String message = JaiI18N.getString("TIFFImage13");
                ImagingListenerProxy.errorOccurred(message,
                                       new ImagingException(message, ioe),
                                       this, false);
//    throw new RuntimeException(JaiI18N.getString("TIFFImage13"));
      }

            // Modify the data for certain special cases.
            switch(imageType) {
            case TYPE_GRAY:
            case TYPE_GRAY_ALPHA:
                if(isWhiteZero) {
                    // Since we are using a ComponentColorModel with this
                    // image, we need to change the WhiteIsZero data to
                    // BlackIsZero data so it will display properly.
                    if (dataType == DataBuffer.TYPE_BYTE &&
                        !(colorModel instanceof IndexColorModel)) {

                        for (int l = 0; l < bdata.length; l += numBands) {
                            bdata[l] = (byte)(255 - bdata[l]);
                        }
                    } else if (dataType == DataBuffer.TYPE_USHORT) {

                        int ushortMax = Short.MAX_VALUE - Short.MIN_VALUE;
                        for (int l = 0; l < sdata.length; l += numBands) {
                            sdata[l] = (short)(ushortMax - sdata[l]);
                        }

                    } else if (dataType == DataBuffer.TYPE_SHORT) {

                        for (int l = 0; l < sdata.length; l += numBands) {
                            sdata[l] = (short)(~sdata[l]);
                        }
                    } else if (dataType == DataBuffer.TYPE_INT) {

                        long uintMax = Integer.MAX_VALUE - Integer.MIN_VALUE;
                        for (int l = 0; l < idata.length; l += numBands) {
                            idata[l] = (int)(uintMax - (long)idata[l]);
                        }
                    }
                }
                break;
            case TYPE_YCBCR_SUB:
                // Post-processing for YCbCr with subsampled chrominance:
                // simply replicate the chroma channels for displayability.
                int pixelsPerDataUnit = chromaSubH*chromaSubV;

                int numH = newRect.width/chromaSubH;
                int numV = newRect.height/chromaSubV;

                byte[] tempData = new byte[numH*numV*(pixelsPerDataUnit + 2)];
                System.arraycopy(bdata, 0, tempData, 0, tempData.length);

                int samplesPerDataUnit = pixelsPerDataUnit*3;
                int[] pixels = new int[samplesPerDataUnit];

                int bOffset = 0;
                int offsetCb = pixelsPerDataUnit;
                int offsetCr = offsetCb + 1;

                int y = newRect.y;
                for(int j = 0; j < numV; j++) {
                    int x = newRect.x;
                    for(int i = 0; i < numH; i++) {
                        int Cb = tempData[bOffset + offsetCb];
                        int Cr = tempData[bOffset + offsetCr];
                        int k = 0;
                        while(k < samplesPerDataUnit) {
                            pixels[k++] = tempData[bOffset++];
                            pixels[k++] = Cb;
                            pixels[k++] = Cr;
                        }
                        bOffset += 2;
                        tile.setPixels(x, y, chromaSubH, chromaSubV, pixels);
                        x += chromaSubH;
                    }
                    y += chromaSubV;
                }

View Full Code Here

                    catch (Exception e) {
                        // ignore
                    }
                }

                WritableRaster raster = bi.getRaster();
                DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
                ThemeReader.paintBackground(buffer.getData(),
                        part.getControlName(c), part.getValue(),
                        TMSchema.State.getValue(part, state),
                        0, 0, w, h, w);

                if (sm.getClass().getName().equals("sun.awt.image.CachingSurfaceManager")) {
                    try {
                        boolean oldAccEnabled = (Boolean) ReflectionUtils.callGet(sm, "isLocalAccelerationEnabled");
                        if (accEnabled != oldAccEnabled) {
                            ReflectionUtils.callSet(sm, "setLocalAccelerationEnabled", accEnabled);
                            ReflectionUtils.call(sm, "rasterChanged");
                        }
                    }
                    catch (Exception e) {
                        // ignore
                    }
                }
            }
            else // copied from JDK7 XPStyle. To make the code compilable under JDk6, we use RefectionUtils
                boolean accEnabled = false;
                Skin skin = (Skin) args[0];
                Part part = skin.part;
                State state = (State) args[1];
                if (state == null) {
                    state = skin.state;
                }
                if (c == null) {
                    c = skin.component;
                }
                BufferedImage bi = (BufferedImage) image;

                WritableRaster raster = bi.getRaster();
                DataBufferInt dbi = (DataBufferInt) raster.getDataBuffer();
                // Note that stealData() requires a markDirty() afterwards
                // since we modify the data in it.
                try {
                    ThemeReader.paintBackground(
                            (int[]) ReflectionUtils.callStatic(SunWritableRaster.class, "stealData", new Class[]{DataBufferInt.class, int.class}, new Object[]{dbi, 0}),
View Full Code Here

        int historyIdx;

        int aSum;

        ColorModel srcColorModel = src.getColorModel();
        WritableRaster srcRaster = src.getRaster();
        int[] dstBuffer = ((DataBufferInt) dst.getRaster().getDataBuffer()).getData();

        int lastPixelOffset = right * dstWidth;
        float hSumDivider = 1.0f / size;
        float vSumDivider = opacity / size;

        // horizontal pass : extract the alpha mask from the source picture and
        // blur it into the destination picture
        for (int srcY = 0, dstOffset = left * dstWidth; srcY < srcHeight; srcY++) {

            // first pixels are empty
            for (historyIdx = 0; historyIdx < shadowSize; ) {
                aHistory[historyIdx++] = 0;
            }

            aSum = 0;
            historyIdx = 0;

            // compute the blur average with pixels from the source image
            for (int srcX = 0; srcX < srcWidth; srcX++) {

                int a = (int) (aSum * hSumDivider); // calculate alpha value
                dstBuffer[dstOffset++] = a << 24;   // store the alpha value only
                                                    // the shadow color will be added in the next pass

                aSum -= aHistory[historyIdx]; // substract the oldest pixel from the sum

                // extract the new pixel ...
                a = srcColorModel.getAlpha(srcRaster.getDataElements(srcX, srcY, null));
                aHistory[historyIdx] = a;   // ... and store its value into history
                aSum += a;                  // ... and add its value to the sum

                if (++historyIdx >= shadowSize) {
                    historyIdx -= shadowSize;
View Full Code Here

            }
            PaletteData palette = new PaletteData(rgbs);
            ImageData data = new ImageData(
                image.getWidth(), image.getHeight(), cmodel.getPixelSize(), palette);
            data.transparentPixel = cmodel.getTransparentPixel();
            WritableRaster raster = image.getRaster();
            int[] pixelArray = new int[1];
            for (int y = 0; y < data.height; y++) {
                for (int x = 0; x < data.width; x++) {
                    raster.getPixel(x, y, pixelArray);
                    data.setPixel(x, y, pixelArray[0]);
                }
            }
            return data;
        } else if (image.getColorModel() instanceof ComponentColorModel) {
            ComponentColorModel cmodel = (ComponentColorModel)image.getColorModel();
            PaletteData palette = new PaletteData(0x0000FF, 0x00FF00, 0xFF0000); // BGR
            ImageData data = new ImageData(image.getWidth(), image.getHeight(), 24, palette);
            if (cmodel.hasAlpha()) data.alphaData = new byte[image.getWidth() * image.getHeight()];
            WritableRaster raster = image.getRaster();
            int[] pixelArray = new int[4];
            for (int y = 0; y < data.height; y++) {
                for (int x = 0; x < data.width; x++) {
                    raster.getPixel(x, y, pixelArray);
                    data.setPixel(x, y,
                        (pixelArray[2] << 16) | (pixelArray[1] << 8) | (pixelArray[0]));
                    if (data.alphaData != null)
                        data.alphaData[y*data.width + x] = (byte)pixelArray[3];
                }
View Full Code Here

    public Raster computeTile(int tileX, int tileY) {
        /* The origin of the tile. */
        Point org = new Point(tileXToX(tileX), tileYToY(tileY));

        /* Create a new WritableRaster to represent this tile. */
        WritableRaster dest = createWritableRaster(sampleModel, org);

        /* Find the intersection between this tile and the writable bounds. */
        Rectangle rect0 = new Rectangle(org.x, org.y, tileWidth, tileHeight);
        Rectangle destRect = rect0.intersection(computableBounds);
        Rectangle destRect1 = rect0.intersection(getBounds());
View Full Code Here

    final int        imgW      = tmpBufSize2; //  << info.shift;
    final int        maxLen      = imgW << info.shift; // * stepSize;

//    final int        imgW      = view.getWidth(); // (int) info.sublength;
    final BufferedImage    bufImg      = new BufferedImage( imgW, modelChannels, BufferedImage.TYPE_INT_RGB );
    final WritableRaster  raster      = bufImg.getRaster();
    final int[]        data      = new int[ imgW * modelChannels ];
    final int        dataStartOff  = imgW * (modelChannels - 1);

//    final AffineTransform  atOrig      = g2.getTransform();
    final Shape        clipOrig    = g2.getClip();
   
//    float[]          chanBuf;
    long          start      = info.span.start;
    long          totalLength    = info.getTotalLength();
    Span          chunkSpan;
    long          fullLen;
//    long          fullStop;
    int            chunkLen; // decimLen;
    float          scaleX, ampLog;
    Rectangle        r;
   
//    final float        scaleX      = (float) (view.getWidth() * stepSize) / totalLength;
   
//final float pixScale = 1072 / (view.getAmpLogMax() - view.getAmpLogMin());
//final float pixOff   = -view.getAmpLogMin();
final float pixScale = 10720 / (view.getAmpLogMax() - view.getAmpLogMin());
final float pixOff   = -view.getAmpLogMin() / 10;

    g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
   
    try {
      drawBusyList.clear(); // "must be called in the event thread"

      synchronized( bufSync ) {
        createBuffers();

long screenOffX = 0;
        while( totalLength > 0 ) {
          fullLen    = Math.min( maxLen, totalLength );
//          chunkLen  = (int) (fromPCM ? fullLen : decimHelps[ info.idx ].fullrateToSubsample( fullLen ));
          chunkLen  = (int) (decimHelps[ info.idx ].fullrateToSubsample( fullLen ));
//          decimLen  = chunkLen / info.inlineDecim;
//          chunkLen  = decimLen * info.inlineDecim;
          fullLen    = (long) chunkLen << info.shift;
//          chunkLen  = (int) (fullLen / stepSize);

//          if( fromPCM ) {
//            fullStop = fullScale.getSpan().stop;
//            if( start + fullLen <= fullStop ) {
//              chunkSpan = new Span( start, start + fullLen );
//              fullScale.readFrames( tmpBuf, 0, chunkSpan );
//            } else {
//              chunkSpan = new Span( start, fullStop );
//              fullScale.readFrames( tmpBuf, 0, chunkSpan );
//              // duplicate last frames
////              for( int i = (int) chunkSpan.getLength(), j = i - 1; i < (int) fullLen; i++ ) {
////                for( int ch = 0; ch < fullChannels; ch++ ) {
////                  sPeakP    = tmpBuf[ ch ];
////                  sPeakP[ i ]  = sPeakP[ j ];
////                }
////              }
//            }
//            decimator.decimatePCM( tmpBuf, tmpBuf2, fftBuf, 0, decimLen, info.inlineDecim );
//          } else {
////            chunkSpan = new Span( start, start + fullLen );
//            chunkSpan = new Span( start, start + fullLen );
//            readFrames( info.idx, tmpBuf2, 0, drawBusyList, chunkSpan, null);
//            if( info.inlineDecim > 1 ) decimator.decimate( tmpBuf2, tmpBuf2, 0, decimLen, info.inlineDecim );
//          }

          chunkSpan = new Span( start, start + fullLen );
         
//System.out.println( "chunkSpan = " + chunkSpan + "; chunkLen = " + chunkLen + "; fullLen = " + fullLen + "; screenOffX " + screenOffX + "; subLength " + info.sublength + "; shift " + info.shift + "; totalLen " + info.getTotalLength() );
         
          if( readFrames( info.idx, tmpBuf2, 0, drawBusyList, chunkSpan, null )) {
//          if( tempFAsync == null || tempFAsync[0] == null ) break;
//          tempFAsync[0].seekFrame( Math.min( start / stepSize, tempFAsync[0].getFrameNum() ));
//          int gaga = (int) Math.min( fullLen, Math.min( tmpBufSize2, tempFAsync[0].getFrameNum() - tempFAsync[0].getFramePosition() ));
//          tempFAsync[0].readFrames( tmpBuf2, 0, gaga);
         
            for( int ch = 0, tmpChReset = 0; ch < fullChannels; ch++, tmpChReset += modelChannels ) {
              r = view.rectForChannel( ch );
              scaleX = (float) r.width / info.getTotalLength();
  //System.out.println( " ... for ch = " + ch + "; scaleX = " + scaleX );
              for( int x = 0, off = 0; x < chunkLen; x++ ) {
                for( int y = 0, off2 = x + dataStartOff, tmpCh = tmpChReset; y < modelChannels; y++, tmpCh++, off2 -= imgW, off++ ) {
  //                ampLog = log10.calc( tmpBuf2[ tmpCh ][ x ]) * 20;
                  ampLog = log10.calc( Math.max( 1.0e-9f, tmpBuf2[ tmpCh ][ x ]));
                  data[ off2 ] = colors[ Math.max( 0, Math.min( 1072, (int) ((ampLog + pixOff) * pixScale) ))];
                }
              }
              raster.setDataElements( 0, 0, imgW, modelChannels, data );
              g2.drawImage( bufImg, r.x + (int) (screenOffX * scaleX + 0.5f), r.y, r.x + (int) ((screenOffX + fullLen) * scaleX + 0.5f), r.y + r.height, 0, 0, chunkLen, modelChannels, view );
            }
          }
          start += fullLen; // chunkLen * stepSize;
          totalLength -= fullLen; // chunkLen * stepSize;
 
View Full Code Here

    public void copyDataToRaster() {
        if (isDataCopy()) {

            // Writeback should only be necessary on destRasters which
            // should be writable so this cast should succeed.
            WritableRaster wr = (WritableRaster)raster;
            switch (getDataType()) {
            case DataBuffer.TYPE_BYTE:
                // Note: ALL branches of this case have been tested.
                // (bpb 10 May 2000)
                if(!isBinary()) {
                    // If this exception occurs then there is a logic
                    // error within this accessor since the only case
                    // wherein byte data should be COPIED is when the
                    // data set is binary.
                    throw new RuntimeException(JaiI18N.getString("RasterAccessor1"));
                }

                // This case only occurs for binary src and dst.

                ImageUtil.setUnpackedBinaryData(byteDataArrays[0],
                                                wr,
                                                new Rectangle(rectX, rectY,
                                                              rectWidth,
                                                              rectHeight));
                break;
            case DataBuffer.TYPE_INT:
                wr.setPixels(rectX,rectY,
                             rectWidth,rectHeight,
                             intDataArrays[0]);
                break;

            case DataBuffer.TYPE_FLOAT:
                wr.setPixels(rectX,rectY,
                             rectWidth,rectHeight,
                             floatDataArrays[0]);
                break;

            case DataBuffer.TYPE_DOUBLE:
                wr.setPixels(rectX,rectY,
                             rectWidth,rectHeight,
                             doubleDataArrays[0]);
                break;
            }
        }
View Full Code Here

    }

    public Raster computeTile(int tileX, int tileY) {
        // Create a new WritableRaster.
        Point org = new Point(tileXToX(tileX), tileYToY(tileY));
        WritableRaster dest = createWritableRaster(sampleModel, org);

        // Output bounds are initially equal to the tile bounds.
        int destMinX = dest.getMinX();
        int destMinY = dest.getMinY();
        int destMaxX = destMinX + dest.getWidth();
        int destMaxY = destMinY + dest.getHeight();

        // Clip output bounds to the dest image bounds.
        Rectangle bounds = getBounds();
        if (destMinX < bounds.x) {
            destMinX = bounds.x;
View Full Code Here

TOP

Related Classes of java.awt.image.WritableRaster

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.