image_bmp.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * File : image_bmp.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-01-24 onelife Reimplement to improve efficiency and add
  23. * features. The new decoder uses configurable fixed size working buffer and
  24. * provides scaledown function.
  25. */
  26. #include <rtthread.h>
  27. #include <rtgui/dc.h>
  28. #include <rtgui/image.h>
  29. #include <rtgui/rtgui_system.h>
  30. #include <rtgui/image_bmp.h>
  31. #include <rtgui/blit.h>
  32. #include <math.h>
  33. #ifdef RTGUI_USING_DFS_FILERW
  34. #include <dfs_posix.h>
  35. #endif
  36. #ifdef RTGUI_IMAGE_BMP
  37. /* Compression encodings for BMP files */
  38. #ifndef BI_RGB
  39. #define BI_RGB 0
  40. #define BI_RLE8 1
  41. #define BI_RLE4 2
  42. #define BI_BITFIELDS 3
  43. #endif
  44. #define BMP_WORKING_BUFFER_SIZE (384) /* In multiple of 12 and bigger than 48 */
  45. #define BMP_MAX_SCALING_FACTOR (10) // TODO: find the max value!
  46. #define hw_driver (rtgui_graphic_driver_get_default())
  47. struct rtgui_image_bmp
  48. {
  49. rt_bool_t is_loaded;
  50. rt_uint8_t *pixels;
  51. struct rtgui_filerw *filerw;
  52. rt_uint32_t w, h;
  53. rt_uint32_t pixel_offset;
  54. rt_uint32_t pitch;
  55. rt_uint8_t scale;
  56. rt_uint8_t bit_per_pixel;
  57. rt_uint8_t pad;
  58. };
  59. static rt_bool_t rtgui_image_bmp_check(struct rtgui_filerw *file);
  60. static rt_bool_t rtgui_image_bmp_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load);
  61. static void rtgui_image_bmp_unload(struct rtgui_image *image);
  62. static void rtgui_image_bmp_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect);
  63. struct rtgui_image_engine rtgui_image_bmp_engine =
  64. {
  65. "bmp",
  66. { RT_NULL },
  67. rtgui_image_bmp_check,
  68. rtgui_image_bmp_load,
  69. rtgui_image_bmp_unload,
  70. rtgui_image_bmp_blit,
  71. };
  72. static rt_bool_t rtgui_image_bmp_check(struct rtgui_filerw *file)
  73. {
  74. rt_uint8_t buffer[18];
  75. rt_bool_t is_bmp = RT_FALSE;
  76. do
  77. {
  78. if (!file)
  79. {
  80. break;
  81. }
  82. /* Prepare to decode */
  83. if (rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET) < 0)
  84. {
  85. break;
  86. }
  87. if (rtgui_filerw_read(file, (void *)buffer, 18, 1) != 18)
  88. {
  89. break;
  90. }
  91. /* Read file type */
  92. if (buffer[0] != 'B' || buffer[1] != 'M')
  93. {
  94. break;
  95. }
  96. /* Read BMP header size */
  97. if (*(rt_uint32_t *)&buffer[14] == 12)
  98. {
  99. /* Bitmap Header Version 2.x */
  100. if (rtgui_filerw_read(file, (void *)buffer, 8, 1) != 8)
  101. {
  102. break;
  103. }
  104. /* Read image size */
  105. is_bmp = RT_TRUE;
  106. }
  107. else
  108. {
  109. /* Bitmap Header Version bigger than 2.x */
  110. if (rtgui_filerw_read(file, (void *)buffer, 8, 1) != 8)
  111. {
  112. break;
  113. }
  114. /* Read image size */
  115. is_bmp = RT_TRUE;
  116. }
  117. } while (0);
  118. return is_bmp;
  119. }
  120. static struct rtgui_image_palette *rtgui_image_bmp_load_palette(
  121. struct rtgui_filerw *file,
  122. rt_uint32_t colorsUsed,
  123. rt_bool_t alpha)
  124. {
  125. /* Load the palette, if any */
  126. rt_uint32_t i;
  127. struct rtgui_image_palette *palette;
  128. palette = rtgui_image_palette_create(colorsUsed);
  129. if (palette == RT_NULL)
  130. {
  131. return RT_NULL;
  132. }
  133. palette->ncolors = colorsUsed;
  134. if (alpha)
  135. {
  136. rt_uint8_t temp[4];
  137. for (i = 0; i < colorsUsed; i++)
  138. {
  139. if (rtgui_filerw_read(file, (void *)&temp, 1, 4) != 4)
  140. {
  141. rtgui_free(palette);
  142. return RT_NULL;
  143. }
  144. palette->colors[i] = RTGUI_ARGB(temp[3], temp[2], temp[1], temp[0]);
  145. }
  146. }
  147. else
  148. {
  149. rt_uint8_t temp[3];
  150. for (i = 0; i < colorsUsed; i++)
  151. {
  152. if (rtgui_filerw_read(file, (void *)&temp, 1, 3) != 3)
  153. {
  154. rtgui_free(palette);
  155. return RT_NULL;
  156. }
  157. palette->colors[i] = RTGUI_RGB(temp[2], temp[1], temp[0]);
  158. }
  159. }
  160. return palette;
  161. }
  162. static rt_bool_t rtgui_image_bmp_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
  163. {
  164. rt_uint8_t scale = 0;
  165. rt_uint8_t *wrkBuffer = RT_NULL;
  166. struct rtgui_image_bmp *bmp = RT_NULL;
  167. rt_uint32_t bmpHeaderSize;
  168. rt_uint32_t colorsUsed;
  169. if (scale > BMP_MAX_SCALING_FACTOR)
  170. {
  171. return RT_FALSE;
  172. }
  173. do
  174. {
  175. wrkBuffer = (rt_uint8_t *)rtgui_malloc(BMP_WORKING_BUFFER_SIZE);
  176. if (wrkBuffer == RT_NULL)
  177. {
  178. rt_kprintf("BMP err: no mem\n");
  179. break;
  180. }
  181. bmp = (struct rtgui_image_bmp *)rtgui_malloc(sizeof(struct rtgui_image_bmp));
  182. if (bmp == RT_NULL)
  183. {
  184. break;
  185. }
  186. bmp->pixels = RT_NULL;
  187. /* Prepare to decode */
  188. if (rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET) < 0)
  189. {
  190. break;
  191. }
  192. if (rtgui_filerw_read(file, (void *)wrkBuffer, 18, 1) != 18)
  193. {
  194. break;
  195. }
  196. /* Read file type */
  197. if (wrkBuffer[0] != 'B' || wrkBuffer[1] != 'M')
  198. {
  199. break;
  200. }
  201. /* Read pixel array offset */
  202. bmp->pixel_offset = *(rt_uint32_t *)&wrkBuffer[10];
  203. /* Read BMP header size */
  204. bmpHeaderSize = *(rt_uint32_t *)&wrkBuffer[14];
  205. colorsUsed = 0;
  206. if (bmpHeaderSize == 12)
  207. {
  208. /* Bitmap Header Version 2.x */
  209. if (rtgui_filerw_read(file, (void *)wrkBuffer, 8, 1) != 8)
  210. {
  211. break;
  212. }
  213. /* Read image size */
  214. bmp->w = (rt_uint32_t) * (rt_uint16_t *)&wrkBuffer[0];
  215. bmp->h = (rt_uint32_t) * (rt_uint16_t *)&wrkBuffer[2];
  216. /* Read bits per pixel */
  217. bmp->bit_per_pixel = (rt_uint8_t) * (rt_uint16_t *)&wrkBuffer[6];
  218. }
  219. else
  220. {
  221. /* Bitmap Header Version bigger than 2.x */
  222. rt_uint32_t compression;
  223. if (rtgui_filerw_read(file, (void *)wrkBuffer, 36, 1) != 36)
  224. {
  225. break;
  226. }
  227. /* Read image size */
  228. bmp->w = *(rt_uint32_t *)&wrkBuffer[0];
  229. bmp->h = *(rt_uint32_t *)&wrkBuffer[4];
  230. /* Read bits per pixel */
  231. bmp->bit_per_pixel = (rt_uint8_t) * (rt_uint16_t *)&wrkBuffer[10];
  232. if (bmp->bit_per_pixel > 32)
  233. {
  234. rt_kprintf("BMP err: unsupported format\n");
  235. break;
  236. }
  237. /* Read compression method */
  238. compression = *(rt_uint32_t *)&wrkBuffer[12];
  239. if (compression != BI_RGB && compression != BI_BITFIELDS)
  240. {
  241. rt_kprintf("BMP err: unsupported format\n");
  242. break;
  243. }
  244. /* Read number of colors */
  245. colorsUsed = *(rt_uint32_t *)&wrkBuffer[28];
  246. }
  247. if (!colorsUsed)
  248. {
  249. colorsUsed = 1 << bmp->bit_per_pixel;
  250. }
  251. /* Load palette */
  252. if (bmp->bit_per_pixel <= 8)
  253. {
  254. if (rtgui_filerw_seek(file, 14 + bmpHeaderSize, RTGUI_FILE_SEEK_SET) < 0)
  255. {
  256. break;
  257. }
  258. image->palette = rtgui_image_bmp_load_palette(file, colorsUsed,
  259. bmpHeaderSize > 12 ? RT_TRUE : RT_FALSE);
  260. if (image->palette == RT_NULL)
  261. {
  262. break;
  263. }
  264. }
  265. /* Set image information */
  266. bmp->is_loaded = RT_FALSE;
  267. bmp->scale = scale;
  268. if (bmp->bit_per_pixel == 1)
  269. {
  270. bmp->pitch = (bmp->w + 7) >> 3;
  271. }
  272. else if (bmp->bit_per_pixel == 4)
  273. {
  274. bmp->pitch = (bmp->w + 1) >> 1;
  275. }
  276. else
  277. {
  278. bmp->pitch = bmp->w * (bmp->bit_per_pixel >> 3);
  279. }
  280. bmp->pad = ((bmp->pitch % 4) ? (4 - (bmp->pitch % 4)) : 0);
  281. bmp->filerw = file;
  282. image->w = (rt_uint16_t)bmp->w >> bmp->scale;
  283. image->h = (rt_uint16_t)bmp->h >> bmp->scale;
  284. image->engine = &rtgui_image_bmp_engine;
  285. image->data = bmp;
  286. /* Start to decode */
  287. if (load == RT_TRUE)
  288. {
  289. rt_bool_t error = RT_FALSE;
  290. rt_uint8_t *dst;
  291. rt_uint32_t imageWidth;
  292. rt_uint16_t readLength, readIndex, loadIndex;
  293. rt_uint8_t skipLength;
  294. rt_uint16_t y;
  295. rt_uint8_t bytePerPixel;
  296. rt_int8_t scale1, scale2;
  297. bytePerPixel = _UI_BITBYTES(bmp->bit_per_pixel);
  298. imageWidth = image->w * bytePerPixel; /* Scaled width in byte */
  299. bmp->pixels = rtgui_malloc(image->h * imageWidth);
  300. if (bmp->pixels == RT_NULL)
  301. {
  302. rt_kprintf("BMP err: no mem to load (%d)\n", image->h * imageWidth);
  303. break;
  304. }
  305. /* Read the pixels. Note that the bmp image is upside down */
  306. if (rtgui_filerw_seek(file, bmp->pixel_offset, RTGUI_FILE_SEEK_SET) < 0)
  307. {
  308. break;
  309. }
  310. if (bmp->bit_per_pixel == 1)
  311. {
  312. if (bmp->scale > 3)
  313. {
  314. scale1 = bmp->scale - 3;
  315. scale2 = 3;
  316. }
  317. else
  318. {
  319. scale1 = 0;
  320. scale2 = bmp->scale;
  321. }
  322. }
  323. else if (bmp->bit_per_pixel == 4)
  324. {
  325. if (bmp->scale > 1)
  326. {
  327. scale1 = bmp->scale - 1;
  328. scale2 = 1;
  329. }
  330. else
  331. {
  332. scale1 = 0;
  333. scale2 = bmp->scale;
  334. }
  335. }
  336. /* Process whole image */
  337. y = 0;
  338. while (y < image->h)
  339. {
  340. dst = bmp->pixels + (image->h - y - 1) * imageWidth;
  341. readIndex = 0;
  342. skipLength = 0;
  343. /* Process a line */
  344. while (readIndex < bmp->pitch)
  345. {
  346. /* Read data to buffer */
  347. readLength = (BMP_WORKING_BUFFER_SIZE > ((rt_uint16_t)bmp->pitch - readIndex)) ? \
  348. ((rt_uint16_t)bmp->pitch - readIndex) : BMP_WORKING_BUFFER_SIZE;
  349. if (rtgui_filerw_read(file, (void *)wrkBuffer, 1, readLength) != readLength)
  350. {
  351. rt_kprintf("BMP err: read failed\n");
  352. error = RT_TRUE;
  353. break;
  354. }
  355. readIndex += readLength;
  356. /* Process read buffer */
  357. if (bmp->bit_per_pixel == 1)
  358. {
  359. rt_uint8_t j;
  360. for (loadIndex = skipLength; loadIndex < readLength; loadIndex += 1 << scale1)
  361. {
  362. for (j = 0; j < 8; j += 1 << scale2)
  363. {
  364. *(dst++) = (wrkBuffer[loadIndex] & (1 << (7 - j))) >> (7 - j);
  365. }
  366. }
  367. if (scale1 && (readLength % (1 << scale1)))
  368. {
  369. skipLength = (1 << scale1) - readLength % (1 << scale1);
  370. }
  371. }
  372. else if (bmp->bit_per_pixel == 4)
  373. {
  374. rt_uint8_t j;
  375. for (loadIndex = skipLength; loadIndex < readLength; loadIndex += 1 << scale1)
  376. {
  377. for (j = 0; j < 8; j += 1 << (2 + scale2))
  378. {
  379. *(dst++) = (wrkBuffer[loadIndex] & (0x0F << (4 - j))) >> (4 - j);
  380. }
  381. }
  382. if (scale1 && (readLength % (1 << scale1)))
  383. {
  384. skipLength = (1 << scale1) - readLength % (1 << scale1);
  385. }
  386. }
  387. else
  388. {
  389. if (bmp->scale == 0)
  390. {
  391. rt_memcpy((void *)dst, (void *)wrkBuffer, readLength);
  392. dst += readLength;
  393. }
  394. else
  395. {
  396. for (loadIndex = skipLength; loadIndex < readLength; loadIndex += bytePerPixel << bmp->scale)
  397. {
  398. rt_memcpy((void *)dst, (void *)&wrkBuffer[loadIndex], bytePerPixel);
  399. dst += bytePerPixel;
  400. }
  401. if (readLength % (1 << bmp->scale))
  402. {
  403. skipLength = (1 << bmp->scale) - readLength % (1 << bmp->scale);
  404. }
  405. }
  406. }
  407. }
  408. if (error)
  409. {
  410. break;
  411. }
  412. y++;
  413. /* Skip padding bytes */
  414. if (bmp->pad)
  415. {
  416. if (rtgui_filerw_seek(file, bmp->pad, RTGUI_FILE_SEEK_CUR) < 0)
  417. {
  418. error = RT_TRUE;
  419. break;
  420. }
  421. }
  422. /* Height scale down */
  423. if (bmp->scale)
  424. {
  425. if (rtgui_filerw_seek(file, (bmp->pitch + bmp->pad) * ((1 << bmp->scale) - 1),
  426. RTGUI_FILE_SEEK_CUR) < 0)
  427. {
  428. error = RT_TRUE;
  429. break;
  430. }
  431. }
  432. }
  433. if (error)
  434. {
  435. break;
  436. }
  437. /* Close file */
  438. rtgui_filerw_close(bmp->filerw);
  439. bmp->filerw = RT_NULL;
  440. bmp->is_loaded = RT_TRUE;
  441. }
  442. /* Release memory */
  443. rtgui_free(wrkBuffer);
  444. return RT_TRUE;
  445. } while (0);
  446. /* Release memory */
  447. rtgui_free(wrkBuffer);
  448. rtgui_free(image->palette);
  449. if (bmp)
  450. rtgui_free(bmp->pixels);
  451. rtgui_free(bmp);
  452. return RT_FALSE;
  453. }
  454. static void rtgui_image_bmp_unload(struct rtgui_image *image)
  455. {
  456. struct rtgui_image_bmp *bmp;
  457. if (image != RT_NULL)
  458. {
  459. bmp = (struct rtgui_image_bmp *)image->data;
  460. /* Release memory */
  461. rtgui_free(bmp->pixels);
  462. if (bmp->filerw != RT_NULL)
  463. {
  464. /* Close file */
  465. rtgui_filerw_close(bmp->filerw);
  466. bmp->filerw = RT_NULL;
  467. }
  468. rtgui_free(bmp);
  469. }
  470. }
  471. static void rtgui_image_bmp_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
  472. {
  473. rt_uint16_t w, h;
  474. struct rtgui_image_bmp *bmp;
  475. rt_uint8_t bytePerPixel;
  476. rt_uint32_t imageWidth;
  477. rt_bool_t error;
  478. bmp = (struct rtgui_image_bmp *)image->data;
  479. RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL || bmp != RT_NULL);
  480. bytePerPixel = _UI_BITBYTES(bmp->bit_per_pixel);
  481. imageWidth = image->w * bytePerPixel; /* Scaled width in byte */
  482. error = RT_FALSE;
  483. do
  484. {
  485. /* this dc is not visible */
  486. if (rtgui_dc_get_visible(dc) != RT_TRUE) break;
  487. /* the minimum rect */
  488. w = _UI_MIN(image->w, rtgui_rect_width(*dst_rect));
  489. h = _UI_MIN(image->h, rtgui_rect_height(*dst_rect));
  490. if (!bmp->is_loaded)
  491. {
  492. rt_uint8_t *wrkBuffer;
  493. rt_uint16_t readLength, readIndex, loadIndex;
  494. rt_uint8_t skipLength;
  495. rt_uint16_t x, y;
  496. rt_int8_t scale1, scale2;
  497. rt_uint16_t y_start = dst_rect->y1 + h - 1;
  498. /* Read the pixels. Note that the bmp image is upside down */
  499. if (rtgui_filerw_seek(bmp->filerw, bmp->pixel_offset, RTGUI_FILE_SEEK_SET) < 0)
  500. {
  501. break;
  502. }
  503. /* the image is upside down. So we need to start from middle if the
  504. * image is higher than the dst_rect. */
  505. if (image->h > rtgui_rect_height(*dst_rect))
  506. {
  507. int hdelta = image->h - rtgui_rect_height(*dst_rect);
  508. if (rtgui_filerw_seek(bmp->filerw, hdelta * (bmp->pitch + bmp->pad) * (1 << bmp->scale),
  509. RTGUI_FILE_SEEK_CUR) < 0)
  510. {
  511. error = RT_TRUE;
  512. break;
  513. }
  514. }
  515. if (bmp->bit_per_pixel == 1)
  516. {
  517. if (bmp->scale > 3)
  518. {
  519. scale1 = bmp->scale - 3;
  520. scale2 = 3;
  521. }
  522. else
  523. {
  524. scale1 = 0;
  525. scale2 = bmp->scale;
  526. }
  527. }
  528. else if (bmp->bit_per_pixel == 4)
  529. {
  530. if (bmp->scale > 1)
  531. {
  532. scale1 = bmp->scale - 1;
  533. scale2 = 1;
  534. }
  535. else
  536. {
  537. scale1 = 0;
  538. scale2 = bmp->scale;
  539. }
  540. }
  541. wrkBuffer = (rt_uint8_t *)rtgui_malloc(
  542. (BMP_WORKING_BUFFER_SIZE > bmp->pitch) ? \
  543. bmp->pitch : BMP_WORKING_BUFFER_SIZE);
  544. if (wrkBuffer == RT_NULL)
  545. {
  546. rt_kprintf("BMP err: no mem (%d)\n", BMP_WORKING_BUFFER_SIZE);
  547. break;
  548. }
  549. /* Process whole image */
  550. y = 0;
  551. while (y < h)
  552. {
  553. x = 0;
  554. readIndex = 0;
  555. skipLength = 0;
  556. /* Process a line */
  557. while (readIndex < bmp->pitch)
  558. {
  559. /* Read data to buffer */
  560. readLength = (BMP_WORKING_BUFFER_SIZE > ((rt_uint16_t)bmp->pitch - readIndex)) ? \
  561. ((rt_uint16_t)bmp->pitch - readIndex) : BMP_WORKING_BUFFER_SIZE;
  562. if (rtgui_filerw_read(bmp->filerw, (void *)wrkBuffer, 1, readLength) != readLength)
  563. {
  564. rt_kprintf("BMP err: read failed\n");
  565. error = RT_TRUE;
  566. break;
  567. }
  568. readIndex += readLength;
  569. /* Process read buffer */
  570. if (bmp->bit_per_pixel == 1)
  571. {
  572. for (loadIndex = skipLength; loadIndex < readLength; loadIndex += 1 << scale1)
  573. {
  574. rt_uint8_t j;
  575. for (j = 0; j < 8; j += 1 << scale2)
  576. {
  577. rtgui_color_t color;
  578. color = image->palette->colors[(wrkBuffer[loadIndex] & (1 << (7 - j))) >> (7 - j)];
  579. rtgui_dc_draw_color_point(dc,
  580. dst_rect->x1 + x++,
  581. y_start - y,
  582. color);
  583. if (x >= w)
  584. break;
  585. }
  586. if (scale1 && (readLength % (1 << scale1)))
  587. {
  588. skipLength = (1 << scale1) - readLength % (1 << scale1);
  589. }
  590. }
  591. }
  592. else if (bmp->bit_per_pixel == 4)
  593. {
  594. for (loadIndex = skipLength; loadIndex < readLength; loadIndex += 1 << scale1)
  595. {
  596. rt_uint8_t j;
  597. for (j = 0; j < 8; j += 1 << (2 + scale2))
  598. {
  599. rtgui_color_t color;
  600. color = image->palette->colors[(wrkBuffer[loadIndex] & (0x0F << (4 - j))) >> (4 - j)];
  601. rtgui_dc_draw_color_point(dc,
  602. dst_rect->x1 + x++,
  603. y_start - y,
  604. color);
  605. if (x >= w)
  606. break;
  607. }
  608. }
  609. if (scale1 && (readLength % (1 << scale1)))
  610. {
  611. skipLength = (1 << scale1) - readLength % (1 << scale1);
  612. }
  613. }
  614. else if (bmp->bit_per_pixel == 8)
  615. {
  616. for (loadIndex = skipLength; loadIndex < readLength; loadIndex += 1 << bmp->scale)
  617. {
  618. rtgui_color_t color;
  619. color = image->palette->colors[wrkBuffer[loadIndex]];
  620. rtgui_dc_draw_color_point(dc,
  621. dst_rect->x1 + x++,
  622. y_start - y,
  623. color);
  624. if (x >= w)
  625. break;
  626. }
  627. if (readLength % (1 << bmp->scale))
  628. {
  629. skipLength = (1 << bmp->scale) - readLength % (1 << bmp->scale);
  630. }
  631. }
  632. else
  633. {
  634. rtgui_blit_line_func blit_line;
  635. rt_uint8_t hw_bytePerPixel = _UI_BITBYTES(hw_driver->bits_per_pixel);
  636. rt_uint8_t temp[4] = {0};
  637. if (!hw_bytePerPixel)
  638. {
  639. hw_bytePerPixel = 1;
  640. }
  641. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_BGR565)
  642. {
  643. blit_line = rtgui_blit_line_get_inv(hw_bytePerPixel, bytePerPixel);
  644. }
  645. else
  646. {
  647. blit_line = rtgui_blit_line_get(hw_bytePerPixel, bytePerPixel);
  648. }
  649. for (loadIndex = skipLength;
  650. loadIndex < readLength;
  651. loadIndex += bytePerPixel << bmp->scale)
  652. {
  653. blit_line(temp, &wrkBuffer[loadIndex], bytePerPixel);
  654. dc->engine->blit_line(dc,
  655. dst_rect->x1 + x, dst_rect->x1 + x + 1,
  656. y_start - y,
  657. temp);
  658. x++;
  659. if (x >= w)
  660. break;
  661. }
  662. if (readLength % (1 << bmp->scale))
  663. {
  664. skipLength = (1 << bmp->scale) - readLength % (1 << bmp->scale);
  665. }
  666. }
  667. }
  668. if (error)
  669. {
  670. break;
  671. }
  672. y++;
  673. /* Skip padding bytes */
  674. if (bmp->pad)
  675. {
  676. if (rtgui_filerw_seek(bmp->filerw, bmp->pad, RTGUI_FILE_SEEK_CUR) < 0)
  677. {
  678. error = RT_TRUE;
  679. break;
  680. }
  681. }
  682. /* Height scale down */
  683. if (bmp->scale)
  684. {
  685. if (rtgui_filerw_seek(bmp->filerw, (bmp->pitch + bmp->pad) * ((1 << bmp->scale) - 1),
  686. RTGUI_FILE_SEEK_CUR) < 0)
  687. {
  688. error = RT_TRUE;
  689. break;
  690. }
  691. }
  692. }
  693. if (error)
  694. {
  695. break;
  696. }
  697. /* Release memory */
  698. rtgui_free(wrkBuffer);
  699. }
  700. else
  701. {
  702. rt_uint16_t x, y;
  703. rt_uint8_t *ptr;
  704. if (bmp->bit_per_pixel <= 8)
  705. {
  706. rtgui_color_t color;
  707. /* 1bpp, and using palette */
  708. for (y = 0; y < h; y ++)
  709. {
  710. ptr = bmp->pixels + (y * imageWidth);
  711. for (x = 0; x < w; x ++)
  712. {
  713. color = image->palette->colors[*(ptr++)];
  714. rtgui_dc_draw_color_point(dc,
  715. dst_rect->x1 + x, dst_rect->y1 + y,
  716. color);
  717. }
  718. }
  719. }
  720. else
  721. {
  722. rtgui_blit_line_func blit_line;
  723. rt_uint8_t hw_bytePerPixel = _UI_BITBYTES(hw_driver->bits_per_pixel);
  724. rt_uint8_t *line_data;
  725. if (hw_driver->pixel_format == RTGRAPHIC_PIXEL_FORMAT_BGR565)
  726. {
  727. blit_line = rtgui_blit_line_get_inv(hw_bytePerPixel, bytePerPixel);
  728. }
  729. else
  730. {
  731. blit_line = rtgui_blit_line_get(hw_bytePerPixel, bytePerPixel);
  732. }
  733. line_data = (rt_uint8_t *)rtgui_malloc(w * rtgui_color_get_bpp(hw_driver->pixel_format));
  734. if (line_data == RT_NULL) break; /* out of memory */
  735. ptr = bmp->pixels;
  736. for (y = 0; y < h; y ++)
  737. {
  738. blit_line(line_data, ptr, bytePerPixel * w);
  739. ptr += imageWidth;
  740. dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w,
  741. dst_rect->y1 + y, line_data);
  742. }
  743. rtgui_free(line_data);
  744. }
  745. }
  746. } while (0);
  747. }
  748. /*
  749. * config bitmap header.
  750. */
  751. void rtgui_image_bmp_header_cfg(struct rtgui_image_bmp_header *bhr, rt_int32_t w, rt_int32_t h, rt_uint16_t bits_per_pixel)
  752. {
  753. int image_size = w * h * _UI_BITBYTES(bits_per_pixel);
  754. int header_size = sizeof(struct rtgui_image_bmp_header);
  755. bhr->bfType = 0x4d42; /* BM */
  756. bhr->bfSize = header_size + image_size; /* data size */
  757. bhr->bfReserved1 = 0;
  758. bhr->bfReserved2 = 0;
  759. bhr->bfOffBits = header_size;
  760. bhr->biSize = 40; /* sizeof BITMAPINFOHEADER */
  761. bhr->biWidth = w;
  762. bhr->biHeight = h;
  763. bhr->biPlanes = 1;
  764. bhr->biBitCount = bits_per_pixel;
  765. bhr->biCompression = BI_BITFIELDS;
  766. bhr->biSizeImage = image_size;
  767. bhr->biXPelsPerMeter = 0;
  768. bhr->biYPelsPerMeter = 0;
  769. bhr->biClrUsed = 0;
  770. bhr->biClrImportant = 0;
  771. if (bhr->biBitCount == 16 && bhr->biCompression == BI_BITFIELDS)
  772. {
  773. bhr->bfSize += 12;
  774. bhr->bfOffBits += 12;
  775. }
  776. }
  777. #ifdef RTGUI_USING_DFS_FILERW
  778. #define WRITE_CLUSTER_SIZE 2048
  779. void bmp_align_write(struct rtgui_filerw *file, char *dest, char *src, rt_int32_t len, rt_int32_t *count)
  780. {
  781. rt_int32_t len_bak = len;
  782. while (len)
  783. {
  784. if (*count >= WRITE_CLUSTER_SIZE)
  785. {
  786. rtgui_filerw_write(file, dest, WRITE_CLUSTER_SIZE, 1);
  787. *count = 0;
  788. }
  789. *(dest + *count) = *(src + (len_bak - len));
  790. len --;
  791. (*count) ++;
  792. }
  793. }
  794. /*
  795. * Grab screen and save as a BMP file
  796. * MACRO RGB_CONVERT_TO_BGR: If the pixel of colors is BGR mode, defined it.
  797. */
  798. void screenshot(const char *filename)
  799. {
  800. struct rtgui_filerw *file;
  801. int w, h, i, pitch;
  802. rt_uint16_t *src;
  803. rt_uint32_t mask;
  804. struct rtgui_image_bmp_header bhr;
  805. struct rtgui_graphic_driver *grp = hw_driver;
  806. #ifdef RGB_CONVERT_TO_BGR
  807. int j;
  808. rt_uint16_t *line_buf;
  809. rt_uint16_t color, tmp;
  810. #endif
  811. char *pixel_buf;
  812. rt_int32_t write_count = 0;
  813. file = rtgui_filerw_create_file(filename, "wb");
  814. if (file == RT_NULL)
  815. {
  816. rt_kprintf("create file failed\n");
  817. return;
  818. }
  819. w = grp->width;
  820. h = grp->height;
  821. pitch = w * sizeof(rt_uint16_t);
  822. #ifdef RGB_CONVERT_TO_BGR
  823. line_buf = rt_malloc(pitch);
  824. if (line_buf == RT_NULL)
  825. {
  826. rt_kprintf("no memory!\n");
  827. return;
  828. }
  829. #endif
  830. pixel_buf = rt_malloc(WRITE_CLUSTER_SIZE);
  831. if (pixel_buf == RT_NULL)
  832. {
  833. rt_kprintf("no memory!\n");
  834. #ifdef RGB_CONVERT_TO_BGR
  835. rt_free(line_buf);
  836. #endif
  837. return;
  838. }
  839. rtgui_image_bmp_header_cfg(&bhr, w, h, grp->bits_per_pixel);
  840. bmp_align_write(file, pixel_buf, (char *)&bhr,
  841. sizeof(struct rtgui_image_bmp_header), &write_count);
  842. if (bhr.biCompression == BI_BITFIELDS)
  843. {
  844. mask = 0xF800; /* Red Mask */
  845. bmp_align_write(file, pixel_buf, (char *)&mask, 4, &write_count);
  846. mask = 0x07E0; /* Green Mask */
  847. bmp_align_write(file, pixel_buf, (char *)&mask, 4, &write_count);
  848. mask = 0x001F; /* Blue Mask */
  849. bmp_align_write(file, pixel_buf, (char *)&mask, 4, &write_count);
  850. }
  851. rtgui_screen_lock(RT_WAITING_FOREVER);
  852. if (grp->framebuffer != RT_NULL)
  853. {
  854. src = (rt_uint16_t *)grp->framebuffer;
  855. src += w * h;
  856. for (i = 0; i < h; i++)
  857. {
  858. src -= w;
  859. #ifdef RGB_CONVERT_TO_BGR
  860. for (j = 0; j < w; j++)
  861. {
  862. tmp = *(src + j);
  863. color = (tmp & 0x001F) << 11;
  864. color += (tmp & 0x07E0);
  865. color += (tmp & 0xF800) >> 11;
  866. *(line_buf + i) = color;
  867. }
  868. bmp_align_write(file, pixel_buf, (char *)line_buf, pitch, &write_count);
  869. #else
  870. bmp_align_write(file, pixel_buf, (char *)src, pitch, &write_count);
  871. #endif
  872. }
  873. }
  874. else
  875. {
  876. rtgui_color_t pixel_color;
  877. rt_uint16_t write_color;
  878. int x;
  879. for (i = h - 1; i >= 0; i--)
  880. {
  881. x = 0;
  882. if (i % 10 == 0)rt_kprintf(">", i);
  883. while (x < w)
  884. {
  885. grp->ops->get_pixel(&pixel_color, x, i);
  886. write_color = rtgui_color_to_565p(pixel_color);
  887. bmp_align_write(file, pixel_buf, (char *)&write_color,
  888. sizeof(rt_uint16_t), &write_count);
  889. x++;
  890. }
  891. }
  892. }
  893. /* write The tail of the last */
  894. if (write_count < WRITE_CLUSTER_SIZE)
  895. rtgui_filerw_write(file, pixel_buf, write_count, 1);
  896. rtgui_screen_unlock();
  897. #ifdef RGB_CONVERT_TO_BGR
  898. rt_free(line_buf);
  899. #endif
  900. rt_free(pixel_buf);
  901. rt_kprintf("bmp create succeed.\n");
  902. rtgui_filerw_close(file);
  903. }
  904. #ifdef RT_USING_FINSH
  905. #include <finsh.h>
  906. FINSH_FUNCTION_EXPORT(screenshot, usage: screenshot(filename));
  907. #endif
  908. #endif
  909. void rtgui_image_bmp_init()
  910. {
  911. /* register bmp on image system */
  912. rtgui_image_register_engine(&rtgui_image_bmp_engine);
  913. }
  914. #endif