APEG_STREAM *apeg_open_stream_ex(void *ptr) { APEG_LAYER *layer; Initialize_Decoder(); layer = new_apeg_stream(); if(!layer) return NULL; if(setjmp(jmp_buffer)) { apeg_close_stream((APEG_STREAM*)layer); return NULL; } layer->ext_data.init = _init_func; layer->ext_data.request = _request_func; layer->ext_data.skip = _skip_func; layer->ext_data.ptr = ptr; layer->pf = pack_fopen_vtable(&ext_vtable, layer); if(!layer->pf) apeg_error_jump("Could not open stream"); layer->buffer_type = USER_BUFFER; setup_stream(layer); return (APEG_STREAM*)layer; }
APEG_STREAM *apeg_open_memory_stream(void *mpeg_data, int data_len) { APEG_LAYER *layer; Initialize_Decoder(); layer = new_apeg_stream(); if(!layer) return NULL; if(setjmp(jmp_buffer)) { apeg_close_stream((APEG_STREAM*)layer); return NULL; } layer->mem_data.buf = mpeg_data; layer->mem_data.bytes_left = data_len; layer->pf = pack_fopen_vtable(&mem_vtable, layer); if(!layer->pf) apeg_error_jump("Could not open stream"); layer->buffer_type = MEMORY_BUFFER; layer->buffer_size = data_len; setup_stream(layer); return (APEG_STREAM*)layer; }
/* This demonstrates seeking. It opens expackf.c, and reads some characters * from it. */ static void stdio_seek_test(void) { FILE *fp; PACKFILE *f; char str[8]; fp = fopen("expackf.c", "rb"); if (!fp) { /* Handle the case where the user is running from a build directory * directly under the Allegro root directory. */ fp = fopen("../../examples/expackf.c", "rb"); } CHECK(fp, "opening expackf.c"); f = pack_fopen_vtable(&stdio_vtable, fp); CHECK(f, "reading with stdio"); pack_fseek(f, 33); pack_fread(str, 7, f); str[7] = '\0'; textprintf_ex(screen, font, 0, 0, -1, -1, "Reading from \"expackf.c\" with stdio."); textprintf_ex(screen, font, 0, 20, -1, -1, "Seeking to byte 33, reading 7 bytes:"); textprintf_ex(screen, font, 0, 40, -1, -1, "\"%s\"", str); textprintf_ex(screen, font, 0, 60, -1, -1, "(Should be \"Allegro\")"); pack_fclose(f); next(); }
/* This reads the files mysha.pcx and allegro.pcx into a memory block as * binary data, and then uses the memory vtable to read the bitmaps out of * the memory block. */ static void memread_test(void) { PACKFILE *f; MEMREAD_INFO memread_info; BITMAP *bmp, *bmp2; unsigned char *block; int64_t l1, l2; PACKFILE *f1, *f2; l1 = file_size_ex("allegro.pcx"); l2 = file_size_ex("mysha.pcx"); block = malloc(l1 + l2); /* Read mysha.pcx into the memory block. */ f1 = pack_fopen("allegro.pcx", "rb"); CHECK(f1, "opening allegro.pcx"); pack_fread(block, l1, f1); pack_fclose(f1); /* Read allegro.pcx into the memory block. */ f2 = pack_fopen("mysha.pcx", "rb"); CHECK(f2, "opening mysha.pcx"); pack_fread(block + l1, l2, f2); pack_fclose(f2); /* Open the memory block as PACKFILE, using our memory vtable. */ memread_info.block = block; memread_info.length = l1 + l2; memread_info.offset = 0; f = pack_fopen_vtable(&memread_vtable, &memread_info); CHECK(f, "reading from memory block"); /* Read the bitmaps out of the memory block. */ bmp = load_pcx_pf(f, NULL); CHECK(bmp, "load_pcx_pf"); bmp2 = load_pcx_pf(f, NULL); CHECK(bmp2, "load_pcx_pf"); blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h); textprintf_ex(screen, font, bmp->w + 8, 8, -1, -1, "\"allegro.pcx\""); textprintf_ex(screen, font, bmp->w + 8, 8 + 20, -1, -1, "read out of a memory file"); blit(bmp2, screen, 0, 0, 0, bmp->h + 8, bmp2->w, bmp2->h); textprintf_ex(screen, font, bmp2->w + 8, bmp->h + 8, -1, -1, "\"mysha.pcx\""); textprintf_ex(screen, font, bmp2->w + 8, bmp->h + 8 + 20, -1, -1, "read out of a memory file"); destroy_bitmap(bmp); destroy_bitmap(bmp2); pack_fclose(f); next(); }
/* This reads in allegro.pcx, but it does so by using the stdio vtable. */ static void stdio_read_test(void) { FILE *fp, *fp2; PACKFILE *f; BITMAP *bmp,*bmp2; /* Simply open the file with the libc fopen. */ fp = fopen("allegro.pcx", "rb"); CHECK(fp, "opening allegro.pcx"); /* Create a PACKFILE, with our custom stdio vtable. */ f = pack_fopen_vtable(&stdio_vtable, fp); CHECK(f, "reading with stdio"); /* Now read in the bitmap. */ bmp = load_pcx_pf(f, NULL); CHECK(bmp, "load_pcx_pf"); /* A little bit hackish, we re-assign the file pointer in our PACKFILE * to another file. */ fp2 = freopen("mysha.pcx", "rb", fp); CHECK(fp2, "opening mysha.pcx"); /* Read in the other bitmap. */ bmp2 = load_pcx_pf(f, NULL); CHECK(bmp, "load_pcx_pf"); blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h); textprintf_ex(screen, font, bmp2->w + 8, 8, -1, -1, "\"allegro.pcx\""); textprintf_ex(screen, font, bmp2->w + 8, 8 + 20, -1, -1, "read with stdio functions"); blit(bmp2, screen, 0, 0, 0, bmp->h + 8, bmp2->w, bmp2->h); textprintf_ex(screen, font, bmp2->w + 8, bmp->h + 8, -1, -1, "\"mysha.pcx\""); textprintf_ex(screen, font, bmp2->w + 8, bmp->h + 8 + 20, -1, -1, "read with stdio functions"); destroy_bitmap(bmp); destroy_bitmap(bmp2); pack_fclose(f); next(); }
/* This demonstrates writing. It simply saves the two bitmaps into a binary * file. */ static void stdio_write_test(void) { FILE *fp; PACKFILE *f; BITMAP *bmp, *bmp2; /* Read the bitmaps. */ bmp = load_pcx("allegro.pcx", NULL); CHECK(bmp, "load_pcx"); bmp2 = load_pcx("mysha.pcx", NULL); CHECK(bmp2, "load_pcx"); /* Write them with out custom vtable. */ fp = fopen("expackf.out", "wb"); CHECK(fp, "writing expackf.out"); f = pack_fopen_vtable(&stdio_vtable, fp); CHECK(f, "writing with stdio"); save_tga_pf(f, bmp, NULL); save_bmp_pf(f, bmp2, NULL); destroy_bitmap(bmp); destroy_bitmap(bmp2); pack_fclose(f); /* Now read them in again with our custom vtable. */ fp = fopen("expackf.out", "rb"); CHECK(fp, "fopen"); f = pack_fopen_vtable(&stdio_vtable, fp); CHECK(f, "reading from stdio"); /* Note: in general you would need to implement a "chunking" system * that knows where the boundary of each file is. Many file format * loaders will happily read everything to the end of the file, * whereas others stop reading as soon as they have all the essential * data (e.g. there may be some metadata at the end of the file). * Concatenating bare files together only works in examples programs. */ bmp = load_tga_pf(f, NULL); CHECK(bmp, "load_tga_pf"); bmp2 = load_bmp_pf(f, NULL); CHECK(bmp2, "load_bmp_pf"); blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h); textprintf_ex(screen, font, bmp2->w + 8, 8, -1, -1, "\"allegro.pcx\" (as tga)"); textprintf_ex(screen, font, bmp2->w + 8, 8 + 20, -1, -1, "wrote with stdio functions"); blit(bmp2, screen, 0, 0, 0, bmp->h + 8, bmp2->w, bmp2->h); textprintf_ex(screen, font, bmp2->w + 8, bmp->h + 8, -1, -1, "\"mysha.pcx\" (as bmp)"); textprintf_ex(screen, font, bmp2->w + 8, bmp->h + 8 + 20, -1, -1, "wrote with stdio functions"); destroy_bitmap(bmp); destroy_bitmap(bmp2); pack_fclose(f); next(); }