]> www.fi.muni.cz Git - evince.git/blob - pdf/splash/SplashBitmap.cc
added.
[evince.git] / pdf / splash / SplashBitmap.cc
1 //========================================================================
2 //
3 // SplashBitmap.cc
4 //
5 //========================================================================
6
7 #include <aconf.h>
8
9 #ifdef USE_GCC_PRAGMAS
10 #pragma implementation
11 #endif
12
13 #include <stdio.h>
14 #include "gmem.h"
15 #include "SplashErrorCodes.h"
16 #include "SplashBitmap.h"
17
18 //------------------------------------------------------------------------
19 // SplashBitmap
20 //------------------------------------------------------------------------
21
22 SplashBitmap::SplashBitmap(int widthA, int heightA, SplashColorMode modeA) {
23   width = widthA;
24   height = heightA;
25   mode = modeA;
26   switch (mode) {
27   case splashModeMono1:
28     rowSize = (width + 7) >> 3;
29     data.mono1 = (SplashMono1P *)
30                    gmalloc(rowSize * height * sizeof(SplashMono1P));
31     break;
32   case splashModeMono8:
33     rowSize = width;
34     data.mono8 = (SplashMono8 *)
35                    gmalloc(width * height * sizeof(SplashMono8));
36     break;
37   case splashModeRGB8:
38     rowSize = width << 2;
39     data.rgb8 = (SplashRGB8 *)
40                   gmalloc(width * height * sizeof(SplashRGB8));
41     break;
42   case splashModeBGR8Packed:
43     rowSize = (width * 3 + 3) & ~3;
44     data.bgr8 = (SplashBGR8P *)
45                   gmalloc(rowSize * height * sizeof(SplashMono1P));
46   }
47 }
48
49
50 SplashBitmap::~SplashBitmap() {
51   switch (mode) {
52   case splashModeMono1:
53     gfree(data.mono1);
54     break;
55   case splashModeMono8:
56     gfree(data.mono8);
57     break;
58   case splashModeRGB8:
59     gfree(data.rgb8);
60     break;
61   case splashModeBGR8Packed:
62     gfree(data.bgr8);
63     break;
64   }
65 }
66
67 SplashError SplashBitmap::writePNMFile(char *fileName) {
68   FILE *f;
69   SplashMono1P *mono1;
70   SplashMono8 *mono8;
71   SplashRGB8 *rgb8;
72   SplashBGR8P *bgr8line, *bgr8;
73   int x, y;
74
75   if (!(f = fopen(fileName, "wb"))) {
76     return splashErrOpenFile;
77   }
78
79   switch (mode) {
80
81   case splashModeMono1:
82     fprintf(f, "P4\n%d %d\n", width, height);
83     mono1 = data.mono1;
84     for (y = 0; y < height; ++y) {
85       for (x = 0; x < width; x += 8) {
86         fputc(*mono1 ^ 0xff, f);
87         ++mono1;
88       }
89     }
90     break;
91
92   case splashModeMono8:
93     fprintf(f, "P5\n%d %d\n255\n", width, height);
94     mono8 = data.mono8;
95     for (y = 0; y < height; ++y) {
96       for (x = 0; x < width; ++x) {
97         fputc(*mono8, f);
98         ++mono8;
99       }
100     }
101     break;
102
103   case splashModeRGB8:
104     fprintf(f, "P6\n%d %d\n255\n", width, height);
105     rgb8 = data.rgb8;
106     for (y = 0; y < height; ++y) {
107       for (x = 0; x < width; ++x) {
108         fputc(splashRGB8R(*rgb8), f);
109         fputc(splashRGB8G(*rgb8), f);
110         fputc(splashRGB8B(*rgb8), f);
111         ++rgb8;
112       }
113     }
114     break;
115
116   case splashModeBGR8Packed:
117     fprintf(f, "P6\n%d %d\n255\n", width, height);
118     bgr8line = data.bgr8;
119     for (y = 0; y < height; ++y) {
120       bgr8 = bgr8line;
121       for (x = 0; x < width; ++x) {
122         fputc(bgr8[2], f);
123         fputc(bgr8[1], f);
124         fputc(bgr8[0], f);
125         bgr8 += 3;
126       }
127       bgr8line += rowSize;
128     }
129     break;
130   }
131
132   fclose(f);
133   return splashOk;
134 }