private void writePixels(String chunk, byte[] stream, int x, int y, int width, int height)
      throws FormatException, IOException {
    MetadataRetrieve r = getMetadataRetrieve();
    int sizeC = getSamplesPerPixel();
    String type = r.getPixelsType(series).toString();
    int pixelType = FormatTools.pixelTypeFromString(type);
    boolean signed = FormatTools.isSigned(pixelType);

    if (!isFullPlane(x, y, width, height)) {
      throw new FormatException("APNGWriter does not support writing tiles.");
    }

    ByteArrayOutputStream s = new ByteArrayOutputStream();
    s.write(chunk.getBytes());
    if (chunk.equals("fdAT")) {
      s.write(DataTools.intToBytes(nextSequenceNumber++, false));
    }
    DeflaterOutputStream deflater = new DeflaterOutputStream(s);
    int planeSize = stream.length / sizeC;
    int rowLen = stream.length / height;
    int bytesPerPixel = stream.length / (width * height * sizeC);
    byte[] rowBuf = new byte[rowLen];
    for (int i = 0; i < height; i++) {
      deflater.write(0);
      if (interleaved) {
        if (littleEndian) {
          for (int col = 0; col < width * sizeC; col++) {
            int offset = (i * sizeC * width + col) * bytesPerPixel;
            int pixel = DataTools.bytesToInt(stream, offset, bytesPerPixel, littleEndian);
            DataTools.unpackBytes(pixel, rowBuf, col * bytesPerPixel, bytesPerPixel, false);
          }
        } else System.arraycopy(stream, i * rowLen, rowBuf, 0, rowLen);
      } else {
        int max = (int) Math.pow(2, bytesPerPixel * 8 - 1);
        for (int col = 0; col < width; col++) {
          for (int c = 0; c < sizeC; c++) {
            int offset = c * planeSize + (i * width + col) * bytesPerPixel;
            int pixel = DataTools.bytesToInt(stream, offset, bytesPerPixel, littleEndian);
            if (signed) {
              if (pixel < max) pixel += max;
              else pixel -= max;
            }
            int output = (col * sizeC + c) * bytesPerPixel;
            DataTools.unpackBytes(pixel, rowBuf, output, bytesPerPixel, false);
          }
        }
      }
      deflater.write(rowBuf);
    }
    deflater.finish();
    byte[] b = s.toByteArray();

    // write chunk length
    out.writeInt(b.length - 4);
    out.write(b);

    // write checksum
    out.writeInt(crc(b));
  }
