Image cropping C# without .net library - c#

Can anyone advise on how to crop an image, let's say jpeg, without using any .NET framework constructs, just raw bytes? Since this is the only* way in Silverlight...
Or point to a library?
I'm not concerned with rendering i'm wanting to manipulate a jpg before uploading.
*There are no GDI+(System.Drawing) or WPF(System.Windows.Media.Imaging) libraries available in Silverlight.
Lockbits requires GDI+, clarified question
Using fjcore: http://code.google.com/p/fjcore/ to resize but no way to crop :(

You could easily write crop yourself in fjcore. Start with the code for Resizer
http://web.archive.org/web/20140304090029/http://code.google.com:80/p/fjcore/source/browse/trunk/FJCore/Resize/ImageResizer.cs?
and FilterNNResize -- you can see how the image data is stored -- it's just simple arrays of pixels.
The important part is:
for (int y = 0; y < _newHeight; y++)
{
i_sY = (int)sY; sX = 0;
UpdateProgress((double)y / _newHeight);
for (int x = 0; x < _newWidth; x++)
{
i_sX = (int)sX;
_destinationData[0][x, y] = _sourceData[0][i_sX, i_sY];
if (_color) {
_destinationData[1][x, y] = _sourceData[1][i_sX, i_sY];
_destinationData[2][x, y] = _sourceData[2][i_sX, i_sY];
}
sX += xStep;
}
sY += yStep;
}
shows you that the data is stored in an array of color planes (1 element for 8bpp gray, 3 elements for color) and each element has a 2-D array of bytes (x, y) for the image.
You just need to loop through the destination pixels, copying then from the appropriate place in the source.
edit: don't forget to provide the patch to the author of fjcore

ImageMagick does a pretty good job. If you're ok with handing off editing tasks to your server...
(Seriously? The recommended way of manipulating images in Silverlight is to work with raw bytes? That's... incredibly lame.)

I'm taking a look at : http://code.google.com/p/fjcore/source/checkout
A dependency free image processing library.

where is silverlight executed?
Is there any reason at all to send an complete picture to the client to make the client crop it?
Do it on the server... (if you are not creating an image editor that is..)

Related

Bitmap pixel replacement based on brightness

Simply, I want to set any pixel in an image to white where the brightness is greater than a given threshold. I have written the following code which works suitably fast and accurately for my purposes.
using (Bitmap image = new Bitmap("C:\\temp\\test1.png"))
{
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
Color c = image.GetPixel(x, y);
float f = c.GetBrightness(); //From 0 (black) to 1 (white)
if (f > 0.1) { image.SetPixel(x, y, Color.White); }
}
}
image.Save("C:\\temp\\test2.png");
}
However, it just feels wrong to have to loop through every pixel one-by-one. Is there an optimized approach to this problem using another .NET Imaging or Graphics approach?
For some special cases (such as power of 2 thresholds), you can work on 32-bit chunks using bitmasking tricks, but I doubt it would buy you very much. The far bigger overhead in the code you provided are the GetPixel and SetPixel calls, which are very inefficient. You can greatly speed up the procedure using the LockBits method and processing the underlying data directly from a byte[].
You might optimize by using proven native algorithm implementations (e.g. OpenCV). Although I'm not an expert on this subject, think you will need to iterate through all pixels anyway. Even scientific papers about thresholding algorithms (which rather focus on finding a good threshold) loop through the whole image, so I don't think you have to feel wrong about.
Anyway, if there's some mathematical approach that works on any image, I would also be interested, but I doubt the existence.
Consider it this way, if you want to know if a pixel is above or below the threshold for all the pixels, you are going to have to visit all the pixels. Any pixel not visited will mean you don't know its value.
There is no way to write this in better than O(n) time, especially if you intend to change values of certain pixels.

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));

