home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xldimage / part02 / zoom.c < prev   
Encoding:
C/C++ Source or Header  |  1989-11-13  |  4.0 KB  |  167 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. { char          buf[BUFSIZ];
  43.   Image        *image;
  44.   unsigned int *xindex, *yindex;
  45.   unsigned int  xwidth, ywidth;
  46.   unsigned int  x, y, xsrc, ysrc;
  47.   unsigned int  pixlen;
  48.   unsigned int  srclinelen;
  49.   byte         *srcline, *srcptr;
  50.   byte         *destptr;
  51.   byte          srcmask, destmask, bit;
  52.   Pixel         value;
  53.  
  54.   goodImage(oimage, "zoom");
  55.  
  56.   if (!xzoom && !yzoom) /* stupid user */
  57.     return(NULL);
  58.  
  59.   if (verbose) {
  60.     if (!xzoom) {
  61.       printf("  Zooming image Y axis by %d%%...", yzoom);
  62.       sprintf(buf, "%s (Y zoom %d%%)", oimage->title, yzoom);
  63.     }
  64.     else if (!yzoom) {
  65.       printf("  Zooming image X axis by %d%%...", xzoom);
  66.       sprintf(buf, "%s (X zoom %d%%)", oimage->title, xzoom);
  67.     }
  68.     else if (xzoom == yzoom) {
  69.       printf("  Zooming image by %d%%...", xzoom);
  70.       sprintf(buf, "%s (%d%% zoom)", oimage->title, xzoom);
  71.     }
  72.     else {
  73.       printf("  Zooming image X axis by %d%% and Y axix by %d%%...",
  74.          xzoom, yzoom);
  75.       sprintf(buf, "%s (X zoom %d%% Y zoom %d%%)", oimage->title,
  76.           xzoom, yzoom);
  77.     }
  78.     fflush(stdout);
  79.   }
  80.  
  81.   xindex= buildIndex(oimage->width, xzoom, &xwidth);
  82.   yindex= buildIndex(oimage->height, yzoom, &ywidth);
  83.  
  84.   switch (oimage->type) {
  85.   case IBITMAP:
  86.     image= newBitImage(xwidth, ywidth);
  87.     for (x= 0; x < oimage->rgb.used; x++) {
  88.       *(image->rgb.red + x)= *(oimage->rgb.red + x);
  89.       *(image->rgb.green + x)= *(oimage->rgb.green + x);
  90.       *(image->rgb.blue + x)= *(oimage->rgb.blue + x);
  91.     }
  92.     image->rgb.used= oimage->rgb.used;
  93.     destptr= image->data;
  94.     srcline= oimage->data;
  95.     srclinelen= (oimage->width / 8) + (oimage->width % 8 ? 1 : 0);
  96.     for (y= 0, ysrc= *(yindex + y); y < ywidth; y++) {
  97.       while (ysrc != *(yindex + y)) {
  98.     ysrc++;
  99.     srcline += srclinelen;
  100.       }
  101.       srcptr= srcline;
  102.       srcmask= 0x80;
  103.       destmask= 0x80;
  104.       bit= srcmask & *srcptr;
  105.       for (x= 0, xsrc= *(xindex + x); x < xwidth; x++) {
  106.     if (xsrc != *(xindex + x)) {
  107.       do {
  108.         xsrc++;
  109.         if (!(srcmask >>= 1)) {
  110.           srcmask= 0x80;
  111.           srcptr++;
  112.         }
  113.       } while (xsrc != *(xindex + x));
  114.       bit= srcmask & *srcptr;
  115.     }
  116.     if (bit)
  117.       *destptr |= destmask;
  118.     if (!(destmask >>= 1)) {
  119.       destmask= 0x80;
  120.       destptr++;
  121.     }
  122.       }
  123.     }
  124.     break;
  125.  
  126.   case IRGB:
  127.     image= newRGBImage(xwidth, ywidth, oimage->depth);
  128.     for (x= 0; x < oimage->rgb.used; x++) {
  129.       *(image->rgb.red + x)= *(oimage->rgb.red + x);
  130.       *(image->rgb.green + x)= *(oimage->rgb.green + x);
  131.       *(image->rgb.blue + x)= *(oimage->rgb.blue + x);
  132.     }
  133.     image->rgb.used= oimage->rgb.used;
  134.     pixlen= oimage->pixlen;
  135.     destptr= image->data;
  136.     srcline= oimage->data;
  137.     srclinelen= oimage->width * pixlen;
  138.     for (y= 0, ysrc= *(yindex + y); y < ywidth; y++) {
  139.       while (ysrc != *(yindex + y)) {
  140.     ysrc++;
  141.     srcline += srclinelen;
  142.       }
  143.  
  144.       srcptr= srcline;
  145.       value= memToVal(srcptr, pixlen);
  146.       for (x= 0, xsrc= *(xindex + x); x < xwidth; x++) {
  147.     if (xsrc != *(xindex + x)) {
  148.       do {
  149.         xsrc++;
  150.         srcptr += image->pixlen;
  151.       } while (xsrc != *(xindex + x));
  152.       value= memToVal(srcptr, pixlen);
  153.     }
  154.     valToMem(value, destptr++, pixlen);
  155.       }
  156.     }
  157.     break;
  158.   }
  159.  
  160.   image->title= dupString(buf);
  161.   lfree(xindex);
  162.   lfree(yindex);
  163.   if (verbose)
  164.     printf("done\n");
  165.   return(image);
  166. }
  167.