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.

IDEStructureParameter.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.ioca;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import org.apache.fop.afp.Streamable;
  23. /**
  24. * This class represents the IOCA IDE Structure parameter (X'9B').
  25. */
  26. public class IDEStructureParameter implements Streamable {
  27. /** The RGB color model used by the IDE Structure parameter */
  28. public static final byte COLOR_MODEL_RGB = (byte)0x01;
  29. /** The YCrCb color model used by the IDE Structure parameter */
  30. public static final byte COLOR_MODEL_YCRCB = (byte)0x02;
  31. /** The CMYK color model used by the IDE Structure parameter */
  32. public static final byte COLOR_MODEL_CMYK = (byte)0x04;
  33. /** The YCbCr color model used by the IDE Structure parameter */
  34. public static final byte COLOR_MODEL_YCBCR = (byte)0x12;
  35. /** additive/subtractive setting for ASFLAG */
  36. private boolean subtractive;
  37. /** setting for GRAYCODE flag */
  38. private boolean grayCoding;
  39. /** the image color model */
  40. private byte colorModel = COLOR_MODEL_RGB;
  41. /** the array with the number of bits/IDE for each component */
  42. private byte[] bitsPerIDE = new byte[] {(byte)1}; //1-bit by default
  43. /**
  44. * Creates a new IDE Structure parameter. The values are initialized for a bi-level image
  45. * using the RGB color model.
  46. */
  47. public IDEStructureParameter() {
  48. //nop
  49. }
  50. /**
  51. * Sets the image IDE color model.
  52. *
  53. * @param color the IDE color model.
  54. */
  55. public void setColorModel(byte color) {
  56. this.colorModel = color;
  57. }
  58. /**
  59. * Establishes the parameter values for the normal RGB 24bit color model.
  60. */
  61. public void setDefaultRGBColorModel() {
  62. this.colorModel = COLOR_MODEL_RGB;
  63. setUniformBitsPerComponent(3, 8);
  64. }
  65. /**
  66. * Establishes the parameter values for the normal CMYK 32bit color model.
  67. */
  68. public void setDefaultCMYKColorModel() {
  69. this.colorModel = COLOR_MODEL_CMYK;
  70. setUniformBitsPerComponent(4, 8);
  71. }
  72. /**
  73. * Sets uniform bits per component.
  74. * @param numComponents the number of components
  75. * @param bitsPerComponent number of bits per component
  76. */
  77. public void setUniformBitsPerComponent(int numComponents, int bitsPerComponent) {
  78. if (bitsPerComponent < 0 || bitsPerComponent >= 256) {
  79. throw new IllegalArgumentException(
  80. "The number of bits per component must be between 0 and 255");
  81. }
  82. this.bitsPerIDE = new byte[numComponents];
  83. for (int i = 0; i < numComponents; i++) {
  84. this.bitsPerIDE[i] = (byte)bitsPerComponent;
  85. }
  86. }
  87. /**
  88. * Sets the array for the bits/IDE, one entry per component.
  89. * @param bitsPerComponent the
  90. */
  91. public void setBitsPerComponent(int[] bitsPerComponent) {
  92. int numComponents = bitsPerComponent.length;
  93. this.bitsPerIDE = new byte[numComponents];
  94. for (int i = 0; i < numComponents; i++) {
  95. int bits = bitsPerComponent[i];
  96. if (bits < 0 || bits >= 256) {
  97. throw new IllegalArgumentException(
  98. "The number of bits per component must be between 0 and 255");
  99. }
  100. this.bitsPerIDE[i] = (byte)bits;
  101. }
  102. }
  103. /**
  104. * Set either additive or subtractive mode (used for ASFLAG).
  105. * @param subtractive true for subtractive mode, false for additive mode
  106. */
  107. public void setSubtractive(boolean subtractive) {
  108. this.subtractive = subtractive;
  109. }
  110. /** {@inheritDoc} */
  111. public void writeToStream(OutputStream os) throws IOException {
  112. int length = 7 + bitsPerIDE.length;
  113. byte flags = 0x00;
  114. if (subtractive) {
  115. flags |= 1 << 7;
  116. }
  117. if (grayCoding) {
  118. flags |= 1 << 6;
  119. }
  120. DataOutputStream dout = new DataOutputStream(os);
  121. dout.writeByte(0x9B); //ID
  122. dout.writeByte(length - 2); //LENGTH
  123. dout.writeByte(flags); //FLAGS
  124. dout.writeByte(this.colorModel); //FORMAT
  125. for (int i = 0; i < 3; i++) {
  126. dout.writeByte(0); //RESERVED
  127. }
  128. dout.write(this.bitsPerIDE); //component sizes
  129. }
  130. }