What’s an efficient way to convert “unsigned char” array to its “unsigned short” counterpart? I usually use the following code snippet to do so.
#define CH_LINE_PIXELS 2291
#define SCANLINE_SIZE 57301
#define CH1_INDEX 2297
#define CH2_INDEX 4592
#define CH3_INDEX 6887
#define CH4_INDEX 9182
unsigned char* pUChar = new unsigned char[SCANLINE_SIZE];
unsigned short *pUS1, *pUS2, *pUS3, *pUS4;
pUS1 = reinterpret_cast<unsigned short *>(&pUChar[CH1_INDEX]);
pUS2 = reinterpret_cast<unsigned short *>(&pUChar[CH2_INDEX]);
pUS3 = reinterpret_cast<unsigned short *>(&pUChar[CH3_INDEX]);
pUS4 = reinterpret_cast<unsigned short *>(&pUChar[CH4_INDEX]);
unsigned short us1, us2;
for (unsigned int i = 0; i < CH_LINE_PIXELS; i++)
{
us1 = pUChar[CH1_INDEX + 2 * i];
us2 = pUChar[CH1_INDEX + 2 * x + 1];
pUS1[x] = us1 * 0x100 + us2;
us1 = pUChar[CH2_INDEX + 2 * i];
us2 = pUChar[CH2_INDEX + 2 * i + 1];
pUS2[x] = us1 * 0x100 + us2;
us1 = pUChar[CH3_INDEX + 2 * i];
us2 = pUChar[CH3_INDEX + 2 * i + 1];
pUS3[x] = us1 * 0x100 + us2;
us1 = pUChar[CH4_INDEX + 2 * i];
us2 = pUChar[CH4_INDEX + 2 * i + 1];
pUS4[x] = us1 * 0x100 + us2;
}
Addressing
shorton byte boundary may (or may not) cause alignment issues, depending on platform.Also, multiplying is very ineffective, why not use shifting instead? (some compilers may optimize
x * 0x100, but if they don’t – it’s a huge performance hit when all you want is justx << 8…)Also, as noted,
reinterpret_castmay not work as you expect it to.I would suggest, since you do assignments anyway, to copy values from array of
charto a separate array ofshort. It costs some memory, but will save you oh so much trouble with unexpected crashes and what else.