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.
 
 
 
 
 

103 lines
2.8 KiB

  1. /****************************************************************************
  2. jpeg.c - read and write jpeg images using libjpeg routines
  3. Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. ****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <jpeglib.h>
  20. #include "const.h"
  21. int
  22. read_jpeg(const char *filename, int *width, int *height, unsigned char **rgb)
  23. {
  24. int ret = 0;
  25. struct jpeg_decompress_struct cinfo;
  26. struct jpeg_error_mgr jerr;
  27. unsigned char *ptr = NULL;
  28. unsigned int i, ipos;
  29. FILE *infile = fopen(filename, "rb");
  30. if (infile == NULL) {
  31. fprintf(stderr, "Can not fopen file: %s\n",filename);
  32. return ret;
  33. }
  34. cinfo.err = jpeg_std_error(&jerr);
  35. jpeg_create_decompress(&cinfo);
  36. jpeg_stdio_src(&cinfo, infile);
  37. jpeg_read_header(&cinfo, TRUE);
  38. jpeg_start_decompress(&cinfo);
  39. /* Prevent against integer overflow */
  40. if(cinfo.output_width >= MAX_DIMENSION || cinfo.output_height >= MAX_DIMENSION) {
  41. fprintf(stderr, "Unreasonable dimension found in file: %s\n",filename);
  42. goto close_file;
  43. }
  44. *width = cinfo.output_width;
  45. *height = cinfo.output_height;
  46. rgb[0] = malloc(3 * cinfo.output_width * cinfo.output_height);
  47. if (rgb[0] == NULL) {
  48. fprintf(stderr, "Can't allocate memory for JPEG file.\n");
  49. goto close_file;
  50. }
  51. if (cinfo.output_components == 3) {
  52. ptr = rgb[0];
  53. while (cinfo.output_scanline < cinfo.output_height) {
  54. jpeg_read_scanlines(&cinfo, &ptr, 1);
  55. ptr += 3 * cinfo.output_width;
  56. }
  57. } else if (cinfo.output_components == 1) {
  58. ptr = malloc(cinfo.output_width);
  59. if (ptr == NULL) {
  60. fprintf(stderr, "Can't allocate memory for JPEG file.\n");
  61. goto rgb_free;
  62. }
  63. ipos = 0;
  64. while (cinfo.output_scanline < cinfo.output_height) {
  65. jpeg_read_scanlines(&cinfo, &ptr, 1);
  66. for (i = 0; i < cinfo.output_width; i++) {
  67. memset(rgb[0] + ipos, ptr[i], 3);
  68. ipos += 3;
  69. }
  70. }
  71. free(ptr);
  72. }
  73. jpeg_finish_decompress(&cinfo);
  74. ret = 1;
  75. goto close_file;
  76. rgb_free:
  77. free(rgb[0]);
  78. close_file:
  79. jpeg_destroy_decompress(&cinfo);
  80. fclose(infile);
  81. return(ret);
  82. }