会飞的菠菜 2021-11-02 09:51 采纳率: 0%
浏览 81
已结题

bmp文件存储时图像发生偏移

本人在用VS2019进行bmp文件读取与存储时,彩色图像的输出图像与输入图像之间发生了偏移,但是灰度图像没有影响,请问是什么原因,代码如下


#include<Windows.h>
#include<stdio.h>
#include<iostream>
using namespace std;

unsigned char* pBmpBuf;//读入图像数据的指针
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD* pColorTable;//颜色表指针
int biBitCount;//图像类型

bool readBmp(char* bmpName)
{
    FILE* fp = fopen(bmpName, "rb");
    if (fp == 0) return 0;

    fseek(fp, sizeof(BITMAPFILEHEADER), 0);

    BITMAPINFOHEADER head;
    fread(&head, sizeof(BITMAPINFOHEADER), 1, fp);

    bmpWidth = head.biWidth;
    bmpHeight = head.biHeight;
    biBitCount = head.biBitCount;

    int lineByte = (bmpWidth * biBitCount/8+3)/4*4;

    if (biBitCount == 8) 
    {
        pColorTable = new RGBQUAD[256];
        fread(pColorTable, sizeof(RGBQUAD), 256, fp);
    }

    pBmpBuf = new unsigned char[lineByte * bmpHeight];
    fread(pBmpBuf, 1, lineByte * bmpHeight, fp);

    fclose(fp);
    return 1;
}

bool saveBmp(char* bmpName, unsigned char* imgBuf, int width, int height,int biBitCount, RGBQUAD* pColorTable)
{

    if (!imgBuf)
    {
        return 0;
    }

    int colorTablesize = 0;
    if (biBitCount == 8)
    {
        colorTablesize = 768;
    }

    int lineByte = (width * biBitCount/8+3)/4*4;

    FILE* fp = fopen(bmpName, "wb");
    if (fp == 0) { return 0; }

    BITMAPFILEHEADER fileHead;
    fileHead.bfType = 0x4D42;
    fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte * height;
    fileHead.bfReserved1 = 0;
    fileHead.bfReserved2 = 0;
    fileHead.bfOffBits = 54 + colorTablesize;
    fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);

    BITMAPINFOHEADER head;
    head.biBitCount = biBitCount;
    head.biClrImportant = 0;
    head.biClrUsed = 0;
    head.biCompression = 0;
    head.biHeight = height;
    head.biPlanes = 1;
    head.biSize = 40;
    head.biSizeImage = lineByte * height;
    head.biWidth = width;
    head.biXPelsPerMeter = 0;
    head.biYPelsPerMeter = 0;
    fwrite(&head, sizeof(BITMAPINFOHEADER), 1, fp);

    if (biBitCount == 8)
    {
        fwrite(pColorTable, sizeof(RGBQUAD), 256, fp);
    }
    fwrite(imgBuf, height * lineByte, 1, fp);
    
    fclose(fp);
    return 1;
}

void main()
{

    char readPath[]="C:\\Users\\123\\Desktop\\123.bmp";
    readBmp(readPath);

    cout<<"width="<<bmpWidth<<" "<<"height="<<bmpHeight <<" " << biBitCount;

    char writePath[]="C:\\Users\\123\\Desktop\\1231.bmp";
    saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);

    delete []pBmpBuf;
    if(biBitCount==8)
        delete []pColorTable;
}

运行结果如下:

img

img

  • 写回答

1条回答 默认 最新

  • 有问必答小助手 2021-11-04 10:26
    关注

    你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


    本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


    因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

    评论

报告相同问题?

问题事件

  • 系统已结题 11月10日
  • 创建了问题 11月2日

悬赏问题

  • ¥15 信贷平台.用户信用评估和风险评估怎么做,希望来个做过的Java.有合作的机会
  • ¥15 IMageEN获得图形顶点坐标的问题
  • ¥50 软件PC客户端抓包,获取http请求和响应
  • ¥15 手机被安装黑客软件怎么办?
  • ¥15 Windows C++ PaddleOcr 中文模型的训练方法
  • ¥15 c# 用scottplot画 以时间为纵坐标,数值为横坐标画曲线图
  • ¥15 手机应用程序安装异常
  • ¥15 grbl的G92修改MPos的问题。
  • ¥15 vue2中,Ant Design Pro s-table中,使用服务端排序怎么做
  • ¥15 阿里巴巴国际站的商品详细页如何做到图文分离