1 #include <stdio.h>
2 #include <string.h>
3 #include "imagelib.h"
4 #include "cards.h"
5 
6 image_list *image_root = 0;
7 
8 int
register_imagelib(image_list * images)9 register_imagelib (image_list *images)
10 {
11   int i, j, k;
12   for (i=0; images[i].name; i++)
13     {
14       if (images[i].next)
15 	continue;
16       images[i].next = image_root;
17       image_root = images+i;
18       for (j=0; j<3; j++)
19 	if (images[i].subimage[j])
20 	  for (k=0; images[i].subimage[j][k].width; k++)
21 	    {
22 	      if (images[i].subimage[j][k+1].width)
23 		images[i].subimage[j][k].next = images[i].subimage[j] + k + 1;
24 	      images[i].subimage[j][k].list = images+i;
25 	      images[i].subimage[j][k].type = j;
26 	    }
27     }
28 }
29 
30 static int type_search[3][3] = {
31   { TABLE_MONO,  TABLE_GRAY, TABLE_COLOR },
32   { TABLE_GRAY,  TABLE_MONO, TABLE_COLOR },
33   { TABLE_COLOR, TABLE_GRAY, TABLE_MONO  }};
34 
35 #define abs(a) ((a) < 0 ? -(a) : (a))
36 
37 image *
get_image(char * name,int preferred_width,int preferred_height,int flags)38 get_image (char *name,
39 	   int preferred_width,
40 	   int preferred_height,
41 	   int flags)
42 {
43   image_list *list;
44   image *img, *best;
45   int type, best_w, best_h;
46 
47   for (list=image_root; list; list=list->next)
48     if (strcmp(name, list->name) == 0)
49       break;
50   if (!list)
51     {
52       printf("No image named `%s' available\n", name);
53       return 0;
54     }
55   if (list->synth_func)
56     return list->synth_func (list, table_type, preferred_width, preferred_height);
57 
58   best = 0;
59   best_w = 0;
60   best_h = 0;
61   for (type = 0; type<3; type++)
62     {
63       for (img = list->subimage[type_search[table_type][type]]; img; img=img->next)
64 	{
65 	  if (flags & GI_NOT_BIGGER)
66 	    {
67 	      if (img->width > best_w && img->width <= preferred_width
68 		  && img->height > best_h && img->height <= preferred_height)
69 		{
70 		  best = img;
71 		  best_w = img->width;
72 		  best_h = img->height;
73 		}
74 	    }
75 	  else
76 	    {
77 	      int diff = abs(preferred_width - img->width) + abs(preferred_height - img->height);
78 	      if (diff < best_w || !best)
79 		{
80 		  best = img;
81 		  best_w = diff;
82 		}
83 	    }
84 	}
85       if (best && !(flags & GI_ANY_TYPE))
86 	break;
87     }
88 
89   return best;
90 }
91 
put_subimage(image * src,int col,int row,image * dest,int dx,int dy,int flags)92 void put_subimage (image *src, int col, int row,
93 		   image *dest, int dx, int dy, int flags)
94 {
95   int x, y, w, h;
96   if (src->list->across == 1 && src->list->down == 1)
97     col = row = 0;
98   w = src->width / src->list->across;
99   h = src->height / src->list->down;
100   x = w * col;
101   y = h * row;
102   put_image (src, x, y, w, h, dest, dx-x, dy-y, flags);
103 }
104 
105 image *
alloc_synth_image(image_list * list,int width,int height,int type)106 alloc_synth_image (image_list *list, int width, int height, int type)
107 {
108   image *rv;
109   rv = (image *)malloc (sizeof(image));
110   rv->width = width;
111   rv->height = height;
112   rv->file_data = 0;
113   rv->next = list->subimage[type];
114   list->subimage[type] = rv;
115   rv->type = type;
116   rv->pixels = 0;
117   rv->list = list;
118   return rv;
119 }
120