PRB: Image Functions Fail with Image Outside of Viewport

ID: Q118682

7.00 | 1.00 1.50 MS-DOS | WINDOWS kbprg kbprb

The information in this article applies to:

SYMPTOMS

The GRAPHICS.LIB image functions _putimage, _getimage(), _putimage_w(), and getimage_w() fail when the specified image falls outside the current viewport. Calling _grstatus() in this instance returns _GRERROR.

CAUSE

Though it is not documented anywhere else, this behavior is by design. The functions listed above fail when you attempt to place even part of an image outside the viewport. Images are never clipped to the viewport.

RESOLUTION

Do not attempt to place images outside of the current viewport. Either make the viewport larger or work with partial (smaller) images.

MORE INFORMATION

The following program shows common mistakes that cause _getimage and _putimage to fail:

Sample Code

   /* Compile options needed: none
   */ 

   #include <stdio.h>
   #include <conio.h>
   #include <malloc.h>
   #include <graph.h>

   main()
   {
      char __huge *buffer;     // image buffer
      long buff_size;          // size of the image
      struct _videoconfig vc;

      if (!_setvideomode(_VRES16COLOR))
      {
         printf("Can\'t set video mode\n");
         exit(1);
      }

      _getvideoconfig(&vc);

    // error - should go to vc.numxpixels-1 and vc.numypixels-1
    // however _imagesize will just return a larger buffer as needed

      buff_size = _imagesize( 0,0,vc.numxpixels,vc.numypixels );
      buffer = (char __huge *) _halloc( buff_size, sizeof( char ) );
      if (buffer == (char __huge *) NULL)
      {
         printf("halloc failed allocating %ld bytes\n", buff_size);
         _getch();
         _setvideomode(_DEFAULTMODE);
         exit(1);
      }

    // error - should go to vc.numxpixels-1 and vc.numypixels-1

      _getimage( 0, 0, vc.numxpixels, vc.numypixels, buffer );
      if (_grstatus() < _GROK)
      {
         printf("Getimage error %d\n",_grstatus());
         _getch();
         _setvideomode(_DEFAULTMODE);
         exit(1);
      }

      _setvieworg( vc.numxpixels/2, vc.numypixels/2 );

    // error - we've translated the origin so vc.numxpixels-1 and
    // vc.numypixels-1 are out of range

      _getimage( 0, 0, vc.numxpixels-1, vc.numypixels-1, buffer );
      if (_grstatus() < _GROK)
      {
         printf("Getimage error %d\n",_grstatus());
         _getch();
         _setvideomode(_DEFAULTMODE);
         exit(1);
      }
      _setvideomode(_DEFAULTMODE);
   }

Additional reference words: 7.00 1.00 1.50 KBCategory: kbprg kbprb KBSubcategory: CRTIss GraphicsIss Keywords : kb16bitonly

Last Reviewed: July 23, 1997