xref: /aosp_15_r20/external/tensorflow/tensorflow/python/kernel_tests/image_ops/decode_png_op_test.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for DecodePngOp."""
16
17from tensorflow.python.framework import constant_op
18from tensorflow.python.framework import dtypes
19from tensorflow.python.ops import array_ops
20from tensorflow.python.ops import image_ops
21import tensorflow.python.ops.nn_grad  # pylint: disable=unused-import
22from tensorflow.python.platform import test
23
24
25class DecodePngOpTest(test.TestCase):
26
27  def test16bit(self):
28    img_bytes = [[0, 255], [1024, 1024 + 255]]
29    # Encoded PNG bytes resulting from encoding the above img_bytes
30    # using go's image/png encoder.
31    encoded_bytes = [
32        137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0,
33        2, 0, 0, 0, 2, 16, 0, 0, 0, 0, 7, 77, 142, 187, 0, 0, 0, 21, 73, 68, 65,
34        84, 120, 156, 98, 98, 96, 96, 248, 207, 194, 2, 36, 1, 1, 0, 0, 255,
35        255, 6, 60, 1, 10, 68, 160, 26, 131, 0, 0, 0, 0, 73, 69, 78, 68, 174,
36        66, 96, 130
37    ]
38
39    byte_string = bytes(bytearray(encoded_bytes))
40    img_in = constant_op.constant(byte_string, dtype=dtypes.string)
41    decode = array_ops.squeeze(
42        image_ops.decode_png(
43            img_in, dtype=dtypes.uint16))
44
45    with self.cached_session():
46      decoded = self.evaluate(decode)
47      self.assertAllEqual(decoded, img_bytes)
48
49
50if __name__ == "__main__":
51  test.main()
52