Tscope5
bitmaps.c
Go to the documentation of this file.
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 #include "../include/tscope5/bitmaps.h"
13 #include "../include/tscope5/display.h"
14 #include "../include/tscope5/graphics.h"
15 #include "../include/tscope5/bitmaps_internal.h"
16 #include "../include/tscope5/system_internal.h"
17 #include "../include/tscope5/graphics_internal.h"
18 
19 
26 
27 
28 
29 
40 TS5_BITMAP *ts5_alloc_bitmap(double w, double h)
41 {
42  ts5_check_bitmaps("ts5_alloc_bitmap");
43  ts5_log(TS5_LOGLEVEL_2, "ts5_alloc_bitmap(%f,%f)\n", w, h);
44 
45  if (_ts5_status.graphics_status.coordinate_scale==TS5_RELATIVE_COORDINATES) {
46  w *= _ts5_status.graphics_status.target_width;
47  h *= _ts5_status.graphics_status.target_height;
48  }
49 
50  TS5_BITMAP *map;
51  map = al_create_bitmap(w, h);
52 
53  if (map == NULL) {
54  ts5_fatal("ts5_alloc_bitmap: could not make bitmap\n");
55  }
56 
57  TS5_BITMAP *oldval = ts5_set_drawing_target(map);
59  ts5_set_drawing_target(oldval);
60 
61  return map;
62 }
63 
64 
74 TS5_BITMAP *ts5_read_bitmap(const char *file)
75 {
76  ts5_check_bitmaps("ts5_read_bitmap");
77  ts5_log(TS5_LOGLEVEL_2, "ts5_read_bitmap(%s)\n", file);
78 
79  TS5_BITMAP *map;
80  map = al_load_bitmap(file);
81 
82  if (map == NULL) {
83  ts5_fatal("ts5_read_bitmap: could not read bitmap\n");
84  }
85 
86  al_convert_bitmap(map);
87 
88  return map;
89 }
90 
91 
104 void ts5_write_bitmap(const char *file, TS5_BITMAP *map)
105 {
106  ts5_check_bitmaps("ts5_write_bitmap");
107  ts5_log(TS5_LOGLEVEL_2, "ts5_write_bitmap(%s,%p)\n", file, map);
108 
109  if (!al_save_bitmap(file, map)) {
110  ts5_fatal("ts5_write_bitmap: could not write bitmap\n");
111  }
112 }
113 
114 
123 void ts5_free_bitmap(TS5_BITMAP *map)
124 {
125  ts5_check_bitmaps("ts5_free_bitmap");
126  ts5_log(TS5_LOGLEVEL_2, "ts5_free_bitmap(%p)\n", map);
127 
128  al_destroy_bitmap(map);
129  map = NULL;
130 }
131 
132 
134 
135 
136 
137 
141 
142 
143 
144 
156 void ts5_get_bitmap_size(TS5_BITMAP *map, double *w, double *h)
157 {
158  ts5_check_bitmaps("ts5_get_bitmap_size");
159  ts5_log(TS5_LOGLEVEL_5, "ts5_get_bitmap_size(%p,%p,%p)\n", map, w, h);
160 
161  if (!map) {
162  ts5_fatal("ts5_get_bitmap_size: bitmap pointer is null\n");
163  }
164 
165  double ww, hh;
166 
167  ww = al_get_bitmap_width(map);
168  hh = al_get_bitmap_height(map);
169 
170  if (_ts5_status.graphics_status.coordinate_scale==TS5_RELATIVE_COORDINATES) {
171  ww /= _ts5_status.graphics_status.target_width;
172  hh /= _ts5_status.graphics_status.target_height;
173  }
174 
175  if (w) {
176  *w = ww;
177  }
178 
179  if (h) {
180  *h = hh;
181  }
182 }
183 
184 
192 double ts5_get_bitmap_width(TS5_BITMAP *map)
193 {
194  ts5_check_bitmaps("ts5_get_bitmap_width");
195  ts5_log(TS5_LOGLEVEL_5, "ts5_get_bitmap_width(%p)\n", map);
196 
197  double w;
198  ts5_get_bitmap_size(map, &w, NULL);
199 
200  return w;
201 }
202 
203 
211 double ts5_get_bitmap_height(TS5_BITMAP *map)
212 {
213  ts5_check_bitmaps("ts5_get_bitmap_height");
214  ts5_log(TS5_LOGLEVEL_5, "ts5_get_bitmap_width(%d)\n", map);
215 
216  double h;
217  ts5_get_bitmap_size(map, NULL, &h);
218 
219  return h;
220 }
221 
222 
224 
225 
226 
227 
235 
236 
237 
238 
246 void ts5_draw_bitmap(TS5_BITMAP *map, double dx, double dy)
247 {
248  ts5_check_bitmaps2("ts5_draw_bitmap", map);
249  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_bitmap(%p,%f,%f)\n", map, dx, dy);
250 
251  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
254  }
255 
256  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
257  dx = ts5_cartesian_to_display_coordinate_x(dx) - al_get_bitmap_width(map) / 2.0;
258  dy = ts5_cartesian_to_display_coordinate_y(dy) - al_get_bitmap_height(map) / 2.0;
259  }
260 
261  al_draw_bitmap(map, dx, dy, 0);
262 }
263 
264 
273 void ts5_draw_flipped_bitmap(TS5_BITMAP *map, double dx, double dy, const int flip)
274 {
275  ts5_check_bitmaps2("ts5_draw_flipped_bitmap", map);
276  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_flipped_bitmap(%p,%f,%f,%d)\n", map, dx, dy, flip);
277 
278  if (flip != TS5_FLIP_HORIZONTAL && flip != TS5_FLIP_VERTICAL && flip != TS5_FLIP_BOTH) {
279  ts5_fatal("ts5_draw_flipped_bitmap: flip should be",
280  " TS5_FLIP_HORIZONTAL, TS5_FLIP_VERTICAL or TS5_FLIP_BOTH\n");
281  }
282 
283  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
286  }
287 
288  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
289  dx = ts5_cartesian_to_display_coordinate_x(dx) - al_get_bitmap_width(map) / 2.0;
290  dy = ts5_cartesian_to_display_coordinate_y(dy) - al_get_bitmap_height(map) / 2.0;
291  }
292 
293  al_draw_bitmap(map, dx, dy, flip);
294 }
295 
296 
305 void ts5_draw_tinted_bitmap(TS5_BITMAP *map, TS5_COLOR tint, double dx, double dy)
306 {
307  ts5_check_bitmaps2("ts5_draw_tinted_bitmap", map);
308  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_tinted_bitmap(%p,(%f,%f,%f,%f),%f,%f)\n",
309  map, tint.r, tint.g, tint.b, tint.a, dx, dy);
310 
311  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
314  }
315 
316  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
317  dx = ts5_cartesian_to_display_coordinate_x(dx) - al_get_bitmap_width(map) / 2.0;
318  dy = ts5_cartesian_to_display_coordinate_y(dy) - al_get_bitmap_height(map) / 2.0;
319  }
320 
321  al_draw_tinted_bitmap(map, tint, dx, dy, 0);
322 }
323 
324 
336 void ts5_draw_bitmap_region(TS5_BITMAP *map, double sx1, double sy1, double sx2, double sy2,
337  double dx, double dy)
338 {
339  ts5_check_bitmaps2("ts5_draw_bitmap_region", map);
340  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_bitmap_region(%p,%f,%f,%f,%f,%f,%f)\n",
341  map, sx1, sy1, sx2, sy2, dx, dy);
342 
343  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
346  TS5_BITMAP *oldval = ts5_set_drawing_target (map);
351  ts5_set_drawing_target(oldval);
352  }
353 
354  double width = abs(sx2 - sx1);
355  double height = abs(sy2 - sy1);
356  double x = sx1 < sx2 ? sx1 : sx2;
357  double y = sy1 < sy2 ? sy1 : sy2;
358 
359  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
360  dx = ts5_cartesian_to_display_coordinate_x(dx - width / 2.0);
361  dy = ts5_cartesian_to_display_coordinate_y(height / 2.0 - dy);
362  x = x + al_get_bitmap_width(map) / 2.0;
363  y = y + al_get_bitmap_height(map) / 2.0;
364  }
365 
366  al_draw_bitmap_region(map, x, y, width, height, dx, dy, 0);
367 }
368 
369 
382 void ts5_draw_rotated_bitmap(TS5_BITMAP *map, double cx, double cy, double angle, double dx, double dy)
383 {
384  ts5_check_bitmaps2("ts5_draw_rotated_bitmap", map);
385  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_rotated_bitmap(%p,%f,%f,%f,%f,%f)\n", map, cx, cy, angle, dx, dy);
386 
387  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
390  TS5_BITMAP *oldval = ts5_set_drawing_target (map);
393  ts5_set_drawing_target(oldval);
394  }
395 
396  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
399  cx = al_get_bitmap_width(map) / 2.0 + cx;
400  cy = al_get_bitmap_height(map) / 2.0 - cy;
401  }
402 
403  angle = angle * TS5_PI / 180.0;
404 
405  al_draw_rotated_bitmap(map, cx, cy, dx, dy, angle, 0);
406 }
407 
408 
418 void ts5_draw_scaled_bitmap(TS5_BITMAP *map, double xratio, double yratio, double dx, double dy)
419 {
420  ts5_check_bitmaps2("ts5_draw_scaled_bitmap", map);
421  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_scaled_bitmap(%p,%f,%f,%f,%f)\n", map, xratio, yratio, dx, dy);
422 
423  double h = al_get_bitmap_height(map);
424  double w = al_get_bitmap_width(map);
425 
426  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
429  }
430 
431  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
432  dx = ts5_cartesian_to_display_coordinate_x(dx) - w * xratio / 2.0;
433  dy = ts5_cartesian_to_display_coordinate_y(dy) - h * yratio / 2.0;
434  }
435 
436  al_draw_scaled_bitmap(map, 0.0, 0.0, w, h, dx, dy, w * xratio, h * yratio, 0);
437 }
438