#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include “bmpfile.h”
#pragma pack(2)
typedef struct {
unsigned char bfType[2];
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BMPFileHeader;
#pragma pack(2)
typedef struct {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BMPInfoHeader;
typedef struct {
BMPFileHeader fileHeader;
BMPInfoHeader infoHeader;
int stride;
unsigned char *imageData;
} BMPImage;
void* bmpfile_create(int w, int h) {
BMPImage *bmpImage = malloc(sizeof(BMPImage));
if ( !bmpImage->imageData )
printf(" BMP image creation failed\n ");
bmpImage->fileHeader.bfType[0] = ‘B’;
bmpImage->fileHeader.bfType[1] = ‘M’;
bmpImage->fileHeader.bfSize = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + w * h * 3;
bmpImage->fileHeader.bfReserved1 = 0;
bmpImage->fileHeader.bfReserved2 = 0;
bmpImage->fileHeader.bfOffBits = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
bmpImage->infoHeader.biSize = sizeof(BMPInfoHeader);
bmpImage->infoHeader.biWidth = w;
bmpImage->infoHeader.biHeight = h;
bmpImage->infoHeader.biPlanes = 1;
bmpImage->infoHeader.biBitCount = 24;
bmpImage->infoHeader.biCompression = 0;
bmpImage->infoHeader.biSizeImage = w * h * 3;
bmpImage->infoHeader.biXPelsPerMeter = 0;
bmpImage->infoHeader.biYPelsPerMeter = 0;
bmpImage->infoHeader.biClrUsed = 0;
bmpImage->infoHeader.biClrImportant = 0;
bmpImage->stride = (((bmpImage->infoHeader.biWidth * bmpImage->infoHeader.biBitCount) + 31) / 32) * 4;
bmpImage->imageData = malloc(bmpImage->stride * bmpImage->infoHeader.biHeight * 3);
return bmpImage;
}
void bmpfile_destroy(void *ctx) {
BMPImage bmpImage = (BMPImage)ctx;
free(bmpImage->imageData);
free(bmpImage);
}
void* bmpfile_load(char *file) {
FILE *fp = fopen(file, “rb”);
if (fp == NULL) {
return NULL;
}
BMPImage *bmpImage = malloc(sizeof(BMPImage));
fread(&(bmpImage->fileHeader), sizeof(BMPFileHeader), 1, fp);
fread(&(bmpImage->infoHeader), sizeof(BMPInfoHeader), 1, fp);
bmpImage->imageData = malloc(bmpImage->infoHeader.biSizeImage);
fseek(fp, bmpImage->fileHeader.bfOffBits, SEEK_SET);
fread(bmpImage->imageData, bmpImage->infoHeader.biSizeImage, 1, fp);
fclose(fp);
return bmpImage;
}
int bmpfile_save(void *ctx, char *file) {
BMPImage bmpImage = (BMPImage)ctx;
FILE *fp = fopen(file, “wb”);
if (fp == NULL) {
return -1;
}
fwrite(&(bmpImage->fileHeader), sizeof(BMPFileHeader), 1, fp);
fwrite(&(bmpImage->infoHeader), sizeof(BMPInfoHeader), 1, fp);
fwrite(bmpImage->imageData, bmpImage->infoHeader.biSizeImage, 1, fp);
fclose(fp);
return 0;
}
void bmpfile_pixel(void *ctx, int x, int y, int c) {
if (ctx == NULL)
return;
BMPImage bmpImage = (BMPImage)ctx;
if ( x < 0 || x >= bmpImage->infoHeader.biWidth || y < 0 || y >= bmpImage->infoHeader.biHeight )
return;
int index = (bmpImage->infoHeader.biHeight - y - 1) * bmpImage->stride + x;// bmpImage->infoHeader.biWidth + x;//按照反的方向存储(从左向右,从下向上)图片的最左下角的像素是首先被存储在bmp文件中的,所以这里代码的最后是加+x
bmpImage->imageData[index * 3 + 0] = c & 0xFF;
bmpImage->imageData[index * 3 + 1] = (c >> 8) & 0xFF;
bmpImage->imageData[index * 3 + 2] = (c >> 16) & 0xFF;
}
int main() {
srand((unsigned)time(NULL));
int width = 400;
int height = 300;
void *ctx = bmpfile_create(width, height);
int i;
for (i = 0; i < 1000; i++) {
int x = rand() % width;
int y = rand() % height;
int r = rand() % 256;
int g = rand() % 256;
int b = rand() % 256;
int color = r | (g << 8) | (b << 16);
bmpfile_pixel(ctx, x, y, color);
}
bmpfile_save(ctx, "test.bmp");
bmpfile_destroy(ctx);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "bmpfile.h"
#pragma pack(2)
typedef struct
{
unsigned char bfType[2];
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BMPFileHeader;
#pragma pack(2)
typedef struct
{
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BMPInfoHeader;
typedef struct
{
BMPFileHeader fileHeader;
BMPInfoHeader infoHeader;
int stride;
unsigned char *imageData;
} BMPImage;
void *bmpfile_create(int w, int h)
{
BMPImage *bmpImage = malloc(sizeof(BMPImage));
if (!bmpImage->imageData)
printf(" BMP image creation failed\n ");
bmpImage->fileHeader.bfType[0] = 'B';
bmpImage->fileHeader.bfType[1] = 'M';
bmpImage->fileHeader.bfSize = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + w * h * 3;
bmpImage->fileHeader.bfReserved1 = 0;
bmpImage->fileHeader.bfReserved2 = 0;
bmpImage->fileHeader.bfOffBits = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
bmpImage->infoHeader.biSize = sizeof(BMPInfoHeader);
bmpImage->infoHeader.biWidth = w;
bmpImage->infoHeader.biHeight = h;
bmpImage->infoHeader.biPlanes = 1;
bmpImage->infoHeader.biBitCount = 24;
bmpImage->infoHeader.biCompression = 0;
bmpImage->infoHeader.biSizeImage = w * h * 3;
bmpImage->infoHeader.biXPelsPerMeter = 0;
bmpImage->infoHeader.biYPelsPerMeter = 0;
bmpImage->infoHeader.biClrUsed = 0;
bmpImage->infoHeader.biClrImportant = 0;
bmpImage->stride = (((bmpImage->infoHeader.biWidth * bmpImage->infoHeader.biBitCount) + 31) / 32) * 4;
bmpImage->imageData = malloc(bmpImage->stride * bmpImage->infoHeader.biHeight * 3);
return bmpImage;
}
void bmpfile_destroy(void *ctx)
{
BMPImage *bmpImage = (BMPImage *)ctx;
free(bmpImage->imageData);
free(bmpImage);
}
void *bmpfile_load(char *file)
{
FILE *fp = fopen(file, "rb");
if (fp == NULL)
{
return NULL;
}
BMPImage *bmpImage = malloc(sizeof(BMPImage));
fread(&(bmpImage->fileHeader), sizeof(BMPFileHeader), 1, fp);
fread(&(bmpImage->infoHeader), sizeof(BMPInfoHeader), 1, fp);
bmpImage->imageData = malloc(bmpImage->infoHeader.biSizeImage);
fseek(fp, bmpImage->fileHeader.bfOffBits, SEEK_SET);
fread(bmpImage->imageData, bmpImage->infoHeader.biSizeImage, 1, fp);
fclose(fp);
return bmpImage;
}
int bmpfile_save(void *ctx, char *file)
{
BMPImage *bmpImage = (BMPImage *)ctx;
FILE *fp = fopen(file, "wb");
if (fp == NULL)
{
return -1;
}
fwrite(&(bmpImage->fileHeader), sizeof(BMPFileHeader), 1, fp);
fwrite(&(bmpImage->infoHeader), sizeof(BMPInfoHeader), 1, fp);
fwrite(bmpImage->imageData, bmpImage->infoHeader.biSizeImage, 1, fp);
fclose(fp);
return 0;
}
void bmpfile_pixel(void *ctx, int x, int y, int c)
{
if (ctx == NULL)
return;
BMPImage *bmpImage = (BMPImage *)ctx;
if (x < 0 || x >= bmpImage->infoHeader.biWidth || y < 0 || y >= bmpImage->infoHeader.biHeight)
return;
// int index = (bmpImage->infoHeader.biHeight - y - 1) * bmpImage->stride + x;
// bmpImage->imageData[index * 3 + 0] = c & 0xFF;
// bmpImage->imageData[index * 3 + 1] = (c >> 8) & 0xFF;
// bmpImage->imageData[index * 3 + 2] = (c >> 16) & 0xFF;
bmpImage->imageData[y * bmpImage->stride + x * 3 + 0] = (int)(c >> 0);
bmpImage->imageData[y * bmpImage->stride + x * 3 + 0] = (int)(c >> 8);
bmpImage->imageData[y * bmpImage->stride + x * 3 + 0] = (int)(c >> 16);
// if (ctx == NULL)
// return;
// BMPImage *BMP = (BMPImage *)ctx;
// if (x < 0 || x >= BMP->infoHeader.biWidth || y < 0 || y >= (BMP->infoHeader.biHeight))
// return;
// int *pbyte = BMP->imageData;
// pbyte[y * BMP->stride + x * 3 + 0] = (int)(c >> 0);
// pbyte[y * BMP->stride + x * 3 + 1] = (int)(c >> 8);
// pbyte[y * BMP->stride + x * 3 + 2] = (int)(c >> 16);
}
// int main()
// {
// srand((unsigned)time(NULL));
// int width = 400;
// int height = 300;
// void *ctx = bmpfile_create(width, height);
// int i;
// for (i = 0; i < 1000; i++)
// {
// int x = rand() % width;
// int y = rand() % height;
// int r = rand() % 256;
// int g = rand() % 256;
// int b = rand() % 256;
// int color = r | (g << 8) | (b << 16);
// bmpfile_pixel(ctx, x, y, color);
// }
// bmpfile_save(ctx, "test.bmp");
// bmpfile_destroy(ctx);
// return 0;
// }
实现基于点阵字库的中英文混合显示,在 BMP 上输出自己的名字和 APICAL(字库文件在 006/font 目录下)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmpfile.h"
void printHZK(void *ctx, int qm, int wm, int x) {
// BMPImage *bmp = (BMPImage*)ctx;
FILE *HZK = fopen("./font/HZK1616.dat", "rb");
if(HZK == NULL) {
printf("Can not open HZK16\n");
return;
}
int offset = (94 * (qm - 1) + (wm - 1)) * 32;
char mat[16][2];
fseek(HZK, offset, SEEK_SET);
fread(mat, 32, 1, HZK);
for(int i = 0; i < 16; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 8; k++) {
if(mat[15 - i][j]&(0x80>>k)) {
bmpfile_pixel(ctx, j * 8 + k + 20 * x + 200, 200 + i, 0xff0000);
// printf("#");
} else {
// printf("-");
}
}
}
printf("\n");
}
fclose(HZK);
}
void printAsc(void *ctx, unsigned char *c, int x, int y, int d)
{
// void *bmp = (BMPImage*)ctx;
int i, j;
FILE *ASC;
if ((ASC = fopen("./font/ASC0814.dat", "rb")) == NULL)
{
printf("Can't open asc file!");
return ;
}
unsigned char buf[14];
int offset = *(c) * 14 + 1;
printf("%d\n",offset);
fseek(ASC, offset, SEEK_SET);
fread(buf, 14, 1, ASC);
printf("%d\n", offset);
for (i = 0; i < 14; i++)
{
for (j = 0; j < 8; j++)
{
if (buf[i] & (0x80 >> j))
{
bmpfile_pixel(ctx, j + x + d * 16, 14-i + y, 0xff00ff);
}
}
}
}
int main() {
unsigned char *str = "袁子玄";
unsigned char *Eng = "APICAL";
int w = 500, h = 600;
void *bmp = bmpfile_create(w, h);
int len = strlen(str);
// printf("%d %d\n",len, len1);
printf("%d %d %d\n", str[0], str[2], str[3]);
for(int x = 0; x < len; x += 2) { //汉字
int qm = str[x] - 0xa0;
int wm = str[x + 1] - 0xa0;
printHZK(bmp, qm, wm, x);
}
int len1 = strlen(Eng);
for(int i = 0; i < len1; i++) { //英文
printAsc(bmp, &Eng[i], 100, 41, i);
}
bmpfile_save(bmp, "bmpfont.bmp");
bmpfile_destroy(bmp);
return 0;
}