Skip to content

Commit

Permalink
Some Lump cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ioan-chera committed Nov 4, 2023
1 parent ccd43a9 commit 660f367
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 54 deletions.
33 changes: 14 additions & 19 deletions src/w_loadpic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ static void DrawColumn(const Palette &pal, const ConfigData &config,Img_c& img,
}


tl::optional<Img_c> LoadImage_PNG(Lump_c *lump, const SString &name)
tl::optional<Img_c> LoadImage_PNG(const Lump_c &lump, const SString &name)
{
// load the raw data
const std::vector<byte> &tex_data = lump->getData();
const std::vector<byte> &tex_data = lump.getData();

// pass it to FLTK for decoding
Fl_PNG_Image fltk_img(NULL, tex_data.data(), (int)tex_data.size());
Expand All @@ -126,10 +126,10 @@ tl::optional<Img_c> LoadImage_PNG(Lump_c *lump, const SString &name)
}


tl::optional<Img_c> LoadImage_JPEG(Lump_c *lump, const SString &name)
tl::optional<Img_c> LoadImage_JPEG(const Lump_c &lump, const SString &name)
{
// load the raw data
const std::vector<byte> &tex_data = lump->getData();
const std::vector<byte> &tex_data = lump.getData();

// pass it to FLTK for decoding
Fl_JPEG_Image fltk_img(NULL, tex_data.data());
Expand All @@ -147,10 +147,10 @@ tl::optional<Img_c> LoadImage_JPEG(Lump_c *lump, const SString &name)
}


