<commit_before>/** \file
 * File for the image resource.
 *
 * @author <NAME>
*/

#include <algorithm>
#include <sstream>

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_rotozoom.h" 

#include "resources/resourcemanager.h"
#include "resources/imageresource.h"

namespace Zabbr {

	/**
	 * Constructor.
	 *
	 * @param name The name of the image.
	 * @param surface The surface of the image.
	*/
	ImageResource::ImageResource(std::string name, SDL_Surface* surface)
	              : SDLSurfaceResource(name, surface) {
	}
	
	/**
	 * Destructor.
	*/
	ImageResource::~ImageResource() {
	}

	/**
	 * Opens an image.
	 *
	 * @param name the filename of the image.
	 *
	 * @throw ResourceNotLoadedException.
	*/
	ImageResource* ImageResource::open(std::string name) {
		SDL_Surface* temp;
		SDL_Surface* image;
		
		temp = IMG_Load((ResourceManager::fgDataPath+name).c_str());
		if (temp == 0) {
			throw ResourceNotLoadedException(name, IMG_GetError());
		}
		
		image = SDL_DisplayFormatAlpha(temp);
		SDL_FreeSurface(temp);
		return new ImageResource(createID(name), image);
	}
	
	/**
	 * Scale and rotate the image.
	 *
	 * @param w The maximum width of the new image.
	 * @param h The maximul height of the new image.
	 * @param keepRatio True if the result shoul be the same ratio as the original.
	 * @param angle The angle of the rotation (in degrees).
	*/
	ImageResource* ImageResource::scaleAndRotate(int w, int h, bool keepRatio, int angle) {
		SDL_Surface* surf;
		if (keepRatio) {
			surf = rotozoomSurface(fSurface, angle, std::min(w*1.0/fSurface->w, h*1.0/fSurface->h), SMOOTHING_ON);
		} else {
			SDL_Surface* t;
			t = zoomSurface(fSurface, w*1.0/fSurface->w, h*1.0/fSurface->h, SMOOTHING_ON);
			surf = rotozoomSurface(fSurface, angle, 1, SMOOTHING_ON);
			delete t;
			
		}
		return new ImageResource(createID(getName(), w, h, keepRatio, angle), surf);<commit_msg>Fix image scaling with non-fixed resolution<commit_after>/** \file
 * File for the image resource.
 *
 * @author <NAME>
*/

#include <algorithm>
#include <sstream>

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_rotozoom.h" 

#include "resources/resourcemanager.h"
#include "resources/imageresource.h"

namespace Zabbr {

	/**
	 * Constructor.
	 *
	 * @param name The name of the image.
	 * @param surface The surface of the image.
	*/
	ImageResource::ImageResource(std::string name, SDL_Surface* surface)
	              : SDLSurfaceResource(name, surface) {
	}
	
	/**
	 * Destructor.
	*/
	ImageResource::~ImageResource() {
	}

	/**
	 * Opens an image.
	 *
	 * @param name the filename of the image.
	 *
	 * @throw ResourceNotLoadedException.
	*/
	ImageResource* ImageResource::open(std::string name) {
		SDL_Surface* temp;
		SDL_Surface* image;
		
		temp = IMG_Load((ResourceManager::fgDataPath+name).c_str());
		if (temp == 0) {
			throw ResourceNotLoadedException(name, IMG_GetError());
		}
		
		image = SDL_DisplayFormatAlpha(temp);
		SDL_FreeSurface(temp);
		return new ImageResource(createID(name), image);
	}
	
	/**
	 * Scale and rotate the image.
	 *
	 * @param w The maximum width of the new image.
	 * @param h The maximul height of the new image.
	 * @param keepRatio True if the result shoul be the same ratio as the original.
	 * @param angle The angle of the rotation (in degrees).
	*/
	ImageResource* ImageResource::scaleAndRotate(int w, int h, bool keepRatio, int angle) {
		SDL_Surface* surf;
		if (keepRatio) {
			surf = rotozoomSurface(fSurface, angle, std::min(w*1.0/fSurface->w, h*1.0/fSurface->h), SMOOTHING_ON);
		} else {
			SDL_Surface* t;
			t = zoomSurface(fSurface, w*1.0/fSurface->w, h*1.0/fSurface->h, SMOOTHING_ON);
			surf = rotozoomSurface(t, angle, 1, SMOOTHING_ON);
			SDL_FreeSurface(t);
			
		}
		return new ImageResource(createID(getName(), w, h, keepRatio, angle), surf);
{'fraction_non_alphanumeric': 0.13338595106550907, 'fraction_numerical': 0.005261773217574322, 'mean_word_length': 3.812658227848101, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3191979', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1274, 'n_words': 343}