Mercurial > love-experiments
annotate src/modules/image/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 | 730ed5f01ab0 |
| children | 2a5ef265e040 3242694a254e |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1158
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 | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
23 #include <stdio.h> |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
24 |
| 1096 | 25 using love::thread::Lock; |
| 26 | |
| 27 namespace love | |
| 28 { | |
| 29 namespace image | |
| 30 { | |
| 31 | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
32 ImageData::ImageData() |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
33 { |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
34 mutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
35 } |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
36 |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
37 ImageData::~ImageData() |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
38 { |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
39 delete mutex; |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
40 } |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
41 |
| 1096 | 42 void *ImageData::getData() const |
| 43 { | |
| 44 return data; | |
| 45 } | |
| 46 | |
| 47 int ImageData::getSize() const | |
| 48 { | |
| 49 return getWidth()*getHeight()*sizeof(pixel); | |
| 50 } | |
| 51 | |
| 52 int ImageData::getWidth() const | |
| 53 { | |
| 54 return width; | |
| 55 } | |
| 56 | |
| 57 int ImageData::getHeight() const | |
| 58 { | |
| 59 return height; | |
| 60 } | |
| 61 | |
| 62 bool ImageData::inside(int x, int y) const | |
| 63 { | |
| 64 return x >= 0 && x < getWidth() && y >= 0 && y < getHeight(); | |
| 65 } | |
| 66 | |
| 67 void ImageData::setPixel(int x, int y, pixel c) | |
| 68 { | |
| 69 if (!inside(x, y)) | |
| 70 throw love::Exception("Attempt to set out-of-range pixel!"); | |
| 71 | |
| 72 Lock lock(mutex); | |
| 73 | |
| 74 pixel *pixels = (pixel *)getData(); | |
| 75 pixels[y*getWidth()+x] = c; | |
| 76 } | |
| 77 | |
| 78 pixel ImageData::getPixel(int x, int y) | |
| 79 { | |
| 80 if (!inside(x, y)) | |
| 81 throw love::Exception("Attempt to get out-of-range pixel!"); | |
| 82 | |
| 83 Lock lock(mutex); | |
| 84 | |
| 85 pixel *pixels = (pixel *)getData(); | |
| 86 return pixels[y*getWidth()+x]; | |
| 87 } | |
| 88 | |
| 89 void ImageData::paste(ImageData *src, int dx, int dy, int sx, int sy, int sw, int sh) | |
| 90 { | |
| 91 Lock lock2(src->mutex); | |
| 92 Lock lock1(mutex); | |
| 93 | |
| 94 pixel *s = (pixel *)src->getData(); | |
| 95 pixel *d = (pixel *)getData(); | |
| 96 | |
| 97 // Check bounds; if the data ends up completely out of bounds, get out early. | |
| 98 if (sx >= src->getWidth() || sx + sw < 0 || sy >= src->getHeight() || sy + sh < 0 | |
| 99 || dx >= getWidth() || dx + sw < 0 || dy >= getHeight() || dy + sh < 0) | |
| 100 return; | |
| 101 | |
| 102 // Normalize values to the inside of both images. | |
| 103 if (dx < 0) | |
| 104 { | |
| 105 sw += dx; | |
| 106 sx -= dx; | |
| 107 dx = 0; | |
| 108 } | |
| 109 if (dy < 0) | |
| 110 { | |
| 111 sh += dy; | |
| 112 sy -= dy; | |
| 113 dy = 0; | |
| 114 } | |
| 115 if (sx < 0) | |
| 116 { | |
| 117 sw += sx; | |
| 118 dx -= sx; | |
| 119 sx = 0; | |
| 120 } | |
| 121 if (sy < 0) | |
| 122 { | |
| 123 sh += sy; | |
| 124 dy -= sy; | |
| 125 sy = 0; | |
| 126 } | |
| 127 if (dx + sw > getWidth()) | |
| 128 { | |
| 129 sw = getWidth() - dx; | |
| 130 } | |
| 131 if (dy + sh > getHeight()) | |
| 132 { | |
| 133 sh = getHeight() - dy; | |
| 134 } | |
| 135 if (sx + sw > src->getWidth()) | |
| 136 { | |
| 137 sw = src->getWidth() - sx; | |
| 138 } | |
| 139 if (sy + sh > src->getHeight()) | |
| 140 { | |
| 141 sh = src->getHeight() - sy; | |
| 142 } | |
| 143 | |
| 144 // If the dimensions match up, copy the entire memory stream in one go | |
| 145 if (sw == getWidth() && getWidth() == src->getWidth() | |
| 146 && sh == getHeight() && getHeight() == src->getHeight()) | |
| 147 memcpy(d, s, sizeof(pixel) * sw * sh); | |
|
1158
82df76da9b39
Fix issue #514: Redundant if-clause in ImageData::paste
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
148 else if (sw > 0) // Otherwise, copy each row individually |
| 1096 | 149 { |
| 150 for (int i = 0; i < sh; i++) | |
| 151 { | |
| 152 memcpy(d + dx + (i + dy) * getWidth(), s + sx + (i + sy) * src->getWidth(), sizeof(pixel) * sw); | |
| 153 } | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 bool ImageData::getConstant(const char *in, ImageData::Format &out) | |
| 158 { | |
| 159 return formats.find(in, out); | |
| 160 } | |
| 161 | |
| 162 bool ImageData::getConstant(ImageData::Format in, const char *&out) | |
| 163 { | |
| 164 return formats.find(in, out); | |
| 165 } | |
| 166 | |
| 167 StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM>::Entry ImageData::formatEntries[] = | |
| 168 { | |
| 169 {"tga", ImageData::FORMAT_TGA}, | |
| 170 {"bmp", ImageData::FORMAT_BMP}, | |
| 171 {"jpg", ImageData::FORMAT_JPG}, | |
| 172 {"png", ImageData::FORMAT_PNG}, | |
| 173 }; | |
| 174 | |
| 175 StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM> ImageData::formats(ImageData::formatEntries, sizeof(ImageData::formatEntries)); | |
| 176 | |
| 177 } // image | |
| 178 } // love |
