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.

BmpImage.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.image;
  18. // Java
  19. import java.io.IOException;
  20. import java.awt.color.ColorSpace;
  21. /**
  22. * Bitmap image.
  23. * This supports loading a bitmap image into bitmap data.
  24. *
  25. * @author Art WELCH
  26. * @see AbstractFopImage
  27. * @see FopImage
  28. */
  29. public class BmpImage extends AbstractFopImage {
  30. /**
  31. * Create a bitmap image with the image data.
  32. *
  33. * @param imgInfo the image information
  34. */
  35. public BmpImage(FopImage.ImageInfo imgInfo) {
  36. super(imgInfo);
  37. }
  38. /**
  39. * Load the bitmap.
  40. * This laods the bitmap data from the bitmap image.
  41. *
  42. * @return true if it was loaded successfully
  43. */
  44. protected boolean loadBitmap() {
  45. int wpos = 18;
  46. int hpos = 22; // offset positioning for w and height in bmp files
  47. int[] headermap = new int[54];
  48. int filepos = 0;
  49. byte[] palette = null;
  50. try {
  51. boolean eof = false;
  52. while ((!eof) && (filepos < 54)) {
  53. int input = inputStream.read();
  54. if (input == -1) {
  55. eof = true;
  56. } else {
  57. headermap[filepos++] = input;
  58. }
  59. }
  60. if (headermap[28] == 4 || headermap[28] == 8) {
  61. int palettesize = 1 << headermap[28];
  62. palette = new byte[palettesize * 3];
  63. int countr = 0;
  64. while (!eof && countr < palettesize) {
  65. int count2 = 2;
  66. while (!eof && count2 >= -1) {
  67. int input = inputStream.read();
  68. if (input == -1) {
  69. eof = true;
  70. } else if (count2 >= 0) {
  71. palette[countr * 3 + count2] = (byte)(input & 0xFF);
  72. }
  73. count2--;
  74. filepos++;
  75. }
  76. countr++;
  77. }
  78. }
  79. } catch (IOException e) {
  80. log.error("Error while loading image "
  81. + "" + " : "
  82. + e.getClass() + " - "
  83. + e.getMessage(), e);
  84. return false;
  85. }
  86. // gets h & w from headermap
  87. this.width = headermap[wpos]
  88. + headermap[wpos + 1] * 256
  89. + headermap[wpos + 2] * 256 * 256
  90. + headermap[wpos + 3] * 256 * 256 * 256;
  91. this.height = headermap[hpos]
  92. + headermap[hpos + 1] * 256
  93. + headermap[hpos + 2] * 256 * 256
  94. + headermap[hpos + 3] * 256 * 256 * 256;
  95. int imagestart = headermap[10]
  96. + headermap[11] * 256
  97. + headermap[12] * 256 * 256
  98. + headermap[13] * 256 * 256 * 256;
  99. this.bitsPerPixel = headermap[28];
  100. this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
  101. int bytes = 0;
  102. if (this.bitsPerPixel == 1) {
  103. bytes = (this.width + 7) / 8;
  104. } else if (this.bitsPerPixel == 24) {
  105. bytes = this.width * 3;
  106. } else if (this.bitsPerPixel == 4 || this.bitsPerPixel == 8) {
  107. bytes = this.width / (8 / this.bitsPerPixel);
  108. } else {
  109. log.error("Image (" + ""
  110. + ") has " + this.bitsPerPixel
  111. + " which is not a supported BMP format.");
  112. return false;
  113. }
  114. if ((bytes & 0x03) != 0) {
  115. bytes |= 0x03;
  116. bytes++;
  117. }
  118. // Should take care of the ColorSpace and bitsPerPixel
  119. this.bitmapsSize = this.width * this.height * 3;
  120. this.bitmaps = new byte[this.bitmapsSize];
  121. int[] temp = new int[bytes * this.height];
  122. try {
  123. int input;
  124. int count = 0;
  125. inputStream.skip((long)(imagestart - filepos));
  126. while ((input = inputStream.read()) != -1) {
  127. if (count >= temp.length) {
  128. log.warn("Data longer than expected while loading image");
  129. break;
  130. } else {
  131. temp[count++] = input;
  132. }
  133. }
  134. inputStream.close();
  135. inputStream = null;
  136. } catch (IOException e) {
  137. log.error("Error while loading image "
  138. + "" + " : "
  139. + e.getClass() + " - "
  140. + e.getMessage(), e);
  141. return false;
  142. }
  143. for (int i = 0; i < this.height; i++) {
  144. int x = 0;
  145. int j = 0;
  146. while (j < bytes) {
  147. int p = temp[(this.height - i - 1) * bytes + j];
  148. if (this.bitsPerPixel == 24 && x < this.width) {
  149. int countr = 2;
  150. do {
  151. this.bitmaps[3 * (i * this.width + x) + countr] =
  152. (byte)(temp[(this.height - i - 1)
  153. * bytes + j] & 0xFF);
  154. j++;
  155. } while (--countr >= 0)
  156. ;
  157. x++;
  158. } else if (this.bitsPerPixel == 1) {
  159. for (int countr = 0;
  160. countr < 8 && x < this.width; countr++) {
  161. if ((p & 0x80) != 0) {
  162. this.bitmaps[3 * (i * this.width + x)] = (byte) 0xFF;
  163. this.bitmaps[3 * (i * this.width + x) + 1] = (byte) 0xFF;
  164. this.bitmaps[3 * (i * this.width + x) + 2] = (byte) 0xFF;
  165. } else {
  166. this.bitmaps[3 * (i * this.width + x)] = (byte) 0;
  167. this.bitmaps[3 * (i * this.width + x) + 1] = (byte) 0;
  168. this.bitmaps[3 * (i * this.width + x) + 2] = (byte) 0;
  169. }
  170. p <<= 1;
  171. x++;
  172. }
  173. j++;
  174. } else if (this.bitsPerPixel == 4) {
  175. for (int countr = 0;
  176. countr < 2 && x < this.width; countr++) {
  177. int pal = ((p & 0xF0) >> 4) * 3;
  178. this.bitmaps[3 * (i * this.width + x)] = palette[pal];
  179. this.bitmaps[3 * (i * this.width + x) + 1] = palette[pal + 1];
  180. this.bitmaps[3 * (i * this.width + x) + 2] = palette[pal + 2];
  181. p <<= 4;
  182. x++;
  183. }
  184. j++;
  185. } else if (this.bitsPerPixel == 8) {
  186. if (x < this.width) {
  187. p *= 3;
  188. this.bitmaps[3 * (i * this.width + x)] = palette[p];
  189. this.bitmaps[3 * (i * this.width + x) + 1] = palette[p + 1];
  190. this.bitmaps[3 * (i * this.width + x) + 2] = palette[p + 2];
  191. j++;
  192. x++;
  193. } else {
  194. j = bytes;
  195. }
  196. } else {
  197. j++;
  198. }
  199. }
  200. }
  201. // This seems really strange to me, but I noticed that
  202. // JimiImage hardcodes bitsPerPixel to 8. If I do not
  203. // do this Acrobat is unable to read the resultant PDF,
  204. // so we will hardcode this...
  205. this.bitsPerPixel = 8;
  206. return true;
  207. }
  208. }