// TODO: Remove the code duplication between this and LoadFromFileData Thin3DTexture *Thin3DContext::CreateTextureFromFileData(const uint8_t *data, int size, T3DImageType type) { int width[16], height[16], zim_flags = 0; uint8_t *image[16] = { nullptr }; int num_levels = 0; T3DImageFormat fmt; if (type == DETECT) { type = DetectImageFileType(data, size); } if (type == TYPE_UNKNOWN) { ELOG("File has unknown format"); return nullptr; } switch (type) { case ZIM: num_levels = LoadZIMPtr((const uint8_t *)data, size, width, height, &zim_flags, image); fmt = ZimToT3DFormat(zim_flags & ZIM_FORMAT_MASK); break; case PNG: if (1 != pngLoadPtr((const unsigned char *)data, size, &width[0], &height[0], &image[0], false)) { return NULL; } num_levels = 1; fmt = RGBA8888; break; default: ELOG("Unknown image format"); return nullptr; } if (num_levels == 0) { return NULL; } Thin3DTexture *tex = CreateTexture(LINEAR2D, fmt, width[0], height[0], 1, num_levels); for (int i = 0; i < num_levels; i++) { tex->SetImageData(0, 0, 0, width[i], height[i], 1, i, width[i] * 4, image[i]); free(image[i]); } tex->Finalize(zim_flags); return tex; }
static bool LoadTextureLevels(const uint8_t *data, size_t size, T3DImageType type, int width[16], int height[16], int *num_levels, T3DImageFormat *fmt, uint8_t *image[16], int *zim_flags) { if (type == DETECT) { type = DetectImageFileType(data, size); } if (type == TYPE_UNKNOWN) { ELOG("File has unknown format"); return false; } *num_levels = 0; *zim_flags = 0; switch (type) { case ZIM: { *num_levels = LoadZIMPtr((const uint8_t *)data, size, width, height, zim_flags, image); *fmt = ZimToT3DFormat(*zim_flags & ZIM_FORMAT_MASK); } break; case PNG: if (1 == pngLoadPtr((const unsigned char *)data, size, &width[0], &height[0], &image[0], false)) { *num_levels = 1; *fmt = RGBA8888; } break; case JPEG: { int actual_components = 0; unsigned char *jpegBuf = jpgd::decompress_jpeg_image_from_memory(data, (int)size, &width[0], &height[0], &actual_components, 4); if (jpegBuf) { *num_levels = 1; *fmt = RGBA8888; image[0] = (uint8_t *)jpegBuf; } } break; default: ELOG("Unknown image format"); return false; } return *num_levels > 0; }
bool Thin3DTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, T3DImageType type) { int width[16], height[16], zim_flags; uint8_t *image[16] = { nullptr }; int num_levels = LoadZIMPtr(data, dataSize, width, height, &zim_flags, image); if (num_levels == 0) { return false; } T3DImageFormat fmt = ZimToT3DFormat(zim_flags & ZIM_FORMAT_MASK); // TODO: Cube map / 3D texture / texture array support Create(LINEAR2D, fmt, width[0], height[0], 1, num_levels); for (int i = 0; i < num_levels; i++) { SetImageData(0, 0, 0, width[i], height[i], 1, i, width[i] * 4, image[i]); free(image[i]); } Finalize(zim_flags); return true; }