Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

223 wiersze
5.2 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * jpeg.c: libjpeg I/O functions
  17. */
  18. #include "../modular.h"
  19. #include <setjmp.h>
  20. static int pipi_free_jpeg(pipi_image_t *img);
  21. struct my_error_mgr {
  22. struct jpeg_error_mgr pub;
  23. jmp_buf setjmp_buffer;
  24. };
  25. typedef struct my_error_mgr * my_error_ptr;
  26. static void format_msg(j_common_ptr cinfo, char *buf)
  27. {
  28. }
  29. static void emit_msg(j_common_ptr cinfo, int level)
  30. {
  31. }
  32. static void error_msg(j_common_ptr cinfo)
  33. {
  34. my_error_ptr myerr = (my_error_ptr) cinfo->err;
  35. cinfo->client_data = (void*)0x1;
  36. longjmp(myerr->setjmp_buffer, 1);
  37. }
  38. static void output_msg(j_common_ptr cinfo)
  39. {
  40. }
  41. pipi_image_t *pipi_load_jpeg(const char *name)
  42. {
  43. struct jpeg_decompress_struct cinfo;
  44. struct my_error_mgr jerr;
  45. unsigned char *image = NULL, *scanline = NULL;
  46. pipi_image_t *img = NULL;
  47. unsigned int i, j, k = 0;
  48. FILE *fp = fopen(name, "rb");
  49. if(!fp) goto end;
  50. if (setjmp(jerr.setjmp_buffer)) {
  51. goto end;
  52. }
  53. cinfo.err = jpeg_std_error(&jerr.pub);
  54. jerr.pub.error_exit = error_msg;
  55. jerr.pub.emit_message = emit_msg;
  56. jerr.pub.output_message = output_msg;
  57. jerr.pub.format_message = format_msg;
  58. /* Initialize libjpeg */
  59. jpeg_create_decompress(&cinfo);
  60. cinfo.client_data = 0x0;
  61. jpeg_stdio_src(&cinfo, fp);
  62. if(cinfo.client_data == (void *)0x1) {
  63. goto end;
  64. }
  65. jpeg_read_header(&cinfo, TRUE);
  66. if(cinfo.client_data == (void *)0x1) {
  67. goto end;
  68. }
  69. jpeg_start_decompress(&cinfo);
  70. if(cinfo.client_data == (void *)0x1) {
  71. goto end;
  72. }
  73. /* One scanline */
  74. image = malloc(cinfo.output_width * cinfo.output_height * 4);
  75. if(!image) goto end;
  76. scanline = malloc(cinfo.output_width * 3);
  77. for(i=0; i < cinfo.output_height; i++)
  78. {
  79. jpeg_read_scanlines(&cinfo, &scanline, 1);
  80. if(cinfo.client_data == (void *)0x1) {
  81. free(img);
  82. img = NULL;
  83. goto end;
  84. }
  85. for(j=0 ; j<cinfo.output_width*3; j+=3)
  86. {
  87. image[k+2] = scanline[j];
  88. image[k+1] = scanline[j+1];
  89. image[k] = scanline[j+2];
  90. image[k+3] = 255;
  91. k+=4;
  92. }
  93. }
  94. img = pipi_new(cinfo.output_width, cinfo.output_height);
  95. img->p[PIPI_PIXELS_RGBA_U8].pixels = image;
  96. img->p[PIPI_PIXELS_RGBA_U8].w = cinfo.output_width;
  97. img->p[PIPI_PIXELS_RGBA_U8].h = cinfo.output_height;
  98. img->p[PIPI_PIXELS_RGBA_U8].pitch = cinfo.output_width*4;
  99. img->p[PIPI_PIXELS_RGBA_U8].bpp = 24;
  100. img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h;
  101. img->last_modified = PIPI_PIXELS_RGBA_U8;
  102. img->codec_priv = (void *)&cinfo;
  103. img->codec_format = PIPI_PIXELS_RGBA_U8;
  104. img->codec_free = pipi_free_jpeg;
  105. img->wrap = 0;
  106. img->u8 = 1;
  107. end:
  108. if(fp) fclose(fp);
  109. if(scanline) free(scanline);
  110. jpeg_destroy_decompress(&cinfo);
  111. return img;
  112. }
  113. int pipi_save_jpeg(pipi_image_t *img, const char *name)
  114. {
  115. int quality = 75; /* FIXME */
  116. struct jpeg_compress_struct cinfo;
  117. struct my_error_mgr jerr;
  118. unsigned char *data = NULL;
  119. unsigned char *line = NULL;
  120. pipi_pixels_t *pixels = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
  121. if (!pixels)
  122. return 0;
  123. data = pixels->pixels;
  124. line = malloc(img->w * 3 * sizeof(unsigned char));
  125. if (!line)
  126. return 0;
  127. FILE *fp = fopen(name, "wb");
  128. if (!fp) {
  129. free(line);
  130. return 0;
  131. }
  132. if (setjmp(jerr.setjmp_buffer)) {
  133. goto end;
  134. }
  135. jerr.pub.error_exit = error_msg;
  136. jerr.pub.emit_message = emit_msg;
  137. jerr.pub.output_message = output_msg;
  138. cinfo.err = jpeg_std_error(&(jerr.pub));
  139. jpeg_create_compress(&cinfo);
  140. jpeg_stdio_dest(&cinfo, fp);
  141. cinfo.image_width = img->w;
  142. cinfo.image_height = img->h;
  143. cinfo.input_components = 3;
  144. cinfo.in_color_space = JCS_RGB;
  145. jpeg_set_defaults(&cinfo);
  146. jpeg_set_quality(&cinfo, quality, TRUE);
  147. jpeg_start_compress(&cinfo, TRUE);
  148. int j, i, y = 0;
  149. uint32_t *ptr = (uint32_t*)data;
  150. JSAMPROW *jbuf;
  151. while (cinfo.next_scanline < cinfo.image_height)
  152. {
  153. for (j = 0, i = 0; i < img->w; i++)
  154. {
  155. line[j++] = ((*ptr) >> 16) & 0xff;
  156. line[j++] = ((*ptr) >> 8) & 0xff;
  157. line[j++] = ((*ptr)) & 0xff;
  158. ptr++;
  159. }
  160. jbuf = (JSAMPROW *) (&line);
  161. jpeg_write_scanlines(&cinfo, jbuf, 1);
  162. y++;
  163. }
  164. jpeg_finish_compress(&cinfo);
  165. end:
  166. jpeg_destroy_compress(&cinfo);
  167. free(line);
  168. fclose(fp);
  169. pipi_release_pixels(img, pixels);
  170. return 1;
  171. }
  172. static int pipi_free_jpeg(pipi_image_t *img)
  173. {
  174. if(img->p[PIPI_PIXELS_RGBA_U8].pixels)
  175. free(img->p[PIPI_PIXELS_RGBA_U8].pixels);
  176. return 0;
  177. }