tl::optional<Img_c> LoadImage_TGA(Lump_c *lump, const SString &name)
tl::optional<Img_c> LoadImage_TGA(const Lump_c &lump, const SString &name)
{
// load the raw data
const std::vector<byte> &tex_data = lump->getData();
const std::vector<byte> &tex_data = lump.getData();

// decode it
int width;
Expand Down Expand Up @@ -215,7 +215,7 @@ static bool ComposePicture(Img_c& dest, const tl::optional<Img_c> &sub,
// Return true on success, false on failure.
//
bool LoadPicture(const Palette &pal, const ConfigData &config, Img_c& dest, // image to load picture into
Lump_c *lump,
const Lump_c &lump,
const SString &pic_name, // picture name (for messages)
int pic_x_offset, // coordinates of top left corner of picture
int pic_y_offset, // relative to top left corner of buffer
Expand Down Expand Up @@ -275,7 +275,7 @@ bool LoadPicture(const Palette &pal, const ConfigData &config, Img_c& dest,

/* DOOM format */

const std::vector<byte> &raw_data = lump->getData();
const std::vector<byte> &raw_data = lump.getData();

auto pat = reinterpret_cast<const patch_t *>(raw_data.data());

Expand All @@ -301,7 +301,7 @@ bool LoadPicture(const Palette &pal, const ConfigData &config, Img_c& dest,
{
int offset = LE_S32(pat->columnofs[x]);

if (offset < 0 || offset >= lump->Length())
if (offset < 0 || offset >= lump.Length())
{
gLog.printf("WARNING: bad image offset 0x%08x in patch [%s]\n",
offset, pic_name.c_str());
Expand All @@ -317,19 +317,14 @@ bool LoadPicture(const Palette &pal, const ConfigData &config, Img_c& dest,
}


ImageFormat W_DetectImageFormat(Lump_c *lump)
ImageFormat W_DetectImageFormat(const Lump_c &lump)
{
byte header[20];
int length = lump.Length();

int length = lump->Length();

if (length < (int)sizeof(header))
return ImageFormat::unrecognized;

lump->Seek();

if (! lump->Read(header, (int)sizeof(header)))
if (length < 20)
return ImageFormat::unrecognized;

const byte *header = lump.getData().data();

// PNG is clearly marked in the header, so check it first.

Expand Down
10 changes: 5 additions & 5 deletions src/w_loadpic.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ enum class ImageFormat
gif,
dds,
};
ImageFormat W_DetectImageFormat(Lump_c *lump);
ImageFormat W_DetectImageFormat(const Lump_c &lump);

tl::optional<Img_c> LoadImage_JPEG(Lump_c *lump, const SString &name);
tl::optional<Img_c> LoadImage_PNG(Lump_c *lump, const SString &name);
tl::optional<Img_c> LoadImage_TGA(Lump_c *lump, const SString &name);
bool LoadPicture(const Palette &pal, const ConfigData &config, Img_c &dest, Lump_c *lump, const SString &pic_name, int pic_x_offset, int pic_y_offset, int *pic_width = nullptr, int *pic_height = nullptr);
tl::optional<Img_c> LoadImage_JPEG(const Lump_c &lump, const SString &name);
tl::optional<Img_c> LoadImage_PNG(const Lump_c &lump, const SString &name);
tl::optional<Img_c> LoadImage_TGA(const Lump_c &lump, const SString &name);
bool LoadPicture(const Palette &pal, const ConfigData &config, Img_c &dest, const Lump_c &lump, const SString &pic_name, int pic_x_offset, int pic_y_offset, int *pic_width = nullptr, int *pic_height = nullptr);

#endif /* __EUREKA_W_LOADPIC_H__ */

Expand Down
30 changes: 15 additions & 15 deletions src/w_texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static void LoadTextureEntry_Strife(WadData &wad, const ConfigData &config, cons
Lump_c *lump = wad.master.findGlobalLump(picname);

if (! lump ||
! LoadPicture(wad.palette, config, img, lump, picname, xofs, yofs))
! LoadPicture(wad.palette, config, img, *lump, picname, xofs, yofs))
{
gLog.printf("texture '%.8s': patch '%.8s' not found.\n", raw->name, picname);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ static void LoadTextureEntry_DOOM(WadData &wad, const ConfigData &config, const
Lump_c *lump = wad.master.findGlobalLump(picname);

if (! lump ||
! LoadPicture(wad.palette, config, img, lump, picname, xofs, yofs))
! LoadPicture(wad.palette, config, img, *lump, picname, xofs, yofs))
{
gLog.printf("texture '%.8s': patch '%.8s' not found.\n", raw->name, picname);
}
Expand All @@ -221,7 +221,7 @@ static void LoadTextureEntry_DOOM(WadData &wad, const ConfigData &config, const
}


static void LoadTexturesLump(WadData &wad, const ConfigData &config, Lump_c *lump, const byte *pnames, int pname_size,
static void LoadTexturesLump(WadData &wad, const ConfigData &config, const Lump_c &lump, const byte *pnames, int pname_size,
bool skip_first)
{
// TODO : verify size word at front of PNAMES ??
Expand All @@ -232,7 +232,7 @@ static void LoadTexturesLump(WadData &wad, const ConfigData &config, Lump_c *lum
pname_size /= 8;

// load TEXTUREx data into memory for easier processing
const std::vector<byte> &tex_data = lump->getData();
const std::vector<byte> &tex_data = lump.getData();

// at the front of the TEXTUREx lump are some 4-byte integers
s32_t *tex_data_s32 = (s32_t *)tex_data.data();
Expand Down Expand Up @@ -276,30 +276,30 @@ static void W_LoadTextures_TX_START(WadData &wad, const ConfigData &config, cons
continue;
Lump_c *lump = lumpRef.lump.get();

ImageFormat img_fmt = W_DetectImageFormat(lump);
ImageFormat img_fmt = W_DetectImageFormat(*lump);
const SString &name = lump->Name();
tl::optional<Img_c> img;

switch (img_fmt)
{
case ImageFormat::doom: /* Doom patch */
img = Img_c();
if (! LoadPicture(wad.palette, config, *img, lump, name, 0, 0))
if (! LoadPicture(wad.palette, config, *img, *lump, name, 0, 0))
{
img.reset();
}
break;

case ImageFormat::png: /* PNG */
img = LoadImage_PNG(lump, name);
img = LoadImage_PNG(*lump, name);
break;

case ImageFormat::tga: /* TGA */
img = LoadImage_TGA(lump, name);
img = LoadImage_TGA(*lump, name);
break;

case ImageFormat::jpeg: /* JPEG */
img = LoadImage_JPEG(lump, name);
img = LoadImage_JPEG(*lump, name);
break;

case ImageFormat::unrecognized:
Expand Down Expand Up @@ -328,9 +328,9 @@ void WadData::W_LoadTextures(const ConfigData &config)
{
gLog.printf("Loading Textures from WAD #%d\n", i+1);

Lump_c *pnames = master.getDir()[i]->FindLumpInNamespace("PNAMES", WadNamespace::Global);
Lump_c *texture1 = master.getDir()[i]->FindLumpInNamespace("TEXTURE1", WadNamespace::Global);
Lump_c *texture2 = master.getDir()[i]->FindLumpInNamespace("TEXTURE2", WadNamespace::Global);
const Lump_c *pnames = master.getDir()[i]->FindLumpInNamespace("PNAMES", WadNamespace::Global);
const Lump_c *texture1 = master.getDir()[i]->FindLumpInNamespace("TEXTURE1", WadNamespace::Global);
const Lump_c *texture2 = master.getDir()[i]->FindLumpInNamespace("TEXTURE2", WadNamespace::Global);

// Note that we _require_ the PNAMES lump to exist along
// with the TEXTURE1/2 lump which uses it. Probably a
Expand All @@ -344,10 +344,10 @@ void WadData::W_LoadTextures(const ConfigData &config)
const std::vector<byte> &pname_data = pnames->getData();

if (texture1)
LoadTexturesLump(*this, config, texture1, pname_data.data(), (int)pname_data.size(), true);
LoadTexturesLump(*this, config, *texture1, pname_data.data(), (int)pname_data.size(), true);

if (texture2)
LoadTexturesLump(*this, config, texture2, pname_data.data(), (int)pname_data.size(), false);
LoadTexturesLump(*this, config, *texture2, pname_data.data(), (int)pname_data.size(), false);
}

if (config.features.tx_start)
Expand Down Expand Up @@ -689,7 +689,7 @@ const Img_c *WadData::getSprite(const ConfigData &config, int type, const Loadin
{
result = Img_c();

if (! LoadPicture(palette, config, *result, lump, info.sprite, 0, 0))
if (! LoadPicture(palette, config, *result, *lump, info.sprite, 0, 0))
{
result.reset();
}
Expand Down
30 changes: 15 additions & 15 deletions test/w_loadpic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ void DetectImageFormat::SetUp()
TEST_F(DetectImageFormat, ShortFile)
{
// Empty lump give error
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::unrecognized);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::unrecognized);

// Write too little data error
lump->Write(header, (int)(sizeof(header) - 1));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::unrecognized);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::unrecognized);

// Blank data error
lump->clearData();
lump->Write(header, (int)sizeof(header));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::unrecognized);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::unrecognized);
}

TEST_F(DetectImageFormat, PNG)
{
header[0] = 0x89;
memcpy(header + 1, "PNG\r\n", 5);
lump->Write(header, (int)sizeof(header));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::png);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::png);
}

TEST_F(DetectImageFormat, JPEG)
Expand All @@ -81,7 +81,7 @@ TEST_F(DetectImageFormat, JPEG)
memcpy(header + 6, tag, 2);
lump->clearData();
lump->Write(header, (int)sizeof(header));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::jpeg);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::jpeg);
}
}
}
Expand All @@ -94,7 +94,7 @@ TEST_F(DetectImageFormat, GIF)
memcpy(header, tag, 6);
lump->clearData();
lump->Write(header, (int)sizeof(header));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::gif);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::gif);
}
}

