Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ImageConvert
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhaozl
ImageConvert
Commits
e8f3fe64
Commit
e8f3fe64
authored
Jan 22, 2025
by
zhaozl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feature: 验证RGBA to NV21成功 (恢复默认顶点和纹理坐标的对应关系发现部分区域不糊了,为什么?).
parent
b54be368
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
56 deletions
+45
-56
FRAGMENT_SHADER_RGB2NV21.glsl
app/src/main/assets/glsl/FRAGMENT_SHADER_RGB2NV21.glsl
+17
-22
ImageRenderer.java
app/src/main/java/tv/yunxi/imgfmt/ImageRenderer.java
+28
-34
No files found.
app/src/main/assets/glsl/FRAGMENT_SHADER_RGB2NV21.glsl
View file @
e8f3fe64
...
...
@@ -7,13 +7,10 @@ uniform sampler2D sTexture;
uniform
float
uWidth
;
uniform
float
uHeight
;
/**
*
*/
float
cY
(
float
x
,
float
y
)
{
vec4
c
=
texture2D
(
sTexture
,
vec2
(
x
,
y
));
// return c.r * 0.257 + c.g * 0.504 + c.b * 0.098 + 0.0625;
return
0
.
299
*
c
.
r
+
0
.
587
*
c
.
g
+
0
.
114
*
c
.
b
;
return
c
.
r
*
0
.
257
+
c
.
g
*
0
.
504
+
c
.
b
*
0
.
098
+
0
.
0625
;
}
vec4
cC
(
float
x
,
float
y
,
float
dx
,
float
dy
)
{
...
...
@@ -26,14 +23,12 @@ vec4 cC(float x, float y, float dx, float dy) {
float
cU
(
float
x
,
float
y
,
float
dx
,
float
dy
)
{
vec4
c
=
cC
(
x
,
y
,
dx
,
dy
);
// return -0.148 * c.r - 0.291 * c.g + 0.439 * c.b + 0.5000;
return
-
0
.
14713
*
c
.
r
-
0
.
28886
*
c
.
g
+
0
.
436
*
c
.
b
;
return
-
0
.
148
*
c
.
r
-
0
.
291
*
c
.
g
+
0
.
439
*
c
.
b
+
0
.
5000
;
}
float
cV
(
float
x
,
float
y
,
float
dx
,
float
dy
)
{
vec4
c
=
cC
(
x
,
y
,
dx
,
dy
);
// return 0.439 * c.r - 0.368 * c.g - 0.071 * c.b + 0.5000;
return
0
.
615
*
c
.
r
-
0
.
51499
*
c
.
g
-
0
.
10001
*
c
.
b
;
return
0
.
439
*
c
.
r
-
0
.
368
*
c
.
g
-
0
.
071
*
c
.
b
+
0
.
5000
;
}
vec2
cPos
(
float
t
,
float
shiftx
,
float
gy
)
{
...
...
@@ -50,7 +45,7 @@ vec2 cPos(float t, float shiftx, float gy) {
* 输入: x[0, 1920] y[0, 270)
* 输出: x[0, 1920] y[0, 1080)
* [0, 0]=>[0, 0]; [1, 0]=>[4, 0]; [2, 0]=>[8, 0]; ...;[479, 0]=>[1916, 0]; [480, 0]=>[0, 1]??; [481, 0]=>[4, 1];...;
* [1919, 0]=>[1916, 3]; [1920, 0]=>[0, 4]??
* [1919, 0]=>[1916, 3]; [1920, 0]=>[0, 4]
TODO: 是否需要闭1920
??
*
* [0, 1]=>[0, 4]; [1, 1]=>[4, 4];
*/
...
...
@@ -118,21 +113,21 @@ vec4 calculateVU(float dx, float dy) {
return
oColor
;
}
void
main
()
{
if
(
vTextureCoord
.
x
>=
0
.
&&
vTextureCoord
.
x
<
1
.)
{
if
(
vTextureCoord
.
y
>=
0
.
625
&&
vTextureCoord
.
y
<
0
.
875
)
{
gl_FragColor
=
calculateY
();
}
else
if
(
vTextureCoord
.
y
>=
0
.
875
)
{
gl_FragColor
=
calculateVU
(
1
.
/
uWidth
,
1
.
/
uHeight
);
}
else
{
gl_FragColor
=
vec4
(
0
,
0
,
0
,
0
);
}
if
(
vTextureCoord
.
y
>=
0
.
625
&&
vTextureCoord
.
y
<
0
.
875
)
{
gl_FragColor
=
calculateY
();
}
else
if
(
vTextureCoord
.
y
>=
0
.
875
)
{
gl_FragColor
=
calculateVU
(
1
.
/
uWidth
,
1
.
/
uHeight
);
}
else
{
gl_FragColor
=
vec4
(
0
,
0
,
0
,
0
);
}
// if (vTextureCoord.y >= 0.625 && vTextureCoord.y < 0.875) {
// gl_FragColor = calculateY();
// } else if (vTextureCoord.y >= 0.875) {
// gl_FragColor = calculateVU(1. / uWidth, 1. / uHeight);
// if (vTextureCoord.x >= 0. && vTextureCoord.x < 1.) {
// if (vTextureCoord.y >= 0.625 && vTextureCoord.y < 0.875) {
// gl_FragColor = calculateY();
// } else if (vTextureCoord.y >= 0.875) {
// gl_FragColor = calculateVU(1. / uWidth, 1. / uHeight);
// } else {
// gl_FragColor = vec4(0, 0, 0, 0);
// }
// } else {
// gl_FragColor = vec4(0, 0, 0, 0);
// }
...
...
app/src/main/java/tv/yunxi/imgfmt/ImageRenderer.java
View file @
e8f3fe64
...
...
@@ -64,7 +64,8 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
/* Set up alpha blending and an Android background color */
GLES20
.
glEnable
(
GLES20
.
GL_BLEND
);
GLES20
.
glBlendFunc
(
GLES20
.
GL_SRC_ALPHA
,
GLES20
.
GL_ONE_MINUS_SRC_ALPHA
);
GLES20
.
glBlendFunc
(
GLES20
.
GL_ONE
,
GLES20
.
GL_ZERO
);
// GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20
.
glClearColor
(
0.643f
,
0.776f
,
0.223f
,
1.0f
);
/* Set up shaders and handles to their variables */
...
...
@@ -127,7 +128,7 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
0
f
,
1.0f
,
0.0f
);
Log
.
v
(
TAG
,
"mVMatrix: "
+
Arrays
.
toString
(
mVMatrix
));
mPixelBuffer
=
ByteBuffer
.
allocateDirect
(
NV21_SIZE
);
// RGBA format
mPixelBuffer
=
ByteBuffer
.
allocateDirect
(
RGBA_SIZE
);
mPixelBuffer
.
order
(
ByteOrder
.
nativeOrder
());
}
...
...
@@ -140,13 +141,17 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
renderTexture
();
mPixelBuffer
.
clear
();
GLES20
.
glPixelStorei
(
GLES20
.
GL_PACK_ALIGNMENT
,
1
);
GLES20
.
glReadPixels
(
0
,
0
,
mWidth
,
mHeight
,
GLES20
.
GL_RGBA
,
GLES20
.
GL_UNSIGNED_BYTE
,
mPixelBuffer
);
checkGlError
(
"glReadPixels"
);
mPixelBuffer
.
rewind
();
mPixelBuffer
.
position
(
Y_SIZE
/
2
);
// mPixelBuffer.position(Y_SIZE / 2);
mPixelBuffer
.
position
(
NV21_OFFSET
);
mPixelBuffer
.
get
(
mImageBytes
);
saveImage
(
mImageBytes
,
"gray"
);
// mPixelBuffer.position(0);
// mPixelBuffer.get(mImageBytes, Y_SIZE, Y_SIZE / 2);
saveImage
(
mImageBytes
,
"nv21"
);
GLES20
.
glBindFramebuffer
(
GLES20
.
GL_FRAMEBUFFER
,
0
);
checkGlError
(
"glBindFramebuffer 0"
);
...
...
@@ -243,16 +248,6 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
GLES20
.
glBindFramebuffer
(
GLES20
.
GL_FRAMEBUFFER
,
mFrameBuffer
);
checkGlError
(
"glBindFramebuffer mFrameBuffer: "
+
mFrameBuffer
);
// 创建纹理
// int[] textures = new int[1];
// GLES20.glGenTextures(1, textures, 0);
// mFrameTextureID = textures[0];
// GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrameTextureID);
// checkGlError("glBindTexture mFrameTextureID");
// GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
// checkGlError("glTexImage2D mFrameTextureID");
mFrameTextureID
=
createTexture
(
null
);
GLES20
.
glBindTexture
(
GLES20
.
GL_TEXTURE_2D
,
mFrameTextureID
);
checkGlError
(
"glBindTexture mFrameTextureID"
);
...
...
@@ -330,22 +325,22 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
private
static
final
int
VERTICES_DATA_POS_OFFSET
=
0
;
private
static
final
int
VERTICES_DATA_UV_OFFSET
=
3
;
// private final float[] mVerticesData = {
// // X, Y, Z, U, V
// -1.0f, -1.0f, 0, 0.f, 0.f, // 左下
// 1.0f, -1.0f, 0, 1.f, 0.f, // 右下
// -1.0f, 1.0f, 0, 0.f, 1.f, // 左上
// 1.0f, 1.0f, 0, 1.f, 1.f, // 右上
// };
private
final
float
[]
mVerticesData
=
{
// X, Y, Z, U, V
-
1.0f
,
-
1.0f
,
0
/* 左下 */
,
0
.
f
,
1
.
f
,
/* 左上 */
1.0f
,
-
1.0f
,
0
/* 右下 */
,
1
.
f
,
1
.
f
,
/* 右上 */
-
1.0f
,
1.0f
,
0
/* 左上 */
,
0
.
f
,
0
.
f
,
/* 左下 */
1.0f
,
1.0f
,
0
/* 右上 */
,
1
.
f
,
0
.
f
,
/* 右下 */
-
1.0f
,
-
1.0f
,
0
,
0
.
f
,
0
.
f
,
// 左下
1.0f
,
-
1.0f
,
0
,
1
.
f
,
0
.
f
,
// 右下
-
1.0f
,
1.0f
,
0
,
0
.
f
,
1
.
f
,
// 左上
1.0f
,
1.0f
,
0
,
1
.
f
,
1
.
f
,
// 右上
};
// private final float[] mVerticesData = {
// // X, Y, Z, U, V
// -1.0f, -1.0f, 0 /* 左下 */, 0.f, 1.f, /* 左上 */
// 1.0f, -1.0f, 0 /* 右下 */, 1.f, 1.f, /* 右上 */
// -1.0f, 1.0f, 0 /* 左上 */, 0.f, 0.f, /* 左下 */
// 1.0f, 1.0f, 0 /* 右上 */, 1.f, 0.f, /* 右下 */
// };
private
FloatBuffer
mVertices
;
private
final
String
mVertexShader
=
...
...
@@ -420,10 +415,10 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
private
static
final
int
RGBA_SIZE
=
mWidth
*
mHeight
*
4
;
private
static
final
int
NV21_SIZE
=
RGBA_SIZE
*
3
/
8
;
private
static
final
int
NV21_OFFSET
=
RGBA_SIZE
*
5
/
8
;
private
static
final
int
Y_SIZE
=
mWidth
*
mHeight
;
private
byte
[]
mImageBytes
=
new
byte
[
Y
_SIZE
];
private
byte
[]
mImageBytes
=
new
byte
[
NV21
_SIZE
];
private
static
final
String
TAG
=
"ImageRenderer"
;
...
...
@@ -459,22 +454,21 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
}
}
private
int
index
=
0
;
private
void
saveImage
(
byte
[]
buffer
,
String
suffix
)
{
Log
.
v
(
TAG
,
"saveImage buffer length: "
+
buffer
.
length
);
FileOutputStream
fos
=
null
;
try
{
// String path = "/storage/9016-4EF8/dump_rgba";
String
path
=
"/data/data/tv.yunxi.imgfmt/files/dump_rgba"
;
File
filePath
=
new
File
(
path
);
if
(!
filePath
.
exists
())
{
filePath
.
mkdirs
();
}
File
file
=
new
File
(
path
+
"/image_"
+
index
++
+
"."
+
suffix
);
if
(!
file
.
exists
())
{
File
file
=
new
File
(
path
+
"/image."
+
suffix
);
// 只保存一张
if
(
file
.
exists
())
{
return
;
}
else
{
file
.
createNewFile
();
}
// 写入文件
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment