File: RGBAImage.cpp

package info (click to toggle)
perceptualdiff 1.2-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,008 kB
  • ctags: 76
  • sloc: cpp: 948; sh: 100; makefile: 50
file content (162 lines) | stat: -rw-r--r-- 4,406 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
RGBAImage.cpp
Copyright (C) 2006-2011 Yangli Hector Yee
Copyright (C) 2011-2014 Steven Myint

(This entire file was rewritten by Jim Tilander)

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "RGBAImage.h"
#include "FreeImage.h"

#include <iostream>
#include <string>
#include <cstring>
#include <cassert>


static FIBITMAP *ToFreeImage(const RGBAImage &image)
{
    const auto *data = image.Get_Data();

    auto bitmap = FreeImage_Allocate(image.Get_Width(), image.Get_Height(), 32,
                                     0x000000ff, 0x0000ff00, 0x00ff0000);
    assert(bitmap);

    for (auto y = 0u; y < image.Get_Height();
         y++, data += image.Get_Width())
    {
        auto scanline = reinterpret_cast<unsigned int *>(
            FreeImage_GetScanLine(bitmap, image.Get_Height() - y - 1));
        memcpy(scanline, data, sizeof(data[0]) * image.Get_Width());
    }

    return bitmap;
}


static std::shared_ptr<RGBAImage> ToRGBAImage(FIBITMAP *image,
                                              const std::string &filename = "")
{
    const auto w = FreeImage_GetWidth(image);
    const auto h = FreeImage_GetHeight(image);

    auto result = std::make_shared<RGBAImage>(w, h, filename);
    // Copy the image over to our internal format, FreeImage has the scanlines
    // bottom to top though.
    auto dest = result->Get_Data();
    for (unsigned int y = 0; y < h; y++, dest += w)
    {
        const auto scanline = reinterpret_cast<const unsigned int *>(
            FreeImage_GetScanLine(image, h - y - 1));
        memcpy(dest, scanline, sizeof(dest[0]) * w);
    }

    return result;

}

std::shared_ptr<RGBAImage> RGBAImage::DownSample(unsigned int w,
                                                 unsigned int h) const
{
    if (w == 0)
    {
        w = Width / 2;
    }

    if (h == 0)
    {
        h = Height / 2;
    }

    if (Width <= 1 or Height <= 1)
    {
        return nullptr;
    }
    if (Width == w and Height == h)
    {
        return nullptr;
    }
    assert(w <= Width);
    assert(h <= Height);

    auto bitmap = ToFreeImage(*this);
    auto converted = FreeImage_Rescale(bitmap, w, h, FILTER_BICUBIC);

    FreeImage_Unload(bitmap);
    bitmap = nullptr;

    auto img = ToRGBAImage(converted, Name);

    FreeImage_Unload(converted);

    return img;
}

bool RGBAImage::WriteToFile(const std::string &filename) const
{
    const auto fileType = FreeImage_GetFIFFromFilename(filename.c_str());
    if (FIF_UNKNOWN == fileType)
    {
        std::cerr << "Can't save to unknown filetype " << filename
                  << std::endl;
        return false;
    }

    auto bitmap = ToFreeImage(*this);

    FreeImage_SetTransparent(bitmap, false);
    auto converted = FreeImage_ConvertTo24Bits(bitmap);

    const bool result =
        !!FreeImage_Save(fileType, converted, filename.c_str());
    if (not result)
    {
        std::cerr << "Failed to save to " << filename << std::endl;
    }

    FreeImage_Unload(converted);
    FreeImage_Unload(bitmap);
    return result;
}

std::shared_ptr<RGBAImage> RGBAImage::ReadFromFile(const std::string &filename)
{
    const auto fileType = FreeImage_GetFileType(filename.c_str());
    if (FIF_UNKNOWN == fileType)
    {
        std::cerr << "Unknown filetype " << filename << std::endl;
        return nullptr;
    }

    FIBITMAP *freeImage = nullptr;
    if (auto temporary = FreeImage_Load(fileType, filename.c_str(), 0))
    {
        freeImage = FreeImage_ConvertTo32Bits(temporary);
        FreeImage_Unload(temporary);
    }
    if (not freeImage)
    {
        std::cerr << "Failed to load the image " << filename << std::endl;
        return nullptr;
    }

    auto result = ToRGBAImage(freeImage);

    FreeImage_Unload(freeImage);

    return result;
}