Image processing - C++ freeimage uses online examples to invert the image, and a very strange result appears.
给我你的怀抱
给我你的怀抱 2017-05-16 13:21:19
0
1
884

I have recently been using the C++ image processing library freeimage. I encountered a problem before. See here. The reason for that problem should be what I asked in this question, but I have not found the answer to this question.

Problem Description:

I found an example here, so I used it. Some pictures can be reversed normally, but some pictures (some jpg) have strange phenomena after being reversed:

The program code is simple and easy to understand, but the processing results are incredible

Original picture:

The result after application processing:

Another original picture:

Result after applying image processing:

Of course, there are some good pictures, so I won’t post the ones that went well here.

It bothers me a lot. I am still learning my personal image knowledge. If you have used similar libraries or can solve the problem, please provide answers. If you can really solve the problem, you can also send me a private message and I will send you a 10 yuan red envelope or something.

Thanks in advance.
Attached code:

#include <iostream>

#include <vector>

#include <stdlib.h>

#include "FreeImage.h"

using namespace std;

int main(){
    // 初始化
    FreeImage_Initialise(TRUE);
    
    // 文件名
    const char* imageFile = "/Users/hh/Desktop/Possion/pool-target.jpg";
    const char* saveFile = "/Users/hh/Desktop/Possion/pool-target2.jpg";
    
    // 图片格式
    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!\n");
        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!\n");
            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\t Height:%d\t 像素总数:%d\t 每像素字节数:%d\n", 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;
}
给我你的怀抱
给我你的怀抱

reply all(1)
phpcn_u1582
int pitch = FreeImage_GetPitch(bitmap1);

// 获取保存图片的字节数组
unsigned char *bits1 = FreeImage_GetBits(bitmap1);
unsigned char *bits2 = FreeImage_GetBits(bitmap2);

// 上下倒转
for (int y = 0; y < height; y++) {
    memcpy(bits2 + (y * pitch),
        bits1 + ((height - y - 1) * pitch),
        pitch);
}

pitch is how many bytes there are in each line. In order to speed up reading, it is generally necessary to ensure that the memory address is a multiple of 4, which is usually more than the picture itself.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template