Skip to content

Commit ed4c70c

Browse files
iche033mergify[bot]
authored andcommitted
refactor
Signed-off-by: Ian Chen <[email protected]> (cherry picked from commit 0e307e7) # Conflicts: # graphics/src/Image.cc
1 parent 68abe32 commit ed4c70c

File tree

2 files changed

+48
-71
lines changed

2 files changed

+48
-71
lines changed

graphics/src/Image.cc

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ namespace gz
4242
/// \brief path name of the image file
4343
public: std::string fullName;
4444

45+
<<<<<<< HEAD
4546
/// \brief Implementation of GetData
4647
/// \deprecated remove once the Data functions using raw pointers
4748
/// are removed, in favor of returning vectors of bytes
4849
public: void DataImpl(unsigned char **_data, unsigned int &_count,
4950
FIBITMAP *_img) const;
51+
=======
52+
/// \brief Color type for this image
53+
public: FREE_IMAGE_COLOR_TYPE colorType;
54+
55+
/// \brief Image type, i.e. pixel format
56+
public: FREE_IMAGE_TYPE imageType;
57+
>>>>>>> 0e307e7 (refactor)
5058

5159
/// \brief Implementation of Data, returns vector of bytes
5260
public: std::vector<unsigned char> DataImpl(FIBITMAP *_img) const;
@@ -156,6 +164,9 @@ int Image::Load(const std::string &_filename)
156164
return -1;
157165
}
158166

167+
this->dataPtr->colorType = FreeImage_GetColorType(this->dataPtr->bitmap);
168+
this->dataPtr->imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
169+
159170
return 0;
160171
}
161172

@@ -263,6 +274,8 @@ void Image::SetFromData(const unsigned char *_data,
263274
this->Height());
264275
FreeImage_Unload(toDelete);
265276
}
277+
this->dataPtr->colorType = FreeImage_GetColorType(this->dataPtr->bitmap);
278+
this->dataPtr->imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
266279
}
267280

268281
//////////////////////////////////////////////////
@@ -291,6 +304,8 @@ void Image::SetFromCompressedData(unsigned char *_data,
291304
FIMEMORY *fiMem = FreeImage_OpenMemory(_data, _size);
292305
this->dataPtr->bitmap = FreeImage_LoadFromMemory(format, fiMem);
293306
FreeImage_CloseMemory(fiMem);
307+
this->dataPtr->colorType = FreeImage_GetColorType(this->dataPtr->bitmap);
308+
this->dataPtr->imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
294309
}
295310
else
296311
{
@@ -528,10 +543,9 @@ math::Color Image::Pixel(unsigned int _x, unsigned int _y) const
528543
return clr;
529544
}
530545

531-
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(this->dataPtr->bitmap);
532-
FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
533-
534-
if ((type == FIC_RGB || type == FIC_RGBALPHA) && (imageType == FIT_BITMAP))
546+
if ((this->dataPtr->colorType == FIC_RGB ||
547+
this->dataPtr->colorType == FIC_RGBALPHA) &&
548+
(this->dataPtr->imageType == FIT_BITMAP))
535549
{
536550
RGBQUAD firgb;
537551
if (FreeImage_GetPixelColor(this->dataPtr->bitmap, _x, _y, &firgb) == FALSE)
@@ -586,65 +600,20 @@ math::Color Image::AvgColor() const
586600
//////////////////////////////////////////////////
587601
math::Color Image::MaxColor() const
588602
{
589-
unsigned int x, y;
590-
math::Color clr;
591603
math::Color maxClr;
592604

593-
maxClr.Set(0, 0, 0, 0);
594-
595605
if (!this->Valid())
596-
return clr;
597-
598-
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(this->dataPtr->bitmap);
599-
FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
600-
601-
if ((type == FIC_RGB || type == FIC_RGBALPHA) && (imageType == FIT_BITMAP))
602-
{
603-
RGBQUAD firgb;
606+
return maxClr;
604607

605-
for (y = 0; y < this->Height(); y++)
606-
{
607-
for (x = 0; x < this->Width(); x++)
608-
{
609-
clr.Set(0, 0, 0, 0);
610-
611-
if (FALSE ==
612-
FreeImage_GetPixelColor(this->dataPtr->bitmap, x, y, &firgb))
613-
{
614-
gzerr << "Failed to get pixel value at ["
615-
<< x << " " << y << "] \n";
616-
continue;
617-
}
618-
clr.Set(firgb.rgbRed / 255.0f, firgb.rgbGreen / 255.0f,
619-
firgb.rgbBlue / 255.0f);
620-
621-
if (clr.R() + clr.G() + clr.B() > maxClr.R() + maxClr.G() + maxClr.B())
622-
{
623-
maxClr = clr;
624-
}
625-
}
626-
}
627-
}
628-
else
608+
maxClr.Set(0, 0, 0, 0);
609+
for (unsigned int y = 0; y < this->Height(); y++)
629610
{
630-
for (y = 0; y < this->Height(); y++)
611+
for (unsigned int x = 0; x < this->Width(); x++)
631612
{
632-
for (x = 0; x < this->Width(); x++)
613+
math::Color clr = this->Pixel(x, y);
614+
if (clr.R() + clr.G() + clr.B() > maxClr.R() + maxClr.G() + maxClr.B())
633615
{
634-
clr.Set(0, 0, 0, 0);
635-
636-
if (this->dataPtr->PixelIndex(
637-
this->dataPtr->bitmap, x, y, clr) == FALSE)
638-
{
639-
gzerr << "Failed to get pixel value at ["
640-
<< x << " " << y << "] \n";
641-
continue;
642-
}
643-
644-
if (clr.R() + clr.G() + clr.B() > maxClr.R() + maxClr.G() + maxClr.B())
645-
{
646-
maxClr = clr;
647-
}
616+
maxClr = clr;
648617
}
649618
}
650619
}
@@ -659,12 +628,11 @@ BOOL Image::Implementation::PixelIndex(
659628
if (!_dib)
660629
return FALSE;
661630

662-
if (_x >= FreeImage_GetWidth(_dib) || _y >= FreeImage_GetHeight(_dib))
631+
if (_x >= this->Width() || _y >= this->Height())
663632
return FALSE;
664633

665-
FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(_dib);
666634
// 8 bit images
667-
if (imageType == FIT_BITMAP)
635+
if (this->imageType == FIT_BITMAP)
668636
{
669637
BYTE byteValue;
670638
// FreeImage_GetPixelIndex should also work with 1 and 4 bit images
@@ -680,23 +648,23 @@ BOOL Image::Implementation::PixelIndex(
680648
_color.Set(value, value, value);
681649
}
682650
// 16 bit images
683-
else if (imageType == FIT_UINT16)
651+
else if (this->imageType == FIT_UINT16)
684652
{
685653
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));
686654
uint16_t word = static_cast<uint16_t>(bits[_x]);
687655
// convert to float value between 0-1
688656
float value = word / static_cast<float>(math::MAX_UI16);
689657
_color.Set(value, value, value);
690658
}
691-
else if (imageType == FIT_INT16)
659+
else if (this->imageType == FIT_INT16)
692660
{
693661
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));
694662
int16_t word = static_cast<int16_t>(bits[_x]);
695663
// convert to float value between 0-1
696664
float value = word / static_cast<float>(math::MAX_I16);
697665
_color.Set(value, value, value);
698666
}
699-
else if (imageType == FIT_RGB16)
667+
else if (this->imageType == FIT_RGB16)
700668
{
701669
const unsigned int channels = 3u;
702670
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));
@@ -709,7 +677,7 @@ BOOL Image::Implementation::PixelIndex(
709677
float valueB = b / static_cast<float>(math::MAX_UI16);
710678
_color.Set(valueR, valueG, valueB);
711679
}
712-
else if (imageType == FIT_RGBA16)
680+
else if (this->imageType == FIT_RGBA16)
713681
{
714682
const unsigned int channels = 4u;
715683
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));

