Deutsch   English   Français   Italiano  
<d0fcbe9e7b29ce6f9a0604058475b0aa9a23d5cb.camel@gmail.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: wij <wyniijj5@gmail.com>
Newsgroups: comp.lang.c
Subject: Can 'graphics' be a file descriptor?
Date: Wed, 01 Jan 2025 15:48:32 +0800
Organization: A noiseless patient Spider
Lines: 206
Message-ID: <d0fcbe9e7b29ce6f9a0604058475b0aa9a23d5cb.camel@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Date: Wed, 01 Jan 2025 08:48:33 +0100 (CET)
Injection-Info: dont-email.me; posting-host="07e9084d35e98e2c9c23b4b63e81553f";
	logging-data="2825580"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18KgTc9rHURl1U6KOdhCfV2"
User-Agent: Evolution 3.54.2 (3.54.2-1.fc41)
Cancel-Lock: sha1:YhzIHBRaljeF6JsmrVXmzufbw8o=
Bytes: 6278

In recent revision of libwy (a C++ library that wraps Clib functions), I fe=
el
the so called 'graphics' can be a file descriptor.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D q_mand2.cpp
/* Copyright is licensed by GNU LGPL, see file COPYING.       by I.J.Wang 2=
024
=20
  Draw image of Mandelbrot set (can zoom-in, one thread, slower)

  Build: make q_mand2
*/
#include <Wy.stdio.h>
#include <Wy.complex.h>
#include <CSCall/Face.h>
#include <CSCall/Array2D.h>

using namespace Wy;

//--------- Drawing --------
typedef Face::RGB32_Pixel Pixel;
constexpr Pixel White=3DFace::RGB32_White;
constexpr Pixel Black=3DFace::RGB32_Black;
const char GrSvr[]=3D"../face/face";   // see ../face/README

typedef long double MandFType;
constexpr int MaxItrNum=3D255;

// [Ret] Conversion table from itrnum to Pixel
Array<Pixel> itrnum_pixel() {
 Array<Pixel> tab;
 for(int n=3D0; n<=3DMaxItrNum; ++n) {
   // transform $itrnum to approximate temperature color=20
   Pixel pix(Black);
   pix[2]=3D (n&7)<<5;
   pix[1]=3D ((n>>3)&7)<<5;
   pix[0]=3D ((n>>6)&3)<<6;
   tab << pix;
 }
 return tab;
};
const Array<Pixel> ctab( itrnum_pixel() );

constexpr unsigned int Dim=3D800;
MandFType dist_pd=3D(4.0/(Dim-1)); // mand distance per dot distance
MandFType mand_lft=3D-3.0;
MandFType mand_top=3D2.0;
Array2D<Pixel> mand_img(Dim,Dim);

// [Syn] Draw Mand. image in line range [lbgn,lend)
//
void draw_mand(const Hdr2D<Pixel>& hdr, int lbgn, int lend) {
 for(int y=3Dlbgn; y<lend; ++y) {
   for(int x=3D0; x<static_cast<int>(hdr.width()); ++x) {
     const Complex<MandFType> c( x*dist_pd +mand_lft, mand_top -y*dist_pd )=
;
     const MandFType MaxValue=3D2.0*abs(c);
     Complex<MandFType> z;
     int itrnum;
     for(itrnum=3D0; itrnum<MaxItrNum; ++itrnum) {
       z=3D(z*z)+c;
       if(abs(z)>MaxValue) {
         break;
       } =20
     };    =20
     hdr.write_point(x,y,ctab[itrnum]);
   };
 };
};

//--------------- Process mouse events
FifoFile frd,fwr;

class StrmRcvr : public Face::Receiver {
    int m_px,m_py;
  public:
    StrmRcvr() : Face::Receiver() {};
    StrmRcvr(RdBuf& strm) : Face::Receiver(strm) {};

    Errno cb_headpkt(const char*) override { return Ok; };

    Errno cb_MO(Wy::WeeStr mtype, int x, int y, unsigned int butt) override=
 {
      Errno r;
      if(mtype=3D=3D"press") {
        if(butt&Face::LeftButton) {
          m_px=3Dx;
          m_py=3Dy;
        }
      } else if(mtype=3D=3D"release") {
        if(butt&Face::LeftButton) {
          int dx,dy;     // process the zoom-in box
          if(x>=3Dm_px) {
            dx=3Dx-m_px;
          } else {
            dx=3Dm_px;
            m_px=3Dx;
            x=3Ddx;
            dx-=3Dm_px;
          }
          if(y>=3Dm_py) {
            dy=3Dy-m_py;
          } else {
            dy=3Dm_py;
            m_py=3Dy;
            y=3Ddy;
            dy-=3Dm_py;
          }
          if((dx<10)||(dy<10)) {
            return Ok;  // not to zoom-in
          }
          MandFType mand_rit=3D mand_lft+x*dist_pd;
          MandFType mand_btm=3D mand_top-y*dist_pd;
          mand_lft+=3D m_px*dist_pd;
          mand_top-=3D m_py*dist_pd;
          if(dx>=3Ddy) {
            dist_pd=3D (mand_rit-mand_lft)/Dim;
          } else {
            dist_pd=3D (mand_top-mand_btm)/Dim;
          }
          if(dx>10) {
            draw_mand(mand_img.header(),0,mand_img.height()-1);
            if((r=3DFace::put_image(fwr, mand_img.header()))!=3DOk) {
              WY_THROW(r);
            }
          }
        }
      } else {
      }
      return Ok;
    };

    Wy::Errno cb_error(Wy::Errno err, int id) override {
      cerr << "q_mand2: " << Wy::wrd(err) << ", id=3D" << id << WY_ENDL;
      return err;
    };
};

int main(int argc, char *argv[]) try {
 Errno r;
 ProcessID cpid;

 draw_mand(mand_img.header(),0,mand_img.height()-1);

 if((r=3Dpopen(GrSvr,cpid,frd,fwr))!=3DOk) {
   WY_THROW(r);
 }
 if((r=3DFace::put_image(fwr, mand_img._data(),Dim,Dim))!=3DOk) {
   WY_THROW(r);
 }

 RdBuf strm(frd);
 StrmRcvr rcvr(strm);
 if((r=3Drcvr.pkt_dispatch())!=3DOk) {
   WY_THROW(r);
 }

 WaitStat wstt;
 if((r=3Dwaitpid(cpid,&wstt,0))!=3DOk) {
   WY_THROW(r);
 }
 cout << "Child exit " << wstt.exit_status() << WY_ENDL;

 cout << "OK" WY_ENDL;
 return 0;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
 return -1;  // e.c_errno();
}
catch(...) {
 cerr << "main() caught(...)" WY_ENDL;
 throw;
};

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D end of q_mand2.cp=
p

We open 'graphics' with a pathname GrSvr (whatever) and get a file descript=
or (fwr=20
and frd can be the same file descriptor if supported):

========== REMAINDER OF ARTICLE TRUNCATED ==========