| 340 | } |
| 341 | |
| 342 | bool 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 |
nothing calls this directly
no test coverage detected