我将rgb
图像传递给以下javascript
函数
function floyd_steinberg(image) {
alert("h");
// var data;
var imageData = image.data;
var imageDataLength = imageData;
var w = image.width;
var lumR = [],
lumG = [],
lumB = [];
var newPixel, err;
for (var i = 0; i < 256; i++) {
lumR[i] = i * 0.299;
lumG[i] = i * 0.587;
lumB[i] = i * 0.110;
}
// Greyscale luminance (sets r pixels to luminance of rgb)
for (var i = 0; i <= imageDataLength; i += 4) {
imageData[i] = Math.floor(lumR[imageData[i]] + lumG[imageData[i+1]] + lumB[imageData[i+2]]);
}
for (var currentPixel = 0; currentPixel <= imageDataLength; currentPixel += 4) {
// threshold for determining current pixel's conversion to a black or white pixel
newPixel = imageData[currentPixel] < 150 ? 0 : 255;
err = Math.floor((imageData[currentPixel] - newPixel) / 23);
imageData[currentPixel] = newPixel;
imageData[currentPixel + 4 ] += err * 7;
imageData[currentPixel + 4 * w - 4 ] += err * 3;
imageData[currentPixel + 4 * w ] += err * 5;
imageData[currentPixel + 4 * w + 4 ] += err * 1;
// Set g and b pixels equal to r (effectively greyscales the image fully)
imageData[currentPixel + 1] = imageData[currentPixel + 2] = imageData[currentPixel];
}
image.data = imageData;
console.log(image);
return image;
}
当图像传递到上述功能时。该函数提供图像的输出,并且该输出应存储在变量中。
我尝试使用rgb
转换php
图像单色图像并将输出图像存储在成功发生的变量中,但无法存储在javascript
函数中。以下是php
函数,用于获取输入图像并将其存储在目标文件中
function image2grf($filename='photo_url', $targetname = 'R:IMAGE.GRF')
{
$info = getimagesize($filename);
$im = imagecreatefrompng($filename);
$width = $info[0]; // imagesx($im);
$height = $info[1]; // imagesy($im);
$depth = $info['bits'] ?: 1;
$threshold = $depth > 1 ? 100 : 0;
$hexString = '';
$byteShift = 7;
$currentByte = 0;
// iterate over all image pixels
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$color = imagecolorat($im, $x, $y);
// $color = imagetruecolortopalette($photo_url, true, 2);
// compute gray value from RGB color
if ($depth > 1) {
$value = max($color >> 16, $color >> 8 & 255, $color & 255);
} else {
$value = $color;
}
// set (inverse) bit for the current pixel
$currentByte |= (($value > $threshold ? 0 : 1) << $byteShift);
$byteShift--;
// 8 pixels filled one byte => append to output as hex
if ($byteShift < 0) {
$hexString .= sprintf('%02X', $currentByte);
$currentByte = 0;
$byteShift = 7;
}
}
// append last byte at end of row
if ($byteShift < 7) {
$hexString .= sprintf('%02X', $currentByte);
$currentByte = 0;
$byteShift = 7;
}
$hexString .= PHP_EOL;
}
// compose ZPL ~DG command
$totalBytes = ceil(($width * $height) / 8);
$bytesPerRow = ceil($width / 8);
return sprintf('~DG%s,%05d,%03d,' . PHP_EOL, $targetname, $totalBytes, $bytesPerRow) . $hexString;
}
// Usage:
print "^XA N ^XZ" . PHP_EOL;
print image2grf($photo_url, 'R:SAMPLE.GRF');
在php
代码image2grf()
的最后一行中,此函数的输出存储在R:SAMPLE.GRF
请帮助我解决此问题,因为我是javascript
的新手