#include "stdafx.h" #include #include #include #include "image.h" int setSize(Image *im, int rows, int columns) { im->rows=rows; im->columns=columns; im->data = (uchar *) malloc(rows * columns * sizeof(char)); if(im->data == NULL) { fprintf(stderr, "Insufficient memory for new image\n"); exit(1); } setColors(im, 255); return 0; } int setSizeColor(ImageColor *im, int rows, int columns) { im->rows=rows; im->columns=columns; im->dataR = (uchar *) malloc(rows * columns * sizeof(char)); if(im->dataR == NULL) { fprintf(stderr, "Insufficient memory for new image\n"); exit(1); } im->dataG = (uchar *) malloc(rows * columns * sizeof(char)); if(im->dataG == NULL) { fprintf(stderr, "Insufficient memory for new image\n"); exit(1); } im->dataB = (uchar *) malloc(rows * columns * sizeof(char)); if(im->dataB == NULL) { fprintf(stderr, "Insufficient memory for new image\n"); exit(1); } setColorsColor(im, 255); return 0; } int getNCols(const Image *im) { return im->columns; } int getNColsColor(const ImageColor *im) { return im->columns; } int getNRows(const Image *im) { return im->rows; } int getNRowsColor(const ImageColor *im) { return im->rows; } int setColors(Image *im, int colors) { im->colors=colors; return 0; } int setColorsColor(ImageColor *im, int colors) { im->colors=colors; return 0; } int getColors(const Image *im) { return im->colors; } int getColorsColor(const ImageColor *im) { return im->colors; } int setPixel(Image *im, int i, int j, int color) { i=CLAMP(i, 0, im->rows-1); j=CLAMP(j, 0, im->columns-1); im->data[i*(im->columns)+j]=color; return 0; } int setPixelColor(ImageColor *im, int i, int j, int colorR, int colorG, int colorB) { i=CLAMP(i, 0, im->rows-1); j=CLAMP(j, 0, im->columns-1); im->dataR[i*(im->columns)+j]=colorR; im->dataG[i*(im->columns)+j]=colorG; im->dataB[i*(im->columns)+j]=colorB; return 0; } int getPixel(const Image *im, int i, int j) { i=CLAMP(i, 0, im->rows-1); j=CLAMP(j, 0, im->columns-1); return (int) im->data[i*(im->columns)+j]; } int getPixelColor(const ImageColor *im, int i, int j, int colorNum) { i=CLAMP(i, 0, im->rows-1); j=CLAMP(j, 0, im->columns-1); if (colorNum==1) return (int) im->dataR[i*(im->columns)+j]; if (colorNum==2) return (int) im->dataG[i*(im->columns)+j]; if (colorNum==3) return (int) im->dataB[i*(im->columns)+j]; return -1; } int readImage(Image *im, const char *fname) { FILE *input; char line[1024]; int nCols,nRows; int levels; int i, j; /* open it */ if (!fname || (input=fopen(fname,"rb"))==0) return-1; /* check for the right "magic number" */ if ( fread(line,1,3,input)!=3 ||strncmp(line,"P5\n",3) ) { fclose(input); return -1; } /* skip the comments */ do fgets(line,sizeof line,input); while(*line=='#'); /* read the width and height */ sscanf(line,"%d %d\n",&nCols,&nRows); setSize(im, nRows, nCols); /* read # of gray levels */ fgets(line,sizeof line,input); sscanf(line,"%d\n",&levels); setColors(im, levels); /* read pixel row by row */ for(i=0;idy*dy) /* horizontal scan */ { dir=DIR_X; if (xmax=0) { mpCase=1; d=2*dy-dx; } else { mpCase=2; d=2*dy+dx; } incrNE=2*(dy-dx); incrE=2*dy; incrSE=2*(dy+dx); } else /* vertical scan */ { dir=DIR_Y; if (ymax=0) { mpCase=1; d=2*dx-dy; } else { mpCase=2; d=2*dx+dy; } incrNE=2*(dx-dy); incrE=2*dx; incrSE=2*(dx+dy); } /* start the scan */ x=xmin; y=ymin; done=0; while(!done) { setPixel(im,x,y,color); /* move to the next point */ switch(dir) { case DIR_X: /* horizontal */ { if (x