Transcoding from raw pixel data and how to properly construct an in memory save

Part of this may be out of scope, but i am trying to use PVRTexLib to transcode files. At the moment we are capable of using the CLI tool, but are moving towards using the lib to do it on in memory images.

The majority of my code is in c#, but i am using pinvoke to call native dlls. What has me stuck (partially due to the fact the documentation link for the library returns an error on the new documentation setup) is how to load my data into a pvrtexture to then transcode it to the compressed formats i need. I am using skiasharp to load my files (png, or jpeg) as raw bitmaps, and then getting a byte array of the pixels.

PVRHeader_CreateParams pvrHeader;
    pvrHeader.preMultiplied = preMultiplied;
    pvrHeader.pixelFormat = ptFormat;
    pvrHeader.colourSpace = eColourspace;
    pvrHeader.channelType = eChannelType;
    pvrHeader.width = u32Width;
    pvrHeader.height = u32Height;
    pvrHeader.depth = u32Depth;
    pvrHeader.numFaces = 1;
    PVRTextureHeader header(&pvrHeader);
    PVRTexture* pvrTexture = new PVRTexture(header, data);

is how i am currently attempting to load this - and i pass the properties via conversions from skiasharps header:
data, (uint)bitmap.Width, (uint)bitmap.Height, 1, FromColorType(bitmap.Info.ColorType), bitmap.Info.AlphaType == SkiaSharp.SKAlphaType.Premul, PVRTexLibNET.VariableType.PVRTLVT_UnsignedByteNorm, FromColorSpace(bitmap.Info.ColorSpace)

however when i run this i am getting a memory access violation - meaning my header is defining more data that i’ve actually passed to the method and i’m not sure where i should be looking to resolve this.

Also, assuming i can resolve the above, how would i structure an in memory copy of a pvrtexture such that i could either write it to file myself, or upload it somewhere? i know i can use the GetTextureDataSize and GetTextureDataPointer methods, but i read that doesn’t include the header and i would need more to be able to output a valid file?

any help, or a link to working documentation would be appreciated.

Hi rdpeake,

Thanks for your questions and welcome to the PVR Developer Forum!

Apologies for the documentation error in the 2021 R2 release of PVRTexTool, it will be fixed in the next release (2022 R1). In the meantime you can access the documentation of the previous release of PVRTexTool (2021 R1) in our Developer Portal, under Recent Releases - Imagination Developers (please note you need to have a Developer Portal account and login in order to download any files).

In the 2021 R1 documentation, under PVRTexTool LibraryExamples of Using the PVRTexTool Library you will find quite a few examples with source code on how to use PVRTexTool library. There is a specific example Reading and Transcoding an Image which should help you advance transcoding files.

Best regards and many thanks,

Thanks for the help AlejadroC,

I have successfully been able to create and transcode my bitmaps with that documentation. my issue now is writing them out. if i use the saveToFile api i am able to get a pvr file with all of the encoded data in it writting. however i am wanting a byte array of this so that i can write it to places other than the local file system. The examples i can see of getting the texture data seem to only deal with non-compressed textures and manipulating the raw pixel data - is there another way i am supposed to go about creating an in memory file of my already transcoded image for use later?



Hi rdpeake,

In our upcoming release of PVRTexTool we have added a new API to PVRTexLib: “PVRTexLib_SaveTextureToMemory”, which if I have not misunderstood does exactly what your asking for, it’s similar to the current file saving APIs but redirects the file data to a memory buffer that’s accessible by the user. The C++ wrapper variant “SaveTextureToMemory” has convenient overloads such as using a std::vector or std::unique_ptr to wrap the raw buffer. In the meantime I am afraid the only solution is to save the texture to a file and then load the data back in as a byte stream.


Hi Shaun,

That sounds like the exact API i am wanting, do you have an eta, or anywhere i can track the release status for the next release with this API?