# Code coverage report for src/util/hdr.js

# all files / src/util/ hdr.js

70.75% Statements 75/106
60% Branches 21/35
57.14% Functions 4/7
72.12% Lines 75/104

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178

    1×

    1×
    1×

    524288×
    468143×
    468143×
    468143×
    468143×

    56145×
    56145×
    56145×

    524288×
    524288×

    1×
    1×
    2×

    1×

    512×

    512×
    512×

    512×
    512×

    512×
    512×

    512×
    2048×
    83718×
    83718×
    50288×
    50288×
    50288×
    827515×

    33430×
    1269637×

    512×

    1×

    1×

    1×
    1×
    1×

    1×
    60×
    1×

    1×

    1×
    1×
    1×
    15×
    15×
    1×

    14×

    1×
    1×
    1×
    1×

    1×
    1×

    1×
    1024×
    1024×
    4096×

    1×
    1×
    1×
    512×
    512×

    512×
    524288×
    524288×

    1×

    1×
    1×
    1×

    1×
    1×

     import Texture from '../Texture';
    import Texture2D from '../Texture2D';
    var toChar = String.fromCharCode;

    var MINELEN = 8;
    var MAXELEN = 0x7fff;
    function rgbe2float(rgbe, buffer, offset, exposure) {
        if (rgbe[3] > 0) {
            var f = Math.pow(2.0, rgbe[3] - 128 - 8 + exposure);
            buffer[offset + 0] = rgbe[0] * f;
            buffer[offset + 1] = rgbe[1] * f;
            buffer[offset + 2] = rgbe[2] * f;
        }
        else {
            buffer[offset + 0] = 0;
            buffer[offset + 1] = 0;
            buffer[offset + 2] = 0;
        }
        buffer[offset + 3] = 1.0;
        return buffer;
    }

    function uint82string(array, offset, size) {
        var str = '';
        for (var i = offset; i < size; i++) {
            str += toChar(array[i]);
        }
        return str;
    }

    function copyrgbe(s, t) {
        t[0] = s[0];
        t[1] = s[1];
        t[2] = s[2];
        t[3] = s[3];
    }

    // TODO : check
    function oldReadColors(scan, buffer, offset, xmax) {
        var rshift = 0, x = 0, len = xmax;
        while (len > 0) {
            scan[x][0] = buffer[offset++];
            scan[x][1] = buffer[offset++];
            scan[x][2] = buffer[offset++];
            scan[x][3] = buffer[offset++];
            if (scan[x][0] === 1 && scan[x][1] === 1 && scan[x][2] === 1) {
                // exp is count of repeated pixels
                for (var i = (scan[x][3] << rshift) >>> 0; i > 0; i--) {
                    copyrgbe(scan[x-1], scan[x]);
                    x++;
                    len--;
                }
                rshift += 8;
            } else {
                x++;
                len--;
                rshift = 0;
            }
        }
        return offset;
    }

    function readColors(scan, buffer, offset, xmax) {
        Iif ((xmax < MINELEN) | (xmax > MAXELEN)) {
            return oldReadColors(scan, buffer, offset, xmax);
        }
        var i = buffer[offset++];
        Iif (i != 2) {
            return oldReadColors(scan, buffer, offset - 1, xmax);
        }
        scan[0][1] = buffer[offset++];
        scan[0][2] = buffer[offset++];

        i = buffer[offset++];
        Iif ((((scan[0][2] << 8) >>> 0) | i) >>> 0 !== xmax) {
            return null;
        }
        for (var i = 0; i < 4; i++) {
            for (var x = 0; x < xmax;) {
                var code = buffer[offset++];
                if (code > 128) {
                    code = (code & 127) >>> 0;
                    var val = buffer[offset++];
                    while (code--) {
                        scan[x++][i] = val;
                    }
                } else {
                    while (code--) {
                        scan[x++][i] = buffer[offset++];
                    }
                }
            }
        }
        return offset;
    }

    var ret = {
        // http://www.graphics.cornell.edu/~bjw/rgbe.html
        // Blender source
        // http://radsite.lbl.gov/radiance/refer/Notes/picture_format.html
        parseRGBE: function(arrayBuffer, texture, exposure) {
            Iif (exposure == null) {
                exposure = 0;
            }
            var data = new Uint8Array(arrayBuffer);
            var size = data.length;
            Iif (uint82string(data, 0, 2) !== '#?') {
                return;
            }
            // find empty line, next line is resolution info
            for (var i = 2; i < size; i++) {
                if (toChar(data[i]) === '\n' && toChar(data[i+1]) === '\n') {
                    break;
                }
            }
            Iif (i >= size) { // not found
                return;
            }
            // find resolution info line
            i += 2;
            var str = '';
            for (; i < size; i++) {
                var _char = toChar(data[i]);
                if (_char === '\n') {
                    break;
                }
                str += _char;
            }
            // -Y M +X N
            var tmp = str.split(' ');
            var height = parseInt(tmp[1]);
            var width = parseInt(tmp[3]);
            Iif (!width || !height) {
                return;
            }

            // read and decode actual data
            var offset = i+1;
            var scanline = [];
            // memzero
            for (var x = 0; x < width; x++) {
                scanline[x] = [];
                for (var j = 0; j < 4; j++) {
                    scanline[x][j] = 0;
                }
            }
            var pixels = new Float32Array(width * height * 4);
            var offset2 = 0;
            for (var y = 0; y < height; y++) {
                var offset = readColors(scanline, data, offset, width);
                Iif (!offset) {
                    return null;
                }
                for (var x = 0; x < width; x++) {
                    rgbe2float(scanline[x], pixels, offset2, exposure);
                    offset2 += 4;
                }
            }

            Iif (!texture) {
                texture = new Texture2D();
            }
            texture.width = width;
            texture.height = height;
            texture.pixels = pixels;
            // HALF_FLOAT can't use Float32Array
            texture.type = Texture.FLOAT;
            return texture;
        },

        parseRGBEFromPNG: function(png) {

        }
    };

    export default ret;