/**************************************************************************** Copyright (C) 2010 Laszlo Simon This file is part of the HidraVFX project. HidraVFX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. HidraVFX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with HidraVFX. If not, see . ****************************************************************************/ #include #include #include #include #include #include "interpol.h" #include "layer.h" #include "color.h" tLayerF layerF(int w, int h) { tLayerF layer; int i, y; layer.l = 0; layer.t = 0; layer.w = w; layer.h = h; for (i = 0; i < CHANNELS; i++) { /* todo: check malloc return value */ layer.ch[i] = (tMapF)malloc(h * sizeof(tLineF)); assert(layer.ch[i] != (tMapF)0); layer.bg[i] = 0.0/0.0; } for (y = 0; y < h; y++) { for (i = 0; i < CHANNELS; i++) { /* todo: check malloc return value */ layer.ch[i][y] = (tLineF)malloc(w * sizeof(float)); assert(layer.ch[i][y] != (tLineF)0); } } return(layer); } tLayerF layerbgs(float bgs[]) { tLayerF layer; int i; layer.l = 0; layer.t = 0; layer.w = 0; layer.h = 0; for (i = 0; i < CHANNELS; i++) { layer.ch[i] = NULL; layer.bg[i] = bgs[i]; } return(layer); } tLayerF layerbg(float bg) { tLayerF layer; int i; layer.l = 0; layer.t = 0; layer.w = 0; layer.h = 0; for (i = 0; i < CHANNELS; i++) { layer.ch[i] = NULL; layer.bg[i] = bg; } return(layer); } float getpix(tLayerF l, int ch, int x, int y) { return( ( (l.ch[ch] != NULL) && (x>=l.l) && (x=l.t) && (y=l.l) && (x=l.t) && (y=l.l) && (x<=l.l+l.w-1) && (y>=l.t) && (y<=l.t+l.h-1)) { x1 = x; y1 = y; if((x1 == x) && (y1 == y)) { return(l.ch[ch][y1][x1]); } else { x0 = x-x1; y0 = y-y1; x1 = x1-l.l; y1 = y1-l.t; x2 = (x1==x) ? x1 : x1+1; y2 = (y1==y) ? y1 : y1+1; return linear2(l.ch[ch][y1][x1],l.ch[ch][y1][x2], l.ch[ch][y2][x1],l.ch[ch][y2][x2], x0,y0); } } else { return(l.bg[ch]); } } /** Calculate statistics about image: * \return error value */ int stats(tLayerF srcimg, tLayerStats *s) { int err = 0; int ix, iy, ch; int w = srcimg.w; int h = srcimg.h; int count = 0; float src; for (ch = 0; ch < CHANNELS; ch++) { s->min[ch] = 0.0/0.0; s->max[ch] = 0.0/0.0; s->mean[ch] = 0.0/0.0; } for (iy = 0; iy < h; iy++) { for (ix = 0; ix < w; ix++) { if(srcimg.ch[3][iy][ix] > 0.0) { for (ch = 0; ch < CHANNELS; ch++) { src = srcimg.ch[ch][iy][ix]; if ( (s->min[ch] != s->min[ch]) || (src < s->min[ch])) { s->min[ch] = src; } if ( (s->max[ch] != s->max[ch]) || (src > s->max[ch])) { s->max[ch] = src; } s->mean[ch] = (s->mean[ch] != s->mean[ch]) ? src : s->mean[ch] + src; } src = (srcimg.ch[0][iy][ix] + srcimg.ch[1][iy][ix] + srcimg.ch[2][iy][ix]) / 3.0; if ( (s->intmin != s->intmin) || (src < s->intmin)) { s->intmin = src; } if ( (s->intmax != s->intmax) || (src > s->intmax)) { s->intmax = src; } s->intmean = (s->intmean != s->intmean) ? src : s->intmean + src; count++; } } } for (ch = 0; ch < CHANNELS; ch++) { s->mean[ch] = s->mean[ch]/count; } s->visible = count; return(err); }