-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStyledImage.cpp
More file actions
731 lines (638 loc) · 31.4 KB
/
StyledImage.cpp
File metadata and controls
731 lines (638 loc) · 31.4 KB
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
/*
* Copyright © (2011) Institut national de l'information
* géographique et forestière
*
* Géoportail SAV <contact.geoservices@ign.fr>
*
* This software is a computer program whose purpose is to publish geographic
* data using OGC WMS and WMTS protocol.
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
*
* knowledge of the CeCILL-C license and that you accept its terms.
*/
/**
* \file StyledImage.cpp
* \~french
* \brief Implémentation de la classe StyledImage permettant l'application des styles à une image.
* \~english
* \brief Implement the StyledImage Class handling computing every style to an image.
*/
#include "image/StyledImage.h"
#include <boost/log/trivial.hpp>
int StyledImage::get_line(float *buffer, int line) {
if (style != NULL && !style->is_identity()) {
return _getline(buffer, line);
}
else {
return source_image->get_line(buffer, line);
}
}
int StyledImage::get_line(uint16_t *buffer, int line) {
if (style != NULL && !style->is_identity()) {
return _getline(buffer, line);
}
else {
return source_image->get_line(buffer, line);
}
}
int StyledImage::get_line(uint8_t *buffer, int line) {
if (style != NULL && !style->is_identity()) {
return _getline(buffer, line);
}
else {
return source_image->get_line(buffer, line);
}
}
StyledImage::StyledImage(Image *input_image, Style *input_style, int offset) : Image(input_image->get_width() - offset, input_image->get_height() - offset, input_style->get_channels(input_image->get_channels()), input_image->get_bbox()), multi_line_buffer(false) {
style = input_style;
source_image = input_image;
if (style->estompage_defined()){
// On réduit la bbox d'un pixel de chaque côté
BoundingBox<double> bb = source_image->get_bbox();
bb.xmin += source_image->get_resx();
bb.ymin += source_image->get_resy();
bb.xmax -= source_image->get_resx();
bb.ymax -= source_image->get_resy();
set_bbox(bb);
set_crs(source_image->get_crs());
// On calcule une seule fois la résolution en mètre
resxmeter = get_resx(true);
resymeter = get_resy(true);
// Buffer de lignes sources
memorized_source_lines = 3;
source_lines = new int[memorized_source_lines];
for (int i = 0; i < memorized_source_lines; i++) {
source_lines[i] = -1;
}
source_lines_buffer = new float[source_image->get_width() * memorized_source_lines];
}
else if (style->pente_defined()) {
// On réduit la bbox d'un pixel de chaque côté
BoundingBox<double> bb = source_image->get_bbox();
bb.xmin += source_image->get_resx();
bb.ymin += source_image->get_resy();
bb.xmax -= source_image->get_resx();
bb.ymax -= source_image->get_resy();
set_bbox(bb);
set_crs(source_image->get_crs());
// On calcule une seule fois la résolution en mètre
resxmeter = get_resx(true);
resymeter = get_resy(true);
// Buffer de lignes sources
memorized_source_lines = 3;
source_lines = new int[memorized_source_lines];
for (int i = 0; i < memorized_source_lines; i++) {
source_lines[i] = -1;
}
source_lines_buffer = new float[source_image->get_width() * memorized_source_lines];
}
else if (style->aspect_defined()) {
resolution = (input_image->get_mean_resolution());
// On réduit la bbox d'un pixel de chaque côté
BoundingBox<double> bb = source_image->get_bbox();
bb.xmin += source_image->get_resx();
bb.ymin += source_image->get_resy();
bb.xmax -= source_image->get_resx();
bb.ymax -= source_image->get_resy();
set_bbox(bb);
set_crs(source_image->get_crs());
// Buffer de lignes sources
memorized_source_lines = 3;
source_lines = new int[memorized_source_lines];
for (int i = 0; i < memorized_source_lines; i++) {
source_lines[i] = -1;
}
source_lines_buffer = new float[source_image->get_width() * memorized_source_lines];
matrix[0] = 1 / (8.0*resolution) ;
matrix[1] = 2 / (8.0*resolution) ;
matrix[2] = 1 / (8.0*resolution) ;
matrix[3] = 2 / (8.0*resolution) ;
matrix[4] = 0 ;
matrix[5] = 2 / (8.0*resolution) ;
matrix[6] = 1 / (8.0*resolution) ;
matrix[7] = 2 / (8.0*resolution) ;
matrix[8] = 1 / (8.0*resolution) ;
}
else if (style->terrainrgb_defined()) {
if (source_image->get_channels() == 1) {
channels = 3;
} else {
channels = input_image->get_channels();
}
}
else if (style->colorize_defined()) {
if (source_image->get_channels() == 3 || source_image->get_channels() == 4) {
channels = style->get_colorize()->destination.size();
} else {
channels = input_image->get_channels();
}
}
if (style->palette_defined()){
// Il n'y aura application de la palette et modification des canaux que si
// - la palette n'est pas nulle et pas vide
// - l'image source est sur un canal
if ( source_image->get_channels() == 1 && style->get_palette() != NULL && ! style->get_palette()->is_empty() ) {
if (style->get_palette()->is_no_alpha()) {
channels = 3;
} else {
channels = 4;
}
} else {
channels = source_image->get_channels();
}
}
}
template <typename T>
int StyledImage::_getline(T *buffer, int line) {
int space;
float *source = new float[source_image->get_width() * source_image->get_channels()];
source_image->get_line(source, line);
if (style->estompage_defined()) {
// L'image source fait une ligne de plus en haut et en bas (ligne 0 de l'image estompée = ligne 1 de l'image source)
// et une colonne de plus à gauche et à droite
// Pour obtenir la ligne 0 de l'image estompée, on a besoin des lignes 0, 1 et 2 de l'image source
// Plus généralement, pour avoir la ligne n de l'image estompée, on a besoin des lignes
// n, n+1 et n+2 de l'image source
// On range les lignes sources dans un buffer qui peut en stocker 3
// La ligne source n est stockée en (n % memorized_source_lines) ème position
multi_line_buffer = true;
// calcul des emplacements dans le buffer des 3 lignes sources nécessaires
float *line1 = source_lines_buffer + (line % memorized_source_lines) * source_image->get_width();
float *line2 = source_lines_buffer + ((line + 1) % memorized_source_lines) * source_image->get_width();
float *line3 = source_lines_buffer + ((line + 2) % memorized_source_lines) * source_image->get_width();
// ligne du dessus
if (source_lines[line % memorized_source_lines] != line) {
// la ligne source 'line' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line(line1, line);
source_lines[line % memorized_source_lines] = line;
}
// ligne du milieu
if (source_lines[(line + 1) % memorized_source_lines] != line + 1) {
// la ligne source 'line + 1' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line(line2, line + 1);
source_lines[(line + 1) % memorized_source_lines] = line + 1;
}
// ligne du dessous
if (source_lines[(line + 2) % memorized_source_lines] != line + 2) {
// la ligne source 'line + 2' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line(line3, line + 2);
source_lines[(line + 2) % memorized_source_lines] = line + 2;
}
int column_orig = 1;
int column = 0;
double value;
float dzdx, dzdy, slope, aspect;
float a, b, c, d, e, f, g, h, i;
while (column < width) {
a = (*(line1 + column_orig - 1));
b = (*(line1 + column_orig));
c = (*(line1 + column_orig + 1));
d = (*(line2 + column_orig - 1));
e = (*(line2 + column_orig));
f = (*(line2 + column_orig + 1));
g = (*(line3 + column_orig - 1));
h = (*(line3 + column_orig));
i = (*(line3 + column_orig + 1));
if (a == style->get_estompage()->input_nodata_value || b == style->get_estompage()->input_nodata_value || c == style->get_estompage()->input_nodata_value || d == style->get_estompage()->input_nodata_value || e == style->get_estompage()->input_nodata_value ||
f == style->get_estompage()->input_nodata_value || g == style->get_estompage()->input_nodata_value || h == style->get_estompage()->input_nodata_value || i == style->get_estompage()->input_nodata_value) {
value = style->get_estompage()->estompage_nodata_value;
}
else {
dzdx = ((c + 2 * f + i) - (a + 2 * d + g)) / (8 * resxmeter);
dzdy = ((g + 2 * h + i) - (a + 2 * b + c)) / (8 * resymeter);
slope = atan(style->get_estompage()->z_factor * sqrt(dzdx * dzdx + dzdy * dzdy));
if (dzdx != 0) {
aspect = atan2(dzdy, -dzdx);
if (aspect < 0) {
aspect = 2 * M_PI + aspect;
}
}
else {
if (dzdy > 0) {
aspect = M_PI_2;
}
else {
aspect = 2 * M_PI - M_PI_2;
}
}
value = 255.0 * ((cos(style->get_estompage()->zenith) * cos(slope)) + (sin(style->get_estompage()->zenith) * sin(slope) * cos(style->get_estompage()->azimuth - aspect)));
if (value < 0) {
value = style->get_estompage()->estompage_nodata_value;
}
}
*(buffer + (column++)) = (T)(value);
column_orig++;
}
space = width * sizeof(T);
}
else if (style->pente_defined()) {
// L'image source fait une ligne de plus en haut et en bas (ligne 0 de l'image estompée = ligne 1 de l'image source)
// et une colonne de plus à gauche et à droite
// Pour obtenir la ligne 0 de l'image estompée, on a besoin des lignes 0, 1 et 2 de l'image source
// Plus généralement, pour avoir la ligne n de l'image estompée, on a besoin des lignes
// n, n+1 et n+2 de l'image source
// On range les lignes sources dans un buffer qui peut en stocker "memorized_source_lines"
// La ligne source n est stockée en (n % memorized_source_lines) ème position
multi_line_buffer = true;
// calcul des emplacements dans le buffer des 3 lignes sources nécessaires
float* line1 = source_lines_buffer + (line % memorized_source_lines) * source_image->get_width();
float* line2 = source_lines_buffer + ((line + 1) % memorized_source_lines) * source_image->get_width();
float* line3 = source_lines_buffer + ((line + 2) % memorized_source_lines) * source_image->get_width();
// ligne du dessus
if (source_lines[line % memorized_source_lines] != line) {
// la ligne source 'line' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line (line1 , line);
source_lines[line % memorized_source_lines] = line;
}
// ligne du milieu
if (source_lines[(line + 1) % memorized_source_lines] != line + 1) {
// la ligne source 'line + 1' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line (line2 , line + 1);
source_lines[(line + 1) % memorized_source_lines] = line + 1;
}
// ligne du dessous
if (source_lines[(line + 2) % memorized_source_lines] != line + 2) {
// la ligne source 'line + 2' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line (line3 , line + 2);
source_lines[(line + 2) % memorized_source_lines] = line + 2;
}
//on commence a la premiere colonne
int columnOrig = 1;
int column = 0;
//creation de la variable sur laquelle on travaille pour trouver le seuil
double dzdx,dzdy,rise,slope;
float a,b,c,d,e,f,g,h,i;
float resx,resy;
if (style->get_pente()->algo == "H") {
resx = 8.0 * resxmeter;
resy = 8.0 * resymeter;
} else if (style->get_pente()->algo == "Z") {
resx = 2.0 * resxmeter;
resy = 2.0 * resymeter;
}
//calcul de la variable sur toutes les autres colonnes
while ( column < width ) {
a = ( * ( line1+columnOrig-1 ) );
b = ( * ( line1+columnOrig ) );
c = ( * ( line1+columnOrig+1 ) );
d = ( * ( line2+columnOrig-1 ) );
e = ( * ( line2+columnOrig ) );
f = ( * ( line2+columnOrig+1 ) );
g = ( * ( line3+columnOrig-1 ) );
h = ( * ( line3+columnOrig ) );
i = ( * ( line3+columnOrig+1 ) );
if (a == style->get_pente()->input_nodata_value || b == style->get_pente()->input_nodata_value || c == style->get_pente()->input_nodata_value || d == style->get_pente()->input_nodata_value || e == style->get_pente()->input_nodata_value ||
f == style->get_pente()->input_nodata_value || g == style->get_pente()->input_nodata_value || h == style->get_pente()->input_nodata_value || i == style->get_pente()->input_nodata_value) {
slope = style->get_pente()->slope_nodata_value;
} else {
if (style->get_pente()->algo == "H") {
dzdx = (( c + 2.0 * f + i) - (a + 2.0 * d + g)) / resx;
dzdy = (( g + 2.0 * h + i) - (a + 2.0 * b + c)) / resy;
} else if (style->get_pente()->algo == "Z" ) {
dzdx = (f - d) / resx;
dzdy = (h - b) / resy;
} else {
}
if (style->get_pente()->unit == "pourcent") {
slope = sqrt(pow(dzdx,2.0) + pow(dzdy,2.0)) * 100.0;
} else if (style->get_pente()->unit == "degree") {
rise = sqrt(pow(dzdx,2.0) + pow(dzdy,2.0));
slope = atan(rise) * 180.0 / M_PI;
if (slope>90.0){slope = 180.0-slope;}
} else {
slope = 0;
}
if (slope > style->get_pente()->max_slope) {
slope = style->get_pente()->max_slope;
}
}
* ( buffer + ( column++ ) ) = ( T ) ( slope );
columnOrig++;
}
space = width * sizeof(T);
}
else if (style->aspect_defined()) {
// L'image source fait une ligne de plus en haut et en bas (ligne 0 de l'image estompée = ligne 1 de l'image source)
// et une colonne de plus à gauche et à droite
// Pour obtenir la ligne 0 de l'image aspect, on a besoin des lignes 0, 1 et 2 de l'image source
// Plus généralement, pour avoir la ligne n de l'image aspect, on a besoin des lignes
// n, n+1 et n+2 de l'image source
// On range les lignes sources dans un buffer qui peut en stocker 3
// La ligne source n est stockée en (n % memorized_source_lines) ème position
multi_line_buffer = true;
// calcul des emplacements dans le buffer des 3 lignes sources nécessaires
float* line1 = source_lines_buffer + (line % memorized_source_lines) * source_image->get_width();
float* line2 = source_lines_buffer + ((line + 1) % memorized_source_lines) * source_image->get_width();
float* line3 = source_lines_buffer + ((line + 2) % memorized_source_lines) * source_image->get_width();
// ligne du dessus
if (source_lines[line % memorized_source_lines] != line) {
// la ligne source 'line' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line (line1 , line);
source_lines[line % memorized_source_lines] = line;
}
// ligne du milieu
if (source_lines[(line + 1) % memorized_source_lines] != line + 1) {
// la ligne source 'line + 1' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line (line2 , line + 1);
source_lines[(line + 1) % memorized_source_lines] = line + 1;
}
// ligne du dessous
if (source_lines[(line + 2) % memorized_source_lines] != line + 2) {
// la ligne source 'line + 2' n'est pas celle stockée dans le buffer, on doit la lire
source_image->get_line (line3 , line + 2);
source_lines[(line + 2) % memorized_source_lines] = line + 2;
}
//on commence a la premiere colonne
int columnOrig = 1;
int column = 0;
//creation de la variable sur laquelle on travaille pour trouver le seuil
double value,value1,value2,slope;
float a,b,c,d,e,f,g,h,i;
//calcul de la variable sur toutes les autres colonnes
while ( column < width ) {
a = ( * ( line1+columnOrig-1 ) );
b = ( * ( line1+columnOrig ) );
c = ( * ( line1+columnOrig+1 ) );
d = ( * ( line2+columnOrig-1 ) );
e = ( * ( line2+columnOrig ) );
f = ( * ( line2+columnOrig+1 ) );
g = ( * ( line3+columnOrig-1 ) );
h = ( * ( line3+columnOrig ) );
i = ( * ( line3+columnOrig+1 ) );
if (a == style->get_aspect()->input_nodata_value || b == style->get_aspect()->input_nodata_value || c == style->get_aspect()->input_nodata_value || d == style->get_aspect()->input_nodata_value || e == style->get_aspect()->input_nodata_value ||
f == style->get_aspect()->input_nodata_value || g == style->get_aspect()->input_nodata_value || h == style->get_aspect()->input_nodata_value || i == style->get_aspect()->input_nodata_value) {
value = style->get_aspect()->aspect_nodata_value;
} else {
value1 = (matrix[2] * c + matrix[5] * f + matrix[8] * i - matrix[0] * a - matrix[3] * d - matrix[6] * g);
value2 = (matrix[0] * a + matrix[1] * b + matrix[2] * c - matrix[6] * g - matrix[7] * h - matrix[8] * i);
//calcul de la pente pour ne pas afficher l'exposition en dessous d'une certaine valeur de pente
slope = sqrt(pow(value1,2.0)+pow(value2,2.0));
if (slope < style->get_aspect()->min_slope) {
value = style->get_aspect()->aspect_nodata_value;
} else {
value = (atan2(value1,value2) + M_PI) * 180 / M_PI;
}
}
* ( buffer + ( column++ ) ) = (T) ( value );
columnOrig++;
}
space = width * sizeof(T);
}
else if (style->terrainrgb_defined()) {
switch ( channels ) {
case 3:
for (int i = 0; i < source_image->get_width() ; i++ ) {
// découpage de l'altitude en RGB suivant la formule suivante : height = min_elevation + ((Red * 256 * 256 + Green * 256 + Blue) * step)
int base = (std::max( *(source+i), style->get_terrainrgb()->min_elevation) - style->get_terrainrgb()->min_elevation) / style->get_terrainrgb()->step;
int red = (base / (256 * 256) % 256);
int green = ((base - red * 256 * 256) / 256 % 256);
int blue = (base - red * 256 * 256 - green * 256);
* ( buffer+i*3 ) = (T) red;
* ( buffer+i*3+1 ) = (T) green;
* ( buffer+i*3+2 ) = (T) blue;
}
break;
}
space = width * sizeof ( T ) * channels;
}
else if (style->colorize_defined()) {
Colorize* wta = style->get_colorize();
switch ( channels ) {
case 3:
if (source_image->get_channels()==3){
for (int i = 0; i < source_image->get_width() ; i++ ) {
//image de départ à 3 canaux pour une arrivée en 3 canaux
int red = *(source+i*3);
int green = *(source+i*3+1);
int blue = *(source+i*3+2);
red = red < 0 ? 0 : (red > 255 ? 255 : red);
green = green < 0 ? 0 : (green > 255 ? 255 : green);
blue = blue < 0 ? 0 : (blue > 255 ? 255 : blue);
int s_red = wta->source[0];
int s_green = wta->source[1];
int s_blue = wta->source[2];
if (red>=s_red-wta->tolerance && red<=s_red+wta->tolerance &&
green>=s_green-wta->tolerance && green<=s_green+wta->tolerance &&
blue>=s_blue-wta->tolerance && blue<=s_blue+wta->tolerance){
* ( buffer+i*3 ) = (T) wta->destination[0];
* ( buffer+i*3+1 ) = (T) wta->destination[1];
* ( buffer+i*3+2 ) = (T) wta->destination[2];
}
else{
* ( buffer+i*3 ) = (T) red;
* ( buffer+i*3+1 ) = (T) green;
* ( buffer+i*3+2 ) = (T) blue;
}
}
}
if (source_image->get_channels()==4){
for (int i = 0; i < source_image->get_width() ; i++ ) {
//image de départ à 4 canaux pour une arrivée en 3 canaux
int red = *(source+i*4);
int green = *(source+i*4+1);
int blue = *(source+i*4+2);
int alpha = *(source+i*4+3);
red = red < 0 ? 0 : (red > 255 ? 255 : red);
green = green < 0 ? 0 : (green > 255 ? 255 : green);
blue = blue < 0 ? 0 : (blue > 255 ? 255 : blue);
alpha = alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha);
int s_red = wta->source[0];
int s_green = wta->source[1];
int s_blue = wta->source[2];
int s_alpha = wta->source[3];
if (red>=s_red-wta->tolerance && red<=s_red+wta->tolerance &&
green>=s_green-wta->tolerance && green<=s_green+wta->tolerance &&
blue>=s_blue-wta->tolerance && blue<=s_blue+wta->tolerance &&
alpha>=s_alpha-wta->tolerance && alpha<=s_alpha+wta->tolerance){
* ( buffer+i*3 ) = (T) wta->destination[0];
* ( buffer+i*3+1 ) = (T) wta->destination[1];
* ( buffer+i*3+2 ) = (T) wta->destination[2];
}
else{
* ( buffer+i*3 ) = (T) red;
* ( buffer+i*3+1 ) = (T) green;
* ( buffer+i*3+2 ) = (T) blue;
}
}
}
break;
case 4:
if (source_image->get_channels()==3){
for (int i = 0; i < source_image->get_width() ; i++ ) {
//image de départ à 3 canaux pour une arrivée en 4 canaux
int red = *(source+i*3);
int green = *(source+i*3+1);
int blue = *(source+i*3+2);
red = red < 0 ? 0 : (red > 255 ? 255 : red);
green = green < 0 ? 0 : (green > 255 ? 255 : green);
blue = blue < 0 ? 0 : (blue > 255 ? 255 : blue);
int s_red = wta->source[0];
int s_green = wta->source[1];
int s_blue = wta->source[2];
if (red>=s_red-wta->tolerance && red<=s_red+wta->tolerance &&
green>=s_green-wta->tolerance && green<=s_green+wta->tolerance &&
blue>=s_blue-wta->tolerance && blue<=s_blue+wta->tolerance){
* ( buffer+i*4 ) = (T) wta->destination[0];
* ( buffer+i*4+1 ) = (T) wta->destination[1];
* ( buffer+i*4+2 ) = (T) wta->destination[2];
* ( buffer+i*4+3 ) = (T) wta->destination[3];
}
else{
* ( buffer+i*4 ) = (T) red;
* ( buffer+i*4+1 ) = (T) green;
* ( buffer+i*4+2 ) = (T) blue;
* ( buffer+i*4+3 ) = (T) 255;
}
}
}
if (source_image->get_channels()==4){
for (int i = 0; i < source_image->get_width() ; i++ ) {
//image de départ à 4 canaux pour une arrivée en 4 canaux
int red = *(source+i*4);
int green = *(source+i*4+1);
int blue = *(source+i*4+2);
int alpha = *(source+i*4+3);
red = red < 0 ? 0 : (red > 255 ? 255 : red);
green = green < 0 ? 0 : (green > 255 ? 255 : green);
blue = blue < 0 ? 0 : (blue > 255 ? 255 : blue);
alpha = alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha);
int s_red = wta->source[0];
int s_green = wta->source[1];
int s_blue = wta->source[2];
int s_alpha = wta->source[3];
if (red>=s_red-wta->tolerance && red<=s_red+wta->tolerance &&
green>=s_green-wta->tolerance && green<=s_green+wta->tolerance &&
blue>=s_blue-wta->tolerance && blue<=s_blue+wta->tolerance &&
alpha>=s_alpha-wta->tolerance && alpha<=s_alpha+wta->tolerance){
* ( buffer+i*4 ) = (T) wta->destination[0];
* ( buffer+i*4+1 ) = (T) wta->destination[1];
* ( buffer+i*4+2 ) = (T) wta->destination[2];
* ( buffer+i*4+3 ) = (T) wta->destination[3];
}
else{
* ( buffer+i*4 ) = (T) red;
* ( buffer+i*4+1 ) = (T) green;
* ( buffer+i*4+2 ) = (T) blue;
* ( buffer+i*4+3 ) = (T) alpha;
}
}
}
break;
}
space = width * sizeof ( T ) * channels;
}
if (style->palette_defined()){
switch ( channels ) {
case 4:
for (int i = 0; i < source_image->get_width() ; i++ ) {
Colour iColour = style->get_palette()->get_colour ( * ( source+i ) );
* ( buffer+i*4 ) = (T) iColour.r;
* ( buffer+i*4+1 ) = (T) iColour.g;
* ( buffer+i*4+2 ) = (T) iColour.b;
* ( buffer+i*4+3 ) = (T) iColour.a;
}
break;
case 3:
for (int i = 0; i < source_image->get_width() ; i++ ) {
Colour iColour = style->get_palette()->get_colour ( * ( source+i ) );
* ( buffer+i*3 ) = (T) iColour.r;
* ( buffer+i*3+1 ) = (T) iColour.g;
* ( buffer+i*3+2 ) = (T) iColour.b;
}
break;
}
space = width * sizeof ( T ) * channels;
}
delete[] source;
return space;
}
StyledImage *StyledImage::create(Image *input_image, Style *input_style) {
int offset=0;
if (input_style->estompage_defined() || input_style->pente_defined() || input_style->aspect_defined()) {
if (input_image->get_width()<=2 && input_image->get_height()<=2){
BOOST_LOG_TRIVIAL(error)<<"L'image source est trop petite pour appliquer ce style";
return NULL;
}
if (input_image->get_channels()!=1){
BOOST_LOG_TRIVIAL(error)<<"Ce style ne s'applique que sur une image source à un canal";
return NULL;
}
offset=2;
}
if (input_style->terrainrgb_defined() && input_style->palette_defined()) {
BOOST_LOG_TRIVIAL(error)<<"Les styles terrainrgb et palette ne sont pas compatibles";
return NULL;
}
if (input_style->terrainrgb_defined()){
if (input_image->get_channels()!=1){
BOOST_LOG_TRIVIAL(error)<<"Ce style ne s'applique que sur une image source à un canal";
return NULL;
}
}
if (input_style->colorize_defined()){
if (input_image->get_channels()!=3 && input_image->get_channels()!=4){
BOOST_LOG_TRIVIAL(error)<<"Ce style ne s'applique que sur une image source à trois ou quatre canaux";
return NULL;
}
if (input_style->palette_defined()){
BOOST_LOG_TRIVIAL(error)<<"Le style colorize n'est pas compatible avec une palette";
return NULL;
}
}
return new StyledImage(input_image,input_style,offset);
}
StyledImage::~StyledImage() {
if (multi_line_buffer) {
delete[] source_lines;
delete[] source_lines_buffer;
}
delete source_image;
}
void StyledImage::print() {
BOOST_LOG_TRIVIAL(info) << "" ;
BOOST_LOG_TRIVIAL(info) << "--------- StyledImage -----------" ;
if (style->aspect_defined()){
BOOST_LOG_TRIVIAL(info) << "--------- Aspect -----------" ;
}
if (style->estompage_defined()){
BOOST_LOG_TRIVIAL(info) << "--------- Estompage -----------" ;
}
if (style->pente_defined()){
BOOST_LOG_TRIVIAL(info) << "--------- Pente -----------" ;
}
if (style->terrainrgb_defined()){
BOOST_LOG_TRIVIAL(info) << "--------- Terrainrgb -----------" ;
}
if (style->colorize_defined()){
BOOST_LOG_TRIVIAL(info) << "--------- Colorize -----------" ;
}
if (style->palette_defined()){
BOOST_LOG_TRIVIAL(info) << "--------- Palette -----------" ;
}
Image::print();
BOOST_LOG_TRIVIAL(info) << "" ;
}