#include #include #include #define THRESHOLD 0 #define FRACTION(x) ((x)-(floor(x))) #define INT(x) ((int)floor(x)) #define ERR(s) fprintf(stderr,"%s",s); using namespace std; void WritePPM(char *file, unsigned char **buf, int width, int height) { FILE *fp = fopen(file,"w"); int i,j; fprintf(fp,"P5\n%d %d\n255\n",width,height); for(i = 0; i < height; i++) for(j = 0; j < width; j++) fprintf(fp,"%c",buf[i][j]); fclose(fp); } void WriteTiff(char* fileName, unsigned char ** array, int width, int height) { TIFF* out = TIFFOpen(fileName,"w"); int sampleperpixel = 1; unsigned char* image = new unsigned char[width*height*sampleperpixel]; for( int i = 0 ; i < height ; i++) for( int j = 0 ; j < width ; j++) image[i*width+j] = array[i][j]; TIFFSetField (out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, height); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); if(TIFFWriteEncodedStrip(out, 0, image, width * height * sampleperpixel) == 0){ cout <<"Cud not write the image\n"; return ; } TIFFClose(out); delete [] image; } unsigned char **scale(unsigned char **src, int top,int bottom,int left, int right, int destrow,int destcol,unsigned char ** dest) { int srcrow = 1 + bottom - top; int srccol = 1 + right - left; float xint = (float)srcrow/(destrow); float yint = (float)srccol/(destcol); float i,j; int l=0,m=0; float pval1,pval2,pval; i = top; for (l = 0; l < destrow; l++) { if ( !dest[l] ) { perror("calloc"); cout<<"Error here,..."<= right-1) pval1 = src[INT(i)][INT(j)]; else pval1 = (1 - FRACTION(j))*src[INT(i)][INT(j)] + FRACTION(j)*src[INT(i)][INT(j)+1]; if ( INT(i) >= bottom-1 || INT (j) >= right-1 ) pval2 = src[INT(i)][INT(j)]; else pval2 = (1 - FRACTION(j))*src[INT(i)+1][INT(j)] + FRACTION(j)*src[INT(i)+1][INT(j)+1]; pval = (1 - FRACTION(i))*pval1 + FRACTION(i)*pval2; if ( pval > THRESHOLD) { dest[l][m] = 255; } else dest[l][m] = 0; j += yint; } i += xint; } // cout<<"Done scaling"<, argv[2] is { TIFF* image; uint32* raster; int imagewidth, imageheight; image=TIFFOpen(argv[1],"r"); TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &imagewidth); TIFFGetField(image, TIFFTAG_IMAGELENGTH, &imageheight); uint32 npixels=imagewidth*imageheight; raster=(uint32 *) _TIFFmalloc(imagewidth *imageheight*sizeof(uint32)); if(TIFFReadRGBAImage(image, imagewidth, imageheight, raster)==0) { cout <<"Error reading the tiff file\n"; return 1; } unsigned char **wordBuffer, **scaledWord; wordBuffer = (unsigned char **) calloc(imageheight, sizeof(unsigned char *)); for(int i = 0; i < imageheight; i++) wordBuffer[i] = (unsigned char *) calloc(imagewidth,sizeof(unsigned char)); //binarising the image here... for(int i=0; i< imagewidth; i++) for(int j=0; j< imageheight; j++) { int k=imageheight - 1 - j; wordBuffer[j][i] = (unsigned char)TIFFGetR(raster[k*imagewidth+i]); if(wordBuffer[j][i] < 250) // set binarisation threshold here wordBuffer[j][i] = 0; else wordBuffer[j][i] = 255; } // wordBuffer now contains the image // WRITE YOUR CODE HERE // WritePPM(argv[2], wordBuffer, imagewidth, imageheight); // WriteTiff(argv[2], wordBuffer, imagewidth, imageheight); // Scaling of the image is done here... int scaledHeight = 200, scaledWidth = 800; // set canonical scale here scaledWord = (unsigned char **) calloc(scaledHeight, sizeof(unsigned char *)); for(int i = 0; i < scaledHeight; i++) scaledWord[i] = (unsigned char *) calloc(scaledWidth,sizeof(unsigned char)); scale(wordBuffer, 0, imageheight-1, 0, imagewidth-1, scaledHeight, scaledWidth, scaledWord); // WritePPM(argv[2], scaledWord, scaledWidth, scaledHeight); WriteTiff(argv[2], scaledWord, scaledWidth, scaledHeight); }