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;
}
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
(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)