Exemple #2
0
  /** @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int) */
  public void saveBytes(int no, byte[] buf, int x, int y, int w, int h)
      throws FormatException, IOException {
    checkParams(no, buf, x, y, w, h);

    MetadataRetrieve meta = getMetadataRetrieve();
    int sizeX = meta.getPixelsSizeX(series).getValue().intValue();
    int nChannels = getSamplesPerPixel();

    // write pixel data
    // for simplicity, write 80 char lines

    if (!initialized[series][no]) {
      initialized[series][no] = true;

      writeHeader();

      if (!isFullPlane(x, y, w, h)) {
        // write a dummy plane that will be overwritten in sections
        int planeSize = w * h * nChannels;
        for (int i = 0; i < planeSize; i++) {
          out.writeBytes(DUMMY_PIXEL);
        }
      }
    }

    int planeSize = w * h;

    StringBuffer buffer = new StringBuffer();

    int offset = y * sizeX * nChannels * 2;
    out.seek(planeOffset + offset);
    for (int row = 0; row < h; row++) {
      out.skipBytes(nChannels * x * 2);
      for (int col = 0; col < w * nChannels; col++) {
        int i = row * w * nChannels + col;
        int index =
            interleaved || nChannels == 1 ? i : (i % nChannels) * planeSize + (i / nChannels);
        String s = Integer.toHexString(buf[index]);
        // only want last 2 characters of s
        if (s.length() > 1) buffer.append(s.substring(s.length() - 2));
        else {
          buffer.append("0");
          buffer.append(s);
        }
      }
      out.writeBytes(buffer.toString());
      buffer.delete(0, buffer.length());
      out.skipBytes(nChannels * (sizeX - w - x) * 2);
    }

    // write footer

    out.seek(out.length());
    out.writeBytes("\nshowpage\n");
  }
  /* @see loci.formats.IFormatHandler#setId(String) */
  public void setId(String id) throws FormatException, IOException {
    super.setId(id);

    if (out.length() == 0) {
      MetadataRetrieve r = getMetadataRetrieve();
      int width = r.getPixelsSizeX(series).getValue().intValue();
      int height = r.getPixelsSizeY(series).getValue().intValue();
      int bytesPerPixel = FormatTools.getBytesPerPixel(r.getPixelsType(series).toString());
      int nChannels = getSamplesPerPixel();
      boolean indexed = getColorModel() != null && (getColorModel() instanceof IndexColorModel);
      littleEndian = !r.getPixelsBinDataBigEndian(series, 0);

      // write 8-byte PNG signature
      out.write(PNG_SIG);

      // write IHDR chunk

      out.writeInt(13);
      byte[] b = new byte[17];
      b[0] = 'I';
      b[1] = 'H';
      b[2] = 'D';
      b[3] = 'R';

      DataTools.unpackBytes(width, b, 4, 4, false);
      DataTools.unpackBytes(height, b, 8, 4, false);

      b[12] = (byte) (bytesPerPixel * 8);
      if (indexed) b[13] = (byte) 3;
      else if (nChannels == 1) b[13] = (byte) 0;
      else if (nChannels == 2) b[13] = (byte) 4;
      else if (nChannels == 3) b[13] = (byte) 2;
      else if (nChannels == 4) b[13] = (byte) 6;
      b[14] = (byte) 0;
      b[15] = (byte) 0;
      b[16] = (byte) 0;

      out.write(b);
      out.writeInt(crc(b));

      // write acTL chunk

      out.writeInt(8);
      out.writeBytes("acTL");
      numFramesPointer = out.getFilePointer();
      out.writeInt(0);
      out.writeInt(0);
      out.writeInt(0); // save a place for the CRC
    }
  }
  /** @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int) */
  public void saveBytes(int no, byte[] buf, int x, int y, int w, int h)
      throws FormatException, IOException {
    checkParams(no, buf, x, y, w, h);
    if (!isFullPlane(x, y, w, h)) {
      throw new FormatException("APNGWriter does not yet support saving image tiles.");
    }
    MetadataRetrieve meta = getMetadataRetrieve();

    int width = meta.getPixelsSizeX(series).getValue().intValue();
    int height = meta.getPixelsSizeY(series).getValue().intValue();

    if (!initialized[series][no]) {
      writeFCTL(width, height);
      if (numFrames == 0) writePLTE();
      initialized[series][no] = true;
    }

    writePixels(numFrames == 0 ? "IDAT" : "fdAT", buf, x, y, w, h);
    numFrames++;
  }
Exemple #5
0
  private void writeHeader() throws IOException {
    MetadataRetrieve r = getMetadataRetrieve();
    int width = r.getPixelsSizeX(series).getValue().intValue();
    int height = r.getPixelsSizeY(series).getValue().intValue();
    int nChannels = getSamplesPerPixel();

    out.writeBytes("%!PS-Adobe-2.0 EPSF-1.2\n");
    out.writeBytes("%%Title: " + currentId + "\n");
    out.writeBytes("%%Creator: OME Bio-Formats\n");
    out.writeBytes("%%Pages: 1\n");
    out.writeBytes("%%BoundingBox: 0 0 " + width + " " + height + "\n");
    out.writeBytes("%%EndComments\n\n");

    out.writeBytes("/ld {load def} bind def\n");
    out.writeBytes(
        "/s /stroke ld /f /fill ld /m /moveto ld /l "
            + "/lineto ld /c /curveto ld /rgb {255 div 3 1 roll 255 div 3 1 "
            + "roll 255 div 3 1 roll setrgbcolor} def\n");
    out.writeBytes("0 0 translate\n");
    out.writeBytes(((float) width) + " " + ((float) height) + " scale\n");
    out.writeBytes("/picstr 40 string def\n");
    out.writeBytes(
        width
            + " "
            + height
            + " 8 ["
            + width
            + " 0 0 "
            + (-1 * height)
            + " 0 "
            + height
            + "] {currentfile picstr readhexstring pop} ");
    if (nChannels == 1) {
      out.writeBytes("image\n");
    } else {
      out.writeBytes("false 3 colorimage\n");
    }
    planeOffset = out.getFilePointer();
  }
