Skip to content

Commit

Permalink
Replace doc::Stock with doc::ImageRef shared pointer
Browse files Browse the repository at this point in the history
Changes:
* Add doc::ImageRef to count references to the same image between Cels
  (at this moment we cannot generate linked cels anyway)
* Remove doc:Stock class and doc::Sprite::m_stock member variable
* Remove app::undoers::Add/RemoveImage
* Add doc::SubObjectsIO and app::undoers::ObjectIO to
  replace doc::LayerSubObjectsSerializer
  • Loading branch information
dacap committed Jan 4, 2015
1 parent 1b55fe3 commit f1f24cb
Show file tree
Hide file tree
Showing 61 changed files with 514 additions and 953 deletions.
3 changes: 1 addition & 2 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ add_library(app-lib
undo_transaction.cpp
undoers/add_cel.cpp
undoers/add_frame.cpp
undoers/add_image.cpp
undoers/add_layer.cpp
undoers/add_palette.cpp
undoers/close_group.cpp
Expand All @@ -274,11 +273,11 @@ add_library(app-lib
undoers/image_area.cpp
undoers/modified_region.cpp
undoers/move_layer.cpp
undoers/object_io.cpp
undoers/open_group.cpp
undoers/remap_palette.cpp
undoers/remove_cel.cpp
undoers/remove_frame.cpp
undoers/remove_image.cpp
undoers/remove_layer.cpp
undoers/remove_palette.cpp
undoers/replace_image.cpp
Expand Down
1 change: 0 additions & 1 deletion src/app/commands/cmd_cel_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "doc/image.h"
#include "doc/layer.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/ui.h"

namespace app {
Expand Down
9 changes: 2 additions & 7 deletions src/app/commands/cmd_export_sprite_sheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include "doc/palette.h"
#include "doc/primitives.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/ui.h"

#include "generated_export_sprite_sheet.h"
Expand Down Expand Up @@ -327,7 +326,7 @@ void ExportSpriteSheetCommand::onExecute(Context* context)
if (sheet_h == 0) sheet_h = sprite->height()*((nframes/columns)+((nframes%columns)>0?1:0));
columns = sheet_w / sprite->width();

base::UniquePtr<Image> resultImage(Image::create(sprite->pixelFormat(), sheet_w, sheet_h));
ImageRef resultImage(Image::create(sprite->pixelFormat(), sheet_w, sheet_h));
doc::clear_image(resultImage, 0);

render::Render render;
Expand Down Expand Up @@ -360,12 +359,8 @@ void ExportSpriteSheetCommand::onExecute(Context* context)
// Add the layer in the sprite.
LayerImage* resultLayer = api.newLayer(sprite);

// Add the image into the sprite's stock
int indexInStock = api.addImageInStock(sprite, resultImage);
resultImage.release();

// Create the cel.
base::UniquePtr<Cel> resultCel(new Cel(frame_t(0), indexInStock));
base::UniquePtr<Cel> resultCel(new Cel(frame_t(0), resultImage));

// Add the cel in the layer.
api.addCel(resultLayer, resultCel);
Expand Down
1 change: 0 additions & 1 deletion src/app/commands/cmd_flip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "doc/layer.h"
#include "doc/mask.h"
#include "doc/sprite.h"
#include "doc/stock.h"

namespace app {

Expand Down
16 changes: 3 additions & 13 deletions src/app/commands/cmd_import_sprite_sheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include "doc/palette.h"
#include "doc/primitives.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/ui.h"

namespace app {
Expand Down Expand Up @@ -160,7 +159,7 @@ class ImportSpriteSheetWindow : public Window,
}

// The list of frames imported from the sheet
std::vector<Image*> animation;
std::vector<ImageRef> animation;

try {
Sprite* sprite = m_document->sprite();
Expand All @@ -170,15 +169,14 @@ class ImportSpriteSheetWindow : public Window,
// As first step, we cut each tile and add them into "animation" list.
for (int y=m_rect.y; y<sprite->height(); y += m_rect.h) {
for (int x=m_rect.x; x<sprite->width(); x += m_rect.w) {
base::UniquePtr<Image> resultImage(
ImageRef resultImage(
Image::create(sprite->pixelFormat(), m_rect.w, m_rect.h));

// Render the portion of sheet.
render.renderSprite(resultImage, sprite, currentFrame,
gfx::Clip(0, 0, x, y, m_rect.w, m_rect.h));

animation.push_back(resultImage);
resultImage.release();
}
}

Expand All @@ -201,14 +199,8 @@ class ImportSpriteSheetWindow : public Window,

// Add all frames+cels to the new layer
for (size_t i=0; i<animation.size(); ++i) {
int indexInStock;

// Add the image into the sprite's stock
indexInStock = api.addImageInStock(sprite, animation[i]);
animation[i] = NULL;

// Create the cel.
base::UniquePtr<Cel> resultCel(new Cel(frame_t(i), indexInStock));
base::UniquePtr<Cel> resultCel(new Cel(frame_t(i), animation[i]));

// Add the cel in the layer.
api.addCel(resultLayer, resultCel);
Expand All @@ -233,8 +225,6 @@ class ImportSpriteSheetWindow : public Window,
undoTransaction.commit();
}
catch (...) {
for (size_t i=0; i<animation.size(); ++i)
delete animation[i];
throw;
}

Expand Down
36 changes: 12 additions & 24 deletions src/app/commands/cmd_merge_down_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "app/modules/gui.h"
#include "app/undo_transaction.h"
#include "app/undoers/add_cel.h"
#include "app/undoers/add_image.h"
#include "app/undoers/remove_layer.h"
#include "app/undoers/replace_image.h"
#include "app/undoers/set_cel_position.h"
Expand All @@ -38,7 +37,6 @@
#include "doc/layer.h"
#include "doc/primitives.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "render/render.h"
#include "ui/ui.h"

Expand Down Expand Up @@ -87,7 +85,6 @@ void MergeDownLayerCommand::onExecute(Context* context)
UndoTransaction undo(writer.context(), "Merge Down Layer", undo::ModifyDocument);
Layer* src_layer = writer.layer();
Layer* dst_layer = src_layer->getPrevious();
int index;

for (frame_t frpos = 0; frpos<sprite->totalFrames(); ++frpos) {
// Get frames
Expand All @@ -101,29 +98,21 @@ void MergeDownLayerCommand::onExecute(Context* context)
else
src_image = NULL;

Image* dst_image;
if (dst_cel != NULL)
dst_image = dst_cel->image();
else
dst_image = NULL;
ImageRef dst_image;
if (dst_cel)
dst_image = dst_cel->imageRef();

// With source image?
if (src_image != NULL) {
if (src_image) {
// No destination image
if (dst_image == NULL) { // Only a transparent layer can have a null cel
// Copy this cel to the destination layer...

// Creating a copy of the image
dst_image = Image::createCopy(src_image);

// Adding it in the stock of images
index = sprite->stock()->addImage(dst_image);
if (undo.isEnabled())
undo.pushUndoer(new undoers::AddImage(
undo.getObjects(), sprite->stock(), index));
dst_image.reset(Image::createCopy(src_image));

// Creating a copy of the cell
dst_cel = new Cel(frpos, index);
dst_cel = new Cel(frpos, dst_image);
dst_cel->setPosition(src_cel->x(), src_cel->y());
dst_cel->setOpacity(src_cel->opacity());

Expand Down Expand Up @@ -153,10 +142,10 @@ void MergeDownLayerCommand::onExecute(Context* context)

doc::color_t bgcolor = app_get_color_to_clear_layer(dst_layer);

Image* new_image = doc::crop_image(dst_image,
x1-dst_cel->x(),
y1-dst_cel->y(),
x2-x1+1, y2-y1+1, bgcolor);
ImageRef new_image(doc::crop_image(dst_image,
x1-dst_cel->x(),
y1-dst_cel->y(),
x2-x1+1, y2-y1+1, bgcolor));

// Merge src_image in new_image
render::composite_image(new_image, src_image,
Expand All @@ -172,10 +161,9 @@ void MergeDownLayerCommand::onExecute(Context* context)

if (undo.isEnabled())
undo.pushUndoer(new undoers::ReplaceImage(undo.getObjects(),
sprite->stock(), dst_cel->imageIndex()));
sprite, dst_cel->image(), new_image));

sprite->stock()->replaceImage(dst_cel->imageIndex(), new_image);
delete dst_image;
sprite->replaceImage(dst_cel->image()->id(), new_image);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/app/commands/cmd_new_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "doc/palette.h"
#include "doc/primitives.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/ui.h"

#include "generated_new_sprite.h"
Expand Down
1 change: 0 additions & 1 deletion src/app/commands/cmd_palette_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#include "doc/image.h"
#include "doc/palette.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "gfx/hsv.h"
#include "gfx/rgb.h"
#include "gfx/size.h"
Expand Down
9 changes: 4 additions & 5 deletions src/app/commands/cmd_rotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "doc/image.h"
#include "doc/mask.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/ui.h"

namespace app {
Expand Down Expand Up @@ -116,12 +115,12 @@ class RotateJob : public Job
}

// rotate the image
Image* new_image = Image::create(image->pixelFormat(),
m_angle == 180 ? image->width(): image->height(),
m_angle == 180 ? image->height(): image->width());
ImageRef new_image(Image::create(image->pixelFormat(),
m_angle == 180 ? image->width(): image->height(),
m_angle == 180 ? image->height(): image->width()));
doc::rotate_image(image, new_image, m_angle);

api.replaceStockImage(m_sprite, cel->imageIndex(), new_image);
api.replaceImage(m_sprite, cel->imageRef(), new_image);
}

jobProgress((float)i / m_cels.size());
Expand Down
19 changes: 9 additions & 10 deletions src/app/commands/cmd_sprite_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "doc/mask.h"
#include "doc/primitives.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/ui.h"

#define PERC_FORMAT "%.1f"
Expand Down Expand Up @@ -105,15 +104,15 @@ class SpriteSizeJob : public Job {
// Resize the image
int w = scale_x(image->width());
int h = scale_y(image->height());
Image* new_image = Image::create(image->pixelFormat(), MAX(1, w), MAX(1, h));
ImageRef new_image(Image::create(image->pixelFormat(), MAX(1, w), MAX(1, h)));

doc::algorithm::fixup_image_transparent_colors(image);
doc::algorithm::resize_image(image, new_image,
m_resize_method,
m_sprite->palette(cel->frame()),
m_sprite->rgbMap(cel->frame()));
m_resize_method,
m_sprite->palette(cel->frame()),
m_sprite->rgbMap(cel->frame()));

api.replaceStockImage(m_sprite, cel->imageIndex(), new_image);
api.replaceImage(m_sprite, cel->imageRef(), new_image);

jobProgress((float)progress / cels.size());

Expand All @@ -124,7 +123,7 @@ class SpriteSizeJob : public Job {

// Resize mask
if (m_document->isMaskVisible()) {
base::UniquePtr<Image> old_bitmap
ImageRef old_bitmap
(crop_image(m_document->mask()->bitmap(), -1, -1,
m_document->mask()->bitmap()->width()+2,
m_document->mask()->bitmap()->height()+2, 0));
Expand All @@ -137,9 +136,9 @@ class SpriteSizeJob : public Job {
scale_x(m_document->mask()->bounds().x-1),
scale_y(m_document->mask()->bounds().y-1), MAX(1, w), MAX(1, h)));
algorithm::resize_image(old_bitmap, new_mask->bitmap(),
m_resize_method,
m_sprite->palette(0), // Ignored
m_sprite->rgbMap(0)); // Ignored
m_resize_method,
m_sprite->palette(0), // Ignored
m_sprite->rgbMap(0)); // Ignored

// Reshrink
new_mask->intersect(new_mask->bounds());
Expand Down
1 change: 0 additions & 1 deletion src/app/commands/filters/filter_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "doc/layer.h"
#include "doc/mask.h"
#include "doc/sprite.h"
#include "doc/stock.h"
#include "ui/manager.h"
#include "ui/view.h"
#include "ui/widget.h"
Expand Down
1 change: 0 additions & 1 deletion src/app/context_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "doc/cel.h"
#include "doc/layer.h"
#include "doc/sprite.h"
#include "doc/stock.h"

namespace app {

Expand Down
18 changes: 7 additions & 11 deletions src/app/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "app/file/format_options.h"
#include "app/flatten.h"
#include "app/objects_container_impl.h"
#include "app/undoers/add_image.h"
#include "app/undoers/add_layer.h"
#include "app/util/boundary.h"
#include "base/memory.h"
Expand All @@ -41,7 +40,6 @@
#include "doc/mask.h"
#include "doc/palette.h"
#include "doc/sprite.h"
#include "doc/stock.h"

namespace app {

Expand Down Expand Up @@ -235,18 +233,17 @@ void Document::generateMaskBoundaries(Mask* mask)
void Document::destroyExtraCel()
{
delete m_extraCel;
delete m_extraImage;

m_extraCel = NULL;
m_extraImage = NULL;
m_extraImage.reset(NULL);
}

void Document::prepareExtraCel(const gfx::Rect& bounds, int opacity)
{
ASSERT(sprite() != NULL);

if (!m_extraCel)
m_extraCel = new Cel(frame_t(0), 0); // Ignored fields for this cell (frame, and image index)
m_extraCel = new Cel(frame_t(0), ImageRef(NULL)); // Ignored fields for this cel (frame, and image index)

m_extraCel->setPosition(bounds.getOrigin());
m_extraCel->setOpacity(opacity);
Expand All @@ -255,9 +252,8 @@ void Document::prepareExtraCel(const gfx::Rect& bounds, int opacity)
m_extraImage->pixelFormat() != sprite()->pixelFormat() ||
m_extraImage->width() != bounds.w ||
m_extraImage->height() != bounds.h) {
delete m_extraImage; // image
m_extraImage = Image::create(sprite()->pixelFormat(),
bounds.w, bounds.h);
m_extraImage.reset(Image::create(sprite()->pixelFormat(),
bounds.w, bounds.h));
}
}

Expand All @@ -268,7 +264,7 @@ Cel* Document::getExtraCel() const

Image* Document::getExtraCelImage() const
{
return m_extraImage;
return m_extraImage.get();
}

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -342,8 +338,8 @@ void Document::copyLayerContent(const Layer* sourceLayer0, Document* destDoc, La
const Image* sourceImage = sourceCel->image();
ASSERT(sourceImage != NULL);

Image* newImage = Image::createCopy(sourceImage);
newCel->setImage(destLayer->sprite()->stock()->addImage(newImage));
ImageRef newImage(Image::createCopy(sourceImage));
newCel->setImage(newImage);

destLayer->addCel(newCel);
newCel.release();
Expand Down
Loading

0 comments on commit f1f24cb

Please sign in to comment.