~ubuntu-branches/ubuntu/edgy/tilp/edgy

« back to all changes in this revision

Viewing changes to src/img/bmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien BLACHE
  • Date: 2004-05-22 21:12:03 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040522211203-awg2cuw03guyvyz9
Tags: 6.72-2
* debian/control
  + Build-Depends: libticables3 (>= 3.8.4-1).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  tilp - a linking program for TI graphing calculators
2
 
 *  Copyright (C) 1999-2002  Romain Lievin
3
 
 *
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.
8
 
 *
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.
13
 
 *
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.
17
 
 */
18
 
 
19
 
#ifdef HAVE_CONFIG_H
20
 
#include <config.h>
21
 
#endif
22
 
 
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#include <math.h>
26
 
 
27
 
#ifdef HAVE_UNISTD_H
28
 
#include <unistd.h>
29
 
#else
30
 
#include "win32/unistd.h"
31
 
#endif
32
 
 
33
 
#ifdef __MACOSX__
34
 
#include <libticables/typedefs.h>
35
 
#include <libticables/macros.h>
36
 
#include <libticables/verbose.h>
37
 
#include <glib/glib.h>
38
 
#else
39
 
#include "tilibs.h"
40
 
#include <glib.h>
41
 
#endif
42
 
 
43
 
#include "img_fmt.h"
44
 
#include "bmpfile.h"
45
 
 
46
 
 
47
 
/***********/
48
 
/* Writing */
49
 
/***********/
50
 
 
51
 
int write_bmp_2_colors(FILE *file, Image *img) //tested: OK (09/04/2002)
52
 
{
53
 
  int           ImageHeight, ImageWidth;
54
 
  int           RowBytes, PixelDepth, BytesPerLine;
55
 
  int           *ColorTable, NbBytesColorTable, NbColors;
56
 
  int           NbBytesImage;
57
 
  unsigned char *BitsImage, *TrueBitsImage;
58
 
  int           Err;
59
 
  int           i;     
60
 
  unsigned char *PtrSource, *PtrTarget, *PtrDummy;
61
 
  
62
 
  BITMAPFILEHEADER    BmpFileHeader;  
63
 
  BITMAPINFOHEADER    BmpInfoHeader; 
64
 
 
65
 
  FILE *Stream = file;
66
 
  
67
 
  /* Invert bitmap */
68
 
  invert_bitmap(img);
69
 
  
70
 
  /* Image characteristics */
71
 
  PixelDepth = 1;
72
 
  ImageWidth = img->width;
73
 
  ImageHeight = img->height;
74
 
  
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 */
81
 
  
82
 
  /* Alloc mermory */
83
 
  BitsImage = img->bitmap;
84
 
  ColorTable = (int*)malloc(NbBytesColorTable);
85
 
  
86
 
  /* Fill */
87
 
  ColorTable[0] = 0x00ffffff;
88
 
  ColorTable[1] = 0x00000000;
89
 
  
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.  */
102
 
  
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;
109
 
  
110
 
  
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);
114
 
  
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)); 
118
 
  
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;
125
 
    PtrSource+=RowBytes; 
126
 
  }
127
 
  
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       */
131
 
  /* with biHeight                                                            */
132
 
  
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;
144
 
  }
145
 
  free(PtrDummy);
146
 
  
147
 
  /* Copy the array of color indices into the BMP file. */
148
 
  Err = fwrite ((void*)TrueBitsImage, sizeof(unsigned char), BmpInfoHeader.biSizeImage, Stream);  
149
 
  
150
 
  free(TrueBitsImage);
151
 
  free(ColorTable);   
152
 
  
153
 
  return 0;
154
 
}
155
 
 
156
 
/*
157
 
  Write a 256 colors bytemap
158
 
*/
159
 
int write_bmp_256_colors(FILE *file, Image *img) //tested: NOK (09/04/2002)
160
 
{
161
 
  int           ImageHeight, ImageWidth;
162
 
  int           RowBytes, PixelDepth, BytesPerLine;
163
 
  int           *ColorTable, NbBytesColorTable, NbColors;
164
 
  int           NbBytesImage;
165
 
  unsigned char *BitsImage, *TrueBitsImage;
166
 
  int           Err;
167
 
  int           i;     
168
 
  unsigned char *PtrSource, *PtrTarget, *PtrDummy;
169
 
  
170
 
  BITMAPFILEHEADER    BmpFileHeader;  
171
 
  BITMAPINFOHEADER    BmpInfoHeader; 
172
 
 
173
 
  FILE *Stream = file;
174
 
  
175
 
  /* Image characteristics */
176
 
  PixelDepth = 8;
177
 
  ImageWidth = img->width;
178
 
  ImageHeight = img->height;
179
 
  
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 */
186
 
  
187
 
  /* Alloc mermory */
188
 
  BitsImage = img->bytemap;
189
 
  ColorTable = (int*)malloc(NbBytesColorTable);
190
 
  
191
 
  /* Fill color map */
192
 
        for(i=0; i<256; i++) {
193
 
    int r, g, b;
194
 
    unsigned char *cmp = img->colormap;
195
 
    
196
 
    r = cmp[3*i+0];
197
 
    g = cmp[3*i+1];
198
 
    b = cmp[3*i+2];
199
 
    
200
 
    ColorTable[i] = (r << 16) | (g << 8) | b;
201
 
  }
202
 
 
203
 
        //ColorTable[0] = 0x000000;
204
 
        //ColorTable[1] = 0xffffff;
205
 
  
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.  */
218
 
  
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;
225
 
  
226
 
  
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);
230
 
  
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)); 
234
 
  
235
 
  
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;
242
 
    PtrSource+=RowBytes; 
243
 
  }
244
 
  
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       */
248
 
  /* with biHeight                                                            */
249
 
  
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;
261
 
  }
262
 
  free(PtrDummy);
263
 
  
264
 
  /* Copy the array of color indices into the BMP file. */
265
 
  Err = fwrite ((void*)TrueBitsImage, sizeof(unsigned char), BmpInfoHeader.biSizeImage, Stream);  
266
 
  
267
 
  free(TrueBitsImage);
268
 
  free(ColorTable);
269
 
  
270
 
  return 0;
271
 
}
272
 
 
273
 
int write_bmp_format(FILE *file, Image *img)
274
 
{
275
 
  if(img->encoding == IMG_COL_TYPE)
276
 
    return write_bmp_256_colors(file, img);
277
 
  else
278
 
    return write_bmp_2_colors(file, img);
279
 
}
280
 
 
281
 
 
282
 
/***********/
283
 
/* Reading */
284
 
/***********/
285
 
 
286
 
int read_bmp_2_colors(FILE *file, Image *img) //tested: NOK! (12/05)
287
 
{
288
 
  return 0;
289
 
}
290
 
 
291
 
 
292
 
int read_bmp_256_colors(FILE *file, Image *img) // to do ...
293
 
{
294
 
  return 0;
295
 
}
296
 
 
297
 
int read_bmp_format(FILE *file, Image *img)
298
 
{
299
 
  if(img->encoding == IMG_COL_TYPE)
300
 
    return read_bmp_256_colors(file, img);
301
 
  else
302
 
    return read_bmp_2_colors(file, img);
303
 
}