Ejemplo n.º 1
0
 /**
  * The method to invoke the application.
  *
  * @param arg a list of DICOM files which may contain chest x-ray images
  */
 public static void main(String arg[]) {
   try {
     new ChestImageViewer(arg);
   } catch (Exception e) {
     e.printStackTrace(System.err);
   }
 }
 public DisplayImage16() {
   super();
   try {
     jbInit();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 3
0
  /** @param arg */
  public static void main(String arg[]) {
    TestApp af = new TestApp();

    SourceImage sImg = null;

    int imagesPerRow = 0;
    int imagesPerCol = 0;

    int imgMin = 65536;
    int imgMax = 0;

    boolean signed = false;
    boolean inverted = false;
    boolean hasPad = false;
    int padValue = 0;

    if (arg.length == 6) {
      // do it with raw file
      int w = 0;
      int h = 0;
      int d = 0;
      try {
        w = Integer.valueOf(arg[1]).intValue();
        h = Integer.valueOf(arg[2]).intValue();
        d = Integer.valueOf(arg[3]).intValue();
        imagesPerRow = Integer.valueOf(arg[4]).intValue();
        imagesPerCol = Integer.valueOf(arg[5]).intValue();
      } catch (Exception e) {
        System.err.println(e);
        System.exit(0);
      }

      try {
        FileInputStream i = new FileInputStream(arg[0]);
        sImg = new SourceImage(i, w, h, d);
      } catch (Exception e) {
        System.err.println(e);
        System.exit(0);
      }
    } else {
      // do it with DICOM file

      if (arg.length > 2) {
        try {
          imagesPerRow = Integer.valueOf(arg[1]).intValue();
          imagesPerCol = Integer.valueOf(arg[2]).intValue();
        } catch (Exception e) {
          System.err.println(e);
          e.printStackTrace(System.err);
          System.exit(0);
        }
      } else {
        imagesPerRow = 1;
        imagesPerCol = 1;
      }

      try {
        DicomInputStream i = new DicomInputStream(new FileInputStream(arg[0]));
        sImg = new SourceImage(i);
      } catch (Exception e) {
        System.err.println(e);
        e.printStackTrace(System.err);
        System.exit(0);
      }
    }

    try {
      // com.apple.cocoa.application.NSMenu.setMenuBarVisible(false);							// Won't compile on
      // other platforms
      // Class classToUse =
      // ClassLoader.getSystemClassLoader().loadClass("com.apple.cocoa.application.NSMenu");	//
      // Needs "/System/Library/Java" in classpath
      Class classToUse =
          new java.net.URLClassLoader(new java.net.URL[] {new File("/System/Library/Java").toURL()})
              .loadClass("com.apple.cocoa.application.NSMenu");
      Class[] parameterTypes = {Boolean.TYPE};
      java.lang.reflect.Method methodToUse =
          classToUse.getDeclaredMethod("setMenuBarVisible", parameterTypes);
      Object[] args = {Boolean.FALSE};
      methodToUse.invoke(null /*since static*/, args);
    } catch (Exception e) { // ClassNotFoundException,NoSuchMethodException,IllegalAccessException
      e.printStackTrace(System.err);
    }

    java.awt.Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    int frameWidth = (int) d.getWidth();
    int frameHeight = (int) d.getHeight();
    System.err.println("frameWidth=" + frameWidth);
    System.err.println("frameHeight=" + frameHeight);
    af.setUndecorated(true);
    af.setLocation(0, 0);
    af.setSize(frameWidth, frameHeight);

    JPanel multiPanel = new JPanel();
    multiPanel.setLayout(new GridLayout(imagesPerCol, imagesPerRow));
    multiPanel.setBackground(Color.black);

    SingleImagePanel imagePanel[] = new SingleImagePanel[imagesPerRow * imagesPerCol];

    int singleWidth = frameWidth / imagesPerRow;
    int singleHeight = frameHeight / imagesPerCol;
    System.err.println("singleWidth=" + singleWidth);
    System.err.println("singleHeight=" + singleHeight);

    for (int x = 0; x < imagesPerCol; ++x) {
      for (int y = 0; y < imagesPerRow; ++y) {
        SingleImagePanel ip = new SingleImagePanel(sImg);
        // ip.setPreferredSize(new Dimension(img.getWidth(),img.getHeight()));
        // ip.setPreferredSize(new Dimension(sImg.getWidth(),sImg.getHeight()));
        ip.setPreferredSize(new Dimension(singleWidth, singleHeight));
        multiPanel.add(ip);
        imagePanel[x * imagesPerRow + y] = ip;
      }
    }

    // multiPanel.setSize(img.getWidth()*imagesPerRow,img.getHeight()*imagesPerRow);

    // JScrollPane scrollPane = new JScrollPane(multiPanel);

    Container content = af.getContentPane();
    content.setLayout(new GridLayout(1, 1));
    // content.add(scrollPane);
    content.add(multiPanel);

    af.pack();
    af.setVisible(true);
  }
Ejemplo n.º 4
0
  /**
   * @param filenames
   * @exception Exception if internal error
   */
  public void loadMultiPanelFromSpecifiedFiles(String filenames[]) throws Exception {

    int nFiles = filenames.length;

    SingleImagePanel imagePanels[] = new SingleImagePanel[nFiles];

    String orientations[][] = new String[nFiles][];
    String views[] = new String[nFiles];
    String lateralityViewAndModifiers[] = new String[nFiles];
    String lateralities[] = new String[nFiles];
    int widths[] = new int[nFiles];
    int heights[] = new int[nFiles];
    PixelSpacing spacing[] = new PixelSpacing[nFiles];

    String rowOrientations[] = new String[nFiles];
    String columnOrientations[] = new String[nFiles];

    HashMap eventContexts = new HashMap();

    double maximumHorizontalExtentInMm = 0;
    double maximumVerticalExtentInMm = 0;

    StructuredReport sr[] = new StructuredReport[nFiles];

    int nImages = 0;
    int nCAD = 0;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    for (int f = 0; f < nFiles; ++f) {
      try {
        String filename = filenames[f];
        DicomInputStream distream = null;
        InputStream in = classLoader.getResourceAsStream(filename);
        if (in != null) {
          distream = new DicomInputStream(in);
        } else {
          distream = new DicomInputStream(new File(filename));
        }
        AttributeList list = new AttributeList();
        list.read(distream);
        if (list.isImage()) {
          int i = nImages++;
          System.err.println("IMAGE [" + i + "] is file " + f + " (" + filenames[f] + ")");

          orientations[i] = getPatientOrientation(list);
          // System.err.println("IMAGE ["+i+"] orientation="+(orientations[i] == null &&
          // orientations[i].length == 2 ? "" : (orientations[i][0] + " " + orientations[i][1])));
          views[i] = getView(list);
          // System.err.println("IMAGE ["+i+"] view="+views[i]);
          lateralityViewAndModifiers[i] = getImageLateralityViewModifierAndViewModifier(list);
          // System.err.println("IMAGE ["+i+"]
          // lateralityViewAndModifiers="+lateralityViewAndModifiers[i]);
          // System.err.println("File "+filenames[f]+": "+lateralityViewAndModifiers[i]);
          lateralities[i] = getLaterality(list);
          // System.err.println("IMAGE ["+i+"] laterality="+lateralities[i]);
          spacing[i] = new PixelSpacing(list);
          // System.err.println("IMAGE ["+i+"] spacing="+spacing[i]);

          SourceImage sImg = new SourceImage(list);
          BufferedImage img = sImg.getBufferedImage();

          widths[i] = sImg.getWidth();
          heights[i] = sImg.getHeight();

          boolean shareVOIEventsInStudy =
              false; // does not seem to work anyway, since adding VOITransform to panel constructor
                     // :(

          EventContext eventContext = new EventContext(Integer.toString(i));

          SingleImagePanel imagePanel = makeNewImagePanel(sImg, eventContext);
          imagePanel.setDemographicAndTechniqueAnnotations(
              new DemographicAndTechniqueAnnotations(list),
              "SansSerif",
              Font.PLAIN,
              10,
              Color.pink);
          imagePanel.setOrientationAnnotations(
              new OrientationAnnotations(rowOrientations[i], columnOrientations[i]),
              "SansSerif",
              Font.PLAIN,
              20,
              Color.pink);
          imagePanel.setPixelSpacingInSourceImage(
              spacing[i].getSpacing(), spacing[i].getDescription());
          if (Attribute.getSingleStringValueOrEmptyString(list, TagFromName.VOILUTFunction)
              .equals("SIGMOID")) {
            imagePanel.setVOIFunctionToLogistic();
          }
          imagePanels[i] = imagePanel;
        } else {
          throw new DicomException("Unsupported SOP Class in file " + filenames[f]);
        }
      } catch (Exception e) { // FileNotFoundException,IOException,DicomException
        e.printStackTrace(System.err);
      }
    }

    // int imagesPerRow = nImages;			// i.e., 1 -> 1, 2 -> 1, 4 -> 4, 5 -> 4, 8 -> 4
    // int imagesPerCol = 1;

    int imagesPerRow = nImages >= 8 ? 8 : nImages; // i.e., 1 -> 1, 2 -> 1, 4 -> 4, 5 -> 4, 8 -> 4
    int imagesPerCol =
        (nImages - 1) / imagesPerRow + 1; // i.e., 1 -> 1, 2 -> 2, 4 -> 1, 5 -> 2, 8 -> 2

    int singleWidth = frameWidth / imagesPerRow;
    int singleHeight = frameHeight / imagesPerCol;

    if (nImages == 1 && singleWidth > singleHeight) {
      singleWidth =
          singleWidth / 2; // use only half the screen for a single view and a landscape monitor
    }

    for (int i = 0; i < nImages; ++i) {
      DisplayedAreaSelection displayedAreaSelection = null;
      displayedAreaSelection =
          new DisplayedAreaSelection(
              widths[i],
              heights[i],
              0,
              0,
              widths[i],
              heights[i],
              true, // in case spacing was not supplied
              0,
              0,
              0,
              0,
              0,
              false /*crop*/);
      imagePanels[i].setDisplayedAreaSelection(displayedAreaSelection);
      imagePanels[i].setPreTransformImageRelativeCoordinates(null);
    }

    SingleImagePanel.deconstructAllSingleImagePanelsInContainer(multiPanel);
    multiPanel.removeAll();
    multiPanel.setLayout(new GridLayout(imagesPerCol, imagesPerRow));
    multiPanel.setBackground(Color.black);

    for (int x = 0; x < imagesPerCol; ++x) {
      for (int y = 0; y < imagesPerRow; ++y) {
        int i = x * imagesPerRow + y;
        if (i < nImages) {
          imagePanels[i].setPreferredSize(new Dimension(singleWidth, singleHeight));
          multiPanel.add(imagePanels[i]);
        }
      }
    }
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
  }
Ejemplo n.º 5
0
  private IFD writeCMYKImage(ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param)
      throws IOException {
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(width)); // 256
      ifd.add(new DEFactory.ImageLengthDE(height)); // 257

      DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(4);
      bpss.setBitsPerSample(0, 8); // cyan
      bpss.setBitsPerSample(1, 8); // magneta
      bpss.setBitsPerSample(2, 8); // yellow
      bpss.setBitsPerSample(3, 8); // key (black)
      ifd.add(bpss); // 258

      ifd.add(new DEFactory.CompressionDE(NOCOMPRESSION)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(CMYK)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per strip
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }

      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.SamplesPerPixelDE(4)); // 277
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279

      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      int index = 0;
      for (int y = 0; y < height; y += maxrps) {
        /*
        Assume rgb image.

        Each strip: evaluate c m y k colour
                    save in byte array
                    write to image file
        */

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        for (int i = 0; i < maxrps; i++) {
          if ((y + i) == height) {
            break;
          } // last strip might have less rows
          for (int x = 0; x < width; x++) {
            int c = image.getRGB(x, y + i);

            int R = (c >> 16) & 0x00FF;
            int G = (c >> 8) & 0x00FF;
            int B = (c) & 0x00FF;

            if ((R == 255) && (G == 255) && (B == 255)) {
              baos.write(0);
              baos.write(0);
              baos.write(0);
              baos.write(0);
            } else {
              double C = 1.0 - R / 255.0;
              double M = 1.0 - G / 255.0;
              double Y = 1.0 - B / 255.0;

              double K = C;
              if (M < K) {
                K = M;
              }
              if (Y < K) {
                K = Y;
              }

              C = ((C - K) / (1.0 - K)) * 255.0;
              M = ((M - K) / (1.0 - K)) * 255.0;
              Y = ((Y - K) / (1.0 - K)) * 255.0;
              K *= 255.0;

              baos.write((int) C);
              baos.write((int) M);
              baos.write((int) Y);
              baos.write((int) K);
            }
          }
        }
        baos.close();

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream

        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeCMYKImage:\n\t" + e.getMessage());
    }
  }
