Decode an JFIF image file in C# - c#

I have a JPEG image which is JFIF formatted. I want to decode it and get the dimension.
Here is my code:
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
ushort chunkLength = binaryReader.ReadLittleEndianInt16();
if (marker == 0xc0)
{
binaryReader.ReadByte();
int height = binaryReader.ReadLittleEndianInt16();
int width = binaryReader.ReadLittleEndianInt16();
return new Size(width, height);
}
binaryReader.ReadBytes(chunkLength - 2);
}
Ok. This piece of code is common and you can find it all over the internet. It works fine for most of the JPEG images.
Now, this specific image which was taken by the camera - "Canon EOS 300D DIGITAL", does not support this piece of code. The marker for the dimension is 0xFFC2 instead of 0xFFC0.
My question is which one is correct? If the code is correct, then how can a Canon camera produce a non-standard image? If the Canon camera is correct, then how can we fix the code to correct find the dimension of this image?
Thanks.

FFC2 seems to be the marker for progressive images.
please see for example http://en.wikipedia.org/wiki/JPEG which explains what "progressive" format is (see "JPEG compression" section).
yes, i think you can change your if statement to check for both 0xc0 (SOF0 marker) and 0xc2 (SOF2 marker) because they seem to have similar structure (see "syntax and structure" section). see also here: http://fjcore.googlecode.com/svn/trunk/FJCore/Decoder/JpegDecoder.cs
i am not expert in JPEG formats, so you might want to check with specialised forums if you are developing a mission critical code.

Related

How to Display this image in C#

I have an image https://drive.google.com/file/d/16Xotc-2CJ6HkEJDysfKBkjClkU1OGiyQ/view?usp=sharing that is GrayScale but every library I have tried, ImageMagick, ImageSharp, System.Drawing seem to interpret it as black and white, but when you open it in ImageJ or Photoshop or Incarta or many other software you can clearly see it is grayscale.
can anyone help me find a way to display this image? here is something I've tried but i've tried almost a dozen different things
TiffEncoder encoder = new TiffEncoder();
encoder.PhotometricInterpretation = SixLabors.ImageSharp.Formats.Tiff.Constants.TiffPhotometricInterpretation.BlackIsZero;
SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(mysteryTiff);
PixelTypeInfo pixType = image.PixelType;
// Stretches the image to fit the pictureBox.
Stream stream = new MemoryStream();
image.SaveAsTiff(stream, encoder);
stream.Position = 0;
MagickImage magickImage = new MagickImage(stream);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.ClientSize = new System.Drawing.Size(1200, 1200);
pictureBox1.Image = magickImage.ToBitmap();
Can anyone display this image correctly. It will display correctly when uploaded to
What you have there, according to the image tag directory, is a 2024x2024 16-bpp greyscale LZW-compressed extended TIFF. It even opens in some software, which proves that it's not malformed. So far so good.
Now here's where it breaks down: 16-bpp greyscale is not supported by a lot of things. The 'why' is mildly convoluted, having to do largely with "but we all use 8 bits per channel, and so does the hardware, so why bother", but the end result isn't: if you want to use anything above 8 bits per channel, you'll either have to find something that will do the work for you or convert the data to 8-bpp at some point.
Even when the file format explicitly support 16-bpp greyscale (TIFF and PNG for instance), most libraries tend not to support either read or write in that format because it is so rarely used that they don't bother to implement it. I ended up writing my own PNG encoder for 16-bpp greyscale images (converted from 12-bpp and 16-bpp XRAY images), but the images aren't viewable in most programs that supposedly support the full PNG standard.
In this case your best option is probably going to be to write a conversion of your own for this type of file. Assuming that the same format (16-bpp, LZW-compressed) is produced by the source application every time, it shouldn't be too difficult to convert the pixel buffer to 8-bpp and save out as TIFF, PNG or whatever you like. You'll lose half of your greyscale (depth) resolution, but for display purposes they're not going to help much anyway. It only really matters when there's a good reason to retain the full range of values.

C# - Can I use Libtiff to output Tiff encoded JPEG (In YCbCr)

