Skip to content

Commit

Permalink
png resize
Browse files Browse the repository at this point in the history
rand uid support
  • Loading branch information
Kronos2308 committed Jun 7, 2023
1 parent c387e89 commit 6fb345f
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ INCLUDES := include
ROMFS := romfs
APP_TITLE := Amiigo
APP_AUTHOR := Kronos2308 Fork of CompSciOrBust
APP_VERSION := 1.6.9
APP_VERSION := 1.7.0

#---------------------------------------------------------------------------------
# options for code generation
Expand Down
1 change: 1 addition & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ bool copy_me(string origen, string destino);
void DrawJsonColorConfig(SDL_Renderer* renderer, string Head);
void toemu(std::string ID,json& JSID);
void toamii(std::string& ID, json JSID);
void rezize_PNG (string file_name, int limit);
52 changes: 36 additions & 16 deletions include/emu_Service.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

#pragma once
#include <vector>
#include <string>
#include <switch.h>
#include <cstring>

namespace emu {

struct VirtualAmiiboUuidInfo {
bool random_uuid;
bool use_random_uuid;
u8 uuid[10];
};

Expand All @@ -19,16 +16,20 @@ namespace emu {
};

struct VirtualAmiiboData {
VirtualAmiiboUuidInfo uuid;
VirtualAmiiboUuidInfo uuid_info;
char name[40 + 1];
VirtualAmiiboDate first_write_date;
VirtualAmiiboDate last_write_date;
// MiiCharInfo mii_charinfo;
MiiCharInfo mii_charinfo;

inline bool IsValid() {
return strlen(this->name);
return strlen(this->name) > 0;
}
};

struct VirtualAmiiboAreaEntry {
u64 program_id;
u32 access_id;
};

enum class EmulationStatus : u32 {
Expand All @@ -47,6 +48,10 @@ namespace emu {
u8 minor;
u8 micro;
bool dev_build;

inline constexpr bool EqualsExceptBuild(const Version &other) {
return (other.major == this->major) && (other.minor == this->minor) && (other.micro == this->micro);
}
};

bool IsAvailable();
Expand All @@ -56,21 +61,36 @@ namespace emu {

Version GetVersion();

void GetVirtualAmiiboDirectory(char *out_path, size_t out_path_size);
void GetVirtualAmiiboDirectory(char *out_path, const size_t out_path_size);

EmulationStatus GetEmulationStatus();
void SetEmulationStatus(EmulationStatus status);
void SetEmulationStatus(const EmulationStatus status);

Result GetActiveVirtualAmiibo(VirtualAmiiboData *out_amiibo_data, char *out_path, size_t out_path_size);
Result SetActiveVirtualAmiibo(char *path, size_t path_size);
Result SetActiveVirtualAmiibo(const char *path, const size_t path_size);
void ResetActiveVirtualAmiibo();

VirtualAmiiboStatus GetActiveVirtualAmiiboStatus();
void SetActiveVirtualAmiiboStatus(VirtualAmiiboStatus status);

void IsApplicationIdIntercepted(u64 app_id, bool *out_intercepted);
void IsCurrentApplicationIdIntercepted(bool *out_intercepted);

Result TryParseVirtualAmiibo(char *path, size_t path_size, VirtualAmiiboData *out_amiibo_data);
void SetActiveVirtualAmiiboStatus(const VirtualAmiiboStatus status);

bool IsApplicationIdIntercepted(const u64 app_id);

inline bool IsCurrentApplicationIdIntercepted() {
bool intercepted = false;
u64 process_id = 0;
if(R_SUCCEEDED(pmdmntGetApplicationProcessId(&process_id))) {
u64 program_id = 0;
if(R_SUCCEEDED(pmdmntGetProgramId(&program_id, process_id))) {
intercepted = IsApplicationIdIntercepted(program_id);
}
}
return intercepted;
}

Result TryParseVirtualAmiibo(const char *path, const size_t path_size, VirtualAmiiboData *out_amiibo_data);
Result GetActiveVirtualAmiiboAreas(VirtualAmiiboAreaEntry *out_area_buf, const size_t out_area_size, u32 *out_area_count);
Result GetActiveVirtualAmiiboCurrentArea(u32 *out_access_id);
Result SetActiveVirtualAmiiboCurrentArea(const u32 access_id);
Result SetActiveVirtualAmiiboUuidInfo(const VirtualAmiiboUuidInfo uuid_info);

}
82 changes: 0 additions & 82 deletions source/AmiigoUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,6 @@ extern PadState pad;
static bool isswiping;
emu::VirtualAmiiboData g_activevirtualamiibodata;

void rezize_PNG (string file_name, int limit) {
SDL_Surface* surface = IMG_Load(file_name.c_str());
//const int limit = limit;//512;
int width = surface->w;
int height = surface->h;

if ((width > limit) ||
(height > 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 < scale) {
scale = scaleH;
}

SDL_Rect targetDimensions;
targetDimensions.x = 0;
targetDimensions.y = 0;
targetDimensions.w = (int)(width * scale);
targetDimensions.h = (int)(height * scale);

// create a 32 bits per pixel surface to Blit the image to first, before BlitScaled
// https://stackoverflow.com/questions/33850453/sdl2-blit-scaled-from-a-palettized-8bpp-surface-gives-error-blit-combination/33944312
SDL_Surface *p32BPPSurface = SDL_CreateRGBSurface(
surface->flags,
sourceDimensions.w,
sourceDimensions.h,
32,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);

if (SDL_BlitSurface(surface, NULL, p32BPPSurface, NULL) < 0) {
printf("Error did not blit surface: %s\n", SDL_GetError());
}
else {
// create another 32 bits per pixel surface are the desired scale
SDL_Surface *pScaleSurface = SDL_CreateRGBSurface(
p32BPPSurface->flags,
targetDimensions.w,
targetDimensions.h,
p32BPPSurface->format->BitsPerPixel,
p32BPPSurface->format->Rmask,
p32BPPSurface->format->Gmask,
p32BPPSurface->format->Bmask,
p32BPPSurface->format->Amask);

// 32 bit per pixel surfaces (loaded from the original file) won't scale down with BlitScaled, suggestion to first fill the surface
// 8 and 24 bit depth pngs did not require this
// https://stackoverflow.com/questions/20587999/sdl-blitscaled-doesnt-work
SDL_FillRect(pScaleSurface, &targetDimensions, SDL_MapRGBA(pScaleSurface->format, 255, 0, 0, 255));

if (SDL_BlitScaled(p32BPPSurface, NULL, pScaleSurface, NULL) < 0) {
printf("Error did not scale surface: %s\n", SDL_GetError());

SDL_FreeSurface(pScaleSurface);
pScaleSurface = NULL;
}
else {
SDL_FreeSurface(surface);

surface = pScaleSurface;
width = surface->w;
height = surface->h;
}
}

SDL_FreeSurface(p32BPPSurface);
p32BPPSurface = NULL;
IMG_SavePNG(surface, file_name.c_str());
}
SDL_FreeSurface(surface);
}

AmiigoUI::AmiigoUI()
{
//Load the header font
Expand Down Expand Up @@ -360,7 +279,6 @@ void AmiigoUI::DrawUI()
//load the selected image
string ImgPath = std::string(ListDir)+ std::string(Files.at(list).d_name)+"/amiibo.png";
if(CheckFileExists(ImgPath)&(fsize(ImgPath) != 0)){
rezize_PNG(ImgPath,512);
SelAmiibo = IMG_Load(ImgPath.c_str());
}else{
if(CheckFileExists(std::string(ListDir)+ std::string(Files.at(list).d_name)+"/amiibo.json"))
Expand Down
17 changes: 12 additions & 5 deletions source/CreatorUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,10 @@ void CreatorUI::DrawUI()
PleaseWait("Please wait, Downloading...");
string icontemp = ImgPath+".temp";
RetrieveToFile(JData["amiibo"][IndexInJdata]["image"].get<std::string>(), icontemp);
if (fsize(icontemp) != 0) rename(icontemp.c_str(), ImgPath.c_str());
if (fsize(icontemp) != 0){
rename(icontemp.c_str(), ImgPath.c_str());
rezize_PNG(ImgPath,512);
}
}
imgres++;
DownPrev = 0;
Expand All @@ -319,7 +322,7 @@ void CreatorUI::DrawUI()
if (imgres != SeriesList->SelectedIndex && !isswiping)
{
imgres = SeriesList->SelectedIndex;
if(CheckFileExists(ImgPath)&(fsize(ImgPath) != 0)){PrevIcon = IMG_Load(ImgPath.c_str()); }else PrevIcon = IMG_Load("romfs:/download.png");
if(CheckFileExists(ImgPath)&(fsize(ImgPath) != 0)){PrevIcon = IMG_Load(ImgPath.c_str()); } else PrevIcon = IMG_Load("romfs:/download.png");
//printf("%s\n",ImgPath.c_str());
}

Expand Down Expand Up @@ -391,10 +394,14 @@ void CreatorUI::ListSelect()
if(!CheckFileExists(iconname)&HasConnection()){
RetrieveToFile(JData["amiibo"][IndexInJdata]["image"].get<std::string>(), icontemp);
if (fsize(icontemp) != 0){
copy_me(icontemp, iconDBex);
rename(icontemp.c_str(), iconname.c_str());
rezize_PNG(icontemp,512);
copy_me(icontemp, iconDBex);
rename(icontemp.c_str(), iconname.c_str());
}
}
} else {
rezize_PNG(iconDBex,512);
rezize_PNG(iconname,512);
}
imgres++;//refresh signal for preview
}
//Add the Amiibos from the selected series to the list
Expand Down
9 changes: 6 additions & 3 deletions source/Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,12 @@ void Scandownload(string folder)
} else if (HasConnection()){
printf("%s - Downloading %s\nTo %s\n",route.c_str(),iconURL.c_str(),iconAmii.c_str());
RetrieveToFile(iconURL, iconTemp);
if (fsize(iconTemp) != 0)
rename(iconTemp.c_str(), iconAmii.c_str());
copy_me(iconAmii, IconCache);
if (fsize(iconTemp) != 0){
rezize_PNG(iconTemp,512);
rename(iconTemp.c_str(), iconAmii.c_str());
copy_me(iconAmii, IconCache);

}
printf("Downloaded \n");
}
}//else printf("The icon exist %s OK\n",route.c_str());
Expand Down
1 change: 1 addition & 0 deletions source/UI.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL_image.h>
#include <string>
#include <vector>
#include <switch.h>
Expand Down
52 changes: 52 additions & 0 deletions source/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <chrono>
#include "Utils.h"
#include "ConvertBase.hpp"
#include <SDL_image.h>

using namespace std;

Expand Down Expand Up @@ -85,3 +86,54 @@ void toamii(std::string& ID, json JSID)
"02";
printf("AmiiboID: %s -\n",ID.c_str());
}

void rezize_PNG (string file_name, int limit) {
SDL_Surface* pSurface = IMG_Load(file_name.c_str());
//const int limit = limit;//512;
int width = pSurface->w;
int height = pSurface->h;

if ((width > limit) || (height > 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 < 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->flags,
targetDimensions.w,
targetDimensions.h,
pSurface->format->BitsPerPixel,
pSurface->format->Rmask,
pSurface->format->Gmask,
pSurface->format->Bmask,
pSurface->format->Amask);

if (SDL_BlitScaled(pSurface, NULL, pScaleSurface, &targetDimensions) < 0) {
printf("Error did not scale surface: %s\n", SDL_GetError());
SDL_FreeSurface(pScaleSurface);
pScaleSurface = NULL;
} else {
SDL_FreeSurface(pSurface);
pSurface = pScaleSurface;
width = pSurface->w;
height = pSurface->h;
IMG_SavePNG(pSurface, file_name.c_str());
}
}
SDL_FreeSurface(pSurface);
}
Loading

0 comments on commit 6fb345f

Please sign in to comment.