2013-08-09 08:01:20 +08:00
|
|
|
/* Aseprite
|
2013-01-27 23:13:13 +08:00
|
|
|
* Copyright (C) 2001-2013 David Capello
|
2012-01-06 06:45:03 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/document_api.h"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/modules/palettes.h"
|
|
|
|
#include "app/undo_transaction.h"
|
2013-03-12 07:29:45 +08:00
|
|
|
#include "raster/dithering_method.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/image.h"
|
|
|
|
#include "raster/sprite.h"
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
|
|
|
|
|
|
|
class ChangePixelFormatCommand : public Command {
|
2012-02-13 10:21:06 +08:00
|
|
|
PixelFormat m_format;
|
2012-01-06 06:45:03 +08:00
|
|
|
DitheringMethod m_dithering;
|
|
|
|
public:
|
2012-02-13 10:21:06 +08:00
|
|
|
ChangePixelFormatCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new ChangePixelFormatCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void onLoadParams(Params* params);
|
|
|
|
bool onEnabled(Context* context);
|
|
|
|
bool onChecked(Context* context);
|
|
|
|
void onExecute(Context* context);
|
|
|
|
};
|
|
|
|
|
2012-02-13 10:21:06 +08:00
|
|
|
ChangePixelFormatCommand::ChangePixelFormatCommand()
|
|
|
|
: Command("ChangePixelFormat",
|
|
|
|
"Change Pixel Format",
|
2012-01-06 06:45:03 +08:00
|
|
|
CmdUIOnlyFlag)
|
|
|
|
{
|
2012-02-13 10:21:06 +08:00
|
|
|
m_format = IMAGE_RGB;
|
2012-01-06 06:45:03 +08:00
|
|
|
m_dithering = DITHERING_NONE;
|
|
|
|
}
|
|
|
|
|
2012-02-13 10:21:06 +08:00
|
|
|
void ChangePixelFormatCommand::onLoadParams(Params* params)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2012-02-13 10:21:06 +08:00
|
|
|
std::string format = params->get("format");
|
|
|
|
if (format == "rgb") m_format = IMAGE_RGB;
|
|
|
|
else if (format == "grayscale") m_format = IMAGE_GRAYSCALE;
|
|
|
|
else if (format == "indexed") m_format = IMAGE_INDEXED;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
std::string dithering = params->get("dithering");
|
|
|
|
if (dithering == "ordered")
|
|
|
|
m_dithering = DITHERING_ORDERED;
|
|
|
|
else
|
|
|
|
m_dithering = DITHERING_NONE;
|
|
|
|
}
|
|
|
|
|
2012-02-13 10:21:06 +08:00
|
|
|
bool ChangePixelFormatCommand::onEnabled(Context* context)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
Sprite* sprite(writer.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
if (sprite != NULL &&
|
2014-07-30 12:28:15 +08:00
|
|
|
sprite->pixelFormat() == IMAGE_INDEXED &&
|
2012-02-13 10:21:06 +08:00
|
|
|
m_format == IMAGE_INDEXED &&
|
2012-01-06 06:45:03 +08:00
|
|
|
m_dithering == DITHERING_ORDERED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return sprite != NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-13 10:21:06 +08:00
|
|
|
bool ChangePixelFormatCommand::onChecked(Context* context)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
|
|
|
const Sprite* sprite = reader.sprite();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
if (sprite != NULL &&
|
2014-07-30 12:28:15 +08:00
|
|
|
sprite->pixelFormat() == IMAGE_INDEXED &&
|
2012-02-13 10:21:06 +08:00
|
|
|
m_format == IMAGE_INDEXED &&
|
2012-01-06 06:45:03 +08:00
|
|
|
m_dithering == DITHERING_ORDERED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return
|
|
|
|
sprite != NULL &&
|
2014-07-30 12:28:15 +08:00
|
|
|
sprite->pixelFormat() == m_format;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2012-02-13 10:21:06 +08:00
|
|
|
void ChangePixelFormatCommand::onExecute(Context* context)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
UndoTransaction undoTransaction(writer.context(), "Color Mode Change");
|
|
|
|
Document* document(writer.document());
|
|
|
|
Sprite* sprite(writer.sprite());
|
|
|
|
|
|
|
|
document->getApi().setPixelFormat(sprite, m_format, m_dithering);
|
2012-01-06 06:45:03 +08:00
|
|
|
undoTransaction.commit();
|
|
|
|
}
|
2013-03-12 07:29:45 +08:00
|
|
|
app_refresh_screen();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2012-02-13 10:21:06 +08:00
|
|
|
Command* CommandFactory::createChangePixelFormatCommand()
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2012-02-13 10:21:06 +08:00
|
|
|
return new ChangePixelFormatCommand;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|