MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / ImportTextureStb

Method ImportTextureStb

Source/Engine/Tools/TextureTool/TextureTool.stb.cpp:342–560  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

340}
341
342bool TextureTool::ImportTextureStb(ImageType type, const StringView& path, TextureData& textureData, bool& hasAlpha)
343{
344 Array<byte> fileData;
345 if (File::ReadAllBytes(path, fileData))
346 {
347 LOG(Warning, "Failed to read data from file.");
348 return true;
349 }
350
351 switch (type)
352 {
353 case ImageType::PNG:
354 case ImageType::BMP:
355 case ImageType::GIF:
356 case ImageType::JPEG:
357 case ImageType::HDR:
358 case ImageType::TGA:
359 {
360 int width, height, components;
361 stbi_uc* stbData = stbi_load_from_memory(fileData.Get(), fileData.Count(), &width, &height, &components, 4);
362 if (!stbData)
363 {
364 LOG(Warning, "Failed to load image. {0}", String(stbi_failure_reason()));
365 return false;
366 }
367 fileData.Resize(0);
368
369 // Setup texture data
370 textureData.Width = width;
371 textureData.Height = height;
372 textureData.Depth = 1;
373 textureData.Format = PixelFormat::R8G8B8A8_UNorm;
374 textureData.Items.Resize(1);
375 textureData.Items[0].Mips.Resize(1);
376 auto& mip = textureData.Items[0].Mips[0];
377 mip.RowPitch = sizeof(Color32) * width;
378 mip.DepthPitch = mip.RowPitch * height;
379 mip.Lines = height;
380 mip.Data.Copy(stbData, mip.DepthPitch);
381
382#if USE_EDITOR
383 // Detect alpha channel usage
384 auto ptrAlpha = (Color32*)mip.Data.Get();
385 for (int32 y = 0; y < height && !hasAlpha; y++)
386 {
387 for (int32 x = 0; x < width && !hasAlpha; x++)
388 {
389 hasAlpha |= ptrAlpha->A < 255;
390 ptrAlpha++;
391 }
392 }
393#endif
394 stbi_image_free(stbData);
395 break;
396 }
397 case ImageType::RAW:
398 {
399 // Assume 16-bit, grayscale .RAW file in little-endian byte order

Callers

nothing calls this directly

Calls 15

stbi_load_from_memoryFunction · 0.85
stbi_failure_reasonFunction · 0.85
stbi_image_freeFunction · 0.85
LoadEXRFunction · 0.85
FreeEXRErrorMessageFunction · 0.85
decode_headerFunction · 0.85
get_row_pitchFunction · 0.85
get_height_pixels_blocksFunction · 0.85
get_offsetFunction · 0.85
ClampFunction · 0.85
IsPowerOfTwoFunction · 0.85

Tested by

no test coverage detected