void invertImage(FILE *inf, FILE *outf) { int i=0; int width; int height; int max; int w=0; // Get width, height, and max from the file rewind(inf); eatLine(inf); eatLine(inf); fscanf(inf,"%d",&width); fscanf(inf,"%d",&height); fscanf(inf,"%d",&max); // Initialize the array float *darkValues; // Read the values from the pgm file darkValues = readImage(inf); // Find the total number of 'i's we will have int amtValues = width*height; // Print the header to our pgm file fprintf(outf,"P2\n"); fprintf(outf,"# CREATOR: BLAKE, THE MASTER OF C\n"); fprintf(outf,"%d %d\n",width,height); fprintf(outf,"%d\n",max); // For each pixel, invert the grey value while (i < amtValues) { fprintf(outf,"%d\n",((int)(max*(1.0-darkValues[i])))); i++; } }
float *readImage(FILE *inf) { int width,height,max; rewind(inf); eatLine(inf); eatLine(inf); fscanf(inf,"%d",&width); fscanf(inf,"%d",&height); fscanf(inf,"%d",&max); int r,c; int pixel; float gval; float *gvalues = (float *)malloc((width*height)*sizeof(float)); int i; int k; for(r=0; r<height; r++) { for(c=0; c<width; c++) { fscanf(inf, "%d", &pixel); gval = ((float)pixel/(float)max); gvalues[i] = gval; i++; } } return gvalues; }
// echoASCII // // Read the a PGM file opened as "inf" and write a text file // of characters to "outf", ones whose brightness suggest the // levels of grey specified by the original image. // void echoASCII(FILE *inf, FILE *outf) { int width, height; int max; int pixel; int r,c; // Read the PGM file's header info, // for example: eatLine(inf); // P5 (or P2) eatLine(inf); // # this was produced by some software fscanf(inf, "%d", &width); // 9 5 fscanf(inf, "%d", &height); fscanf(inf, "%d", &max); // 255 // for each image row for (r=0; r<height; r++) { // for each image column for (c=0; c<width; c++) { // read a PGM pixel grey value (from 0 to max) fscanf(inf,"%d", &pixel); // 0 10 30 80 120 135 225 245 255 // (4 more rows) // output a pixel character to make ASCII art outASCII(outf,(float)pixel/(float)max); } // end the ASCII text line fprintf(outf,"\n"); } }
void *readImage(FILE *inf, int opt, FILE *outf){ int width, height; int max; int r,c; int pixel; // Process the pgm file as a bufft // I.e., eat the header eatLine(inf); eatLine(inf); fscanf(inf,"%d",&width); // the first integer on line 1 is the width fscanf(inf,"%d",&height); // second is the height fscanf(inf,"%d",&max); // third is max number of pixels float *array = malloc(sizeof(float)*width*height); int p; // iterate through rows and columns // scan in graymap values, convert to pixels // fill array with pixel values for (r=0; r < height; r++) { for (c=0; c < width; c++) { fscanf(inf,"%d",&pixel); p = c + r*width; array[p] = (float)pixel/(float)max; } } // Select function to call // switch (opt) { case(ASCII): echoASCII(array,height,width,outf); break; case(INV): invert(array,height,width,outf); case(BLUR): blur(array,height,width,outf); } // exit(-1); }
MenuConf::MenuConf(const char* filename) : filename(filename) , lineIndex(0) , indentChar(0) , parentIndent(0) , firstItem(0) , curParent(0) , caret(0) { printf("MenuConf (%s)\n", filename); FILE* f = fopen(filename, "rt"); if(! f) { printf("MenuConf: failed open %s", filename); return; } nextChar(f); while(true) { caret = 0; lineIndex ++; if(curChar == EOF) break; int indent = 0; while(curChar == ' ' || curChar == '\t') { if(indentChar == 0) indentChar = curChar; if(indentChar != curChar) { printf("Mixed tabs and whitespace in %s.\nUse either tabs or whitespace for indentation.\n", filename); return; } indent ++; nextChar(f); } if(curChar == EOF) { //printf("%d: EOF\n", lineIndex); break; } else if(curChar == '\n') { //printf("%d: blank\n", lineIndex); nextChar(f); } else if(isAlpha(curChar) || curChar=='"') { //printf("%d: item\n", lineIndex); //eatLine(f); Item* item = new Item(new ItemData()); item->indent = indent; if(readItemNameIfPresent(f, item)) { if(readItemCaption(f, item)) { readItemHotkeysIfPresent(f, item); } } eatLine(f); while(curParent && item->indent <= curParent->indent) { curParent = curParent->parent; } addChild(curParent, item); curParent = item; } else if(curChar == '-' && nextChar(f) == '-' && nextChar(f) == '-') { //separator Item* item = new Item(NULL); item->indent = indent; while(curParent && item->indent <= curParent->indent) curParent = curParent->parent; addChild(curParent, item); curParent = item; eatLine(f); } else { //comment eatLine(f); } } fclose(f); printItems(firstItem, 0); }
void blurImage(FILE *inf, FILE *outf) { int width; int height; int max; int x,y; int i=0; rewind(inf); eatLine(inf); eatLine(inf); fscanf(inf,"%d",&width); fscanf(inf,"%d",&height); fscanf(inf,"%d",&max); float *gValues; float gInstance; int amtValues = width*height; gValues = readImage(inf); fprintf(outf,"P2\n"); fprintf(outf,"# CREATOR: BLAKE, THE MASTER OF C\n"); fprintf(outf,"%d %d\n",width,height); fprintf(outf,"%d\n",max); while (i < amtValues) { // Determine x,y value of i x = (int)(i % width); y = (int)floor(i/width); // Blur depending on which of 9 cases we fall into if (x == 0 && y == 0) { // 1. gInstance = (.25)*(2*gValues[i] + gValues[i+1] + gValues[width]); } else if (y == 0 && (0 < x) && (x < (width-1))) { // 2. gInstance = (.167)*(3*gValues[i] + gValues[i-1] + gValues[i+1] + gValues[i+width]); } else if (y == 0 && x == (width-1)) { // 3. gInstance = (.25)*(2*gValues[i] + gValues[i-1] + gValues[i+width]); } else if (x == (width-1) && (0 < y) && (y < (height-1))) { // 4. gInstance = (.167)*(3*gValues[i] + gValues[i-1] + gValues[i-width] + gValues[i+width]); } else if (y == (height-1) && x == (width-1)) { // 5. gInstance = (.25)*(2*gValues[i] + gValues[i-1] + gValues[i-width]); } else if (y == (height-1) && (0 < x) && (x < (width-1))) { // 6. gInstance = (.167)*(3*gValues[i] + gValues[i-1] + gValues[i+1] + gValues[i-width]); } else if (x == 0 && y == (height-1)) { // 7. gInstance = (.25)*(2*gValues[i] + gValues[i+1] + gValues[i-width]); } else if (x == 0 && (0 < y) && (y < (height-1))) { // 8. gInstance = (.167)*(3*gValues[i] + gValues[i+1] + gValues[i+width] + gValues[i-width]); } else { // 9. gInstance = (.125)*(4.0*gValues[i] + gValues[i-1] + gValues[i+1] + gValues[i-width] + gValues[i+width]); } if (gInstance > 1) { gInstance = 1; } gValues[i] = gInstance; fprintf(outf,"%d\n",(int)(max*gValues[i])); i++; } }