Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Zgv v2.7 - GIF, JPEG and PBM/PGM/PPM viewer, for VGA PCs running Linux.
  2.  * Copyright (C) 1993-1995 Russell Marks. See README for license details.
  3.  *
  4.  * readjpeg.c - interface to the IJG's JPEG software, derived from
  5.  *               their example.c.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <setjmp.h>
  13. #include <sys/file.h>  /* for open et al */
  14. #include <jpeglib.h>
  15. #include "libmgfx.h"
  16.  
  17. static mgfx_image_t * the_image;
  18. static FILE * global_jpeg_infile;
  19. static struct jpeg_decompress_struct cinfo;
  20.  
  21. struct my_error_mgr
  22. {
  23.  struct jpeg_error_mgr pub;
  24.  jmp_buf setjmp_buffer;
  25. };
  26.  
  27. typedef struct my_error_mgr * my_error_ptr;
  28.  
  29. static void jpegerr(char *msgtext)
  30. {
  31. }
  32.  
  33. static void my_error_exit(j_common_ptr cinfo)
  34. {
  35.  my_error_ptr myerr=(my_error_ptr) cinfo->err;
  36.  char buf[JMSG_LENGTH_MAX];
  37.  (*cinfo->err->format_message)(cinfo,buf);
  38.  jpegerr(buf);
  39.  longjmp(myerr->setjmp_buffer, 1);
  40. }
  41.  
  42. static void my_output_message(j_common_ptr cinfo)
  43. {
  44. }
  45.  
  46. static void aborted_file_jpeg_cleanup()
  47. {
  48.  jpeg_destroy_decompress(&cinfo);
  49.  fclose(global_jpeg_infile);
  50. }
  51.  
  52. int read_JPEG_file(FILE * in,mgfx_image_t * img)
  53. {
  54.  struct my_error_mgr jerr;
  55.  int row_stride;
  56.  int tmp,f;
  57.  unsigned char *ptr;
  58.  the_image=img;
  59.  if((img->pal=(byte *)malloc(768))==NULL) return(_PICERR_NOMEM);
  60.  cinfo.err=jpeg_std_error(&jerr.pub);
  61.  jerr.pub.error_exit=my_error_exit;
  62.  jerr.pub.output_message=my_output_message;
  63.  if(setjmp(jerr.setjmp_buffer))
  64.  {
  65.   jpeg_destroy_decompress(&cinfo);
  66.   free(img->pal);
  67.   return(_PICERR_CORRUPT);
  68.  }
  69.  jpeg_create_decompress(&cinfo);
  70.  jpeg_stdio_src(&cinfo,in);
  71.  jpeg_read_header(&cinfo,TRUE);
  72.  cinfo.dct_method=JDCT_FLOAT;
  73.  img->bpp=24;
  74.  if(cinfo.jpeg_color_space==JCS_GRAYSCALE)
  75.  {
  76.   cinfo.out_color_space=JCS_GRAYSCALE;
  77.   cinfo.desired_number_of_colors=256;
  78.   cinfo.quantize_colors=FALSE;
  79.   cinfo.two_pass_quantize=FALSE;
  80.   img->bpp=8;
  81.   for(f=0;f<256;f++)
  82.    img->pal[f]=img->pal[256+f]=img->pal[512+f]=f;
  83.  }
  84.  img->width=cinfo.image_width;
  85.  img->height=cinfo.image_height;
  86.  img->the_image=(byte *)malloc(((img->bpp+7)/8)*img->width*img->height);
  87.  if(img->the_image==NULL)
  88.  {
  89.   jpegerr("Out of memory");
  90.   longjmp(jerr.setjmp_buffer,1);
  91.  }
  92.  jpeg_start_decompress(&cinfo);
  93.  if(img->bpp==8 && cinfo.jpeg_color_space!=JCS_GRAYSCALE)
  94.    for(f=0;f<cinfo.actual_number_of_colors;f++)
  95.    {
  96.     img->pal[    f]=cinfo.colormap[0][f];
  97.     img->pal[256+f]=cinfo.colormap[1][f];
  98.     img->pal[512+f]=cinfo.colormap[2][f];
  99.    }
  100.  ptr=img->the_image;
  101.  row_stride=((img->bpp+7)/8)*img->width;
  102.  if(img->bpp==8)
  103.    while(cinfo.output_scanline<img->height)
  104.     {
  105.     jpeg_read_scanlines(&cinfo,&ptr,1);
  106.     ptr+=row_stride;
  107.     }
  108.  else
  109.   while(cinfo.output_scanline<img->height)
  110.     {
  111.     jpeg_read_scanlines(&cinfo,&ptr,1);
  112.     for(f=0;f<img->width;f++) { tmp=*ptr; *ptr=ptr[2]; ptr[2]=tmp; ptr+=3; }
  113.     }
  114.  jpeg_finish_decompress(&cinfo);
  115.  jpeg_destroy_decompress(&cinfo);
  116.  return(_PIC_OK);
  117. }
  118.  
  119. static struct mgfx_image_format this_fmt_1={
  120.  "JPEG",
  121.  "jpg",
  122.  read_JPEG_file,
  123.  NULL,
  124. };
  125.  
  126. static struct mgfx_image_format this_fmt_2={
  127.  "JPEG",
  128.  "jpeg",
  129.  read_JPEG_file,
  130.  NULL,
  131. };
  132.  
  133. void mgfx_register_jpeg(void)
  134. {
  135.  register_image_format(&this_fmt_1);
  136.  register_image_format(&this_fmt_2);
  137. }
  138.