RGB三通道数据转换bmp时图像错位

问题遇到的现象和发生背景

将red、green、blue三个通道的数据转换bmp时,生成的图像错位了,代码段如下:

else if (3 == channels) {
    LPBYTE red_data_buf = nullptr;
    LPBYTE green_data_buf = nullptr;
    LPBYTE blue_data_buf = nullptr;
    
    // 获取RGB图像的三个通道的数据
    image.GetImagePointer3((LPVOID*)&red_data_buf, (LPVOID*)&green_data_buf, (LPVOID*)&blue_data_buf, &type, &width, &height);

    // 计算每行字节数
    pitch *= ((width * channels * 8 + 31) & ~31) / 8;
    len = abs(pitch) * height;
    image_data_buf = new BYTE[len];
    memset(image_data_buf, 0, len);

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            image_data_buf[i * abs(pitch) + 3 * j + 0] = blue_data_buf[i * width + j];
            image_data_buf[i * abs(pitch) + 3 * j + 1] = green_data_buf[i * width + j];
            image_data_buf[i * abs(pitch) + 3 * j + 2] = red_data_buf[i * width + j];
        }
    }
}

原始图片如下

img

img

img

效果图如下:

img

问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果