Ejemplo n.º 6
0
  private IFD writeYCbCrImage(
      ImageOutputStream out, BufferedImage image, int comp, TIFFImageWriteParam param)
      throws IOException {
    image = convert(image, BufferedImage.TYPE_INT_RGB);
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      int ss = (param == null) ? 0x22 : param.getSubSampling();

      int ssh = (ss >> 4) & 0x0F;
      int ssv = ss & 0x0F;

      if (ssh
          < ssv) { // YCbCrSubsampleVert shall always be less than or equal to YCbCrSubsampleHoriz.
        throw new IOException(
            "Internal error: YCbCrSubsampleVert is not less than YCbCrSubsampleHoriz.");
      }

      //      int ww=((width +ssh-1)/ssh)*ssh;                                   // [1] p.92
      //      int hh=((height+ssv-1)/ssv)*ssv;
      int ww = width;
      int hh = height;

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(ww)); // 256
      ifd.add(new DEFactory.ImageLengthDE(hh)); // 257

      DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(3);
      bpss.setBitsPerSample(0, 8); // Y
      bpss.setBitsPerSample(1, 8); // Cb
      bpss.setBitsPerSample(2, 8); // Cr
      ifd.add(bpss); // 258

      ifd.add(new DEFactory.CompressionDE(comp)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(YCbCr)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per strip
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }
      if (comp == JPEG) {
        maxrps = ((maxrps + 8 * ssv - 1) / (8 * ssv)) * (8 * ssv);
        maxstripes = (height + maxrps - 1) / maxrps;
      }

      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.SamplesPerPixelDE(3)); // 277
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279

      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream os = baos;
      JPEGOutputStream jpegos = null;

      if (comp == JPEG) {
        jpegos = new JPEGOutputStream(baos);

        int quality = (param == null) ? 50 : (int) (param.getCompressionQuality() * 100);

        jpegos.setZZQuantizationTable(0, JPEGConstants.LQT, quality);
        jpegos.setZZQuantizationTable(1, JPEGConstants.CQT, quality);

        jpegos.setRawDCHuffmanTable(0, JPEGConstants.HLDCTable);
        jpegos.setRawACHuffmanTable(0, JPEGConstants.HLACTable);
        jpegos.setRawDCHuffmanTable(1, JPEGConstants.HCDCTable);
        jpegos.setRawACHuffmanTable(1, JPEGConstants.HCACTable);

        jpegos.defineQuantizationTables();
        jpegos.defineHuffmanTables();
        jpegos.close();

        DEFactory.JPEGTablesDE jpegtables = new DEFactory.JPEGTablesDE(baos.toByteArray());
        ifd.add(jpegtables); // 347

        baos.reset();

        os = jpegos;
      }

      //      CCIR Recommendation 601-1  LumaRed=299/1000 LumaGreen=587/1000 LumeBlue=114/1000
      //      Y  = ( LumaRed * R + LumaGreen * G + LumaBlue * B )
      //      Cb = ( B - Y ) / ( 2 - 2 * LumaBlue )
      //      Cr = ( R - Y ) / ( 2 - 2 * LumaRed )

      double LumaRed = 299.0 / 1000.0;
      double LumaGreen = 587.0 / 1000.0;
      double LumaBlue = 114.0 / 1000.0;

      DEFactory.YCbCrCoefficientsDE YCbCrCoeff = new DEFactory.YCbCrCoefficientsDE();
      YCbCrCoeff.setLumaRed(LumaRed); // Y
      YCbCrCoeff.setLumaGreen(LumaGreen); // Cb
      YCbCrCoeff.setLumaBlue(LumaBlue); // Cr
      ifd.add(YCbCrCoeff); // 529

      DEFactory.YCbCrSubSamplingDE YCbCrSubSampling = new DEFactory.YCbCrSubSamplingDE();
      YCbCrSubSampling.setHoriz(ssh);
      YCbCrSubSampling.setVert(ssv);
      ifd.add(YCbCrSubSampling); // 530

      double RfBY = 0;
      double RfWY = 255;
      double RfBCb = 128;
      double RfWCb = 255;
      double RfBCr = 128;
      double RfWCr = 255;

      DEFactory.ReferenceBlackWhiteDE ReferenceBlackWhite = new DEFactory.ReferenceBlackWhiteDE();
      ReferenceBlackWhite.setY(RfBY, RfWY);
      ReferenceBlackWhite.setCb(RfBCb, RfWCb);
      ReferenceBlackWhite.setCr(RfBCr, RfWCr);
      ifd.add(ReferenceBlackWhite); // 532

      TIFFYCbCrOutputStream ycbcros;
      if (jpegos == null) {
        ycbcros = new TIFFYCbCrOutputStream(os, width, ssv, ssh);
        os = new TIFFSubSamplingOutputStream(ycbcros, width, ssv, ssh);
      } else {
        ycbcros = new TIFFYCbCrOutputStream(os, width, 1, 1); // jpeg does own subsampling
        os = ycbcros;
      }

      ycbcros.setPositioning(1);
      ycbcros.setColourCoefficients(LumaRed, LumaGreen, LumaBlue);
      ycbcros.setRfBWY(RfBY, RfWY);
      ycbcros.setRfBWCb(RfBCb, RfWCb);
      ycbcros.setRfBWCr(RfBCr, RfWCr);

      WritableRaster raster = image.getRaster();
      DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
      int[] imgdata = (int[]) buffer.getData();

      int c = 0, index = 0;
      for (int y = 0; y < height; y += maxrps) {

        if ((height - y) < maxrps) {
          maxrps = height - y;
        }

        if (jpegos != null) {
          jpegos.startOfImage();
          int[] hv = {(ssh << 4) | ssv, 0x11, 0x11}; // (Hi<<4)|Vi
          int[] q = {0, 1, 1}; // quantization table Y=0, Cb=Cr=1
          //          jpegos.startOfFrame(((maxrps+ssv-1)/ssv)*ssv,ww,hv,q);
          jpegos.startOfFrame(maxrps, ww, hv, q);
          int[] sel = {0, 1, 1}; // DC,AC code table Y=0, Cb=Cr=1
          jpegos.startOfScan(sel);
        }

        for (int i = 0; i < maxrps; i++) {
          int x = 0;
          while (x < width) {
            c = imgdata[x + (y + i) * width];
            //            c = image.getRGB(x,y+i);
            os.write((c >> 16) & 0x000000FF);
            os.write((c >> 8) & 0x000000FF);
            os.write(c & 0x000000FF);
            x++;
          }
          while (x < ww) {
            os.write((c >> 16) & 0x000000FF);
            os.write((c >> 8) & 0x000000FF);
            os.write(c & 0x000000FF);
            x++;
          }
        }
        os.close();

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream
        baos.reset();
        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeYCbCrImage:\n\t" + e.getMessage());
    }
  }
