Mercurial > love-experiments
annotate src/modules/image/devil/ImageData.cpp @ 1395:d46820870f0a
Removed gif 'support' from ImageData:encode (it never worked, DevIL doesn't support saving gifs)
| author | Alex Szpakowski <slime73@gmail.com> |
|---|---|
| date | Thu, 21 Mar 2013 03:17:37 -0300 |
| parents | a742f6edaf92 |
| children | 09e4aec8f429 |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1096
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 3 * |
| 4 * This software is provided 'as-is', without any express or implied | |
| 5 * warranty. In no event will the authors be held liable for any damages | |
| 6 * arising from the use of this software. | |
| 7 * | |
| 8 * Permission is granted to anyone to use this software for any purpose, | |
| 9 * including commercial applications, and to alter it and redistribute it | |
| 10 * freely, subject to the following restrictions: | |
| 11 * | |
| 12 * 1. The origin of this software must not be misrepresented; you must not | |
| 13 * claim that you wrote the original software. If you use this software | |
| 14 * in a product, an acknowledgment in the product documentation would be | |
| 15 * appreciated but is not required. | |
| 16 * 2. Altered source versions must be plainly marked as such, and must not be | |
| 17 * misrepresented as being the original software. | |
| 18 * 3. This notice may not be removed or altered from any source distribution. | |
| 19 **/ | |
| 20 | |
| 21 #include "ImageData.h" | |
| 22 | |
| 23 // STD | |
| 24 #include <cstring> | |
| 25 #include <iostream> | |
| 26 | |
| 27 // LOVE | |
| 28 #include "common/Exception.h" | |
| 29 #include "common/math.h" | |
| 30 #include "filesystem/File.h" | |
| 31 | |
| 32 using love::thread::Lock; | |
| 33 | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
34 static Mutex *devilMutex = 0; |
| 1096 | 35 |
| 36 namespace love | |
| 37 { | |
| 38 namespace image | |
| 39 { | |
| 40 namespace devil | |
| 41 { | |
| 42 | |
| 43 static inline void ilxClearErrors() | |
| 44 { | |
| 45 while (ilGetError() != IL_NO_ERROR); | |
| 46 } | |
| 47 | |
| 48 ImageData::ImageData(Data *data) | |
| 49 { | |
| 50 load(data); | |
| 51 } | |
| 52 | |
| 53 ImageData::ImageData(filesystem::File *file) | |
| 54 { | |
| 55 Data *data = file->read(); | |
|
1391
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
56 try |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
57 { |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
58 load(data); |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
59 } |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
60 catch (love::Exception &) |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
61 { |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
62 data->release(); |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
63 throw; |
|
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
64 } |
| 1096 | 65 data->release(); |
| 66 } | |
| 67 | |
| 68 ImageData::ImageData(int width, int height) | |
| 69 { | |
| 70 this->width = width; | |
| 71 this->height = height; | |
| 72 create(width, height); | |
| 73 | |
| 74 // Set to black. | |
| 75 memset(data, 0, width*height*4); | |
| 76 } | |
| 77 | |
| 78 ImageData::ImageData(int width, int height, void *data) | |
| 79 { | |
| 80 this->width = width; | |
| 81 this->height = height; | |
| 82 create(width, height, data); | |
| 83 } | |
| 84 | |
| 85 ImageData::~ImageData() | |
| 86 { | |
| 87 delete[] data; | |
| 88 } | |
| 89 | |
| 90 void ImageData::create(int width, int height, void *data) | |
| 91 { | |
| 92 try | |
| 93 { | |
| 94 this->data = new unsigned char[width*height*sizeof(pixel)]; | |
| 95 } | |
| 96 catch(std::bad_alloc &) | |
| 97 { | |
| 98 throw love::Exception("Out of memory"); | |
| 99 } | |
| 100 | |
| 101 if (data) | |
| 102 memcpy(this->data, data, width*height*sizeof(pixel)); | |
| 103 } | |
| 104 | |
| 105 void ImageData::load(Data *data) | |
| 106 { | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
107 if (!devilMutex) |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
108 devilMutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
109 |
| 1096 | 110 Lock lock(devilMutex); |
| 111 ILuint image; | |
| 112 ilGenImages(1, &image); | |
| 113 ilBindImage(image); | |
| 114 | |
| 115 try | |
| 116 { | |
| 117 bool success = IL_TRUE == ilLoadL(IL_TYPE_UNKNOWN, (void *)data->getData(), data->getSize()); | |
| 118 | |
| 119 if (!success) | |
| 120 throw love::Exception("Could not decode image!"); | |
| 121 | |
| 122 width = ilGetInteger(IL_IMAGE_WIDTH); | |
| 123 height = ilGetInteger(IL_IMAGE_HEIGHT); | |
| 124 | |
| 125 // Make sure the image is in RGBA format. | |
| 126 ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); | |
| 127 | |
| 128 // This should always be four. | |
| 129 int bpp = ilGetInteger(IL_IMAGE_BPP); | |
| 130 if (bpp != sizeof(pixel)) | |
| 131 throw love::Exception("Could not convert image!"); | |
| 132 | |
| 133 create(width, height, ilGetData()); | |
| 134 } | |
|
1391
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
135 catch(std::exception &e) |
| 1096 | 136 { |
| 137 ilDeleteImages(1, &image); | |
|
1391
a742f6edaf92
Fixed filedata not being cleaned up when love.image.newImageData(filepath) fails
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
138 throw love::Exception("%s", e.what()); |
| 1096 | 139 } |
| 140 | |
| 141 ilDeleteImages(1, &image); | |
| 142 } | |
| 143 | |
| 144 void ImageData::encode(love::filesystem::File *f, ImageData::Format format) | |
| 145 { | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
146 if (!devilMutex) |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
147 devilMutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
148 |
| 1096 | 149 Lock lock1(devilMutex); |
| 150 Lock lock2(mutex); | |
| 151 | |
| 152 ILuint tempimage; | |
| 153 ilGenImages(1, &tempimage); | |
| 154 ilBindImage(tempimage); | |
| 155 ilxClearErrors(); | |
| 156 | |
| 157 ILubyte *encoded_data = NULL; | |
| 158 | |
| 159 try | |
| 160 { | |
| 161 bool success = IL_TRUE == ilTexImage(width, height, 1, sizeof(pixel), IL_RGBA, IL_UNSIGNED_BYTE, this->data); | |
| 162 | |
| 163 ILenum err = ilGetError(); | |
| 164 ilxClearErrors(); | |
| 165 | |
| 166 if (!success) | |
| 167 { | |
| 168 if (err != IL_NO_ERROR) | |
| 169 { | |
| 170 switch (err) | |
| 171 { | |
| 172 case IL_ILLEGAL_OPERATION: | |
| 173 throw love::Exception("Illegal operation"); | |
| 174 case IL_INVALID_PARAM: | |
| 175 throw love::Exception("Invalid parameters"); | |
| 176 case IL_OUT_OF_MEMORY: | |
| 177 throw love::Exception("Out of memory"); | |
| 178 default: | |
| 179 throw love::Exception("Unknown error (%d)", (int) err); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 throw love::Exception("Could not create image for the encoding!"); | |
| 184 } | |
| 185 | |
| 186 ilRegisterOrigin(IL_ORIGIN_UPPER_LEFT); | |
| 187 | |
| 188 ILuint ilFormat; | |
| 189 switch (format) | |
| 190 { | |
| 191 case ImageData::FORMAT_BMP: | |
| 192 ilFormat = IL_BMP; | |
| 193 break; | |
| 194 case ImageData::FORMAT_TGA: | |
| 195 ilFormat = IL_TGA; | |
| 196 break; | |
| 197 case ImageData::FORMAT_JPG: | |
| 198 ilFormat = IL_JPG; | |
| 199 break; | |
| 200 case ImageData::FORMAT_PNG: | |
| 201 default: // PNG is the default format | |
| 202 ilFormat = IL_PNG; | |
| 203 break; | |
| 204 } | |
| 205 | |
| 206 ILuint size = ilSaveL(ilFormat, NULL, 0); | |
| 207 if (!size) | |
| 208 throw love::Exception("Could not encode image!"); | |
| 209 | |
| 210 try | |
| 211 { | |
| 212 encoded_data = new ILubyte[size]; | |
| 213 } | |
| 214 catch(std::bad_alloc &) | |
| 215 { | |
| 216 throw love::Exception("Out of memory"); | |
| 217 } | |
| 218 | |
| 219 ilSaveL(ilFormat, encoded_data, size); | |
| 220 | |
| 221 f->open(love::filesystem::File::WRITE); | |
| 222 f->write(encoded_data, size); | |
| 223 f->close(); | |
| 224 } | |
| 225 catch(std::exception &) | |
| 226 { | |
| 227 ilDeleteImages(1, &tempimage); | |
| 228 delete[] encoded_data; | |
| 229 throw; | |
| 230 } | |
| 231 | |
| 232 ilDeleteImages(1, &tempimage); | |
| 233 delete[] encoded_data; | |
| 234 } | |
| 235 | |
| 236 } // devil | |
| 237 } // image | |
| 238 } // love |
