⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grx11.c

📁 Ftee type Demo for Linux open source
💻 C
📖 第 1 页 / 共 3 页
字号:
    unsigned char*  line_write = blit->dst_line + blit->x * 3;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*   read  = line_read;
      unsigned char*   write = line_write;
      int              x     = blit->width;


      for ( ; x > 0; x--, read++, write += 3 )
      {
        unsigned char  p = read[0];


        write[0] = p;
        write[1] = p;
        write[2] = p;
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static const grX11Format  gr_x11_format_rgb888 =
  {
    24, 24, 0xFF0000L, 0x00FF00U, 0x0000FF,
    gr_x11_convert_rgb_to_rgb888,
    gr_x11_convert_gray_to_rgb888
  };


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                BLITTING ROUTINES FOR  BGR888                 *****/
  /*****                                                              *****/
  /************************************************************************/
  /************************************************************************/

  static void
  gr_x11_convert_rgb_to_bgr888( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x * 3;
    unsigned char*  line_write = blit->dst_line + blit->x * 3;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*   read  = line_read;
      unsigned char*   write = line_write;
      int              x     = blit->width;


      for ( ; x > 0; x--, read += 3, write += 3 )
      {
        write[0] = read[2];
        write[1] = read[1];
        write[2] = read[0];
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static const grX11Format  gr_x11_format_bgr888 =
  {
    24, 24, 0x0000FF, 0x00FF00U, 0xFF0000L,
    gr_x11_convert_rgb_to_bgr888,
    gr_x11_convert_gray_to_rgb888   /* the same for bgr888 */
  };


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                BLITTING ROUTINES FOR RGB8880                 *****/
  /*****                                                              *****/
  /************************************************************************/
  /************************************************************************/

  static void
  gr_x11_convert_rgb_to_rgb8880( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x * 3;
    unsigned char*  line_write = blit->dst_line + blit->x * 4;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*   read  = line_read;
      unsigned char*   write = line_write;
      int              x     = blit->width;


      for ( ; x > 0; x--, read += 3, write += 4 )
      {
        uint32  r = read[0];
        uint32  g = read[1];
        uint32  b = read[2];

        *(uint32*)write = ( ( r << 24 ) | ( g << 16 ) | ( b << 8 ) );
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static void
  gr_x11_convert_gray_to_rgb8880( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x;
    unsigned char*  line_write = blit->dst_line + blit->x*4;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*  read  = line_read;
      unsigned char*  write = line_write;
      int             x     = blit->width;


      for ( ; x > 0; x--, read ++, write += 4 )
      {
        uint32  p = read[0];


        *(uint32*)write = ( ( p << 24 ) | ( p << 16 ) | ( p << 8 ) );
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static const grX11Format  gr_x11_format_rgb8880 =
  {
    24, 32, 0xFF000000UL, 0x00FF0000L, 0x0000FF00U,
    gr_x11_convert_rgb_to_rgb8880,
    gr_x11_convert_gray_to_rgb8880
  };


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                BLITTING ROUTINES FOR RGB0888                 *****/
  /*****                                                              *****/
  /************************************************************************/
  /************************************************************************/

  static void
  gr_x11_convert_rgb_to_rgb0888( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x * 3;
    unsigned char*  line_write = blit->dst_line + blit->x * 4;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*  read  = line_read;
      unsigned char*  write = line_write;
      int             x     = blit->width;


      for ( ; x > 0; x--, read += 3, write += 4 )
      {
        uint32  r = read[0];
        uint32  g = read[1];
        uint32  b = read[2];

        *(uint32*)write = ( ( r << 16 ) | ( g << 8 ) | ( b << 0 ) );
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static void
  gr_x11_convert_gray_to_rgb0888( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x;
    unsigned char*  line_write = blit->dst_line + blit->x * 4;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*  read  = line_read;
      unsigned char*  write = line_write;
      int             x     = blit->width;


      for ( ; x > 0; x--, read ++, write += 4 )
      {
        uint32  p = read[0];


        *(uint32*)write = ( ( p << 16 ) | ( p << 8 ) | ( p << 0 ) );
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static const grX11Format  gr_x11_format_rgb0888 =
  {
    24, 32, 0x00FF0000L, 0x0000FF00U, 0x000000FF,
    gr_x11_convert_rgb_to_rgb0888,
    gr_x11_convert_gray_to_rgb0888
  };


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                BLITTING ROUTINES FOR BGR8880                 *****/
  /*****                                                              *****/
  /************************************************************************/
  /************************************************************************/

  static void
  gr_x11_convert_rgb_to_bgr8880( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x * 3;
    unsigned char*  line_write = blit->dst_line + blit->x * 4;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*  read  = line_read;
      unsigned char*  write = line_write;
      int             x     = blit->width;


      for ( ; x > 0; x--, read += 3, write += 4 )
      {
        uint32  r = read[0];
        uint32  g = read[1];
        uint32  b = read[2];

        *(uint32*)write = ( ( r << 8 ) | ( g << 16 ) | ( b << 24 ) );
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static const grX11Format  gr_x11_format_bgr8880 =
  {
    24, 32, 0x0000FF00U, 0x00FF0000L, 0xFF000000UL,
    gr_x11_convert_rgb_to_bgr8880,
    gr_x11_convert_gray_to_rgb8880  /* the same for bgr8880 */
  };


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                BLITTING ROUTINES FOR BGR0888                 *****/
  /*****                                                              *****/
  /************************************************************************/
  /************************************************************************/

  static void
  gr_x11_convert_rgb_to_bgr0888( grX11Blitter*  blit )
  {
    unsigned char*  line_read  = blit->src_line + blit->x * 3;
    unsigned char*  line_write = blit->dst_line + blit->x * 4;
    int             h          = blit->height;


    for ( ; h > 0; h-- )
    {
      unsigned char*  read  = line_read;
      unsigned char*  write = line_write;
      int             x     = blit->width;


      for ( ; x > 0; x--, read += 3, write += 4 )
      {
        uint32  r = read[0];
        uint32  g = read[1];
        uint32  b = read[2];

        *(uint32*)write = ( ( r << 0 ) | ( g << 8 ) | ( b << 16 ) );
      }

      line_read  += blit->src_pitch;
      line_write += blit->dst_pitch;
    }
  }


  static const grX11Format  gr_x11_format_bgr0888 =
  {
    24, 32, 0x000000FF, 0x0000FF00U, 0x00FF0000L,
    gr_x11_convert_rgb_to_bgr0888,
    gr_x11_convert_gray_to_rgb0888  /* the same for bgr0888 */
  };


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                   X11 DEVICE SUPPORT                         *****/
  /*****                                                              *****/
  /************************************************************************/
  /************************************************************************/

  static const grX11Format*  gr_x11_formats[] =
  {
    &gr_x11_format_rgb565,
    &gr_x11_format_bgr565,
    &gr_x11_format_rgb555,
    &gr_x11_format_bgr555,
    &gr_x11_format_rgb888,
    &gr_x11_format_bgr888,
    &gr_x11_format_rgb0888,
    &gr_x11_format_bgr0888,
    &gr_x11_format_rgb8880,
    &gr_x11_format_bgr8880,

    NULL,
  };

  typedef struct  grX11DeviceRec_
  {
    Display*            display;
    Cursor              idle;
    Cursor              busy;
    const grX11Format*  format;
    int                 scanline_pad;
    Visual*             visual;

  } grX11Device;


  static grX11Device  x11dev;


  static void
  gr_x11_device_done( void )
  {
    if ( x11dev.display )
    {
      XCloseDisplay( x11dev.display );
      x11dev.display = NULL;
    }
  }


  static int
  gr_x11_device_init( void )
  {
    memset( &x11dev, 0, sizeof ( x11dev ) );

    XrmInitialize();


    x11dev.display = XOpenDisplay( "" );
    if ( !x11dev.display )
    {
      fprintf( stderr, "cannot open X11 display\n" );
      return -1;
    }

    x11dev.idle = XCreateFontCursor( x11dev.display, XC_left_ptr );
    x11dev.busy = XCreateFontCursor( x11dev.display, XC_watch );

    {
      int          count;
      XDepth*      format;
      XDepth*      formats;
      XVisualInfo  templ;


      formats = XListPixmapFormats( x11dev.display, &count );

#ifdef TEST
      printf( "available pixmap formats\n" );
      printf( "depth  pixbits  scanpad\n" );
#endif /* TEST */

      for ( format = formats; count > 0; count--, format++ )
      {
#ifdef TEST
        printf( " %3d     %3d      %3d\n",
                format->depth,
                format->bits_per_pixel,
                format->scanline_pad );
#endif /* TEST */

        /* note, the 32-bit modes return a depth of 24, */
        /* and 32 bits per pixel                        */
        switch ( format->depth )
        {
        case 16:
        case 24:
        case 32:
          {
            int           count2;
            XVisualInfo*  visuals;
            XVisualInfo*  visual;


            templ.screen = DefaultScreen( x11dev.display );
            templ.depth = format->depth;
            visuals      = XGetVisualInfo( x11dev.display,
                                           VisualScreenMask | VisualDepthMask,
                                          &templ,
                                          &count2 );

            for ( visual = visuals; count2 > 0; count2--, visual++ )
            {
#ifdef TEST
              const char*  string = "unknown";


              switch ( visual->Class )
              {
              case TrueColor:
                string = "TrueColor";
                break;
              case DirectColor:
                string = "DirectColor";
                break;
              case PseudoColor:
                string = "PseudoColor";
                break;
              case StaticGray:
                string = "StaticGray";
                break;
              case StaticColor:
                string = "StaticColor";
                break;
              case GrayScale:
                string = "GrayScale";
                break;
              }

              printf( ">   RGB %04lx:%04lx:%04lx, colors %3d, bits %2d  %s\n",
                      visual->red_mask,
                      visual->green_mask,
                      visual->blue_mask,
                      visual->colormap_size,
                      visual->bits_per_rgb,
                      string );
#endif /* TEST */

              /* compare to the list of supported formats */
              {
                const grX11Format**  pcur_format = gr_x11_formats;
                const grX11Format*   cur_format;


                for (;;)
                {
                  cur_format = *pcur_format++;
                  if ( cur_format == NULL )
                    break;

                  if ( format->depth          == cur_format->x_depth          &&
                       format->bits_per_pixel == cur_format->x_bits_per_pixel &&
                       visual->red_mask       == cur_format->x_red_mask       &&
                       visual->green_mask     == cur_format->x_green_mask     &&
                       visual->blue_mask      == cur_format->x_blue_mask      )
                  {
                    x11dev.format       = cur_format;
                    x11dev.scanline_pad = format->scanline_pad;
                    x11dev.visual       = visual->visual;
                    
                    XFree( visuals );
                    XFree( formats );
                    return 0;
                  }
                }
              }
            } /* for visuals */
            
            XFree( visuals );
          }
          break;

        default:
          ;
        } /* switch format depth */
      } /* for formats */
      XFree( formats );
    }

    
    fprintf( stderr, "unsupported X11 display depth!\n" );

    return -1;
  }


  /************************************************************************/
  /************************************************************************/
  /*****                                                              *****/
  /*****                   X11 SURFACE SUPPORT                        *****/
  /*****                                                              *****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -