Add ImageBuffer class

It tries to mitigate issue #239, but it's not a full fix yet. A good
possibility would be to change the internal structure of images, creating
tiles. So we don't need to allocate/copy/clear/etc. huge images in each
step.
This commit is contained in:
David Capello 2013-11-10 13:26:48 -03:00
parent 5db38c4a87
commit 48864b440b
7 changed files with 119 additions and 34 deletions

View File

@ -22,6 +22,7 @@
#include "app/util/expand_cel_canvas.h"
#include "app/app.h"
#include "app/context.h"
#include "app/document.h"
#include "app/document_location.h"
@ -39,6 +40,32 @@
#include "raster/sprite.h"
#include "raster/stock.h"
namespace {
static raster::ImageBufferPtr cel_buffer;
static raster::ImageBufferPtr src_buffer;
static raster::ImageBufferPtr dst_buffer;
static void destroy_buffers()
{
cel_buffer.reset(NULL);
src_buffer.reset(NULL);
dst_buffer.reset(NULL);
}
static void create_buffers()
{
if (!cel_buffer) {
app::App::instance()->Exit.connect(&destroy_buffers);
cel_buffer.reset(new raster::ImageBuffer(1));
src_buffer.reset(new raster::ImageBuffer(1));
dst_buffer.reset(new raster::ImageBuffer(1));
}
}
}
namespace app {
ExpandCelCanvas::ExpandCelCanvas(Context* context, TiledMode tiledMode, UndoTransaction& undo)
@ -49,6 +76,8 @@ ExpandCelCanvas::ExpandCelCanvas(Context* context, TiledMode tiledMode, UndoTran
, m_committed(false)
, m_undo(undo)
{
create_buffers();
DocumentLocation location = context->getActiveLocation();
m_document = location.document();
m_sprite = location.sprite();
@ -63,7 +92,8 @@ ExpandCelCanvas::ExpandCelCanvas(Context* context, TiledMode tiledMode, UndoTran
// If there is no Cel
if (m_cel == NULL) {
// Create the image
m_celImage = Image::create(m_sprite->getPixelFormat(), m_sprite->getWidth(), m_sprite->getHeight());
m_celImage = Image::create(m_sprite->getPixelFormat(), m_sprite->getWidth(),
m_sprite->getHeight(), cel_buffer);
clear_image(m_celImage, m_sprite->getTransparentColor());
// Create the cel
@ -96,9 +126,11 @@ ExpandCelCanvas::ExpandCelCanvas(Context* context, TiledMode tiledMode, UndoTran
m_srcImage = crop_image(m_celImage,
x1-m_cel->getX(),
y1-m_cel->getY(), x2-x1, y2-y1,
m_sprite->getTransparentColor());
m_sprite->getTransparentColor(),
src_buffer);
m_dstImage = Image::createCopy(m_srcImage);
m_dstImage = Image::createCopy(m_srcImage,
dst_buffer);
// We have to adjust the cel position to match the m_dstImage
// position (the new m_dstImage will be used in RenderEngine to

View File

@ -61,22 +61,23 @@ int Image::getRowStrideSize(int pixels_per_row) const
}
// static
Image* Image::create(PixelFormat format, int width, int height)
Image* Image::create(PixelFormat format, int width, int height,
const ImageBufferPtr& buffer)
{
switch (format) {
case IMAGE_RGB: return new ImageImpl<RgbTraits>(width, height);
case IMAGE_GRAYSCALE: return new ImageImpl<GrayscaleTraits>(width, height);
case IMAGE_INDEXED: return new ImageImpl<IndexedTraits>(width, height);
case IMAGE_BITMAP: return new ImageImpl<BitmapTraits>(width, height);
case IMAGE_RGB: return new ImageImpl<RgbTraits>(width, height, buffer);
case IMAGE_GRAYSCALE: return new ImageImpl<GrayscaleTraits>(width, height, buffer);
case IMAGE_INDEXED: return new ImageImpl<IndexedTraits>(width, height, buffer);
case IMAGE_BITMAP: return new ImageImpl<BitmapTraits>(width, height, buffer);
}
return NULL;
}
// static
Image* Image::createCopy(const Image* image)
Image* Image::createCopy(const Image* image, const ImageBufferPtr& buffer)
{
ASSERT(image);
return crop_image(image, 0, 0, image->getWidth(), image->getHeight(), 0);
return crop_image(image, 0, 0, image->getWidth(), image->getHeight(), 0, buffer);
}
} // namespace raster

View File

@ -24,6 +24,7 @@
#include "gfx/size.h"
#include "raster/blend.h"
#include "raster/color.h"
#include "raster/image_buffer.h"
#include "raster/object.h"
#include "raster/pixel_format.h"
@ -42,8 +43,10 @@ namespace raster {
ReadWriteLock // Read and write
};
static Image* create(PixelFormat format, int width, int height);
static Image* createCopy(const Image* image);
static Image* create(PixelFormat format, int width, int height,
const ImageBufferPtr& buffer = ImageBufferPtr());
static Image* createCopy(const Image* image,
const ImageBufferPtr& buffer = ImageBufferPtr());
virtual ~Image();

49
src/raster/image_buffer.h Normal file
View File

@ -0,0 +1,49 @@
/* Aseprite
* Copyright (C) 2001-2013 David Capello
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef RASTER_IMAGE_BUFFER_H_INCLUDED
#define RASTER_IMAGE_BUFFER_H_INCLUDED
#include "base/shared_ptr.h"
#include <vector>
namespace raster {
class ImageBuffer {
public:
ImageBuffer(size_t size) : m_buffer(size) {
}
size_t size() const { return m_buffer.size(); }
uint8_t* buffer() { return &m_buffer[0]; }
void resizeIfNecessary(size_t size) {
if (size > m_buffer.size())
m_buffer.resize(size);
}
private:
std::vector<uint8_t> m_buffer;
};
typedef SharedPtr<ImageBuffer> ImageBufferPtr;
} // namespace raster
#endif

View File

@ -33,8 +33,9 @@ namespace raster {
typedef typename Traits::address_t address_t;
typedef typename Traits::const_address_t const_address_t;
address_t m_bits; // Pixmap data.
address_t* m_rows; // Start of each scanline.
ImageBufferPtr m_buffer;
address_t m_bits;
address_t* m_rows;
inline address_t address(int x, int y) const {
return (address_t)(m_rows[y] + x / (Traits::pixels_per_byte == 0 ? 1 : Traits::pixels_per_byte));
@ -59,32 +60,30 @@ namespace raster {
}
public:
ImageImpl(int width, int height)
ImageImpl(int width, int height,
const ImageBufferPtr& buffer)
: Image(static_cast<PixelFormat>(Traits::pixel_format), width, height)
, m_buffer(buffer)
{
int rowstrideBytes = Traits::getRowStrideBytes(width);
size_t for_rows = sizeof(address_t) * height;
size_t rowstride_bytes = Traits::getRowStrideBytes(width);
size_t required_size = for_rows + rowstride_bytes*height;
m_bits = (address_t)new uint8_t[rowstrideBytes * height];
try {
m_rows = new address_t[height];
}
catch (...) {
delete[] m_bits;
throw;
}
if (!m_buffer)
m_buffer.reset(new ImageBuffer(required_size));
else
m_buffer->resizeIfNecessary(required_size);
m_rows = (address_t*)m_buffer->buffer();
m_bits = (address_t)(m_buffer->buffer() + for_rows);
address_t addr = m_bits;
for (int y=0; y<getHeight(); ++y) {
for (int y=0; y<height; ++y) {
m_rows[y] = addr;
addr = (address_t)(((uint8_t*)addr) + rowstrideBytes);
addr = (address_t)(((uint8_t*)addr) + rowstride_bytes);
}
}
~ImageImpl() {
if (m_bits) delete[] m_bits;
if (m_rows) delete[] m_rows;
}
uint8_t* getPixelAddress(int x, int y) const OVERRIDE {
ASSERT(x >= 0 && x < getWidth());
ASSERT(y >= 0 && y < getHeight());

View File

@ -87,12 +87,12 @@ void composite_image(Image* dst, const Image* src, int x, int y, int opacity, in
dst->merge(src, x, y, opacity, blend_mode);
}
Image* crop_image(const Image* image, int x, int y, int w, int h, color_t bg)
Image* crop_image(const Image* image, int x, int y, int w, int h, color_t bg, const ImageBufferPtr& buffer)
{
if (w < 1) throw std::invalid_argument("image_crop: Width is less than 1");
if (h < 1) throw std::invalid_argument("image_crop: Height is less than 1");
Image* trim = Image::create(image->getPixelFormat(), w, h);
Image* trim = Image::create(image->getPixelFormat(), w, h, buffer);
trim->setMaskColor(image->getMaskColor());
clear_image(trim, bg);

View File

@ -20,6 +20,7 @@
#define RASTER_PRIMITIVES_H_INCLUDED
#include "raster/color.h"
#include "raster/image_buffer.h"
namespace raster {
class Image;
@ -35,7 +36,7 @@ namespace raster {
void copy_image(Image* dst, const Image* src, int x, int y);
void composite_image(Image* dst, const Image* src, int x, int y, int opacity, int blend_mode);
Image* crop_image(const Image* image, int x, int y, int w, int h, color_t bg);
Image* crop_image(const Image* image, int x, int y, int w, int h, color_t bg, const ImageBufferPtr& buffer = ImageBufferPtr());
void rotate_image(const Image* src, Image* dst, int angle);
void draw_hline(Image* image, int x1, int y, int x2, color_t c);