LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2004, 11:59 PM   #1
ideasman
LQ Newbie
 
Registered: Mar 2004
Location: Australia
Distribution: Home brew
Posts: 25

Rep: Reputation: 15
C Number crunching example please?


Hi, Im a python/blender programmer but I need to write some C code because of speed issues.

Would sombody be able to write a liitle program that opens an image file and loops through each pixel?
I need to be able to access the pixel values as an int's from 0 to 255 and stip out an image.

If this is greedy then a script that reads numeric values from a file and doubles them would be good, I could then modify it to my needs.

Thanks-
Cam
 
Old 04-09-2004, 01:25 AM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Loading what type of image? png, bmp etc...

Anything except bmp or similar uncompressed types isn't going to be a little bit of code.
 
Old 04-09-2004, 02:25 AM   #3
ideasman
LQ Newbie
 
Registered: Mar 2004
Location: Australia
Distribution: Home brew
Posts: 25

Original Poster
Rep: Reputation: 15
Realy any image type will do- RAW, PNM, PCX, RGB, Whatevers easyest-
A reries of rgb values if that easier-

I just want to test an image processing function, Ill worry about the format later.

- Cam
 
Old 04-09-2004, 02:48 AM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Okay heres the code to load a bmp image

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

typedef struct {
    int width;
    int height;
    unsigned char *data;
} ImageData;

/* simple loader for 24bit bitmaps (data is in rgb-format) */
int loadBMP(char *filename, ImageData *image )
{
    FILE *file;
    unsigned short int type;
    long int offsetBits;
    short int numPlanes;
    short int numBits;
    long int imageSize;
    int i;
    unsigned char temp;
    /* make sure the file is there and open it read-only (binary) */
    if ((file = fopen(filename, "rb")) == NULL)
        return 0;

    if(!fread(&type, sizeof(short int), 1, file))
        return 0;

    /* check if file is a bitmap */
    if (type != 19778)
        return 0;

    /* get the file size */
    /* skip file size and reserved fields of bitmap file header */
    fseek(file, 8, SEEK_CUR);
    /* get the position of the actual bitmap data */
    if (!fread(&offsetBits, sizeof(long int), 1, file))
        return 0;
  
    /* skip size of bitmap info header */
    fseek(file, 4, SEEK_CUR);
    /* get the width of the bitmap */
    fread(&image->width, sizeof(int), 1, file);
    /* get the height of the bitmap */
    fread(&image->height, sizeof(int), 1, file);
    /* get the number of planes (must be set to 1) */
    fread(&numPlanes, sizeof(short int), 1, file);
    if (numPlanes != 1)
        return 0;

    /* get the number of bits per pixel */
    if (!fread(&numBits, sizeof(short int), 1, file))
        return 0;

    if (biBitCount != 24)
        return 0;

    /* calculate the size of the image in bytes */
    imageSize = image->width * image->height * 3;
    image->data = malloc(imageSize);
    /* seek to the actual data */
    fseek(file, offsetBits, SEEK_SET);
    if (!fread(image->data, imageSize, 1, file))
        return 0;

    /* swap red and blue (bgr -> rgb) */
    for (i = 0; i < imageSize; i += 3)
    {
        temp = image->data[i];
        image->data[i] = image->data[i + 2];
        image->data[i + 2] = temp;
    }
    return 1;
}
to use
Code:
char *filename="myimage.bmp";
ImageData *image;

if ( loadBMP( filename, image ) == 1 )
   /* success! */
else
   /* failed */

I haven't tested it. Its an adaption of some code from NeHe. I haven't tested this particular version, but it should work as is. The datas in one big array to access any pixel at a position of x,y use

image->[ (x + (y * image->width )) * 3 );

Only 24 bit, bmp images are supported.

Last edited by leonscape; 04-09-2004 at 02:50 AM.
 
Old 04-09-2004, 05:02 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Here's a page with links to all kinds of bitmap image formats:
http://www.dcs.ed.ac.uk/home/mxr/gfx/2d-hi.html

May you could also use the SDL, and maybe SDL_Image libs.
SDL has all kind of functions for fiddling with pixels, colors and palettes. But it lacks many functions for saving bitmap pictures though.
 
