Skip to content

Commit 0e307e7

Browse files
committed
refactor
Signed-off-by: Ian Chen <[email protected]>
1 parent e9b5fdc commit 0e307e7

File tree

2 files changed

+46
-71
lines changed

2 files changed

+46
-71
lines changed

graphics/src/Image.cc

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

45+
/// \brief Color type for this image
46+
public: FREE_IMAGE_COLOR_TYPE colorType;
47+
48+
/// \brief Image type, i.e. pixel format
49+
public: FREE_IMAGE_TYPE imageType;
50+
4551
/// \brief Implementation of Data, returns vector of bytes
4652
public: std::vector<unsigned char> DataImpl(FIBITMAP *_img) const;
4753

@@ -150,6 +156,9 @@ int Image::Load(const std::string &_filename)
150156
return -1;
151157
}
152158

159+
this->dataPtr->colorType = FreeImage_GetColorType(this->dataPtr->bitmap);
160+
this->dataPtr->imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
161+
153162
return 0;
154163
}
155164

@@ -257,6 +266,8 @@ void Image::SetFromData(const unsigned char *_data,
257266
this->Height());
258267
FreeImage_Unload(toDelete);
259268
}
269+
this->dataPtr->colorType = FreeImage_GetColorType(this->dataPtr->bitmap);
270+
this->dataPtr->imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
260271
}
261272

262273
//////////////////////////////////////////////////
@@ -285,6 +296,8 @@ void Image::SetFromCompressedData(unsigned char *_data,
285296
FIMEMORY *fiMem = FreeImage_OpenMemory(_data, _size);
286297
this->dataPtr->bitmap = FreeImage_LoadFromMemory(format, fiMem);
287298
FreeImage_CloseMemory(fiMem);
299+
this->dataPtr->colorType = FreeImage_GetColorType(this->dataPtr->bitmap);
300+
this->dataPtr->imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
288301
}
289302
else
290303
{
@@ -422,10 +435,9 @@ math::Color Image::Pixel(unsigned int _x, unsigned int _y) const
422435
return clr;
423436
}
424437

