Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BmpImage.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 1999-2004 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. // FOP
  22. import org.apache.avalon.framework.logger.Logger;
  23. /**
  24. * Bitmap image.
  25. * This supports loading a bitmap image into bitmap data.
  26. *
  27. * @author Art WELCH
  28. * @see AbstractFopImage
  29. * @see FopImage
  30. */
  31. public class BmpImage extends AbstractFopImage {
  32. /**
  33. * Create a bitmap image with the image data.
  34. *
  35. * @param imgInfo the image information
  36. */
  37. public BmpImage(FopImage.ImageInfo imgInfo) {
  38. super(imgInfo);
  39. }
  40. /**
  41. * Load the bitmap.
  42. * This laods the bitmap data from the bitmap image.
  43. *
  44. * @param ua the user agent
  45. * @return true if it was loaded successfully
  46. */
  47. protected boolean loadBitmap(Logger logger) {
  48. int wpos = 18;
  49. int hpos = 22; // offset positioning for w and height in bmp files
  50. int[] headermap = new int[54];
  51. int filepos = 0;
  52. byte palette[] = null;
  53. try {
  54. boolean eof = false;
  55. while ((!eof) && (filepos < 54)) {
  56. int input = inputStream.read();
  57. if (input == -1) {
  58. eof = true;
  59. } else {
  60. headermap[filepos++] = input;
  61. }
  62. }
  63. if (headermap[28] == 4 || headermap[28] == 8) {
  64. int palettesize = 1 << headermap[28];
  65. palette = new byte[palettesize * 3];
  66. int countr = 0;
  67. while (!eof && countr < palettesize) {
  68. int count2 = 2;
  69. while (!eof && count2 >= -1) {
  70. int input = inputStream.read();
  71. if (input == -1) {
  72. eof = true;
  73. } else if (count2 >= 0) {
  74. palette[countr * 3 + count2] =
  75. (byte)(input & 0xFF);
  76. }
  77. count2--;
  78. filepos++;
  79. }
  80. countr++;
  81. }
  82. }
  83. } catch (IOException e) {
  84. logger.error("Error while loading image "
  85. + "" + " : "
  86. + e.getClass() + " - "
  87. + e.getMessage(), e);
  88. return false;
  89. }
  90. // gets h & w from headermap
  91. this.width = headermap[wpos]
  92. + headermap[wpos + 1] * 256
  93. + headermap[wpos + 2] * 256 * 256
  94. + headermap[wpos + 3] * 256 * 256 * 256;
  95. this.height = headermap[hpos]
  96. + headermap[hpos + 1] * 256
  97. + headermap[hpos + 2] * 256 * 256
  98. + headermap[hpos + 3] * 256 * 256 * 256;
  99. int imagestart = headermap[10]
  100. + headermap[11] * 256
  101. + headermap[12] * 256 * 256
  102. + headermap[13] * 256 * 256 * 256;
  103. this.bitsPerPixel = headermap[28];
  104. this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
  105. int bytes = 0;
  106. if (this.bitsPerPixel == 1) {
  107. bytes = (this.width + 7) / 8;
  108. } else if (this.bitsPerPixel == 24) {
  109. bytes = this.width * 3;
  110. } else if (this.bitsPerPixel == 4 || this.bitsPerPixel == 8) {
  111. bytes = this.width / (8 / this.bitsPerPixel);
  112. } else {
  113. logger.error("Image (" + ""
  114. + ") has " + this.bitsPerPixel
  115. + " which is not a supported BMP format.");
  116. return false;
  117. }
  118. if ((bytes & 0x03) != 0) {
  119. bytes |= 0x03;
  120. bytes++;
  121. }
  122. // Should take care of the ColorSpace and bitsPerPixel
  123. this.bitmapsSize = this.width * this.height * 3;
  124. this.bitmaps = new byte[this.bitmapsSize];
  125. int[] temp = new int[bytes * this.height];
  126. try {
  127. int input;
  128. int count = 0;
  129. inputStream.skip((long)(imagestart - filepos));
  130. while ((input = inputStream.read()) != -1) {
  131. temp[count++] = input;
  132. }
  133. inputStream.close();
  134. inputStream = null;
  135. } catch (IOException e) {
  136. logger.error("Error while loading image "
  137. + "" + " : "
  138. + e.getClass() + " - "
  139. + e.getMessage(), e);
  140. return false;
  141. }
  142. for (int i = 0; i < this.height; i++) {
  143. int x = 0;
  144. int j = 0;
  145. while (j < bytes) {
  146. int p = temp[(this.height - i - 1) * bytes + j];
  147. if (this.bitsPerPixel == 24 && x < this.width) {
  148. int countr = 2;
  149. do {
  150. this.bitmaps[3 * (i * this.width + x) + countr] =
  151. (byte)(temp[(this.height - i - 1)
  152. * bytes + j] & 0xFF);
  153. j++;
  154. } while (--countr >= 0)
  155. ;
  156. x++;
  157. } else if (this.bitsPerPixel == 1) {
  158. for (int countr = 0;
  159. countr < 8 && x < this.width; countr++) {
  160. if ((p & 0x80) != 0) {
  161. this.bitmaps[3 * (i * this.width + x)] = (byte) 0xFF;
  162. this.bitmaps[3 * (i * this.width + x) + 1] = (byte) 0xFF;
  163. this.bitmaps[3 * (i * this.width + x) + 2] = (byte) 0xFF;
  164. } else {
  165. this.bitmaps[3 * (i * this.width + x)] = (byte) 0;
  166. this.bitmaps[3 * (i * this.width + x) + 1] = (byte) 0;
  167. this.bitmaps[3 * (i * this.width + x) + 2] = (byte) 0;
  168. }
  169. p <<= 1;
  170. x++;
  171. }
  172. j++;
  173. } else if (this.bitsPerPixel == 4) {
  174. for (int countr = 0;
  175. countr < 2 && x < this.width; countr++) {
  176. int pal = ((p & 0xF0) >> 4) * 3;
  177. this.bitmaps[3 * (i * this.width + x)] = palette[pal];
  178. this.bitmaps[3 * (i * this.width + x) + 1] = palette[pal + 1];
  179. this.bitmaps[3 * (i * this.width + x) + 2] = palette[pal + 2];
  180. p <<= 4;
  181. x++;
  182. }
  183. j++;
  184. } else if (this.bitsPerPixel == 8) {
  185. if (x < this.width) {
  186. p *= 3;
  187. this.bitmaps[3 * (i * this.width + x)] = palette[p];
  188. this.bitmaps[3 * (i * this.width + x) + 1] = palette[p + 1];
  189. this.bitmaps[3 * (i * this.width + x) + 2] = palette[p + 2];
  190. j++;
  191. x++;
  192. } else {
  193. j = bytes;
  194. }
  195. } else {
  196. j++;
  197. }
  198. }
  199. }
  200. // This seems really strange to me, but I noticed that
  201. // JimiImage hardcodes bitsPerPixel to 8. If I do not
  202. // do this Acrobat is unable to read the resultant PDF,
  203. // so we will hardcode this...
  204. this.bitsPerPixel = 8;
  205. return true;
  206. }
  207. }