Scroom  0.14
bitmap-helpers.hh
Go to the documentation of this file.
1 /*
2  * Scroom - Generic viewer for 2D data
3  * Copyright (C) 2009-2022 Kees-Jan Dijkzeul
4  *
5  * SPDX-License-Identifier: LGPL-2.1
6  */
7 
8 #pragma once
9 
10 #include <type_traits>
11 
12 #include <boost/operators.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <boost/utility.hpp>
15 
16 #include <cairo.h>
17 
18 #include <scroom/global.hh>
19 
20 namespace Scroom::Bitmap
21 {
23  {
24  public:
25  using Ptr = boost::shared_ptr<BitmapSurface>;
26 
27  private:
28  cairo_surface_t* const surface;
29  boost::shared_ptr<unsigned char> const data;
30 
31  public:
32  static Ptr create(int width, int height, cairo_format_t format);
33  static Ptr create(int width, int height, cairo_format_t format, int stride, boost::shared_ptr<unsigned char> const& data);
34 
36  BitmapSurface(const BitmapSurface&) = delete;
37  BitmapSurface(BitmapSurface&&) = delete;
38  BitmapSurface& operator=(const BitmapSurface&) = delete;
40 
41  cairo_surface_t* get();
42 
43  private:
44  BitmapSurface(int width, int height, cairo_format_t format);
45  BitmapSurface(int width, int height, cairo_format_t format, int stride, boost::shared_ptr<unsigned char> const& data);
46  };
47 
49  // SampleIterator
50 
60  template <typename ConstBase>
61  class SampleIterator : public boost::addable2<SampleIterator<ConstBase>, unsigned int>
62  {
63  public:
64  using Base = typename std::remove_const<ConstBase>::type;
65 
66  static const int bitsPerBase{8 * sizeof(ConstBase) / sizeof(byte)};
67 
68  const int bps;
69  const int samplesPerBase;
70  const int pixelOffset;
71  const ConstBase pixelMask;
72 
73  ConstBase* currentBase;
75 
76  private:
77  static Base mask(int bps) { return (((ConstBase(1) << (bps - 1)) - 1) << 1) | 1; }
78 
79  SampleIterator(div_t d, ConstBase* base, int bps_)
80  : bps(bps_)
82  , pixelOffset(bps)
83  , pixelMask(mask(bps))
84  , currentBase(base + d.quot)
85  , currentOffset(samplesPerBase - 1 - d.rem)
86  {
87  }
88 
89  public:
97  // https://bugs.llvm.org/show_bug.cgi?id=37902
98  // NOLINTNEXTLINE (cppcoreguidelines-pro-type-member-init,hicpp-member-init)
99  explicit SampleIterator(ConstBase* base, int offset = 0, int bps_ = 1)
100  : SampleIterator(div(offset, /* samplesPerBase */ bitsPerBase / bps_), base, bps_)
101  {
102  }
103 
106 
108  void set(ConstBase value)
109  {
111  }
112 
115  {
116  // Prefix operator
117  if(!(currentOffset--))
118  {
120  ++currentBase;
121  }
122 
123  return *this;
124  }
125 
128  {
129  // Postfix operator
130  SampleIterator<ConstBase> result = *this;
131 
132  if(!(currentOffset--))
133  {
135  ++currentBase;
136  }
137 
138  return result;
139  }
140 
142  SampleIterator& operator+=(unsigned int x)
143  {
144  const int offset = samplesPerBase - 1 - currentOffset + x;
145  const div_t d = div(offset, samplesPerBase);
146  currentBase += d.quot;
147  currentOffset = samplesPerBase - 1 - d.rem;
148 
149  return *this;
150  }
151 
154 
155  bool operator==(const SampleIterator<ConstBase>& other) const
156  {
157  return currentBase == other.currentBase && currentOffset == other.currentOffset && bps == other.bps;
158  }
159  };
160 
161 } // namespace Scroom::Bitmap
Scroom::Bitmap::SampleIterator::operator*
Base operator*()
Definition: bitmap-helpers.hh:153
global.hh
Scroom::Bitmap::SampleIterator::set
void set(ConstBase value)
Definition: bitmap-helpers.hh:108
Scroom::Bitmap::SampleIterator
Definition: bitmap-helpers.hh:61
Scroom::Bitmap::SampleIterator::operator+=
SampleIterator & operator+=(unsigned int x)
Definition: bitmap-helpers.hh:142
Scroom::Bitmap::BitmapSurface::data
const boost::shared_ptr< unsigned char > data
Definition: bitmap-helpers.hh:29
Scroom::Bitmap::BitmapSurface::Ptr
boost::shared_ptr< BitmapSurface > Ptr
Definition: bitmap-helpers.hh:25
Scroom::Bitmap::SampleIterator::mask
static Base mask(int bps)
Definition: bitmap-helpers.hh:77
Scroom::Bitmap::BitmapSurface::operator=
BitmapSurface & operator=(const BitmapSurface &)=delete
Scroom::Bitmap::SampleIterator::operator++
SampleIterator & operator++()
Definition: bitmap-helpers.hh:114
Scroom::Bitmap::BitmapSurface::create
static Ptr create(int width, int height, cairo_format_t format)
Definition: bitmap-helpers.cc:13
Scroom::Bitmap::SampleIterator::operator++
SampleIterator operator++(int)
Definition: bitmap-helpers.hh:127
Scroom::Bitmap::SampleIterator::get
Base get()
Definition: bitmap-helpers.hh:105
Scroom::Bitmap::SampleIterator::bps
const int bps
Definition: bitmap-helpers.hh:68
Scroom::Bitmap::SampleIterator::bitsPerBase
static const int bitsPerBase
Definition: bitmap-helpers.hh:66
Scroom::Bitmap::BitmapSurface::surface
cairo_surface_t *const surface
Definition: bitmap-helpers.hh:28
Scroom::Bitmap::SampleIterator::SampleIterator
SampleIterator(div_t d, ConstBase *base, int bps_)
Definition: bitmap-helpers.hh:79
Scroom::Bitmap::SampleIterator::SampleIterator
SampleIterator(ConstBase *base, int offset=0, int bps_=1)
Definition: bitmap-helpers.hh:99
Scroom::Bitmap::SampleIterator::pixelOffset
const int pixelOffset
Definition: bitmap-helpers.hh:70
Scroom::Bitmap::BitmapSurface::get
cairo_surface_t * get()
Definition: bitmap-helpers.cc:26
Scroom::Bitmap::BitmapSurface::~BitmapSurface
~BitmapSurface()
Definition: bitmap-helpers.cc:24
Scroom::Bitmap::SampleIterator::currentOffset
int currentOffset
Definition: bitmap-helpers.hh:74
Scroom::Bitmap::SampleIterator::pixelMask
const ConstBase pixelMask
Definition: bitmap-helpers.hh:71
Scroom::Bitmap::BitmapSurface
Definition: bitmap-helpers.hh:22
Scroom::Bitmap
Definition: bitmap-helpers.hh:20
Scroom::Bitmap::BitmapSurface::BitmapSurface
BitmapSurface(const BitmapSurface &)=delete
Scroom::Bitmap::SampleIterator::Base
typename std::remove_const< ConstBase >::type Base
Definition: bitmap-helpers.hh:64
Scroom::Bitmap::SampleIterator::operator==
bool operator==(const SampleIterator< ConstBase > &other) const
Definition: bitmap-helpers.hh:155
Scroom::Bitmap::SampleIterator::currentBase
ConstBase * currentBase
Definition: bitmap-helpers.hh:73
Scroom::Bitmap::SampleIterator::samplesPerBase
const int samplesPerBase
Definition: bitmap-helpers.hh:69