标签:style class blog code color width
我突然很喜欢发明轮子。。
早就想实现一下图片解码器和声音解码器。
bmp的图片没有压缩,解码最简单,今天下午研究了一下bmp的编码,然后写了个24位色的解码器。(24位色的bmp没有调色板,8位和16位都有)。
代码很简单,直接就能看懂,所以不说很多,直接贴了。。
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <stdio.h> 4 #include <Windows.h> 5 #include <gl/freeglut.h> 6 7 float colorR[2048][2048] = { 0 }; 8 float colorG[2048][2048] = { 0 }; 9 float colorB[2048][2048] = { 0 }; 10 unsigned int width = 0; 11 unsigned int height = 0; 12 13 //从data的index开始,把前面count个字节(char)转化成int 14 unsigned int transByteToInt(char* data, int index, int count) 15 { 16 int res = data[index] & 0xff; 17 for (int i = 1; i < count; i++) { 18 res <<= 8; 19 res |= data[index - i] & 0xff; 20 } 21 return res; 22 } 23 24 void readBmp() 25 { 26 FILE* bmpFile = fopen("F:/test.bmp", "rb"); 27 char bmpHeader[14]; 28 char infoHeader[40]; 29 fread(bmpHeader, 1, 14, bmpFile); 30 fread(infoHeader, 1, 40, bmpFile); 31 width = transByteToInt(infoHeader, 7, 4); 32 height = transByteToInt(infoHeader, 11, 4); 33 int bitCount = transByteToInt(infoHeader, 15, 2); 34 int sizeImage = transByteToInt(infoHeader, 23, 4); 35 char color[3] = { 0 }; 36 for (int h = height - 1; h >= 0; --h) { 37 for (int w = 0; w < width; ++w) { 38 fread(color, 1, 3, bmpFile); 39 colorR[h][w] = (color[2] & 0xff) / 255.0f; 40 colorG[h][w] = (color[1] & 0xff) / 255.0f; 41 colorB[h][w] = (color[0] & 0xff) / 255.0f; 42 } 43 } 44 fclose(bmpFile); 45 } 46 47 void glInit() 48 { 49 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 50 glMatrixMode(GL_PROJECTION); 51 glLoadIdentity(); 52 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 53 } 54 55 void display() 56 { 57 glClear(GL_COLOR_BUFFER_BIT); 58 glBegin(GL_POINTS); 59 for (int i = 0; i < width; ++i) { 60 for (int j = 0; j < height; ++j) { 61 glColor3f(colorR[height - j][i], colorG[height - j][i], colorB[height - j][i]); 62 glVertex3f((float)i / width, (float)j / height, 0.0f); 63 } 64 } 65 glEnd(); 66 glFlush(); 67 } 68 69 int main(int argc, char* argv[]) 70 { 71 readBmp(); 72 glutInit(&argc, argv); 73 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 74 glutInitWindowPosition(0, 0); 75 glutInitWindowSize(width, height); 76 glutCreateWindow("bmp - Anti-Magic"); 77 glInit(); 78 glutDisplayFunc(display); 79 glutMainLoop(); 80 return 0; 81 }
C语言和OpenGL实现的24位色BMP解码器,布布扣,bubuko.com
标签:style class blog code color width
原文地址:http://www.cnblogs.com/wolfred7464/p/3799840.html