+/**
+ * \brief Deal with Grey levels i.e. re-arange them
+ * to have low values = dark, high values = bright
+ */
+void FileHelper::ConvertFixGreyLevels(uint8_t *raw, size_t rawSize)
+{
+ uint32_t i; // to please M$VC6
+ int16_t j;
+
+ // Number of Bits Allocated for storing a Pixel is defaulted to 16
+ // when absent from the file.
+ int bitsAllocated = FileInternal->GetBitsAllocated();
+ if ( bitsAllocated == 0 )
+ {
+ bitsAllocated = 16;
+ }
+
+ else if (bitsAllocated > 8 && bitsAllocated < 16 && bitsAllocated != 12)
+ {
+ bitsAllocated = 16;
+ }
+ // Number of "Bits Stored", defaulted to number of "Bits Allocated"
+ // when absent from the file.
+ int bitsStored = FileInternal->GetBitsStored();
+ if ( bitsStored == 0 )
+ {
+ bitsStored = bitsAllocated;
+ }
+
+ if (!FileInternal->IsSignedPixelData())
+ {
+ if ( bitsAllocated == 8 )
+ {
+ uint8_t *deb = (uint8_t *)raw;
+ for (i=0; i<rawSize; i++)
+ {
+ *deb = 255 - *deb;
+ deb++;
+ }
+ return;
+ }
+
+ if ( bitsAllocated == 16 )
+ {
+ uint16_t mask =1;
+ for (j=0; j<bitsStored-1; j++)
+ {
+ mask = (mask << 1) +1; // will be fff when BitsStored=12
+ }
+
+ uint16_t *deb = (uint16_t *)raw;
+ for (i=0; i<rawSize/2; i++)
+ {
+ *deb = mask - *deb;
+ deb++;
+ }
+ return;
+ }
+ }
+ else
+ {
+ if ( bitsAllocated == 8 )
+ {
+ uint8_t smask8 = 255;
+ uint8_t *deb = (uint8_t *)raw;
+ for (i=0; i<rawSize; i++)
+ {
+ *deb = smask8 - *deb;
+ deb++;
+ }
+ return;
+ }
+ if ( bitsAllocated == 16 )
+ {
+ uint16_t smask16 = 65535;
+ uint16_t *deb = (uint16_t *)raw;
+ for (i=0; i<rawSize/2; i++)
+ {
+ *deb = smask16 - *deb;
+ deb++;
+ }
+ return;
+ }
+ }
+}
+