Save an array of pixels to a file - haxe

In the Haxe programming language, is there any cross-language way to save an array of pixel data to a file (e. g., in BMP or PNG format)?
class SavePixelsToFile {
static function main(){
//how can I save this array of pixel data to a file? It is a simple 2D array of RGB arrays, with the red, green, and blue components in the respective order.
var arr = [
[[0, 0, 0],[255, 255, 255]],
[[255, 255, 255],[0, 0, 0]]
];
}
}

The format library will do what you want. http://code.google.com/p/hxformat/
Install this library: haxelib install format
Link it in your hxml file using: -lib format
To write image data to a file, do the following:
function writePixels24(file:String, pixels:haxe.io.Bytes, width:Int, height:Int) {
var handle = sys.io.File.write(file, true);
new format.png.Writer(handle)
.write(format.png.Tools.build24(width, height, pixels));
handle.close();
}
var bo = new haxe.io.BytesOutput();
for (pixel in pixels)
for (channel in pixel)
bo.writeByte(channel);
var bytes = bo.getBytes();
writePixels24("Somefile.png", bytes);
This will work for targets that have the sys.* package (Not flash). you can still generate the png without the sys.* package, but will need an alternate method of saving the file.

An easy to write format is PPM. The netpbm tools can easily manipulate those and also convert to many formats including PBM and PNG.

Related

Stitch multiple pngs with Jimp

I need to stitch multiple pngs into a long png, and I believe I can use Jimp's blit method:
// blit the image with another Jimp image at x, y, optionally cropped.
image.blit( src, x, y, [srcx, srcy, srcw, srch] );
My question is, how do I loop along all the pngs and make the current png attach to the end of the previous one? For example, they are all in a same folder with file names like img-1, img-2 ... img-10. And their sizes are not necessarily the same. This is what I have in mind, any suggestions would be greatly appreciated!
// This is dummy code
const bg = white_background
bg.width = Math.max(img-1...img-10 width)
bg.height = sum(img-1...img-10 height)
for (let i=0;i<imgs.length;i++) {
if(i=0) bg.blit(img, 0, 0)
else bg.blit(img, imgs[i-1].width, imgs[i-1].height)
}

Can fabric.js parse raster graphic to "real" svg?

I have a question/problem with fabric.js - in my code the user can upload a picture, with filters he can convert it to a black/white image. When I export the picture with canvas.toSVG(); it exports a svg image, but it is no real vector graphic - it loses quality when scaling up.
function handleImage(e) {
var reader = new FileReader();
reader.onload = function (event) {
var img = new Image();
img.onload = function () {
var imgInstance = new fabric.Image(img, {
scaleX: 0.7,
scaleY: 0.7
})
canvas.add(imgInstance);
}
img.src = event.target.result;
} reader.readAsDataURL(e.target.files[0]);}
$('saveBtn').onclick = function() {
var filedata= canvas.toSVG(); // the SVG file is now in filedata
var locfile = new Blob([filedata], {type: "image/svg+xml;charset=utf-8"});
var locfilesrc = URL.createObjectURL(locfile);//mylocfile);
var dwn = document.getElementById('dwn');
dwn.innerHTML = "<a href=" + locfilesrc + " download='mysvg.svg'>Download</a>";}
What am I doing wrong?
There is no easy way to "parse" raster graphics to a vector image. Vector graphics include information for how to draw an image, while raster images only include the pixel data for how an image appears at a given size and resolution. That's enough for many purposes, but it means that while it's easy to go from vector to raster (just execute the instructions), it's not easy to go from raster to vector.
It is possible to "trace" the edges of a raster image to obtain vectors that can approximate the raster: in other words, a set of vector instructions that, for that particular resolution and depth, yields an image that is the same as the original raster (or something very like it). But there is no guarantee that these actually correspond in any way to the original vectors (if there are any original vectors at all). Usually there is no correspondence, in fact, unless your tracing algorithm is very specialized: for example, tracing images of a font to make a vector copy of that font. Because they don't correspond, there's no guarantee that the image will scale up the way you want it to: it'll scale, but things may enlarge in strange ways.
It is possible to implement tracing algorithms in JavaScript, by drawing the image into a <canvas> element, using getImageData() to grab the pixel information from that, and performing your operations on the pixel information. Doing this, though, is beyond the scope of this question.

Poor quality jpeg resize on GD library use. Wrong steps?

