Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ME] Fix mipmap calculation in WebP #268

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Shared.TextureContainer/Shared.ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ public static Texture2D LoadTexture2DFromBytes(byte[] texBytes, TextureFormat fo
return tex;
}

internal static int CalculateTextureBytes(int width, int height, bool mipmap)
{
int bytes = width * height * 4;

if (!mipmap)
return bytes;

while (width > 1 || height > 1)
{
width = (width + 1) >> 1;
height = (height + 1) >> 1;
bytes += width * height * 4;
}

return bytes;
}

/// <summary>
/// Load all needed dependencies for loading all the supported file formats.
/// Needs to be to be run before any classes using these dependencies are loaded!
Expand Down
12 changes: 3 additions & 9 deletions src/Shared.TextureContainer/WebP/Texture2DExt.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//https://github.com/octo-code/webp-unity3d
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using UnityEngine;

using WebP.Extern;
using KK_Plugins;

namespace WebP
{
Expand Down Expand Up @@ -112,13 +111,8 @@ public static unsafe byte[] LoadRGBAFromWebP(byte[] lData, ref int lWidth, ref i
{
scalingFunction(ref lWidth, ref lHeight);
}

// If mipmaps are requested we need to create 1/3 more memory for the mipmaps to be generated in.
int numBytesRequired = lWidth * lHeight * 4;
if (lMipmaps)
{
numBytesRequired = Mathf.CeilToInt((numBytesRequired * 4.0f) / 3.0f);
}

int numBytesRequired = ImageHelper.CalculateTextureBytes(lWidth, lHeight, lMipmaps);
RikkiBalboa marked this conversation as resolved.
Show resolved Hide resolved

lRawData = new byte[numBytesRequired];
fixed (byte* lRawDataPtr = lRawData)
Expand Down