obeid salem
obeid salem

Reputation: 147

Convert SDL_Texture to SDL_Surface to save an image in c++

Update:

Thank you @HolyBlackCat for the comments, Its working now:

code updated:

if (isImageCompleted) {
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 50);
  SDL_RenderClear(renderer);
  isImageCompleted = false;
  totalReadBytes = 0;
  sdlRWops = SDL_RWFromMem(&imgData[0], imgData.size());
  if (sdlRWops != NULL) {
    // texture = IMG_LoadTexture_RW(renderer, sdlRWops, 0);
    mSurface = IMG_Load_RW(sdlRWops, 0);
    texture = SDL_CreateTextureFromSurface(renderer, mSurface);
    SDL_RenderCopyEx(renderer, texture, NULL, &rect, 0, NULL, SDL_FLIP_NONE);
    if (isSavingImage) {
      isSavingImage = false;
      IMG_SaveJPG(mSurface, "test.jpg", 100);          
    }
    drawButtonPic();
    SDL_DestroyTexture(texture);
    SDL_RenderPresent(renderer);
  }
  SDL_FreeRW(sdlRWops);
  imgData.clear();
}

Created a Program to receive a Camera video from an Android Phone using USB,

Everything is working and I receive 30 images per second ( 30fps )

on the program I did a button to save a frame ( Image ) and my problem is Converting the image Texture into SDL_Surface so i can use it ( IMG_SaveJPG )

How do I convert the Image Texture to Surface ?

Here is some code after i fully receive the full image data:

       if (isImageCompleted) {
          SDL_SetRenderDrawColor(renderer, 0, 0, 0, 50);
          SDL_RenderClear(renderer);
          isImageCompleted = false;
          totalReadBytes = 0;
          sdlRWops = SDL_RWFromMem(&imgData[0], imgData.size());
          if (sdlRWops != NULL) {
            texture = IMG_LoadTexture_RW(renderer, sdlRWops, 0);
            SDL_RenderCopyEx(renderer, texture, NULL, &rect, 0, NULL, SDL_FLIP_NONE);
          // if I Click the Button the isSavingImage will be true
            if (isSavingImage) {
              isSavingImage = false;
                // TODO save the image file
            }
            drawButtonPic();
            SDL_DestroyTexture(texture);
            SDL_RenderPresent(renderer);
          }
          SDL_FreeRW(sdlRWops);
          imgData.clear();
        }

Everything works great, except the saving part

Thank you.

Upvotes: 0

Views: 294

Answers (1)

obeid salem
obeid salem

Reputation: 147

with help from @HolyBlackCat, I created the image surface first then the Texture, in this way I'll still have the surface ready to use on each frame..

if (isImageCompleted) {
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 50);
  SDL_RenderClear(renderer);
  isImageCompleted = false;
  totalReadBytes = 0;
  sdlRWops = SDL_RWFromMem(&imgData[0], imgData.size());
  if (sdlRWops != NULL) {
    // texture = IMG_LoadTexture_RW(renderer, sdlRWops, 0);
    mSurface = IMG_Load_RW(sdlRWops, 0);
    texture = SDL_CreateTextureFromSurface(renderer, mSurface);
    SDL_RenderCopyEx(renderer, texture, NULL, &rect, 0, NULL, SDL_FLIP_NONE);
    if (isSavingImage) {
      isSavingImage = false;
      IMG_SaveJPG(mSurface, "test.jpg", 100);          
    }
    drawButtonPic();
    SDL_DestroyTexture(texture);
    SDL_RenderPresent(renderer);
  }
  SDL_FreeRW(sdlRWops);
  imgData.clear();
}

Upvotes: 2

Related Questions