home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 12 / MA_Cover_12.iso / source / xpdf-0.80 / xpdf / imageoutputdev.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-27  |  3.3 KB  |  152 lines

  1. //========================================================================
  2. //
  3. // ImageOutputDev.cc
  4. //
  5. // Copyright 1998 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifdef __GNUC__
  10. #pragma implementation
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <ctype.h>
  17. #include "gmem.h"
  18. #include "config.h"
  19. #include "Error.h"
  20. #include "GfxState.h"
  21. #include "Object.h"
  22. #include "Stream.h"
  23. #include "ImageOutputDev.h"
  24.  
  25. ImageOutputDev::ImageOutputDev(char *fileRoot1, GBool dumpJPEG1) {
  26.   fileRoot = copyString(fileRoot1);
  27.   fileName = (char *)gmalloc(strlen(fileRoot) + 20);
  28.   dumpJPEG = dumpJPEG1;
  29.   imgNum = 0;
  30.   ok = gTrue;
  31. }
  32.  
  33. ImageOutputDev::~ImageOutputDev() {
  34.   gfree(fileName);
  35.   gfree(fileRoot);
  36. }
  37.  
  38. void ImageOutputDev::drawImageMask(GfxState *state, Stream *str,
  39.                    int width, int height, GBool invert,
  40.                    GBool inlineImg) {
  41.   FILE *f;
  42.   int c;
  43.  
  44.   // dump JPEG file
  45.   if (dumpJPEG && str->getKind() == strDCT) {
  46.  
  47.     // open the image file
  48.     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
  49.     ++imgNum;
  50.     if (!(f = fopen(fileName, "wb"))) {
  51.       error(-1, "Couldn't open image file '%s'", fileName);
  52.       return;
  53.     }
  54.  
  55.     // initialize stream
  56.     str = ((DCTStream *)str)->getRawStream();
  57.     str->reset();
  58.  
  59.     // copy the stream
  60.     while ((c = str->getChar()) != EOF)
  61.       fputc(c, f);
  62.  
  63.     fclose(f);
  64.  
  65.   // dump PBM file
  66.   } else {
  67.  
  68.     // open the image file and write the PBM header
  69.     sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
  70.     ++imgNum;
  71.     if (!(f = fopen(fileName, "wb"))) {
  72.       error(-1, "Couldn't open image file '%s'", fileName);
  73.       return;
  74.     }
  75.     fprintf(f, "P4\n");
  76.     fprintf(f, "%d %d\n", width, height);
  77.  
  78.     // initialize stream
  79.     str->reset();
  80.  
  81.     // copy the stream
  82.     while ((c = str->getChar()) != EOF)
  83.       fputc(c, f);
  84.  
  85.     fclose(f);
  86.   }
  87. }
  88.  
  89. void ImageOutputDev::drawImage(GfxState *state, Stream *str, int width,
  90.                    int height, GfxImageColorMap *colorMap,
  91.                    GBool inlineImg) {
  92.   FILE *f;
  93.   Guchar pixBuf[4];
  94.   GfxColor color;
  95.   int x, y;
  96.   int c;
  97.  
  98.   // dump JPEG file
  99.   if (dumpJPEG && str->getKind() == strDCT) {
  100.  
  101.     // open the image file
  102.     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
  103.     ++imgNum;
  104.     if (!(f = fopen(fileName, "wb"))) {
  105.       error(-1, "Couldn't open image file '%s'", fileName);
  106.       return;
  107.     }
  108.  
  109.     // initialize stream
  110.     str = ((DCTStream *)str)->getRawStream();
  111.     str->reset();
  112.  
  113.     // copy the stream
  114.     while ((c = str->getChar()) != EOF)
  115.       fputc(c, f);
  116.  
  117.     fclose(f);
  118.  
  119.   // dump PPM file
  120.   } else {
  121.  
  122.     // open the image file and write the PPM header
  123.     sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum);
  124.     ++imgNum;
  125.     if (!(f = fopen(fileName, "wb"))) {
  126.       error(-1, "Couldn't open image file '%s'", fileName);
  127.       return;
  128.     }
  129.     fprintf(f, "P6\n");
  130.     fprintf(f, "%d %d\n", width, height);
  131.     fprintf(f, "255\n");
  132.  
  133.     // initialize stream
  134.     str->resetImage(width, colorMap->getNumPixelComps(), colorMap->getBits());
  135.  
  136.     // for each line...
  137.     for (y = 0; y < height; ++y) {
  138.  
  139.       // write the line
  140.       for (x = 0; x < width; ++x) {
  141.     str->getImagePixel(pixBuf);
  142.     colorMap->getColor(pixBuf, &color);
  143.     fputc((int)(color.getR() * 255 + 0.5), f);
  144.     fputc((int)(color.getG() * 255 + 0.5), f);
  145.     fputc((int)(color.getB() * 255 + 0.5), f);
  146.       }
  147.     }
  148.  
  149.     fclose(f);
  150.   }
  151. }
  152.