Ejemplo n.º 7
0
  private IFD writeRGBImage(
      ImageOutputStream out, BufferedImage image, int comp, TIFFImageWriteParam param)
      throws IOException {
    image = convert(image, BufferedImage.TYPE_INT_RGB);
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(width)); // 256
      ifd.add(new DEFactory.ImageLengthDE(height)); // 257

      DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(3);
      bpss.setBitsPerSample(0, 8); // red
      bpss.setBitsPerSample(1, 8); // green
      bpss.setBitsPerSample(2, 8); // blue
      ifd.add(bpss); // 258

      ifd.add(new DEFactory.CompressionDE(comp)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(RGB)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per strip
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }
      if (comp == JPEG) {
        maxrps = ((maxrps + 8 - 1) / 8) * 8;
        maxstripes = (height + maxrps - 1) / maxrps;
      }
      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.SamplesPerPixelDE(3)); // 277
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279
      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream os = baos;
      JPEGOutputStream jpegos = null;

      if (comp == JPEG) { // add JPEGTables tag
        jpegos = new JPEGOutputStream(baos);

        int quality = (param == null) ? 50 : (int) (param.getCompressionQuality() * 100);

        jpegos.setZZQuantizationTable(0, JPEGConstants.LQT, quality);
        jpegos.setRawDCHuffmanTable(0, JPEGConstants.HLDCTable);
        jpegos.setRawACHuffmanTable(0, JPEGConstants.HLACTable);

        jpegos.defineQuantizationTables();
        jpegos.defineHuffmanTables();
        jpegos.close();

        DEFactory.JPEGTablesDE jpegtables = new DEFactory.JPEGTablesDE(baos.toByteArray());
        ifd.add(jpegtables); // 347

        baos.reset();

        os = jpegos;
      }

      WritableRaster raster = image.getRaster();
      DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
      int[] imgdata = (int[]) buffer.getData();

      int index = 0;
      for (int y = 0; y < height; y += maxrps) {
        /*
        Assume rgb image.

        Each strip: evaluate r g b colour
                    save in byte array
                    write to image file
        */

        if ((height - y) < maxrps) {
          maxrps = height - y;
        }

        if (jpegos != null) { // jpeg: SOI,SOF,SOS marker
          jpegos.startOfImage();
          int[] hv = {0x11, 0x11, 0x11}; // (Hi<<4)|Vi
          int[] q = {0, 0, 0}; // quantization table 0
          jpegos.startOfFrame(maxrps, width, hv, q);
          int[] sel = {0, 0, 0}; // DC,AC code table 0
          jpegos.startOfScan(sel);
        }

        for (int i = 0; i < maxrps; i++) { // write RGB data
          for (int x = 0; x < width; x++) {
            int c = imgdata[x + (y + i) * width];
            os.write((c >> 16) & 0x000000FF);
            os.write((c >> 8) & 0x000000FF);
            os.write(c & 0x000000FF);
          }
        }
        os.close(); // jpeg: EOI marker

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream
        baos.reset();
        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeRGBImage:\n\t" + e.getMessage());
    }
  }
