Snippets

Elvis Morales Steganography with JavaScript

Created by Elvis Morales last modified
function shift( im ){
    var nim = new SimpleImage( im.getWidth(), im.getHeight() );
    
    for ( var px of im.values() ) {
        var x = px.getX();
        var y = px.getY();
        var npx = nim.getPixel( x, y );
        
        npx.setRed( Math.floor( px.getRed()/16 ) );
        npx.setGreen( Math.floor( px.getGreen()/16 ) );
        npx.setBlue( Math.floor( px.getBlue()/16 ) );
    }
    return nim;
}


function pixchange( pixval ) {
    var x = Math.floor( pixval/16 ) * 16;
    return x;
}

function chop2Hide( image ) {
    for ( var px of image.values() ) {
        px.setRed( pixchange( px.getRed() ) );
        px.setGreen( pixchange( px.getGreen() ) );
        px.setBlue( pixchange( px.getBlue() ) );
    }
    return image;
}

function crop( image, width, height ) {
    
    var croppedImage = new SimpleImage( width, height );
    
    for ( var px of image.values() ) {
        var x = px.getX();
        var y = px.getY();
        
        if ( x < width && y < height ) {
            var npx = croppedImage.getPixel( x, y );
            npx.setRed( px.getRed() );
            npx.setGreen( px.getGreen() );
            npx.setBlue( px.getBlue() );
        }
        
    }
    
    return croppedImage;
}

function combine( a, b ) {
    
     var combinedImage = new SimpleImage( a.getWidth(), a.getHeight() );
    
    for ( var px of a.values() ) {
        var x = px.getX();
        var y = px.getY();
        
        var bImgPx = b.getPixel( x, y );
        
        var newImgPx = combinedImage.getPixel( x, y );
        
        newImgPx.setRed( newpv( px.getRed(), bImgPx.getRed() ) );
        newImgPx.setGreen( newpv( px.getGreen(), bImgPx.getGreen() ) );
        newImgPx.setBlue( newpv( px.getBlue(), bImgPx.getBlue() ) );
        
    }
    
    return combinedImage;
    
}

function newpv( p, q ) {
    var result = p+q; 
    if ( result > 255 ) {
        print( "Error on newpv() function, the sum of P and Q is greater than 255" );
    } else {
        return result;
    }
}

// Program - Put it all together.

var start = new SimpleImage( "astrachan.jpg" );
var hide = new SimpleImage( "duvall.jpg" );

print( start );
print( hide );

print( "Width and height of Astrachan picture" );
print( start.getWidth(), start.getHeight() );
print( "Width and height of Duvall picture" );
print( hide.getWidth(), hide.getHeight() );

var cropWidth = 200;
var cropHeight = 300;

var start = crop( start, cropWidth, cropHeight );
var hide = crop( hide, cropWidth, cropHeight );

print( "Cropped two pictures" );

print( start );
print( hide );
print( "Width and height of Astrachan cropped picture" );
print( start.getWidth(), start.getHeight() );
print( "Width and height of Duvall cropped picture" );
print( hide.getWidth(), hide.getHeight() );

// Hide picture in image - in lower half of each pixel.

print( "red at (42,42)" );
var sPixel = start.getPixel( 42,42 );
print( sPixel.getRed() );

start = chop2Hide( start );

print( "Red at (42,42) after chop2Hide" );
var sPixel = start.getPixel( 42,42 );
print( sPixel.getRed() );

print( "Red at (42,42) before shift" );
var hPixel = hide.getPixel( 42, 42 );
print( hPixel.getRed() );

hide = shift( hide );

print( "Red at (42,42) after shift" );
var hPixel = hide.getPixel(42,42);
print( hPixel.getRed() );

var stego = combine( hide, start );
print( "Here is the image with the hidden picture" );
print( stego );

print( "red at (42,42) after combine" );
var hPixel = stego.getPixel( 42,42 );
print( hPixel.getRed() );

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.