readded support for screenshots - love.graphics.newScreenshot() returns ImageData that can be manipulated as one pleases

This commit is contained in:
bill@Ixion
2009-09-01 03:56:00 -05:00
parent b8677cebd0
commit cece5fe625
11 changed files with 475 additions and 364 deletions
+48 -39
View File
@@ -1,21 +1,21 @@
/**
* 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.
/**
* 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
@@ -45,28 +45,37 @@ namespace image
* 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 new ImageData from a raw Data.
* @param data The object containing encoded pixel data.
* @return The new ImageData.
**/
virtual ImageData * newImageData(Data * data) = 0;
/**
* Creates empty ImageData with the given size.
* @param The width of the ImageData.
* @param The height of the ImageData.
* @return The new ImageData.
**/
/**
* 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 new ImageData from a raw Data.
* @param data The object containing encoded pixel data.
* @return The new ImageData.
**/
virtual ImageData * newImageData(Data * data) = 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;
/**
* Creates empty ImageData with the given size.
* @param The width of the ImageData.
* @param The height of the ImageData.
* @param The data to load into the ImageData.
* @return The new ImageData.
**/
virtual ImageData * newImageData(int width, int height, void *data) = 0;
}; // Image
+41 -31
View File
@@ -1,21 +1,21 @@
/**
* 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.
/**
* 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"
@@ -46,19 +46,29 @@ namespace devil
return "love.image.devil";
}
love::image::ImageData * Image::newImageData(love::filesystem::File * file)
{
return new ImageData(file);
}
love::image::ImageData * Image::newImageData(Data * data)
{
return new ImageData(data);
}
love::image::ImageData * Image::newImageData(int width, int height)
{
return new ImageData(width, height);
love::image::ImageData * Image::newImageData(love::filesystem::File * file)
{
return new ImageData(file);
}
love::image::ImageData * Image::newImageData(Data * data)
{
try {
return new ImageData(data);
} catch (love::Exception & e) {
throw love::Exception(e.what());
}
}
love::image::ImageData * Image::newImageData(int width, int height)
{
return new ImageData(width, height);
}
love::image::ImageData * Image::newImageData(int width, int height, void *data)
{
return new ImageData(width, height, data);
}
} // devil
+22 -21
View File
@@ -1,21 +1,21 @@
/**
* 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.
/**
* 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
@@ -39,10 +39,11 @@ namespace devil
// Implements Module.
const char * getName() const;
love::image::ImageData * newImageData(love::filesystem::File * file);
love::image::ImageData * newImageData(Data * data);
love::image::ImageData * newImageData(love::filesystem::File * file);
love::image::ImageData * newImageData(Data * data);
love::image::ImageData * newImageData(int width, int height);
love::image::ImageData * newImageData(int width, int height, void *data);
}; // Image
+159 -124
View File
@@ -1,135 +1,170 @@
/**
* 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"
/**
* 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
{
void ImageData::load(Data * data)
{
#include <iostream>
// LOVE
#include <common/Exception.h>
namespace love
{
namespace image
{
namespace devil
{
void ImageData::load(Data * data)
{
// Generate DevIL image.
ilGenImages(1, &image);
ilGenImages(1, &image);
// Bind the image.
ilBindImage(image);
ilBindImage(image);
// Try to load the image.
ILboolean success = ilLoadL(IL_TYPE_UNKNOWN, (void*)data->getData(), data->getSize());
ILboolean success = ilLoadL(IL_TYPE_UNKNOWN, (void*)data->getData(), data->getSize());
// Check for errors
if(!success)
{
std::cerr << "Could not decode image." << std::endl;
throw love::Exception("Could not decode image!");
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(Data * data)
{
load(data);
}
ImageData::ImageData(filesystem::File * file)
{
Data * data = file->read();
load(data);
data->release();
}
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, 1, 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
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(Data * data)
{
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
load(data);
}
ImageData::ImageData(filesystem::File * file)
{
Data * data = file->read();
load(data);
data->release();
}
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, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, 0);
}
ImageData::ImageData(int width, int height, void *data)
: width(width), height(height), origin(IL_ORIGIN_UPPER_LEFT), bpp(4)
{
// Generate DevIL image.
ilGenImages(1, &image);
// Bind the image.
ilBindImage(image);
// Try to load the data.
bool success = ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data);
int err = ilGetError();
if (err != IL_NO_ERROR){
switch (err) {
case IL_ILLEGAL_OPERATION:
throw love::Exception("Error: Illegal operation");
break;
case IL_INVALID_PARAM:
throw love::Exception("Error: invalid parameters");
break;
case IL_OUT_OF_MEMORY:
throw love::Exception("Error: out of memory");
break;
default:
throw love::Exception("Error: unknown error");
break;
}
}
if(!success) {
throw love::Exception("Could not decode image data.");
}
}
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
+1
View File
@@ -60,6 +60,7 @@ namespace devil
ImageData(Data * data);
ImageData(love::filesystem::File * file);
ImageData(int width, int height);
ImageData(int width, int height, void *data);
virtual ~ImageData();
// Implements Data.
+6 -1
View File
@@ -45,7 +45,12 @@ namespace image
if(luax_istype(L, 1, DATA_T))
{
Data * d = luax_checktype<Data>(L, 1, "Data", DATA_T);
ImageData * t = instance->newImageData(d);
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;
}