-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpnm.hpp
2031 lines (1850 loc) · 65.6 KB
/
pnm.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
// MIT License
//
// Copyright (c) 2018 Toru Niina
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef PNM_PLUS_PLUS_H
#define PNM_PLUS_PLUS_H
#if !defined(__cplusplus)
#error "pnm++ is a library for C++."
#endif
#if __cplusplus < 201103L
#error "pnm++ requires C++11 or later."
#endif
#include <cctype>
#include <type_traits>
#include <utility>
#include <stdexcept>
#include <iterator>
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include <array>
#include <ostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdint>
namespace pnm
{
// --------------------------------------------------------------------------
// * basic_pixel
// _ _ - basic_pixel<T, 1>
// _ __ (_)__ _ ___ | | ___ - basic_pixel<T, 3>
// | '_ \ | |\ \/ / _ \| |/ __| * aliases
// | |_) )| | ) ( __/| |\__ \ - bit_pixel for bitmap image
// | .__/ |_|/_/\_\___||_||___/ - gray_pixel for grayscale image
// |_| - pix_pixel for RGB color image
// * literals
// --------------------------------------------------------------------------
template<typename T, std::size_t N>
struct basic_pixel;
template<typename T>
struct basic_pixel<T, 1>
{
public:
using value_type = T;
static constexpr std::size_t colors = 1;
basic_pixel() = default;
~basic_pixel() = default;
basic_pixel(const basic_pixel&) = default;
basic_pixel(basic_pixel&&) = default;
basic_pixel& operator=(const basic_pixel&) = default;
basic_pixel& operator=(basic_pixel&&) = default;
basic_pixel(const value_type& v)
noexcept(std::is_nothrow_copy_constructible<value_type>::value)
: value(v)
{}
basic_pixel(value_type&& v)
noexcept(std::is_nothrow_move_constructible<value_type>::value)
: value(std::move(v))
{}
value_type value;
};
template<typename T>
constexpr std::size_t basic_pixel<T, 1>::colors;
template<typename T>
inline bool operator==(const basic_pixel<T, 1>& lhs, const basic_pixel<T, 1>& rhs) noexcept
{
return lhs.value == rhs.value;
}
template<typename T>
inline bool operator!=(const basic_pixel<T, 1>& lhs, const basic_pixel<T, 1>& rhs) noexcept
{
return lhs.value != rhs.value;
}
template<typename T>
inline bool operator<(const basic_pixel<T, 1>& lhs, const basic_pixel<T, 1>& rhs) noexcept
{
return lhs.value < rhs.value;
}
template<typename T>
inline bool operator>(const basic_pixel<T, 1>& lhs, const basic_pixel<T, 1>& rhs) noexcept
{
return lhs.value > rhs.value;
}
template<typename T>
inline bool operator<=(const basic_pixel<T, 1>& lhs, const basic_pixel<T, 1>& rhs) noexcept
{
return lhs.value <= rhs.value;
}
template<typename T>
inline bool operator>=(const basic_pixel<T, 1>& lhs, const basic_pixel<T, 1>& rhs) noexcept
{
return lhs.value >= rhs.value;
}
template<typename T>
struct basic_pixel<T, 3>
{
public:
using value_type = T;
static constexpr std::size_t colors = 3;
basic_pixel() = default;
~basic_pixel() = default;
basic_pixel(const basic_pixel&) = default;
basic_pixel(basic_pixel&&) = default;
basic_pixel& operator=(const basic_pixel&) = default;
basic_pixel& operator=(basic_pixel&&) = default;
basic_pixel(const value_type& R, const value_type& G, const value_type& B)
noexcept(std::is_nothrow_copy_constructible<value_type>::value)
: red(R), green(G), blue(B)
{}
basic_pixel(value_type&& R, value_type&& G, value_type&& B)
noexcept(std::is_nothrow_move_constructible<value_type>::value)
: red(std::move(R)), green(std::move(G)), blue(std::move(B))
{}
basic_pixel(const std::array<value_type, 3>& values)
noexcept(std::is_nothrow_copy_constructible<value_type>::value)
: red(values[0]), green(values[1]), blue(values[2])
{}
value_type red;
value_type green;
value_type blue;
};
template<typename T>
constexpr std::size_t basic_pixel<T, 3>::colors;
template<typename T>
inline bool operator==(const basic_pixel<T, 3>& lhs, const basic_pixel<T, 3>& rhs) noexcept
{
return lhs.red == rhs.red && lhs.green == rhs.green && lhs.blue == rhs.blue;
}
template<typename T>
inline bool operator!=(const basic_pixel<T, 3>& lhs, const basic_pixel<T, 3>& rhs) noexcept
{
return !(lhs == rhs);
}
template<typename T>
inline bool operator<(const basic_pixel<T, 3>& lhs, const basic_pixel<T, 3>& rhs) noexcept
{
return std::array<T, 3>{{lhs.red, lhs.green, lhs.blue}} <
std::array<T, 3>{{rhs.red, rhs.green, rhs.blue}};
}
template<typename T>
inline bool operator<=(const basic_pixel<T, 3>& lhs, const basic_pixel<T, 3>& rhs) noexcept
{
return (lhs < rhs) || (lhs == rhs);
}
template<typename T>
inline bool operator>(const basic_pixel<T, 3>& lhs, const basic_pixel<T, 3>& rhs) noexcept
{
return !(lhs <= rhs);
}
template<typename T>
inline bool operator>=(const basic_pixel<T, 3>& lhs, const basic_pixel<T, 3>& rhs) noexcept
{
return !(lhs < rhs);
}
using bit_pixel = basic_pixel<bool, 1>;
using gray_pixel = basic_pixel<std::uint8_t, 1>;
using rgb_pixel = basic_pixel<std::uint8_t, 3>;
// XXX not supported yet
// using gray16_pixel = basic_pixel<std::uint16_t, 1>;
// using rgb16_pixel = basic_pixel<std::uint16_t, 3>;
template<typename T>
struct is_pixel : std::false_type {};
template<typename T, std::size_t N>
struct is_pixel<basic_pixel<T, N>> : std::true_type {};
template<typename From, typename To>
struct is_narrowing_conversion : std::false_type {};
template<>
struct is_narrowing_conversion<gray_pixel,/* -> */ bit_pixel>: std::true_type{};
template<>
struct is_narrowing_conversion< rgb_pixel,/* -> */ bit_pixel>: std::true_type{};
template<>
struct is_narrowing_conversion< rgb_pixel,/* -> */gray_pixel>: std::true_type{};
namespace detail
{
template<typename From, typename To>
struct convert_impl
{
static inline To invoke(From)
{
throw std::runtime_error("pnm::convert_to(pixel): "
"narrowing conversion is not allowed by default.");
}
};
template<>
struct convert_impl<bit_pixel, bit_pixel>
{
static inline bit_pixel invoke(bit_pixel pixel) noexcept {return pixel;}
};
template<>
struct convert_impl<bit_pixel, gray_pixel>
{
static inline gray_pixel invoke(bit_pixel pixel) noexcept
{return (pixel.value) ? gray_pixel(0) : gray_pixel(255);}
};
template<>
struct convert_impl<bit_pixel, rgb_pixel>
{
static inline rgb_pixel invoke(bit_pixel pixel) noexcept
{return (pixel.value) ? rgb_pixel(0, 0, 0) : rgb_pixel(255, 255, 255);}
};
template<>
struct convert_impl<gray_pixel, gray_pixel>
{
static inline gray_pixel invoke(gray_pixel pixel) noexcept {return pixel;}
};
template<>
struct convert_impl<gray_pixel, rgb_pixel>
{
static inline rgb_pixel invoke(gray_pixel pixel) noexcept
{return rgb_pixel(pixel.value, pixel.value, pixel.value);}
};
template<>
struct convert_impl<rgb_pixel, rgb_pixel>
{
static inline rgb_pixel invoke(rgb_pixel pixel) noexcept {return pixel;}
};
} // detail
template<typename To, typename From>
inline typename std::enable_if<is_pixel<typename std::remove_cv<
typename std::remove_reference<From>::type>::type>::value &&
is_pixel<To>::value, To>::type
convert_to(From&& pixel)
{
return detail::convert_impl<typename std::remove_cv<
typename std::remove_reference<From>::type>::type, To
>::invoke(std::forward<From>(pixel));
}
namespace literals
{
inline namespace pixel_literals
{
inline bit_pixel operator"" _bit(unsigned long long x)
{
return bit_pixel(x != 0);
}
inline gray_pixel operator"" _gray(unsigned long long x)
{
return gray_pixel(static_cast<std::uint8_t>(x & 0xFFu));
}
inline rgb_pixel operator"" _rgb(unsigned long long x)
{
const std::uint8_t R(static_cast<std::uint8_t>((x >> 16) & 0xFFu));
const std::uint8_t G(static_cast<std::uint8_t>((x >> 8) & 0xFFu));
const std::uint8_t B(static_cast<std::uint8_t>( x & 0xFFu));
return rgb_pixel(R, G, B);
}
} // pixel_literals
} // literals
// --------------------------------------------------------------------------
// _ __ _ __ _____ ____ __ * detail::(const_)line_proxy
// | '_ \| '_// _ \ \/ /\ \/ / to enable `image[y][x]`
// | |_) | | ( (_) ) ( \ / * detail::(const_)line_iterator
// | .__/|_| \___/_/\_\ / / to enable `for(auto line : image.lines())`
// |_| /_/ * detail::range<iterator>
// just a pair of iterators.
// --------------------------------------------------------------------------
namespace detail
{
template<typename Container> struct line_proxy_iterator;
template<typename Container> struct const_line_proxy_iterator;
// XXX not supported yet
// template<typename Container> struct reverse_line_proxy_iterator;
// template<typename Container> struct const_reverse_line_proxy_iterator;
template<typename Container> struct const_line_proxy;
template<typename Container>
struct line_proxy
{
using container_type = Container;
using pixel_type = typename container_type::value_type;
using reference = typename container_type::reference;
using const_reference = typename container_type::const_reference;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
line_proxy(container_type& c, std::size_t iy, std::size_t nx) noexcept
: nx_(nx), iy_(iy), offset_(nx * iy), container_(std::addressof(c))
{}
~line_proxy() = default;
line_proxy(const line_proxy& other)
: nx_(other.nx_), iy_(other.iy_), offset_(other.offset_),
container_(other.container_)
{}
line_proxy& operator=(const line_proxy& other)
{
if(this->nx_ != other.nx_)
{
throw std::out_of_range("pnm::image::line_proxy::copy this->width("+
std::to_string(this->nx_) + std::string(") differs from arg (")+
std::to_string(other.nx_) + std::string(")"));
}
for(std::size_t i=0; i<this->nx_; ++i)
{
(*this)[i] = other[i];
}
return *this;
}
line_proxy& operator=(const const_line_proxy<container_type>& other)
{
if(this->nx_ != other.width())
{
throw std::out_of_range("pnm::image::line_proxy::copy this->width("+
std::to_string(this->nx_) + std::string(") differs from arg (")+
std::to_string(other.width()) + std::string(")"));
}
for(std::size_t i=0; i<this->nx_; ++i)
{
(*this)[i] = other[i];
}
return *this;
}
line_proxy& operator=(const std::vector<pixel_type>& other)
{
if(this->nx_ != other.size())
{
throw std::out_of_range("pnm::image::line_proxy::copy this->width("+
std::to_string(this->nx_) + std::string(") differs from arg (")+
std::to_string(other.size()) + std::string(")"));
}
for(std::size_t i=0; i<this->nx_; ++i)
{
(*this)[i] = other[i];
}
return *this;
}
std::size_t width() const noexcept {return nx_;}
std::size_t y_position() const noexcept {return iy_;}
reference operator[](const std::size_t i) noexcept
{return (*container_)[offset_ + i];}
const_reference operator[](const std::size_t i) const noexcept
{return (*container_)[offset_ + i];}
reference at(const std::size_t i)
{
if(nx_ <= i)
{
throw std::out_of_range("pnm::image::line_proxy::at: index (" +
std::to_string(i) + std::string(") exceeds width(") +
std::to_string(nx_) + std::string(")"));
}
return container_->at(offset_ + i);
}
const_reference at(const std::size_t i) const
{
if(nx_ <= i)
{
throw std::out_of_range("pnm::image::line_proxy::at: index (" +
std::to_string(i) + std::string(") exceeds width(") +
std::to_string(nx_) + std::string(")"));
}
return container_->at(offset_ + i);
}
iterator begin() noexcept {return container_->begin() + offset_;}
iterator end() noexcept {return container_->begin() + offset_ + nx_;}
const_iterator begin() const noexcept {return container_->begin() + offset_;}
const_iterator end() const noexcept {return container_->begin() + offset_ + nx_;}
const_iterator cbegin() const noexcept {return container_->begin() + offset_;}
const_iterator cend() const noexcept {return container_->begin() + offset_ + nx_;}
bool operator==(const line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ &&
this->iy_ == rhs.iy_ &&
this->container_ == rhs.container_;
}
bool operator!=(const line_proxy& rhs) const noexcept
{
return !(*this == rhs);
}
bool operator<(const line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ < rhs.iy_;
}
bool operator>(const line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ > rhs.iy_;
}
bool operator<=(const line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ <= rhs.iy_;
}
bool operator>=(const line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ >= rhs.iy_;
}
private:
friend struct line_proxy_iterator<container_type>;
// friend struct reverse_line_proxy_iterator<container_type>;
std::size_t nx_, iy_;
std::size_t offset_;
container_type* container_;
};
template<typename Container>
struct const_line_proxy
{
using container_type = Container;
using reference = typename container_type::reference;
using const_reference = typename container_type::const_reference;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
const_line_proxy(const container_type& c,
std::size_t iy, std::size_t nx) noexcept
: nx_(nx), iy_(iy), offset_(nx * iy), container_(std::addressof(c))
{}
~const_line_proxy() = default;
const_line_proxy(const line_proxy<container_type>& other)
: nx_(other.nx_), iy_(other.iy_), offset_(other.offset_),
container_(other.container_)
{}
const_line_proxy(const const_line_proxy& other)
: nx_(other.nx_), iy_(other.iy_), offset_(other.offset_),
container_(other.container_)
{}
std::size_t width() const noexcept {return nx_;}
std::size_t y_position() const noexcept {return iy_;}
const_reference operator[](const std::size_t i) const noexcept
{return (*container_)[offset_ + i];}
const_reference at(const std::size_t i) const noexcept
{
if(nx_ <= i)
{
throw std::out_of_range("pnm::image::line_proxy::at: index (" +
std::to_string(i) + std::string(") exceeds width(") +
std::to_string(nx_) + std::string(")"));
}
return container_->at(offset_ + i);
}
iterator begin() noexcept {return container_->begin() + offset_;}
iterator end() noexcept {return container_->begin() + offset_ + nx_;}
const_iterator begin() const noexcept {return container_->begin() + offset_;}
const_iterator end() const noexcept {return container_->begin() + offset_ + nx_;}
const_iterator cbegin() const noexcept {return container_->begin() + offset_;}
const_iterator cend() const noexcept {return container_->begin() + offset_ + nx_;}
bool operator==(const const_line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ &&
this->iy_ == rhs.iy_ &&
this->container_ == rhs.container_;
}
bool operator!=(const const_line_proxy& rhs) const noexcept
{
return !(*this == rhs);
}
bool operator<(const const_line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ < rhs.iy_;
}
bool operator>(const const_line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ > rhs.iy_;
}
bool operator<=(const const_line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ <= rhs.iy_;
}
bool operator>=(const const_line_proxy& rhs) const noexcept
{
return this->nx_ == rhs.nx_ && this->container_ == rhs.container_ &&
this->iy_ >= rhs.iy_;
}
private:
friend struct const_line_proxy_iterator<container_type>;
// friend struct const_reverse_line_proxy_iterator<container_type>;
std::size_t nx_, iy_;
std::size_t offset_;
container_type const* container_;
};
template<typename Container>
struct line_proxy_iterator
{
using container_type = Container;
using value_type = line_proxy<container_type>;
using pointer = value_type const*;
using difference_type = std::ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;
line_proxy_iterator(container_type& c,
std::size_t nx, std::size_t ny, std::size_t iy)
: nx_(nx), ny_(ny), proxy_(c, iy, nx)
{}
~line_proxy_iterator() = default;
line_proxy_iterator(const line_proxy_iterator&) = default;
line_proxy_iterator& operator=(const line_proxy_iterator& rhs)
{
this->nx_ = rhs.nx_;
this->ny_ = rhs.ny_;
this->proxy_.nx_ = rhs.proxy_.nx_;
this->proxy_.iy_ = rhs.proxy_.iy_;
this->proxy_.container_ = rhs.proxy_.container_;
return *this;
}
value_type operator* () const noexcept {return value_type(this->proxy_);}
pointer operator->() const noexcept {return std::addressof(this->proxy_);}
line_proxy_iterator& operator++() noexcept
{
proxy_.iy_ += 1;
proxy_.offset_ += nx_;
return *this;
}
line_proxy_iterator& operator--() noexcept
{
proxy_.iy_ -= 1;
proxy_.offset_ -= nx_;
return *this;
}
line_proxy_iterator operator++(int) noexcept
{
const auto tmp(*this); ++(*this); return tmp;
}
line_proxy_iterator operator--(int) noexcept
{
const auto tmp(*this); --(*this); return tmp;
}
line_proxy_iterator& operator+=(const difference_type d) noexcept
{
proxy_.iy_ += d;
proxy_.offset_ += nx_ * d;
return *this;
}
line_proxy_iterator& operator-=(const difference_type d) noexcept
{
proxy_.iy_ -= d;
proxy_.offset_ -= nx_ * d;
return *this;
}
bool operator==(const line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ == rhs.proxy_;
}
bool operator!=(const line_proxy_iterator& rhs) const noexcept
{
return !(*this == rhs);
}
bool operator<(const line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ < rhs.proxy_;
}
bool operator>(const line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ > rhs.proxy_;
}
bool operator<=(const line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ <= rhs.proxy_;
}
bool operator>=(const line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ >= rhs.proxy_;
}
private:
std::size_t nx_, ny_;
value_type proxy_;
};
template<typename Container>
struct const_line_proxy_iterator
{
using container_type = Container;
using value_type = const_line_proxy<container_type>;
using reference = value_type const&;
using pointer = value_type const*;
using difference_type = std::ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;
const_line_proxy_iterator(const container_type& c,
std::size_t nx, std::size_t ny, std::size_t iy)
: nx_(nx), ny_(ny), proxy_(c, iy, nx)
{}
~const_line_proxy_iterator() = default;
const_line_proxy_iterator(const const_line_proxy_iterator&) = default;
const_line_proxy_iterator& operator=(const const_line_proxy_iterator& rhs)
{
this->nx_ = rhs.nx_;
this->ny_ = rhs.ny_;
this->proxy_.nx_ = rhs.proxy_.nx_;
this->proxy_.iy_ = rhs.proxy_.iy_;
this->proxy_.container_ = rhs.proxy_.container_;
return *this;
}
const_line_proxy_iterator(const line_proxy_iterator<container_type>& rhs)
: nx_(rhs.nx_), ny_(rhs.ny_), proxy_(rhs.proxy_)
{}
const_line_proxy_iterator&
operator=(const line_proxy_iterator<container_type>& rhs)
{
this->nx_ = rhs.nx_;
this->ny_ = rhs.ny_;
this->proxy_.nx_ = rhs.proxy_.nx_;
this->proxy_.iy_ = rhs.proxy_.iy_;
this->proxy_.container_ = rhs.proxy_.container_;
return *this;
}
reference operator* () const noexcept {return this->proxy_;}
pointer operator->() const noexcept {return std::addressof(this->proxy_);}
const_line_proxy_iterator& operator++() noexcept
{
proxy_.iy_ += 1;
proxy_.offset_ += nx_;
return *this;
}
const_line_proxy_iterator& operator--() noexcept
{
proxy_.iy_ -= 1;
proxy_.offset_ -= nx_;
return *this;
}
const_line_proxy_iterator operator++(int) noexcept
{
const auto tmp(*this); ++(*this); return tmp;
}
const_line_proxy_iterator operator--(int) noexcept
{
const auto tmp(*this); --(*this); return tmp;
}
const_line_proxy_iterator& operator+=(const difference_type d) noexcept
{
proxy_.iy_ += d;
proxy_.offset_ += nx_ * d;
return *this;
}
const_line_proxy_iterator& operator-=(const difference_type d) noexcept
{
proxy_.iy_ -= d;
proxy_.offset_ -= nx_ * d;
return *this;
}
bool operator==(const const_line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ == rhs.proxy_;
}
bool operator!=(const const_line_proxy_iterator& rhs) const noexcept
{
return !(*this == rhs);
}
bool operator<(const const_line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ < rhs.proxy_;
}
bool operator>(const const_line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ > rhs.proxy_;
}
bool operator<=(const const_line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ <= rhs.proxy_;
}
bool operator>=(const const_line_proxy_iterator& rhs) const noexcept
{
return this->ny_ == rhs.ny_ && this->proxy_ >= rhs.proxy_;
}
private:
std::size_t nx_, ny_;
value_type proxy_;
};
template<typename Iterator>
struct range
{
using iterator = Iterator;
range(iterator first, iterator last): begin_(first), end_(last){}
~range() = default;
range(const range&) = default;
range(range&&) = default;
range& operator=(const range&) = default;
range& operator=(range&&) = default;
iterator begin() const noexcept {return this->begin_;}
iterator end() const noexcept {return this->end_;}
private:
iterator begin_;
iterator end_;
};
} // detail
// --------------------------------------------------------------------------
// _ * pnm::image
// (_)_ _ _ __ _ __ _ ___ - a container that manages
// | | ` ` \/ _` |/ _` |/ _ \ - nx, ny
// | | | | | (_| | (_| | __/ - vector<pixel>
// |_|_|_|_|\__,_|\__, |\___|
// |___/
// --------------------------------------------------------------------------
template<typename Pixel, typename Alloc = std::allocator<Pixel>>
class image
{
public:
using pixel_type = Pixel;
using allocator_type = Alloc;
using container_type = std::vector<pixel_type, allocator_type>;
using size_type = typename container_type::size_type;
using difference_type = typename container_type::difference_type;
using value_type = typename container_type::value_type;
using pointer = typename container_type::pointer;
using const_pointer = typename container_type::const_pointer;
using reference = typename container_type::reference;
using const_reference = typename container_type::const_reference;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
using reverse_iterator = typename container_type::reverse_iterator;
using const_reverse_iterator = typename container_type::const_reverse_iterator;
using line_proxy = detail::line_proxy<container_type>;
using const_line_proxy = detail::const_line_proxy<container_type>;
using line_proxy_iterator = detail::line_proxy_iterator<container_type>;
using const_line_proxy_iterator = detail::const_line_proxy_iterator<container_type>;
using line_range = detail::range<line_proxy_iterator>;
using const_line_range = detail::range<const_line_proxy_iterator>;
image() = default;
~image() = default;
image(const image&) = default;
image(image&&) = default;
image& operator=(const image&) = default;
image& operator=(image&&) = default;
image(const std::size_t width, const std::size_t height)
: nx_(width), ny_(height), pixels_(width * height)
{}
image(const std::size_t width, const std::size_t height,
const pixel_type& pix)
: nx_(width), ny_(height), pixels_(width * height, pix)
{}
template<typename T>
image(const std::size_t width, const std::size_t height,
const std::vector<T>& values)
: nx_(width), ny_(height), pixels_(width * height)
{
if(values.size() != pixels_.size())
{
throw std::out_of_range("pnm::image::image this->size (" +
std::to_string(this->pixels_.size()) +
std::string(") differs from argument (") +
std::to_string(values.size()) + std::string(")"));
}
std::transform(values.begin(), values.end(), this->pixels_.begin(),
[](const T& v){return pixel_type(v);});
}
template<typename T>
image(const std::vector<std::vector<T>>& values)
{
if(values.empty() || values.front().empty())
{
this->nx_ = 0;
this->ny_ = 0;
}
else
{
this->nx_ = values.front().size();
this->ny_ = values.size();
this->pixels_.resize(this->nx_ * this->ny_);
for(std::size_t j=0; j<this->ny_; ++j)
{
const auto& line = values.at(j);
if(line.size() != this->nx_)
{
throw std::out_of_range("pnm::image::image width of the image (" +
std::to_string(this->nx_) + ") differs for each line");
}
for(std::size_t i=0; i<this->nx_; ++i)
{
(*this)(i, j) = pixel_type(line[i]);
}
}
}
}
template<typename T>
image& operator=(const std::vector<std::vector<T>>& values)
{
this->pixels_.clear();
if(values.empty() || values.front().empty())
{
this->nx_ = 0;
this->ny_ = 0;
return *this;
}
this->nx_ = values.front().size();
this->ny_ = values.size();
this->pixels_.resize(this->nx_ * this->ny_);
for(std::size_t j=0; j<this->ny_; ++j)
{
const auto& line = values.at(j);
if(line.size() != this->nx_)
{
throw std::out_of_range("pnm::image::image width of the image (" +
std::to_string(this->nx_) + ") differs for each line");
}
for(std::size_t i=0; i<this->nx_; ++i)
{
(*this)(i, j) = pixel_type(line[i]);
}
}
return *this;
}
line_proxy operator[](const std::size_t i) noexcept
{
return line_proxy(this->pixels_, i, nx_);
}
const_line_proxy operator[](const std::size_t i) const noexcept
{
return const_line_proxy(this->pixels_, i, nx_);
}
line_proxy at(const std::size_t i)
{
if(this->ny_ <= i)
{
throw std::out_of_range("pnm::image::at index(" +
std::to_string(i) + std::string(") exceeds height (") +
std::to_string(this->ny_) + std::string(") differs"));
}
return line_proxy(this->pixels_, i, nx_);
}
const_line_proxy at(const std::size_t i) const
{
if(this->ny_ <= i)
{
throw std::out_of_range("pnm::image::at index(" +
std::to_string(i) + std::string(") exceeds height (") +
std::to_string(this->ny_) + std::string(") differs"));
}
return const_line_proxy(this->pixels_, i, nx_);
}
reference operator()(const std::size_t ix, const std::size_t iy) noexcept
{
return pixels_[ix + iy * nx_];
}
const_reference
operator()(const std::size_t ix, const std::size_t iy) const noexcept
{
return pixels_[ix + iy * nx_];
}
reference at(const std::size_t ix, const std::size_t iy)
{
return pixels_.at(ix + iy * nx_);
}
const_reference at(const std::size_t ix, const std::size_t iy) const
{
return pixels_.at(ix + iy * nx_);
}
reference raw_access(const std::size_t i) noexcept {return pixels_[i];}
const_reference raw_access(const std::size_t i) const noexcept {return pixels_[i];}
reference raw_at(const std::size_t i) {return pixels_.at(i);}
const_reference raw_at(const std::size_t i) const {return pixels_.at(i);}
std::size_t width() const noexcept {return nx_;}
std::size_t height() const noexcept {return ny_;}
std::size_t x_size() const noexcept {return nx_;}
std::size_t y_size() const noexcept {return ny_;}
std::size_t size() const noexcept {return pixels_.size();}
iterator begin() noexcept {return pixels_.begin();}
iterator end() noexcept {return pixels_.end();}
const_iterator begin() const noexcept {return pixels_.begin();}
const_iterator end() const noexcept {return pixels_.end();}
const_iterator cbegin() const noexcept {return pixels_.cbegin();}
const_iterator cend() const noexcept {return pixels_.cend();}
reverse_iterator rbegin() noexcept {return pixels_.rbegin();}
reverse_iterator rend() noexcept {return pixels_.rend();}
const_reverse_iterator rbegin() const noexcept {return pixels_.rbegin();}
const_reverse_iterator rend() const noexcept {return pixels_.rend();}
const_reverse_iterator crbegin() const noexcept {return pixels_.crbegin();}
const_reverse_iterator crend() const noexcept {return pixels_.crend();}
line_proxy_iterator line_begin() noexcept
{return line_proxy_iterator(pixels_, nx_, ny_, 0);}
line_proxy_iterator line_end() noexcept
{return line_proxy_iterator(pixels_, nx_, ny_, ny_);}
const_line_proxy_iterator line_begin() const noexcept
{return const_line_proxy_iterator(pixels_, nx_, ny_, 0);}
const_line_proxy_iterator line_end() const noexcept
{return const_line_proxy_iterator(pixels_, nx_, ny_, ny_);}
const_line_proxy_iterator line_cbegin() const noexcept
{return const_line_proxy_iterator(pixels_, nx_, ny_, 0);}
const_line_proxy_iterator line_cend() const noexcept
{return const_line_proxy_iterator(pixels_, nx_, ny_, ny_);}
line_range lines() noexcept
{return line_range(this->line_begin(), this->line_end());}
const_line_range lines() const noexcept
{return const_line_range(this->line_cbegin(), this->line_cend());}
private:
std::size_t nx_, ny_;
container_type pixels_;
};
using pbm_image = image< bit_pixel>;
using pgm_image = image<gray_pixel>;
using ppm_image = image< rgb_pixel>;
// --------------------------------------------------------------------------
// __ _ * io functions and operators
// / _| ___ _ __ _ _ _ __ _| |_ * enum class format
// | |_ / _ \| '_/| ` ` \/ _` | _| * operator<<(ostream, image)
// | _| (_) | | | | | | (_| | |_ * operator>>(ostream, image)
// |_| \___/|_| |_|_|_|\__,_|\__| * image read_*(filename)
// * void write_*(filenmae, image, format_flag = ascii)
// --------------------------------------------------------------------------