Support for old style JPEG compression specification

Hi Team,

I have a requirement to merge and compress an old style jpeg and 2 tiff files. Is Aspose .NET or Java API support old style JPEG compression? If so can you please share the code sample for that to give a try. So that I can propose the product to the customer. It is top urgent.

I have attached the files.There is constraint on size it should around 50 to 60 KB and should open in all the viewers. The specification is

A tag-based file format for storing and interchanging raster images for clearing system, which is tailored to wrap/unwrap from/to three images (two B/W CCITT group 4 compressed tiff images and one 8-bit grayscale JPEG compression image) to/from one multi-page (here it is 3-pages) tiff image. The purpose of this appendix is to generate a TIFF 6.0 compatible multi-page image which can be manipulated by some advanced image software who can deal with extension definition of TIFF format as well as to provide a compact and management easy means for clearing system of such images.

Only “II” format is supported in TIFF.
• The offset of IFD in TIFF Header is 1st IFD of cheque.
• All encoded image data will be located by the StripOffsets tag. The actual data can be put in anywhere within the TIFF file.
• All B/W image can be encoded by the standard encoding algorithm defined in TIFF 6.0 specification. CCITT T.6 is recommended in this specification.
• All 8-bit gray scale images are encoded by JPEG. And baseline algorithms are recommended in this specification. The full JPEG stream should be retained in this file, either in JPEG Interchange Format (JIF) or JPEG File Interchange Format (JFIF). BCSIS TIFF only specifies the tags pointing the data within the compressed JIF or JFIF stream.
Thank you.

Arpitha

Hi Arpitha,


Thank you for considering Aspose products.

As per your detailed requirement specification, we think Aspose.Imaging can achieve your goals. Please review the below provided code snippet that concatenates your provided sample images into one Multi-Page Tiff image with CCITT4 compression. Also attached is the resultant image (in an archive) for your review.

In case you wish to give the below code a try on your end, please download the latest version of Aspose.Imaging for .NET 2.5.0, and try it with other samples of yours.

C#

//Create an array of files to be concatenated
string[] files = new string[] { “911123112F.jpg”, “911123112F.tif”, “911123112B.tif” };

//Create an instance of TiffOptions for the resultant image
TiffOptions outputSettings = new TiffOptions();

//Set BitsPerSample
outputSettings.BitsPerSample = new ushort[] { 1 };

//Set Compression mode
outputSettings.Compression = TiffCompressions.CcittFax4;

//Set Source, where the file will be saved
outputSettings.Source = new FileCreateSource(myDir + “output.tiff”, false);

//Set initial page dimensions
const int newWidth = 500;
const int newHeight = 500;

//Create a new TiffImage with instance of TiffOptions & size
using (TiffImage output = (TiffImage)Image.Create(outputSettings, newWidth, newHeight))
{
//Iterate over the files in the file array
foreach (string file in files)
{
//Get the file extension
string extension = Path.GetExtension(file);
//Check if a file is Tiff
if (extension.Equals(“.tiff”, StringComparison.InvariantCultureIgnoreCase) || extension.Equals(“.tif”, StringComparison.InvariantCultureIgnoreCase))
{
//Load tiff file into an instance of TiffImage
TiffImage ti = (TiffImage)Image.Load(myDir + file);
//Iterate over all the frames of loaded tiff image
foreach (TiffFrame frame in ti.Frames)
{
//Create an instance of TIffFrame and copy active frame of source image
TiffFrame CopiedFrame = TiffFrame.CopyFrame(frame);
// Add copied frame to destination image
output.AddFrame(CopiedFrame);
}
}
//Check if file is a Jpeg image
else if (extension.Equals(“.jpeg”, StringComparison.InvariantCultureIgnoreCase) || extension.Equals(“.jpg”, StringComparison.InvariantCultureIgnoreCase))
{
//Load Jpeg image into an instance of RasterImage
RasterImage ri = (RasterImage)Image.Load(myDir + file);
//Create a new frame from the RasterImage, while passing an object of TiffOptions
TiffFrame frame = new TiffFrame(ri, outputSettings);
//Add the newly created frame to the output Tiff image
output.AddFrame(frame);
}
}
//By default, newly created Tiff images have an empty frame
//We will remove this empty frame from the image
if (output.Frames.Length > 1)
{
//Empty frame is ActiveFrame therefore it cannot be deleted,
//First change the ActiveFrame to the next frame in collection
output.ActiveFrame = output.Frames[1];
//Remove the empty frame
output.RemoveFrame(0);
}
//Save the result
output.Save();
}


