diff options
Diffstat (limited to 'thirdparty/thekla_atlas/nvimage')
-rw-r--r-- | thirdparty/thekla_atlas/nvimage/BitMap.cpp | 27 | ||||
-rw-r--r-- | thirdparty/thekla_atlas/nvimage/BitMap.h | 87 | ||||
-rw-r--r-- | thirdparty/thekla_atlas/nvimage/Image.cpp | 210 | ||||
-rw-r--r-- | thirdparty/thekla_atlas/nvimage/Image.h | 89 | ||||
-rw-r--r-- | thirdparty/thekla_atlas/nvimage/nvimage.h | 48 |
5 files changed, 461 insertions, 0 deletions
diff --git a/thirdparty/thekla_atlas/nvimage/BitMap.cpp b/thirdparty/thekla_atlas/nvimage/BitMap.cpp new file mode 100644 index 0000000000..8cc49644ea --- /dev/null +++ b/thirdparty/thekla_atlas/nvimage/BitMap.cpp @@ -0,0 +1,27 @@ +// This code is in the public domain -- castanyo@yahoo.es + +#include "BitMap.h" + +using namespace nv; + +void BitMap::resize(uint w, uint h, bool initValue) +{ + BitArray tmp(w*h); + + if (initValue) tmp.setAll(); + else tmp.clearAll(); + + // @@ Copying one bit at a time. This could be much faster. + for (uint y = 0; y < m_height; y++) + { + for (uint x = 0; x < m_width; x++) + { + //tmp.setBitAt(y*w + x, bitAt(x, y)); + if (bitAt(x, y) != initValue) tmp.toggleBitAt(y*w + x); + } + } + + swap(m_bitArray, tmp); + m_width = w; + m_height = h; +} diff --git a/thirdparty/thekla_atlas/nvimage/BitMap.h b/thirdparty/thekla_atlas/nvimage/BitMap.h new file mode 100644 index 0000000000..a285321176 --- /dev/null +++ b/thirdparty/thekla_atlas/nvimage/BitMap.h @@ -0,0 +1,87 @@ +// This code is in the public domain -- castanyo@yahoo.es + +#pragma once +#ifndef NV_IMAGE_BITMAP_H +#define NV_IMAGE_BITMAP_H + +#include "nvimage.h" + +#include "nvcore/BitArray.h" + +namespace nv +{ + /// Bit map. This should probably be called BitImage. + class NVIMAGE_CLASS BitMap + { + public: + BitMap() : m_width(0), m_height(0) {} + BitMap(uint w, uint h) : m_width(w), m_height(h), m_bitArray(w*h) {} + + uint width() const { return m_width; } + uint height() const { return m_height; } + + void resize(uint w, uint h, bool initValue); + + bool bitAt(uint x, uint y) const + { + nvDebugCheck(x < m_width && y < m_height); + return m_bitArray.bitAt(y * m_width + x); + } + bool bitAt(uint idx) const + { + return m_bitArray.bitAt(idx); + } + + void setBitAt(uint x, uint y) + { + nvDebugCheck(x < m_width && y < m_height); + m_bitArray.setBitAt(y * m_width + x); + } + void setBitAt(uint idx) + { + m_bitArray.setBitAt(idx); + } + + void clearBitAt(uint x, uint y) + { + nvDebugCheck(x < m_width && y < m_height); + m_bitArray.clearBitAt(y * m_width + x); + } + void clearBitAt(uint idx) + { + m_bitArray.clearBitAt(idx); + } + + void clearAll() + { + m_bitArray.clearAll(); + } + + void setAll() + { + m_bitArray.setAll(); + } + + void toggleAll() + { + m_bitArray.toggleAll(); + } + + friend void swap(BitMap & a, BitMap & b) + { + nvCheck(a.m_width == b.m_width); + nvCheck(a.m_height == b.m_height); + swap(a.m_bitArray, b.m_bitArray); + } + + private: + + uint m_width; + uint m_height; + BitArray m_bitArray; + + }; + +} // nv namespace + +#endif // NV_IMAGE_BITMAP_H diff --git a/thirdparty/thekla_atlas/nvimage/Image.cpp b/thirdparty/thekla_atlas/nvimage/Image.cpp new file mode 100644 index 0000000000..8c0cbcf4e3 --- /dev/null +++ b/thirdparty/thekla_atlas/nvimage/Image.cpp @@ -0,0 +1,210 @@ +// This code is in the public domain -- castanyo@yahoo.es + +#include "Image.h" +//#include "ImageIO.h" + +#include "nvmath/Color.h" + +#include "nvcore/Debug.h" +#include "nvcore/Ptr.h" +#include "nvcore/Utils.h" // swap +#include "nvcore/Memory.h" // realloc, free + +#include <string.h> // memcpy + + +using namespace nv; + +Image::Image() : m_width(0), m_height(0), m_format(Format_RGB), m_data(NULL) +{ +} + +Image::Image(const Image & img) : m_data(NULL) +{ + allocate(img.m_width, img.m_height, img.m_depth); + m_format = img.m_format; + memcpy(m_data, img.m_data, sizeof(Color32) * m_width * m_height * m_depth); +} + +Image::~Image() +{ + free(); +} + +const Image & Image::operator=(const Image & img) +{ + allocate(img.m_width, img.m_height, m_depth); + m_format = img.m_format; + memcpy(m_data, img.m_data, sizeof(Color32) * m_width * m_height * m_depth); + return *this; +} + + +void Image::allocate(uint w, uint h, uint d/*= 1*/) +{ + m_width = w; + m_height = h; + m_depth = d; + m_data = realloc<Color32>(m_data, w * h * d); +} + +void Image::acquire(Color32 * data, uint w, uint h, uint d/*= 1*/) +{ + free(); + m_width = w; + m_height = h; + m_depth = d; + m_data = data; +} + +void Image::resize(uint w, uint h, uint d/*= 1*/) { + + Image img; + img.allocate(w, h, d); + + Color32 background(0,0,0,0); + + // Copy image. + uint x, y, z; + for(z = 0; z < min(d, m_depth); z++) { + for(y = 0; y < min(h, m_height); y++) { + for(x = 0; x < min(w, m_width); x++) { + img.pixel(x, y, z) = pixel(x, y, z); + } + for(; x < w; x++) { + img.pixel(x, y, z) = background; + } + } + for(; y < h; y++) { + for(x = 0; x < w; x++) { + img.pixel(x, y, z) = background; + } + } + } + for(; z < d; z++) { + for(y = 0; y < h; y++) { + for(x = 0; x < w; x++) { + img.pixel(x, y, z) = background; + } + } + } + + swap(m_width, img.m_width); + swap(m_height, img.m_height); + swap(m_depth, img.m_depth); + swap(m_format, img.m_format); + swap(m_data, img.m_data); +} + +/*bool Image::load(const char * name) +{ + free(); + + AutoPtr<Image> img(ImageIO::load(name)); + if (img == NULL) { + return false; + } + + swap(m_width, img->m_width); + swap(m_height, img->m_height); + swap(m_depth, img->m_depth); + swap(m_format, img->m_format); + swap(m_data, img->m_data); + + return true; +}*/ + +void Image::wrap(void * data, uint w, uint h, uint d) +{ + free(); + m_data = (Color32 *)data; + m_width = w; + m_height = h; + m_depth = d; +} + +void Image::unwrap() +{ + m_data = NULL; + m_width = 0; + m_height = 0; + m_depth = 0; +} + + +void Image::free() +{ + ::free(m_data); + m_data = NULL; +} + + +uint Image::width() const +{ + return m_width; +} + +uint Image::height() const +{ + return m_height; +} + +uint Image::depth() const +{ + return m_depth; +} + +const Color32 * Image::scanline(uint h) const +{ + nvDebugCheck(h < m_height); + return m_data + h * m_width; +} + +Color32 * Image::scanline(uint h) +{ + nvDebugCheck(h < m_height); + return m_data + h * m_width; +} + +const Color32 * Image::pixels() const +{ + return m_data; +} + +Color32 * Image::pixels() +{ + return m_data; +} + +const Color32 & Image::pixel(uint idx) const +{ + nvDebugCheck(idx < m_width * m_height * m_depth); + return m_data[idx]; +} + +Color32 & Image::pixel(uint idx) +{ + nvDebugCheck(idx < m_width * m_height * m_depth); + return m_data[idx]; +} + + +Image::Format Image::format() const +{ + return m_format; +} + +void Image::setFormat(Image::Format f) +{ + m_format = f; +} + +void Image::fill(Color32 c) +{ + const uint size = m_width * m_height * m_depth; + for (uint i = 0; i < size; ++i) + { + m_data[i] = c; + } +} + diff --git a/thirdparty/thekla_atlas/nvimage/Image.h b/thirdparty/thekla_atlas/nvimage/Image.h new file mode 100644 index 0000000000..4c5748cb00 --- /dev/null +++ b/thirdparty/thekla_atlas/nvimage/Image.h @@ -0,0 +1,89 @@ +// This code is in the public domain -- castanyo@yahoo.es + +#pragma once +#ifndef NV_IMAGE_IMAGE_H +#define NV_IMAGE_IMAGE_H + +#include "nvimage.h" +#include "nvcore/Debug.h" + +namespace nv +{ + class Color32; + + /// 32 bit RGBA image. + class NVIMAGE_CLASS Image + { + public: + + enum Format + { + Format_RGB, + Format_ARGB, + }; + + Image(); + Image(const Image & img); + ~Image(); + + const Image & operator=(const Image & img); + + + void allocate(uint w, uint h, uint d = 1); + void acquire(Color32 * data, uint w, uint h, uint d = 1); + //bool load(const char * name); + + void resize(uint w, uint h, uint d = 1); + + void wrap(void * data, uint w, uint h, uint d = 1); + void unwrap(); + + uint width() const; + uint height() const; + uint depth() const; + + const Color32 * scanline(uint h) const; + Color32 * scanline(uint h); + + const Color32 * pixels() const; + Color32 * pixels(); + + const Color32 & pixel(uint idx) const; + Color32 & pixel(uint idx); + + const Color32 & pixel(uint x, uint y, uint z = 0) const; + Color32 & pixel(uint x, uint y, uint z = 0); + + Format format() const; + void setFormat(Format f); + + void fill(Color32 c); + + private: + void free(); + + private: + uint m_width; + uint m_height; + uint m_depth; + Format m_format; + Color32 * m_data; + }; + + + inline const Color32 & Image::pixel(uint x, uint y, uint z) const + { + nvDebugCheck(x < m_width && y < m_height && z < m_depth); + return pixel((z * m_height + y) * m_width + x); + } + + inline Color32 & Image::pixel(uint x, uint y, uint z) + { + nvDebugCheck(x < m_width && y < m_height && z < m_depth); + return pixel((z * m_height + y) * m_width + x); + } + +} // nv namespace + + +#endif // NV_IMAGE_IMAGE_H diff --git a/thirdparty/thekla_atlas/nvimage/nvimage.h b/thirdparty/thekla_atlas/nvimage/nvimage.h new file mode 100644 index 0000000000..5c89bd4726 --- /dev/null +++ b/thirdparty/thekla_atlas/nvimage/nvimage.h @@ -0,0 +1,48 @@ +// This code is in the public domain -- castanyo@yahoo.es + +#pragma once +#ifndef NV_IMAGE_H +#define NV_IMAGE_H + +#include "nvcore/nvcore.h" +#include "nvcore/Debug.h" // nvDebugCheck +#include "nvcore/Utils.h" // isPowerOfTwo + +// Function linkage +#if NVIMAGE_SHARED +#ifdef NVIMAGE_EXPORTS +#define NVIMAGE_API DLL_EXPORT +#define NVIMAGE_CLASS DLL_EXPORT_CLASS +#else +#define NVIMAGE_API DLL_IMPORT +#define NVIMAGE_CLASS DLL_IMPORT +#endif +#else +#define NVIMAGE_API +#define NVIMAGE_CLASS +#endif + + +namespace nv { + + // Some utility functions: + + inline uint computeBitPitch(uint w, uint bitsize, uint alignmentInBits) + { + nvDebugCheck(isPowerOfTwo(alignmentInBits)); + + return ((w * bitsize + alignmentInBits - 1) / alignmentInBits) * alignmentInBits; + } + + inline uint computeBytePitch(uint w, uint bitsize, uint alignmentInBytes) + { + uint pitch = computeBitPitch(w, bitsize, 8*alignmentInBytes); + nvDebugCheck((pitch & 7) == 0); + + return (pitch + 7) / 8; + } + + +} // nv namespace + +#endif // NV_IMAGE_H |