如何获得一个字节的最后2位(How to get the last 2 bits in a byte)
在一个字节数组中,例如
[40, 0, 156, 243]
,我需要从数组的每个字节的最后两位中取出1个字节。 这是解码和编码PICO-8盒式磁带( 见这里 ),它将程序数据存储在PNG图像中。 以下是关于数据存储方式的信息:
For png files, the lower 2 bits of each pixel component makes up one byte, so 1 byte per pixel. The last 32 bytes are metadata for the PNG (since 160*205 = 0x8020, 32 extra bytes) byte = (a << 6) | (r << 4) | (g << 2) | b;
这是我到目前为止所拥有的,但输出都是胡言乱语:
var imgWidth = 160; var imgHeight = 205; var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); var img = document.createElement('img'); img.src = 'Invasion.png'; canvas.width = imgWidth; canvas.height = imgHeight; img.onload = function() { var imgData = []; var str = ''; var i = 0, n = 0, dataLen = 0; ctx.drawImage(img, 0, 0); imgData = ctx.getImageData(0, 0, imgWidth, imgHeight).data; dataLen = imgData.length; for(i; i < dataLen; i+=4) { var r = imgData[i]; var g = imgData[i + 1]; var b = imgData[i + 2]; var a = imgData[i + 3]; r = (r & 3) << 4; g = (g & 3) << 2; b = (b & 3); a = (a & 3) << 6; byte = a | r | g | b; // assembled byte str += String.fromCharCode(byte); } console.log(str); };
小提琴: https : //jsfiddle.net/24o3fzg3/
非常感谢任何帮助,谢谢!
In an array of bytes, such as
[40, 0, 156, 243]
, I need to make 1 byte from the last two bits in each byte of the array. This is for decoding and encoding PICO-8 cartridges (see here), which stores the program data in PNG images. Here is the information that I've been given on how the data is stored:
For png files, the lower 2 bits of each pixel component makes up one byte, so 1 byte per pixel. The last 32 bytes are metadata for the PNG (since 160*205 = 0x8020, 32 extra bytes) byte = (a << 6) | (r << 4) | (g << 2) | b;
Here's what I have so far, but the output is all gibberish:
var imgWidth = 160; var imgHeight = 205; var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); var img = document.createElement('img'); img.src = 'Invasion.png'; canvas.width = imgWidth; canvas.height = imgHeight; img.onload = function() { var imgData = []; var str = ''; var i = 0, n = 0, dataLen = 0; ctx.drawImage(img, 0, 0); imgData = ctx.getImageData(0, 0, imgWidth, imgHeight).data; dataLen = imgData.length; for(i; i < dataLen; i+=4) { var r = imgData[i]; var g = imgData[i + 1]; var b = imgData[i + 2]; var a = imgData[i + 3]; r = (r & 3) << 4; g = (g & 3) << 2; b = (b & 3); a = (a & 3) << 6; byte = a | r | g | b; // assembled byte str += String.fromCharCode(byte); } console.log(str); };
Fiddle: https://jsfiddle.net/24o3fzg3/
Greatly appreciate any help on this, thanks!
原文:https://stackoverflow.com/questions/39666716
最满意答案
好吧,我的问题是我忘记在生产条件下将ReduxReactRouter包含在我的商店的compose函数中。
enhancer = compose(middleware, reduxReactRouter({ routes, createHistory }));
我怀疑任何人都会遇到同样的问题,但无论如何我更愿意解决它。
Well my issue was that I forgot to include ReduxReactRouter inside the compose function in my store, in the production condition.
enhancer = compose(middleware, reduxReactRouter({ routes, createHistory }));
I doubt anyone will have the same issue, but I prefer to solve it anyway.
相关问答
更多-
工作代码! 它输出你想要的所有东西! 更新你的代码我有一个错误,我也修复了.. Ranger2将始终驻留在undefined,因为当计数为数组时没有3个站 ,记住javascript中的数组从0(零)开始计数。 我将Drew barontini改为“0” addRanger(lighthouseRock, "Nick Walsh", "magnification burn", 2); addRanger(lighthouseRock, "Drew Barontini", "uppercut launch", ...
-
在package.json中我改变了: "devDependencies": { "@angular/cli": "1.0.0" 至: "devDependencies": { "@angular/cli": "1.2.0" In package.json I changed: "devDependencies": { "@angular/cli": "1.0.0" To: "devDependencies": { "@angular/cli": "1.2.0"
-
React Native - 无法读取未定义的属性“addListener”(React Native - Cannot read property 'addListener' of undefined)[2022-06-08]
尝试使用EventEmitter和Subscribable注释掉这两行。 这些库应该用作mixin,因此ES6不支持它。 Try to comment out those two lines with EventEmitter and Subscribable. Those libs should be used as mixins, so ES6 does not support it. -
无法读取React native中未定义的属性[PROPERTY](Cannot read property [PROPERTY] of undefined in React native)[2023-04-15]
constructor(props) { super(props); this.addNewTodo = this.addNewTodo.bind(this); // Here is the key ... } constructor(props) { super(props); this.addNewTodo = this.addNewTodo.bind(this); // Here is the key ... } -
无法读取未定义的React材料设计的属性“日期”(Cannot read property 'date' of undefined React material design)[2022-03-12]
看看这个包含DatePicker组件代码的工作示例 。 这个错误可能在下面 - 您MuiPickersUtilsProvider使用所提供的utils prop将您的组件包装在MuiPickersUtilsProvider ? -
反应“Uncaught TypeError:无法读取未定义的属性”(React “Uncaught TypeError: Cannot read property of undefined”)[2022-02-10]
你需要将这个.omeFn作为道具传递给NumberList,它是另一个组件(我把它放在我的代码中的模型组件之外)。 你不需要绑定箭头函数或方法。 尽管如此,您尝试从您的组件NumberList访问另一个组件Modal的方法。 请在下面找到一个工作沙箱(点击数字列表项后查看控制台;它应该显示来自someFn的“作品”)。 const NumberList = props => { const letters = ["3d_rotation e84d", "ac_unit eb3b"]; const l ... -
您是否设置了Android虚拟设备(AVD)? 有可能是因为没有Android模拟器可以连接到该错误。 我能够用没有配置AVD的新鲜tns run android --emulator重现错误,但是在我设置了一个tns run android --emulator工作顺利。 如果您没有AVD设置,可以使用Android Studio和本指南启动并运行: https : //developer.android.com/studio/run/managing-avds.html Have you set up ...
-
您错过了从getPicture函数返回promise,以便您可以使用.then函数。 码 getPicture () : any { // my options here return this.platform.ready().then(() => { navigator.camera.getPicture((result) => { console.log(result); // Called return Promise.resolve(r ...
-
好吧,我的问题是我忘记在生产条件下将ReduxReactRouter包含在我的商店的compose函数中。 enhancer = compose(middleware, reduxReactRouter({ routes, createHistory })); 我怀疑任何人都会遇到同样的问题,但无论如何我更愿意解决它。 Well my issue was that I forgot to include ReduxReactRouter inside the compose function in my ...
-
React未定义(无法读取未定义的属性'createElement')(React is undefined (Cannot read property 'createElement' of undefined))[2023-11-06]
模块解析有点复杂,因为Typescript与Babel和Webpack不同。 如果您想了解更多信息,可以查看以下评论: https : //github.com/Microsoft/TypeScript/issues/5565#issuecomment-155216760 回到你的问题, allowSyntheticDefaultImports告诉Typescript允许从默认导入模块没有默认导出,但发出的代码不会改变。 因此,您需要将解析模块的责任移至Webpack或Babel。 在Typescript配 ...