@@ -16,34 +16,41 @@ import java.nio.FloatBuffer
1616 * [android.media.Image] source.
1717 */
1818object TensorImageUtils {
19- @JvmField
20- var TORCHVISION_NORM_MEAN_RGB : FloatArray = floatArrayOf(0.485f , 0.456f , 0.406f )
21- @JvmField
22- var TORCHVISION_NORM_STD_RGB : FloatArray = floatArrayOf(0.229f , 0.224f , 0.225f )
19+ @JvmField var TORCHVISION_NORM_MEAN_RGB : FloatArray = floatArrayOf(0.485f , 0.456f , 0.406f )
20+
21+ @JvmField var TORCHVISION_NORM_STD_RGB : FloatArray = floatArrayOf(0.229f , 0.224f , 0.225f )
2322
2423 /* *
25- * Creates new [Tensor] from full [android.graphics.Bitmap], normalized with specified
26- * in parameters mean and std.
24+ * Creates new [Tensor] from full [android.graphics.Bitmap], normalized with specified in
25+ * parameters mean and std.
2726 *
2827 * @param normMeanRGB means for RGB channels normalization, length must equal 3, RGB order
2928 * @param normStdRGB standard deviation for RGB channels normalization, length must equal 3, RGB
30- * order
29+ * order
3130 */
3231 @JvmStatic
3332 fun bitmapToFloat32Tensor (
34- bitmap : Bitmap , normMeanRGB : FloatArray , normStdRGB : FloatArray
33+ bitmap : Bitmap ,
34+ normMeanRGB : FloatArray ,
35+ normStdRGB : FloatArray ,
3536 ): Tensor {
3637 checkNormMeanArg(normMeanRGB)
3738 checkNormStdArg(normStdRGB)
3839
3940 return bitmapToFloat32Tensor(
40- bitmap, 0 , 0 , bitmap.width, bitmap.height, normMeanRGB, normStdRGB
41+ bitmap,
42+ 0 ,
43+ 0 ,
44+ bitmap.width,
45+ bitmap.height,
46+ normMeanRGB,
47+ normStdRGB,
4148 )
4249 }
4350
4451 /* *
45- * Writes tensor content from specified [android.graphics.Bitmap], normalized with specified
46- * in parameters mean and std to specified [java.nio.FloatBuffer] with specified offset.
52+ * Writes tensor content from specified [android.graphics.Bitmap], normalized with specified in
53+ * parameters mean and std to specified [java.nio.FloatBuffer] with specified offset.
4754 *
4855 * @param bitmap [android.graphics.Bitmap] as a source for Tensor data
4956 * @param x - x coordinate of top left corner of bitmap's area
@@ -52,7 +59,7 @@ object TensorImageUtils {
5259 * @param height - height of bitmap's area
5360 * @param normMeanRGB means for RGB channels normalization, length must equal 3, RGB order
5461 * @param normStdRGB standard deviation for RGB channels normalization, length must equal 3, RGB
55- * order
62+ * order
5663 */
5764 fun bitmapToFloatBuffer (
5865 bitmap : Bitmap ,
@@ -63,7 +70,7 @@ object TensorImageUtils {
6370 normMeanRGB : FloatArray ,
6471 normStdRGB : FloatArray ,
6572 outBuffer : FloatBuffer ,
66- outBufferOffset : Int
73+ outBufferOffset : Int ,
6774 ) {
6875 checkOutBufferCapacity(outBuffer, outBufferOffset, width, height)
6976 checkNormMeanArg(normMeanRGB)
@@ -88,8 +95,8 @@ object TensorImageUtils {
8895 }
8996
9097 /* *
91- * Creates new [Tensor] from specified area of [android.graphics.Bitmap], normalized
92- * with specified in parameters mean and std.
98+ * Creates new [Tensor] from specified area of [android.graphics.Bitmap], normalized with
99+ * specified in parameters mean and std.
93100 *
94101 * @param bitmap [android.graphics.Bitmap] as a source for Tensor data
95102 * @param x - x coordinate of top left corner of bitmap's area
@@ -98,7 +105,7 @@ object TensorImageUtils {
98105 * @param height - height of bitmap's area
99106 * @param normMeanRGB means for RGB channels normalization, length must equal 3, RGB order
100107 * @param normStdRGB standard deviation for RGB channels normalization, length must equal 3, RGB
101- * order
108+ * order
102109 */
103110 fun bitmapToFloat32Tensor (
104111 bitmap : Bitmap ,
@@ -107,7 +114,7 @@ object TensorImageUtils {
107114 width : Int ,
108115 height : Int ,
109116 normMeanRGB : FloatArray ,
110- normStdRGB : FloatArray
117+ normStdRGB : FloatArray ,
111118 ): Tensor {
112119 checkNormMeanArg(normMeanRGB)
113120 checkNormStdArg(normStdRGB)
@@ -118,17 +125,31 @@ object TensorImageUtils {
118125 }
119126
120127 private fun checkOutBufferCapacity (
121- outBuffer : FloatBuffer , outBufferOffset : Int , tensorWidth : Int , tensorHeight : Int
128+ outBuffer : FloatBuffer ,
129+ outBufferOffset : Int ,
130+ tensorWidth : Int ,
131+ tensorHeight : Int ,
122132 ) {
123- check(outBufferOffset + 3 * tensorWidth * tensorHeight <= outBuffer.capacity()) { " Buffer underflow" }
133+ check(outBufferOffset + 3 * tensorWidth * tensorHeight <= outBuffer.capacity()) {
134+ " Buffer underflow"
135+ }
124136 }
125137
126138 private fun checkTensorSize (tensorWidth : Int , tensorHeight : Int ) {
127- require(! (tensorHeight <= 0 || tensorWidth <= 0 )) { " tensorHeight and tensorWidth must be positive" }
139+ require(! (tensorHeight <= 0 || tensorWidth <= 0 )) {
140+ " tensorHeight and tensorWidth must be positive"
141+ }
128142 }
129143
130144 private fun checkRotateCWDegrees (rotateCWDegrees : Int ) {
131- require(! (rotateCWDegrees != 0 && rotateCWDegrees != 90 && rotateCWDegrees != 180 && rotateCWDegrees != 270 )) { " rotateCWDegrees must be one of 0, 90, 180, 270" }
145+ require(
146+ ! (rotateCWDegrees != 0 &&
147+ rotateCWDegrees != 90 &&
148+ rotateCWDegrees != 180 &&
149+ rotateCWDegrees != 270 )
150+ ) {
151+ " rotateCWDegrees must be one of 0, 90, 180, 270"
152+ }
132153 }
133154
134155 private fun checkNormStdArg (normStdRGB : FloatArray ) {
0 commit comments