1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*
Copyright (C) 2000 Paul Wilkins
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* render.c by Paul Wilkins 1/2/2000 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gtk/gtk.h>
#include <gdk_imlib.h>
#include "globals.h"
#include "read_db.h"
#include "display.h"
#include "stats.h"
#include "status.h"
#include "find_match.h"
#include "draw_image.h"
#include "cursor.h"
#include "render_image.h"
#include "render.h"
#define STATS_SIZE 6
struct PIX {
int cnt;
double r;
double g;
double b;
};
#define PIX_VAL(ptr, x, y) (((ptr)->rgb_data) + (3*((x)+((y)*((ptr)->rgb_width)))) )
void average_image_area(struct PIX *avg, GdkImlibImage *im, int x, int y, int w, int h){
int i, j;
unsigned char *p1;
int r, g, b;
int xx, yy;
/* printf("average_image_area: x: %d y: %d w: %d h: %d\n", x, y, w, h); */
avg->r = 0.0;
avg->g = 0.0;
avg->b = 0.0;
for(j=0; j<h; j++){
yy = j + y;
for(i=0; i<w; i++){
xx = i + x;
p1 = PIX_VAL(im, xx, yy);
r = *p1++;
g = *p1++;
b = *p1++;
/* printf("average_image_area: pixel %dx%d r: %d g: %d b: %d\n", xx, yy, r, g, b); */
avg->r += r;
avg->g += g;
avg->b += b;
}
}
avg->cnt = w * h;
avg->r /= (double)(w*h);
avg->g /= (double)(w*h);
avg->b /= (double)(w*h);
/* printf("average_image_area: r: %g g: %g b: %g\n", avg->r, avg->g, avg->b); */
}
double calc_stddev(GdkImlibImage *im, int x, int y, int nPixW, int nPixH){
int w, h, n;
int xx, yy, ww, hh;
int pxx, pyy;
double xoff, yoff;
double meanR, stdR;
double meanG, stdG;
double meanB, stdB;
struct PIX avg;
double dataR[STATS_SIZE*STATS_SIZE];
double dataG[STATS_SIZE*STATS_SIZE];
double dataB[STATS_SIZE*STATS_SIZE];
/* printf("calc_stddev: x: %d y: %d\n", x, y); */
xoff = (double)x / (double)nPixW * (double)im->rgb_width;
yoff = (double)y / (double)nPixH * (double)im->rgb_height;
n = 0;
pyy = (int)yoff;
for(h=0; h<STATS_SIZE; h++){
yy = (int)(yoff + (double)(h+1) * (double)im->rgb_height / (double)nPixH / (double)STATS_SIZE);
hh = yy - pyy;
pxx = (int)xoff;
for(w=0; w<STATS_SIZE; w++){
xx = (int)(xoff + (double)(w+1) * (double)im->rgb_width / (double)nPixW / (double)STATS_SIZE);
ww = xx - pxx;
average_image_area(&avg, im, pxx, pyy, ww, hh);
dataR[n] = avg.r;
dataG[n] = avg.g;
dataB[n] = avg.b;
n++;
pxx = xx;
}
pyy = yy;
}
calc_mead_std(dataR, n, &meanR, &stdR);
calc_mead_std(dataG, n, &meanG, &stdG);
calc_mead_std(dataB, n, &meanB, &stdB);
return (stdR + stdG + stdB) / 3.0;
}
int init_db(){
static int init = 0;
if(init == 0){
/* read in the picture database */
if(NULL == (globals.head = read_database(&(globals.max_order)))){
fprintf(stderr, "Error reading database.\n");
exit(1);
}
init = 1;
}
return 1;
}
int *gen_master_data(GdkImlibImage *im, int x, int y, int nPixW, int nPixH, int order){
int w, h;
int xx, yy, ww, hh;
int pxx, pyy;
int *p1;
int *data;
double xoff, yoff;
struct PIX avg;
/* this must be free'd outside this function */
if(NULL == (data=malloc(3*order*order*sizeof(int)))){
perror("malloc");
exit(1);
}
xoff = (double)x / (double)nPixW * (double)im->rgb_width;
yoff = (double)y / (double)nPixH * (double)im->rgb_height;
pyy = (int)yoff;
p1 = data;
for(h=0; h<order; h++){
yy = (int)(yoff + (double)(h+1) * (double)im->rgb_height / (double)nPixH / (double)order);
hh = yy - pyy;
pxx = (int)xoff;
for(w=0; w<order; w++){
xx = (int)(xoff + (double)(w+1) * (double)im->rgb_width / (double)nPixW / (double)order);
ww = xx - pxx;
average_image_area(&avg, im, pxx, pyy, ww, hh);
*p1++ = (int)avg.r;
*p1++ = (int)avg.g;
*p1++ = (int)avg.b;
pxx = xx;
}
pyy = yy;
}
return data;
}
void free_image_data(){
int ww, hh;
if(globals.image == NULL) return;
/* free old image data */
for(hh=0; hh<globals.cur_opt.nPixH; hh++){
for(ww=0; ww<globals.cur_opt.nPixW; ww++){
free(globals.image[hh][ww].matches);
}
free(globals.image[hh]);
}
free(globals.image);
globals.image = NULL;
}
int image_borders_n(struct IMAGE_INFO **image, struct PIC_DB *match, int n, int cx, int cy, int maxx, int maxy){
int x, y;
for(y=cy-n; y<=cy+n; y++){
if(y<0 || y>=maxy) continue;
for(x=cx-n; x<=cx+n; x++){
if(x<0 || x>=maxx) continue;
if(x==cx && y==cy) continue;
if(image[y][x].db == match) return 1;
}
}
return 0;
}
int guess_order(double val, int pixW, int pixH, int max_order){
int order;
order = (int)(sqrt((double)(pixW*pixH))/9.0 + (val / 6.0));
if(order > max_order) order = max_order;
return order;
}
int render(){
int i, j;
int pixW, pixH;
int nPixW, nPixH;
int *match_data;
int proximity;
double percent;
GdkImlibImage *im;
GdkImlibImage *out_im;
int order;
int ww, hh;
double stddev;
struct IMAGE_INFO **image;
struct PIC_DB **matches;
if(globals.in_fname == NULL || globals.in_im == NULL){
fprintf(stderr, "Error: Open an Image first!\n");
return 0;
}
/* free the old data, if any */
free_image_data();
/* calculate the new images dimensions */
if(0 == calc_dimensions(&(globals.new_opt))){
fprintf(stderr, "Invalid option. Bye.\n");
return 0;
}
/* set the cursor */
cursor_busy();
/* copy the image rendering data from new_opt to cur_opt */
if(copy_opt_data()){
resize_window();
}
/* update the mode display info */
refresh_mode_display();
/* set our local copies of some of the globals */
im = globals.in_im;
pixW = globals.cur_opt.pixW;
pixH = globals.cur_opt.pixH;
nPixW = globals.cur_opt.nPixW;
nPixH = globals.cur_opt.nPixH;
/* malloc the array to store the image info */
if(NULL == (image=malloc(nPixH*sizeof(struct IMAGE_INFO *)))){
perror("Malloc");
exit(1);
}
for(hh=0; hh<nPixH; hh++){
if(NULL == (image[hh]=malloc(nPixW*sizeof(struct IMAGE_INFO)))){
perror("Malloc");
exit(1);
}
for(ww=0; ww<nPixW; ww++){
image[hh][ww].nMatch = 0;
image[hh][ww].db = NULL;
image[hh][ww].do_highlight = 0;
}
}
globals.image = image;
/* initialize the database */
init_db();
/* clear data stored in the database from the last render */
reset_db_data(globals.head);
/* turn on the progress bar */
toggle_progress_indicator(ST_PERCENT);
set_progress_indicator(0.0);
/* go pix by pix through the image calculating images that match well */
for(hh=0; hh<nPixH; hh++){
for(ww=0; ww<nPixW; ww++){
/* calc_stats */
stddev = calc_stddev(im, ww, hh, nPixW, nPixH);
/* depending on stddev, match to different orders */
order = guess_order(stddev, pixW, pixH, globals.max_order);
/* generate the data to match to from the master image */
match_data = gen_master_data(im, ww, hh, nPixW, nPixH, order);
/* find the best matching picture for this "pixel" */
matches = find_match(order, match_data, globals.head);
/* we are done with it now */
free(match_data);
/* store the image we picked */
image[hh][ww].matches = matches;
/* update the progress bar */
percent = (double)(hh*nPixW + ww + 1) / (double)(nPixH*nPixW);
set_progress_indicator(percent);
}
}
/* go pix by pix through the image finding the best match */
for(j=0; j<nPixH*nPixW; j++){
/* randomly pick an image to match */
hh = random() % nPixH;
ww = random() % nPixW;
while(image[hh][ww].db != NULL){
ww++;
if(ww >= nPixW){
ww = 0;
hh++;
if(hh >= nPixH) hh = 0;
}
}
matches = image[hh][ww].matches;
/* pick one of the (hopefully the best) matches to fill this spot */
proximity = globals.cur_opt.proximity;
do {
for(i=0;
i<MAX_MATCHES && 1==image_borders_n(image, matches[i], proximity, ww, hh, nPixW, nPixH);
i++) /* do nothing */;
proximity--;
} while (proximity>=0 && i == MAX_MATCHES);
/* probably only one file */
if(matches[i] == NULL) i = 0;
/* store the image we picked */
image[hh][ww].match_no = i;
image[hh][ww].db = matches[i];
image[hh][ww].db->refcnt++;
image[hh][ww].db->done = 0;
}
/* render the image */
if(NULL == (out_im = render_image(image, nPixW, nPixH, pixW, pixH))){
fprintf(stderr, "Error: Can't render image.\n");
exit(1);
}
globals.out_im = out_im;
/* display it */
draw_big_image(out_im);
/* turn off the progress bar */
toggle_progress_indicator(ST_READY);
/* set the cursor */
cursor_normal();
return 1;
}
|