title: SDL2 resize a surface
body: <p>We want to create an SDL surface by loading an image with SDL_Image and if the dimensions exceed a limit resize the surface.</p>
  
  <p>The reason we need to do this is on Raspbian SDL throws an error creating a texture from the surface ('<em>Texture dimensions are limited to 2048x2048</em>').  Whilst that's a very large image we don't want users to be concerned about image size, we want to resize it for them.  Although we haven't encountered this limit on Windows, we're trying to develop the solution on windows and having issues resizing the texture.</p>
  
  <p>Looking for a solution there have been similar questions...:</p>
  
  <ul>
  <li><a href="http://www.sdltutorials.com/sdl-scale-surface" rel="nofollow noreferrer">2008 not SDL2 custom blitting</a></li>
  <li><a href="http://www.cplusplus.com/forum/general/22157/" rel="nofollow noreferrer">2010 use SDL_gfx</a></li>
  <li><a href="https://stackoverflow.com/questions/328500/proper-way-to-scale-an-sdl-surface-without-clipping">2008 can't be done use SDL_gfx, 2015 use SDL_BlitScaled, 2015 use SDL_RenderCopyEx</a></li>
  </ul>
  
  <p>Is a custom blitter or SDL_gfx necessary with current SDL2 (those answers pre-date SDL2's 2013 release)?  SDLRenderCopyEx doesn't help as you need to generate the texture which is where our problem occurs.</p>
  
  <p>So we tried some of the available blitting functions like SDL_BlitScaled, below is a simple program to render a 2500x2500 PNG with no scaling:</p>
  
  <pre><code>#include &lt;SDL.h&gt;
  #include &lt;SDL_image.h&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  
  SDL_Texture * get_texture(
      SDL_Renderer * pRenderer,
      std::string image_filename) {
      SDL_Texture * result = NULL;
  
      SDL_Surface * pSurface = IMG_Load(image_filename.c_str());
  
      if (pSurface == NULL) {
          printf("Error image load: %s\n", IMG_GetError());
      }
      else {
          SDL_Texture * pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
  
          if (pTexture == NULL) {
              printf("Error image load: %s\n", SDL_GetError());
          }
          else {
              SDL_SetTextureBlendMode(
                  pTexture,
                  SDL_BLENDMODE_BLEND);
  
              result = pTexture;
          }
  
          SDL_FreeSurface(pSurface);
          pSurface = NULL;
      }
  
      return result;
  }
  
  int main(int argc, char* args[]) {
      SDL_Window * pWindow = NULL;
      SDL_Renderer * pRenderer = NULL;
  
      // set up
      SDL_Init(SDL_INIT_VIDEO);
  
      SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
  
      SDL_Rect screenDimensions;
  
      screenDimensions.x = 0;
      screenDimensions.y = 0;
  
      screenDimensions.w = 640;
      screenDimensions.h = 480;
  
      pWindow = SDL_CreateWindow("Resize Test",
          SDL_WINDOWPOS_UNDEFINED,
          SDL_WINDOWPOS_UNDEFINED,
          screenDimensions.w, 
          screenDimensions.h,
          SDL_WINDOW_SHOWN);
  
      pRenderer = SDL_CreateRenderer(pWindow,
          -1,
          SDL_RENDERER_ACCELERATED);
  
      IMG_Init(IMG_INIT_PNG);
  
      // render
      SDL_SetRenderDrawColor(
          pRenderer,
          0,
          0,
          0,
          0);
  
      SDL_RenderClear(pRenderer);
  
      SDL_Texture * pTexture = get_texture(
          pRenderer,
          "2500x2500.png");
  
      if (pTexture != NULL) {
          SDL_RenderCopy(
              pRenderer,
              pTexture,
              NULL,
              &amp;screenDimensions);
  
          SDL_DestroyTexture(pTexture);
          pTexture = NULL;
      }
  
      SDL_RenderPresent(pRenderer);
  
      // wait for quit
      bool quit = false;
  
      while (!quit)
      {
          // poll input for quit
          SDL_Event inputEvent;
  
          while (SDL_PollEvent(&amp;inputEvent) != 0) {
              if ((inputEvent.type == SDL_KEYDOWN) &amp;&amp;
                  (inputEvent.key.keysym.sym == 113)) {
                  quit = true;
              }
          }
      }
  
      IMG_Quit();
  
      SDL_DestroyRenderer(pRenderer);
  
      pRenderer = NULL;
  
      SDL_DestroyWindow(pWindow);
  
      pWindow = NULL;
  
      return 0;
  }
  </code></pre>
  
  <p>Changing the get_texture function so it identifies a limit and tries to create a new surface:</p>
  
  <pre><code>SDL_Texture * get_texture(
      SDL_Renderer * pRenderer,
      std::string image_filename) {
      SDL_Texture * result = NULL;
  
      SDL_Surface * pSurface = IMG_Load(image_filename.c_str());
  
      if (pSurface == NULL) {
          printf("Error image load: %s\n", IMG_GetError());
      }
      else {
          const int limit = 1024;
          int width = pSurface-&gt;w;
          int height = pSurface-&gt;h;
  
          if ((width &gt; limit) ||
              (height &gt; limit)) {
              SDL_Rect sourceDimensions;
              sourceDimensions.x = 0;
              sourceDimensions.y = 0;
              sourceDimensions.w = width;
              sourceDimensions.h = height;
  
              float scale = (float)limit / (float)width;
              float scaleH = (float)limit / (float)height;
  
              if (scaleH &lt; scale) {
                  scale = scaleH;
              }
  
              SDL_Rect targetDimensions;
              targetDimensions.x = 0;
              targetDimensions.y = 0;
              targetDimensions.w = (int)(width * scale);
              targetDimensions.h = (int)(height * scale);
  
              SDL_Surface *pScaleSurface = SDL_CreateRGBSurface(
                  pSurface-&gt;flags,
                  targetDimensions.w,
                  targetDimensions.h,
                  pSurface-&gt;format-&gt;BitsPerPixel,
                  pSurface-&gt;format-&gt;Rmask,
                  pSurface-&gt;format-&gt;Gmask,
                  pSurface-&gt;format-&gt;Bmask,
                  pSurface-&gt;format-&gt;Amask);
  
              if (SDL_BlitScaled(pSurface, NULL, pScaleSurface, &amp;targetDimensions) &lt; 0) {
                  printf("Error did not scale surface: %s\n", SDL_GetError());
  
                  SDL_FreeSurface(pScaleSurface);
                  pScaleSurface = NULL;
              }
              else {
                  SDL_FreeSurface(pSurface);
  
                  pSurface = pScaleSurface;
                  width = pSurface-&gt;w;
                  height = pSurface-&gt;h;
              }
          }
  
          SDL_Texture * pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
  
          if (pTexture == NULL) {
              printf("Error image load: %s\n", SDL_GetError());
          }
          else {
              SDL_SetTextureBlendMode(
                  pTexture,
                  SDL_BLENDMODE_BLEND);
  
              result = pTexture;
          }
  
          SDL_FreeSurface(pSurface);
          pSurface = NULL;
      }
  
      return result;
  }
  </code></pre>
  
  <p>SDL_BlitScaled fails with an error '<em>Blit combination not supported</em>' other variations have a similar error:</p>
  
  <pre><code>SDL_BlitScaled(pSurface, NULL, pScaleSurface, NULL)
  SDL_BlitScaled(pSurface, &amp;sourceDimensions, pScaleSurface, &amp;targetDimensions)
  SDL_LowerBlitScaled(pSurface, &amp;sourceDimensions, pScaleSurface, &amp;targetDimensions) // from the wiki this is the call SDL_BlitScaled makes internally
  </code></pre>
  
  <p>Then we tried a non-scaled blit... which didn't throw an error but just shows white (not the clear colour or a colour in the image).</p>
  
  <pre><code>SDL_BlitSurface(pSurface, &amp;targetDimensions, pScaleSurface, &amp;targetDimensions)
  </code></pre>
  
  <p>With that blitting function not working we then tried it with the same image as a bitmap (just exporting the .png as .bmp), still loading the file with SDL_Image and both those functions work with SDL_BlitScaled scaling as expected </p>
  
  <p>Not sure what's going wrong here (we expect and need support for major image file formats like .png) or if this is the recommended approach, any help appreciated!</p>