Artist needs help with bitmaps

M

Michael Schwab

Hi,

I am an artist with limited computer knowledge (but I am able to compile
some code) who needs for a project I am working on a way to read in a bmp
file, loop through the pixels and store their rgb value in a file in the
format (x, y, r, g, b). I want to use these values as the basis for some
paintings.
I looked around on the internet to find some code, but my skills are just
not enough to do something useful with what I have found. Unfortunately,
images are rather large, so I need a way to automaise this. I am sure that
there is somebody out there who could do this very quickly (or has some code
already). It would mean a lot to me - since I am increasingly stuck with
this and nobody I know seems to be able to help me.

All the best,

Michael
 
J

James Hu

I am an artist with limited computer knowledge (but I am able to compile
some code) who needs for a project I am working on a way to read in a bmp
file, loop through the pixels and store their rgb value in a file in the
format (x, y, r, g, b). I want to use these values as the basis for some
paintings.
I looked around on the internet to find some code, but my skills are just
not enough to do something useful with what I have found. Unfortunately,
images are rather large, so I need a way to automaise this. I am sure that
there is somebody out there who could do this very quickly (or has some code
already). It would mean a lot to me - since I am increasingly stuck with
this and nobody I know seems to be able to help me.

If you are trying to make sense of some C code, and the question is
about the C language (and not about and graphics library APIs that the
program is using), then you can post the confusing portion here and
someone may be able to explain it to you.

If you are trying to make sense of some graphics library APIs,
then you are better off asking in a graphics newsgroup. Perhaps
comp.graphics.misc as a starting point, and they may be able to answer
your question or point you to a more specific group.

If you are coding up your project and you run into a C related problem
that you cannot figure out for yourself, first see if your problem is a
frequently asked question:

http://www.eskimo.com/~scs/C-faq/top.html

If not, then you can post what you have here and ask what is wrong.
However, if your question is about how to use a graphics library API,
then you will be redirected to a graphics newsgroup.

Generally speaking, when you read in a bitmap file, you will need to
know the file format. The file format will tell you how each pixel
in the bitmap file is encoded. For example, perhaps the bitmap file
is encoded like:

XDIM, YDIM
COLOR_1_1, COLOR_2_1, ... COLOR_XDIM_1
COLOR_1_2, COLOR_2_2, ... COLOR_XDIM_2
.
.
.
COLOR_1_YDIM, COLOR_2_YDIM, ... COLOR_XDIM_YDIM

Where XDIM and YDIM are scalars describing the dimensions of the bitmap,
and each color consists of #RRGGBB, where each color component is
specified by 2 hexadecimal digits.

So, a 3x3 bitmap would look like:

3, 3
#000000, #FFFFFF, #000000
#FFFFFF, #FFFFFF, #FFFFFF
#000000, #FFFFFF, #000000

Your job would be to write a C program that read in such a file and
converts it to the format you want. To read in the file format above,
you can use calls to the C functions fgets, sscanf, and strtol. You
may want to store each pixed of the bitmap into some data structure
that you will later iterate over to generate your output. Your
data structure may involve using a struct.

Your specification of the output format was not very specific, but it
seemed like:

(1, 1, 0, 0, 0)
(2, 1, 255, 255, 255)
(3, 1, 0, 0, 0)
(1, 2, 255, 255, 255)
(2, 2, 255, 255, 255)
(3, 2, 255, 255, 255)
(1, 3, 0, 0, 0)
(2, 3, 255, 255, 255)
(3, 3, 0, 0, 0)

Each line of output could be generated by a single print statement
using the C function printf.

If you have not already done so, please acquire a good C reference, and
lookup any function or term that I have mentioned that seems unfamiliar
to you.

-- James
 
R

Rouben Rostamian

Michael Schwab said:
I am an artist with limited computer knowledge (but I am able to compile
some code) who needs for a project I am working on a way to read in a bmp
file, loop through the pixels and store their rgb value in a file in the
format (x, y, r, g, b). I want to use these values as the basis for some
paintings.

This is off-topic in this newsgroup. Nevertheless, here is someting
to help you in your quest.

I don't know what operating system you use. I use Linux. Several
image conversion utilities on Linux let you convert an image from
one format to any other format.

Such utilities may be available in other platforms as well.

What you need to do is to convert your image to the "ASCII PPM" format.
An ASCII PPM file stores the image as a sequence of r, g, b triples.
where the scanning order corresponds to reading the image in the
English reading order (left to right, top to bottom). That seems to
be what you want.
 
R

Robert Stankowic

Michael Schwab said:
Hi,

I am an artist with limited computer knowledge (but I am able to compile
some code) who needs for a project I am working on a way to read in a bmp
file, loop through the pixels and store their rgb value in a file in the
format (x, y, r, g, b). I want to use these values as the basis for some
paintings.
I looked around on the internet to find some code, but my skills are just
not enough to do something useful with what I have found. Unfortunately,
images are rather large, so I need a way to automaise this. I am sure that
there is somebody out there who could do this very quickly (or has some code
already). It would mean a lot to me - since I am increasingly stuck with
this and nobody I know seems to be able to help me.

Michael,
what you are looking for _can_ be done in standard C, but it is not as
simple as you might think.
www.wotsit.org has the information about the format of a .bmp file
Use this information to write some code which reads the file and separates
the descriptional part from the actual data, something like

#include <stdio.h>
#include <stdlib.h>

