* Fix compiler warnings

* Attempt to sort out byte order for 15 bit images.

* Try to get integer types from standard location

* Remove extra swap code by using more complex condition.
Thanks to Riccardo Mottola.
This commit is contained in:
Fred Kiefer 2021-11-08 22:46:25 +01:00 committed by GitHub
parent 6ecfa261f6
commit a047b10a51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 163 additions and 79 deletions

View file

@ -670,33 +670,62 @@
* this is strictly necessary for OpenStep tiffs.
*/
static unsigned int
_get_bit_value(unsigned char *base, long msb_off, int bit_width)
_get_bit_value(unsigned char *base, long msb_off, int bit_width,
NSBitmapFormat format)
{
long lsb_off, byte1, byte2;
int shift, value;
if (bit_width == 16)
{
long byte1 = msb_off / 8;
long byte2 = byte1 + 1;
uint16_t value;
/*
* Firstly we calculate the position of the msb and lsb in terms
* of bit offsets and thus byte offsets. The shift is the number of
* spare bits left in the byte containing the lsb
*/
lsb_off= msb_off+bit_width-1;
byte1= msb_off/8;
byte2= lsb_off/8;
shift= 7-(lsb_off%8);
if ((NSHostByteOrder() == NS_BigEndian && !(format & NSBitmapFormatSixteenBitLittleEndian)) ||
(NSHostByteOrder() == NS_LittleEndian && (format & NSBitmapFormatSixteenBitBigEndian)))
{
value = base[byte2] | base[byte1] << 8;
}
else
{
value = base[byte1] | base[byte2] << 8;
}
/*
* We now get the value from the byte array, possibly using two bytes if
* the required set of bits crosses the byte boundary. This is then shifted
* down to it's correct position and extraneous bits masked off before
* being returned.
*/
value=base[byte2];
if (byte1!=byte2)
value|= base[byte1]<<8;
value >>= shift;
return value;
}
else if (bit_width == 32)
{
// FIXME Currently not handled
return 0;
}
else
{
long lsb_off, byte1, byte2;
unsigned int shift, value;
return value & ((1<<bit_width)-1);
/*
* Firstly we calculate the position of the msb and lsb in terms
* of bit offsets and thus byte offsets. The shift is the number of
* spare bits left in the byte containing the lsb
*/
lsb_off = msb_off + bit_width - 1;
byte1 = msb_off / 8;
byte2 = lsb_off / 8;
shift = 7 - (lsb_off % 8);
/*
* We now get the value from the byte array, possibly using two bytes if
* the required set of bits crosses the byte boundary. This is then shifted
* down to it's correct position and extraneous bits masked off before
* being returned.
*/
value = base[byte2];
if (byte1 != byte2)
{
value |= base[byte1] << 8;
}
value >>= shift;
return value & ((1 << bit_width) - 1);
}
}
/**
@ -732,7 +761,7 @@ _get_bit_value(unsigned char *base, long msb_off, int bit_width)
for (i = 0; i < _numColors; i++)
{
pixelData[i] = _get_bit_value(_imagePlanes[i] + line_offset,
offset, _bitsPerSample);
offset, _bitsPerSample, _format);
}
}
}
@ -752,7 +781,7 @@ _get_bit_value(unsigned char *base, long msb_off, int bit_width)
for (i = 0; i < _numColors; i++)
{
pixelData[i] = _get_bit_value(_imagePlanes[0] + line_offset,
offset, _bitsPerSample);
offset, _bitsPerSample, _format);
offset += _bitsPerSample;
}
}
@ -761,35 +790,62 @@ _get_bit_value(unsigned char *base, long msb_off, int bit_width)
static void
_set_bit_value(unsigned char *base, long msb_off, int bit_width,
unsigned int value)
NSBitmapFormat format, unsigned int value)
{
long lsb_off, byte1, byte2;
int shift;
int all;
if (bit_width == 16)
{
long byte1 = msb_off / 8;
long byte2 = byte1 + 1;
uint16_t value16 = value;
/*
* Firstly we calculate the position of the msb and lsb in terms
* of bit offsets and thus byte offsets. The shift is the number of
* spare bits left in the byte containing the lsb
*/
lsb_off= msb_off+bit_width-1;
byte1= msb_off/8;
byte2= lsb_off/8;
shift= 7-(lsb_off%8);
if ((NSHostByteOrder() == NS_BigEndian && !(format & NSBitmapFormatSixteenBitLittleEndian)) ||
(NSHostByteOrder() == NS_LittleEndian && (format & NSBitmapFormatSixteenBitBigEndian)))
{
base[byte1] = (value16 >> 8);
base[byte2] = (value16 & 255);
}
else
{
base[byte2] = (value16 >> 8);
base[byte1] = (value16 & 255);
}
}
else if (bit_width == 32)
{
// FIXME Currently not handled
}
else
{
long lsb_off, byte1, byte2;
int shift;
int all;
/*
* We now set the value in the byte array, possibly using two bytes if
* the required set of bits crosses the byte boundary. This value is
* first shifted up to it's correct position and extraneous bits are
* masked off.
*/
value &= ((1<<bit_width)-1);
value <<= shift;
all = ((1<<bit_width)-1) << shift;
/*
* Firstly we calculate the position of the msb and lsb in terms
* of bit offsets and thus byte offsets. The shift is the number of
* spare bits left in the byte containing the lsb
*/
lsb_off = msb_off + bit_width - 1;
byte1 = msb_off / 8;
byte2 = lsb_off/ 8;
shift = 7 - (lsb_off % 8);
if (byte1 != byte2)
base[byte1] = (value >> 8) | (base[byte1] & ~(all >> 8));
base[byte2] = (value & 255) | (base[byte2] & ~(all & 255));
/*
* We now set the value in the byte array, possibly using two bytes if
* the required set of bits crosses the byte boundary. This value is
* first shifted up to it's correct position and extraneous bits are
* masked off.
*/
value &= ((1 << bit_width) - 1);
value <<= shift;
all = ((1 << bit_width) - 1) << shift;
if (byte1 != byte2)
{
base[byte1] = (value >> 8) | (base[byte1] & ~(all >> 8));
}
base[byte2] = (value & 255) | (base[byte2] & ~(all & 255));
}
}
/**
@ -831,7 +887,7 @@ _set_bit_value(unsigned char *base, long msb_off, int bit_width,
for (i = 0; i < _numColors; i++)
{
_set_bit_value(_imagePlanes[i] + line_offset,
offset, _bitsPerSample, pixelData[i]);
offset, _bitsPerSample, _format, pixelData[i]);
}
}
}
@ -851,7 +907,7 @@ _set_bit_value(unsigned char *base, long msb_off, int bit_width,
for (i = 0; i < _numColors; i++)
{
_set_bit_value(_imagePlanes[0] + line_offset,
offset, _bitsPerSample, pixelData[i]);
offset, _bitsPerSample, _format, pixelData[i]);
offset += _bitsPerSample;
}
}