Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

color halftone and module test #1678

Merged
merged 2 commits into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'canvas-resize': require('./modules/CanvasResize'),
'channel': require('./modules/Channel'),
'colorbar': require('./modules/Colorbar'),
'color-halftone': require('./modules/ColorHalftone'),
'color-temperature': require('./modules/ColorTemperature'),
'colormap': require('./modules/Colormap'),
'constrained-crop': require('./modules/ConstrainedCrop'),
Expand Down
135 changes: 135 additions & 0 deletions src/modules/ColorHalftone/Module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Generate halftone versions of CMYK channels and blend them with varying rotations as in analog print color separation processes.
* Simulates a CMYK halftone rendering of the image by multiplying pixel values with a four rotated 2D sine wave patterns, one each for cyan, magenta, yellow, and black.
* http://evanw.github.io/glfx.js/docs/#colorHalftone
*/
module.exports = function ColorHalftone(options, UI) {

var defaults = require('./../../util/getDefaults.js')(require('./info.json'));

var output;
var fx = require('glfx');

var dataURItoBlob = function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);

// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}

return new Blob([ia], { type: mimeString });
};

var canvasToBlobUrl = function canvasToBlobUrl(canvas) {

var blob = dataURItoBlob(canvas.toDataURL('image/png'));
return window.URL.createObjectURL(blob);

};

var colorHalftone = function colorHalftone(id, options, download) {

// try to create a WebGL canvas (will fail if WebGL isn't supported)
try {
var canvas = fx.canvas(1500, 1500);
} catch (e) {
alert(e);
return;
}

// convert the image to a texture
var imageEl = document.getElementById(id);

var image = new Image();

image.onload = function() {

var texture = canvas.texture(image);

canvas.draw(texture,
image.width, // * ratio,
image.height// * ratio
);

canvas.colorHalftone(
image.width / 2,
image.height / 2,
parseFloat(options.angle),
parseFloat(options.size)
).update();

var burl = canvasToBlobUrl(canvas);

if (download) {

window.open(burl);

} else { // replace the image

// keep non-blob version in case we have to fall back:
// image.src = canvas.toDataURL('image/png');
// window.location = canvas.toDataURL('image/png');
imageEl.src = burl;

}

};

$(image).hide();
image.src = imageEl.src;

};

function draw(input, callback) {

var step = this;

options.angle = options.angle || defaults.angle;
options.size = options.size || defaults.size;

if (!options.inBrowser) {
// this.output = input;
// callback();
require('../_nomodule/gl-context')(input, callback, step, options);
}
else {
var image = document.createElement('img');
image.onload = () => {
colorHalftone(
'img',
options
);
image.onload = () => {
var canvas = document.createElement('canvas');
canvas.width = image.naturalWidth; // or 'width' if you want a special/scaled size
canvas.height = image.naturalHeight; // or 'height' if you want a special/scaled size
canvas.getContext('2d').drawImage(image, 0, 0);

step.output = { src: canvas.toDataURL('image/png'), format: 'png' };
image.remove();
callback();
};
};
image.src = input.src;
image.id = 'img';
document.body.appendChild(image);
}
}

return {
options: options,
draw: draw,
output: output,
UI: UI
};
};
4 changes: 4 additions & 0 deletions src/modules/ColorHalftone/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
require('./Module'),
require('./info.json')
];
22 changes: 22 additions & 0 deletions src/modules/ColorHalftone/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "color-halftone",
"requires": ["webgl"],
"description": "Generate halftone versions of CMYK channels and blend them with varying rotations as in analog print color separation processes.",
"inputs": {
"angle": {
"type": "float",
"desc": "angle of rotation of the halftone patterns in radians",
"default": "0.25",
"min": "0",
"max": "1.57"
},
"size": {
"type": "integer",
"desc": "the diameter of a dot in pixels",
"default": "4",
"min": "3",
"max": "20"
}
},
"docs-link":"https://github.com/publiclab/image-sequencer/blob/main/docs/MODULES.md#color-halftone-module"
}
2 changes: 1 addition & 1 deletion src/modules/WebglDistort/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function DoNothing(options, UI) {
var defaults = require('./../../util/getDefaults.js')(require('./info.json'));

var output;
var fx = require('./glfx');
var fx = require('glfx');

var dataURItoBlob = function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
Expand Down
Loading