Skip to content

Commit 1b504cc

Browse files
committed
Implements Scale_Surface.
1 parent d47bd90 commit 1b504cc

2 files changed

Lines changed: 176 additions & 0 deletions

File tree

src/core/surfacescale.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*******************************************************************************
2+
/* O P E N S O U R C E -- V I N I F E R A **
3+
/*******************************************************************************
4+
*
5+
* @project Vinifera
6+
*
7+
* @file SURFACESCALE.CPP
8+
*
9+
* @author CCHyper
10+
*
11+
* @brief
12+
*
13+
* @license Vinifera is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU General Public License
15+
* as published by the Free Software Foundation, either version
16+
* 3 of the License, or (at your option) any later version.
17+
*
18+
* Vinifera is distributed in the hope that it will be
19+
* useful, but WITHOUT ANY WARRANTY; without even the implied
20+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21+
* PURPOSE. See the GNU General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU General Public
24+
* License along with this program.
25+
* If not, see <http://www.gnu.org/licenses/>.
26+
*
27+
******************************************************************************/
28+
#include "surfacescale.h"
29+
#include "vinifera_util.h"
30+
#include "xsurface.h"
31+
#include "vnImagine.h"
32+
33+
34+
/**
35+
* The scaling is performed with the use of the Image-Resampler library;
36+
* https://github.com/ramenhut/image-resampler
37+
*/
38+
39+
40+
/**
41+
* Scales an input surface to fit the destination surface using the Image-Resampler library.
42+
*
43+
* @author: CCHyper
44+
*/
45+
static bool Scale_Surface_ImageResampler(XSurface *src, XSurface *dst, VN_IMAGE_KERNEL_TYPE kernel)
46+
{
47+
if (!src || !dst) {
48+
return false;
49+
}
50+
51+
int src_width = src->Get_Width();
52+
int src_height = src->Get_Height();
53+
int src_bpp = src->Get_Bytes_Per_Pixel();
54+
55+
int dst_width = dst->Get_Width();
56+
int dst_height = dst->Get_Height();
57+
int dst_bpp = dst->Get_Bytes_Per_Pixel();
58+
59+
if (src_bpp != 2 || dst_bpp != 2) {
60+
return false;
61+
}
62+
63+
unsigned char *src_buff = (unsigned char *)src->Lock();
64+
unsigned char *dst_buff = (unsigned char *)dst->Lock();
65+
66+
if (!src_buff || !dst_buff) {
67+
src->Unlock();
68+
dst->Unlock();
69+
return false;
70+
}
71+
72+
CVImage source_image;
73+
CVImage resampled_image;
74+
75+
VN_IMAGE_FORMAT format = VN_IMAGE_FORMAT_R5G6B5; // 16-bit RGB
76+
77+
/**
78+
* Create a new image buffer, this will be where the source image data will be stored.
79+
*/
80+
if (VN_FAILED(vnCreateImage(format, src_width, src_height, &source_image))) {
81+
return false;
82+
}
83+
84+
/**
85+
* Copy the source image data into the new image buffer.
86+
*/
87+
std::memcpy(source_image.QueryData(), src_buff, src_width*src_height*src_bpp);
88+
89+
/**
90+
* Resize the source image to fit the destination surface.
91+
*/
92+
if (VN_FAILED(vnResizeImage(source_image, kernel, dst_width, dst_height, 0, &resampled_image))) {
93+
return false;
94+
}
95+
96+
/**
97+
* Now copy the resized image data back into the destination surface.
98+
*/
99+
std::memcpy(dst_buff, resampled_image.QueryData(), dst_width*dst_height*dst_bpp);
100+
101+
src->Unlock();
102+
dst->Unlock();
103+
104+
return true;
105+
}
106+
107+
108+
/**
109+
* Scales an input surface to fit the destination surface using various algorithms.
110+
*
111+
* @author: CCHyper
112+
*/
113+
bool Scale_Surface_Nearest(XSurface *src, XSurface *dst)
114+
{
115+
return Scale_Surface_ImageResampler(src, dst, VN_IMAGE_KERNEL_NEAREST);
116+
}
117+
118+
bool Scale_Surface_Bilinear(XSurface *src, XSurface *dst)
119+
{
120+
return Scale_Surface_ImageResampler(src, dst, VN_IMAGE_KERNEL_BILINEAR);
121+
}
122+
123+
bool Scale_Surface_Bicubic(XSurface *src, XSurface *dst)
124+
{
125+
return Scale_Surface_ImageResampler(src, dst, VN_IMAGE_KERNEL_BICUBIC);
126+
}
127+
128+
bool Scale_Surface_Cardinal(XSurface *src, XSurface *dst)
129+
{
130+
return Scale_Surface_ImageResampler(src, dst, VN_IMAGE_KERNEL_CARDINAL);
131+
}
132+
133+
bool Scale_Surface_Lanczos(XSurface *src, XSurface *dst)
134+
{
135+
return Scale_Surface_ImageResampler(src, dst, VN_IMAGE_KERNEL_LANCZOS3);
136+
}

src/core/surfacescale.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************************
2+
/* O P E N S O U R C E -- V I N I F E R A **
3+
/*******************************************************************************
4+
*
5+
* @project Vinifera
6+
*
7+
* @file SURFACESCALE.H
8+
*
9+
* @author CCHyper
10+
*
11+
* @brief
12+
*
13+
* @license Vinifera is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU General Public License
15+
* as published by the Free Software Foundation, either version
16+
* 3 of the License, or (at your option) any later version.
17+
*
18+
* Vinifera is distributed in the hope that it will be
19+
* useful, but WITHOUT ANY WARRANTY; without even the implied
20+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21+
* PURPOSE. See the GNU General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU General Public
24+
* License along with this program.
25+
* If not, see <http://www.gnu.org/licenses/>.
26+
*
27+
******************************************************************************/
28+
#pragma once
29+
30+
#include <always.h>
31+
32+
33+
class XSurface;
34+
35+
36+
bool Scale_Surface_Nearest(XSurface *src, XSurface *dst);
37+
bool Scale_Surface_Bilinear(XSurface *src, XSurface *dst);
38+
bool Scale_Surface_Bicubic(XSurface *src, XSurface *dst);
39+
bool Scale_Surface_Cardinal(XSurface *src, XSurface *dst);
40+
bool Scale_Surface_Lanczos(XSurface *src, XSurface *dst);

0 commit comments

Comments
 (0)