Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / bitmap.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Bitmap File Format Utils
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <freerdp/types.h>
24
25 #include <freerdp/utils/bitmap.h>
26
27 typedef struct
28 {
29         uint8 magic[2];
30 } bmpfile_magic;
31
32 typedef struct
33 {
34         uint32 filesz;
35         uint16 creator1;
36         uint16 creator2;
37         uint32 bmp_offset;
38 } bmpfile_header;
39
40 typedef struct
41 {
42         uint32 header_sz;
43         sint32 width;
44         sint32 height;
45         uint16 nplanes;
46         uint16 bitspp;
47         uint32 compress_type;
48         uint32 bmp_bytesz;
49         sint32 hres;
50         sint32 vres;
51         uint32 ncolors;
52         uint32 nimpcolors;
53 } BITMAPINFOHEADER;
54
55 void freerdp_bitmap_write(char* filename, void* data, int width, int height, int bpp)
56 {
57         FILE* fp;
58         bmpfile_magic magic;
59         bmpfile_header header;
60         BITMAPINFOHEADER info_header;
61
62         fp = fopen(filename, "w+b");
63
64         if (fp == NULL)
65         {
66                 printf("failed to open file %s\n", filename);
67                 return;
68         }
69
70         magic.magic[0] = 'B';
71         magic.magic[1] = 'M';
72
73         header.creator1 = 0;
74         header.creator2 = 0;
75
76         header.bmp_offset =
77                         sizeof(bmpfile_magic) +
78                         sizeof(bmpfile_header) +
79                         sizeof(BITMAPINFOHEADER);
80
81         info_header.bmp_bytesz = width * height * (bpp / 8);
82
83         header.filesz =
84                 header.bmp_offset +
85                 info_header.bmp_bytesz;
86
87         info_header.width = width;
88         info_header.height = (-1) * height;
89         info_header.nplanes = 1;
90         info_header.bitspp = bpp;
91         info_header.compress_type = 0;
92         info_header.hres = width;
93         info_header.vres = height;
94         info_header.ncolors = 0;
95         info_header.nimpcolors = 0;
96         info_header.header_sz = sizeof(BITMAPINFOHEADER);
97
98         fwrite((void*) &magic, sizeof(bmpfile_magic), 1, fp);
99         fwrite((void*) &header, sizeof(bmpfile_header), 1, fp);
100         fwrite((void*) &info_header, sizeof(BITMAPINFOHEADER), 1, fp);
101         fwrite((void*) data, info_header.bmp_bytesz, 1, fp);
102
103         fclose(fp);
104 }
105