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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* The Plain Old Data encapsulation of pixel, raster data.
* Copyright (C) 2005 - 2015 René Rebe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2. A copy of the GNU General
* Public License can be found in the file LICENSE.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
* ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* Alternatively, commercial licensing options are available from the
* copyright holder ExactCODE GmbH Germany.
*/
#include <string.h> // memcpy
#include <iostream>
#define DEPRECATED
#include "Image.hh"
#include "Codecs.hh"
Image::Image ()
: modified(false), meta_modified(false), xres(0), yres(0), codec(0), data(0), w(0), h(0), bps(0), spp(0), rowstride(0)
{
}
Image::Image (Image& other)
: modified(false), meta_modified(false), xres(0), yres(0), codec(0), data(0), w(0), h(0), bps(0), spp(0), rowstride(0)
{
operator= (other);
}
Image::~Image () {
// release attached codec
if (codec)
delete (codec); codec = 0;
// release POD
if (data)
free (data); data = 0;
}
void Image::copyMeta (const Image& other)
{
w = other.w;
h = other.h;
bps = other.bps;
spp = other.spp;
rowstride = other.rowstride;
xres = other.xres;
yres = other.yres;
}
Image& Image::operator= (const Image& other)
{
uint8_t* d = other.getRawData();
copyMeta (other);
resize (w, h, rowstride); // allocate
if (d && data) {
memcpy (data, d, stride() * h);
}
setRawData();
return *this;
}
void Image::copyTransferOwnership (Image& other)
{
copyMeta (other);
uint8_t* d = other.getRawData();
other.setRawDataWithoutDelete (0);
setRawData (d);
}
uint8_t* Image::getRawData () const {
// ask codec about it
if (!data && codec) {
Image* image = const_cast<Image*>(this);
codec->decodeNow (image);
if (data) // if data was added
image->modified = false;
}
return data;
}
uint8_t* Image::getRawDataEnd () const {
// we call getRawData as it might have to query the codec to actually load it
return getRawData() + h * stride();
}
void Image::setRawData () {
if (!modified) {
// DEBUG
// std::cerr << "Image modified" << std::endl;
modified = true;
}
}
void Image::setRawData (uint8_t* _data) {
if (_data != data && data) {
free (data);
data = 0;
}
// reuse:
setRawDataWithoutDelete (_data);
}
void Image::setRawDataWithoutDelete (uint8_t* _data) {
data = _data;
// reuse
setRawData ();
}
void Image::resize (int _w, int _h, unsigned _stride) {
std::swap(w, _w);
std::swap(h, _h);
std::swap(rowstride, _stride);
uint8_t* ptr = (uint8_t*)::realloc(data, stride() * h);
if (ptr) {
setRawDataWithoutDelete(ptr);
} else {
// if non-zero size
if (w * h != 0) {
// restore
w = _w;
h = _h;
rowstride = _stride;
throw std::bad_alloc();
}
}
}
void Image::realloc () {
if (!data)
return; // not yet loaded, delayed, no-op
#if !defined(__APPLE__)
resize(w, h); // realloc, supposed to shrink
#else
// the OS allocator may not shrink the buffer, though that may be important for the application, ...
uint8_t* newdata = (uint8_t*)malloc(stride() * h);
if (newdata) {
memcpy(newdata, data, stride() * h);
setRawData(newdata);
} else {
resize(w, h); // at least try realloc, ...
}
#endif
}
void Image::setDecoderID (const std::string& id) {
decoderID = id;
}
const std::string& Image::getDecoderID () {
return decoderID;
}
ImageCodec* Image::getCodec() {
return codec;
}
void Image::setCodec (ImageCodec* _codec) {
// do not reset, nor free when the same codec is re-sety
if (codec == _codec)
return;
// release attached codec
if (codec) {
delete (codec);
}
codec = _codec;
// at the moment the codec is attached the data recent, 0 on detach
if (codec) {
modified = meta_modified = false;
}
}
|