#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ccnn.h"

/******************************************/
char *ReadImg(const char *fileImg, int *len)
{
    FILE *file;
    file = fopen(fileImg, "rb");
    if (file == NULL) {
        return NULL;
    }

    int allocSize = 10240000;
    char *data = new char[allocSize];
    int readNum = (int)fread(data, sizeof(char), allocSize, file);
    fclose(file);

    *len = readNum;
    return data;
}

/************************************************************/
void InferenceImg(const char *cnfXMLName, const char *fileImg)
{
    int bufLen, outType;
    float typePab;
    char *imgBuff = ReadImg(fileImg, &bufLen);
        
    //利用XML配置，构建深度神经网络
    struct CcnnContext *pCtx = LcfNet::CreateNetFromXML(cnfXMLName, 1, 1);
    
    //读取网络输入图像长宽大小   
    int mPicHWLen = LcfNet::ReadInputHeightWidth(pCtx);
        
    //图像长宽读取到指定的大小    
    float *imgP = ImgHandle::LoadSquareImg((unsigned char *)imgBuff, bufLen, 1, NULL, NULL, NULL, mPicHWLen);
    delete []imgBuff;
    
    //进行推理预测
    LcfNet::NetPredictClassify(pCtx, imgP, &outType, &typePab);               
    delete []imgP;
    
    //释放网络结构及参数
    LcfNet::RealeaseCcnnStructMemory(pCtx);
        
    //输出预测类别及概率    
    printf("%d %f\n", outType, typePab);
}

/******************************/
int main(int argc, char *argv[])
{
    InferenceImg(argv[1], argv[2]);
    
    return 1;
}