graphics/src/Image_TEST.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,10 @@ TEST_F(ImageTest, Color16bit)
739739
std::string fileName = common::testing::TestFile("data",
740740
"rgb_16bit.png");
741741
EXPECT_EQ(0, img.Load(fileName));
742-
unsigned int width = 4u;
743-
unsigned int height = 4u;
744-
unsigned int channels = 3u;
745-
unsigned int bpp = channels * 16u;
742+
const unsigned int width = 4u;
743+
const unsigned int height = 4u;
744+
const unsigned int channels = 3u;
745+
const unsigned int bpp = channels * 16u;
746746
EXPECT_TRUE(img.Valid());
747747
EXPECT_EQ(width, img.Width());
748748
EXPECT_EQ(height, img.Height());
@@ -753,16 +753,20 @@ TEST_F(ImageTest, Color16bit)
753753
EXPECT_NEAR(maxColor.R(), img.MaxColor().R(), 1e-3);
754754
EXPECT_NEAR(maxColor.G(), img.MaxColor().G(), 1e-3);
755755
EXPECT_NEAR(maxColor.B(), img.MaxColor().B(), 1e-3);
756+
math::Color pixelWithMaxColor = img.Pixel(3, 1);
757+
EXPECT_NEAR(maxColor.R(), pixelWithMaxColor.R(), 1e-3);
758+
EXPECT_NEAR(maxColor.G(), pixelWithMaxColor.G(), 1e-3);
759+
EXPECT_NEAR(maxColor.B(), pixelWithMaxColor.B(), 1e-3);
756760
}
757761
{
758762
common::Image img;
759763
std::string fileName = common::testing::TestFile("data",
760764
"rgba_16bit.png");
761765
EXPECT_EQ(0, img.Load(fileName));
762-
unsigned int width = 4u;
763-
unsigned int height = 4u;
764-
unsigned int channels = 4u;
765-
unsigned int bpp = channels * 16u;
766+
const unsigned int width = 4u;
767+
const unsigned int height = 4u;
768+
const unsigned int channels = 4u;
769+
const unsigned int bpp = channels * 16u;
766770
EXPECT_TRUE(img.Valid());
767771
EXPECT_EQ(width, img.Width());
768772
EXPECT_EQ(height, img.Height());
@@ -774,6 +778,11 @@ TEST_F(ImageTest, Color16bit)
774778
EXPECT_NEAR(maxColor.G(), img.MaxColor().G(), 1e-3);
775779
EXPECT_NEAR(maxColor.B(), img.MaxColor().B(), 1e-3);
776780
EXPECT_NEAR(maxColor.A(), img.MaxColor().A(), 1e-3);
781+
math::Color pixelWithMaxColor = img.Pixel(3, 1);
782+
EXPECT_NEAR(maxColor.R(), pixelWithMaxColor.R(), 1e-3);
783+
EXPECT_NEAR(maxColor.G(), pixelWithMaxColor.G(), 1e-3);
784+
EXPECT_NEAR(maxColor.B(), pixelWithMaxColor.B(), 1e-3);
785+
EXPECT_NEAR(maxColor.A(), pixelWithMaxColor.A(), 1e-3);
777786
}
778787
}
779788

0 commit comments

Comments
 (0)