| 1 | // Aseprite | 
|---|
| 2 | // Copyright (C) 2019  Igara Studio S.A. | 
|---|
| 3 | // | 
|---|
| 4 | // This program is distributed under the terms of | 
|---|
| 5 | // the End-User License Agreement for Aseprite. | 
|---|
| 6 |  | 
|---|
| 7 | #ifdef HAVE_CONFIG_H | 
|---|
| 8 | #include "config.h" | 
|---|
| 9 | #endif | 
|---|
| 10 |  | 
|---|
| 11 | #include "app/util/buffer_region.h" | 
|---|
| 12 |  | 
|---|
| 13 | #include "doc/image.h" | 
|---|
| 14 | #include "gfx/region.h" | 
|---|
| 15 |  | 
|---|
| 16 | #include <algorithm> | 
|---|
| 17 |  | 
|---|
| 18 | namespace app { | 
|---|
| 19 |  | 
|---|
| 20 | void save_image_region_in_buffer( | 
|---|
| 21 | const gfx::Region& region, | 
|---|
| 22 | const doc::Image* image, | 
|---|
| 23 | const gfx::Point& imagePos, | 
|---|
| 24 | base::buffer& buffer) | 
|---|
| 25 | { | 
|---|
| 26 | // Calculate buffer size for the region | 
|---|
| 27 | const size_t bytesPerPixel = image->getRowStrideSize(1); | 
|---|
| 28 | size_t reqBytes = 0; | 
|---|
| 29 | for (const auto& rc : region) | 
|---|
| 30 | reqBytes += bytesPerPixel*rc.w*rc.h; | 
|---|
| 31 |  | 
|---|
| 32 | // Save region pixels | 
|---|
| 33 | buffer.resize(reqBytes); | 
|---|
| 34 | auto it = buffer.begin(); | 
|---|
| 35 | for (const auto& rc : region) { | 
|---|
| 36 | for (int y=0; y<rc.h; ++y) { | 
|---|
| 37 | auto p = (const uint8_t*)image->getPixelAddress(rc.x-imagePos.x, | 
|---|
| 38 | rc.y-imagePos.y+y); | 
|---|
| 39 | const size_t rowBytes = bytesPerPixel*rc.w; | 
|---|
| 40 | std::copy(p, p+rowBytes, it); | 
|---|
| 41 | it += rowBytes; | 
|---|
| 42 | } | 
|---|
| 43 | } | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | void swap_image_region_with_buffer( | 
|---|
| 47 | const gfx::Region& region, | 
|---|
| 48 | doc::Image* image, | 
|---|
| 49 | base::buffer& buffer) | 
|---|
| 50 | { | 
|---|
| 51 | const size_t bytesPerPixel = image->getRowStrideSize(1); | 
|---|
| 52 | auto it = buffer.begin(); | 
|---|
| 53 | for (const auto& rc : region) { | 
|---|
| 54 | for (int y=0; y<rc.h; ++y) { | 
|---|
| 55 | auto p = (uint8_t*)image->getPixelAddress(rc.x, rc.y+y); | 
|---|
| 56 | const size_t rowBytes = bytesPerPixel*rc.w; | 
|---|
| 57 | std::swap_ranges(it, it+rowBytes, p); | 
|---|
| 58 | it += rowBytes; | 
|---|
| 59 | } | 
|---|
| 60 | } | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | } // namespace app | 
|---|
| 64 |  | 
|---|