Skip to content

Commit af22f32

Browse files
committed
add HeifPixelImage::get_channel() with type conversion
1 parent 176c415 commit af22f32

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

libheif/pixelimage.cc

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include <cstring>
2727
#include <utility>
2828

29-
struct complex64 { uint64_t a,b; }; // dummy data structure, 128 bits long
30-
3129

3230
heif_chroma chroma_from_subsampling(int h, int v)
3331
{
@@ -372,36 +370,6 @@ uint8_t HeifPixelImage::get_bits_per_pixel(enum heif_channel channel) const
372370
}
373371

374372

375-
uint8_t* HeifPixelImage::get_plane(enum heif_channel channel, int* out_stride)
376-
{
377-
auto iter = m_planes.find(channel);
378-
if (iter == m_planes.end()) {
379-
return nullptr;
380-
}
381-
382-
if (out_stride) {
383-
*out_stride = iter->second.stride;
384-
}
385-
386-
return static_cast<uint8_t*>(iter->second.mem);
387-
}
388-
389-
390-
const uint8_t* HeifPixelImage::get_plane(enum heif_channel channel, int* out_stride) const
391-
{
392-
auto iter = m_planes.find(channel);
393-
if (iter == m_planes.end()) {
394-
return nullptr;
395-
}
396-
397-
if (out_stride) {
398-
*out_stride = iter->second.stride;
399-
}
400-
401-
return static_cast<uint8_t*>(iter->second.mem);
402-
}
403-
404-
405373
void HeifPixelImage::copy_new_plane_from(const std::shared_ptr<const HeifPixelImage>& src_image,
406374
heif_channel src_channel,
407375
heif_channel dst_channel)
@@ -671,19 +639,20 @@ Error HeifPixelImage::mirror_inplace(heif_transform_mirror_direction direction)
671639
int HeifPixelImage::ImagePlane::get_bytes_per_pixel() const
672640
{
673641
if (m_bit_depth <= 8) {
674-
return 8;
642+
return 1;
675643
}
676644
else if (m_bit_depth <= 16) {
677-
return 16;
645+
return 2;
678646
}
679647
else if (m_bit_depth <= 32) {
680-
return 32;
648+
return 4;
681649
}
682650
else if (m_bit_depth <= 64) {
683-
return 64;
651+
return 8;
684652
}
685653
else {
686-
return 128;
654+
assert(m_bit_depth <= 128);
655+
return 16;
687656
}
688657
}
689658

libheif/pixelimage.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <map>
3232
#include <set>
3333
#include <utility>
34+
#include <cassert>
3435

3536

3637
heif_chroma chroma_from_subsampling(int h, int v);
@@ -46,6 +47,15 @@ bool is_integer_multiple_of_chroma_size(int width,
4647
// Returns the list of valid heif_chroma values for a given colorspace.
4748
std::vector<heif_chroma> get_valid_chroma_values_for_colorspace(heif_colorspace colorspace);
4849

50+
struct complex32 {
51+
float real, imaginary;
52+
};
53+
54+
struct complex64 {
55+
double real, imaginary;
56+
};
57+
58+
4959
class HeifPixelImage : public std::enable_shared_from_this<HeifPixelImage>,
5060
public ErrorBuffer
5161
{
@@ -87,9 +97,32 @@ class HeifPixelImage : public std::enable_shared_from_this<HeifPixelImage>,
8797

8898
uint8_t get_bits_per_pixel(enum heif_channel channel) const;
8999

90-
uint8_t* get_plane(enum heif_channel channel, int* out_stride);
100+
uint8_t* get_plane(enum heif_channel channel, int* out_stride) { return get_channel<uint8_t>(channel, out_stride); }
101+
102+
const uint8_t* get_plane(enum heif_channel channel, int* out_stride) const { return get_channel<uint8_t>(channel, out_stride); }
103+
104+
template <typename T>
105+
T* get_channel(enum heif_channel channel, int* out_stride)
106+
{
107+
auto iter = m_planes.find(channel);
108+
if (iter == m_planes.end()) {
109+
return nullptr;
110+
}
111+
112+
if (out_stride) {
113+
*out_stride = iter->second.stride;
114+
}
91115

92-
const uint8_t* get_plane(enum heif_channel channel, int* out_stride) const;
116+
assert(sizeof(T) == iter->second.get_bytes_per_pixel());
117+
118+
return static_cast<T*>(iter->second.mem);
119+
}
120+
121+
template <typename T>
122+
const T* get_channel(enum heif_channel channel, int* out_stride) const
123+
{
124+
return const_cast<HeifPixelImage*>(this)->get_channel<T>(channel, out_stride);
125+
}
93126

94127
void copy_new_plane_from(const std::shared_ptr<const HeifPixelImage>& src_image,
95128
heif_channel src_channel,

0 commit comments

Comments
 (0)