Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
itdan3344
stb
提交
d54e7409
S
stb
项目概览
itdan3344
/
stb
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
stb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d54e7409
编写于
7月 21, 2014
作者:
J
Jorge Rodriguez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
stb_resample initial implementation
上级
bcefca10
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
263 addition
and
0 deletion
+263
-0
stb_resample.h
stb_resample.h
+214
-0
tests/resample_test.c
tests/resample_test.c
+49
-0
未找到文件。
stb_resample.h
0 → 100644
浏览文件 @
d54e7409
/* stb_resample - v0.1 - public domain image resampling
no warranty implied; use at your own risk
Do this:
#define STB_RESAMPLE_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
#define STBR_ASSERT(x) to avoid using assert.h.
Latest revisions:
See end of file for full revision history.
Initial implementation by Jorge L Rodriguez
*/
#ifndef STBR_INCLUDE_STB_RESAMPLE_H
#define STBR_INCLUDE_STB_RESAMPLE_H
// Basic usage:
// unsigned char *data = stbr_resize(input_data, input_w, input_h, input_components, STBR_FILTER_NEAREST, output_data, output_w, output_h);
//
// input_data is your supplied texels.
// output_data will be the resized texels. It should be of size output_w * output_h * input_components.
// Returned data == output_data, for convenience, or 0 in case of an error.
//
//
typedef
enum
{
STBR_FILTER_NEAREST
=
1
,
}
stbr_filter
;
typedef
unsigned
char
stbr_uc
;
#ifdef __cplusplus
extern
"C"
{
#endif
#ifdef STB_RESAMPLE_STATIC
#define STBRDEF static
#else
#define STBRDEF extern
#endif
//////////////////////////////////////////////////////////////////////////////
//
// PRIMARY API - resize an image
//
STBRDEF
stbr_uc
*
stbr_resize
(
const
stbr_uc
*
input_data
,
int
input_w
,
int
input_h
,
int
input_components
,
stbr_filter
filter
,
stbr_uc
*
output_data
,
int
output_w
,
int
output_h
);
#ifdef __cplusplus
}
#endif
//
//
//// end header file /////////////////////////////////////////////////////
#endif // STBR_INCLUDE_STB_RESAMPLE_H
#ifdef STB_RESIZE_IMPLEMENTATION
#ifndef STBR_ASSERT
#include <assert.h>
#define STBR_ASSERT(x) assert(x)
#endif
#ifdef STBR_DEBUG_OVERWRITE_TEST
#include <string.h>
#endif
// For size_t
#include <stdlib.h>
#ifndef _MSC_VER
#ifdef __cplusplus
#define stbr_inline inline
#else
#define stbr_inline
#endif
#else
#define stbr_inline __forceinline
#endif
#ifdef _MSC_VER
typedef
unsigned
short
stbr__uint16
;
typedef
signed
short
stbr__int16
;
typedef
unsigned
int
stbr__uint32
;
typedef
signed
int
stbr__int32
;
#else
#include <stdint.h>
typedef
uint16_t
stbr__uint16
;
typedef
int16_t
stbr__int16
;
typedef
uint32_t
stbr__uint32
;
typedef
int32_t
stbr__int32
;
#endif
// should produce compiler error if size is wrong
typedef
unsigned
char
stbr__validate_uint32
[
sizeof
(
stbr__uint32
)
==
4
?
1
:
-
1
];
#ifdef _MSC_VER
#define STBR_NOTUSED(v) (void)(v)
#else
#define STBR_NOTUSED(v) (void)sizeof(v)
#endif
// i0 is a texel in [0, n0-1]
// What's the nearest texel center to i0's center in [0, n1-1] ?
// Remapping [0, n0-1] to [0, n1-1] gives (i0 + 0.5)*n1/n0 but we want to avoid
// floating point math so we rearrange it as (n1*i0 + n1/2)/n0
stbr_inline
static
int
stbr__nearest_texel
(
int
i0
,
int
n0
,
int
n1
)
{
return
(
n1
*
i0
+
n1
/
2
)
/
n0
;
}
stbr_inline
static
size_t
stbr__texel_index
(
int
x
,
int
y
,
int
c
,
int
width_stride
,
int
num_c
,
int
w
,
int
h
)
{
STBR_ASSERT
(
x
>=
0
&&
x
<
w
);
STBR_ASSERT
(
y
>=
0
&&
y
<
h
);
return
y
*
width_stride
+
x
*
num_c
+
c
;
}
static
void
stbr__filter_nearest_1
(
const
stbr_uc
*
input_data
,
stbr_uc
*
output_data
,
size_t
input_texel_index
,
size_t
output_texel_index
,
size_t
n
)
{
output_data
[
output_texel_index
]
=
input_data
[
input_texel_index
];
}
static
void
stbr__filter_nearest_3
(
const
stbr_uc
*
input_data
,
stbr_uc
*
output_data
,
size_t
input_texel_index
,
size_t
output_texel_index
,
size_t
n
)
{
output_data
[
output_texel_index
]
=
input_data
[
input_texel_index
];
output_data
[
output_texel_index
+
1
]
=
input_data
[
input_texel_index
+
1
];
output_data
[
output_texel_index
+
2
]
=
input_data
[
input_texel_index
+
2
];
}
static
void
stbr__filter_nearest_4
(
const
stbr_uc
*
input_data
,
stbr_uc
*
output_data
,
size_t
input_texel_index
,
size_t
output_texel_index
,
size_t
n
)
{
output_data
[
output_texel_index
]
=
input_data
[
input_texel_index
];
output_data
[
output_texel_index
+
1
]
=
input_data
[
input_texel_index
+
1
];
output_data
[
output_texel_index
+
2
]
=
input_data
[
input_texel_index
+
2
];
output_data
[
output_texel_index
+
3
]
=
input_data
[
input_texel_index
+
3
];
}
static
void
stbr__filter_nearest_n
(
const
stbr_uc
*
input_data
,
stbr_uc
*
output_data
,
size_t
input_texel_index
,
size_t
output_texel_index
,
size_t
n
)
{
size_t
c
;
for
(
c
=
0
;
c
<
n
;
c
++
)
output_data
[
output_texel_index
+
c
]
=
input_data
[
input_texel_index
+
c
];
}
typedef
void
(
stbr__filter_fn
)(
const
stbr_uc
*
input_data
,
stbr_uc
*
output_data
,
size_t
input_texel_index
,
size_t
output_texel_index
,
size_t
n
);
STBRDEF
stbr_uc
*
stbr_resize
(
const
stbr_uc
*
input_data
,
int
input_w
,
int
input_h
,
int
input_components
,
stbr_filter
filter
,
stbr_uc
*
output_data
,
int
output_w
,
int
output_h
)
{
int
x
,
y
;
int
width_stride_input
=
input_components
*
input_w
;
int
width_stride_output
=
input_components
*
output_w
;
#ifdef STBR_DEBUG_OVERWRITE_TEST
#define OVERWRITE_ARRAY_SIZE 64
unsigned
char
overwrite_contents_pre
[
OVERWRITE_ARRAY_SIZE
];
memcpy
(
overwrite_contents_pre
,
&
output_data
[
output_w
*
output_h
*
input_components
],
OVERWRITE_ARRAY_SIZE
);
#endif
if
(
filter
==
STBR_FILTER_NEAREST
)
{
stbr__filter_fn
*
filter_fn
;
filter_fn
=
&
stbr__filter_nearest_n
;
if
(
input_components
==
1
)
filter_fn
=
&
stbr__filter_nearest_1
;
else
if
(
input_components
==
3
)
filter_fn
=
&
stbr__filter_nearest_3
;
else
if
(
input_components
==
4
)
filter_fn
=
&
stbr__filter_nearest_4
;
for
(
y
=
0
;
y
<
output_h
;
y
++
)
{
int
nearest_y
=
stbr__nearest_texel
(
y
,
output_h
,
input_h
);
for
(
x
=
0
;
x
<
output_w
;
x
++
)
{
int
nearest_x
=
stbr__nearest_texel
(
x
,
output_w
,
input_w
);
size_t
input_texel_index
=
stbr__texel_index
(
nearest_x
,
nearest_y
,
0
,
width_stride_input
,
input_components
,
input_w
,
input_h
);
size_t
output_texel_index
=
stbr__texel_index
(
x
,
y
,
0
,
width_stride_output
,
input_components
,
output_w
,
output_h
);
filter_fn
(
input_data
,
output_data
,
input_texel_index
,
output_texel_index
,
input_components
);
}
}
}
else
return
0
;
#ifdef STBR_DEBUG_OVERWRITE_TEST
STBR_ASSERT
(
memcmp
(
overwrite_contents_pre
,
&
output_data
[
output_w
*
output_h
*
input_components
],
OVERWRITE_ARRAY_SIZE
)
==
0
);
#endif
return
output_data
;
}
#endif // STB_RESAMPLE_IMPLEMENTATION
/*
revision history:
*/
tests/resample_test.c
0 → 100644
浏览文件 @
d54e7409
#ifdef _WIN32
#define STBR_ASSERT(x) \
if (!(x)) \
__debugbreak();
#endif
#define STB_RESAMPLE_IMPLEMENTATION
#include "stb_resample.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int
main
(
int
argc
,
char
**
argv
)
{
unsigned
char
*
input_data
;
unsigned
char
*
output_data
;
int
w
,
h
;
int
n
;
int
out_w
,
out_h
;
if
(
argc
<=
1
)
{
printf
(
"No input image
\n
"
);
return
1
;
}
input_data
=
stbi_load
(
argv
[
1
],
&
w
,
&
h
,
&
n
,
0
);
if
(
!
input_data
)
{
printf
(
"Input image could not be loaded"
);
return
1
;
}
out_w
=
512
;
out_h
=
512
;
output_data
=
malloc
(
out_w
*
out_h
*
n
);
stbr_resize
(
input_data
,
w
,
h
,
n
,
STBR_FILTER_NEAREST
,
output_data
,
out_w
,
out_h
);
stbi_write_png
(
"output.png"
,
out_w
,
out_h
,
n
,
output_data
,
out_w
*
n
);
free
(
output_data
);
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录