Jason
Jason

Reputation: 2572

Convert "Picture"/"Image" format to my own

Given an image (format is part of the question) I want to be able to convert it to my "format". My format is, say, just a simple X by Y array of boolean covering the size of the time image stating if that particular pixel is "on". Colour is irrelevant. I have no idea about image formats (such as BMP, JPG, GIF) so given any of those would it be possible to read the image to then "map" that pixel state.

I'm guessing the difficulty might be that determining in an image if a particular pixel is "on". As this forms part of a specific part of the program I am creating, I can ensure users only create their image in a single colour and of a specific range. eg usual red, green, yellow etc.

What I'm trying to achieve is let users use any image program out there they want (eg mspaint) to draw their image then save it and import it into my application at a later point.

Do any of the graphic formats allow me to do this? I hope I have provided enough information for what I'm after. I will provide a very basic example.

eg: image created is 20,10 and the user has drawn a straight line from point 0,0 -> 6,0 (ie 7 pixels wide). They've saved it as a BMP, JPG whatever. I want to then open that image, and scan it to determine basically if a particular pixel has been drawn on or not. So pixels 0,0 1,0 2,0 3,0 4,0 5,0 6,0 would be "on" and all others would be off. If I have to force them to save it as a particular type, I am happy with that.

I am using Delphi as my development language.

Thanks

Jason

Upvotes: 0

Views: 890

Answers (1)

Jenakai
Jenakai

Reputation: 385

Out of the box you can load a few different image formats with Delphi (BMP, JPG, PNG, GIF I think these days too). Then there are plenty of 3rd party options available to support other image types.

Once you've loaded the image into memory the easiest thing to do would be to convert it to a Tbitmap. There are a few ways to do this too. But since you want to mess with the pixels then I would create a Tbitmap and set it's pixel format to 24-bit, then I would draw your loaded image onto the Tbitmap's Canvas.

bmp.Width:=sourecimage.Width;
bmp.Height:=sourceimage.Height;
bmp.PixelFormat:=pf24bit;
bmp.Canvas.Draw(0,0,sourceimage);

While this may not be the most efficient way to do this, it gets you an image in memory of a known format and you only need to worry about one method of using it, rather than coding for different formats.

You now have 2 options for accessing the image information: - Pixels Property (which is very slow, but easy to understand) - Scanlines Property (which is fast but a bit more complicated to use)

Both will allow you to iterate over all the pixels in the image and determine their color.

Now that you've got the pixels, you need to work out what defines a pixel as "ON" and what defines a pixel as "OFF". For that I think you're going to have to pick a colour to represent an "OFF" pixel (for example white - $FFFFFF) so if a pixel is that colour then it's off, otherwise it is on.

It's a bit hard to go any further without knowing too much about what it is you're really trying to achieve. Do you need performance or simplicity? Do you really need your own structure or is just the Bitmap data (effectively a 2 dimensional array) good enough.

And an important note. Using a JPG file is no good. JPG uses approximate colours, there's no way (that I am aware of) that you can guarantee what colour will be loaded after an image is compressed/decompressed. While your "OFF" pixels might be "nearly white" there's no guarantee that they will be "exactly white".

Upvotes: 3

Related Questions