Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 24, Digital fingerprints IIT BOMBAY Dr. Deepak B Phatak 2 IIT BOMBAY CS 101 - Lecture 24 Image files Overview Image representation Standard file formats Tools and utilities XPM files Sample program Retrieving fingerprint image from a scanner Scanner software and APIs Course Projects Dr. Deepak B Phatak 3 IIT BOMBAY CS 101 - Lecture 24 Image files Image representation revisited Digital images are a collection of pixel values These are arranged in an array (W x H) Each pixel value can be represented by 1 bit (m : mono colour, e.g. black and white) 8 bits (g : gray scale 0 black to 255 white) 24 bits (c: Red, Blue, Green, each one byte) One can have 16 million colours! Usually limited to 200 to 2000 colours A colour palette is used to map a pixel value to a specific colour. Multiple pixel values may map to same colour Dr. Deepak B Phatak 4 IIT BOMBAY CS 101 - Lecture 24 Image files Image representation in a data file Some compression is mandatory to keep the file size within limits 12 M pixel camera can produce 36 M bytes image Some compression is necessary lossy, lossless While storing information about an image in a file, we mainly need values of Width, Height, the type of colours present, and values for each pixel Several file formats have evolved over the years raw, png, bmp, tiff, giff, jpeg, xmp Refer to wikipedia (Image_file_formats) Dr. Deepak B Phatak 5 IIT BOMBAY CS 101 - Lecture 24 Image files XPM format Most file formats hold binary data Not visible, cannot be edited An editable format was desired, specially to create icons through normal text editing Created in 1989 by Daniel Dardailler and Colas Nahaboo in Groupe Bull Research Centre, France /* XPM */ static char * <pixmap name>[] = { "Values-string", "Colors-strings", "Pixel-strings"}; Dr. Deepak B Phatak 6 IIT BOMBAY CS 101 - Lecture 24 Image files XPM format Value string Width height ncolors chars_per_pixel, Array of key and colour values, each element typically has: Key c Hex value of colour, key is the colour code characters For 2 chars per pixel, it could be, say, X., or Aa, Each picture can have its own colour code Pixel strings Each row of in the image has one line in the file X.X.AaAaBqBwBq ., Dr. Deepak B Phatak 7 IIT BOMBAY CS 101 - Lecture 24 Image files Gray scale images The fingerprint images we will use are in Grayscale Each pixel is a shade of black to white Can be represented as a 1 byte pixel value A gray shade can be represented by a colour value which has identical red, green and blue pixel components, for example Hex 000000 is black Hex 808080 is some intermediate gray shade Hex FFFFFF is white Dr. Deepak B Phatak 8 IIT BOMBAY CS 101 - Lecture 24 Image files Sample (dot) image /* XPM */ static char * dot[] = { "5 5 2 1", ". c #000000", "X c #ff0000", " X ", " XXX ", "XXXXX", " XXX ", " X ", }; Dr. Deepak B Phatak 9 IIT BOMBAY CS 101 - Lecture 24 Image files Sample Image Dr. Deepak B Phatak 10 IIT BOMBAY CS 101 - Lecture 24 Image files Program // Program to create .xpm file for a sample image // written by D. B. Phatak for CS101 // Dept of CSE, IIT Bombay, Oct09 // Copyright under Creative Commons #include <iostream> #include <fstream> #include <string> #include <cstring> #include <math.h> using namespace std; Dr. Deepak B Phatak 11 IIT BOMBAY CS 101 - Lecture 24 Image files int main(){ unsigned char* Image; unsigned int nWidth, nHeight, val; char symbols[256][2], base1, base2,ch1,ch2; int i, j, k, pos, sympos; // Define colour palette chosen arbitrarily to //simplify subsequent value mapping // Aa Ab Ac ... upto Ay, then Ba, Bb, etc. // First character changes after every 25 symbols // second character is {a,b, .. y} repeated Dr. Deepak B Phatak 12 IIT BOMBAY CS 101 - Lecture 24 Image files base1 = 'A'; base2 = 'a'; for(k=0; k<=255; k++){ symbols[k][0]= base1 + k/25; symbols[k][1]= base2 + k%25; symbols[k][2]='\0'; } cout << symbols[0] << endl; Dr. Deepak B Phatak 13 IIT BOMBAY CS 101 - Lecture 24 Image files // fill artificial pixel values in an image array nWidth = 256 ; nHeight = 256; Image = new unsigned char[nWidth*nHeight]; for (i=0; i < nHeight; i++){ for (j=0; j < nWidth; j++){ pos = i * nWidth + j; Image[pos] = (unsigned char)j%256; } } Dr. Deepak B Phatak 14 IIT BOMBAY CS 101 - Lecture 24 Image files // Write the file in xpm format ofstream myfile; myfile.open("sampleI.xpm", ios::out); if (myfile.is_open()){ // Write first three lines myfile << "/* XPM */" << endl; myfile << "static char *sampleI[] = {" <<endl; myfile << "/* width height ncolors chars_per_pixel */" << endl; Dr. Deepak B Phatak 15 IIT BOMBAY CS 101 - Lecture 24 Image files // Write image header info line myfile << "\"" << nWidth<< " " << nHeight <<" " << 256 <<" " << 2 <<"\"," << endl; // write colour pallete header myfile << "/* colors */" << endl; Dr. Deepak B Phatak 16 IIT BOMBAY CS 101 - Lecture 24 Image files // insert colour palette for (i=0; i<256; i++){ myfile << '\"' << (char)symbols[i][0] <<(char) symbols[i][1]<< " c "; myfile.setf(ios::hex, ios::basefield); if (i < 16){ myfile <<"#" <<"0"<<i <<"0"<<i <<"0" <<i;} else{ myfile <<"#" << i << i << i;} myfile.unsetf(ios::hex); myfile << "\","<<endl; } Dr. Deepak B Phatak 17 IIT BOMBAY CS 101 - Lecture 24 Image files // Now output lines containing coded pixel values // for each row in the image for (i=0; i < nHeight; i ++){ myfile << "\""; // for each row, compose remaining line Dr. Deepak B Phatak 18 IIT BOMBAY CS 101 - Lecture 24 Image files for (j=0; j<nWidth; j++){ // get pixel value pos = i * nWidth + j; val = (unsigned int) Image[pos]; // locate two characters symbol for this pixel sympos = (val/25)*25+val%25; ch1 = (char)symbols[sympos][0]; ch2 = (char)symbols[sympos][1]; // and write these to the file myfile << ch1<<ch2; } Dr. Deepak B Phatak 19 IIT BOMBAY CS 101 - Lecture 24 Image files // Push closing quotes and newline myfile << "\","<<endl; } // Now write the last line in the file myfile << "};" << endl; myfile.close(); } else cout << "Unable to open file"; delete[] Image; return 0; } Dr. Deepak B Phatak 20 IIT BOMBAY CS 101 - Lecture 24 Image files Portion of XPM file (first few rows) /* XPM */ static char *sampleI[] = { /* width height ncolors chars_per_pixel */ "256 256 256 2", /* colors */ "Aa c #000000", "Ab c #010101", ... ... Dr. Deepak B Phatak 21 IIT BOMBAY CS 101 - Lecture 24 Image files Portion of XPM file (last 2 rows) AaAbAcAdAeAfAgAhAiAjAkAlAmAnAoApAqArAsAtAuA vAwAxAyBaBb ... JvJwJxJyKaKbKcKdKeKf, }; Dr. Deepak B Phatak 22 IIT BOMBAY CS 101 - Lecture 24 Image files Sample image revisited Dr. Deepak B Phatak 23 IIT BOMBAY CS 101 - Lecture 24 Image files Using a file name of your choice string filename, filenameWithextension ; // get a filename for fingerprint storage cout << " Give a filename without extension" << endl; cin >> filename; filenameWithextension= filenames + ".xpm"; myfile.open(filenameWithextension.c_str(), ios::out); Dr. Deepak B Phatak 24 IIT BOMBAY CS 101 - Lecture 24 Image files Sample fingerprint xpm file /* XPM */ static char *LT0person2[] = { /* width height ncolors chars_per_pixel */ "352 544 256 2", /* colors */ "Aa c #000000", "Ab c #010101", "Ac c #020202", "Ad c #030303", ... Dr. Deepak B Phatak 25 IIT BOMBAY CS 101 - Lecture 24 Image files Fingerprint image of a person (.bmp) file Dr. Deepak B Phatak 26 IIT BOMBAY CS 101 - Lecture 24 Image files Fingerprints of two different persons Dr. Deepak B Phatak 27 IIT BOMBAY CS 101 - Lecture 24 Image files Same person, different capture Dr. Deepak B Phatak 28 IIT BOMBAY CS 101 - Lecture 24 Image files Schedule for remaining lecture sessions Additional lecture slot on Thursday will not be used any more (available for project discussions. Lecture(s) Date(s) 23 12 Oct 24 20 Oct 25, 26, 27 26, 27, and 31 Oct 28, 29, 30 2, 3, and 7 Nov 31, 32 9 and 10 Nov 33 16 Nov Dr. Deepak B Phatak 29 IIT BOMBAY CS 101 - Lecture 24 Image files Course Projects Formal start This Week (19 October 2009) Duration: four weeks Final Submission Saturday 14 November 2009 Details will be on the course home page by 14:30 Hrs