Open huge TIF in .NET and copy parts to new image [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I'm looking for a library that can open and copy sections of a large TIFF file. I've looked at LibTiff.Net which opens the file very quickly but it doesn't have any functions for cropping or copying sections of the image. My image is 100,000 x 100,000 pixels upwards and creating a System.Drawing.Bitmap of that size crashes the application so converting to a Bitmap first is not an option.
Can anyone recommend a .NET library?
If your file is less than 4GB on disk than I recommend you to take another look at LibTiff.Net. Even with such large images you have some options.
First of all, check whether your image is tiled or stripped. Tiff.IsTiled method will give you the answer.
If your image is tiled, than you probably shouldn't read it using ReadScanline method. It might be better to use ReadEncodedTile method in that case.
If your images is stripped, than you can use ReadScanline and ReadEncodedStrip methods to read it.
If you want to use something that expects System.Drawing.Bitmap than try using ReadRGBATile or ReadRGBAStrip. These methods can be used to create bitmaps from portions of your image. There is no sample for this, but Convert color TIFF to a 32-bit System.Drawing.Bitmap should give you almost all required information about how to convert tile or strip of an image to a bitmap.
EDIT:
LibTiff.Net 2.4.508 adds support for BigTiff so files larger than 4GB are also supported.
Your image must be in BigTIFF format, since normal TIFF can't be larger than 4 GB.
BigTIFF can be read with a modified version of libtiff (available in BigTIFF website), this library allows to handle such images the way you want without loading all pixel data in memory.
I didn't see bindings for .NET but it shouldn't be too long to do it.
Atalasoft dotImage has this ability built-in to the TIFF decoder. The decode implements the interface IRegionReadable, which lets you read a rectangular section from a given page of an image in a Stream.
In a TIFF, this section will honor the orientation tag and in stripped or tiled tiffs uses the minimum set of tiles and strips to fill the rectangle.
(disclaimer, I work for Atalasoft and wrote that interface and implemented it in the TIFF decoder)
As Bobrovsky mentioned you should check if your file image is tiled or not. In the following, I've presented the snippet code to read a stream tiff and crop the upper left part of the image.
using (Tiff input = Tiff.Open(#"imageFile.tif", "r"))
{
// get properties to use in writing output image file
int width = input.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
int height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
int samplesPerPixel = input.GetField(TiffTag.SAMPLESPERPIXEL)[0].ToInt();
int bitsPerSample = input.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
int photo = input.GetField(TiffTag.PHOTOMETRIC)[0].ToInt();
int scanlineSize = input.ScanlineSize();
byte[][] buffer = new byte[height][];
for (int i = 0; i < height; ++i)
{
buffer[i] = new byte[scanlineSize];
input.ReadScanline(buffer[i], i);
}
using (Tiff output = Tiff.Open("splitedImage.tif", "w"))
{
output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
output.SetField(TiffTag.IMAGEWIDTH, width/2);
output.SetField(TiffTag.IMAGELENGTH, height/2);
output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));
output.SetField(TiffTag.PHOTOMETRIC, photo);
output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
int c = 0;
byte[][] holder = new byte[height][];
for (int i = height/2; i < height; i++)
//for (int j = 0; j < height/2 ; j++)
{
holder[i] = buffer[i].Skip(buffer[i].Length/2).ToArray();
output.WriteScanline(holder[i], c);
c++;
}
}
}
System.Diagnostics.Process.Start("splitedImage.tif");
For other parts of the image, you can change the range of "i" in for loop.

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

C# - How to convert an Image into an 8-bit color Image?

I need to convert a PNG Image loaded from a file into an 8 bit-per-pixel byte array used by another device (embedded programming).
I'm copying over pixel by pixel into a new Image created with a 16-bit color - Format16bppRgb565, but I need Format8bpp, which is not present in the .NET Framework. (I cannot use Format8bppIndexed since the other device cannot handle indexed bitmaps)
Bitmap img = new Bitmap(imgPath);
Bitmap img8 = new Bitmap(imgW, imgH, PixelFormat.Format16bppRgb565);
for (int I = 0; I <= img.Width - 1; I++) {
for (int J = 0; J <= img.Height - 1; J++) {
img8.SetPixel(I, J, img.GetPixel(I, J));
}
}
So how can I convert an Image into an 8-bit per pixel byte array?
Can I use the 8-bit indexed format and extract the bytes? - Format8bppIndexed
Can I calculate the pixel colors by reducing the 32-bit color R,G,B values to 8-bit?
Is there a C# library that does this already?
"The easiest way is to create a new 8 bit bitmap, and draw your 24 bit image onto it." - how?
Although I'm not sure how to do it with plain vanilla C# you can use the FreeImage Project libraries to convert images formats:
FreeImage is an Open Source library
project for developers who would like
to support popular graphics image
formats like PNG, BMP, JPEG, TIFF and
others as needed by today's multimedia
applications. FreeImage is easy to
use, fast, multithreading safe,
compatible with all 32-bit versions of
Windows, and cross-platform (works
both with Linux and Mac OS X).
It's written in C++ but has good .NET wrappers you can use.
I already did it in one Pattern Recognition project. Searching some image processing and pattern recognition books you can find these solutions.
a) If you want to convert to greyscale you can find in general two solutions.
#1 Averaging RGB channels. - You can take 8-bit values for avery single channel and calculate pixel = ( R+G+B )/3
#2 Take only the Green component. It is best viewed.
b) If you want to convert to 8bit color representation you need to use a Color table.
You can see a solution here http://www.codeproject.com/KB/graphics/Image_Bitdepth_Conversion.aspx

Categories

Resources