// Copyright (c) 2008 Jakub Travnik // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // Note that this code produces by no means accurate // images of what the camera sensor and its software does. PImage img; int imagesize=400; // expected size of source image side int windowsizew=3*imagesize, windowsizeh=2*imagesize; int fullcolorpixelsize=16; // side of square area in source image that makes full color pixel float fullcolorfillfactor=0.8; // square root of fill factor of full color pixel, 1.0 means full fill, 0.707 means half 50% area fill int cfapixelsize=9; // side of square area in source image that makes cfa subpixel float cfaaafilter=2.0; // 1.0 means no aafilter (1x1 pixel area), 2.0 means moderate filter (2x2 pixel area is used) boolean strongcfaaafilter=true; // if true, aa filter area will be used for subpixel color // if false, aa filter area color blended with subpixel area color are used for subpixel color (center is prefered) void setup() { //fullcolorfillfactor=1.0; //cfaaafilter=1.0; size(windowsizew,windowsizeh); colorMode(RGB, 1.0); frameRate(10); img = loadImage("input.jpg"); img.loadPixels(); } void drawGridCFA(int ofsx, int ofsy, int gridsize) { pushMatrix(); image(img, 0, 0); rectMode(CORNER); int xlen=imagesize/gridsize-2; int ylen=imagesize/gridsize-2; PImage fimg = createImage(xlen, ylen, RGB); PImage fimgr = createImage(xlen, ylen, RGB); PImage fimgg = createImage(xlen, ylen, RGB); PImage fimgb = createImage(xlen, ylen, RGB); int samplesizeaa = int(cfaaafilter*gridsize); int sampleofsaa = (samplesizeaa-gridsize)/2; int samplesizecore = gridsize; int sampleofscore = 0; for (int x=0; x=im.width || y>=im.height) { return color(0.0, 0.0, 0.0, 0.0); } return im.pixels[y*im.width+x]; } color blendColors4(color c1, color c2, color c3, color c4) { float r=0.0,g=0.0,b=0.0,a=0.0; float a1=alpha(c1); r+=red(c1)*a1; g+=green(c1)*a1; b+=blue(c1)*a1; a+=a1; float a2=alpha(c2); r+=red(c2)*a2; g+=green(c2)*a2; b+=blue(c2)*a2; a+=a2; float a3=alpha(c3); r+=red(c3)*a3; g+=green(c3)*a3; b+=blue(c3)*a3; a+=a3; float a4=alpha(c4); r+=red(c4)*a4; g+=green(c4)*a4; b+=blue(c4)*a4; a+=a4; return color(r/a,g/a,b/a); } color blendColors2(color c1, color c2) { float r=0.0,g=0.0,b=0.0,a=0.0; float a1=alpha(c1); r+=red(c1)*a1; g+=green(c1)*a1; b+=blue(c1)*a1; a+=a1; float a2=alpha(c2); r+=red(c2)*a2; g+=green(c2)*a2; b+=blue(c2)*a2; a+=a2; return color(r/a,g/a,b/a); } color cfaMergeColors3(color c1, color c2, color c3) { float r=0.0,g=0.0,b=0.0; r+=red(c1); g+=green(c1); b+=blue(c1); r+=red(c2); g+=green(c2); b+=blue(c2); r+=red(c3); g+=green(c3); b+=blue(c3); return color(r,g,b); } void drawGridFullcolor(int ofsx, int ofsy, int gridsize, int fillsize) { pushMatrix(); image(img, 0, 0); rectMode(CORNER); int xlen=imagesize/gridsize-1; int ylen=imagesize/gridsize-1; PImage fimg = createImage(xlen, ylen, RGB); for (int x=0; xim.width) w=im.width-x; if (y+h>im.height) h=im.height-y; for(int yy=y; yy