425-
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(this->dataPtr->bitmap);
426-
FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
427-
428-
if ((type == FIC_RGB || type == FIC_RGBALPHA) && (imageType == FIT_BITMAP))
438+
if ((this->dataPtr->colorType == FIC_RGB ||
439+
this->dataPtr->colorType == FIC_RGBALPHA) &&
440+
(this->dataPtr->imageType == FIT_BITMAP))
429441
{
430442
RGBQUAD firgb;
431443
if (FreeImage_GetPixelColor(this->dataPtr->bitmap, _x, _y, &firgb) == FALSE)
@@ -480,65 +492,20 @@ math::Color Image::AvgColor() const
480492
//////////////////////////////////////////////////
481493
math::Color Image::MaxColor() const
482494
{
483-
unsigned int x, y;
484-
math::Color clr;
485495
math::Color maxClr;
486496

487-
maxClr.Set(0, 0, 0, 0);
488-
489497
if (!this->Valid())
490-
return clr;
498+
return maxClr;
491499

492-
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(this->dataPtr->bitmap);
493-
FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(this->dataPtr->bitmap);
494-
495-
if ((type == FIC_RGB || type == FIC_RGBALPHA) && (imageType == FIT_BITMAP))
496-
{
497-
RGBQUAD firgb;
498-
499-
for (y = 0; y < this->Height(); y++)
500-
{
501-
for (x = 0; x < this->Width(); x++)
502-
{
503-
clr.Set(0, 0, 0, 0);
504-
505-
if (FALSE ==
506-
FreeImage_GetPixelColor(this->dataPtr->bitmap, x, y, &firgb))
507-
{
508-
gzerr << "Failed to get pixel value at ["
509-
<< x << " " << y << "] \n";
510-
continue;
511-
}
512-
clr.Set(firgb.rgbRed / 255.0f, firgb.rgbGreen / 255.0f,
513-
firgb.rgbBlue / 255.0f);
514-
515-
if (clr.R() + clr.G() + clr.B() > maxClr.R() + maxClr.G() + maxClr.B())
516-
{
517-
maxClr = clr;
518-
}
519-
}
520-
}
521-
}
522-
else
500+
maxClr.Set(0, 0, 0, 0);
501+
for (unsigned int y = 0; y < this->Height(); y++)
523502
{
524-
for (y = 0; y < this->Height(); y++)
503+
for (unsigned int x = 0; x < this->Width(); x++)
525504
{
526-
for (x = 0; x < this->Width(); x++)
505+
math::Color clr = this->Pixel(x, y);
506+
if (clr.R() + clr.G() + clr.B() > maxClr.R() + maxClr.G() + maxClr.B())
527507
{
528-
clr.Set(0, 0, 0, 0);
529-
530-
if (this->dataPtr->PixelIndex(
531-
this->dataPtr->bitmap, x, y, clr) == FALSE)
532-
{
533-
gzerr << "Failed to get pixel value at ["
534-
<< x << " " << y << "] \n";
535-
continue;
536-
}
537-
538-
if (clr.R() + clr.G() + clr.B() > maxClr.R() + maxClr.G() + maxClr.B())
539-
{
540-
maxClr = clr;
541-
}
508+
maxClr = clr;
542509
}
543510
}
544511
}
@@ -553,12 +520,11 @@ BOOL Image::Implementation::PixelIndex(
553520
if (!_dib)
554521
return FALSE;
555522

556-
if (_x >= FreeImage_GetWidth(_dib) || _y >= FreeImage_GetHeight(_dib))
523+
if (_x >= this->Width() || _y >= this->Height())
557524
return FALSE;
558525

559-
FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(_dib);
560526
// 8 bit images
561-
if (imageType == FIT_BITMAP)
527+
if (this->imageType == FIT_BITMAP)
562528
{
563529
BYTE byteValue;
564530
// FreeImage_GetPixelIndex should also work with 1 and 4 bit images
@@ -574,23 +540,23 @@ BOOL Image::Implementation::PixelIndex(
574540
_color.Set(value, value, value);
575541
}
576542
// 16 bit images
577-
else if (imageType == FIT_UINT16)
543+
else if (this->imageType == FIT_UINT16)
578544
{
579545
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));
580546
uint16_t word = static_cast<uint16_t>(bits[_x]);
581547
// convert to float value between 0-1
582548
float value = word / static_cast<float>(math::MAX_UI16);
583549
_color.Set(value, value, value);
584550
}
585-
else if (imageType == FIT_INT16)
551+
else if (this->imageType == FIT_INT16)
586552
{
587553
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));
588554
int16_t word = static_cast<int16_t>(bits[_x]);
589555
// convert to float value between 0-1
590556
float value = word / static_cast<float>(math::MAX_I16);
591557
_color.Set(value, value, value);
592558
}
593-
else if (imageType == FIT_RGB16)
559+
else if (this->imageType == FIT_RGB16)
594560
{
595561
const unsigned int channels = 3u;
596562
WORD *bits = reinterpret_cast<WORD *>(FreeImage_GetScanLine(_dib, _y));
@@ -603,7 +569,7 @@ BOOL Image::Implementation::PixelIndex(
603569
float valueB = b / static_cast<float>(math::MAX_UI16);
604570
_color.Set(valueR, valueG, valueB);
605571
}
606-
else if (imageType == FIT_RGBA16)
572+
else if (this->imageType == FIT_RGBA16)
607573
{
608574
const unsigned int channels = 4u;
609575
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
@@ -684,10 +684,10 @@ TEST_F(ImageTest, Color16bit)
684684
std::string fileName = common::testing::TestFile("data",
685685
"rgb_16bit.png");
686686
EXPECT_EQ(0, img.Load(fileName));
687-
unsigned int width = 4u;
688-
unsigned int height = 4u;
689-
unsigned int channels = 3u;
690-
unsigned int bpp = channels * 16u;
687+
const unsigned int width = 4u;
688+
const unsigned int height = 4u;
689+
const unsigned int channels = 3u;
690+
const unsigned int bpp = channels * 16u;
691691
EXPECT_TRUE(img.Valid());
692692
EXPECT_EQ(width, img.Width());
693693
EXPECT_EQ(height, img.Height());
@@ -698,16 +698,20 @@ TEST_F(ImageTest, Color16bit)
698698
EXPECT_NEAR(maxColor.R(), img.MaxColor().R(), 1e-3);
699699
EXPECT_NEAR(maxColor.G(), img.MaxColor().G(), 1e-3);
700700
EXPECT_NEAR(maxColor.B(), img.MaxColor().B(), 1e-3);
701+
math::Color pixelWithMaxColor = img.Pixel(3, 1);
702+
EXPECT_NEAR(maxColor.R(), pixelWithMaxColor.R(), 1e-3);
703+
EXPECT_NEAR(maxColor.G(), pixelWithMaxColor.G(), 1e-3);
704+
EXPECT_NEAR(maxColor.B(), pixelWithMaxColor.B(), 1e-3);
701705
}
702706
{
703707
common::Image img;
704708
std::string fileName = common::testing::TestFile("data",
705709
"rgba_16bit.png");
706710
EXPECT_EQ(0, img.Load(fileName));
707-
unsigned int width = 4u;
708-
unsigned int height = 4u;
709-
unsigned int channels = 4u;
710-
unsigned int bpp = channels * 16u;
711+
const unsigned int width = 4u;
712+
const unsigned int height = 4u;
713+
const unsigned int channels = 4u;
714+
const unsigned int bpp = channels * 16u;
711715
EXPECT_TRUE(img.Valid());
712716
EXPECT_EQ(width, img.Width());
713717
EXPECT_EQ(height, img.Height());
@@ -719,6 +723,11 @@ TEST_F(ImageTest, Color16bit)
719723
EXPECT_NEAR(maxColor.G(), img.MaxColor().G(), 1e-3);
720724
EXPECT_NEAR(maxColor.B(), img.MaxColor().B(), 1e-3);
721725
EXPECT_NEAR(maxColor.A(), img.MaxColor().A(), 1e-3);
726+
math::Color pixelWithMaxColor = img.Pixel(3, 1);
727+
EXPECT_NEAR(maxColor.R(), pixelWithMaxColor.R(), 1e-3);
728+
EXPECT_NEAR(maxColor.G(), pixelWithMaxColor.G(), 1e-3);
729+
EXPECT_NEAR(maxColor.B(), pixelWithMaxColor.B(), 1e-3);
730+
EXPECT_NEAR(maxColor.A(), pixelWithMaxColor.A(), 1e-3);
722731
}
723732
}
724733

0 commit comments

Comments
 (0)