Expand All @@ -103,7 +103,7 @@ TEST_F(DetectImageFormat, DDS)
static const char tag[] = "DDS |";
memcpy(header, tag, 5);
lump->Write(header, (int)sizeof(header));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::dds);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::dds);
}

TEST_F(DetectImageFormat, TGA)
Expand All @@ -121,7 +121,7 @@ TEST_F(DetectImageFormat, TGA)
int depth = 24;
header[16] = (byte)depth;
lump->Write(header, (int)sizeof(header));
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::tga);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::tga);
}

TEST_F(DetectImageFormat, doom)
Expand All @@ -141,7 +141,7 @@ TEST_F(DetectImageFormat, doom)
std::vector<byte> padding;
padding.resize(4 * width);
lump->Write(padding.data(), (int)padding.size());
ASSERT_EQ(W_DetectImageFormat(lump), ImageFormat::doom);
ASSERT_EQ(W_DetectImageFormat(*lump), ImageFormat::doom);
}

//==================================================================================================
Expand Down Expand Up @@ -180,7 +180,7 @@ TEST(LoadImage, PNG)
// Prepare data
auto data = prepareData(pngData);

auto image = LoadImage_PNG(data.second, "our image");
auto image = LoadImage_PNG(*data.second, "our image");
assertImageValid(image);
}

Expand All @@ -193,7 +193,7 @@ TEST(LoadImage, PNGBroken)
// Prepare data
auto data = prepareData(brokenPNG);

auto image = LoadImage_PNG(data.second, "our image");
auto image = LoadImage_PNG(*data.second, "our image");
ASSERT_FALSE(image);
}

Expand Down Expand Up @@ -235,7 +235,7 @@ static const std::vector<uint8_t> jpgData = {
TEST(LoadImage, JPEG)
{
auto data = prepareData(jpgData);
auto image = LoadImage_JPEG(data.second, "our jpg");
auto image = LoadImage_JPEG(*data.second, "our jpg");
assertImageValid(image);
}

Expand All @@ -244,7 +244,7 @@ TEST(LoadImage, JPEGBroken)
auto brokenJPG = jpgData;
brokenJPG[brokenJPG.size() - 1] ^= 1;
auto data = prepareData(brokenJPG);
auto image = LoadImage_JPEG(data.second, "our jpg");
auto image = LoadImage_JPEG(*data.second, "our jpg");
ASSERT_FALSE(image);
}

Expand All @@ -258,6 +258,6 @@ TEST(LoadImage, TGA)
};

auto data = prepareData(tgaData);
auto image = LoadImage_TGA(data.second, "our tga");
auto image = LoadImage_TGA(*data.second, "our tga");
assertImageValid(image);
}
}

0 comments on commit 660f367

Please sign in to comment.