Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. void ImageDataUtil::CopyPixels (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, Image* alphaImage, Vector2* alphaPoint, bool mergeAlpha) {
  2.  
  3. uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->Data ();
  4. uint8_t* destData = (uint8_t*)image->buffer->data->Data ();
  5.  
  6. ImageDataView sourceView = ImageDataView (sourceImage, sourceRect);
  7. Rectangle destRect = Rectangle (destPoint->x, destPoint->y, sourceView.width, sourceView.height);
  8. ImageDataView destView = ImageDataView (image, &destRect);
  9.  
  10. PixelFormat sourceFormat = sourceImage->buffer->format;
  11. PixelFormat destFormat = image->buffer->format;
  12. bool sourcePremultiplied = sourceImage->buffer->premultiplied;
  13. bool destPremultiplied = image->buffer->premultiplied;
  14.  
  15. int sourcePosition, destPosition;
  16. RGBA sourcePixel;
  17.  
  18. if (!mergeAlpha || !sourceImage->buffer->transparent) {
  19.  
  20. if(sourceFormat == destFormat && sourcePremultiplied == destPremultiplied) {
  21.  
  22. //I need the syntax for an ImageBuffer.blit() call or something like that to draw the whole rectangle
  23. //in one whack (as a series of single-row memcpy()'s rather than read/write each pixel by hand
  24. //trying this for now:
  25.  
  26. for (int y = 0; y < destView.height; y++) {
  27.  
  28. sourcePosition = sourceView.Row (y);
  29. destPosition = destView.Row (y);
  30.  
  31. image->buffer->BlitRow(sourceData, sourcePosition, destPosition, sourceView.width, destView.x, destView.y+y);
  32.  
  33. }
  34.  
  35. }
  36. else {
  37.  
  38. for (int y = 0; y < destView.height; y++) {
  39.  
  40. sourcePosition = sourceView.Row (y);
  41. destPosition = destView.Row (y);
  42.  
  43. for (int x = 0; x < destView.width; x++) {
  44.  
  45. sourcePixel.ReadUInt8 (sourceData, sourcePosition, sourceFormat, sourcePremultiplied);
  46. sourcePixel.WriteUInt8 (destData, destPosition, destFormat, destPremultiplied);
  47.  
  48. sourcePosition += 4;
  49. destPosition += 4;
  50.  
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. } else {
  58.  
  59. float sourceAlpha, destAlpha, oneMinusSourceAlpha, blendAlpha;
  60. RGBA destPixel;
  61.  
  62. if (alphaImage == 0) {
  63.  
  64. for (int y = 0; y < destView.height; y++) {
  65.  
  66. sourcePosition = sourceView.Row (y);
  67. destPosition = destView.Row (y);
  68.  
  69. for (int x = 0; x < destView.width; x++) {
  70.  
  71. sourcePixel.ReadUInt8 (sourceData, sourcePosition, sourceFormat, sourcePremultiplied);
  72. destPixel.ReadUInt8 (destData, destPosition, destFormat, destPremultiplied);
  73.  
  74. sourceAlpha = sourcePixel.a / 255.0;
  75. destAlpha = destPixel.a / 255.0;
  76. oneMinusSourceAlpha = 1 - sourceAlpha;
  77. blendAlpha = sourceAlpha + (destAlpha * oneMinusSourceAlpha);
  78.  
  79. if (blendAlpha == 0) {
  80.  
  81. destPixel.Set (0, 0, 0, 0);
  82.  
  83. } else {
  84.  
  85. destPixel.r = __clamp[int (0.5 + (sourcePixel.r * sourceAlpha + destPixel.r * destAlpha * oneMinusSourceAlpha) / blendAlpha)];
  86. destPixel.g = __clamp[int (0.5 + (sourcePixel.g * sourceAlpha + destPixel.g * destAlpha * oneMinusSourceAlpha) / blendAlpha)];
  87. destPixel.b = __clamp[int (0.5 + (sourcePixel.b * sourceAlpha + destPixel.b * destAlpha * oneMinusSourceAlpha) / blendAlpha)];
  88. destPixel.a = __clamp[int (0.5 + blendAlpha * 255.0)];
  89.  
  90. }
  91.  
  92. destPixel.WriteUInt8 (destData, destPosition, destFormat, destPremultiplied);
  93.  
  94. sourcePosition += 4;
  95. destPosition += 4;
  96.  
  97. }
  98.  
  99. }
  100.  
  101. } else {
  102.  
  103. uint8_t* alphaData = (uint8_t*)alphaImage->buffer->data->Data ();
  104. PixelFormat alphaFormat = alphaImage->buffer->format;
  105. bool alphaPremultiplied = alphaImage->buffer->premultiplied;
  106.  
  107. Rectangle alphaRect = Rectangle (alphaPoint->x, alphaPoint->y, destView.width, destView.height);
  108. ImageDataView alphaView = ImageDataView (alphaImage, &alphaRect);
  109. int alphaPosition;
  110. RGBA alphaPixel;
  111.  
  112. for (int y = 0; y < alphaView.height; y++) {
  113.  
  114. sourcePosition = sourceView.Row (y);
  115. destPosition = destView.Row (y);
  116. alphaPosition = alphaView.Row (y);
  117.  
  118. for (int x = 0; x < alphaView.width; x++) {
  119.  
  120. sourcePixel.ReadUInt8 (sourceData, sourcePosition, sourceFormat, sourcePremultiplied);
  121. destPixel.ReadUInt8 (destData, destPosition, destFormat, destPremultiplied);
  122. alphaPixel.ReadUInt8 (alphaData, alphaPosition, alphaFormat, alphaPremultiplied);
  123.  
  124. sourceAlpha = alphaPixel.a / 0xFF;
  125. destAlpha = destPixel.a / 0xFF;
  126. oneMinusSourceAlpha = 1 - sourceAlpha;
  127. blendAlpha = sourceAlpha + (destAlpha * oneMinusSourceAlpha);
  128.  
  129. if (blendAlpha == 0) {
  130.  
  131. destPixel.Set (0, 0, 0, 0);
  132.  
  133. } else {
  134.  
  135. destPixel.r = __clamp[int (0.5 + (sourcePixel.r * sourceAlpha + destPixel.r * destAlpha * oneMinusSourceAlpha) / blendAlpha)];
  136. destPixel.g = __clamp[int (0.5 + (sourcePixel.g * sourceAlpha + destPixel.g * destAlpha * oneMinusSourceAlpha) / blendAlpha)];
  137. destPixel.b = __clamp[int (0.5 + (sourcePixel.b * sourceAlpha + destPixel.b * destAlpha * oneMinusSourceAlpha) / blendAlpha)];
  138. destPixel.a = __clamp[int (0.5 + blendAlpha * 255.0)];
  139.  
  140. }
  141.  
  142. destPixel.WriteUInt8 (destData, destPosition, destFormat, destPremultiplied);
  143.  
  144. sourcePosition += 4;
  145. destPosition += 4;
  146.  
  147. }
  148.  
  149. }
  150.  
  151. }
  152.  
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement