Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Displaying histogram pixel values from PGM image (p5) using Pure C without any image processing library
- #include <stdio.h>
- #include <stdlib.h>
- #define WIDTH 1024
- #define HEIGHT 768
- #define READ_IMAGE_NAME "MY_PGM_FILE_NAME.pgm"
- void print_histogram_table(int *histog);
- main() {
- FILE *fp;
- int i,j, height= HEIGHT, width=WIDTH;
- char line[100];
- // Color depth is 255.
- unsigned char pixel_value;
- fp = fopen(READ_IMAGE_NAME,"r");
- // get the first four lines.
- fgets (line,100,fp);
- fgets (line,100,fp);
- fgets (line,100,fp);
- fgets (line,100,fp);
- // Histogram helper
- int histo[65536];
- int x;
- for ( x =0; x < 65536; x++) {
- histo[x] = 0;
- }
- for(j=0;j<height;j++) {
- for(i=0;i<width;i++) {
- fread(&pixel_value, sizeof(unsigned char), 1, fp);
- // Turn on the code below, if you want to check color on specific row and column.
- // printf("row num. %d column num. %d pixel value=%dn",j,i,pixel_value);
- histo[pixel_value]++;
- }
- }
- // Make histogram
- print_histogram_table(histo);
- fclose(fp);
- getch();
- }
- void print_histogram_table(int *histog)
- {
- int x;
- for (x= 0; x < 255; x++) {
- if ( histog[x] != 0)
- printf("Color number %d count %dn", x, histog[x]);
- }
- }
- int histo[65536];
- // ...
- print_histogram_table(histo);
- // ...
- void print_histogram_table(int *histog)
- {
- int x;
- for (x= 0; x < 255; x++) {
- if ( histog[x] != 0)
- printf("Color number %d count %dn", x, histog[x]);
- }
- fp = fopen(READ_IMAGE_NAME,"r");
- // change to:
- fp = fopen(READ_IMAGE_NAME,"rb");
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char* argv[])
- {
- char line[100];
- int width, height, maxval;
- unsigned char* image;
- unsigned int* histo;
- int i;
- FILE *fd;
- fd = fopen(argv[1], "rb");
- if (fd == NULL) {
- printf("could not open image filen");
- exit(1);
- }
- // magic number
- fgets(line, sizeof(line), fd);
- if (strcmp(line, "P5n") != 0) {
- printf("image is not in PGM format!n");
- fclose(fd);
- exit(1);
- }
- // skip comment
- fgets(line, sizeof(line), fd);
- // read header
- if (fscanf(fd, "%d %d %dn", &width, &height, &maxval) != 3) {
- printf("invalid headern");
- fclose(fd);
- exit(1);
- }
- if (maxval > 255) {
- printf("sorry, only 8-bit PGMs are supported at this time!n");
- fclose(fd);
- exit(1);
- }
- printf("width: %dnheight: %dnmaxval: %dn", width, height, maxval);
- // read image data
- image = malloc(width * height);
- if (fread(image, sizeof(unsigned char), width * height, fd) != width * height) {
- printf("could not read imagen");
- fclose(fd);
- exit(1);
- }
- fclose(fd);
- // compute histogram
- histo = (int*)calloc(maxval+1, sizeof(int));
- for (i = 0; i < width * height; i++)
- histo[image[i]]++;
- // print histogram
- for (i = 0; i <= maxval; i++)
- printf("Color number %d count %dn", i, histo[i]);
- // release memory
- free(image);
- free(histo);
- }
Advertisement
Add Comment
Please, Sign In to add comment