Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified Image class so you can init an image with ImageData without copying the data #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions oxygine/src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,22 @@ namespace oxygine

void Image::fill_zero()
{
if (_buffer.empty())
if (_image.data == nullptr)
return;
memset(&_buffer.front(), 0, _buffer.size());

int size = _image.pitch * _image.h;

memset(_image.data, 0, size);
}

void Image::fill(unsigned int val)
{
if (_buffer.empty())
if (_image.data == nullptr)
return;
memset(&_buffer.front(), val, _buffer.size());

int size = _image.pitch * _image.h;

memset(_image.data, val, size);
}

bool Image::init(file::buffer& buffer, bool premultiplied, TextureFormat format)
Expand Down Expand Up @@ -593,6 +599,7 @@ namespace oxygine
_offset = sizeof(pkm_header);
_image.pitch = int(buffer.getSize() - _offset) / _image.h;
_buffer.swap(buffer.data);
_image.data = &_buffer.front();
}
return true;
case IT_PVR:
Expand Down Expand Up @@ -631,6 +638,7 @@ namespace oxygine
_offset = sizeof(*header) + header->meta_data_size;
_image.pitch = int(buffer.getSize() - _offset) / _image.h;
_buffer.swap(buffer.data);
_image.data = &_buffer.front();
return true;
}
break;
Expand Down Expand Up @@ -676,6 +684,7 @@ namespace oxygine
_offset = sizeof(*header);
_image.pitch = int(buffer.getSize() - _offset) / _image.h;
_buffer.swap(buffer.data);
_image.data = &_buffer.front();
return true;

}
Expand Down Expand Up @@ -740,10 +749,18 @@ namespace oxygine
return false;
}

void Image::init(const ImageData& src)
void Image::init(const ImageData& src, bool copyImageData)
{
init(src.w, src.h, src.format);
updateRegion(0, 0, src);
if(copyImageData)
{
init(src.w, src.h, src.format);
updateRegion(0, 0, src);
}
else
{
_buffer.clear();
_image = src;
}
}

void Image::init(int w, int h, TextureFormat Format)
Expand Down Expand Up @@ -788,9 +805,9 @@ namespace oxygine
OX_ASSERT(rect.getY() + rect.getHeight() <= _image.h);
}

ImageData im = _image;
OX_ASSERT(_buffer.size() < 1 || _image.data == &_buffer.front());

void* ptr = &_buffer.front() + rect.getX() * _image.bytespp + rect.getY() * _image.pitch + _offset;
void* ptr = _image.data + rect.getX() * _image.bytespp + rect.getY() * _image.pitch + _offset;

return ImageData(rect.getWidth(), rect.getHeight(), _image.pitch, _image.format, ptr);
}
Expand Down Expand Up @@ -849,5 +866,7 @@ namespace oxygine
r._image = copy;

std::swap(_buffer, r._buffer);

_image.data = &_buffer.front();
}
}
6 changes: 3 additions & 3 deletions oxygine/src/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace oxygine
~Image();

bool init(file::buffer& bf, bool premultiplied = false, TextureFormat format = TF_UNDEFINED);
void init(const ImageData& src);
void init(const ImageData& src, bool copyImageData = true);
void init(int w, int h, TextureFormat Format);

void cleanup();
Expand All @@ -40,7 +40,7 @@ namespace oxygine
void fillZero() { fill(0); }
void fill(unsigned int val);

unsigned int getSizeVRAM() const {return (unsigned int)_buffer.size();}
unsigned int getSizeVRAM() const {return (unsigned int) _image.pitch * _image.h;}
int getWidth() const;
int getHeight() const;
const Point& getSize() const;
Expand Down Expand Up @@ -70,4 +70,4 @@ namespace oxygine
void setJpegImageLoader(cbLoadImageFromBuffer);
void setPngImageLoader(cbLoadImageFromBuffer);
void setCustomImageLoader(cbLoadImageFromBuffer);
}
}