您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BmpImage.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. /**
  8. * FopImage object for BMP images.
  9. * @author Art WELCH
  10. * @see AbstractFopImage
  11. * @see FopImage
  12. */
  13. package org.apache.fop.image;
  14. // Java
  15. import java.net.URL;
  16. import java.io.InputStream;
  17. import java.io.IOException;
  18. import java.awt.color.ColorSpace;
  19. // FOP
  20. import org.apache.fop.image.analyser.ImageReader;
  21. import org.apache.fop.fo.FOUserAgent;
  22. public class BmpImage extends AbstractFopImage {
  23. public BmpImage(URL href, FopImage.ImageInfo imgReader) {
  24. super(href, imgReader);
  25. }
  26. protected boolean loadBitmap(FOUserAgent ua) {
  27. int wpos = 18;
  28. int hpos = 22; // offset positioning for w and height in bmp files
  29. int[] headermap = new int[54];
  30. int filepos = 0;
  31. InputStream file = null;
  32. byte palette[] = null;
  33. try {
  34. file = this.m_href.openStream();
  35. boolean eof = false;
  36. while ((!eof) && (filepos < 54)) {
  37. int input = file.read();
  38. if (input == -1)
  39. eof = true;
  40. else
  41. headermap[filepos++] = input;
  42. }
  43. if (headermap[28] == 4 || headermap[28] == 8) {
  44. int palettesize = 1 << headermap[28];
  45. palette = new byte[palettesize * 3];
  46. int countr = 0;
  47. while (!eof && countr < palettesize) {
  48. int count2 = 2;
  49. while (!eof && count2 >= -1) {
  50. int input = file.read();
  51. if (input == -1)
  52. eof = true;
  53. else if (count2 >= 0) {
  54. palette[countr * 3 + count2] =
  55. (byte)(input & 0xFF);
  56. }
  57. count2--;
  58. filepos++;
  59. }
  60. countr++;
  61. }
  62. }
  63. } catch (IOException e) {
  64. ua.getLogger().error("Error while loading image "
  65. + this.m_href.toString() + " : "
  66. + e.getClass() + " - "
  67. + e.getMessage(), e);
  68. return false;
  69. }
  70. // gets h & w from headermap
  71. this.m_width = headermap[wpos] + headermap[wpos + 1] * 256 +
  72. headermap[wpos + 2] * 256 * 256 +
  73. headermap[wpos + 3] * 256 * 256 * 256;
  74. this.m_height = headermap[hpos] + headermap[hpos + 1] * 256 +
  75. headermap[hpos + 2] * 256 * 256 +
  76. headermap[hpos + 3] * 256 * 256 * 256;
  77. int imagestart = headermap[10] + headermap[11] * 256 +
  78. headermap[12] * 256 * 256 + headermap[13] * 256 * 256 * 256;
  79. this.m_bitsPerPixel = headermap[28];
  80. this.m_colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
  81. int bytes = 0;
  82. if (this.m_bitsPerPixel == 1)
  83. bytes = (this.m_width + 7) / 8;
  84. else if (this.m_bitsPerPixel == 24)
  85. bytes = this.m_width * 3;
  86. else if (this.m_bitsPerPixel == 4 || this.m_bitsPerPixel == 8)
  87. bytes = this.m_width / (8 / this.m_bitsPerPixel);
  88. else {
  89. ua.getLogger().error("Image (" + this.m_href.toString()
  90. + ") has " + this.m_bitsPerPixel
  91. + " which is not a supported BMP format.");
  92. return false;
  93. }
  94. if ((bytes & 0x03) != 0) {
  95. bytes |= 0x03;
  96. bytes++;
  97. }
  98. // Should take care of the ColorSpace and bitsPerPixel
  99. this.m_bitmapsSize = this.m_width * this.m_height * 3;
  100. this.m_bitmaps = new byte[this.m_bitmapsSize];
  101. int[] temp = new int[bytes * this.m_height];
  102. try {
  103. int input;
  104. int count = 0;
  105. file.skip((long)(imagestart - filepos));
  106. while ((input = file.read()) != -1)
  107. temp[count++] = input;
  108. file.close();
  109. } catch (IOException e) {
  110. ua.getLogger().error("Error while loading image "
  111. + this.m_href.toString() + " : "
  112. + e.getClass() + " - "
  113. + e.getMessage(), e);
  114. return false;
  115. }
  116. for (int i = 0; i < this.m_height; i++) {
  117. int x = 0;
  118. int j = 0;
  119. while (j < bytes) {
  120. int p = temp[(this.m_height - i - 1) * bytes + j];
  121. if (this.m_bitsPerPixel == 24 && x < this.m_width) {
  122. int countr = 2;
  123. do {
  124. this.m_bitmaps[3 * (i * this.m_width + x) +
  125. countr] =
  126. (byte)(temp[(this.m_height - i - 1) *
  127. bytes + j] & 0xFF);
  128. j++;
  129. } while (--countr >= 0)
  130. ;
  131. x++;
  132. } else if (this.m_bitsPerPixel == 1) {
  133. for (int countr = 0;
  134. countr < 8 && x < this.m_width; countr++) {
  135. if ((p & 0x80) != 0) {
  136. this.m_bitmaps[3 *
  137. (i * this.m_width + x)] = (byte) 0xFF;
  138. this.m_bitmaps[3 * (i * this.m_width + x) +
  139. 1] = (byte) 0xFF;
  140. this.m_bitmaps[3 * (i * this.m_width + x) +
  141. 2] = (byte) 0xFF;
  142. } else {
  143. this.m_bitmaps[3 *
  144. (i * this.m_width + x)] = (byte) 0;
  145. this.m_bitmaps[3 * (i * this.m_width + x) +
  146. 1] = (byte) 0;
  147. this.m_bitmaps[3 * (i * this.m_width + x) +
  148. 2] = (byte) 0;
  149. }
  150. p <<= 1;
  151. x++;
  152. }
  153. j++;
  154. } else if (this.m_bitsPerPixel == 4) {
  155. for (int countr = 0;
  156. countr < 2 && x < this.m_width; countr++) {
  157. int pal = ((p & 0xF0) >> 4) * 3;
  158. this.m_bitmaps[3 * (i * this.m_width + x)] =
  159. palette[pal];
  160. this.m_bitmaps[3 * (i * this.m_width + x) +
  161. 1] = palette[pal + 1];
  162. this.m_bitmaps[3 * (i * this.m_width + x) +
  163. 2] = palette[pal + 2];
  164. p <<= 4;
  165. x++;
  166. }
  167. j++;
  168. } else if (this.m_bitsPerPixel == 8) {
  169. if (x < this.m_width) {
  170. p *= 3;
  171. this.m_bitmaps[3 * (i * this.m_width + x)] =
  172. palette[p];
  173. this.m_bitmaps[3 * (i * this.m_width + x) +
  174. 1] = palette[p + 1];
  175. this.m_bitmaps[3 * (i * this.m_width + x) +
  176. 2] = palette[p + 2];
  177. j++;
  178. x++;
  179. } else
  180. j = bytes;
  181. } else
  182. j++;
  183. }
  184. }
  185. // This seems really strange to me, but I noticed that JimiImage hardcodes
  186. // m_bitsPerPixel to 8. If I do not do this Acrobat is unable to read the resultant PDF,
  187. // so we will hardcode this...
  188. this.m_bitsPerPixel = 8;
  189. return true;
  190. }
  191. }