Issue 849 attachment: test.c (4.5 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>

#include <fpdfview.h>

static void writedword(FILE *fp, unsigned int d)
{
fputc(d & 0x000000ff, fp);
fputc((d & 0x0000ff00) >> 8, fp);
fputc((d & 0x00ff0000) >> 16, fp);
fputc((d & 0xff000000) >> 24, fp);
}

static void writeword(FILE *fp, unsigned short d)
{
fputc(d & 0x00ff, fp);
fputc((d & 0xff00) >> 8, fp);
}

static void writebmp(unsigned int *rgb, int width, int height, int modulo)
{
FILE *fp;
int x, y;
int bytewidth = width * 3 + width % 4; // line width must be a multiple of 4 for BMPs
unsigned char *linebuf, *bak_linebuf;
unsigned int *tmprgb = rgb;

modulo /= 4;

if(!(fp = fopen("page.bmp", "wb"))) return;

linebuf = malloc(bytewidth);

fwrite("BM", 2, 1, fp);

writedword(fp, bytewidth * height + 54); // size
writedword(fp, 0); // reserved
writedword(fp, 0x36); // offset
writedword(fp, 0x28); // sizeof(struct BITMAPINFO)
writedword(fp, width);
writedword(fp, height);
writeword(fp, 1); // biPlanes
writeword(fp, 24); // bits
writedword(fp, 0); // compression
writedword(fp, 0); // size in bytes, may be zero for no compression
writedword(fp, 0); // pixels per meter
writedword(fp, 0); // pixels per meter
writedword(fp, 0); // biClrUsed
writedword(fp, 0); // biClrImportant

bak_linebuf = linebuf;

tmprgb += height * modulo;

// rgb data is stored in bottom-up format, pixel data is BGR
for(y = 0; y < height; y++) {

tmprgb -= modulo;
linebuf = bak_linebuf;

for(x = 0; x < width; x++) {
*linebuf++ = (unsigned char) (tmprgb[x] & 0x0000ff);
*linebuf++ = (unsigned char) ((tmprgb[x] & 0x00ff00) >> 8);
*linebuf++ = (unsigned char) ((tmprgb[x] & 0xff0000) >> 16);
}

fwrite(bak_linebuf, bytewidth, 1, fp);
}

free(bak_linebuf);
fclose(fp);
}

struct hwfloatpoint
{
double x;
double y;
};

static void transformrectf(double x, double y, double width, double height, double ax, double ay, FS_MATRIX *m, double *tx, double *ty, double *twidth, double *theight)
{
double midx = width * ax;
double midy = height * ay;
double maxx = -100000;
double maxy = -100000;
double minx = 100000;
double miny = 100000;
int k;
struct hwfloatpoint p[4];

p[0].x = 0;
p[0].y = 0;
p[1].x = width;
p[1].y = 0;
p[2].x = width;
p[2].y = height;
p[3].x = 0;
p[3].y = height;

for(k = 0; k < 4; k++) {

double tmpx = p[k].x - midx;
double tmpy = p[k].y - midy;

p[k].x = tmpx * m->a + tmpy * m->c;
p[k].y = tmpx * m->b + tmpy * m->d;
p[k].x += midx;
p[k].y += midy;

if(p[k].x < minx) minx = p[k].x;
if(p[k].y < miny) miny = p[k].y;
if(p[k].x > maxx) maxx = p[k].x;
if(p[k].y > maxy) maxy = p[k].y;
}

if(tx) *tx = x + minx;
if(ty) *ty = y + miny;
if(twidth) *twidth = maxx - minx;
if(theight) *theight = maxy - miny;
}

int main(int argc, char *argv[])
{
FPDF_DOCUMENT doc;
FPDF_PAGE page;
FPDF_BITMAP bm;
FPDF_LIBRARY_CONFIG config;
FS_MATRIX transform;
FS_RECTF rc;
int pwidth, pheight, bwidth, bheight;
double twidth, theight;
float angle;

if(argc < 3) {
printf("Wrong args\n");
return 0;
}

memset(&config, 0, sizeof(config));
config.version = 2;

FPDF_InitLibraryWithConfig(&config);

if(!(doc = FPDF_LoadDocument((const char *) argv[1], NULL))) {
FPDF_DestroyLibrary();
printf("Failed to load PDF\n");
return 0;
}

angle = atof(argv[2]) * (M_PI / 180.0f);

page = FPDF_LoadPage(doc, 0);

transform.a = cos(angle);
transform.b = sin(angle);
transform.c = -sin(angle);
transform.d = cos(angle);
transform.e = 0;
transform.f = 0;

pwidth = (int) FPDF_GetPageWidth(page);
pheight = (int) FPDF_GetPageHeight(page);

transformrectf(0, 0, pwidth, pheight, 0, 0, &transform, NULL, NULL, &twidth, &theight);

bwidth = (int) twidth;
bheight = (int) theight;

printf("PAGE SIZE: %d %d BITMAP SIZE: %d %d\n", pwidth, pheight, bwidth, bheight);

bm = FPDFBitmap_Create(bwidth, bheight, 1);

FPDFBitmap_FillRect(bm, 0, 0, bwidth, bheight, 0xffffffff);

rc.left = 0;
rc.top = 0;
rc.right = (float) bwidth;
rc.bottom = (float) bheight;

FPDF_RenderPageBitmapWithMatrix(bm, page, &transform, &rc, 0);

writebmp(FPDFBitmap_GetBuffer(bm), bwidth, bheight, FPDFBitmap_GetStride(bm));

FPDFBitmap_Destroy(bm);

FPDF_ClosePage(page);
FPDF_CloseDocument(doc);

FPDF_DestroyLibrary();

return 0;
}