void PSOutputDev::doImageL1(Object *ref, GfxImageColorMap *colorMap,
			    GBool invert, GBool inlineImg,
			    Stream *str, int width, int height, int len,
			    int *maskColors, Stream *maskStr,
			    int maskWidth, int maskHeight, GBool maskInvert) {
  ImageStream *imgStr;
  Guchar pixBuf[gfxColorMaxComps];
  GfxGray gray;
  int col, x, y, c, i;
  char hexBuf[32*2 + 2];	// 32 values X 2 chars/value + line ending + null
  Guchar digit, grayValue;
  const GBool useBinary = globalParams->getPSBinary();

  // explicit masking
  if (maskStr && !(maskColors && colorMap)) {
    maskToClippingPath(maskStr, maskWidth, maskHeight, maskInvert);
  }

  if ((inType3Char || preload) && !colorMap) {
    if (inlineImg) {
      // create an array
      str = new FixedLengthEncoder(str, len);
      str = new ASCIIHexEncoder(str);
      str->reset();
      col = 0;
      writePS("[<");
      do {
	do {
	  c = str->getChar();
	} while (c == '\n' || c == '\r');
	if (c == '>' || c == EOF) {
	  break;
	}
	writePSChar(c);
	++col;
	// each line is: "<...data...><eol>"
	// so max data length = 255 - 4 = 251
	// but make it 240 just to be safe
	// chunks are 2 bytes each, so we need to stop on an even col number
	if (col == 240) {
	  writePS(">\n<");
	  col = 0;
	}
      } while (c != '>' && c != EOF);
      writePS(">]\n");
      writePS("0\n");
      str->close();
      delete str;
    } else {
      // make sure the image is setup, it sometimes is not like on bug #17645
      setupImage(ref->getRef(), str);
      // set up to use the array already created by setupImages()
      writePSFmt("ImData_{0:d}_{1:d} 0 0\n", ref->getRefNum(), ref->getRefGen());
    }
  }

  // image/imagemask command
  if ((inType3Char || preload) && !colorMap) {
    writePSFmt("{0:d} {1:d} {2:s} [{3:d} 0 0 {4:d} 0 {5:d}] pdfImM1a\n",
	       width, height, invert ? "true" : "false",
	       width, -height, height);
  } else if (colorMap) {
    writePSFmt("{0:d} {1:d} 8 [{2:d} 0 0 {3:d} 0 {4:d}] pdfIm1{5:s}\n",
	       width, height,
	       width, -height, height,
	       useBinary ? "Bin" : "");
  } else {
    writePSFmt("{0:d} {1:d} {2:s} [{3:d} 0 0 {4:d} 0 {5:d}] pdfImM1{6:s}\n",
	       width, height, invert ? "true" : "false",
	       width, -height, height,
	       useBinary ? "Bin" : "");
  }

  // image data
  if (!((inType3Char || preload) && !colorMap)) {

    if (colorMap) {

      // set up to process the data stream
      imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
			       colorMap->getBits());
      imgStr->reset();

      // process the data stream
      i = 0;
      for (y = 0; y < height; ++y) {

	// write the line
	for (x = 0; x < width; ++x) {
	  imgStr->getPixel(pixBuf);
	  colorMap->getGray(pixBuf, &gray);
	  grayValue = colToByte(gray);
	  if (useBinary) {
	    hexBuf[i++] = grayValue;
	  } else {
	    digit = grayValue / 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	    digit = grayValue % 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	  }
	  if (i >= 64) {
	    if (!useBinary) {
	      hexBuf[i++] = '\n';
	    }
	    writePSBuf(hexBuf, i);
	    i = 0;
	  }
	}
      }
      if (i != 0) {
	if (!useBinary) {
	  hexBuf[i++] = '\n';
	}
	writePSBuf(hexBuf, i);
      }
      str->close();
      delete imgStr;

    // imagemask
    } else {
      str->reset();
      i = 0;
      for (y = 0; y < height; ++y) {
	for (x = 0; x < width; x += 8) {
	  grayValue = str->getChar();
	  if (useBinary) {
	    hexBuf[i++] = grayValue;
	  } else {
	    digit = grayValue / 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	    digit = grayValue % 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	  }
	  if (i >= 64) {
	    if (!useBinary) {
	      hexBuf[i++] = '\n';
	    }
	    writePSBuf(hexBuf, i);
	    i = 0;
	  }
	}
      }
      if (i != 0) {
	if (!useBinary) {
	  hexBuf[i++] = '\n';
	}
	writePSBuf(hexBuf, i);
      }
      str->close();
    }
  }

  if (maskStr && !(maskColors && colorMap)) {
    writePS("pdfImClipEnd\n");
  }
}
void PSOutputDev::doImageL1(Object *ref, GfxImageColorMap *colorMap,
			    GBool invert, GBool inlineImg,
			    Stream *str, int width, int height, int len,
			    int *maskColors, Stream *maskStr,
			    int maskWidth, int maskHeight, GBool maskInvert) {
  ImageStream *imgStr;
  Guchar pixBuf[gfxColorMaxComps];
  GfxGray gray;
  int col, x, y, c, i;
  char hexBuf[32*2 + 2];	 
  Guchar digit, grayValue;
  const GBool useBinary = globalParams->getPSBinary();

   
  if (maskStr && !(maskColors && colorMap)) {
    maskToClippingPath(maskStr, maskWidth, maskHeight, maskInvert);
  }

  if ((inType3Char || preload) && !colorMap) {
    if (inlineImg) {
       
      str = new FixedLengthEncoder(str, len);
      str = new ASCIIHexEncoder(str);
      str->reset();
      col = 0;
      writePS("[<");
      do {
	do {
	  c = str->getChar();
	} while (c == '\n' || c == '\r');
	if (c == '>' || c == EOF) {
	  break;
	}
	writePSChar(c);
	++col;
	 
	 
	 
	 
	if (col == 240) {
	  writePS(">\n<");
	  col = 0;
	}
      } while (c != '>' && c != EOF);
      writePS(">]\n");
      writePS("0\n");
      str->close();
      delete str;
    } else {
       
      setupImage(ref->getRef(), str);
       
      writePSFmt("ImData_{0:d}_{1:d} 0 0\n", ref->getRefNum(), ref->getRefGen());
    }
  }

   
  if ((inType3Char || preload) && !colorMap) {
    writePSFmt("{0:d} {1:d} {2:s} [{3:d} 0 0 {4:d} 0 {5:d}] pdfImM1a\n",
	       width, height, invert ? "true" : "false",
	       width, -height, height);
  } else if (colorMap) {
    writePSFmt("{0:d} {1:d} 8 [{2:d} 0 0 {3:d} 0 {4:d}] pdfIm1{5:s}\n",
	       width, height,
	       width, -height, height,
	       useBinary ? "Bin" : "");
  } else {
    writePSFmt("{0:d} {1:d} {2:s} [{3:d} 0 0 {4:d} 0 {5:d}] pdfImM1{6:s}\n",
	       width, height, invert ? "true" : "false",
	       width, -height, height,
	       useBinary ? "Bin" : "");
  }

   
  if (!((inType3Char || preload) && !colorMap)) {

    if (colorMap) {

       
      imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
			       colorMap->getBits());
      imgStr->reset();

       
      i = 0;
      for (y = 0; y < height; ++y) {

	 
	for (x = 0; x < width; ++x) {
	  imgStr->getPixel(pixBuf);
	  colorMap->getGray(pixBuf, &gray);
	  grayValue = colToByte(gray);
	  if (useBinary) {
	    hexBuf[i++] = grayValue;
	  } else {
	    digit = grayValue / 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	    digit = grayValue % 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	  }
	  if (i >= 64) {
	    if (!useBinary) {
	      hexBuf[i++] = '\n';
	    }
	    writePSBuf(hexBuf, i);
	    i = 0;
	  }
	}
      }
      if (i != 0) {
	if (!useBinary) {
	  hexBuf[i++] = '\n';
	}
	writePSBuf(hexBuf, i);
      }
      str->close();
      delete imgStr;

     
    } else {
      str->reset();
      i = 0;
      for (y = 0; y < height; ++y) {
	for (x = 0; x < width; x += 8) {
	  grayValue = str->getChar();
	  if (useBinary) {
	    hexBuf[i++] = grayValue;
	  } else {
	    digit = grayValue / 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	    digit = grayValue % 16;
	    hexBuf[i++] = digit + ((digit >= 10)? 'a' - 10: '0');
	  }
	  if (i >= 64) {
	    if (!useBinary) {
	      hexBuf[i++] = '\n';
	    }
	    writePSBuf(hexBuf, i);
	    i = 0;
	  }
	}
      }
      if (i != 0) {
	if (!useBinary) {
	  hexBuf[i++] = '\n';
	}
	writePSBuf(hexBuf, i);
      }
      str->close();
    }
  }

  if (maskStr && !(maskColors && colorMap)) {
    writePS("pdfImClipEnd\n");
  }
}