1

Updated: webOS version 2.1...

This base64 encoded image data generates the correct data image when I append it to the source of an image, like this:

var img = new Image();
img.src= data

var data =   "data:image/png;base64,iVBORw0KGgoAAAANSUh
EUgAAAJYAAACmCAIAAAC3GeEYAAEkgklEQVR4AQD9/wIAADt8Fj5/GUSA IESAIEN/GUd/H0V+IEaEIE
WFHUOEHEqJIE2KIk2KI1GQKFSQLFaQKlaOKVaQKlORLlaYLVaVKFWV KVWUKliWKFiXJlKVIliTJW2oP
m6jOW+kPGqkPmehPWWePWagOmmjP2ykQ2ulQmylRWykQmqmP2qr QGqnP26qSGyvRmquQWyyQGquP2yu
RHSzSHWwSXGxSGytRG+vSG6vSW2vRWquRGqtRGmsQnO1R3Gw SG+rSXavUXWwUnKqR3CuRWquP26zQ3C
yRXK0SHG0SWupR2qoR3CuS2qrQ3CsSG6vS22pR26qSGyq RWetO22uQ2yqP22wRGetP2yyP4TEWgElAB
UrBRYmAx05AidHBB9MCydRDylSGChWGCZUFyFLEyNK Ex5IDBtJBhc/Bx9FDBxDDh5HDyRGExs8DRs4D
B04DRw8Exo6DxMuBw8kAhEeABIYAQ4VABAUAA0S AAwVAg8bAw0bAgwaAxAYAAULAQgQAQcQBQsPAAwQ
AggMAwMLAQAIAAgOBAYOAAsWBg4bChMgDxUk DxcmERopEh8vFBwuExspEhcnDxUpDhcqERUnDhUnDRQ
rDxgsERgvEx8xGQA+fxk7gxU9hBc9ghg/ gR1CgBxBhBtChRxIhyFMiyNMiyNNjiZNiypRkCpSjydRkC
VVkSpTkihYmi9YlC9XlCxVlClYlixW lSpZlS1eli16skJnqDxppj1qpDxmpD9mpD1loj1opz9qqENvq
Udpp0FmqD9npkFtpUVvp0ZvrUVs q0NsrEFtrURsrkBrsT9vskFvrj5srz5ssUJsrkJsrkNtr0NusEVm
qjxrrz5ttkNquEFqtEFu"

I am trying to save the image using my custom service, but it does not save the correct image. Here is the Node.js code for saving the image:

var fs = IMPORTS.require('fs');
var path = IMPORTS.require('path');
var buff = new Buffer(img.substr('data:image/png;base64,'.length), 'base64');
path.exists('/media/internal/wallpapers/', function(exists){
    if (exists) {
        var id = fs.openSync('/media/internal/wallpapers/i.png', 'w', 666);
        var written =  fs.writeSync(id, buff,0, buff.length, 0);
        fs.closeSync(id);
    }
});

The saved version of the image has totally different pixel values.

EDIT

I came to find out that the image data above isn't correct. I am using a PNG library to generate base64 image data of a canvas pixel array. The link is http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/.

That is an alternative to canvas.toDataURL();. webOS does not support toDataURL, so I had to use a library.

Here is the code I have used with this library to manipulate my canvas image data as a pixel array:

    EditorAssistant.prototype.getDataURL = function(width,height,pixelArray) {
        var p = new PNGlib(height, width, 256); // Construcor takes height, weight and color-depth.
        var background = p.color(0, 0, 0, 0);

        var k = 0;
        for (var j = 0; j<height; j++) {
            for (var i =0; i<width; i++) {
                var x =i;
                var y =j;
                p.buffer[p.index(x,y)] = p.color(pixelArray[k], pixelArray[k+1], pixelArray[k+2]);
                k+=4;
            }
        }
        return 'data:image/png;base64,'+p.getBase64() ;
    }

Does this library work and where am I doing it wrong?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
madiha.athar
  • 1,103
  • 3
  • 12
  • 25
  • 1
    I've noticed that you've asked nearly the same question several times, each time giving us just a little more info. FYI, on Stack Overflow, it is generally better to edit your question with updated information rather than ask a new one if it is essentially the same one. This makes it easier to help you since other's can more easily put together the information you've gathered while working on the problem yourself. – Gordon Seidoh Worley May 09 '11 at 22:39
  • thnks Gordon .. i will keep it in mind... – madiha.athar May 10 '11 at 06:59

1 Answers1

0

libpng.js only supports 256 color images, so that may be the problem. There are other base64 encoding utilities which may work as expected.

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265