# # v-ppmRMS.awk - take root-mean-square of 2 PPMs # by uratan! 2023.11.17 # # # usage: pass 2 output of "ppmtoppm -plain" like: # +------------------------------------------------------- # |ppmANY ... | ppmtoppm -plain > inputA # |ppmANY ... | ppmtoppm -plain > inputB # |awk -f v-ppmRMS.awk -v p=3 inputA inputB | ppmtoppm > result.ppm # +------------------------------------------------------- # BEGIN { #p = 0; # gain, [0, 5], give this from command line ni = 1; state = 1; gain = sqrt(2.0) ^ p; } { # # load 2 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[ni,x,y] = $(i++); pG[ni,x,y] = $(i++); pB[ni,x,y] = $(i++); x++; if(x >= width) { y++; x = 0; } } # # switch to 2nd PPM if 1st is satisfied # if((y >= height) && (ni == 1)) { ni = 2; state = 1; } } else { ; # ignore any } } END { if(state != 4) { print "error!"; } else { # # issue root mean squaer calclation # for(y=0; y 255) { RR = 255; } if(GG > 255) { GG = 255; } if(BB > 255) { BB = 255; } pR[1,x,y] = RR; pG[1,x,y] = GG; pB[1,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