Transcript PPM Files
Image representation methods
Bitmap graphic method of a black-and-white image
Representation of color pixels
Representation of image data
• • • • • Images (e.g. digital photos) consist of a rectanglular array of discrete picture elements called pixels.
An image consisting of 200 pixels rows of 300 pixels per row contains 200 x 300 = 60,000 individual pixels.
The Portable PixMap (.
ppm ) format is a particularly simple way used to encode a rectangular image (picture) as an uncompressed data file.
The .ppm file can be viewed with a number of tools, including xv and gimp .
Other well-known image formats include JPEG (.jpg), TIFF (.tif), and PNG (.png)
Types of Images
• • • • We will use .ppm files to represent two types of images A .ppm image file contains the representation of an image (like a .jpg or .gif file) and can be in one of several different formats for an image. The PPM (Portable PixMap) format is used to represent images that contain color. A PGM (Portable GrayMap) format is used for grayscale images. The format of the file can be determined by the first two bytes. The code “P5” indicates that this is a PGM file, the code “P6” indicates this is a PPM file.
Types of Images
• Grayscale images – correspond to black / white photographs – – each pixel consists of 1 byte which should be represented as unsigned char a value of 0 is solid black – – a value of 255 is bright white intermediate are "gray" values of increasing brightness.
Types of Images
• Color images – – – correspond to color photographs each pixel consists of 3 bytes with each byte represented as an unsigned char this format is called RGB. three bytes represent the – – • • red component green component • blue component when red == green == blue a grayscale "color" is produced.
(255, 255, 255) is bright white.
Images
Colors are additive (255, 255, 0) = red + green = bright yellow (255, 0, 255) = red + blue = magenta (purple) (0, 255, 255) = green + blue = cyan (turquoise) (255, 255, 255) = red + green + blue = white
PPM file structure
A ppm file consists of two components: – ppm header – packed image data
PPM file structure
Example of a .
ppm header for a color image P6 # This is a comment 600 400 255 – – – – – The P6 indicates this is a color image (P5 means grayscale) The width The height of the image is 600 pixels of the image is 400 pixels The 255 is the maximum value of a pixel Following the 255 is a \n (0x0a) character.
PPM file structure
The image data – – The red component of the upper left pixel must immediately follow the new line.
For the image described earlier, there must be a total of 3 x 600 x 400 = 72,000 bytes of data.
Processing Heterogeneous Input Files
As we have seen, the header of a .ppm file contains mixed input data .
While scanf() provides a handy way to process input files consisting of only numeric data, the handling of mixed input can be more difficult.
Processing Heterogeneous Input Files
The following is an example of the header for a PGM file: P5 # CREATOR: XV Version 3.10a Rev: 12/29/94 (PNG patch 1.2) # This is another comment 1024 814 # So is this 255 Items of useful information in this header include: P - this is a .ppm file 5 1024 - the number of columns of pixels in each row of the 814 - the number of rows of pixels in the image 255 - this is a .ppm file containing a grayscale PGM (as opposed to color “P6”) image image - the maximum brightness value for a pixel
Processing Heterogeneous Input Files
Comment lines Lines beginning with # are comments .
Any number (including 0) of comment lines may be present.
It is possible for all the useful values to appear on one line: P5 1024 814 255 Or they could be specified as follows: P5 1024 814 255 Or they could be specified as follows: P5 # 1024 # This is a comment 814 255
Reading
.ppm
headers with
fgets()
and
sscanf()
Unfortunately life is rarely nice and simple and this program won't work if the .ppm header looks like P6 # comment 768 # another
Reading
.ppm
headers with
fgets()
and
sscanf()
Because of the arbitrary location and number of comment lines there is no good way to read the data using scanf(). A better approach is to use a combination of fgets() and the sscanf() function. The sscanf() from a function operates in a manner similar to but instead of consuming data from a file it will consume data memory resident buffer. fscanf() Like fscanf() it returns the number of items it successfully consumed from the buffer.
Reading
.ppm
headers with
fgets()
and
sscanf()
So if the .ppm header is nice and simple like: P6 768 1024 255 the following program would suffice to read it:
13 int main() 14 { 15 char id[3] = {0, 0, 0}; /* Will hold P5 or P6*/ 16 17 19 20 21 22 23 24 25 27 28 29 } long vals[5]; /* The 3 ints */ int count = 0; /* # of vals so far */ char *buf = malloc(256); /* the line buffer */ fgets(buf, 256, stdin); id[0] = buf[0]; id[1] = buf[1]; count = sscanf(buf + 2, “%d %d %d”, &vals[0], &vals[1], &vals[2]); printf(“Got %d vals \n”, count); printf(“%4d %4d %4d\n”, vals[0], vals[1],vals[2]);
Buliding a .
ppm
file
/ * image1.c */ /* Construct a solid color image of the specified color */ /* Input arguments */ /* width in pixels */ /* height in pixels */ /* red value */ /* green value */ /* blue value */ #include
Buliding a .
ppm
file
int main() { int width; int height; int count = 0; unsigned int red; unsigned int green; } unsigned int blue; fscanf(stdin, "%d", &width); fscanf(stdin, "%d", &height); fscanf(stdin, "%d", &red); fscanf(stdin, "%d", &green); fscanf(stdin, "%d", &blue); make_ppm_header(width, height); make_ppm_image(width, height, red, green, blue); return 0;
Building
a .ppm file
/* create the ppm image void make_ppm_image(int width, int height, unsigned char r, unsigned char g, unsigned char b) { } int count = 0; int size = width * height; while (count < size) { make_pixel(r, g, b); count = count + 1; }
Buliding a .
ppm
file
/* print void make_pixel(int r, int g, int b) { fprintf(stdout, “%c%c%c”, r, g, b); } /* print the ppm header */ void make_ppm_header(int width, int height) { fprintf(stdout, “P6\n”)’ fprintf(stdout, “%d %d %d\n”, width, height, 255); }
The .ppm file
You can view the details of the file with the command.
ls -l rlowe@joey7[102] ls -l pic1.ppm
-rw------- 1 rlowe faculty 90015 Sep 14 20:35 pic1.ppm
Note: size of file is 90015 15 (header length) 3 x 200 x 150 = 90000 (image data) 90015
The image
Example 2
/* CPSC 2100: redgreen.c
This program creates a ppm file for a 600x400 box, that is red in the left half and green in the right half. The ppm image is written to the file specified on the command line, e.g. the program is initiated as: ./redgreen output-filename which would create the PPM image file specified by output-filename */
Example 2
#include
Example 2
/* Open the PPM output file */ if (argc != 2) { fprintf(stderr, "Usage: ./redgreen output-filename\n"); exit(1); } imageFP = fopen(argv[1], "w"); assert(imageFP != NULL); img = (pixel_t *) malloc(sizeof(pixel_t) * 600 * 400); if(img == NULL) { fprintf(stderr, "Could not malloc memory for img. Exiting\n"); exit(2); }
Example 2
pixaddr = img; /* Write the ppm header */ fprintf(imageFP, "P6 600 400 255\n"); /* Write the pixel data */ for (rowNdx = 0; rowNdx < 400; rowNdx++) { for (colNdx = 0; colNdx < 600; colNdx++) { pixaddr->blue = 0; /* No blue in image */ if (colNdx < 300 ) { /* Left half of image */ pixaddr->red = 255; pixaddr->green = 0; } else { /* Right half of image */ pixaddr->red = 0; pixaddr->green = 255; } pixaddr++; } }
Example 2
/* Write out the pixel */ fwrite(img, sizeof(pixel_t), 600 * 400, imageFP); /* release allocated memory */ free(img); return 0; } The code produces the following red-green image