当前位置:   article > 正文

BMP二进制图片处理_bmp 生成2进制

bmp 生成2进制

BMP文件格式详解(BMP file format)

TypedArray.prototype

JS BMP二进制图片的处理

bmpDeal(arrybuffer){
   let w = 1280;
   let h = 960;
   let color = 0;
   let size = w * h;
   let allsize = size + 1080;
   let patte = new Array(1024);
   let end = new Array(2);
   let pos = 0;
   let idx = 0;
   let line = 0;
   let head = [
     0x42, 0x4d, 0x30, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x36, 0x04, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0xF5, 0x00,
     0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00,
     0x00, 0x00, 0x00, 0x00, 0xF8, 0x0b, 0x01, 0x00, 0x12, 0x0b,
     0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00
   ];
   //位图信息头处理
   head[2] = allsize & 0xFF;
   head[3] = (allsize >> 8) & 0xFF;
   head[4] = (allsize >> 16) & 0xFF;
   head[5] = (allsize >> 24) & 0xFF;

   head[18] = w & 0xFF;
   head[19] = (w >> 8) & 0xFF;
   head[20] = (w >> 16) & 0xFF;
   head[21] = (w >> 24) & 0xFF;

   head[22] = h & 0xFF;
   head[23] = (h >> 8) & 0xFF;
   head[24] = (h >> 16) & 0xFF;
   head[25] = (h >> 24) & 0xFF;

   allsize -= 1078;
   head[34] = allsize & 0xFF;
   head[35] = (allsize >> 8) & 0xFF;
   head[36] = (allsize >> 16) & 0xFF;
   head[37] = (allsize >> 24) & 0xFF;

//调色板处理
   for (idx = 0; idx < 1024; idx += 4){
     patte[pos++] = color;
     patte[pos++] = color;
     patte[pos++] = color;
     patte[pos++] = 0;
     color++;
   }

   allsize += 1078;
   let all_un8 = new Uint8Array(allsize);

   //位图信息头
   let head_un8 = new Uint8Array(head);
   for (let i = 0; i < head_un8.length; i++) {
     all_un8[i] = head_un8[i];
   }

   //颜色表
   let patte_un8 =  new Uint8Array(patte);
   for (let i = 0; i < patte_un8.length; i++) {
     all_un8[54 + i] = patte_un8[i];
   }

   //位图数据
   let rgb_un8 = new Uint8Array(arrybuffer);
   for (let idx = h - 1; idx >= 0; idx--){
     //这里注意  BMP二进制的处理是逆序行读的
     let sub_rgb_un8 = rgb_un8.subarray(idx * w, (idx+1)*w);
     for(let i = 0; i < sub_rgb_un8.length; i++){
       all_un8[1078 + line * w + i] = sub_rgb_un8[i]
     }
     line ++
   }
   //尾部处理
   let end_un8 = new Uint8Array(end);
   all_un8[allsize-2] = end_un8[0];
   all_un8[allsize-1] = end_un8[1];

   //也可以通过set方式写入
   //all_un8.set(new Uint8Array(head));
   //all_un8.set(new Uint8Array(patte),  54);
   //for (let idx = h - 1; idx >= 0; idx--){
   //  all_un8.set(rgb_un8.subarray(idx * w, (idx+1)*w),  1078 + line * w);
   //  line ++
   //}
   //all_un8.set(new Uint8Array(end),allsize-2);

   return all_un8;
 },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/983061
推荐阅读
相关标签
  

闽ICP备14008679号