From 9e9e188ebfab66692d41267886454ddeb6681b0b Mon Sep 17 00:00:00 2001 From: Francois-Olivier Devaux Date: Wed, 8 Dec 2004 12:12:00 +0000 Subject: [PATCH] This tool extracts J2K codestreams from a MJ2 file (designed to work with other codecs than OpenJPEG) --- mj2/extract_j2k_from_mj2.c | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 mj2/extract_j2k_from_mj2.c diff --git a/mj2/extract_j2k_from_mj2.c b/mj2/extract_j2k_from_mj2.c new file mode 100644 index 00000000..86ce02ad --- /dev/null +++ b/mj2/extract_j2k_from_mj2.c @@ -0,0 +1,80 @@ +#include +#include +#include + +#include "mj2.h" + +//MEMORY LEAK +#ifdef _DEBUG +#define _CRTDBG_MAP_ALLOC +#include // Must be included first +#include +#endif +//MEM + +jmp_buf j2k_error; + +int main(int argc, char *argv[]) { + + unsigned int tnum, snum; + mj2_movie_t movie; + mj2_tk_t *track; + mj2_sample_t *sample; + unsigned char* frame_codestream; + FILE *file, *outfile; + char outfilename[50]; + + if (argc != 3) { + printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n"); + printf("Example: MJ2_extractor foreman.mj2 output/foreman\n"); + return 1; + } + + file = fopen(argv[1], "rb"); + + if (!file) { + fprintf(stderr, "failed to open %s for reading\n", argv[1]); + return 1; + } + + if (mj2_read_struct(file, &movie)) // Creating the movie structure + return 1; + + // Decode first video track + tnum = 0; + while (movie.tk[tnum].track_type != 0) + tnum ++; + + track = &movie.tk[tnum]; + + fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples); + + for (snum=0; snum < track->num_samples; snum++) + { + sample = &track->sample[snum]; + frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker + fseek(file,sample->offset+8,SEEK_SET); + fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do + + sprintf(outfilename,"%s_%d.j2k",argv[2],snum); + outfile = fopen(outfilename, "wb"); + if (!outfile) { + fprintf(stderr, "failed to open %s for writing\n",outfilename); + return 1; + } + fwrite(frame_codestream,sample->sample_size-8,1,outfile); + fclose(outfile); + free(frame_codestream); + } + fclose(file); + fprintf(stdout, "%d frames correctly extracted\n", snum); + mj2_memory_free(&movie); + + //MEMORY LEAK + #ifdef _DEBUG + _CrtDumpMemoryLeaks(); + #endif + //MEM + + return 0; +} \ No newline at end of file