C# PDF compression / recompress JBIG2 to JPEG - c#

I have PDF compressed with JBIG2. How can I recompress it to JPEG or any other compression algorithms?
I want to use open source solution like Itextsharp/PDFSharp or any other c# .net open source project.

You would need to decompress the image data, convert it from 1 bit per component to 8 bits per component, then apply JPEG compression.
But it's a bit unusual to convert monochrome images to color. This should increase the size actually. JBIG2 is pretty good for many monochrome images, why do you want to use JPEG compression on it?

Related

File size converting pdf to tiff

I'm using ghostscriptSharp to convert PDF files to TIFF files for faxing. The PDF files sometimes contain photocopies of receipts.
I'm using the tiffg3 driver with a height x width of 400 x 400. I've noticed that the PDFs that contain photocopies tend to expand in size when converting to TIFFs, while the ones without those shrink in size. A typical increase that I'm seeing is going from 1 MB to 25 MB.
I've tried adding compression to the TIFF, but then the fax process can't read it. Is there a way to reduce the output size in ghostscriptSharp without reducing the resolution?
Creating a bitmap, even a low resolution monochrome bitmap, is likely to be larger than a vector-based description language.
Consider:
(Hello World) Tj
That's 16 bytes in a PDF file, and it doens't change if you change the font size. If you turn it into a bitmap, even at low resolution and compressed, it probably exceeds that size.
That's why rendering a page description language to a bitmap produces larger files and is one of the reasons for using a page description language for printing, instead of sending large bitmaps around.
The tiffg3 and tiffg4 devices in Ghostscript only produce monochrome output, because that's all you can encode with G3 and G4 encoding. TIFF G3 is already compressed using the Fax CCITT group 3 compression scheme (Group 3 = g3). If you try to compress that using some other scheme, then your fax software wont be able to read it.
You could try using CCITT Group 4 fax compression instead (the tiffg4 device) but if that doesn't help then basically that's what you get. Your only other option is to create the TIFF at a lower resolution. You don't say what resolution you are currently using. Fax normally supports 3 resolutions; 408x391, 204x196 and 204x98. If you are using superfine (408x391) then you could switch to a lower resolution.
I'm at a loss to see why this is a problem since you are sending the files by fax anyway, why do you care how large an intermediate TIFF file you get ?
If compression won't work and you can't reduce resolution, then the only remaining option would be color depth. It's plausible that the conversion could be using more colors when a photocopy is attached (because of gradients in shadows, or the particular color of the paper, or whatever); yet the receipt might be totally readable without all the colors (as long as the "ink" shows up as distinct from the "paper").
If your conversion tool has a setting for selecting a color depth, tinkering with that is likely your best bet.
If your toolkit allows encoding options, for faxing, your best bet will be to produce a bitonal (black and white) tiff with Group 4 encoding. The downside of that compression scheme is that the more "gray" you have (typical with color pictures converted to grayscale), the bigger your file will be, otherwise, for most things, the compression ratio will be just fine.

Can we have a multi-page JPEG image?

Can we have a multi-page JPEG image?
I have a TIFF image file with multiple pages, but it's too big and first thing comes to mind is to change it to JPEG format, however in JPEG I can see only first page. Therefore I realized only TIFF format allows multiple images in one file. Is that true?
Now I tried to apply different EncoderParameters to reduce the size of TIFF file but no luck. Has someone worked on this issue before? How did you manage to reduce the size of TIFF image?
Encoder.Quality does not seem to work with TIFF at all.
EncoderValue.CompressionLZW is the best option to reduce the size, but I still want to reduce the size more.
Changing dpi to 50 reduced the size, but that made image too blurry.
Thanks for any help.
JPEG is technically a image compression, the JPEG file type is actually JFIF which does not support multi-frames. TIFF is an image container that does support the JPEG compression algorithm though, so you should be able to save the TIFF file as small as saving each page as a seperate JPEG added up.
Here is a stackoverflow post that goes into more detail on how to achieve this:
Create multipage Tiff with JPEG compression .NET

TIFF compression using LZW

Is there a class in C# library which does LZW compression on TIFF images. I know there is a compression scheme inviolving LZW being present, but using that doesnt decrease the file size whatsoever. Is there any thing that Im assuming wrong? Please correct me if I am.
Because LZW is loseless compression, you can compress TIFF images or any other kind of data using the same way. In C# you can use the SharpLZW library.
--EDIT (1)--
If you want to produce a TIFF file with embedded LZW compression respecting the TIFF specification look at section 13 of the specification.
--EDIT (2)--
There was a patent but it is now expired.

Convert TIFF LZW to CCITT

This is a question about TIFF and compression. I have hundreds of LZW compressed tiff images. I wonder, is it possible to convert those to CCITT T.6? Is there some API? Please help.
LZW compression can be used to compress almost any image. CCITT T.6 can compress only bilevel (black and white) images.
If your images are bilevel ones and they are compressed with LZW compression then you can recompress them using tiffcp utility (comes with LibTiff.Net library, free, source code available).
If your images are full-color ones then you will have to convert them to bilevel first. One of my answers contains sample code for such conversion.

resize picture c# for web

Goal:
I have lots of pictures in many sizes (both dimensions and file size)
I'd like to convert these files twice:
thumbnail-size pictures
pictures that will look OK on a web page and will be as close to a full screen as possible... and keeping the file size under 500KB.
HTML Questions:
A. What is the best file format to use (jpg, png or other) ?
B. What is the best configuration for web ... as small as possible file size with reasonable quality?
C# Questions
A Is there a good way to achieve this conversion using C# code (if yes, how)?
Try the code in this small C# app for resizing and compressing the graphics. I have reused this code for use in an ASP.NET site without too much work, hopefully you can make use of it. You can run the app to check quality fits your needs etc.
http://blog.bombdefused.com/2010/08/bulk-image-optimizer-in-c-full-source.html
You can pass the image twice, specifying dimensions for a thumbnail, and then again for your display image. It can handle multiple formats (jpg, png, bmp, tiff, gif), and reduce file size significantly without loosing noticeable quality.
On .jpg vs .png, generally jpg is better as you will get a smaller file size than with png. I've generally used this code passing a quality of 90%, which reduces file size significantly, but still looks perfect.
I think PNG is better format for WEB than JPEG that always uses lossy JPG compression, but its degree is selectable, for higher quality and larger files, or lower quality and smaller files. PNG uses ZIP compression which is lossless, and slightly more effective than LZW (slightly smaller files).
In C# you can use System.Drawing namespace types to load, resize and convert mages. This namespace wraps GDI+ API.
A. For graphics I would use png and for fotos jpg.
B. Configuration?
C. There are tons of post that explain that:
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
Resizing an Image without losing any quality

Categories