Scroom 0.14-48-ga0fee447
Loading...
Searching...
No Matches
bitmap-helpers.hh
Go to the documentation of this file.
1/*
2 * Scroom - Generic viewer for 2D data
3 * Copyright (C) 2009-2026 Kees-Jan Dijkzeul
4 *
5 * SPDX-License-Identifier: LGPL-2.1
6 */
7
8#pragma once
9
10#include <memory>
11#include <type_traits>
12
13#include <boost/operators.hpp>
14#include <boost/utility.hpp>
15
16#include <cairo.h>
17
18#include <scroom/global.hh>
19
21{
23 {
24 public:
25 using Ptr = std::shared_ptr<BitmapSurface>;
26
27 private:
28 cairo_surface_t* const surface;
29 std::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, std::shared_ptr<unsigned char> const& data);
34
36 BitmapSurface(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, std::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 = std::remove_const_t<ConstBase>;
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_)
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
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 bool operator!=(const SampleIterator<ConstBase>& other) const { return !(*this == other); }
161 };
162
163} // namespace Scroom::Bitmap
const uint8_t value
Definition blob-tests.cc:114
Definition bitmap-helpers.hh:23
BitmapSurface(const BitmapSurface &)=delete
std::shared_ptr< unsigned char > const data
Definition bitmap-helpers.hh:29
std::shared_ptr< BitmapSurface > Ptr
Definition bitmap-helpers.hh:25
cairo_surface_t * get()
Definition bitmap-helpers.cc:26
cairo_surface_t *const surface
Definition bitmap-helpers.hh:28
BitmapSurface(BitmapSurface &&)=delete
static Ptr create(int width, int height, cairo_format_t format)
Definition bitmap-helpers.cc:13
BitmapSurface & operator=(const BitmapSurface &)=delete
~BitmapSurface()
Definition bitmap-helpers.cc:24
BitmapSurface & operator=(BitmapSurface &&)=delete
Definition bitmap-helpers.hh:62
Base get()
Definition bitmap-helpers.hh:105
bool operator!=(const SampleIterator< ConstBase > &other) const
Definition bitmap-helpers.hh:160
static const int bitsPerBase
Definition bitmap-helpers.hh:66
const int bps
Definition bitmap-helpers.hh:68
void set(ConstBase value)
Definition bitmap-helpers.hh:108
int currentOffset
Definition bitmap-helpers.hh:74
const int pixelOffset
Definition bitmap-helpers.hh:70
SampleIterator operator++(int)
Definition bitmap-helpers.hh:127
SampleIterator & operator++()
Definition bitmap-helpers.hh:114
static Base mask(int bps)
Definition bitmap-helpers.hh:77
const ConstBase pixelMask
Definition bitmap-helpers.hh:71
SampleIterator(div_t d, ConstBase *base, int bps_)
Definition bitmap-helpers.hh:79
SampleIterator & operator+=(unsigned int x)
Definition bitmap-helpers.hh:142
Base operator*()
Definition bitmap-helpers.hh:153
SampleIterator(ConstBase *base, int offset=0, int bps_=1)
Definition bitmap-helpers.hh:99
ConstBase * currentBase
Definition bitmap-helpers.hh:73
const int samplesPerBase
Definition bitmap-helpers.hh:69
std::remove_const_t< ConstBase > Base
Definition bitmap-helpers.hh:64
bool operator==(const SampleIterator< ConstBase > &other) const
Definition bitmap-helpers.hh:155
Definition bitmap-helpers.hh:21
auto mask
Definition ruler-tests.cc:21
SampleIterator< const uint8_t > result
Definition sampleiterator-tests.cc:94