2014-02-24 18:17:47 +09:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 yvt
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
This file is part of OpenSpades.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
OpenSpades 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
OpenSpades 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.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
*/
|
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
|
|
|
|
2016-12-03 19:04:58 +09:00
|
|
|
#include <Imports/SDL.h>
|
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
#include "Bitmap.h"
|
2014-02-24 18:17:47 +09:00
|
|
|
#include "Debug.h"
|
|
|
|
#include "Exception.h"
|
2016-12-03 18:23:47 +09:00
|
|
|
#include "IBitmapCodec.h"
|
2014-02-24 18:17:47 +09:00
|
|
|
#include "IStream.h"
|
|
|
|
|
|
|
|
namespace spades {
|
2016-12-03 18:23:47 +09:00
|
|
|
class SdlImageReader : public IBitmapCodec {
|
2014-02-24 18:17:47 +09:00
|
|
|
public:
|
2016-12-03 18:23:47 +09:00
|
|
|
virtual SDL_Surface *LoadSdlImage(const std::string &data) = 0;
|
|
|
|
|
2018-10-13 14:43:17 +09:00
|
|
|
bool CanLoad() override { return true; }
|
|
|
|
bool CanSave() override { return false; }
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2018-10-13 14:43:17 +09:00
|
|
|
Bitmap *Load(IStream *stream) override {
|
2014-02-24 18:17:47 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
// read all
|
|
|
|
std::string data = stream->ReadAllBytes();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
auto deleter = [](SDL_Surface *s) { SDL_FreeSurface(s); };
|
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
// copy to buffer
|
|
|
|
std::unique_ptr<SDL_Surface, decltype(deleter)> imgraw(LoadSdlImage(data), deleter);
|
2016-12-03 18:23:47 +09:00
|
|
|
if (imgraw == nullptr) {
|
2014-02-24 18:17:47 +09:00
|
|
|
SPRaise("SDL surface was not loaded.");
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
std::unique_ptr<SDL_Surface, decltype(deleter)> img(
|
|
|
|
SDL_ConvertSurfaceFormat(imgraw.get(), SDL_PIXELFORMAT_ABGR8888, 0), deleter);
|
|
|
|
if (img == nullptr) {
|
2014-02-24 18:17:47 +09:00
|
|
|
SPRaise("SDL surface was loaded, but format conversion failed.");
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
const unsigned char *inPixels = (const unsigned char *)img->pixels;
|
2014-02-24 18:17:47 +09:00
|
|
|
int width = img->w;
|
|
|
|
int height = img->h;
|
|
|
|
int pitch = img->pitch;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
Handle<Bitmap> bmp;
|
|
|
|
bmp.Set(new Bitmap(width, height), false);
|
2016-12-03 18:23:47 +09:00
|
|
|
try {
|
2014-02-24 18:17:47 +09:00
|
|
|
unsigned char *outPixels = (unsigned char *)bmp->GetPixels();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (pitch == width * 4) {
|
2014-02-24 18:17:47 +09:00
|
|
|
// if the pitch matches the requirement of Bitmap,
|
|
|
|
// just use it
|
|
|
|
memcpy(outPixels, inPixels, pitch * height);
|
|
|
|
} else {
|
|
|
|
// convert
|
2016-12-03 18:23:47 +09:00
|
|
|
for (int y = 0; y < height; y++) {
|
2014-02-24 18:17:47 +09:00
|
|
|
memcpy(outPixels, inPixels, width * 4);
|
|
|
|
outPixels += width * 4;
|
|
|
|
inPixels += pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bmp.Unmanage();
|
2016-12-03 18:23:47 +09:00
|
|
|
} catch (...) {
|
2014-02-24 18:17:47 +09:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2018-10-13 14:43:17 +09:00
|
|
|
void Save(IStream *, Bitmap *) override {
|
2014-02-24 18:17:47 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
2018-10-13 14:43:17 +09:00
|
|
|
SPUnreachable();
|
2014-02-24 18:17:47 +09:00
|
|
|
}
|
|
|
|
};
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
class StringSdlRWops {
|
|
|
|
SDL_RWops *op;
|
|
|
|
std::string str;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-02-24 18:17:47 +09:00
|
|
|
public:
|
2016-12-03 18:23:47 +09:00
|
|
|
StringSdlRWops(std::string s) : str(s) {
|
|
|
|
op = SDL_RWFromConstMem(str.data(), (int)str.size());
|
2014-02-24 18:17:47 +09:00
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
~StringSdlRWops() { SDL_RWclose(op); }
|
2014-02-24 18:17:47 +09:00
|
|
|
operator SDL_RWops *() { return op; }
|
|
|
|
};
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
class SdlImageImageReader : public SdlImageReader {
|
2014-02-24 18:17:47 +09:00
|
|
|
public:
|
2018-10-13 14:43:17 +09:00
|
|
|
std::string GetName() override { return "SDL_image Image Reader"; }
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2018-10-13 14:43:17 +09:00
|
|
|
SDL_Surface *LoadSdlImage(const std::string &data) override {
|
2014-02-24 18:17:47 +09:00
|
|
|
StringSdlRWops ops(data);
|
2016-12-03 18:23:47 +09:00
|
|
|
int flags = IMG_INIT_PNG | IMG_INIT_JPG;
|
2014-02-26 17:21:20 +01:00
|
|
|
int initted = IMG_Init(flags);
|
2016-12-03 18:23:47 +09:00
|
|
|
if ((initted & flags) != flags) {
|
2014-02-26 17:21:20 +01:00
|
|
|
SPRaise("IMG_Init failed: %s", IMG_GetError());
|
|
|
|
}
|
2014-02-24 18:17:47 +09:00
|
|
|
auto *s = IMG_Load_RW(ops, 0);
|
2016-12-03 18:23:47 +09:00
|
|
|
if (s == nullptr) {
|
2014-02-24 18:17:47 +09:00
|
|
|
SPRaise("IMG_Load_RW failed: %s", IMG_GetError());
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2018-10-13 14:43:17 +09:00
|
|
|
bool CheckExtension(const std::string &fn) override {
|
2016-12-03 18:23:47 +09:00
|
|
|
return EndsWith(fn, ".png") || EndsWith(fn, ".jpg") || EndsWith(fn, ".tif") ||
|
|
|
|
EndsWith(fn, ".bmp");
|
2014-02-24 18:17:47 +09:00
|
|
|
}
|
|
|
|
} imgReader;
|
|
|
|
}
|