Lots changed: ImageData can now encode itself to any format you like as long as it's tga, love.filesystem can now write data to files, added EncodedImageData class.

This commit is contained in:
bill@Ixion
2009-09-04 00:34:13 -05:00
parent bd1e587811
commit 3655c9caae
10 changed files with 411 additions and 193 deletions
+14 -5
View File
@@ -274,10 +274,10 @@ namespace physfs
// on-the-fly, or passed as a parameter.
File * file;
// We know for sure that the second parameter must be a
// a string, so let's check that first.
if(!lua_isstring(L, 2))
return luaL_error(L, "Second argument must be a string.");
// We know for sure that we need a second parameter, so
// let's check that first.
if(lua_isnoneornil(L, 2))
return luaL_error(L, "Second argument needed.");
if(lua_isstring(L, 1))
{
@@ -302,7 +302,16 @@ namespace physfs
}
size_t length = 0;
const char * input = lua_tolstring(L, 2, &length);
const char * input;
if(lua_isstring(L, 2)) {
input = lua_tolstring(L, 2, &length);
} else if (luax_istype(L, 2, DATA_T)) {
love::Data * data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
length = data->getSize();
input = (char *)data->getData();
} else {
return luaL_error(L, "Expected string or data for argument #2.");
}
// Get how much we should write. Length of string default.
length = luaL_optint(L, 3, length);
+45
View File
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2006-2009 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 "EncodedImageData.h"
namespace love
{
namespace image
{
EncodedImageData::EncodedImageData(void * d, Image::ImageFormat f, int s) {
data = d;
format = f;
size = s;
}
void * EncodedImageData::getData() const {
return data;
}
Image::ImageFormat EncodedImageData::getFormat() {
return format;
}
int EncodedImageData::getSize() const {
return size;
}
} // image
} // love
+82
View File
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2006-2009 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_ENCODED_IMAGE_DATA_H
#define LOVE_IMAGE_ENCODED_IMAGE_DATA_H
// LOVE
#include <common/Data.h>
#include "Image.h"
namespace love
{
namespace image
{
/**
* Represents encoded pixel data.
**/
class EncodedImageData : public Data
{
public:
/**
* Constructor.
**/
EncodedImageData(void * data, Image::ImageFormat format, int size);
/**
* Destructor.
**/
virtual ~EncodedImageData(){};
// Implements Data.
void * getData() const;
int getSize() const;
/**
* Get the format the data is encoded in.
**/
Image::ImageFormat EncodedImageData::getFormat();
private:
/**
* Actual data.
**/
void * data;
/**
* Size of the data.
**/
int size;
/**
* Image format.
**/
Image::ImageFormat format;
}; // EncodedImageData
} // image
} // love
#endif // LOVE_IMAGE_ENCODED_IMAGE_DATA_H
+4
View File
@@ -76,6 +76,10 @@ namespace image
* @return The new ImageData.
**/
virtual ImageData * newImageData(int width, int height, void *data) = 0;
enum ImageFormat {
FORMAT_TGA
};
}; // Image
+50 -50
View File
@@ -1,50 +1,50 @@
/**
* Copyright (c) 2006-2009 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 "ImageData.h"
namespace love
{
namespace image
{
void ImageData::paste(ImageData * src, int dx, int dy, int sx, int sy, int sw, int sh)
{
pixel * s = (pixel *)src->getData();
pixel * d = (pixel *)getData();
for(int i = 0; i < sh; i++)
{
for(int j = 0; j < sw; j++)
{
if(inside(dx+j, dy+i) && src->inside(sx+j, sy+i))
{
d[(dy+i)*getWidth() + (dx+j)] = s[(sy+i)*src->getWidth() + (sx+j)];
}
}
}
}
bool ImageData::inside(int x, int y) const
{
return (x >= 0 && x < getWidth() && y >= 0 && y < getHeight());
}
} // image
} // love
/**
* Copyright (c) 2006-2009 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 "ImageData.h"
namespace love
{
namespace image
{
void ImageData::paste(ImageData * src, int dx, int dy, int sx, int sy, int sw, int sh)
{
pixel * s = (pixel *)src->getData();
pixel * d = (pixel *)getData();
for(int i = 0; i < sh; i++)
{
for(int j = 0; j < sw; j++)
{
if(inside(dx+j, dy+i) && src->inside(sx+j, sy+i))
{
d[(dy+i)*getWidth() + (dx+j)] = s[(sy+i)*src->getWidth() + (sx+j)];
}
}
}
}
bool ImageData::inside(int x, int y) const
{
return (x >= 0 && x < getWidth() && y >= 0 && y < getHeight());
}
} // image
} // love
+48
View File
@@ -164,6 +164,54 @@ namespace devil
pixel * pixels = (pixel *)getData();
return pixels[y*width+x];
}
love::image::EncodedImageData * ImageData::encodeImageData(love::image::ImageData * d, love::image::Image::ImageFormat f) {
ILubyte * data;
ILuint w = d->getWidth();
ILuint h = d->getHeight();
int headerLen, bpp;
switch (f) {
default: // since we only support one format
headerLen = 18;
bpp = 3;
int size = h * w * bpp;
data = new ILubyte[size + headerLen];
// here's the header for the Targa file format.
data[0] = 0; // ID field size
data[1] = 0; // colormap type
data[2] = 2; // image type
data[3] = data[4] = 0; // colormap start
data[5] = data[6] = 0; // colormap length
data[7] = 32; // colormap bits
data[8] = data[9] = 0; // x origin
data[10] = data[11] = 0; // y origin
// Targa is little endian, so:
data[12] = w & 255; // least significant byte of width
data[13] = w >> 8; // most significant byte of width
data[14] = h & 255; // least significant byte of height
data[15] = h >> 8; // most significant byte of height
data[16] = bpp * 8; // bits per pixel
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.
int row = w * bpp;
ILubyte * temp = new ILubyte[row];
ILubyte * src = data - row;
ILubyte * dst = data + size;
for (int i = 0; i < (h >> 1); i++) {
memcpy(temp,src+=row,row);
memcpy(src,dst-=row,row);
memcpy(dst,temp,row);
}
data -= headerLen;
delete [] temp;
return new love::image::EncodedImageData(data, f, size + headerLen);
}
}
} // devil
} // image
+10
View File
@@ -23,7 +23,9 @@
// LOVE
#include <filesystem/File.h>
#include <image/Image.h>
#include <image/ImageData.h>
#include <image/EncodedImageData.h>
// DevIL
#include <IL/il.h>
@@ -72,6 +74,14 @@ namespace devil
int getHeight() const ;
void setPixel(int x, int y, pixel c);
pixel getPixel(int x, int y) const;
/**
* 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, love::image::Image::ImageFormat f);
}; // ImageData
+111 -99
View File
@@ -1,97 +1,109 @@
/**
* Copyright (c) 2006-2009 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_Image.h"
#include "devil/Image.h"
namespace love
{
namespace image
{
static Image * instance = 0;
int w_newImageData(lua_State * L)
{
// Case 1: Integers.
if(lua_isnumber(L, 1))
{
int w = luaL_checkint(L, 1);
int h = luaL_checkint(L, 2);
ImageData * t = instance->newImageData(w, h);
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
return 1;
}
// Case 2: Data
if(luax_istype(L, 1, DATA_T))
{
Data * d = luax_checktype<Data>(L, 1, "Data", DATA_T);
ImageData * t;
try {
t = instance->newImageData(d);
} catch (love::Exception & e) {
return luaL_error(L, e.what());
}
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
return 1;
}
// Case 3: String/File.
// Convert to File, if necessary.
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
ImageData * t = instance->newImageData(file);
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] = {
{ "newImageData", w_newImageData },
{ 0, 0 }
};
static const lua_CFunction types[] = {
luaopen_imagedata,
0
};
int luaopen_love_image(lua_State * L)
{
if(instance == 0)
{
try
{
instance = new love::image::devil::Image();
}
catch(Exception & e)
{
return luaL_error(L, e.what());
}
}
/**
* Copyright (c) 2006-2009 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_Image.h"
#include <common/Data.h>
#include "devil/Image.h"
#include "devil/ImageData.h"
namespace love
{
namespace image
{
static Image * instance = 0;
int w_newImageData(lua_State * L)
{
// Case 1: Integers.
if(lua_isnumber(L, 1))
{
int w = luaL_checkint(L, 1);
int h = luaL_checkint(L, 2);
ImageData * t = instance->newImageData(w, h);
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
return 1;
}
// Case 2: Data
if(luax_istype(L, 1, DATA_T))
{
Data * d = luax_checktype<Data>(L, 1, "Data", DATA_T);
ImageData * t;
try {
t = instance->newImageData(d);
} catch (love::Exception & e) {
return luaL_error(L, e.what());
}
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
return 1;
}
// Case 3: String/File.
// Convert to File, if necessary.
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
ImageData * t = instance->newImageData(file);
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t);
return 1;
}
int w_newEncodedImageData(lua_State * L) {
ImageData * t = luax_checkimagedata(L, 1);
Image::ImageFormat format = (Image::ImageFormat)luaL_optint(L, 2, Image::FORMAT_TGA);
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
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] = {
{ "newImageData", w_newImageData },
{ "newEncodedImageData", w_newEncodedImageData },
{ 0, 0 }
};
static const lua_CFunction types[] = {
luaopen_imagedata,
0
};
int luaopen_love_image(lua_State * L)
{
if(instance == 0)
{
try
{
instance = new love::image::devil::Image();
}
catch(Exception & e)
{
return luaL_error(L, e.what());
}
}
WrappedModule w;
w.module = instance;
w.name = "image";
@@ -100,8 +112,8 @@ namespace image
w.types = types;
w.constants = 0;
return luax_register_module(L, w);
}
} // image
} // love
return luax_register_module(L, w);
}
} // image
} // love
+41 -39
View File
@@ -1,39 +1,41 @@
/**
* Copyright (c) 2006-2009 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_IMAGE_H
#define LOVE_IMAGE_WRAP_IMAGE_H
// LOVE
#include "Image.h"
#include "wrap_ImageData.h"
namespace love
{
namespace image
{
int w_getFormats(lua_State * L);
int w_newImageData(lua_State * L);
extern "C" LOVE_EXPORT int luaopen_love_image(lua_State * L);
} // image
} // love
#endif // LOVE_IMAGE_WRAP_IMAGE_H
/**
* Copyright (c) 2006-2009 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_IMAGE_H
#define LOVE_IMAGE_WRAP_IMAGE_H
// LOVE
#include "Image.h"
#include "EncodedImageData.h"
#include "wrap_ImageData.h"
namespace love
{
namespace image
{
int w_getFormats(lua_State * L);
int w_newImageData(lua_State * L);
int w_newEncodedImageData(lua_State * L);
extern "C" LOVE_EXPORT int luaopen_love_image(lua_State * L);
} // image
} // love
#endif // LOVE_IMAGE_WRAP_IMAGE_H