"Fossies" - the Fresh Open Source Software Archive

Member "libgd-2.3.3/src/gd_io_stream.h" (11 Sep 2021, 3603 Bytes) of package /linux/www/libgd-2.3.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gd_io_stream.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.3.2_vs_2.3.3.

    1 /* *****************************************************************************
    2 ** Initial file written and documented by:
    3 ** Kevin Shepherd <kshepherd@php.net> December 2007
    4 ** of Scarlet Line http://www.scarletline.com/
    5 *******************************************************************************/
    6 /** \file gd_io_stream.h
    7     \brief C++ standard library iostream specializations of gdIOCtx.
    8 
    9     Note that all of the methods defined in this header are internal to the
   10     libgd library, except for the constructors.
   11     Only the constructors are needed by a user of the libgd API.
   12     This file does not use or need gdpp.h, but if GD::Image is
   13     used, then C++ coding becomes even simpler, and the classes below
   14     become entirely hidden implementation details.
   15     Example usage, convert png to gif:
   16     #include <fstream>
   17     #include "gd_io_stream.h"
   18     std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
   19     if (in.good())
   20         {
   21         istreamIOCtx _in_ctx(in);
   22         gdImagePtr im_in = gdImageCreateFromPngCtx ( & _in_ctx);
   23         std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
   24         ostreamIOCtx _out_ctx(out);
   25         gdImageGifCtx(im_in, & _out_ctx);
   26         }
   27     gdImageDestroy(im_in);
   28 */
   29 #ifndef _gd_io_stream_h
   30 #define _gd_io_stream_h
   31 #ifdef __cplusplus
   32 
   33 #include "gd.h"
   34 #include <iostream>
   35 
   36 /** Standard library input stream specialization of gdIOCtx
   37 */
   38 class BGD_EXPORT_DATA_IMPL istreamIOCtx : public gdIOCtx
   39 {
   40 public:
   41     typedef std::istream    stream_type;
   42     /** Construct an instance of this input stream specialization,
   43         given an input stream.
   44         For example:
   45         std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
   46         istreamIOCtx in_ctx(in);
   47     */
   48     istreamIOCtx(stream_type & __stream) {
   49         init( & __stream);
   50     }
   51 
   52     static int  Getbuf(gdIOCtxPtr ctx, void *buf, int size);
   53     static int  Putbuf(gdIOCtxPtr, const void *, int);
   54     static void Putchar(gdIOCtxPtr, int);
   55     static int  Getchar(gdIOCtxPtr ctx);
   56     static int  Seek(gdIOCtxPtr ctx, const int pos);
   57     static long Tell(gdIOCtxPtr ctx);
   58     static void FreeCtx(gdIOCtxPtr ctx);
   59 
   60     void init(stream_type * __stream) {
   61         getC = Getchar;
   62         putC = Putchar;
   63         getBuf = Getbuf;
   64         putBuf = Putbuf;
   65         tell = Tell;
   66         seek = Seek;
   67         gd_free = FreeCtx;
   68         _M_stream = __stream;
   69     }
   70 private:
   71     stream_type *   _M_stream;
   72 };
   73 /** Allocate a new instance of the class
   74 */
   75 inline gdIOCtxPtr gdNewIstreamCtx(std::istream *__stream)
   76 {
   77     return new istreamIOCtx(* __stream);
   78 }
   79 
   80 /** Standard library output stream specialization of gdIOCtx
   81 */
   82 class BGD_EXPORT_DATA_IMPL ostreamIOCtx : public gdIOCtx
   83 {
   84 public:
   85     typedef std::ostream    stream_type;
   86     /** Construct an instance of this output stream specialization,
   87         given an output stream.
   88         For example:
   89         std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
   90         ostreamIOCtx out_ctx(out);
   91     */
   92     ostreamIOCtx(stream_type & __stream) {
   93         init( & __stream);
   94     }
   95 
   96     static int  Getbuf(gdIOCtxPtr, void *, int);
   97     static int  Putbuf(gdIOCtxPtr ctx, const void *buf, int size);
   98     static int  Getchar(gdIOCtxPtr);
   99     static void Putchar(gdIOCtxPtr ctx, int a);
  100     static int  Seek(gdIOCtxPtr ctx, const int pos);
  101     static long Tell(gdIOCtxPtr ctx);
  102     static void FreeCtx(gdIOCtxPtr ctx);
  103 
  104     void init(stream_type * __stream) {
  105         getC = Getchar;
  106         putC = Putchar;
  107         getBuf = Getbuf;
  108         putBuf = Putbuf;
  109         tell = Tell;
  110         seek = Seek;
  111         gd_free = FreeCtx;
  112         _M_stream = __stream;
  113     }
  114 private:
  115     stream_type *   _M_stream;
  116 };
  117 /** Allocate a new instance of the class
  118 */
  119 inline gdIOCtxPtr gdNewOstreamCtx(std::ostream *__stream)
  120 {
  121     return new ostreamIOCtx(* __stream);
  122 }
  123 
  124 #endif /* __cplusplus */
  125 #endif /* _gd_io_stream_h */