Subversion Repositories tpanel

Rev

Rev 284 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 284 Rev 289
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
2
 * Copyright (C) 2023 by Andreas Theofilu <andreas@theosys.at>
3
 *
3
 *
4
 * This program is free software; you can redistribute it and/or modify
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
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
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
7
 * (at your option) any later version.
Line 14... Line 14...
14
 * You should have received a copy of the GNU General Public License
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,
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
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
17
 */
18
 
18
 
19
#include <string.h>
19
#include <cstring>
20
 
20
 
21
#include "tbitmap.h"
21
#include "tbitmap.h"
22
#include "terror.h"
22
#include "terror.h"
23
 
23
 
24
TBitmap::TBitmap()
24
TBitmap::TBitmap()
Line 56... Line 56...
56
 
56
 
57
TBitmap::TBitmap(const TBitmap& bm)
57
TBitmap::TBitmap(const TBitmap& bm)
58
{
58
{
59
    DECL_TRACER("TBitmap::TBitmap(const TBitmap& bm)");
59
    DECL_TRACER("TBitmap::TBitmap(const TBitmap& bm)");
60
 
60
 
61
    if (mData)
-
 
62
    {
-
 
63
        delete[] mData;
-
 
64
        mData = nullptr;
-
 
65
        mSize = 0;
-
 
66
    }
-
 
67
 
-
 
68
    if (bm.mSize > 0)
61
    if (bm.mSize > 0)
69
    {
62
    {
70
        mData = new unsigned char[bm.mSize];
63
        mData = new unsigned char[bm.mSize];
71
        memmove(mData, bm.mData, bm.mSize);
64
        memmove(mData, bm.mData, bm.mSize);
72
        mSize = bm.mSize;
65
        mSize = bm.mSize;
Line 99... Line 92...
99
    int width = pl / mPixelSize;
92
    int width = pl / mPixelSize;
100
    int height = mSize / pl;
93
    int height = mSize / pl;
101
 
94
 
102
    if ((size_t)(height * pl) > mSize)
95
    if ((size_t)(height * pl) > mSize)
103
    {
96
    {
104
        MSG_ERROR("Number of pixels exceeds the allocated size of image!");
97
        MSG_ERROR("TBitmap::setPixline: Number of pixels exceeds the allocated size of image!");
105
        return;
98
        return;
106
    }
99
    }
107
 
100
 
108
    mPixline = pl;
101
    mPixline = pl;
109
    mWidth = width;
102
    mWidth = width;
Line 112... Line 105...
112
 
105
 
113
void TBitmap::setBitmap(const unsigned char* data, size_t size)
106
void TBitmap::setBitmap(const unsigned char* data, size_t size)
114
{
107
{
115
    DECL_TRACER("TBitmap::setBitmap(unsigned char* data, size_t size)");
108
    DECL_TRACER("TBitmap::setBitmap(unsigned char* data, size_t size)");
116
 
109
 
-
 
110
    clear();
-
 
111
 
117
    if (!data || !size)
112
    if (!data || !size)
118
        return;
113
        return;
119
 
114
 
120
    if (mData)
-
 
121
        delete[] mData;
-
 
122
 
-
 
123
    mData = new unsigned char [size];
115
    mData = new unsigned char [size];
124
    memmove(mData, data, size);
116
    memmove(mData, data, size);
125
    mSize = size;
117
    mSize = size;
126
}
118
}
127
 
119
 
128
void TBitmap::setBitmap(const unsigned char* data, int width, int height, int pixsize)
120
void TBitmap::setBitmap(const unsigned char* data, int width, int height, int pixsize)
129
{
121
{
130
    DECL_TRACER("TBitmap::setBitmap(const unsigned char* data, int width, int height, int pixsize)");
122
    DECL_TRACER("TBitmap::setBitmap(const unsigned char* data, int width, int height, int pixsize)");
131
 
123
 
-
 
124
    clear();
-
 
125
 
132
    if (!data || width <= 0 || height <= 0 || pixsize < 1)
126
    if (!data || width <= 0 || height <= 0 || pixsize < 1)
133
        return;
127
        return;
134
 
128
 
135
    if (mData)
-
 
136
        delete[] mData;
-
 
137
 
-
 
138
    mSize = (width * pixsize) * height;
129
    mSize = (width * pixsize) * height;
139
    mData = new unsigned char[mSize];
130
    mData = new unsigned char[mSize];
140
    memmove(mData, data, mSize);
131
    memmove(mData, data, mSize);
141
    mPixelSize = pixsize;
132
    mPixelSize = pixsize;
142
    mPixline = width * pixsize;
133
    mPixline = width * pixsize;
Line 226... Line 217...
226
 
217
 
227
    mPixelSize = ps;
218
    mPixelSize = ps;
228
    mPixline = pixline;
219
    mPixline = pixline;
229
    mHeight = mSize / pixline;
220
    mHeight = mSize / pixline;
230
}
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
}