Advertisement
honey_the_codewitch

pack bytes

Jan 31st, 2023
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1.  
  2. var imageData = new byte[64];
  3. var imageWidth = 8;
  4. var imageHeight = 8;
  5. var bitDepth = 1;
  6.  
  7. var dstData = new byte[imageData.Length / (8 / bitDepth)];
  8. var dstIndex = 0;
  9. var dstSubIndex = 0;
  10.  
  11. // initialize imageData
  12. for (var i = 0;i<imageData.Length;++i)
  13. {
  14.     imageData[i] = (byte)( 0==(i % 2) ? 0 : 0xFF);
  15. }
  16.  
  17. for(var y = 0;y<imageHeight;++y)
  18. {
  19.     for(var x = 0;x<imageWidth;++x)
  20.     {
  21.         var luma = imageData[y * imageWidth + x];
  22.  
  23.         luma >>= (8 - bitDepth);
  24.  
  25.         luma <<= (dstSubIndex++ * bitDepth);
  26.  
  27.         dstData[dstIndex] |= luma;
  28.         if (dstSubIndex == (8 / bitDepth))
  29.         {
  30.             dstSubIndex = 0;
  31.             ++dstIndex;
  32.         }
  33.  
  34.     }
  35. }
  36. for(var i = 0;i<dstData.Length;++i)
  37. {
  38.     Console.Write(dstData[i].ToString("X2"));
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement