Converting bmp files

DarkLight 101 Mar 21, 2003 at 06:32

Dear all,
I am making my first Delphi/OpenGL game
Within a few days I’ll post here the demo. But for now it uses for textures external BMP files. I would like to convert them into RGB arrays for internal use. Can you told me the easiest and fastest way to do this

P.S. Althrough I’m writting my game in Delphi, I’m not using VCL. Also, code for converting in VC++, Delphi or VB is acceptable. I need only application for converting, separate from the game - there’s no matter in which of these languages it will be because I have these compilers.

7 Replies

Please log in or register to post a reply.

dk 158 Mar 21, 2003 at 08:28

Do you mean you want to include the picture data inside the executable? If you using delphi, I’m not sure how. But for Visual C++, all you have to do is use resources. If you want more details on how do it, then let me know :D

DarkLight 101 Mar 21, 2003 at 16:13

I know how to include bitmaps and waves with VC++. But that is not what I need. I meant is that I want to get the RGB values for all pixels in the bitmap (bitmap is stored is this way: header and BGR values, BGR means that it is stored Blue Green Red instead of Red Green Blue) and put all RGB values for all pixels of all bitmaps into one file, called textures.dat
In this way the game will be easy to port on Linux and graphics can be updated
Also I don’t know how to build bitmaps into resource of Delphi :( I know how only if VCL is used but my game don’t use VCL

dk 158 Mar 21, 2003 at 17:31

oh ok, now I understand your question.

Ok, first of all you need to load the pixel data of the file into memory. Having an array like this is good enough.

unsigned char *data = new unsigned char[width*height*bpp];      // where bpp is bytes per pixel

then load the pixel data from the file into that array. Parsing files depends from format to format. But since your loading BMP files, then its easy (if no compression is applied). Its just a matter of copying. All you need is to point the file pointer to the pixel data and do a fread() or similar in a for loop. To make the pixel data consistent with the RGB sequence instead of the BGR sequence just do this:

     for (int i=0;i<height;i++)
          int index = i*width*3;
          int j,k;
          for (j=0;j<width;j++)
              data[index+j*3+2] = read8BitValue(filePtr, bytesRead);
              data[index+j*3+1] = read8BitValue(filePtr, bytesRead);
              data[index+j*3] = read8BitValue(filePtr, bytesRead);

Lets say for the sake of clarity that you are loading a 24-bit image, then each 4 indicies of the array would represent a pixel. e.g. data[0] represents the blue, data[1] green, data[2] blue, etc…

To write all that to a file, all you need to is to fwrite() the data array to the file. You will also need to store other information, like width, height, depth, etc… in the file.

I hope I answered your question. I can post a whole demo on loading BMP files if you want, but then it would be worth to make it a tutorial for the general public.

DarkLight 101 Mar 21, 2003 at 17:55

I’m not going to teaching myself, I’m just seeking for cutting-time solution. I know the basic idea but my problem is reading the file. I have no expirience with file manipulation in C++ and I know nothing about BMP header. I’m writting my games in Delphi/OpenGL or C++/OpenGL (Delphi is my prefered tool, becouse I’m writting Windows applications with it). If you can make bmp to array converter I’ll be gratefull
I’ll post demo and level editor these days, I’m just seeking for good enought web server

dk 158 Mar 21, 2003 at 18:00

Ok, I’ll post a demo sometimes tomorrow if not today…. However, I will be writing it in C/C++, not Delphi, as I have little experience with Pascal.

baldurk 101 Mar 21, 2003 at 18:26

file manipulation in C++ I can’t really help you with, but here’s a crash course in C file manipulation.

FILE *filePointer;

filePointer = fopen("filename", "rb"); // in windows, you need to use "rb", but in most other OSes, you only need "r"

  printf("something went wrong!1!1!1!1\n");
  return 42;

char string[512];

fgets(string, 512, filePointer);


just to explain that, you first declare a FILE *foo variable. It must be a pointer to a FILE.
Then you use fopen to open the file. The first parameter is the filename, the second is how to open it. “r” is “for reading”.
Then you check to see that it isn’t NULL. If it is, something went wrong and we abort.
Then we declare a string that’s 512 chars long, and read 512 chars into it. fgets reads chars into the first argument, until either the second argument is reached (ie. 512 chars have been read) or the end of the line is reached.
Then we close the file.
There are many more functions you can use, so look them up :)

DarkLight 101 Apr 17, 2003 at 17:30

Ok, I wrote little program in Delphi, which converts images into arrays. It uses high-level libraries (for Delphi programmers: VCL’s TImage class), becouse I cannot work with files in binary mode :) But It works. I just wondering is such program is useful for other developers (not me) and I’m wondering of interface and futures of this program. I also wondering what formats program should support - the idea is to give the developers ability to create .txt file and then paste the array direct to the code. This means that there are should be options about delimiter and the way array is stored in general (hexadecimal, decimal, etc.). If you think that this program should be finished (it will be able to convert jpeg files as well as bmp) please let me know so I’ll make it and send it to apex. But I have no many time, becouse of this I want to know if someone really need this or I can skip this project and continue developing DLTank.
Also, suggestion about interface are needed. I have no idea. For now there are only menu with open, save and exit commands and image is visualized in center of the form
Thank you