diff options
author | Yong Tang <yong.tang.github@outlook.com> | 2017-11-13 14:35:27 -0800 |
---|---|---|
committer | Martin Wicke <martin.wicke@gmail.com> | 2017-11-13 14:35:27 -0800 |
commit | b066496f625930bc00397ad9d000741d724598eb (patch) | |
tree | 71071a314ddd4e5481ec73406b20d8dbd7d058f6 | |
parent | 2ae9c6c7a20dbd8f05e4b60e921e60986e2968bf (diff) |
Add support for grayscale bmp image (#14296)
* Add support for grayscale bmp image
This fix tries to address the issue raised in 13942 to support grayscale
bmp image. Previously only channels of 3 or 4 are supported in bmp decoding.
This fix adds the support to have 1 channel grayscale image.
This fix fixes 13942.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
* Add test case for grayscale bmp image decoding
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
-rw-r--r-- | tensorflow/core/kernels/decode_bmp_op.cc | 16 | ||||
-rw-r--r-- | tensorflow/python/kernel_tests/decode_bmp_op_test.py | 34 |
2 files changed, 44 insertions, 6 deletions
diff --git a/tensorflow/core/kernels/decode_bmp_op.cc b/tensorflow/core/kernels/decode_bmp_op.cc index 086369a9f1..cd7956e1cb 100644 --- a/tensorflow/core/kernels/decode_bmp_op.cc +++ b/tensorflow/core/kernels/decode_bmp_op.cc @@ -33,9 +33,10 @@ class DecodeBmpOp : public OpKernel { public: explicit DecodeBmpOp(OpKernelConstruction* context) : OpKernel(context) { OP_REQUIRES_OK(context, context->GetAttr("channels", &channels_)); - OP_REQUIRES( - context, channels_ == 0 || channels_ == 3 || channels_ == 4, - errors::InvalidArgument("channels must be 0, 3 or 4, got ", channels_)); + OP_REQUIRES(context, channels_ == 0 || channels_ == 1 || channels_ == 3 || + channels_ == 4, + errors::InvalidArgument("channels must be 0, 1, 3 or 4, got ", + channels_)); } void Compute(OpKernelContext* context) override { @@ -66,11 +67,11 @@ class DecodeBmpOp : public OpKernel { channels_ = bpp / 8; } - // Current implementation only supports 3 or 4 channel + // Current implementation only supports 1, 3 or 4 channel // bitmaps. - OP_REQUIRES(context, (channels_ == 3 || channels_ == 4), + OP_REQUIRES(context, (channels_ == 1 || channels_ == 3 || channels_ == 4), errors::InvalidArgument( - "Number of channels must be 3 or 4, was ", channels_)); + "Number of channels must be 1, 3 or 4, was ", channels_)); // if height is negative, data layout is top down // otherwise, it's bottom up @@ -117,6 +118,9 @@ uint8* DecodeBmpOp::Decode(const uint8* input, uint8* const output, dst_pos = (i * width + j) * channels; switch (channels) { + case 1: + output[dst_pos] = input[src_pos]; + break; case 3: // BGR -> RGB output[dst_pos] = input[src_pos + 2]; diff --git a/tensorflow/python/kernel_tests/decode_bmp_op_test.py b/tensorflow/python/kernel_tests/decode_bmp_op_test.py index 783492a6f2..e7b472240e 100644 --- a/tensorflow/python/kernel_tests/decode_bmp_op_test.py +++ b/tensorflow/python/kernel_tests/decode_bmp_op_test.py @@ -64,6 +64,40 @@ class DecodeBmpOpTest(test.TestCase): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes) + def testGrayscale(self): + img_bytes = [[[255], [0]], [[255], [0]]] + encoded_bytes = [ + 0x42, 0x40, + 0x3d, 0, 0, 0, + 0, 0, + 0, 0, + 0x36, 0, 0, 0, + 0x28, 0, 0, 0, + 0x2, 0, 0, 0, + 0x2, 0, 0, 0, + 0x1, 0, + 0x8, 0, + 0, 0, 0, 0, + 0x10, 0, 0, 0, + 0x13, 0xb, 0, 0, + 0x13, 0xb, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0xff, + 0, + 0, 0, + 0xff, + 0, + 0, 0, + ] + + byte_string = bytes(bytearray(encoded_bytes)) + img_in = constant_op.constant(byte_string, dtype=dtypes.string) + decode = image_ops.decode_bmp(img_in) + + with self.test_session(): + decoded = decode.eval() + self.assertAllEqual(decoded, img_bytes) if __name__ == "__main__": test.main() |