Avif image - read/convert to dibitmap

New to FreeBASIC? Post your questions here.
Iczer
Posts: 80
Joined: Jul 04, 2017 18:09

Avif image - read/convert to dibitmap

Post by Iczer »

I use for image-related tasks well known library - FreeImage.dll, but recently appeared avif-format and its not supported.
It is possible to find pre-compiled libavif, but example-decode is in C and it not clear how to re-scale image and convert it to HBITMAP ( convert to freebasic, windows)

Code: Select all

// Copyright 2020 Joe Drago. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause

#include "avif/avif.h"

#include <inttypes.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
    if (argc != 2) {
        fprintf(stderr, "avif_example_decode_file [filename.avif]\n");
        return 1;
    }
    const char * inputFilename = argv[1];

    int returnCode = 1;
    avifRGBImage rgb;
    memset(&rgb, 0, sizeof(rgb));

    avifDecoder * decoder = avifDecoderCreate();
    // Override decoder defaults here (codecChoice, requestedSource, ignoreExif, ignoreXMP, etc)

    avifResult result = avifDecoderSetIOFile(decoder, inputFilename);
    if (result != AVIF_RESULT_OK) {
        fprintf(stderr, "Cannot open file for read: %s\n", inputFilename);
        goto cleanup;
    }

    result = avifDecoderParse(decoder);
    if (result != AVIF_RESULT_OK) {
        fprintf(stderr, "Failed to decode image: %s\n", avifResultToString(result));
        goto cleanup;
    }

    // Now available:
    // * All decoder->image information other than pixel data:
    //   * width, height, depth
    //   * transformations (pasp, clap, irot, imir)
    //   * color profile (icc, CICP)
    //   * metadata (Exif, XMP)
    // * decoder->alphaPresent
    // * number of total images in the AVIF (decoder->imageCount)
    // * overall image sequence timing (including per-frame timing with avifDecoderNthImageTiming())

    printf("Parsed AVIF: %ux%u (%ubpc)\n", decoder->image->width, decoder->image->height, decoder->image->depth);

    while (avifDecoderNextImage(decoder) == AVIF_RESULT_OK) {
        // Now available (for this frame):
        // * All decoder->image YUV pixel data (yuvFormat, yuvPlanes, yuvRange, yuvChromaSamplePosition, yuvRowBytes)
        // * decoder->image alpha data (alphaPlane, alphaRowBytes)
        // * this frame's sequence timing

        avifRGBImageSetDefaults(&rgb, decoder->image);
        // Override YUV(A)->RGB(A) defaults here: depth, format, chromaUpsampling, ignoreAlpha, alphaPremultiplied, libYUVUsage, etc

        // Alternative: set rgb.pixels and rgb.rowBytes yourself, which should match your chosen rgb.format
        // Be sure to use uint16_t* instead of uint8_t* for rgb.pixels/rgb.rowBytes if (rgb.depth > 8)
        avifRGBImageAllocatePixels(&rgb);

        if (avifImageYUVToRGB(decoder->image, &rgb) != AVIF_RESULT_OK) {
            fprintf(stderr, "Conversion from YUV failed: %s\n", inputFilename);
            goto cleanup;
        }

        // Now available:
        // * RGB(A) pixel data (rgb.pixels, rgb.rowBytes)

        if (rgb.depth > 8) {
            uint16_t * firstPixel = (uint16_t *)rgb.pixels;
            printf(" * First pixel: RGBA(%u,%u,%u,%u)\n", firstPixel[0], firstPixel[1], firstPixel[2], firstPixel[3]);
        } else {
            uint8_t * firstPixel = rgb.pixels;
            printf(" * First pixel: RGBA(%u,%u,%u,%u)\n", firstPixel[0], firstPixel[1], firstPixel[2], firstPixel[3]);
        }
    }

    returnCode = 0;
cleanup:
    avifRGBImageFreePixels(&rgb); // Only use in conjunction with avifRGBImageAllocatePixels()
    avifDecoderDestroy(decoder);
    return returnCode;
}
I use this function for task:

Code: Select all

Sub PreViewRender_Preview_Image_Thread(ByVal pUserData As Any Ptr)
	'-----------------------------------------------------------------------------------------
	'
	'-----------------------------------------------------------------------------------------
	Dim As UserData_ISSDBPreviewImage Ptr UserData = CPtr(UserData_ISSDBPreviewImage Ptr, pUserData)
	'-----------------------------------------------------------------------------------------
	'
	'-----------------------------------------------------------------------------------------
	Dim As FREE_IMAGE_FORMAT ImageFIF
	Dim As FIBITMAP Ptr hImageOriginal, hImageResized, hImageResized2
	Dim As HDC hDC
	' ........................................................................................
	Dim As ULong iBPP
	'-----------------------------------------------------------------------------------------
	ImageFIF = FreeImage_GetFileTypeU(StrPtr(UserData->Path))
	' ........................................................................................
	If ImageFIF = FIF_UNKNOWN Then
		ImageFIF = FreeImage_GetFIFFromFilenameU(UserData->Path)
		
		If ImageFIF = FIF_UNKNOWN Then
			UserData->iRetVal = 1
			Return
		EndIf
	EndIf
	'-----------------------------------------------------------------------------------------
	hImageOriginal = FreeImage_LoadU(ImageFIF, UserData->Path)
	' ........................................................................................
	If hImageOriginal = 0 Then
		UserData->iRetVal = 2
		Return
	EndIf
	' ........................................................................................
	UserData->iH = FreeImage_GetHeight(hImageOriginal)
	UserData->iW = FreeImage_GetWidth(hImageOriginal)
	'-----------------------------------------------------------------------------------------
	'
	'-----------------------------------------------------------------------------------------
	PreViewRender_CalcScalePicAndPicFrame()
	' ........................................................................................
	If UserData->iRetVal <> 0 Then
		If hImageOriginal <> 0 Then
			FreeImage_Unload(hImageOriginal)
		EndIf
		
		Return
	EndIf
	'-----------------------------------------------------------------------------------------
	'
	'-----------------------------------------------------------------------------------------
	hImageResized = FreeImage_Rescale(hImageOriginal, UserData->iW, UserData->iH, FILTER_CATMULLROM)' FILTER_LANCZOS3
	' ........................................................................................
	iBPP = FreeImage_GetBPP(hImageResized)
	' ........................................................................................
	If ((iBPP = 32) And (ImageFIF = FIF_PNG)) Or (iBPP > 32) Then
		hImageResized2 = FreeImage_ConvertTo24Bits(hImageResized)
		
		If hImageResized <> 0 Then
			FreeImage_Unload(hImageResized)
		EndIf
		
		hImageResized = hImageResized2
	EndIf
	' ........................................................................................
	If hImageOriginal <> 0 Then
		FreeImage_Unload(hImageOriginal)
	EndIf
	'-----------------------------------------------------------------------------------------
	If PreViewRender__hDIBitmap <> 0 Then
		DeleteObject(PreViewRender__hDIBitmap)	:	PreViewRender__hDIBitmap = 0
	EndIf
	' ........................................................................................
	hDC = GetDC(UserData->hwndMainGUI)
	PreViewRender__hDIBitmap = CreateDIBitmap(hDC,FreeImage_GetInfoHeader(hImageResized), CBM_INIT, FreeImage_GetBits(hImageResized), FreeImage_GetInfo(hImageResized), 0)
	ReleaseDC(UserData->hwndMainGUI, hDC)
	' ........................................................................................
	'DeleteObject(Cast(HGDIOBJ, SendMessage(UserData->hwndPicFrame, STM_SETIMAGE, Cast(WPARAM, IMAGE_BITMAP), Cast(LPARAM, PreViewRender__hDIBitmap))))
	PostMessage(UserData->hwndPicFrame, STM_SETIMAGE, Cast(WPARAM, IMAGE_BITMAP), Cast(LPARAM, PreViewRender__hDIBitmap))
	' ........................................................................................
	If hImageResized <> 0 Then
		FreeImage_Unload(hImageResized)
	EndIf
	'-----------------------------------------------------------------------------------------
	UserData->iRetVal = 0
	'-----------------------------------------------------------------------------------------
End Sub
Can someone help me with getting HBITMA from avif image or maybe there some examples for avif in freebasic?

