# Path: moonlight/image.py
# def decode_music_score_png(contents):
#   """Reads a music score image.
# 
#   This reads a binary or grayscale image and takes the only channel. If the
#       image is detected to be inverted, the values will be flipped so that
#       the white background has value 255 and the black content has value 0.
# 
#   Args:
#     contents: PNG data in a scalar string tensor.
# 
#   Returns:
#     The music score image. A two-dimensional tensor (HW) of type uint8.
#   """
#   with tf.name_scope("decode_music_score_png"):
#     contents = tf.convert_to_tensor(contents, name="contents")
#     image_t = tf.image.decode_png(contents, channels=1, dtype=tf.uint8)[:, :, 0]
# 
#     def inverted_image():
#       # Sub op is not defined for uint8.
#       int32_image = tf.cast(image_t, tf.int32)
#       return tf.cast(255 - int32_image, tf.uint8)
# 
#     threshold = 127
#     num_pixels = tf.shape(image_t)[0] * tf.shape(image_t)[1]
#     majority_dark = tf.greater(
#         tf.reduce_sum(tf.cast(image_t < threshold, tf.int32)), num_pixels // 2)
#     return tf.cond(majority_dark, inverted_image, lambda: image_t)
# 
# Path: moonlight/staves/staffline_distance.py
# _MAX_STAFFLINE_DISTANCE_THICKNESS_VALUE = 256
# _MIN_STAFFLINE_DISTANCE_SCORE = 10000
# _MAX_ALLOWED_UNIQUE_STAFFLINE_DISTANCES = 3
# _STAFFLINE_DISTANCE_INVALIDATE_DISTANCE = 1
# _STAFFLINE_THICKNESS_INVALIDATE_DISTANCE = 1
# _PEAK_CUTOFF = 0.5
# def _single_peak(values, relative_cutoff, minval, invalidate_distance):
# def _estimate_staffline_distance(columns, lengths):
#     def do_filter_peaks():
# def _estimate_staffline_thickness(columns, values, lengths, staffline_distance):
#     def do_estimate():
# def estimate_staffline_distance_and_thickness(image, threshold=127):


import os.path
import tensorflow as tf
from moonlight.image import decode_music_score_png
from moonlight.staves import staffline_distance
# limitations under the License.
"""Tests for staffline distance estimation."""

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





class StafflineDistanceTest(tf.test.TestCase):

  def testCorpusImage(self):
    filename = os.path.join(tf.resource_loader.get_data_files_path(),
                            '../testdata/IMSLP00747-000.png')
    image_contents = open(filename, 'rb').read()
    image_t = decode_music_score_png(tf.constant(image_contents))
    staffdist_t, staffthick_t = (
        staffline_distance.estimate_staffline_distance_and_thickness(image_t,))
    with self.test_session() as sess:
      staffdist, staffthick = sess.run((staffdist_t, staffthick_t))
    # Manually determined values for the image.
    self.assertAllEqual(staffdist, [16])
    self.assertEquals(staffthick, 2)

  def testZeros(self):
    # All white (0) shouldn't be picked up as a music score.
    image_t = tf.zeros((512, 512), dtype=tf.uint8)
    staffdist_t, staffthick_t = (

# Path: moonlight/image.py
# def decode_music_score_png(contents):
#   """Reads a music score image.
# 
#   This reads a binary or grayscale image and takes the only channel. If the
#       image is detected to be inverted, the values will be flipped so that
#       the white background has value 255 and the black content has value 0.
# 
#   Args:
#     contents: PNG data in a scalar string tensor.
# 
#   Returns:
#     The music score image. A two-dimensional tensor (HW) of type uint8.
#   """
#   with tf.name_scope("decode_music_score_png"):
#     contents = tf.convert_to_tensor(contents, name="contents")
#     image_t = tf.image.decode_png(contents, channels=1, dtype=tf.uint8)[:, :, 0]
# 
#     def inverted_image():
#       # Sub op is not defined for uint8.
#       int32_image = tf.cast(image_t, tf.int32)
#       return tf.cast(255 - int32_image, tf.uint8)
# 
#     threshold = 127
#     num_pixels = tf.shape(image_t)[0] * tf.shape(image_t)[1]
#     majority_dark = tf.greater(
#         tf.reduce_sum(tf.cast(image_t < threshold, tf.int32)), num_pixels // 2)
#     return tf.cond(majority_dark, inverted_image, lambda: image_t)
# 
# Path: moonlight/staves/staffline_distance.py
# _MAX_STAFFLINE_DISTANCE_THICKNESS_VALUE = 256
# _MIN_STAFFLINE_DISTANCE_SCORE = 10000
# _MAX_ALLOWED_UNIQUE_STAFFLINE_DISTANCES = 3
# _STAFFLINE_DISTANCE_INVALIDATE_DISTANCE = 1
# _STAFFLINE_THICKNESS_INVALIDATE_DISTANCE = 1
# _PEAK_CUTOFF = 0.5
# def _single_peak(values, relative_cutoff, minval, invalidate_distance):
# def _estimate_staffline_distance(columns, lengths):
#     def do_filter_peaks():
# def _estimate_staffline_thickness(columns, values, lengths, staffline_distance):
#     def do_estimate():
# def estimate_staffline_distance_and_thickness(image, threshold=127):


# Path: moonlight/staves/staffline_distance_test.py
import os.path
import tensorflow as tf
from moonlight.image import decode_music_score_png
from moonlight.staves import staffline_distance

# limitations under the License.
"""Tests for staffline distance estimation."""

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





class StafflineDistanceTest(tf.test.TestCase):

  def testCorpusImage(self):
    filename = os.path.join(tf.resource_loader.get_data_files_path(),
                            '../testdata/IMSLP00747-000.png')
    image_contents = open(filename, 'rb').read()
    image_t = decode_music_score_png(tf.constant(image_contents))
    staffdist_t, staffthick_t = (
        staffline_distance.estimate_staffline_distance_and_thickness(image_t,))
    with self.test_session() as sess:
      staffdist, staffthick = sess.run((staffdist_t, staffthick_t))
    # Manually determined values for the image.
    self.assertAllEqual(staffdist, [16])
    self.assertEquals(staffthick, 2)

  def testZeros(self):
    # All white (0) shouldn't be picked up as a music score.
    image_t = tf.zeros((512, 512), dtype=tf.uint8)
    staffdist_t, staffthick_t = (

        staffline_distance.estimate_staffline_distance_and_thickness(image_t))