Hi I am quite new to Libtiff and image processing, and I have a question when I try to use Libtiff.net from Bitmiracle.
I have some OJPEG TIFF image and want to convert them into nowadays JPEG TIFF. I achieved that by converting the source into BMP and then save as TIFF (compression: JPEG; photometric: RGB), but the size of the image is quite large. So I thought if I can compress them with photometric of YCbCr, which can reduce the size a lot.
However, when I change the photometric from RGB to YCbCr, the program just don't work: the output is only 8 bytes (the input is about 400kb). When open the image as TXT, it shows:
"II* "
The code I use is:
byte[] raster1 = getImageRasterBytes(inputbmp[0], PixelFormat.Format24bppRgb);
tif1.SetField(TiffTag.IMAGEWIDTH, inputbmp[0].Width);
tif1.SetField(TiffTag.IMAGELENGTH, inputbmp[0].Height);
tif1.SetField(TiffTag.COMPRESSION, Compression.JPEG);
tif1.SetField(TiffTag.PHOTOMETRIC, Photometric.YCBCR);
tif1.SetField(TiffTag.ROWSPERSTRIP, inputbmp[0].Height);
//tif1.SetField(TiffTag.JPEGQUALITY, confidence);
tif1.SetField(TiffTag.XRESOLUTION, 200);
tif1.SetField(TiffTag.YRESOLUTION, 200);
tif1.SetField(TiffTag.BITSPERSAMPLE,8);
tif1.SetField(TiffTag.SAMPLESPERPIXEL, 3);
int stride = raster1.Length / inputbmp[0].Height;
convertSamples(raster1, inputbmp[0].Width, inputbmp[0].Height);
for (int i = 0, offset = 0; i < inputbmp[0].Height; i++)
{
tif1.WriteScanline(raster1, offset, i, 0);
offset += stride;
}
I'm not quite sure if WriteScanline can handle YCbCr output or not, and if there are other ways, It's totally cool too.
Thank you for the help!
Output images are definetely broken. It usually happens when some fatal error occurred. The library should output warnings and errors into console in such a case.
Please investigate the warnings and errors. They might give you the answer.
Based on experience, I think you might have provided incorrect value for ROWSPERSTRIP tag. The value should be a multiple of 8.
JPEG converts image into YCbCr internally already, so I don't think you will benefit from this.

Checking to see if an image is Blank in C#

