VC中如何读取BMP文件?

zhoyan 2002-08-30 10:21:21
我是初学者,请说详细点。
...全文
568 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
future0723 2002-08-30
  • 打赏
  • 举报
回复
有问题再联系。
future0723 2002-08-30
  • 打赏
  • 举报
回复
接下来的就是图像的像素数据部分了。直接读取就行了,但是要注意字节的对齐问题,即4字节填充。
future0723 2002-08-30
  • 打赏
  • 举报
回复
BMP文件格式如下:
文件头:typedef struct tagBITMAPFILEHEADER {
WORD bfType; //must be "0x424D"
DWORD bfSize; //指定文件大小,包括这14字节。
WORD bfReserved1; //保留,不考虑
WORD bfReserved2; //保留,不考虑
DWORD bfOffBits; //文件头到图象数据的偏移字节
} BITMAPFILEHEADER, *PBITMAPFILEHEADER; //14BYTES
+
位图信息头:typedef struct tagBITMAPINFOHEADER {
DWORD biSize; //本结构长度,40字节
LONG biWidth; //图像宽度,像素
LONG biHeight; //图像高度,像素
WORD biPlanes; //一定是1,不考虑
WORD biBitCount; //表示颜色的位数:1,4,8,24,32
DWORD biCompression; //是否压缩
DWORD biSizeImage; //位图实际占用的字节数
LONG biXPelsPerMeter; //水平分辨率,单位是像素
LONG biYPelsPerMeter; //垂直.................
DWORD biClrUsed; //本图像实际用到的颜色数
DWORD biClrImportant; //重要的颜色数,如果为0,则全部
} BITMAPINFOHEADER;
+
调色板:(biBitCount<=8时才有调色板)
typedef struct tagLOGPALETTE {
WORD palVersion; //版本
WORD palNumEntries; //颜色结构的数目
PALETTEENTRY palPalEntry[1]; //下面的结构
} LOGPALETTE;


typedef struct tagRGBQUAD {
BYTE rgbBlue; //该颜色的蓝色分量。
BYTE rgbGreen; .....
BYTE rgbRed; .....
BYTE rgbReserved; //保留
} RGBQUAD;




kingzai 2002-08-30
  • 打赏
  • 举报
回复
try this code,diplay BMP file
BITMAPINFOHEADER *bmih;
VOID Load(CFile &File) {
BITMAPFILEHEADER bmfh;
if (!File.Read(&bmfh, sizeof(BITMAPFILEHEADER))) {
throw "Failed to read file";
}
if (bmfh.bfType != *(WORD *)"BM") {
throw "Invalid BMP file";
}

int bmih_size= bmfh.bfSize - sizeof(BITMAPFILEHEADER);
bmih= (BITMAPINFOHEADER *)new BYTE[bmih_size];

if (!bmih) {
throw "Failed to allocate memory";
}
if (!File.Read(bmih, bmih_size)) {
throw "Failed to read file";
}
if (bmih->biCompression != BI_RGB || bmih->biCompression != BI_BITFIELDS)
{
throw "Unsupported compression";
}
}

VOID Draw(CDC *pDC, INT x= 0, INT y= 0) {
INT result= StretchDIBits(
dc->GetSafeHdc(),
x, y, bmih->biWidth, bmih->biHeight,
0, 0, bmih->biWidth, bmih->biHeight,
GetBits(), &bmi,
DIB_RGB_COLORS, SRCCOPY
);

if (result == GDI_ERROR) {
throw "Failed to StretchDIBits";
}
}
wq_quake 2002-08-30
  • 打赏
  • 举报
回复
你的邮箱地址给我我给你发解码部分的两个文件
zzq_fortu 2002-08-30
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/Read_Article.asp?Id=12484
zhoyan 2002-08-30
  • 打赏
  • 举报
回复
我就是不知道BMP文件的格式才问的,听说里面有很多的结构体,别拿一大堆的洋文来胡弄我,说点干货好不好?
zzq_fortu 2002-08-30
  • 打赏
  • 举报
回复
你可以先找到bmp的文件格式,然后用CFile按照文件格式来读取文件。


.NET Framework Developer's Guide

Types of BitmapsA bitmap is an array of bits that specify the color of each pixel in a rectangular array of pixels. The number of bits devoted to an individual pixel determines the number of colors that can be assigned to that pixel. For example, if each pixel is represented by 4 bits, then a given pixel can be assigned one of 16 different colors (2^4 = 16). The following table shows a few examples of the number of colors that can be assigned to a pixel represented by a given number of bits.

Bits per pixel Number of colors that can be assigned to a pixel
1 2^1 = 2
2 2^2 = 4
4 2^4 = 16
8 2^8 = 256
16 2^16 = 65536
24 2^24 = 16, 777, 216

Disk files that store bitmaps usually contain one or more information blocks that store information such as the number of bits per pixel, number of pixels in each row, and number of rows in the array. Such a file might also contain a color table (sometimes called a color palette). A color table maps numbers in the bitmap to specific colors. The following illustration shows an enlarged image along with its bitmap and color table. Each pixel is represented by a 4-bit number, so there are 2^4 = 16 colors in the color table. Each color in the table is represented by a 24-bit number: 8 bits for red, 8 bits for green, and 8 bits for blue. The numbers are shown in hexadecimal (base 16) form: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.



