home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / toollib_2 / Examples / GFXView / Builds / c / jpeg_pic < prev    next >
Encoding:
Text File  |  1996-06-13  |  3.3 KB  |  150 lines

  1. /*** jpeg_pic.c ***/
  2. /* Routines for loading and displaying a jpeg
  3.  * (c) Paul Field 1995
  4.  */
  5.  
  6.  
  7. #include "jpeg_pic.h"
  8.  
  9. #include <assert.h>
  10. #include <stdlib.h>
  11.  
  12. #include "jpeg.h"
  13. #include "osfile.h"
  14. #include "wimpreadsysinfo.h"
  15.  
  16. #include "allocate.h"
  17. #include "exception.h"
  18. #include "shifting.h"
  19.  
  20.  
  21. struct jpeg_pic_str
  22.  { jpeg_image *jpeg;
  23.  };
  24.  
  25.  
  26.  
  27. bool jpeg_pic_supported(void)
  28.  { return getenv("GFXView$AllowJPEGs") != NULL;
  29.  }
  30.  
  31.  
  32.  
  33.  
  34. jpeg_pic *jpeg_pic_create(const char *file_name)
  35.  { int      size;
  36.    bits     file_type;
  37.    jpeg_pic *pic;
  38.  
  39.    osfile_read_stamped_no_path(file_name, NULL, NULL, &size, NULL, &file_type);
  40.    assert(file_type == jpeg_FILE_TYPE);
  41.  
  42.    pic = memory_allocate(sizeof(*pic));
  43.    pic->jpeg = NULL;   /* It's OK to 'free' a NULL */
  44.  
  45.    try
  46.     { shifting_allocate((shifting_ptr)&pic->jpeg, size);
  47.       osfile_load_stamped_no_path(file_name, (byte *)pic->jpeg, NULL, NULL, NULL, NULL);
  48.     }
  49.    catch
  50.     { jpeg_pic_destroy(pic);
  51.       throw();
  52.     }
  53.    catch_end
  54.  
  55.    return pic;
  56.  }
  57.  
  58.  
  59.  
  60.  
  61. void jpeg_pic_destroy(jpeg_pic *pic)
  62.  { if (pic != NULL)
  63.     { shifting_free((shifting_ptr)&pic->jpeg);
  64.       memory_free(pic);
  65.     }
  66.  }
  67.  
  68.  
  69.  
  70.  
  71. void jpeg_pic_draw(const jpeg_pic *pic, int origin_x, int origin_y, unsigned scale_percent, const os_box *clip)
  72.  { os_factors scale_factors;
  73.    int        xdpi;
  74.    int        ydpi;
  75.    int        x_eig;
  76.    int        y_eig;
  77.    unsigned   x_os_per_pixel;
  78.    unsigned   y_os_per_pixel;
  79.    unsigned   x_pixels_per_inch;
  80.    unsigned   y_pixels_per_inch;
  81.  
  82.    assert(pic != NULL);
  83.    NOT_USED(clip);
  84.  
  85.    jpeginfo_dimensions(pic->jpeg, shifting_size((shifting_ptr)&pic->jpeg), NULL, NULL, &xdpi, &ydpi, NULL);
  86.  
  87.    os_read_mode_variable(os_CURRENT_MODE, os_MODEVAR_XEIG_FACTOR, &x_eig);
  88.    x_os_per_pixel    = 1 << x_eig;
  89.    x_pixels_per_inch = os_INCH / x_os_per_pixel;
  90.  
  91.    os_read_mode_variable(os_CURRENT_MODE, os_MODEVAR_YEIG_FACTOR, &y_eig);
  92.    y_os_per_pixel    = 1 << y_eig;
  93.    y_pixels_per_inch = os_INCH / y_os_per_pixel;
  94.  
  95.    /* Set up scale factors */
  96.    scale_factors.xmul = x_pixels_per_inch * scale_percent;
  97.    scale_factors.xdiv = xdpi              * 100;
  98.  
  99.    scale_factors.ymul = y_pixels_per_inch * scale_percent;
  100.    scale_factors.ydiv = ydpi              * 100;
  101.  
  102.    jpeg_plot_scaled
  103.     ( pic->jpeg,
  104.       origin_x,
  105.       origin_y,
  106.       &scale_factors,
  107.       shifting_size((shifting_ptr)&pic->jpeg),
  108.       jpeg_SCALE_DITHERED | jpeg_SCALE_ERROR_DIFFUSED
  109.     );
  110.  }
  111.  
  112.  
  113.  
  114. void jpeg_pic_size(const jpeg_pic *pic, unsigned *width, unsigned *height)
  115.  { int jpeg_width;
  116.    int jpeg_height;
  117.    int xdpi;
  118.    int ydpi;
  119.  
  120.    assert(pic != NULL);
  121.    assert(width != NULL);
  122.    assert(height != NULL);
  123.  
  124.    jpeginfo_dimensions(pic->jpeg, shifting_size((shifting_ptr)&pic->jpeg), &jpeg_width, &jpeg_height, &xdpi, &ydpi, NULL);
  125.  
  126.    /* Convert width,height from pixels to OS units */
  127.    *width  = (jpeg_width  * os_INCH)/xdpi;
  128.    *height = (jpeg_height * os_INCH)/ydpi;
  129.  }
  130.  
  131.  
  132.  
  133.  
  134. unsigned jpeg_pic_suggested_scale(const jpeg_pic *pic)
  135.  { int        xdpi;
  136.    int        x_eig;
  137.    unsigned   x_os_per_pixel;
  138.    unsigned   x_pixels_per_inch;
  139.  
  140.    assert(pic != NULL);
  141.  
  142.    jpeginfo_dimensions(pic->jpeg, shifting_size((shifting_ptr)&pic->jpeg), NULL, NULL, &xdpi, NULL, NULL);
  143.  
  144.    os_read_mode_variable(os_CURRENT_MODE, os_MODEVAR_XEIG_FACTOR, &x_eig);
  145.    x_os_per_pixel    = 1 << x_eig;
  146.    x_pixels_per_inch = os_INCH / x_os_per_pixel;
  147.  
  148.    return (xdpi*100) / x_pixels_per_inch;
  149.  }
  150.