• FreeImage.lib库的配置和简单使用 转


    转载地址 http://www.codeweblog.com/win8-1%E4%B8%8Bfreeimage-lib%E5%BA%93%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/

    配置过OpenGL的同学,可以直接跳过这个部分,VS中配置FreeImage库和配置OpenGL库的流程完全相似。使用的是win7下的VS2013,可以按照下面步骤进行配置然后参考Demo使用该库。

    • 首先把FreeImage.h 头文件在"VS安装目录"/VC/include/FreeImage/,FreeImage文件夹需要自己新建;
    • 紧接着把FreeImage.lib静态库扔到"VS安装目录"/VC/lib/中;
    • 最后,把FreeImage.dll动态链接库放在应用程序的目录下
    • 到这里简单的配置过程就王完成了。

    下面通过Demo来介绍一下FreeImage的一些基本用法,包括图片的加载,获取图片的像素数组,图片保存等。

    int main(){
        // 初始化
        FreeImage_Initialise(TRUE);
    
        // 文件名
        const char* imageFile = "lenna.png";
        const char* saveFile = "lenna_rotate.png";
    
        // 图片格式
        FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    
        // 获取图片格式
        /* 此处需要特别注意,即使后缀名是.png并不代表图片格式就真是PNG格式,这里先通过FreeImage_GetFileType函数获取图片格式,
        然后再进行加载,否则,也许会出现加载失败的情况。
        */
        fif = FreeImage_GetFileType(imageFile);
        if (fif == FIF_UNKNOWN)
            fif = FreeImage_GetFIFFromFilename(imageFile);
    
        FIBITMAP *bitmap1 = NULL;
        FIBITMAP *bitmap2 = NULL;
        if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)){
            bitmap1 = FreeImage_Load(fif, imageFile, PNG_DEFAULT);
        }
        if (!bitmap1){
            fprintf(stderr, "Fail to Load Image!
    ");
            exit(-1);
        }
        else{
            FreeImage_Save(fif, bitmap1, saveFile, PNG_DEFAULT);
            bitmap2 = FreeImage_Load(fif, saveFile, PNG_DEFAULT);
            if (!bitmap2){
                fprintf(stderr, "Fail to Load saved Image!
    ");
                exit(-1);
            }
        }
    
        // 获取影像的宽高,都以像素为单位
        int width = FreeImage_GetWidth(bitmap1);
        int height = FreeImage_GetHeight(bitmap1);
    
        // 获取总共的像素数目
        int pixel_num = width*height;
    
        // 获取保存每个像素的字节数 这里为3,分别为RGB
        unsigned int byte_per_pixel = FreeImage_GetLine(bitmap1) / width;
    
        printf("Width:%d	 Height:%d	 像素总数:%d	 每像素字节数:%d
    ", width, height, pixel_num, byte_per_pixel);
    
        // 获取保存图片的字节数组
        unsigned char *bits1 = FreeImage_GetBits(bitmap1);
        unsigned char *bits2 = FreeImage_GetBits(bitmap2);
    
        // 获取每个像素对应的RGB
        unsigned char *reds = new unsigned char[pixel_num];
        unsigned char *greens = new unsigned char[pixel_num];
        unsigned char *blues = new unsigned char[pixel_num];
    
        int cur = 0;
        for (int x = 0; x < pixel_num; ++x){
            // 这里对应于上述的每个像素的字节数:3
            reds[x] = bits1[cur++];
            greens[x] = bits1[cur++];
            blues[x] = bits1[cur++];
        }
    
        // 反序更新saveFile的字节数组
        cur = 0;
        for (int x = pixel_num - 1; x >= 0; --x){
            bits2[cur++] = reds[x];
            bits2[cur++] = greens[x];
            bits2[cur++] = blues[x];
        }
    
        // 保存更新后的图片
        FreeImage_Save(fif, bitmap2, saveFile, PNG_DEFAULT);
    
        // 从内存中删除载入图片,防止内存泄漏
        FreeImage_Unload(bitmap1);
        FreeImage_Unload(bitmap2);
        // 撤销初始化
        FreeImage_DeInitialise();
    
        return 0;
    }


    上述代码运行后会在给定工程下的testFreeImage/下生成lenna_rotate.png图片,为原先图片旋转后的图片。
  • 相关阅读:
    爱奇艺大数据招聘
    服务设计要解决的问题
    化腐朽为神奇:简明日志规范
    免费攻读人工智能专业,让自己的身价翻番
    JAVA日志的前世今生
    盖洛普Q12在团队中的应用
    漫画:鉴权与安全访问控制的技术血脉
    正确的git开发流程
    'Attempt to create two animations for cell' iOS
    Swift中的for循环基本使用
  • 原文地址:https://www.cnblogs.com/wolfplan/p/6085691.html
Copyright © 2020-2023  润新知