You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageRawCCITTFaxAdapter.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.pdf;
  19. import java.awt.color.ColorSpace;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import org.apache.xmlgraphics.image.codec.tiff.TIFFImage;
  23. import org.apache.xmlgraphics.image.loader.impl.ImageRawCCITTFax;
  24. import org.apache.fop.pdf.CCFFilter;
  25. import org.apache.fop.pdf.PDFDeviceColorSpace;
  26. import org.apache.fop.pdf.PDFDictionary;
  27. import org.apache.fop.pdf.PDFDocument;
  28. import org.apache.fop.pdf.PDFFilter;
  29. import org.apache.fop.pdf.PDFFilterList;
  30. /**
  31. * PDFImage implementation for the PDF renderer which handles raw CCITT fax images.
  32. */
  33. public class ImageRawCCITTFaxAdapter extends AbstractImageAdapter {
  34. private PDFFilter pdfFilter;
  35. /**
  36. * Creates a new PDFImage from an Image instance.
  37. * @param image the CCITT encoded image
  38. * @param key XObject key
  39. */
  40. public ImageRawCCITTFaxAdapter(ImageRawCCITTFax image, String key) {
  41. super(image, key);
  42. }
  43. /**
  44. * Returns the {@link ImageRawCCITTFax} instance for this adapter.
  45. * @return the image instance
  46. */
  47. public ImageRawCCITTFax getImage() {
  48. return ((ImageRawCCITTFax)this.image);
  49. }
  50. /** {@inheritDoc} */
  51. public void setup(PDFDocument doc) {
  52. pdfFilter = new CCFFilter();
  53. pdfFilter.setApplied(true);
  54. PDFDictionary dict = new PDFDictionary();
  55. dict.put("Columns", this.image.getSize().getWidthPx());
  56. int compression = getImage().getCompression();
  57. switch (compression) {
  58. case TIFFImage.COMP_FAX_G3_1D :
  59. dict.put("K", 0);
  60. break;
  61. case TIFFImage.COMP_FAX_G3_2D :
  62. dict.put("K", 1);
  63. break;
  64. case TIFFImage.COMP_FAX_G4_2D :
  65. dict.put("K", -1);
  66. break;
  67. default:
  68. throw new IllegalStateException("Invalid compression scheme: " + compression);
  69. }
  70. ((CCFFilter)pdfFilter).setDecodeParms(dict);
  71. super.setup(doc);
  72. }
  73. /** {@inheritDoc} */
  74. public PDFDeviceColorSpace getColorSpace() {
  75. return toPDFColorSpace(ColorSpace.getInstance(ColorSpace.CS_GRAY));
  76. }
  77. /** {@inheritDoc} */
  78. public int getBitsPerComponent() {
  79. return 1;
  80. }
  81. /** {@inheritDoc} */
  82. public PDFFilter getPDFFilter() {
  83. return pdfFilter;
  84. }
  85. /** {@inheritDoc} */
  86. public void outputContents(OutputStream out) throws IOException {
  87. getImage().writeTo(out);
  88. }
  89. /** {@inheritDoc} */
  90. public String getFilterHint() {
  91. return PDFFilterList.TIFF_FILTER;
  92. }
  93. }