Artist needs help with bitmaps

Discussion in 'C Programming' started by Michael Schwab, Nov 17, 2003.

  1. 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
     
    Michael Schwab, Nov 17, 2003
    #1
    1. Advertising

  2. Michael Schwab

    James Hu Guest

    On 2003-11-17, Michael Schwab <> wrote:
    > 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
     
    James Hu, Nov 17, 2003
    #2
    1. Advertising

  3. [OT] Re: Artist needs help with bitmaps

    In article <bpabnb$r89$>,
    Michael Schwab <> wrote:

    >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.

    --
    Rouben Rostamian
     
    Rouben Rostamian, Nov 17, 2003
    #3
  4. "Michael Schwab" <> schrieb im Newsbeitrag
    news:bpabnb$r89$...
    > 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
     
    Robert Stankowic, Nov 17, 2003
    #4
  5. On Mon, 17 Nov 2003, Michael Schwab wrote:
    >
    > 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
     
    Arthur J. O'Dwyer, Nov 17, 2003
    #5
  6. Thank You!!! & apologies for the off-topic post

    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" <> schrieb im Newsbeitrag
    news:p...
    >
    > On Mon, 17 Nov 2003, Michael Schwab wrote:
    > >
    > > 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
     
    Michael Schwab, Nov 18, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rithesh Pai
    Replies:
    1
    Views:
    476
  2. Bitmaps in picturebox

    , Oct 24, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    460
    Kevin Spencer
    Oct 24, 2003
  3. =?Utf-8?B?bWpjYXN0?=

    byte[]s, images and bitmaps with ASP.NET

    =?Utf-8?B?bWpjYXN0?=, Jun 7, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    12,721
    Pedro Lopez
    Aug 21, 2008
  4. Thomas Richter

    Render bitmaps really fast

    Thomas Richter, Oct 20, 2003, in forum: Java
    Replies:
    6
    Views:
    643
    Thomas Weidenfeller
    Oct 21, 2003
  5. Peter Moscatt

    HELP: Bitmaps Using Tkinter ?

    Peter Moscatt, Mar 17, 2005, in forum: Python
    Replies:
    0
    Views:
    356
    Peter Moscatt
    Mar 17, 2005
Loading...

Share This Page