1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <string.h>
5 #include <assert.h>
6 
7 #include "readers.h"
8 
9 /* ----------------------------------------------------------------------- */
10 
load_bits_lsb(unsigned char * dst,unsigned char * src,int width,int on,int off)11 void load_bits_lsb(unsigned char *dst, unsigned char *src, int width,
12 		   int on, int off)
13 {
14     int i,mask,bit;
15 
16     for (i = 0; i < width; i++) {
17 	mask = 1 << (i & 0x07);
18 	bit  = src[i>>3] & mask;
19 	dst[0] = bit ? on : off;
20 	dst[1] = bit ? on : off;
21 	dst[2] = bit ? on : off;
22 	dst += 3;
23     }
24 }
25 
load_bits_msb(unsigned char * dst,unsigned char * src,int width,int on,int off)26 void load_bits_msb(unsigned char *dst, unsigned char *src, int width,
27 		   int on, int off)
28 {
29     int i,mask,bit;
30 
31     for (i = 0; i < width; i++) {
32 	mask = 1 << (7 - (i & 0x07));
33 	bit  = src[i>>3] & mask;
34 	dst[0] = bit ? on : off;
35 	dst[1] = bit ? on : off;
36 	dst[2] = bit ? on : off;
37 	dst += 3;
38     }
39 }
40 
load_gray(unsigned char * dst,unsigned char * src,int width)41 void load_gray(unsigned char *dst, unsigned char *src, int width)
42 {
43     int i;
44 
45     for (i = 0; i < width; i++) {
46 	dst[0] = src[0];
47 	dst[1] = src[0];
48 	dst[2] = src[0];
49 	dst += 3;
50 	src += 1;
51     }
52 }
53 
load_graya(unsigned char * dst,unsigned char * src,int width)54 void load_graya(unsigned char *dst, unsigned char *src, int width)
55 {
56     int i;
57 
58     for (i = 0; i < width; i++) {
59 	dst[0] = src[0];
60 	dst[1] = src[0];
61 	dst[2] = src[0];
62 	dst += 3;
63 	src += 2;
64     }
65 }
66 
load_rgba(unsigned char * dst,unsigned char * src,int width)67 void load_rgba(unsigned char *dst, unsigned char *src, int width)
68 {
69     int i;
70 
71     for (i = 0; i < width; i++) {
72 	dst[0] = src[0];
73 	dst[1] = src[1];
74 	dst[2] = src[2];
75 	dst += 3;
76 	src += 4;
77     }
78 }
79 
80 /* ----------------------------------------------------------------------- */
81 
load_add_extra(struct ida_image_info * info,enum ida_extype type,unsigned char * data,unsigned int size)82 int load_add_extra(struct ida_image_info *info, enum ida_extype type,
83 		   unsigned char *data, unsigned int size)
84 {
85     struct ida_extra *extra;
86 
87     extra = malloc(sizeof(*extra));
88     if (NULL == extra)
89 	return -1;
90     memset(extra,0,sizeof(*extra));
91     extra->data = malloc(size);
92     if (NULL == extra->data) {
93 	free(extra);
94 	return -1;
95     }
96     extra->type = type;
97     extra->size = size;
98     memcpy(extra->data,data,size);
99     extra->next = info->extra;
100     info->extra = extra;
101     return 0;
102 };
103 
load_find_extra(struct ida_image_info * info,enum ida_extype type)104 struct ida_extra* load_find_extra(struct ida_image_info *info,
105 				  enum ida_extype type)
106 {
107     struct ida_extra *extra;
108 
109     for (extra = info->extra; NULL != extra; extra = extra->next)
110 	if (type == extra->type)
111 	    return extra;
112     return NULL;
113 }
114 
load_free_extras(struct ida_image_info * info)115 int load_free_extras(struct ida_image_info *info)
116 {
117     struct ida_extra *next;
118 
119     while (NULL != info->extra) {
120 	next = info->extra->next;
121 	free(info->extra->data);
122 	free(info->extra);
123 	info->extra = next;
124     }
125     return 0;
126 }
127 
128 /* ----------------------------------------------------------------------- */
129 
ida_image_alloc(struct ida_image * img)130 void ida_image_alloc(struct ida_image *img)
131 {
132     assert(img->p == NULL);
133     img->p = pixman_image_create_bits(PIXMAN_r8g8b8,
134                                       img->i.width, img->i.height, NULL, 0);
135 }
136 
ida_image_scanline(struct ida_image * img,int y)137 uint8_t *ida_image_scanline(struct ida_image *img, int y)
138 {
139     uint8_t *scanline;
140 
141     assert(img->p != NULL);
142     scanline  = (void*)pixman_image_get_data(img->p);
143     scanline += pixman_image_get_stride(img->p) * y;
144     return scanline;
145 }
146 
ida_image_stride(struct ida_image * img)147 uint32_t ida_image_stride(struct ida_image *img)
148 {
149     return pixman_image_get_stride(img->p);
150 }
151 
ida_image_bpp(struct ida_image * img)152 uint32_t ida_image_bpp(struct ida_image *img)
153 {
154     uint32_t bits = PIXMAN_FORMAT_BPP(pixman_image_get_format(img->p));
155     uint32_t bytes = bits / 8;
156     assert(bytes * 8 == bits);
157     return bytes;
158 }
159 
ida_image_free(struct ida_image * img)160 void ida_image_free(struct ida_image *img)
161 {
162     assert(img->p != NULL);
163     pixman_image_unref(img->p);
164     img->p = NULL;
165 }
166 
167 /* ----------------------------------------------------------------------- */
168 
169 LIST_HEAD(loaders);
170 
load_register(struct ida_loader * loader)171 void load_register(struct ida_loader *loader)
172 {
173     list_add_tail(&loader->list, &loaders);
174 }
175