-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap_image.hpp
4996 lines (4444 loc) · 242 KB
/
bitmap_image.hpp
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
/*
*****************************************************************************
* *
* Platform Independent *
* Bitmap Image Reader Writer Library *
* *
* Author: Arash Partow - 2002 *
* Developler Author : Talha Celik - 2018 *
* URL: http://partow.net/programming/bitmap/index.html *
* URL: http://talhacelik.com.tr *
* *
* Note: This library only supports 24-bits per pixel bitmap format files. *
* *
* Copyright notice: *
* Free use of the Platform Independent Bitmap Image Reader Writer Library *
* is permitted under the guidelines and in accordance with the most current *
* version of the MIT License. *
* http://www.opensource.org/licenses/MIT *
* *
*****************************************************************************
*/
#ifndef INCLUDE_BITMAP_IMAGE_HPP
#define INCLUDE_BITMAP_IMAGE_HPP
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <limits>
#include <string>
#include <vector>
class bitmap_image
{
public:
enum channel_mode {
rgb_mode = 0,
bgr_mode = 1
};
enum color_plane {
blue_plane = 0,
green_plane = 1,
red_plane = 2
};
struct rgb_t
{
unsigned char red;
unsigned char green;
unsigned char blue;
};
bitmap_image() //cunstructor
: file_name_(""),
width_ (0),
height_ (0),
row_increment_ (0),
bytes_per_pixel_(3),
channel_mode_(bgr_mode)
{}
bitmap_image(const std::string& filename)
: file_name_(filename),
width_ (0),
height_ (0),
row_increment_ (0),
bytes_per_pixel_(0),
channel_mode_(bgr_mode)
{
load_bitmap();
}
bitmap_image(const unsigned int width, const unsigned int height)
: file_name_(""),
width_ (width ),
height_(height),
row_increment_ (0),
bytes_per_pixel_(3),
channel_mode_(bgr_mode)
{
create_bitmap();
}
bitmap_image(const bitmap_image& image)
: file_name_(image.file_name_),
width_ (image.width_ ),
height_ (image.height_ ),
row_increment_ (0),
bytes_per_pixel_(3),
channel_mode_(bgr_mode)
{
create_bitmap();
data_ = image.data_;
}
bitmap_image& operator=(const bitmap_image& image)
{
if (this != &image)
{
file_name_ = image.file_name_;
bytes_per_pixel_ = image.bytes_per_pixel_;
width_ = image.width_;
height_ = image.height_;
row_increment_ = 0;
channel_mode_ = image.channel_mode_;
create_bitmap();
data_ = image.data_;
}
return *this;
}
inline bool operator!()
{
return (data_.size() == 0) ||
(width_ == 0) ||
(height_ == 0) ||
(row_increment_ == 0);
}
inline void clear(const unsigned char v = 0x00)
{
std::fill(data_.begin(), data_.end(), v);
}
inline unsigned char red_channel(const unsigned int x, const unsigned int y) const
{
return data_[(y * row_increment_) + (x * bytes_per_pixel_ + 2)];
}
inline unsigned char green_channel(const unsigned int x, const unsigned int y) const
{
return data_[(y * row_increment_) + (x * bytes_per_pixel_ + 1)];
}
inline unsigned char blue_channel (const unsigned int x, const unsigned int y) const
{
return data_[(y * row_increment_) + (x * bytes_per_pixel_ + 0)];
}
inline void red_channel(const unsigned int x, const unsigned int y, const unsigned char value)
{
data_[(y * row_increment_) + (x * bytes_per_pixel_ + 2)] = value;
}
inline void green_channel(const unsigned int x, const unsigned int y, const unsigned char value)
{
data_[(y * row_increment_) + (x * bytes_per_pixel_ + 1)] = value;
}
inline void blue_channel (const unsigned int x, const unsigned int y, const unsigned char value)
{
data_[(y * row_increment_) + (x * bytes_per_pixel_ + 0)] = value;
}
inline unsigned char* row(unsigned int row_index) const
{
return const_cast<unsigned char*>(&data_[(row_index * row_increment_)]);
}
inline void get_pixel(const unsigned int x, const unsigned int y,
unsigned char& red,
unsigned char& green,
unsigned char& blue) const
{
const unsigned int y_offset = y * row_increment_;
const unsigned int x_offset = x * bytes_per_pixel_;
const unsigned int offset = y_offset + x_offset;
blue = data_[offset + 0];
green = data_[offset + 1];
red = data_[offset + 2];
}
template <typename RGB>
inline void get_pixel(const unsigned int x, const unsigned int y, RGB& colour) const
{
get_pixel(x, y, colour.red, colour.green, colour.blue);
}
inline rgb_t get_pixel(const unsigned int x, const unsigned int y) const
{
rgb_t colour;
get_pixel(x, y, colour.red, colour.green, colour.blue);
return colour;
}
inline void set_pixel(const unsigned int x, const unsigned int y,
const unsigned char red,
const unsigned char green,
const unsigned char blue)
{
const unsigned int y_offset = y * row_increment_;
const unsigned int x_offset = x * bytes_per_pixel_;
const unsigned int offset = y_offset + x_offset;
data_[offset + 0] = blue;
data_[offset + 1] = green;
data_[offset + 2] = red;
}
template <typename RGB>
inline void set_pixel(const unsigned int x, const unsigned int y, const RGB& colour)
{
set_pixel(x, y, colour.red, colour.green, colour.blue);
}
inline bool copy_from(const bitmap_image& image)
{
if (
(image.height_ != height_) ||
(image.width_ != width_ )
)
{
return false;
}
data_ = image.data_;
return true;
}
inline bool copy_from(const bitmap_image& source_image,
const unsigned int& x_offset,
const unsigned int& y_offset)
{
if ((x_offset + source_image.width_ ) > width_ ) { return false; }
if ((y_offset + source_image.height_) > height_) { return false; }
for (unsigned int y = 0; y < source_image.height_; ++y)
{
unsigned char* itr1 = row(y + y_offset) + x_offset * bytes_per_pixel_;
const unsigned char* itr2 = source_image.row(y);
const unsigned char* itr2_end = itr2 + source_image.width_ * bytes_per_pixel_;
std::copy(itr2, itr2_end, itr1);
}
return true;
}
inline bool region(const unsigned int& x ,
const unsigned int& y ,
const unsigned int& width ,
const unsigned int& height,
bitmap_image& dest_image ) const
{
if ((x + width ) > width_ ) { return false; }
if ((y + height) > height_) { return false; }
if (
(dest_image.width_ < width_ ) ||
(dest_image.height_ < height_)
)
{
dest_image.setwidth_height(width,height);
}
for (unsigned int r = 0; r < height; ++r)
{
unsigned char* itr1 = row(r + y) + x * bytes_per_pixel_;
unsigned char* itr1_end = itr1 + (width * bytes_per_pixel_);
unsigned char* itr2 = dest_image.row(r);
std::copy(itr1, itr1_end, itr2);
}
return true;
}
inline bool roi_from_center(const unsigned int& cx ,
const unsigned int& cy ,
const unsigned int& width ,
const unsigned int& height,
bitmap_image& dest_image ) const
{
return region(cx - (width / 2), cy - (height / 2),
width, height,
dest_image);
}
inline bool set_region(const unsigned int& x ,
const unsigned int& y ,
const unsigned int& width ,
const unsigned int& height,
const unsigned char& value )
{
if ((x + width ) > width_ ) { return false; }
if ((y + height) > height_) { return false; }
for (unsigned int r = 0; r < height; ++r)
{
unsigned char* itr = row(r + y) + x * bytes_per_pixel_;
unsigned char* itr_end = itr + (width * bytes_per_pixel_);
std::fill(itr, itr_end, value);
}
return true;
}
inline bool set_region(const unsigned int& x ,
const unsigned int& y ,
const unsigned int& width ,
const unsigned int& height,
const color_plane color ,
const unsigned char& value )
{
if ((x + width ) > width_ ) { return false; }
if ((y + height) > height_) { return false; }
const unsigned int color_plane_offset = offset(color);
for (unsigned int r = 0; r < height; ++r)
{
unsigned char* itr = row(r + y) + x * bytes_per_pixel_ + color_plane_offset;
unsigned char* itr_end = itr + (width * bytes_per_pixel_);
while (itr != itr_end)
{
*itr = value;
itr += bytes_per_pixel_;
}
}
return true;
}
inline bool set_region(const unsigned int& x ,
const unsigned int& y ,
const unsigned int& width ,
const unsigned int& height,
const unsigned char& red ,
const unsigned char& green ,
const unsigned char& blue )
{
if ((x + width ) > width_ ) { return false; }
if ((y + height) > height_) { return false; }
for (unsigned int r = 0; r < height; ++r)
{
unsigned char* itr = row(r + y) + x * bytes_per_pixel_;
unsigned char* itr_end = itr + (width * bytes_per_pixel_);
while (itr != itr_end)
{
*(itr++) = blue;
*(itr++) = green;
*(itr++) = red;
}
}
return true;
}
void reflective_image(bitmap_image& image, const bool include_diagnols = false)
{
image.setwidth_height(3 * width_, 3 * height_, true);
image.copy_from(*this, width_, height_);
vertical_flip();
image.copy_from(*this, width_, 0);
image.copy_from(*this, width_, 2 * height_);
vertical_flip();
horizontal_flip();
image.copy_from(*this, 0, height_);
image.copy_from(*this, 2 * width_, height_);
horizontal_flip();
if (include_diagnols)
{
bitmap_image tile = *this;
tile.vertical_flip();
tile.horizontal_flip();
image.copy_from(tile, 0, 0);
image.copy_from(tile, 2 * width_, 0);
image.copy_from(tile, 2 * width_, 2 * height_);
image.copy_from(tile, 0 , 2 * height_);
}
}
inline unsigned int width() const
{
return width_;
}
inline unsigned int height() const
{
return height_;
}
inline unsigned int bytes_per_pixel() const
{
return bytes_per_pixel_;
}
inline unsigned int pixel_count() const
{
return width_ * height_;
}
inline void setwidth_height(const unsigned int width,
const unsigned int height,
const bool clear = false)
{
data_.clear();
width_ = width;
height_ = height;
create_bitmap();
if (clear)
{
std::fill(data_.begin(), data_.end(), static_cast<unsigned char>(0x00));
}
}
void save_image(const std::string& file_name) const
{
std::ofstream stream(file_name.c_str(),std::ios::binary);
if (!stream)
{
std::cerr << "bitmap_image::save_image(): Error - Could not open file " << file_name << " for writing!" << std::endl;
return;
}
bitmap_information_header bih;
bih.width = width_;
bih.height = height_;
bih.bit_count = static_cast<unsigned short>(bytes_per_pixel_ << 3);
bih.clr_important = 0;
bih.clr_used = 0;
bih.compression = 0;
bih.planes = 1;
bih.size = bih.struct_size();
bih.x_pels_per_meter = 0;
bih.y_pels_per_meter = 0;
bih.size_image = (((bih.width * bytes_per_pixel_) + 3) & 0x0000FFFC) * bih.height;
bitmap_file_header bfh;
bfh.type = 19778;
bfh.size = bfh.struct_size() + bih.struct_size() + bih.size_image;
bfh.reserved1 = 0;
bfh.reserved2 = 0;
bfh.off_bits = bih.struct_size() + bfh.struct_size();
write_bfh(stream,bfh);
write_bih(stream,bih);
unsigned int padding = (4 - ((3 * width_) % 4)) % 4;
char padding_data[4] = { 0x00, 0x00, 0x00, 0x00 };
for (unsigned int i = 0; i < height_; ++i)
{
const unsigned char* data_ptr = &data_[(row_increment_ * (height_ - i - 1))];
stream.write(reinterpret_cast<const char*>(data_ptr), sizeof(unsigned char) * bytes_per_pixel_ * width_);
stream.write(padding_data,padding);
}
stream.close();
}
inline void set_all_ith_bits_low(const unsigned int bitr_index)
{
unsigned char mask = static_cast<unsigned char>(~(1 << bitr_index));
for (unsigned char* itr = data(); itr != end(); ++itr)
{
*itr &= mask;
}
}
inline void set_all_ith_bits_high(const unsigned int bitr_index)
{
unsigned char mask = static_cast<unsigned char>(1 << bitr_index);
for (unsigned char* itr = data(); itr != end(); ++itr)
{
*itr |= mask;
}
}
inline void set_all_ith_channels(const unsigned int& channel, const unsigned char& value)
{
for (unsigned char* itr = (data() + channel); itr < end(); itr += bytes_per_pixel_)
{
*itr = value;
}
}
inline void set_channel(const color_plane color,const unsigned char& value)
{
for (unsigned char* itr = (data() + offset(color)); itr < end(); itr += bytes_per_pixel_)
{
*itr = value;
}
}
inline void ror_channel(const color_plane color, const unsigned int& ror)
{
for (unsigned char* itr = (data() + offset(color)); itr < end(); itr += bytes_per_pixel_)
{
*itr = static_cast<unsigned char>(((*itr) >> ror) | ((*itr) << (8 - ror)));
}
}
inline void set_all_channels(const unsigned char& value)
{
for (unsigned char* itr = data(); itr < end(); )
{
*(itr++) = value;
}
}
inline void set_all_channels(const unsigned char& r_value,
const unsigned char& g_value,
const unsigned char& b_value)
{
for (unsigned char* itr = (data() + 0); itr < end(); itr += bytes_per_pixel_)
{
*(itr + 0) = b_value;
*(itr + 1) = g_value;
*(itr + 2) = r_value;
}
}
inline void invert_color_planes()
{
for (unsigned char* itr = data(); itr < end(); *itr = ~(*itr), ++itr);
}
inline void add_to_color_plane(const color_plane color, const unsigned char& value)
{
for (unsigned char* itr = (data() + offset(color)); itr < end(); itr += bytes_per_pixel_)
{
(*itr) += value;
}
}
inline void convert_to_grayscale()
{
double r_scaler = 0.299;
double g_scaler = 0.587;
double b_scaler = 0.114;
if (rgb_mode == channel_mode_)
{
std::swap(r_scaler, b_scaler);
}
for (unsigned char* itr = data(); itr < end(); )
{
unsigned char gray_value = static_cast<unsigned char>
(
(r_scaler * (*(itr + 2))) +
(g_scaler * (*(itr + 1))) +
(b_scaler * (*(itr + 0)))
);
*(itr++) = gray_value;
*(itr++) = gray_value;
*(itr++) = gray_value;
}
}
inline const unsigned char* data() const
{
return data_.data();
}
inline unsigned char* data()
{
return const_cast<unsigned char*>(data_.data());
}
inline void bgr_to_rgb()
{
if ((bgr_mode == channel_mode_) && (3 == bytes_per_pixel_))
{
reverse_channels();
channel_mode_ = rgb_mode;
}
}
inline void rgb_to_bgr()
{
if ((rgb_mode == channel_mode_) && (3 == bytes_per_pixel_))
{
reverse_channels();
channel_mode_ = bgr_mode;
}
}
inline void reverse()
{
unsigned char* itr1 = data();
unsigned char* itr2 = end() - bytes_per_pixel_;
while (itr1 < itr2)
{
for (std::size_t i = 0; i < bytes_per_pixel_; ++i)
{
unsigned char* citr1 = itr1 + i;
unsigned char* citr2 = itr2 + i;
std::swap(*citr1,*citr2);
}
itr1 += bytes_per_pixel_;
itr2 -= bytes_per_pixel_;
}
}
inline void horizontal_flip()
{
for (unsigned int y = 0; y < height_; ++y)
{
unsigned char* itr1 = row(y);
unsigned char* itr2 = itr1 + row_increment_ - bytes_per_pixel_;
while (itr1 < itr2)
{
for (unsigned int i = 0; i < bytes_per_pixel_; ++i)
{
unsigned char* p1 = (itr1 + i);
unsigned char* p2 = (itr2 + i);
std::swap(*p1,*p2);
}
itr1 += bytes_per_pixel_;
itr2 -= bytes_per_pixel_;
}
}
}
inline void vertical_flip()
{
for (unsigned int y = 0; y < (height_ / 2); ++y)
{
unsigned char* itr1 = row(y);
unsigned char* itr2 = row(height_ - y - 1);
for (std::size_t x = 0; x < row_increment_; ++x)
{
std::swap(*(itr1 + x),*(itr2 + x));
}
}
}
inline void export_color_plane(const color_plane color, unsigned char* image)
{
for (unsigned char* itr = (data() + offset(color)); itr < end(); ++image, itr += bytes_per_pixel_)
{
(*image) = (*itr);
}
}
inline void export_color_plane(const color_plane color, bitmap_image& image)
{
if (
(width_ != image.width_ ) ||
(height_ != image.height_)
)
{
image.setwidth_height(width_,height_);
}
image.clear();
unsigned char* itr1 = (data() + offset(color));
unsigned char* itr1_end = end();
unsigned char* itr2 = (image.data() + offset(color));
while (itr1 < itr1_end)
{
(*itr2) = (*itr1);
itr1 += bytes_per_pixel_;
itr2 += bytes_per_pixel_;
}
}
inline void export_response_image(const color_plane color, double* response_image)
{
double* resp_itr = response_image;
for (unsigned char* itr = (data() + offset(color)); itr < end(); ++response_image, itr += bytes_per_pixel_)
{
*(resp_itr++) = (1.0 * (*itr)) / 256.0;
}
}
inline void export_gray_scale_response_image(double* response_image) const
{
double* resp_itr = response_image;
for (const unsigned char* itr = data(); itr < end(); itr += bytes_per_pixel_)
{
unsigned char gray_value = static_cast<unsigned char>
(
(0.299 * (*(itr + 2))) +
(0.587 * (*(itr + 1))) +
(0.114 * (*(itr + 0)))
);
*(resp_itr++) = (1.0 * gray_value) / 256.0;
}
}
inline void export_rgb(double* red, double* green, double* blue) const
{
if (bgr_mode != channel_mode_)
return;
for (const unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
(*blue ) = (1.0 * (*(itr++))) / 256.0;
(*green) = (1.0 * (*(itr++))) / 256.0;
(*red ) = (1.0 * (*(itr++))) / 256.0;
}
}
inline void export_rgb(float* red, float* green, float* blue) const
{
if (bgr_mode != channel_mode_)
return;
for (const unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
(*blue ) = (1.0f * (*(itr++))) / 256.0f;
(*green) = (1.0f * (*(itr++))) / 256.0f;
(*red ) = (1.0f * (*(itr++))) / 256.0f;
}
}
inline void export_rgb(unsigned char* red, unsigned char* green, unsigned char* blue) const
{
if (bgr_mode != channel_mode_)
return;
for (const unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
(*blue ) = *(itr++);
(*green) = *(itr++);
(*red ) = *(itr++);
}
}
inline void export_ycbcr(double* y, double* cb, double* cr) const
{
if (bgr_mode != channel_mode_)
return;
for (const unsigned char* itr = data(); itr < end(); ++y, ++cb, ++cr)
{
const double blue = (1.0 * (*(itr++)));
const double green = (1.0 * (*(itr++)));
const double red = (1.0 * (*(itr++)));
( *y) = clamp<double>( 16.0 + (1.0/256.0) * ( 65.738 * red + 129.057 * green + 25.064 * blue),1.0,254);
(*cb) = clamp<double>(128.0 + (1.0/256.0) * (- 37.945 * red - 74.494 * green + 112.439 * blue),1.0,254);
(*cr) = clamp<double>(128.0 + (1.0/256.0) * ( 112.439 * red - 94.154 * green - 18.285 * blue),1.0,254);
}
}
inline void export_rgb_normal(double* red, double* green, double* blue) const
{
if (bgr_mode != channel_mode_)
return;
for (const unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
(*blue ) = (1.0 * (*(itr++)));
(*green) = (1.0 * (*(itr++)));
(*red ) = (1.0 * (*(itr++)));
}
}
inline void export_rgb_normal(float* red, float* green, float* blue) const
{
if (bgr_mode != channel_mode_)
return;
for (const unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
(*blue ) = (1.0f * (*(itr++)));
(*green) = (1.0f * (*(itr++)));
(*red ) = (1.0f * (*(itr++)));
}
}
inline void import_rgb(double* red, double* green, double* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = static_cast<unsigned char>(256.0 * (*blue ));
*(itr++) = static_cast<unsigned char>(256.0 * (*green));
*(itr++) = static_cast<unsigned char>(256.0 * (*red ));
}
}
inline void import_rgb(float* red, float* green, float* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = static_cast<unsigned char>(256.0f * (*blue ));
*(itr++) = static_cast<unsigned char>(256.0f * (*green));
*(itr++) = static_cast<unsigned char>(256.0f * (*red ));
}
}
inline void import_rgb(unsigned char* red, unsigned char* green, unsigned char* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = (*blue );
*(itr++) = (*green);
*(itr++) = (*red );
}
}
inline void import_ycbcr(double* y, double* cb, double* cr)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++y, ++cb, ++cr)
{
double y_ = (*y);
double cb_ = (*cb);
double cr_ = (*cr);
*(itr++) = static_cast<unsigned char>(clamp((298.082 * y_ + 516.412 * cb_ ) / 256.0 - 276.836,0.0,255.0));
*(itr++) = static_cast<unsigned char>(clamp((298.082 * y_ - 100.291 * cb_ - 208.120 * cr_ ) / 256.0 + 135.576,0.0,255.0));
*(itr++) = static_cast<unsigned char>(clamp((298.082 * y_ + 408.583 * cr_ ) / 256.0 - 222.921,0.0,255.0));
}
}
inline void import_gray_scale_clamped(double* gray)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++gray)
{
unsigned char c = static_cast<unsigned char>(clamp<double>(256.0 * (*gray),0.0,255.0));
*(itr + 0) = c;
*(itr + 1) = c;
*(itr + 2) = c;
itr += 3;
}
}
inline void import_rgb_clamped(double* red, double* green, double* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = static_cast<unsigned char>(clamp<double>(256.0 * (*blue ),0.0,255.0));
*(itr++) = static_cast<unsigned char>(clamp<double>(256.0 * (*green),0.0,255.0));
*(itr++) = static_cast<unsigned char>(clamp<double>(256.0 * (*red ),0.0,255.0));
}
}
inline void import_rgb_clamped(float* red, float* green, float* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = static_cast<unsigned char>(clamp<double>(256.0f * (*blue ),0.0,255.0));
*(itr++) = static_cast<unsigned char>(clamp<double>(256.0f * (*green),0.0,255.0));
*(itr++) = static_cast<unsigned char>(clamp<double>(256.0f * (*red ),0.0,255.0));
}
}
inline void import_rgb_normal(double* red, double* green, double* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = static_cast<unsigned char>(*blue );
*(itr++) = static_cast<unsigned char>(*green);
*(itr++) = static_cast<unsigned char>(*red );
}
}
inline void import_rgb_normal(float* red, float* green, float* blue)
{
if (bgr_mode != channel_mode_)
return;
for (unsigned char* itr = data(); itr < end(); ++red, ++green, ++blue)
{
*(itr++) = static_cast<unsigned char>(*blue );
*(itr++) = static_cast<unsigned char>(*green);
*(itr++) = static_cast<unsigned char>(*red );
}
}
inline void subsample(bitmap_image& dest) const
{
/*
Half sub-sample of original image.
*/
unsigned int w = 0;
unsigned int h = 0;
bool odd_width = false;
bool odd_height = false;
if (0 == (width_ % 2))
w = width_ / 2;
else
{
w = 1 + (width_ / 2);
odd_width = true;
}
if (0 == (height_ % 2))
h = height_ / 2;
else
{
h = 1 + (height_ / 2);
odd_height = true;
}
unsigned int horizontal_upper = (odd_width) ? (w - 1) : w;
unsigned int vertical_upper = (odd_height) ? (h - 1) : h;
dest.setwidth_height(w,h);
dest.clear();
unsigned char* s_itr[3];
const unsigned char* itr1[3];
const unsigned char* itr2[3];
s_itr[0] = dest.data() + 0;
s_itr[1] = dest.data() + 1;
s_itr[2] = dest.data() + 2;
itr1[0] = data() + 0;
itr1[1] = data() + 1;
itr1[2] = data() + 2;
itr2[0] = data() + row_increment_ + 0;
itr2[1] = data() + row_increment_ + 1;
itr2[2] = data() + row_increment_ + 2;
unsigned int total = 0;
for (unsigned int j = 0; j < vertical_upper; ++j)
{
for (unsigned int i = 0; i < horizontal_upper; ++i)
{
for (unsigned int k = 0; k < bytes_per_pixel_; s_itr[k] += bytes_per_pixel_, ++k)
{
total = 0;
total += *(itr1[k]);
total += *(itr1[k]);
total += *(itr2[k]);
total += *(itr2[k]);
itr1[k] += bytes_per_pixel_;