-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImgBuffer.cpp
More file actions
155 lines (129 loc) · 3.81 KB
/
ImgBuffer.cpp
File metadata and controls
155 lines (129 loc) · 3.81 KB
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
////////////////////////////////////////////////////////////////////////////////
// MODULE: ImgBuffer.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: MMDevice - Device adapter kit
// AUTHOR: Nenad Amodaj, nenad@amodaj.com
//
// COPYRIGHT: Nenad Amodaj, 2005. All rights reserved.
//
// LICENSE: This file is free for use, modification and distribution and
// is distributed under terms specified in the BSD license
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
//
// NOTE: Imported from ADVI for use in Micro-Manager
///////////////////////////////////////////////////////////////////////////////
#include "ImgBuffer.h"
#include <cassert>
#include <cstring>
ImgBuffer::ImgBuffer(unsigned xSize, unsigned ySize, unsigned pixDepth) :
pixels_(0), width_(xSize), height_(ySize), pixDepth_(pixDepth)
{
pixels_ = new unsigned char[xSize * ySize * pixDepth];
assert(pixels_);
std::memset(pixels_, 0, xSize * ySize * pixDepth);
}
ImgBuffer::ImgBuffer() :
pixels_(0),
width_(0),
height_(0),
pixDepth_(0)
{
}
ImgBuffer::ImgBuffer(const ImgBuffer& right)
{
pixels_ = 0;
*this = right;
}
ImgBuffer::~ImgBuffer()
{
delete[] pixels_;
}
const unsigned char* ImgBuffer::GetPixels() const
{
return pixels_;
}
unsigned char* ImgBuffer::GetPixelsRW()
{
return pixels_;
}
void ImgBuffer::SetPixels(const void* pix)
{
std::memcpy((void*)pixels_, pix, width_ * height_ * pixDepth_);
}
// Set pixels, from a source that has extra bytes at the end of each scanline
// (row).
void ImgBuffer::SetPixelsPadded(const void* pixArray, int paddingBytesPerLine)
{
const char* src = reinterpret_cast<const char*>(pixArray);
char* dst = reinterpret_cast<char*>(pixels_);
const size_t lineSize = width_ * pixDepth_;
for(size_t i = 0; i < height_; i++)
{
std::memcpy(dst, src, lineSize);
src += lineSize + paddingBytesPerLine;
dst += lineSize;
}
}
void ImgBuffer::ResetPixels()
{
if (pixels_)
std::memset(pixels_, 0, width_ * height_ * pixDepth_);
}
bool ImgBuffer::Compatible(const ImgBuffer& img) const
{
if ( Height() != img.Height() ||
Width() != img.Width() ||
Depth() != img.Depth())
return false;
return true;
}
void ImgBuffer::Resize(unsigned xSize, unsigned ySize, unsigned pixDepth)
{
// re-allocate internal buffer if it is not big enough
if (width_ * height_ * pixDepth_ < xSize * ySize * pixDepth)
{
delete[] pixels_;
pixels_ = new unsigned char [xSize * ySize * pixDepth];
assert(pixels_);
}
width_ = xSize;
height_ = ySize;
pixDepth_ = pixDepth;
}
void ImgBuffer::Resize(unsigned xSize, unsigned ySize)
{
// re-allocate internal buffer if it is not big enough
if (width_ * height_ < xSize * ySize)
{
delete[] pixels_;
pixels_ = new unsigned char[xSize * ySize * pixDepth_];
}
width_ = xSize;
height_ = ySize;
std::memset(pixels_, 0, width_ * height_ * pixDepth_);
}
void ImgBuffer::Copy(const ImgBuffer& right)
{
if (!Compatible(right))
Resize(right.width_, right.height_, right.pixDepth_);
SetPixels((void*)right.GetPixels());
}
ImgBuffer& ImgBuffer::operator=(const ImgBuffer& img)
{
if(this == &img)
return *this;
if (pixels_)
delete[] pixels_;
width_ = img.Width();
height_ = img.Height();
pixDepth_ = img.Depth();
pixels_ = new unsigned char[width_ * height_ * pixDepth_];
Copy(img);
return *this;
}