Fix crash converting sprite to grayscale

This commit is contained in:
David Capello 2015-04-15 13:58:35 -03:00
parent 13a977f0e6
commit 43a0279a24
10 changed files with 27 additions and 108 deletions

View File

@ -116,7 +116,6 @@ add_library(app-lib
cmd/with_frame_tag.cpp
cmd/with_image.cpp
cmd/with_layer.cpp
cmd/with_palette.cpp
cmd/with_sprite.cpp
cmd_sequence.cpp
cmd_transaction.cpp

View File

@ -22,41 +22,28 @@ using namespace doc;
AddPalette::AddPalette(Sprite* sprite, Palette* pal)
: WithSprite(sprite)
, WithPalette(pal)
, m_frame(pal->frame())
{
write_palette(m_stream, pal);
}
void AddPalette::onExecute()
{
Sprite* sprite = this->sprite();
Palette* palette = this->palette();
m_stream.seekp(0);
sprite->setPalette(palette, true);
Sprite* sprite = this->sprite();
Palette* pal = read_palette(m_stream);
sprite->setPalette(pal, true);
sprite->incrementVersion();
}
void AddPalette::onUndo()
{
Sprite* sprite = this->sprite();
Palette* pal = this->palette();
write_palette(m_stream, pal);
sprite->deletePalette(pal);
sprite->deletePalette(m_frame);
sprite->incrementVersion();
delete pal;
}
void AddPalette::onRedo()
{
Sprite* sprite = this->sprite();
Palette* pal = read_palette(m_stream);
sprite->setPalette(pal, true);
sprite->incrementVersion();
m_stream.str(std::string());
m_stream.clear();
}
} // namespace cmd

View File

@ -10,13 +10,13 @@
#pragma once
#include "app/cmd.h"
#include "app/cmd/with_palette.h"
#include "app/cmd/with_sprite.h"
#include "doc/frame.h"
#include <sstream>
namespace doc {
class Palette;
class Sprite;
}
@ -25,15 +25,13 @@ namespace cmd {
using namespace doc;
class AddPalette : public Cmd
, public WithSprite
, public WithPalette {
, public WithSprite {
public:
AddPalette(Sprite* sprite, Palette* pal);
protected:
void onExecute() override;
void onUndo() override;
void onRedo() override;
size_t onMemSize() const override {
return sizeof(*this) +
(size_t)const_cast<std::stringstream*>(&m_stream)->tellp();
@ -41,6 +39,7 @@ namespace cmd {
private:
std::stringstream m_stream;
frame_t m_frame;
};
} // namespace cmd

View File

@ -31,10 +31,5 @@ void RemovePalette::onUndo()
AddPalette::onRedo();
}
void RemovePalette::onRedo()
{
AddPalette::onUndo();
}
} // namespace cmd
} // namespace app

View File

@ -22,7 +22,6 @@ namespace cmd {
protected:
void onExecute() override;
void onUndo() override;
void onRedo() override;
};
} // namespace cmd

View File

@ -11,10 +11,10 @@
#include "app/cmd/set_pixel_format.h"
#include "app/cmd/add_palette.h"
#include "app/cmd/remove_palette.h"
#include "app/cmd/replace_image.h"
#include "app/cmd/set_cel_opacity.h"
#include "app/cmd/set_palette.h"
#include "app/document.h"
#include "base/unique_ptr.h"
#include "doc/cel.h"
@ -85,14 +85,14 @@ SetPixelFormat::SetPixelFormat(Sprite* sprite,
// all palettes and put only one grayscaled-palette at the first
// frame.
if (newFormat == IMAGE_GRAYSCALE) {
// Add undoers to revert all palette changes.
// Add cmds to revert all palette changes.
PalettesList palettes = sprite->getPalettes();
for (Palette* pal : palettes)
m_seq.add(new cmd::RemovePalette(sprite, pal));
if (pal->frame() != 0)
m_seq.add(new cmd::RemovePalette(sprite, pal));
base::UniquePtr<Palette> graypal(Palette::createGrayscale());
graypal->setFrame(0);
m_seq.add(new cmd::AddPalette(sprite, graypal));
m_seq.add(new cmd::SetPalette(sprite, 0, graypal));
}
}

View File

@ -1,32 +0,0 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/cmd/with_palette.h"
#include "doc/palette.h"
namespace app {
namespace cmd {
using namespace doc;
WithPalette::WithPalette(Palette* palette)
: m_paletteId(palette->id())
{
}
Palette* WithPalette::palette()
{
return get<Palette>(m_paletteId);
}
} // namespace cmd
} // namespace app

View File

@ -1,34 +0,0 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifndef APP_CMD_WITH_PALETTE_H_INCLUDED
#define APP_CMD_WITH_PALETTE_H_INCLUDED
#pragma once
#include "doc/object_id.h"
namespace doc {
class Palette;
}
namespace app {
namespace cmd {
using namespace doc;
class WithPalette {
public:
WithPalette(Palette* palette);
Palette* palette();
private:
ObjectId m_paletteId;
};
} // namespace cmd
} // namespace app
#endif

View File

@ -329,12 +329,18 @@ void Sprite::resetPalettes()
}
}
void Sprite::deletePalette(Palette* pal)
void Sprite::deletePalette(frame_t frame)
{
ASSERT(pal != NULL);
auto it = m_palettes.begin(), end = m_palettes.end();
for (; it != end; ++it) {
Palette* pal = *it;
base::remove_from_container(m_palettes, pal);
delete pal; // palette
if (pal->frame() == frame) {
delete pal; // delete palette
m_palettes.erase(it);
break;
}
}
}
RgbMap* Sprite::rgbMap(frame_t frame) const

View File

@ -102,7 +102,7 @@ namespace doc {
// Removes all palettes from the sprites except the first one.
void resetPalettes();
void deletePalette(Palette* pal);
void deletePalette(frame_t frame);
RgbMap* rgbMap(frame_t frame) const;