load-bmp.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. // Copyright (C) 2004-2024 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "image-imp.h"
  24. #include "pixmap-imp.h"
  25. #include <string.h>
  26. #include <limits.h>
  27. #undef BMP_DEBUG
  28. static const unsigned char web_palette[] = {
  29. 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x66, 0x00, 0x00, 0x99, 0x00, 0x00, 0xCC, 0x00, 0x00, 0xFF, 0x00, 0x00,
  30. 0x00, 0x00, 0x33, 0x33, 0x00, 0x33, 0x66, 0x00, 0x33, 0x99, 0x00, 0x33, 0xCC, 0x00, 0x33, 0xFF, 0x00, 0x33,
  31. 0x00, 0x00, 0x66, 0x33, 0x00, 0x66, 0x66, 0x00, 0x66, 0x99, 0x00, 0x66, 0xCC, 0x00, 0x66, 0xFF, 0x00, 0x66,
  32. 0x00, 0x00, 0x99, 0x33, 0x00, 0x99, 0x66, 0x00, 0x99, 0x99, 0x00, 0x99, 0xCC, 0x00, 0x99, 0xFF, 0x00, 0x99,
  33. 0x00, 0x00, 0xCC, 0x33, 0x00, 0xCC, 0x66, 0x00, 0xCC, 0x99, 0x00, 0xCC, 0xCC, 0x00, 0xCC, 0xFF, 0x00, 0xCC,
  34. 0x00, 0x00, 0xFF, 0x33, 0x00, 0xFF, 0x66, 0x00, 0xFF, 0x99, 0x00, 0xFF, 0xCC, 0x00, 0xFF, 0xFF, 0x00, 0xFF,
  35. 0x00, 0x33, 0x00, 0x33, 0x33, 0x00, 0x66, 0x33, 0x00, 0x99, 0x33, 0x00, 0xCC, 0x33, 0x00, 0xFF, 0x33, 0x00,
  36. 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x66, 0x33, 0x33, 0x99, 0x33, 0x33, 0xCC, 0x33, 0x33, 0xFF, 0x33, 0x33,
  37. 0x00, 0x33, 0x66, 0x33, 0x33, 0x66, 0x66, 0x33, 0x66, 0x99, 0x33, 0x66, 0xCC, 0x33, 0x66, 0xFF, 0x33, 0x66,
  38. 0x00, 0x33, 0x99, 0x33, 0x33, 0x99, 0x66, 0x33, 0x99, 0x99, 0x33, 0x99, 0xCC, 0x33, 0x99, 0xFF, 0x33, 0x99,
  39. 0x00, 0x33, 0xCC, 0x33, 0x33, 0xCC, 0x66, 0x33, 0xCC, 0x99, 0x33, 0xCC, 0xCC, 0x33, 0xCC, 0xFF, 0x33, 0xCC,
  40. 0x00, 0x33, 0xFF, 0x33, 0x33, 0xFF, 0x66, 0x33, 0xFF, 0x99, 0x33, 0xFF, 0xCC, 0x33, 0xFF, 0xFF, 0x33, 0xFF,
  41. 0x00, 0x66, 0x00, 0x33, 0x66, 0x00, 0x66, 0x66, 0x00, 0x99, 0x66, 0x00, 0xCC, 0x66, 0x00, 0xFF, 0x66, 0x00,
  42. 0x00, 0x66, 0x33, 0x33, 0x66, 0x33, 0x66, 0x66, 0x33, 0x99, 0x66, 0x33, 0xCC, 0x66, 0x33, 0xFF, 0x66, 0x33,
  43. 0x00, 0x66, 0x66, 0x33, 0x66, 0x66, 0x66, 0x66, 0x66, 0x99, 0x66, 0x66, 0xCC, 0x66, 0x66, 0xFF, 0x66, 0x66,
  44. 0x00, 0x66, 0x99, 0x33, 0x66, 0x99, 0x66, 0x66, 0x99, 0x99, 0x66, 0x99, 0xCC, 0x66, 0x99, 0xFF, 0x66, 0x99,
  45. 0x00, 0x66, 0xCC, 0x33, 0x66, 0xCC, 0x66, 0x66, 0xCC, 0x99, 0x66, 0xCC, 0xCC, 0x66, 0xCC, 0xFF, 0x66, 0xCC,
  46. 0x00, 0x66, 0xFF, 0x33, 0x66, 0xFF, 0x66, 0x66, 0xFF, 0x99, 0x66, 0xFF, 0xCC, 0x66, 0xFF, 0xFF, 0x66, 0xFF,
  47. 0x00, 0x99, 0x00, 0x33, 0x99, 0x00, 0x66, 0x99, 0x00, 0x99, 0x99, 0x00, 0xCC, 0x99, 0x00, 0xFF, 0x99, 0x00,
  48. 0x00, 0x99, 0x33, 0x33, 0x99, 0x33, 0x66, 0x99, 0x33, 0x99, 0x99, 0x33, 0xCC, 0x99, 0x33, 0xFF, 0x99, 0x33,
  49. 0x00, 0x99, 0x66, 0x33, 0x99, 0x66, 0x66, 0x99, 0x66, 0x99, 0x99, 0x66, 0xCC, 0x99, 0x66, 0xFF, 0x99, 0x66,
  50. 0x00, 0x99, 0x99, 0x33, 0x99, 0x99, 0x66, 0x99, 0x99, 0x99, 0x99, 0x99, 0xCC, 0x99, 0x99, 0xFF, 0x99, 0x99,
  51. 0x00, 0x99, 0xCC, 0x33, 0x99, 0xCC, 0x66, 0x99, 0xCC, 0x99, 0x99, 0xCC, 0xCC, 0x99, 0xCC, 0xFF, 0x99, 0xCC,
  52. 0x00, 0x99, 0xFF, 0x33, 0x99, 0xFF, 0x66, 0x99, 0xFF, 0x99, 0x99, 0xFF, 0xCC, 0x99, 0xFF, 0xFF, 0x99, 0xFF,
  53. 0x00, 0xCC, 0x00, 0x33, 0xCC, 0x00, 0x66, 0xCC, 0x00, 0x99, 0xCC, 0x00, 0xCC, 0xCC, 0x00, 0xFF, 0xCC, 0x00,
  54. 0x00, 0xCC, 0x33, 0x33, 0xCC, 0x33, 0x66, 0xCC, 0x33, 0x99, 0xCC, 0x33, 0xCC, 0xCC, 0x33, 0xFF, 0xCC, 0x33,
  55. 0x00, 0xCC, 0x66, 0x33, 0xCC, 0x66, 0x66, 0xCC, 0x66, 0x99, 0xCC, 0x66, 0xCC, 0xCC, 0x66, 0xFF, 0xCC, 0x66,
  56. 0x00, 0xCC, 0x99, 0x33, 0xCC, 0x99, 0x66, 0xCC, 0x99, 0x99, 0xCC, 0x99, 0xCC, 0xCC, 0x99, 0xFF, 0xCC, 0x99,
  57. 0x00, 0xCC, 0xCC, 0x33, 0xCC, 0xCC, 0x66, 0xCC, 0xCC, 0x99, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xFF, 0xCC, 0xCC,
  58. 0x00, 0xCC, 0xFF, 0x33, 0xCC, 0xFF, 0x66, 0xCC, 0xFF, 0x99, 0xCC, 0xFF, 0xCC, 0xCC, 0xFF, 0xFF, 0xCC, 0xFF,
  59. 0x00, 0xFF, 0x00, 0x33, 0xFF, 0x00, 0x66, 0xFF, 0x00, 0x99, 0xFF, 0x00, 0xCC, 0xFF, 0x00, 0xFF, 0xFF, 0x00,
  60. 0x00, 0xFF, 0x33, 0x33, 0xFF, 0x33, 0x66, 0xFF, 0x33, 0x99, 0xFF, 0x33, 0xCC, 0xFF, 0x33, 0xFF, 0xFF, 0x33,
  61. 0x00, 0xFF, 0x66, 0x33, 0xFF, 0x66, 0x66, 0xFF, 0x66, 0x99, 0xFF, 0x66, 0xCC, 0xFF, 0x66, 0xFF, 0xFF, 0x66,
  62. 0x00, 0xFF, 0x99, 0x33, 0xFF, 0x99, 0x66, 0xFF, 0x99, 0x99, 0xFF, 0x99, 0xCC, 0xFF, 0x99, 0xFF, 0xFF, 0x99,
  63. 0x00, 0xFF, 0xCC, 0x33, 0xFF, 0xCC, 0x66, 0xFF, 0xCC, 0x99, 0xFF, 0xCC, 0xCC, 0xFF, 0xCC, 0xFF, 0xFF, 0xCC,
  64. 0x00, 0xFF, 0xFF, 0x33, 0xFF, 0xFF, 0x66, 0xFF, 0xFF, 0x99, 0xFF, 0xFF, 0xCC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. };
  73. static const unsigned char vga_palette[] = {
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0x00, 0xAA, 0x00, 0x00, 0xAA, 0xAA,
  75. 0xAA, 0x00, 0x00, 0xAA, 0x00, 0xAA, 0xAA, 0x55, 0x00, 0xAA, 0xAA, 0xAA,
  76. 0x55, 0x55, 0x55, 0x55, 0x55, 0xFF, 0x55, 0xFF, 0x55, 0x55, 0xFF, 0xFF,
  77. 0xFF, 0x55, 0x55, 0xFF, 0x55, 0xFF, 0xFF, 0xFF, 0x55, 0xFF, 0xFF, 0xFF,
  78. };
  79. static const unsigned char gray_palette[] = {
  80. 0x00, 0x00, 0x00, 0x54, 0x54, 0x54,
  81. 0xA8, 0xA8, 0xA8, 0xFF, 0xFF, 0xFF,
  82. };
  83. static const unsigned char bw_palette[] = {
  84. 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
  85. };
  86. enum {
  87. BI_NONE = 0,
  88. BI_RLE8 = 1,
  89. BI_RLE4 = 2,
  90. BI_BITFIELDS = 3,
  91. BI_HUFFMAN1D = 3,
  92. BI_JPEG = 4,
  93. BI_RLE24 = 4,
  94. BI_PNG = 5,
  95. BI_ALPHABITS = 6,
  96. };
  97. struct info
  98. {
  99. char type[2];
  100. uint32_t version;
  101. uint32_t bitmapoffset;
  102. uint32_t width, height;
  103. uint16_t bitcount;
  104. uint32_t compression;
  105. uint32_t bitmapsize;
  106. uint32_t xres, yres;
  107. uint32_t colors;
  108. uint32_t rmask, gmask, bmask, amask;
  109. uint8_t palette[256 * 3];
  110. uint32_t colorspacetype;
  111. uint32_t endpoints[3 * 3];
  112. uint32_t gamma[3];
  113. uint32_t intent;
  114. uint32_t profileoffset;
  115. uint32_t profilesize;
  116. int topdown;
  117. unsigned int rshift, gshift, bshift, ashift;
  118. unsigned int rbits, gbits, bbits, abits;
  119. unsigned char *samples;
  120. fz_colorspace *cs;
  121. };
  122. #define read8(p) ((p)[0])
  123. #define read16(p) (((p)[1] << 8) | (p)[0])
  124. #define read32(p) (((p)[3] << 24) | ((p)[2] << 16) | ((p)[1] << 8) | (p)[0])
  125. #define DPM_TO_DPI(dpm) ((dpm) * 25.4f / 1000.0f)
  126. #define is_bitmap_array(p) ((p)[0] == 'B' && (p)[1] == 'A')
  127. #define is_bitmap(p) ((p)[0] == 'B' && (p)[1] == 'M')
  128. #define is_os2_bmp(info) ((info)->version == 12 || (info)->version == 16 || (info)->version == 64)
  129. #define is_win_bmp(info) ((info)->version == 12 || (info)->version == 40 || (info)->version == 52 || (info)->version == 56 || (info)->version == 108 || (info)->version == 124)
  130. #define is_valid_win_compression(info) (is_win_bmp(info) && ((info)->compression == BI_NONE || (info)->compression == BI_RLE8 || (info)->compression == BI_RLE4 || (info)->compression == BI_BITFIELDS || (info)->compression == BI_JPEG || (info)->compression == BI_PNG || (info)->compression == BI_ALPHABITS || (info)->compression == BI_RLE24))
  131. #define is_valid_os2_compression(info) (is_os2_bmp(info) && ((info)->compression == BI_NONE || (info)->compression == BI_RLE8 || (info)->compression == BI_RLE4 || (info)->compression == BI_HUFFMAN1D || (info)->compression == BI_RLE24))
  132. #define is_valid_compression(info) (is_valid_win_compression(info) || is_valid_os2_compression(info))
  133. #define is_valid_rgb_bitcount(info) ((info)->compression == BI_NONE && ((info)->bitcount == 1 || (info)->bitcount == 2 || (info)->bitcount == 4 || (info)->bitcount == 8 || (info)->bitcount == 16 || (info)->bitcount == 24 || (info)->bitcount == 32 || (info)->bitcount == 64))
  134. #define is_valid_rle8_bitcount(info) ((info)->compression == BI_RLE8 && (info)->bitcount == 8)
  135. #define is_valid_rle4_bitcount(info) ((info)->compression == BI_RLE4 && (info)->bitcount == 4)
  136. #define is_valid_bitfields_bitcount(info) (is_win_bmp(info) && (info)->compression == BI_BITFIELDS && ((info)->bitcount == 16 || (info)->bitcount == 32))
  137. #define is_valid_jpeg_bitcount(info) (is_win_bmp(info) && (info)->compression == BI_JPEG && (info)->bitcount == 0)
  138. #define is_valid_png_bitcount(info) (is_win_bmp(info) && (info)->compression == BI_PNG && (info)->bitcount == 0)
  139. #define is_valid_alphabits_bitcount(info) (is_win_bmp(info) && (info)->compression == BI_ALPHABITS && ((info)->bitcount == 16 || (info)->bitcount == 32))
  140. #define is_valid_rle24_bitcount(info) (is_os2_bmp(info) && (info)->compression == BI_RLE24 && (info)->bitcount == 24)
  141. #define is_valid_huffman1d_bitcount(info) (is_os2_bmp(info) && (info)->compression == BI_HUFFMAN1D && (info)->bitcount == 1)
  142. #define is_valid_bitcount(info) (is_valid_rgb_bitcount(info) || is_valid_rle8_bitcount(info) || is_valid_rle4_bitcount(info) || is_valid_bitfields_bitcount(info) || is_valid_jpeg_bitcount(info) || is_valid_png_bitcount(info) || is_valid_alphabits_bitcount(info) || is_valid_rle24_bitcount(info) || is_valid_huffman1d_bitcount(info))
  143. #define has_palette(info) ((info)->bitcount == 1 || (info)->bitcount == 2 || (info)->bitcount == 4 || (info)->bitcount == 8)
  144. #define has_color_masks(info) (((info)->bitcount == 16 || (info)->bitcount == 32) && (info)->version == 40 && ((info)->compression == BI_BITFIELDS || (info)->compression == BI_ALPHABITS))
  145. #define has_color_profile(info) ((info)->version == 108 || (info)->version == 124)
  146. #define palette_entry_size(info) ((info)->version == 12 ? 3 : 4)
  147. static const unsigned char *
  148. bmp_read_file_header(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end, const unsigned char *p)
  149. {
  150. if (end - p < 14)
  151. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in file header in bmp image");
  152. if (!is_bitmap(p))
  153. fz_throw(ctx, FZ_ERROR_FORMAT, "invalid signature %02x%02x in bmp image", p[0], p[1]);
  154. info->type[0] = read8(p + 0);
  155. info->type[1] = read8(p + 1);
  156. /* read32(p+2) == file or header size */
  157. /* read16(p+6) == hotspot x for icons/cursors */
  158. /* read16(p+8) == hotspot y for icons/cursors */
  159. info->bitmapoffset = read32(p + 10);
  160. return p + 14;
  161. }
  162. static unsigned char *
  163. bmp_decompress_huffman1d(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char **end)
  164. {
  165. fz_stream *encstm, *decstm;
  166. fz_buffer *buf;
  167. unsigned char *decoded;
  168. size_t size;
  169. encstm = fz_open_memory(ctx, p, *end - p);
  170. fz_var(decstm);
  171. fz_var(buf);
  172. fz_try(ctx)
  173. {
  174. decstm = fz_open_faxd(ctx, encstm,
  175. 0, /* 1 dimensional encoding */
  176. 0, /* end of line not required */
  177. 0, /* encoded byte align */
  178. info->width, info->height,
  179. 0, /* end of block expected */
  180. 1 /* black is 1 */
  181. );
  182. buf = fz_read_all(ctx, decstm, 1024);
  183. size = fz_buffer_extract(ctx, buf, &decoded);
  184. *end = decoded + size;
  185. }
  186. fz_always(ctx)
  187. {
  188. fz_drop_buffer(ctx, buf);
  189. fz_drop_stream(ctx, decstm);
  190. fz_drop_stream(ctx, encstm);
  191. }
  192. fz_catch(ctx)
  193. fz_rethrow(ctx);
  194. return decoded;
  195. }
  196. static unsigned char *
  197. bmp_decompress_rle24(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char **end)
  198. {
  199. const unsigned char *sp;
  200. unsigned char *dp, *ep, *decompressed;
  201. uint32_t width = info->width;
  202. uint32_t height = info->height;
  203. uint32_t stride;
  204. uint32_t x, y;
  205. int i;
  206. stride = (width*3 + 3) / 4 * 4;
  207. sp = p;
  208. dp = decompressed = fz_calloc(ctx, height, stride);
  209. ep = dp + height * stride;
  210. x = 0;
  211. y = 0;
  212. while (sp + 2 <= *end)
  213. {
  214. if (sp[0] == 0 && sp[1] == 0)
  215. { /* end of line */
  216. sp += 2;
  217. x = 0;
  218. y++;
  219. }
  220. else if (sp[0] == 0 && sp[1] == 1)
  221. { /* end of bitmap */
  222. sp += 2;
  223. break;
  224. }
  225. else if (sp[0] == 0 && sp[1] == 2)
  226. { /* delta */
  227. sp += 2;
  228. x += sp < *end ? *sp++ : 0;
  229. y += sp < *end ? *sp++ : 0;
  230. }
  231. else if (sp[0] == 0 && sp[1] >= 3)
  232. { /* absolute */
  233. int dn, sn, pad;
  234. dn = sp[1];
  235. sn = (dn * 3 + 1) / 2 * 2;
  236. pad = sn & 1;
  237. sp += 2;
  238. if (sn > *end - sp)
  239. {
  240. fz_warn(ctx, "premature end of pixel data in absolute code in bmp image");
  241. sn = ((*end - sp) / 3) * 3;
  242. pad = (*end - sp) % 3;
  243. dn = sn / 3;
  244. }
  245. else if (sn + pad > *end - sp)
  246. {
  247. fz_warn(ctx, "premature end of padding in absolute code in bmp image");
  248. pad = 0;
  249. }
  250. for (i = 0; i < dn; i++)
  251. {
  252. uint32_t actualx = x;
  253. uint32_t actualy = y;
  254. if (actualx >= width || actualy >= height)
  255. {
  256. actualx = x % width;
  257. actualy = y + x / width;
  258. }
  259. if (actualx < width && actualy < height)
  260. {
  261. dp = decompressed + actualy * stride + actualx * 3;
  262. *dp++ = sp[i * 3 + 0];
  263. *dp++ = sp[i * 3 + 1];
  264. *dp++ = sp[i * 3 + 2];
  265. }
  266. x++;
  267. }
  268. sp += sn + pad;
  269. }
  270. else
  271. { /* encoded */
  272. int dn, sn;
  273. dn = sp[0];
  274. sn = 3;
  275. sp++;
  276. if (sn > *end - sp)
  277. {
  278. fz_warn(ctx, "premature end of pixel data in encoded code in bmp image");
  279. sn = 0;
  280. dn = 0;
  281. }
  282. for (i = 0; i < dn; i++)
  283. {
  284. uint32_t actualx = x;
  285. uint32_t actualy = y;
  286. if (actualx >= width || actualy >= height)
  287. {
  288. actualx = x % width;
  289. actualy = y + x / width;
  290. }
  291. if (actualx < width && actualy < height)
  292. {
  293. dp = decompressed + actualy * stride + actualx * 3;
  294. *dp++ = sp[0];
  295. *dp++ = sp[1];
  296. *dp++ = sp[2];
  297. }
  298. x++;
  299. }
  300. sp += sn;
  301. }
  302. }
  303. info->compression = BI_NONE;
  304. info->bitcount = 24;
  305. *end = ep;
  306. return decompressed;
  307. }
  308. static unsigned char *
  309. bmp_decompress_rle8(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char **end)
  310. {
  311. const unsigned char *sp;
  312. unsigned char *dp, *ep, *decompressed;
  313. uint32_t width = info->width;
  314. uint32_t height = info->height;
  315. uint32_t stride;
  316. uint32_t x, y;
  317. int i;
  318. stride = (width + 3) / 4 * 4;
  319. sp = p;
  320. dp = decompressed = fz_calloc(ctx, height, stride);
  321. ep = dp + height * stride;
  322. x = 0;
  323. y = 0;
  324. while (sp + 2 <= *end)
  325. {
  326. if (sp[0] == 0 && sp[1] == 0)
  327. { /* end of line */
  328. sp += 2;
  329. x = 0;
  330. y++;
  331. }
  332. else if (sp[0] == 0 && sp[1] == 1)
  333. { /* end of bitmap */
  334. sp += 2;
  335. break;
  336. }
  337. else if (sp[0] == 0 && sp[1] == 2)
  338. { /* delta */
  339. sp +=2;
  340. x += sp < *end ? *sp++ : 0;
  341. y += sp < *end ? *sp++ : 0;
  342. }
  343. else if (sp[0] == 0 && sp[1] >= 3)
  344. { /* absolute */
  345. int dn, sn, pad;
  346. dn = sp[1];
  347. sn = dn;
  348. pad = sn & 1;
  349. sp += 2;
  350. if (sn > *end - sp)
  351. {
  352. fz_warn(ctx, "premature end of pixel data in absolute code in bmp image");
  353. sn = *end - sp;
  354. pad = 0;
  355. dn = sn;
  356. }
  357. else if (sn + pad > *end - sp)
  358. {
  359. fz_warn(ctx, "premature end of padding in absolute code in bmp image");
  360. pad = 0;
  361. }
  362. for (i = 0; i < dn; i++)
  363. {
  364. uint32_t actualx = x;
  365. uint32_t actualy = y;
  366. if (actualx >= width || actualy >= height)
  367. {
  368. actualx = x % width;
  369. actualy = y + x / width;
  370. }
  371. if (actualx < width && actualy < height)
  372. {
  373. dp = decompressed + actualy * stride + actualx;
  374. *dp++ = sp[i];
  375. }
  376. x++;
  377. }
  378. sp += sn + pad;
  379. }
  380. else
  381. { /* encoded */
  382. int dn, sn;
  383. dn = sp[0];
  384. sn = 1;
  385. sp++;
  386. for (i = 0; i < dn; i++)
  387. {
  388. uint32_t actualx = x;
  389. uint32_t actualy = y;
  390. if (actualx >= width || actualy >= height)
  391. {
  392. actualx = x % width;
  393. actualy = y + x / width;
  394. }
  395. if (actualx < width && actualy < height)
  396. {
  397. dp = decompressed + actualy * stride + actualx;
  398. *dp++ = sp[0];
  399. }
  400. x++;
  401. }
  402. sp += sn;
  403. }
  404. }
  405. info->compression = BI_NONE;
  406. info->bitcount = 8;
  407. *end = ep;
  408. return decompressed;
  409. }
  410. static unsigned char *
  411. bmp_decompress_rle4(fz_context *ctx, struct info *info, const unsigned char *p, const unsigned char **end)
  412. {
  413. const unsigned char *sp;
  414. unsigned char *dp, *ep, *decompressed;
  415. uint32_t width = info->width;
  416. uint32_t height = info->height;
  417. uint32_t stride;
  418. uint32_t x, y;
  419. int i;
  420. stride = ((width + 1) / 2 + 3) / 4 * 4;
  421. sp = p;
  422. dp = decompressed = fz_calloc(ctx, height, stride);
  423. ep = dp + height * stride;
  424. x = 0;
  425. y = 0;
  426. while (sp + 2 <= *end)
  427. {
  428. if (sp[0] == 0 && sp[1] == 0)
  429. { /* end of line */
  430. sp += 2;
  431. x = 0;
  432. y++;
  433. }
  434. else if (sp[0] == 0 && sp[1] == 1)
  435. { /* end of bitmap */
  436. sp += 2;
  437. break;
  438. }
  439. else if (sp[0] == 0 && sp[1] == 2)
  440. { /* delta */
  441. sp += 2;
  442. x += sp < *end ? *sp++ : 0;
  443. y += sp < *end ? *sp++ : 0;
  444. }
  445. else if (sp[0] == 0 && sp[1] >= 3)
  446. { /* absolute */
  447. int dn, sn, pad;
  448. dn = sp[1];
  449. sn = (dn + 1) / 2;
  450. pad = sn & 1;
  451. sp += 2;
  452. if (sn > *end - sp)
  453. {
  454. fz_warn(ctx, "premature end of pixel data in absolute code in bmp image");
  455. sn = *end - sp;
  456. pad = 0;
  457. dn = sn * 2;
  458. }
  459. else if (sn + pad > *end - sp)
  460. {
  461. fz_warn(ctx, "premature end of padding in absolute code in bmp image");
  462. pad = 0;
  463. }
  464. for (i = 0; i < dn; i++)
  465. {
  466. uint32_t actualx = x;
  467. uint32_t actualy = y;
  468. if (actualx >= width || actualy >= height)
  469. {
  470. actualx = x % width;
  471. actualy = y + x / width;
  472. }
  473. if (actualx < width && actualy < height)
  474. {
  475. int val = i & 1 ? (sp[i >> 1]) & 0xF : (sp[i >> 1] >> 4) & 0xF;
  476. dp = decompressed + actualy * stride + actualx / 2;
  477. if (x & 1)
  478. *dp++ |= val;
  479. else
  480. *dp |= val << 4;
  481. }
  482. x++;
  483. }
  484. sp += sn + pad;
  485. }
  486. else
  487. { /* encoded */
  488. int dn, sn;
  489. dn = sp[0];
  490. sn = 1;
  491. sp++;
  492. for (i = 0; i < dn; i++)
  493. {
  494. uint32_t actualx = x;
  495. uint32_t actualy = y;
  496. if (actualx >= width || actualy >= height)
  497. {
  498. actualx = x % width;
  499. actualy = y + x / width;
  500. }
  501. if (actualx < width && actualy < height)
  502. {
  503. int val = i & 1 ? (sp[0] & 0xf) : (sp[0] >> 4) & 0xf;
  504. dp = decompressed + actualy * stride + actualx / 2;
  505. if (x & 1)
  506. *dp++ |= val;
  507. else
  508. *dp |= val << 4;
  509. }
  510. x++;
  511. }
  512. sp += sn;
  513. }
  514. }
  515. info->compression = BI_NONE;
  516. info->bitcount = 4;
  517. *end = ep;
  518. return decompressed;
  519. }
  520. static fz_pixmap *
  521. bmp_read_bitmap(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end, const unsigned char *p)
  522. {
  523. const unsigned int mults[] = { 0, 8191, 2730, 1170, 546, 264, 130, 64 };
  524. fz_pixmap *pix;
  525. const unsigned char *ssp;
  526. unsigned char *ddp;
  527. unsigned char *decompressed = NULL;
  528. uint32_t bitcount;
  529. uint32_t width;
  530. int32_t height;
  531. uint32_t sstride;
  532. int32_t dstride;
  533. unsigned int rmult, gmult, bmult, amult;
  534. unsigned int rtrunc, gtrunc, btrunc, atrunc;
  535. uint32_t x;
  536. int32_t y;
  537. assert(info->width > 0 && info->width <= SHRT_MAX);
  538. assert(info->height > 0 && info->height <= SHRT_MAX);
  539. if (info->compression == BI_NONE)
  540. ssp = p;
  541. else if (info->compression == BI_RLE4)
  542. ssp = decompressed = bmp_decompress_rle4(ctx, info, p, &end);
  543. else if (info->compression == BI_RLE8)
  544. ssp = decompressed = bmp_decompress_rle8(ctx, info, p, &end);
  545. else if (is_win_bmp(info) && (info->compression == BI_BITFIELDS || info->compression == BI_ALPHABITS))
  546. ssp = p;
  547. else if (is_os2_bmp(info) && info->compression == BI_RLE24)
  548. ssp = decompressed = bmp_decompress_rle24(ctx, info, p, &end);
  549. else if (is_os2_bmp(info) && info->compression == BI_HUFFMAN1D)
  550. ssp = decompressed = bmp_decompress_huffman1d(ctx, info, p, &end);
  551. else
  552. fz_throw(ctx, FZ_ERROR_FORMAT, "unhandled compression (%u) in bmp image", info->compression);
  553. bitcount = info->bitcount;
  554. width = info->width;
  555. height = info->height;
  556. sstride = ((width * bitcount + 31) / 32) * 4;
  557. if (ssp + sstride * height > end)
  558. {
  559. int32_t h = (end - ssp) / sstride;
  560. if (h == 0 || h > SHRT_MAX)
  561. {
  562. fz_free(ctx, decompressed);
  563. fz_throw(ctx, FZ_ERROR_LIMIT, "image dimensions out of range in bmp image");
  564. }
  565. }
  566. fz_try(ctx)
  567. {
  568. pix = fz_new_pixmap(ctx, info->cs, width, height, NULL, 1);
  569. fz_set_pixmap_resolution(ctx, pix, info->xres, info->yres);
  570. fz_clear_pixmap(ctx, pix);
  571. }
  572. fz_catch(ctx)
  573. {
  574. fz_free(ctx, decompressed);
  575. fz_rethrow(ctx);
  576. }
  577. ddp = pix->samples;
  578. dstride = pix->stride;
  579. if (!info->topdown)
  580. {
  581. ddp = pix->samples + ((size_t) (height - 1)) * ((size_t) dstride);
  582. dstride = -dstride;
  583. }
  584. if (ssp + sstride * height > end)
  585. {
  586. fz_warn(ctx, "premature end in bitmap data in bmp image");
  587. height = (end - ssp) / sstride;
  588. }
  589. /* These are only used for 16- and 32-bit components
  590. 1-bit (1 * 8191) / 32
  591. 2-bit (3 * 2730) / 32
  592. 3-bit (7 * 1170) / 32
  593. 4-bit (15 * 546) / 32
  594. 5-bit (31 * 264) / 32
  595. 6-bit (63 * 130) / 32
  596. 7-bit (127 * 64) / 32
  597. */
  598. rmult = info->rbits < 8 ? mults[info->rbits] : 1;
  599. gmult = info->gbits < 8 ? mults[info->gbits] : 1;
  600. bmult = info->bbits < 8 ? mults[info->bbits] : 1;
  601. amult = info->abits < 8 ? mults[info->abits] : 1;
  602. rtrunc = info->rbits < 8 ? 5 : (info->rbits - 8);
  603. gtrunc = info->gbits < 8 ? 5 : (info->gbits - 8);
  604. btrunc = info->bbits < 8 ? 5 : (info->bbits - 8);
  605. atrunc = info->abits < 8 ? 5 : (info->abits - 8);
  606. #ifdef BMP_DEBUG
  607. fz_warn(ctx, "rbits = %2d mult = %2d trunc = %2d", info->rbits, rmult, rtrunc);
  608. fz_warn(ctx, "gbits = %2d mult = %2d trunc = %2d", info->gbits, gmult, gtrunc);
  609. fz_warn(ctx, "bbits = %2d mult = %2d trunc = %2d", info->bbits, bmult, btrunc);
  610. fz_warn(ctx, "abits = %2d mult = %2d trunc = %2d", info->abits, amult, atrunc);
  611. #endif
  612. for (y = 0; y < height; y++)
  613. {
  614. const unsigned char *sp = ssp + ((size_t) y) * ((size_t) sstride);
  615. unsigned char *dp = ddp + ((size_t) y) * ((size_t) dstride);
  616. switch (bitcount)
  617. {
  618. case 64:
  619. for (x = 0; x < width; x++)
  620. {
  621. uint32_t a = (((uint16_t)sp[7]) << 8) | (((uint16_t)sp[6]) << 0);
  622. uint32_t r = (((uint16_t)sp[5]) << 8) | (((uint16_t)sp[4]) << 0);
  623. uint32_t g = (((uint16_t)sp[3]) << 8) | (((uint16_t)sp[2]) << 0);
  624. uint32_t b = (((uint16_t)sp[1]) << 8) | (((uint16_t)sp[0]) << 0);
  625. r = (r * 255 + 4096) >> 13;
  626. g = (g * 255 + 4096) >> 13;
  627. b = (b * 255 + 4096) >> 13;
  628. a = (a * 255 + 4096) >> 13;
  629. *dp++ = r;
  630. *dp++ = g;
  631. *dp++ = b;
  632. *dp++ = a;
  633. sp += 8;
  634. }
  635. break;
  636. case 32:
  637. for (x = 0; x < width; x++)
  638. {
  639. uint32_t sample =
  640. (((uint32_t) sp[3]) << 24) |
  641. (((uint32_t) sp[2]) << 16) |
  642. (((uint32_t) sp[1]) << 8) |
  643. (((uint32_t) sp[0]) << 0);
  644. uint32_t r = (sample & info->rmask) >> info->rshift;
  645. uint32_t g = (sample & info->gmask) >> info->gshift;
  646. uint32_t b = (sample & info->bmask) >> info->bshift;
  647. uint32_t a = info->abits == 0 ? 255 : (sample & info->amask) >> info->ashift;
  648. *dp++ = (r * rmult) >> rtrunc;
  649. *dp++ = (g * gmult) >> gtrunc;
  650. *dp++ = (b * bmult) >> btrunc;
  651. *dp++ = info->abits == 0 ? a : (a * amult) >> atrunc;
  652. sp += 4;
  653. }
  654. break;
  655. case 24:
  656. for (x = 0; x < width; x++)
  657. {
  658. *dp++ = sp[2];
  659. *dp++ = sp[1];
  660. *dp++ = sp[0];
  661. *dp++ = 255;
  662. sp += 3;
  663. }
  664. break;
  665. case 16:
  666. for (x = 0; x < width; x++)
  667. {
  668. uint16_t sample =
  669. (((uint16_t)sp[1]) << 8) |
  670. (((uint16_t)sp[0]) << 0);
  671. uint16_t r = (sample & info->rmask) >> info->rshift;
  672. uint16_t g = (sample & info->gmask) >> info->gshift;
  673. uint16_t b = (sample & info->bmask) >> info->bshift;
  674. uint16_t a = (sample & info->amask) >> info->ashift;
  675. *dp++ = (r * rmult) >> rtrunc;
  676. *dp++ = (g * gmult) >> gtrunc;
  677. *dp++ = (b * bmult) >> btrunc;
  678. *dp++ = info->abits == 0 ? 255 : (a * amult) >> atrunc;
  679. sp += 2;
  680. }
  681. break;
  682. case 8:
  683. for (x = 0; x < width; x++)
  684. {
  685. *dp++ = info->palette[3 * sp[0] + 0];
  686. *dp++ = info->palette[3 * sp[0] + 1];
  687. *dp++ = info->palette[3 * sp[0] + 2];
  688. *dp++ = 255;
  689. sp++;
  690. }
  691. break;
  692. case 4:
  693. for (x = 0; x < width; x++)
  694. {
  695. int idx;
  696. switch (x & 1)
  697. {
  698. case 0: idx = (sp[0] >> 4) & 0x0f; break;
  699. case 1: idx = (sp[0] >> 0) & 0x0f; sp++; break;
  700. }
  701. *dp++ = info->palette[3 * idx + 0];
  702. *dp++ = info->palette[3 * idx + 1];
  703. *dp++ = info->palette[3 * idx + 2];
  704. *dp++ = 255;
  705. }
  706. break;
  707. case 2:
  708. for (x = 0; x < width; x++)
  709. {
  710. int idx;
  711. switch (x & 3)
  712. {
  713. case 0: idx = (sp[0] >> 6) & 0x03; break;
  714. case 1: idx = (sp[0] >> 4) & 0x03; break;
  715. case 2: idx = (sp[0] >> 2) & 0x03; break;
  716. case 3: idx = (sp[0] >> 0) & 0x03; sp++; break;
  717. }
  718. *dp++ = info->palette[3 * idx + 0];
  719. *dp++ = info->palette[3 * idx + 1];
  720. *dp++ = info->palette[3 * idx + 2];
  721. *dp++ = 255;
  722. }
  723. break;
  724. case 1:
  725. for (x = 0; x < width; x++)
  726. {
  727. int idx;
  728. switch (x & 7)
  729. {
  730. case 0: idx = (sp[0] >> 7) & 0x01; break;
  731. case 1: idx = (sp[0] >> 6) & 0x01; break;
  732. case 2: idx = (sp[0] >> 5) & 0x01; break;
  733. case 3: idx = (sp[0] >> 4) & 0x01; break;
  734. case 4: idx = (sp[0] >> 3) & 0x01; break;
  735. case 5: idx = (sp[0] >> 2) & 0x01; break;
  736. case 6: idx = (sp[0] >> 1) & 0x01; break;
  737. case 7: idx = (sp[0] >> 0) & 0x01; sp++; break;
  738. }
  739. *dp++ = info->palette[3 * idx + 0];
  740. *dp++ = info->palette[3 * idx + 1];
  741. *dp++ = info->palette[3 * idx + 2];
  742. *dp++ = 255;
  743. }
  744. break;
  745. }
  746. }
  747. fz_free(ctx, decompressed);
  748. fz_premultiply_pixmap(ctx, pix);
  749. return pix;
  750. }
  751. static fz_colorspace *
  752. bmp_read_color_profile(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end)
  753. {
  754. if (info->colorspacetype == 0)
  755. {
  756. float matrix[9] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
  757. float wp[3] = { 0.95047f, 1.0f, 1.08883f }; /* D65 white point */
  758. float bp[3] = { 0, 0, 0 };
  759. float gamma[3] = { 1, 1, 1 };
  760. int i;
  761. for (i = 0; i < 3; i++)
  762. gamma[i] = (float) info->gamma[i] / (float) (1 << 16);
  763. for (i = 0; i < 9; i++)
  764. matrix[i] = (float) info->endpoints[i] / (float) (1 << 30);
  765. #ifdef BMP_DEBUG
  766. fz_warn(ctx, "wp = %.6f %.6f %.6f", wp[0], wp[1], wp[2]);
  767. fz_warn(ctx, "bp = %.6f %.6f %.6f", bp[0], bp[1], bp[2]);
  768. fz_warn(ctx, "endpoints = %.6f %.6f %.6f", matrix[0], matrix[1], matrix[2]);
  769. fz_warn(ctx, "endpoints = %.6f %.6f %.6f", matrix[3], matrix[4], matrix[5]);
  770. fz_warn(ctx, "endpoints = %.6f %.6f %.6f", matrix[6], matrix[7], matrix[8]);
  771. fz_warn(ctx, "gamma = %.6f %.6f %.6f", gamma[0], gamma[1], gamma[2]);
  772. #endif
  773. return fz_new_cal_rgb_colorspace(ctx, wp, bp, gamma, matrix);
  774. }
  775. else if (info->colorspacetype == 0x4c494e4b)
  776. {
  777. fz_warn(ctx, "ignoring linked color profile in bmp image");
  778. return NULL;
  779. }
  780. else if (info->colorspacetype == 0x57696e20)
  781. {
  782. fz_warn(ctx, "ignoring windows color profile in bmp image");
  783. return NULL;
  784. }
  785. else if (info->colorspacetype == 0x4d424544)
  786. {
  787. fz_buffer *profile;
  788. fz_colorspace *cs;
  789. if ((uint32_t)(end - begin) <= info->profileoffset)
  790. {
  791. fz_warn(ctx, "ignoring color profile located outside bmp image");
  792. return NULL;
  793. }
  794. if ((uint32_t)(end - begin) - info->profileoffset < info->profilesize)
  795. {
  796. fz_warn(ctx, "ignoring truncated color profile in bmp image");
  797. return NULL;
  798. }
  799. if (info->profilesize == 0)
  800. {
  801. fz_warn(ctx, "ignoring color profile without data in bmp image");
  802. return NULL;
  803. }
  804. profile = fz_new_buffer_from_copied_data(ctx, begin + info->profileoffset, info->profilesize);
  805. fz_try(ctx)
  806. cs = fz_new_icc_colorspace(ctx, FZ_COLORSPACE_RGB, 0, "BMPRGB", profile);
  807. fz_always(ctx)
  808. fz_drop_buffer(ctx, profile);
  809. fz_catch(ctx)
  810. fz_rethrow(ctx);
  811. return cs;
  812. }
  813. else if (info->colorspacetype == 0x73524742)
  814. {
  815. return fz_keep_colorspace(ctx, fz_device_rgb(ctx));
  816. }
  817. fz_warn(ctx, "ignoring color profile with unknown type in bmp image");
  818. return NULL;
  819. }
  820. static void
  821. compute_mask_info(unsigned int mask, unsigned int *shift, unsigned int *bits)
  822. {
  823. *bits = 0;
  824. *shift = 0;
  825. while (mask && (mask & 1) == 0) {
  826. *shift += 1;
  827. mask >>= 1;
  828. }
  829. while (mask && (mask & 1) == 1) {
  830. *bits += 1;
  831. mask >>= 1;
  832. }
  833. }
  834. static const unsigned char *
  835. bmp_read_color_masks(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end, const unsigned char *p)
  836. {
  837. int size = 0;
  838. if (info->compression == BI_BITFIELDS)
  839. {
  840. size = 12;
  841. if (end - p < 12)
  842. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in mask header in bmp image");
  843. info->rmask = read32(p + 0);
  844. info->gmask = read32(p + 4);
  845. info->bmask = read32(p + 8);
  846. }
  847. else if (info->compression == BI_ALPHABITS)
  848. {
  849. size = 16;
  850. if (end - p < 16)
  851. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in mask header in bmp image");
  852. info->rmask = read32(p + 0);
  853. info->gmask = read32(p + 4);
  854. info->bmask = read32(p + 8);
  855. info->amask = read32(p + 12);
  856. }
  857. return p + size;
  858. }
  859. static int
  860. bmp_palette_is_gray(fz_context *ctx, struct info *info, int readcolors)
  861. {
  862. int i;
  863. for (i = 0; i < readcolors; i++)
  864. {
  865. int rgdiff = fz_absi(info->palette[3 * i + 0] - info->palette[3 * i + 1]);
  866. int gbdiff = fz_absi(info->palette[3 * i + 1] - info->palette[3 * i + 2]);
  867. int rbdiff = fz_absi(info->palette[3 * i + 0] - info->palette[3 * i + 2]);
  868. if (rgdiff > 2 || gbdiff > 2 || rbdiff > 2)
  869. return 0;
  870. }
  871. return 1;
  872. }
  873. static void
  874. bmp_load_default_palette(fz_context *ctx, struct info *info, int readcolors)
  875. {
  876. int i;
  877. fz_warn(ctx, "color table too short; loading default palette");
  878. if (info->bitcount == 8)
  879. {
  880. if (!bmp_palette_is_gray(ctx, info, readcolors))
  881. memcpy(&info->palette[readcolors * 3], &web_palette[readcolors * 3],
  882. sizeof(web_palette) - readcolors * 3);
  883. else
  884. for (i = readcolors; i < 256; i++)
  885. {
  886. info->palette[3 * i + 0] = i;
  887. info->palette[3 * i + 1] = i;
  888. info->palette[3 * i + 2] = i;
  889. }
  890. }
  891. else if (info->bitcount == 4)
  892. {
  893. if (!bmp_palette_is_gray(ctx, info, readcolors))
  894. memcpy(&info->palette[readcolors * 3], &vga_palette[readcolors * 3],
  895. sizeof(vga_palette) - readcolors * 3);
  896. else
  897. for (i = readcolors; i < 16; i++)
  898. {
  899. info->palette[3 * i + 0] = (i << 4) | i;
  900. info->palette[3 * i + 1] = (i << 4) | i;
  901. info->palette[3 * i + 2] = (i << 4) | i;
  902. }
  903. }
  904. else if (info->bitcount == 2)
  905. memcpy(info->palette, gray_palette, sizeof(gray_palette));
  906. else if (info->bitcount == 1)
  907. memcpy(info->palette, bw_palette, sizeof(bw_palette));
  908. }
  909. static const unsigned char *
  910. bmp_read_palette(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end, const unsigned char *p)
  911. {
  912. int i, expected, present, entry_size;
  913. entry_size = palette_entry_size(info);
  914. if (info->colors == 0)
  915. expected = info->colors = 1 << info->bitcount;
  916. else
  917. expected = fz_mini(info->colors, 1 << info->bitcount);
  918. if (info->bitmapoffset == 0)
  919. present = fz_mini(expected, (end - p) / entry_size);
  920. else
  921. present = fz_mini(expected, (begin + info->bitmapoffset - p) / entry_size);
  922. for (i = 0; i < present; i++)
  923. {
  924. /* ignore alpha channel even if present */
  925. info->palette[3 * i + 0] = read8(p + i * entry_size + 2);
  926. info->palette[3 * i + 1] = read8(p + i * entry_size + 1);
  927. info->palette[3 * i + 2] = read8(p + i * entry_size + 0);
  928. }
  929. if (present < expected)
  930. bmp_load_default_palette(ctx, info, present);
  931. return p + present * entry_size;
  932. }
  933. static const unsigned char *
  934. bmp_read_info_header(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end, const unsigned char *p)
  935. {
  936. uint32_t size;
  937. if (end - p < 4)
  938. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in info header in bmp image");
  939. size = info->version = read32(p + 0);
  940. if (!is_win_bmp(info) && !is_os2_bmp(info))
  941. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown header version (%u) in bmp image", info->version);
  942. if ((uint32_t)(end - p) < size)
  943. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in info header in bmp image");
  944. /* default compression */
  945. info->compression = BI_NONE;
  946. /* OS/2 1.x or Windows v2 */
  947. if (size == 12)
  948. {
  949. /* read32(p+0) == header size */
  950. info->width = read16(p + 4);
  951. info->height = read16(p + 6);
  952. /* read16(p+8) == planes */
  953. info->bitcount = read16(p + 10);
  954. }
  955. /* OS/2 2.x short header */
  956. if (size >= 16)
  957. {
  958. /* read32(p+0) == header size */
  959. info->width = read32(p + 4);
  960. info->height = read32(p + 8);
  961. /* read16(p+12) == planes */
  962. info->bitcount = read16(p + 14);
  963. }
  964. /* default masks */
  965. if (info->bitcount == 16)
  966. {
  967. info->rmask = 0x00007c00;
  968. info->gmask = 0x000003e0;
  969. info->bmask = 0x0000001f;
  970. info->amask = 0x00000000;
  971. }
  972. else if (info->bitcount == 24 || info->bitcount == 32)
  973. {
  974. info->rmask = 0x00ff0000;
  975. info->gmask = 0x0000ff00;
  976. info->bmask = 0x000000ff;
  977. info->amask = 0x00000000;
  978. }
  979. /* Windows v3 header */
  980. if (size >= 40)
  981. {
  982. info->compression = read32(p + 16);
  983. info->bitmapsize = read32(p + 20);
  984. info->xres = read32(p + 24);
  985. info->yres = read32(p + 28);
  986. info->colors = read32(p + 32);
  987. if (info->bitcount >= 32)
  988. {
  989. if (info->colors != 0)
  990. fz_warn(ctx, "Suspect BMP header; bitcount=%d, colors=%d", info->bitcount, info->colors);
  991. info->colors = 0;
  992. }
  993. else if (info->colors > (1U<<info->bitcount))
  994. {
  995. fz_warn(ctx, "Suspect BMP header; bitcount=%d, colors=%d", info->bitcount, info->colors);
  996. info->colors = 1<<info->bitcount;
  997. }
  998. /* read32(p+36) == important colors */
  999. }
  1000. /* Windows v3 header with RGB masks */
  1001. if (size == 52 || size == 56 || size == 108 || size == 124)
  1002. {
  1003. info->rmask = read32(p + 40);
  1004. info->gmask = read32(p + 44);
  1005. info->bmask = read32(p + 48);
  1006. }
  1007. /* Windows v3 header with RGBA masks */
  1008. if (size == 56 || size == 108 || size == 124)
  1009. {
  1010. info->amask = read32(p + 52);
  1011. }
  1012. /* OS/2 2.x long header */
  1013. if (size == 64)
  1014. {
  1015. /* read16(p+40) == units */
  1016. /* read16(p+42) == reserved */
  1017. /* read16(p+44) == recording */
  1018. /* read16(p+46) == rendering */
  1019. /* read32(p+48) == size1 */
  1020. /* read32(p+52) == size2 */
  1021. /* read32(p+56) == color encoding */
  1022. /* read32(p+60) == identifier */
  1023. }
  1024. /* Windows v4 header */
  1025. if (size >= 108)
  1026. {
  1027. info->colorspacetype = read32(p + 56);
  1028. info->endpoints[0] = read32(p + 60);
  1029. info->endpoints[1] = read32(p + 64);
  1030. info->endpoints[2] = read32(p + 68);
  1031. info->endpoints[3] = read32(p + 72);
  1032. info->endpoints[4] = read32(p + 76);
  1033. info->endpoints[5] = read32(p + 80);
  1034. info->endpoints[6] = read32(p + 84);
  1035. info->endpoints[7] = read32(p + 88);
  1036. info->endpoints[8] = read32(p + 92);
  1037. info->gamma[0] = read32(p + 96);
  1038. info->gamma[1] = read32(p + 100);
  1039. info->gamma[2] = read32(p + 104);
  1040. }
  1041. /* Windows v5 header */
  1042. if (size >= 124)
  1043. {
  1044. info->intent = read32(p + 108);
  1045. info->profileoffset = read32(p + 112);
  1046. info->profilesize = read32(p + 116);
  1047. /* read32(p+120) == reserved */
  1048. }
  1049. return p + size;
  1050. }
  1051. static fz_pixmap *
  1052. bmp_read_image(fz_context *ctx, struct info *info, const unsigned char *begin, const unsigned char *end, const unsigned char *p, int only_metadata)
  1053. {
  1054. const unsigned char *profilebegin;
  1055. memset(info, 0x00, sizeof (*info));
  1056. info->colorspacetype = 0xffffffff;
  1057. p = profilebegin = bmp_read_file_header(ctx, info, begin, end, p);
  1058. p = bmp_read_info_header(ctx, info, begin, end, p);
  1059. /* clamp bitmap offset to buffer size */
  1060. if (info->bitmapoffset < (uint32_t)(p - begin))
  1061. info->bitmapoffset = (uint32_t)(p - begin);
  1062. if ((uint32_t)(end - begin) < info->bitmapoffset)
  1063. info->bitmapoffset = end - begin;
  1064. if (has_palette(info))
  1065. p = bmp_read_palette(ctx, info, begin, end, p);
  1066. if (has_color_masks(info))
  1067. p = bmp_read_color_masks(ctx, info, begin, end, p);
  1068. info->xres = DPM_TO_DPI(info->xres);
  1069. info->yres = DPM_TO_DPI(info->yres);
  1070. /* extract topdown/bottomup from height for windows bitmaps */
  1071. if (is_win_bmp(info))
  1072. {
  1073. int bits = info->version == 12 ? 16 : 32;
  1074. info->topdown = (info->height >> (bits - 1)) & 1;
  1075. if (info->topdown)
  1076. {
  1077. info->height--;
  1078. info->height = ~info->height;
  1079. info->height &= bits == 16 ? 0xffff : 0xffffffff;
  1080. }
  1081. }
  1082. /* GIMP incorrectly writes BMP v5 headers that omit color masks
  1083. but include colorspace information. This means they look like
  1084. BMP v4 headers and that we interpret the colorspace information
  1085. partially as color mask data, partially as colorspace information.
  1086. Let's work around this... */
  1087. if (info->version == 108 &&
  1088. info->rmask == 0x73524742 && /* colorspacetype */
  1089. info->gmask == 0x00000000 && /* endpoints[0] */
  1090. info->bmask == 0x00000000 && /* endpoints[1] */
  1091. info->amask == 0x00000000 && /* endpoints[2] */
  1092. info->colorspacetype == 0x00000000 && /* endpoints[3] */
  1093. info->endpoints[0] == 0x00000000 && /* endpoints[4] */
  1094. info->endpoints[1] == 0x00000000 && /* endpoints[5] */
  1095. info->endpoints[2] == 0x00000000 && /* endpoints[6] */
  1096. info->endpoints[3] == 0x00000000 && /* endpoints[7] */
  1097. info->endpoints[4] == 0x00000000 && /* endpoints[8] */
  1098. info->endpoints[5] == 0x00000000 && /* gamma[0] */
  1099. info->endpoints[6] == 0x00000000 && /* gamma[1] */
  1100. info->endpoints[7] == 0x00000000 && /* gamma[2] */
  1101. info->endpoints[8] == 0x00000002) /* intent */
  1102. {
  1103. info->rmask = 0;
  1104. /* default masks */
  1105. if (info->bitcount == 16)
  1106. {
  1107. info->rmask = 0x00007c00;
  1108. info->gmask = 0x000003e0;
  1109. info->bmask = 0x0000001f;
  1110. info->amask = 0x00000000;
  1111. }
  1112. else if (info->bitcount >= 24)
  1113. {
  1114. info->rmask = 0x00ff0000;
  1115. info->gmask = 0x0000ff00;
  1116. info->bmask = 0x000000ff;
  1117. info->amask = 0x00000000;
  1118. }
  1119. info->colorspacetype = 0x73524742;
  1120. info->intent = 0x00000002;
  1121. }
  1122. /* get number of bits per component and component shift */
  1123. compute_mask_info(info->rmask, &info->rshift, &info->rbits);
  1124. compute_mask_info(info->gmask, &info->gshift, &info->gbits);
  1125. compute_mask_info(info->bmask, &info->bshift, &info->bbits);
  1126. compute_mask_info(info->amask, &info->ashift, &info->abits);
  1127. #ifdef BMP_DEBUG
  1128. {
  1129. #define chr(c) (((c) >= ' ' && (c) <= '~') ? (c) : '?')
  1130. fz_warn(ctx, "type = %02x%02x %c%c", info->type[0], info->type[1], chr(info->type[0]), chr(info->type[1]));
  1131. if (is_bitmap_array(info->type)) fz_warn(ctx, "\tbitmap array");
  1132. if (is_bitmap(info->type)) fz_warn(ctx, "\tbitmap");
  1133. fz_warn(ctx, "version = %zu", (size_t) info->version);
  1134. if (is_os2_bmp(info)) fz_warn(ctx, "OS/2 bmp");
  1135. if (is_win_bmp(info)) fz_warn(ctx, "Windows bmp");
  1136. fz_warn(ctx, "bitmapoffset = %zu", (size_t) info->bitmapoffset);
  1137. fz_warn(ctx, "width = %zu", (size_t) info->width);
  1138. fz_warn(ctx, "height = %zu", (size_t) info->height);
  1139. fz_warn(ctx, "bitcount = %zu", (size_t) info->bitcount);
  1140. fz_warn(ctx, "compression = %zu", (size_t) info->compression);
  1141. if (info->compression == BI_NONE) fz_warn(ctx, "\tNone");
  1142. if (info->compression == BI_RLE8) fz_warn(ctx, "\tRLE 8");
  1143. if (info->compression == BI_RLE4) fz_warn(ctx, "\tRLE 4");
  1144. if (is_valid_win_compression(info) && info->compression == BI_BITFIELDS) fz_warn(ctx, "\tBITFIELDS");
  1145. if (is_valid_os2_compression(info) && info->compression == BI_HUFFMAN1D) fz_warn(ctx, "\tHUFFMAN1D");
  1146. if (info->compression == BI_JPEG) fz_warn(ctx, "\tJPEG");
  1147. if (info->compression == BI_RLE24) fz_warn(ctx, "\tRLE24");
  1148. if (info->compression == BI_PNG) fz_warn(ctx, "\tPNG");
  1149. if (info->compression == BI_ALPHABITS) fz_warn(ctx, "\tALPHABITS");
  1150. fz_warn(ctx, "bitmapsize = %zu", (size_t) info->bitmapsize);
  1151. fz_warn(ctx, "xres = %zu", (size_t) info->xres);
  1152. fz_warn(ctx, "yres = %zu", (size_t) info->yres);
  1153. fz_warn(ctx, "colors = %zu", (size_t) info->colors);
  1154. fz_warn(ctx, "rmask = 0x%08zx rshift = %d rbits = %d", (size_t) info->rmask, info->rshift, info->rbits);
  1155. fz_warn(ctx, "gmask = 0x%08zx gshift = %d gbits = %d", (size_t) info->gmask, info->gshift, info->gbits);
  1156. fz_warn(ctx, "bmask = 0x%08zx bshift = %d bbits = %d", (size_t) info->bmask, info->bshift, info->bbits);
  1157. fz_warn(ctx, "amask = 0x%08zx ashift = %d abits = %d", (size_t) info->amask, info->ashift, info->abits);
  1158. fz_warn(ctx, "colorspacetype = %08zx %c%c%c%c", (size_t) info->colorspacetype,
  1159. chr((info->colorspacetype >> 24) & 0xff),
  1160. chr((info->colorspacetype >> 16) & 0xff),
  1161. chr((info->colorspacetype >> 8) & 0xff),
  1162. chr((info->colorspacetype >> 0) & 0xff));
  1163. fz_warn(ctx, "endpoints[%d] = 0x%08zx 0x%08zx 0x%08zx", 0, (size_t) info->endpoints[0], (size_t) info->endpoints[1], (size_t) info->endpoints[2]);
  1164. fz_warn(ctx, "endpoints[%d] = 0x%08zx 0x%08zx 0x%08zx", 3, (size_t) info->endpoints[3], (size_t) info->endpoints[4], (size_t) info->endpoints[5]);
  1165. fz_warn(ctx, "endpoints[%d] = 0x%08zx 0x%08zx 0x%08zx", 6, (size_t) info->endpoints[6], (size_t) info->endpoints[7], (size_t) info->endpoints[8]);
  1166. fz_warn(ctx, "gamma = 0x%08zx 0x%08zx 0x%08zx", (size_t) info->gamma[0], (size_t) info->gamma[1], (size_t) info->gamma[2]);
  1167. fz_warn(ctx, "profileoffset = %zu", (size_t) info->profileoffset);
  1168. fz_warn(ctx, "profilesize = %zu", (size_t) info->profilesize);
  1169. #undef chr
  1170. }
  1171. #endif
  1172. if (info->width == 0 || info->width > SHRT_MAX || info->height == 0 || info->height > SHRT_MAX)
  1173. fz_throw(ctx, FZ_ERROR_LIMIT, "image dimensions (%u x %u) out of range in bmp image", info->width, info->height);
  1174. if (!is_valid_compression(info))
  1175. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported compression method (%u) in bmp image", info->compression);
  1176. if (!is_valid_bitcount(info))
  1177. fz_throw(ctx, FZ_ERROR_FORMAT, "invalid bits per pixel (%u) for compression (%u) in bmp image", info->bitcount, info->compression);
  1178. if (info->rbits > info->bitcount)
  1179. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported %u bit red mask in bmp image", info->rbits);
  1180. if (info->gbits > info->bitcount)
  1181. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported %u bit green mask in bmp image", info->gbits);
  1182. if (info->bbits > info->bitcount)
  1183. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported %u bit blue mask in bmp image", info->bbits);
  1184. if (info->abits > info->bitcount)
  1185. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported %u bit alpha mask in bmp image", info->abits);
  1186. /* Read color profile or default to RGB */
  1187. if (has_color_profile(info))
  1188. info->cs = bmp_read_color_profile(ctx, info, profilebegin, end);
  1189. if (!info->cs)
  1190. info->cs = fz_keep_colorspace(ctx, fz_device_rgb(ctx));
  1191. if (only_metadata)
  1192. return NULL;
  1193. /* bitmap cannot begin before headers have ended */
  1194. if ((uint32_t)(p - begin) < info->bitmapoffset)
  1195. p = begin + info->bitmapoffset;
  1196. if (is_win_bmp(info) && info->compression == BI_JPEG)
  1197. {
  1198. if ((uint32_t)(end - p) < info->bitmapsize)
  1199. fz_warn(ctx, "premature end in jpeg image embedded in bmp image");
  1200. return fz_load_jpeg(ctx, p, end - p);
  1201. }
  1202. else if (is_win_bmp(info) && info->compression == BI_PNG)
  1203. {
  1204. if ((uint32_t)(end - p) < info->bitmapsize)
  1205. fz_warn(ctx, "premature end in png image embedded in bmp image");
  1206. return fz_load_png(ctx, p, end - p);
  1207. }
  1208. else
  1209. return bmp_read_bitmap(ctx, info, begin, end, p);
  1210. }
  1211. fz_pixmap *
  1212. fz_load_bmp_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int subimage)
  1213. {
  1214. const unsigned char *begin = buf;
  1215. const unsigned char *end = buf + len;
  1216. const unsigned char *p = begin;
  1217. struct info info = { 0 };
  1218. int nextoffset = 0;
  1219. fz_pixmap *image = NULL;
  1220. int origidx = subimage;
  1221. (void) p;
  1222. do
  1223. {
  1224. p = begin + nextoffset;
  1225. if (end - p < 14)
  1226. fz_throw(ctx, FZ_ERROR_FORMAT, "not enough data for bitmap array (%02x%02x) in bmp image", p[0], p[1]);
  1227. if (is_bitmap_array(p))
  1228. {
  1229. /* read16(p+0) == type */
  1230. /* read32(p+2) == size of this header in bytes */
  1231. nextoffset = read32(p + 6);
  1232. /* read16(p+10) == suitable pelx dimensions */
  1233. /* read16(p+12) == suitable pely dimensions */
  1234. p += 14;
  1235. (void) p;
  1236. }
  1237. else if (is_bitmap(p))
  1238. {
  1239. nextoffset = 0;
  1240. }
  1241. else
  1242. {
  1243. fz_warn(ctx, "treating invalid subimage as end of file");
  1244. nextoffset = 0;
  1245. }
  1246. if (end - begin < nextoffset)
  1247. {
  1248. fz_warn(ctx, "treating invalid next subimage offset as end of file");
  1249. nextoffset = 0;
  1250. }
  1251. else
  1252. subimage--;
  1253. } while (subimage >= 0 && nextoffset > 0);
  1254. if (subimage != -1)
  1255. fz_throw(ctx, FZ_ERROR_ARGUMENT, "subimage index (%d) out of range in bmp image", origidx);
  1256. fz_try(ctx)
  1257. image = bmp_read_image(ctx, &info, begin, end, p, 0);
  1258. fz_always(ctx)
  1259. fz_drop_colorspace(ctx, info.cs);
  1260. fz_catch(ctx)
  1261. fz_rethrow(ctx);
  1262. return image;
  1263. }
  1264. void
  1265. fz_load_bmp_info_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep, int subimage)
  1266. {
  1267. const unsigned char *begin = buf;
  1268. const unsigned char *end = buf + len;
  1269. const unsigned char *p = begin;
  1270. struct info info = { 0 };
  1271. int nextoffset = 0;
  1272. int origidx = subimage;
  1273. (void) p;
  1274. do
  1275. {
  1276. p = begin + nextoffset;
  1277. if (end - p < 14)
  1278. fz_throw(ctx, FZ_ERROR_FORMAT, "not enough data for bitmap array (%02x%02x) in bmp image", p[0], p[1]);
  1279. if (is_bitmap_array(p))
  1280. {
  1281. /* read16(p+0) == type */
  1282. /* read32(p+2) == size of this header in bytes */
  1283. nextoffset = read32(p + 6);
  1284. /* read16(p+10) == suitable pelx dimensions */
  1285. /* read16(p+12) == suitable pely dimensions */
  1286. p += 14;
  1287. (void) p;
  1288. }
  1289. else if (is_bitmap(p))
  1290. {
  1291. nextoffset = 0;
  1292. }
  1293. else
  1294. {
  1295. fz_warn(ctx, "treating invalid subimage as end of file");
  1296. nextoffset = 0;
  1297. }
  1298. if (end - begin < nextoffset)
  1299. {
  1300. fz_warn(ctx, "treating invalid next subimage offset as end of file");
  1301. nextoffset = 0;
  1302. }
  1303. else
  1304. subimage--;
  1305. } while (subimage >= 0 && nextoffset > 0);
  1306. if (subimage != -1)
  1307. fz_throw(ctx, FZ_ERROR_ARGUMENT, "subimage index (%d) out of range in bmp image", origidx);
  1308. fz_try(ctx)
  1309. {
  1310. (void) bmp_read_image(ctx, &info, begin, end, p, 1);
  1311. *cspacep = fz_keep_colorspace(ctx, info.cs);
  1312. *wp = info.width;
  1313. *hp = info.height;
  1314. *xresp = info.xres;
  1315. *yresp = info.yres;
  1316. }
  1317. fz_always(ctx)
  1318. fz_drop_colorspace(ctx, info.cs);
  1319. fz_catch(ctx)
  1320. fz_rethrow(ctx);
  1321. }
  1322. int
  1323. fz_load_bmp_subimage_count(fz_context *ctx, const unsigned char *buf, size_t len)
  1324. {
  1325. const unsigned char *begin = buf;
  1326. const unsigned char *end = buf + len;
  1327. uint32_t nextoffset = 0;
  1328. int count = 0;
  1329. do
  1330. {
  1331. const unsigned char *p = begin + nextoffset;
  1332. if (end - p < 14)
  1333. fz_throw(ctx, FZ_ERROR_FORMAT, "not enough data for bitmap array in bmp image");
  1334. if (is_bitmap_array(p))
  1335. {
  1336. /* read16(p+0) == type */
  1337. /* read32(p+2) == size of this header in bytes */
  1338. nextoffset = read32(p + 6);
  1339. /* read16(p+10) == suitable pelx dimensions */
  1340. /* read16(p+12) == suitable pely dimensions */
  1341. p += 14;
  1342. }
  1343. else if (is_bitmap(p))
  1344. {
  1345. nextoffset = 0;
  1346. }
  1347. else
  1348. {
  1349. fz_warn(ctx, "treating invalid subimage as end of file");
  1350. nextoffset = 0;
  1351. }
  1352. if (end - begin < nextoffset)
  1353. {
  1354. fz_warn(ctx, "treating invalid next subimage offset as end of file");
  1355. nextoffset = 0;
  1356. }
  1357. else
  1358. count++;
  1359. } while (nextoffset > 0);
  1360. return count;
  1361. }
  1362. fz_pixmap *
  1363. fz_load_bmp(fz_context *ctx, const unsigned char *p, size_t total)
  1364. {
  1365. return fz_load_bmp_subimage(ctx, p, total, 0);
  1366. }
  1367. void
  1368. fz_load_bmp_info(fz_context *ctx, const unsigned char *p, size_t total, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
  1369. {
  1370. fz_load_bmp_info_subimage(ctx, p, total, wp, hp, xresp, yresp, cspacep, 0);
  1371. }