1
/* tilp - a linking program for TI graphing calculators
2
* Copyright (C) 1999-2002 Romain Lievin
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30
#include "win32/unistd.h"
34
#include <libticables/typedefs.h>
35
#include <libticables/macros.h>
36
#include <libticables/verbose.h>
37
#include <glib/glib.h>
51
int write_bmp_2_colors(FILE *file, Image *img) //tested: OK (09/04/2002)
53
int ImageHeight, ImageWidth;
54
int RowBytes, PixelDepth, BytesPerLine;
55
int *ColorTable, NbBytesColorTable, NbColors;
57
unsigned char *BitsImage, *TrueBitsImage;
60
unsigned char *PtrSource, *PtrTarget, *PtrDummy;
62
BITMAPFILEHEADER BmpFileHeader;
63
BITMAPINFOHEADER BmpInfoHeader;
70
/* Image characteristics */
72
ImageWidth = img->width;
73
ImageHeight = img->height;
75
/* Compute parameters */
76
NbBytesImage = (ImageWidth * ImageHeight) / 8;
77
RowBytes = ImageWidth / 8;
78
NbColors = pow(2,PixelDepth);
79
NbBytesColorTable = NbColors * sizeof(int);
80
BytesPerLine = 4*((RowBytes+3)/4); /* Z! Modulo 4 */
83
BitsImage = img->bitmap;
84
ColorTable = (int*)malloc(NbBytesColorTable);
87
ColorTable[0] = 0x00ffffff;
88
ColorTable[1] = 0x00000000;
90
/* Initialize the fields in the BITMAPINFO structure */
91
BmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
92
BmpInfoHeader.biWidth = ImageWidth;
93
BmpInfoHeader.biHeight = ImageHeight; /* I can't use negative number, why ???? */
94
BmpInfoHeader.biPlanes = 1;
95
BmpInfoHeader.biBitCount = PixelDepth;
96
BmpInfoHeader.biCompression = BI_RGB;
97
BmpInfoHeader.biSizeImage = BytesPerLine*ImageHeight;
98
BmpInfoHeader.biXPelsPerMeter = 0;
99
BmpInfoHeader.biYPelsPerMeter = 0;
100
BmpInfoHeader.biClrUsed = NbColors;
101
BmpInfoHeader.biClrImportant = 0; /* All of the device colors are important. */
103
BmpFileHeader.bfType = 0x4d42; /* Signature 0x42="B" 0x4d="M" */
104
BmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
105
NbBytesColorTable + BmpInfoHeader.biSizeImage;
106
BmpFileHeader.bfReserved1 = 0;
107
BmpFileHeader.bfReserved2 = 0;
108
BmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + NbBytesColorTable;
111
Err = fwrite ((void*)&BmpFileHeader, sizeof(BITMAPFILEHEADER), 1, Stream);
112
Err = fwrite ((void*)&BmpInfoHeader, sizeof(BITMAPINFOHEADER), 1, Stream);
113
Err = fwrite ((void*)ColorTable, sizeof(int), NbColors , Stream);
115
/* Create (in memory) the image to be copied on file */
116
/* Working that way allow us tro save the array with fwrite function */
117
TrueBitsImage = (unsigned char*)calloc(BmpInfoHeader.biSizeImage+BytesPerLine,sizeof(unsigned char));
119
/* We copy each scan line already filled with 0 */
120
PtrSource = BitsImage;
121
PtrTarget = TrueBitsImage;
122
for (i=0; i<ImageHeight; i++){
123
memcpy (PtrTarget, PtrSource, RowBytes);
124
PtrTarget+=BytesPerLine;
128
/* I should not do the next few line but I'm not able to use negative value */
129
/* for biHeight property so I need to upside down the array by hand ... */
130
/* This code should be remove as soon as I can understand what happen */
133
/* PtrSource points to the beginning of the last scan line */
134
PtrSource = TrueBitsImage + ((ImageHeight-1)*BytesPerLine);
135
/* PtrTarget points to the beginning of the first scan line */
136
PtrTarget = TrueBitsImage;
137
PtrDummy = (unsigned char*)malloc(BytesPerLine);
138
for (i=0; i<ImageHeight/2; i++){
139
memcpy (PtrDummy, PtrTarget, BytesPerLine);
140
memcpy (PtrTarget, PtrSource, BytesPerLine);
141
memcpy (PtrSource, PtrDummy, BytesPerLine);
142
PtrTarget+=BytesPerLine;
143
PtrSource-=BytesPerLine;
147
/* Copy the array of color indices into the BMP file. */
148
Err = fwrite ((void*)TrueBitsImage, sizeof(unsigned char), BmpInfoHeader.biSizeImage, Stream);
157
Write a 256 colors bytemap
159
int write_bmp_256_colors(FILE *file, Image *img) //tested: NOK (09/04/2002)
161
int ImageHeight, ImageWidth;
162
int RowBytes, PixelDepth, BytesPerLine;
163
int *ColorTable, NbBytesColorTable, NbColors;
165
unsigned char *BitsImage, *TrueBitsImage;
168
unsigned char *PtrSource, *PtrTarget, *PtrDummy;
170
BITMAPFILEHEADER BmpFileHeader;
171
BITMAPINFOHEADER BmpInfoHeader;
175
/* Image characteristics */
177
ImageWidth = img->width;
178
ImageHeight = img->height;
180
/* Compute parameters */
181
NbBytesImage = (ImageWidth * ImageHeight);
182
RowBytes = ImageWidth;
183
NbColors = pow(2,PixelDepth);
184
NbBytesColorTable = NbColors * sizeof(int);
185
BytesPerLine = 4*((RowBytes+3)/4); /* Z! Modulo 4 */
188
BitsImage = img->bytemap;
189
ColorTable = (int*)malloc(NbBytesColorTable);
192
for(i=0; i<256; i++) {
194
unsigned char *cmp = img->colormap;
200
ColorTable[i] = (r << 16) | (g << 8) | b;
203
//ColorTable[0] = 0x000000;
204
//ColorTable[1] = 0xffffff;
206
/* Initialize the fields in the BITMAPINFO structure */
207
BmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
208
BmpInfoHeader.biWidth = ImageWidth;
209
BmpInfoHeader.biHeight = ImageHeight; /* I can't use negative number, why ???? */
210
BmpInfoHeader.biPlanes = 1;
211
BmpInfoHeader.biBitCount = PixelDepth;
212
BmpInfoHeader.biCompression = BI_RGB;
213
BmpInfoHeader.biSizeImage = BytesPerLine*ImageHeight;
214
BmpInfoHeader.biXPelsPerMeter = 0;
215
BmpInfoHeader.biYPelsPerMeter = 0;
216
BmpInfoHeader.biClrUsed = NbColors;
217
BmpInfoHeader.biClrImportant = 0; /* All of the device colors are important. */
219
BmpFileHeader.bfType = 0x4d42; /* Signature 0x42="B" 0x4d="M" */
220
BmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
221
NbBytesColorTable + BmpInfoHeader.biSizeImage;
222
BmpFileHeader.bfReserved1 = 0;
223
BmpFileHeader.bfReserved2 = 0;
224
BmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + NbBytesColorTable;
227
Err = fwrite ((void*)&BmpFileHeader, sizeof(BITMAPFILEHEADER), 1, Stream);
228
Err = fwrite ((void*)&BmpInfoHeader, sizeof(BITMAPINFOHEADER), 1, Stream);
229
Err = fwrite ((void*)ColorTable, sizeof(int), NbColors , Stream);
231
/* Create (in memory) the image to be copied on file */
232
/* Working that way allow us tro save the array with fwrite function */
233
TrueBitsImage = (unsigned char*)calloc(BmpInfoHeader.biSizeImage+BytesPerLine,sizeof(unsigned char));
236
/* We copy each scan line already filled with 0 */
237
PtrSource = BitsImage;
238
PtrTarget = TrueBitsImage;
239
for (i=0; i<ImageHeight; i++){
240
memcpy (PtrTarget, PtrSource, RowBytes);
241
PtrTarget+=BytesPerLine;
245
/* I should not do the next few line but I'm not able to use negative value */
246
/* for biHeight property so I need to upside down the array by hand ... */
247
/* This code should be remove as soon as I can understand what happen */
250
/* PtrSource points to the beginning of the last scan line */
251
PtrSource = TrueBitsImage + ((ImageHeight-1)*BytesPerLine);
252
/* PtrTarget points to the beginning of the first scan line */
253
PtrTarget = TrueBitsImage;
254
PtrDummy = (unsigned char*)malloc(BytesPerLine);
255
for (i=0; i<ImageHeight/2; i++){
256
memcpy (PtrDummy, PtrTarget, BytesPerLine);
257
memcpy (PtrTarget, PtrSource, BytesPerLine);
258
memcpy (PtrSource, PtrDummy, BytesPerLine);
259
PtrTarget+=BytesPerLine;
260
PtrSource-=BytesPerLine;
264
/* Copy the array of color indices into the BMP file. */
265
Err = fwrite ((void*)TrueBitsImage, sizeof(unsigned char), BmpInfoHeader.biSizeImage, Stream);
273
int write_bmp_format(FILE *file, Image *img)
275
if(img->encoding == IMG_COL_TYPE)
276
return write_bmp_256_colors(file, img);
278
return write_bmp_2_colors(file, img);
286
int read_bmp_2_colors(FILE *file, Image *img) //tested: NOK! (12/05)
292
int read_bmp_256_colors(FILE *file, Image *img) // to do ...
297
int read_bmp_format(FILE *file, Image *img)
299
if(img->encoding == IMG_COL_TYPE)
300
return read_bmp_256_colors(file, img);
302
return read_bmp_2_colors(file, img);