Vc6.0 reads BMP image information to byte array

Source: Internet
Author: User
# Include "stdafx. H "// note that the bitmap of the tested BMP image is 256 # include <math. h> # include <iomanip. h> # include <stdlib. h> # include <windows. h> # include <stdio. h> # include <stdlib. h> # include <iostream. h> # include <fstream. h> unsigned char * pbmpbuf; // the pointer to read image data int BMP width; // The Image Width int bmpheight; // The Image Height rgbquad * pcolortable; // color table pointer int ititcount; // image type, number of digits per pixel //--------------------------------------------------------------------------- ---------------- // Read the bitmap data, width, height, color table, and number of bits per pixel into the memory, and store the data in the corresponding global variable bool readbmp (char * BMP name) {file * fp = fopen (BMP name, "rb"); // If (FP = 0) return 0 to open the specified image file in binary read mode; // skip bitmap file header structure bitmapfileheader fseek (FP, sizeof (bitmapfileheader), 0); // define bitmap information header structure variable, read bitmap information header into memory, in the variable head, bitmapinfoheader head; fread (& head, sizeof (bitmapinfoheader), 1, FP ); // obtain information such as image width, height, and the number of digits occupied by each pixel. BMP width = head. biwidth; bmpheight = head. biheight; B Ibitcount = head. bibitcount; // defines the variable to calculate the number of bytes occupied by pixels in each line of the image (must be a multiple of 4) int linebyte = (BMP width * bibitcount/8 + 3)/4*4; // grayscale images have a color table, and the color table items are 256if (bibitcount = 8) {// apply for the space required by the color table, read the color table into the memory pcolortable = new rgbquad [256]; fread (pcolortable, sizeof (rgbquad), 256, FP);} // space required to apply for bitmap data, read bitmap data into memory pbmpbuf = new unsigned char [linebyte * bmpheight]; fread (pbmpbuf, 1, linebyte * bmpheight, FP); fclose (FP ); // close the file return 1; // The file is successfully read }//-------- #// Specify the bitmap data, width, height, color table pointer, and the number of digits occupied by each pixel, and write it to the bool savebmp (char * BMP name, unsigned char * imgbuf, int width, int height, int bibitcount, rgbquad * pcolortable) {// If the bitmap data pointer is 0, no data is transmitted, and the function returns if (! Imgbuf) return 0; // color table size, in bytes. The gray image color table size is 1024 bytes, and the color image color table size is 0 int colortablesize = 0; If (bibitcount = 8) colortablesize = 1024; // The number of bytes in each row of the image data to be stored is a multiple of 4. Int linebyte = (width * bibitcount/8 + 3)/4*4; // open the file * fp = fopen (BMP name, "WB") in binary mode; If (FP = 0) return 0; // apply for bitmap file header Structure Variables, fill in the file header information bitmapfileheader filehead; filehead. bftype = 0x4d42; // BMP Type // bfsize is the sum of the four components of the image file filehead. bfsize = sizeof (bitmapfileheader) + size Of (bitmapinfoheader) + colortablesize + linebyte * height; filehead. bfreserved1 = 0; filehead. bfreserved2 = 0; // bfoffbits is the sum of the space required for the first three parts of the image file filehead. bfoffbits = 54 + colortablesize; // write the file header into the file fwrite (& filehead, sizeof (bitmapfileheader), 1, FP); // apply for bitmap information header Structure Variables, fill in the Information header information bitmapinfoheader head; head. bibitcount = bibitcount; head. biclrimportant = 0; head. biclrused = 0; head. bicompression = 0; head. biheight = height; head. B Iplanes = 1; head. bisize = 40; head. bisizeimage = linebyte * height; head. biwidth = width; head. bixpelspermeter = 0; head. biypelspermeter = 0; // write the bitmap information header into the memory fwrite (& head, sizeof (bitmapinfoheader), 1, FP); // If the grayscale image has a color table, write File if (bibitcount = 8) fwrite (pcolortable, sizeof (rgbquad), 256, FP); // write bitmap data into file fwrite (imgbuf, height * linebyte, 1, FP); // close the file fclose (FP); return 1 ;}//----------------------------------------------- --------------------------------------- // The following is the pixel READ function void doit () {// read the specified BMP file into the memory char readpath [] = "Lena. BMP "; readbmp (readpath ); // output the image information cout <"width =" <BMP width <"Height =" <bmpheight <"bibitcount =" <bibitcount <Endl; // cyclic variable, image coordinate // number of bytes per line int linebyte = (BMP width * bibitcount/8 + 3)/4*4; // cyclic variable, for color images, traverse the three components of each pixel: int m = 0, n = 0, count_xiang_su = 0; // set the first part of the image in the lower left corner to black ofstream OUTFILE ("image example .txt", IOS :: in | IOs :: Trunc); If (! OUTFILE) {cout <"Open error! "<Endl; exit (1);} If (bibitcount = 8) // For the grayscale image {// assets // The image is divided into 8*8 small units and the pixel value is stored in the specified text. Since the pixel data of the BMP image is from // bottom left: from left to right, from top to bottom int L1 = 0; int hang = 63; int lie = 0; for (INT fen_ge_hang = 0; fen_ge_hang <8; fen_ge_hang ++) // 64*64 matrix row loops {for (INT fen_ge_lie = 0; fen_ge_lie <8; fen_ge_lie ++) // 64*64 column matrix loop {// -------------------------------------------------- for (L1 = hang; L1> hang-8; L1 --) // 8*8 matrix rows {for (INT L2 = lie; L2 <lie + 8; L2 ++) {M = * (pbmpbuf + L1 * linebyte + l2 ); OUTFILE <m <"; count_xiang_su ++; If (count_xiang_su % 8 = 0) // read the text file {OUTFILE <Endl ;}} hang = 63-fen_ge_hang * 8 per 8*8 matrix; // 64*64 matrix line transformation lie ++ = 8; // 64*64 matrix column transformation // This row (64) is composed of eight 8*8 matrix rows} Hang-= 8; // 64*64 matrix column transformation lie = 0; // 64*64 juzhen} else if (bibitcount = 24) {// color image for (INT I = 0; I <bmpheight; I ++) {for (Int J = 0; j <BMP width; j ++) {for (int K = 0; k <3; k ++) // set each pixel RGB to 0 to black {M = * (pbmpbuf + I * linebyte + J * 3 + k ); OUTFILE <m <"; count_xiang_su ++; If (count_xiang_su % 8 = 0) {OUTFILE <Endl ;}} n ++ ;}} cout <"Total pixel elements:" <n <Endl; cout <"------------------------------------------------------" <Endl ;} // Save the image data disk char writepath [] = "nvcpy. BMP "; // save savebmp (writepath, pbmpbuf, BMP width, bmpheight, bibitcount, pcolortable) after image processing; // clear the buffer. pbmpbuf and pcolortable are global variables, delete [] pbmpbuf; If (bibitcount = 8) Delete [] pcolortable;} int main () {doit (); Return 0 ;}

From: http://blog.sina.com.cn/s/blog_8bdd25f80101900h.html

Record the call.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.