-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGT_Image.cpp
1301 lines (1013 loc) · 31.4 KB
/
GT_Image.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
// Done by TheTutor -- 2002 (Updated Last: 4/27/04)
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include "GT_Image.h"
#include "GT_Util.h"
// Load needed lib
#pragma comment(lib, "msimg32.lib")
#pragma pack(push, 1)
struct STGAHeader
{
char idFieldLength; // 1 - 255 bytes of optional identification before pixel bits (usually 0)
char colorMapType;
char dataType; // Type of TGA file -- For us always TGA_RGB
short colorMapOrigin;
short colorMapLength;
char colorMapEntrySize;
short x; // x pos of image origin
short y; // y pos of image origin
short width;
short height;
char bitsPerPixel;
char descriptor;
};
#pragma pack(pop)
// Windows bitmaps are in BGR format instead of RGB.
// These defines will help let us index into a "color" more clearly
enum {
eBlue = 0,
eGreen = 1,
eRed = 2,
eAlpha = 3,
};
const int kMaxARGB = 255; // Maximum ARGB value
// Returns true if (R1,G1,B1) == (R2,G2,B2)
inline bool SameRGB(uchar R1, uchar G1, uchar B1, uchar R2, uchar G2, uchar B2)
{
return ((R1 == R2) && (G1 == G2) && (B1 == B2));
}
// JHACK -- Only clips on the X, CLAMPS on the Y
inline void Clip(int &dx, int &dy, int dw, int dh, int &sx, int &sy, int &sw, int &sh)
{
if(dx < 0)
{
sx = -dx;
dx = 0;
}
if(dy < 0)
{
//sy = -dy;
dy = 0;
}
if(dx + sw > dw)
{
sw -= ((dx + sw) - dw);
}
if(dy + sh > dh)
{
//sh -= ((dy + sh) - dh);
dy = dh - sh;
}
}
// Constructor
CImage::CImage()
{
zeroVars();
GdiFlush(); // Make sure that writing to CImage is okay
}
// Copy Constructor
CImage::CImage(const CImage &image)
{
zeroVars();
GdiFlush(); // Make sure that writing to CImage is okay
*this = image;
}
// Assignment Operator
CImage& CImage::operator =(const CImage &image)
{
// Check for assigning to self
if(this == &image) return *this;
// Set the size of the image (allocate space for the image)
init(image.getWidth(), image.getHeight(), image.getChannels());
// Copy over pixel data
memcpy(mPixels, image.mPixels, image.getStride() * image.getHeight());
return *this;
}
// Returns true for success -- false otherwise
bool CImage::init(int wid, int hgt, int chn)
{
// Error Check -- We only support 24 and 32-bit bitmaps
if(!(chn == 3 || chn == 4))
return false;
// If CImage has already been set -- clear it out first
freeMem();
// Create a compatible HDC
mHDC = CreateCompatibleDC(NULL);
// Error Check
if(!mHDC)
return false;
mWidth = wid;
mHeight = hgt;
mChannels = chn;
// Set stride
mStride = mWidth * mChannels;
while((mStride % 4) != 0) // Ensure stride is DWORD aligned
mStride++;
BITMAPINFO info = {0};
// Initialize the parameters that we care about
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = mWidth;
info.bmiHeader.biHeight = mHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = mChannels * 8;
info.bmiHeader.biCompression = BI_RGB;
// Create a DIBSection. Note that this returns us two
// things - an HBITMAP handle and a memory pointer, in
// pixels.
mBmp = CreateDIBSection(mHDC, &info, DIB_RGB_COLORS,
(void**)&mPixels, 0, 0);
// Error Check
if(!mBmp)
return false;
// Select our DIBSection bitmap into our DC.
// This allows us to draw to the bitmap using GDI functions.
mOldBmp = (HBITMAP)SelectObject(mHDC,mBmp);
// Error Check
if (!mOldBmp)
return false;
return true;
} // end of init(int width, int height, int channels)
// Loads a Win32 resource
bool CImage::loadWin32Resource(int resourceID)
{
HBITMAP hbitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(resourceID),
IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_CREATEDIBSECTION);
// Error check
if(!hbitmap)
return false;
HDC hdc = CreateCompatibleDC(NULL);
// Error check
if(!hdc)
return false;
HBITMAP oldBmp = (HBITMAP)SelectObject(hdc, hbitmap);
// Error check
if(!oldBmp)
return false;
BITMAP info = {0};
// Get image's properties
if(GetObject(hbitmap, sizeof(BITMAP), &info) != sizeof(BITMAP))
return false;
// Set the size of the CImage
if(!init(info.bmWidth, info.bmHeight, info.bmBitsPixel / 8))
return false;
// Copy over pixel data
BitBlt(mHDC, 0, 0, getWidth(), getHeight(), hdc, 0, 0, SRCCOPY);
// Free up resource
SelectObject(hdc, oldBmp);
DeleteObject(hbitmap);
DeleteDC(hdc);
return true; // Load was successful
}
bool CImage::loadBMP(const char *file_name)
{
// Error Check -- Make sure they passed in a valid file name
if(!file_name)
return false;
FILE *file = fopen(file_name, "rb");
// Error Check -- Make sure the file could be opened
if(!file)
return false;
try
{
BITMAPFILEHEADER fileheader;
// Read the BITMAPFILEHEADER
if(!fread(&fileheader, sizeof(BITMAPFILEHEADER), 1, file))
throw GT_IMAGE_ERROR_LOADING;
// Check the type field to make sure we have a .bmp file
if(memcmp(&fileheader.bfType, "BM", 2))
throw GT_IMAGE_ERROR_LOADING;
BITMAPINFOHEADER infoheader;
// Read the BITMAPINFOHEADER.
if(!fread(&infoheader, sizeof(BITMAPINFOHEADER), 1, file))
throw GT_IMAGE_ERROR_LOADING;
// Double Check to make sure we got a correct BITMAPINFOHEADER
if(infoheader.biSize != sizeof(infoheader))
throw GT_IMAGE_ERROR_LOADING;
// Check for unsupported format
if(infoheader.biPlanes != 1)
throw GT_IMAGE_ERROR_LOADING;
// One more double check that we have a .bmp we can handle
if(infoheader.biCompression != BI_RGB)
throw GT_IMAGE_ERROR_LOADING;
if(init(infoheader.biWidth,infoheader.biHeight,infoheader.biBitCount / 8) == false)
throw GT_IMAGE_ERROR_LOADING;
// Jump to the location where the bitmap data is stored
// fseek() returns 0 on success
if(fseek(file, fileheader.bfOffBits, SEEK_SET))
throw GT_IMAGE_ERROR_LOADING;
{ // Now read in the pixel bits
unsigned int bytesUsed = mWidth * mChannels;
unsigned int padding = mStride - bytesUsed;
for(int y = 0; y < mHeight; ++y)
{
// Get the current line
uchar *line_ptr = getLinePtr(y);
// Read the precise number of bytes that the line requires into the bitmap.
// Don't read the padding bytes, because the in memory alignment requirements
// may vary
if(!fread(line_ptr, bytesUsed, 1, file))
throw GT_IMAGE_ERROR_LOADING;
// Skip over any padding bytes.
if(fseek(file, padding, SEEK_CUR))
throw GT_IMAGE_ERROR_LOADING;
} // end of for(int y = 0; y < height; ++y)
}
}
catch(int error)
{
fclose(file);
throw error;
return false;
}
fclose(file);
return true; // Load was successful
} // end of loadBMP(char *file_name)
bool CImage::loadTGA(const char *file_name)
{
// Make sure we have a legal file name
if(!file_name)
return false;
FILE *file = fopen(file_name,"rb");
// Error Check
if(!file)
return false;
try
{
STGAHeader header = {0};
if(!fread(&header, sizeof(STGAHeader), 1, file))
throw GT_IMAGE_ERROR_LOADING;
if(!init(header.width,header.height,header.bitsPerPixel / 8))
throw GT_IMAGE_ERROR_LOADING;
if(header.idFieldLength > 0)
fseek(file,header.idFieldLength,SEEK_CUR); // Seek past optional image description
{ // Now load in the pixel bits
int bytesUsed = mWidth * mChannels;
// Load in all the pixel data
for(int y = 0; y < mHeight; ++y)
{
// Get the current line
uchar *line_ptr = getLinePtr(y);
if(!fread(line_ptr,bytesUsed,1,file))
throw GT_IMAGE_ERROR_LOADING;
}
}
}
catch(int error)
{
fclose(file);
throw error;
return false;
}
fclose(file);
return true; // Load was successful
} // end of loadTGA(char *file_name)
bool CImage::saveAsBMP(const char *fileName)
{
// Error Check -- Make sure they passed in a valid file name
if(!fileName)
return false;
char *saveFileName = NULL;
// Allocate string long enough for "fileName" + ".bmp" + a terminating NULL
saveFileName = new char[strlen(fileName) + 5];
// Error check
if(!saveFileName)
return false;
// Copy over "fileName"
strcpy(saveFileName, fileName);
// See if ".bmp" is already in string, if not append it
if(!strstr(saveFileName, ".bmp"))
strcat(saveFileName, ".bmp"); // Add on the ".bmp"
FILE *file = fopen(saveFileName, "wb");
// Error Check -- Make sure the file could be opened for writing
if(!file)
return false;
// We can free up the temporary file name
SAFE_DELETE_ARRAY(saveFileName);
try
{
BITMAPFILEHEADER fileheader = {0};
// Fill in the BITMAPFILEHEADER
fileheader.bfType = 19778; // 19778 == "BM"
fileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fileheader.bfSize = fileheader.bfOffBits + (getHeight() * getStride()); // The size of the file in bytes
// Write the BITMAPFILEHEADER
if(!fwrite(&fileheader, sizeof(BITMAPFILEHEADER), 1, file))
throw GT_IMAGE_ERROR_SAVING;
BITMAPINFOHEADER infoheader = {0};
// Fill in the BITMAPINFOHEADER
infoheader.biSize = sizeof(BITMAPINFOHEADER);
infoheader.biPlanes = 1;
infoheader.biCompression = BI_RGB;
infoheader.biWidth = getWidth();
infoheader.biHeight = getHeight();
infoheader.biBitCount = getChannels() * 8;
infoheader.biXPelsPerMeter = 2834; // Resolution of 72 pixels per inch
infoheader.biYPelsPerMeter = 2834; // Resolution of 72 pixels per inch
// Write the BITMAPINFOHEADER
if(!fwrite(&infoheader, sizeof(BITMAPINFOHEADER), 1, file))
throw GT_IMAGE_ERROR_SAVING;
for(int y = 0; y < mHeight; ++y)
{
// Get the current line
uchar *linePtr = getLinePtr(y);
// Write out the bytes
if(!fwrite(linePtr, getStride(), 1, file))
throw GT_IMAGE_ERROR_SAVING;
} // end of for(int y = 0; y < height; ++y)
}
catch(int error)
{
fclose(file);
throw error;
return false;
}
fclose(file);
return true; // Load was successful
}
bool CImage::saveAsTGA(const char *fileName)
{
// Error Check -- Make sure they passed in a valid file name
if(!fileName)
return false;
char *saveFileName = NULL;
// Allocate string long enough for "fileName" + ".tga" + a terminating NULL
saveFileName = new char[strlen(fileName) + 5];
// Error check
if(!saveFileName)
return false;
// Copy over "fileName"
strcpy(saveFileName, fileName);
// See if ".bmp" is already in string, if not append it
if(!strstr(saveFileName, ".tga"))
strcat(saveFileName, ".tga"); // Add on the ".tga"
FILE *file = fopen(saveFileName, "wb");
// Error Check -- Make sure the file could be opened for writing
if(!file)
return false;
// We can free up the temporary file name
SAFE_DELETE_ARRAY(saveFileName);
try
{
STGAHeader fileheader = {0};
fileheader.dataType = TGA_RGB;
fileheader.bitsPerPixel = getChannels() * 8;
fileheader.width = getWidth();
fileheader.height = getHeight();
fileheader.descriptor = (getChannels() == 4) ? 8 : 0;
// Write the header
if(!fwrite(&fileheader, sizeof(STGAHeader), 1, file))
throw GT_IMAGE_ERROR_SAVING;
for(int y = 0; y < mHeight; ++y)
{
// Get the current line
uchar *linePtr = getLinePtr(y);
// Write out the bytes
if(!fwrite(linePtr, getStride(), 1, file))
throw GT_IMAGE_ERROR_SAVING;
} // end of for(int y = 0; y < height; ++y)
}
catch(int error)
{
fclose(file);
throw error;
return false;
}
fclose(file);
return true; // Load was successful
}
// Set pixel at (x,y) to (R,G,B)
void CImage::setPixel(int x, int y, uchar R, uchar G, uchar B)
{
assert((x >= 0) && (x < mWidth));
assert((y >= 0) && (y < mHeight));
uchar *destLine = getLinePtr(mHeight - y - 1); // Flip for Win32 coordinates
destLine += (x * mChannels);
destLine[eRed] = R;
destLine[eGreen] = G;
destLine[eBlue] = B;
}
// Set pixel at (x,y) to (R,G,B,A)
// If image is 24-bit the A component will be skipped
void CImage::setPixel(int x, int y, int argb)
{
assert((x >= 0) && (x < mWidth));
assert((y >= 0) && (y < mHeight));
uchar *destLine = getLinePtr(mHeight - y - 1); // Flip for Win32 coordinates
destLine += (x * mChannels);
destLine[eRed] = GET_R(argb);
destLine[eGreen] = GET_G(argb);
destLine[eBlue] = GET_B(argb);
if(mChannels == 4)
destLine[eAlpha] = GET_A(argb);
}
// Set CImage to color passed in
void CImage::setToColor(uchar red, uchar green, uchar blue)
{
int x = 0, y = 1; // Loop variables
uchar *line_ptr = getLinePtr(0); // Get pointer to first line of pixels
for(x = 0; x < mWidth; ++x)
{
line_ptr[eRed] = red;
line_ptr[eGreen] = green;
line_ptr[eBlue] = blue;
line_ptr += mChannels;
}
line_ptr = getLinePtr(0); // Move back to start of line 0
for(; y < mHeight; ++y)
memcpy(getLinePtr(y), line_ptr, mStride);
}
// Set CImage to color passed in
void CImage::setToColor(int argb)
{
int x = 0, y = 1; // Loop variables
uchar *line_ptr = getLinePtr(0); // Pointer to the first line of pixels
// If it's a 32-bit image
if(mChannels == 4)
{
// Set first line of pixels
for(x = 0; x < mWidth; ++x)
{
memcpy(line_ptr, &argb, 4);
line_ptr += mChannels;
}
}
else
{
uchar red = GET_R(argb);
uchar green = GET_G(argb);
uchar blue = GET_B(argb);
// Set the first line to the color passed in
for(x = 0; x < mWidth; ++x)
{
*line_ptr++ = blue;
*line_ptr++ = green;
*line_ptr++ = red;
}
}
line_ptr = getLinePtr(0); // Move back to start of line 0
// Copy first line to rest of image
for(; y < mHeight; ++y)
memcpy(getLinePtr(y), line_ptr, mStride);
}
// Fill the rect area with the color passed in
void CImage::fillRect(const RECT &rect, uchar R, uchar G, uchar B)
{
assert(rect.left >= 0 && rect.right <= mWidth);
assert(rect.top >= 0 && rect.bottom <= mHeight);
int x; // for the x loops
uchar *line_ptr;
for(int y = rect.top; y < rect.bottom; ++y)
{
line_ptr = getLinePtr(mHeight - y - 1); // Flip for Win32 coordinates
line_ptr += (rect.left * mChannels);
for(x = rect.left; x < rect.right; ++x)
{
*line_ptr++ = B;
*line_ptr++ = G;
*line_ptr++ = R;
}
}
} // end of fillRect(const RECT &rect, uchar R, uchar G, uchar B)
// Fill the rect area with the color passed in
void CImage::fillRect(const RECT &rect, int argb)
{
assert(rect.left >= 0 && rect.right <= mWidth);
assert(rect.top >= 0 && rect.bottom <= mHeight);
int x; // for the x loops
uchar *line_ptr; // pointer to a line of pixels
// If we're filling in a 32-bit image
if(mChannels == 4)
{
for(int y = rect.top; y < rect.bottom; ++y)
{
line_ptr = getLinePtr(mHeight - y - 1); // Flip for Win32 coordinates
line_ptr += (rect.left * mChannels);
for(x = rect.left; x < rect.right; ++x)
{
memcpy(line_ptr, &argb, 4);
line_ptr += mChannels;
}
}
}
else // Image is 24-bit
{
// Get R, G, and B component
uchar red = GET_R(argb);
uchar green = GET_G(argb);
uchar blue = GET_B(argb);
for(int y = rect.top; y < rect.bottom; ++y)
{
line_ptr = getLinePtr(mHeight - y - 1); // Flip for Win32 coordinates;
line_ptr += (rect.left * mChannels);
for(x = rect.left; x < rect.right; ++x)
{
*line_ptr++ = blue;
*line_ptr++ = green;
*line_ptr++ = red;
}
}
}
} // end of fillRect(const RECT &rect, int argb)
// Blit CImage to "dest" starting at upper-left coordinate (x,y)
void CImage::blit(HDC destDC, int x, int y, DWORD rop) const
{
BitBlt(destDC,x,y,getWidth(),getHeight(),mHDC,0,0,rop);
}
// Blit CImage to "destDC" using coordinates specified by "rect"
void CImage::blit(HDC destDC, const RECT &rect, DWORD rop) const
{
BitBlt(destDC,rect.left,rect.top,(rect.right - rect.left),(rect.bottom - rect.top),
mHDC,0,0,rop);
}
// Scale blit using bilinear filtering
void CImage::scaleBlit(CImage &dest)
{
float yDelta = mHeight / (float)dest.getHeight();
float xDelta = mWidth / (float)dest.getWidth();
// Weights
float topWeight, botWeight, rtWeight, ltWeight;
for(int y = 0; y < dest.getHeight(); ++y)
{
float srcY = ((y + 0.5f) * yDelta) - 0.5f;
assert(srcY < mHeight);
// Clamp
if(srcY < 0.0f)
srcY = 0.0f;
int topLine = (int)srcY;
int botLine = (topLine == (mHeight - 1)) ? topLine : topLine + 1;
// Set top and bottom weight
botWeight = srcY - topLine;
topWeight = 1.0f - botWeight;
uchar *srcLineTop = getLinePtr(topLine);
uchar *srcLineBot = getLinePtr(botLine);
uchar *destLine = dest.getLinePtr(y);
for(int x = 0; x < dest.getWidth(); ++x)
{
float srcX = ((x + 0.5f) * xDelta) - 0.5f;
assert(srcX < mWidth);
// Clamp
if(srcX < 0.0f)
srcX = 0.0f;
int ltPixel = (int)srcX;
int rtPixel = (ltPixel == mWidth - 1) ? ltPixel : ltPixel + 1;
// Set right and left weights
rtWeight = srcX - ltPixel;
ltWeight = 1.0f - rtWeight;
// Make ltPixel and rtPixel correct indices
ltPixel *= mChannels;
rtPixel *= mChannels;
for(int c = 0; (c < mChannels) && (c < dest.getChannels()); ++c)
{
destLine[c] = uchar((float)srcLineTop[ltPixel + c] * topWeight * ltWeight +
(float)srcLineTop[rtPixel + c] * topWeight * rtWeight +
(float)srcLineBot[ltPixel + c] * botWeight * ltWeight +
(float)srcLineBot[rtPixel + c] * botWeight * rtWeight);
}
destLine += dest.getChannels();
}
}
}
void CImage::transparentBlit(HDC destDC, const RECT &rect, uchar R, uchar G, uchar B) const
{
TransparentBlt(destDC,rect.left,rect.top,(rect.right - rect.left),(rect.bottom - rect.top),
mHDC, 0, 0, mWidth, mHeight, RGB(R, G, B));
}
void CImage::transparentBlit(HDC destDC, const RECT &rect, int color) const
{
TransparentBlt(destDC,rect.left,rect.top,(rect.right - rect.left),(rect.bottom - rect.top),
mHDC, 0, 0, mWidth, mHeight, RGB(GET_R(color), GET_G(color), GET_B(color)));
}
// Draws to "dest", starting at upper left corner (destX, destY), using transparency
// color (R, G, B)
void CImage::transparentBlit(CImage &dest, int destX, int destY, uchar R, uchar G, uchar B) const
{
int x, y;
int srcWid = getWidth();
int srcHgt = getHeight();
int sX = 0;
int sY = srcHgt - 1;
Clip(destX, destY, dest.getWidth(), dest.getHeight(), sX, sY, srcWid, srcHgt);
int srcOffset = sX * getChannels();
int destOffset = destX * dest.getChannels();
// Flip for Win32 coords
destY = (dest.getHeight() - destY - 1);
for(y = sY; y >= 0; --y)
{
uchar *srcLine = getLinePtr(y);
uchar *destLine = dest.getLinePtr(destY);
// Adjust over
destLine += destOffset;
srcLine += srcOffset;
for(x = sX; x < srcWid; ++x)
{
if(!SameRGB(srcLine[eRed], srcLine[eGreen], srcLine[eBlue], R, G, B))
{
destLine[eRed] = srcLine[eRed];
destLine[eGreen] = srcLine[eGreen];
destLine[eBlue] = srcLine[eBlue];
}
srcLine += getChannels();
destLine += dest.getChannels();
}
// Can't draw past out destination image
if(--destY < 0)
return;
}
}
// Draws to "dest", starting at upper left corner (destX, destY), using transparency "color"
void CImage::transparentBlit(CImage &dest, int destX, int destY, int color) const
{
transparentBlit(dest, destX, destY, GET_R(color), GET_G(color), GET_B(color));
}
// Draws to "dest" starting at upper left corner (destX, destY) using
// the RGB in "color" as the color key and uses the A in "color" for the
// constant alpha component
void CImage::alphaTransBlit(CImage &dest, int destX, int destY, int color) const
{
int x, y;
int srcWid = getWidth();
int srcHgt = getHeight();
int sX = 0;
int sY = srcHgt - 1;
Clip(destX, destY, dest.getWidth(), dest.getHeight(), sX, sY, srcWid, srcHgt);
int destOffset = destX * dest.getChannels();
uchar R = GET_R(color);
uchar G = GET_G(color);
uchar B = GET_B(color);
uchar alpha = GET_A(color);
// Flip for Win32 coords
destY = (dest.getHeight() - destY - 1);
// Loop backwards to handle Win32 coords
for(y = sY; y >= 0; --y)
{
uchar *srcLine = getLinePtr(y);
uchar *destLine = dest.getLinePtr(destY);
// Adjust over
destLine += destOffset;
for(x = sX; x < srcWid; ++x)
{
if(!SameRGB(srcLine[eRed], srcLine[eGreen], srcLine[eBlue], R, G, B))
{
// Set the RGB
destLine[eRed] += ((srcLine[eRed] - destLine[eRed]) * alpha + kMaxARGB) >> 8;
destLine[eGreen] += ((srcLine[eGreen] - destLine[eGreen]) * alpha + kMaxARGB) >> 8;
destLine[eBlue] += ((srcLine[eBlue] - destLine[eBlue]) * alpha + kMaxARGB) >> 8;
}
srcLine += getChannels();
destLine += dest.getChannels();
}
// Can't draw past out destination image
if(--destY < 0)
return;
}
}
// Draws "this" CImage to "dest" starting at upper-left corner (destX, destY) using
// per-pixel alpha in "this" image
void CImage::alphaBlit(CImage &dest, int destX, int destY) const
{
assert(destX >= 0 && destX < dest.getWidth());
assert(destY >= 0 && destY < dest.getHeight());
assert(mChannels == 4); // Make sure "this" image has alpha
uchar *srcLine;
uchar *destLine;
// Flip for Win32 coords
destY = (dest.getHeight() - destY - 1);
int x, y;
// Offset, to move over by, for the destination line
int dest_offset = destX * mChannels;
int dest_width = mWidth; // Assume we can draw entire CImage
// If source CImage is wider than destination, set dest_width to clip source
if(dest_width > (destX + dest.getWidth()))
dest_width -= (dest_width - (destX + dest.getWidth())); // Subtract off amount over
assert(dest_width > 0);
// Walk through image backwards for Win32 coords
for(y = mHeight - 1; y >= 0; --y)
{
srcLine = getLinePtr(y);
destLine = dest.getLinePtr(destY);
// Move to starting position in destination line
destLine += dest_offset;
for(x = 0; x < dest_width; ++x)
{
uchar alpha = srcLine[eAlpha];
// Set the RGB
destLine[eRed] += ((srcLine[eRed] - destLine[eRed]) * alpha + kMaxARGB) >> 8;
destLine[eGreen] += ((srcLine[eGreen] - destLine[eGreen]) * alpha + kMaxARGB) >> 8;
destLine[eBlue] += ((srcLine[eBlue] - destLine[eBlue]) * alpha + kMaxARGB) >> 8;
srcLine += mChannels;
destLine += dest.getChannels();
}
// Walk backwards for Win32 image, make sure we don't draw past our CImage
if(--destY < 0)
return;
}
}
// Draws "this" CImage to "dest" starting at upper-left corner (destX, destY) using
// constant per-pixel "alpha"
void CImage::constAlphaBlit(CImage &dest, int destX, int destY, uchar alpha) const
{
assert(destX >= 0 && destX < dest.getWidth());
assert(destY >= 0 && destY < dest.getHeight());
uchar *srcLine;
uchar *destLine;
// Flip for Win32 coords
destY = (dest.getHeight() - destY - 1);
int x, y;
// Offset, to move over by, for the destination line
int dest_offset = destX * mChannels;
int dest_width = mWidth; // Assume we can draw entire CImage
// If source CImage is wider than destination, set dest_width to clip source
if(dest_width > (destX + dest.getWidth()))
dest_width -= (dest_width - (destX + dest.getWidth())); // Subtract off amount over
assert(dest_width > 0);
// Walk through image backwards for Win32 coords
for(y = mHeight - 1; y >= 0; --y)
{
srcLine = getLinePtr(y);
destLine = dest.getLinePtr(destY);
// Move to starting position in destination line
destLine += dest_offset;
for(x = 0; x < dest_width; ++x)
{
// Set the RGB
destLine[eRed] += ((srcLine[eRed] - destLine[eRed]) * alpha + kMaxARGB) >> 8;
destLine[eGreen] += ((srcLine[eGreen] - destLine[eGreen]) * alpha + kMaxARGB) >> 8;
destLine[eBlue] += ((srcLine[eBlue] - destLine[eBlue]) * alpha + kMaxARGB) >> 8;
srcLine += mChannels;
destLine += dest.getChannels();
}
// Walk backwards for Win32 image, make sure we don't draw past our CImage
if(--destY < 0)
return;
}
}
// Fills the CImage with "src" by the dimensions specified in "srcRect"
// starting at upper-left corner (destX, destY) using transparency color (R,G,B)
void CImage::transparentFill(CImage &src, const RECT &srcRect, int destX, int destY,
uchar R, uchar G, uchar B)
{
assert(destX >= 0 && destX < mWidth);
assert(destY >= 0 && destY < mHeight);
assert(srcRect.left >= 0 && srcRect.right <= src.getWidth());
assert(srcRect.top >= 0 && srcRect.bottom <= src.getHeight());
assert(destX + (srcRect.right - srcRect.left) <= mWidth);
// Flip for Win32 coords
destY = (mHeight - destY - 1);
uchar *srcLine;
uchar *destLine;
int x, y;
int src_offset = srcRect.left * src.getChannels(); // Offset into source line
int dest_offset = destX * mChannels; // Offset into dest line
for(y = srcRect.top; y < srcRect.bottom; ++y)
{
srcLine = src.getLinePtr(src.getHeight() - y - 1); // Flip for Win32 coords
destLine = getLinePtr(destY);
// Move to correct offset for source and dest lines
srcLine += src_offset;
destLine += dest_offset;
for(x = srcRect.left; x < srcRect.right; ++x)
{
// If it's not the transparency color, copy it over to the destination
if(SameRGB(R,G,B,srcLine[eRed],srcLine[eGreen],srcLine[eBlue]) == false)
{
destLine[eRed] = srcLine[eRed];
destLine[eGreen] = srcLine[eGreen];
destLine[eBlue] = srcLine[eBlue];
}
srcLine += src.getChannels();
destLine += mChannels;
}