MagickImage.Write from geoTiff to output stream with jp2 format

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
lavoyech
Posts: 6
Joined: 2016-06-01T08:22:26-07:00
Authentication code: 1151

MagickImage.Write from geoTiff to output stream with jp2 format

Post by lavoyech »

Hello,

For a geoTransform .NET project i want to generate an output stream in jp1 format from a geoTiff file.

I use ImageMagik to manage jp2.

My problem occurs in the final part of the process.

I load my geoTiff file in an instance of ImageMagick

Code: Select all

imgResult = new MagickImage(imageIdentifier + "_crop.tiff");
then further on process i set the response of my webservice

Code: Select all

                    // Context for response
                    WebOperationContext context = WebOperationContext.Current;

                    context.OutgoingResponse.ContentType = streamFormat;
                    context.OutgoingResponse.Headers.Add("Content-Disposition", string.Format("inline;filename={0}.{1}", featureid, context.OutgoingResponse.ContentType.Substring(context.OutgoingResponse.ContentType.LastIndexOf('/') + 1)));

                    // Create outPut stream with jpeg or jp2 format based on initial request
                    if (streamFormat == "image/jp2")
                    {
                        magick.Write(responseStream, MagickFormat.Jp2);
                    }
                    else
                    {
                        magick.Write(responseStream, MagickFormat.Jpeg);
                    }
                    
All is good with jpeg format.
All is jpg2 format if it has not been processed with GDAL utility

The problem occurs with jp2 format when my geoTiff file has been cropped by GDAL utility "gdal_translate" or "gdalwarp".

I can display the geoTiff with a viewer such as IrfanViewer or Windows Viewer. But when it's loaded on the browser the image i get is white.

Please find result from command line tiffInfo:

for ImageBase (ok):

Code: Select all

TIFFReadDirectory: Warning, Unknown field with tag 34264 (0x85d8) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34736 (0x87b0) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.
TIFF Directory at offset 0x1ca09a (1876122)
  Image Width: 900 Image Length: 811
  Resolution: 96, 96
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: JPEG
  Photometric Interpretation: RGB color
  YCbCr Subsampling: 2, 2
  Samples/Pixel: 4
  Rows/Strip: 811
  Planar Configuration: single image plane
  Tag 34264: 0.000970,-0.000006,0.000000,-73.722622,0.000005,-0.000666,0.000000,
-50.173928,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1.0000
00
  Tag 34735: 1,1,0,7,1024,0,1,2,1025,0,1,1,2048,0,1,4326,2049,34737,7,0,2054,0,1
,9102,2057,34736,1,1,2059,34736,1,0
  Tag 34736: 298.257224,6378137.000000
  Tag 34737: WGS 84|
  JPEG Tables: (289 bytes)
for ImageCrop (error):

Code: Select all

TIFFReadDirectory: Warning, Unknown field with tag 34264 (0x85d8) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34736 (0x87b0) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.
TIFF Directory at offset 0x8 (8)
  Image Width: 200 Image Length: 200
  Resolution: 96, 96
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Extra Samples: 1<assoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 10
  Planar Configuration: single image plane
  Tag 34264: 0.000970,-0.000006,0.000000,-73.722622,0.000005,-0.000666,0.000000,
-50.173928,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1.0000
00
  Tag 34735: 1,1,0,7,1024,0,1,2,1025,0,1,1,2048,0,1,4326,2049,34737,7,0,2054,0,1
,9102,2057,34736,1,1,2059,34736,1,0
  Tag 34736: 298.257224,6378137.000000
  Tag 34737: WGS 84|

the original geoTiff is created programatically from a stream and geotransform information calculate with metadata:

This is how i set basic tifftag (i could give more details about this process)

Code: Select all

            // Use ImageMagick to load stream and save as temp bmp file.
            // This API enable to manage jp2 formatted stream
            MagickImage img = new MagickImage(sourceStream);

            string tempFile = Path.GetDirectoryName(destinationPath) + "\\" + Path.GetFileNameWithoutExtension(destinationPath) + ".bmp";
            img.Write(tempFile);

            using (Bitmap bmp = new Bitmap(tempFile))
            {
            ...
            ...
                    else if (pixFormat == PixelFormat.Format32bppArgb)
                    {
                        tif.SetField(TiffTag.IMAGEWIDTH, bmp.Width);
                        tif.SetField(TiffTag.IMAGELENGTH, bmp.Height);
                        tif.SetField(TiffTag.BITSPERSAMPLE, 8);
                        tif.SetField(TiffTag.COMPRESSION, Compression.JPEG);
                        tif.SetField(TiffTag.JPEGQUALITY, 100);
                        tif.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
                        tif.SetField(TiffTag.ROWSPERSTRIP, bmp.Height);
                        tif.SetField(TiffTag.XRESOLUTION, bmp.HorizontalResolution);
                        tif.SetField(TiffTag.YRESOLUTION, bmp.VerticalResolution);
                        tif.SetField(TiffTag.SAMPLESPERPIXEL, 4);
                        tif.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
                        tif.SetField(TiffTag.SAMPLEFORMAT, 1);
                        tif.SetField(TiffTag.DATATYPE, 2);
                    }
              ...
              ...
is someone already use image magik is those kind of process?

Best regards,
Christophe.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickImage.Write from geoTiff to output stream with jp2 format

Post by snibgo »

lavoyech wrote:The problem occurs with jp2 format when my geoTiff file has been cropped by GDAL utility "gdal_translate" or "gdalwarp".
Those utilities are not part of ImageMagick.

If ImageMagick is doing something wrong, please say what it is, and provide examples.

Please also say what version of IM you are using, on what platform.
snibgo's IM pages: im.snibgo.com
lavoyech
Posts: 6
Joined: 2016-06-01T08:22:26-07:00
Authentication code: 1151

Re: MagickImage.Write from geoTiff to output stream with jp2 format

Post by lavoyech »

Hello,

I'm using version Magick.NET-Q8-AnyCPU 7.0.1.500.

I know GDAL is not part of ImageMagick. I just present my problem and my code which are using both.
And may be someone will have already experienced such a case.

I'm not sure the problem comes from ImageMagick but more from me ;)

basically i have two geoTiff File. When i load them in an instance of IM and then write them to stream with jp2 format i have one goog image and the otherone is white.

I can't or i don't find how to attached files so i precised in my prvious post geoTiff informations for both tiff files.

Best regards.
lavoyech
Posts: 6
Joined: 2016-06-01T08:22:26-07:00
Authentication code: 1151

Re: MagickImage.Write from geoTiff to output stream with jp2 format

Post by lavoyech »

I identify the problem.

In the beginning of my process i use ImageMagick to load stream and then write a temp bmp file.

Then I built my geoTiff from this bmp file which is loading in an instance of Bitmap .NET object.

If i do the same thing but same the temporary file as jpeg the result is ok.

I don't really understand but maybe it's related to the quality difference in between jpeg and bmp files...
Post Reply