mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
EncodedImageData was implemented really stupidly. Now it's less stupid.
This commit is contained in:
@@ -25,8 +25,6 @@
|
||||
#include <common/Data.h>
|
||||
#include <common/StringMap.h>
|
||||
|
||||
#include "Image.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
// LOVE
|
||||
#include <common/Data.h>
|
||||
|
||||
#include "EncodedImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
@@ -94,6 +96,13 @@ namespace image
|
||||
* @return The color for the given location.
|
||||
**/
|
||||
virtual pixel getPixel(int x, int y) const = 0;
|
||||
|
||||
/**
|
||||
* Encodes raw pixel data into a given format.
|
||||
* @param f The format to convert to.
|
||||
* @return A pointer to the encoded image data.
|
||||
**/
|
||||
virtual EncodedImageData * encode(EncodedImageData::Format f) = 0;
|
||||
|
||||
}; // ImageData
|
||||
|
||||
|
||||
@@ -168,10 +168,10 @@ namespace devil
|
||||
return pixels[y*width+x];
|
||||
}
|
||||
|
||||
love::image::EncodedImageData * ImageData::encodeImageData(love::image::ImageData * d, EncodedImageData::Format f) {
|
||||
EncodedImageData * ImageData::encode(EncodedImageData::Format f) {
|
||||
ILubyte * data;
|
||||
ILuint w = d->getWidth();
|
||||
int h = d->getHeight(); // has to be a signed int so we can make it negative for BMPs
|
||||
ILuint w = getWidth();
|
||||
int h = getHeight(); // has to be a signed int so we can make it negative for BMPs
|
||||
int headerLen, bpp, row, size, padding, filesize;
|
||||
switch (f) {
|
||||
case EncodedImageData::FORMAT_BMP:
|
||||
@@ -227,7 +227,6 @@ namespace devil
|
||||
data[50] = data[51] = data[52] = data[53] = 0; // all colors are important!
|
||||
// Okay, header's done! Now for the pixel data...
|
||||
data += headerLen;
|
||||
d->getData(); // bind the imagedata's image
|
||||
for (int i = 0; i < h; i++) { // we've got to loop through the rows, adding the pixel data plus padding
|
||||
ilCopyPixels(0,i,0,w,1,1,IL_BGR,IL_UNSIGNED_BYTE,data);
|
||||
data += row;
|
||||
@@ -258,7 +257,6 @@ namespace devil
|
||||
data[17] = 0; // descriptor bits
|
||||
// header done. write the pixel data to TGA:
|
||||
data += headerLen;
|
||||
d->getData(); // bind the imagedata's image
|
||||
ilCopyPixels(0,0,0,w,h,1,IL_BGR,IL_UNSIGNED_BYTE,data); // convert the pixels to BGR (remember, little-endian) and copy them to data
|
||||
|
||||
// It's Targa, so we have to flip the image.
|
||||
@@ -274,7 +272,7 @@ namespace devil
|
||||
data -= headerLen;
|
||||
delete [] temp;
|
||||
}
|
||||
return new love::image::EncodedImageData(data, f, size + headerLen);
|
||||
return new EncodedImageData(data, f, size + headerLen);
|
||||
}
|
||||
|
||||
} // devil
|
||||
|
||||
@@ -77,14 +77,8 @@ namespace devil
|
||||
int getHeight() const ;
|
||||
void setPixel(int x, int y, pixel c);
|
||||
pixel getPixel(int x, int y) const;
|
||||
EncodedImageData * encode(EncodedImageData::Format f);
|
||||
|
||||
/**
|
||||
* Encodes raw pixel data into a given format.
|
||||
* @param d The pixel data to be converted.
|
||||
* @param f The format to convert to.
|
||||
* @return A pointer to the encoded image data.
|
||||
**/
|
||||
static love::image::EncodedImageData * encodeImageData(love::image::ImageData * d, EncodedImageData::Format f);
|
||||
|
||||
}; // ImageData
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2010 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "wrap_ImageData.h"
|
||||
|
||||
#include <common/wrap_Data.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
EncodedImageData * luax_checkencodedimagedata(lua_State * L, int idx)
|
||||
{
|
||||
return luax_checktype<EncodedImageData>(L, idx, "EncodedImageData", IMAGE_ENCODED_IMAGE_DATA_T);
|
||||
}
|
||||
|
||||
int w_EncodedImageData_getFormat(lua_State * L) {
|
||||
EncodedImageData * e = luax_checkencodedimagedata(L, 1);
|
||||
EncodedImageData::Format f = e->getFormat();
|
||||
const char * fmt;
|
||||
EncodedImageData::getConstant(f, fmt);
|
||||
lua_pushstring(L, fmt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] = {
|
||||
|
||||
// Data
|
||||
{ "getPointer", w_Data_getPointer },
|
||||
{ "getSize", w_Data_getSize },
|
||||
|
||||
{ "getFormat", w_EncodedImageData_getFormat },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
int luaopen_encodedimagedata(lua_State * L)
|
||||
{
|
||||
return luax_register_type(L, "EncodedImageData", functions);
|
||||
}
|
||||
|
||||
} // image
|
||||
} // love
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2010 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_IMAGE_WRAP_ENCODED_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_WRAP_ENCODED_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include <common/runtime.h>
|
||||
#include "ImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
EncodedImageData * luax_checkencodedimagedata(lua_State * L, int idx);
|
||||
|
||||
int w_EncodedImageData_getFormat(lua_State * L);
|
||||
int luaopen_encodedimagedata(lua_State * L);
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_WRAP_ENCODED_IMAGE_DATA_H
|
||||
@@ -20,6 +20,9 @@
|
||||
|
||||
#include "wrap_Image.h"
|
||||
|
||||
#include "EncodedImageData.h"
|
||||
#include "wrap_EncodedImageData.h"
|
||||
|
||||
#include <common/Data.h>
|
||||
#include <common/StringMap.h>
|
||||
|
||||
@@ -79,13 +82,14 @@ namespace image
|
||||
|
||||
int w_newEncodedImageData(lua_State * L) {
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
EncodedImageData::Format format;
|
||||
EncodedImageData::getConstant(luaL_checkstring(L, 2), format);
|
||||
EncodedImageData * e = love::image::devil::ImageData::encodeImageData(t, format);
|
||||
luax_newtype(L, "Data", DATA_T, (void*)e); // since we don't need any of EncodedImageData's features
|
||||
const char * fmt = luaL_checkstring(L, 2);
|
||||
EncodedImageData::Format f;
|
||||
EncodedImageData::getConstant(fmt, f);
|
||||
EncodedImageData * eid = t->encode(f);
|
||||
luax_newtype(L, "EncodedImageData", IMAGE_ENCODED_IMAGE_DATA_T, (void*)eid);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] = {
|
||||
{ "newImageData", w_newImageData },
|
||||
@@ -95,6 +99,7 @@ namespace image
|
||||
|
||||
static const lua_CFunction types[] = {
|
||||
luaopen_imagedata,
|
||||
luaopen_encodedimagedata,
|
||||
0
|
||||
};
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
// LOVE
|
||||
#include "Image.h"
|
||||
#include "EncodedImageData.h"
|
||||
#include "wrap_ImageData.h"
|
||||
|
||||
namespace love
|
||||
|
||||
@@ -126,6 +126,17 @@ namespace image
|
||||
t->paste((love::image::ImageData *)src, dx, dy, sx, sy, sw, sh);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_ImageData_encode(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
const char * fmt = luaL_checkstring(L, 2);
|
||||
EncodedImageData::Format f;
|
||||
EncodedImageData::getConstant(fmt, f);
|
||||
EncodedImageData * eid = t->encode(f);
|
||||
luax_newtype(L, "EncodedImageData", IMAGE_ENCODED_IMAGE_DATA_T, (void*)eid);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] = {
|
||||
|
||||
@@ -140,6 +151,7 @@ namespace image
|
||||
{ "mapPixel", w_ImageData_mapPixel },
|
||||
{ "getString", w_ImageData_getString },
|
||||
{ "paste", w_ImageData_paste },
|
||||
{ "encode", w_ImageData_encode },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace image
|
||||
int w_ImageData_mapPixel(lua_State * L);
|
||||
int w_ImageData_getString(lua_State * L);
|
||||
int w_ImageData_paste(lua_State * L);
|
||||
int w_ImageData_encode(lua_State * L);
|
||||
int luaopen_imagedata(lua_State * L);
|
||||
|
||||
} // image
|
||||
|
||||
Reference in New Issue
Block a user