Subversion Repositories tpanel

Rev

Rev 446 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2023 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <cstring>
20
 
21
#include "tbitmap.h"
22
#include "terror.h"
23
 
24
TBitmap::TBitmap()
25
{
26
    DECL_TRACER("TBitmap::TBitmap()");
27
}
28
 
29
TBitmap::TBitmap(const unsigned char* data, size_t size)
30
{
31
    DECL_TRACER("TBitmap::TBitmap(unsigned char* data, size_t size)");
32
 
33
    if (!data || !size)
34
        return;
35
 
36
    mData = new unsigned char[size];
37
    memmove(mData, data, size);
38
    mSize = size;
39
}
40
 
41
TBitmap::TBitmap(const unsigned char* data, int width, int height, int pixsize)
42
{
43
    DECL_TRACER("TBitmap::TBitmap(const unsigned char* data, int width, int height, int pixsize)");
44
 
45
    if (!data || width <= 0 || height <= 0 || pixsize < 1)
46
        return;
47
 
48
    mSize = ((size_t)width * (size_t)pixsize) * (size_t)height;
49
    mData = new unsigned char[mSize];
50
    memmove(mData, data, mSize);
51
    mPixelSize = pixsize;
52
    mPixline = width * pixsize;
53
    mWidth = width;
54
    mHeight = height;
55
}
56
 
57
TBitmap::TBitmap(const TBitmap& bm)
58
{
59
    DECL_TRACER("TBitmap::TBitmap(const TBitmap& bm)");
60
 
61
    if (bm.mSize > 0)
62
    {
63
        mData = new unsigned char[bm.mSize];
64
        memmove(mData, bm.mData, bm.mSize);
65
        mSize = bm.mSize;
66
    }
67
 
68
    mPixline = bm.mPixline;
69
    mPixelSize = bm.mPixelSize;
70
    mWidth = bm.mWidth;
71
    mHeight = bm.mHeight;
72
}
73
 
74
TBitmap::~TBitmap()
75
{
76
    DECL_TRACER("TBitmap::~TBitmap()");
77
 
78
    if (mData)
79
        delete[] mData;
80
 
81
    mData = nullptr;
82
    mSize = 0;
83
}
84
 
85
void TBitmap::setPixline(int pl)
86
{
87
    DECL_TRACER("TBitmap::setPixline(int pl)");
88
 
89
    if (pl < 1 || pl < mPixelSize)
90
        return;
91
 
92
    int width = pl / mPixelSize;
482 andreas 93
    int height = static_cast<int>(mSize) / pl;
446 andreas 94
 
95
    if ((size_t)(height * pl) > mSize)
96
    {
97
        MSG_ERROR("TBitmap::setPixline: Number of pixels exceeds the allocated size of image!");
98
        return;
99
    }
100
 
101
    mPixline = pl;
102
    mWidth = width;
103
    mHeight = height;
104
}
105
 
106
void TBitmap::setBitmap(const unsigned char* data, size_t size)
107
{
108
    DECL_TRACER("TBitmap::setBitmap(unsigned char* data, size_t size)");
109
 
110
    clear();
111
 
112
    if (!data || !size)
113
        return;
114
 
115
    mData = new unsigned char [size];
116
    memmove(mData, data, size);
117
    mSize = size;
118
}
119
 
120
void TBitmap::setBitmap(const unsigned char* data, int width, int height, int pixsize)
121
{
122
    DECL_TRACER("TBitmap::setBitmap(const unsigned char* data, int width, int height, int pixsize)");
123
 
124
    clear();
125
 
126
    if (!data || width <= 0 || height <= 0 || pixsize < 1)
127
        return;
128
 
129
    mSize = ((size_t)width * (size_t)pixsize) * (size_t)height;
130
    mData = new unsigned char[mSize];
131
    memmove(mData, data, mSize);
132
    mPixelSize = pixsize;
133
    mPixline = width * pixsize;
134
    mWidth = width;
135
    mHeight = height;
136
}
137
 
