ngageoint / tiff-java

Tagged Image File Format Java Library
https://ngageoint.github.io/tiff-java
MIT License
72 stars 33 forks source link

Exception when trying write tiff-file with setX- and setYResolution with single long value (not a List) #26

Closed Volfer closed 5 years ago

Volfer commented 5 years ago

Version Information:

Expected Results:

Observed Results:

Output:

Steps to Reproduce:

  1. Step One

    • long xResolution = 254;
    • long yResolution = 254;
  2. Step Two ...

    • fileDirs.setXResolution(xResolution);
    • fileDirs.setYResolution(yResolution); ...
  3. Step Three newImage = new TIFFImage(); writeTiff(file, newImage);

Relevant Code:

// Code to reproduce the problem?
  long xResolution = 254;
  long yResolution = 254;

    newRaster=new Rasters(18, 11, 1, 16, TiffConstants.SAMPLE_FORMAT_UNSIGNED_INT);
      fileDirs=new FileDirectory();

      int rowsPerStrip = newRaster.calculateRowsPerStrip(TiffConstants.PLANAR_CONFIGURATION_CHUNKY);
      fileDirs.setImageWidth(inpWidth);
      fileDirs.setImageHeight(inpHeight);
      fileDirs.setBitsPerSample(bitsPerSample);
      fileDirs.setSamplesPerPixel(samplesPerPixel);
      fileDirs.setSampleFormat(TiffConstants.SAMPLE_FORMAT_UNSIGNED_INT);
      fileDirs.setRowsPerStrip(rowsPerStrip);
      fileDirs.setResolutionUnit(TiffConstants.RESOLUTION_UNIT_INCH);
      fileDirs.setXResolution(xResolution);
      fileDirs.setYResolution(yResolution);
      fileDirs.setPhotometricInterpretation(TiffConstants.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO);
      fileDirs.setPlanarConfiguration(TiffConstants.PLANAR_CONFIGURATION_CHUNKY);
      fileDirs.setCompression(TiffConstants.COMPRESSION_NO);
      fileDirs.setWriteRasters(newRaster);

      int tmpCount = 0;
      for(int y = 0; y < inpHeight; y++){
        for(int x = 0; x < inpWidth; x++){
          newRaster.setFirstPixelSample(x, y, inpPixVals[tmpCount]);
          tmpCount++;
        }
      }
      newImage = new TIFFImage();
      newImage.add(fileDirs);

      File file = new File("d:\\test.tif");
      TiffWriter.writeTiff(file, newImage);

Test Files:

Additional Information:

bosborn commented 5 years ago

Made some fixes on the develop branch. The xResolution and yResolution are now set as rationals ("Two LONGs: the first represents the numerator of a fraction; the second, the denominator."). When you set the value as a single long, a denominator of 1 is added.

This test writes and reads the created tiff.

Volfer commented 5 years ago

Good solution, thanks. Yesterday I've tried to set resolutions as a list of two long values (255, 1) and it worked, but some Image-soft also shows resolution as two values. And now everything is ok, the rational values exactly what is needed.