Look at the pixel in row 3, column 5 of the image. The corresponding number in the bitmap is 1. The color table tells us that 1 represents the color red so the pixel is red. All the entries in the top row of the bitmap are 3. The color table tells us that 3 represents blue, so all the pixels in the top row of the image are blue.

Note Some bitmaps are stored in bottom-up format; the numbers in the first row of the bitmap correspond to the pixels in the bottom row of the image.
A bitmap that stores indexes into a color table is called a palette-indexed bitmap. Some bitmaps have no need for a color table. For example, if a bitmap uses 24 bits per pixel, that bitmap can store the colors themselves rather than indexes into a color table. The following illustration shows a bitmap that stores colors directly (24 bits per pixel) rather than using a color table. The illustration also shows an enlarged view of the corresponding image. In the bitmap, FFFFFF represents white, FF0000 represents red, 00FF00 represents green, and 0000FF represents blue.



Graphics File Formats
There are many standard formats for saving bitmaps in disk files. GDI+ supports the graphics file formats described in the following paragraphs.

BMP

BMP is a standard format used by Windows to store device-independent and application-independent images. The number of bits per pixel (1, 4, 8, 15, 24, 32, or 64) for a given BMP file is specified in a file header. BMP files with 24 bits per pixel are common. BMP files are usually not compressed and therefore are not well suited for transfer across the Internet.

Graphics Interchange Format (GIF)

GIF is a common format for images that appear on Web pages. GIFs work well for line drawings, pictures with blocks of solid color, and pictures with sharp boundaries between colors. GIFs are compressed, but no information is lost in the compression process; a decompressed image is exactly the same as the original. One color in a GIF can be designated as transparent, so that the image will have the background color of any Web page that displays it. A sequence of GIF images can be stored in a single file to form an animated GIF. GIFs store at most 8 bits per pixel, so they are limited to 256 colors.

Joint Photographic Experts Group (JPEG)

JPEG is a compression scheme that works well for natural scenes such as scanned photographs. Some information is lost in the compression process, but often the loss is imperceptible to the human eye. JPEGs store 24 bits per pixel, so they are capable of displaying more than 16 million colors. JPEGs do not support transparency or animation.

The level of compression in JPEG images is configurable, but higher compression levels (smaller files) result in more loss of information. A 20:1 compression ratio often produces an image that the human eye finds difficult to distinguish from the original. The following illustration shows a BMP image and two JPEG images that were compressed from that BMP image. The first JPEG has a compression ratio of 4:1 and the second JPEG has a compression ratio of about 8:1.



JPEG compression does not work well for line drawings, blocks of solid color, and sharp boundaries. The following illustration shows a BMP along with two JPEGs and a GIF. The JPEGs and the GIF were compressed from the BMP. The compression ratio is 4:1 for the GIF, 4:1 for the smaller JPEG, and 8:3 for the larger JPEG. Note that the GIF maintains the sharp boundaries along the lines, but the JPEGs tends to blur the boundaries.



JPEG is a compression scheme, not a file format. JPEG File Interchange Format (JFIF) is a file format commonly used for storing and transferring images that have been compressed according to the JPEG scheme. JFIF files displayed by Web browsers use the .jpg extension.

Exchangeable Image File (EXIF)

EXIF is a file format used for photographs captured by digital cameras. An EXIF file contains an image that is compressed according to the JPEG specification. An EXIF file also contains information about the photograph (date taken, shutter speed, exposure time, and so on) and information about the camera (manufacturer, model, and so on).

Portable Network Graphics (PNG)

The PNG format retains many of the advantages of the GIF format but also provides capabilities beyond those of GIF. Like GIF files, PNG files are compressed with no loss of information. PNG files can store colors with 8, 24, or 48 bits per pixel and grayscales with 1, 2, 4, 8, or 16 bits per pixel. In contrast, GIF files can use only 1, 2, 4, or 8 bits per pixel. A PNG file can also store an alpha value for each pixel, which specifies the degree to which the color of that pixel is blended with the background color.

PNG improves on GIF in its ability to progressively display an image; that is, to display better and better approximations of the image as it arrives over a network connection. PNG files can contain gamma correction and color correction information so that the images can be accurately rendered on a variety of display devices.

Tag Image File Format (TIFF)

TIFF is a flexible and extendable format that is supported by a wide variety of platforms and image-processing applications. TIFF files can store images with an arbitrary number of bits per pixel and can employ a variety of compression algorithms. Several images can be stored in a single, multiple-page TIFF file. Information related to the image (scanner make, host computer, type of compression, orientation, samples per pixel, and so on) can be stored in the file and arranged through the use of tags. The TIFF format can be extended as needed by the approval and addition of new tags.


--------------------------------------------------------------------------------

Send comments on this topic.

© 2001 Microsoft Corporation. All rights reserved.

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