Index: tools/SkCodecTools.h |
diff --git a/tools/SkCodecTools.h b/tools/SkCodecTools.h |
index 097915b5d719711c37559f860791ca3b50936dd7..413f0335f3cadeacf8e0fdf2b24e94383b1f3cb0 100644 |
--- a/tools/SkCodecTools.h |
+++ b/tools/SkCodecTools.h |
@@ -12,4 +12,28 @@ inline float get_scale_from_sample_size(uint32_t sampleSize) { |
return 1.0f / (float) sampleSize; |
} |
+/* |
+ * Chooses the correct image subset offsets and dimensions for the partial decode. |
+ * |
+ * @return true if the subset is completely contained within the image |
+ * false otherwise |
+ */ |
+inline bool set_subset_region(int inputOffset, int inputDimension, |
+ int imageOriginalDimension, int* imageSubsetOffset, int* outOffset, |
+ int* imageSubsetDimension) { |
+ |
+ // This must be at least zero, we can't start decoding the image at a negative coordinate. |
+ *imageSubsetOffset = SkTMax(0, inputOffset); |
+ |
+ // If inputOffset is less than zero, we decode to an offset location in the output bitmap. |
+ *outOffset = *imageSubsetOffset - inputOffset; |
+ |
+ // Use imageSusetOffset to make sure we don't decode pixels past the edge of the image. |
+ // Use outOffset to make sure we don't decode pixels past the edge of the region. |
+ *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, |
+ inputDimension - *outOffset); |
+ |
+ return (*outOffset == 0) && (*imageSubsetDimension == inputDimension); |
+} |
+ |
#endif // SkCodecTools_DEFINED |