# Path: bssrdf_estimate/hdr/hdr_pixel.py
# class HDRPixel(object):
#     def __init__(self, r, g, b):
#         d = max(r, max(g, b))
#         if (d < 1.0e-32):
#             self.r = 0
#             self.g = 0
#             self.b = 0
#             self.e = 0
#             return
# 
#         m, ie = math.frexp(d)
#         d = m * 256.0 / d
# 
#         self.r = int(clamp(r * d, 0, 255))
#         self.g = int(clamp(g * d, 0, 255))
#         self.b = int(clamp(b * d, 0, 255))
#         self.e = int(clamp(ie + 128, 0, 255))
# 
#     def get(self, i):
#         if i == 0: return self.r
#         if i == 1: return self.g
#         if i == 2: return self.b
#         if i == 3: return self.e
#         raise Exception('Channel index out of bounds')


import struct
from .hdr_pixel import HDRPixel
def hdr_save(filename, hdr):
    with open(filename, 'wb') as fp:
        # Write header
        ret = 0x0a
        strwrite(fp, '#?RADIANCE%c' % ret)
        strwrite(fp, '# Made with100%% pure HDR Shop%c' % ret)
        strwrite(fp, 'FORMAT=32-bit_rle_rgbe%c' % ret)
        strwrite(fp, 'EXPOSURE=1.0000000000000%c%c' % (ret, ret))

        # Write size
        [height, width, dim] = hdr.shape
        if dim != 3:
            raise Exception('HDR image must have 3 channels')

        strwrite(fp, '-Y %d +X %d%c' % (height, width, ret))

        for i in range(height):
            line = [None] * width
            for j in range(width):
                r = hdr[i, j, 0]
                g = hdr[i, j, 1]
                b = hdr[i, j, 2]
                line[j] = HDRPixel(r, g, b)

            fp.write(struct.pack('BBBB', 0x02, 0x02, (width >> 8) & 0xff, width & 0xff))

            buf = []
            for ch in range(4):
                cursor = 0
                while cursor < width:

# Path: bssrdf_estimate/hdr/hdr_pixel.py
# class HDRPixel(object):
#     def __init__(self, r, g, b):
#         d = max(r, max(g, b))
#         if (d < 1.0e-32):
#             self.r = 0
#             self.g = 0
#             self.b = 0
#             self.e = 0
#             return
# 
#         m, ie = math.frexp(d)
#         d = m * 256.0 / d
# 
#         self.r = int(clamp(r * d, 0, 255))
#         self.g = int(clamp(g * d, 0, 255))
#         self.b = int(clamp(b * d, 0, 255))
#         self.e = int(clamp(ie + 128, 0, 255))
# 
#     def get(self, i):
#         if i == 0: return self.r
#         if i == 1: return self.g
#         if i == 2: return self.b
#         if i == 3: return self.e
#         raise Exception('Channel index out of bounds')


# Path: bssrdf_estimate/hdr/hdr_save.py
import struct
from .hdr_pixel import HDRPixel

def hdr_save(filename, hdr):
    with open(filename, 'wb') as fp:
        # Write header
        ret = 0x0a
        strwrite(fp, '#?RADIANCE%c' % ret)
        strwrite(fp, '# Made with100%% pure HDR Shop%c' % ret)
        strwrite(fp, 'FORMAT=32-bit_rle_rgbe%c' % ret)
        strwrite(fp, 'EXPOSURE=1.0000000000000%c%c' % (ret, ret))

        # Write size
        [height, width, dim] = hdr.shape
        if dim != 3:
            raise Exception('HDR image must have 3 channels')

        strwrite(fp, '-Y %d +X %d%c' % (height, width, ret))

        for i in range(height):
            line = [None] * width
            for j in range(width):
                r = hdr[i, j, 0]
                g = hdr[i, j, 1]
                b = hdr[i, j, 2]
                line[j] = HDRPixel(r, g, b)

            fp.write(struct.pack('BBBB', 0x02, 0x02, (width >> 8) & 0xff, width & 0xff))

            buf = []
            for ch in range(4):
                cursor = 0
                while cursor < width:

                    cursor_move = min(127, width - cursor)