Image quality is lost on manipulating JPEG Tiff Compressed files (C# .NET)

Can you please share the demo code, which you have used for this ?
did you tried with JPEG compression type?

@ankitraman,

We have used the same sample code that have been previously shared with us from your end.

SourceCodeConsole.zip (1.1 KB)

@mudassir.fayyaz ,
I checked with this code, and for first 2 or 3 times rotation , it rotate all the pages within 30 seconds but after that when I goes to rotate these files then it is giving “SystemOutOfMemory” exception.

Also I updated you already that it is taking more time while calling the Save() method.

My system configuration are below:

Processor : Intel® Core™ i7 CPU@2.60Hz 2.59Hz
RAM: 16GB
System Type: 64-bit OS x64-based processor
Windows edition: Windows 10 Pro
Aspose.Imaging Version : 19.11.0.0 for .NET
.NET Framework version : 4.6.2

@ankitraman,

Thank you for sharing the feedback with us. I have added the details in our issue tracking system and will share good news with you as soon as possible.

@mudassir.fayyaz ,
Any update on it ?

@ankitraman,

I have verified from our issue tracking system and like to share that the concerned IMAGINGNET-3666 is likely to get fixed in upcoming Aspose.Imaging for .NET 20.3. Once the product will be shared online, I will also share the sample code as well that will be compatible to work in your environment using latest version.

@mudassir.fayyaz,
By when we get Aspose.Imaging for .NET 20.3 ?

@ankitraman,

Aspose.Imaging for .NET 20.3 is expected to be available by end of March 2020. You will be notified once the concerned issues will be fixed.

Okay. thank you.

@ankitraman,

We have Aspose.Imaging for .NET 20.3 available publicly now. The problem with slowdown described by you when performing operations on a large Tiff image was eliminated in IMAGINGNET-3656. In IMAGINGNET-3656, a memory leak was eliminated, leading to subsequent fragmentation of the image processing area, which led to a slowdown.
You will now be able to receive an update from IMAGINGNET-3656 in present Aspose.Imaging for .NET 20.3.

We also like to share that in spite of the fact that the memory leak was fixed, the fact remains that the data of all the pages of the large Tiff image get into the memory during processing, so there is a large memory consumption (up to 2Gb for you 10Mb Tiff image). To avoid this, we supported the batch mode of processing multi-page images. It allows you to get rid of storing in memory the data of all pages of a multi-page Tiff image, realizing the idea of “Loading a page into memory from an input stream --> processing a page (for example, a rotation operation) --> exporting a page to an output stream --> freeing memory --> loading the next page, and so on”.

Please try using the following batch mode implementation

using (TiffImage tiffImage = (TiffImage)Image.Load("10MB_Tif.tif"))
{
    // Set batch operation for pages
    tiffImage.PageExportingAction = delegate(int index, Image page)
         {
             // Fires garbage collection to avoid unnecessary garbage storage from previous pages
             GC.Collect();

             ((RasterImage)page).Rotate(90);
         };

    tiffImage.Save("rotated.tif"); /* or export through tiffImage.Save("rotated.tif", new TiffOptions(TIFF_EXPECTED_FORMAT))*/

    /* Attention! In batch mode all pages will be released in this line!
    If you want to further perform operations on the original image, you should reload it from the source to another instance. */
}

@ankitraman,

The problem with slowdown described by you when performing operations on a large Tiff image was eliminated in IMAGINGNET-3656. There was a memory leak was eliminated, leading to subsequent fragmentation of the image processing area, which led to a slowdown.

You can please try using Aspose.Imaging for .NET 20.3 on your end. In spite of the fact that the memory leak was fixed, the fact remains that the data of all the pages of the large Tiff image get into the memory during processing, so there is a large memory consumption (up to 2Gb for your 10Mb Tiff image). To avoid this, we have supported the batch mode of processing multi-page images. It allows you to get rid of storing in memory the data of all pages of a multi-page Tiff image, realizing the idea of “Loading a page into memory from an input stream --> processing a page (for example, a rotation operation) --> exporting a page to an output stream --> freeing memory --> loading the next page, and so on”.

This way you will also be able to manage well and using following example of batch mode/

using (TiffImage tiffImage = (TiffImage)Image.Load("10MB_Tif.tif"))
{
    // Set batch operation for pages
    tiffImage.PageExportingAction = delegate(int index, Image page)
         {
             // Fires garbage collection to avoid unnecessary garbage storage from previous pages
             GC.Collect();

             ((RasterImage)page).Rotate(90);
         };

    tiffImage.Save("rotated.tif"); /* or export through tiffImage.Save("rotated.tif", new TiffOptions(TIFF_EXPECTED_FORMAT))*/

    /* Attention! In batch mode all pages will be released in this line!
    If you want to further perform operations on the original image, you should reload it from the source to another instance. */
}

@mudassir.fayyaz ,
I have tried this, no improvement yet in terms of performance.
still it is taking 30 mins to rotate all whereas when I did same operation with Microsoft libraries, it took 4 mins 40 seconds since in my case 4 mins 30 seconds is also not a proper solution for only 50 pages so I moved to Aspose.Imaging for better result but here it is taking 8x time than microsoft libraries.

Is there any way so that we could do this with JPEG, TIFF and other formats that support lossless transformations ?

@ankitraman,

I have associated the information in our issue tracking system and will get back to you with feedback as soon as it will be shared. You have shared the feedback after 3 months of our suggestion. Can you please confirm which version you have now tried. Please try latest version on your end.

@mudassir.fayyaz ,
Earlier, we have put this issue on backlog after discussing with client and now we picked it up for our August’s release, so I checked it now.

I have tried with Aspose.Imaging,20.6(dlls_Only),I think this is the latest ones which delivered by your team.

After searching more on google, I found that there is one other LEADTOOLS SDK library available and by this we can speed things considerably if we perform lossless rotation without decoding and re-encoding the pages.

Please see example of same operation by LEADTOOLS SDK below:

    RasterCodecs codecs = new RasterCodecs();

CodecsImageInfo info = codecs.GetInformation(inputFile, true);

for (int i = 1; i <= info.TotalPages; i++)

{

   // The CompactFile method splits the pages without re-encoding them

   codecs.CompactFile(inputFile, tempFile1, 1, i, false, 0, 1, false, 0, CodecsSavePageMode.Overwrite, false, false);

   // The Transform method preforms lossless rotation

   codecs.Transform(tempFile1, tempFile2, CodecsTransformFlags.Rotate90, 0, null);

   // The CompactFile method is used again to re-group the pages after they're transformed

   codecs.CompactFile(tempFile2, outputFile, 1, 1, false, 0, i, false, 0, CodecsSavePageMode.Insert, false, false);

   File.Delete(tempFile1);

   File.Delete(tempFile2);

}

@ankitraman,

I have associated the information in our issue tracking system and we will get back to you with feedback as soon as possible.

OK, Thank you for your consistent reply.

@ankitraman,

You are welcome. We will update you as soon as they will be shared.

1 Like

@ankitraman

We have performed verifications using Aspose.Imaging for NET on our end. Here are the results of measuring the performance for the attachment:“10MB_Tif.tif” and several others on my side, for Aspose.Imaging 20.6 and 20.12 from Nuget:
tiff_rotate_measures.png

Testing Environment:
CPU Intel Core i7-8700 3.2GHz
RAM 32Gb
System Type: 64-bit OS x64-based processor
Windows edition: Windows 10 Pro
Aspose.Imaging Version : 20.6.0.0 for .NET
.NET Framework version : 4.7.2

From the results, you can see that TIFF images up to 100 pages rotate in less than a minute. A book in TIFF format of 1,790 pages rotated in almost 16 minutes. On other machines, these results will be proportional to their performance, this has been tested repeatedly in practice on our CI servers.

Please verify the [Sample Example Project] that we have created specifically to test this case. The archive does not contain only a test 1790 page TIFF image due to its large size (about 150 megabytes).

Can you please try this and share if you are still facing such a long time to complete these operations, since we cannot reproduce the case when a 100-page image is rotated for more than one minute.

Also, regarding the table of results above in shared image, we would like to note several recommendations:

  • Longer code execution in non licensed mode;
  • For turns to right angles, it is recommended to use: RasterCachedMultipageImage.RotateFlipAll operation, because it is much faster than RasterImage.Rotate operation
  • For multi-page images, it is recommended to use batch export. This will allow you to economically use RAM without compromising performance. Please read more about batch export mode over this documentation link.
  • For large multi-page images, it is highly recommended to use batch export as the only option.

About your example by LEADTOOLS SDK:

The Aspose.Imaging library also doesn’t have unnecessary intermediate codec calls, for any image format. The decode operation is called once when the image is loaded, then the bitmap data is stored as pixels. When exporting, the encoding operation is called once.

Please share, if we may assist you further in this regard.