-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathTiffMddConverter.java
More file actions
199 lines (176 loc) · 6.71 KB
/
Copy pathTiffMddConverter.java
File metadata and controls
199 lines (176 loc) · 6.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package test.Examples;
import com.icafe4j.image.ImageParam;
import com.icafe4j.image.ImageType;
import com.icafe4j.image.options.PNGOptions;
import com.icafe4j.image.writer.ImageWriter;
import com.knziha.plod.PlainDict.utils.JAIConverter;
import com.knziha.plod.PlainDict.utils.TiffTerminator;
import com.knziha.plod.dictionary.Utils.BU;
import com.knziha.plod.dictionary.mdict;
import com.knziha.plod.dictionary.mdictRes;
import com.knziha.plod.dictionaryBuilder.mdictBuilder;
import com.knziha.plod.dictionaryBuilder.mdictResBuilder;
import test.CMN;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
/** test.privateTest.tiff is small but unsupported by most browsers. */
public class TiffMddConverter {
public static void main(String[] args) throws IOException {
mdictRes mddRaw = new mdictRes("F:\\assets\\mdicts\\汉语\\文言快易通.mdd");
if(false){/* true false */
mdictResBuilder builder = new mdictResBuilder("","test.privateTest.tiff pics to png");
/* JAIConverter or ICAFEConverter */
TiffTerminator hey = new JAIConverter();
long len = mddRaw.getNumberEntries();
for (int i = 0; i < len; i++) {
String key = mddRaw.getEntryAt(i);
byte[] data = mddRaw.getRecordData(i);
try {
builder.insert(key.replace(".tif",".png"), hey.terminateTiff(data));
CMN.Log(i, " / ", len);
} catch (Exception e) {
e.printStackTrace();
builder.insert(key, data);
}
}
builder.write("F:\\assets\\mdicts\\汉语\\文言快易通.converted.mdd");
}
if(true){/* true false */
mdict mdxRaw = new mdict("F:\\assets\\mdicts\\汉语\\文言快易通.mdx");
mdictBuilder builder = new mdictBuilder("文言快易通","", mdxRaw.getEncoding());
builder.setCompressionType(0,2);
for (int i = 0; i < mdxRaw.getNumberEntries(); i++) {
builder.insert(mdxRaw.getEntryAt(i), mdxRaw.getRecordAt(i).replace(".tif", ".png").replace("src=\"", "src=\"/"));
}
builder.write("F:\\assets\\mdicts\\汉语\\文言快易通.converted.mdx");
}
if(false){/* true false */
mdict mdxRaw = new mdict("D:\\Code\\Fillin\\writemdict-master\\example_output\\no_compression.mdx");
mdictBuilder builder = new mdictBuilder("ABOUT--ABOUT--","", mdxRaw.getEncoding());
builder.setCompressionType(0,0);
for (int i = 0; i < mdxRaw.getNumberEntries(); i++) {
builder.insert(mdxRaw.getEntryAt(i), mdxRaw.getRecordAt(i));
}
builder.write("D:\\Code\\Fillin\\writemdict-master\\example_output\\no_compression.converted.mdx");
}
if(false){/* true false */
UnpackMdd("F:\\assets\\mdicts\\汉语\\文言快易通\\", mddRaw, false);
}
if(false){/* true false */
mdictResBuilder builder = new mdictResBuilder("","");
File startPath = new File("F:\\assets\\mdicts\\汉语\\文言快易通\\");
int basePathLen = startPath.getAbsolutePath().length();
ProcessAllFiles(startPath,
fI -> builder.insert(fI.getAbsolutePath().substring(basePathLen), fI));
//builder.insert("\\happy.mp3",new File("F:\\Music\\虾米音乐 - Heartbreak Hotel.mp3"));
builder.write("F:\\assets\\mdicts\\汉语\\文言快易通.converted2.mdd");
}
if(false){/* true false */
File startPath = new File("F:\\assets\\mdicts\\汉语\\文言快易通\\");
File toPath = new File("F:\\assets\\mdicts\\汉语\\文言快易通Con\\");
int basePathLen = startPath.getAbsolutePath().length();
TiffTerminator hey = new JAIConverter();
ProcessAllFiles(startPath,
fI -> {
try {
FileInputStream fin = new FileInputStream(fI);
byte[] data = new byte[(int) fI.length()];
fin.read(data);
fin.close();
FileOutputStream fout = new FileOutputStream(new File(toPath, fI.getAbsolutePath().substring(basePathLen)));
fout.write(hey.terminateTiff(data));
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public static void UnpackMdd(String startPath, mdictRes mdd, boolean overWrite) throws IOException {
final String SepWindows = "\\";
File spf=new File(startPath);
spf.mkdirs();
if(!spf.isDirectory())
throw new FileNotFoundException(startPath);
File targetFile;
for (int i = 0; i < mdd.getNumberEntries(); i++) {
String key = mdd.getEntryAt(i);
try {
targetFile=new File(startPath, key.replace(SepWindows, File.separator));
if(!overWrite && targetFile.exists()) continue;
if(!targetFile.getParentFile().isDirectory())
targetFile.getParentFile().mkdirs();
BU.printFile(mdd.getRecordData(i), targetFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public interface ProcessAllFilesLogicLayer{
void process(File fI);
}
public static void ProcessAllFiles(File startPath, ProcessAllFilesLogicLayer processor) throws IOException {
ProcessAllFiles(startPath, null, processor);
}
public static void ProcessAllFiles(File startPath, HashSet<String> exemption, ProcessAllFilesLogicLayer processor) throws IOException {
int foldeNum = 0;
int fileNum = 0, folderNum = 0;
int baseLen=startPath.getAbsolutePath().length()+1;
if (startPath.exists()) {
LinkedList<File> list = new LinkedList<>();
File[] files = startPath.listFiles();
for (File fI : files) {
if(exemption==null || !exemption.contains(fI.getAbsolutePath().substring(baseLen)))
if (fI.isDirectory()) {
list.add(fI);
foldeNum++;
} else {
//处理文件
processor.process(fI);
fileNum++;
}
}
File temp_file;
while (!list.isEmpty()) {
temp_file = list.removeFirst();
files = temp_file.listFiles();
for (File fI : files) {
if(exemption==null || !exemption.contains(fI.getAbsolutePath().substring(baseLen)))
if (fI.isDirectory()) {
list.add(fI);
folderNum++;
} else {
//处理文件
processor.process(fI);
fileNum++;
}
}
}
} else {
throw new FileNotFoundException(startPath.getAbsolutePath());
}
CMN.Log("文件夹 =", folderNum, ", 文件 =", fileNum);
}
@Deprecated
public static class ICAFEConverter implements TiffTerminator {
@Override
public byte[] terminateTiff(byte[] data) throws Exception {
return terminateTiff(new ByteArrayInputStream(data));
}
@Override
public byte[] terminateTiff(InputStream data) throws Exception {
BufferedImage img = javax.imageio.ImageIO.read(data);
PNGOptions pngOptions = new PNGOptions();
pngOptions.setApplyAdaptiveFilter(false);
pngOptions.setCompressionLevel(9);
ImageWriter writer = com.icafe4j.image.ImageIO.getWriter(ImageType.PNG);
writer.setImageParam(ImageParam.getBuilder().imageOptions(pngOptions).build());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
writer.write(img, bos);
return bos.toByteArray();
}
}
}