00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <aconf.h>
00010
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <stddef.h>
00018 #include <ctype.h>
00019 #include "gmem.h"
00020 #include "config.h"
00021 #include "Error.h"
00022 #include "GfxState.h"
00023 #include "Object.h"
00024 #include "Stream.h"
00025 #include "ImageOutputDev.h"
00026
00027 ImageOutputDev::ImageOutputDev(char *fileRootA, GBool dumpJPEGA) {
00028 fileRoot = copyString(fileRootA);
00029 fileName = (char *)gmalloc(strlen(fileRoot) + 20);
00030 dumpJPEG = dumpJPEGA;
00031 imgNum = 0;
00032 ok = gTrue;
00033 }
00034
00035 ImageOutputDev::~ImageOutputDev() {
00036 gfree(fileName);
00037 gfree(fileRoot);
00038 }
00039
00040 void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
00041 int width, int height, GBool invert,
00042 GBool inlineImg) {
00043 FILE *f;
00044 int c;
00045 int size, i;
00046
00047
00048 if (dumpJPEG && str->getKind() == strDCT && !inlineImg) {
00049
00050
00051 sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
00052 ++imgNum;
00053 if (!(f = fopen(fileName, "wb"))) {
00054 error(-1, "Couldn't open image file '%s'", fileName);
00055 return;
00056 }
00057
00058
00059 str = ((DCTStream *)str)->getRawStream();
00060 str->reset();
00061
00062
00063 while ((c = str->getChar()) != EOF)
00064 fputc(c, f);
00065
00066 str->close();
00067 fclose(f);
00068
00069
00070 } else {
00071
00072
00073 sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
00074 ++imgNum;
00075 if (!(f = fopen(fileName, "wb"))) {
00076 error(-1, "Couldn't open image file '%s'", fileName);
00077 return;
00078 }
00079 fprintf(f, "P4\n");
00080 fprintf(f, "%d %d\n", width, height);
00081
00082
00083 str->reset();
00084
00085
00086 size = height * ((width + 7) / 8);
00087 for (i = 0; i < size; ++i) {
00088 fputc(str->getChar(), f);
00089 }
00090
00091 str->close();
00092 fclose(f);
00093 }
00094 }
00095
00096 void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
00097 int width, int height,
00098 GfxImageColorMap *colorMap,
00099 int *maskColors, GBool inlineImg) {
00100 FILE *f;
00101 ImageStream *imgStr;
00102 Guchar *p;
00103 GfxRGB rgb;
00104 int x, y;
00105 int c;
00106 int size, i;
00107
00108
00109 if (dumpJPEG && str->getKind() == strDCT &&
00110 colorMap->getNumPixelComps() == 3 &&
00111 !inlineImg) {
00112
00113
00114 sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
00115 ++imgNum;
00116 if (!(f = fopen(fileName, "wb"))) {
00117 error(-1, "Couldn't open image file '%s'", fileName);
00118 return;
00119 }
00120
00121
00122 str = ((DCTStream *)str)->getRawStream();
00123 str->reset();
00124
00125
00126 while ((c = str->getChar()) != EOF)
00127 fputc(c, f);
00128
00129 str->close();
00130 fclose(f);
00131
00132
00133 } else if (colorMap->getNumPixelComps() == 1 &&
00134 colorMap->getBits() == 1) {
00135
00136
00137 sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
00138 ++imgNum;
00139 if (!(f = fopen(fileName, "wb"))) {
00140 error(-1, "Couldn't open image file '%s'", fileName);
00141 return;
00142 }
00143 fprintf(f, "P4\n");
00144 fprintf(f, "%d %d\n", width, height);
00145
00146
00147 str->reset();
00148
00149
00150 size = height * ((width + 7) / 8);
00151 for (i = 0; i < size; ++i) {
00152 fputc(str->getChar() ^ 0xff, f);
00153 }
00154
00155 str->close();
00156 fclose(f);
00157
00158
00159 } else {
00160
00161
00162 sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum);
00163 ++imgNum;
00164 if (!(f = fopen(fileName, "wb"))) {
00165 error(-1, "Couldn't open image file '%s'", fileName);
00166 return;
00167 }
00168 fprintf(f, "P6\n");
00169 fprintf(f, "%d %d\n", width, height);
00170 fprintf(f, "255\n");
00171
00172
00173 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
00174 colorMap->getBits());
00175 imgStr->reset();
00176
00177
00178 for (y = 0; y < height; ++y) {
00179
00180
00181 p = imgStr->getLine();
00182 for (x = 0; x < width; ++x) {
00183 colorMap->getRGB(p, &rgb);
00184 fputc((int)(rgb.r * 255 + 0.5), f);
00185 fputc((int)(rgb.g * 255 + 0.5), f);
00186 fputc((int)(rgb.b * 255 + 0.5), f);
00187 p += colorMap->getNumPixelComps();
00188 }
00189 }
00190 delete imgStr;
00191
00192 fclose(f);
00193 }
00194 }