multiply overflow fixes
[swftools.git] / lib / readers / image.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory.h>
4 #include "../gfxdevice.h"
5 #include "../gfxsource.h"
6 #include "../gfxtools.h"
7 #include "../log.h"
8 #include "../mem.h"
9 #include "../jpeg.h"
10 #include "../png.h"
11 #include "image.h"
12
13 typedef struct _image_page_internal
14 {
15 } image_page_internal_t;
16
17 typedef struct _image_doc_internal
18 {
19     gfximage_t img;
20 } image_doc_internal_t;
21
22 void imagepage_destroy(gfxpage_t*image_page)
23 {
24     image_page_internal_t*i= (image_page_internal_t*)image_page->internal;
25     free(image_page->internal);image_page->internal = 0;
26     free(image_page);image_page=0;
27 }
28
29 void imagepage_render(gfxpage_t*page, gfxdevice_t*output)
30 {
31     image_page_internal_t*i = (image_page_internal_t*)page->internal;
32     image_doc_internal_t*pi = (image_doc_internal_t*)page->parent->internal;
33
34     gfxcxform_t cxform;
35     memset(&cxform, 0, sizeof(cxform));
36     cxform.rr = 1;
37     cxform.gg = 1;
38     cxform.bb = 1;
39     cxform.aa = 1;
40
41     gfxmatrix_t m;
42     memset(&m, 0, sizeof(m));
43     m.m00 = 1;
44     m.m11 = 1;
45
46     gfxline_t* rect = gfxline_makerectangle(0, 0, pi->img.width, pi->img.height);
47     output->fillbitmap(output, rect, &pi->img, &m, &cxform);
48     gfxline_free(rect);
49 }
50
51 void imagepage_rendersection(gfxpage_t*page, gfxdevice_t*output, gfxcoord_t x, gfxcoord_t y, gfxcoord_t _x1, gfxcoord_t _y1, gfxcoord_t _x2, gfxcoord_t _y2)
52 {
53     image_page_internal_t*i = (image_page_internal_t*)page->internal;
54     image_doc_internal_t*pi = (image_doc_internal_t*)page->parent->internal;
55
56     gfxcxform_t cxform;
57     memset(&cxform, 0, sizeof(cxform));
58     cxform.rr = 1;
59     cxform.gg = 1;
60     cxform.bb = 1;
61     cxform.aa = 1;
62
63     gfxmatrix_t m;
64     memset(&m, 0, sizeof(m));
65     m.m00 = 1;
66     m.m11 = 1;
67     m.tx = x;
68     m.ty = y;
69     
70     gfxline_t* rect = gfxline_makerectangle(0, 0, pi->img.width, pi->img.height);
71     gfxline_t* rect2 = gfxline_makerectangle(_x1, _y1, _x2, _y2);
72
73     output->startclip(output, rect2);
74     output->fillbitmap(output, rect, &pi->img, &m, &cxform);
75     output->endclip(output);
76     gfxline_free(rect);
77     gfxline_free(rect2);
78 }
79
80 void image_doc_destroy(gfxdocument_t*gfx)
81 {
82     image_doc_internal_t*i= (image_doc_internal_t*)gfx->internal;
83
84     free(i->img.data);i->img.data = 0;
85
86     free(gfx->internal);gfx->internal=0;
87     free(gfx);gfx=0;
88 }
89
90 void image_doc_setparameter(gfxdocument_t*gfx, const char*name, const char*value)
91 {
92     image_doc_internal_t*i= (image_doc_internal_t*)gfx->internal;
93 }
94
95 gfxpage_t* image_doc_getpage(gfxdocument_t*doc, int page)
96 {
97     image_doc_internal_t*di= (image_doc_internal_t*)doc->internal;
98     if(page != 1)
99         return 0;
100     
101     gfxpage_t* image_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
102     image_page_internal_t*pi= (image_page_internal_t*)malloc(sizeof(image_page_internal_t));
103     memset(pi, 0, sizeof(image_page_internal_t));
104
105     image_page->internal = pi;
106     image_page->destroy = imagepage_destroy;
107     image_page->render = imagepage_render;
108     image_page->rendersection = imagepage_rendersection;
109     image_page->width = di->img.width;
110     image_page->height = di->img.height;
111     image_page->parent = doc;
112     image_page->nr = page;
113     return image_page;
114 }
115
116 static void image_setparameter(gfxsource_t*src, const char*name, const char*value)
117 {
118     msg("<verbose> (gfxsource_image) setting parameter %s to \"%s\"", name, value);
119 }
120
121 static gfxdocument_t*image_open(gfxsource_t*src, const char*filename)
122 {
123     gfxdocument_t*image_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
124     memset(image_doc, 0, sizeof(gfxdocument_t));
125     image_doc_internal_t*i= (image_doc_internal_t*)malloc(sizeof(image_doc_internal_t));
126     memset(i, 0, sizeof(image_doc_internal_t));
127
128     gfxcolor_t*data = 0;
129     unsigned width = 0;
130     unsigned height = 0;
131
132     if(!getPNG(filename, &width, &height, (unsigned char**)&data)) {
133         if(!jpeg_load(filename, (unsigned char**)&data, &width, &height)) {
134             msg("<error> Couldn't load image %s", filename);
135             return 0;
136         }
137     }
138     i->img.data = data;
139     i->img.width = width;
140     i->img.height = height;
141
142     image_doc->num_pages = 1;
143     image_doc->internal = i;
144     image_doc->get = 0;
145     image_doc->destroy = image_doc_destroy;
146     image_doc->setparameter = image_doc_setparameter;
147     image_doc->getpage = image_doc_getpage;
148
149     return image_doc;
150 }
151
152 gfxsource_t*gfxsource_image_create()
153 {
154     gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
155     memset(src, 0, sizeof(gfxsource_t));
156     src->setparameter = image_setparameter;
157     src->open = image_open;
158     return src;
159 }
160