2

ここからのコードに基づいて、画像を(HTMLキャンバスを使用して)RGBからYCoCg色空間に変換し、キャンバスからの出力をロスレスで変換できるPNGとして保存しようとしています:

https://stackoverflow.com/a/12146329/2399347

このプロセスは元に戻すことができますが、私の試行は現在、中間形式が使用されていない場合にのみ機能します。これがないと、コードの実際の適用が制限されます。

「リフティング」プロセスで 8 ビットの符号付き整数 (疑似コードでは -128 から 127、モジュロ 0x100 であると考えられます) を使用し、キャンバスに表示される値 (0 から 255) を変換する必要があります。保存できます。成功した場合、変換された画像がキャンバスに配置されたときに、値を再変換して逆リフトすると、元の画像が生成されます。現在生成されている値は、キャンバスに正しく表示できません (-255 から 255 まで)。

上記の問題は私の現在の知識を超えており、オンライン検索や Javascript のドキュメントを調べても、これを行う方法が見つかりませんでした。知っている(または興味がある)人は、コードを見て、これを行う実用的な方法、またはその効果に対する回避策を提案または提供できますか?

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="charset=UTF-8" />
    <style>
    img, canvas {
        display: block;
        padding-bottom: 1em;
    }
    </style>
    <script>
function go() {

//  Extract PNG image to Canvas

    var canvas = document.getElementById('ycocg');
    var ctx = canvas.getContext('2d');
    var source = document.querySelector("#original");

    canvas.width = source.width;
    canvas.height = source.height;

    ctx.drawImage(source, 0, 0);

    var canvasData = ctx.getImageData(0, 0, canvas.width, canvas.height);


//  Canvas to convert image to YCoCg and back in one step

    var canvasBack = document.getElementById('back2rgb');
    var ctxBack = canvasBack.getContext('2d');

    canvasBack.width = source.width;
    canvasBack.height = source.height;

    ctxBack.drawImage(canvas, 0, 0);

    var canvasBackData = ctxBack.getImageData(0, 0, canvas.width, canvas.height);


//  Colour space functions

function forward_lift( x1, y1 ) {
    var diff = ( ( y1 - x1 ) ) % 256;
    var average = ( x1 + ( diff >> 1 ) ) % 256;

    return  { c1: average, c2: diff };
}

function reverse_lift( average, diff ) {
    var x2 = ( average - ( diff >> 1 ) ) % 256;
    var y2 = ( ( x2 + diff ) ) % 256;

    return  { c1: x2, c2: y2 };
}

function RGB_to_YCoCg24( red, green, blue ) {
    // Conversion and back

    var lift1 = forward_lift( red, blue );
    var lift2 = forward_lift( green, lift1.c1 );

    var rlift1 = reverse_lift( lift2.c1, lift2.c2 );
    var rlift2 = reverse_lift( rlift1.c2, lift1.c2 );

    canvasBackData.data[idx + 0] = rlift2.c1 % 256; // R
    canvasBackData.data[idx + 1] = rlift1.c1 % 256; // G
    canvasBackData.data[idx + 2] = rlift2.c2 % 256; // B

    // Attempt at conversion only and store

    canvasData.data[idx + 0] = lift2.c1 % 256; // Y
    canvasData.data[idx + 1] = lift2.c2 % 256; // Cg
    canvasData.data[idx + 2] = lift1.c2 % 256; // Co
}

function YCoCg24_to_RGB( Yimg, Cg, Co ) {
    // Attempt at reversing stored image

    var rlift1 = reverse_lift( Yimg, Cg );
    var rlift2 = reverse_lift( rlift1.c2, Co );

    canvasStepData.data[idx + 0] = rlift2.c1 % 256; // R
    canvasStepData.data[idx + 1] = rlift1.c1 % 256; // G
    canvasStepData.data[idx + 2] = rlift2.c2 % 256; // B
}

//  Conversion of RGB image to YCoCg

    for (var x = 0; x < canvasData.width; x++) {
        for (var y = 0; y < canvasData.height; y++) {
            // Index of the pixel in the array
            var idx = (x + y * canvas.width) * 4;

            RGB_to_YCoCg24( canvasData.data[idx + 0], canvasData.data[idx + 1], canvasData.data[idx + 2]  );
        }
    }
    ctxBack.putImageData(canvasBackData, 0, 0);
    ctx.putImageData(canvasData, 0, 0);


//  Convert from YCoCg to RGB, in two steps

    var canvasStep = document.getElementById('twostep');
    var ctxStep = canvasStep.getContext('2d');

    canvasStep.width = source.width;
    canvasStep.height = source.height;

    ctxStep.drawImage(canvas, 0, 0);

    var canvasStepData = ctxStep.getImageData(0, 0, canvas.width, canvas.height);

    for (var x = 0; x < canvasData.width; x++) {
        for (var y = 0; y < canvasData.height; y++) {
            // Index of the pixel in the array
            var idx = (x + y * canvas.width) * 4;

            YCoCg24_to_RGB( canvasStepData.data[idx + 0], canvasStepData.data[idx + 1], canvasStepData.data[idx + 2]  );
        }
    }
    ctxStep.putImageData(canvasStepData, 0, 0);
}
    </script>
</head>

<body onload="go()">

<div id="container">
    Original:
    <img id="original" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAAR00lEQVR4Xu3a0QnDAAxDQTVkAO8/bbdQKTq4Afz7kD9PnuT9Y5/X/e53/59yf5/73e9+97vf/c+bjAAAAH4ZAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAABAAAACAAAAAAAQAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAABAAAAAAAIAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAACAAAAAAAQAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAABAAAAAAAIAAAAEwGUDAADQWQAAAAAvQAAAgAAAAAAEAAAAIAAAAAABAAAACAAAABAAAACAAAAAAAQAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAABAAAAAAAIAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAACAAAAAAAQAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAABAAAAAAAIAAAAEwGUDAADQWQAAAAAvQAAAgAAAAAAEAAAAIAAAAAABAAAACAAAABAAAACAAAAAAAQAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAAB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AAAAABAAAACAAAAAAAQAAAAgAAABAAAAAAAIAAAAEwGUDAADQWQAAAAAvQAAAgAAAAAAEAAAAIAAAAAABAAAACAAAABAAAACAAAAAAAQAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAABAAAAAAAIAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAACAAAAAAAQAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAABAAAAAAAIAAAAEwGUDAABgAQAAAC9AAACAAAAAAAQAAAAgAAAAAAEAAAAIAAAAQAA0AACAAAAAAAQAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAABAAAAAAAIAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAAAgAAAAAAEAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAAAQAAAAgAAAAAAEAAAAIAAAAAABAAAACAAAAEAAAAAAAgAAABAAAACAAAAAAAQAAAAIAAAAQAAAAAACAAAAEAAAAIAAAAAABAAAACAAAAAAAQAAAAgAAABAAAAAAAIAAAD4Aozry/w+U2+oAAAAAElFTkSuQmCC" />

    Converted and back in one step:
    <canvas id="back2rgb"></canvas>

    Converted and stored (right click, save image as):
    <canvas id="ycocg"></canvas>

    Restored previous image:
    <canvas id="twostep"></canvas>
</div>

</body>
</html>
4

0 に答える 0