Ejemplo n.º 8
0
  private IFD writeBModHufImage(
      ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param) throws IOException {
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(width)); // 256
      ifd.add(new DEFactory.ImageLengthDE(height)); // 257
      ifd.add(new DEFactory.CompressionDE(CCITTGROUP3MODHUFFMAN)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(WhiteIsZero)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per stripe
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }

      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279

      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      int index = 0;
      for (int y = 0; y < height; y += maxrps) {
        /*
        Assume bilevel image (black/white[=-1])

        Each strip: count run length
                    encode into modified hufman codes
                    swap bits
                    save in byte array
                    write to image file
        */

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BitSwapOutputStream bsos = new BitSwapOutputStream(baos);
        ModHuffmanOutputStream mhos = new ModHuffmanOutputStream(bsos);

        RLEOutputStream rlos =
            new RLEOutputStream(mhos, 3); // rgb = 3 bytes per sample code word (not needed here)

        for (int i = 0; i < maxrps; i++) {
          if ((y + i) == height) {
            break;
          } // last strip might have less rows
          rlos.setStartCodeWord(-1); // white run first
          for (int x = 0; x < width; x++) {
            rlos.write(image.getRGB(x, y + i));
          }
          rlos.flush(); // write padding after ever image row
        }
        rlos.close();

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream

        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeBModHufImage:\n\t" + e.getMessage());
    }
  }
