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.

RawPNGTestUtil.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.util.zip.Deflater;
  22. import java.util.zip.DeflaterOutputStream;
  23. import org.apache.xmlgraphics.image.loader.ImageSize;
  24. public final class RawPNGTestUtil {
  25. private static int NUM_ROWS = 32;
  26. private static int NUM_COLUMNS = 32;
  27. private static int DPI = 72;
  28. private RawPNGTestUtil() {
  29. }
  30. /**
  31. * Builds a PNG IDAT section for a square of a given color and alpha; the filter is fixed.
  32. * @param gray the gray color; set to -1 if using RGB
  33. * @param red the red color; ignored if gray > -1
  34. * @param green the green color; ignored if gray > -1
  35. * @param blue the blue color; ignored if gray > -1
  36. * @param alpha the alpha color; set to -1 if not present
  37. * @return the PNG IDAT byte array
  38. * @throws IOException
  39. */
  40. public static byte[] buildGRGBAData(int gray, int red, int green, int blue, int alpha) throws IOException {
  41. // build an image, 32x32, Gray or RGB, with or without alpha, and with filter
  42. int filter = 0;
  43. int numRows = NUM_ROWS;
  44. int numColumns = NUM_COLUMNS;
  45. int numComponents = (gray > -1 ? 1 : 3) + (alpha > -1 ? 1 : 0);
  46. int numBytesPerRow = numColumns * numComponents + 1; // 1 for filter
  47. int numBytes = numRows * numBytesPerRow;
  48. byte[] data = new byte[numBytes];
  49. for (int r = 0; r < numRows; r++) {
  50. data[r * numBytesPerRow] = (byte) filter;
  51. for (int c = 0; c < numColumns; c++) {
  52. if (numComponents == 1) {
  53. data[r * numBytesPerRow + numComponents * c + 1] = (byte) gray;
  54. } else if (numComponents == 2) {
  55. data[r * numBytesPerRow + numComponents * c + 1] = (byte) gray;
  56. data[r * numBytesPerRow + numComponents * c + 2] = (byte) alpha;
  57. } else if (numComponents == 3) {
  58. data[r * numBytesPerRow + numComponents * c + 1] = (byte) red;
  59. data[r * numBytesPerRow + numComponents * c + 2] = (byte) green;
  60. data[r * numBytesPerRow + numComponents * c + 3] = (byte) blue;
  61. } else if (numComponents == 4) {
  62. data[r * numBytesPerRow + numComponents * c + 1] = (byte) red;
  63. data[r * numBytesPerRow + numComponents * c + 2] = (byte) green;
  64. data[r * numBytesPerRow + numComponents * c + 3] = (byte) blue;
  65. data[r * numBytesPerRow + numComponents * c + 4] = (byte) alpha;
  66. }
  67. }
  68. }
  69. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  70. DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater());
  71. dos.write(data);
  72. dos.close();
  73. return baos.toByteArray();
  74. }
  75. /**
  76. *
  77. * @return a default ImageSize
  78. */
  79. public static ImageSize getImageSize() {
  80. return new ImageSize(NUM_ROWS, NUM_COLUMNS, DPI);
  81. }
  82. }