• Advertisement


This topic is now archived and is closed to further replies.

Raw image file format

This topic is 5802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to find out a way to display raw images on the screen... I''ve made my own raw graphics format, the game will use 256 colors (1 byte per pixel), so it''s possible for me to store each pixel in one byte, the value of the byte describes a color, eg 132 = color number 132! GRAPHICS FORMAT (RAW 1 byte per pixel): --------------------- HEADER: 4 bytes (two integer values, first: width, second: height) PIXDATA: (width * height) bytes My problem is due to lack of experience on the subject, I want to load each raw data image into memory as soon as my program (game) starts, and then be able to acces it from the game... If anyone can give me a piece if code or a tutorial on the subject I would be grateful (I code in C++)...

Share this post

Link to post
Share on other sites
I know how to do that, I also know that the data I load should be loaded in binary format...
But wich data type should be the one that gets the data in the program, 1 unsigned char = 1 byte?
is there anyway to load all the data into different vektors, in a simple and elegant way, is use of pointers the best way to go, how should I interface with the loaded data from within the program?
That''s the type of things I would like to know...

Share this post

Link to post
Share on other sites
Do something like this:

typedef unsigned char byte;
typedef unsigned short word;

/* Simple structure for your format */
typedef struct {
word width, height;
byte *data;
} Image;

/* Node for linked list */

typedef struct ImageListNode {
Image image;
ImageListNode *next;
} ImageList;

ImageList *imageList;

For each image you load, add a node into the linked list. You could also add some ID-tag on each image for searching.

Share this post

Link to post
Share on other sites

//Since you didn''t say what compiler I''m going to show you in standard C.

struct Image_S
short width, height;
unsigned long TotalBytes;
unsigned char *Data;

/* Returns:
0 = success
1 = File Not Found
2 = Not enough memory

char Load_Image(char *fName, struct Image_S *Img)
FILE *in;
if (Img->Data) //Is something loaded already?

free(Img->Data); //Yup, lets free it!

in = fopen(fName,"rb"); //open for reading!

if (!in) //Is it open?

return 1; //File not found!

Img->width = getw(in);
Img->height = getw(in);
Img->TotalBytes = Img->width * Img->height;
Img->Data = (unsigned char*)malloc(Img->TotalBytes);
if (!Img->Data) //Did we get memory?

fclose(in); //Close our file

return 2; //Not enough memory

fread(Img->Data,1,Img->TotalBytes,in); //Read our file into our buffer!

return 0; //Success!!


void Kill_Image(struct Image_S *Img)
if (Img->Data)
Img->Data = 0; //Set it to null!


//now you use it like this

struct Image_S Image = {0,0,0}; //Initialize to 0!

char Ret;

Ret = Load_Image("Test.raw",&Image);
if (Ret)

return -1;


Of course you can put this into a class, or into a linked list, or a vector, or whatever you''d like. You can replace the malloc/free with new/delete, or whatever you''d like :D

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)

Share this post

Link to post
Share on other sites
You could also just have a small primitive structure that has R, G, and B values to make up the color you want. Then maybe have a header for your image file that tells it the dimensions of the image and such. I have some old pascal code of this if you''d like to take a look at it, I did it back in 98.

Share this post

Link to post
Share on other sites
Original post by torquel
"I have some old pascal code of this if you''d like to take a look at it"
Yes that''d be nice, torquel, please send it to this email adress:

Share this post

Link to post
Share on other sites
Hello, thanks for all the examples, I''ve been looking at your code and it looks great...
I decided to have all the images stored in one single file instead with the format:


Here is my version of the code to load the images:
(I haven''t tried it yet, I hope just pray it will compile well)

typedef unsigned char byte;

byte numberOfImages;

typedef struct
byte imageWidth;
byte imageHeight;
byte imagePixels[imageWidth * imageHeigh];

void loadImages(void)
ifstream imageFile("test.dat", ios::binary);

imageFile.read((char *)&numberOfImages, sizeof(byte));

image images[numberOfImages];

while(currentImage <= numberOfImages)
imageFile.read((char *)&images[currentImage].imageWidth, sizeof(byte));
imageFile.read((char *)&images[currentImage].imageHeight, sizeof(byte));

imageFile.read((char *)&images[currentImage].imagePixels, sizeof(images[currentImage].imagePixels));



I''m very fresh on C++ but this code seems all right to me...
My question is:
How do I prevent the hole lot of image data (the variable: images)
to be deleted when the function loadImages has executed, should I make it a pointer or what...

If you see anything else that is wrong with the code or just have any tips on how to improve it in general, please tell me...
(1te01b_anle@tjelvar.org or here in the forum)

Share this post

Link to post
Share on other sites

  • Advertisement