MCPcopy Create free account
hub / github.com/defold/defold / generate_mipmaps

Method generate_mipmaps

engine/dlib/src/basis/encoder/basisu_comp.cpp:377–464  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

375 }
376
377 bool basis_compressor::generate_mipmaps(const image &img, basisu::vector<image> &mips, bool has_alpha)
378 {
379 debug_printf("basis_compressor::generate_mipmaps\n");
380
381 interval_timer tm;
382 tm.start();
383
384 uint32_t total_levels = 1;
385 uint32_t w = img.get_width(), h = img.get_height();
386 while (maximum<uint32_t>(w, h) > (uint32_t)m_params.m_mip_smallest_dimension)
387 {
388 w = maximum(w >> 1U, 1U);
389 h = maximum(h >> 1U, 1U);
390 total_levels++;
391 }
392
393#if BASISU_USE_STB_IMAGE_RESIZE_FOR_MIPMAP_GEN
394 // Requires stb_image_resize
395 stbir_filter filter = STBIR_FILTER_DEFAULT;
396 if (m_params.m_mip_filter == "box")
397 filter = STBIR_FILTER_BOX;
398 else if (m_params.m_mip_filter == "triangle")
399 filter = STBIR_FILTER_TRIANGLE;
400 else if (m_params.m_mip_filter == "cubic")
401 filter = STBIR_FILTER_CUBICBSPLINE;
402 else if (m_params.m_mip_filter == "catmull")
403 filter = STBIR_FILTER_CATMULLROM;
404 else if (m_params.m_mip_filter == "mitchell")
405 filter = STBIR_FILTER_MITCHELL;
406
407 for (uint32_t level = 1; level < total_levels; level++)
408 {
409 const uint32_t level_width = maximum<uint32_t>(1, img.get_width() >> level);
410 const uint32_t level_height = maximum<uint32_t>(1, img.get_height() >> level);
411
412 image &level_img = *enlarge_vector(mips, 1);
413 level_img.resize(level_width, level_height);
414
415 int result = stbir_resize_uint8_generic(
416 (const uint8_t *)img.get_ptr(), img.get_width(), img.get_height(), img.get_pitch() * sizeof(color_rgba),
417 (uint8_t *)level_img.get_ptr(), level_img.get_width(), level_img.get_height(), level_img.get_pitch() * sizeof(color_rgba),
418 has_alpha ? 4 : 3, has_alpha ? 3 : STBIR_ALPHA_CHANNEL_NONE, m_params.m_mip_premultiplied ? STBIR_FLAG_ALPHA_PREMULTIPLIED : 0,
419 m_params.m_mip_wrapping ? STBIR_EDGE_WRAP : STBIR_EDGE_CLAMP, filter, m_params.m_mip_srgb ? STBIR_COLORSPACE_SRGB : STBIR_COLORSPACE_LINEAR,
420 nullptr);
421
422 if (result == 0)
423 {
424 error_printf("basis_compressor::generate_mipmaps: stbir_resize_uint8_generic() failed!\n");
425 return false;
426 }
427
428 if (m_params.m_mip_renormalize)
429 level_img.renormalize_normal_map();
430 }
431#else
432 for (uint32_t level = 1; level < total_levels; level++)
433 {
434 const uint32_t level_width = maximum<uint32_t>(1, img.get_width() >> level);

Callers

nothing calls this directly

Calls 13

debug_printfFunction · 0.85
enlarge_vectorFunction · 0.85
error_printfFunction · 0.85
image_resampleFunction · 0.85
get_elapsed_secsMethod · 0.80
maximumFunction · 0.70
startMethod · 0.45
get_widthMethod · 0.45
get_heightMethod · 0.45
resizeMethod · 0.45
get_ptrMethod · 0.45

Tested by

no test coverage detected