tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

tcuLogImage.js (5993B)


      1 /*-------------------------------------------------------------------------
      2 * drawElements Quality Program OpenGL ES Utilities
      3 * ------------------------------------------------
      4 *
      5 * Copyright 2014 The Android Open Source Project
      6 *
      7 * Licensed under the Apache License, Version 2.0 (the "License");
      8 * you may not use this file except in compliance with the License.
      9 * You may obtain a copy of the License at
     10 *
     11 *      http://www.apache.org/licenses/LICENSE-2.0
     12 *
     13 * Unless required by applicable law or agreed to in writing, software
     14 * distributed under the License is distributed on an "AS IS" BASIS,
     15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 * See the License for the specific language governing permissions and
     17 * limitations under the License.
     18 *
     19 */
     20 
     21 'use strict';
     22 goog.provide('framework.common.tcuLogImage');
     23 goog.require('framework.common.tcuSurface');
     24 goog.require('framework.common.tcuTexture');
     25 goog.require('framework.delibs.debase.deMath');
     26 
     27 goog.scope(function() {
     28 
     29 var tcuLogImage = framework.common.tcuLogImage;
     30 var tcuTexture = framework.common.tcuTexture;
     31 var tcuSurface = framework.common.tcuSurface;
     32 var deMath = framework.delibs.debase.deMath;
     33 
     34 /** @const */ var MAX_IMAGE_SIZE_2D = 4096;
     35 /**
     36 * @param {tcuTexture.ConstPixelBufferAccess} src
     37 */
     38 tcuLogImage.createImage = function(ctx, src) {
     39    var w = src.getWidth();
     40    var h = src.getHeight();
     41    var pixelSize = src.getFormat().getPixelSize();
     42    var imgData = ctx.createImageData(w, h);
     43    var index = 0;
     44    for (var y = 0; y < h; y++) {
     45        for (var x = 0; x < w; x++) {
     46            var pixel = src.getPixelInt(x, h - y - 1, 0);
     47            for (var i = 0; i < pixelSize; i++) {
     48                imgData.data[index] = pixel[i];
     49                index = index + 1;
     50            }
     51            if (pixelSize < 4)
     52                imgData.data[index++] = 255;
     53        }
     54    }
     55    return imgData;
     56 };
     57 
     58 /**
     59 * @param {tcuTexture.ConstPixelBufferAccess} image
     60 * @param {string} info
     61 */
     62 tcuLogImage.logImageWithInfo = function(image, info) {
     63    var elem = document.getElementById('console');
     64    var span = document.createElement('span');
     65    tcuLogImage.logImage.counter = tcuLogImage.logImage.counter || 0;
     66    var i = tcuLogImage.logImage.counter++;
     67    var width = image.getWidth();
     68    var height = image.getHeight();
     69 
     70    elem.appendChild(span);
     71    span.innerHTML = info + '<br> <canvas id="logImage' + i + '" width=' + width + ' height=' + height + '></canvas><br>';
     72 
     73    var imageCanvas = document.getElementById('logImage' + i);
     74    var ctx = imageCanvas.getContext('2d');
     75    var data = tcuLogImage.createImage(ctx, image);
     76    ctx.putImageData(data, 0, 0);
     77 };
     78 
     79 
     80 /**
     81 * @param {Array<number>=} scale
     82 * @param {Array<number>=} bias
     83 * @return {string} HTML string to add to log.
     84 */
     85 tcuLogImage.logScaleAndBias = function(scale, bias) {
     86    if (scale && bias)
     87        return '<br> Image normalized with formula p * (' + scale + ') + (' + bias + ')';
     88    else if (scale)
     89        return '<br> Image normalized with formula p * (' + scale + ')';
     90    else if (bias)
     91        return '<br> Image normalized with formula p + (' + bias + ')';
     92    return '';
     93 };
     94 
     95 /**
     96 * @param {string} name
     97 * @param {string} description
     98 * @param {tcuTexture.ConstPixelBufferAccess} image
     99 * @param {Array<number>=} scale
    100 * @param {Array<number>=} bias
    101 */
    102 tcuLogImage.logImageRGB = function(name, description, image, scale, bias) {
    103    var elem = document.getElementById('console');
    104    var span = document.createElement('span');
    105    var info = name + ' ' + description + '<br> ' + image;
    106    if (scale || bias)
    107        info += tcuLogImage.logScaleAndBias(scale, bias);
    108    tcuLogImage.logImageWithInfo(image, info);
    109 };
    110 
    111 /**
    112 * @param {string} name
    113 * @param {string} description
    114 * @param {tcuTexture.ConstPixelBufferAccess} access
    115 * @param {Array<number>=} pixelScale
    116 * @param {Array<number>=} pixelBias
    117 */
    118 tcuLogImage.logImage = function(name, description, access, pixelScale, pixelBias) {
    119    pixelScale = pixelScale || [1, 1, 1, 1];
    120    pixelBias = pixelBias || [0, 0, 0, 0];
    121    var format = access.getFormat();
    122    var width = access.getWidth();
    123    var height = access.getHeight();
    124    var depth = access.getDepth();
    125    var needScaling = pixelBias[0] != 0 || pixelBias[1] != 0 || pixelBias[2] != 0 || pixelBias[3] != 0 ||
    126        pixelScale[0] != 1 || pixelScale[1] != 1 || pixelScale[2] != 1 || pixelScale[3] != 1;
    127 
    128    if (depth == 1 && format.type == tcuTexture.ChannelType.UNORM_INT8 &&
    129        width <= MAX_IMAGE_SIZE_2D && height <= MAX_IMAGE_SIZE_2D &&
    130        (format.order == tcuTexture.ChannelOrder.RGB || tcuTexture.ChannelOrder.RGBA) &&
    131        !needScaling)
    132        // Fast-path.
    133        tcuLogImage.logImageRGB(name, description, access);
    134    else if (depth == 1) {
    135        var sampler = new tcuTexture.Sampler(tcuTexture.WrapMode.CLAMP_TO_EDGE, tcuTexture.WrapMode.CLAMP_TO_EDGE, tcuTexture.WrapMode.CLAMP_TO_EDGE,
    136            tcuTexture.FilterMode.LINEAR, tcuTexture.FilterMode.NEAREST);
    137        var logImageSize = [width, height]; /* TODO: Add scaling */
    138        var logImageAccess = new tcuSurface.Surface(width, height).getAccess();
    139 
    140        for (var y = 0; y < logImageAccess.getHeight(); y++) {
    141            for (var x = 0; x < logImageAccess.getWidth(); x++) {
    142                var yf = (y + 0.5) / logImageAccess.getHeight();
    143                var xf = (x + 0.5) / logImageAccess.getWidth();
    144                var s = access.sample2D(sampler, sampler.minFilter, xf, yf, 0);
    145 
    146                if (needScaling)
    147                    s = deMath.add(deMath.multiply(s, pixelScale), pixelBias);
    148 
    149                logImageAccess.setPixel(s, x, y);
    150            }
    151        }
    152        var info = name + ' ' + description + '<br> ' + access;
    153        if (needScaling) {
    154            info += tcuLogImage.logScaleAndBias(pixelScale, pixelBias);
    155        }
    156 
    157        tcuLogImage.logImageWithInfo(logImageAccess, info);
    158    } else {
    159        /* TODO: Implement */
    160    }
    161 };
    162 
    163 });