29
Image representation methods

Image representation methods. Bitmap graphic method of a black-and-white image

Embed Size (px)

Citation preview

Page 1: Image representation methods. Bitmap graphic method of a black-and-white image

Image representation methods

Page 2: Image representation methods. Bitmap graphic method of a black-and-white image

Bitmap graphic method of a black-and-white image

Page 3: Image representation methods. Bitmap graphic method of a black-and-white image

Representation of color pixels

Page 4: Image representation methods. Bitmap graphic method of a black-and-white image

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)

Page 5: Image representation methods. Bitmap graphic method of a black-and-white image

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.

Page 6: Image representation methods. Bitmap graphic method of a black-and-white image

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.

Page 7: Image representation methods. Bitmap graphic method of a black-and-white image

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.

Page 8: Image representation methods. Bitmap graphic method of a black-and-white image

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

Page 9: Image representation methods. Bitmap graphic method of a black-and-white image

PPM file structure

A ppm file consists of two components:– ppm header– packed image data

Page 10: Image representation methods. Bitmap graphic method of a black-and-white image

PPM file structure

Example of a .ppm header for a color imageP6# This is a comment600 400 255

– The P6 indicates this is a color image (P5 means grayscale)

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

Page 11: Image representation methods. Bitmap graphic method of a black-and-white image

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.

Page 12: Image representation methods. Bitmap graphic method of a black-and-white image

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

Page 13: Image representation methods. Bitmap graphic method of a black-and-white image

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 comment1024 814# So is this255

Items of useful information in this header include:P - this is a .ppm file5 - this is a .ppm file containing a grayscale PGM

(as opposed to color “P6”) image1024 - the number of columns of pixels in each row

of the image814 - the number of rows of pixels in the image255 - the maximum brightness value for a pixel

Processing Heterogeneous Input Files

Page 14: Image representation methods. Bitmap graphic method of a black-and-white image

Comment linesLines 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 255Or they could be specified as follows:P5 1024 814 255

Or they could be specified as follows:P5#1024# This is a comment814 255

Processing Heterogeneous Input Files

Page 15: Image representation methods. Bitmap graphic method of a black-and-white image

Unfortunately life is rarely nice and simple and this program won't work if the .ppm header looks likeP6# comment768# another

Reading .ppm headers with fgets() and sscanf()

Page 16: Image representation methods. Bitmap graphic method of a black-and-white image

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() function operates in a manner similar to fscanf() but instead of consuming data from a file it will consume data from a memory resident buffer.

Like fscanf() it returns the number of items it successfully consumed from the buffer.

Reading .ppm headers with fgets() and sscanf()

Page 17: Image representation methods. Bitmap graphic method of a black-and-white image

So if the .ppm header is nice and simple like:P6 768 1024 255the following program would suffice to read it:13 int main()14 {15 char id[3] = {0, 0, 0}; /* Will hold P5 or P6*/16 long vals[5]; /* The 3 ints */17 int count = 0; /* # of vals so far */19 char *buf = malloc(256); /* the line buffer */2021 fgets(buf, 256, stdin);22 id[0] = buf[0];23 id[1] = buf[1];24 count = sscanf(buf + 2, “%d %d %d”,25 &vals[0], &vals[1], &vals[2]);27 printf(“Got %d vals \n”, count);28 printf(“%4d %4d %4d\n”, vals[0], vals[1],vals[2]);29 }

Reading .ppm headers with fgets() and sscanf()

Page 18: Image representation methods. Bitmap graphic method of a black-and-white image

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 <stdio.h>void make_ppm_header(int width, int len);void make_pixel (unsigned char r, unsigned char g, unsigned char b);void make_ppm_image(int width, int height, unsigned char r, unsigned

char g, unsigned char b);

Page 19: Image representation methods. Bitmap graphic method of a black-and-white image

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;}

Page 20: Image representation methods. Bitmap graphic method of a black-and-white image

Building a .ppm file

/* create the ppm imagevoid 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; }}

Page 21: Image representation methods. Bitmap graphic method of a black-and-white image

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);}

Page 22: Image representation methods. Bitmap graphic method of a black-and-white image

The .ppm fileYou can view the details of the file with the ls -l command.

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

Page 23: Image representation methods. Bitmap graphic method of a black-and-white image

The image

Page 24: Image representation methods. Bitmap graphic method of a black-and-white 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

*/

Page 25: Image representation methods. Bitmap graphic method of a black-and-white image

Example 2

#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "rgb.h" int main(int argc, char *argv[]) { pixel_t *img; /* image array */ pixel_t *pixaddr = NULL; /* address of current pixel */ int rowNdx, colNdx; /* Row and column indices */ FILE *imageFP; /* PPM output file */

Page 26: Image representation methods. Bitmap graphic method of a black-and-white image

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

}

Page 27: Image representation methods. Bitmap graphic method of a black-and-white image

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++; } }

Page 28: Image representation methods. Bitmap graphic method of a black-and-white image

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

Page 29: Image representation methods. Bitmap graphic method of a black-and-white image

The image