Ejemplo n.º 9
0
  public BufferedImage getBufferedImage(int type, Color c) {

    BufferedImage image = null;
    float[] colComp = new float[3];
    c.getRGBColorComponents(colComp);
    double red = (double) colComp[0];
    double green = (double) colComp[1];
    double blue = (double) colComp[2];
    // System.out.println("blue, green, red = "+ blue +", " + green + ", " + red);

    double x = 0.0;
    double x2;
    switch (type) {
      case ScalarImage.TYPE_BYTE_RANDOM:
        {
          int numCol = 256;
          byte[] bBuf = new byte[numCol * 3];
          blue *= 255 * 4.;
          green *= 255 * 4.;
          red *= 255 * 4.;
          double delta = 1.0 / (double) (numCol + 1);
          int j = 0;
          for (int i = 0; i < numCol; i++) {
            if (i % 5 == 0) x = 0.7 * Math.random() + 0.3 * x;
            x2 = x * x;
            bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
            bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
            bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
            // x += delta;
          }
          IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false);
          // image = new
          // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm);
          byte[] idxBuffer = new byte[size];
          for (int i = 0; i < size; i++) {
            idxBuffer[i] = (byte) (clamp(f[i] * 255.));
          }
          DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size);
          int idxOffset[] = {0};
          int idxBits[] = {8};
          try {
            ComponentSampleModel idxSampleModel =
                new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset);
            WritableRaster rasterIdx =
                java.awt.image.Raster.createWritableRaster(
                    idxSampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(cm, rasterIdx, false, null);
          } catch (Exception e) {
            System.out.println("Exception caught while acquiring image:");
            System.out.println(e.getMessage());
          }
        }
        break;
      case BufferedImage.TYPE_BYTE_INDEXED:
        {
          int numCol = 256;
          byte[] bBuf = new byte[numCol * 3];
          blue *= 255 * 4.;
          green *= 255 * 4.;
          red *= 255 * 4.;
          double delta = 1.0 / (double) (numCol + 1);
          int j = 0;
          for (int i = 0; i < numCol; i++) {
            x2 = x * x;
            bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
            bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
            bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
            x += delta;
          }
          IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false);
          // image = new
          // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm);
          byte[] idxBuffer = new byte[size];
          for (int i = 0; i < size; i++) {
            idxBuffer[i] = (byte) (clamp(f[i] * 255.));
          }
          DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size);
          int idxOffset[] = {0};
          int idxBits[] = {8};
          try {
            ComponentSampleModel idxSampleModel =
                new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset);
            WritableRaster rasterIdx =
                java.awt.image.Raster.createWritableRaster(
                    idxSampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(cm, rasterIdx, false, null);
          } catch (Exception e) {
            System.out.println("Exception caught while acquiring image:");
            System.out.println(e.getMessage());
          }
        }
        break;
      case BufferedImage.TYPE_BYTE_GRAY:
        break;
      case BufferedImage.TYPE_3BYTE_BGR:
      default:
        byte[] byteBuffer = new byte[size * 3];
        blue *= 255 * 4.;
        green *= 255 * 4.;
        red *= 255 * 4.;

        int j = 0;
        for (int i = 0; i < size; i++) {
          x = f[i];
          x2 = x * x;
          /*
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * red ) * x * x + ( 4 * red - 255 ) * x);
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * green ) * x * x + ( 4 * green - 255 ) * x);
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * blue ) * x * x + ( 4 * blue - 255 ) * x);
          */
          byteBuffer[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
          byteBuffer[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
          byteBuffer[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
        }
        DataBufferByte dataBuffer = new DataBufferByte(byteBuffer, size * 3);
        int componentOffset[] = {0, 1, 2};
        int componentBits[] = {8, 8, 8};
        try {
          WritableRaster raster =
              java.awt.image.Raster.createWritableRaster(
                  new PixelInterleavedSampleModel(
                      DataBuffer.TYPE_BYTE, width, height, 3, width * 3, componentOffset),
                  dataBuffer,
                  new Point(0, 0));
          image =
              new BufferedImage(
                  new ComponentColorModel(
                      ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB),
                      componentBits,
                      false,
                      false,
                      ColorModel.OPAQUE,
                      DataBuffer.TYPE_BYTE),
                  raster,
                  false,
                  null);

        } catch (Exception e) {
          System.out.println("Exception caught while acquiring image:");
          System.out.println(e.getMessage());
        }
        break;
    }
    return image;
  }