Please feel free to write back in case you have any concerns or questions.

Hi Team,


We tried above code.JPEG is converted to black and white and merging into Tiff Frame. But the requirement is to convert the JPEG to Tiff as it is without converting to black and white. I appreciate your help to achieve the exact requirement, based on which i can propose the product to cutomer.

Thanks a lot for the support
Arpitha

Hi Arpitha,


Thank you for writing back. Please try the code snippet as follow.

C#

//Create an array of files to be concatenated
string[] files = new string[] { “911123112F.jpg”, “911123112F.tif”, “911123112B.tif” };

//Create an instance of TiffOptions for the resultant image
TiffOptions outputSettings = new TiffOptions();

//Set BitsPerSample
outputSettings.BitsPerSample = new ushort[] { 8, 8, 8 };

//Set Compression mode
outputSettings.Compression = TiffCompressions.Lzw;
//Set Photometric mode
outputSettings.Photometric = TiffPhotometrics.Rgb;

//Set Source, where the file will be saved
outputSettings.Source = new FileCreateSource(myDir + “output.tiff”, false);

//Set initial page dimensions
const int newWidth = 500;
const int newHeight = 500;

//Create a new TiffImage with instance of TiffOptions & size
using (TiffImage output = (TiffImage)Image.Create(outputSettings, newWidth, newHeight))
{
//Iterate over the files in the file array
foreach (string file in files)
{
//Get the file extension
string extension = Path.GetExtension(file);
//Check if a file is Tiff
if (extension.Equals(".tiff", StringComparison.InvariantCultureIgnoreCase) || extension.Equals(".tif", StringComparison.InvariantCultureIgnoreCase))
{
//Load tiff file into an instance of TiffImage
TiffImage ti = (TiffImage)Image.Load(myDir + file);
//Iterate over all the frames of loaded tiff image
foreach (TiffFrame frame in ti.Frames)
{
//Create an instance of TIffFrame and copy active frame of source image
TiffFrame CopiedFrame = TiffFrame.CopyFrame(frame);
// Add copied frame to destination image
output.AddFrame(CopiedFrame);
}
}
//Check if file is a Jpeg image
else if (extension.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase) || extension.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase))
{
//Load Jpeg image into an instance of RasterImage
RasterImage ri = (RasterImage)Image.Load(myDir + file);
//Create a new frame from the RasterImage, while passing an object of TiffOptions
TiffFrame frame = new TiffFrame(ri, outputSettings);
//Add the newly created frame to the output Tiff image
output.AddFrame(frame);
}
}
//By default, newly created Tiff images have an empty frame
//We will remove this empty frame from the image
if (output.Frames.Length > 1)
{
//Empty frame is ActiveFrame therefore it cannot be deleted,
//First change the ActiveFrame to the next frame in collection
output.ActiveFrame = output.Frames[1];
//Remove the empty frame
output.RemoveFrame(0);
}
//Save the result
output.Save();
}

Please note, the above code saves the resultant TIFF image with RGB colors and LZW compression. Unfortunately, the JPEG compression modes are not supported for saving the results. However, images having any JPEG compression mode (OLD or NEW) can be loaded and inter-converted.

Please feel free to write back in case you have further concerns.

Hi Team,

We have used above code and we got result which has converted JPG with out changing its color. But our main concern is SIZE 30 KB files after merging coming as 236 KB. Can you please help us to resolve the issues to compress size with in 50 to 60 KB.
Thanks a lot for support
Arpitha

Hi Arpitha,


Thank you for writing back.

I am afraid, it is hard to bring the resultant file size below 60 KB when result has to be saved in RGB color mode. However, the best compression ratio is offered by the CCIITT4 compression mode, but unfortunately you have to sacrifice the colors for it.

