-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetapixel.c
2561 lines (2111 loc) · 65.8 KB
/
metapixel.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* metapixel.c
*
* metapixel
*
* Copyright (C) 1997-2006 Mark Probst
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <float.h>
#include "getopt.h"
#include "vector.h"
#include "zoom.h"
#include "readimage.h"
#include "writeimage.h"
#include "lispreader.h"
#include "metapixel.h"
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
static int index_order[IMAGE_SIZE * IMAGE_SIZE];
static metapixel_t *first_pixel = 0;
static int num_metapixels = 0;
static float sqrt_of_two, sqrt_of_image_size;
static float index_weights[NUM_INDEXES];
static index_t weight_ordered_index_to_index[NUM_INDEXES];
static index_t index_to_weight_ordered_index[NUM_INDEXES];
/* default settings */
static char *default_prepare_directory = 0;
static int default_prepare_width = DEFAULT_PREPARE_WIDTH, default_prepare_height = DEFAULT_PREPARE_HEIGHT;
static string_list_t *default_library_directories = 0;
static int default_small_width = DEFAULT_WIDTH, default_small_height = DEFAULT_HEIGHT;
static float default_weight_factors[NUM_CHANNELS] = { 1.0, 1.0, 1.0 };
static int default_metric = METRIC_SUBPIXEL;
static int default_search = SEARCH_LOCAL;
static int default_classic_min_distance = DEFAULT_CLASSIC_MIN_DISTANCE;
static int default_collage_min_distance = DEFAULT_COLLAGE_MIN_DISTANCE;
static int default_cheat_amount = 0;
static int default_forbid_reconstruction_radius = 0;
/* actual settings */
static int small_width, small_height;
static float weight_factors[NUM_CHANNELS];
static int forbid_reconstruction_radius;
static int benchmark_rendering = 0;
static string_list_t*
string_list_prepend_copy (string_list_t *lst, const char *str)
{
string_list_t *new = (string_list_t*)malloc(sizeof(string_list_t));
new->str = strdup(str);
new->required = 0;
new->next = lst;
return new;
}
static char*
strip_path (char *name)
{
char *p = strrchr(name, '/');
if (p == 0)
return name;
return p + 1;
}
static metapixel_t*
new_metapixel (void)
{
metapixel_t *pixel = (metapixel_t*)malloc(sizeof(metapixel_t));
assert(pixel != 0);
memset(pixel, 0, sizeof(metapixel_t));
return pixel;
}
static unsigned char*
scale_image (unsigned char *image, int image_width, int image_height, int x, int y,
int width, int height, int new_width, int new_height)
{
unsigned char *new_image = (unsigned char*)malloc(new_width * new_height * NUM_CHANNELS);
unsigned char *image_start = image + (x + y * image_width) * NUM_CHANNELS;
zoom_image(new_image, image_start, get_filter(FILTER_MITCHELL), NUM_CHANNELS,
new_width, new_height, new_width * NUM_CHANNELS,
width, height, image_width * NUM_CHANNELS);
return new_image;
}
void
alpha_compose (unsigned char *dst, int width, int height, unsigned char *src, int perc)
{
int i;
for (i = 0; i < width * height * NUM_CHANNELS; ++i)
dst[i] = dst[i] * (100 - perc) / 100 + src[i] * perc / 100;
}
void
generate_index_order (void)
{
int index = 0,
begin = 0,
row = 0,
col = 0,
first_half = 1;
do
{
index_order[index++] = col + IMAGE_SIZE * row;
if (first_half)
{
if (row == 0)
{
if (begin == IMAGE_SIZE - 1)
{
first_half = 0;
col = begin = 1;
row = IMAGE_SIZE - 1;
}
else
{
++begin;
row = begin;
col = 0;
}
}
else
{
++col;
--row;
}
}
else
{
if (col == IMAGE_SIZE - 1)
{
++begin;
col = begin;
row = IMAGE_SIZE - 1;
}
else
{
++col;
--row;
}
}
} while (index < IMAGE_SIZE * IMAGE_SIZE);
}
static int
compute_index (int real_index, int channel, int sign)
{
return real_index + (channel + (sign > 0 ? 1 : 0) * NUM_CHANNELS) * IMAGE_SIZE * IMAGE_SIZE;
}
static void
uncompute_index (int index, int *real_index, int *channel, int *sign)
{
*real_index = index % (IMAGE_SIZE * IMAGE_SIZE);
*channel = (index / (IMAGE_SIZE * IMAGE_SIZE)) % NUM_CHANNELS;
*sign = (index / (NUM_CHANNELS * IMAGE_SIZE * IMAGE_SIZE)) ? 1 : -1;
}
void
cut_last_coefficients (float *image, int channel, int howmany)
{
int i;
for (i = IMAGE_SIZE * IMAGE_SIZE - howmany; i < IMAGE_SIZE * IMAGE_SIZE; ++i)
image[index_order[i] * NUM_CHANNELS + channel] = 0.0;
}
void
transform_rgb_to_yiq (float *image, int num_pixels)
{
const static Matrix3D conversion_matrix =
{
{ 0.299, 0.587, 0.114 },
{ 0.596, -0.275, -0.321 },
{ 0.212, -0.528, 0.311 }
};
int i;
for (i = 0; i < num_pixels; ++i)
{
Vector3D rgb_vec,
yiq_vec;
InitVector3D(&rgb_vec,
image[NUM_CHANNELS * i + 0],
image[NUM_CHANNELS * i + 1],
image[NUM_CHANNELS * i + 2]);
MultMatrixVector3D(&yiq_vec, &conversion_matrix, &rgb_vec);
image[NUM_CHANNELS * i + 0] = yiq_vec.x;
image[NUM_CHANNELS * i + 1] = yiq_vec.y / 1.192 + 127.5;
image[NUM_CHANNELS * i + 2] = yiq_vec.z / 1.051 + 128.106565176;
}
}
void
transform_yiq_to_rgb (float *image)
{
const static Matrix3D conversion_matrix =
{
{ 1.00308929854, 0.954849063112, 0.61785970812 },
{ 0.996776058337, -0.270706233074, -0.644788332692 },
{ 1.00849783766, -1.1104851847, 1.69956753125 }
};
int i;
for (i = 0; i < IMAGE_SIZE * IMAGE_SIZE; ++i)
{
Vector3D rgb_vec,
yiq_vec;
InitVector3D(&yiq_vec,
image[NUM_CHANNELS * i + 0],
image[NUM_CHANNELS * i + 1] - 127.5,
image[NUM_CHANNELS * i + 2] - 127.5);
MultMatrixVector3D(&rgb_vec, &conversion_matrix, &yiq_vec);
image[NUM_CHANNELS * i + 0] = rgb_vec.x;
image[NUM_CHANNELS * i + 1] = rgb_vec.y;
image[NUM_CHANNELS * i + 2] = rgb_vec.z;
}
}
void
transpose_image (float *old_image, float *new_image)
{
int i,
j,
channel;
for (i = 0; i < IMAGE_SIZE; ++i)
for (j = 0; j < IMAGE_SIZE; ++j)
for (channel = 0; channel < NUM_CHANNELS; ++channel)
new_image[channel + (i + j * IMAGE_SIZE) * NUM_CHANNELS] =
old_image[channel + (j + i * IMAGE_SIZE) * NUM_CHANNELS];
}
void
decompose_row (float *row)
{
int h = IMAGE_SIZE,
i;
float new_row[NUM_CHANNELS * IMAGE_SIZE];
for (i = 0; i < NUM_CHANNELS * IMAGE_SIZE; ++i)
row[i] = row[i] / sqrt_of_image_size;
while (h > 1)
{
h = h / 2;
for (i = 0; i < h; ++i)
{
int channel;
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
float val1 = row[channel + 2 * i * NUM_CHANNELS],
val2 = row[channel + (2 * i + 1) * NUM_CHANNELS];
new_row[channel + i * NUM_CHANNELS] = (val1 + val2) / sqrt_of_two;
new_row[channel + (h + i) * NUM_CHANNELS] = (val1 - val2) / sqrt_of_two;
}
}
memcpy(row, new_row, sizeof(float) * NUM_CHANNELS * h * 2);
}
}
void
decompose_column (float *column)
{
int h = IMAGE_SIZE,
i,
channel;
float new_column[ROW_LENGTH];
for (i = 0; i < IMAGE_SIZE; ++i)
for (channel = 0; channel < NUM_CHANNELS; ++channel)
column[channel + i * ROW_LENGTH] =
column[channel + i * ROW_LENGTH] / sqrt_of_image_size;
while (h > 1)
{
h = h / 2;
for (i = 0; i < h; ++i)
{
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
float val1 = column[channel + (2 * i) * ROW_LENGTH],
val2 = column[channel + (2 * i + 1) * ROW_LENGTH];
new_column[channel + i * NUM_CHANNELS] = (val1 + val2) / sqrt_of_two;
new_column[channel + (h + i) * NUM_CHANNELS] = (val1 - val2) / sqrt_of_two;
}
}
for (i = 0; i < h * 2; ++i)
for (channel = 0; channel < NUM_CHANNELS; ++channel)
column[channel + i * ROW_LENGTH] = new_column[channel + i * NUM_CHANNELS];
}
}
void
decompose_image (float *image)
{
int row;
for (row = 0; row < IMAGE_SIZE; ++row)
decompose_row(image + NUM_CHANNELS * IMAGE_SIZE * row);
for (row = 0; row < IMAGE_SIZE; ++row)
decompose_column(image + NUM_CHANNELS * row);
}
void
compose_row (float *row)
{
int h = 1,
i;
float new_row[NUM_CHANNELS * IMAGE_SIZE];
memcpy(new_row, row, sizeof(float) * NUM_CHANNELS * IMAGE_SIZE);
while (h < IMAGE_SIZE)
{
for (i = 0; i < h; ++i)
{
int channel;
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
float val1 = row[channel + i * NUM_CHANNELS],
val2 = row[channel + (h + i) * NUM_CHANNELS];
new_row[channel + 2 * i * NUM_CHANNELS] = (val1 + val2) / sqrt_of_two;
new_row[channel + (2 * i + 1) * NUM_CHANNELS] = (val1 - val2) / sqrt_of_two;
}
}
memcpy(row, new_row, sizeof(float) * NUM_CHANNELS * IMAGE_SIZE);
h = h * 2;
}
for (i = 0; i < NUM_CHANNELS * IMAGE_SIZE; ++i)
row[i] = row[i] * sqrt_of_image_size;
}
void
compose_image (float *image)
{
int row;
float *transposed_image = (float*)malloc(sizeof(float) * 3 * IMAGE_SIZE * IMAGE_SIZE);
transpose_image(image, transposed_image);
for (row = 0; row < IMAGE_SIZE; ++row)
compose_row(transposed_image + NUM_CHANNELS * IMAGE_SIZE * row);
transpose_image(transposed_image, image);
for (row = 0; row < IMAGE_SIZE; ++row)
compose_row(image + NUM_CHANNELS * IMAGE_SIZE * row);
free(transposed_image);
}
int
compare_coeffs_with_index (const void *p1, const void *p2)
{
const coefficient_with_index_t *coeff1 = (const coefficient_with_index_t*)p1;
const coefficient_with_index_t *coeff2 = (const coefficient_with_index_t*)p2;
if (fabs(coeff1->coeff) < fabs(coeff2->coeff))
return 1;
else if (fabs(coeff1->coeff) == fabs(coeff2->coeff))
return 0;
return -1;
}
void
find_highest_coefficients (float *image, coefficient_with_index_t highest_coeffs[NUM_COEFFS])
{
int index, channel;
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
for (index = 1; index < SIGNIFICANT_COEFFS + 1; ++index)
{
float coeff = image[channel + NUM_CHANNELS * index];
int sign = coeff > 0.0 ? 1 : -1;
highest_coeffs[index - 1 + channel * SIGNIFICANT_COEFFS].index = compute_index(index, channel, sign);
highest_coeffs[index - 1 + channel * SIGNIFICANT_COEFFS].coeff = coeff;
}
qsort(highest_coeffs + channel * SIGNIFICANT_COEFFS, SIGNIFICANT_COEFFS,
sizeof(coefficient_with_index_t), compare_coeffs_with_index);
}
for (index = SIGNIFICANT_COEFFS + 1; index < IMAGE_SIZE * IMAGE_SIZE; ++index)
{
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
float coeff = image[channel + NUM_CHANNELS * index];
if (fabs(coeff) > fabs(highest_coeffs[(channel + 1) * SIGNIFICANT_COEFFS - 1].coeff))
{
int significance;
int sign = coeff > 0.0 ? 1 : -1;
for (significance = (channel + 1) * SIGNIFICANT_COEFFS - 2;
significance >= channel * SIGNIFICANT_COEFFS;
--significance)
if (fabs(coeff) <= fabs(highest_coeffs[significance].coeff))
break;
++significance;
memmove(highest_coeffs + significance + 1,
highest_coeffs + significance,
sizeof(coefficient_with_index_t) * ((channel + 1) * SIGNIFICANT_COEFFS - 1 - significance));
highest_coeffs[significance].index = compute_index(index, channel, sign);
highest_coeffs[significance].coeff = coeff;
}
}
}
}
static float
weight_function (int index)
{
static float weight_table[NUM_CHANNELS][6] =
{
{ 5.00, 0.83, 1.01, 0.52, 0.47, 0.30 },
{ 19.21, 1.26, 0.44, 0.53, 0.28, 0.14 },
{ 34, 0.36, 0.45, 0.14, 0.18, 0.27 }
};
int real_index, channel, sign;
int i, j, bin;
uncompute_index(index, &real_index, &channel, &sign);
i = real_index % IMAGE_SIZE;
j = real_index / IMAGE_SIZE;
bin = MIN(MAX(i, j), 5);
return weight_table[channel][bin] * weight_factors[channel];
}
static int
compare_indexes_by_weight_descending (const void *p1, const void *p2)
{
const index_t *i1 = (const index_t*)p1, *i2 = (const index_t*)p2;
if (index_weights[*i1] < index_weights[*i2])
return 1;
else if (index_weights[*i1] == index_weights[*i2])
return 0;
return -1;
}
static void
compute_index_weights (void)
{
int i;
for (i = 0; i < NUM_INDEXES; ++i)
index_weights[i] = weight_function(i);
for (i = 0; i < NUM_INDEXES; ++i)
weight_ordered_index_to_index[i] = i;
qsort(weight_ordered_index_to_index, NUM_INDEXES, sizeof(index_t), compare_indexes_by_weight_descending);
for (i = 0; i < NUM_INDEXES; ++i)
index_to_weight_ordered_index[weight_ordered_index_to_index[i]] = i;
}
static float
wavelet_compare (coeffs_t *coeffs, metapixel_t *pixel, float best_score)
{
search_coefficients_t *query = &coeffs->wavelet.coeffs;
float *query_means = coeffs->wavelet.means;
float *sums = coeffs->wavelet.sums;
search_coefficients_t *target = &pixel->coeffs;
float *target_means = pixel->means;
float score = 0.0;
int i;
int j;
int channel;
for (channel = 0; channel < NUM_CHANNELS; ++channel)
score += index_weights[compute_index(0, channel, 0)]
* fabs(query_means[channel] - target_means[channel]) * 0.05;
j = 0;
for (i = 0; i < NUM_COEFFS; ++i)
{
if (score - sums[i] > best_score)
return FLT_MAX;
while (target->coeffs[j] < query->coeffs[i] && j < NUM_COEFFS)
++j;
if (j >= NUM_COEFFS)
break;
if (target->coeffs[j] > query->coeffs[i])
continue;
score -= index_weights[weight_ordered_index_to_index[target->coeffs[j]]];
}
return score;
}
static float
subpixel_compare (coeffs_t *coeffs, metapixel_t *pixel, float best_score)
{
int channel;
float score = 0.0;
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
int i;
for (i = 0; i < NUM_SUBPIXELS; ++i)
{
float dist = (int)coeffs->subpixel.subpixels[channel * NUM_SUBPIXELS + i]
- (int)pixel->subpixels[channel * NUM_SUBPIXELS + i];
score += dist * dist * weight_factors[channel];
if (score >= best_score)
return FLT_MAX;
}
}
return score;
}
void
add_metapixel (metapixel_t *pixel)
{
pixel->next = first_pixel;
first_pixel = pixel;
++num_metapixels;
}
static int
compare_indexes (const void *p1, const void *p2)
{
index_t *i1 = (index_t*)p1;
index_t *i2 = (index_t*)p2;
return *i1 - *i2;
}
void
generate_search_coeffs (search_coefficients_t *search_coeffs, float sums[NUM_COEFFS],
coefficient_with_index_t raw_coeffs[NUM_COEFFS])
{
int i;
float sum;
for (i = 0; i < NUM_COEFFS; ++i)
search_coeffs->coeffs[i] = index_to_weight_ordered_index[raw_coeffs[i].index];
qsort(search_coeffs->coeffs, NUM_COEFFS, sizeof(index_t), compare_indexes);
sum = 0.0;
for (i = NUM_COEFFS - 1; i >= 0; --i)
{
sum += index_weights[weight_ordered_index_to_index[search_coeffs->coeffs[i]]];
sums[i] = sum;
}
}
void
generate_search_coeffs_for_subimage (coeffs_t *coeffs, unsigned char *image_data, int image_width, int image_height,
int x, int y, int width, int height, int metric)
{
static float *float_image = 0;
if (metric == METRIC_WAVELET)
{
coefficient_with_index_t raw_coeffs[NUM_COEFFS];
int i;
if (float_image == 0)
float_image = (float*)malloc(sizeof(float) * IMAGE_SIZE * IMAGE_SIZE * NUM_CHANNELS);
if (width != IMAGE_SIZE || height != IMAGE_SIZE)
{
unsigned char *scaled_data;
scaled_data = scale_image(image_data, image_width, image_height, x, y, width, height, IMAGE_SIZE, IMAGE_SIZE);
assert(scaled_data != 0);
for (i = 0; i < IMAGE_SIZE * IMAGE_SIZE * NUM_CHANNELS; ++i)
float_image[i] = scaled_data[i];
free(scaled_data);
}
else
{
int j, channel;
for (j = 0; j < IMAGE_SIZE; ++j)
for (i = 0; i < IMAGE_SIZE; ++i)
for (channel = 0; channel < NUM_CHANNELS; ++channel)
float_image[(j * IMAGE_SIZE + i) * NUM_CHANNELS + channel] =
image_data[((y + j) * image_width + (x + i)) * NUM_CHANNELS + channel];
}
transform_rgb_to_yiq(float_image, IMAGE_SIZE * IMAGE_SIZE);
decompose_image(float_image);
find_highest_coefficients(float_image, raw_coeffs);
generate_search_coeffs(&coeffs->wavelet.coeffs, coeffs->wavelet.sums, raw_coeffs);
for (i = 0; i < NUM_CHANNELS; ++i)
coeffs->wavelet.means[i] = float_image[i];
}
else if (metric == METRIC_SUBPIXEL)
{
unsigned char *scaled_data;
int i;
int channel;
if (float_image == 0)
float_image = (float*)malloc(sizeof(float) * NUM_SUBPIXELS * NUM_CHANNELS);
scaled_data = scale_image(image_data, image_width, image_height, x, y, width, height,
NUM_SUBPIXEL_ROWS_COLS, NUM_SUBPIXEL_ROWS_COLS);
for (i = 0; i < NUM_SUBPIXELS * NUM_CHANNELS; ++i)
float_image[i] = scaled_data[i];
free(scaled_data);
transform_rgb_to_yiq(float_image, NUM_SUBPIXELS);
for (channel = 0; channel < NUM_CHANNELS; ++channel)
for (i = 0; i < NUM_SUBPIXELS; ++i)
coeffs->subpixel.subpixels[channel * NUM_SUBPIXELS + i] = float_image[i * NUM_CHANNELS + channel];
}
else
assert(0);
}
static void
generate_metapixel_coefficients (metapixel_t *pixel, unsigned char *image_data,
coefficient_with_index_t raw_coeffs[NUM_COEFFS])
{
static float float_image[NUM_CHANNELS * IMAGE_SIZE * IMAGE_SIZE];
static float sums[NUM_COEFFS];
unsigned char *scaled_data;
int i, channel;
/* generate wavelet coefficients */
if (small_width != IMAGE_SIZE || small_height != IMAGE_SIZE)
{
scaled_data = scale_image(image_data, small_width, small_height,
0, 0, small_width, small_height, IMAGE_SIZE, IMAGE_SIZE);
assert(scaled_data != 0);
}
else
scaled_data = image_data;
for (i = 0; i < IMAGE_SIZE * IMAGE_SIZE * NUM_CHANNELS; ++i)
float_image[i] = scaled_data[i];
if (scaled_data != image_data)
free(scaled_data);
transform_rgb_to_yiq(float_image, IMAGE_SIZE * IMAGE_SIZE);
decompose_image(float_image);
find_highest_coefficients(float_image, raw_coeffs);
generate_search_coeffs(&pixel->coeffs, sums, raw_coeffs);
for (i = 0; i < NUM_CHANNELS; ++i)
pixel->means[i] = float_image[i];
/* generate subpixel coefficients */
if (small_width != NUM_SUBPIXEL_ROWS_COLS || small_height != NUM_SUBPIXEL_ROWS_COLS)
{
scaled_data = scale_image(image_data, small_width, small_height, 0, 0, small_width, small_height,
NUM_SUBPIXEL_ROWS_COLS, NUM_SUBPIXEL_ROWS_COLS);
assert(scaled_data != 0);
}
else
scaled_data = image_data;
assert(NUM_SUBPIXELS <= IMAGE_SIZE * IMAGE_SIZE);
for (i = 0; i < NUM_SUBPIXELS * NUM_CHANNELS; ++i)
float_image[i] = scaled_data[i];
transform_rgb_to_yiq(float_image, NUM_SUBPIXELS);
for (channel = 0; channel < NUM_CHANNELS; ++channel)
for (i = 0; i < NUM_SUBPIXELS; ++i)
pixel->subpixels[channel * NUM_SUBPIXELS + i] = (int)float_image[i * NUM_CHANNELS + channel];
if (scaled_data != image_data)
free(scaled_data);
}
static int
metapixel_in_array (metapixel_t *pixel, metapixel_t **array, int size)
{
int i;
for (i = 0; i < size; ++i)
if (array[i] == pixel)
return 1;
return 0;
}
compare_func_t
compare_func_for_metric (int metric)
{
if (metric == METRIC_WAVELET)
return wavelet_compare;
else if (metric == METRIC_SUBPIXEL)
return subpixel_compare;
else
assert(0);
return 0;
}
static int
manhattan_distance (int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
static match_t
metapixel_nearest_to (coeffs_t *coeffs, int metric, int x, int y,
metapixel_t **forbidden, int num_forbidden,
int (*validity_func) (void*, metapixel_t*, int, int),
void *validity_func_data)
{
float best_score = FLT_MAX;
metapixel_t *best_fit = 0, *pixel;
compare_func_t compare_func = compare_func_for_metric(metric);
match_t match;
for (pixel = first_pixel; pixel != 0; pixel = pixel->next)
{
float score;
if (manhattan_distance(x, y, pixel->anti_x, pixel->anti_y)
< forbid_reconstruction_radius)
continue;
score = compare_func(coeffs, pixel, best_score);
if (score < best_score && !metapixel_in_array(pixel, forbidden, num_forbidden)
&& (validity_func == 0 || validity_func(validity_func_data, pixel, x, y)))
{
best_score = score;
best_fit = pixel;
}
}
match.pixel = best_fit;
match.score = best_score;
return match;
}
static void
get_n_metapixel_nearest_to (int n, global_match_t *matches, coeffs_t *coeffs, int metric)
{
compare_func_t compare_func = compare_func_for_metric(metric);
int i;
metapixel_t *pixel;
assert(num_metapixels >= n);
i = 0;
for (pixel = first_pixel; pixel != 0; pixel = pixel->next)
{
float score = compare_func(coeffs, pixel, (i < n) ? FLT_MAX : matches[n - 1].score);
if (i < n || score < matches[n - 1].score)
{
int j, m;
m = MIN(i, n);
for (j = 0; j < m; ++j)
if (matches[j].score > score)
break;
assert(j <= m && j < n);
memmove(matches + j + 1, matches + j, sizeof(global_match_t) * (MIN(n, m + 1) - (j + 1)));
matches[j].pixel = pixel;
matches[j].score = score;
}
++i;
}
assert(i >= n);
}
static void
paste_metapixel (metapixel_t *pixel, unsigned char *data, int width, int height, int x, int y)
{
int i;
int pixel_width, pixel_height;
unsigned char *pixel_data;
if (pixel->data != 0)
{
pixel_data = pixel->data;
pixel_width = pixel->width;
pixel_height = pixel->height;
}
else
{
pixel_data = read_image(pixel->filename, &pixel_width, &pixel_height);
if (pixel_data == 0)
{
fprintf(stderr, "Error: cannot read metapixel file `%s'\n", pixel->filename);
exit(1);
}
}
if (pixel_width != small_width || pixel_height != small_height)
{
unsigned char *scaled_data = scale_image(pixel_data, pixel_width, pixel_height,
0, 0, pixel_width, pixel_height,
small_width, small_height);
if (pixel->data == 0)
free(pixel_data);
pixel_data = scaled_data;
}
for (i = 0; i < small_height; ++i)
memcpy(data + NUM_CHANNELS * (x + (y + i) * width),
pixel_data + NUM_CHANNELS * i * small_width, NUM_CHANNELS * small_width);
if (pixel_data != pixel->data)
free(pixel_data);
}
static classic_reader_t*
init_classic_reader (char *input_name, float scale)
{
classic_reader_t *reader = (classic_reader_t*)malloc(sizeof(classic_reader_t));
assert(reader != 0);
reader->image_reader = open_image_reading(input_name);
if (reader->image_reader == 0)
{
fprintf(stderr, "cannot read image `%s'\n", input_name);
exit(1);
}
reader->in_image_width = reader->image_reader->width;
reader->in_image_height = reader->image_reader->height;
reader->out_image_width = (((int)((float)reader->in_image_width * scale) - 1) / small_width + 1) * small_width;
reader->out_image_height = (((int)((float)reader->in_image_height * scale) - 1) / small_height + 1) * small_height;
assert(reader->out_image_width % small_width == 0);
assert(reader->out_image_height % small_height == 0);
reader->metawidth = reader->out_image_width / small_width;
reader->metaheight = reader->out_image_height / small_height;
reader->in_image_data = 0;
reader->y = 0;
return reader;
}
static void
read_classic_row (classic_reader_t *reader)
{
if (reader->in_image_data != 0)
{
assert(reader->y > 0);
free(reader->in_image_data);
}
else
assert(reader->y == 0);
reader->num_lines = (reader->y + 1) * reader->in_image_height / reader->metaheight
- reader->y * reader->in_image_height / reader->metaheight;
reader->in_image_data = (unsigned char*)malloc(reader->num_lines * reader->in_image_width * NUM_CHANNELS);
assert(reader->in_image_data != 0);
read_lines(reader->image_reader, reader->in_image_data, reader->num_lines);
++reader->y;
}
static void
compute_classic_column_coords (classic_reader_t *reader, int x, int *left_x, int *width)
{
*left_x = x * reader->in_image_width / reader->metawidth;
*width = (x + 1) * reader->in_image_width / reader->metawidth - *left_x;
}
static void
generate_search_coeffs_for_classic_subimage (classic_reader_t *reader, int x, coeffs_t *coeffs, int metric)
{
int left_x, width;
compute_classic_column_coords(reader, x, &left_x, &width);
generate_search_coeffs_for_subimage(coeffs, reader->in_image_data, reader->in_image_width, reader->num_lines,
left_x, 0, width, reader->num_lines, metric);
}
static void
free_classic_reader (classic_reader_t *reader)
{
if (reader->in_image_data != 0)
free(reader->in_image_data);
free_image_reader(reader->image_reader);
free(reader);
}
static mosaic_t*
init_mosaic_from_reader (classic_reader_t *reader)
{
mosaic_t *mosaic = (mosaic_t*)malloc(sizeof(mosaic_t));
int metawidth = reader->metawidth, metaheight = reader->metaheight;
int i;
assert(mosaic != 0);
mosaic->metawidth = metawidth;
mosaic->metaheight = metaheight;
mosaic->matches = (match_t*)malloc(sizeof(match_t) * metawidth * metaheight);
for (i = 0; i < metawidth * metaheight; ++i) {
mosaic->matches[i].pixel = 0;
mosaic->matches[i].fixed = 0;
}
return mosaic;
}
static mosaic_t*
generate_local_classic (classic_reader_t *reader, int min_distance, int metric)
{
mosaic_t *mosaic = init_mosaic_from_reader(reader);
int metawidth = reader->metawidth, metaheight = reader->metaheight;