1// ======================================================================== //
2// Copyright 2009-2019 Intel Corporation //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include "common.h"
20#include "buffer.h"
21
22namespace oidn {
23
24 struct Image
25 {
26 static constexpr int maxSize = 65536;
27
28 char* ptr; // pointer to the first pixel
29 int width; // width in number of pixels
30 int height; // height in number of pixels
31 size_t bytePixelStride; // pixel stride in number of *bytes*
32 size_t rowStride; // row stride in number of *pixel strides*
33 Format format; // pixel format
34 Ref<Buffer> buffer; // buffer containing the image data
35
36 Image() : ptr(nullptr), width(0), height(0), bytePixelStride(0), rowStride(0), format(Format::Undefined) {}
37
38 Image(void* ptr, Format format, int width, int height, size_t byteOffset, size_t inBytePixelStride, size_t inByteRowStride)
39 {
40 if (ptr == nullptr)
41 throw Exception(Error::InvalidArgument, "buffer pointer null");
42
43 init((char*)ptr + byteOffset, format, width, height, inBytePixelStride, inByteRowStride);
44 }
45
46 Image(const Ref<Buffer>& buffer, Format format, int width, int height, size_t byteOffset, size_t inBytePixelStride, size_t inByteRowStride)
47 {
48 init(buffer->data() + byteOffset, format, width, height, inBytePixelStride, inByteRowStride);
49
50 if (byteOffset + height * rowStride * bytePixelStride > buffer->size())
51 throw Exception(Error::InvalidArgument, "buffer region out of range");
52 }
53
54 void init(char* ptr, Format format, int width, int height, size_t inBytePixelStride, size_t inByteRowStride)
55 {
56 assert(width >= 0);
57 assert(height >= 0);
58 if (width > maxSize || height > maxSize)
59 throw Exception(Error::InvalidArgument, "image size too large");
60
61 this->ptr = ptr;
62 this->width = width;
63 this->height = height;
64
65 const size_t pixelSize = getFormatBytes(format);
66 if (inBytePixelStride != 0)
67 {
68 if (inBytePixelStride < pixelSize)
69 throw Exception(Error::InvalidArgument, "pixel stride smaller than pixel size");
70
71 this->bytePixelStride = inBytePixelStride;
72 }
73 else
74 {
75 this->bytePixelStride = pixelSize;
76 }
77
78 if (inByteRowStride != 0)
79 {
80 if (inByteRowStride < width * this->bytePixelStride)
81 throw Exception(Error::InvalidArgument, "row stride smaller than width * pixel stride");
82 if (inByteRowStride % this->bytePixelStride != 0)
83 throw Exception(Error::InvalidArgument, "row stride not integer multiple of pixel stride");
84
85 this->rowStride = inByteRowStride / this->bytePixelStride;
86 }
87 else
88 {
89 this->rowStride = width;
90 }
91
92 this->format = format;
93 }
94
95 __forceinline char* get(int y, int x)
96 {
97 return ptr + ((size_t(y) * rowStride + size_t(x)) * bytePixelStride);
98 }
99
100 __forceinline const char* get(int y, int x) const
101 {
102 return ptr + ((size_t(y) * rowStride + size_t(x)) * bytePixelStride);
103 }
104
105 operator bool() const
106 {
107 return ptr != nullptr;
108 }
109 };
110
111} // namespace oidn
112