]> www.fi.muni.cz Git - evince.git/blob - pdf/splash/SplashBitmap.cc
Updated German translation.
[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 splashModeRGB8Packed:
43     rowSize = (width * 3 + 3) & ~3;
44     data.rgb8p = (SplashRGB8P *)
45                   gmalloc(rowSize * height * sizeof(SplashRGB8P));
46     break;
47   case splashModeBGR8Packed:
48     rowSize = (width * 3 + 3) & ~3;
49     data.bgr8 = (SplashBGR8P *)
50                   gmalloc(rowSize * height * sizeof(SplashMono1P));
51   }
52 }
53
54
55 SplashBitmap::~SplashBitmap() {
56   switch (mode) {
57   case splashModeMono1:
58     gfree(data.mono1);
59     break;
60   case splashModeMono8:
61     gfree(data.mono8);
62     break;
63   case splashModeRGB8:
64     gfree(data.rgb8);
65     break;
66   case splashModeRGB8Packed:
67     gfree(data.rgb8p);
68     break;
69   case splashModeBGR8Packed:
70     gfree(data.bgr8);
71   }
72 }
73
74 SplashError SplashBitmap::writePNMFile(char *fileName) {
75   FILE *f;
76   SplashMono1P *mono1;
77   SplashMono8 *mono8;
78   SplashRGB8 *rgb8;
79   SplashRGB8P *rgb8pline, *rgb8p;
80   SplashBGR8P *bgr8line, *bgr8;
81   int x, y;
82
83   if (!(f = fopen(fileName, "wb"))) {
84     return splashErrOpenFile;
85   }
86
87   switch (mode) {
88
89   case splashModeMono1:
90     fprintf(f, "P4\n%d %d\n", width, height);
91     mono1 = data.mono1;
92     for (y = 0; y < height; ++y) {
93       for (x = 0; x < width; x += 8) {
94         fputc(*mono1 ^ 0xff, f);
95         ++mono1;
96       }
97     }
98     break;
99
100   case splashModeMono8:
101     fprintf(f, "P5\n%d %d\n255\n", width, height);
102     mono8 = data.mono8;
103     for (y = 0; y < height; ++y) {
104       for (x = 0; x < width; ++x) {
105         fputc(*mono8, f);
106         ++mono8;
107       }
108     }
109     break;
110
111   case splashModeRGB8:
112     fprintf(f, "P6\n%d %d\n255\n", width, height);
113     rgb8 = data.rgb8;
114     for (y = 0; y < height; ++y) {
115       for (x = 0; x < width; ++x) {
116         fputc(splashRGB8R(*rgb8), f);
117         fputc(splashRGB8G(*rgb8), f);
118         fputc(splashRGB8B(*rgb8), f);
119         ++rgb8;
120       }
121     }
122     break;
123
124   case splashModeRGB8Packed:
125     fprintf(f, "P6\n%d %d\n255\n", width, height);
126     rgb8pline = data.rgb8p;
127     for (y = 0; y < height; ++y) {
128       rgb8p = rgb8pline;
129       for (x = 0; x < width; ++x) {
130         fputc(rgb8p[0], f);
131         fputc(rgb8p[1], f);
132         fputc(rgb8p[2], f);
133         rgb8p += 3;
134       }
135       rgb8pline += rowSize;
136     }
137     break;
138
139   case splashModeBGR8Packed:
140     fprintf(f, "P6\n%d %d\n255\n", width, height);
141     bgr8line = data.bgr8;
142     for (y = 0; y < height; ++y) {
143       bgr8 = bgr8line;
144       for (x = 0; x < width; ++x) {
145         fputc(bgr8[2], f);
146         fputc(bgr8[1], f);
147         fputc(bgr8[0], f);
148         bgr8 += 3;
149       }
150       bgr8line += rowSize;
151     }
152     break;
153   }
154
155   fclose(f);
156   return splashOk;
157 }