home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / CreativeComputers.iso / shareware / text / dvi_3.62 / source / dvisrc.lha / dviimg.c < prev    next >
C/C++ Source or Header  |  1993-10-26  |  5KB  |  234 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdarg.h>
  4. #include "dvi.h"
  5. #include "dvidvi.h"
  6. #include "dviframe.h"
  7.  
  8. #define Fixed unsigned long
  9.  
  10. static Fixed FixRatio(int numer, int denom)
  11. {
  12.     Fixed result;
  13.     result = (Fixed)(numer/denom)<<16l;
  14.     result |= ((Fixed)(numer%denom)<<16l)/denom;
  15.     return result;
  16. }
  17.  
  18. #ifndef ATARI_ST
  19. static Fixed fixumul(Fixed x, Fixed y)
  20. {
  21.     register Fixed r, xi, yi, xf, yf;
  22.  
  23.     xi = x>>16;
  24.     yi = y>>16;
  25.     xf = x & 0xFFFF;
  26.     yf = y & 0xFFFF;
  27.  
  28.     r = (unsigned long)(xi*yi)<<16l;
  29.     r += (unsigned long)xf*(unsigned long)yi;
  30.     r += (unsigned long)yf*(unsigned long)xi;
  31.     r += ((unsigned long)xf*(unsigned long)yf)>>16l;
  32.  
  33.     return r;
  34. }
  35. #else
  36. extern Fixed fixumul(Fixed,Fixed);
  37. #endif
  38.  
  39. static void conv_line(Fixed *pixel, char *line, Fixed cw, int w_neu, Fixed conv)
  40. {
  41.     int     i;
  42.     register Fixed  fpos, ffrac;
  43.  
  44.     register char   *fptr;
  45.     register int    fmask, fpix;
  46.  
  47.     fpos = 0l;
  48.  
  49.     fpix  = 0;
  50.     ffrac = 0x10000l;
  51.     fmask = 128;
  52.     fptr  = line;
  53.  
  54.     for (i=0; i<w_neu; i++, pixel++)
  55.     {
  56.     register Fixed  lpos  = fpos+cw;
  57.     register Fixed  lfrac = lpos & 0xFFFFl;
  58.     register int    lpix  = (int)(lpos>>16);
  59.  
  60.     if (fpix==lpix)
  61.     {
  62.         if (*fptr & fmask)
  63.         *pixel += fixumul(lfrac+ffrac-0x10000l,conv);
  64.     }
  65.     else
  66.     {
  67.         register int j;
  68.  
  69.         if (*fptr & fmask) *pixel += fixumul(ffrac,conv);
  70.         if ( (fmask >>= 1)==0 ) fmask=128, fptr++;
  71.  
  72.         for (j=fpix+1; j<lpix; j++)
  73.         {
  74.         if (*fptr & fmask) *pixel += conv;
  75.         if ( (fmask >>= 1)==0 ) fmask=128, fptr++;
  76.         }
  77.         if (*fptr & fmask) *pixel += fixumul(lfrac,conv);
  78.     }
  79.  
  80.     fpix  = lpix;
  81.     fpos  = lpos;
  82.     ffrac = 0x10000l-lfrac;
  83.     }
  84. }
  85.  
  86. char line[10000];
  87. Fixed pixel[10000];
  88.  
  89. static void conv_pic(long screen, int offset, int height, int x, int y,
  90.           IMG_FILE *img, int w_alt, int w_neu, int h_alt,
  91.           int h_neu, double dens)
  92. {
  93.     int     i, xstart, ystart, xmask;
  94.     Fixed   ch, cw, k, fpos, ffrac, *p;
  95.     long    ptr;
  96.     register int fpix, mask;
  97.  
  98.     ch    = FixRatio(h_alt,h_neu);
  99.     cw    = FixRatio(w_alt,w_neu);
  100.     k     = fixumul(ch,cw);
  101.     fpos  = 0l;
  102.     fpix  = 0;
  103.     ffrac = 0x10000l;
  104.  
  105.     k = fixumul(k,FixRatio((int)(1000.0*(1.0-dens)),1000));
  106.     xmask = 128>>(x%8);
  107.     screen += (long)y*(long)offset;
  108.  
  109.     if (op.landscape)
  110.     {
  111.     if (y<0) ystart = -y, y=0;
  112.     else ystart=0;
  113.     x /= 8;
  114.     if (x<0) return;
  115.     }
  116.     else
  117.     {
  118.     if (x<0) xstart = -x, x=0;
  119.     else xstart=0;
  120.     if (y>=height) return;
  121.     }
  122.  
  123.     if (img_read(line,img)==NULL) return;
  124.  
  125.     for (i=0; i<h_neu; i++)
  126.     {
  127.     Fixed  lpos  = fpos+ch;
  128.     Fixed  lfrac = lpos & 0xFFFFl;
  129.     register int lpix  = (int)(lpos>>16);
  130.     int j;
  131.  
  132.     memset((char*)pixel,0,sizeof(Fixed)*w_neu);
  133.  
  134.     if (fpix==lpix)
  135.     {
  136.         conv_line(pixel,line,cw,w_neu,lfrac+ffrac-0x10000l);
  137.     }
  138.     else
  139.     {
  140.         register int j;
  141.  
  142.         conv_line(pixel,line,cw,w_neu,ffrac);
  143.         img_read(line,img);         
  144.         for (j=fpix+1; j<lpix; j++) 
  145.         {
  146.         conv_line(pixel,line,cw,w_neu,0x10000l);
  147.         img_read(line,img);
  148.         }
  149.         conv_line(pixel,line,cw,w_neu,lfrac);
  150.     }
  151.  
  152.     if (op.landscape)
  153.     {
  154.         ptr = screen + x;
  155.         p = pixel + ystart;
  156.         if (x<offset)
  157.         {
  158.         j=w_neu-ystart;
  159.         if (y+j>=height) j=height-y;
  160.         for (; j--; ptr += offset)
  161.             if (*p++>k) frame_or(ptr,xmask);
  162.         }
  163.         if ( (xmask <<=1 )>128 )
  164.         {
  165.         if (--x < 0) return;
  166.         xmask=1;
  167.         }
  168.     }
  169.     else
  170.     {
  171.         ptr  = screen+x/8;
  172.         mask = xmask;
  173.         p = pixel+xstart;
  174.         if (y>=0)
  175.         for (j=w_neu-xstart; j-- > 0;)
  176.         {
  177.             if (*p++>k) frame_or(ptr,mask);
  178.             if ( (mask>>=1)==0 )
  179.             {
  180.             mask=128; 
  181.             if (++ptr-screen>=offset) break;
  182.             }
  183.         }
  184.         screen += offset;
  185.         if (++y >= height) break;
  186.     }
  187.     fpix  = lpix;
  188.     fpos  = lpos;
  189.     ffrac = 0x10000l-lfrac;
  190.     }
  191. }
  192.  
  193. int read_img(char *name, int x, int y, double dens)
  194. {
  195.     IMG_FILE *img;
  196.     int w,h,bytes,pw,ph,hpixel,wpixel;
  197.  
  198.     img = img_open(name,&w,&h,&bytes,&pw,&ph,op.img_path);
  199.     if (img==NULL) return 1;
  200.  
  201.     hpixel = (int)((double)op.vres*(double)ph*(double)h / 25400.0);
  202.     wpixel = (int)((double)op.hres*(double)pw*(double)w / 25400.0);
  203.  
  204.     if (!op.show_img)
  205.     {
  206.     if (op.landscape)
  207.     {
  208.         draw_rule(x,y,1,wpixel);
  209.         draw_rule(x,y,hpixel,1);
  210.         draw_rule(x,y+wpixel,hpixel,1);
  211.         draw_rule(x+hpixel,y,1,wpixel);
  212.     }
  213.     else
  214.     {
  215.         draw_rule(x,y-hpixel,wpixel,1);
  216.         draw_rule(x,y,wpixel,1);
  217.         draw_rule(x,y-hpixel,1,hpixel);
  218.         draw_rule(x+wpixel,y-hpixel,1,hpixel);
  219.     }
  220.     }
  221.     else
  222.     {
  223.     if (op.landscape)
  224.         conv_pic(0l,frame_width,frame_height,
  225.              x+hpixel,y,img,w,wpixel,h,hpixel,dens);
  226.     else
  227.         conv_pic(0l,frame_width,frame_height,
  228.              x,y-hpixel,img,w,wpixel,h,hpixel,dens);
  229.     }
  230.     img_close(img);
  231.     return 0;
  232. }
  233.  
  234.