mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
Initial Mercurial commit.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 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_IMAGE_H
|
||||
#define LOVE_IMAGE_IMAGE_H
|
||||
|
||||
// LOVE
|
||||
#include <common/Module.h>
|
||||
#include <filesystem/File.h>
|
||||
#include "ImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
/**
|
||||
* This module is responsible for decoding files such as PNG, GIF, JPEG
|
||||
* into raw pixel data. This module does not know how to draw images on
|
||||
* screen; only love.graphics knows that.
|
||||
**/
|
||||
class Image : public Module
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
**/
|
||||
virtual ~Image(){};
|
||||
|
||||
/**
|
||||
* Creates new ImageData from a file.
|
||||
* @param file The file containing the encoded image data.
|
||||
* @return The new ImageData.
|
||||
**/
|
||||
virtual ImageData * newImageData(love::filesystem::File * file) = 0;
|
||||
|
||||
/**
|
||||
* Creates empty ImageData with the given size.
|
||||
* @param The width of the ImageData.
|
||||
* @param The height of the ImageData.
|
||||
* @return The new ImageData.
|
||||
**/
|
||||
virtual ImageData * newImageData(int width, int height) = 0;
|
||||
|
||||
}; // Image
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_IMAGE_H
|
||||
@@ -0,0 +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
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* 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_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include <common/Data.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
// Pixel format structure.
|
||||
struct pixel
|
||||
{
|
||||
// Red, green, blue, alpha.
|
||||
unsigned char r, g, b, a;
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents raw pixel data.
|
||||
**/
|
||||
class ImageData : public Data
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
**/
|
||||
virtual ~ImageData(){};
|
||||
|
||||
/**
|
||||
* Paste part of one ImageData onto another. The subregion defined by the top-left
|
||||
* corner (sx, sy) and the size (sw,sh) will be pasted to (dx,dy) in this ImageData.
|
||||
* @param dx The destination x-coordinate.
|
||||
* @param dy The destination y-coordinate.
|
||||
* @param sx The source x-coordinate.
|
||||
* @param sy The source y-coordinate.
|
||||
* @param sw The source width.
|
||||
* @param sh The source height.
|
||||
**/
|
||||
void paste(ImageData * src, int dx, int dy, int sx, int sy, int sw, int sh);
|
||||
|
||||
/**
|
||||
* Checks whether a position is inside this ImageData. Useful for checking bounds.
|
||||
* @param x The position along the x-axis.
|
||||
* @param y The position along the y-axis.
|
||||
**/
|
||||
bool inside(int x, int y) const;
|
||||
|
||||
/**
|
||||
* Gets the width of this ImageData.
|
||||
* @return The width of this ImageData.
|
||||
**/
|
||||
virtual int getWidth() const = 0;
|
||||
|
||||
/**
|
||||
* Gets the height of this ImageData.
|
||||
* @return The height of this ImageData.
|
||||
**/
|
||||
virtual int getHeight() const = 0;
|
||||
|
||||
/**
|
||||
* Sets the pixel at location (x,y). No effect if out of bounds.
|
||||
* @param x The location along the x-axis.
|
||||
* @param y The location along the y-axis.
|
||||
* @param p The color to use for the given location.
|
||||
**/
|
||||
virtual void setPixel(int x, int y, pixel p) = 0;
|
||||
|
||||
/**
|
||||
* Gets the pixel at location (x,y). Returns black (0,0,0,0) if out
|
||||
* out of bounds.
|
||||
* @param x The location along the x-axis.
|
||||
* @param y The location along the y-axis.
|
||||
* @return The color for the given location.
|
||||
**/
|
||||
virtual pixel getPixel(int x, int y) const = 0;
|
||||
|
||||
|
||||
}; // ImageData
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_IMAGE_DATA_H
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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 "Image.h"
|
||||
|
||||
// DevIL
|
||||
#include <IL/il.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace devil
|
||||
{
|
||||
Image::Image()
|
||||
{
|
||||
ilInit();
|
||||
}
|
||||
|
||||
Image::~Image()
|
||||
{
|
||||
ilShutDown();
|
||||
}
|
||||
|
||||
const char * Image::getName() const
|
||||
{
|
||||
return "love.image.devil";
|
||||
}
|
||||
|
||||
love::image::ImageData * Image::newImageData(love::filesystem::File * file)
|
||||
{
|
||||
return new ImageData(file);
|
||||
}
|
||||
|
||||
love::image::ImageData * Image::newImageData(int width, int height)
|
||||
{
|
||||
return new ImageData(width, height);
|
||||
}
|
||||
|
||||
} // devil
|
||||
} // image
|
||||
} // love
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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_DEVIL_IMAGE_H
|
||||
#define LOVE_IMAGE_DEVIL_IMAGE_H
|
||||
|
||||
// LOVE
|
||||
#include <image/Image.h>
|
||||
#include "ImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace devil
|
||||
{
|
||||
class Image : public love::image::Image
|
||||
{
|
||||
public:
|
||||
|
||||
Image();
|
||||
~Image();
|
||||
|
||||
// Implements Module.
|
||||
const char * getName() const;
|
||||
|
||||
love::image::ImageData * newImageData(love::filesystem::File * file);
|
||||
love::image::ImageData * newImageData(int width, int height);
|
||||
|
||||
}; // Image
|
||||
|
||||
} // devil
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_DEVIL_IMAGE_H
|
||||
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* 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"
|
||||
|
||||
// STD
|
||||
#include <iostream>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace devil
|
||||
{
|
||||
ImageData::ImageData(filesystem::File * file)
|
||||
{
|
||||
// Read the data.
|
||||
Data * data = file->read();
|
||||
|
||||
// Generate DevIL image.
|
||||
ilGenImages(1, &image);
|
||||
|
||||
// Bind the image.
|
||||
ilBindImage(image);
|
||||
|
||||
// Try to load the image.
|
||||
ILboolean success = ilLoadL(IL_TYPE_UNKNOWN, (void*)data->getData(), data->getSize());
|
||||
|
||||
// Free local image data.
|
||||
data->release();
|
||||
|
||||
// Check for errors
|
||||
if(!success)
|
||||
{
|
||||
std::cerr << "Could not decode image." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
width = ilGetInteger(IL_IMAGE_WIDTH);
|
||||
height = ilGetInteger(IL_IMAGE_HEIGHT);
|
||||
origin = ilGetInteger(IL_IMAGE_ORIGIN);
|
||||
|
||||
// Make sure the image is in RGBA format.
|
||||
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
|
||||
|
||||
// This should always be four.
|
||||
bpp = ilGetInteger(IL_IMAGE_BPP);
|
||||
|
||||
if(bpp != 4)
|
||||
{
|
||||
std::cerr << "Bits per pixel != 4" << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ImageData::ImageData(int width, int height)
|
||||
: width(width), height(height), origin(IL_ORIGIN_UPPER_LEFT), bpp(4)
|
||||
{
|
||||
// Generate DevIL image.
|
||||
ilGenImages(1, &image);
|
||||
|
||||
// Bind the image.
|
||||
ilBindImage(image);
|
||||
|
||||
ilTexImage(width, height, 0, bpp, IL_RGBA, IL_UNSIGNED_BYTE, 0);
|
||||
}
|
||||
|
||||
ImageData::~ImageData()
|
||||
{
|
||||
ilDeleteImages(1, &image);
|
||||
}
|
||||
|
||||
int ImageData::getWidth() const
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
int ImageData::getHeight() const
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
void * ImageData::getData() const
|
||||
{
|
||||
ilBindImage(image);
|
||||
return ilGetData();
|
||||
}
|
||||
|
||||
int ImageData::getSize() const
|
||||
{
|
||||
return width*height*bpp;
|
||||
}
|
||||
|
||||
void ImageData::setPixel(int x, int y, pixel c)
|
||||
{
|
||||
int tx = x > width-1 ? width-1 : x;
|
||||
int ty = y > height-1 ? height-1 : y;
|
||||
pixel * pixels = (pixel *)getData();
|
||||
pixels[y*width+x] = c;
|
||||
}
|
||||
|
||||
pixel ImageData::getPixel(int x, int y) const
|
||||
{
|
||||
int tx = x > width-1 ? width-1 : x;
|
||||
int ty = y > height-1 ? height-1 : y;
|
||||
pixel * pixels = (pixel *)getData();
|
||||
return pixels[y*width+x];
|
||||
}
|
||||
|
||||
} // devil
|
||||
} // image
|
||||
} // love
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 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_DEVIL_IMAGE_DATA_H
|
||||
#define LOVE_DEVIL_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include <filesystem/File.h>
|
||||
#include <image/ImageData.h>
|
||||
|
||||
// DevIL
|
||||
#include <IL/il.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
namespace devil
|
||||
{
|
||||
class ImageData : public love::image::ImageData
|
||||
{
|
||||
private:
|
||||
|
||||
// The width of the image data.
|
||||
int width;
|
||||
|
||||
// The height of the image data.
|
||||
int height;
|
||||
|
||||
// The origin of the image.
|
||||
int origin;
|
||||
|
||||
// The bits per pixel.
|
||||
int bpp;
|
||||
|
||||
// DevIL image identifier.
|
||||
ILuint image;
|
||||
|
||||
public:
|
||||
|
||||
ImageData(love::filesystem::File * file);
|
||||
ImageData(int width, int height);
|
||||
virtual ~ImageData();
|
||||
|
||||
// Implements Data.
|
||||
void * getData() const;
|
||||
int getSize() const;
|
||||
|
||||
// Implements ImageData.
|
||||
int getWidth() const ;
|
||||
int getHeight() const ;
|
||||
void setPixel(int x, int y, pixel c);
|
||||
pixel getPixel(int x, int y) const;
|
||||
|
||||
}; // ImageData
|
||||
|
||||
} // devil
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_DEVIL_IMAGE_DATA_H
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 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 _wrap_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", LOVE_IMAGE_IMAGE_DATA_BITS, (void*)t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Convert to File, if necessary.
|
||||
if(lua_isstring(L, 1))
|
||||
luax_strtofile(L, 1);
|
||||
|
||||
// Case 2: String/File.
|
||||
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", LOVE_FILESYSTEM_FILE_BITS);
|
||||
ImageData * t = instance->newImageData(file);
|
||||
luax_newtype(L, "ImageData", LOVE_IMAGE_IMAGE_DATA_BITS, (void*)t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// List of functions to wrap.
|
||||
const luaL_Reg wrap_Image_functions[] = {
|
||||
{ "newImageData", _wrap_newImageData },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
static const lua_CFunction wrap_Image_types[] = {
|
||||
wrap_ImageData_open,
|
||||
0
|
||||
};
|
||||
|
||||
int wrap_Image_open(lua_State * L)
|
||||
{
|
||||
|
||||
if(instance == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
instance = new love::image::devil::Image();
|
||||
}
|
||||
catch(Exception & e)
|
||||
{
|
||||
return luaL_error(L, e.what());
|
||||
}
|
||||
}
|
||||
|
||||
luax_register_gc(L, "love.image", instance);
|
||||
|
||||
return luax_register_module(L, wrap_Image_functions, wrap_Image_types);
|
||||
}
|
||||
|
||||
} // image
|
||||
} // love
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 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 _wrap_getFormats(lua_State * L);
|
||||
int _wrap_newImageData(lua_State * L);
|
||||
int wrap_Image_open(lua_State * L);
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_WRAP_IMAGE_H
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* 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_ImageData.h"
|
||||
|
||||
#include <common/wrap_Data.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
ImageData * luax_checkimagedata(lua_State * L, int idx)
|
||||
{
|
||||
return luax_checktype<ImageData>(L, idx, "ImageData", LOVE_IMAGE_IMAGE_DATA_BITS);
|
||||
}
|
||||
|
||||
int _wrap_ImageData_getWidth(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
lua_pushinteger(L, t->getWidth());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _wrap_ImageData_getHeight(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
lua_pushinteger(L, t->getHeight());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _wrap_ImageData_getPixel(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
int x = luaL_checkint(L, 2);
|
||||
int y = luaL_checkint(L, 3);
|
||||
pixel c = t->getPixel(x, y);
|
||||
lua_pushnumber(L, c.r);
|
||||
lua_pushnumber(L, c.g);
|
||||
lua_pushnumber(L, c.b);
|
||||
lua_pushnumber(L, c.a);
|
||||
return 4;
|
||||
}
|
||||
|
||||
int _wrap_ImageData_setPixel(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
int x = luaL_checkint(L, 2);
|
||||
int y = luaL_checkint(L, 3);
|
||||
pixel c;
|
||||
c.r = luaL_checkint(L, 4);
|
||||
c.g = luaL_checkint(L, 5);
|
||||
c.b = luaL_checkint(L, 6);
|
||||
c.a = luaL_checkint(L, 7);
|
||||
t->setPixel(x, y, c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _wrap_ImageData_mapPixel(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
|
||||
if(!lua_isfunction(L, 2))
|
||||
return luaL_error(L, "Function expected");
|
||||
|
||||
int w = t->getWidth();
|
||||
int h = t->getHeight();
|
||||
|
||||
for(int i = 0; i < w; i++)
|
||||
{
|
||||
for(int j = 0; j < h; j++)
|
||||
{
|
||||
lua_pushvalue(L, 2);
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushnumber(L, j);
|
||||
pixel c = t->getPixel(i, j);
|
||||
lua_pushnumber(L, c.r);
|
||||
lua_pushnumber(L, c.g);
|
||||
lua_pushnumber(L, c.b);
|
||||
lua_pushnumber(L, c.a);
|
||||
lua_call(L, 6, 4);
|
||||
c.r = luaL_optint(L, -4, 0);
|
||||
c.g = luaL_optint(L, -3, 0);
|
||||
c.b = luaL_optint(L, -2, 0);
|
||||
c.a = luaL_optint(L, -1, 0);
|
||||
t->setPixel(i, j, c);
|
||||
lua_pop(L, 4);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _wrap_ImageData_getString(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
lua_pushlstring(L, (const char *)t->getData(), t->getSize());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _wrap_ImageData_paste(lua_State * L)
|
||||
{
|
||||
ImageData * t = luax_checkimagedata(L, 1);
|
||||
ImageData * src = luax_checkimagedata(L, 2);
|
||||
int dx = luaL_checkint(L, 3);
|
||||
int dy = luaL_checkint(L, 4);
|
||||
int sx = luaL_optint(L, 5, 0);
|
||||
int sy = luaL_optint(L, 6, 0);
|
||||
int sw = luaL_optint(L, 7, src->getWidth());
|
||||
int sh = luaL_optint(L, 8, src->getHeight());
|
||||
t->paste((love::image::ImageData *)src, dx, dy, sx, sy, sw, sh);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg wrap_ImageData_functions[] = {
|
||||
|
||||
// Data
|
||||
{ "getPointer", _wrap_Data_getPointer },
|
||||
{ "getSize", _wrap_Data_getSize },
|
||||
|
||||
{ "getWidth", _wrap_ImageData_getWidth },
|
||||
{ "getHeight", _wrap_ImageData_getHeight },
|
||||
{ "getPixel", _wrap_ImageData_getPixel },
|
||||
{ "setPixel", _wrap_ImageData_setPixel },
|
||||
{ "mapPixel", _wrap_ImageData_mapPixel },
|
||||
{ "getString", _wrap_ImageData_getString },
|
||||
{ "paste", _wrap_ImageData_paste },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
int wrap_ImageData_open(lua_State * L)
|
||||
{
|
||||
luax_register_type(L, "ImageData", wrap_ImageData_functions);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // image
|
||||
} // love
|
||||
@@ -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.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_IMAGE_WRAP_IMAGE_DATA_H
|
||||
#define LOVE_IMAGE_WRAP_IMAGE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include <common/runtime.h>
|
||||
#include "ImageData.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace image
|
||||
{
|
||||
ImageData * luax_checkimagedata(lua_State * L, int idx);
|
||||
int _wrap_ImageData_getWidth(lua_State * L);
|
||||
int _wrap_ImageData_getHeight(lua_State * L);
|
||||
int _wrap_ImageData_getPixel(lua_State * L);
|
||||
int _wrap_ImageData_setPixel(lua_State * L);
|
||||
int _wrap_ImageData_mapPixel(lua_State * L);
|
||||
int _wrap_ImageData_getString(lua_State * L);
|
||||
int _wrap_ImageData_paste(lua_State * L);
|
||||
int wrap_ImageData_open(lua_State * L);
|
||||
|
||||
} // image
|
||||
} // love
|
||||
|
||||
#endif // LOVE_IMAGE_WRAP_IMAGE_DATA_H
|
||||
Reference in New Issue
Block a user