ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#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;
}
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
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.
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
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.
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.
%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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.