From bc8d609230b770760dd4604aeff924c1a7d9e477 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Sun, 7 Apr 2024 10:36:43 +0900 Subject: [PATCH] =?utf8?q?URL=E3=81=8B=E3=82=89=E7=94=BB=E5=83=8F=E3=83=95?= =?utf8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E9=96=8B=E3=81=91=E3=82=8B?= =?utf8?q?=E6=A7=98=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++ Makefile | 10 ++-- README.md | 51 ++++++++++++++++++ main.c | 143 +++++++++++++++++++++++++++++++++++---------------- 4 files changed, 160 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a8d580..640ffed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.4.0 +* URLから画像ファイルを開ける様に + # 0.3.0 * 負荷をすくなくするために画面の更新を減らした (たかしさん) diff --git a/Makefile b/Makefile index 95b9afa..3123103 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,16 @@ NAME=mivfx -VERSION=0.3.0 +VERSION=0.4.0 # Linux、Haiku、かIllumos = /usr、FreeBSDかOpenBSD = /usr/local、NetBSD = /usr/pkg -PREFIX=/usr +PREFIX=/usr/local CC=cc FILES=main.c CFLAGS=-Wall -Wextra -g -LDFLAGS=-lSDL2 -lSDL2_image +LIBS=-lSDL2 -lSDL2_image -lcurl +LDFLAGS=-L${PREFIX}/lib +CPPFLAGS=-I${PREFIX}/include all: - ${CC} ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} + ${CC} ${CFLAGS} ${CPPFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} ${LIBS} clean: rm -f ${NAME} diff --git a/README.md b/README.md index e4de789..1d0f12d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,53 @@ # mivfx - minimarist image view for x ミニマリストな画像ビューアー + +# インストールする方法 + +## OpenBSD +```sh +doas pkg_add sdl2 sdl2_image curl +make +doas make install +``` + +## FreeBSD +```sh +doas pkg install sdl2 sdl2_image curl +make +doas make install +``` + +## NetBSD +```sh +doas pkgin install SDL2-2.28.5 SDL2_image curl +make PREFIX=/usr/pkg +doas make install PREFIX=/usr/pkg +``` + +## CRUX +```sh +doas prt-get depinst libsdl2 sdl2_image curl +make PREFIX=/usr +doas make install PREFIX=/usr +``` + +## Void Linux +```sh +doas xbps-install -S SDL2 SDL2_image curl +make PREFIX=/usr +doas make install PREFIX=/usr +``` + +## Artix Linux +```sh +doas pacman -S sdl2 sdl2_image curl +make PREFIX=/usr +doas make install PREFIX=/usr +``` + +## Devuan GNU/Linux +```sh +doas apt install libsdl2-dev libsdl2-image-dev libcurl4 +make PREFIX=/usr +doas make install PREFIX=/usr +``` diff --git a/main.c b/main.c index d950680..3a6f7fc 100644 --- a/main.c +++ b/main.c @@ -2,18 +2,84 @@ #include #include #include +#include + #define DELAY_MS 50 +SDL_Window* window = NULL; +SDL_Renderer* renderer = NULL; +SDL_Texture* texture = NULL; +bool quit = false; + +bool dlfile(const char* url, const char* filename) { + CURL* curl = curl_easy_init(); + if (!curl) { + return false; + } + + FILE* file = fopen(filename, "wb"); + if (!file) { + return false; + } + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); + CURLcode res = curl_easy_perform(curl); + fclose(file); + + if (res != CURLE_OK) { + remove(filename); + return false; + } + + return true; +} + +void windowevent(SDL_Event e) { + if (e.type == SDL_QUIT) { + quit = true; + } else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) { + // ウィンドウのサイズが変わった場合 + int newWidth = e.window.data1; + int newHeight = e.window.data2; + + // 縦横比を変わらずに新しい大きさの算数 + /* float newAspectRatio = (float)newWidth / newHeight; */ + int scaledWidth, scaledHeight; + /* if (newAspectRatio > aspectRatio) { */ + // 画像よりウィンドウの方が広い場合 + scaledHeight = newHeight; + scaledWidth = newWidth; + //= (int)(scaledHeight * aspectRatio); + /* } else { */ + /* // 画像よりウィンドウの方が高い場合 */ + /* scaledWidth = newWidth; */ + /* scaledHeight = (int)(scaledWidth / aspectRatio); */ + /* } */ + + // テキスチャーのれんダーリングサイズの設定 + SDL_Rect renderQuad = { (newWidth - scaledWidth) / 2, (newHeight - scaledHeight) / 2, scaledWidth, scaledHeight }; + SDL_RenderCopy(renderer, texture, NULL, &renderQuad); + } else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_EXPOSED) { + // 再描画が必要な場合 + + // 画面の更新 + SDL_RenderClear(renderer); + + // テキスチャーの表示 + SDL_RenderCopy(renderer, texture, NULL, NULL); + + // 画面の更新 + SDL_RenderPresent(renderer); + } +} + int main(int argc, char* argv[]) { if (argc < 2) { printf("使用方法: %s <画像ファイル>\n", argv[0]); return 1; } - SDL_Window* window = NULL; - SDL_Renderer* renderer = NULL; - SDL_Texture* texture = NULL; - // SDL2とSDL2_imageの初期設定 if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDLを初期設定出来なかった:%s\n", SDL_GetError()); @@ -26,8 +92,25 @@ int main(int argc, char* argv[]) { return 1; } + const char* imgfile = argv[1]; + + // URLの場合、仮にダウンロードして + bool isurl = strncmp(imgfile, "http://", 7) == 0 || + strncmp(imgfile, "https://", 8) == 0; + char tmpname[] = "/tmp/netimg.png"; + if (isurl) { + if (!dlfile(imgfile, tmpname)) { + printf("画像をダウンロードに失敗。URL: %s\n", imgfile); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 1; + } + imgfile = tmpname; + } + // 画像の読込 - SDL_Surface* imgsurface = IMG_Load(argv[1]); + SDL_Surface* imgsurface = IMG_Load(imgfile); if (imgsurface == NULL) { printf("画像の読込に失敗:%s\n", IMG_GetError()); SDL_DestroyRenderer(renderer); @@ -59,7 +142,14 @@ int main(int argc, char* argv[]) { // ウィンドウの作成 SDL_SetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, "1"); - window = SDL_CreateWindow("mivfx", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); + window = SDL_CreateWindow( + "mivfx", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + windowWidth, + windowHeight, + SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE + ); if (window == NULL) { printf("ウィンドウの作成に失敗:%s\n", SDL_GetError()); SDL_Quit(); @@ -86,46 +176,10 @@ int main(int argc, char* argv[]) { // メインループ SDL_Event e; - bool quit = false; while (!quit) { // イベント while (SDL_PollEvent(&e) != 0) { - if (e.type == SDL_QUIT) { - quit = true; - } else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) { - // ウィンドウのサイズが変わった場合 - int newWidth = e.window.data1; - int newHeight = e.window.data2; - - // 縦横比を変わらずに新しい大きさの算数 - /* float newAspectRatio = (float)newWidth / newHeight; */ - int scaledWidth, scaledHeight; - /* if (newAspectRatio > aspectRatio) { */ - // 画像よりウィンドウの方が広い場合 - scaledHeight = newHeight; - scaledWidth = newWidth; - //= (int)(scaledHeight * aspectRatio); - /* } else { */ - /* // 画像よりウィンドウの方が高い場合 */ - /* scaledWidth = newWidth; */ - /* scaledHeight = (int)(scaledWidth / aspectRatio); */ - /* } */ - - // テキスチャーのれんダーリングサイズの設定 - SDL_Rect renderQuad = { (newWidth - scaledWidth) / 2, (newHeight - scaledHeight) / 2, scaledWidth, scaledHeight }; - SDL_RenderCopy(renderer, texture, NULL, &renderQuad); - } else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_EXPOSED) { - // 再描画が必要な場合 - - // 画面の更新 - SDL_RenderClear(renderer); - - // テキスチャーの表示 - SDL_RenderCopy(renderer, texture, NULL, NULL); - - // 画面の更新 - SDL_RenderPresent(renderer); - } + windowevent(e); } // 休ませる @@ -133,6 +187,9 @@ int main(int argc, char* argv[]) { } // 掃除 + if (isurl) { + remove(tmpname); + } SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); -- 2.43.0