Mercurial > love-experiments
annotate src/modules/image/ImageData.cpp @ 1448:108ee2e81f65 default tip
Fix a case typo, imageData.h -> ImageData.h
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sat, 18 Jan 2014 13:57:44 +0100 |
| parents | 59f5284fe9eb |
| children |
| 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 | |
| 23 using love::thread::Lock; | |
| 24 | |
| 25 namespace love | |
| 26 { | |
| 27 namespace image | |
| 28 { | |
| 29 | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
30 ImageData::ImageData() |
|
1422
377f9dad7a2d
Replaced the devil love.image module with love.image.magpie, which can decide which image library to use in a similar manner to love.sound.lullaby. Some rough edges still exist.
Alex Szpakowski <slime73@gmail.com>
parents:
1420
diff
changeset
|
31 : data(0) |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
32 { |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
33 mutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
34 } |
|
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 ImageData::~ImageData() |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
37 { |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
38 delete mutex; |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
39 } |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
40 |
|
1420
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
41 int ImageData::getSize() const |
|
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
42 { |
|
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
43 return getWidth()*getHeight()*sizeof(pixel); |
|
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
44 } |
|
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
45 |
| 1096 | 46 void *ImageData::getData() const |
| 47 { | |
| 48 return data; | |
| 49 } | |
| 50 | |
|
1420
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
51 bool ImageData::inside(int x, int y) const |
| 1096 | 52 { |
|
1420
2a5ef265e040
Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy.
Alex Szpakowski <slime73@gmail.com>
parents:
1395
diff
changeset
|
53 return x >= 0 && x < getWidth() && y >= 0 && y < getHeight(); |
| 1096 | 54 } |
| 55 | |
| 56 int ImageData::getWidth() const | |
| 57 { | |
| 58 return width; | |
| 59 } | |
| 60 | |
| 61 int ImageData::getHeight() const | |
| 62 { | |
| 63 return height; | |
| 64 } | |
| 65 | |
| 66 void ImageData::setPixel(int x, int y, pixel c) | |
| 67 { | |
| 68 if (!inside(x, y)) | |
| 69 throw love::Exception("Attempt to set out-of-range pixel!"); | |
| 70 | |
| 71 Lock lock(mutex); | |
| 72 | |
| 73 pixel *pixels = (pixel *)getData(); | |
| 74 pixels[y*getWidth()+x] = c; | |
| 75 } | |
| 76 | |
| 77 pixel ImageData::getPixel(int x, int y) | |
| 78 { | |
| 79 if (!inside(x, y)) | |
| 80 throw love::Exception("Attempt to get out-of-range pixel!"); | |
| 81 | |
| 82 pixel *pixels = (pixel *)getData(); | |
| 83 return pixels[y*getWidth()+x]; | |
| 84 } | |
| 85 | |
| 86 void ImageData::paste(ImageData *src, int dx, int dy, int sx, int sy, int sw, int sh) | |
| 87 { | |
| 88 Lock lock2(src->mutex); | |
| 89 Lock lock1(mutex); | |
| 90 | |
| 91 pixel *s = (pixel *)src->getData(); | |
| 92 pixel *d = (pixel *)getData(); | |
| 93 | |
| 94 // Check bounds; if the data ends up completely out of bounds, get out early. | |
| 95 if (sx >= src->getWidth() || sx + sw < 0 || sy >= src->getHeight() || sy + sh < 0 | |
| 96 || dx >= getWidth() || dx + sw < 0 || dy >= getHeight() || dy + sh < 0) | |
| 97 return; | |
| 98 | |
| 99 // Normalize values to the inside of both images. | |
| 100 if (dx < 0) | |
| 101 { | |
| 102 sw += dx; | |
| 103 sx -= dx; | |
| 104 dx = 0; | |
| 105 } | |
| 106 if (dy < 0) | |
| 107 { | |
| 108 sh += dy; | |
| 109 sy -= dy; | |
| 110 dy = 0; | |
| 111 } | |
| 112 if (sx < 0) | |
| 113 { | |
| 114 sw += sx; | |
| 115 dx -= sx; | |
| 116 sx = 0; | |
| 117 } | |
| 118 if (sy < 0) | |
| 119 { | |
| 120 sh += sy; | |
| 121 dy -= sy; | |
| 122 sy = 0; | |
| 123 } | |
| 124 if (dx + sw > getWidth()) | |
| 125 { | |
| 126 sw = getWidth() - dx; | |
| 127 } | |
| 128 if (dy + sh > getHeight()) | |
| 129 { | |
| 130 sh = getHeight() - dy; | |
| 131 } | |
| 132 if (sx + sw > src->getWidth()) | |
| 133 { | |
| 134 sw = src->getWidth() - sx; | |
| 135 } | |
| 136 if (sy + sh > src->getHeight()) | |
| 137 { | |
| 138 sh = src->getHeight() - sy; | |
| 139 } | |
| 140 | |
| 141 // If the dimensions match up, copy the entire memory stream in one go | |
| 142 if (sw == getWidth() && getWidth() == src->getWidth() | |
| 143 && sh == getHeight() && getHeight() == src->getHeight()) | |
| 144 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
|
145 else if (sw > 0) // Otherwise, copy each row individually |
| 1096 | 146 { |
| 147 for (int i = 0; i < sh; i++) | |
| 148 { | |
| 149 memcpy(d + dx + (i + dy) * getWidth(), s + sx + (i + sy) * src->getWidth(), sizeof(pixel) * sw); | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 bool ImageData::getConstant(const char *in, ImageData::Format &out) | |
| 155 { | |
| 156 return formats.find(in, out); | |
| 157 } | |
| 158 | |
| 159 bool ImageData::getConstant(ImageData::Format in, const char *&out) | |
| 160 { | |
| 161 return formats.find(in, out); | |
| 162 } | |
| 163 | |
| 164 StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM>::Entry ImageData::formatEntries[] = | |
| 165 { | |
| 166 {"tga", ImageData::FORMAT_TGA}, | |
| 167 {"bmp", ImageData::FORMAT_BMP}, | |
| 168 {"jpg", ImageData::FORMAT_JPG}, | |
| 169 {"png", ImageData::FORMAT_PNG}, | |
| 170 }; | |
| 171 | |
| 172 StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM> ImageData::formats(ImageData::formatEntries, sizeof(ImageData::formatEntries)); | |
| 173 | |
| 174 } // image | |
| 175 } // love |
