#include "StdAfx.h"
#include "stdio.h"
#include "Memory.h"
#include "Stdlib.h"
#define PIXPLINE 320
typedef struct tagrgbquad{//define data types for each pixel
unsigned char rgbblue;
unsigned char rgbgreen;
unsigned char rgbred;
} Rgbquad;
int bmp_read (unsigned char *image, int xsize, int ysize, char *filename) {
Char fname_bmp[128];
sprintf (fname_bmp, "%s.bmp", filename);
FILE *FP;
if (!) ( fp = fopen (Fname_bmp, "RB"))
return-1;
unsigned char header[54];
Fread (header, sizeof (unsigned char), SI, FP);
Fread (image, sizeof (unsigned char), (size_t) (long) xsize * ysize * 3, FP);
Fclose (FP);
return 0;
}
int bmp_write (unsigned char *image, int xsize, int ysize, char *filename) {
unsigned char header[54] ={
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0.
};
Long file_size = (long) xsize * (Long) Ysize * 3 + 54;
HEADER[2] = (unsigned char) (file_size &0x000000ff);
HEADER[3] = (file_size >> 8) & 0x000000ff;
HEADER[4] = (file_size >>) & 0x000000ff;
HEADER[5] = (file_size >>) & 0x000000ff;
Long width = xsize;
HEADER[18] = width & 0x000000ff;
HEADER[19] = (width >> 8) &0x000000ff;
HEADER[20] = (width >>) &0x000000ff;
HEADER[21] = (width >>) &0x000000ff;
Long height = ysize;
HEADER[22] = height &0x000000ff;
HEADER[23] = (height >> 8) &0x000000ff;
HEADER[24] = (height >>) &0x000000ff;
HEADER[25] = (height >>) &0x000000ff;
Char fname_bmp[128];
sprintf (fname_bmp, "%s.bmp", filename);
FILE *FP;
if (!) ( fp = fopen (fname_bmp, "WB"))
return-1;
Fwrite (header, sizeof (unsigned char), SI, FP);
Fwrite (image, sizeof (unsigned char), (size_t) (long) xsize * ysize * 3, FP);
Fclose (FP);
return 0;
}
void clonebmp (unsigned char *image,int xsize,int ysize)
{
Bmp_read (image, Xsize, Ysize, ".//1");
Bmp_write (image, Xsize, Ysize, ".//2");
}
/**//****************************************************************************
* Name: Youwritetobmp ()
* Function: Write BMP file
* Entry parameters: Rgbquad *pixarr----The pixel array pointer to write, int xsize----image width, int ysize----image height, char *filename-image Name
* Export parameters: None
* Return Value:-1: Error; 0: Correct
****************************************************************************/
int Youwritetobmp (rgbquad *pixarr, int xsize, int ysize, char *filename) {
unsigned char header[54] ={
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0.
};
int i;
Int J;
Long file_size = (long) xsize * (Long) Ysize * 3 + 54;
HEADER[2] = (unsigned char) (file_size &0x000000ff);
HEADER[3] = (file_size >> 8) & 0x000000ff;
HEADER[4] = (file_size >>) & 0x000000ff;
HEADER[5] = (file_size >>) & 0x000000ff;
Long width;
if (!) ( xsize%4)) Width=xsize;
else width= xsize+ (4-xsize%4); If it is not a multiple of 4, it is converted to a multiple of 4
HEADER[18] = width & 0x000000ff;
HEADER[19] = (width >> 8) &0x000000ff;
HEADER[20] = (width >>) &0x000000ff;
HEADER[21] = (width >>) &0x000000ff;
Long height = ysize;
HEADER[22] = height &0x000000ff;
HEADER[23] = (height >> 8) &0x000000ff;
HEADER[24] = (height >>) &0x000000ff;
HEADER[25] = (height >>) &0x000000ff;
Char fname_bmp[128];
sprintf (fname_bmp, "%s.bmp", filename);
FILE *FP;
if (!) ( fp = fopen (fname_bmp, "WB"))
return-1;
Fwrite (header, sizeof (unsigned char), SI, FP);
Rgbquad zero = {0,0,0}; Less bytes, padding with zero
for (j=0;j<ysize;j++) {
if (!) ( xsize%4)) {
for (i=0;i<xsize;i++) {
Fwrite (pixarr+i, sizeof (Rgbquad), 1, FP);
}
}
Else
{
for (i=0;i<xsize;i++) {
Fwrite (pixarr+i, sizeof (Rgbquad), 1, FP);
}
For (i=xsize;i<xsize+ (4-xsize%4); i++) {
Fwrite (&zero, sizeof (Rgbquad), 1, FP);
}
}
}
Fclose (FP);
return 0;
}
int main ()
{
unsigned char *image;
int xsize = 320;
int ysize = 163;
Rgbquad Pixarray[pixpline]; Array of one-line pixel values
Image = (unsigned char *) malloc ((size_t) xsize * ysize * 3);
if (image = = NULL)
return-1;
Clonebmp (image,xsize,ysize); Function one: To achieve a copy of the image
Youwritetobmp (Pixarray,xsize,ysize, "yourimage");//Function Two: Achieve accurate pixel rendering, pixel value in Pixarray array
Free (image);
return 0;
}