image_loader_jpg.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*************************************************/
  2. /* image_loader_jpg.cpp */
  3. /*************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /*************************************************/
  7. /* Source code within this file is: */
  8. /* (c) 2007-2010 Juan Linietsky, Ariel Manzur */
  9. /* All Rights Reserved. */
  10. /*************************************************/
  11. #include "image_loader_jpg.h"
  12. #include "print_string.h"
  13. #include "os/os.h"
  14. #include "drivers/jpg/tinyjpeg.h"
  15. static void* _tinyjpg_alloc(unsigned int amount) {
  16. return memalloc(amount);
  17. }
  18. static void _tinyjpg_free(void *ptr) {
  19. memfree(ptr);
  20. }
  21. Error ImageLoaderJPG::load_image(Image *p_image,FileAccess *f) {
  22. DVector<uint8_t> src_image;
  23. int src_image_len = f->get_len();
  24. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  25. src_image.resize(src_image_len);
  26. DVector<uint8_t>::Write w = src_image.write();
  27. f->get_buffer(&w[0],src_image_len);
  28. f->close();
  29. jdec_private* jdec=tinyjpeg_init(_tinyjpg_alloc,_tinyjpg_free);
  30. ERR_FAIL_COND_V(!jdec,ERR_UNAVAILABLE);
  31. int ret = tinyjpeg_parse_header(jdec,&w[0],src_image_len);
  32. if (ret!=0) {
  33. tinyjpeg_free(jdec);
  34. }
  35. ERR_FAIL_COND_V(ret!=0,ERR_FILE_CORRUPT);
  36. unsigned int width,height;
  37. tinyjpeg_get_size(jdec,&width,&height);
  38. DVector<uint8_t> imgdata;
  39. imgdata.resize(width*height*3);
  40. DVector<uint8_t>::Write imgdataw = imgdata.write();
  41. unsigned char *components[1]={&imgdataw[0]};
  42. tinyjpeg_set_components(jdec,components,1);
  43. tinyjpeg_decode(jdec,TINYJPEG_FMT_RGB24);
  44. imgdataw = DVector<uint8_t>::Write();
  45. Image dst_image(width,height,0,Image::FORMAT_RGB,imgdata);
  46. tinyjpeg_free(jdec);
  47. *p_image=dst_image;
  48. return OK;
  49. }
  50. void ImageLoaderJPG::get_recognized_extensions(List<String> *p_extensions) const {
  51. p_extensions->push_back("jpg");
  52. p_extensions->push_back("jpeg");
  53. }
  54. ImageLoaderJPG::ImageLoaderJPG() {
  55. }