How to read a C source file into a array

B

bashcommando

Guest
Hey guys it's me again,
I am trying to understand how to read a GIMP C source file that I generated from a graphic. I want to know how to read it into a array like this(x being x position on the screen and y being y position on the screen):
Code:
unsigned short* buffer;
buffer[y*320+x] = (color code);
Using this method I can write it to the screen buffer in the terminal(or anywhere to be precise). Ideas?
 


Are you trying to read source code, which would be text, into an array to write to a screen buffer?

Also, how are you doing this? C code? Something else?

If it's C code, then you simply open the file for reading, then pass your buffer as an argument to the read function (using fopen() and fread() ).
 
Are you trying to read source code, which would be text, into an array to write to a screen buffer?

Also, how are you doing this? C code? Something else?

If it's C code, then you simply open the file for reading, then pass your buffer as an argument to the read function (using fopen() and fread() ).
Not at all... Okay, find a image and open it in GIMP. Then export it as a c source file. Make sense?
 
I'm not sure why you would want to do this but you can treat the file as a standard text file. OR you could include it in your project and access the struct. As for writing to the screen buffer...probably not a good idea but maybe. Not really sure.
 
Not at all... Okay, find a image and open it in GIMP. Then export it as a c source file. Make sense?

OK, I see what you're doing now. It's not something I would do UNLESS I wanted to make sure that the image was available at all times, in which case I'd just compile that exported file into the binary, in which case you've already got it in the buffer.
 
As Ryan and GrumpyOldMan have said: if it's in a c file, you should just need to include it in your project and access the image-data in whatever container it is presented in.

Being a C file, I imagine that the data will most likely be some kind of struct with an array member containing the pixel/colour information.

If you need to do something to convert the data to another format in order to do what you need to do, then you could perhaps write a function that can convert the data from the format exported by gimp and into whatever format you require. Or better still, directly edit the C file exported by Gimp so the data is already in the format that you actually want it in.

I've seen the export as .h and .c file options in Gimp before, but I've never tried exporting in those formats. So I haven't seen the code that is output. Usually in my projects, I load external images. Never really had the inclination to embed image data in my executables. Might take a look at this at some point!
 
As Ryan and GrumpyOldMan have said: if it's in a c file, you should just need to include it in your project and access the image-data in whatever container it is presented in.

Being a C file, I imagine that the data will most likely be some kind of struct with an array member containing the pixel/colour information.

If you need to do something to convert the data to another format in order to do what you need to do, then you could perhaps write a function that can convert the data from the format exported by gimp and into whatever format you require. Or better still, directly edit the C file exported by Gimp so the data is already in the format that you actually want it in.

I've seen the export as .h and .c file options in Gimp before, but I've never tried exporting in those formats. So I haven't seen the code that is output. Usually in my projects, I load external images. Never really had the inclination to embed image data in my executables. Might take a look at this at some point!
Code:
/* GIMP RGBA C-Source image dump (arch-linux-logo.c) */

static const struct {
  guint      width;
  guint      height;
  guint      bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
  guint8      pixel_data[1200 * 1200 * 4 + 1];
} gimp_image = {
  1200, 1200, 4,
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
...............
}
 
Referring to Ryans file above:
guint8 is a GTK type, which if memory serves is typedef'd as unsigned char. So it is guaranteed to be exactly 8 bits wide, allowing it to represent the values from 0..255. Perfect for pixel values!

The image-data above is 4 bytes per pixel, so 4 unsigned chars represents each pixel (RGBA). Each byte is listed in octal, so the first pixel would consist of the values "\377,\377,\377,\0" or in decimal 255,255,255,0 which would be solid white, with no alpha/transparency.

If bashcommando wants to put those values into an array of unsigned shorts (which, incidentally are 16-bits wide) there are two options, as outlined in my original post.
1. Read every element in the original array in the struct, cast it to unsigned short and copy it to his array.
2. Alter the original file to use the types he wants to use - e.g. change the guint's to unsigned int and the guint8 to unsigned short.

But unsigned short can represent a wider range of values than unsigned char, so both of these options could present some unwanted problems/side-effects, so the question to bashcommando is how do you plan to represent each pixel in the array of unsigned shorts?

It would be wasteful to use a 16 bit type to store 8 bit values......You could perhaps store two of the 8 bit bytes in each unsigned short value, which I guess would mean casting the first byte to unsigned short and then bit-shifting it to the left by 8 bits before adding the second byte. So two unsigned shorts would contain the four bytes of data for a single pixel. But when it comes to extracting the RGBA values, you'd need to do more bit-shifting to get the values.... So you would almost certainly be best leaving it as guint8/unsigned char IMO!

(BTW: if you aren't using gtk, you could just change guint8 to unsigned char and change guint to unsigned int!)
 
(BTW: if you aren't using gtk, you could just change guint8 to unsigned char and change guint to unsigned int!)

Or just uint8_t and uint16_t, which have the advantage of making the width clear to the reader...
 
Code:
/* GIMP RGBA C-Source image dump (arch-linux-logo.c) */

static const struct {
  guint      width;
  guint      height;
  guint      bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
  guint8      pixel_data[1200 * 1200 * 4 + 1];
} gimp_image = {
  1200, 1200, 4,
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
...............
}
Okay, I made it a 16-bit image with no GNU data types. I would have to write it to 0xB8000?
 
Umm, 0xB8000 is a hexdecimal address leading to the VGA memory, It is a uint16_t array.
 

Members online


Top