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.

GraphicsSetProcessColor.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.afp.goca;
  19. import java.awt.Color;
  20. import java.awt.color.ColorSpace;
  21. import java.io.DataOutput;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import org.apache.commons.io.output.ByteArrayOutputStream;
  25. import org.apache.xmlgraphics.java2d.color.CIELabColorSpace;
  26. import org.apache.xmlgraphics.java2d.color.ColorUtil;
  27. import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
  28. /**
  29. * Sets the current processing color for the following GOCA structured fields
  30. */
  31. public class GraphicsSetProcessColor extends AbstractGraphicsDrawingOrder {
  32. /*
  33. * GOCA Color space support:
  34. * X'01' RGB
  35. * X'04' CMYK
  36. * X'06' Highlight color space
  37. * X'08' CIELAB
  38. * X'40' Standard OCA color space
  39. */
  40. private static final byte RGB = 0x01, CMYK = 0x04, CIELAB = 0x08;
  41. private final Color color;
  42. private final int componentsSize;
  43. /**
  44. * Main constructor
  45. *
  46. * @param color the color to set
  47. */
  48. public GraphicsSetProcessColor(Color color) {
  49. if (color instanceof ColorWithAlternatives) {
  50. ColorWithAlternatives cwa = (ColorWithAlternatives)color;
  51. Color alt = cwa.getFirstAlternativeOfType(ColorSpace.TYPE_CMYK);
  52. if (alt != null) {
  53. this.color = alt;
  54. this.componentsSize = 4;
  55. return;
  56. }
  57. }
  58. ColorSpace cs = color.getColorSpace();
  59. int colSpaceType = cs.getType();
  60. if (colSpaceType == ColorSpace.TYPE_CMYK) {
  61. this.color = color;
  62. } else if (cs instanceof CIELabColorSpace) {
  63. //TODO Convert between illuminants if not D50 according to rendering intents
  64. //Right now, we're assuming D50 as the GOCA spec requires.
  65. this.color = color;
  66. //16bit components didn't work, and 8-bit sadly has reduced accuracy.
  67. } else {
  68. if (!color.getColorSpace().isCS_sRGB()) {
  69. this.color = ColorUtil.toSRGBColor(color);
  70. } else {
  71. this.color = color;
  72. }
  73. }
  74. this.componentsSize = this.color.getColorSpace().getNumComponents();
  75. }
  76. /** {@inheritDoc} */
  77. public int getDataLength() {
  78. return 12 + this.componentsSize;
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. byte getOrderCode() {
  83. return (byte) 0xB2;
  84. }
  85. /** {@inheritDoc} */
  86. public void writeToStream(OutputStream os) throws IOException {
  87. float[] colorComponents = color.getColorComponents(null);
  88. // COLSPCE
  89. byte colspace;
  90. ColorSpace cs = color.getColorSpace();
  91. int colSpaceType = cs.getType();
  92. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  93. byte[] colsizes;
  94. if (colSpaceType == ColorSpace.TYPE_CMYK) {
  95. colspace = CMYK;
  96. colsizes = new byte[] {0x08, 0x08, 0x08, 0x08};
  97. for (int i = 0; i < colorComponents.length; i++) {
  98. baout.write(Math.round(colorComponents[i] * 255));
  99. }
  100. } else if (colSpaceType == ColorSpace.TYPE_RGB) {
  101. colspace = RGB;
  102. colsizes = new byte[] {0x08, 0x08, 0x08, 0x00};
  103. for (int i = 0; i < colorComponents.length; i++) {
  104. baout.write(Math.round(colorComponents[i] * 255));
  105. }
  106. } else if (cs instanceof CIELabColorSpace) {
  107. colspace = CIELAB;
  108. colsizes = new byte[] {0x08, 0x08, 0x08, 0x00};
  109. DataOutput dout = new java.io.DataOutputStream(baout);
  110. //According to GOCA, I'd expect the multiplicator below to be 255f, not 100f
  111. //But only IBM AFP Workbench seems to support Lab colors and it requires "c * 100f"
  112. int l = Math.round(colorComponents[0] * 100f);
  113. int a = Math.round(colorComponents[1] * 255f) - 128;
  114. int b = Math.round(colorComponents[2] * 255f) - 128;
  115. dout.writeByte(l);
  116. dout.writeByte(a);
  117. dout.writeByte(b);
  118. } else {
  119. throw new IllegalStateException();
  120. }
  121. int len = getDataLength();
  122. byte[] data = new byte[12];
  123. data[0] = getOrderCode(); // GSPCOL order code
  124. data[1] = (byte) (len - 2); // LEN
  125. data[2] = 0x00; // reserved; must be zero
  126. data[3] = colspace; // COLSPCE
  127. data[4] = 0x00; // reserved; must be zero
  128. data[5] = 0x00; // reserved; must be zero
  129. data[6] = 0x00; // reserved; must be zero
  130. data[7] = 0x00; // reserved; must be zero
  131. data[8] = colsizes[0]; // COLSIZE(S)
  132. data[9] = colsizes[1];
  133. data[10] = colsizes[2];
  134. data[11] = colsizes[3];
  135. os.write(data);
  136. baout.writeTo(os);
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public String toString() {
  141. return "GraphicsSetProcessColor(col=" + color + ")";
  142. }
  143. }