Exemple #6
0
  /** Retrieves calibration for X,Y,Z,T * */
  private float[] getCalibration(final IFormatReader r, final int[] dimensions) {
    float[] calibration = new float[dimensions.length];
    for (int i = 0; i < calibration.length; ++i) calibration[i] = 1;

    try {
      final String dimOrder = r.getDimensionOrder().toUpperCase();
      final MetadataRetrieve retrieve = (MetadataRetrieve) r.getMetadataStore();

      PositiveFloat cal;

      final int posX = dimOrder.indexOf('X');
      cal = retrieve.getPixelsPhysicalSizeX(0);
      if (posX >= 0 && posX < calibration.length && cal != null && cal.getValue() != 0)
        calibration[posX] = cal.getValue().floatValue();

      final int posY = dimOrder.indexOf('Y');
      cal = retrieve.getPixelsPhysicalSizeY(0);
      if (posY >= 0 && posY < calibration.length && cal != null && cal.getValue() != 0)
        calibration[posY] = cal.getValue().floatValue();

      final int posZ = dimOrder.indexOf('Z');
      cal = retrieve.getPixelsPhysicalSizeZ(0);
      if (posZ >= 0 && posZ < calibration.length && cal != null && cal.getValue() != 0)
        calibration[posZ] = cal.getValue().floatValue();

      final int posT = dimOrder.indexOf('T');
      retrieve.getPixelsTimeIncrement(0);
      Double cal1 = retrieve.getPixelsTimeIncrement(0);
      if (posT >= 0 && posT < calibration.length && cal1 != null && cal1.floatValue() != 0)
        calibration[posT] = cal1.floatValue();
    } catch (Exception e) {
      // somehow an error occured reading the calibration
    }

    return calibration;
  }
  public static double[] readImageInfo(String id) {
    try {
      reader.setId(id);
    } catch (FormatException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // ORDERING: 0 little, 1 seriesCount, 2 pixelType, 3 bpp, 4 itkComponentType, 5 sizeX,
    //           6 sizeY, 7 sizeZ, 8 sizeT, 9 sizeC, 10 effSizeC, 11 rgbChannelCount, 12 imageCount
    //           13 physX, 14 physY, 15 physZ, 16 timeIncrement
    double[] returnValues = new double[17];

    // return this and use SetByteOrderToLittleEndian or SetByteOrderToBigEndian in C++ land
    boolean little = reader.isLittleEndian();

    if (little) {
      returnValues[0] = 1;
    } else {
      returnValues[0] = 0;
    }

    returnValues[1] = reader.getSeriesCount();

    // return bpp and set an IOComponent based on it
    int pixelType = reader.getPixelType();
    returnValues[2] = (double) pixelType;
    returnValues[3] = (double) FormatTools.getBytesPerPixel((int) returnValues[2]);

    // 0 UCHAR, 1 CHAR, 2 USHORT, 3 SHORT, 4 UINT, 5 INT, 6 FLOAT, 7 DOUBLE, 8 UNKNOWN
    if (pixelType == FormatTools.UINT8) returnValues[4] = (double) 0;
    else if (pixelType == FormatTools.INT8) returnValues[4] = (double) 1;
    else if (pixelType == FormatTools.UINT16) returnValues[4] = (double) 2;
    else if (pixelType == FormatTools.INT16) returnValues[4] = (double) 3;
    else if (pixelType == FormatTools.UINT32) returnValues[4] = (double) 4;
    else if (pixelType == FormatTools.INT32) returnValues[4] = (double) 5;
    else if (pixelType == FormatTools.FLOAT) returnValues[4] = (double) 6;
    else if (pixelType == FormatTools.DOUBLE) returnValues[4] = (double) 7;
    else returnValues[4] = (double) 8;

    // return these
    returnValues[5] = (double) reader.getSizeX();
    returnValues[6] = (double) reader.getSizeY();
    returnValues[7] = (double) reader.getSizeZ();
    returnValues[8] = (double) reader.getSizeT();
    returnValues[9] = (double) reader.getSizeC();
    returnValues[10] = (double) reader.getEffectiveSizeC();
    returnValues[11] = (double) reader.getRGBChannelCount();
    returnValues[12] = (double) reader.getImageCount();

    MetadataRetrieve retrieve = MetadataTools.asRetrieve(reader.getMetadataStore());
    Double d = retrieve.getPixelsPhysicalSizeX(0).getValue();
    double d2 = d == null ? 1.0 : d.doubleValue();
    returnValues[13] = d2;
    d = retrieve.getPixelsPhysicalSizeY(0).getValue();
    d2 = d == null ? 1.0 : d.doubleValue();
    returnValues[14] = d2;
    d = retrieve.getPixelsPhysicalSizeZ(0).getValue();
    d2 = d == null ? 1.0 : d.doubleValue();
    returnValues[15] = d2;
    d = retrieve.getPixelsTimeIncrement(0);
    d2 = d == null ? 1.0 : d.doubleValue();
    returnValues[16] = d2;

    return returnValues;
  }