LibImage: Add image format probing

Instead of determining the image type in Image.cpp call image probing
functions for each supported image type
This commit is contained in:
Bananymous 2024-06-17 18:02:22 +03:00
parent 17ecbca204
commit 0bc8d49684
3 changed files with 24 additions and 20 deletions

View File

@ -41,12 +41,6 @@ namespace LibImage
return BAN::Error::from_errno(errno);
}
if (st.st_size < 2)
{
fprintf(stddbg, "invalid image (too small)\n");
return BAN::Error::from_errno(EINVAL);
}
void* addr = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
{
@ -58,21 +52,10 @@ namespace LibImage
auto image_data_span = BAN::ConstByteSpan(reinterpret_cast<uint8_t*>(addr), st.st_size);
uint16_t u16_signature = image_data_span.as<const uint16_t>();
switch (u16_signature)
{
case 0x3650:
case 0x3550:
case 0x3450:
case 0x3350:
case 0x3250:
case 0x3150:
if (probe_netbpm(image_data_span))
return TRY(load_netbpm(image_data_span));
default:
fprintf(stderr, "unrecognized image format\n");
break;
}
fprintf(stderr, "unrecognized image format\n");
return BAN::Error::from_errno(ENOTSUP);
}

View File

@ -34,6 +34,26 @@ namespace LibImage
return result;
}
bool probe_netbpm(BAN::ConstByteSpan image_data)
{
if (image_data.size() < 2)
return false;
uint16_t u16_signature = image_data.as<const uint16_t>();
switch (u16_signature)
{
case 0x3650:
case 0x3550:
case 0x3450:
case 0x3350:
case 0x3250:
case 0x3150:
return true;
default:
return false;
}
}
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(BAN::ConstByteSpan image_data)
{
if (image_data.size() < 11)

View File

@ -5,6 +5,7 @@
namespace LibImage
{
bool probe_netbpm(BAN::ConstByteSpan);
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(BAN::ConstByteSpan);
}