Old 04-09-2004, 10:26 AM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
You can use gdk-pixbuf to read most image formats, without caring which format you are readig. I wrote up a small example here: http://alf.hopto.org/misc/imgtest/ . The Makefile is important: you have to use pkg-config to get the right flags to gcc. I just included code for loading and saving images (The program reads foo.png and writes it back out as foo2.png), but here is an example for reading a single pixel: http://developer.gnome.org/doc/API/2...html#put-pixel
 
Old 04-09-2004, 10:27 AM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Oh yeah, the docs for gdk-pixbuf are here: http://developer.gnome.org/doc/API/2...buf/index.html
 
Old 04-09-2004, 04:52 PM   #8
ideasman
LQ Newbie
 
Registered: Mar 2004
Location: Australia
Distribution: Home brew
Posts: 25

Original Poster
Rep: Reputation: 15
Thank you very much!! I git the image loading and I can now loop through *image and print out values.

My problem is now that I need to deal with the r/g/b pixel values as ints from 0 to 255
eg
if (image[i] > 128) {....

How would I do this?
Is it possible to do it without re-assigning the value/data type for each pixel?
-Thanks

PS- I know there are a few image loading LIBS but Id rather not rely on any ATM, there are enough simple image formats about.

Last edited by ideasman; 04-09-2004 at 04:53 PM.
 
Old 04-09-2004, 05:56 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're if() statement is already correct. image[i] would be the red value, image[i+1] the green, and image[i+2] the blue.

If you're looking to combine the 3 values into a single 8-bit int then there's some things to consider like defining your palette. I mean like, what color does color 0 actually correspond to? What does color 1 actually correspond to, all the way up through 255.
 
Old 04-09-2004, 11:14 PM   #10
ideasman
LQ Newbie
 
Registered: Mar 2004
Location: Australia
Distribution: Home brew
Posts: 25

Original Poster
Rep: Reputation: 15
When I do a printf(%d, image[i]) // exact syntax might not be right in this example
It prints a huge number like -11643232, worth noting that the first number printer is 255, when cycling through image[]

how can I print image[i] as a value from 0 to 255? even for debugging
Thanks- you have been very helpfull.
 
Old 04-10-2004, 10:21 PM   #11
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
%d is for a signed decimal integer. So it will read from image[i] to image[i+3] ( int are 32bits while you only want 8 ) To Change this behaviour cast image[i] to a char ( 8 bit ). So the command would be
Code:
printf( "%d ", (unsigned char)image[i] );
I set unsigned so it gives you values from 0 to 255, If you just said char it would print values from -128 to 127.

Last edited by leonscape; 04-10-2004 at 10:22 PM.
 
Old 04-11-2004, 02:15 AM   #12
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
You really should cast to the type that printf expects, (int) in this case. On my machine it doesn't seem to matter either way, unless you use a long long format to printf:
Code:
03:13 aluser@alf:~/C$ cat cast.c
#include <stdio.h>

int main() {
        unsigned char foo[12];
        int i;
        for (i=0; i < 12; ++i) {
                foo[i] = i;
        }
        foo[4] = 255;
        printf("size of long long: %d\n", sizeof(long long));
        printf("foo[4] = %x\n", foo[4]);
        printf("foo[4] = %x\n", (unsigned char)foo[4]);
        printf("foo[4] = %llx\n", (unsigned char)foo[4]);
        printf("foo[4] = %llx\n", (unsigned long long)foo[4]);
        return 0;
}
03:13 aluser@alf:~/C$ gcc -o cast cast.c
03:13 aluser@alf:~/C$ ./cast
size of long long: 8
foo[4] = ff
foo[4] = ff
foo[4] = bffffa18000000ff
foo[4] = ff
03:13 aluser@alf:~/C$
Even in that case, it does not seem to be reading from adjacent parts of the array.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is a major number and a minor number ? Menestrel Linux - Newbie 2 10-11-2004 07:53 AM
how do you edit your virtual console number? (or VT number 3) jjorloff1 Linux - General 2 04-03-2004 07:21 PM
Octave-Number Crunching Language? southsibling Linux - Software 5 02-22-2004 09:26 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM
seti, boot from floppy or CD for linux-cluster WU crunching JoDerBaer Linux - Newbie 1 06-11-2002 08:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration