# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy of
# the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.
# ==============================================================================
"""Tests for Image Dataset."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

from tensorflow.python.platform import test

from tensorflow_io.image.python.ops import image_dataset_ops
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.ops import image_ops
from tensorflow.python.platform import resource_loader


class ImageDatasetTest(test.TestCase):

  def test_webp_file_dataset(self):
    """Test case for WebPDataset.
    """
    width = 400
    height = 301
    channel = 4
    png_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_image", "sample.png")
    with open(png_file, 'rb') as f:
      png_contents = f.read()
    with self.cached_session():
      image_op = image_ops.decode_png(png_contents, channels=channel)
      image = image_op.eval()
      self.assertEqual(image.shape, (height, width, channel))

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_image", "sample.webp")

    filenames = constant_op.constant([filename], dtypes.string)
    num_repeats = 2

    dataset = image_dataset_ops.WebPDataset(filenames).repeat(
        num_repeats)
    iterator = dataset.make_initializable_iterator()
    init_op = iterator.initializer
    get_next = iterator.get_next()

    with self.cached_session() as sess:
      sess.run(init_op)
      for _ in range(num_repeats):  # Dataset is repeated.
        v = sess.run(get_next)
        self.assertAllEqual(image, v)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)

  def test_tiff_file_dataset(self):
    """Test case for TIFFDataset.
    """
    width = 560
    height = 320
    channels = 4

    images = []
    for filename in ["small-00.png", "small-01.png", "small-02.png", "small-03.png", "small-04.png"]:
      with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_image", filename), 'rb') as f:
        png_contents = f.read()
      with self.cached_session():
        image_op = image_ops.decode_png(png_contents, channels=channels)
        image = image_op.eval()
        self.assertEqual(image.shape, (height, width, channels))
        images.append(image)

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_image", "small.tiff")

    num_repeats = 2

    dataset = image_dataset_ops.TIFFDataset([filename]).repeat(num_repeats)
    iterator = dataset.make_initializable_iterator()
    init_op = iterator.initializer
    get_next = iterator.get_next()
    with self.cached_session() as sess:
      sess.run(init_op)
      for _ in range(num_repeats):
        for i in range(5):
          v = sess.run(get_next)
          self.assertAllEqual(images[i], v)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)

if __name__ == "__main__":
  test.main()

@@ -1230,16 +1230,853 @@
 Case):%0A%0A
+  def test_decode_webp(self):%0A    %22%22%22Test case for decode_webp.%0A    %22%22%22%0A    width = 400%0A    height = 301%0A    channel = 4%0A    png_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), %22test_image%22, %22sample.png%22)%0A    with open(png_file, 'rb') as f:%0A      png_contents = f.read()%0A    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), %22test_image%22, %22sample.webp%22)%0A    with open(filename, 'rb') as f:%0A      webp_contents = f.read()%0A%0A    with self.cached_session():%0A      png_op = image_ops.decode_png(png_contents, channels=channel)%0A      png = png_op.eval()%0A      self.assertEqual(png.shape, (height, width, channel))%0A%0A      webp_p = image_dataset_ops.decode_webp(webp_contents)%0A      webp_v = webp_p.eval()%0A      self.assertEqual(webp_v.shape, (height, width, channel))%0A%0A      self.assertAllEqual(webp_v, png)%0A%0A%0A
   def te

Add test case for decode_webp op