#define CHUNK_SIZE 4096 /*adjust that to the actual size of the header
including the color table*/
int main( int argc, char *argv[] )
{
FILE *bmp_in;
int success = 0;
size_t size_read;
size_t bmp_size;
size_t hdr_size;
unsigned char *bmp_data;
unsigned char *my_hdr;

my_hdr = malloc(CHUNK_SIZE);
if(my_hdr)
{
if(argc == 2)
{
bmp_in = fopen(argv[1], "rb");
if(bmp_in)
{
size_read = fread(my_hdr, 1, CHUNK_SIZE, bmp_in);
if(size_read == CHUNK_SIZE)
{
/*now the header info is in your buffer (in my_bmp)
[OT] look at the description of the file format
(www.wotsit.org).
The header contains info about the size of the whole
bmp. let's assume you have extracted the size correctly
and stored it in bmp_size and stored the size of the header
correctly in hdr_size [/OT]*/
bmp_data = malloc(bmp_size);
if(bmp_data)
{
fseek(bmp_in, hdr_size, SEEK_SET); /*skip the header*/
size_read = fread(bmp_data, 1, bmp_size, bmp_in);
if(size_read == bmp_size)
{
/*now you have the pixel information in the malloc()ed
array. [OT]The header also contains info about the bmp
dimensions (width and height) as well as a color table.
Use this information to scan the array and convert the
values found there to the proper format for the output
(fprintf() is your friend)[/OT]
A lot of work, but not really difficult....
if all was successful, set:*/
success = 1;
}
}
}
}
}
}
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

HTH
Robert
 
A

Arthur J. O'Dwyer

I am an artist with limited computer knowledge (but I am able to compile
some code) who needs for a project I am working on a way to read in a bmp
file, loop through the pixels and store their rgb value in a file in the
format (x, y, r, g, b). I want to use these values as the basis for some
paintings.

The format of BMP files is off-topic here in comp.lang.c, but
I can still help! :) If you are looking for a C program that
can output (x, y, r, g, b) based on an input BMP image, then
you can try downloading this really basic [but pure C] library:

http://www.contrib.andrew.cmu.edu/~ajo/free-software/ImageFmtc.h
http://www.contrib.andrew.cmu.edu/~ajo/free-software/ImageFmtc.c

plus a simple main routine in another file, say, "myprog.c":


#include <stdio.h>
#include <stdlib.h>
#include "ImageFmtc.h"

int main(int argc, char **argv)
{
unsigned char (*image)[3] = NULL;
int w, h;
int x, y;

if (argc != 2) {
puts("Usage: myprog <something.bmp>");
return EXIT_FAILURE;
}

if (ReadBMP(argv[1], &image, &w, &h) != 0) {
puts("Error: I couldn't read that BMP file!");
free(image);
return EXIT_FAILURE;
}

printf("Pixels in form (x, y, r, g, b)\n");

for (y=0; y < h; ++y) {
for (x=0; x < w; ++x) {
int ofs = y*w+x;
printf("(%d, %d, %d, %d, %d)\n",
x, y,
image[ofs][0], image[ofs][1], image[ofs][2]);
}
}

free(image);
return 0;
}


Now just compile the two C files together, something like this
(depending on your C compiler implementation):

gcc -W -Wall -O2 -ansi -pedantic -o myprog.exe myprog.c ImageFmtc.c

and run the resulting program like this:

myprog.exe myimage.bmp


Is this close to what you want?

If not, try posting to comp.programming or
to some image-processsing or image-formats
group (I don't know any off the top of my
head, but try Google Groups for ideas).

-Arthur
 
M

Michael Schwab

Dear Arthur,

thank you so much for you help. I finally, I can't tell you how much trouble
I had with this one, was able to work out the colour values I needed as the
basis for my new project! Your help was spot on!
Thanks also to everybody else who answered and sorry for posting my question
to this group.

Michael

Arthur J. O'Dwyer said:
I am an artist with limited computer knowledge (but I am able to compile
some code) who needs for a project I am working on a way to read in a bmp
file, loop through the pixels and store their rgb value in a file in the
format (x, y, r, g, b). I want to use these values as the basis for some
paintings.

The format of BMP files is off-topic here in comp.lang.c, but
I can still help! :) If you are looking for a C program that
can output (x, y, r, g, b) based on an input BMP image, then
you can try downloading this really basic [but pure C] library:

http://www.contrib.andrew.cmu.edu/~ajo/free-software/ImageFmtc.h
http://www.contrib.andrew.cmu.edu/~ajo/free-software/ImageFmtc.c

plus a simple main routine in another file, say, "myprog.c":


#include <stdio.h>
#include <stdlib.h>
#include "ImageFmtc.h"

int main(int argc, char **argv)
{
unsigned char (*image)[3] = NULL;
int w, h;
int x, y;

if (argc != 2) {
puts("Usage: myprog <something.bmp>");
return EXIT_FAILURE;
}

if (ReadBMP(argv[1], &image, &w, &h) != 0) {
puts("Error: I couldn't read that BMP file!");
free(image);
return EXIT_FAILURE;
}

printf("Pixels in form (x, y, r, g, b)\n");

for (y=0; y < h; ++y) {
for (x=0; x < w; ++x) {
int ofs = y*w+x;
printf("(%d, %d, %d, %d, %d)\n",
x, y,
image[ofs][0], image[ofs][1], image[ofs][2]);
}
}

free(image);
return 0;
}


Now just compile the two C files together, something like this
(depending on your C compiler implementation):

gcc -W -Wall -O2 -ansi -pedantic -o myprog.exe myprog.c ImageFmtc.c

and run the resulting program like this:

myprog.exe myimage.bmp


Is this close to what you want?

If not, try posting to comp.programming or
to some image-processsing or image-formats
group (I don't know any off the top of my
head, but try Google Groups for ideas).

-Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top