Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

201 linhas
4.8 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. static int pipi_free_jpeg(pipi_image_t *img);
  20. static void format_msg(j_common_ptr cinfo, char *buf)
  21. {
  22. }
  23. static void emit_msg(j_common_ptr cinfo, int level)
  24. {
  25. }
  26. static void error_msg(j_common_ptr cinfo)
  27. {
  28. cinfo->client_data = (void*)0x1;
  29. }
  30. static void output_msg(j_common_ptr cinfo)
  31. {
  32. }
  33. pipi_image_t *pipi_load_jpeg(const char *name)
  34. {
  35. struct jpeg_decompress_struct cinfo;
  36. struct jpeg_error_mgr jerr;
  37. unsigned char *image = NULL, *scanline = NULL;
  38. pipi_image_t *img = NULL;
  39. unsigned int i, j, k = 0;
  40. FILE *fp = fopen(name, "rb");
  41. if(!fp) goto end;
  42. /* Set callbacks */
  43. cinfo.err = jpeg_std_error(&jerr);
  44. jerr.error_exit = error_msg;
  45. jerr.emit_message = emit_msg;
  46. jerr.output_message = output_msg;
  47. jerr.format_message = format_msg;
  48. /* Initialize libjpeg */
  49. jpeg_create_decompress(&cinfo);
  50. cinfo.client_data = 0x0;
  51. jpeg_stdio_src(&cinfo, fp);
  52. if(cinfo.client_data == (void *)0x1) {
  53. goto end;
  54. }
  55. jpeg_read_header(&cinfo, TRUE);
  56. if(cinfo.client_data == (void *)0x1) {
  57. goto end;
  58. }
  59. jpeg_start_decompress(&cinfo);
  60. if(cinfo.client_data == (void *)0x1) {
  61. goto end;
  62. }
  63. /* One scanline */
  64. image = malloc(cinfo.output_width * cinfo.output_height * 4);
  65. if(!image) goto end;
  66. scanline = malloc(cinfo.output_width * 3);
  67. for(i=0; i < cinfo.output_height; i++)
  68. {
  69. jpeg_read_scanlines(&cinfo, &scanline, 1);
  70. if(cinfo.client_data == (void *)0x1) {
  71. free(img);
  72. img = NULL;
  73. goto end;
  74. }
  75. for(j=0 ; j<cinfo.output_width*3; j+=3)
  76. {
  77. image[k+2] = scanline[j];
  78. image[k+1] = scanline[j+1];
  79. image[k] = scanline[j+2];
  80. image[k+3] = 255;
  81. k+=4;
  82. }
  83. }
  84. img = pipi_new(cinfo.output_width, cinfo.output_height);
  85. img->p[PIPI_PIXELS_RGBA_C].pixels = image;
  86. img->p[PIPI_PIXELS_RGBA_C].w = cinfo.output_width;
  87. img->p[PIPI_PIXELS_RGBA_C].h = cinfo.output_height;
  88. img->p[PIPI_PIXELS_RGBA_C].pitch = cinfo.output_width*4;
  89. img->p[PIPI_PIXELS_RGBA_C].bpp = 24;
  90. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  91. img->last_modified = PIPI_PIXELS_RGBA_C;
  92. img->codec_priv = (void *)&cinfo;
  93. img->codec_format = PIPI_PIXELS_RGBA_C;
  94. img->codec_free = pipi_free_jpeg;
  95. img->wrap = 0;
  96. img->u8 = 1;
  97. end:
  98. if(fp) fclose(fp);
  99. if(scanline) free(scanline);
  100. jpeg_destroy_decompress(&cinfo);
  101. return img;
  102. }
  103. int pipi_save_jpeg(pipi_image_t *img, const char *name)
  104. {
  105. int quality = 75; /* FIXME */
  106. struct jpeg_compress_struct cinfo;
  107. struct jpeg_error_mgr jerr;
  108. unsigned char *data = NULL;
  109. unsigned char *line = NULL;
  110. pipi_pixels_t *pixels = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  111. if (!pixels)
  112. return 0;
  113. data = pixels->pixels;
  114. line = malloc(img->w * 3 * sizeof(unsigned char));
  115. if (!line)
  116. return 0;
  117. FILE *fp = fopen(name, "wb");
  118. if (!fp) {
  119. free(line);
  120. return 0;
  121. }
  122. jerr.error_exit = error_msg;
  123. jerr.emit_message = emit_msg;
  124. jerr.output_message = output_msg;
  125. cinfo.err = jpeg_std_error(&(jerr));
  126. jpeg_create_compress(&cinfo);
  127. jpeg_stdio_dest(&cinfo, fp);
  128. cinfo.image_width = img->w;
  129. cinfo.image_height = img->h;
  130. cinfo.input_components = 3;
  131. cinfo.in_color_space = JCS_RGB;
  132. jpeg_set_defaults(&cinfo);
  133. jpeg_set_quality(&cinfo, quality, TRUE);
  134. jpeg_start_compress(&cinfo, TRUE);
  135. int j, i, y = 0;
  136. uint32_t *ptr = (uint32_t*)data;
  137. JSAMPROW *jbuf;
  138. while (cinfo.next_scanline < cinfo.image_height)
  139. {
  140. for (j = 0, i = 0; i < img->w; i++)
  141. {
  142. line[j++] = ((*ptr) >> 16) & 0xff;
  143. line[j++] = ((*ptr) >> 8) & 0xff;
  144. line[j++] = ((*ptr)) & 0xff;
  145. ptr++;
  146. }
  147. jbuf = (JSAMPROW *) (&line);
  148. jpeg_write_scanlines(&cinfo, jbuf, 1);
  149. y++;
  150. }
  151. jpeg_finish_compress(&cinfo);
  152. jpeg_destroy_compress(&cinfo);
  153. free(line);
  154. fclose(fp);
  155. return 1;
  156. }
  157. static int pipi_free_jpeg(pipi_image_t *img)
  158. {
  159. if(img->p[PIPI_PIXELS_RGBA_C].pixels)
  160. free(img->p[PIPI_PIXELS_RGBA_C].pixels);
  161. return 0;
  162. }