fszontagh
2024-02-25 5d543bcc0d15b871315a2123aec01041d73c53fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef __ImageViewerImageWindow__
#define __ImageViewerImageWindow__
 
/**
@file
Subclass of ImageWindow, which is generated by wxFormBuilder.
*/
 
#include "ImageViewer.h"
 
//// end generated include
#include <map>
#include <iostream>
#include "../res/app_icon.h"
#include "../utils.h"
 
/** Implementing ImageWindow */
class ImageViewerImageWindow : public ImageWindow
{
protected:
    // Handlers for ImageWindow events.
    void OnDropFile(wxDropFilesEvent &event);
    void OnWindowKeyUp(wxKeyEvent &event);
    void OnWindowMouseWheel(wxMouseEvent &event);
    void WindowOnSize(wxSizeEvent &event);
    void OnBitmapDoubleLeftClick(wxMouseEvent &event);
    void OnListItemSelected(wxListEvent &event);
    void OnRotateLeftClicked(wxCommandEvent &event);
    void OnRotateRightClicked(wxCommandEvent &event);
    void OnZoomInClicked(wxCommandEvent &event);
    void OnZoomResetClicked(wxCommandEvent &event);
    void OnZoomOutClicked(wxCommandEvent &event);
    void OnShowFullScreenClick(wxCommandEvent &event);
 
public:
    /** Constructor */
    ImageViewerImageWindow(wxWindow *parent);
    //// end generated class members
 
private:
    wxString windowTitle;
    /// @brief int imagelist id, wxImage the image
    std::map<int, ImageUtils::ImageFileInfo *> images;
    wxImage currentVisibleImage;
    wxImage currentOriginalImage;
    wxImageList *imgList;
    int currentZoom = 100;
    void ShowImage(wxImage img);
    wxImage FitImage(wxImage img, int targetWidth, int targetHeight);
    wxImage RotateImage(ImageUtils::ROTATE_DIRECTION dir, wxImage img);
    void ZoomImage(int factor);
 
    inline wxImage cropResizeImage(const wxImage &originalImage, int targetWidth, int targetHeight)
    {
        int originalWidth = originalImage.GetWidth();
        int originalHeight = originalImage.GetHeight();
 
        double aspectRatio = static_cast<double>(originalWidth) / static_cast<double>(originalHeight);
        int newWidth = targetWidth;
        int newHeight = targetHeight;
 
        // Kiszámítjuk az új méreteket, hogy megtartsuk a képarányt
        if (originalWidth > targetWidth || originalHeight > targetHeight)
        {
            if (aspectRatio > 1.0)
            {
                // Szélesség alapján skálázzuk az új méretet
                newWidth = targetWidth;
                newHeight = static_cast<int>(targetWidth / aspectRatio);
            }
            else
            {
                // Magasság alapján skálázzuk az új méretet
                newHeight = targetHeight;
                newWidth = static_cast<int>(targetHeight * aspectRatio);
            }
        }
 
        // Méretezzük az eredeti képet az új méretekre
        wxImage resizedImage = originalImage.Scale(newWidth, newHeight);
 
        // Üres terület hozzáadása és transzparens töltése
        if (newWidth < targetWidth || newHeight < targetHeight)
        {
            wxImage finalImage(targetWidth, targetHeight);
            finalImage.SetAlpha();
            finalImage.Paste(resizedImage, (targetWidth - newWidth) / 2, (targetHeight - newHeight) / 2);
            return finalImage;
        }
 
        return resizedImage;
    }
};
 
#endif // __ImageViewerImageWindow__