138
unsigned char *TBitmap::getBitmap(size_t* size)
139
{
140
    DECL_TRACER("TBitmap::getBitmap(size_t* size)");
141
 
142
    if (size)
143
        *size = mSize;
144
 
145
    return mData;
146
}
147
 
148
void TBitmap::setWidth(int w)
149
{
150
    DECL_TRACER("TBitmap::setWidth(int w)");
151
 
152
    if (w < 1)
153
        return;
154
 
155
    if ((size_t)(w * mPixelSize * mHeight) > mSize)
156
    {
157
        MSG_ERROR("New width would exceed allocated image size!");
158
        return;
159
    }
160
 
161
    mPixline = w * mPixelSize;
162
    mWidth = w;
482 andreas 163
    mHeight = static_cast<int>(mSize) / mPixline;
446 andreas 164
}
165
 
166
void TBitmap::setHeight(int h)
167
{
168
    DECL_TRACER("TBitmap::setHeight(int h)");
169
 
170
    if (h < 1)
171
        return;
172
 
173
    if ((mSize / mPixline) < (size_t)h)
174
    {
175
        MSG_ERROR("New height would exceed allocated image size!");
176
        return;
177
    }
178
 
179
    mHeight = h;
180
}
181
 
182
void TBitmap::setSize(int w, int h)
183
{
184
    DECL_TRACER("TBitmap::setSize(int w, int h)");
185
 
186
    if (w < 1 || h < 1)
187
        return;
188
 
189
    int pixline = w * mPixelSize;
482 andreas 190
    int maxHeight = static_cast<int>(mSize) / pixline;
446 andreas 191
 
192
    if (h > maxHeight || (size_t)(pixline * h) > mSize)
193
    {
194
        MSG_ERROR("Width and height exceeds allocated image size!");
195
        return;
196
    }
197
 
198
    mPixline = pixline;
199
    mWidth = w;
200
    mHeight = h;
201
}
202
 
203
void TBitmap::setPixelSize(int ps)
204
{
205
    DECL_TRACER("TBitmap::setPixelSize(int ps)");
206
 
207
    if (ps < 1)
208
        return;
209
 
210
    int pixline = mWidth * ps;
211
 
212
    if ((size_t)pixline > mSize)
213
    {
214
        MSG_ERROR("New pixel size would exceed allocated image size!");
215
        return;
216
    }
217
 
218
    mPixelSize = ps;
219
    mPixline = pixline;
482 andreas 220
    mHeight = static_cast<int>(mSize) / pixline;
446 andreas 221
}
222
 
223
bool TBitmap::isValid()
224
{
225
    DECL_TRACER("TBitmap::isValid()");
226
 
227
    if (!mData)         // If there is no data, the content is invalid.
228
        return false;
229
 
230
    // Here we make sure the stored values are totaly valid.
231
    // First we check that there is potential content.
232
    if (mSize > 0 && mPixline > 0 && mPixelSize > 0)    // Content?
233
    {                                                   // Yes, then validate it ...
234
        int pxl = mWidth * mPixelSize;                  // Calculate the pixels per line
235
        size_t s = (size_t)(pxl * mHeight);             // Calculate the minimum size of buffer
236
 
237
        if (pxl == mPixline && s <= mSize)              // Compare the pixels per line and the allocated buffer size
238
            return true;                                // Everything is plausible.
239
    }
240
 
241
    return false;                                       // Some values failed!
242
}
243
 
244
void TBitmap::clear()
245
{
246
    DECL_TRACER("TBitmap::clear()");
247
 
248
    if (mData)
249
    {
250
        delete[] mData;
251
        mData = nullptr;
252
    }
253
 
254
    mSize = 0;
255
    mPixline = mWidth = mHeight = 0;
256
    mPixelSize = 4;
257
}