-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPNGCodec.java
More file actions
110 lines (102 loc) · 4.34 KB
/
PNGCodec.java
File metadata and controls
110 lines (102 loc) · 4.34 KB
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
/*
* @(#)PNGCodec.java
*
* Copyright (c) 2011-2012 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
/**
* {@code PNGCodec} encodes a BufferedImage as a byte[] array..
* <p>
* Supported input formats:
* <ul>
* {@code VideoFormat} with {@code BufferedImage.class}, any width, any height,
* any depth.
* </ul>
* Supported output formats:
* <ul>
* {@code VideoFormat} with {@code byte[].class}, same width and height as input
* format, depth=24.
* </ul>
*
* @author Werner Randelshofer
* @version $Id: PNGCodec.java 188 2012-03-28 14:03:19Z werner $
*/
public class PNGCodec extends AbstractVideoCodec {
public PNGCodec() {
super(new Format[]{
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_JAVA,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_BUFFERED_IMAGE), //
},
new Format[]{
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_QUICKTIME,
VideoFormatKeys.DepthKey, 24,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_QUICKTIME_PNG, VideoFormatKeys.DataClassKey, byte[].class), //
//
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_AVI,
VideoFormatKeys.DepthKey, 24,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_PNG, VideoFormatKeys.DataClassKey, byte[].class), //
});
}
@Override
public Format setOutputFormat(Format f) {
String mimeType = f.get(VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_QUICKTIME);
if (mimeType != null && !mimeType.equals(VideoFormatKeys.MIME_AVI)) {
return super.setOutputFormat(
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_QUICKTIME,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_QUICKTIME_PNG, VideoFormatKeys.DataClassKey,
byte[].class, VideoFormatKeys.DepthKey, 24).append(f));
} else {
return super.setOutputFormat(
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_AVI,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_PNG, VideoFormatKeys.DataClassKey,
byte[].class, VideoFormatKeys.DepthKey, 24).append(f));
}
}
@Override
public int process(Buffer in, Buffer out) {
out.setMetaTo(in);
out.format = outputFormat;
if (in.isFlag(BufferFlag.DISCARD)) {
return CODEC_OK;
}
BufferedImage image = getBufferedImage(in);
if (image == null) {
out.setFlag(BufferFlag.DISCARD);
return CODEC_FAILED;
}
ByteArrayImageOutputStream tmp;
if (out.data instanceof byte[]) {
tmp = new ByteArrayImageOutputStream((byte[]) out.data);
} else {
tmp = new ByteArrayImageOutputStream();
}
try {
ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
ImageWriteParam iwParam = iw.getDefaultWriteParam();
iw.setOutput(tmp);
IIOImage img = new IIOImage(image, null, null);
iw.write(null, img, iwParam);
iw.dispose();
out.setFlag(BufferFlag.KEYFRAME);
out.header = null;
out.data = tmp.getBuffer();
out.offset = 0;
out.length = (int) tmp.getStreamPosition();
return CODEC_OK;
} catch (IOException ex) {
ex.printStackTrace();
out.setFlag(BufferFlag.DISCARD);
return CODEC_FAILED;
}
}
}