ImageData.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright (c) 2006-2013 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. #include "ImageData.h"
  21. #include <stdio.h>
  22. using love::thread::Lock;
  23. namespace love
  24. {
  25. namespace image
  26. {
  27. ImageData::ImageData()
  28. {
  29. mutex = thread::newMutex();
  30. }
  31. ImageData::~ImageData()
  32. {
  33. delete mutex;
  34. }
  35. void *ImageData::getData() const
  36. {
  37. return data;
  38. }
  39. int ImageData::getSize() const
  40. {
  41. return getWidth()*getHeight()*sizeof(pixel);
  42. }
  43. int ImageData::getWidth() const
  44. {
  45. return width;
  46. }
  47. int ImageData::getHeight() const
  48. {
  49. return height;
  50. }
  51. bool ImageData::inside(int x, int y) const
  52. {
  53. return x >= 0 && x < getWidth() && y >= 0 && y < getHeight();
  54. }
  55. void ImageData::setPixel(int x, int y, pixel c)
  56. {
  57. if (!inside(x, y))
  58. throw love::Exception("Attempt to set out-of-range pixel!");
  59. Lock lock(mutex);
  60. pixel *pixels = (pixel *)getData();
  61. pixels[y*getWidth()+x] = c;
  62. }
  63. pixel ImageData::getPixel(int x, int y)
  64. {
  65. if (!inside(x, y))
  66. throw love::Exception("Attempt to get out-of-range pixel!");
  67. pixel *pixels = (pixel *)getData();
  68. return pixels[y*getWidth()+x];
  69. }
  70. void ImageData::paste(ImageData *src, int dx, int dy, int sx, int sy, int sw, int sh)
  71. {
  72. Lock lock2(src->mutex);
  73. Lock lock1(mutex);
  74. pixel *s = (pixel *)src->getData();
  75. pixel *d = (pixel *)getData();
  76. // Check bounds; if the data ends up completely out of bounds, get out early.
  77. if (sx >= src->getWidth() || sx + sw < 0 || sy >= src->getHeight() || sy + sh < 0
  78. || dx >= getWidth() || dx + sw < 0 || dy >= getHeight() || dy + sh < 0)
  79. return;
  80. // Normalize values to the inside of both images.
  81. if (dx < 0)
  82. {
  83. sw += dx;
  84. sx -= dx;
  85. dx = 0;
  86. }
  87. if (dy < 0)
  88. {
  89. sh += dy;
  90. sy -= dy;
  91. dy = 0;
  92. }
  93. if (sx < 0)
  94. {
  95. sw += sx;
  96. dx -= sx;
  97. sx = 0;
  98. }
  99. if (sy < 0)
  100. {
  101. sh += sy;
  102. dy -= sy;
  103. sy = 0;
  104. }
  105. if (dx + sw > getWidth())
  106. {
  107. sw = getWidth() - dx;
  108. }
  109. if (dy + sh > getHeight())
  110. {
  111. sh = getHeight() - dy;
  112. }
  113. if (sx + sw > src->getWidth())
  114. {
  115. sw = src->getWidth() - sx;
  116. }
  117. if (sy + sh > src->getHeight())
  118. {
  119. sh = src->getHeight() - sy;
  120. }
  121. // If the dimensions match up, copy the entire memory stream in one go
  122. if (sw == getWidth() && getWidth() == src->getWidth()
  123. && sh == getHeight() && getHeight() == src->getHeight())
  124. memcpy(d, s, sizeof(pixel) * sw * sh);
  125. else if (sw > 0) // Otherwise, copy each row individually
  126. {
  127. for (int i = 0; i < sh; i++)
  128. {
  129. memcpy(d + dx + (i + dy) * getWidth(), s + sx + (i + sy) * src->getWidth(), sizeof(pixel) * sw);
  130. }
  131. }
  132. }
  133. bool ImageData::getConstant(const char *in, ImageData::Format &out)
  134. {
  135. return formats.find(in, out);
  136. }
  137. bool ImageData::getConstant(ImageData::Format in, const char *&out)
  138. {
  139. return formats.find(in, out);
  140. }
  141. StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM>::Entry ImageData::formatEntries[] =
  142. {
  143. {"tga", ImageData::FORMAT_TGA},
  144. {"bmp", ImageData::FORMAT_BMP},
  145. {"jpg", ImageData::FORMAT_JPG},
  146. {"png", ImageData::FORMAT_PNG},
  147. };
  148. StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM> ImageData::formats(ImageData::formatEntries, sizeof(ImageData::formatEntries));
  149. } // image
  150. } // love