Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo bytes into one
    primarykey
    data
    text
    <p>First off, I apologize if this is a duplicate; but my Google-fu seems to be failing me today. </p> <p>I'm in the middle of writing an image format module for Photoshop, and one of the save options for this format, includes a 4-bit alpha channel. Of course, the data I have to convert is 8-bit/1 byte alpha - so I need to essentially take every two bytes of alpha, and merge it into one. </p> <p>my attempt (below), I believe has a lot of room for improvement:</p> <pre><code>for(int x=0,w=0;x &lt; alphaData.size();x+=2,w++) { short ashort=(alphaData[x] &lt;&lt; 8)+alphaData[x+1]; alphaFinal[w]=(unsigned char)ashort; } </code></pre> <p>alphaData and alphaFinal are vectors that contains the 8-bit alpha data and the 4-bit alpha data, respectively. I realize that reducing two bytes into the value of one, is bound to result in loss of "resolution", but I can't help but think there's a better way of doing this. </p> <p>For extra information, here's the loop that does the reverse (converts 4-bit alpha from the format to 8-bit for Photoshop)</p> <p>alphaData serves the same purpose as above, and imgData is an unsigned char vector that holds the raw image data. (alpha data is tacked on after the actual rgb data for the image in this particular variant of the format)</p> <pre><code>for(int b=alphaOffset,x2=0;b &lt; (alphaOffset+dataLength); b++,x2+=2) { unsigned char lo = (imgData[b] &amp; 15); unsigned char hi = ((imgData[b] &gt;&gt; 4) &amp; 15); alphaData[x2]=lo*17; alphaData[x2+1]=hi*17; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload