Two bytes into one
First off, I apologize if this is a duplicate; but my Google-fu seems to
be failing me today.
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.
my attempt (below), I believe has a lot of room for improvement:
for(int x=0,w=0;x < alphaData.size();x+=2,w++)
{
short ashort=(alphaData[x] << 8)+alphaData[x+1];
alphaFinal[w]=(unsigned char)ashort;
}
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.
For extra information, here's the loop that does the reverse (converts
4-bit alpha from the format to 8-bit for Photoshop)
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)
for(int b=alphaOffset,x2=0;b < (alphaOffset+dataLength); b++,x2+=2)
{
unsigned char lo = (imgData[b] & 15);
unsigned char hi = ((imgData[b] >> 4) & 15);
alphaData[x2]=lo*17;
alphaData[x2+1]=hi*17;
}
No comments:
Post a Comment