Ejemplo n.º 10
0
  protected void f_unfuck(int[] pixel, final int count, final int iter) throws Exception {
    HuffmanTree tree = f_ht1;
    int pi = 0;
    int[] diffbuf = new int[64];
    int[] leaves = new int[64];
    int token = -1;
    int zeros = 0;
    int bitcount = 0;
    int sign = 0;
    int diff = 0;

    for (int j = 0; j < count; ++j) {
      tree = f_ht1;

      for (int i = 0; i < 64; ++i) {
        diffbuf[i] = 0;
        leaves[i] = -1;
      }

      for (int i = 0; i < 64; ++i) {
        try {
          while ((token = tree.find(f_stream.bits(1))) < 0) ;

          /*
          do
          {
          	final int dir = f_stream.bits(1);

          	System.err.println("  dir: " + dir);

          	token = tree.find(dir);
          }
          while ( token < 0 );
          */

          tree = f_ht2;

          // System.err.println("token: " + token);
          // leaves[i] = token;

          if (token == 0 && i > 0) break;

          if (token == 0xff) continue;

          zeros = token >> 4;
          bitcount = token & 15;
          i += zeros;

          if (bitcount == 0) continue;

          sign = f_stream.bits(1);
          diff = f_stream.bits(bitcount - 1);

          if (sign == 1) diff += 1 << (bitcount - 1);
          else diff += (-1 << bitcount) + 1;

          if (i < 64) diffbuf[i] = diff;
        } catch (Exception e) {
          System.err.println();
          System.err.println("--");
          System.err.println("iter: " + iter);
          System.err.println("i: " + i);
          // System.err.println("path: " + tree.findPath());
          System.err.println("token: " + token);
          System.err.println("--");
          System.err.println();

          e.printStackTrace();
          System.exit(1);
        }
      }

      diffbuf[0] += f_carry;
      f_carry = diffbuf[0];

      /*
      System.err.print("diffbuf: " );
      for ( int i = 0; i < 64; ++i )
      	System.err.print(diffbuf[i] + " ");
      System.err.println();
      System.exit(1);
      */

      for (int i = 0; i < 64; ++i) {
        if (f_pixelIndex++ % f_info.rawWidth == 0) f_base[0] = f_base[1] = 512;

        final int pii = pi + i;

        pixel[pii] = (f_base[i & 1] += diffbuf[i]);

        while (pixel[pii] < 0) pixel[pii] += 65536;
        while (pixel[pii] >= 65536) pixel[pii] -= 65536;

        // System.out.print(" " + pixel[pi + i]);
      }
      // System.out.println();

      pi += 64;
    }
  }
