building grayscale images out of array in c/c++

A place where newbies can post without (much) fear of reprisal. All mission posts should still go in the applicable forum.
Forum rules
Older HTS users: Be nice to the new people.

NEW USERS: This is NOT the place to post about missions! Refer to "Missions" category.

building grayscale images out of array in c/c++

Post by fuchsteufel on Mon Feb 11, 2013 3:46 pm
([msg=73666]see building grayscale images out of array in c/c++[/msg])

hi folks, I hope I'm in the right section here. First of all, please excuse my poor english, I will do my best ;)

I really enjoyed your challenges (the ones I was able to solve so far), most of them the one with XECryption. That's why I had the idea to create kind of such a challenge for our local hackerspace. First idea was to write some information in a picture. As a single plane grayscale picture can be interpreted as a 2d-matrix of integer-values between 0 and 255 which corresponds with the range of decimal-coded ascii, there is no problem to do that.

I'm able to write a program in labview, but I want to do it in C or C++ because most people who are not in engineering like I am or science don't know labview and additionally it's pretty expensive and restrictive and I want to share the code to modify it to create new challenges of similar kind.

I looked in google but I didn't find anything pretty useful, so I hope someone here can tell me or at least give some advice how to create an image file (best case a png) of a 2d- array in C or C++.

Thanks in advance
Fuchs
fuchsteufel
New User
New User
 
Posts: 12
Joined: Sat Feb 09, 2013 11:45 pm
Blog: View Blog (0)


Re: building grayscale images out of array in c/c++

Post by WallShadow on Mon Feb 11, 2013 7:22 pm
([msg=73673]see Re: building grayscale images out of array in c/c++[/msg])

fuchsteufel wrote:hi folks, I hope I'm in the right section here. First of all, please excuse my poor english, I will do my best ;)
...
Thanks in advance
Fuchs


Through my experience as a programmer, it's pretty difficult to make images directly from arrays due to compression, color pallets, header information, etc. . I recommend finding a way to display it on the screen and then either doing alt+Prt Sc or having the program itself capture an image of it. Generally anything that gives you access to drawing on a canvas will have some kind of functionality to save it as an image file.

- WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 626
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: building grayscale images out of array in c/c++

Post by fuchsteufel on Mon Feb 11, 2013 9:15 pm
([msg=73677]see Re: building grayscale images out of array in c/c++[/msg])

thanks for your reply, Wallshadow.

I think the labview solution has all that stuff implemented in any way I don't know. Compression might be the biggest problem, I can provide header information and the color palette isn't that weird in a single pane grayscale picture (as far as I know). I just didn't think about compression and I really don't know much about that.

Do you think it is possible to store information in the pixel values and make it readable in a more or less simple way?
fuchsteufel
New User
New User
 
Posts: 12
Joined: Sat Feb 09, 2013 11:45 pm
Blog: View Blog (0)


Re: building grayscale images out of array in c/c++

Post by cberg22 on Wed Feb 13, 2013 8:17 am
([msg=73740]see Re: building grayscale images out of array in c/c++[/msg])

Hi Fuchsteufel,

I had a project quite a while ago where I created a bitmap image (which just so happened to be greyscale) with a C program. Due to it being at least 2 years ago, I can't quite remember everything about it but I have dragged it up to help you.

The bitmap header contains a field for compression which can be set to 0, which I think makes it a lot easier.
There 2 headers, the main header of the file and then the info header. These can be created using structs (I'm going to give you the code in C which you can easily adapt to C++ if needed) I'm also going to give you the values I used for each field.

Main header
Code: Select all
typedef struct
{
unsigned short int type;       //set to 4D42 hex (19778)
unsigned int size;               //size of the BMP file including headers in bytes
unsigned short int reserved1,reserved2;   //both set to 0
unsigned int offset;       //offset to start of image, set to 54
}Header;


Info Header
Code: Select all
typedef struct
{
unsigned int size;   //size of infoheader, set to 40
int width, height;   //dimensions of image in pixels
unsigned short int planes;  //set to 1
unsigned short int bits;      //number of bits per pixel (I used 24)
unsigned int compression;  //set to 0 for no compression
unsigned int imagesize;    //same as the size field in the main header
int xresolution, yresolution;  //resolution in pixels per meter (I used 3780)
unsigned int ncolors;   //you can specify your own colour scheme, but can set it to 0 for default
unsigned int importantcolors;//likewise, I set to 0
}Infoheader


Finally, you can define a pixel struct:
Code: Select all
typedef struct
{
unsigned char B, G, R;
} Pixel;


Keep in mind this will only be valid if using 24 bit pixels.

Another thing is that you must use #pragma pack(2) with your list of includes before the Header struct declaration and change it back with #pragma pack() before the Info header struct declaration.

After having all of this set up, you can simply create a nester loop for the width and height of the image and assign values for each colour of each pixel. For greyscale, simply set each of the values the same.
Also, keep in mind that the bitmap file is written right to left from bottom to top :?
Each row must also be a multiple of 4 bytes, so calculate how many 'spacer' bytes you need to write at the end of each row

Apologies if any of this doesn't make sense, and it has dragged on a lot longer than I intended, but hopefully that gives you all the information you need to be able to do it yourself :)

Good luck!
Cberg
cberg22
New User
New User
 
Posts: 7
Joined: Wed Feb 13, 2013 7:29 am
Blog: View Blog (0)



Return to NZone

Who is online

Users browsing this forum: No registered users and 0 guests