I've looked everywhere but there doesn't seem to be a standard (I could see) of how one would go about checking to see if an image is blank. In C#
I have a way of doing this, but would love to know what the correct way is of checking to see if an image is blank, so everyone could also know in the future.
I'm not going to copy paste a bunch of code in, if you want me to, it will be my pleasure, but I just first want to explain how i go about checking to see if an image is blank.
You take a .jpg image, Get the width of it. For example 500 pixels
Then you divide that by 2
giving you 250
Then you check what the colour of every pixel is in the location of (250 width, and i height) (where you iterate thought the hight of the image.
What this then do is only check the middle line of pixels of an image, vertically. It goes though all the pixels checking to see if the colour is anything Except white. I've done this so you wont have to search ALL 500*height of pixels and since you will almost always come across a colour in the middle of the page.
Its working... a bit slow...There must be a better way to do this? You can change it to search 2/3/4 lines vertically to increase your chance to spot a page that's not blank, but that will take even longer.
(Also note, using the size of the image to check if it contains something will not work in this case, since a page with two sentences on and a blank page's size is too close to one another)
After solution has been added.
Resources to help with the implementation and understanding of the solution.
Writing unsafe code - pointers in C
Using Pointers in C#
/unsafe (C# Compiler Options)
Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)
(Note that on the first website, the stated Pizelformat is actually Pixelformat) - Small error i know, just mentioning, might cause some confusion to some.
After I implemented the method to speed up the pixel hunting, the speed didn't increase that much. So I would think I'm doing something wrong.
Old time = 15.63 for 40 images.
New time = 15.43 for 40 images
I saw with the great article DocMax quoted, that the code "locks" in a set of pixels. (or thats how i understood it)
So what I did is lock in the middle row of pixels of each page. Would that be the right move to do?
private int testPixels(String sourceDir)
{
//iterate through images
string[] fileEntries = Directory.GetFiles(sourceDir).Where(x => x.Contains("JPG")).ToArray();
var q = from string x in Directory.GetFiles(sourceDir)
where x.ToLower().EndsWith(".jpg")
select new FileInfo(x);
int holder = 1;
foreach (var z in q)
{
Bitmap mybm= Bitmap.FromFile(z.FullName) as Bitmap;
int blank = getPixelData2(mybm);
if (blank == 0)
{
holder = 0;
break;
}
}
return holder;
}
And then the class
private unsafe int getPixelData2(Bitmap bm)
{
BitmapData bmd = bm.LockBits(new System.Drawing.Rectangle((bm.Width / 2), 0, 1, bm.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bm.PixelFormat);
int blue;
int green;
int red;
int width = bmd.Width / 2;
for (int y = 0; y < bmd.Height; y++)
{
byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);
blue = row[width * 3];
green = row[width * 2];
red = row[width * 1];
// Console.WriteLine("Blue= " + blue + " Green= " + green + " Red= " + red);
//Check to see if there is some form of color
if ((blue != 255) || (green != 255) || (red != 255))
{
bm.Dispose();
return 1;
}
}
bm.Dispose();
return 0;
}
If you can tolerate the chance of getting it wrong, the approach seems fine; I have done something very similar in my case, although I always had a visual confirmation to deal with errors.
For the performance, the key open question is how you are getting the pixels to test. If you are using Bitmap.GetPixel, you are bound to have performance problems. (Search for "Bitmap.GetPixel slow" in Google to see lots of discussion.)
Far better performance will come from getting all the pixels at once and then looping over them. I personally like Bob Powell's LockBits discussion for clarity and completeness. With that approach, checking all of the pixels may well be reasonable depending on your performance needs.
If you're using System.Drawing.Bitmap you can speed up things up (substantially), by:
Not using GetPixel to access the pixels, use LockBits and UnlockBits to copy the image bitmap to regular memory. See the examples on the MSDN documentation for usage.
Not calling the Width, Height or Size properties in for loop. Call Size once, store the values in a local variable and use those in the loop.
Notes:
When using System.Drawing.Bitmap your image may be in device memory and accessing it may be time consuming.
I don't remember whether loading an image into a Bitmap already converts it to RGB format as other formats are more difficult to work with, but if that is not the case you can create an RGB Bitmap of the same size as your original image, get it's Graphic object (Graphics.FromImage) and use DrawImage to draw the original image in the RGB bitmap.
Edit: Beat to the punch by DocMax.
In any case for speed you can also try using alternative libraries such as the excellent FreeImage which includes C# wrappers.
Scale the image to 1x1 then check one pixel
new Bitmap(previousImage, new Size(1, 1));

Is it possible to know if a JPEG image was rotated only from its raw bytes?

Can you tell (let's say using .NET 4.0, WinForms) if a JPEG image is rotated only from its binary (like the result of File.ReadAllBytes())?
UPDATE
Thank you all for your answers so far.
Just a heads-up for anybody trying to solve the same problem. I was tricked by the System.Drawing.Image class, which loads the EXIF tags when initialized with FromFile(...) but seems to ignore them when initialized from a stream. I was using the ExifTagCollection library to read the EXIF tags but I guess the results would be comparable with any other lib.
var bytes = (get binary from server)
File.WriteAllBytes(path, bytes);
WORKS:
var image = Image.FromFile(path);
DOES NOT WORK: (fails for FileStream too)
using (var ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
Continued with:
ExifTagCollection exif = new ExifTagCollection(image);
foreach (ExifTag tag in exif)
{
Console.WriteLine(tag.ToString());
}
there are no tags if loading from stream.
http://jpegclub.org/exif_orientation.html details the exif orientation flag. Find that, find the orientation.
Of course, this only applies to rotating an image by setting that flag, as is often done by cameras themselves, some image-viewing software that isn't designed for more detailed editing, and some straight-from-the-file-manager tools. It won't work if someone loaded the image into a more general image-editor, turned it around, and then saved it.
Edit:
Landscape vs. Portrait is different to "rotated from image-devices natural orientation". It's also simpler:
if(img.Height == img.Width)
return MyAspectEnum.Square;
if(img.Height > img.Width)
return MyAspectEnum.Portrait;
return MyAspectEnum.Landscape;
That may be closer to what you really want to know about.
In case EXIF data is not available / not reliable you might assume this to identify a picture format:
Height > Width ? Portrait format
Width > Height ? Landscape format
Width = height ? Picture is perfectly square, either one is fine
Same restriction as EXIF applies: a physical editing which turned around the picture and didn't update/set EXIF info accordingly will fool this check too.
If you know how to read the JPEG encoded data, you could look for the EXIF and get the rotation from EXIF. It would be tough if EXIF data is not available.
It is necessary to read the EXIF to determite the JPEG image orientation.
Please take a look at the ExifLib - A Fast Exif Data Extractor for .NET 2.0+. It seems the library returns the Orientation like specified here.

Convert 1-bit image to 8-bits

How can I convert a 1 bit image to an 8 bit image using C#? What color matrix should be used? Can you please provide samples or links?
Disclaimer: I don't know C#, but I've done too much image processing on C/C++ so I can't pass on answering - I'll answer in C since I think C# shares a similar syntax.
Both 1bit (two colors) and 8bit (256 colors) images have a palette. but transitioning a 1bit to 8bit transition is easy - since there's no quantization involved, just up-sampling.
First you need to choose (or import) the two colors of the 1bit image's palette. If you don't have any, I suggest using black (0x000000FF) and white (0xFFFFFFFF) for clarity (note: both colors are RGBA, I think windows uses ABGR). This will be your 'palette'.
Then map each color to the palette - The input image is going to have width * height / 8 bytes. Each byte represents eight pixels. Since I don't know about your expertise in bittwiddling (i.e I don't want to confuse you and I don't want you to mindlessly copy and paste code you've been granted on the internets), I'll keep this answer simple.
// Insert your image's attributes here
int w = image.width;
int h = image.height;
// Data of the image
u8* data = image.data;
/*
* Here, you should allocate (w * h) bytes of data.
* I'm sure C# has ByteArray or something similar...
* I'll call it output in my code.
*/
u8* output = new u8[w * h];
u8* walker = output;
// Loop across each byte (8 pixels)
for(int i=0; i<w*h/8; ++i) {
// Loop across each pixel
for(int b=(1<<7); b>0; b>>=1) {
// Expand pixel data to output
*walker++ = !!(data[i] & b);
}
}
Hope that helps!
Does this help:
http://www.wischik.com/lu/programmer/1bpp.html
But of course it needs some clean up. It could use some try..finally's for all the delete's and releasing of DC's

Categories

Resources