# # v-emu-ivsat.awk - emulate IrfanView saturation correction # by uratan! 2021.12.15 # # # usage: pass output of "ppmtoppm -plain" like: # +------------------------------------------------------- # |ppmANY ... | ppmtoppm -plain | awk -v sat=32 -f v-emu-ivsat.awk \ # | | ppmtoppm > result.ppm # +------------------------------------------------------- # BEGIN { state = 1; #sat = 0; # saturation level, [-255, 255], give this from command line } { # # load PPM-ASCII image # if(state == 1) { # # wait for the file type # type = $1; if(type == "P3") { state++; # expect PPM, ASCII only } else { ; # ignore anything else } } else if(state == 2) { # # wait for width and height # width = $1; height = $2; x = 0; y = 0; state++; } else if(state == 3) { # # wait for maxV # maxV = $1; state++; } else if(state == 4) { # # feed pixmap data # i = 1; while(i <= NF) { pR[x,y] = $(i++); pG[x,y] = $(i++); pB[x,y] = $(i++); x++; if(x >= width) { y++; x = 0; } } } else { ; # ignore any } } END { if(state != 4) { print "error!"; } else { # # issue saturation correction # for(y=0; y 255) { RR = 255; } else if(RR < 0) { RR = 0; } if(GG > 255) { GG = 255; } else if(GG < 0) { GG = 0; } if(BB > 255) { BB = 255; } else if(BB < 0) { BB = 0; } pR[x,y] = RR; pG[x,y] = GG; pB[x,y] = BB; } } # # output them with PPM-ASCII # printf("%s\n", "P3"); # ppm, ascii printf("%d %d\n", width, height); printf("%d\n", maxV); for(y=0; y # -2 -1 0 1 2 # # # FYI-2: about grayscale conversion # # ppmtopgm uses: (MAXVAL <= 255) # YY = int( (77 * RR + 150 * GG + 29 * BB + 128) / 256 ); # # 'Convert to Grayscale' of IrfanView uses: # YY = int( 0.30 * RR + 0.59 * GG + 0.11 * BB + 0.5 ); #