So I have the following script to resize an incoming jpg and save it to the server in a new size. The created jpg file has terrible quality, even when I am making the quality 90 on the imagejpeg. I am wondering if I am messing it up earlier in my efforts.
// Get new sizes
list($width, $height) = getimagesize($filename);
$percentW=298/$width;
$newwidth = $width * $percentW;
$newheight = $height * $percentW;
// Creating a blank canvas to put the new file. Is this an extra step?
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Now I create the resized photo with the needed width and height on the blank canvas with the source file resized
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output to server, define 90% quality. My guess is the quality is destroyed before now.
imagejpeg($thumb,"../../uploads/buckets/" . $_POST["bucket"] . "/" .$fileNameBucket,90);
imagedestroy($thumb);
Am I messing up the quality before I even output the file to the server? Should I use resample instead of resize? I am stuck with using the GD library so ImageMagick is not an option. Thanks.

view RGBA image

Can someone tell me how can I view an RGBA image? I just want a tool that I can display an RGBA image with!
I have written a code, which outputs only RGBA format. I just want to verify if my code worked, and just want to find a simple tool to view this image.
I wasn't able to come across a software to be able to display a RGBA image.
Thanks in advance.
RGBA files only contain raw channel data. The binary data will not have enough information to display an image (ie. width,height,depth, &tc).
If you know the image dimensions of each .rgba file, you should be able to work with the data. Here's an example of viewing raw date in javascript.
var fr = new FileReader(),
myWidth = 200,
myHeight = 200;
fr.onload = function(frEvent) {
var canvasElement = document.getElementById("myCanvas"),
ctx = canvasElement.getContext("2d"),
blob = ctx.createImageData(myWidth,myHeight),
index = 0;
while(index < frEvent.target.result.length)
blob.data[index] = frEvent.target.result.charCodeAt(index++);
ctx.putImageData(blob,0,0);
}
Imagemagick will be able to display raw RGBA data. Assuming that each color sample is 8 bits.
display -size 200x200 -depth 8 mySimpleData.rgba

vtkMarchingCubes export nifti surfaces to wavefront OBJ

I want to run vtkMarchingCubes on a nifti label set. Regions of voxels, for which I want to produce surfaces all share the same value. I have two problems. First, I seem to be setting up the algorithm incorrectly because the resulting vtkPolyData apparently has no vertices. Secondly, it is not clear to me from the vtkOBJExporter documentation how to export the vtkPolyData as a wavefront .OBJ file. If anyone sees any issues with the code below or can tell me how to export the vtkPolyData as an OBJ, I would be grateful.
//Read The Nifti Label File
string input_path = "/MyPath/labels.nii";
nifti_image *im = nifti_image_read(input_path.c_str(),true);
cout<<im->nx<<","<<im->ny<<","<<im->nz<<endl; //Confirms Read Works
// Set up vtk image data
vtkImageImport* importer = vtkImageImport::New();
importer->SetImportVoidPointer((void*)im->data);
importer->SetDataScalarTypeToFloat();
importer->SetDataExtent(0, im->nx-1, 0, im->ny-1, 0, im->nz-1);
importer->SetWholeExtent(0, im->nx-1, 0, im->ny-1, 0, im->nz-1);
vtkImageData* point_cloud = importer->GetOutput();
point_cloud->SetScalarTypeToFloat();
point_cloud->SetExtent(0, im->nx-1, 0, im->ny-1, 0, im->nz-1);
point_cloud->SetSpacing(im->dx, im->dy, im->dz);
//Apply Threshold To Cut Out Other Data
//Is this needed or will Marching Cubes properly identify the region
vtkImageThreshold* threshold = vtkImageThreshold::New();
threshold->ThresholdBetween(label_number,label_number);
threshold->SetInValue(255);
threshold->SetOutValue(0);
threshold->SetInput(point_cloud);
//Apply the Marching Cubes algorithm
vtkMarchingCubes* marching_cubes = vtkMarchingCubes::New();
marching_cubes->SetValue(0, 127.0f);
marching_cubes->SetInput(threshold->GetOutput()); //(vtkDataObject*)point_cloud);
vtkPolyData* surface = marching_cubes->GetOutput();
marching_cubes->Update();
//See That Marching Cubes Worked
cout<<"# Vertices: "<< surface->GetNumberOfVerts()<<endl;
cout<<"# Cells: "<< surface->GetNumberOfCells()<<endl;
//Export (How is this done properly?)
vtkOBJExporter* exporter = vtkOBJExporter::New();
exporter->SetInput(vtkRenderWindow *renWin); //I don't want a render window, I want at file
exporter->SetFilePrefix("/MyPath/surface");
exporter->Write();
You can use this class https://github.com/daviddoria/vtkOBJWriter to write the obj file in the way you would expect (like every other VTK writer). Unfortunately the vtkOBJExporter wants to also write additional information that I never have.

Resources