Ejemplo n.º 11
0
  public CaptureData decode() {
    f_cd = new CaptureData(f_info.width, f_info.height, FILTER_RGGB, 12);
    f_cd.setImageInfo(f_info);

    final int table = (f_info.table > 2) ? 2 : f_info.table;
    f_info.table = table;

    f_ht1 = new HuffmanTree(StaticTree.FIRST_TREE[table], 16);
    f_ht2 = new HuffmanTree(StaticTree.SECOND_TREE[table], 16);

    int black = 0;
    int black_count = 0;

    try {
      /*
       * Create fast reading stream from file.
       */
      final byte[] array = new byte[f_size];

      f_stream.seek(0);
      f_stream.readFully(array, 0, f_size);

      f_stream = SeekableInputStream.getInstance(f_info.order, array);

      /*
       * Begin the decompression by seeking to place of the good 10-bits.
       */
      int top = 0;
      int left = 0;

      switch (f_info.rawWidth) {
        case 2144:
          top = 8;
          left = 4;
          break; // G1
        case 2224:
          top = 6;
          left = 48;
          break; // EOS D30
        case 2376:
          top = 6;
          left = 12;
          break; // G2 or G3
        case 2672:
          top = 6;
          left = 12;
          break; // S50
        case 3152:
          top = 12;
          left = 64;
          break; // EOS D60
      }

      final int lowbits = f_canonHasLowbits();

      if (lowbits == 0) System.err.println("Yikes--we have low bit data!");
      if (lowbits == 1) System.err.println("Weird--we have uncompressed low-order bits first!");

      System.err.println("lowbits: " + lowbits);

      final int hiLocation = 540 + lowbits * f_info.rawHeight * f_info.rawWidth / 4;

      f_stream.seek(hiLocation);

      int[] pixel = new int[f_info.rawWidth * 8];
      int unfuck_iter = 0;

      System.err.println("Using decode table " + f_info.table + "...");
      System.err.println(
          "Beginning total decompression (at file offset "
              + hiLocation
              + ") ["
              + f_stream.getPosition()
              + "]...");

      for (int row = 0; row < f_info.rawHeight; row += 8) {
        final long _before_save = f_stream.getPosition();

        f_unfuck(pixel, f_info.rawWidth >> 3, unfuck_iter);

        final long _after_save = f_stream.getPosition();

        System.err.println(
            "Decompressed rows "
                + (row)
                + " through "
                + (row + 7)
                + " (file: "
                + _before_save
                + " - "
                + _after_save
                + ") iter "
                + unfuck_iter);
        ++unfuck_iter;

        if (lowbits != 0) {
          final long save = f_stream.getPosition();

          f_stream.seek(26 + row * f_info.rawWidth / 4);

          int pi = 0;

          for (int i = 0; i < f_info.rawWidth << 1; ++i) {
            int c = f_stream.read();

            for (int r = 0; r < 8; r += 2) {
              pixel[pi] = (pixel[pi] << 2) + ((c >> r) & 3);

              while (pixel[pi] >= 65536) pixel[pi] -= 65536;

              ++pi;
            }
          }

          f_stream.seek(save);
        }

        // DEBUG: StringBuffer buffer = new StringBuffer("pixel row " + row + ": "); for ( int pi =
        // 0; pi < f_info.rawWidth; ++pi ) buffer.append(pixel[pi] + " ");
        // System.out.println(buffer);

        for (int r = 0; r < 8; ++r) {
          for (int col = 0; col < f_info.rawWidth; ++col) {
            // DEBUG: System.out.println("  @@ pixel[" + (r * f_info.rawWidth + col) + "]: " +
            // pixel[r * f_info.rawWidth + col]);

            final int irow = row + r - top;

            if (irow >= 0 && irow < f_info.height) {
              if (col < left) {
                // System.out.println("  black (" + black_count + "): " + pixel[r * f_info.rawWidth
                // + col]);
                black += pixel[r * f_info.rawWidth + col];
                ++black_count;
              } else {
                final int i = irow * f_info.width + col - left;
                f_cd.setElem(i, pixel[r * f_info.rawWidth + col]);
              }
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    // DEBUG: for ( int y = 0; y < f_info.height; ++y ) { StringBuffer buffer = new
    // StringBuffer("irow " + y + ": "); for ( int x = 0; x < f_info.width; ++x )
    // buffer.append(f_cd.get(x,y) + " "); System.out.println(buffer.toString()); }

    System.err.println("total black: " + black + " (" + black_count + ")");

    // -- 3 March 2004 -- black = (black << BIT_REMAINDER) / ((f_info.rawWidth - f_info.width) *
    // f_info.height);
    black = black / ((f_info.rawWidth - f_info.width) * f_info.height);

    f_info.black = black;

    System.err.println("average black: " + black);

    f_blackScale();

    // f_averageGreens();

    return f_cd;
  }