ImageData.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. int ImageData::getSize() const
  36. {
  37. return getWidth()*getHeight()*sizeof(pixel);
  38. }
  39. void *ImageData::getData() const
  40. {
  41. return data;
  42. }
  43. bool ImageData::inside(int x, int y) const
  44. {
  45. return x >= 0 && x < getWidth() && y >= 0 && y < getHeight();
  46. }
  47. int ImageData::getWidth() const
  48. {
  49. return width;
  50. }
  51. int ImageData::getHeight() const
  52. {
  53. return height;
  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. Lock lock(mutex);
  68. pixel *pixels = (pixel *)getData();
  69. return pixels[y*getWidth()+x];
  70. }
  71. void ImageData::paste(ImageData *src, int dx, int dy, int sx, int sy, int sw, int sh)
  72. {
  73. Lock lock2(src->mutex);
  74. Lock lock1(mutex);
  75. pixel *s = (pixel *)src->getData();
  76. pixel *d = (pixel *)getData();
  77. // Check bounds; if the data ends up completely out of bounds, get out early.
  78. if (sx >= src->getWidth() || sx + sw < 0 || sy >= src->getHeight() || sy + sh < 0
  79. || dx >= getWidth() || dx + sw < 0 || dy >= getHeight() || dy + sh < 0)
  80. return;
  81. // Normalize values to the inside of both images.
  82. if (dx < 0)
  83. {
  84. sw += dx;
  85. sx -= dx;
  86. dx = 0;
  87. }
  88. if (dy < 0)
  89. {
  90. sh += dy;
  91. sy -= dy;
  92. dy = 0;
  93. }
  94. if (sx < 0)
  95. {
  96. sw += sx;
  97. dx -= sx;
  98. sx = 0;
  99. }
  100. if (sy < 0)
  101. {
  102. sh += sy;
  103. dy -= sy;
  104. sy = 0;
  105. }
  106. if (dx + sw > getWidth())
  107. {
  108. sw = getWidth() - dx;
  109. }
  110. if (dy + sh > getHeight())
  111. {
  112. sh = getHeight() - dy;
  113. }
  114. if (sx + sw > src->getWidth())
  115. {
  116. sw = src->getWidth() - sx;
  117. }
  118. if (sy + sh > src->getHeight())
  119. {
  120. sh = src->getHeight() - sy;
  121. }
  122. // If the dimensions match up, copy the entire memory stream in one go
  123. if (sw == getWidth() && getWidth() == src->getWidth()
  124. && sh == getHeight() && getHeight() == src->getHeight())
  125. memcpy(d, s, sizeof(pixel) * sw * sh);
  126. else if (sw > 0) // Otherwise, copy each row individually
  127. {
  128. for (int i = 0; i < sh; i++)
  129. {
  130. memcpy(d + dx + (i + dy) * getWidth(), s + sx + (i + sy) * src->getWidth(), sizeof(pixel) * sw);
  131. }
  132. }
  133. }
  134. bool ImageData::getConstant(const char *in, ImageData::Format &out)
  135. {
  136. return formats.find(in, out);
  137. }
  138. bool ImageData::getConstant(ImageData::Format in, const char *&out)
  139. {
  140. return formats.find(in, out);
  141. }
  142. StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM>::Entry ImageData::formatEntries[] =
  143. {
  144. {"tga", ImageData::FORMAT_TGA},
  145. {"bmp", ImageData::FORMAT_BMP},
  146. {"jpg", ImageData::FORMAT_JPG},
  147. {"png", ImageData::FORMAT_PNG},
  148. };
  149. StringMap<ImageData::Format, ImageData::FORMAT_MAX_ENUM> ImageData::formats(ImageData::formatEntries, sizeof(ImageData::formatEntries));
  150. } // image
  151. } // love