Dump a BMP file 2 an array of arrays (of arrays)

Posted on
  • Hi,

    I've been trying to dump a bmp file in to an array as follows:
    an array of 32 arrays of 32 arrays of 4 byte values, but have not been able to make it work, I would appreciate any help, this is what I've been trying:

    The first 54 bytes have header information:

    var img = new Array();
    function rcolor(x){
        return (TONES*x/256);
    }
    
    function readbmp(myfile){
        try{
            var i=0;
            var k=0;
            var x=0;
            var flag=0;
            var px = new Array();
            px[0] = 0;
            px[1] = 0;
            px[2] = 0;
            px[3] = 0;
            var line=new Array();
            if (myfile.length==4096){
                flag=4;
            }
            else {
                flag =3;
            }
            for (var byte in myfile){
                z= myfile[byte];
                console.log(z);
                k+=1;
                if(k==1){
                    px[0]=myfile[byte];
                }
                if(k==2){
                    px[1]=myfile[byte];
                }
                if(k==3){
                    px[2]=myfile[byte];
                }
                if(k==4){
                    px[3]=myfile[byte];
                }
                if(k==flag){
                    k=0;
                    line.push(px);
                    x+=1;
                    if(x==32){
                        img.push(line);
                        line.splice(0, line.length); 
                        x=0;
                    }
      
                }
            }
            console.log(img);
            return x;
        }
        catch(err){
            return 0;
        }
    }
    
    var image = require("fs").readFile("GIF/wizardcity00­001.bmp", "binary");
    image = image.substr(54,image.length);
    readbmp(image);
    

    I would really appreciate any help.

    Bests

  • One thing you're going to need to do is use Uint32Arrays. An array of 32 32 entry simple arrays will take up a bit over 2048 memory units, which is more than the espruino board has. Even with typed arrays, that's going to be like 500-something memory units.

  • Tryed several things always running out of memory, un the end I used a unit8array and stored everything on a 4096 lineal array and I will have to calculate the positions every time

  • So you have it working now?

    You should have some luck using 32 x Uint32Arrays (or Uint8Arrays)?

  • In the end I dumped the 32 X 32 bmp into Uint32Arrays, here is the code:

    var image = require("fs").readFile("GIF/blue.bmp", "binary");
    image = image.substr(54,image.length);
    if (image.length==4096){
      var bytes = new Uint8Array(4096);
      for (var j=0;j<4096;j++){ 
        //bytes[j]=Math.round(image[j].charCodeA­t()*TONES/256);
        bytes[j]=image[j].charCodeAt();
        //console.log(image[j].charCodeAt());
      }
    }
    if (image.length==3072){
      var c = 0;
      var bytes = new Uint8Array(4096);
      for (var j=0;j<3072;j++){
        if ((c+1)%4==0){
          bytes[c]=128;
          c+=1;
        } else {
          //bytes[j]=Math.round(image[j].charCodeA­t()*TONES/256);
          bytes[c]=image[j].charCodeAt();
          
          //console.log(image[j].charCodeAt());
        }
        c+=1;
      }
    }
    
  • Could you please modify your post, as it looks broken :)

    You can extract colour data from bmp into Uint8Array on desktop computer, then using atob convert into binary string, and save into a file.
    Then it will be much easier and faster to load it from SD card on Espruino from that file (binary string) using atob, speed should spike dramatically.

  • I just edited the post - you just need to highlight the code and click the 'code' button.

    You can actually set the value of the Uint8Array to the string with a simple:

    bytes.set(image);
    

    Which could be a bit faster for the first case.

    Thanks @moka - If anyone's interested there's a little bit on how to do this at the bottom of http://www.espruino.com/Graphics

    Having said that, a module to load Bitmaps off the SD card would be pretty cool :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Dump a BMP file 2 an array of arrays (of arrays)

Posted by Avatar for user6837 @user6837

Actions