449111a7a55da41949ac1bc78a99d2bdca62ceef
[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(gfx->internal);gfx->internal=0;
85     free(gfx);gfx=0;
86 }
87
88 void image_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
89 {
90     image_doc_internal_t*i= (image_doc_internal_t*)gfx->internal;
91 }
92
93 gfxpage_t* image_doc_getpage(gfxdocument_t*doc, int page)
94 {
95     image_doc_internal_t*di= (image_doc_internal_t*)doc->internal;
96     if(page != 1)
97         return 0;
98     
99     gfxpage_t* image_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
100     image_page_internal_t*pi= (image_page_internal_t*)malloc(sizeof(image_page_internal_t));
101     memset(pi, 0, sizeof(image_page_internal_t));
102
103     image_page->internal = pi;
104     image_page->destroy = imagepage_destroy;
105     image_page->render = imagepage_render;
106     image_page->rendersection = imagepage_rendersection;
107     image_page->width = di->img.width;
108     image_page->height = di->img.height;
109     image_page->parent = doc;
110     image_page->nr = page;
111     return image_page;
112 }
113
114 static void image_set_parameter(gfxsource_t*src, const char*name, const char*value)
115 {
116     msg("<verbose> (gfxsource_image) setting parameter %s to \"%s\"", name, value);
117 }
118
119 static gfxdocument_t*image_open(gfxsource_t*src, const char*filename)
120 {
121     gfxdocument_t*image_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
122     memset(image_doc, 0, sizeof(gfxdocument_t));
123     image_doc_internal_t*i= (image_doc_internal_t*)malloc(sizeof(image_doc_internal_t));
124     memset(i, 0, sizeof(image_doc_internal_t));
125
126     gfxcolor_t*data = 0;
127     int width = 0;
128     int height = 0;
129
130     if(!getPNG(filename, &width, &height, (unsigned char**)&data)) {
131         if(!jpeg_load(filename, (unsigned char**)&data, &width, &height)) {
132             msg("<error> Couldn't load image %s", filename);
133             return 0;
134         }
135     }
136     i->img.data = data;
137     i->img.width = width;
138     i->img.height = height;
139
140     image_doc->num_pages = 1;
141     image_doc->internal = i;
142     image_doc->get = 0;
143     image_doc->destroy = image_doc_destroy;
144     image_doc->set_parameter = image_doc_set_parameter;
145     image_doc->getpage = image_doc_getpage;
146
147     return image_doc;
148 }
149
150 gfxsource_t*gfxsource_image_create()
151 {
152     gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
153     memset(src, 0, sizeof(gfxsource_t));
154     src->set_parameter = image_set_parameter;
155     src->open = image_open;
156     return src;
157 }