Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representation of image data

Similar presentations


Presentation on theme: "Representation of image data"— Presentation transcript:

1 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)

2 Representation of image data
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 a PGM file, the code “P6” indicates a PPM file.

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

4 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

5 PPM file structure A ppm file consists of two components: ppm header
packed image data

6 PPM file structure Example of a .ppm header for a color image P6
# This is a comment The P6 indicates this is a color image The width of the image is 600 pixels The height of the image is 400 pixels The 255 is the maximum value of a pixel Following the 255 is a \n (0x0a) character.

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

8 Example /* CPSC 1070: 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 */

9 Example const int WIDTH = 600; const int HEIGHT = 400;
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "image.h" int main(int argc, char *argv[]) { color_t *img; /* image array */ color_t *pixaddr = NULL; /* address of current pixel */ int rowNdx, colNdx; /* row and column indices */ int half; /* half-way width */

10 Example /* Declare and open the PPM output file */
FILE *imageFP = fopen("img.ppm", "w"); half = WIDTH / 2; pixaddr = img; /* Write the ppm header */ fprintf(imageFP, "P6 %d %d 255\n", WIDTH, HEIGHT);

11 Example /* Write the pixel data */
for (rowNdx = 0; rowNdx < HEIGHT; rowNdx++) { for (colNdx = 0; colNdx < WIDTH; colNdx++) { pixaddr->blue = 0; /* No blue in image */ if (colNdx < half ) { /* Left half of image */ pixaddr->red = 255; pixaddr->green = 0; } else { /* Right half of image */ pixaddr->red = 0; pixaddr->green = 255; pixaddr++;

12 Example 2 /* Write out the pixel */
fwrite(img, sizeof(pixel_t), WIDTH * HEIGHT, imageFP); /* release allocated memory */ free(img); return 0; } The code produces the following red-green image


Download ppt "Representation of image data"

Similar presentations


Ads by Google