(actually there a exist An add-on library to FreeImage, which adds loading support of HEIF and AVIF file format : https://github.com/mnaydenov/FreeImage-Sidecar but I do not know how to compile it)
UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

Hi Iczer,

do you have a link to download the needed lib files?
Iczer
Posts: 80
Joined: Jul 04, 2017 18:09

Re: Avif image - read/convert to dibitmap

Post by Iczer »

main project is here: https://github.com/AOMediaCodec/libavif
fresh static library builds in vs2019 is here: https://ci.appveyor.com/project/louquil ... /artifacts
UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

When I use "libavif_x64_Release.lib" I get plenty of errors:

Code: Select all

Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.text$mn+0x2d8): undefined reference to `__security_check_cookie'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.text$mn+0x51f): undefined reference to `__security_check_cookie'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.text$mn+0x7f): undefined reference to `__security_check_cookie'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.text$mn+0x505): undefined reference to `__security_check_cookie'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.text$mn+0x31f): undefined reference to `__security_check_cookie'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.text$mn+0x2b0): more undefined references to `__security_check_cookie' follow
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.xdata[$unwind$avifDecoderReset]+0x10): undefined reference to `__GSHandlerCheck'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.xdata[$unwind$avifPeekCompatibleFileType]+0x8): undefined reference to `__GSHandlerCheck'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.xdata[$unwind$avifCodecDecodeInputFillFromDecoderItem]+0x10): undefined reference to `__GSHandlerCheck'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.xdata[$unwind$avifDecoderItemValidateAV1]+0x10): undefined reference to `__GSHandlerCheck'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.xdata[$unwind$avifDecoderDataFillImageGrid]+0x18): undefined reference to `__GSHandlerCheck'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/read.obj):(.xdata[$unwind$avifParseItemPropertyContainerBox]+0x18): more undefined references to `__GSHandlerCheck' follow
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/diag.obj):(.text$mn+0x57): undefined reference to `__imp___stdio_common_vsprintf'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x17): undefined reference to `aom_codec_destroy'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x2a): undefined reference to `aom_codec_destroy'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x3c): undefined reference to `aom_codec_encode'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x65): undefined reference to `aom_codec_get_cx_data'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0xa0): undefined reference to `aom_codec_get_cx_data'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0xc3): undefined reference to `aom_codec_encode'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0xd4): undefined reference to `aom_codec_error_detail'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0xe4): undefined reference to `aom_codec_error'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0xd4): undefined reference to `aom_codec_version'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x171): undefined reference to `aom_codec_av1_cx'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x185): undefined reference to `aom_codec_enc_config_default'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x190): undefined reference to `aom_codec_err_to_string'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x434): undefined reference to `aom_codec_enc_init_ver'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x445): undefined reference to `aom_codec_error_detail'
freebasic64\bin\win64\ld.exe: ./libavif_x64_Release.a(avif.dir/Release/codec_aom.obj):(.text$mn+0x455): undefined reference to `aom_codec_error'
...
Seems that "libavif_x64_Release.lib" isn't compatible...
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: Avif image - read/convert to dibitmap

Post by srvaldez »

UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

Thanks srvaldez but it doesn't work.

Here the Freebasic code which I started. It compiles when running the code an em appears "The code execution cannot proceed because libavif.so was not found. Reinstalling the program may fix this problem."

Converted your DLL with
gendef.exe libavif.dll
dlltool.exe -k -d libavif.def -l libavif.dll.a

Code: Select all

'#Inclib "libavif"
#Inclib "avif"

#Define AVIF_API
Const AVIF_VERSION_MAJOR = 0
Const AVIF_VERSION_MINOR = 10
Const AVIF_VERSION_PATCH = 1
Const AVIF_VERSION_DEVEL = 1
Const AVIF_VERSION = (((AVIF_VERSION_MAJOR * 1000000) + (AVIF_VERSION_MINOR * 10000)) + (AVIF_VERSION_PATCH * 100)) + AVIF_VERSION_DEVEL
Type avifBool As Long
Const AVIF_TRUE = 1
Const AVIF_FALSE = 0
Const AVIF_DIAGNOSTICS_ERROR_BUFFER_SIZE = 256
Const AVIF_DEFAULT_IMAGE_SIZE_LIMIT = 16384 * 16384
Const AVIF_DEFAULT_IMAGE_COUNT_LIMIT = (12 * 3600) * 60
Const AVIF_QUANTIZER_LOSSLESS = 0
Const AVIF_QUANTIZER_BEST_QUALITY = 0
Const AVIF_QUANTIZER_WORST_QUALITY = 63
Const AVIF_PLANE_COUNT_YUV = 3
Const AVIF_SPEED_DEFAULT = -1
Const AVIF_SPEED_SLOWEST = 0
Const AVIF_SPEED_FASTEST = 10

Type avifPlanesFlag As Long
Enum
	AVIF_PLANES_YUV = 1 Shl 0
	AVIF_PLANES_A = 1 Shl 1
	AVIF_PLANES_ALL = &hff
End Enum

Type avifPlanesFlags As Ulong

Type avifChannelIndex As Long
Enum
	AVIF_CHAN_R = 0
	AVIF_CHAN_G = 1
	AVIF_CHAN_B = 2
	AVIF_CHAN_Y = 0
	AVIF_CHAN_U = 1
	AVIF_CHAN_V = 2
End Enum

Type avifResult As Long
Enum
	AVIF_RESULT_OK = 0
	AVIF_RESULT_UNKNOWN_ERROR
	AVIF_RESULT_INVALID_FTYP
	AVIF_RESULT_NO_CONTENT
	AVIF_RESULT_NO_YUV_FORMAT_SELECTED
	AVIF_RESULT_REFORMAT_FAILED
	AVIF_RESULT_UNSUPPORTED_DEPTH
	AVIF_RESULT_ENCODE_COLOR_FAILED
	AVIF_RESULT_ENCODE_ALPHA_FAILED
	AVIF_RESULT_BMFF_PARSE_FAILED
	AVIF_RESULT_NO_AV1_ITEMS_FOUND
	AVIF_RESULT_DECODE_COLOR_FAILED
	AVIF_RESULT_DECODE_ALPHA_FAILED
	AVIF_RESULT_COLOR_ALPHA_SIZE_MISMATCH
	AVIF_RESULT_ISPE_SIZE_MISMATCH
	AVIF_RESULT_NO_CODEC_AVAILABLE
	AVIF_RESULT_NO_IMAGES_REMAINING
	AVIF_RESULT_INVALID_EXIF_PAYLOAD
	AVIF_RESULT_INVALID_IMAGE_GRID
	AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION
	AVIF_RESULT_TRUNCATED_DATA
	AVIF_RESULT_IO_NOT_SET
	AVIF_RESULT_IO_ERROR
	AVIF_RESULT_WAITING_ON_IO
	AVIF_RESULT_INVALID_ARGUMENT
	AVIF_RESULT_NOT_IMPLEMENTED
	AVIF_RESULT_OUT_OF_MEMORY
End Enum

Type avifROData
	Data As Const Ubyte Ptr
	size As Uinteger
End Type

Type avifRWData
	Data As Ubyte Ptr
	size As Uinteger
End Type

#Define AVIF_DATA_EMPTY (NULL, 0)

Type avifPixelFormat As Long
Enum
	AVIF_PIXEL_FORMAT_NONE = 0
	AVIF_PIXEL_FORMAT_YUV444
	AVIF_PIXEL_FORMAT_YUV422
	AVIF_PIXEL_FORMAT_YUV420
	AVIF_PIXEL_FORMAT_YUV400
End Enum

Type avifPixelFormatInfo
	monochrome As avifBool
	chromaShiftX As Long
	chromaShiftY As Long
End Type

Type avifChromaSamplePosition As Long
Enum
	AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN = 0
	AVIF_CHROMA_SAMPLE_POSITION_VERTICAL = 1
	AVIF_CHROMA_SAMPLE_POSITION_COLOCATED = 2
End Enum

Type avifRange As Long
Enum
	AVIF_RANGE_LIMITED = 0
	AVIF_RANGE_FULL = 1
End Enum

Enum
	AVIF_COLOR_PRIMARIES_UNKNOWN = 0
	AVIF_COLOR_PRIMARIES_BT709 = 1
	AVIF_COLOR_PRIMARIES_IEC61966_2_4 = 1
	AVIF_COLOR_PRIMARIES_UNSPECIFIED = 2
	AVIF_COLOR_PRIMARIES_BT470M = 4
	AVIF_COLOR_PRIMARIES_BT470BG = 5
	AVIF_COLOR_PRIMARIES_BT601 = 6
	AVIF_COLOR_PRIMARIES_SMPTE240 = 7
	AVIF_COLOR_PRIMARIES_GENERIC_FILM = 8
	AVIF_COLOR_PRIMARIES_BT2020 = 9
	AVIF_COLOR_PRIMARIES_XYZ = 10
	AVIF_COLOR_PRIMARIES_SMPTE431 = 11
	AVIF_COLOR_PRIMARIES_SMPTE432 = 12
	AVIF_COLOR_PRIMARIES_EBU3213 = 22
End Enum

Type avifColorPrimaries As Ushort
	
Enum
	AVIF_TRANSFER_CHARACTERISTICS_UNKNOWN = 0
	AVIF_TRANSFER_CHARACTERISTICS_BT709 = 1
	AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED = 2
	AVIF_TRANSFER_CHARACTERISTICS_BT470M = 4
	AVIF_TRANSFER_CHARACTERISTICS_BT470BG = 5
	AVIF_TRANSFER_CHARACTERISTICS_BT601 = 6
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE240 = 7
	AVIF_TRANSFER_CHARACTERISTICS_LINEAR = 8
	AVIF_TRANSFER_CHARACTERISTICS_LOG100 = 9
	AVIF_TRANSFER_CHARACTERISTICS_LOG100_SQRT10 = 10
	AVIF_TRANSFER_CHARACTERISTICS_IEC61966 = 11
	AVIF_TRANSFER_CHARACTERISTICS_BT1361 = 12
	AVIF_TRANSFER_CHARACTERISTICS_SRGB = 13
	AVIF_TRANSFER_CHARACTERISTICS_BT2020_10BIT = 14
	AVIF_TRANSFER_CHARACTERISTICS_BT2020_12BIT = 15
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084 = 16
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE428 = 17
	AVIF_TRANSFER_CHARACTERISTICS_HLG = 18
End Enum

Type avifTransferCharacteristics As Ushort

Enum
	AVIF_MATRIX_COEFFICIENTS_IDENTITY = 0
	AVIF_MATRIX_COEFFICIENTS_BT709 = 1
	AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED = 2
	AVIF_MATRIX_COEFFICIENTS_FCC = 4
	AVIF_MATRIX_COEFFICIENTS_BT470BG = 5
	AVIF_MATRIX_COEFFICIENTS_BT601 = 6
	AVIF_MATRIX_COEFFICIENTS_SMPTE240 = 7
	AVIF_MATRIX_COEFFICIENTS_YCGCO = 8
	AVIF_MATRIX_COEFFICIENTS_BT2020_NCL = 9
	AVIF_MATRIX_COEFFICIENTS_BT2020_CL = 10
	AVIF_MATRIX_COEFFICIENTS_SMPTE2085 = 11
	AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL = 12
	AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_CL = 13
	AVIF_MATRIX_COEFFICIENTS_ICTCP = 14
End Enum

Type avifMatrixCoefficients As Ushort

Type avifDiagnostics
	Error As Zstring * 256
End Type


Type avifTransformFlag As Long
Enum
	AVIF_TRANSFORM_NONE = 0
	AVIF_TRANSFORM_PASP = 1 Shl 0
	AVIF_TRANSFORM_CLAP = 1 Shl 1
	AVIF_TRANSFORM_IROT = 1 Shl 2
	AVIF_TRANSFORM_IMIR = 1 Shl 3
End Enum

Type avifTransformFlags As Ulong

Type avifPixelAspectRatioBox
	hSpacing As Ulong
	vSpacing As Ulong
End Type

Type avifCleanApertureBox
	widthN As Ulong
	widthD As Ulong
	heightN As Ulong
	heightD As Ulong
	horizOffN As Ulong
	horizOffD As Ulong
	vertOffN As Ulong
	vertOffD As Ulong
End Type

Type avifImageRotation
	angle As Ubyte
End Type

Type avifImageMirror
	mode As Ubyte
End Type

Type avifCropRect
	x As Ulong
	y As Ulong
	Width As Ulong
	height As Ulong
End Type

Type avifImage
	Width As Ulong
	height As Ulong
	depth As Ulong
	yuvFormat As avifPixelFormat
	yuvRange As avifRange
	yuvChromaSamplePosition As avifChromaSamplePosition
	yuvPlanes(0 To 2) As Ubyte Ptr
	yuvRowBytes(0 To 2) As Ulong
	imageOwnsYUVPlanes As avifBool
	alphaPlane As Ubyte Ptr
	alphaRowBytes As Ulong
	imageOwnsAlphaPlane As avifBool
	alphaPremultiplied As avifBool
	icc As avifRWData
	colorPrimaries As avifColorPrimaries
	transferCharacteristics As avifTransferCharacteristics
	matrixCoefficients As avifMatrixCoefficients
	transformFlags As avifTransformFlags
	pasp As avifPixelAspectRatioBox
	clap As avifCleanApertureBox
	irot As avifImageRotation
	imir As avifImageMirror
	exif As avifRWData
	xmp As avifRWData
End Type

Type avifRGBFormat As Long
Enum
	AVIF_RGB_FORMAT_RGB = 0
	AVIF_RGB_FORMAT_RGBA
	AVIF_RGB_FORMAT_ARGB
	AVIF_RGB_FORMAT_BGR
	AVIF_RGB_FORMAT_BGRA
	AVIF_RGB_FORMAT_ABGR
End Enum

Type avifChromaUpsampling As Long
Enum
	AVIF_CHROMA_UPSAMPLING_AUTOMATIC = 0
	AVIF_CHROMA_UPSAMPLING_FASTEST = 1
	AVIF_CHROMA_UPSAMPLING_BEST_QUALITY = 2
	AVIF_CHROMA_UPSAMPLING_NEAREST = 3
	AVIF_CHROMA_UPSAMPLING_BILINEAR = 4
End Enum

Type avifRGBImage
	Width As Ulong
	height As Ulong
	depth As Ulong
	Format As avifRGBFormat
	chromaUpsampling As avifChromaUpsampling
	ignoreAlpha As avifBool
	alphaPremultiplied As avifBool
	isFloat As avifBool
	pixels As Ubyte Ptr
	rowBytes As Ulong
End Type


Type avifCodecChoice As Long
Enum
	AVIF_CODEC_CHOICE_AUTO = 0
	AVIF_CODEC_CHOICE_AOM
	AVIF_CODEC_CHOICE_DAV1D
	AVIF_CODEC_CHOICE_LIBGAV1
	AVIF_CODEC_CHOICE_RAV1E
	AVIF_CODEC_CHOICE_SVT
End Enum

Type avifCodecFlag As Long
Enum
	AVIF_CODEC_FLAG_CAN_DECODE = 1 Shl 0
	AVIF_CODEC_FLAG_CAN_ENCODE = 1 Shl 1
End Enum

Type avifCodecFlags As Ulong

Type avifCodecConfigurationBox
	seqProfile As Ubyte
	seqLevelIdx0 As Ubyte
	seqTier0 As Ubyte
	highBitdepth As Ubyte
	twelveBit As Ubyte
	monochrome As Ubyte
	chromaSubsamplingX As Ubyte
	chromaSubsamplingY As Ubyte
	chromaSamplePosition As Ubyte
End Type

Type avifIO As avifIO_
Type avifIODestroyFunc As Sub(Byval io As avifIO Ptr)
Type avifIOReadFunc As Function(Byval io As avifIO Ptr, Byval readFlags As Ulong, Byval offset As Ulongint, Byval size As Uinteger, Byval Out As avifROData Ptr) As avifResult
Type avifIOWriteFunc As Function(Byval io As avifIO Ptr, Byval writeFlags As Ulong, Byval offset As Ulongint, Byval Data As Const Ubyte Ptr, Byval size As Uinteger) As avifResult

Type avifIO_
	destroy As avifIODestroyFunc
	Read As avifIOReadFunc
	Write As avifIOWriteFunc
	sizeHint As Ulongint
	persistent As avifBool
	Data As Any Ptr
End Type

Type avifStrictFlag As Long
Enum
	AVIF_STRICT_DISABLED = 0
	AVIF_STRICT_PIXI_REQUIRED = 1 Shl 0
	AVIF_STRICT_CLAP_VALID = 1 Shl 1
	AVIF_STRICT_ALPHA_ISPE_REQUIRED = 1 Shl 2
	AVIF_STRICT_ENABLED = (AVIF_STRICT_PIXI_REQUIRED Or AVIF_STRICT_CLAP_VALID) Or AVIF_STRICT_ALPHA_ISPE_REQUIRED
End Enum

Type avifStrictFlags As Ulong
Type avifEncoderData As Ulong
Type avifDecoderData As Ulong
Type avifCodecSpecificOptions As Ulong

Type avifIOStats
	colorOBUSize As Uinteger
	alphaOBUSize As Uinteger
End Type

Type avifDecoderSource As Long
Enum
	AVIF_DECODER_SOURCE_AUTO = 0
	AVIF_DECODER_SOURCE_PRIMARY_ITEM
	AVIF_DECODER_SOURCE_TRACKS
End Enum

Type avifImageTiming
	timescale As Ulongint
	pts As Double
	ptsInTimescales As Ulongint
	duration As Double
	durationInTimescales As Ulongint
End Type

Type avifProgressiveState As Long
Enum
	AVIF_PROGRESSIVE_STATE_UNAVAILABLE = 0
	AVIF_PROGRESSIVE_STATE_AVAILABLE
	AVIF_PROGRESSIVE_STATE_ACTIVE
End Enum

Type avifDecoder
	codecChoice As avifCodecChoice
	maxThreads As Long
	requestedSource As avifDecoderSource
	allowProgressive As avifBool
	allowIncremental As avifBool
	ignoreExif As avifBool
	ignoreXMP As avifBool
	imageSizeLimit As Ulong
	imageCountLimit As Ulong
	strictFlags As avifStrictFlags
	image As avifImage Ptr
	imageIndex As Long
	imageCount As Long
	progressiveState As avifProgressiveState
	imageTiming As avifImageTiming
	timescale As Ulongint
	duration As Double
	durationInTimescales As Ulongint
	alphaPresent As avifBool
	ioStats As avifIOStats
	diag As avifDiagnostics
	io As avifIO Ptr
	Data As avifDecoderData Ptr
End Type

Type avifExtent
	offset As Ulongint
	size As Uinteger
End Type


Type avifEncoder
	codecChoice As avifCodecChoice
	maxThreads As Long
	minQuantizer As Long
	maxQuantizer As Long
	minQuantizerAlpha As Long
	maxQuantizerAlpha As Long
	tileRowsLog2 As Long
	tileColsLog2 As Long
	speed As Long
	keyframeInterval As Long
	timescale As Ulongint
	ioStats As avifIOStats
	diag As avifDiagnostics
	Data As avifEncoderData Ptr
	csOptions As avifCodecSpecificOptions Ptr
End Type


Type avifAddImageFlag As Long
Enum
	AVIF_ADD_IMAGE_FLAG_NONE = 0
	AVIF_ADD_IMAGE_FLAG_FORCE_KEYFRAME = 1 Shl 0
	AVIF_ADD_IMAGE_FLAG_SINGLE = 1 Shl 1
End Enum

Type avifAddImageFlags As Ulong
	

Declare Function avifDecoderCreate Alias "avifDecoderCreate" () As avifDecoder Ptr
Declare Function avifDecoderSetIOFile Alias "avifDecoderSetIOFile" (Byval decoder As avifDecoder Ptr, Byval filename As Const Zstring Ptr) As avifResult
Declare Function avifDecoderParse Alias "avifDecoderParse" (Byval decoder As avifDecoder Ptr) As avifResult

Dim As String inputFilename = "kodim23_yuv420_8bpc.avif"
Dim As avifDecoder Ptr decoder = avifDecoderCreate()
Dim As avifResult result = avifDecoderSetIOFile(decoder, inputFilename)

? avifDecoderParse(decoder)
'Dim As avifRGBImage _rgb
The quality seems to be better than Webp - cool.

Btw, how did you compile that stuff? I'm too silly for such things... :roll:
Last edited by UEZ on May 25, 2022 21:21, edited 1 time in total.
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: Avif image - read/convert to dibitmap

Post by srvaldez »

hi UEZ
hopefully the last link will work
Btw, how did you compile that stuff?
after extracting the archive I edited CMakeLists.txt line 31? from
option(AVIF_CODEC_AOM "Use the AOM codec for encoding/decoding (see AVIF_CODEC_AOM_DECODE/AVIF_CODEC_AOM_ENCODE)" OFF) to
option(AVIF_CODEC_AOM "Use the AOM codec for encoding/decoding (see AVIF_CODEC_AOM_DECODE/AVIF_CODEC_AOM_ENCODE)" ON)
make a build folder inside libavif-main and launch the msys2 shell and CD to the build folder, then
cmake ..
make
unfortunately I don't know how to change CMakeLists.txt so that it will recognize msys2 mingw so that it will build a .dll and not a .so
Last edited by srvaldez on May 25, 2022 21:26, edited 1 time in total.
UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

Looks good now - getting pointer address back - thx.

I will continue now...
:)
UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

Always getting an error: "Error = 9 / Failed to decode image -> BMFF parsing failed"

Code: Select all

#include "file.bi"
#Inclib "avif"

#Define AVIF_API
Const AVIF_VERSION_MAJOR = 0
Const AVIF_VERSION_MINOR = 10
Const AVIF_VERSION_PATCH = 1
Const AVIF_VERSION_DEVEL = 1
Const AVIF_VERSION = (((AVIF_VERSION_MAJOR * 1000000) + (AVIF_VERSION_MINOR * 10000)) + (AVIF_VERSION_PATCH * 100)) + AVIF_VERSION_DEVEL
Type avifBool As Long
Const AVIF_TRUE = 1
Const AVIF_FALSE = 0
Const AVIF_DIAGNOSTICS_ERROR_BUFFER_SIZE = 256
Const AVIF_DEFAULT_IMAGE_SIZE_LIMIT = 16384 * 16384
Const AVIF_DEFAULT_IMAGE_COUNT_LIMIT = (12 * 3600) * 60
Const AVIF_QUANTIZER_LOSSLESS = 0
Const AVIF_QUANTIZER_BEST_QUALITY = 0
Const AVIF_QUANTIZER_WORST_QUALITY = 63
Const AVIF_PLANE_COUNT_YUV = 3
Const AVIF_SPEED_DEFAULT = -1
Const AVIF_SPEED_SLOWEST = 0
Const AVIF_SPEED_FASTEST = 10

Type avifPlanesFlag As Long
Enum
	AVIF_PLANES_YUV = 1 Shl 0
	AVIF_PLANES_A = 1 Shl 1
	AVIF_PLANES_ALL = &hff
End Enum

Type avifPlanesFlags As Ulong

Type avifChannelIndex As Long
Enum
	AVIF_CHAN_R = 0
	AVIF_CHAN_G = 1
	AVIF_CHAN_B = 2
	AVIF_CHAN_Y = 0
	AVIF_CHAN_U = 1
	AVIF_CHAN_V = 2
End Enum

Type avifResult As Long
Enum
	AVIF_RESULT_OK = 0
	AVIF_RESULT_UNKNOWN_ERROR
	AVIF_RESULT_INVALID_FTYP
	AVIF_RESULT_NO_CONTENT
	AVIF_RESULT_NO_YUV_FORMAT_SELECTED
	AVIF_RESULT_REFORMAT_FAILED
	AVIF_RESULT_UNSUPPORTED_DEPTH
	AVIF_RESULT_ENCODE_COLOR_FAILED
	AVIF_RESULT_ENCODE_ALPHA_FAILED
	AVIF_RESULT_BMFF_PARSE_FAILED
	AVIF_RESULT_NO_AV1_ITEMS_FOUND
	AVIF_RESULT_DECODE_COLOR_FAILED
	AVIF_RESULT_DECODE_ALPHA_FAILED
	AVIF_RESULT_COLOR_ALPHA_SIZE_MISMATCH
	AVIF_RESULT_ISPE_SIZE_MISMATCH
	AVIF_RESULT_NO_CODEC_AVAILABLE
	AVIF_RESULT_NO_IMAGES_REMAINING
	AVIF_RESULT_INVALID_EXIF_PAYLOAD
	AVIF_RESULT_INVALID_IMAGE_GRID
	AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION
	AVIF_RESULT_TRUNCATED_DATA
	AVIF_RESULT_IO_NOT_SET
	AVIF_RESULT_IO_ERROR
	AVIF_RESULT_WAITING_ON_IO
	AVIF_RESULT_INVALID_ARGUMENT
	AVIF_RESULT_NOT_IMPLEMENTED
	AVIF_RESULT_OUT_OF_MEMORY
End Enum

Type avifROData
	Data As Const Ubyte Ptr
	size As Uinteger
End Type

Type avifRWData
	Data As Ubyte Ptr
	size As Uinteger
End Type

#Define AVIF_DATA_EMPTY (NULL, 0)

Type avifPixelFormat As Long
Enum
	AVIF_PIXEL_FORMAT_NONE = 0
	AVIF_PIXEL_FORMAT_YUV444
	AVIF_PIXEL_FORMAT_YUV422
	AVIF_PIXEL_FORMAT_YUV420
	AVIF_PIXEL_FORMAT_YUV400
End Enum

Type avifPixelFormatInfo
	monochrome As avifBool
	chromaShiftX As Long
	chromaShiftY As Long
End Type

Type avifChromaSamplePosition As Long
Enum
	AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN = 0
	AVIF_CHROMA_SAMPLE_POSITION_VERTICAL = 1
	AVIF_CHROMA_SAMPLE_POSITION_COLOCATED = 2
End Enum

Type avifRange As Long
Enum
	AVIF_RANGE_LIMITED = 0
	AVIF_RANGE_FULL = 1
End Enum

Enum
	AVIF_COLOR_PRIMARIES_UNKNOWN = 0
	AVIF_COLOR_PRIMARIES_BT709 = 1
	AVIF_COLOR_PRIMARIES_IEC61966_2_4 = 1
	AVIF_COLOR_PRIMARIES_UNSPECIFIED = 2
	AVIF_COLOR_PRIMARIES_BT470M = 4
	AVIF_COLOR_PRIMARIES_BT470BG = 5
	AVIF_COLOR_PRIMARIES_BT601 = 6
	AVIF_COLOR_PRIMARIES_SMPTE240 = 7
	AVIF_COLOR_PRIMARIES_GENERIC_FILM = 8
	AVIF_COLOR_PRIMARIES_BT2020 = 9
	AVIF_COLOR_PRIMARIES_XYZ = 10
	AVIF_COLOR_PRIMARIES_SMPTE431 = 11
	AVIF_COLOR_PRIMARIES_SMPTE432 = 12
	AVIF_COLOR_PRIMARIES_EBU3213 = 22
End Enum

Type avifColorPrimaries As Ushort
	
Enum
	AVIF_TRANSFER_CHARACTERISTICS_UNKNOWN = 0
	AVIF_TRANSFER_CHARACTERISTICS_BT709 = 1
	AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED = 2
	AVIF_TRANSFER_CHARACTERISTICS_BT470M = 4
	AVIF_TRANSFER_CHARACTERISTICS_BT470BG = 5
	AVIF_TRANSFER_CHARACTERISTICS_BT601 = 6
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE240 = 7
	AVIF_TRANSFER_CHARACTERISTICS_LINEAR = 8
	AVIF_TRANSFER_CHARACTERISTICS_LOG100 = 9
	AVIF_TRANSFER_CHARACTERISTICS_LOG100_SQRT10 = 10
	AVIF_TRANSFER_CHARACTERISTICS_IEC61966 = 11
	AVIF_TRANSFER_CHARACTERISTICS_BT1361 = 12
	AVIF_TRANSFER_CHARACTERISTICS_SRGB = 13
	AVIF_TRANSFER_CHARACTERISTICS_BT2020_10BIT = 14
	AVIF_TRANSFER_CHARACTERISTICS_BT2020_12BIT = 15
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084 = 16
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE428 = 17
	AVIF_TRANSFER_CHARACTERISTICS_HLG = 18
End Enum

Type avifTransferCharacteristics As Ushort

Enum
	AVIF_MATRIX_COEFFICIENTS_IDENTITY = 0
	AVIF_MATRIX_COEFFICIENTS_BT709 = 1
	AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED = 2
	AVIF_MATRIX_COEFFICIENTS_FCC = 4
	AVIF_MATRIX_COEFFICIENTS_BT470BG = 5
	AVIF_MATRIX_COEFFICIENTS_BT601 = 6
	AVIF_MATRIX_COEFFICIENTS_SMPTE240 = 7
	AVIF_MATRIX_COEFFICIENTS_YCGCO = 8
	AVIF_MATRIX_COEFFICIENTS_BT2020_NCL = 9
	AVIF_MATRIX_COEFFICIENTS_BT2020_CL = 10
	AVIF_MATRIX_COEFFICIENTS_SMPTE2085 = 11
	AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL = 12
	AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_CL = 13
	AVIF_MATRIX_COEFFICIENTS_ICTCP = 14
End Enum

Type avifMatrixCoefficients As Ushort

Type avifDiagnostics
	Error As Zstring * 256
End Type


Type avifTransformFlag As Long
Enum
	AVIF_TRANSFORM_NONE = 0
	AVIF_TRANSFORM_PASP = 1 Shl 0
	AVIF_TRANSFORM_CLAP = 1 Shl 1
	AVIF_TRANSFORM_IROT = 1 Shl 2
	AVIF_TRANSFORM_IMIR = 1 Shl 3
End Enum

Type avifTransformFlags As Ulong

Type avifPixelAspectRatioBox
	hSpacing As Ulong
	vSpacing As Ulong
End Type

Type avifCleanApertureBox
	widthN As Ulong
	widthD As Ulong
	heightN As Ulong
	heightD As Ulong
	horizOffN As Ulong
	horizOffD As Ulong
	vertOffN As Ulong
	vertOffD As Ulong
End Type

Type avifImageRotation
	angle As Ubyte
End Type

Type avifImageMirror
	mode As Ubyte
End Type

Type avifCropRect
	x As Ulong
	y As Ulong
	Width As Ulong
	height As Ulong
End Type

Type avifImage
	Width As Ulong
	height As Ulong
	depth As Ulong
	yuvFormat As avifPixelFormat
	yuvRange As avifRange
	yuvChromaSamplePosition As avifChromaSamplePosition
	yuvPlanes(0 To 2) As Ubyte Ptr
	yuvRowBytes(0 To 2) As Ulong
	imageOwnsYUVPlanes As avifBool
	alphaPlane As Ubyte Ptr
	alphaRowBytes As Ulong
	imageOwnsAlphaPlane As avifBool
	alphaPremultiplied As avifBool
	icc As avifRWData
	colorPrimaries As avifColorPrimaries
	transferCharacteristics As avifTransferCharacteristics
	matrixCoefficients As avifMatrixCoefficients
	transformFlags As avifTransformFlags
	pasp As avifPixelAspectRatioBox
	clap As avifCleanApertureBox
	irot As avifImageRotation
	imir As avifImageMirror
	exif As avifRWData
	xmp As avifRWData
End Type

Type avifRGBFormat As Long
Enum
	AVIF_RGB_FORMAT_RGB = 0
	AVIF_RGB_FORMAT_RGBA
	AVIF_RGB_FORMAT_ARGB
	AVIF_RGB_FORMAT_BGR
	AVIF_RGB_FORMAT_BGRA
	AVIF_RGB_FORMAT_ABGR
End Enum

Type avifChromaUpsampling As Long
Enum
	AVIF_CHROMA_UPSAMPLING_AUTOMATIC = 0
	AVIF_CHROMA_UPSAMPLING_FASTEST = 1
	AVIF_CHROMA_UPSAMPLING_BEST_QUALITY = 2
	AVIF_CHROMA_UPSAMPLING_NEAREST = 3
	AVIF_CHROMA_UPSAMPLING_BILINEAR = 4
End Enum

Type avifRGBImage
	Width As Ulong
	height As Ulong
	depth As Ulong
	Format As avifRGBFormat
	chromaUpsampling As avifChromaUpsampling
	ignoreAlpha As avifBool
	alphaPremultiplied As avifBool
	isFloat As avifBool
	pixels As Ubyte Ptr
	rowBytes As Ulong
End Type


Type avifCodecChoice As Long
Enum
	AVIF_CODEC_CHOICE_AUTO = 0
	AVIF_CODEC_CHOICE_AOM
	AVIF_CODEC_CHOICE_DAV1D
	AVIF_CODEC_CHOICE_LIBGAV1
	AVIF_CODEC_CHOICE_RAV1E
	AVIF_CODEC_CHOICE_SVT
End Enum

Type avifCodecFlag As Long
Enum
	AVIF_CODEC_FLAG_CAN_DECODE = 1 Shl 0
	AVIF_CODEC_FLAG_CAN_ENCODE = 1 Shl 1
End Enum

Type avifCodecFlags As Ulong

Type avifCodecConfigurationBox
	seqProfile As Ubyte
	seqLevelIdx0 As Ubyte
	seqTier0 As Ubyte
	highBitdepth As Ubyte
	twelveBit As Ubyte
	monochrome As Ubyte
	chromaSubsamplingX As Ubyte
	chromaSubsamplingY As Ubyte
	chromaSamplePosition As Ubyte
End Type

Type avifIO As avifIO_
Type avifIODestroyFunc As Sub(Byval io As avifIO Ptr)
Type avifIOReadFunc As Function(Byval io As avifIO Ptr, Byval readFlags As Ulong, Byval offset As Ulongint, Byval size As Uinteger, Byval Out As avifROData Ptr) As avifResult
Type avifIOWriteFunc As Function(Byval io As avifIO Ptr, Byval writeFlags As Ulong, Byval offset As Ulongint, Byval Data As Const Ubyte Ptr, Byval size As Uinteger) As avifResult

Type avifIO_
	destroy As avifIODestroyFunc
	Read As avifIOReadFunc
	Write As avifIOWriteFunc
	sizeHint As Ulongint
	persistent As avifBool
	Data As Any Ptr
End Type

Type avifStrictFlag As Long
Enum
	AVIF_STRICT_DISABLED = 0
	AVIF_STRICT_PIXI_REQUIRED = 1 Shl 0
	AVIF_STRICT_CLAP_VALID = 1 Shl 1
	AVIF_STRICT_ALPHA_ISPE_REQUIRED = 1 Shl 2
	AVIF_STRICT_ENABLED = (AVIF_STRICT_PIXI_REQUIRED Or AVIF_STRICT_CLAP_VALID) Or AVIF_STRICT_ALPHA_ISPE_REQUIRED
End Enum

Type avifStrictFlags As Ulong
Type avifEncoderData As Ulong
Type avifDecoderData As Ulong
Type avifCodecSpecificOptions As Ulong

Type avifIOStats
	colorOBUSize As Uinteger
	alphaOBUSize As Uinteger
End Type

Type avifDecoderSource As Long
Enum
	AVIF_DECODER_SOURCE_AUTO = 0
	AVIF_DECODER_SOURCE_PRIMARY_ITEM
	AVIF_DECODER_SOURCE_TRACKS
End Enum

Type avifImageTiming
	timescale As Ulongint
	pts As Double
	ptsInTimescales As Ulongint
	duration As Double
	durationInTimescales As Ulongint
End Type

Type avifProgressiveState As Long
Enum
	AVIF_PROGRESSIVE_STATE_UNAVAILABLE = 0
	AVIF_PROGRESSIVE_STATE_AVAILABLE
	AVIF_PROGRESSIVE_STATE_ACTIVE
End Enum

Type avifDecoder
	codecChoice As avifCodecChoice
	maxThreads As Long
	requestedSource As avifDecoderSource
	allowProgressive As avifBool
	allowIncremental As avifBool
	ignoreExif As avifBool
	ignoreXMP As avifBool
	imageSizeLimit As Ulong
	imageCountLimit As Ulong
	strictFlags As avifStrictFlags
	image As avifImage Ptr
	imageIndex As Long
	imageCount As Long
	progressiveState As avifProgressiveState
	imageTiming As avifImageTiming
	timescale As Ulongint
	duration As Double
	durationInTimescales As Ulongint
	alphaPresent As avifBool
	ioStats As avifIOStats
	diag As avifDiagnostics
	io As avifIO Ptr
	Data As avifDecoderData Ptr
End Type

Type avifExtent
	offset As Ulongint
	size As Uinteger
End Type


Type avifEncoder
	codecChoice As avifCodecChoice
	maxThreads As Long
	minQuantizer As Long
	maxQuantizer As Long
	minQuantizerAlpha As Long
	maxQuantizerAlpha As Long
	tileRowsLog2 As Long
	tileColsLog2 As Long
	speed As Long
	keyframeInterval As Long
	timescale As Ulongint
	ioStats As avifIOStats
	diag As avifDiagnostics
	Data As avifEncoderData Ptr
	csOptions As avifCodecSpecificOptions Ptr
End Type


Type avifAddImageFlag As Long
Enum
	AVIF_ADD_IMAGE_FLAG_NONE = 0
	AVIF_ADD_IMAGE_FLAG_FORCE_KEYFRAME = 1 Shl 0
	AVIF_ADD_IMAGE_FLAG_SINGLE = 1 Shl 1
End Enum

Type avifAddImageFlags As Ulong
	

Declare Function avifResultToString Alias "avifResultToString" (Byval result As avifResult) As Const Zstring Ptr
Declare Function avifDecoderCreate Alias "avifDecoderCreate" () As avifDecoder Ptr
Declare Function avifDecoderSetIOFile Alias "avifDecoderSetIOFile" (Byval decoder As avifDecoder Ptr, Byval filename As Const Zstring Ptr) As avifResult
Declare Function avifDecoderSetIOMemory Alias "avifDecoderSetIOMemory" (Byval decoder As avifDecoder Ptr, Byval Data As Const Ubyte Ptr, Byval size As Uinteger) As avifResult
Declare Function avifDecoderParse Alias "avifDecoderParse" (Byval decoder As avifDecoder Ptr) As avifResult
Declare Function avifDecoderNextImage Alias "avifDecoderNextImage" (byval decoder as avifDecoder Ptr) As avifResult
Declare Sub avifRGBImageSetDefaults Alias "avifRGBImageSetDefaults" (Byval Rgb As avifRGBImage Ptr, Byval image As Const avifImage Ptr)
Declare Sub avifRGBImageAllocatePixels Alias "avifRGBImageAllocatePixels" (Byval Rgb As avifRGBImage Ptr)
Declare Function avifImageYUVToRGB Alias "avifImageYUVToRGB" (Byval image As Const avifImage Ptr, Byval Rgb As avifRGBImage Ptr) As avifResult
Declare Sub avifRGBImageFreePixels Alias "avifRGBImageFreePixels" (Byval Rgb As avifRGBImage Ptr)
Declare Sub avifDecoderDestroy Alias "avifDecoderDestroy" (Byval decoder As avifDecoder Ptr)

Dim As String inputFilename = "kodim23_yuv420_8bpc.avif"
Dim As avifRGBImage _rgb
Dim As avifDecoder Ptr decoder = avifDecoderCreate()
? "decoder ptr = " & decoder

Dim As avifResult result = avifDecoderSetIOFile(decoder, inputFilename)

'Dim As Integer hFile, iFileSize = Filelen(inputFilename)
'Dim pMem As Ubyte Ptr
'pMem = Allocate(iFileSize)
'hFile = Freefile()
'Open inputFilename For Binary Access Read As #hFile
'Get #hFile, 0, pMem[0], iFileSize
'Close #hFile
'Dim As avifResult result = avifDecoderSetIOMemory(decoder, pMem, iFileSize)

If result = AVIF_RESULT_OK Then
	result = avifDecoderParse(decoder)
	If result = AVIF_RESULT_OK Then
		? decoder->image->width, decoder->image->height, decoder->image->depth
		While avifDecoderNextImage(decoder) = AVIF_RESULT_OK
			avifRGBImageSetDefaults(@_rgb, decoder->image)
			avifRGBImageAllocatePixels(@_rgb)
			avifImageYUVToRGB(decoder->image, @_rgb)
			? _rgb.depth
		Wend
	Else
		? "Error = " & result & " / Failed to decode image -> " & *avifResultToString(result)
	Endif
Else
	? "Error = " & result & " / Cannot open file for read -> " & *avifResultToString(result)
End If

avifRGBImageFreePixels(@_rgb)
avifDecoderDestroy(decoder)
'Deallocate(pMem)
? "Done"
Sleep
I don't know what the problem is... :?:
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: Avif image - read/convert to dibitmap

Post by srvaldez »

a google search shows that you are not the only one having this problem, unfortunately I haven't found a good source of avif pictures to test, except for http://download.opencontent.netflix.com ... s-100.avif
UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

This works with kodim03_yuv420_8bpc.avif (Windows only):

Code: Select all

'Coded by UEZ build 2022-05-26 beta
#include "file.bi"
#Include "fbgfx.bi"
#Ifdef __Fb_64bit__
	#Inclib "avif"
	#Inclib "gdiplus"
    #Include Once "win/gdiplus-c.bi"
#Else
    #Include Once "win/gdiplus.bi"
    Using gdiplus
#Endif
Using FB

Dim Shared gdipToken As ULONG_PTR
Dim Shared GDIp As GdiplusStartupInput 

Private Function _GDIPlus_Startup() As Bool
	GDIp.GdiplusVersion = 1
	If GdiplusStartup(@gdipToken, @GDIp, NULL) <> 0 Then
		Error 1
		Return False
	Endif
	Return True
End Function

Private Sub _GDIPlus_Shutdown()
	GdiplusShutdown(gdipToken)
End Sub


Private Function AVIF_BitmapCreateGDIp_internal(pRgba As Ubyte Ptr, w As Long, h As Long, bGDI As Bool = False) As Any Ptr
	Dim hBitmap As Any Ptr
	Dim As Rect tRect = Type(0, 0, w - 1, h - 1)
	Dim As BitmapData tBitmapData
	Dim As Integer i = 0, ub = (w * h) ' Shl 2
	GdipCreateBitmapFromScan0(w, h, 0, PixelFormat32bppARGB, 0, @hBitmap)
	GdipBitmapLockBits(hBitmap, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData)
	'Memcpy(tBitmapData.Scan0, pRgba, ub)
	Dim As Ulong c
	While i < w * h
		c = Cast(ULong Ptr, pRgba)[i]
		Cast(ULong Ptr, tBitmapData.Scan0)[i] = (c And &h000000FF) Shl 16 Or (c And &h00FF0000) Shr 16 Or c And &h00FF00 Or c And &hFF000000
		i += 1
	Wend
	GdipBitmapUnlockBits(hBitmap, @tBitmapData)
	If bGDI Then
		Dim As HBITMAP hGDIBitmap
		GdipCreateHBITMAPFromBitmap(hBitmap, @hGDIBitmap, &hFF000000)
		GdipDisposeImage(hBitmap)
		Return hGDIBitmap
	Endif
	Return hBitmap
End Function

Sub Display_GDIpImage(hImage As Any Ptr, w As long, h As Long)
	ScreenControl SET_DRIVER_NAME, "GDI"
	ScreenRes w, h, 32, 1, GFX_HIGH_PRIORITY Or GFX_NO_SWITCH
	Dim as HWND hHWND
	ScreenControl(GET_WINDOW_HANDLE, Cast(Integer, hHWND))
	Dim As Any Ptr hCanvas, hDC = GetDC(hHWND), hDC_backbuffer = CreateCompatibleDC(hDC), hHBitmap = CreateCompatibleBitmap(hDC, w, h)
	Var hObjOld = SelectObject(hDC_backbuffer, hHBitmap)
	GdipCreateFromHDC(hDC_backbuffer, @hCanvas)
	GdipDrawImageRect(hCanvas, hImage, 0, 0, w, h)
	Do
		BitBlt(hDC, 0, 0, w, h, hDC_backbuffer, 0, 0, SRCCOPY)
		Sleep(10)
	Loop Until Len(Inkey())
	GdipDeleteGraphics(hCanvas)
	SelectObject(hDC_backbuffer, hObjOld)
	DeleteDC(hDC_backbuffer)
	ReleaseDC(hHWND, hDC)
	DeleteObject(hHBitmap)
End Sub

#Define AVIF_API
Const AVIF_VERSION_MAJOR = 0
Const AVIF_VERSION_MINOR = 10
Const AVIF_VERSION_PATCH = 1
Const AVIF_VERSION_DEVEL = 1
Const AVIF_VERSION = (((AVIF_VERSION_MAJOR * 1000000) + (AVIF_VERSION_MINOR * 10000)) + (AVIF_VERSION_PATCH * 100)) + AVIF_VERSION_DEVEL
Type avifBool As Long
Const AVIF_TRUE = 1
Const AVIF_FALSE = 0
Const AVIF_DIAGNOSTICS_ERROR_BUFFER_SIZE = 256
Const AVIF_DEFAULT_IMAGE_SIZE_LIMIT = 16384 * 16384
Const AVIF_DEFAULT_IMAGE_COUNT_LIMIT = (12 * 3600) * 60
Const AVIF_QUANTIZER_LOSSLESS = 0
Const AVIF_QUANTIZER_BEST_QUALITY = 0
Const AVIF_QUANTIZER_WORST_QUALITY = 63
Const AVIF_PLANE_COUNT_YUV = 3
Const AVIF_SPEED_DEFAULT = -1
Const AVIF_SPEED_SLOWEST = 0
Const AVIF_SPEED_FASTEST = 10

Type avifPlanesFlag As Long
Enum
	AVIF_PLANES_YUV = 1 Shl 0
	AVIF_PLANES_A = 1 Shl 1
	AVIF_PLANES_ALL = &hff
End Enum

Type avifPlanesFlags As Ulong

Type avifChannelIndex As Long
Enum
	AVIF_CHAN_R = 0
	AVIF_CHAN_G = 1
	AVIF_CHAN_B = 2
	AVIF_CHAN_Y = 0
	AVIF_CHAN_U = 1
	AVIF_CHAN_V = 2
End Enum

Type avifResult As Long
Enum
	AVIF_RESULT_OK = 0
	AVIF_RESULT_UNKNOWN_ERROR
	AVIF_RESULT_INVALID_FTYP
	AVIF_RESULT_NO_CONTENT
	AVIF_RESULT_NO_YUV_FORMAT_SELECTED
	AVIF_RESULT_REFORMAT_FAILED
	AVIF_RESULT_UNSUPPORTED_DEPTH
	AVIF_RESULT_ENCODE_COLOR_FAILED
	AVIF_RESULT_ENCODE_ALPHA_FAILED
	AVIF_RESULT_BMFF_PARSE_FAILED
	AVIF_RESULT_NO_AV1_ITEMS_FOUND
	AVIF_RESULT_DECODE_COLOR_FAILED
	AVIF_RESULT_DECODE_ALPHA_FAILED
	AVIF_RESULT_COLOR_ALPHA_SIZE_MISMATCH
	AVIF_RESULT_ISPE_SIZE_MISMATCH
	AVIF_RESULT_NO_CODEC_AVAILABLE
	AVIF_RESULT_NO_IMAGES_REMAINING
	AVIF_RESULT_INVALID_EXIF_PAYLOAD
	AVIF_RESULT_INVALID_IMAGE_GRID
	AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION
	AVIF_RESULT_TRUNCATED_DATA
	AVIF_RESULT_IO_NOT_SET
	AVIF_RESULT_IO_ERROR
	AVIF_RESULT_WAITING_ON_IO
	AVIF_RESULT_INVALID_ARGUMENT
	AVIF_RESULT_NOT_IMPLEMENTED
	AVIF_RESULT_OUT_OF_MEMORY
End Enum

Type avifROData
	Data As Const Ubyte Ptr
	size As Uinteger
End Type

Type avifRWData
	Data As Ubyte Ptr
	size As Uinteger
End Type

#Define AVIF_DATA_EMPTY (NULL, 0)

Type avifPixelFormat As Long
Enum
	AVIF_PIXEL_FORMAT_NONE = 0
	AVIF_PIXEL_FORMAT_YUV444
	AVIF_PIXEL_FORMAT_YUV422
	AVIF_PIXEL_FORMAT_YUV420
	AVIF_PIXEL_FORMAT_YUV400
End Enum

Type avifPixelFormatInfo
	monochrome As avifBool
	chromaShiftX As Long
	chromaShiftY As Long
End Type

Type avifChromaSamplePosition As Long
Enum
	AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN = 0
	AVIF_CHROMA_SAMPLE_POSITION_VERTICAL = 1
	AVIF_CHROMA_SAMPLE_POSITION_COLOCATED = 2
End Enum

Type avifRange As Long
Enum
	AVIF_RANGE_LIMITED = 0
	AVIF_RANGE_FULL = 1
End Enum

Enum
	AVIF_COLOR_PRIMARIES_UNKNOWN = 0
	AVIF_COLOR_PRIMARIES_BT709 = 1
	AVIF_COLOR_PRIMARIES_IEC61966_2_4 = 1
	AVIF_COLOR_PRIMARIES_UNSPECIFIED = 2
	AVIF_COLOR_PRIMARIES_BT470M = 4
	AVIF_COLOR_PRIMARIES_BT470BG = 5
	AVIF_COLOR_PRIMARIES_BT601 = 6
	AVIF_COLOR_PRIMARIES_SMPTE240 = 7
	AVIF_COLOR_PRIMARIES_GENERIC_FILM = 8
	AVIF_COLOR_PRIMARIES_BT2020 = 9
	AVIF_COLOR_PRIMARIES_XYZ = 10
	AVIF_COLOR_PRIMARIES_SMPTE431 = 11
	AVIF_COLOR_PRIMARIES_SMPTE432 = 12
	AVIF_COLOR_PRIMARIES_EBU3213 = 22
End Enum

Type avifColorPrimaries As Ushort
	
Enum
	AVIF_TRANSFER_CHARACTERISTICS_UNKNOWN = 0
	AVIF_TRANSFER_CHARACTERISTICS_BT709 = 1
	AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED = 2
	AVIF_TRANSFER_CHARACTERISTICS_BT470M = 4
	AVIF_TRANSFER_CHARACTERISTICS_BT470BG = 5
	AVIF_TRANSFER_CHARACTERISTICS_BT601 = 6
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE240 = 7
	AVIF_TRANSFER_CHARACTERISTICS_LINEAR = 8
	AVIF_TRANSFER_CHARACTERISTICS_LOG100 = 9
	AVIF_TRANSFER_CHARACTERISTICS_LOG100_SQRT10 = 10
	AVIF_TRANSFER_CHARACTERISTICS_IEC61966 = 11
	AVIF_TRANSFER_CHARACTERISTICS_BT1361 = 12
	AVIF_TRANSFER_CHARACTERISTICS_SRGB = 13
	AVIF_TRANSFER_CHARACTERISTICS_BT2020_10BIT = 14
	AVIF_TRANSFER_CHARACTERISTICS_BT2020_12BIT = 15
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084 = 16
	AVIF_TRANSFER_CHARACTERISTICS_SMPTE428 = 17
	AVIF_TRANSFER_CHARACTERISTICS_HLG = 18
End Enum

Type avifTransferCharacteristics As Ushort

Enum
	AVIF_MATRIX_COEFFICIENTS_IDENTITY = 0
	AVIF_MATRIX_COEFFICIENTS_BT709 = 1
	AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED = 2
	AVIF_MATRIX_COEFFICIENTS_FCC = 4
	AVIF_MATRIX_COEFFICIENTS_BT470BG = 5
	AVIF_MATRIX_COEFFICIENTS_BT601 = 6
	AVIF_MATRIX_COEFFICIENTS_SMPTE240 = 7
	AVIF_MATRIX_COEFFICIENTS_YCGCO = 8
	AVIF_MATRIX_COEFFICIENTS_BT2020_NCL = 9
	AVIF_MATRIX_COEFFICIENTS_BT2020_CL = 10
	AVIF_MATRIX_COEFFICIENTS_SMPTE2085 = 11
	AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL = 12
	AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_CL = 13
	AVIF_MATRIX_COEFFICIENTS_ICTCP = 14
End Enum

Type avifMatrixCoefficients As Ushort

Type avifDiagnostics
	Error As Zstring * 256
End Type


Type avifTransformFlag As Long
Enum
	AVIF_TRANSFORM_NONE = 0
	AVIF_TRANSFORM_PASP = 1 Shl 0
	AVIF_TRANSFORM_CLAP = 1 Shl 1
	AVIF_TRANSFORM_IROT = 1 Shl 2
	AVIF_TRANSFORM_IMIR = 1 Shl 3
End Enum

Type avifTransformFlags As Ulong

Type avifPixelAspectRatioBox
	hSpacing As Ulong
	vSpacing As Ulong
End Type

Type avifCleanApertureBox
	widthN As Ulong
	widthD As Ulong
	heightN As Ulong
	heightD As Ulong
	horizOffN As Ulong
	horizOffD As Ulong
	vertOffN As Ulong
	vertOffD As Ulong
End Type

Type avifImageRotation
	angle As Ubyte
End Type

Type avifImageMirror
	mode As Ubyte
End Type

Type avifCropRect
	x As Ulong
	y As Ulong
	Width As Ulong
	height As Ulong
End Type

Type avifImage
	Width As Ulong
	height As Ulong
	depth As Ulong
	yuvFormat As avifPixelFormat
	yuvRange As avifRange
	yuvChromaSamplePosition As avifChromaSamplePosition
	yuvPlanes(0 To 2) As Ubyte Ptr
	yuvRowBytes(0 To 2) As Ulong
	imageOwnsYUVPlanes As avifBool
	alphaPlane As Ubyte Ptr
	alphaRowBytes As Ulong
	imageOwnsAlphaPlane As avifBool
	alphaPremultiplied As avifBool
	icc As avifRWData
	colorPrimaries As avifColorPrimaries
	transferCharacteristics As avifTransferCharacteristics
	matrixCoefficients As avifMatrixCoefficients
	transformFlags As avifTransformFlags
	pasp As avifPixelAspectRatioBox
	clap As avifCleanApertureBox
	irot As avifImageRotation
	imir As avifImageMirror
	exif As avifRWData
	xmp As avifRWData
End Type

Type avifRGBFormat As Long
Enum
	AVIF_RGB_FORMAT_RGB = 0
	AVIF_RGB_FORMAT_RGBA
	AVIF_RGB_FORMAT_ARGB
	AVIF_RGB_FORMAT_BGR
	AVIF_RGB_FORMAT_BGRA
	AVIF_RGB_FORMAT_ABGR
End Enum

Type avifChromaUpsampling As Long
Enum
	AVIF_CHROMA_UPSAMPLING_AUTOMATIC = 0
	AVIF_CHROMA_UPSAMPLING_FASTEST = 1
	AVIF_CHROMA_UPSAMPLING_BEST_QUALITY = 2
	AVIF_CHROMA_UPSAMPLING_NEAREST = 3
	AVIF_CHROMA_UPSAMPLING_BILINEAR = 4
End Enum

Type avifRGBImage
	Width As Ulong
	height As Ulong
	depth As Ulong
	Format As avifRGBFormat
	chromaUpsampling As avifChromaUpsampling
	ignoreAlpha As avifBool
	alphaPremultiplied As avifBool
	isFloat As avifBool
	pixels As Ubyte Ptr
	rowBytes As Ulong
End Type


Type avifCodecChoice As Long
Enum
	AVIF_CODEC_CHOICE_AUTO = 0
	AVIF_CODEC_CHOICE_AOM
	AVIF_CODEC_CHOICE_DAV1D
	AVIF_CODEC_CHOICE_LIBGAV1
	AVIF_CODEC_CHOICE_RAV1E
	AVIF_CODEC_CHOICE_SVT
End Enum

Type avifCodecFlag As Long
Enum
	AVIF_CODEC_FLAG_CAN_DECODE = 1 Shl 0
	AVIF_CODEC_FLAG_CAN_ENCODE = 1 Shl 1
End Enum

Type avifCodecFlags As Ulong

Type avifCodecConfigurationBox
	seqProfile As Ubyte
	seqLevelIdx0 As Ubyte
	seqTier0 As Ubyte
	highBitdepth As Ubyte
	twelveBit As Ubyte
	monochrome As Ubyte
	chromaSubsamplingX As Ubyte
	chromaSubsamplingY As Ubyte
	chromaSamplePosition As Ubyte
End Type

Type avifIO As avifIO_
Type avifIODestroyFunc As Sub(Byval io As avifIO Ptr)
Type avifIOReadFunc As Function(Byval io As avifIO Ptr, Byval readFlags As Ulong, Byval offset As Ulongint, Byval size As Uinteger, Byval Out As avifROData Ptr) As avifResult
Type avifIOWriteFunc As Function(Byval io As avifIO Ptr, Byval writeFlags As Ulong, Byval offset As Ulongint, Byval Data As Const Ubyte Ptr, Byval size As Uinteger) As avifResult

Type avifIO_
	destroy As avifIODestroyFunc
	Read As avifIOReadFunc
	Write As avifIOWriteFunc
	sizeHint As Ulongint
	persistent As avifBool
	Data As Any Ptr
End Type

Type avifStrictFlag As Long
Enum
	AVIF_STRICT_DISABLED = 0
	AVIF_STRICT_PIXI_REQUIRED = 1 Shl 0
	AVIF_STRICT_CLAP_VALID = 1 Shl 1
	AVIF_STRICT_ALPHA_ISPE_REQUIRED = 1 Shl 2
	AVIF_STRICT_ENABLED = (AVIF_STRICT_PIXI_REQUIRED Or AVIF_STRICT_CLAP_VALID) Or AVIF_STRICT_ALPHA_ISPE_REQUIRED
End Enum

Type avifStrictFlags As Ulong
Type avifEncoderData As Ulong
Type avifDecoderData As Ulong
Type avifCodecSpecificOptions As Ulong

Type avifIOStats
	colorOBUSize As Uinteger
	alphaOBUSize As Uinteger
End Type

Type avifDecoderSource As Long
Enum
	AVIF_DECODER_SOURCE_AUTO = 0
	AVIF_DECODER_SOURCE_PRIMARY_ITEM
	AVIF_DECODER_SOURCE_TRACKS
End Enum

Type avifImageTiming
	timescale As Ulongint
	pts As Double
	ptsInTimescales As Ulongint
	duration As Double
	durationInTimescales As Ulongint
End Type

Type avifProgressiveState As Long
Enum
	AVIF_PROGRESSIVE_STATE_UNAVAILABLE = 0
	AVIF_PROGRESSIVE_STATE_AVAILABLE
	AVIF_PROGRESSIVE_STATE_ACTIVE
End Enum

Type avifDecoder
	codecChoice As avifCodecChoice
	maxThreads As Long
	requestedSource As avifDecoderSource
	allowProgressive As avifBool
	allowIncremental As avifBool
	ignoreExif As avifBool
	ignoreXMP As avifBool
	imageSizeLimit As Ulong
	imageCountLimit As Ulong
	strictFlags As avifStrictFlags
	image As avifImage Ptr
	imageIndex As Long
	imageCount As Long
	progressiveState As avifProgressiveState
	imageTiming As avifImageTiming
	timescale As Ulongint
	duration As Double
	durationInTimescales As Ulongint
	alphaPresent As avifBool
	ioStats As avifIOStats
	diag As avifDiagnostics
	io As avifIO Ptr
	data As avifDecoderData Ptr
End Type

Type avifExtent
	offset As Ulongint
	size As Uinteger
End Type


Type avifEncoder
	codecChoice As avifCodecChoice
	maxThreads As Long
	minQuantizer As Long
	maxQuantizer As Long
	minQuantizerAlpha As Long
	maxQuantizerAlpha As Long
	tileRowsLog2 As Long
	tileColsLog2 As Long
	speed As Long
	keyframeInterval As Long
	timescale As Ulongint
	ioStats As avifIOStats
	diag As avifDiagnostics
	Data As avifEncoderData Ptr
	csOptions As avifCodecSpecificOptions Ptr
End Type


Type avifAddImageFlag As Long
Enum
	AVIF_ADD_IMAGE_FLAG_NONE = 0
	AVIF_ADD_IMAGE_FLAG_FORCE_KEYFRAME = 1 Shl 0
	AVIF_ADD_IMAGE_FLAG_SINGLE = 1 Shl 1
End Enum

Type avifAddImageFlags As Ulong
	

Declare Function avifResultToString Alias "avifResultToString" (Byval result As avifResult) As Const Zstring Ptr
Declare Function avifDecoderCreate Alias "avifDecoderCreate" () As avifDecoder Ptr
Declare Function avifDecoderSetIOFile Alias "avifDecoderSetIOFile" (Byval decoder As avifDecoder Ptr, Byval filename As Const Zstring Ptr) As avifResult
Declare Function avifDecoderSetIOMemory Alias "avifDecoderSetIOMemory" (Byval decoder As avifDecoder Ptr, Byval Data As Const Ubyte Ptr, Byval size As Uinteger) As avifResult
Declare Function avifDecoderParse Alias "avifDecoderParse" (Byval decoder As avifDecoder Ptr) As avifResult
Declare Function avifDecoderNextImage Alias "avifDecoderNextImage" (byval decoder as avifDecoder Ptr) As avifResult
Declare Sub avifRGBImageSetDefaults Alias "avifRGBImageSetDefaults" (Byval Rgb As avifRGBImage Ptr, Byval image As Const avifImage Ptr)
Declare Sub avifRGBImageAllocatePixels Alias "avifRGBImageAllocatePixels" (Byval Rgb As avifRGBImage Ptr)
Declare Function avifImageYUVToRGB Alias "avifImageYUVToRGB" (Byval image As Const avifImage Ptr, Byval Rgb As avifRGBImage Ptr) As avifResult
Declare Sub avifRGBImageFreePixels Alias "avifRGBImageFreePixels" (Byval Rgb As avifRGBImage Ptr)
Declare Sub avifDecoderDestroy Alias "avifDecoderDestroy" (Byval decoder As avifDecoder Ptr)

Dim As String inputFilename = "kodim03_yuv420_8bpc.avif"
Dim As avifRGBImage _rgb
Dim As avifDecoder Ptr decoder = avifDecoderCreate()
? "decoder ptr = " & decoder

Dim As avifResult result = avifDecoderSetIOFile(decoder, inputFilename)

'Dim As Integer hFile, iFileSize = Filelen(inputFilename)
'Dim pMem As Ubyte Ptr
'pMem = Allocate(iFileSize)
'hFile = Freefile()
'Open inputFilename For Binary Access Read As #hFile
'Get #hFile, 0, pMem[0], iFileSize
'Close #hFile
'Dim As avifResult result = avifDecoderSetIOMemory(decoder, pMem, iFileSize)

_GDIPlus_Startup()

If result = AVIF_RESULT_OK Then
	result = avifDecoderParse(decoder)
	If result = AVIF_RESULT_OK Then
		? decoder->image->width, decoder->image->height, decoder->image->depth
		Dim As Ulong c = 0
		While avifDecoderNextImage(decoder) = AVIF_RESULT_OK
			avifRGBImageSetDefaults(@_rgb, decoder->image)
			avifRGBImageAllocatePixels(@_rgb)
			avifImageYUVToRGB(decoder->image, @_rgb)
			c += 1
		Wend
		Dim As Any Ptr hBitmap = AVIF_BitmapCreateGDIp_internal(_rgb.pixels, decoder->image->width, decoder->image->height)
		Display_GDIpImage(hBitmap, decoder->image->width, decoder->image->height)
		
	Else
		? "Error = " & result & " / Failed to decode image -> " & *avifResultToString(result)
	Endif
Else
	? "Error = " & result & " / Cannot open file for read -> " & *avifResultToString(result)
End If

avifRGBImageFreePixels(@_rgb)
avifDecoderDestroy(decoder)
'Deallocate(pMem)
_GDIPlus_Shutdown()
? "Done"
Sleep
The working image can be found here: https://github.com/AOMediaCodec/libavif ... _8bpc.avif
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: Avif image - read/convert to dibitmap

Post by srvaldez »

Hi UEZ
here are both 32 and 64-bit versions of the lib https://drive.google.com/file/d/1yVTGws ... sp=sharing
however I am still getting the error even when using the picture you linked to
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: Avif image - read/convert to dibitmap

Post by srvaldez »

I just compiled the C example "avif_example_decode_file.c" and also get the same error
UEZ
Posts: 834
Joined: May 05, 2017 19:59
Location: Germany

Re: Avif image - read/convert to dibitmap

Post by UEZ »

@srvaldez: thank you very much. For me the test avif image works:

Image

Screenshot is the x86 version.

GIMP loads the images without any issue. Probably they fixed something....
Post Reply