In order to look further into this matter, we have logged an investigative ticket IMAGING-34292 in our bug tracking system, and have requested the core development team to share their opinion on your requirement of low file size with RGB color mode. Please spare us little time to properly analyze the scenario on our end. In the meanwhile, we will keep you posted with updates in this regard.

Hi Arpitha,


Thank you for your patience with us.

We have investigated several settings to bring the resultant file size below 60KB limit. Please check the below provided approach offering the best compression, however source colors are slightly dithered.

Please note, we have just changed/updated the code segment to set settings for TiffOptions. Rest of the code is exactly the same as delivered in previous posts.

C#

string[] files = new string[] { “911123112F.jpg”, “911123112F.tif”, “911123112B.tif” };

//Create an instance of TiffOptions for the resultant image
TiffOptions outputSettings = new TiffOptions();

//Set BitsPerSample
outputSettings.BitsPerSample = new ushort[] { 4 };

//Set Compression mode
outputSettings.Compression = TiffCompressions.Lzw;

//Set Photometric mode
outputSettings.Photometric = TiffPhotometrics.Palette;

//set graycale palette
outputSettings.Palette = ColorPaletteHelper.Create4BitGrayscale(false);

//Set Source, where the file will be saved
outputSettings.Source = new FileCreateSource(myDir + “output.tiff”, false);

//Set initial page dimensions
const int newWidth = 500;
const int newHeight = 500;

//Create a new TiffImage with instance of TiffOptions & size
using (TiffImage output = (TiffImage)Image.Create(outputSettings, newWidth, newHeight))
{
//Iterate over the files in the file array
foreach (string file in files)
{
//Get the file extension
string extension = Path.GetExtension(file);
//Check if a file is Tiff
if (extension.Equals(".tiff", StringComparison.InvariantCultureIgnoreCase) || extension.Equals(".tif", StringComparison.InvariantCultureIgnoreCase))
{
//Load tiff file into an instance of TiffImage
TiffImage ti = (TiffImage)Image.Load(myDir + file);
//Iterate over all the frames of loaded tiff image
foreach (TiffFrame frame in ti.Frames)
{
//Create an instance of TIffFrame and copy active frame of source image
TiffFrame CopiedFrame = TiffFrame.CopyFrame(frame);
// Add copied frame to destination image
output.AddFrame(CopiedFrame);
}
}
//Check if file is a Jpeg image
else if (extension.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase) || extension.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase))
{
//Load Jpeg image into an instance of RasterImage
RasterImage ri = (RasterImage)Image.Load(myDir + file);
//Create a new frame from the RasterImage, while passing an object of TiffOptions
TiffFrame frame = new TiffFrame(ri, outputSettings);
//Add the newly created frame to the output Tiff image
output.AddFrame(frame);
}
}
//By default, newly created Tiff images have an empty frame
//We will remove this empty frame from the image
if (output.Frames.Length > 1)
{
//Empty frame is ActiveFrame therefore it cannot be deleted,
//First change the ActiveFrame to the next frame in collection
output.ActiveFrame = output.Frames[1];
//Remove the empty frame
output.RemoveFrame(0);
}
//Save the result
output.Save();
}

Note: The image compression in any case can be performed by reducing the source data size, regardless of the compression algorithm used.

Hi again,


Adding more to my previous response, Aspose.Imaging for .NET API will be offering Adobe Deflate compression mode that can be used to further compress the resultant image to under 50KB file size. The aforesaid enhancements will be available with v2.6.0 of Aspose.Imaging for .NET, that is scheduled for publication by the end of August 2014.

Please feel fee to write back in case you have any questions or concerns.

Hi Team,
We have tried with above code and we got expected result. But with some images we are getting this exception “Cannot decode strip 0. Details: There is no more data to read.” . Please help us to track this issue.
Please find files above.
Thanks,
Arpitha

Hi Arpitha,


Thank you for the confirmation on the previously shared solution.

Regarding the recent concerns, I believe you are getting the said exception on 4.tif from your provided sample archive. Please note, the aforesaid sample is empty with 0KB file size; it does not contain any data so the Aspose.Imaging API will reject it from processing. You may confirm this by opening the 4.tif sample in any of the image viewing applications.

