{
#ifndef __DAVIF_H__
#define __DAVIF_H__
#include <defineds.h>
#include <iostream>
#include <string>
#include <string.h>
#include <queue>
using namespace std;
extern "C"
{
#include <avif/avif.h>
#include <SOIL2.h>
}
class EX_DLL davif
{
avifDecoder * m_decoder;
avifResult result;
public:
davif();
~davif();
avifResult load(std::string path);
//please free rame pix data
int get(avifRGBImage *frame);
void free(avifRGBImage *frame);
///
int saveImage(avifRGBImage *frame,const char *filename, int image_type = SOIL_SAVE_TYPE_PNG);
private:
avifDecoder *createDecoder();
};
#endif // !__DAVIF_H__
}
{
#include "davif.h"
avifDecoder *davif::createDecoder()
{
avifDecoder * decoder = avifDecoderCreate();
if (decoder)
{
return decoder;
}
return nullptr;
}
davif::davif()
{
m_decoder = this->createDecoder();
}
davif::~davif()
{
avifDecoderDestroy(m_decoder);
}
avifResult davif::load(std::string path)
{
avifResult result = avifDecoderSetIOFile(m_decoder, path.c_str());
if (result != AVIF_RESULT_OK)
{
//fprintf(stderr, "Cannot open file for read: %s\n", path.c_str());
return result;
}
printf("open file for read: %s\n", path.c_str());
result = avifDecoderParse(m_decoder);
if (result != AVIF_RESULT_OK)
{
//fprintf(stderr, "Failed to decode image: %s\n", avifResultToString(result));
return result;
}
printf("decode image: %s\n", avifResultToString(result));
//printf("Parsed AVIF: %ux%u (%ubpc)\n",
// m_decoder->image->width, m_decoder->image->height, m_decoder->image->depth);
return AVIF_RESULT_OK;
}
void davif::free(avifRGBImage *frame)
{
avifRGBImageFreePixels(frame);
// Only use in conjunction with avifRGBImageAllocatePixels()
}
int davif::get(avifRGBImage *frame)
{
result = avifDecoderNextImage(m_decoder);
while (result == 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(frame, m_decoder->image);
// Override YUV(A)->RGB(A) defaults here:
// depth, format, chromaUpsampling, avoidLibYUV, ignoreAlpha, alphaPremultiplied, 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)
result = avifRGBImageAllocatePixels(frame);
if (result != AVIF_RESULT_OK)
{
printf("avifRGBImageAllocatePixels faild \n");
return result;
}
result = avifImageYUVToRGB(m_decoder->image, frame);
if (result != AVIF_RESULT_OK)
{
printf("avifImageYUVToRGB faild \n");
return result;
}
// Now available:
// * RGB(A) pixel data (rgb.pixels, rgb.rowBytes)
if (frame->depth > 8)
{
uint16_t * firstPixel = (uint16_t *)frame->pixels;
}
else
{
uint8_t * firstPixel = frame->pixels;
}
frame->depth = frame->depth;
frame->width = frame->width;
frame->height = frame->height;
frame->pixels = frame->pixels;
return result;
}
return result;
}
int davif::saveImage(avifRGBImage *frame, const char *filename, int image_type)
{
int re = SOIL_save_image(filename, image_type,
frame->width, frame->height,
SOIL_LOAD_RGBA, frame->pixels);
return re;
}
}
{
davif davifs;
//H:/image/cfimage/26.avif
void HelloWorld::avif(string file)
{
avifRGBImage rgb;
memset(&rgb, 0, sizeof(rgb));
cocos2d::log("avif version: %s\n", avifVersion());
auto l = davifs.load(file);
auto g = davifs.get(&rgb);
cocos2d::log("avif file load: %d\n", l);
cocos2d::log("avif file get: %d\n", g);
char buf[1024] = { 0 };
avifCodecVersions(buf);
cocos2d::log("avif v: %s\n", buf);
Texture2D *texture = new Texture2D();
if (texture)
{
texture->autorelease();
}
auto data = rgb.pixels;
auto width = rgb.width;
auto height = rgb.height;
cocos2d::log("avif file width : %d height : %d\n", width, height);
texture->initWithData(data,
width * height * 4,
Texture2D::PixelFormat::RGBA8888,
width, height,
Size(width, height));
auto __s = Sprite::createWithTexture(texture);
__s->setPosition(visibleSize / 2);
__s->setScale(0.5f);
this->addChild(__s);
davifs.free(&rgb);
}
}
标签:封装,frame,davif,liavif,rgb,result,decoder,image From: https://www.cnblogs.com/YZFHKMS-X/p/18295939