home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / indispensabili / utility / apdf / xpdf-0.80 / xpdf / agfx.h < prev    next >
C/C++ Source or Header  |  1999-06-10  |  12KB  |  631 lines

  1. //========================================================================
  2. //
  3. // AGfx.h
  4. //
  5. // Copyright 1999 Emmanuel Lesueur
  6. //
  7. //========================================================================
  8.  
  9. #ifndef AGFX_H
  10. #define AGFX_H
  11.  
  12. #include "poly.h"
  13.  
  14. #ifdef __PPC__
  15. #   include <string.h>
  16. #   include "AComm.h"
  17. #   include "AGfxcomm.h"
  18.     typedef unsigned long ULONG;
  19.     typedef unsigned char UBYTE;
  20. #else
  21. #   define Object ZZObject
  22. #   include <graphics/layers.h>
  23. #   include <hardware/blit.h>
  24. #   include <cybergraphics/cybergraphics.h>
  25. #   include <proto/graphics.h>
  26. #   include <proto/cybergraphics.h>
  27. #   undef Object
  28.     struct DocData;
  29.     extern "C" void initgfx(DocData**,struct RastPort**,short*,short*);
  30.     extern "C" void initarea(struct RastPort*,int);
  31.     extern "C" void areapoly(struct RastPort*,int,short*,short,short);
  32.     extern "C" void areaend(struct RastPort*);
  33.     extern "C" void reset_pens(DocData*);
  34.     extern "C" void add_pens(DocData*,ULONG*,int);
  35.     extern "C" void add_pen(DocData*,ULONG,ULONG,ULONG);
  36.     extern "C" ULONG get_pen(DocData*,short);
  37.     extern "C" void clear_clip(DocData*);
  38.     extern "C" void init_clip(DocData*);
  39.     extern "C" void rect_clip(DocData*,short,short,short,short);
  40.     extern "C" void poly_clip(DocData*,int,short*);
  41.     extern "C" void install_clip(DocData*);
  42.     extern "C" void openfont(DocData*,int,const char*,short,short);
  43.     extern "C" void closefont(DocData*,int);
  44.     extern "C" void setfont(DocData*,int);
  45. #endif
  46.  
  47. typedef Vertex<short> XPoint;
  48.  
  49. struct Color {
  50.     UBYTE r, g, b;
  51. };
  52.  
  53. struct Color32 {
  54.     ULONG r, g, b;
  55. };
  56.  
  57. struct ColorEntry {
  58.     int index;
  59.     Color32 color;
  60. };
  61.  
  62.  
  63. class AGfx {
  64. #ifdef __PPC__
  65. #   ifdef USE_GFX_PIPE
  66.     void check_size(int n) {
  67.     if(ptr+n>end)
  68.         flush(false);
  69.     }
  70.     bool fits(int n) {
  71.     return ptr+n<=end;
  72.     }
  73.     short* get_ptr() {
  74.     return ptr;
  75.     }
  76.     void set_ptr(short* p) {
  77.     pipe->writep=ptr=p;
  78.     }
  79. #   else
  80.     void check_size(int n) {
  81.     if(sz<n)
  82.         flush(false);
  83.     }
  84.     bool fits(int n) {
  85.     return sz>=n;
  86.     }
  87.     short* get_ptr() {
  88.     return ptr;
  89.     }
  90.     void set_ptr(short* p) {
  91.     sz-=p-ptr;
  92.     ptr=p;
  93.     }
  94. #   endif
  95. #endif
  96.   public:
  97.  
  98.     typedef unsigned char pixel_color;
  99.  
  100.     class ColorTable {
  101.     friend class AGfx;
  102.       public:
  103.     explicit ColorTable(int);
  104.     ~ColorTable();
  105.     ColorEntry* data() const { return entries; }
  106.       private:
  107.     ColorTable(const ColorTable&);
  108.     ColorTable& operator = (const ColorTable&);
  109.     ColorEntry* entries;
  110.     int num;
  111.     };
  112.  
  113.     class ColorTablePtr {
  114.       public:
  115.     explicit ColorTablePtr(ColorTable* t=NULL) : table(t) {}
  116.     ColorTablePtr(ColorTablePtr& t) : table(t.table) { t.table=NULL; }
  117.     ~ColorTablePtr() { delete table; }
  118.     ColorTablePtr& operator = (ColorTablePtr& t) {
  119.         delete table;
  120.         table=t.table;
  121.         t.table=NULL;
  122.         return *this;
  123.     }
  124.     ColorTablePtr& operator = (ColorTable* t) {
  125.         delete table;
  126.         table=t;
  127.         return *this;
  128.     }
  129.     ColorTable& operator * () const { return *table; }
  130.     ColorTable* operator -> () const { return table; }
  131.       private:
  132.     ColorTable* table;
  133.     };
  134.  
  135.     class Image {
  136.     friend class AGfx;
  137.       public:
  138.     Image(short w,short h);
  139.     ~Image();
  140.  
  141.     void put(short x,short y,pixel_color c) {
  142.         im[y*bytes_per_row+x]=c;
  143.     }
  144.     pixel_color* data() const { return im; }
  145.  
  146.       private:
  147.     Image(const Image&);
  148.     Image& operator = (const Image&);
  149.  
  150.     pixel_color* im;
  151.     short width;
  152.     short height;
  153.     short bytes_per_row;
  154.     };
  155.  
  156.     class ImagePtr {
  157.       public:
  158.     explicit ImagePtr(Image* q=NULL) : p(q) {}
  159.     ImagePtr(ImagePtr& x) : p(x.p) { x.p=NULL; }
  160.     ~ImagePtr() { delete p; }
  161.     ImagePtr& operator = (ImagePtr& x) {
  162.         delete p;
  163.         p=x.p;
  164.         x.p=NULL;
  165.         return *this;
  166.     }
  167.     ImagePtr& operator = (Image* q) {
  168.         delete p;
  169.         p=q;
  170.         return *this;
  171.     }
  172.     Image& operator * () const { return *p; }
  173.     Image* operator -> () const { return p; }
  174.     Image* get() const { return p; }
  175.     operator bool () const { return p!=NULL; }
  176.       private:
  177.     Image* p;
  178.     };
  179.  
  180.     class Image24 {
  181.     friend class AGfx;
  182.       public:
  183.     Image24(short w,short h);
  184.     ~Image24();
  185.  
  186.     void put(short x,short y,const Color& c) {
  187.         im[y*width+x]=c;
  188.     }
  189.     Color* data() const { return im; }
  190.  
  191.       private:
  192.     Image24(const Image24&);
  193.     Image24& operator = (const Image24&);
  194.  
  195.     Color* im;
  196.     short width;
  197.     short height;
  198.     };
  199.  
  200.     class Image24Ptr {
  201.       public:
  202.     explicit Image24Ptr(Image24* q=NULL) : p(q) {}
  203.     Image24Ptr(Image24Ptr& x) : p(x.p) { x.p=NULL; }
  204.     ~Image24Ptr() { delete p; }
  205.     Image24Ptr& operator = (Image24Ptr& x) {
  206.         delete p;
  207.         p=x.p;
  208.         x.p=NULL;
  209.         return *this;
  210.     }
  211.     Image24Ptr& operator = (Image24* q) {
  212.         delete p;
  213.         p=q;
  214.         return *this;
  215.     }
  216.     Image24& operator * () const { return *p; }
  217.     Image24* operator -> () const { return p; }
  218.     Image24* get() const { return p; }
  219.     operator bool () const { return p!=NULL; }
  220.       private:
  221.     Image24* p;
  222.     };
  223.  
  224.  
  225. #ifdef __PPC__
  226.     AGfx(PPCPort* port1);
  227.     ~AGfx();
  228. #else
  229.     AGfx() : rp(NULL),x0(0),y0(0),dat(NULL),in_page_draw(false) {
  230.     initgfx(&dat,&rp,&x0,&y0);
  231.     }
  232. #endif
  233.  
  234.     // those would be better, but SAS/C++ and gcc have
  235.     // troubles with A(A&) constructors...
  236.     /*ImagePtr allocate_image(short width,short height) {
  237.     return ImagePtr(new Image(width,height));
  238.     }
  239.     Image24Ptr allocate_image24(short width,short height) {
  240.     return Image24Ptr(new Image24(width,height));
  241.     }
  242.     ColorTablePtr allocate_color_table(int n) {
  243.     return ColorTablePtr(new ColorTable(n));
  244.     }*/
  245.     Image* allocate_image(short width,short height) {
  246.     return new Image(width,height);
  247.     }
  248.     Image24* allocate_image24(short width,short height) {
  249.     return new Image24(width,height);
  250.     }
  251.     ColorTable* allocate_color_table(int n) {
  252.     return new ColorTable(n);
  253.     }
  254.  
  255. #if defined(__PPC__) && defined(USE_GFX_PIPE)
  256.     void init();
  257. #else
  258.     void init() {}
  259. #endif
  260.  
  261. #ifdef __PPC__
  262.     void flush(bool complete=true);
  263. #else
  264.     void flush(bool complete=true) {}
  265. #endif
  266.  
  267.     void setapen(short n) {
  268. #ifdef __PPC__
  269.     check_size(2);
  270.     short* p=get_ptr();
  271.     *p++=AGFX_SETAPEN;
  272.     *p++=n;
  273.     set_ptr(p);
  274. #else
  275.     SetAPen(rp,get_pen(dat,n));
  276. #endif
  277.     }
  278.  
  279.     void openfont(int id,const char* name,short size,short style) {
  280. #ifdef __PPC__
  281.     size_t sz=(strlen(name)+2)/2;
  282.     check_size(sz+6);
  283.     if(fits(sz+6)) {
  284.         short* p=get_ptr();
  285.         *p++=AGFX_OPENFONT;
  286.         *reinterpret_cast<int*>(p)=id;
  287.         p+=2;
  288.         *p++=sz;
  289.         strcpy((char*)p,name);
  290.         p+=sz;
  291.         *p++=size;
  292.         *p++=style;
  293.         set_ptr(p);
  294.     }
  295. #else
  296.     ::openfont(dat,id,name,size,style);
  297. #endif
  298.     }
  299.  
  300.     void closefont(int id) {
  301.     if(in_page_draw) {
  302. #ifdef __PPC__
  303.         check_size(3);
  304.         short* p=get_ptr();
  305.         *p++=AGFX_CLOSEFONT;
  306.         *reinterpret_cast<int*>(p)=id;
  307.         p+=2;
  308.         set_ptr(p);
  309. #else
  310.         ::closefont(dat,id);
  311. #endif
  312.     }
  313.     }
  314.  
  315.     void setfont(int id) {
  316. #ifdef __PPC__
  317.     check_size(3);
  318.     short* p=get_ptr();
  319.     *p++=AGFX_SETFONT;
  320.     *reinterpret_cast<int*>(p)=id;
  321.     p+=2;
  322.     set_ptr(p);
  323. #else
  324.     ::setfont(dat,id);
  325. #endif
  326.     }
  327.  
  328.     void set_line_ptrn(short mask,short start) {
  329. #ifdef __PPC__
  330.     check_size(3);
  331.     short* p=get_ptr();
  332.     *p++=AGFX_LINEPTRN;
  333.     *p++=mask;
  334.     *p++=start;
  335.     set_ptr(p);
  336. #else
  337.     rp->LinePtrn=mask;
  338.     rp->linpatcnt=start;
  339. #endif
  340.     }
  341.  
  342. #ifdef __PPC__
  343.     void polydraw(const Polygon<short>& q) {
  344.     int n=q.size();
  345.     check_size(n*2+2);
  346.     if(fits(n*2+2)) {
  347.         short* p=get_ptr();
  348.         *p++=AGFX_POLYDRAW;
  349.         *p++=n;
  350.         memcpy(p,q.begin(),n*4);
  351.         p+=n*2;
  352.         set_ptr(p);
  353.     }
  354.     }
  355. #else
  356.     void polydraw(const Polygon<short>&);
  357. #endif
  358.  
  359.     void addchar(short x,short y,char c) {
  360. #ifdef __PPC__
  361.     check_size(4);
  362.     short* p=get_ptr();
  363.     *p++=AGFX_ADDCHAR;
  364.     *p++=x;
  365.     *p++=y;
  366.     *p++=c;
  367.     set_ptr(p);
  368. #else
  369.     Move(rp,x-x0,y-y0);
  370.     Text(rp,&c,1);
  371. #endif
  372.     }
  373.  
  374.     void rectfill(short x1,short y1,short x2,short y2) {
  375. #ifdef __PPC__
  376.     check_size(5);
  377.     short* p=get_ptr();
  378.     *p++=AGFX_RECTFILL;
  379.     *p++=x1;
  380.     *p++=y1;
  381.     *p++=x2;
  382.     *p++=y2;
  383.     set_ptr(p);
  384. #else
  385.     RectFill(rp,x1-x0,y1-y0,x2-x0,y2-y0);
  386. #endif
  387.     }
  388.  
  389. #ifdef __PPC__
  390.     void get_image(Image&,short x,short y);
  391. #else
  392.     void get_image(Image& im,short x,short y) {
  393.     x-=x0;
  394.     y-=y0;
  395.     struct BitMap* bm;
  396.     if(bm=AllocBitMap((im.width+15)&~15,1,8,BMF_MINPLANES,rp->BitMap)) {
  397.         struct RastPort rp2;
  398.         rp2=*rp;
  399.         rp2.Layer=NULL;
  400.         rp2.BitMap=bm;
  401.         ReadPixelArray8(rp,x,y,x+im.width-1,y+im.height-1,im.data(),&rp2);
  402.         FreeBitMap(bm);
  403.     }
  404.     }
  405. #endif
  406.  
  407. #ifdef __PPC__
  408.     void get_image(Image24&,short x,short y);
  409. #else
  410.     void get_image(Image24& im,short x,short y) {
  411.     ReadPixelArray(im.data(),0,0,im.width*3,rp,x-x0,y-y0,im.width,im.height,RECTFMT_RGB);
  412.     }
  413. #endif
  414.  
  415. #ifdef __PPC__
  416.     void put_image(const Image&,short x,short y);
  417. #else
  418.     void put_image(const Image& im,short x,short y) {
  419.     x-=x0;
  420.     y-=y0;
  421.     struct BitMap* bm;
  422.     if(bm=AllocBitMap((im.width+15)&~15,1,8,BMF_MINPLANES,rp->BitMap)) {
  423.         struct RastPort rp2;
  424.         rp2=*rp;
  425.         rp2.Layer=NULL;
  426.         rp2.BitMap=bm;
  427.         WritePixelArray8(rp,x,y,x+im.width-1,y+im.height-1,im.data(),&rp2);
  428.         WaitBlit();
  429.         FreeBitMap(bm);
  430.     }
  431.     }
  432. #endif
  433.  
  434. #ifdef __PPC__
  435.     void put_image(const Image24&,short x,short y);
  436. #else
  437.     void put_image(const Image24& im,short x,short y) {
  438.     WritePixelArray(im.data(),0,0,im.width*3,rp,x-x0,y-y0,im.width,im.height,RECTFMT_RGB);
  439.     }
  440. #endif
  441.  
  442.  
  443.     friend class AreaDrawer {
  444.       public:
  445.     AreaDrawer(AGfx& g) : gfx(g),n(0) {}
  446.     ~AreaDrawer();
  447.     void add(const Polygon<short>& p);
  448.       private:
  449.     AreaDrawer(const AreaDrawer&);
  450.     AreaDrawer& operator = (const AreaDrawer&);
  451.     AGfx& gfx;
  452.     int n;
  453.     PArea<short> area;
  454.     };
  455.  
  456.  
  457.     void clearclip() {
  458. #ifdef __PPC__
  459.     check_size(1);
  460.     short* p=get_ptr();
  461.     *p++=AGFX_INITCLIP;
  462.     set_ptr(p);
  463. #else
  464.     init_clip(dat);
  465. #endif
  466.     }
  467.  
  468.     void rectclip(short x1,short y1,short x2,short y2) {
  469. #ifdef __PPC__
  470.     check_size(5);
  471.     short* p=get_ptr();
  472.     *p++=AGFX_RECTCLIP;
  473.     *p++=x1;
  474.     *p++=y1;
  475.     *p++=x2;
  476.     *p++=y2;
  477.     set_ptr(p);
  478. #else
  479.     rect_clip(dat,x1,y1,x2,y2);
  480. #endif
  481.     }
  482.     void polyclip(const Polygon<short>& q) {
  483.     int n=q.size();
  484. #ifdef __PPC__
  485.     check_size(n*2+2);
  486.     if(fits(n*2+2)) {
  487.         short* p=get_ptr();
  488.         *p++=AGFX_POLYCLIP;
  489.         *p++=n;
  490.         memcpy(p,q.begin(),n*4);
  491.         p+=n*2;
  492.         set_ptr(p);
  493.     }
  494. #else
  495.     poly_clip(dat,n,(short*)q.begin());
  496. #endif
  497.     }
  498.  
  499.     void installclip() {
  500. #ifdef __PPC__
  501.     check_size(1);
  502.     short* p=get_ptr();
  503.     *p++=AGFX_INSTALLCLIP;
  504.     set_ptr(p);
  505. #else
  506.     install_clip(dat);
  507. #endif
  508.     }
  509.  
  510.     void start_page() {
  511. #ifdef __PPC__
  512.     check_size(1);
  513.     short* p=get_ptr();
  514.     *p++=AGFX_STARTPAGE;
  515.     set_ptr(p);
  516. #else
  517.     reset_pens(dat);
  518.     SetRast(rp,get_pen(dat,1));
  519.     SetAPen(rp,get_pen(dat,0));
  520. #endif
  521.     in_page_draw=true;
  522.     }
  523.  
  524.     void end_page() {
  525.     in_page_draw=false;
  526. #if 0
  527.     check_size(1);
  528.     short* p=get_ptr();
  529.     *p++=AGFX_ENDPAGE;
  530.     set_ptr(p);
  531. #endif
  532.     }
  533.  
  534.     void color(ULONG r,ULONG g,ULONG b) {
  535. #ifdef __PPC__
  536.     check_size(7);
  537.     short* p=get_ptr();
  538.     *p++=AGFX_PENCOLOR;
  539.     *reinterpret_cast<ULONG*>(p)=r;
  540.     p+=2;
  541.     *reinterpret_cast<ULONG*>(p)=g;
  542.     p+=2;
  543.     *reinterpret_cast<ULONG*>(p)=b;
  544.     p+=2;
  545.     set_ptr(p);
  546. #else
  547.     add_pen(dat,r,g,b);
  548. #endif
  549.     }
  550.  
  551. #ifdef __PPC__
  552.     void get_colors(ColorTable&,int);
  553. #else
  554.     void get_colors(ColorTable& ct,int num) {
  555.     add_pens(dat,(ULONG*)ct.entries,num);
  556.     }
  557. #endif
  558.  
  559.   private:
  560.     AGfx(const AGfx&);
  561.     AGfx& operator = (const AGfx&);
  562.     bool in_page_draw;
  563. #ifdef __PPC__
  564. #   ifdef USE_GFX_PIPE
  565.     volatile GfxPipe* pipe;
  566.     volatile short* buf;
  567.     volatile short* end;
  568.     volatile short* ptr;
  569. #   else
  570.     short* buf;
  571.     short* ptr;
  572.     short* buf2;
  573.     int sz;
  574. #   endif
  575.     PPCPort* port;
  576.     PPCPort* reply_port;
  577.     PPCMsg* msg;
  578.     bool msg_sent;
  579. #else
  580.     struct RastPort* rp;
  581.     short x0;
  582.     short y0;
  583.     DocData* dat;
  584. #endif
  585.  
  586.     void initarea(int n) {
  587. #ifdef __PPC__
  588.     check_size(2);
  589.     short* p=get_ptr();
  590.     *p++=AGFX_INITAREA;
  591.     *p++=n;
  592.     set_ptr(p);
  593. #else
  594.     ::initarea(rp,n);
  595. #endif
  596.     }
  597.  
  598.     void areapoly(const Polygon<short>& q) {
  599.     int n=q.size();
  600. #ifdef __PPC__
  601.     check_size(n*2+2);
  602.     if(fits(n*2+2)) {
  603.         short* p=get_ptr();
  604.         *p++=AGFX_AREAPOLY;
  605.         *p++=n;
  606.         memcpy(p,q.begin(),n*4);
  607.         p+=n*2;
  608.         set_ptr(p);
  609.     }
  610. #else
  611.     ::areapoly(rp,n,(short*)q.begin(),0,0);
  612. #endif
  613.     }
  614.  
  615.     void areaend() {
  616. #ifdef __PPC__
  617.     check_size(1);
  618.     short* p=get_ptr();
  619.     *p++=AGFX_AREAEND;
  620.     set_ptr(p);
  621. #else
  622.     ::areaend(rp);
  623. #endif
  624.     }
  625.  
  626.  
  627. };
  628.  
  629. #endif
  630.  
  631.