Created
April 3, 2012 10:35
-
-
Save stiletto/2290939 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <netpbm/pam.h> | |
//int getpx(struct pam *inpam, int currow, int row, int col) { | |
#define DIST 8 | |
#define min(x,y) ( (x<y) ? (x) : (y) ) | |
#define max(x,y) ( (x>y) ? (x) : (y) ) | |
int main(int argc, char *argv[]) { | |
struct pam inpam, outpam; | |
tuple * tuplerow; | |
unsigned int row; | |
unsigned int *data; | |
pm_init(argv[0], 0); | |
pnm_readpaminit(stdin, &inpam, PAM_STRUCT_SIZE(tuple_type)); | |
data = malloc(inpam.width*inpam.height*inpam.depth*sizeof(unsigned int)); | |
outpam = inpam; outpam.file = stdout; | |
tuplerow = pnm_allocpamrow(&inpam); | |
size_t i = 0; | |
for (row = 0; row < inpam.height; row++) { | |
unsigned int column; | |
pnm_readpamrow(&inpam, tuplerow); | |
for (column = 0; column < inpam.width; ++column) { | |
unsigned int plane; | |
for (plane = 0; plane < inpam.depth; ++plane) { | |
data[i] = tuplerow[column][plane]; | |
i++; | |
} | |
} | |
} | |
unsigned int y; | |
pnm_writepaminit(&outpam); | |
for (y = 0; y < inpam.height; y++) { | |
unsigned int x; | |
for (x = 0; x < inpam.width; x++) { | |
unsigned int p; | |
for (p = 0; p < inpam.depth; p++) { | |
unsigned int avg = 0; | |
unsigned int avgn = 0; | |
unsigned int ay; | |
for (ay = max(0,y-DIST); ay < min(inpam.height,y+DIST) ; ay++) { | |
unsigned int ax; | |
for (ax = max(0,x-DIST); ax < min(inpam.width,x+DIST) ; ax++) { | |
avg += data[(ay*inpam.width + ax)*inpam.depth + p]; | |
avgn += 1; | |
} | |
} | |
avg = (!avgn) ? 0 : avg/avgn; | |
avg = (avg==0) ? 1 : avg; | |
ay = data[(y*inpam.width + x)*inpam.depth + p]; | |
//fprintf(stderr,"data %d, avg %d\n",ay,avg); | |
//if ( ay >= avg*3/2) | |
tuplerow[x][p] = 255*ay/avg; | |
//else | |
// tuplerow[x][p] = 0x00; | |
} | |
} | |
fprintf(stderr,"Processing %d\n",y*100/inpam.height); | |
pnm_writepamrow(&outpam, tuplerow); | |
} | |
pnm_freepamrow(tuplerow); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment