Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
itdan3344
stb
提交
3d7b4251
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,体验更适合开发者的 AI 搜索 >>
提交
3d7b4251
编写于
1月 29, 2018
作者:
S
Sean Barrett
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'png-ext-write' of
https://github.com/akx/stb
Conflicts: stb_image_write.h
上级
b48101c9
2c7b00ac
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
86 addition
and
53 deletion
+86
-53
stb_image_write.h
stb_image_write.h
+86
-53
未找到文件。
stb_image_write.h
浏览文件 @
3d7b4251
...
@@ -57,6 +57,12 @@ USAGE:
...
@@ -57,6 +57,12 @@ USAGE:
where the callback is:
where the callback is:
void stbi_write_func(void *context, void *data, int size);
void stbi_write_func(void *context, void *data, int size);
You can configure it with these global variables:
int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode
You can define STBI_WRITE_NO_STDIO to disable the file variant of these
You can define STBI_WRITE_NO_STDIO to disable the file variant of these
functions, so the library will not use stdio.h at all. However, this will
functions, so the library will not use stdio.h at all. However, this will
also disable HDR writing, because it requires stdio for formatted output.
also disable HDR writing, because it requires stdio for formatted output.
...
@@ -108,6 +114,7 @@ CREDITS:
...
@@ -108,6 +114,7 @@ CREDITS:
Emmanuel Julien - initial file IO callback implementation
Emmanuel Julien - initial file IO callback implementation
Jon Olick - original jo_jpeg.cpp code
Jon Olick - original jo_jpeg.cpp code
Daniel Gibson - integrate JPEG, allow external zlib
Daniel Gibson - integrate JPEG, allow external zlib
Aarni Koskela - allow choosing PNG filter
bugfixes:
bugfixes:
github:Chribba
github:Chribba
...
@@ -138,10 +145,12 @@ extern "C" {
...
@@ -138,10 +145,12 @@ extern "C" {
#define STBIWDEF static
#define STBIWDEF static
#else
#else
#define STBIWDEF extern
#define STBIWDEF extern
extern
int
stbi_write_tga_with_rle
;
extern
int
stbi_write_png_level
;
#endif
#endif
STBIWDEF
int
stbi_write_tga_with_rle
;
STBIWDEF
int
stbi_write_png_comperssion_level
;
STBIWDEF
int
stbi_write_force_png_filter
;
#ifndef STBI_WRITE_NO_STDIO
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF
int
stbi_write_png
(
char
const
*
filename
,
int
w
,
int
h
,
int
comp
,
const
void
*
data
,
int
stride_in_bytes
);
STBIWDEF
int
stbi_write_png
(
char
const
*
filename
,
int
w
,
int
h
,
int
comp
,
const
void
*
data
,
int
stride_in_bytes
);
STBIWDEF
int
stbi_write_bmp
(
char
const
*
filename
,
int
w
,
int
h
,
int
comp
,
const
void
*
data
);
STBIWDEF
int
stbi_write_bmp
(
char
const
*
filename
,
int
w
,
int
h
,
int
comp
,
const
void
*
data
);
...
@@ -158,7 +167,7 @@ STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w,
...
@@ -158,7 +167,7 @@ STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w,
STBIWDEF
int
stbi_write_hdr_to_func
(
stbi_write_func
*
func
,
void
*
context
,
int
w
,
int
h
,
int
comp
,
const
float
*
data
);
STBIWDEF
int
stbi_write_hdr_to_func
(
stbi_write_func
*
func
,
void
*
context
,
int
w
,
int
h
,
int
comp
,
const
float
*
data
);
STBIWDEF
int
stbi_write_jpg_to_func
(
stbi_write_func
*
func
,
void
*
context
,
int
x
,
int
y
,
int
comp
,
const
void
*
data
,
int
quality
);
STBIWDEF
int
stbi_write_jpg_to_func
(
stbi_write_func
*
func
,
void
*
context
,
int
x
,
int
y
,
int
comp
,
const
void
*
data
,
int
quality
);
extern
void
stbi_flip_vertically_on_write
(
int
flip_boolean
);
STBIWDEF
void
stbi_flip_vertically_on_write
(
int
flip_boolean
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
@@ -217,9 +226,19 @@ extern void stbi_flip_vertically_on_write(int flip_boolean);
...
@@ -217,9 +226,19 @@ extern void stbi_flip_vertically_on_write(int flip_boolean);
#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
#ifdef STB_IMAGE_WRITE_STATIC
static
stbi__flip_vertically_on_write
=
0
;
static
int
stbi_write_png_compression
level
=
8
;
static
int
stbi_write_tga_with_rle
=
1
;
static
int
stbi_write_force_png_filter
=
-
1
;
#else
int
stbi_write_png_compression_level
=
8
;
int
stbi__flip_vertically_on_write
=
0
;
int
stbi__flip_vertically_on_write
=
0
;
int
stbi_write_tga_with_rle
=
1
;
int
stbi_write_force_png_filter
=
-
1
;
#endif
void
stbi_flip_vertically_on_write
(
int
flag
)
STBIWDEF
void
stbi_flip_vertically_on_write
(
int
flag
)
{
{
stbi__flip_vertically_on_write
=
flag
;
stbi__flip_vertically_on_write
=
flag
;
}
}
...
@@ -261,12 +280,6 @@ static void stbi__end_write_file(stbi__write_context *s)
...
@@ -261,12 +280,6 @@ static void stbi__end_write_file(stbi__write_context *s)
typedef
unsigned
int
stbiw_uint32
;
typedef
unsigned
int
stbiw_uint32
;
typedef
int
stb_image_write_test
[
sizeof
(
stbiw_uint32
)
==
4
?
1
:
-
1
];
typedef
int
stb_image_write_test
[
sizeof
(
stbiw_uint32
)
==
4
?
1
:
-
1
];
#ifdef STB_IMAGE_WRITE_STATIC
static
int
stbi_write_tga_with_rle
=
1
;
#else
int
stbi_write_tga_with_rle
=
1
;
#endif
static
void
stbiw__writefv
(
stbi__write_context
*
s
,
const
char
*
fmt
,
va_list
v
)
static
void
stbiw__writefv
(
stbi__write_context
*
s
,
const
char
*
fmt
,
va_list
v
)
{
{
while
(
*
fmt
)
{
while
(
*
fmt
)
{
...
@@ -947,68 +960,88 @@ static unsigned char stbiw__paeth(int a, int b, int c)
...
@@ -947,68 +960,88 @@ static unsigned char stbiw__paeth(int a, int b, int c)
return
STBIW_UCHAR
(
c
);
return
STBIW_UCHAR
(
c
);
}
}
#ifdef STB_IMAGE_WRITE_STATIC
static
int
stbi_write_png_level
=
8
;
#else
int
stbi_write_png_level
=
8
;
#endif
// @OPTIMIZE: provide an option that always forces left-predict or paeth predict
// @OPTIMIZE: provide an option that always forces left-predict or paeth predict
static
void
stbiw__encode_png_line
(
unsigned
char
*
pixels
,
int
stride_bytes
,
int
width
,
int
height
,
int
y
,
int
n
,
int
filter_type
,
signed
char
*
line_buffer
)
{
static
int
mapping
[]
=
{
0
,
1
,
2
,
3
,
4
};
static
int
firstmap
[]
=
{
0
,
1
,
0
,
5
,
6
};
int
*
mymap
=
(
y
!=
0
)
?
mapping
:
firstmap
;
int
i
;
int
type
=
mymap
[
filter_type
];
unsigned
char
*
z
=
pixels
+
stride_bytes
*
(
stbi__flip_vertically_on_write
?
height
-
1
-
y
:
y
);
for
(
i
=
0
;
i
<
n
;
++
i
)
{
switch
(
type
)
{
case
0
:
line_buffer
[
i
]
=
z
[
i
];
break
;
case
1
:
line_buffer
[
i
]
=
z
[
i
];
break
;
case
2
:
line_buffer
[
i
]
=
z
[
i
]
-
z
[
i
-
stride_bytes
];
break
;
case
3
:
line_buffer
[
i
]
=
z
[
i
]
-
(
z
[
i
-
stride_bytes
]
>>
1
);
break
;
case
4
:
line_buffer
[
i
]
=
(
signed
char
)
(
z
[
i
]
-
stbiw__paeth
(
0
,
z
[
i
-
stride_bytes
],
0
));
break
;
case
5
:
line_buffer
[
i
]
=
z
[
i
];
break
;
case
6
:
line_buffer
[
i
]
=
z
[
i
];
break
;
}
}
for
(
i
=
n
;
i
<
width
*
n
;
++
i
)
{
switch
(
type
)
{
case
0
:
line_buffer
[
i
]
=
z
[
i
];
break
;
case
1
:
line_buffer
[
i
]
=
z
[
i
]
-
z
[
i
-
n
];
break
;
case
2
:
line_buffer
[
i
]
=
z
[
i
]
-
z
[
i
-
stride_bytes
];
break
;
case
3
:
line_buffer
[
i
]
=
z
[
i
]
-
((
z
[
i
-
n
]
+
z
[
i
-
stride_bytes
])
>>
1
);
break
;
case
4
:
line_buffer
[
i
]
=
z
[
i
]
-
stbiw__paeth
(
z
[
i
-
n
],
z
[
i
-
stride_bytes
],
z
[
i
-
stride_bytes
-
n
]);
break
;
case
5
:
line_buffer
[
i
]
=
z
[
i
]
-
(
z
[
i
-
n
]
>>
1
);
break
;
case
6
:
line_buffer
[
i
]
=
z
[
i
]
-
stbiw__paeth
(
z
[
i
-
n
],
0
,
0
);
break
;
}
}
}
unsigned
char
*
stbi_write_png_to_mem
(
unsigned
char
*
pixels
,
int
stride_bytes
,
int
x
,
int
y
,
int
n
,
int
*
out_len
)
unsigned
char
*
stbi_write_png_to_mem
(
unsigned
char
*
pixels
,
int
stride_bytes
,
int
x
,
int
y
,
int
n
,
int
*
out_len
)
{
{
int
force_filter
=
stbi_write_force_png_filter
;
int
ctype
[
5
]
=
{
-
1
,
0
,
4
,
2
,
6
};
int
ctype
[
5
]
=
{
-
1
,
0
,
4
,
2
,
6
};
unsigned
char
sig
[
8
]
=
{
137
,
80
,
78
,
71
,
13
,
10
,
26
,
10
};
unsigned
char
sig
[
8
]
=
{
137
,
80
,
78
,
71
,
13
,
10
,
26
,
10
};
unsigned
char
*
out
,
*
o
,
*
filt
,
*
zlib
;
unsigned
char
*
out
,
*
o
,
*
filt
,
*
zlib
;
signed
char
*
line_buffer
;
signed
char
*
line_buffer
;
int
i
,
j
,
k
,
p
,
zlen
;
int
j
,
zlen
;
if
(
stride_bytes
==
0
)
if
(
stride_bytes
==
0
)
stride_bytes
=
x
*
n
;
stride_bytes
=
x
*
n
;
if
(
force_filter
>=
5
)
{
force_filter
=
-
1
;
}
filt
=
(
unsigned
char
*
)
STBIW_MALLOC
((
x
*
n
+
1
)
*
y
);
if
(
!
filt
)
return
0
;
filt
=
(
unsigned
char
*
)
STBIW_MALLOC
((
x
*
n
+
1
)
*
y
);
if
(
!
filt
)
return
0
;
line_buffer
=
(
signed
char
*
)
STBIW_MALLOC
(
x
*
n
);
if
(
!
line_buffer
)
{
STBIW_FREE
(
filt
);
return
0
;
}
line_buffer
=
(
signed
char
*
)
STBIW_MALLOC
(
x
*
n
);
if
(
!
line_buffer
)
{
STBIW_FREE
(
filt
);
return
0
;
}
for
(
j
=
0
;
j
<
y
;
++
j
)
{
for
(
j
=
0
;
j
<
y
;
++
j
)
{
static
int
mapping
[]
=
{
0
,
1
,
2
,
3
,
4
};
int
filter_type
;
static
int
firstmap
[]
=
{
0
,
1
,
0
,
5
,
6
};
if
(
force_filter
>
-
1
)
{
int
*
mymap
=
(
j
!=
0
)
?
mapping
:
firstmap
;
filter_type
=
force_filter
;
int
best
=
0
,
bestval
=
0x7fffffff
;
stbiw__encode_png_line
(
pixels
,
stride_bytes
,
x
,
y
,
j
,
n
,
force_filter
,
line_buffer
);
for
(
p
=
0
;
p
<
2
;
++
p
)
{
}
else
{
// Estimate the best filter by running through all of them:
for
(
k
=
p
?
best
:
0
;
k
<
5
;
++
k
)
{
// @TODO: clarity: rewrite this to go 0..5, and 'continue' the unwanted ones during 2nd pass
int
best_filter
=
0
,
best_filter_val
=
0x7fffffff
,
est
,
i
;
int
type
=
mymap
[
k
],
est
=
0
;
for
(
filter_type
=
0
;
filter_type
<
5
;
filter_type
++
)
{
unsigned
char
*
z
=
pixels
+
stride_bytes
*
(
stbi__flip_vertically_on_write
?
y
-
1
-
j
:
j
);
stbiw__encode_png_line
(
pixels
,
stride_bytes
,
x
,
y
,
j
,
n
,
filter_type
,
line_buffer
);
for
(
i
=
0
;
i
<
n
;
++
i
)
switch
(
type
)
{
// Estimate the entropy of the line using this filter; the less, the better.
case
0
:
line_buffer
[
i
]
=
z
[
i
];
break
;
est
=
0
;
case
1
:
line_buffer
[
i
]
=
z
[
i
];
break
;
for
(
i
=
0
;
i
<
x
*
n
;
++
i
)
{
case
2
:
line_buffer
[
i
]
=
z
[
i
]
-
z
[
i
-
stride_bytes
];
break
;
case
3
:
line_buffer
[
i
]
=
z
[
i
]
-
(
z
[
i
-
stride_bytes
]
>>
1
);
break
;
case
4
:
line_buffer
[
i
]
=
(
signed
char
)
(
z
[
i
]
-
stbiw__paeth
(
0
,
z
[
i
-
stride_bytes
],
0
));
break
;
case
5
:
line_buffer
[
i
]
=
z
[
i
];
break
;
case
6
:
line_buffer
[
i
]
=
z
[
i
];
break
;
}
for
(
i
=
n
;
i
<
x
*
n
;
++
i
)
{
switch
(
type
)
{
case
0
:
line_buffer
[
i
]
=
z
[
i
];
break
;
case
1
:
line_buffer
[
i
]
=
z
[
i
]
-
z
[
i
-
n
];
break
;
case
2
:
line_buffer
[
i
]
=
z
[
i
]
-
z
[
i
-
stride_bytes
];
break
;
case
3
:
line_buffer
[
i
]
=
z
[
i
]
-
((
z
[
i
-
n
]
+
z
[
i
-
stride_bytes
])
>>
1
);
break
;
case
4
:
line_buffer
[
i
]
=
z
[
i
]
-
stbiw__paeth
(
z
[
i
-
n
],
z
[
i
-
stride_bytes
],
z
[
i
-
stride_bytes
-
n
]);
break
;
case
5
:
line_buffer
[
i
]
=
z
[
i
]
-
(
z
[
i
-
n
]
>>
1
);
break
;
case
6
:
line_buffer
[
i
]
=
z
[
i
]
-
stbiw__paeth
(
z
[
i
-
n
],
0
,
0
);
break
;
}
}
if
(
p
!=
0
)
break
;
for
(
i
=
0
;
i
<
x
*
n
;
++
i
)
est
+=
abs
((
signed
char
)
line_buffer
[
i
]);
est
+=
abs
((
signed
char
)
line_buffer
[
i
]);
if
(
est
<
bestval
)
{
bestval
=
est
;
best
=
k
;
}
}
if
(
est
<
best_filter_val
)
{
best_filter_val
=
est
;
best_filter
=
filter_type
;
}
}
if
(
filter_type
!=
best_filter
)
{
// If the last iteration already got us the best filter, don't redo it
stbiw__encode_png_line
(
pixels
,
stride_bytes
,
x
,
y
,
j
,
n
,
best_filter
,
line_buffer
);
filter_type
=
best_filter
;
}
}
}
}
// when we get here,
best
contains the filter type, and line_buffer contains the data
// when we get here,
filter_type
contains the filter type, and line_buffer contains the data
filt
[
j
*
(
x
*
n
+
1
)]
=
(
unsigned
char
)
best
;
filt
[
j
*
(
x
*
n
+
1
)]
=
(
unsigned
char
)
filter_type
;
STBIW_MEMMOVE
(
filt
+
j
*
(
x
*
n
+
1
)
+
1
,
line_buffer
,
x
*
n
);
STBIW_MEMMOVE
(
filt
+
j
*
(
x
*
n
+
1
)
+
1
,
line_buffer
,
x
*
n
);
}
}
STBIW_FREE
(
line_buffer
);
STBIW_FREE
(
line_buffer
);
zlib
=
stbi_zlib_compress
(
filt
,
y
*
(
x
*
n
+
1
),
&
zlen
,
stbi_write_png_level
);
zlib
=
stbi_zlib_compress
(
filt
,
y
*
(
x
*
n
+
1
),
&
zlen
,
stbi_write_png_
compression_
level
);
STBIW_FREE
(
filt
);
STBIW_FREE
(
filt
);
if
(
!
zlib
)
return
0
;
if
(
!
zlib
)
return
0
;
...
@@ -1421,7 +1454,7 @@ STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const
...
@@ -1421,7 +1454,7 @@ STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const
/* Revision history
/* Revision history
1.08 (2018-01-29)
1.08 (2018-01-29)
add stbi__flip_vertically_on_write
add stbi__flip_vertically_on_write
, external zlib, zlib quality, choose PNG filter
1.07 (2017-07-24)
1.07 (2017-07-24)
doc fix
doc fix
1.06 (2017-07-23)
1.06 (2017-07-23)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录