Last active
January 7, 2017 18:58
-
-
Save GeorgesOatesLarsen/5348ffd29c408122f5caa79c7bc45899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
packBlockData(rawdata, bitsPerBlock) | |
{ | |
let blockCount = Chunk.l * Chunk.w * 16; | |
let resultantBuffer = Buffer.alloc(blockCount * bitsPerBlock/8); | |
for (let block = 0; block < blockCount; block++) { | |
//Determine the start-bit for the block. | |
let bit = block * bitsPerBlock; | |
//Determine the start-byte for that bit. | |
let targetlong = Math.floor(bit/64); | |
//We write backwards from the end of the long. First, we determine the local writing start. | |
let localstart = 64 - bit%64 - bitsPerBlock; | |
//Next, we determine if we need to write across multiple branches | |
if (localstart >= 0) { //We do not! | |
//We'll read both halves of the long out of what we've already written: | |
let longhalfa = resultantBuffer.readUInt32BE(targetlong * 4, true); | |
let longhalfb = resultantBuffer.readUInt32BE(targetlong * 4 + 1, true); | |
//Pretend our data starts at the end of the first half. Bit shift it into the second half accordingly: | |
let dataToWritea = rawdata >>> (localstart - (64 - bitsPerBlock)); | |
//Pretend our data starts at the end of the second half. Bit shift it into the first half accordingly: | |
let dataToWriteb = rawdata << (64 - localstart - bitsPerBlock); | |
//Now, we write into our long-halves: | |
longhalfa = longhalfa | dataToWritea; | |
longhalfb = longhalfb | dataToWriteb; | |
//Finally, we write our long-halves back into the buffer: | |
resultantBuffer.writeUInt32BE(longhalfa, targetlong * 4); | |
resultantBuffer.writeUInt32BE(longhalfb, targetlong * 4 + 1); | |
} | |
else { //We do. | |
//We now have two different local starts: | |
localstarta = 64 + localstart; | |
localstartb = 0; | |
//We'll load the first half of the first long, and the last half of the last long | |
let longhalfa = resultantBuffer.readUInt32BE(targetlong * 4 + 4, true);//last half of last long | |
let longhalfb = resultantBuffer.readUInt32BE(targetlong * 4, true);//First half of first long | |
//Pretend our data starts at the end of the first half. Bit shift it into the second half accordingly: | |
let dataToWritea = rawdata >>> (localstarta - (64 - bitsPerBlock)); | |
//Pretend our data starts at the end of the second half. Bit shift it into the first half accordingly: | |
let dataToWriteb = rawdata << (64 - localstartb - bitsPerBlock); | |
//Now, we write into our long-halves: | |
longhalfa = longhalfa | dataToWritea; | |
longhalfb = longhalfb | dataToWriteb; | |
//Finally, we write our long-halves back into the buffer: | |
resultantBuffer.writeUInt32BE(longhalfa, targetlong * 4 + 4); | |
resultantBuffer.writeUInt32BE(longhalfb, targetlong * 4); | |
} | |
} | |
return resultantBuffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment