Mercurial > love-experiments
annotate src/modules/image/devil/ImageData.cpp @ 1098:a74672ba1320 minor
Merge in channels from love-experiments
Rewritten love.thread and changed communication
method from weird system before, to channels.
| author | Bart van Strien <bart.bes+projects@gmail.com> |
|---|---|
| date | Wed, 04 Jul 2012 17:15:16 +0200 |
| parents | 86a44f2bc38f |
| children | 730ed5f01ab0 |
| rev | line source |
|---|---|
| 1096 | 1 /** |
| 2 * Copyright (c) 2006-2012 LOVE Development Team | |
| 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(); | |
| 56 load(data); | |
| 57 data->release(); | |
| 58 } | |
| 59 | |
| 60 ImageData::ImageData(int width, int height) | |
| 61 { | |
| 62 this->width = width; | |
| 63 this->height = height; | |
| 64 create(width, height); | |
| 65 | |
| 66 // Set to black. | |
| 67 memset(data, 0, width*height*4); | |
| 68 } | |
| 69 | |
| 70 ImageData::ImageData(int width, int height, void *data) | |
| 71 { | |
| 72 this->width = width; | |
| 73 this->height = height; | |
| 74 create(width, height, data); | |
| 75 } | |
| 76 | |
| 77 ImageData::~ImageData() | |
| 78 { | |
| 79 delete[] data; | |
| 80 } | |
| 81 | |
| 82 void ImageData::create(int width, int height, void *data) | |
| 83 { | |
| 84 try | |
| 85 { | |
| 86 this->data = new unsigned char[width*height*sizeof(pixel)]; | |
| 87 } | |
| 88 catch(std::bad_alloc &) | |
| 89 { | |
| 90 throw love::Exception("Out of memory"); | |
| 91 } | |
| 92 | |
| 93 if (data) | |
| 94 memcpy(this->data, data, width*height*sizeof(pixel)); | |
| 95 } | |
| 96 | |
| 97 void ImageData::load(Data *data) | |
| 98 { | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
99 if (!devilMutex) |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
100 devilMutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
101 |
| 1096 | 102 Lock lock(devilMutex); |
| 103 ILuint image; | |
| 104 ilGenImages(1, &image); | |
| 105 ilBindImage(image); | |
| 106 | |
| 107 try | |
| 108 { | |
| 109 bool success = IL_TRUE == ilLoadL(IL_TYPE_UNKNOWN, (void *)data->getData(), data->getSize()); | |
| 110 | |
| 111 if (!success) | |
| 112 throw love::Exception("Could not decode image!"); | |
| 113 | |
| 114 width = ilGetInteger(IL_IMAGE_WIDTH); | |
| 115 height = ilGetInteger(IL_IMAGE_HEIGHT); | |
| 116 | |
| 117 // Make sure the image is in RGBA format. | |
| 118 ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); | |
| 119 | |
| 120 // This should always be four. | |
| 121 int bpp = ilGetInteger(IL_IMAGE_BPP); | |
| 122 if (bpp != sizeof(pixel)) | |
| 123 throw love::Exception("Could not convert image!"); | |
| 124 | |
| 125 create(width, height, ilGetData()); | |
| 126 } | |
| 127 catch(std::exception &) | |
| 128 { | |
| 129 ilDeleteImages(1, &image); | |
| 130 throw; | |
| 131 } | |
| 132 | |
| 133 ilDeleteImages(1, &image); | |
| 134 } | |
| 135 | |
| 136 void ImageData::encode(love::filesystem::File *f, ImageData::Format format) | |
| 137 { | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
138 if (!devilMutex) |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
139 devilMutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
140 |
| 1096 | 141 Lock lock1(devilMutex); |
| 142 Lock lock2(mutex); | |
| 143 | |
| 144 ILuint tempimage; | |
| 145 ilGenImages(1, &tempimage); | |
| 146 ilBindImage(tempimage); | |
| 147 ilxClearErrors(); | |
| 148 | |
| 149 ILubyte *encoded_data = NULL; | |
| 150 | |
| 151 try | |
| 152 { | |
| 153 bool success = IL_TRUE == ilTexImage(width, height, 1, sizeof(pixel), IL_RGBA, IL_UNSIGNED_BYTE, this->data); | |
| 154 | |
| 155 ILenum err = ilGetError(); | |
| 156 ilxClearErrors(); | |
| 157 | |
| 158 if (!success) | |
| 159 { | |
| 160 if (err != IL_NO_ERROR) | |
| 161 { | |
| 162 switch (err) | |
| 163 { | |
| 164 case IL_ILLEGAL_OPERATION: | |
| 165 throw love::Exception("Illegal operation"); | |
| 166 case IL_INVALID_PARAM: | |
| 167 throw love::Exception("Invalid parameters"); | |
| 168 case IL_OUT_OF_MEMORY: | |
| 169 throw love::Exception("Out of memory"); | |
| 170 default: | |
| 171 throw love::Exception("Unknown error (%d)", (int) err); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 throw love::Exception("Could not create image for the encoding!"); | |
| 176 } | |
| 177 | |
| 178 ilRegisterOrigin(IL_ORIGIN_UPPER_LEFT); | |
| 179 | |
| 180 ILuint ilFormat; | |
| 181 switch (format) | |
| 182 { | |
| 183 case ImageData::FORMAT_BMP: | |
| 184 ilFormat = IL_BMP; | |
| 185 break; | |
| 186 case ImageData::FORMAT_TGA: | |
| 187 ilFormat = IL_TGA; | |
| 188 break; | |
| 189 case ImageData::FORMAT_GIF: | |
| 190 ilFormat = IL_GIF; | |
| 191 break; | |
| 192 case ImageData::FORMAT_JPG: | |
| 193 ilFormat = IL_JPG; | |
| 194 break; | |
| 195 case ImageData::FORMAT_PNG: | |
| 196 default: // PNG is the default format | |
| 197 ilFormat = IL_PNG; | |
| 198 break; | |
| 199 } | |
| 200 | |
| 201 ILuint size = ilSaveL(ilFormat, NULL, 0); | |
| 202 if (!size) | |
| 203 throw love::Exception("Could not encode image!"); | |
| 204 | |
| 205 try | |
| 206 { | |
| 207 encoded_data = new ILubyte[size]; | |
| 208 } | |
| 209 catch(std::bad_alloc &) | |
| 210 { | |
| 211 throw love::Exception("Out of memory"); | |
| 212 } | |
| 213 | |
| 214 ilSaveL(ilFormat, encoded_data, size); | |
| 215 | |
| 216 f->open(love::filesystem::File::WRITE); | |
| 217 f->write(encoded_data, size); | |
| 218 f->close(); | |
| 219 } | |
| 220 catch(std::exception &) | |
| 221 { | |
| 222 ilDeleteImages(1, &tempimage); | |
| 223 delete[] encoded_data; | |
| 224 throw; | |
| 225 } | |
| 226 | |
| 227 ilDeleteImages(1, &tempimage); | |
| 228 delete[] encoded_data; | |
| 229 } | |
| 230 | |
| 231 } // devil | |
| 232 } // image | |
| 233 } // love |
