-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResize.cpp
More file actions
2116 lines (1863 loc) · 79.8 KB
/
Resize.cpp
File metadata and controls
2116 lines (1863 loc) · 79.8 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
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
// ==========================================================
// Upsampling / downsampling classes
//
// Design and implementation by
// - Hervé Drolon (drolon@infonie.fr)
// - Detlev Vendt (detlev.vendt@brillit.de)
// - Carsten Klein (cklein05@users.sourceforge.net)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "Resize.h"
/**
Returns the color type of a bitmap. In contrast to FreeImage_GetColorType,
this function optionally supports a boolean OUT parameter, that receives TRUE,
if the specified bitmap is greyscale, that is, it consists of grey colors only.
Although it returns the same value as returned by FreeImage_GetColorType for all
image types, this extended function primarily is intended for palletized images,
since the boolean pointed to by 'bIsGreyscale' remains unchanged for RGB(A/F)
images. However, the outgoing boolean is properly maintained for palletized images,
as well as for any non-RGB image type, like FIT_UINTxx and FIT_DOUBLE, for example.
@param dib A pointer to a FreeImage bitmap to calculate the extended color type for
@param bIsGreyscale A pointer to a boolean, that receives TRUE, if the specified bitmap
is greyscale, that is, it consists of grey colors only. This parameter can be NULL.
@return the color type of the specified bitmap
*/
static FREE_IMAGE_COLOR_TYPE
GetExtendedColorType(FIBITMAP *dib, BOOL *bIsGreyscale) {
const unsigned bpp = FreeImage_GetBPP(dib);
const unsigned size = CalculateUsedPaletteEntries(bpp);
const RGBQUAD * const pal = FreeImage_GetPalette(dib);
FREE_IMAGE_COLOR_TYPE color_type = FIC_MINISBLACK;
BOOL bIsGrey = TRUE;
switch (bpp) {
case 1:
{
for (unsigned i = 0; i < size; i++) {
if ((pal[i].rgbRed != pal[i].rgbGreen) || (pal[i].rgbRed != pal[i].rgbBlue)) {
color_type = FIC_PALETTE;
bIsGrey = FALSE;
break;
}
}
if (bIsGrey) {
if (pal[0].rgbBlue == 255 && pal[1].rgbBlue == 0) {
color_type = FIC_MINISWHITE;
} else if (pal[0].rgbBlue != 0 || pal[1].rgbBlue != 255) {
color_type = FIC_PALETTE;
}
}
break;
}
case 4:
case 8:
{
for (unsigned i = 0; i < size; i++) {
if ((pal[i].rgbRed != pal[i].rgbGreen) || (pal[i].rgbRed != pal[i].rgbBlue)) {
color_type = FIC_PALETTE;
bIsGrey = FALSE;
break;
}
if (color_type != FIC_PALETTE && pal[i].rgbBlue != i) {
if ((size - i - 1) != pal[i].rgbBlue) {
color_type = FIC_PALETTE;
if (!bIsGreyscale) {
// exit loop if we're not setting
// bIsGreyscale parameter
break;
}
} else {
color_type = FIC_MINISWHITE;
}
}
}
break;
}
default:
{
color_type = FreeImage_GetColorType(dib);
bIsGrey = (color_type == FIC_MINISBLACK) ? TRUE : FALSE;
break;
}
}
if (bIsGreyscale) {
*bIsGreyscale = bIsGrey;
}
return color_type;
}
/**
Returns a pointer to an RGBA palette, created from the specified bitmap.
The RGBA palette is a copy of the specified bitmap's palette, that, additionally
contains the bitmap's transparency information in the rgbReserved member
of the palette's RGBQUAD elements.
@param dib A pointer to a FreeImage bitmap to create the RGBA palette from.
@param buffer A pointer to the buffer to store the RGBA palette.
@return A pointer to the newly created RGBA palette or NULL, if the specified
bitmap is no palletized standard bitmap. If non-NULL, the returned value is
actually the pointer passed in parameter 'buffer'.
*/
static inline RGBQUAD *
GetRGBAPalette(FIBITMAP *dib, RGBQUAD * const buffer) {
// clone the palette
const unsigned ncolors = FreeImage_GetColorsUsed(dib);
if (ncolors == 0) {
return NULL;
}
memcpy(buffer, FreeImage_GetPalette(dib), ncolors * sizeof(RGBQUAD));
// merge the transparency table
const unsigned ntransp = MIN(ncolors, FreeImage_GetTransparencyCount(dib));
const BYTE * const tt = FreeImage_GetTransparencyTable(dib);
for (unsigned i = 0; i < ntransp; i++) {
buffer[i].rgbReserved = tt[i];
}
for (unsigned i = ntransp; i < ncolors; i++) {
buffer[i].rgbReserved = 255;
}
return buffer;
}
// --------------------------------------------------------------------------
CWeightsTable::CWeightsTable(CGenericFilter *pFilter, unsigned uDstSize, unsigned uSrcSize) {
double dWidth;
double dFScale;
const double dFilterWidth = pFilter->GetWidth();
// scale factor
const double dScale = double(uDstSize) / double(uSrcSize);
if(dScale < 1.0) {
// minification
dWidth = dFilterWidth / dScale;
dFScale = dScale;
} else {
// magnification
dWidth = dFilterWidth;
dFScale = 1.0;
}
// allocate a new line contributions structure
//
// window size is the number of sampled pixels
m_WindowSize = 2 * (int)ceil(dWidth) + 1;
// length of dst line (no. of rows / cols)
m_LineLength = uDstSize;
// allocate list of contributions
m_WeightTable = (Contribution*)malloc(m_LineLength * sizeof(Contribution));
for(unsigned u = 0; u < m_LineLength; u++) {
// allocate contributions for every pixel
m_WeightTable[u].Weights = (double*)malloc(m_WindowSize * sizeof(double));
}
// offset for discrete to continuous coordinate conversion
const double dOffset = (0.5 / dScale);
for(unsigned u = 0; u < m_LineLength; u++) {
// scan through line of contributions
// inverse mapping (discrete dst 'u' to continous src 'dCenter')
const double dCenter = (double)u / dScale + dOffset;
// find the significant edge points that affect the pixel
const int iLeft = MAX(0, (int)(dCenter - dWidth + 0.5));
const int iRight = MIN((int)(dCenter + dWidth + 0.5), int(uSrcSize));
m_WeightTable[u].Left = iLeft;
m_WeightTable[u].Right = iRight;
double dTotalWeight = 0; // sum of weights (initialized to zero)
for(int iSrc = iLeft; iSrc < iRight; iSrc++) {
// calculate weights
const double weight = dFScale * pFilter->Filter(dFScale * ((double)iSrc + 0.5 - dCenter));
// assert((iSrc-iLeft) < m_WindowSize);
m_WeightTable[u].Weights[iSrc-iLeft] = weight;
dTotalWeight += weight;
}
if((dTotalWeight > 0) && (dTotalWeight != 1)) {
// normalize weight of neighbouring points
for(int iSrc = iLeft; iSrc < iRight; iSrc++) {
// normalize point
m_WeightTable[u].Weights[iSrc-iLeft] /= dTotalWeight;
}
}
// simplify the filter, discarding null weights at the right
{
int iTrailing = iRight - iLeft - 1;
while(m_WeightTable[u].Weights[iTrailing] == 0) {
m_WeightTable[u].Right--;
iTrailing--;
if(m_WeightTable[u].Right == m_WeightTable[u].Left) {
break;
}
}
}
} // next dst pixel
}
CWeightsTable::~CWeightsTable() {
for(unsigned u = 0; u < m_LineLength; u++) {
// free contributions for every pixel
free(m_WeightTable[u].Weights);
}
// free list of pixels contributions
free(m_WeightTable);
}
// --------------------------------------------------------------------------
FIBITMAP* CResizeEngine::scale(FIBITMAP *src, unsigned dst_width, unsigned dst_height, unsigned src_left, unsigned src_top, unsigned src_width, unsigned src_height, unsigned flags) {
const FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(src);
const unsigned src_bpp = FreeImage_GetBPP(src);
// determine the image's color type
BOOL bIsGreyscale = FALSE;
FREE_IMAGE_COLOR_TYPE color_type;
if (src_bpp <= 8) {
color_type = GetExtendedColorType(src, &bIsGreyscale);
} else {
color_type = FIC_RGB;
}
// determine the required bit depth of the destination image
unsigned dst_bpp;
unsigned dst_bpp_s1 = 0;
if (color_type == FIC_PALETTE && !bIsGreyscale) {
// non greyscale FIC_PALETTE images require a high-color destination
// image (24- or 32-bits depending on the image's transparent state)
dst_bpp = FreeImage_IsTransparent(src) ? 32 : 24;
} else if (src_bpp <= 8) {
// greyscale images require an 8-bit destination image
// (or a 32-bit image if the image is transparent);
// however, if flag FI_RESCALE_TRUE_COLOR is set, we will return
// a true color (24 bpp) image
if (FreeImage_IsTransparent(src)) {
dst_bpp = 32;
// additionally, for transparent images we always need a
// palette including transparency information (an RGBA palette)
// so, set color_type accordingly
color_type = FIC_PALETTE;
} else {
dst_bpp = ((flags & FI_RESCALE_TRUE_COLOR) == FI_RESCALE_TRUE_COLOR) ? 24 : 8;
// in any case, we use a fast 8-bit temporary image for the
// first filter operation (stage 1, either horizontal or
// vertical) and implicitly convert to 24 bpp (if requested
// by flag FI_RESCALE_TRUE_COLOR) during the second filter
// operation
dst_bpp_s1 = 8;
}
} else if (src_bpp == 16 && image_type == FIT_BITMAP) {
// 16-bit 555 and 565 RGB images require a high-color destination
// image (fixed to 24 bits, since 16-bit RGBs don't support
// transparency in FreeImage)
dst_bpp = 24;
} else {
// bit depth remains unchanged for all other images
dst_bpp = src_bpp;
}
// make 'stage 1' bpp a copy of the destination bpp if it
// was not explicitly set
if (dst_bpp_s1 == 0) {
dst_bpp_s1 = dst_bpp;
}
// early exit if destination size is equal to source size
if ((src_width == dst_width) && (src_height == dst_height)) {
FIBITMAP *out = src;
FIBITMAP *tmp = src;
if ((src_width != FreeImage_GetWidth(src)) || (src_height != FreeImage_GetHeight(src))) {
out = FreeImage_Copy(tmp, src_left, src_top, src_left + src_width, src_top + src_height);
tmp = out;
}
if (src_bpp != dst_bpp) {
switch (dst_bpp) {
case 8:
out = FreeImage_ConvertToGreyscale(tmp);
break;
case 24:
out = FreeImage_ConvertTo24Bits(tmp);
break;
case 32:
out = FreeImage_ConvertTo32Bits(tmp);
break;
default:
break;
}
if (tmp != src) {
FreeImage_Unload(tmp);
tmp = NULL;
}
}
return (out != src) ? out : FreeImage_Clone(src);
}
RGBQUAD pal_buffer[256];
RGBQUAD *src_pal = NULL;
// provide the source image's palette to the rescaler for
// FIC_PALETTE type images (this includes palletized greyscale
// images with an unordered palette as well as transparent images)
if (color_type == FIC_PALETTE) {
if (dst_bpp == 32) {
// a 32-bit destination image signals transparency, so
// create an RGBA palette from the source palette
src_pal = GetRGBAPalette(src, pal_buffer);
} else {
src_pal = FreeImage_GetPalette(src);
}
}
// allocate the dst image
FIBITMAP *dst = FreeImage_AllocateT(image_type, dst_width, dst_height, dst_bpp, 0, 0, 0);
if (!dst) {
return NULL;
}
if (dst_bpp == 8) {
RGBQUAD * const dst_pal = FreeImage_GetPalette(dst);
if (color_type == FIC_MINISWHITE) {
// build an inverted greyscale palette
CREATE_GREYSCALE_PALETTE_REVERSE(dst_pal, 256);
}
/*
else {
// build a default greyscale palette
// Currently, FreeImage_AllocateT already creates a default
// greyscale palette for 8 bpp images, so we can skip this here.
CREATE_GREYSCALE_PALETTE(dst_pal, 256);
}
*/
}
// calculate x and y offsets; since FreeImage uses bottom-up bitmaps, the
// value of src_offset_y is measured from the bottom of the image
unsigned src_offset_x = src_left;
unsigned src_offset_y = FreeImage_GetHeight(src) - src_height - src_top;
/*
Decide which filtering order (xy or yx) is faster for this mapping.
--- The theory ---
Try to minimize calculations by counting the number of convolution multiplies
if(dst_width*src_height <= src_width*dst_height) {
// xy filtering
} else {
// yx filtering
}
--- The practice ---
Try to minimize calculations by counting the number of vertical convolutions (the most time consuming task)
if(dst_width*dst_height <= src_width*dst_height) {
// xy filtering
} else {
// yx filtering
}
*/
if (dst_width <= src_width) {
// xy filtering
// -------------
FIBITMAP *tmp = NULL;
if (src_width != dst_width) {
// source and destination widths are different so, we must
// filter horizontally
if (src_height != dst_height) {
// source and destination heights are also different so, we need
// a temporary image
tmp = FreeImage_AllocateT(image_type, dst_width, src_height, dst_bpp_s1, 0, 0, 0);
if (!tmp) {
FreeImage_Unload(dst);
return NULL;
}
} else {
// source and destination heights are equal so, we can directly
// scale into destination image (second filter method will not
// be invoked)
tmp = dst;
}
// scale source image horizontally into temporary (or destination) image
horizontalFilter(src, src_height, src_width, src_offset_x, src_offset_y, src_pal, tmp, dst_width);
// set x and y offsets to zero for the second filter method
// invocation (the temporary image only contains the portion of
// the image to be rescaled with no offsets)
src_offset_x = 0;
src_offset_y = 0;
// also ensure, that the second filter method gets no source
// palette (the temporary image is palletized only, if it is
// greyscale; in that case, it is an 8-bit image with a linear
// palette so, the source palette is not needed or will even be
// mismatching, if the source palette is unordered)
src_pal = NULL;
} else {
// source and destination widths are equal so, just copy the
// image pointer
tmp = src;
}
if (src_height != dst_height) {
// source and destination heights are different so, scale
// temporary (or source) image vertically into destination image
verticalFilter(tmp, dst_width, src_height, src_offset_x, src_offset_y, src_pal, dst, dst_height);
}
// free temporary image, if not pointing to either src or dst
if (tmp != src && tmp != dst) {
FreeImage_Unload(tmp);
}
} else {
// yx filtering
// -------------
// Remark:
// The yx filtering branch could be more optimized by taking into,
// account that (src_width != dst_width) is always true, which
// follows from the above condition, which selects filtering order.
// Since (dst_width <= src_width) == TRUE selects xy filtering,
// both widths must be different when performing yx filtering.
// However, to make the code more robust, not depending on that
// condition and more symmetric to the xy filtering case, these
// (src_width != dst_width) conditions are still in place.
FIBITMAP *tmp = NULL;
if (src_height != dst_height) {
// source and destination heights are different so, we must
// filter vertically
if (src_width != dst_width) {
// source and destination widths are also different so, we need
// a temporary image
tmp = FreeImage_AllocateT(image_type, src_width, dst_height, dst_bpp_s1, 0, 0, 0);
if (!tmp) {
FreeImage_Unload(dst);
return NULL;
}
} else {
// source and destination widths are equal so, we can directly
// scale into destination image (second filter method will not
// be invoked)
tmp = dst;
}
// scale source image vertically into temporary (or destination) image
verticalFilter(src, src_width, src_height, src_offset_x, src_offset_y, src_pal, tmp, dst_height);
// set x and y offsets to zero for the second filter method
// invocation (the temporary image only contains the portion of
// the image to be rescaled with no offsets)
src_offset_x = 0;
src_offset_y = 0;
// also ensure, that the second filter method gets no source
// palette (the temporary image is palletized only, if it is
// greyscale; in that case, it is an 8-bit image with a linear
// palette so, the source palette is not needed or will even be
// mismatching, if the source palette is unordered)
src_pal = NULL;
} else {
// source and destination heights are equal so, just copy the
// image pointer
tmp = src;
}
if (src_width != dst_width) {
// source and destination heights are different so, scale
// temporary (or source) image horizontally into destination image
horizontalFilter(tmp, dst_height, src_width, src_offset_x, src_offset_y, src_pal, dst, dst_width);
}
// free temporary image, if not pointing to either src or dst
if (tmp != src && tmp != dst) {
FreeImage_Unload(tmp);
}
}
return dst;
}
void CResizeEngine::horizontalFilter(FIBITMAP *const src, unsigned height, unsigned src_width, unsigned src_offset_x, unsigned src_offset_y, const RGBQUAD *const src_pal, FIBITMAP *const dst, unsigned dst_width) {
// allocate and calculate the contributions
CWeightsTable weightsTable(m_pFilter, dst_width, src_width);
// step through rows
switch(FreeImage_GetImageType(src)) {
case FIT_BITMAP:
{
switch(FreeImage_GetBPP(src)) {
case 1:
{
switch(FreeImage_GetBPP(dst)) {
case 8:
{
// transparently convert the 1-bit non-transparent greyscale image to 8 bpp
src_offset_x >>= 3;
if (src_pal) {
// we have got a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE * const dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double value = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const unsigned pixel = (src_bits[i >> 3] & (0x80 >> (i & 0x07))) != 0;
value += (weightsTable.getWeight(x, i - iLeft) * (double)*(BYTE *)&src_pal[pixel]);
}
// clamp and place result in destination pixel
dst_bits[x] = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
}
}
} else {
// we do not have a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE * const dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double value = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const unsigned pixel = (src_bits[i >> 3] & (0x80 >> (i & 0x07))) != 0;
value += (weightsTable.getWeight(x, i - iLeft) * (double)pixel);
}
value *= 0xFF;
// clamp and place result in destination pixel
dst_bits[x] = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
}
}
}
}
break;
case 24:
{
// transparently convert the non-transparent 1-bit image to 24 bpp
src_offset_x >>= 3;
if (src_pal) {
// we have got a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double r = 0, g = 0, b = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i - iLeft);
const unsigned pixel = (src_bits[i >> 3] & (0x80 >> (i & 0x07))) != 0;
const BYTE * const entry = (BYTE *)&src_pal[pixel];
r += (weight * (double)entry[FI_RGBA_RED]);
g += (weight * (double)entry[FI_RGBA_GREEN]);
b += (weight * (double)entry[FI_RGBA_BLUE]);
}
// clamp and place result in destination pixel
dst_bits[FI_RGBA_RED] = (BYTE)CLAMP<int>((int)(r + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_GREEN] = (BYTE)CLAMP<int>((int)(g + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_BLUE] = (BYTE)CLAMP<int>((int)(b + 0.5), 0, 0xFF);
dst_bits += 3;
}
}
} else {
// we do not have a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double value = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const unsigned pixel = (src_bits[i >> 3] & (0x80 >> (i & 0x07))) != 0;
value += (weightsTable.getWeight(x, i - iLeft) * (double)pixel);
}
value *= 0xFF;
// clamp and place result in destination pixel
const BYTE bval = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_RED] = bval;
dst_bits[FI_RGBA_GREEN] = bval;
dst_bits[FI_RGBA_BLUE] = bval;
dst_bits += 3;
}
}
}
}
break;
case 32:
{
// transparently convert the transparent 1-bit image to 32 bpp;
// we always have got a palette here
src_offset_x >>= 3;
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double r = 0, g = 0, b = 0, a = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i - iLeft);
const unsigned pixel = (src_bits[i >> 3] & (0x80 >> (i & 0x07))) != 0;
const BYTE * const entry = (BYTE *)&src_pal[pixel];
r += (weight * (double)entry[FI_RGBA_RED]);
g += (weight * (double)entry[FI_RGBA_GREEN]);
b += (weight * (double)entry[FI_RGBA_BLUE]);
a += (weight * (double)entry[FI_RGBA_ALPHA]);
}
// clamp and place result in destination pixel
dst_bits[FI_RGBA_RED] = (BYTE)CLAMP<int>((int)(r + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_GREEN] = (BYTE)CLAMP<int>((int)(g + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_BLUE] = (BYTE)CLAMP<int>((int)(b + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_ALPHA] = (BYTE)CLAMP<int>((int)(a + 0.5), 0, 0xFF);
dst_bits += 4;
}
}
}
break;
}
}
break;
case 4:
{
switch(FreeImage_GetBPP(dst)) {
case 8:
{
// transparently convert the non-transparent 4-bit greyscale image to 8 bpp;
// we always have got a palette for 4-bit images
src_offset_x >>= 1;
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE * const dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double value = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const unsigned pixel = i & 0x01 ? src_bits[i >> 1] & 0x0F : src_bits[i >> 1] >> 4;
value += (weightsTable.getWeight(x, i - iLeft) * (double)*(BYTE *)&src_pal[pixel]);
}
// clamp and place result in destination pixel
dst_bits[x] = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
}
}
}
break;
case 24:
{
// transparently convert the non-transparent 4-bit image to 24 bpp;
// we always have got a palette for 4-bit images
src_offset_x >>= 1;
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double r = 0, g = 0, b = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i - iLeft);
const unsigned pixel = i & 0x01 ? src_bits[i >> 1] & 0x0F : src_bits[i >> 1] >> 4;
const BYTE * const entry = (BYTE *)&src_pal[pixel];
r += (weight * (double)entry[FI_RGBA_RED]);
g += (weight * (double)entry[FI_RGBA_GREEN]);
b += (weight * (double)entry[FI_RGBA_BLUE]);
}
// clamp and place result in destination pixel
dst_bits[FI_RGBA_RED] = (BYTE)CLAMP<int>((int)(r + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_GREEN] = (BYTE)CLAMP<int>((int)(g + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_BLUE] = (BYTE)CLAMP<int>((int)(b + 0.5), 0, 0xFF);
dst_bits += 3;
}
}
}
break;
case 32:
{
// transparently convert the transparent 4-bit image to 32 bpp;
// we always have got a palette for 4-bit images
src_offset_x >>= 1;
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iRight = weightsTable.getRightBoundary(x); // retrieve right boundary
double r = 0, g = 0, b = 0, a = 0;
for (unsigned i = iLeft; i < iRight; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i - iLeft);
const unsigned pixel = i & 0x01 ? src_bits[i >> 1] & 0x0F : src_bits[i >> 1] >> 4;
const BYTE * const entry = (BYTE *)&src_pal[pixel];
r += (weight * (double)entry[FI_RGBA_RED]);
g += (weight * (double)entry[FI_RGBA_GREEN]);
b += (weight * (double)entry[FI_RGBA_BLUE]);
a += (weight * (double)entry[FI_RGBA_ALPHA]);
}
// clamp and place result in destination pixel
dst_bits[FI_RGBA_RED] = (BYTE)CLAMP<int>((int)(r + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_GREEN] = (BYTE)CLAMP<int>((int)(g + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_BLUE] = (BYTE)CLAMP<int>((int)(b + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_ALPHA] = (BYTE)CLAMP<int>((int)(a + 0.5), 0, 0xFF);
dst_bits += 4;
}
}
}
break;
}
}
break;
case 8:
{
switch(FreeImage_GetBPP(dst)) {
case 8:
{
// scale the 8-bit non-transparent greyscale image
// into an 8 bpp destination image
if (src_pal) {
// we have got a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE * const dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iLimit = weightsTable.getRightBoundary(x) - iLeft; // retrieve right boundary
const BYTE * const pixel = src_bits + iLeft;
double value = 0;
// for(i = iLeft to iRight)
for (unsigned i = 0; i < iLimit; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
value += (weightsTable.getWeight(x, i) * (double)*(BYTE *)&src_pal[pixel[i]]);
}
// clamp and place result in destination pixel
dst_bits[x] = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
}
}
} else {
// we do not have a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE * const dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iLimit = weightsTable.getRightBoundary(x) - iLeft; // retrieve right boundary
const BYTE * const pixel = src_bits + iLeft;
double value = 0;
// for(i = iLeft to iRight)
for (unsigned i = 0; i < iLimit; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
value += (weightsTable.getWeight(x, i) * (double)pixel[i]);
}
// clamp and place result in destination pixel
dst_bits[x] = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
}
}
}
}
break;
case 24:
{
// transparently convert the non-transparent 8-bit image to 24 bpp
if (src_pal) {
// we have got a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iLimit = weightsTable.getRightBoundary(x) - iLeft; // retrieve right boundary
const BYTE * const pixel = src_bits + iLeft;
double r = 0, g = 0, b = 0;
// for(i = iLeft to iRight)
for (unsigned i = 0; i < iLimit; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i);
const BYTE *const entry = (BYTE *)&src_pal[pixel[i]];
r += (weight * (double)entry[FI_RGBA_RED]);
g += (weight * (double)entry[FI_RGBA_GREEN]);
b += (weight * (double)entry[FI_RGBA_BLUE]);
}
// clamp and place result in destination pixel
dst_bits[FI_RGBA_RED] = (BYTE)CLAMP<int>((int)(r + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_GREEN] = (BYTE)CLAMP<int>((int)(g + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_BLUE] = (BYTE)CLAMP<int>((int)(b + 0.5), 0, 0xFF);
dst_bits += 3;
}
}
} else {
// we do not have a palette
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iLimit = weightsTable.getRightBoundary(x) - iLeft; // retrieve right boundary
const BYTE * const pixel = src_bits + iLeft;
double value = 0;
// for(i = iLeft to iRight)
for (unsigned i = 0; i < iLimit; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i);
value += (weight * (double)pixel[i]);
}
// clamp and place result in destination pixel
const BYTE bval = (BYTE)CLAMP<int>((int)(value + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_RED] = bval;
dst_bits[FI_RGBA_GREEN] = bval;
dst_bits[FI_RGBA_BLUE] = bval;
dst_bits += 3;
}
}
}
}
break;
case 32:
{
// transparently convert the transparent 8-bit image to 32 bpp;
// we always have got a palette here
for (unsigned y = 0; y < height; y++) {
// scale each row
const BYTE * const src_bits = FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x;
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iLimit = weightsTable.getRightBoundary(x) - iLeft; // retrieve right boundary
const BYTE * const pixel = src_bits + iLeft;
double r = 0, g = 0, b = 0, a = 0;
// for(i = iLeft to iRight)
for (unsigned i = 0; i < iLimit; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i);
const BYTE * const entry = (BYTE *)&src_pal[pixel[i]];
r += (weight * (double)entry[FI_RGBA_RED]);
g += (weight * (double)entry[FI_RGBA_GREEN]);
b += (weight * (double)entry[FI_RGBA_BLUE]);
a += (weight * (double)entry[FI_RGBA_ALPHA]);
}
// clamp and place result in destination pixel
dst_bits[FI_RGBA_RED] = (BYTE)CLAMP<int>((int)(r + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_GREEN] = (BYTE)CLAMP<int>((int)(g + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_BLUE] = (BYTE)CLAMP<int>((int)(b + 0.5), 0, 0xFF);
dst_bits[FI_RGBA_ALPHA] = (BYTE)CLAMP<int>((int)(a + 0.5), 0, 0xFF);
dst_bits += 4;
}
}
}
break;
}
}
break;
case 16:
{
// transparently convert the 16-bit non-transparent image to 24 bpp
if (IS_FORMAT_RGB565(src)) {
// image has 565 format
for (unsigned y = 0; y < height; y++) {
// scale each row
const WORD * const src_bits = (WORD *)FreeImage_GetScanLine(src, y + src_offset_y) + src_offset_x / sizeof(WORD);
BYTE *dst_bits = FreeImage_GetScanLine(dst, y);
for (unsigned x = 0; x < dst_width; x++) {
// loop through row
const unsigned iLeft = weightsTable.getLeftBoundary(x); // retrieve left boundary
const unsigned iLimit = weightsTable.getRightBoundary(x) - iLeft; // retrieve right boundary
const WORD *pixel = src_bits + iLeft;
double r = 0, g = 0, b = 0;
// for(i = iLeft to iRight)
for (unsigned i = 0; i < iLimit; i++) {
// scan between boundaries
// accumulate weighted effect of each neighboring pixel
const double weight = weightsTable.getWeight(x, i);
r += (weight * (double)((*pixel & FI16_565_RED_MASK) >> FI16_565_RED_SHIFT));
g += (weight * (double)((*pixel & FI16_565_GREEN_MASK) >> FI16_565_GREEN_SHIFT));
b += (weight * (double)((*pixel & FI16_565_BLUE_MASK) >> FI16_565_BLUE_SHIFT));