In case you have further concerns, please feel free to write back.

Hi Team,


I tried with different images, still the same exception. Find the exception below and attached the images. Please suggest

Unhandled Exception: Aspose.Imaging.Exceptions.ImageFormats.TiffImageException: Cannot decode strip 0. Details: There is no more data to read.
at ↨.⌂.:slight_smile:.:slight_smile:.(String )
at ↨.⌂…()
at ↨.⌂.:slight_smile:.:heart:.()
at ↨.⌂.:slight_smile:.:slight_smile:.(Rectangle )
at ↨.⌂.:slight_smile:.:slight_smile:(Rectangle )
at ←.↑.(Rectangle , → , :slight_smile: , Int32 , Int32 )
at ↨.⌂.(TiffStream , Rectangle , IPartialPixelLoader )
at ▬.♫.LoadPartialPixels(Rectangle , IPartialPixelLoader )
at Aspose.Imaging.RasterImage.:slight_smile:.:slight_smile:(Rectangle )
at ←.↑.(Rectangle , → , :slight_smile: , Int32 , Int32 )
at Aspose.Imaging.RasterCachedImage.SavePixelsInternal(Rectangle rectangle, Color[] pixels)
at Aspose.Imaging.RasterCachedImage.CacheData()
at Aspose.Imaging.FileFormats.Tiff.TiffImage.CacheData()
at Aspose.Imaging.DataStreamSupporter.Save(Stream stream)
at Aspose.Imaging.DataStreamSupporter.Save()
at Aspose.Imaging.Image.Save()
at ASPOSEDLL.Program.Main(String[] args)




Hi Arpitha,


Thank you for sharing the samples.

We are able to replicate the said exception while using the latest version of Aspose.Imaging for .NET 2.5.0 against the recently provided samples. The problem seems to related to the Tiff format, however, we are not sure at the moment what could have caused the exception. In order to further investigate the matter, we have logged the problem in our bug tracking system under the ticket IMAGING-34322. Please spare us little time to properly analyze the problem cause. In the meanwhile, we will keep you posted with updates in this regard.

Hi Team,


Any update on the issue?

Thanks,
Arpitha

Hi Arpitha,


I am afraid, the problem logged earlier as IMAGING-34322 is pending for analysis and in the queue with other priority tasks. However, we have recorded a note for the concerned development team member to escalate the process to provide an insight of the problem. As soon as we receive more updates in this regard, we will post here for your kind reference.

Hi Arpitha,


Thank you for your patience with us.

We have investigated the problem logged earlier as IMAGING-34322. The analysis shows that the 018100050B.tif has strip byte counts value equal to 0 probably because the TIFF has been created by some software which does not know which value should be put into that value field. The typical decoders emit a warning message when they encounter such strip byte count value and try to estimate the correct value. Since TIFF may have several frames and decoders do not know for sure the data length so the estimation may lead to incorrect rendering of the data and broken frames. We have formulated some algorithm which helps to estimate the broken strip byte counts used in the most encoders. This fix has been implemented for Aspose.Imaging for .NET 2.6.0. The aforesaid release is currently in testing phase, and we hope to publish it within the upcoming week.

The issues you have found earlier (filed as IMAGING-34322) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi Arpitha,


It is just to update you that Aspose.Imaging for .NET 2.6.0 has been published. You may now use the Adobe Deflate compression method to compress the previously shared samples to a file having size under 50 KB. Please use the following TiffOptions settings if you wish to give the new compression method a try on your end. Also attached is the resultant Tiff produced on my machine.

C#

//Create an instance of TiffOptions for the resultant image
TiffOptions outputSettings = new TiffOptions();

//Set BitsPerSample
outputSettings.BitsPerSample = new ushort[] { 4 };

//Set Compression mode
outputSettings.Compression = TiffCompressions.AdobeDeflate;

//Set Photometric mode
outputSettings.Photometric = TiffPhotometrics.Palette;

// set graycale palette
outputSettings.Palette = ColorPaletteHelper.Create4BitGrayscale(false);

Please note, you may observe some change in the color schema as a side effect of this method.

The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.