1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#include "common/file.h"
#include "graphics/managed_surface.h"
#include "graphics/screen.h"
#include "image/png.h"
#include "crab/crab.h"
#include "crab/filesystem.h"
#include "crab/image/Image.h"
using ImageDecoder = Image::PNGDecoder;
namespace Crab {
using namespace pyrodactyl::image;
//------------------------------------------------------------------------
// Purpose: Load a texture
//------------------------------------------------------------------------
bool Image::load(Graphics::ManagedSurface *surface) {
deleteImage();
_texture = new Graphics::ManagedSurface();
_texture->copyFrom(*surface);
_w = surface->w;
_h = surface->h;
return true;
}
bool Image::load(const Common::Path &path) {
// Get rid of preexisting texture
deleteImage();
// Load image at specified path
Common::File file;
ImageDecoder decoder;
if (fileOpen(path, &file) && decoder.loadStream(file)) {
_texture = new Graphics::ManagedSurface();
_texture->convertFrom(*decoder.getSurface(), *g_engine->_format);
_w = _texture->w;
_h = _texture->h;
file.close();
debugC(kDebugFilePath, "Image::load() Image Texture(%s): w: %d h: %d", path.toString(Common::Path::kNativeSeparator).c_str(), _w, _h);
}
return _texture != nullptr;
}
bool Image::load(rapidxml::xml_node<char> *node, const char *name) {
if (node->first_attribute(name) != nullptr)
return load(node->first_attribute(name)->value());
return false;
}
//------------------------------------------------------------------------
// Purpose: Draw a texture to the screen without cropping
//------------------------------------------------------------------------
void Image::draw(const int &x, const int &y, Common::Rect *clip, const TextureFlipType &flip) {
Common::Rect srcRect;
if (clip != nullptr) {
srcRect = *clip;
} else {
srcRect = Common::Rect(x, y, _w + x, _h + y);
}
Common::Rect destRect(x, y, _w + x, _h + y);
if (clip != nullptr) {
destRect.right = clip->right;
destRect.bottom = clip->bottom;
}
// TODO: Do proper clipping
g_engine->_screen->blitFrom(*_texture, Common::Point(static_cast<int16>(x), static_cast<int16>(y)));
}
Graphics::Surface* Image::rotate(const Graphics::ManagedSurface &src, ImageRotateDegrees rotation) {
assert(!src.empty());
assert(src.w == src.h);
assert(src.format.bytesPerPixel == 4);
Graphics::Surface *dest = new Graphics::Surface();
dest->create(src.w, src.h, src.format);
const uint size = src.w;
const uint32 *s = (const uint32 *)src.getBasePtr(0, 0);
switch (rotation) {
case kImageRotateBy90:
for (uint y = 0; y < size; ++y) {
for (uint x = 0; x < size; ++x, ++s) {
*((uint32 *)dest->getBasePtr(size - y - 1, x)) = *s;
}
}
break;
case kImageRotateBy180: {
// 180 degrees
uint32 *d;
for (uint y = 0; y < size; ++y) {
const uint32 *e = (const uint32 *)src.getBasePtr(size - 1, y);
d = (uint32 *)dest->getBasePtr(size - 1, size - y - 1);
for (; s < e; ++s, --d) {
*d = *s;
}
}
break;
}
case kImageRotateBy270:
for (uint y = 0; y < size; ++y) {
for (uint x = 0; x < size; ++x, ++s) {
*((uint32 *)dest->getBasePtr(y, size - x - 1)) = *s;
}
}
break;
}
return dest;
}
void Image::draw(const int &x, const int &y, Rect *clip, const TextureFlipType &flip, Graphics::ManagedSurface *surf) {
if (surf == nullptr)
surf = g_engine->_screen;
Common::Rect srcRect(0, 0, _w, _h);
Common::Rect destRect(x, y, _w + x, _h + y);
if (clip) {
srcRect = Common::Rect(clip->x, clip->y, clip->x + clip->w, clip->y + clip->h);
destRect.right = static_cast<int16>(clip->w + x);
destRect.bottom = static_cast<int16>(clip->h + y);
}
Graphics::ManagedSurface s;
s.copyFrom(_texture->getSubArea(srcRect));
if (s.w < 1 || s.h < 1)
return;
Graphics::Surface *rotated_surf = nullptr;
switch(flip) {
case FLIP_NONE:
break;
case FLIP_X:
s.surfacePtr()->flipHorizontal(Common::Rect(s.w, s.h));
break;
case FLIP_Y:
s.surfacePtr()->flipVertical(Common::Rect(s.w, s.h));
break;
case FLIP_XY:
s.surfacePtr()->flipHorizontal(Common::Rect(s.w, s.h));
s.surfacePtr()->flipVertical(Common::Rect(s.w, s.h));
break;
case FLIP_D:
s.surfacePtr()->flipHorizontal(Common::Rect(s.w, s.h));
rotated_surf = rotate(s, kImageRotateBy270);
s.copyFrom(*rotated_surf);
delete rotated_surf;
break;
case FLIP_DX:
rotated_surf = rotate(s, kImageRotateBy90);
s.copyFrom(*rotated_surf);
delete rotated_surf;
break;
case FLIP_DY:
rotated_surf = rotate(s, kImageRotateBy270);
s.copyFrom(*rotated_surf);
delete rotated_surf;
break;
case FLIP_XYD:
s.surfacePtr()->flipVertical(Common::Rect(s.w, s.h));
rotated_surf = rotate(s, kImageRotateBy270);
s.copyFrom(*rotated_surf);
delete rotated_surf;
break;
default:
warning("Flipped texture: %d", flip);
}
surf->blitFrom(s, Common::Rect(s.w, s.h), destRect);
}
void Image::fastDraw(const int &x, const int &y, Rect *clip) {
Common::Rect destRect(x, y, _w + x, _h + y);
int in_y = 0, in_x = 0;
if (clip) {
destRect.setWidth(clip->w);
destRect.setHeight(clip->h);
in_x = clip->x;
in_y = clip->y;
}
const int he = destRect.height();
const int destW = destRect.width();
uint32 *out = (uint32*)g_engine->_screen->getBasePtr(destRect.left, destRect.top);
uint32 *in = (uint32*)_texture->getBasePtr(in_x, in_y);
const uint32 outPitch = g_engine->_screen->pitch / sizeof(uint32);
const uint32 inPitch = _texture->pitch / sizeof(uint32);
for (int y_ = 0; y_ < he; y_++) {
memcpy(out, in, destW * 4);
out += outPitch;
in += inPitch;
}
g_engine->_screen->addDirtyRect(destRect);
}
//------------------------------------------------------------------------
// Purpose: Delete texture data
//------------------------------------------------------------------------
void Image::deleteImage() {
if (_texture != nullptr && _w > 0 && _h > 0) {
_texture->free();
delete _texture;
_texture = nullptr;
_w = 0;
_h = 0;
}
}
} // End of namespace Crab
|