home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / zoom.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  4KB  |  181 lines

  1. /* zoom.c:
  2.  *
  3.  * zoom an image
  4.  *
  5.  * jim frost 10.11.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. static unsigned int *buildIndex(width, zoom, rwidth)
  15.      unsigned int  width;
  16.      unsigned int  zoom;
  17.      unsigned int *rwidth;
  18. { float         fzoom;
  19.   unsigned int *index;
  20.   unsigned int  a;
  21.  
  22.   if (!zoom) {
  23.     fzoom= 100.0;
  24.     *rwidth= width;
  25.   }
  26.   else {
  27.     fzoom= (float)zoom / 100.0;
  28.     *rwidth= fzoom * width;
  29.   }
  30.   index= (unsigned int *)lmalloc(sizeof(unsigned int) * *rwidth);
  31.   for (a= 0; a < *rwidth; a++)
  32.     if (zoom)
  33.       *(index + a)= (float)a / fzoom;
  34.     else
  35.       *(index + a)= a;
  36.   return(index);
  37. }
  38.  
  39. Image *zoom(oimage, xzoom, yzoom, verbose)
  40.      Image        *oimage;
  41.      unsigned int  xzoom, yzoom;
  42.      unsigned int  verbose;
  43. { char          buf[BUFSIZ];
  44.   Image        *image;
  45.   unsigned int *xindex, *yindex;
  46.   unsigned int  xwidth, ywidth;
  47.   unsigned int  x, y, xsrc, ysrc;
  48.   unsigned int  pixlen;
  49.   unsigned int  srclinelen;
  50.   unsigned int  destlinelen;
  51.   byte         *srcline, *srcptr;
  52.   byte         *destline, *destptr;
  53.   byte          srcmask, destmask, bit;
  54.   Pixel         value;
  55.  
  56.   goodImage(oimage, "zoom");
  57.  
  58.   if (!xzoom && !yzoom) /* stupid user */
  59.     return(NULL);
  60.  
  61.   if (!xzoom) {
  62.     if (verbose)
  63.       printf("  Zooming image Y axis by %d%%...", yzoom);
  64.       sprintf(buf, "%s (Y zoom %d%%)", oimage->title, yzoom);
  65.   }
  66.   else if (!yzoom) {
  67.     if (verbose)
  68.       printf("  Zooming image X axis by %d%%...", xzoom);
  69.     sprintf(buf, "%s (X zoom %d%%)", oimage->title, xzoom);
  70.   }
  71.   else if (xzoom == yzoom) {
  72.     if (verbose)
  73.       printf("  Zooming image by %d%%...", xzoom);
  74.     sprintf(buf, "%s (%d%% zoom)", oimage->title, xzoom);
  75.   }
  76.   else {
  77.     if (verbose)
  78.       printf("  Zooming image X axis by %d%% and Y axis by %d%%...",
  79.          xzoom, yzoom);
  80.     sprintf(buf, "%s (X zoom %d%% Y zoom %d%%)", oimage->title,
  81.         xzoom, yzoom);
  82.   }
  83.   if (verbose)
  84.     fflush(stdout);
  85.  
  86.   xindex= buildIndex(oimage->width, xzoom, &xwidth);
  87.   yindex= buildIndex(oimage->height, yzoom, &ywidth);
  88.  
  89.   switch (oimage->type) {
  90.   case IBITMAP:
  91.     image= newBitImage(xwidth, ywidth);
  92.     for (x= 0; x < oimage->rgb.used; x++) {
  93.       *(image->rgb.red + x)= *(oimage->rgb.red + x);
  94.       *(image->rgb.green + x)= *(oimage->rgb.green + x);
  95.       *(image->rgb.blue + x)= *(oimage->rgb.blue + x);
  96.     }
  97.     image->rgb.used= oimage->rgb.used;
  98.     destline= image->data;
  99.     destlinelen= (xwidth / 8) + (xwidth % 8 ? 1 : 0);
  100.     srcline= oimage->data;
  101.     srclinelen= (oimage->width / 8) + (oimage->width % 8 ? 1 : 0);
  102.     for (y= 0, ysrc= *(yindex + y); y < ywidth; y++) {
  103.       while (ysrc != *(yindex + y)) {
  104.     ysrc++;
  105.     srcline += srclinelen;
  106.       }
  107.       srcptr= srcline;
  108.       destptr= destline;
  109.       srcmask= 0x80;
  110.       destmask= 0x80;
  111.       bit= srcmask & *srcptr;
  112.       for (x= 0, xsrc= *(xindex + x); x < xwidth; x++) {
  113.     if (xsrc != *(xindex + x)) {
  114.       do {
  115.         xsrc++;
  116.         if (!(srcmask >>= 1)) {
  117.           srcmask= 0x80;
  118.           srcptr++;
  119.         }
  120.       } while (xsrc != *(xindex + x));
  121.       bit= srcmask & *srcptr;
  122.     }
  123.     if (bit)
  124.       *destptr |= destmask;
  125.     if (!(destmask >>= 1)) {
  126.       destmask= 0x80;
  127.       destptr++;
  128.     }
  129.       }
  130.       destline += destlinelen;
  131.     }
  132.     break;
  133.  
  134.   case IRGB:
  135.     image= newRGBImage(xwidth, ywidth, oimage->depth);
  136.     for (x= 0; x < oimage->rgb.used; x++) {
  137.       *(image->rgb.red + x)= *(oimage->rgb.red + x);
  138.       *(image->rgb.green + x)= *(oimage->rgb.green + x);
  139.       *(image->rgb.blue + x)= *(oimage->rgb.blue + x);
  140.     }
  141.     image->rgb.used= oimage->rgb.used;
  142.     /* FALLTHRU */
  143.  
  144.   case ITRUE:
  145.     if (!RGBP(oimage))
  146.       image= newTrueImage(xwidth, ywidth);
  147.     pixlen= oimage->pixlen;
  148.     destptr= image->data;
  149.     srcline= oimage->data;
  150.     srclinelen= oimage->width * pixlen;
  151.     for (y= 0, ysrc= *(yindex + y); y < ywidth; y++) {
  152.       while (ysrc != *(yindex + y)) {
  153.     ysrc++;
  154.     srcline += srclinelen;
  155.       }
  156.  
  157.       srcptr= srcline;
  158.       value= memToVal(srcptr, pixlen);
  159.       for (x= 0, xsrc= *(xindex + x); x < xwidth; x++) {
  160.     if (xsrc != *(xindex + x)) {
  161.       do {
  162.         xsrc++;
  163.         srcptr += image->pixlen;
  164.       } while (xsrc != *(xindex + x));
  165.       value= memToVal(srcptr, pixlen);
  166.     }
  167.     valToMem(value, destptr, pixlen);
  168.     destptr += pixlen;
  169.       }
  170.     }
  171.     break;
  172.   }
  173.  
  174.   image->title= dupString(buf);
  175.   lfree((byte *)xindex);
  176.   lfree((byte *)yindex);
  177.   if (verbose)
  178.     printf("done\n");
  179.   return(image);
  180. }
  181.