You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jpeg.c 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. cinfo.err = jpeg_std_error(&jerr);
  43. jerr.error_exit = error_msg;
  44. jerr.emit_message = emit_msg;
  45. jerr.output_message = output_msg;
  46. jerr.format_message = format_msg;
  47. jpeg_create_decompress(&cinfo);
  48. cinfo.client_data = 0x0;
  49. jpeg_stdio_src(&cinfo, fp);
  50. if((int)cinfo.client_data == 0x1) {
  51. goto end;
  52. }
  53. jpeg_read_header(&cinfo, TRUE);
  54. if((int)cinfo.client_data == 0x1) {
  55. goto end;
  56. }
  57. jpeg_start_decompress(&cinfo);
  58. if((int)cinfo.client_data == 0x1) {
  59. goto end;
  60. }
  61. image = malloc(cinfo.output_width * cinfo.output_height * 4);
  62. if(!image) goto end;
  63. scanline = malloc(cinfo.output_width * 3);
  64. /* Read scanlines, converting them to RGBA */
  65. for(i=0; i < cinfo.output_height; i++)
  66. {
  67. jpeg_read_scanlines(&cinfo, &scanline, 1);
  68. if((int)cinfo.client_data == 0x1) {
  69. free(img);
  70. img = NULL;
  71. goto end;
  72. }
  73. for(j=0 ; j<cinfo.output_width*3; j+=3)
  74. {
  75. image[k+2] = scanline[j];
  76. image[k+1] = scanline[j+1];
  77. image[k] = scanline[j+2];
  78. image[k+3] = 255;
  79. k+=4;
  80. }
  81. }
  82. img = pipi_new(cinfo.output_width, cinfo.output_height);
  83. img->p[PIPI_PIXELS_RGBA_C].pixels = image;
  84. img->p[PIPI_PIXELS_RGBA_C].w = cinfo.output_width;
  85. img->p[PIPI_PIXELS_RGBA_C].h = cinfo.output_height;
  86. img->p[PIPI_PIXELS_RGBA_C].pitch = cinfo.output_width*4;
  87. img->p[PIPI_PIXELS_RGBA_C].bpp = 24;
  88. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  89. img->last_modified = PIPI_PIXELS_RGBA_C;
  90. img->codec_priv = (void *)&cinfo;
  91. img->codec_format = PIPI_PIXELS_RGBA_C;
  92. img->codec_free = pipi_free_jpeg;
  93. img->wrap = 0;
  94. img->u8 = 1;
  95. end:
  96. if(fp) fclose(fp);
  97. if(scanline) free(scanline);
  98. jpeg_destroy_decompress(&cinfo);
  99. return img;
  100. }
  101. int pipi_save_jpeg(pipi_image_t *img, const char *name)
  102. {
  103. return 0;
  104. }
  105. static int pipi_free_jpeg(pipi_image_t *img)
  106. {
  107. return 0;
  108. }