This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
ximatran.cpp
2728 lines (2499 loc) · 88.5 KB
/
ximatran.cpp
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
// xImaTran.cpp : Transformation functions
/* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
* CxImage version 7.0.1 07/Jan/2011
*/
#include "ximage.h"
#include "ximath.h"
#if CXIMAGE_SUPPORT_BASICTRANSFORMATIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Increases the number of bits per pixel of the image.
* \param nbit: 4, 8, 24
*/
bool CxImage::IncreaseBpp(uint32_t nbit)
{
if (!pDib) return false;
switch (nbit){
case 4:
{
if (head.biBitCount==4) return true;
if (head.biBitCount>4) return false;
CxImage tmp;
tmp.CopyInfo(*this);
tmp.Create(head.biWidth,head.biHeight,4,info.dwType);
tmp.SetPalette(GetPalette(),GetNumColors());
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
#if CXIMAGE_SUPPORT_SELECTION
tmp.SelectionCopy(*this);
#endif //CXIMAGE_SUPPORT_SELECTION
#if CXIMAGE_SUPPORT_ALPHA
tmp.AlphaCopy(*this);
#endif //CXIMAGE_SUPPORT_ALPHA
for (int32_t y=0;y<head.biHeight;y++){
if (info.nEscape) break;
for (int32_t x=0;x<head.biWidth;x++){
tmp.BlindSetPixelIndex(x,y,BlindGetPixelIndex(x,y));
}
}
Transfer(tmp);
return true;
}
case 8:
{
if (head.biBitCount==8) return true;
if (head.biBitCount>8) return false;
CxImage tmp;
tmp.CopyInfo(*this);
tmp.Create(head.biWidth,head.biHeight,8,info.dwType);
tmp.SetPalette(GetPalette(),GetNumColors());
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
#if CXIMAGE_SUPPORT_SELECTION
tmp.SelectionCopy(*this);
#endif //CXIMAGE_SUPPORT_SELECTION
#if CXIMAGE_SUPPORT_ALPHA
tmp.AlphaCopy(*this);
#endif //CXIMAGE_SUPPORT_ALPHA
for (int32_t y=0;y<head.biHeight;y++){
if (info.nEscape) break;
for (int32_t x=0;x<head.biWidth;x++){
tmp.BlindSetPixelIndex(x,y,BlindGetPixelIndex(x,y));
}
}
Transfer(tmp);
return true;
}
case 24:
{
if (head.biBitCount==24) return true;
if (head.biBitCount>24) return false;
CxImage tmp;
tmp.CopyInfo(*this);
tmp.Create(head.biWidth,head.biHeight,24,info.dwType);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
if (info.nBkgndIndex>=0) //translate transparency
tmp.info.nBkgndColor=GetPaletteColor((uint8_t)info.nBkgndIndex);
#if CXIMAGE_SUPPORT_SELECTION
tmp.SelectionCopy(*this);
#endif //CXIMAGE_SUPPORT_SELECTION
#if CXIMAGE_SUPPORT_ALPHA
tmp.AlphaCopy(*this);
if (AlphaPaletteIsValid() && !AlphaIsValid()) tmp.AlphaCreate();
#endif //CXIMAGE_SUPPORT_ALPHA
for (int32_t y=0;y<head.biHeight;y++){
if (info.nEscape) break;
for (int32_t x=0;x<head.biWidth;x++){
tmp.BlindSetPixelColor(x,y,BlindGetPixelColor(x,y),true);
}
}
Transfer(tmp);
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool CxImage::GrayScale()
{
if (!pDib) return false;
if (head.biBitCount<=8){
RGBQUAD* ppal=GetPalette();
int32_t gray;
//converts the colors to gray, use the blue channel only
for(uint32_t i=0;i<head.biClrUsed;i++){
gray=(int32_t)RGB2GRAY(ppal[i].rgbRed,ppal[i].rgbGreen,ppal[i].rgbBlue);
ppal[i].rgbBlue = (uint8_t)gray;
}
// preserve transparency
if (info.nBkgndIndex >= 0) info.nBkgndIndex = ppal[info.nBkgndIndex].rgbBlue;
//create a "real" 8 bit gray scale image
if (head.biBitCount==8){
uint8_t *img=info.pImage;
for(uint32_t i=0;i<head.biSizeImage;i++) img[i]=ppal[img[i]].rgbBlue;
SetGrayPalette();
}
//transform to 8 bit gray scale
if (head.biBitCount==4 || head.biBitCount==1){
CxImage ima;
ima.CopyInfo(*this);
if (!ima.Create(head.biWidth,head.biHeight,8,info.dwType)) return false;
ima.SetGrayPalette();
#if CXIMAGE_SUPPORT_SELECTION
ima.SelectionCopy(*this);
#endif //CXIMAGE_SUPPORT_SELECTION
#if CXIMAGE_SUPPORT_ALPHA
ima.AlphaCopy(*this);
#endif //CXIMAGE_SUPPORT_ALPHA
for (int32_t y=0;y<head.biHeight;y++){
uint8_t *iDst = ima.GetBits(y);
uint8_t *iSrc = GetBits(y);
for (int32_t x=0;x<head.biWidth; x++){
//iDst[x]=ppal[BlindGetPixelIndex(x,y)].rgbBlue;
if (head.biBitCount==4){
uint8_t pos = (uint8_t)(4*(1-x%2));
iDst[x]= ppal[(uint8_t)((iSrc[x >> 1]&((uint8_t)0x0F<<pos)) >> pos)].rgbBlue;
} else {
uint8_t pos = (uint8_t)(7-x%8);
iDst[x]= ppal[(uint8_t)((iSrc[x >> 3]&((uint8_t)0x01<<pos)) >> pos)].rgbBlue;
}
}
}
Transfer(ima);
}
} else { //from RGB to 8 bit gray scale
uint8_t *iSrc=info.pImage;
CxImage ima;
ima.CopyInfo(*this);
if (!ima.Create(head.biWidth,head.biHeight,8,info.dwType)) return false;
ima.SetGrayPalette();
if (GetTransIndex()>=0){
RGBQUAD c = GetTransColor();
ima.SetTransIndex((uint8_t)RGB2GRAY(c.rgbRed,c.rgbGreen,c.rgbBlue));
}
#if CXIMAGE_SUPPORT_SELECTION
ima.SelectionCopy(*this);
#endif //CXIMAGE_SUPPORT_SELECTION
#if CXIMAGE_SUPPORT_ALPHA
ima.AlphaCopy(*this);
#endif //CXIMAGE_SUPPORT_ALPHA
uint8_t *img=ima.GetBits();
int32_t l8=ima.GetEffWidth();
int32_t l=head.biWidth * 3;
for(int32_t y=0; y < head.biHeight; y++) {
for(int32_t x=0,x8=0; x < l; x+=3,x8++) {
img[x8+y*l8]=(uint8_t)RGB2GRAY(*(iSrc+x+2),*(iSrc+x+1),*(iSrc+x+0));
}
iSrc+=info.dwEffWidth;
}
Transfer(ima);
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* \sa Mirror
* \author [qhbo]
*/
bool CxImage::Flip(bool bFlipSelection, bool bFlipAlpha)
{
if (!pDib) return false;
uint8_t *buff = (uint8_t*)malloc(info.dwEffWidth);
if (!buff) return false;
uint8_t *iSrc,*iDst;
iSrc = GetBits(head.biHeight-1);
iDst = GetBits(0);
for (int32_t i=0; i<(head.biHeight/2); ++i)
{
memcpy(buff, iSrc, info.dwEffWidth);
memcpy(iSrc, iDst, info.dwEffWidth);
memcpy(iDst, buff, info.dwEffWidth);
iSrc-=info.dwEffWidth;
iDst+=info.dwEffWidth;
}
free(buff);
if (bFlipSelection){
#if CXIMAGE_SUPPORT_SELECTION
SelectionFlip();
#endif //CXIMAGE_SUPPORT_SELECTION
}
if (bFlipAlpha){
#if CXIMAGE_SUPPORT_ALPHA
AlphaFlip();
#endif //CXIMAGE_SUPPORT_ALPHA
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* \sa Flip
*/
bool CxImage::Mirror(bool bMirrorSelection, bool bMirrorAlpha)
{
if (!pDib) return false;
CxImage* imatmp = new CxImage(*this,false,true,true);
if (!imatmp) return false;
if (!imatmp->IsValid()){
delete imatmp;
return false;
}
uint8_t *iSrc,*iDst;
int32_t wdt=(head.biWidth-1) * (head.biBitCount==24 ? 3:1);
iSrc=info.pImage + wdt;
iDst=imatmp->info.pImage;
int32_t x,y;
switch (head.biBitCount){
case 24:
for(y=0; y < head.biHeight; y++){
for(x=0; x <= wdt; x+=3){
*(iDst+x)=*(iSrc-x);
*(iDst+x+1)=*(iSrc-x+1);
*(iDst+x+2)=*(iSrc-x+2);
}
iSrc+=info.dwEffWidth;
iDst+=info.dwEffWidth;
}
break;
case 8:
for(y=0; y < head.biHeight; y++){
for(x=0; x <= wdt; x++)
*(iDst+x)=*(iSrc-x);
iSrc+=info.dwEffWidth;
iDst+=info.dwEffWidth;
}
break;
default:
for(y=0; y < head.biHeight; y++){
for(x=0; x <= wdt; x++)
imatmp->SetPixelIndex(x,y,GetPixelIndex(wdt-x,y));
}
}
if (bMirrorSelection){
#if CXIMAGE_SUPPORT_SELECTION
imatmp->SelectionMirror();
#endif //CXIMAGE_SUPPORT_SELECTION
}
if (bMirrorAlpha){
#if CXIMAGE_SUPPORT_ALPHA
imatmp->AlphaMirror();
#endif //CXIMAGE_SUPPORT_ALPHA
}
Transfer(*imatmp);
delete imatmp;
return true;
}
////////////////////////////////////////////////////////////////////////////////
#define RBLOCK 64
////////////////////////////////////////////////////////////////////////////////
bool CxImage::RotateLeft(CxImage* iDst)
{
if (!pDib) return false;
int32_t newWidth = GetHeight();
int32_t newHeight = GetWidth();
CxImage imgDest;
imgDest.CopyInfo(*this);
imgDest.Create(newWidth,newHeight,GetBpp(),GetType());
imgDest.SetPalette(GetPalette());
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) imgDest.AlphaCreate();
#endif
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsValid()) imgDest.SelectionCreate();
#endif
int32_t x,x2,y,dlineup;
// Speedy rotate for BW images <Robert Abram>
if (head.biBitCount == 1) {
uint8_t *sbits, *dbits, *dbitsmax, bitpos, *nrow,*srcdisp;
ldiv_t div_r;
uint8_t *bsrc = GetBits(), *bdest = imgDest.GetBits();
dbitsmax = bdest + imgDest.head.biSizeImage - 1;
dlineup = 8 * imgDest.info.dwEffWidth - imgDest.head.biWidth;
imgDest.Clear(0);
for (y = 0; y < head.biHeight; y++) {
// Figure out the Column we are going to be copying to
div_r = ldiv(y + dlineup, (int32_t)8);
// set bit pos of src column byte
bitpos = (uint8_t)(1 << div_r.rem);
srcdisp = bsrc + y * info.dwEffWidth;
for (x = 0; x < (int32_t)info.dwEffWidth; x++) {
// Get Source Bits
sbits = srcdisp + x;
// Get destination column
nrow = bdest + (x * 8) * imgDest.info.dwEffWidth + imgDest.info.dwEffWidth - 1 - div_r.quot;
for (int32_t z = 0; z < 8; z++) {
// Get Destination Byte
dbits = nrow + z * imgDest.info.dwEffWidth;
if ((dbits < bdest) || (dbits > dbitsmax)) break;
if (*sbits & (128 >> z)) *dbits |= bitpos;
}
}
}//for y
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) {
for (x = 0; x < newWidth; x++){
x2=newWidth-x-1;
for (y = 0; y < newHeight; y++){
imgDest.AlphaSet(x,y,BlindAlphaGet(y, x2));
}//for y
}//for x
}
#endif //CXIMAGE_SUPPORT_ALPHA
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsValid()) {
imgDest.info.rSelectionBox.left = newWidth-info.rSelectionBox.top;
imgDest.info.rSelectionBox.right = newWidth-info.rSelectionBox.bottom;
imgDest.info.rSelectionBox.bottom = info.rSelectionBox.left;
imgDest.info.rSelectionBox.top = info.rSelectionBox.right;
for (x = 0; x < newWidth; x++){
x2=newWidth-x-1;
for (y = 0; y < newHeight; y++){
imgDest.SelectionSet(x,y,BlindSelectionGet(y, x2));
}//for y
}//for x
}
#endif //CXIMAGE_SUPPORT_SELECTION
} else {
//anything other than BW:
//bd, 10. 2004: This optimized version of rotation rotates image by smaller blocks. It is quite
//a bit faster than obvious algorithm, because it produces much less CPU cache misses.
//This optimization can be tuned by changing block size (RBLOCK). 96 is good value for current
//CPUs (tested on Athlon XP and Celeron D). Larger value (if CPU has enough cache) will increase
//speed somehow, but once you drop out of CPU's cache, things will slow down drastically.
//For older CPUs with less cache, lower value would yield better results.
uint8_t *srcPtr, *dstPtr; //source and destionation for 24-bit version
int32_t xs, ys; //x-segment and y-segment
for (xs = 0; xs < newWidth; xs+=RBLOCK) { //for all image blocks of RBLOCK*RBLOCK pixels
for (ys = 0; ys < newHeight; ys+=RBLOCK) {
if (head.biBitCount==24) {
//RGB24 optimized pixel access:
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){ //do rotation
info.nProgress = (int32_t)(100*x/newWidth);
x2=newWidth-x-1;
dstPtr = (uint8_t*) imgDest.BlindGetPixelPointer(x,ys);
srcPtr = (uint8_t*) BlindGetPixelPointer(ys, x2);
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
//imgDest.SetPixelColor(x, y, GetPixelColor(y, x2));
*(dstPtr) = *(srcPtr);
*(dstPtr+1) = *(srcPtr+1);
*(dstPtr+2) = *(srcPtr+2);
srcPtr += 3;
dstPtr += imgDest.info.dwEffWidth;
}//for y
}//for x
} else {
//anything else than 24bpp (and 1bpp): palette
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
info.nProgress = (int32_t)(100*x/newWidth); //<Anatoly Ivasyuk>
x2=newWidth-x-1;
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
imgDest.SetPixelIndex(x, y, BlindGetPixelIndex(y, x2));
}//for y
}//for x
}//if (version selection)
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) {
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
x2=newWidth-x-1;
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
imgDest.AlphaSet(x,y,BlindAlphaGet(y, x2));
}//for y
}//for x
}//if (alpha channel)
#endif //CXIMAGE_SUPPORT_ALPHA
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsValid()) {
imgDest.info.rSelectionBox.left = newWidth-info.rSelectionBox.top;
imgDest.info.rSelectionBox.right = newWidth-info.rSelectionBox.bottom;
imgDest.info.rSelectionBox.bottom = info.rSelectionBox.left;
imgDest.info.rSelectionBox.top = info.rSelectionBox.right;
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
x2=newWidth-x-1;
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
imgDest.SelectionSet(x,y,BlindSelectionGet(y, x2));
}//for y
}//for x
}//if (selection)
#endif //CXIMAGE_SUPPORT_SELECTION
}//for ys
}//for xs
}//if
//select the destination
if (iDst) iDst->Transfer(imgDest);
else Transfer(imgDest);
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CxImage::RotateRight(CxImage* iDst)
{
if (!pDib) return false;
int32_t newWidth = GetHeight();
int32_t newHeight = GetWidth();
CxImage imgDest;
imgDest.CopyInfo(*this);
imgDest.Create(newWidth,newHeight,GetBpp(),GetType());
imgDest.SetPalette(GetPalette());
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) imgDest.AlphaCreate();
#endif
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsValid()) imgDest.SelectionCreate();
#endif
int32_t x,y,y2;
// Speedy rotate for BW images <Robert Abram>
if (head.biBitCount == 1) {
uint8_t *sbits, *dbits, *dbitsmax, bitpos, *nrow,*srcdisp;
ldiv_t div_r;
uint8_t *bsrc = GetBits(), *bdest = imgDest.GetBits();
dbitsmax = bdest + imgDest.head.biSizeImage - 1;
imgDest.Clear(0);
for (y = 0; y < head.biHeight; y++) {
// Figure out the Column we are going to be copying to
div_r = ldiv(y, (int32_t)8);
// set bit pos of src column byte
bitpos = (uint8_t)(128 >> div_r.rem);
srcdisp = bsrc + y * info.dwEffWidth;
for (x = 0; x < (int32_t)info.dwEffWidth; x++) {
// Get Source Bits
sbits = srcdisp + x;
// Get destination column
nrow = bdest + (imgDest.head.biHeight-1-(x*8)) * imgDest.info.dwEffWidth + div_r.quot;
for (int32_t z = 0; z < 8; z++) {
// Get Destination Byte
dbits = nrow - z * imgDest.info.dwEffWidth;
if ((dbits < bdest) || (dbits > dbitsmax)) break;
if (*sbits & (128 >> z)) *dbits |= bitpos;
}
}
}
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()){
for (y = 0; y < newHeight; y++){
y2=newHeight-y-1;
for (x = 0; x < newWidth; x++){
imgDest.AlphaSet(x,y,BlindAlphaGet(y2, x));
}
}
}
#endif //CXIMAGE_SUPPORT_ALPHA
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsValid()){
imgDest.info.rSelectionBox.left = info.rSelectionBox.bottom;
imgDest.info.rSelectionBox.right = info.rSelectionBox.top;
imgDest.info.rSelectionBox.bottom = newHeight-info.rSelectionBox.right;
imgDest.info.rSelectionBox.top = newHeight-info.rSelectionBox.left;
for (y = 0; y < newHeight; y++){
y2=newHeight-y-1;
for (x = 0; x < newWidth; x++){
imgDest.SelectionSet(x,y,BlindSelectionGet(y2, x));
}
}
}
#endif //CXIMAGE_SUPPORT_SELECTION
} else {
//anything else but BW
uint8_t *srcPtr, *dstPtr; //source and destionation for 24-bit version
int32_t xs, ys; //x-segment and y-segment
for (xs = 0; xs < newWidth; xs+=RBLOCK) {
for (ys = 0; ys < newHeight; ys+=RBLOCK) {
if (head.biBitCount==24) {
//RGB24 optimized pixel access:
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
info.nProgress = (int32_t)(100*y/newHeight); //<Anatoly Ivasyuk>
y2=newHeight-y-1;
dstPtr = (uint8_t*) imgDest.BlindGetPixelPointer(xs,y);
srcPtr = (uint8_t*) BlindGetPixelPointer(y2, xs);
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
//imgDest.SetPixelColor(x, y, GetPixelColor(y2, x));
*(dstPtr) = *(srcPtr);
*(dstPtr+1) = *(srcPtr+1);
*(dstPtr+2) = *(srcPtr+2);
dstPtr += 3;
srcPtr += info.dwEffWidth;
}//for x
}//for y
} else {
//anything else than BW & RGB24: palette
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
info.nProgress = (int32_t)(100*y/newHeight); //<Anatoly Ivasyuk>
y2=newHeight-y-1;
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
imgDest.SetPixelIndex(x, y, BlindGetPixelIndex(y2, x));
}//for x
}//for y
}//if
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()){
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
y2=newHeight-y-1;
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
imgDest.AlphaSet(x,y,BlindAlphaGet(y2, x));
}//for x
}//for y
}//if (has alpha)
#endif //CXIMAGE_SUPPORT_ALPHA
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsValid()){
imgDest.info.rSelectionBox.left = info.rSelectionBox.bottom;
imgDest.info.rSelectionBox.right = info.rSelectionBox.top;
imgDest.info.rSelectionBox.bottom = newHeight-info.rSelectionBox.right;
imgDest.info.rSelectionBox.top = newHeight-info.rSelectionBox.left;
for (y = ys; y < min(newHeight, ys+RBLOCK); y++){
y2=newHeight-y-1;
for (x = xs; x < min(newWidth, xs+RBLOCK); x++){
imgDest.SelectionSet(x,y,BlindSelectionGet(y2, x));
}//for x
}//for y
}//if (has alpha)
#endif //CXIMAGE_SUPPORT_SELECTION
}//for ys
}//for xs
}//if
//select the destination
if (iDst) iDst->Transfer(imgDest);
else Transfer(imgDest);
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CxImage::Negative()
{
if (!pDib) return false;
if (head.biBitCount<=8){
if (IsGrayScale()){ //GRAYSCALE, selection
if (pSelection){
for(int32_t y=info.rSelectionBox.bottom; y<info.rSelectionBox.top; y++){
for(int32_t x=info.rSelectionBox.left; x<info.rSelectionBox.right; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
BlindSetPixelIndex(x,y,(uint8_t)(255-BlindGetPixelIndex(x,y)));
}
}
}
} else {
uint8_t *iSrc=info.pImage;
for(uint32_t i=0; i < head.biSizeImage; i++){
*iSrc=(uint8_t)~(*(iSrc));
iSrc++;
}
}
} else { //PALETTE, full image
RGBQUAD* ppal=GetPalette();
for(uint32_t i=0;i<head.biClrUsed;i++){
ppal[i].rgbBlue =(uint8_t)(255-ppal[i].rgbBlue);
ppal[i].rgbGreen =(uint8_t)(255-ppal[i].rgbGreen);
ppal[i].rgbRed =(uint8_t)(255-ppal[i].rgbRed);
}
}
} else {
if (pSelection==NULL){ //RGB, full image
uint8_t *iSrc=info.pImage;
for(uint32_t i=0; i < head.biSizeImage; i++){
*iSrc=(uint8_t)~(*(iSrc));
iSrc++;
}
} else { // RGB with selection
RGBQUAD color;
for(int32_t y=info.rSelectionBox.bottom; y<info.rSelectionBox.top; y++){
for(int32_t x=info.rSelectionBox.left; x<info.rSelectionBox.right; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
color = BlindGetPixelColor(x,y);
color.rgbRed = (uint8_t)(255-color.rgbRed);
color.rgbGreen = (uint8_t)(255-color.rgbGreen);
color.rgbBlue = (uint8_t)(255-color.rgbBlue);
BlindSetPixelColor(x,y,color);
}
}
}
}
//<DP> invert transparent color too
info.nBkgndColor.rgbBlue = (uint8_t)(255-info.nBkgndColor.rgbBlue);
info.nBkgndColor.rgbGreen = (uint8_t)(255-info.nBkgndColor.rgbGreen);
info.nBkgndColor.rgbRed = (uint8_t)(255-info.nBkgndColor.rgbRed);
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
#endif //CXIMAGE_SUPPORT_BASICTRANSFORMATIONS
////////////////////////////////////////////////////////////////////////////////
#if CXIMAGE_SUPPORT_TRANSFORMATION
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if CXIMAGE_SUPPORT_EXIF
bool CxImage::RotateExif(int32_t orientation /* = 0 */)
{
bool ret = true;
if (orientation <= 0)
orientation = info.ExifInfo.Orientation;
if (orientation == 3)
ret = Rotate180();
else if (orientation == 6)
ret = RotateRight();
else if (orientation == 8)
ret = RotateLeft();
else if (orientation == 5)
ret = RotateLeft();
info.ExifInfo.Orientation = 1;
return ret;
}
#endif //CXIMAGE_SUPPORT_EXIF
////////////////////////////////////////////////////////////////////////////////
bool CxImage::Rotate(float angle, CxImage* iDst)
{
if (!pDib) return false;
if (fmod(angle,180.0f)==0.0f && fmod(angle,360.0f)!=0.0f)
return Rotate180(iDst);
// Copyright (c) 1996-1998 Ulrich von Zadow
// Negative the angle, because the y-axis is negative.
double ang = -angle*acos((float)0)/90;
int32_t newWidth, newHeight;
int32_t nWidth = GetWidth();
int32_t nHeight= GetHeight();
double cos_angle = cos(ang);
double sin_angle = sin(ang);
// Calculate the size of the new bitmap
POINT p1={0,0};
POINT p2={nWidth,0};
POINT p3={0,nHeight};
POINT p4={nWidth,nHeight};
CxPoint2 newP1,newP2,newP3,newP4, leftTop, rightTop, leftBottom, rightBottom;
newP1.x = (float)p1.x;
newP1.y = (float)p1.y;
newP2.x = (float)(p2.x*cos_angle - p2.y*sin_angle);
newP2.y = (float)(p2.x*sin_angle + p2.y*cos_angle);
newP3.x = (float)(p3.x*cos_angle - p3.y*sin_angle);
newP3.y = (float)(p3.x*sin_angle + p3.y*cos_angle);
newP4.x = (float)(p4.x*cos_angle - p4.y*sin_angle);
newP4.y = (float)(p4.x*sin_angle + p4.y*cos_angle);
leftTop.x = min(min(newP1.x,newP2.x),min(newP3.x,newP4.x));
leftTop.y = min(min(newP1.y,newP2.y),min(newP3.y,newP4.y));
rightBottom.x = max(max(newP1.x,newP2.x),max(newP3.x,newP4.x));
rightBottom.y = max(max(newP1.y,newP2.y),max(newP3.y,newP4.y));
leftBottom.x = leftTop.x;
leftBottom.y = rightBottom.y;
rightTop.x = rightBottom.x;
rightTop.y = leftTop.y;
newWidth = (int32_t) floor(0.5f + rightTop.x - leftTop.x);
newHeight= (int32_t) floor(0.5f + leftBottom.y - leftTop.y);
CxImage imgDest;
imgDest.CopyInfo(*this);
imgDest.Create(newWidth,newHeight,GetBpp(),GetType());
imgDest.SetPalette(GetPalette());
#if CXIMAGE_SUPPORT_ALPHA
if(AlphaIsValid()) //MTA: Fix for rotation problem when the image has an alpha channel
{
imgDest.AlphaCreate();
imgDest.AlphaClear();
}
#endif //CXIMAGE_SUPPORT_ALPHA
int32_t x,y,newX,newY,oldX,oldY;
if (head.biClrUsed==0){ //RGB
for (y = (int32_t)leftTop.y, newY = 0; y<=(int32_t)leftBottom.y; y++,newY++){
info.nProgress = (int32_t)(100*newY/newHeight);
if (info.nEscape) break;
for (x = (int32_t)leftTop.x, newX = 0; x<=(int32_t)rightTop.x; x++,newX++){
oldX = (int32_t)(x*cos_angle + y*sin_angle + 0.5);
oldY = (int32_t)(y*cos_angle - x*sin_angle + 0.5);
imgDest.SetPixelColor(newX,newY,GetPixelColor(oldX,oldY));
#if CXIMAGE_SUPPORT_ALPHA
imgDest.AlphaSet(newX,newY,AlphaGet(oldX,oldY)); //MTA: copy the alpha value
#endif //CXIMAGE_SUPPORT_ALPHA
}
}
} else { //PALETTE
for (y = (int32_t)leftTop.y, newY = 0; y<=(int32_t)leftBottom.y; y++,newY++){
info.nProgress = (int32_t)(100*newY/newHeight);
if (info.nEscape) break;
for (x = (int32_t)leftTop.x, newX = 0; x<=(int32_t)rightTop.x; x++,newX++){
oldX = (int32_t)(x*cos_angle + y*sin_angle + 0.5);
oldY = (int32_t)(y*cos_angle - x*sin_angle + 0.5);
imgDest.SetPixelIndex(newX,newY,GetPixelIndex(oldX,oldY));
#if CXIMAGE_SUPPORT_ALPHA
imgDest.AlphaSet(newX,newY,AlphaGet(oldX,oldY)); //MTA: copy the alpha value
#endif //CXIMAGE_SUPPORT_ALPHA
}
}
}
//select the destination
if (iDst) iDst->Transfer(imgDest);
else Transfer(imgDest);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Rotates image around it's center.
* Method can use interpolation with paletted images, but does not change pallete, so results vary.
* (If you have only four colours in a palette, there's not much room for interpolation.)
*
* \param angle - angle in degrees (positive values rotate clockwise)
* \param *iDst - destination image (if null, this image is changed)
* \param inMethod - interpolation method used
* (IM_NEAREST_NEIGHBOUR produces aliasing (fast), IM_BILINEAR softens picture a bit (slower)
* IM_SHARPBICUBIC is slower and produces some halos...)
* \param ofMethod - overflow method (how to choose colour of pixels that have no source)
* \param replColor - replacement colour to use (OM_COLOR, OM_BACKGROUND with no background colour...)
* \param optimizeRightAngles - call faster methods for 90, 180, and 270 degree rotations. Faster methods
* are called for angles, where error (in location of corner pixels) is less
* than 0.25 pixels.
* \param bKeepOriginalSize - rotates the image without resizing.
*
* \author ***bd*** 2.2004
*/
bool CxImage::Rotate2(float angle,
CxImage *iDst,
InterpolationMethod inMethod,
OverflowMethod ofMethod,
RGBQUAD *replColor,
bool const optimizeRightAngles,
bool const bKeepOriginalSize)
{
if (!pDib) return false; //no dib no go
if (fmod(angle,180.0f)==0.0f && fmod(angle,360.0f)!=0.0f)
return Rotate180(iDst);
double ang = -angle*acos(0.0f)/90.0f; //convert angle to radians and invert (positive angle performs clockwise rotation)
float cos_angle = (float) cos(ang); //these two are needed later (to rotate)
float sin_angle = (float) sin(ang);
//Calculate the size of the new bitmap (rotate corners of image)
CxPoint2 p[4]; //original corners of the image
p[0]=CxPoint2(-0.5f,-0.5f);
p[1]=CxPoint2(GetWidth()-0.5f,-0.5f);
p[2]=CxPoint2(-0.5f,GetHeight()-0.5f);
p[3]=CxPoint2(GetWidth()-0.5f,GetHeight()-0.5f);
CxPoint2 newp[4]; //rotated positions of corners
//(rotate corners)
if (bKeepOriginalSize){
for (int32_t i=0; i<4; i++) {
newp[i].x = p[i].x;
newp[i].y = p[i].y;
}//for
} else {
for (int32_t i=0; i<4; i++) {
newp[i].x = (p[i].x*cos_angle - p[i].y*sin_angle);
newp[i].y = (p[i].x*sin_angle + p[i].y*cos_angle);
}//for i
if (optimizeRightAngles) {
//For rotations of 90, -90 or 180 or 0 degrees, call faster routines
if (newp[3].Distance(CxPoint2(GetHeight()-0.5f, 0.5f-GetWidth())) < 0.25)
//rotation right for circa 90 degrees (diagonal pixels less than 0.25 pixel away from 90 degree rotation destination)
return RotateRight(iDst);
if (newp[3].Distance(CxPoint2(0.5f-GetHeight(), -0.5f+GetWidth())) < 0.25)
//rotation left for ~90 degrees
return RotateLeft(iDst);
if (newp[3].Distance(CxPoint2(0.5f-GetWidth(), 0.5f-GetHeight())) < 0.25)
//rotation left for ~180 degrees
return Rotate180(iDst);
if (newp[3].Distance(p[3]) < 0.25) {
//rotation not significant
if (iDst) iDst->Copy(*this); //copy image to iDst, if required
return true; //and we're done
}//if
}//if
}//if
//(read new dimensions from location of corners)
float minx = (float) min(min(newp[0].x,newp[1].x),min(newp[2].x,newp[3].x));
float miny = (float) min(min(newp[0].y,newp[1].y),min(newp[2].y,newp[3].y));
float maxx = (float) max(max(newp[0].x,newp[1].x),max(newp[2].x,newp[3].x));
float maxy = (float) max(max(newp[0].y,newp[1].y),max(newp[2].y,newp[3].y));
int32_t newWidth = (int32_t) floor(maxx-minx+0.5f);
int32_t newHeight= (int32_t) floor(maxy-miny+0.5f);
float ssx=((maxx+minx)- ((float) newWidth-1))/2.0f; //start for x
float ssy=((maxy+miny)- ((float) newHeight-1))/2.0f; //start for y
float newxcenteroffset = 0.5f * newWidth;
float newycenteroffset = 0.5f * newHeight;
if (bKeepOriginalSize){
ssx -= 0.5f * GetWidth();
ssy -= 0.5f * GetHeight();
}
//create destination image
CxImage imgDest;
imgDest.CopyInfo(*this);
imgDest.Create(newWidth,newHeight,GetBpp(),GetType());
imgDest.SetPalette(GetPalette());
#if CXIMAGE_SUPPORT_ALPHA
if(AlphaIsValid()) imgDest.AlphaCreate(); //MTA: Fix for rotation problem when the image has an alpha channel
#endif //CXIMAGE_SUPPORT_ALPHA
RGBQUAD rgb; //pixel colour
RGBQUAD rc;
if (replColor!=0)
rc=*replColor;
else {
rc.rgbRed=255; rc.rgbGreen=255; rc.rgbBlue=255; rc.rgbReserved=0;
}//if
float x,y; //destination location (float, with proper offset)
float origx, origy; //origin location
int32_t destx, desty; //destination location
y=ssy; //initialize y
if (!IsIndexed()){ //RGB24
//optimized RGB24 implementation (direct write to destination):
uint8_t *pxptr;
#if CXIMAGE_SUPPORT_ALPHA
uint8_t *pxptra=0;
#endif //CXIMAGE_SUPPORT_ALPHA
for (desty=0; desty<newHeight; desty++) {
info.nProgress = (int32_t)(100*desty/newHeight);
if (info.nEscape) break;
//initialize x
x=ssx;
//calculate pointer to first byte in row
pxptr=(uint8_t *)imgDest.BlindGetPixelPointer(0, desty);
#if CXIMAGE_SUPPORT_ALPHA
//calculate pointer to first byte in row
if (AlphaIsValid()) pxptra=imgDest.AlphaGetPointer(0, desty);
#endif //CXIMAGE_SUPPORT_ALPHA
for (destx=0; destx<newWidth; destx++) {
//get source pixel coordinate for current destination point
//origx = (cos_angle*(x-head.biWidth/2)+sin_angle*(y-head.biHeight/2))+newWidth/2;
//origy = (cos_angle*(y-head.biHeight/2)-sin_angle*(x-head.biWidth/2))+newHeight/2;
origx = cos_angle*x+sin_angle*y;
origy = cos_angle*y-sin_angle*x;
if (bKeepOriginalSize){
origx += newxcenteroffset;
origy += newycenteroffset;
}
rgb = GetPixelColorInterpolated(origx, origy, inMethod, ofMethod, &rc); //get interpolated colour value
//copy alpha and colour value to destination
#if CXIMAGE_SUPPORT_ALPHA
if (pxptra) *pxptra++ = rgb.rgbReserved;
#endif //CXIMAGE_SUPPORT_ALPHA
*pxptr++ = rgb.rgbBlue;
*pxptr++ = rgb.rgbGreen;
*pxptr++ = rgb.rgbRed;
x++;
}//for destx
y++;
}//for desty
} else {
//non-optimized implementation for paletted images
for (desty=0; desty<newHeight; desty++) {
info.nProgress = (int32_t)(100*desty/newHeight);
if (info.nEscape) break;
x=ssx;
for (destx=0; destx<newWidth; destx++) {
//get source pixel coordinate for current destination point
origx=(cos_angle*x+sin_angle*y);
origy=(cos_angle*y-sin_angle*x);
if (bKeepOriginalSize){
origx += newxcenteroffset;
origy += newycenteroffset;
}
rgb = GetPixelColorInterpolated(origx, origy, inMethod, ofMethod, &rc);
//***!*** SetPixelColor is slow for palleted images
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid())
imgDest.SetPixelColor(destx,desty,rgb,true);
else
#endif //CXIMAGE_SUPPORT_ALPHA
imgDest.SetPixelColor(destx,desty,rgb,false);
x++;
}//for destx
y++;
}//for desty
}
//select the destination
if (iDst) iDst->Transfer(imgDest);
else Transfer(imgDest);
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CxImage::Rotate180(CxImage* iDst)
{
if (!pDib) return false;
int32_t wid = GetWidth();
int32_t ht = GetHeight();
CxImage imgDest;
imgDest.CopyInfo(*this);
imgDest.Create(wid,ht,GetBpp(),GetType());
imgDest.SetPalette(GetPalette());
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) imgDest.AlphaCreate();
#endif //CXIMAGE_SUPPORT_ALPHA
int32_t x,y,y2;
for (y = 0; y < ht; y++){
info.nProgress = (int32_t)(100*y/ht); //<Anatoly Ivasyuk>
y2=ht-y-1;
for (x = 0; x < wid; x++){
if(head.biClrUsed==0)//RGB
imgDest.SetPixelColor(wid-x-1, y2, BlindGetPixelColor(x, y));
else //PALETTE
imgDest.SetPixelIndex(wid-x-1, y2, BlindGetPixelIndex(x, y));
#if CXIMAGE_SUPPORT_ALPHA