BMP image flip

Source: Internet
Author: User
Tags bmp image image flip scale image

// BMP. h

# Ifndef _ BMP _h _
# DEFINE _ BMP _h _
# Pragma pack (1)

Typedef struct tagbitmapfileheader
{
Char bftype [2]; // file type, which is BM in Windows
Long bfsize; // File Size
Short bfreserved1; // reserved, set to 0
Short bfreserved2; // reserved, set to 0
Long bfoffbits; // offset of the actual graph data
} Bitmapfileheader;

Typedef struct tagbitmapinfoheader
{
Long bisize; // The number of bytes required by the Bitmap header. The value is 40 bytes in the windws system.
Long biwidth; // Image Width
Long biheight; // Image Height
Short biplanes; // specifies the number of bits for the target device. The value is 1.
Short bibitcount; // The number of digits occupied by each pixel. The color of 1-16 is 4,256, and the color of 8-24 is 24.
Long bicompression; // compression type, BMP is 0
Long bisizeimage; // image size, no compression set to 0
Long bixpelspermeter; // horizontal resolution
Long biypelspermeter; // vertical resolution
Long biclrused; // The number of colors used by the bitmap.
Long biclrimportant; // number of important colors
} Bitmapinfoheader;

Typedef struct tagrgbquad
{
Unsigned char rgbblue; // specify the blue intensity
Unsigned char rgbgreen; // specify the green intensity
Unsigned char rgbred; // specify the red intensity
Unsigned char rgbreserved; // reserved, set to 0
} Rgbquad;

 

Extern unsigned char * pbmpbuf; // pointer for reading image data
Extern long BMP width; // Image Width
Extern long bmpheight; // Image Height
Extern rgbquad * pcolortable; // color table pointer
Extern int bibitcount; // image type, number of digits per pixel
Extern int linebyte;
Extern bool readbmp (char *);
Extern bool savebmp (char *, unsigned char *, Int, rgbquad *);

# Endif

 

// Read. c

# Include <stdio. h>
# Include "BMP. H"
Bool readbmp (char * BMP name)
{// Open the specified image file in binary read mode
File * fp = fopen (BMP name, "rb ");
If (FP = 0) return 0;
// Define the bitmap file header structure variable, read the bitmap file header into the memory, and store it in the variable File
Bitmapfileheader file;
Fseek (FP, 0, 0 );
Fread (& file, sizeof (bitmapfileheader), 1, FP );
Printf ("%. 2 s", file. bftype );
Long bfsize = file. bfsize;
Short bfreserved1 = file. bfreserved1;
Short bfreserved2 = file. bfreserved2;
Long bfoffbits = file. bfoffbits;
// Define the bitmap information header structure variable, read the bitmap information header into the memory, and store it in the variable head
Bitmapinfoheader head;
Fread (& head, sizeof (bitmapinfoheader), 1, FP );
BMP width = head. biwidth;
Bmpheight = head. biheight;
Bibitcount = head. bibitcount;
// Define the variable to calculate the number of bytes occupied by each pixel in the image
Linebyte = (BMP width * bibitcount/8 + 3)/4*4;
// Grayscale images have a color table, and the color table item is 256
If (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 );
}
// The space required to apply for bitmap data. Read bitmap data into the memory.
Pbmpbuf = new unsigned char [linebyte * bmpheight];
Fread (pbmpbuf, 1, linebyte * bmpheight, FP );
// Close the file
Fclose (FP );
Return 1;
}

 

// Save. c

# Include <stdio. h>
# Include "BMP. H"
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 passed in and the function returns
If (! Imgbuf)
Return 0;
// The color table size, in bytes. The gray scale image color table 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 in binary mode
File * fp = fopen (BMP name, "WB + ");
If (FP = 0) return 0;
// Apply for bitmap file header Structure Variables and fill in the file header information
Bitmapfileheader file;
File. bftype [0] = 0x42; // BMP Type
File. bftype [1] = 0x4d;
// Bfsize is the sum of the four components of an image file.
File. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader)
+ Colortablesize + linebyte * height;
File. bfreserved1 = 0;
File. bfreserved2 = 0;
// Bfoffbits is the sum of the space required for the first three parts of the image file.
File. bfoffbits = 54 + colortablesize;
// Write the file header into the file
Fwrite (& file, sizeof (bitmapfileheader), 1, FP );
// Apply for bitmap information header Structure Variables and fill in information header information
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;
// Write bitmap information header into memory
Fwrite (& head, sizeof (bitmapinfoheader), 1, FP );
// If the grayscale image has a color table, write it to a file
If (bibitcount = 8)
If (fwrite (pcolortable, sizeof (rgbquad), 256, FP )! = 256)
Printf ("/nwrite error/N ");
Else if (bibitcount = 1)
Fwrite (pcolortable, sizeof (rgbquad), 2, FP );
Else if (bibitcount = 4)
Fwrite (pcolortable, sizeof (rgbquad), 2 ^ 4, FP );
// Write bitmap data into the file
Fwrite (imgbuf, height * linebyte, 1, FP );
// Close the file
Fclose (FP );
Return 1;
}

 

// Main. c

# Include <stdio. h>
# Include "BMP. H"
# Include "BMP. H"
Unsigned char * pbmpbuf; // pointer for reading image data
Long BMP width; // Image Width
Long bmpheight; // Image Height
Rgbquad * pcolortable; // color table pointer
Int bibitcount; // image type, number of digits per pixel
Int linebyte;
Void main ()
{
Char readpath [] = "1.bmp ";
Readbmp (readpath );
Unsigned char * dataout;
Linebyte = (BMP width * 3 + 3)/4*4;
Int linebyteout = (BMP width * 3 + 3)/4*4;
Dataout = new unsigned char [linebyteout * bmpheight];
Int I, j, bibitcountout = 24;
For (I = 0; I <bmpheight; I ++)
{
For (j = 0; j <BMP width; j ++)
{
* (Dataout + I * linebyteout + J * 3 + 0) = 255-* (pbmpbuf + I * linebyte + J * 3 + 0 );
* (Dataout + I * linebyteout + J * 3 + 1) = 255-* (pbmpbuf + I * linebyte + J * 3 + 1 );
* (Dataout + I * linebyteout + J * 3 + 2) = 255-* (pbmpbuf + I * linebyte + J * 3 + 2 );
}
}

Printf ("% d, % d", BMP width, bibitcount );

Char writepath [] = "change.bmp ";
Savebmp (writepath, dataout, BMP width, bmpheight, bibitcountout, pcolortable );
// Clear the buffer
Delete [] dataout;
Delete [] pcolortable;
}

 

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.