Repository: xbmc/xbmc
File: xbmc/pictures/Picture.cpp (C++)

Code before:
  unsigned int imageRes = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_imageRes;

  unsigned int tile_width = imageRes / num_across;
  unsigned int tile_height = imageRes / num_down;
  unsigned int tile_gap = 1;
  bool success = false;

  // create a buffer for the resulting thumb
  uint32_t* buffer = static_cast<uint32_t*>(calloc(imageRes * imageRes, 4));
  if (!buffer)
    return {};
  for (unsigned int i = 0; i < files.size(); ++i)
  {
    int x = i % num_across;
    int y = i / num_across;
    // load in the image
    unsigned int width = tile_width - 2 * tile_gap, height = tile_height - 2 * tile_gap;
    std::unique_ptr<CTexture> texture = CTexture::LoadFromFile(files[i], width, height, true);
    if (texture && texture->GetWidth() && texture->GetHeight())
    {
      GetScale(texture->GetWidth(), texture->GetHeight(), width, height);

      // scale appropriately
      uint32_t* scaled = new uint32_t[width * height];
      if (ScaleImage(texture->GetPixels(), texture->GetWidth(), texture->GetHeight(),
                     texture->GetPitch(), AV_PIX_FMT_BGRA, (uint8_t*)scaled, width, height,
                     width * 4, AV_PIX_FMT_BGRA))
      {
        unsigned int stridePixels{width};
        if (!texture->GetOrientation() ||
            OrientateImage(scaled, width, height, texture->GetOrientation(), stridePixels))
        {
          success = true; // Flag that we at least had one successful image processed
          // drop into the texture
          unsigned int posX = x * tile_width + (tile_width - width) / 2;
          unsigned int posY = y * tile_height + (tile_height - height) / 2;
          uint32_t* dest = buffer + posX + posY * imageRes;
          uint32_t* src = scaled;
          for (unsigned int y = 0; y < height; ++y)
          {
            memcpy(dest, src, width * 4);
            dest += imageRes;
            src += stridePixels;
          }
        }
      }
      delete[] scaled;
    }
  }

  std::unique_ptr<CTexture> result = CTexture::CreateTexture();

Review comment (suggestion):
`(uint8_t*)scaled` should be `reinterpret_cast<uint8_t*>(scaled)` to use C++ casts

Code after:

  unsigned int imageRes = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_imageRes;

  unsigned int tile_width = imageRes / num_across;
  unsigned int tile_height = imageRes / num_down;
  unsigned int tile_gap = 1;
  bool success = false; // Flag that we at least had one successful image processed

  // create a buffer for the resulting thumb
  std::unique_ptr<uint32_t[]> buffer = std::make_unique<uint32_t[]>(imageRes * imageRes);
  if (!buffer)
    return {};
  for (unsigned int i = 0; i < files.size(); ++i)
  {
    int x = i % num_across;
    int y = i / num_across;
    // load in the image
    unsigned int width = tile_width - 2 * tile_gap, height = tile_height - 2 * tile_gap;
    std::unique_ptr<CTexture> texture = CTexture::LoadFromFile(files[i], width, height, true);
    if (texture && texture->GetWidth() && texture->GetHeight())
    {
      GetScale(texture->GetWidth(), texture->GetHeight(), width, height);

      // scale appropriately
      std::unique_ptr<uint32_t[]> scaled = std::make_unique<uint32_t[]>(width * height);
      if (ScaleImage(texture->GetPixels(), texture->GetWidth(), texture->GetHeight(),
                     texture->GetPitch(), AV_PIX_FMT_BGRA, reinterpret_cast<uint8_t*>(scaled.get()),
                     width, height, width * 4, AV_PIX_FMT_BGRA))
      {
        unsigned int stridePixels{width};
        uint32_t* scaledL = scaled.get();
        if (!texture->GetOrientation() ||
            OrientateImage(scaledL, width, height, texture->GetOrientation(), stridePixels))
        {
          success = true;
          // drop into the texture
          unsigned int posX = x * tile_width + (tile_width - width) / 2;
          unsigned int posY = y * tile_height + (tile_height - height) / 2;
          uint32_t* dest = buffer.get() + posX + posY * imageRes;
          const uint32_t* src = scaled.get();
          for (unsigned int y = 0; y < height; ++y)
          {
            memcpy(dest, src, width * 4);
            dest += imageRes;
            src += stridePixels;
          }
        }
      }
    }
  }