Mercurial > otakunoraifu
comparison system/file.cc @ 52:15a18fbe6f21
* Known bugs added to the README
* Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author | thib |
---|---|
date | Sat, 18 Apr 2009 18:35:39 +0000 |
parents | 35ce1a30f3f9 |
children | ddbcbd000206 |
comparison
equal
deleted
inserted
replaced
51:cbb301016a4e | 52:15a18fbe6f21 |
---|---|
45 #include <sys/types.h> | 45 #include <sys/types.h> |
46 #include <sys/stat.h> | 46 #include <sys/stat.h> |
47 #include <vector> | 47 #include <vector> |
48 #include <algorithm> | 48 #include <algorithm> |
49 #if HAVE_MMAP | 49 #if HAVE_MMAP |
50 #include<sys/mman.h> | 50 #include <sys/mman.h> |
51 #endif /* HAVE_MMAP */ | 51 #endif /* HAVE_MMAP */ |
52 #if HAVE_DIRENT_H | 52 #if HAVE_DIRENT_H |
53 # include <dirent.h> | 53 # include <dirent.h> |
54 # define NAMLEN(dirent) strlen((dirent)->d_name) | 54 # define NAMLEN(dirent) strlen((dirent)->d_name) |
55 #else | 55 #else |
65 # include <ndir.h> | 65 # include <ndir.h> |
66 # endif | 66 # endif |
67 #endif | 67 #endif |
68 | 68 |
69 #if HAVE_LIBZ | 69 #if HAVE_LIBZ |
70 #include<zlib.h> | 70 #include <zlib.h> |
71 #endif | 71 #endif |
72 #if HAVE_LIBPNG | 72 #if HAVE_LIBPNG |
73 #include<png.h> | 73 #include <png.h> |
74 #endif | 74 #endif |
75 #if HAVE_LIBJPEG | 75 #if HAVE_LIBJPEG |
76 extern "C" { | 76 extern "C" { |
77 #include<jpeglib.h> | 77 #include <jpeglib.h> |
78 } | 78 } |
79 #endif | 79 #endif |
80 | 80 |
81 #include "file.h" | 81 #include "file.h" |
82 #include "file_impl.h" | 82 #include "file_impl.h" |
109 */ | 109 */ |
110 | 110 |
111 ARCFILE::ARCFILE(const char* aname) { | 111 ARCFILE::ARCFILE(const char* aname) { |
112 struct stat sb; | 112 struct stat sb; |
113 /* 変数初期化 */ | 113 /* 変数初期化 */ |
114 arcname = 0; | 114 arcname = NULL; |
115 list_point = 0; | 115 list_point = 0; |
116 filenames_orig = 0; | 116 filenames_orig = NULL; |
117 next = 0; | 117 next = NULL; |
118 if (aname[0] == '\0') {arcname=new char[1]; arcname[0]='\0';return;} // NULFILE | 118 if (aname[0] == '\0') {arcname=new char[1]; arcname[0]='\0';return;} // NULFILE |
119 /* ディレクトリか否かのチェック */ | 119 /* ディレクトリか否かのチェック */ |
120 if (stat(aname,&sb) == -1) { /* error */ | 120 if (stat(aname,&sb) == -1) { /* error */ |
121 perror("stat"); | 121 perror("stat"); |
122 } | 122 } |
131 arcname = new char[strlen(aname)+1]; | 131 arcname = new char[strlen(aname)+1]; |
132 strcpy(arcname,aname); | 132 strcpy(arcname,aname); |
133 if (arcname[strlen(arcname)-1] == DIR_SPLIT) | 133 if (arcname[strlen(arcname)-1] == DIR_SPLIT) |
134 arcname[strlen(arcname)-1] = '\0'; | 134 arcname[strlen(arcname)-1] = '\0'; |
135 } | 135 } |
136 return; | |
137 } | 136 } |
138 | 137 |
139 void ARCFILE::Init(void) { | 138 void ARCFILE::Init(void) { |
140 if (! arc_atom.empty()) return; | 139 if (! arc_atom.empty()) return; |
141 if (arcname == 0) return; | 140 if (arcname == NULL) return; |
142 /* ファイル数を得る */ | 141 /* ファイル数を得る */ |
143 int slen = CheckFileDeal(); | 142 int slen = CheckFileDeal(); |
144 /* ファイル名のセット */ | 143 /* ファイル名のセット */ |
145 ListupFiles(slen); | 144 ListupFiles(slen); |
146 if ( (!arc_atom.empty()) && arc_atom[0].filename) filenames_orig = arc_atom[0].filename; | 145 if ( (!arc_atom.empty()) && arc_atom[0].filename) filenames_orig = arc_atom[0].filename; |
154 | 153 |
155 ARCFILE::iterator ARCFILE::SearchName(const char* f, const char* ext) { | 154 ARCFILE::iterator ARCFILE::SearchName(const char* f, const char* ext) { |
156 char buf[1024]; char buf_ext[1024]; | 155 char buf[1024]; char buf_ext[1024]; |
157 iterator it; | 156 iterator it; |
158 Init(); | 157 Init(); |
159 if (f == 0) return arc_atom.end(); | 158 if (f == NULL) return arc_atom.end(); |
160 if (arc_atom.empty()) return arc_atom.end(); | 159 if (arc_atom.empty()) return arc_atom.end(); |
161 /* エラーチェック */ | 160 /* エラーチェック */ |
162 if (strlen(f)>500) return arc_atom.end(); | 161 if (strlen(f)>500) return arc_atom.end(); |
163 if (ext && strlen(ext)>500) return arc_atom.end(); | 162 if (ext && strlen(ext)>500) return arc_atom.end(); |
164 | 163 |
169 if (it != arc_atom.end() && strcmp(it->filename_lower, buf) == 0) return it; | 168 if (it != arc_atom.end() && strcmp(it->filename_lower, buf) == 0) return it; |
170 // 拡張子をつけて検索 | 169 // 拡張子をつけて検索 |
171 if (ext) { | 170 if (ext) { |
172 strcpy(buf_ext, buf); | 171 strcpy(buf_ext, buf); |
173 char* ext_pt = strrchr(buf_ext, '.'); | 172 char* ext_pt = strrchr(buf_ext, '.'); |
174 if (ext_pt == 0 || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext); | 173 if (ext_pt == NULL || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext); |
175 *ext_pt++ = '.'; | 174 *ext_pt++ = '.'; |
176 while(*ext=='.') ext++; | 175 while(*ext=='.') ext++; |
177 strcat(buf_ext, ext); | 176 strcat(buf_ext, ext); |
178 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf_ext); | 177 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf_ext); |
179 if (it != arc_atom.end() && strcmp(it->filename_lower, buf_ext) == 0) return it; | 178 if (it != arc_atom.end() && strcmp(it->filename_lower, buf_ext) == 0) return it; |
187 buf[i++] = 0; | 186 buf[i++] = 0; |
188 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf); | 187 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf); |
189 if (it != arc_atom.end() && strcmp(it->filename_lower, buf) == 0) return it; | 188 if (it != arc_atom.end() && strcmp(it->filename_lower, buf) == 0) return it; |
190 | 189 |
191 // 拡張子をつけて検索 | 190 // 拡張子をつけて検索 |
192 if (ext == 0) return arc_atom.end(); | 191 if (ext == NULL) return arc_atom.end(); |
193 strcpy(buf_ext, buf); | 192 strcpy(buf_ext, buf); |
194 char* ext_pt = strrchr(buf_ext, '.'); | 193 char* ext_pt = strrchr(buf_ext, '.'); |
195 if (ext_pt == 0 || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext); | 194 if (ext_pt == NULL || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext); |
196 *ext_pt++ = '.'; | 195 *ext_pt++ = '.'; |
197 /* 拡張子の長さを得る */ | 196 /* 拡張子の長さを得る */ |
198 l = strlen(ext); | 197 l = strlen(ext); |
199 for (i=0; i<l; i++) | 198 for (i=0; i<l; i++) |
200 ext_pt[i] = tolower(*ext++); | 199 ext_pt[i] = tolower(*ext++); |
207 ARCINFO* ARCFILE::Find(const char* fname, const char* ext) { | 206 ARCINFO* ARCFILE::Find(const char* fname, const char* ext) { |
208 Init(); | 207 Init(); |
209 iterator atom = SearchName(fname,ext); | 208 iterator atom = SearchName(fname,ext); |
210 if (atom == arc_atom.end()) { | 209 if (atom == arc_atom.end()) { |
211 if (next) return next->Find(fname, ext); | 210 if (next) return next->Find(fname, ext); |
212 else return 0; | 211 else return NULL; |
213 } | 212 } |
214 return MakeARCINFO(*atom); | 213 return MakeARCINFO(*atom); |
215 } | 214 } |
216 ARCINFO* ARCFILE::MakeARCINFO(ARCFILE_ATOM& atom) { | 215 ARCINFO* ARCFILE::MakeARCINFO(ARCFILE_ATOM& atom) { |
217 if (atom.arcsize == atom.filesize) | 216 if (atom.arcsize == atom.filesize) |
241 char* name = atom->filename; | 240 char* name = atom->filename; |
242 // make FILE* | 241 // make FILE* |
243 char* new_path = new char[strlen(arcname)+strlen(name)+1]; | 242 char* new_path = new char[strlen(arcname)+strlen(name)+1]; |
244 strcpy(new_path,arcname); strcat(new_path, name); | 243 strcpy(new_path,arcname); strcat(new_path, name); |
245 FILE* ret = fopen(new_path, "rb+"); | 244 FILE* ret = fopen(new_path, "rb+"); |
246 fseek(ret, 0, 0); | 245 fseek(ret, 0, SEEK_SET); |
247 delete[] new_path; | 246 delete[] new_path; |
248 return ret; | 247 return ret; |
249 } | 248 } |
250 | 249 |
251 char* DIRFILE::SearchFile(const char* fname) { | 250 char* DIRFILE::SearchFile(const char* fname) { |
281 void ARCFILE::InitList(void) { | 280 void ARCFILE::InitList(void) { |
282 Init(); | 281 Init(); |
283 list_point = 0; | 282 list_point = 0; |
284 } | 283 } |
285 char* ARCFILE::ListItem(void) { | 284 char* ARCFILE::ListItem(void) { |
286 if (list_point < 0) return 0; | 285 if (list_point < 0) return NULL; |
287 if (list_point >= arc_atom.size()) return 0; | 286 if (list_point >= arc_atom.size()) return NULL; |
288 char* fname = arc_atom[list_point].filename; | 287 char* fname = arc_atom[list_point].filename; |
289 if (fname == 0) return 0; | 288 if (fname == NULL) return NULL; |
290 char* ret = new char[strlen(fname)+1]; | 289 char* ret = new char[strlen(fname)+1]; |
291 strcpy(ret, fname); | 290 strcpy(ret, fname); |
292 list_point++; | 291 list_point++; |
293 return ret; | 292 return ret; |
294 } | 293 } |
295 | 294 |
296 int ARCFILE::CheckFileDeal(void) { | 295 int ARCFILE::CheckFileDeal(void) { |
297 char buf[0x20]; | 296 char buf[0x20]; |
298 /* ヘッダのチェック */ | 297 /* ヘッダのチェック */ |
299 FILE* stream = fopen(arcname, "rb"); | 298 FILE* stream = fopen(arcname, "rb"); |
300 if (stream == 0) { | 299 if (stream == NULL) { |
301 fprintf(stderr, "Cannot open archive file : %s\n",arcname); | 300 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
302 return 0; | 301 return 0; |
303 } | 302 } |
304 fseek(stream, 0, 2); size_t arc_size = ftell(stream); | 303 fseek(stream, 0, SEEK_END); |
305 fseek(stream, 0, 0); | 304 size_t arc_size = ftell(stream); |
305 fseek(stream, 0, SEEK_SET); | |
306 if (arc_size < 0x20) { | 306 if (arc_size < 0x20) { |
307 fclose(stream); | 307 fclose(stream); |
308 return 0; | 308 return 0; |
309 } | 309 } |
310 fread(buf, 0x20, 1, stream); | 310 fread(buf, 0x20, 1, stream); |
328 void ARCFILE::ListupFiles(int fname_len) { | 328 void ARCFILE::ListupFiles(int fname_len) { |
329 int i; char fbuf[0x20]; | 329 int i; char fbuf[0x20]; |
330 fname_len *= 2; | 330 fname_len *= 2; |
331 char* buf = new char[fname_len]; | 331 char* buf = new char[fname_len]; |
332 FILE* stream = fopen(arcname, "rb"); | 332 FILE* stream = fopen(arcname, "rb"); |
333 if (stream == 0) { | 333 if (stream == NULL) { |
334 fprintf(stderr, "Cannot open archive file : %s\n",arcname); | 334 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
335 return; | 335 return; |
336 } | 336 } |
337 fread(fbuf,0x20,1,stream); | 337 fread(fbuf,0x20,1,stream); |
338 int len = read_little_endian_int(fbuf+0x10); | 338 int len = read_little_endian_int(fbuf+0x10); |
361 } | 361 } |
362 int DIRFILE::CheckFileDeal(void) { | 362 int DIRFILE::CheckFileDeal(void) { |
363 DIR* dir; struct dirent* ent; | 363 DIR* dir; struct dirent* ent; |
364 int flen = 0; | 364 int flen = 0; |
365 dir = opendir(arcname); | 365 dir = opendir(arcname); |
366 if (dir == 0) { | 366 if (dir == NULL) { |
367 fprintf(stderr, "Cannot open dir file : %s\n",arcname); | 367 fprintf(stderr, "Cannot open dir file : %s\n",arcname); |
368 return 0; | 368 return 0; |
369 } | 369 } |
370 int count = 0; | 370 int count = 0; |
371 while( (ent = readdir(dir)) != NULL) { | 371 while( (ent = readdir(dir)) != NULL) { |
377 } | 377 } |
378 void DIRFILE::ListupFiles(int fname_len) { | 378 void DIRFILE::ListupFiles(int fname_len) { |
379 DIR* dir; | 379 DIR* dir; |
380 fname_len *= 2; | 380 fname_len *= 2; |
381 dir = opendir(arcname); | 381 dir = opendir(arcname); |
382 if (dir == 0) { | 382 if (dir == NULL) { |
383 fprintf(stderr, "Cannot open dir file : %s\n",arcname); | 383 fprintf(stderr, "Cannot open dir file : %s\n",arcname); |
384 return; | 384 return; |
385 } | 385 } |
386 /* 一時的に arcname のディレクトリに移動する */ | 386 /* 一時的に arcname のディレクトリに移動する */ |
387 int old_dir_fd = open(".",O_RDONLY); | 387 int old_dir_fd = open(".",O_RDONLY); |
392 if (chdir(arcname) != 0) { | 392 if (chdir(arcname) != 0) { |
393 fprintf(stderr, "Cannot open dir file : %s\n",arcname); | 393 fprintf(stderr, "Cannot open dir file : %s\n",arcname); |
394 closedir(dir); | 394 closedir(dir); |
395 close(old_dir_fd); | 395 close(old_dir_fd); |
396 return; | 396 return; |
397 }; | 397 } |
398 | 398 |
399 char* buf = new char[fname_len]; | 399 char* buf = new char[fname_len]; |
400 ARCFILE_ATOM atom; | 400 ARCFILE_ATOM atom; |
401 struct stat sb; | 401 struct stat sb; |
402 struct dirent* ent; | 402 struct dirent* ent; |
403 while( (ent = readdir(dir)) != NULL) { | 403 while( (ent = readdir(dir)) != NULL) { |
425 buf += l*2+2; fname_len -= l*2+2; | 425 buf += l*2+2; fname_len -= l*2+2; |
426 } | 426 } |
427 /* chdir() したのを元に戻る */ | 427 /* chdir() したのを元に戻る */ |
428 closedir(dir); | 428 closedir(dir); |
429 fchdir(old_dir_fd); close(old_dir_fd); | 429 fchdir(old_dir_fd); close(old_dir_fd); |
430 return; | 430 } |
431 } | 431 |
432 int NULFILE::CheckFileDeal(void) { | 432 int NULFILE::CheckFileDeal(void) { |
433 return 20; | 433 return 20; |
434 } | 434 } |
435 | |
435 void NULFILE::ListupFiles(int fname_len) { | 436 void NULFILE::ListupFiles(int fname_len) { |
436 char* s = new char[40]; | 437 char* s = new char[40]; |
437 ARCFILE_ATOM atom; | 438 ARCFILE_ATOM atom; |
438 atom.offset = 0; atom.arcsize = 0; atom.filesize = 0; | 439 atom.offset = 0; atom.arcsize = 0; atom.filesize = 0; |
439 strcpy(s, "** null dummy **"); | 440 strcpy(s, "** null dummy **"); |
440 atom.filename = s; | 441 atom.filename = s; |
441 atom.filename_lower = s; | 442 atom.filename_lower = s; |
442 arc_atom.push_back(atom); | 443 arc_atom.push_back(atom); |
443 } | 444 } |
445 | |
444 int SCN2kFILE::CheckFileDeal(void) { | 446 int SCN2kFILE::CheckFileDeal(void) { |
445 /* ヘッダのチェック */ | 447 /* ヘッダのチェック */ |
446 FILE* stream = fopen(arcname, "rb"); | 448 FILE* stream = fopen(arcname, "rb"); |
447 if (stream == 0) { | 449 if (stream == NULL) { |
448 fprintf(stderr, "Cannot open archive file : %s\n",arcname); | 450 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
449 return 0; | 451 return 0; |
450 } | 452 } |
451 fseek(stream, 0, 2); size_t arc_size = ftell(stream); | 453 fseek(stream, 0, SEEK_END); |
452 fseek(stream, 0, 0); | 454 size_t arc_size = ftell(stream); |
455 fseek(stream, 0, SEEK_SET); | |
453 if (arc_size < 10000*8) { | 456 if (arc_size < 10000*8) { |
454 fclose(stream); | 457 fclose(stream); |
455 return 0; | 458 return 0; |
456 } | 459 } |
457 char* buf = new char[10000*8]; | 460 char* buf = new char[10000*8]; |
466 } | 469 } |
467 fclose(stream); | 470 fclose(stream); |
468 delete[] buf; | 471 delete[] buf; |
469 return count*13; /* ファイル名は seenXXXX.txt だから、一つ12文字+null */ | 472 return count*13; /* ファイル名は seenXXXX.txt だから、一つ12文字+null */ |
470 } | 473 } |
474 | |
471 void SCN2kFILE::ListupFiles(int fname_len) { | 475 void SCN2kFILE::ListupFiles(int fname_len) { |
472 FILE* stream = fopen(arcname, "rb"); | 476 FILE* stream = fopen(arcname, "rb"); |
473 if (stream == 0) { | 477 if (stream == NULL) { |
474 fprintf(stderr, "Cannot open archive file : %s\n",arcname); | 478 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
475 return; | 479 return; |
476 } | 480 } |
477 char* sbuf = new char[fname_len]; | 481 char* sbuf = new char[fname_len]; |
478 char* buf = new char[10000*8]; | 482 char* buf = new char[10000*8]; |
479 fread(buf, 10000, 8, stream); | 483 fread(buf, 10000, 8, stream); |
480 fseek(stream, 0, 2); size_t arc_size = ftell(stream); | 484 fseek(stream, 0, SEEK_END); |
485 size_t arc_size = ftell(stream); | |
481 ARCFILE_ATOM atom; | 486 ARCFILE_ATOM atom; |
482 int i; for (i=0; i<10000; i++) { | 487 int i; for (i=0; i<10000; i++) { |
483 char header[0x200]; | 488 char header[0x200]; |
484 int tmp_offset = read_little_endian_int(buf+i*8); | 489 int tmp_offset = read_little_endian_int(buf+i*8); |
485 int tmp_size = read_little_endian_int(buf+i*8+4); | 490 int tmp_size = read_little_endian_int(buf+i*8+4); |
486 if (tmp_size <= 0 || tmp_offset < 0 || tmp_offset+tmp_size > int(arc_size) ) continue; | 491 if (tmp_size <= 0 || tmp_offset < 0 || tmp_offset+tmp_size > int(arc_size) ) continue; |
487 /* header を得て圧縮形式などを調べる */ | 492 /* header を得て圧縮形式などを調べる */ |
488 fseek(stream, tmp_offset, 0); | 493 fseek(stream, tmp_offset, SEEK_SET); |
489 fread(header, 0x200, 1, stream); | 494 fread(header, 0x200, 1, stream); |
490 int header_top = read_little_endian_int(header+0); | 495 int header_top = read_little_endian_int(header+0); |
491 int file_version = read_little_endian_int(header+4); | 496 int file_version = read_little_endian_int(header+4); |
492 | 497 |
493 if (file_version == 0x1adb2) ; // Little Busters! | 498 if (file_version == 0x1adb2) ; // Little Busters! |
526 arc_atom.push_back(atom); | 531 arc_atom.push_back(atom); |
527 sprintf(sbuf, "seen%04d.txt",i); sbuf += 13; | 532 sprintf(sbuf, "seen%04d.txt",i); sbuf += 13; |
528 } | 533 } |
529 delete[] buf; | 534 delete[] buf; |
530 fclose(stream); | 535 fclose(stream); |
531 return; | |
532 } | 536 } |
533 | 537 |
534 /******************************************************** | 538 /******************************************************** |
535 ** KEYHOLDER | 539 ** KEYHOLDER |
536 */ | 540 */ |
587 ** FILESEARCH クラスの実装 | 591 ** FILESEARCH クラスの実装 |
588 */ | 592 */ |
589 | 593 |
590 FILESEARCH::FILESEARCH(void) { | 594 FILESEARCH::FILESEARCH(void) { |
591 int i; | 595 int i; |
592 root_dir = 0; dat_dir = 0; | 596 root_dir = NULL; |
597 dat_dir = NULL; | |
593 for (i=0; i<TYPEMAX; i++) { | 598 for (i=0; i<TYPEMAX; i++) { |
594 searcher[i] = 0; | 599 searcher[i] = NULL; |
595 filenames[i] = default_dirnames[i]; | 600 filenames[i] = default_dirnames[i]; |
596 is_archived[i] = default_is_archived[i]; | 601 is_archived[i] = default_is_archived[i]; |
597 } | 602 } |
598 } | 603 } |
599 FILESEARCH::~FILESEARCH(void) { | 604 FILESEARCH::~FILESEARCH(void) { |
600 int i; | 605 int i; |
601 for (i=0; i<TYPEMAX; i++) { | 606 for (i=0; i<TYPEMAX; i++) { |
602 if (filenames[i] != 0 && filenames[i] != default_dirnames[i]) delete[] filenames[i]; | 607 if (filenames[i] != NULL && filenames[i] != default_dirnames[i]) delete[] filenames[i]; |
603 if (searcher[i] && searcher[i] != dat_dir && searcher[i] != root_dir) { | 608 if (searcher[i] && searcher[i] != dat_dir && searcher[i] != root_dir) { |
604 delete searcher[i]; | 609 delete searcher[i]; |
605 } | 610 } |
606 } | 611 } |
607 if (dat_dir && dat_dir != root_dir) delete dat_dir; | 612 if (dat_dir && dat_dir != root_dir) delete dat_dir; |
610 | 615 |
611 int FILESEARCH::InitRoot(char* root) { | 616 int FILESEARCH::InitRoot(char* root) { |
612 /* 必要に応じて ~/ を展開 */ | 617 /* 必要に応じて ~/ を展開 */ |
613 if (root[0] == '~' && root[1] == '/') { | 618 if (root[0] == '~' && root[1] == '/') { |
614 char* home = getenv("HOME"); | 619 char* home = getenv("HOME"); |
615 if (home != 0) { | 620 if (home != NULL) { |
616 char* new_root = new char[strlen(home)+strlen(root)]; | 621 char* new_root = new char[strlen(home)+strlen(root)]; |
617 strcpy(new_root, home); | 622 strcpy(new_root, home); |
618 strcat(new_root, root+1); | 623 strcat(new_root, root+1); |
619 root = new_root; | 624 root = new_root; |
620 } | 625 } |
621 } | 626 } |
622 /* 古いデータを消す */ | 627 /* 古いデータを消す */ |
623 int i; | 628 int i; |
624 for (i=0; i<TYPEMAX; i++) { | 629 for (i=0; i<TYPEMAX; i++) { |
625 if (searcher[i] != 0 && | 630 if (searcher[i] != NULL && |
626 searcher[i] != root_dir && | 631 searcher[i] != root_dir && |
627 searcher[i] != dat_dir) { | 632 searcher[i] != dat_dir) { |
628 delete searcher[i]; | 633 delete searcher[i]; |
629 } | 634 } |
630 searcher[i] = 0; | 635 searcher[i] = NULL; |
631 } | 636 } |
632 if (dat_dir && root_dir != dat_dir) delete dat_dir; | 637 if (dat_dir && root_dir != dat_dir) delete dat_dir; |
633 if (root_dir) delete root_dir; | 638 if (root_dir) delete root_dir; |
634 dat_dir = 0; | 639 dat_dir = NULL; |
635 | 640 |
636 /* 新しいディレクトリのもとで初期化 */ | 641 /* 新しいディレクトリのもとで初期化 */ |
637 root_dir = new DIRFILE(root); | 642 root_dir = new DIRFILE(root); |
638 root_dir->Init(); | 643 root_dir->Init(); |
639 /* dat/ を検索 */ | 644 /* dat/ を検索 */ |
640 char* dat_path = root_dir->SearchFile("dat"); | 645 char* dat_path = root_dir->SearchFile("dat"); |
641 if (dat_path == 0) { | 646 if (dat_path == NULL) { |
642 /* 見つからなかったら root を dat の代わりにつかう */ | 647 /* 見つからなかったら root を dat の代わりにつかう */ |
643 dat_dir = root_dir; | 648 dat_dir = root_dir; |
644 } else { | 649 } else { |
645 dat_dir = new DIRFILE(dat_path); | 650 dat_dir = new DIRFILE(dat_path); |
646 delete[] dat_path; | 651 delete[] dat_path; |
652 } | 657 } |
653 | 658 |
654 void FILESEARCH::SetFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { | 659 void FILESEARCH::SetFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { |
655 int type = tp; | 660 int type = tp; |
656 if (type < 0 || type >= TYPEMAX) return; | 661 if (type < 0 || type >= TYPEMAX) return; |
657 ARCFILE* next_arc = 0; | 662 ARCFILE* next_arc = NULL; |
658 /* すでに searcher が存在すれば解放 */ | 663 /* すでに searcher が存在すれば解放 */ |
659 if (searcher[type] != 0 && | 664 if (searcher[type] != NULL && |
660 searcher[type] != root_dir && | 665 searcher[type] != root_dir && |
661 searcher[type] != dat_dir) { | 666 searcher[type] != dat_dir) { |
662 next_arc = searcher[type]->Next(); | 667 next_arc = searcher[type]->Next(); |
663 delete searcher[type]; | 668 delete searcher[type]; |
664 } | 669 } |
665 searcher[type] = 0; | 670 searcher[type] = NULL; |
666 /* 適当に初期化 */ | 671 /* 適当に初期化 */ |
667 if (filenames[type] != 0 && | 672 if (filenames[type] != NULL && |
668 filenames[type] != default_dirnames[type]) delete[] filenames[type]; | 673 filenames[type] != default_dirnames[type]) delete[] filenames[type]; |
669 filenames[type] = filename; | 674 filenames[type] = filename; |
670 is_archived[type] = is_arc; | 675 is_archived[type] = is_arc; |
671 searcher[type] = MakeARCFILE(is_arc, filename); | 676 searcher[type] = MakeARCFILE(is_arc, filename); |
672 if (searcher[type] && next_arc) | 677 if (searcher[type] != NULL && next_arc) |
673 searcher[type]->SetNext(next_arc); | 678 searcher[type]->SetNext(next_arc); |
674 return; | 679 } |
675 } | 680 |
676 void FILESEARCH::AppendFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { | 681 void FILESEARCH::AppendFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { |
677 int type = tp; | 682 int type = tp; |
678 if (type < 0 || type >= TYPEMAX) return; | 683 if (type < 0 || type >= TYPEMAX) return; |
679 /* searcher がまだ割り当てられてない場合 */ | 684 /* searcher がまだ割り当てられてない場合 */ |
680 if (searcher[type] == 0 || | 685 if (searcher[type] == NULL || |
681 searcher[type] == root_dir || | 686 searcher[type] == root_dir || |
682 searcher[type] == dat_dir) { | 687 searcher[type] == dat_dir) { |
683 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); | 688 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); |
684 if (searcher[type] == 0) { /* 作成できなかった場合 */ | 689 if (searcher[type] == NULL) { /* 作成できなかった場合 */ |
685 /* この型情報を FileInformation とする */ | 690 /* この型情報を FileInformation とする */ |
686 SetFileInformation(tp, is_arc, filename); | 691 SetFileInformation(tp, is_arc, filename); |
687 return; | 692 return; |
688 } | 693 } |
689 } | 694 } |
690 /* 初期化 */ | 695 /* 初期化 */ |
691 ARCFILE* arc = MakeARCFILE(is_arc, filename); | 696 ARCFILE* arc = MakeARCFILE(is_arc, filename); |
692 /* append */ | 697 /* append */ |
693 ARCFILE* cur; | 698 ARCFILE* cur; |
694 for (cur=searcher[type]; cur->Next() != 0; cur = cur->Next()) ; | 699 for (cur=searcher[type]; cur->Next() != NULL; cur = cur->Next()) ; |
695 cur->SetNext(arc); | 700 cur->SetNext(arc); |
696 return; | |
697 } | 701 } |
698 | 702 |
699 ARCFILE* FILESEARCH::MakeARCFILE(ARCTYPE tp, const char* filename) { | 703 ARCFILE* FILESEARCH::MakeARCFILE(ARCTYPE tp, const char* filename) { |
700 ARCFILE* arc = 0; | 704 ARCFILE* arc = NULL; |
701 char* file; | 705 char* file; |
702 if (filename == 0) goto err; | 706 if (filename == NULL) goto err; |
703 if (tp == ATYPE_DIR) { | 707 if (tp == ATYPE_DIR) { |
704 file = root_dir->SearchFile(filename); | 708 file = root_dir->SearchFile(filename); |
705 } else { | 709 } else { |
706 file = dat_dir->SearchFile(filename); | 710 file = dat_dir->SearchFile(filename); |
707 if (file == 0) | 711 if (file == NULL) |
708 file = root_dir->SearchFile(filename); | 712 file = root_dir->SearchFile(filename); |
709 } | 713 } |
710 if (file == 0) goto err; | 714 if (file == NULL) goto err; |
711 switch(tp) { | 715 switch(tp) { |
712 case ATYPE_DIR: arc = new DIRFILE(file); break; | 716 case ATYPE_DIR: arc = new DIRFILE(file); break; |
713 case ATYPE_SCN2k: | 717 case ATYPE_SCN2k: |
714 case ATYPE_ARC: { | 718 case ATYPE_ARC: { |
715 FILE* f = fopen(file, "rb"); | 719 FILE* f = fopen(file, "rb"); |
716 if (f == 0) goto err; | 720 if (f == NULL) goto err; |
717 char header[32]; | 721 char header[32]; |
718 memset(header, 0, 32); | 722 memset(header, 0, 32); |
719 fread(header, 32, 1, f); | 723 fread(header, 32, 1, f); |
720 fclose(f); | 724 fclose(f); |
721 char magic_raf[8] = {'C','A','P','F',1,0,0,0}; | 725 char magic_raf[8] = {'C','A','P','F',1,0,0,0}; |
730 delete[] file; | 734 delete[] file; |
731 return arc; | 735 return arc; |
732 err: | 736 err: |
733 arc = new NULFILE; | 737 arc = new NULFILE; |
734 return arc; | 738 return arc; |
735 | |
736 } | 739 } |
737 | 740 |
738 ARCINFO* FILESEARCH::Find(FILETYPE type, const char* fname, const char* ext) { | 741 ARCINFO* FILESEARCH::Find(FILETYPE type, const char* fname, const char* ext) { |
739 if (searcher[type] == 0) { | 742 if (searcher[type] == NULL) { |
740 /* searcher 作成 */ | 743 /* searcher 作成 */ |
741 if (filenames[type] == 0) { | 744 if (filenames[type] == NULL) { |
742 searcher[type] = dat_dir; | 745 searcher[type] = dat_dir; |
743 } else { | 746 } else { |
744 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); | 747 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); |
745 if (searcher[type] == 0) { | 748 if (searcher[type] == NULL) { |
746 fprintf(stderr,"FILESEARCH::Find : invalid archive type; type %d name %s\n",type,fname); | 749 fprintf(stderr,"FILESEARCH::Find : invalid archive type; type %d name %s\n",type,fname); |
747 return 0; | 750 return NULL; |
748 } | 751 } |
749 } | 752 } |
750 } | 753 } |
751 return searcher[type]->Find(fname,ext); | 754 return searcher[type]->Find(fname,ext); |
752 } | 755 } |
753 | 756 |
754 char** FILESEARCH::ListAll(FILETYPE type) { | 757 char** FILESEARCH::ListAll(FILETYPE type) { |
755 /* とりあえず searcher を初期化 */ | 758 /* とりあえず searcher を初期化 */ |
756 Find(type, "THIS FILENAME MAY NOT EXIST IN THE FILE SYSTEM !!!"); | 759 Find(type, "THIS FILENAME MAY NOT EXIST IN THE FILE SYSTEM !!!"); |
757 if (searcher[type] == 0) return 0; | 760 if (searcher[type] == NULL) return NULL; |
758 /* 全ファイルのリストアップ */ | 761 /* 全ファイルのリストアップ */ |
759 int deal = 0; | 762 int deal = 0; |
760 ARCFILE* file; | 763 ARCFILE* file; |
761 for (file = searcher[type]; file != 0; file = file->Next()) | 764 for (file = searcher[type]; file != NULL; file = file->Next()) |
762 deal += file->Deal(); | 765 deal += file->Deal(); |
763 if (deal <= 0) return 0; | 766 if (deal <= 0) return NULL; |
764 char** ret_list = new char*[deal+1]; | 767 char** ret_list = new char*[deal+1]; |
765 int count = 0; | 768 int count = 0; |
766 for (file = searcher[type]; file != 0; file = file->Next()) { | 769 for (file = searcher[type]; file != NULL; file = file->Next()) { |
767 file->InitList(); | 770 file->InitList(); |
768 char* f; | 771 char* f; |
769 while( (f = file->ListItem() ) != 0) { | 772 while( (f = file->ListItem() ) != 0) { |
770 ret_list[count] = new char[strlen(f)+1]; | 773 ret_list[count] = new char[strlen(f)+1]; |
771 strcpy(ret_list[count], f); | 774 strcpy(ret_list[count], f); |
772 count++; | 775 count++; |
773 } | 776 } |
774 } | 777 } |
775 ret_list[count] = 0; | 778 ret_list[count] = NULL; |
776 return ret_list; | 779 return ret_list; |
777 } | 780 } |
778 | 781 |
779 ARCINFO::ARCINFO(const char* __arcname, ARCFILE_ATOM& atom) : info(atom) { | 782 ARCINFO::ARCINFO(const char* __arcname, ARCFILE_ATOM& atom) : info(atom) { |
780 arcfile = new char[strlen(__arcname)+1]; | 783 arcfile = new char[strlen(__arcname)+1]; |
781 strcpy(arcfile, __arcname); | 784 strcpy(arcfile, __arcname); |
782 use_mmap = false; | 785 use_mmap = false; |
783 mmapped_memory = 0; | 786 mmapped_memory = NULL; |
784 data = 0; | 787 data = NULL; |
785 fd = -1; | 788 fd = -1; |
786 } | 789 } |
787 | 790 |
788 ARCINFO::~ARCINFO() { | 791 ARCINFO::~ARCINFO() { |
789 #ifdef HAVE_MMAP | 792 #ifdef HAVE_MMAP |
799 } | 802 } |
800 | 803 |
801 /* コピーを返す */ | 804 /* コピーを返す */ |
802 char* ARCINFO::CopyRead(void) { | 805 char* ARCINFO::CopyRead(void) { |
803 const char* d = Read(); | 806 const char* d = Read(); |
804 if (d == 0) return 0; | 807 if (d == NULL) return NULL; |
805 int s = Size(); | 808 int s = Size(); |
806 if (s <= 0) return 0; | 809 if (s <= 0) return NULL; |
807 char* ret = new char[s]; memcpy(ret, d, s); | 810 char* ret = new char[s]; memcpy(ret, d, s); |
808 return ret; | 811 return ret; |
809 } | 812 } |
810 | 813 |
811 const char* ARCINFO::Path(void) const { | 814 const char* ARCINFO::Path(void) const { |
812 if (info.offset != 0) return 0; /* archive file なのでパスを帰せない */ | 815 if (info.offset != 0) return NULL; /* archive file なのでパスを帰せない */ |
813 char* ret = new char[strlen(arcfile)+1]; | 816 char* ret = new char[strlen(arcfile)+1]; |
814 strcpy(ret, arcfile); | 817 strcpy(ret, arcfile); |
815 return ret; | 818 return ret; |
816 } | 819 } |
820 | |
817 /* 互換性専用 */ | 821 /* 互換性専用 */ |
818 FILE* ARCINFO::OpenFile(int* length) const { | 822 FILE* ARCINFO::OpenFile(int* length) const { |
819 FILE* f = fopen(arcfile, "rb"); | 823 FILE* f = fopen(arcfile, "rb"); |
820 if (info.offset) lseek(fileno(f), info.offset, SEEK_SET); | 824 if (info.offset) lseek(fileno(f), info.offset, SEEK_SET); |
821 if (length) *length = info.arcsize; | 825 if (length) *length = info.arcsize; |
824 | 828 |
825 // 展開処理はなし | 829 // 展開処理はなし |
826 bool ARCINFO::ExecExtract(void) { | 830 bool ARCINFO::ExecExtract(void) { |
827 return true; | 831 return true; |
828 } | 832 } |
833 | |
829 /* 読み込みを開始する */ | 834 /* 読み込みを開始する */ |
830 const char* ARCINFO::Read(void) { | 835 const char* ARCINFO::Read(void) { |
831 // すでにデータを読み込み済みなら何もしない | 836 // すでにデータを読み込み済みなら何もしない |
832 if (data) return data; | 837 if (data != NULL) return data; |
833 | 838 |
834 if (info.offset < 0 || info.arcsize <= 0) { | 839 if (info.offset < 0 || info.arcsize <= 0) { |
835 return 0; | 840 return NULL; |
836 } | 841 } |
837 /* ファイルを開く */ | 842 /* ファイルを開く */ |
838 fd = open(arcfile, O_RDONLY); | 843 fd = open(arcfile, O_RDONLY); |
839 if (fd < 0) { | 844 if (fd < 0) { |
840 return 0; | 845 return NULL; |
841 } | 846 } |
842 if (lseek(fd, info.offset, 0) != info.offset) { | 847 if (lseek(fd, info.offset, SEEK_SET) != info.offset) { |
843 close(fd); fd = -1; return 0; | 848 close(fd); |
849 fd = -1; | |
850 return NULL; | |
844 } | 851 } |
845 /* mmap を試みる */ | 852 /* mmap を試みる */ |
846 #ifdef HAVE_MMAP | 853 #ifdef HAVE_MMAP |
847 mmapped_memory = (char*)mmap(0, info.arcsize, PROT_READ, MAP_SHARED, fd, info.offset); | 854 mmapped_memory = (char*)mmap(NULL, info.arcsize, PROT_READ, MAP_SHARED, fd, info.offset); |
848 if (mmapped_memory != MAP_FAILED) { | 855 if (mmapped_memory != MAP_FAILED) { |
849 use_mmap = true; | 856 use_mmap = true; |
850 data = (const char*)mmapped_memory; | 857 data = (const char*)mmapped_memory; |
851 } else | 858 } else |
852 #endif /* HAVE_MMAP */ | 859 #endif /* HAVE_MMAP */ |
863 if (! ExecExtract()) { | 870 if (! ExecExtract()) { |
864 // 失敗 | 871 // 失敗 |
865 #ifdef HAVE_MMAP | 872 #ifdef HAVE_MMAP |
866 if (use_mmap) { | 873 if (use_mmap) { |
867 munmap(mmapped_memory, info.arcsize); | 874 munmap(mmapped_memory, info.arcsize); |
868 if (data == (const char*)mmapped_memory) data = 0; | 875 if (data == (const char*)mmapped_memory) data = NULL; |
869 } | 876 } |
870 #endif /* HAVE_MMAP */ | 877 #endif /* HAVE_MMAP */ |
871 delete[] (char*)data; | 878 delete[] (char*)data; |
872 close(fd); | 879 close(fd); |
873 fd = -1; data = 0; | 880 fd = -1; data = NULL; |
874 return 0; | 881 return NULL; |
875 } | 882 } |
876 #ifdef HAVE_MMAP | 883 #ifdef HAVE_MMAP |
877 if (use_mmap && data != (const char*)mmapped_memory) { | 884 if (use_mmap && data != (const char*)mmapped_memory) { |
878 // すでに mmap は必要ない | 885 // すでに mmap は必要ない |
879 munmap(mmapped_memory, info.arcsize); | 886 munmap(mmapped_memory, info.arcsize); |
890 ** 画像展開系クラスの定義、実装 | 897 ** 画像展開系クラスの定義、実装 |
891 ** | 898 ** |
892 *********************************************** | 899 *********************************************** |
893 */ | 900 */ |
894 GRPCONV::GRPCONV(void) { | 901 GRPCONV::GRPCONV(void) { |
895 filename = 0; | 902 filename = NULL; |
896 data = 0; | 903 data = NULL; |
897 } | 904 } |
905 | |
898 GRPCONV::~GRPCONV() { | 906 GRPCONV::~GRPCONV() { |
899 if (filename) delete[] filename; | 907 if (filename) delete[] filename; |
900 } | 908 } |
909 | |
901 void GRPCONV::Init(const char* f, const char* d, int dlen, int w, int h, bool is_m) { | 910 void GRPCONV::Init(const char* f, const char* d, int dlen, int w, int h, bool is_m) { |
902 if (filename) delete[] filename; | 911 if (filename) delete[] filename; |
903 if (f == 0) { | 912 if (f == NULL) { |
904 char* fn = new char[1]; | 913 char* fn = new char[1]; |
905 fn[0] = 0; | 914 fn[0] = 0; |
906 filename = fn; | 915 filename = fn; |
907 } else { | 916 } else { |
908 char* fn = new char[strlen(f)+1]; | 917 char* fn = new char[strlen(f)+1]; |
914 datalen = dlen; | 923 datalen = dlen; |
915 width = w; | 924 width = w; |
916 height = h; | 925 height = h; |
917 is_mask = is_m; | 926 is_mask = is_m; |
918 } | 927 } |
928 | |
919 class PDTCONV : public GRPCONV { | 929 class PDTCONV : public GRPCONV { |
920 bool Read_PDT10(char* image); | 930 private: |
921 bool Read_PDT11(char* image); | 931 bool Read_PDT10(char* image); |
922 public: | 932 bool Read_PDT11(char* image); |
923 PDTCONV(const char* _inbuf, int inlen, const char* fname); | 933 public: |
924 ~PDTCONV() {} | 934 PDTCONV(const char* _inbuf, int inlen, const char* fname); |
925 bool Read(char* image); | 935 ~PDTCONV() {} |
936 bool Read(char* image); | |
926 }; | 937 }; |
938 | |
927 class G00CONV : public GRPCONV { | 939 class G00CONV : public GRPCONV { |
928 struct REGION { | 940 private: |
929 int x1, y1, x2, y2; | 941 struct REGION { |
930 int Width() { return x2-x1+1;} | 942 int x1, y1, x2, y2; |
931 int Height() { return y2-y1+1;} | 943 int Width() { return x2-x1+1;} |
932 void FixVar(int& v, int& w) { | 944 int Height() { return y2-y1+1;} |
933 if (v < 0) v = 0; | 945 void FixVar(int& v, int& w) { |
934 if (v >= w) v = w-1; | 946 if (v < 0) v = 0; |
935 } | 947 if (v >= w) v = w-1; |
936 void Fix(int w, int h) { | 948 } |
937 FixVar(x1,w); | 949 void Fix(int w, int h) { |
938 FixVar(x2,w); | 950 FixVar(x1,w); |
939 FixVar(y1,h); | 951 FixVar(x2,w); |
940 FixVar(y2,h); | 952 FixVar(y1,h); |
941 if (x1 > x2) x2 = x1; | 953 FixVar(y2,h); |
942 if (y1 > y2) y2 = y1; | 954 if (x1 > x2) x2 = x1; |
943 } | 955 if (y1 > y2) y2 = y1; |
944 }; | 956 } |
945 | 957 }; |
946 void Copy_16bpp(char* image, int x, int y, const char* src, int bpl, int h); | 958 |
947 void Copy_32bpp(char* image, int x, int y, const char* src, int bpl, int h); | 959 void Copy_16bpp(char* image, int x, int y, const char* src, int bpl, int h); |
948 bool Read_Type0(char* image); | 960 void Copy_32bpp(char* image, int x, int y, const char* src, int bpl, int h); |
949 bool Read_Type1(char* image); | 961 bool Read_Type0(char* image); |
950 bool Read_Type2(char* image); | 962 bool Read_Type1(char* image); |
951 public: | 963 bool Read_Type2(char* image); |
952 G00CONV(const char* _inbuf, int _inlen, const char* fname); | 964 public: |
953 ~G00CONV() { } | 965 G00CONV(const char* _inbuf, int _inlen, const char* fname); |
954 bool Read(char* image); | 966 ~G00CONV() { } |
967 bool Read(char* image); | |
955 }; | 968 }; |
956 | 969 |
957 class BMPCONV : public GRPCONV { | 970 class BMPCONV : public GRPCONV { |
958 public: | 971 public: |
959 BMPCONV(const char* _inbuf, int _inlen, const char* fname); | 972 BMPCONV(const char* _inbuf, int _inlen, const char* fname); |
960 ~BMPCONV() {}; | 973 ~BMPCONV() {}; |
961 bool Read(char* image); | 974 bool Read(char* image); |
962 }; | 975 }; |
976 | |
963 #if HAVE_LIBPNG | 977 #if HAVE_LIBPNG |
964 class PNGCONV : public GRPCONV { | 978 class PNGCONV : public GRPCONV { |
965 const char* png_data; | 979 private: |
966 static void png_read(png_structp, png_bytep, png_size_t); | 980 const char* png_data; |
967 | 981 static void png_read(png_structp, png_bytep, png_size_t); |
968 public: | 982 |
969 PNGCONV(const char* _inbuf, int _inlen, const char* fname); | 983 public: |
970 ~PNGCONV() {}; | 984 PNGCONV(const char* _inbuf, int _inlen, const char* fname); |
971 bool Read(char* image); | 985 ~PNGCONV() {}; |
986 bool Read(char* image); | |
972 }; | 987 }; |
973 #endif | 988 #endif |
974 | 989 |
975 #if HAVE_LIBJPEG | 990 #if HAVE_LIBJPEG |
976 class JPEGCONV : public GRPCONV { | 991 class JPEGCONV : public GRPCONV { |
977 | 992 public: |
978 public: | 993 JPEGCONV(const char* _inbuf, int _inlen, const char* fname); |
979 JPEGCONV(const char* _inbuf, int _inlen, const char* fname); | 994 ~JPEGCONV() {}; |
980 ~JPEGCONV() {}; | 995 bool Read(char* image); |
981 bool Read(char* image); | 996 void SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size); |
982 void SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size); | 997 static void init_source(j_decompress_ptr cinfo); |
983 static void init_source(j_decompress_ptr cinfo); | 998 static boolean fill_input_buffer(j_decompress_ptr cinfo); |
984 static boolean fill_input_buffer(j_decompress_ptr cinfo); | 999 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes); |
985 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes); | 1000 static boolean resync_to_restart(j_decompress_ptr cinfo, int desired); |
986 static boolean resync_to_restart(j_decompress_ptr cinfo, int desired); | 1001 static void term_source(j_decompress_ptr cinf); |
987 static void term_source(j_decompress_ptr cinf); | |
988 }; | 1002 }; |
989 #endif | 1003 #endif |
990 | 1004 |
991 GRPCONV* GRPCONV::AssignConverter(const char* inbuf, int inlen, const char* fname) { | 1005 GRPCONV* GRPCONV::AssignConverter(const char* inbuf, int inlen, const char* fname) { |
992 /* ファイルの内容に応じたコンバーターを割り当てる */ | 1006 /* ファイルの内容に応じたコンバーターを割り当てる */ |
993 GRPCONV* conv = 0; | 1007 GRPCONV* conv = NULL; |
994 if (inlen < 10) return 0; /* invalid file */ | 1008 if (inlen < 10) return NULL; /* invalid file */ |
995 if (strncmp(inbuf, "PDT10", 5) == 0 || strncmp(inbuf, "PDT11", 5) == 0) { /* PDT10 or PDT11 */ | 1009 if (strncmp(inbuf, "PDT10", 5) == 0 || strncmp(inbuf, "PDT11", 5) == 0) { /* PDT10 or PDT11 */ |
996 conv = new PDTCONV(inbuf, inlen, fname); | 1010 conv = new PDTCONV(inbuf, inlen, fname); |
997 if (conv->data == 0) { delete conv; conv = 0;} | 1011 if (conv->data == NULL) { |
1012 delete conv; | |
1013 conv = NULL; | |
1014 } | |
998 } | 1015 } |
999 #if HAVE_LIBPNG | 1016 #if HAVE_LIBPNG |
1000 unsigned char png_magic[4] = {0x89, 'P', 'N', 'G'}; | 1017 unsigned char png_magic[4] = {0x89, 'P', 'N', 'G'}; |
1001 if (conv == 0 && memcmp(inbuf, png_magic,4) == 0) { | 1018 if (conv == NULL && memcmp(inbuf, png_magic,4) == 0) { |
1002 conv = new PNGCONV(inbuf, inlen, fname); | 1019 conv = new PNGCONV(inbuf, inlen, fname); |
1003 if (conv->data == 0) { delete conv; conv = 0;} | 1020 if (conv->data == NULL) { |
1021 delete conv; | |
1022 conv = NULL; | |
1023 } | |
1004 } | 1024 } |
1005 #endif | 1025 #endif |
1006 #if HAVE_LIBJPEG | 1026 #if HAVE_LIBJPEG |
1007 if ( conv == 0 && *(unsigned char*)inbuf == 0xff && *(unsigned char*)(inbuf+1) == 0xd8 && | 1027 if ( conv == NULL && *(unsigned char*)inbuf == 0xff && *(unsigned char*)(inbuf+1) == 0xd8 && |
1008 (strncmp(inbuf+6, "JFIF",4) == 0 || strncmp(inbuf+6,"Exif",4) == 0)) { | 1028 (strncmp(inbuf+6, "JFIF",4) == 0 || strncmp(inbuf+6,"Exif",4) == 0)) { |
1009 conv = new JPEGCONV(inbuf, inlen, fname); | 1029 conv = new JPEGCONV(inbuf, inlen, fname); |
1010 if (conv->data == 0) { delete conv; conv = 0;} | 1030 if (conv->data == NULL) { |
1031 delete conv; | |
1032 conv = NULL; | |
1033 } | |
1011 } | 1034 } |
1012 #endif | 1035 #endif |
1013 if (conv == 0 && inbuf[0]=='B' && inbuf[1]=='M' && read_little_endian_int(inbuf+10)==0x36 && read_little_endian_int(inbuf+14) == 0x28) { // Windows BMP | 1036 if (conv == NULL && inbuf[0]=='B' && inbuf[1]=='M' && read_little_endian_int(inbuf+10)==0x36 && read_little_endian_int(inbuf+14) == 0x28) { // Windows BMP |
1014 conv = new BMPCONV(inbuf, inlen, fname); | 1037 conv = new BMPCONV(inbuf, inlen, fname); |
1015 if (conv->data == 0) { delete conv; conv = 0;} | 1038 if (conv->data == NULL) { |
1016 } | 1039 delete conv; |
1017 if (conv == 0 && (inbuf[0] == 0 || inbuf[0] == 1 || inbuf[0] == 2)) { /* G00 */ | 1040 conv = NULL; |
1041 } | |
1042 } | |
1043 if (conv == NULL && (inbuf[0] == 0 || inbuf[0] == 1 || inbuf[0] == 2)) { /* G00 */ | |
1018 conv = new G00CONV(inbuf, inlen, fname); | 1044 conv = new G00CONV(inbuf, inlen, fname); |
1019 if (conv->data == 0) { delete conv; conv = 0;} | 1045 if (conv->data == NULL) { |
1046 delete conv; | |
1047 conv = NULL; | |
1048 } | |
1020 } | 1049 } |
1021 return conv; | 1050 return conv; |
1022 } | 1051 } |
1023 | 1052 |
1024 PDTCONV::PDTCONV(const char* _inbuf, int _inlen,const char* filename) { | 1053 PDTCONV::PDTCONV(const char* _inbuf, int _inlen,const char* filename) { |
1048 | 1077 |
1049 int w = read_little_endian_int(_inbuf+0x0c); | 1078 int w = read_little_endian_int(_inbuf+0x0c); |
1050 int h = read_little_endian_int(_inbuf+0x10); | 1079 int h = read_little_endian_int(_inbuf+0x10); |
1051 int mask_pt = read_little_endian_int(_inbuf + 0x1c); | 1080 int mask_pt = read_little_endian_int(_inbuf + 0x1c); |
1052 Init(filename, _inbuf, _inlen, w, h, mask_pt ? true : false); | 1081 Init(filename, _inbuf, _inlen, w, h, mask_pt ? true : false); |
1053 | |
1054 return; | |
1055 } | 1082 } |
1056 | 1083 |
1057 | 1084 |
1058 G00CONV::G00CONV(const char* _inbuf, int _inlen, const char* filename) { | 1085 G00CONV::G00CONV(const char* _inbuf, int _inlen, const char* filename) { |
1059 // G00 FILE のヘッダ | 1086 // G00 FILE のヘッダ |
1109 } | 1136 } |
1110 return; | 1137 return; |
1111 } | 1138 } |
1112 | 1139 |
1113 bool G00CONV::Read(char* image) { | 1140 bool G00CONV::Read(char* image) { |
1114 if (data == 0) return false; | 1141 if (data == NULL) return false; |
1115 /* header 識別 */ | 1142 /* header 識別 */ |
1116 int type = *data; | 1143 int type = *data; |
1117 if (type == 0) return Read_Type0(image); | 1144 if (type == 0) return Read_Type0(image); |
1118 else if (type == 1) return Read_Type1(image); | 1145 else if (type == 1) return Read_Type1(image); |
1119 else if (type == 2) return Read_Type2(image); | 1146 else if (type == 2) return Read_Type2(image); |
1138 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, | 1165 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, |
1139 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, | 1166 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, |
1140 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, | 1167 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, |
1141 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, | 1168 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, |
1142 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff}; | 1169 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff}; |
1170 | |
1143 template<class DataType, class DataSize> inline int lzExtract(DataType& datatype,const char*& src, char*& dest, const char* srcend, char* destend) { | 1171 template<class DataType, class DataSize> inline int lzExtract(DataType& datatype,const char*& src, char*& dest, const char* srcend, char* destend) { |
1144 int count = 0; | 1172 int count = 0; |
1145 const char* lsrcend = srcend; char* ldestend = destend; | 1173 const char* lsrcend = srcend; char* ldestend = destend; |
1146 const char* lsrc = src; char* ldest = dest; | 1174 const char* lsrc = src; char* ldest = dest; |
1147 | 1175 |
1194 } | 1222 } |
1195 } | 1223 } |
1196 dest=ldest; src=lsrc; | 1224 dest=ldest; src=lsrc; |
1197 return 0; | 1225 return 0; |
1198 } | 1226 } |
1227 | |
1199 /* 引数を減らすためのwrapper */ | 1228 /* 引数を減らすためのwrapper */ |
1200 template<class DataType, class DataSize> inline int lzExtract(DataType datatype, DataSize datasize ,const char*& src, char*& dest, const char* srcend, char* destend) { | 1229 template<class DataType, class DataSize> inline int lzExtract(DataType datatype, DataSize datasize ,const char*& src, char*& dest, const char* srcend, char* destend) { |
1201 return lzExtract<DataType, DataSize>(datatype,src,dest,srcend,destend); | 1230 return lzExtract<DataType, DataSize>(datatype,src,dest,srcend,destend); |
1202 } | 1231 } |
1203 | 1232 |
1204 /* 普通の PDT */ | 1233 /* 普通の PDT */ |
1205 class Extract_DataType { | 1234 class Extract_DataType { |
1206 public: | 1235 public: |
1207 static void ExtractData(const char*& lsrc, int& data, int& size) { | 1236 static void ExtractData(const char*& lsrc, int& data, int& size) { |
1208 data = read_little_endian_short(lsrc) & 0xffff; | 1237 data = read_little_endian_short(lsrc) & 0xffff; |
1209 size = (data & 0x0f) + 1; | 1238 size = (data & 0x0f) + 1; |
1210 data = (data>>4)+1; | 1239 data = (data>>4)+1; |
1211 lsrc += 2; | 1240 lsrc += 2; |
1212 } | 1241 } |
1213 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | 1242 static void Copy1Pixel(const char*& lsrc, char*& ldest) { |
1214 #ifdef WORDS_BIGENDIAN | 1243 #ifdef WORDS_BIGENDIAN |
1215 ldest[3] = lsrc[0]; | 1244 ldest[3] = lsrc[0]; |
1216 ldest[2] = lsrc[1]; | 1245 ldest[2] = lsrc[1]; |
1217 ldest[1] = lsrc[2]; | 1246 ldest[1] = lsrc[2]; |
1218 ldest[0] = 0; | 1247 ldest[0] = 0; |
1219 #else | 1248 #else |
1220 *(int*)ldest = read_little_endian_int(lsrc); ldest[3]=0; | 1249 *(int*)ldest = read_little_endian_int(lsrc); ldest[3]=0; |
1221 #endif | 1250 #endif |
1222 lsrc += 3; ldest += 4; | 1251 lsrc += 3; ldest += 4; |
1223 } | 1252 } |
1224 static int IsRev(void) { return 0; } | 1253 static int IsRev(void) { return 0; } |
1225 }; | 1254 }; |
1226 | 1255 |
1227 /* PDT11 の第一段階変換 */ | 1256 /* PDT11 の第一段階変換 */ |
1228 class Extract_DataType_PDT11 { | 1257 class Extract_DataType_PDT11 { |
1229 int* index_table; | 1258 private: |
1230 public: | 1259 int* index_table; |
1231 Extract_DataType_PDT11(int* it) { index_table = it; } | 1260 public: |
1232 void ExtractData(const char*& lsrc, int& data, int& size) { | 1261 Extract_DataType_PDT11(int* it) { index_table = it; } |
1233 data = int(*(const unsigned char*)lsrc); | 1262 void ExtractData(const char*& lsrc, int& data, int& size) { |
1234 size = (data>>4) + 2; | 1263 data = int(*(const unsigned char*)lsrc); |
1235 data = index_table[data&0x0f]; | 1264 size = (data>>4) + 2; |
1236 lsrc++; | 1265 data = index_table[data&0x0f]; |
1237 } | 1266 lsrc++; |
1238 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | 1267 } |
1239 *ldest = *lsrc; | 1268 static void Copy1Pixel(const char*& lsrc, char*& ldest) { |
1240 ldest++; lsrc++; | 1269 *ldest = *lsrc; |
1241 } | 1270 ldest++; lsrc++; |
1242 static int IsRev(void) { return 0; } | 1271 } |
1272 static int IsRev(void) { return 0; } | |
1243 }; | 1273 }; |
1274 | |
1244 /* マスク用 */ | 1275 /* マスク用 */ |
1245 class Extract_DataType_Mask { | 1276 class Extract_DataType_Mask { |
1246 public: | 1277 public: |
1247 void ExtractData(const char*& lsrc, int& data, int& size) { | 1278 void ExtractData(const char*& lsrc, int& data, int& size) { |
1248 int d = read_little_endian_short(lsrc) & 0xffff; | 1279 int d = read_little_endian_short(lsrc) & 0xffff; |
1249 size = (d & 0xff) + 2; | 1280 size = (d & 0xff) + 2; |
1250 data = (d>>8)+1; | 1281 data = (d>>8)+1; |
1251 lsrc += 2; | 1282 lsrc += 2; |
1252 } | 1283 } |
1253 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | 1284 static void Copy1Pixel(const char*& lsrc, char*& ldest) { |
1254 *ldest = *lsrc; | 1285 *ldest = *lsrc; |
1255 ldest++; lsrc++; | 1286 ldest++; lsrc++; |
1256 } | 1287 } |
1257 static int IsRev(void) { return 0; } | 1288 static int IsRev(void) { return 0; } |
1258 }; | 1289 }; |
1290 | |
1259 /* 書庫用 */ | 1291 /* 書庫用 */ |
1260 class Extract_DataType_ARC { | 1292 class Extract_DataType_ARC { |
1261 public: | 1293 public: |
1262 void ExtractData(const char*& lsrc, int& data, int& size) { | 1294 void ExtractData(const char*& lsrc, int& data, int& size) { |
1263 data = read_little_endian_short(lsrc) & 0xffff; | 1295 data = read_little_endian_short(lsrc) & 0xffff; |
1264 size = (data&0x0f) + 2; | 1296 size = (data&0x0f) + 2; |
1265 data = (data>>4) + 1; | 1297 data = (data>>4) + 1; |
1266 lsrc+= 2; | 1298 lsrc+= 2; |
1267 } | 1299 } |
1268 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | 1300 static void Copy1Pixel(const char*& lsrc, char*& ldest) { |
1269 *ldest = *lsrc; | 1301 *ldest = *lsrc; |
1270 ldest++; lsrc++; | 1302 ldest++; lsrc++; |
1271 } | 1303 } |
1272 static int IsRev(void) { return 0; } | 1304 static int IsRev(void) { return 0; } |
1273 }; | 1305 }; |
1306 | |
1274 /* avg2000 のシナリオ用 */ | 1307 /* avg2000 のシナリオ用 */ |
1275 class Extract_DataType_SCN2k { | 1308 class Extract_DataType_SCN2k { |
1276 public: | 1309 public: |
1277 void ExtractData(const char*& lsrc, int& data, int& size) { | 1310 void ExtractData(const char*& lsrc, int& data, int& size) { |
1278 data = read_little_endian_short(lsrc) & 0xffff; | 1311 data = read_little_endian_short(lsrc) & 0xffff; |
1279 size = (data&0x0f) + 2; | 1312 size = (data&0x0f) + 2; |
1280 data = (data>>4); | 1313 data = (data>>4); |
1281 lsrc+= 2; | 1314 lsrc+= 2; |
1282 } | 1315 } |
1283 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | 1316 static void Copy1Pixel(const char*& lsrc, char*& ldest) { |
1284 *ldest = *lsrc; | 1317 *ldest = *lsrc; |
1285 ldest++; lsrc++; | 1318 ldest++; lsrc++; |
1286 } | 1319 } |
1287 static int IsRev(void) { return 1; } | 1320 static int IsRev(void) { return 1; } |
1288 }; | 1321 }; |
1322 | |
1289 /* ReadLive の type0 */ | 1323 /* ReadLive の type0 */ |
1290 class Extract_DataType_G00Type0 { | 1324 class Extract_DataType_G00Type0 { |
1291 public: | 1325 public: |
1292 static void ExtractData(const char*& lsrc, int& data, int& size) { | 1326 static void ExtractData(const char*& lsrc, int& data, int& size) { |
1293 data = read_little_endian_short(lsrc) & 0xffff; | 1327 data = read_little_endian_short(lsrc) & 0xffff; |
1294 size = ((data & 0x0f)+ 1) * 3; | 1328 size = ((data & 0x0f)+ 1) * 3; |
1295 data = (data>>4) * 3; | 1329 data = (data>>4) * 3; |
1296 lsrc += 2; | 1330 lsrc += 2; |
1297 } | 1331 } |
1298 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | 1332 static void Copy1Pixel(const char*& lsrc, char*& ldest) { |
1299 #ifdef WORDS_BIGENDIAN | 1333 #ifdef WORDS_BIGENDIAN |
1300 ldest[0] = lsrc[0]; | 1334 ldest[0] = lsrc[0]; |
1301 ldest[1] = lsrc[1]; | 1335 ldest[1] = lsrc[1]; |
1302 ldest[2] = lsrc[2]; | 1336 ldest[2] = lsrc[2]; |
1303 #else /* LITTLE ENDIAN / intel architecture */ | 1337 #else /* LITTLE ENDIAN / intel architecture */ |
1304 *(int*)ldest = *(int*)lsrc; | 1338 *(int*)ldest = *(int*)lsrc; |
1305 #endif | 1339 #endif |
1306 lsrc += 3; ldest += 3; | 1340 lsrc += 3; ldest += 3; |
1307 } | 1341 } |
1308 static int IsRev(void) { return 1; } | 1342 static int IsRev(void) { return 1; } |
1309 }; | 1343 }; |
1310 | 1344 |
1311 | 1345 |
1312 bool PDTCONV::Read(char* image) { | 1346 bool PDTCONV::Read(char* image) { |
1313 if (data == 0) return false; | 1347 if (data == NULL) return false; |
1314 | 1348 |
1315 if (strncmp(data, "PDT10", 5) == 0) { | 1349 if (strncmp(data, "PDT10", 5) == 0) { |
1316 if (! Read_PDT10(image)) return false; | 1350 if (! Read_PDT10(image)) return false; |
1317 } else if (strncmp(data, "PDT11", 5) == 0) { | 1351 } else if (strncmp(data, "PDT11", 5) == 0) { |
1318 if (! Read_PDT11(image)) return false; | 1352 if (! Read_PDT11(image)) return false; |
1351 | 1385 |
1352 destend = image + width*height*4; | 1386 destend = image + width*height*4; |
1353 while(lzExtract(Extract_DataType(), int(), src, dest, srcend, destend)) ; | 1387 while(lzExtract(Extract_DataType(), int(), src, dest, srcend, destend)) ; |
1354 return true; | 1388 return true; |
1355 } | 1389 } |
1390 | |
1356 bool PDTCONV::Read_PDT11(char* image) { | 1391 bool PDTCONV::Read_PDT11(char* image) { |
1357 int index_table[16]; | 1392 int index_table[16]; |
1358 int color_table[256]; | 1393 int color_table[256]; |
1359 int i; | 1394 int i; |
1360 for (i=0; i<16; i++) | 1395 for (i=0; i<16; i++) |
1389 */ | 1424 */ |
1390 void ARCINFO::Extract(char*& dest_start, char*& src_start, char* dest_end, char* src_end) { | 1425 void ARCINFO::Extract(char*& dest_start, char*& src_start, char* dest_end, char* src_end) { |
1391 const char* src = src_start; | 1426 const char* src = src_start; |
1392 while (lzExtract(Extract_DataType_ARC(), char(), src, dest_start, src_end, dest_end)) ; | 1427 while (lzExtract(Extract_DataType_ARC(), char(), src, dest_start, src_end, dest_end)) ; |
1393 src_start = (char*)src; | 1428 src_start = (char*)src; |
1394 return; | 1429 } |
1395 } | 1430 |
1396 void ARCINFO::Extract2k(char*& dest_start, char*& src_start, char* dest_end, char* src_end) { | 1431 void ARCINFO::Extract2k(char*& dest_start, char*& src_start, char* dest_end, char* src_end) { |
1397 const char* src = src_start; | 1432 const char* src = src_start; |
1398 while (lzExtract(Extract_DataType_SCN2k(), char(), src, dest_start, src_end, dest_end)) ; | 1433 while (lzExtract(Extract_DataType_SCN2k(), char(), src, dest_start, src_end, dest_end)) ; |
1399 src_start = (char*)src; | 1434 src_start = (char*)src; |
1400 return; | |
1401 } | 1435 } |
1402 | 1436 |
1403 bool ARCINFO_AVG32::ExecExtract(void) { | 1437 bool ARCINFO_AVG32::ExecExtract(void) { |
1404 // ヘッダのチェック | 1438 // ヘッダのチェック |
1405 if (strncmp(data, "PACK", 4) != 0) return false; | 1439 if (strncmp(data, "PACK", 4) != 0) return false; |
1494 // image にコピー | 1528 // image にコピー |
1495 CopyRGB(image, uncompress_data); | 1529 CopyRGB(image, uncompress_data); |
1496 delete[] uncompress_data; | 1530 delete[] uncompress_data; |
1497 return true; | 1531 return true; |
1498 } | 1532 } |
1533 | |
1499 bool G00CONV::Read_Type1(char* image) { | 1534 bool G00CONV::Read_Type1(char* image) { |
1500 int i; | 1535 int i; |
1501 int uncompress_size = read_little_endian_int(data+9) + 1; | 1536 int uncompress_size = read_little_endian_int(data+9) + 1; |
1502 char* uncompress_data = new char[uncompress_size + 1024]; | 1537 char* uncompress_data = new char[uncompress_size + 1024]; |
1503 | 1538 |
1609 int* d = (int*)image; | 1644 int* d = (int*)image; |
1610 for(i=0; i<len; i++) { | 1645 for(i=0; i<len; i++) { |
1611 *d = (int(s[2])) | (int(s[1])<<8) | (int(s[0])<<16) | (int(s[3])<<24) | mask; | 1646 *d = (int(s[2])) | (int(s[1])<<8) | (int(s[0])<<16) | (int(s[3])<<24) | mask; |
1612 d++; s += 4; | 1647 d++; s += 4; |
1613 } | 1648 } |
1614 return; | |
1615 } | 1649 } |
1616 | 1650 |
1617 void GRPCONV::CopyRGBA(char* image, const char* buf) { | 1651 void GRPCONV::CopyRGBA(char* image, const char* buf) { |
1618 if (!is_mask) { | 1652 if (!is_mask) { |
1619 CopyRGB(image, buf); | 1653 CopyRGB(image, buf); |
1625 int* outbuf = (int*)image; | 1659 int* outbuf = (int*)image; |
1626 for(i=0; i<len; i++) { | 1660 for(i=0; i<len; i++) { |
1627 *outbuf++ = read_little_endian_int(buf); | 1661 *outbuf++ = read_little_endian_int(buf); |
1628 buf += 4; | 1662 buf += 4; |
1629 } | 1663 } |
1630 return; | 1664 } |
1631 } | 1665 |
1632 void GRPCONV::CopyRGB(char* image, const char* buf) { | 1666 void GRPCONV::CopyRGB(char* image, const char* buf) { |
1633 /* 色変換を行う */ | 1667 /* 色変換を行う */ |
1634 int len = width * height; | 1668 int len = width * height; |
1635 int i; | 1669 int i; |
1636 unsigned char* s = (unsigned char*)buf; | 1670 unsigned char* s = (unsigned char*)buf; |
1637 int* d = (int*)image; | 1671 int* d = (int*)image; |
1638 for(i=0; i<len; i++) { | 1672 for(i=0; i<len; i++) { |
1639 *d = (int(s[0])) | (int(s[1])<<8) | (int(s[2])<<16) | 0xff000000; | 1673 *d = (int(s[0])) | (int(s[1])<<8) | (int(s[2])<<16) | 0xff000000; |
1640 d++; s+=3; | 1674 d++; s+=3; |
1641 } | 1675 } |
1642 return; | |
1643 } | 1676 } |
1644 | 1677 |
1645 #if HAVE_LIBPNG | 1678 #if HAVE_LIBPNG |
1646 PNGCONV::PNGCONV(const char* _inbuf, int _inlen, const char* _filename) { | 1679 PNGCONV::PNGCONV(const char* _inbuf, int _inlen, const char* _filename) { |
1647 int w,h,type; | 1680 int w,h,type; |
1648 png_structp png_ptr = 0; | 1681 png_structp png_ptr = NULL; |
1649 png_infop info_ptr = 0; | 1682 png_infop info_ptr = NULL; |
1650 png_infop end_info = 0; | 1683 png_infop end_info = NULL; |
1651 | 1684 |
1652 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | 1685 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
1653 if (!png_ptr) return; | 1686 if (!png_ptr) return; |
1654 | 1687 |
1655 info_ptr = png_create_info_struct(png_ptr); | 1688 info_ptr = png_create_info_struct(png_ptr); |
1684 else if (info_ptr) | 1717 else if (info_ptr) |
1685 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0); | 1718 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0); |
1686 else | 1719 else |
1687 png_destroy_read_struct(&png_ptr, (png_infopp) 0,(png_infopp)0); | 1720 png_destroy_read_struct(&png_ptr, (png_infopp) 0,(png_infopp)0); |
1688 } | 1721 } |
1689 return; | |
1690 } | 1722 } |
1691 | 1723 |
1692 bool PNGCONV::Read(char* image) { | 1724 bool PNGCONV::Read(char* image) { |
1693 if (data == 0) return false; | 1725 if (data == NULL) return false; |
1694 bool retcode = false; | 1726 bool retcode = false; |
1695 int bpp = is_mask ? 4 : 3; | 1727 int bpp = is_mask ? 4 : 3; |
1696 int i; | 1728 int i; |
1697 char* buf; | 1729 char* buf; |
1698 png_bytepp row_pointers = 0; | 1730 png_bytepp row_pointers = NULL; |
1699 | 1731 |
1700 png_structp png_ptr = 0; | 1732 png_structp png_ptr = NULL; |
1701 png_infop info_ptr = 0; | 1733 png_infop info_ptr = NULL; |
1702 png_infop end_info = 0; | 1734 png_infop end_info = NULL; |
1703 | 1735 |
1704 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | 1736 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
1705 if (!png_ptr) goto err; | 1737 if (!png_ptr) goto err; |
1706 info_ptr = png_create_info_struct(png_ptr); | 1738 info_ptr = png_create_info_struct(png_ptr); |
1707 if (!info_ptr) goto err; | 1739 if (!info_ptr) goto err; |
1746 | 1778 |
1747 void PNGCONV::png_read(png_structp png_ptr, png_bytep d, png_size_t sz) { | 1779 void PNGCONV::png_read(png_structp png_ptr, png_bytep d, png_size_t sz) { |
1748 PNGCONV* orig = (PNGCONV*)png_get_io_ptr(png_ptr); | 1780 PNGCONV* orig = (PNGCONV*)png_get_io_ptr(png_ptr); |
1749 memcpy(d, orig->png_data, sz); | 1781 memcpy(d, orig->png_data, sz); |
1750 orig->png_data += sz; | 1782 orig->png_data += sz; |
1751 return; | |
1752 } | 1783 } |
1753 #endif /* HAVE_LIBPNG */ | 1784 #endif /* HAVE_LIBPNG */ |
1754 | 1785 |
1755 #if HAVE_LIBJPEG | 1786 #if HAVE_LIBJPEG |
1756 JPEGCONV::JPEGCONV(const char* _inbuf, int _inlen, const char* _filename) { | 1787 JPEGCONV::JPEGCONV(const char* _inbuf, int _inlen, const char* _filename) { |
1763 | 1794 |
1764 if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) { | 1795 if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) { |
1765 Init(filename, _inbuf, _inlen, cinfo.image_width, cinfo.image_height, false); | 1796 Init(filename, _inbuf, _inlen, cinfo.image_width, cinfo.image_height, false); |
1766 } | 1797 } |
1767 delete cinfo.src; | 1798 delete cinfo.src; |
1768 cinfo.src = 0; | 1799 cinfo.src = NULL; |
1769 jpeg_destroy_decompress(&cinfo); | 1800 jpeg_destroy_decompress(&cinfo); |
1770 return; | |
1771 } | 1801 } |
1772 | 1802 |
1773 bool JPEGCONV::Read(char* image) { | 1803 bool JPEGCONV::Read(char* image) { |
1774 if (data == 0) return false; | 1804 if (data == NULL) return false; |
1775 bool retcode = false; | 1805 bool retcode = false; |
1776 JSAMPARRAY rows, rows_orig; int i; | 1806 JSAMPARRAY rows, rows_orig; |
1777 char* buf = 0; | 1807 int i; |
1808 char* buf = NULL; | |
1778 | 1809 |
1779 struct jpeg_decompress_struct cinfo; | 1810 struct jpeg_decompress_struct cinfo; |
1780 struct jpeg_error_mgr jerr; | 1811 struct jpeg_error_mgr jerr; |
1781 cinfo.err = jpeg_std_error(&jerr); | 1812 cinfo.err = jpeg_std_error(&jerr); |
1782 jpeg_create_decompress(&cinfo); | 1813 jpeg_create_decompress(&cinfo); |
1805 | 1836 |
1806 jpeg_finish_decompress(&cinfo); | 1837 jpeg_finish_decompress(&cinfo); |
1807 retcode = true; | 1838 retcode = true; |
1808 err: | 1839 err: |
1809 delete cinfo.src; | 1840 delete cinfo.src; |
1810 cinfo.src = 0; | 1841 cinfo.src = NULL; |
1811 jpeg_destroy_decompress(&cinfo); | 1842 jpeg_destroy_decompress(&cinfo); |
1812 return retcode; | 1843 return retcode; |
1813 } | 1844 } |
1814 | 1845 |
1815 void JPEGCONV::init_source(j_decompress_ptr cinfo) { | 1846 void JPEGCONV::init_source(j_decompress_ptr cinfo) { |
1816 } | 1847 } |
1848 | |
1817 boolean JPEGCONV::fill_input_buffer(j_decompress_ptr cinfo) { | 1849 boolean JPEGCONV::fill_input_buffer(j_decompress_ptr cinfo) { |
1818 static char dummy[1024]; | 1850 static char dummy[1024]; |
1819 memset(dummy, 0, 1024); | 1851 memset(dummy, 0, 1024); |
1820 cinfo->src->next_input_byte = (const JOCTET*)dummy; | 1852 cinfo->src->next_input_byte = (const JOCTET*)dummy; |
1821 cinfo->src->bytes_in_buffer = 1024; | 1853 cinfo->src->bytes_in_buffer = 1024; |
1822 fprintf(stderr,"JPEGCONV::fill_input_buffer: warning corrupted jpeg stream\n"); | 1854 fprintf(stderr,"JPEGCONV::fill_input_buffer: warning corrupted jpeg stream\n"); |
1823 return TRUE; | 1855 return TRUE; |
1824 } | 1856 } |
1857 | |
1825 void JPEGCONV::skip_input_data(j_decompress_ptr cinfo, long num_bytes) { | 1858 void JPEGCONV::skip_input_data(j_decompress_ptr cinfo, long num_bytes) { |
1826 if (cinfo->src->bytes_in_buffer > num_bytes) { | 1859 if (cinfo->src->bytes_in_buffer > num_bytes) { |
1827 cinfo->src->next_input_byte += num_bytes; | 1860 cinfo->src->next_input_byte += num_bytes; |
1828 cinfo->src->bytes_in_buffer -= num_bytes; | 1861 cinfo->src->bytes_in_buffer -= num_bytes; |
1829 } | 1862 } |
1830 } | 1863 } |
1864 | |
1831 boolean JPEGCONV::resync_to_restart(j_decompress_ptr cinfo, int desired) { | 1865 boolean JPEGCONV::resync_to_restart(j_decompress_ptr cinfo, int desired) { |
1832 return jpeg_resync_to_restart(cinfo, desired); | 1866 return jpeg_resync_to_restart(cinfo, desired); |
1833 } | 1867 } |
1868 | |
1834 void JPEGCONV::term_source(j_decompress_ptr cinf) { | 1869 void JPEGCONV::term_source(j_decompress_ptr cinf) { |
1835 } | 1870 } |
1836 | 1871 |
1837 void JPEGCONV::SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size) { | 1872 void JPEGCONV::SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size) { |
1838 cinfo->src->next_input_byte = (const JOCTET*) data; | 1873 cinfo->src->next_input_byte = (const JOCTET*) data; |
1841 cinfo->src->fill_input_buffer = fill_input_buffer; | 1876 cinfo->src->fill_input_buffer = fill_input_buffer; |
1842 cinfo->src->skip_input_data = skip_input_data; | 1877 cinfo->src->skip_input_data = skip_input_data; |
1843 cinfo->src->resync_to_restart = resync_to_restart; | 1878 cinfo->src->resync_to_restart = resync_to_restart; |
1844 cinfo->src->term_source = term_source; | 1879 cinfo->src->term_source = term_source; |
1845 } | 1880 } |
1881 | |
1846 #endif /* HAVE_LIBJPEG */ | 1882 #endif /* HAVE_LIBJPEG */ |
1847 BMPCONV::BMPCONV(const char* _inbuf, int _inlen, const char* _filename) { | 1883 BMPCONV::BMPCONV(const char* _inbuf, int _inlen, const char* _filename) { |
1848 /* データから情報読み込み */ | 1884 /* データから情報読み込み */ |
1849 int w = read_little_endian_int(_inbuf + 0x12); | 1885 int w = read_little_endian_int(_inbuf + 0x12); |
1850 int h = read_little_endian_int(_inbuf + 0x16); | 1886 int h = read_little_endian_int(_inbuf + 0x16); |
1851 if (h < 0) h = -h; | 1887 if (h < 0) h = -h; |
1852 int bpp = read_little_endian_short(_inbuf + 0x1c); | 1888 int bpp = read_little_endian_short(_inbuf + 0x1c); |
1853 int comp = read_little_endian_int(_inbuf + 0x1e); | 1889 int comp = read_little_endian_int(_inbuf + 0x1e); |
1854 Init(filename, _inbuf, _inlen, w, h, bpp==32 ? true : false); | 1890 Init(filename, _inbuf, _inlen, w, h, bpp==32 ? true : false); |
1855 return; | |
1856 } | 1891 } |
1857 | 1892 |
1858 bool BMPCONV::Read(char* image) { | 1893 bool BMPCONV::Read(char* image) { |
1859 if (data == 0) return false; | 1894 if (data == NULL) return false; |
1860 | 1895 |
1861 /* マスクのチェック */ | 1896 /* マスクのチェック */ |
1862 int bpp = read_little_endian_short(data+0x1c); | 1897 int bpp = read_little_endian_short(data+0x1c); |
1863 int h = read_little_endian_int(data + 0x16); | 1898 int h = read_little_endian_int(data + 0x16); |
1864 int dsz = read_little_endian_int(data + 0x22); | 1899 int dsz = read_little_endian_int(data + 0x22); |