src/mess/tools/imgtool/imgwave.c
| r1340 | r1341 | |
| 1 | | #include <string.h> |
| 2 | | #include "imgtoolx.h" |
| 3 | | |
| 4 | | typedef struct { |
| 5 | | imgtool_image base; |
| 6 | | imgtool_stream *f; |
| 7 | | int basepos; |
| 8 | | int length; |
| 9 | | int curpos; |
| 10 | | |
| 11 | | int channels; |
| 12 | | int resolution; |
| 13 | | int frequency; |
| 14 | | INT16 lastsample; |
| 15 | | } waveimage; |
| 16 | | |
| 17 | | typedef struct { |
| 18 | | imgtool_directory base; |
| 19 | | waveimage *wimg; |
| 20 | | int pos; |
| 21 | | } waveimageenum; |
| 22 | | |
| 23 | | static int find_wavtag(imgtool_stream *f, int filelen, const char *tag, int *offset, UINT32 *blocklen) |
| 24 | | { |
| 25 | | char buf[4]; |
| 26 | | |
| 27 | | while(1) { |
| 28 | | *offset += stream_read(f, buf, sizeof(buf)); |
| 29 | | *offset += stream_read(f, blocklen, sizeof(*blocklen)); |
| 30 | | *blocklen = LITTLE_ENDIANIZE_INT32(*blocklen); |
| 31 | | |
| 32 | | if (!memcmp(buf, tag, 4)) |
| 33 | | break; |
| 34 | | |
| 35 | | stream_seek(f, *blocklen, SEEK_CUR); |
| 36 | | *offset += *blocklen; |
| 37 | | |
| 38 | | if (*offset >= filelen) |
| 39 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 40 | | } |
| 41 | | return 0; |
| 42 | | } |
| 43 | | |
| 44 | | int imgwave_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 45 | | { |
| 46 | | int err; |
| 47 | | waveimage *wimg; |
| 48 | | struct WaveExtra *extra; |
| 49 | | char buf[4]; |
| 50 | | UINT16 format, channels, resolution; |
| 51 | | UINT32 filelen, blocklen, frequency; |
| 52 | | int offset = 0; |
| 53 | | |
| 54 | | offset += stream_read(f, buf, sizeof(buf)); |
| 55 | | if (offset < 4) |
| 56 | | goto initalt; |
| 57 | | if (memcmp(buf, "RIFF", 4)) |
| 58 | | goto initalt; |
| 59 | | |
| 60 | | offset += stream_read(f, &filelen, sizeof(filelen)); |
| 61 | | if (offset < 8) |
| 62 | | goto initalt; |
| 63 | | filelen = LITTLE_ENDIANIZE_INT32(filelen); |
| 64 | | |
| 65 | | offset += stream_read(f, buf, sizeof(buf)); |
| 66 | | if (offset < 12) |
| 67 | | goto initalt; |
| 68 | | if (memcmp(buf, "WAVE", 4)) |
| 69 | | goto initalt; |
| 70 | | |
| 71 | | err = find_wavtag(f, filelen, "fmt ", &offset, &blocklen); |
| 72 | | if (err) |
| 73 | | return err; |
| 74 | | |
| 75 | | offset += stream_read(f, &format, sizeof(format)); |
| 76 | | format = LITTLE_ENDIANIZE_INT16(format); |
| 77 | | if (format != 1) |
| 78 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 79 | | |
| 80 | | offset += stream_read(f, &channels, sizeof(channels)); |
| 81 | | channels = LITTLE_ENDIANIZE_INT16(channels); |
| 82 | | if (channels != 1 && channels != 2) |
| 83 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 84 | | |
| 85 | | offset += stream_read(f, &frequency, sizeof(frequency)); |
| 86 | | frequency = LITTLE_ENDIANIZE_INT32(frequency); |
| 87 | | |
| 88 | | stream_seek(f, 6, SEEK_CUR); |
| 89 | | |
| 90 | | offset += stream_read(f, &resolution, sizeof(resolution)); |
| 91 | | resolution = LITTLE_ENDIANIZE_INT16(resolution); |
| 92 | | if ((resolution != 8) && (resolution != 16)) |
| 93 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 94 | | |
| 95 | | stream_seek(f, blocklen - 16, SEEK_CUR); |
| 96 | | |
| 97 | | err = find_wavtag(f, filelen, "data", &offset, &blocklen); |
| 98 | | if (err) |
| 99 | | return err; |
| 100 | | |
| 101 | | wimg = malloc(sizeof(waveimage)); |
| 102 | | if (!wimg) |
| 103 | | return IMGTOOLERR_OUTOFMEMORY; |
| 104 | | |
| 105 | | wimg->base.module = mod; |
| 106 | | wimg->f = f; |
| 107 | | wimg->basepos = wimg->curpos = offset; |
| 108 | | wimg->length = blocklen; |
| 109 | | wimg->channels = channels; |
| 110 | | wimg->frequency = frequency; |
| 111 | | wimg->resolution = resolution; |
| 112 | | wimg->lastsample = 0; |
| 113 | | *outimg = &wimg->base; |
| 114 | | return 0; |
| 115 | | |
| 116 | | initalt: |
| 117 | | extra = (struct WaveExtra *) mod->extra; |
| 118 | | if (!extra->initalt) |
| 119 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 120 | | |
| 121 | | stream_seek(f, 0, SEEK_SET); |
| 122 | | |
| 123 | | wimg = malloc(sizeof(waveimage)); |
| 124 | | if (!wimg) |
| 125 | | return IMGTOOLERR_OUTOFMEMORY; |
| 126 | | |
| 127 | | |
| 128 | | err = extra->initalt(f, &wimg->f, &wimg->basepos, &wimg->length, |
| 129 | | &wimg->channels, &wimg->frequency, &wimg->resolution); |
| 130 | | if (err) { |
| 131 | | free(wimg); |
| 132 | | return err; |
| 133 | | } |
| 134 | | |
| 135 | | wimg->base.module = mod; |
| 136 | | wimg->curpos = wimg->basepos; |
| 137 | | wimg->lastsample = 0; |
| 138 | | stream_seek(wimg->f, wimg->basepos, SEEK_SET); |
| 139 | | *outimg = &wimg->base; |
| 140 | | |
| 141 | | if (wimg->f != f) |
| 142 | | stream_close(f); |
| 143 | | |
| 144 | | return 0; |
| 145 | | } |
| 146 | | |
| 147 | | void imgwave_exit(imgtool_image *img) |
| 148 | | { |
| 149 | | waveimage *wimg = (waveimage *) img; |
| 150 | | stream_close(wimg->f); |
| 151 | | free(wimg); |
| 152 | | } |
| 153 | | |
| 154 | | int imgwave_seek(imgtool_image *img, int pos) |
| 155 | | { |
| 156 | | waveimage *wimg = (waveimage *) img; |
| 157 | | |
| 158 | | if (wimg->curpos != pos) { |
| 159 | | stream_seek(wimg->f, pos, SEEK_SET); |
| 160 | | wimg->curpos = pos; |
| 161 | | } |
| 162 | | return 0; |
| 163 | | } |
| 164 | | |
| 165 | | static int imgwave_readsample(imgtool_image *img, INT16 *sample) |
| 166 | | { |
| 167 | | int len; |
| 168 | | INT16 s = 0; |
| 169 | | waveimage *wimg = (waveimage *) img; |
| 170 | | |
| 171 | | union { |
| 172 | | UINT8 buf8[2]; |
| 173 | | INT16 buf16[2]; |
| 174 | | } u; |
| 175 | | |
| 176 | | if (wimg->curpos >= wimg->basepos + wimg->length) { |
| 177 | | *sample = 0; |
| 178 | | return IMGTOOLERR_INPUTPASTEND; |
| 179 | | } |
| 180 | | |
| 181 | | if (wimg->resolution == 8) { |
| 182 | | len = stream_read(wimg->f, u.buf8, wimg->channels); |
| 183 | | wimg->curpos += len; |
| 184 | | if (len < wimg->channels) |
| 185 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 186 | | |
| 187 | | switch(wimg->channels) { |
| 188 | | case 1: |
| 189 | | s = ((INT16) u.buf8[0]) - 0x80; |
| 190 | | s *= 0x100; |
| 191 | | break; |
| 192 | | case 2: |
| 193 | | s = ((INT16) u.buf8[0]) - 0x80; |
| 194 | | s += ((INT16) u.buf8[1]) - 0x80; |
| 195 | | s *= 0x80; |
| 196 | | break; |
| 197 | | } |
| 198 | | } |
| 199 | | else { |
| 200 | | len = stream_read(wimg->f, u.buf16, wimg->channels * 2); |
| 201 | | wimg->curpos += len; |
| 202 | | if (len < (wimg->channels * 2)) |
| 203 | | return IMGTOOLERR_CORRUPTIMAGE; |
| 204 | | |
| 205 | | switch(wimg->channels) { |
| 206 | | case 1: |
| 207 | | s = u.buf16[0]; |
| 208 | | break; |
| 209 | | case 2: |
| 210 | | s = (u.buf16[0] / 2) + (u.buf16[1] / 2); |
| 211 | | break; |
| 212 | | } |
| 213 | | } |
| 214 | | *sample = s; |
| 215 | | return 0; |
| 216 | | } |
| 217 | | |
| 218 | | static int imgwave_readtransition(imgtool_image *img, int *frequency) |
| 219 | | { |
| 220 | | int err; |
| 221 | | int count = 0; |
| 222 | | int transitioned; |
| 223 | | INT16 sample; |
| 224 | | waveimage *wimg; |
| 225 | | |
| 226 | | wimg = (waveimage *) img; |
| 227 | | |
| 228 | | do { |
| 229 | | err = imgwave_readsample(img, &sample); |
| 230 | | if (err) |
| 231 | | return err; |
| 232 | | |
| 233 | | /* Problem - I should be checking for both types of transitions, somehow */ |
| 234 | | #if 0 |
| 235 | | transitioned = (wimg->lastsample >= 0) && (sample < 0); |
| 236 | | #else |
| 237 | | transitioned = (wimg->lastsample < 0) && (sample >= 0); |
| 238 | | #endif |
| 239 | | wimg->lastsample = sample; |
| 240 | | count++; |
| 241 | | } |
| 242 | | while(!transitioned); |
| 243 | | |
| 244 | | *frequency = wimg->frequency / count; |
| 245 | | return 0; |
| 246 | | } |
| 247 | | |
| 248 | | static int imgwave_readbit(imgtool_image *img, UINT8 *bit) |
| 249 | | { |
| 250 | | int err, freq; |
| 251 | | waveimage *wimg; |
| 252 | | struct WaveExtra *extra; |
| 253 | | |
| 254 | | wimg = (waveimage *) img; |
| 255 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 256 | | |
| 257 | | err = imgwave_readtransition(img, &freq); |
| 258 | | if (err) |
| 259 | | return err; |
| 260 | | |
| 261 | | if (extra->zeropulse > extra->onepulse) |
| 262 | | *bit = extra->threshpulse > freq; |
| 263 | | else |
| 264 | | *bit = extra->threshpulse <= freq; |
| 265 | | return 0; |
| 266 | | } |
| 267 | | |
| 268 | | int imgwave_forward(imgtool_image *img) |
| 269 | | { |
| 270 | | int err; |
| 271 | | int i; |
| 272 | | UINT8 carry, newcarry; |
| 273 | | UINT8 *buffer; |
| 274 | | waveimage *wimg; |
| 275 | | struct WaveExtra *extra; |
| 276 | | |
| 277 | | wimg = (waveimage *) img; |
| 278 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 279 | | |
| 280 | | buffer = malloc(extra->blockheadersize); |
| 281 | | if (!buffer) |
| 282 | | return IMGTOOLERR_OUTOFMEMORY; |
| 283 | | |
| 284 | | memset(buffer, 0, extra->blockheadersize); |
| 285 | | |
| 286 | | do { |
| 287 | | err = imgwave_readbit(img, &carry); |
| 288 | | if (err) { |
| 289 | | free(buffer); |
| 290 | | return err; |
| 291 | | } |
| 292 | | |
| 293 | | for (i = extra->blockheadersize-1; i >= 0; i--) { |
| 294 | | if (extra->waveflags & WAVEIMAGE_MSB_FIRST) { |
| 295 | | /* Most significant bit first */ |
| 296 | | newcarry = buffer[i] & 0x80; |
| 297 | | buffer[i] <<= 1; |
| 298 | | if (carry) |
| 299 | | buffer[i] |= 1; |
| 300 | | } |
| 301 | | else { |
| 302 | | /* Least significant bit first */ |
| 303 | | newcarry = buffer[i] & 0x01; |
| 304 | | buffer[i] >>= 1; |
| 305 | | if (carry) |
| 306 | | buffer[i] |= 0x80; |
| 307 | | } |
| 308 | | carry = newcarry; |
| 309 | | } |
| 310 | | } |
| 311 | | while(memcmp(buffer, extra->blockheader, extra->blockheadersize)); |
| 312 | | |
| 313 | | free(buffer); |
| 314 | | return 0; |
| 315 | | } |
| 316 | | |
| 317 | | int imgwave_read(imgtool_image *img, UINT8 *buf, int bufsize) |
| 318 | | { |
| 319 | | int err; |
| 320 | | int i, j; |
| 321 | | UINT8 b, bit; |
| 322 | | waveimage *wimg; |
| 323 | | struct WaveExtra *extra; |
| 324 | | |
| 325 | | wimg = (waveimage *) img; |
| 326 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 327 | | |
| 328 | | for (i = 0; i < bufsize; i++) { |
| 329 | | b = 0; |
| 330 | | for (j = 0; j < 8; j++) { |
| 331 | | err = imgwave_readbit(img, &bit); |
| 332 | | if (err) |
| 333 | | return err; |
| 334 | | if (extra->waveflags & WAVEIMAGE_MSB_FIRST) { |
| 335 | | /* Most significant bit first */ |
| 336 | | b <<= 1; |
| 337 | | if (bit) |
| 338 | | b |= 1; |
| 339 | | } |
| 340 | | else { |
| 341 | | /* Least significant bit first */ |
| 342 | | b >>= 1; |
| 343 | | if (bit) |
| 344 | | b |= 0x80; |
| 345 | | } |
| 346 | | } |
| 347 | | *(buf++) = b; |
| 348 | | } |
| 349 | | return 0; |
| 350 | | } |
| 351 | | |
| 352 | | int imgwave_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 353 | | { |
| 354 | | /* TODO - Enumerating wave images should be cached */ |
| 355 | | |
| 356 | | waveimage *wimg = (waveimage *) img; |
| 357 | | waveimageenum *wenum; |
| 358 | | |
| 359 | | wenum = malloc(sizeof(waveimageenum)); |
| 360 | | if (!wenum) |
| 361 | | return IMGTOOLERR_OUTOFMEMORY; |
| 362 | | |
| 363 | | wenum->base.module = img->module; |
| 364 | | wenum->wimg = wimg; |
| 365 | | wenum->pos = wimg->basepos; |
| 366 | | *outenum = (imgtool_directory *) wenum; |
| 367 | | return 0; |
| 368 | | } |
| 369 | | |
| 370 | | int imgwave_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 371 | | { |
| 372 | | int err; |
| 373 | | waveimageenum *wenum; |
| 374 | | |
| 375 | | wenum = (waveimageenum *) enumeration; |
| 376 | | |
| 377 | | err = imgwave_seek((imgtool_image *) wenum->wimg, wenum->pos); |
| 378 | | if (err) |
| 379 | | return err; |
| 380 | | |
| 381 | | err = ((struct WaveExtra *) wenum->wimg->base.module->extra)->nextfile((imgtool_image *) wenum->wimg, ent); |
| 382 | | if (err) |
| 383 | | return err; |
| 384 | | |
| 385 | | wenum->pos = wenum->wimg->curpos; |
| 386 | | return 0; |
| 387 | | } |
| 388 | | |
| 389 | | void imgwave_closeenum(imgtool_directory *enumeration) |
| 390 | | { |
| 391 | | waveimageenum *wenum = (waveimageenum *) enumeration; |
| 392 | | free(wenum); |
| 393 | | } |
| 394 | | |
| 395 | | int imgwave_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 396 | | { |
| 397 | | int err, pos; |
| 398 | | imgtool_dirent ent; |
| 399 | | imgtool_directory *enumeration; |
| 400 | | waveimageenum *wenum; |
| 401 | | waveimage *wimg; |
| 402 | | struct WaveExtra *extra; |
| 403 | | char fnamebuf[256]; |
| 404 | | |
| 405 | | wimg = (waveimage *) img; |
| 406 | | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 407 | | |
| 408 | | err = imgwave_beginenum(img, &enumeration); |
| 409 | | if (err) |
| 410 | | return err; |
| 411 | | wenum = (waveimageenum *) enumeration; |
| 412 | | |
| 413 | | memset(&ent, 0, sizeof(ent)); |
| 414 | | ent.fname = fnamebuf; |
| 415 | | ent.fname_len = sizeof(fnamebuf); |
| 416 | | |
| 417 | | do { |
| 418 | | pos = wenum->pos; |
| 419 | | err = imgwave_nextenum(enumeration, &ent); |
| 420 | | if (ent.eof) |
| 421 | | err = IMGTOOLERR_FILENOTFOUND; |
| 422 | | if (err) { |
| 423 | | imgwave_closeenum(enumeration); |
| 424 | | return err; |
| 425 | | } |
| 426 | | } |
| 427 | | while(strcmp(ent.fname, fname)); |
| 428 | | |
| 429 | | imgwave_closeenum(enumeration); |
| 430 | | enumeration = NULL; |
| 431 | | |
| 432 | | err = imgwave_seek(img, pos); |
| 433 | | if (err) |
| 434 | | return err; |
| 435 | | |
| 436 | | err = extra->readfile(img, destf); |
| 437 | | if (err) |
| 438 | | return err; |
| 439 | | |
| 440 | | return 0; |
| 441 | | } |
| 442 | | |
| 443 | | |
src/mess/tools/imgtool/modules/imgwave.c
| r0 | r1341 | |
| 1 | #include <string.h> |
| 2 | #include "imgtoolx.h" |
| 3 | |
| 4 | typedef struct { |
| 5 | imgtool_image base; |
| 6 | imgtool_stream *f; |
| 7 | int basepos; |
| 8 | int length; |
| 9 | int curpos; |
| 10 | |
| 11 | int channels; |
| 12 | int resolution; |
| 13 | int frequency; |
| 14 | INT16 lastsample; |
| 15 | } waveimage; |
| 16 | |
| 17 | typedef struct { |
| 18 | imgtool_directory base; |
| 19 | waveimage *wimg; |
| 20 | int pos; |
| 21 | } waveimageenum; |
| 22 | |
| 23 | static int find_wavtag(imgtool_stream *f, int filelen, const char *tag, int *offset, UINT32 *blocklen) |
| 24 | { |
| 25 | char buf[4]; |
| 26 | |
| 27 | while(1) { |
| 28 | *offset += stream_read(f, buf, sizeof(buf)); |
| 29 | *offset += stream_read(f, blocklen, sizeof(*blocklen)); |
| 30 | *blocklen = LITTLE_ENDIANIZE_INT32(*blocklen); |
| 31 | |
| 32 | if (!memcmp(buf, tag, 4)) |
| 33 | break; |
| 34 | |
| 35 | stream_seek(f, *blocklen, SEEK_CUR); |
| 36 | *offset += *blocklen; |
| 37 | |
| 38 | if (*offset >= filelen) |
| 39 | return IMGTOOLERR_CORRUPTIMAGE; |
| 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | int imgwave_init(const imgtool_module *mod, imgtool_stream *f, imgtool_image **outimg) |
| 45 | { |
| 46 | int err; |
| 47 | waveimage *wimg; |
| 48 | struct WaveExtra *extra; |
| 49 | char buf[4]; |
| 50 | UINT16 format, channels, resolution; |
| 51 | UINT32 filelen, blocklen, frequency; |
| 52 | int offset = 0; |
| 53 | |
| 54 | offset += stream_read(f, buf, sizeof(buf)); |
| 55 | if (offset < 4) |
| 56 | goto initalt; |
| 57 | if (memcmp(buf, "RIFF", 4)) |
| 58 | goto initalt; |
| 59 | |
| 60 | offset += stream_read(f, &filelen, sizeof(filelen)); |
| 61 | if (offset < 8) |
| 62 | goto initalt; |
| 63 | filelen = LITTLE_ENDIANIZE_INT32(filelen); |
| 64 | |
| 65 | offset += stream_read(f, buf, sizeof(buf)); |
| 66 | if (offset < 12) |
| 67 | goto initalt; |
| 68 | if (memcmp(buf, "WAVE", 4)) |
| 69 | goto initalt; |
| 70 | |
| 71 | err = find_wavtag(f, filelen, "fmt ", &offset, &blocklen); |
| 72 | if (err) |
| 73 | return err; |
| 74 | |
| 75 | offset += stream_read(f, &format, sizeof(format)); |
| 76 | format = LITTLE_ENDIANIZE_INT16(format); |
| 77 | if (format != 1) |
| 78 | return IMGTOOLERR_CORRUPTIMAGE; |
| 79 | |
| 80 | offset += stream_read(f, &channels, sizeof(channels)); |
| 81 | channels = LITTLE_ENDIANIZE_INT16(channels); |
| 82 | if (channels != 1 && channels != 2) |
| 83 | return IMGTOOLERR_CORRUPTIMAGE; |
| 84 | |
| 85 | offset += stream_read(f, &frequency, sizeof(frequency)); |
| 86 | frequency = LITTLE_ENDIANIZE_INT32(frequency); |
| 87 | |
| 88 | stream_seek(f, 6, SEEK_CUR); |
| 89 | |
| 90 | offset += stream_read(f, &resolution, sizeof(resolution)); |
| 91 | resolution = LITTLE_ENDIANIZE_INT16(resolution); |
| 92 | if ((resolution != 8) && (resolution != 16)) |
| 93 | return IMGTOOLERR_CORRUPTIMAGE; |
| 94 | |
| 95 | stream_seek(f, blocklen - 16, SEEK_CUR); |
| 96 | |
| 97 | err = find_wavtag(f, filelen, "data", &offset, &blocklen); |
| 98 | if (err) |
| 99 | return err; |
| 100 | |
| 101 | wimg = malloc(sizeof(waveimage)); |
| 102 | if (!wimg) |
| 103 | return IMGTOOLERR_OUTOFMEMORY; |
| 104 | |
| 105 | wimg->base.module = mod; |
| 106 | wimg->f = f; |
| 107 | wimg->basepos = wimg->curpos = offset; |
| 108 | wimg->length = blocklen; |
| 109 | wimg->channels = channels; |
| 110 | wimg->frequency = frequency; |
| 111 | wimg->resolution = resolution; |
| 112 | wimg->lastsample = 0; |
| 113 | *outimg = &wimg->base; |
| 114 | return 0; |
| 115 | |
| 116 | initalt: |
| 117 | extra = (struct WaveExtra *) mod->extra; |
| 118 | if (!extra->initalt) |
| 119 | return IMGTOOLERR_CORRUPTIMAGE; |
| 120 | |
| 121 | stream_seek(f, 0, SEEK_SET); |
| 122 | |
| 123 | wimg = malloc(sizeof(waveimage)); |
| 124 | if (!wimg) |
| 125 | return IMGTOOLERR_OUTOFMEMORY; |
| 126 | |
| 127 | |
| 128 | err = extra->initalt(f, &wimg->f, &wimg->basepos, &wimg->length, |
| 129 | &wimg->channels, &wimg->frequency, &wimg->resolution); |
| 130 | if (err) { |
| 131 | free(wimg); |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | wimg->base.module = mod; |
| 136 | wimg->curpos = wimg->basepos; |
| 137 | wimg->lastsample = 0; |
| 138 | stream_seek(wimg->f, wimg->basepos, SEEK_SET); |
| 139 | *outimg = &wimg->base; |
| 140 | |
| 141 | if (wimg->f != f) |
| 142 | stream_close(f); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | void imgwave_exit(imgtool_image *img) |
| 148 | { |
| 149 | waveimage *wimg = (waveimage *) img; |
| 150 | stream_close(wimg->f); |
| 151 | free(wimg); |
| 152 | } |
| 153 | |
| 154 | int imgwave_seek(imgtool_image *img, int pos) |
| 155 | { |
| 156 | waveimage *wimg = (waveimage *) img; |
| 157 | |
| 158 | if (wimg->curpos != pos) { |
| 159 | stream_seek(wimg->f, pos, SEEK_SET); |
| 160 | wimg->curpos = pos; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int imgwave_readsample(imgtool_image *img, INT16 *sample) |
| 166 | { |
| 167 | int len; |
| 168 | INT16 s = 0; |
| 169 | waveimage *wimg = (waveimage *) img; |
| 170 | |
| 171 | union { |
| 172 | UINT8 buf8[2]; |
| 173 | INT16 buf16[2]; |
| 174 | } u; |
| 175 | |
| 176 | if (wimg->curpos >= wimg->basepos + wimg->length) { |
| 177 | *sample = 0; |
| 178 | return IMGTOOLERR_INPUTPASTEND; |
| 179 | } |
| 180 | |
| 181 | if (wimg->resolution == 8) { |
| 182 | len = stream_read(wimg->f, u.buf8, wimg->channels); |
| 183 | wimg->curpos += len; |
| 184 | if (len < wimg->channels) |
| 185 | return IMGTOOLERR_CORRUPTIMAGE; |
| 186 | |
| 187 | switch(wimg->channels) { |
| 188 | case 1: |
| 189 | s = ((INT16) u.buf8[0]) - 0x80; |
| 190 | s *= 0x100; |
| 191 | break; |
| 192 | case 2: |
| 193 | s = ((INT16) u.buf8[0]) - 0x80; |
| 194 | s += ((INT16) u.buf8[1]) - 0x80; |
| 195 | s *= 0x80; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | else { |
| 200 | len = stream_read(wimg->f, u.buf16, wimg->channels * 2); |
| 201 | wimg->curpos += len; |
| 202 | if (len < (wimg->channels * 2)) |
| 203 | return IMGTOOLERR_CORRUPTIMAGE; |
| 204 | |
| 205 | switch(wimg->channels) { |
| 206 | case 1: |
| 207 | s = u.buf16[0]; |
| 208 | break; |
| 209 | case 2: |
| 210 | s = (u.buf16[0] / 2) + (u.buf16[1] / 2); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | *sample = s; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int imgwave_readtransition(imgtool_image *img, int *frequency) |
| 219 | { |
| 220 | int err; |
| 221 | int count = 0; |
| 222 | int transitioned; |
| 223 | INT16 sample; |
| 224 | waveimage *wimg; |
| 225 | |
| 226 | wimg = (waveimage *) img; |
| 227 | |
| 228 | do { |
| 229 | err = imgwave_readsample(img, &sample); |
| 230 | if (err) |
| 231 | return err; |
| 232 | |
| 233 | /* Problem - I should be checking for both types of transitions, somehow */ |
| 234 | #if 0 |
| 235 | transitioned = (wimg->lastsample >= 0) && (sample < 0); |
| 236 | #else |
| 237 | transitioned = (wimg->lastsample < 0) && (sample >= 0); |
| 238 | #endif |
| 239 | wimg->lastsample = sample; |
| 240 | count++; |
| 241 | } |
| 242 | while(!transitioned); |
| 243 | |
| 244 | *frequency = wimg->frequency / count; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int imgwave_readbit(imgtool_image *img, UINT8 *bit) |
| 249 | { |
| 250 | int err, freq; |
| 251 | waveimage *wimg; |
| 252 | struct WaveExtra *extra; |
| 253 | |
| 254 | wimg = (waveimage *) img; |
| 255 | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 256 | |
| 257 | err = imgwave_readtransition(img, &freq); |
| 258 | if (err) |
| 259 | return err; |
| 260 | |
| 261 | if (extra->zeropulse > extra->onepulse) |
| 262 | *bit = extra->threshpulse > freq; |
| 263 | else |
| 264 | *bit = extra->threshpulse <= freq; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int imgwave_forward(imgtool_image *img) |
| 269 | { |
| 270 | int err; |
| 271 | int i; |
| 272 | UINT8 carry, newcarry; |
| 273 | UINT8 *buffer; |
| 274 | waveimage *wimg; |
| 275 | struct WaveExtra *extra; |
| 276 | |
| 277 | wimg = (waveimage *) img; |
| 278 | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 279 | |
| 280 | buffer = malloc(extra->blockheadersize); |
| 281 | if (!buffer) |
| 282 | return IMGTOOLERR_OUTOFMEMORY; |
| 283 | |
| 284 | memset(buffer, 0, extra->blockheadersize); |
| 285 | |
| 286 | do { |
| 287 | err = imgwave_readbit(img, &carry); |
| 288 | if (err) { |
| 289 | free(buffer); |
| 290 | return err; |
| 291 | } |
| 292 | |
| 293 | for (i = extra->blockheadersize-1; i >= 0; i--) { |
| 294 | if (extra->waveflags & WAVEIMAGE_MSB_FIRST) { |
| 295 | /* Most significant bit first */ |
| 296 | newcarry = buffer[i] & 0x80; |
| 297 | buffer[i] <<= 1; |
| 298 | if (carry) |
| 299 | buffer[i] |= 1; |
| 300 | } |
| 301 | else { |
| 302 | /* Least significant bit first */ |
| 303 | newcarry = buffer[i] & 0x01; |
| 304 | buffer[i] >>= 1; |
| 305 | if (carry) |
| 306 | buffer[i] |= 0x80; |
| 307 | } |
| 308 | carry = newcarry; |
| 309 | } |
| 310 | } |
| 311 | while(memcmp(buffer, extra->blockheader, extra->blockheadersize)); |
| 312 | |
| 313 | free(buffer); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | int imgwave_read(imgtool_image *img, UINT8 *buf, int bufsize) |
| 318 | { |
| 319 | int err; |
| 320 | int i, j; |
| 321 | UINT8 b, bit; |
| 322 | waveimage *wimg; |
| 323 | struct WaveExtra *extra; |
| 324 | |
| 325 | wimg = (waveimage *) img; |
| 326 | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 327 | |
| 328 | for (i = 0; i < bufsize; i++) { |
| 329 | b = 0; |
| 330 | for (j = 0; j < 8; j++) { |
| 331 | err = imgwave_readbit(img, &bit); |
| 332 | if (err) |
| 333 | return err; |
| 334 | if (extra->waveflags & WAVEIMAGE_MSB_FIRST) { |
| 335 | /* Most significant bit first */ |
| 336 | b <<= 1; |
| 337 | if (bit) |
| 338 | b |= 1; |
| 339 | } |
| 340 | else { |
| 341 | /* Least significant bit first */ |
| 342 | b >>= 1; |
| 343 | if (bit) |
| 344 | b |= 0x80; |
| 345 | } |
| 346 | } |
| 347 | *(buf++) = b; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int imgwave_beginenum(imgtool_image *img, imgtool_directory **outenum) |
| 353 | { |
| 354 | /* TODO - Enumerating wave images should be cached */ |
| 355 | |
| 356 | waveimage *wimg = (waveimage *) img; |
| 357 | waveimageenum *wenum; |
| 358 | |
| 359 | wenum = malloc(sizeof(waveimageenum)); |
| 360 | if (!wenum) |
| 361 | return IMGTOOLERR_OUTOFMEMORY; |
| 362 | |
| 363 | wenum->base.module = img->module; |
| 364 | wenum->wimg = wimg; |
| 365 | wenum->pos = wimg->basepos; |
| 366 | *outenum = (imgtool_directory *) wenum; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int imgwave_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent) |
| 371 | { |
| 372 | int err; |
| 373 | waveimageenum *wenum; |
| 374 | |
| 375 | wenum = (waveimageenum *) enumeration; |
| 376 | |
| 377 | err = imgwave_seek((imgtool_image *) wenum->wimg, wenum->pos); |
| 378 | if (err) |
| 379 | return err; |
| 380 | |
| 381 | err = ((struct WaveExtra *) wenum->wimg->base.module->extra)->nextfile((imgtool_image *) wenum->wimg, ent); |
| 382 | if (err) |
| 383 | return err; |
| 384 | |
| 385 | wenum->pos = wenum->wimg->curpos; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | void imgwave_closeenum(imgtool_directory *enumeration) |
| 390 | { |
| 391 | waveimageenum *wenum = (waveimageenum *) enumeration; |
| 392 | free(wenum); |
| 393 | } |
| 394 | |
| 395 | int imgwave_readfile(imgtool_image *img, const char *fname, imgtool_stream *destf) |
| 396 | { |
| 397 | int err, pos; |
| 398 | imgtool_dirent ent; |
| 399 | imgtool_directory *enumeration; |
| 400 | waveimageenum *wenum; |
| 401 | waveimage *wimg; |
| 402 | struct WaveExtra *extra; |
| 403 | char fnamebuf[256]; |
| 404 | |
| 405 | wimg = (waveimage *) img; |
| 406 | extra = (struct WaveExtra *) wimg->base.module->extra; |
| 407 | |
| 408 | err = imgwave_beginenum(img, &enumeration); |
| 409 | if (err) |
| 410 | return err; |
| 411 | wenum = (waveimageenum *) enumeration; |
| 412 | |
| 413 | memset(&ent, 0, sizeof(ent)); |
| 414 | ent.fname = fnamebuf; |
| 415 | ent.fname_len = sizeof(fnamebuf); |
| 416 | |
| 417 | do { |
| 418 | pos = wenum->pos; |
| 419 | err = imgwave_nextenum(enumeration, &ent); |
| 420 | if (ent.eof) |
| 421 | err = IMGTOOLERR_FILENOTFOUND; |
| 422 | if (err) { |
| 423 | imgwave_closeenum(enumeration); |
| 424 | return err; |
| 425 | } |
| 426 | } |
| 427 | while(strcmp(ent.fname, fname)); |
| 428 | |
| 429 | imgwave_closeenum(enumeration); |
| 430 | enumeration = NULL; |
| 431 | |
| 432 | err = imgwave_seek(img, pos); |
| 433 | if (err) |
| 434 | return err; |
| 435 | |
| 436 | err = extra->readfile(img, destf); |
| 437 | if (err) |
| 438 | return err; |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | |