MJ2 codec updated from LibOpenJPEG version 0.97 to LibOpenJPEG version 1. Hence, the MJ2 codec will now take advantage of all the improvements and optimizations done on the LibOpenJPEG library.

This commit is contained in:
Francois-Olivier Devaux 2007-05-10 14:21:09 +00:00
parent f3170bf64e
commit 1200dd871a
72 changed files with 5656 additions and 15324 deletions

View File

@ -59,19 +59,6 @@ static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio);
/**
Write the JP2H box - JP2 Header box
@param jp2 JP2 handle
@param cio Output buffer stream
*/
static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
/**
Read the JP2H box - JP2 Header box
@param jp2 JP2 handle
@param cio Input buffer stream
@return Returns true if successful, returns false otherwise
*/
static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
/**
Write the FTYP box - File type box
@param jp2 JP2 handle
@param cio Output buffer stream
@ -308,7 +295,7 @@ static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
return true;
}
static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
opj_jp2_box_t box;
box.init_pos = cio_tell(cio);
@ -328,7 +315,7 @@ static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
cio_seek(cio, box.init_pos + box.length);
}
static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
opj_jp2_box_t box;
int skip_len;

View File

@ -102,6 +102,19 @@ typedef struct opj_jp2_box {
/*@{*/
/* ----------------------------------------------------------------------- */
/**
Write the JP2H box - JP2 Header box (used in MJ2)
@param jp2 JP2 handle
@param cio Output buffer stream
*/
void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
/**
Read the JP2H box - JP2 Header box (used in MJ2)
@param jp2 JP2 handle
@param cio Input buffer stream
@return Returns true if successful, returns false otherwise
*/
bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
/**
Creates a JP2 decompression structure
@param cinfo Codec context info
@return Returns a handle to a JP2 decompressor if successful, returns NULL otherwise

View File

@ -39,9 +39,10 @@ RSC=rc.exe
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
@ -49,7 +50,8 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /nodefaultlib:"libcmt"
# SUBTRACT LINK32 /pdb:none
!ELSEIF "$(CFG)" == "MJ2_Extractor - Win32 Debug"
@ -62,9 +64,10 @@ LINK32=link.exe
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "MJ2_Extractor___Win32_Debug"
# PROP Intermediate_Dir "MJ2_Extractor___Win32_Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /MT /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
@ -72,7 +75,8 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmt" /pdbtype:sept
# SUBTRACT LINK32 /pdb:none
!ENDIF
@ -85,18 +89,10 @@ LINK32=link.exe
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.c
# End Source File
# Begin Source File
SOURCE=.\extract_j2k_from_mj2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.c
# End Source File
# Begin Source File
SOURCE=.\mj2.c
# End Source File
# End Group
@ -105,24 +101,96 @@ SOURCE=.\mj2.c
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# End Group
# Begin Group "Resource Files"
# Begin Group "OpenJPEG Header Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\event.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\image.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k_lib.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\opj_includes.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# End Target
# End Project

44
mj2/MJ2_Extractor.dsw Normal file
View File

@ -0,0 +1,44 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Project: "MJ2_Extractor"=".\MJ2_Extractor.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name LibOpenJPEG
End Project Dependency
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View File

@ -39,9 +39,10 @@ RSC=rc.exe
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
@ -62,9 +63,10 @@ LINK32=link.exe
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "MJ2_Wrapper___Win32_Debug"
# PROP Intermediate_Dir "MJ2_Wrapper___Win32_Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /MT /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
@ -72,7 +74,8 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmt" /pdbtype:sept
# SUBTRACT LINK32 /pdb:none
!ENDIF
@ -85,18 +88,6 @@ LINK32=link.exe
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.c
# End Source File
# Begin Source File
SOURCE=.\mj2.c
# End Source File
# Begin Source File
@ -109,28 +100,96 @@ SOURCE=.\wrap_j2k_in_mj2.c
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# End Group
# Begin Group "Resource Files"
# Begin Group "OpenJPEG Header Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\event.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\image.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k_lib.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\opj_includes.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# End Target
# End Project

44
mj2/MJ2_Wrapper.dsw Normal file
View File

@ -0,0 +1,44 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Project: "MJ2_Wrapper"=".\MJ2_Wrapper.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name LibOpenJPEG
End Project Dependency
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View File

@ -1,79 +0,0 @@
#
# Valentin Mesaros (Octalis | DICE/UCL Belgium)
# 19 April 2005
#
# Makefile for mj2 source files (frames_to_mj2, mj2_to_frames, extract_j2k_from_mj2, wrap_j2k_in_mj2)
#
INCLUDES = -Imj2 \
-I../libopenjpeg_097
C_OPT = -Wall
CFLAGS = $(INCLUDES) $(C_OPT)
CC = gcc
OBJ_MJ2_DIR = obj
BIN_MJ2_DIR = bin
LIB_OPENJPEG_DIR = ../libopenjpeg_097/lib
LIB_OPENJPEG = $(LIB_OPENJPEG_DIR)/libopenjpeg_097.a
all: $(OBJ_MJ2_DIR) $(BIN_MJ2_DIR) \
$(BIN_MJ2_DIR)/frames_to_mj2 \
$(BIN_MJ2_DIR)/mj2_to_frames \
$(BIN_MJ2_DIR)/extract_j2k_from_mj2 \
$(BIN_MJ2_DIR)/wrap_j2k_in_mj2
$(OBJ_MJ2_DIR):
mkdir $(OBJ_MJ2_DIR)
$(BIN_MJ2_DIR):
mkdir $(BIN_MJ2_DIR)
$(OBJ_MJ2_DIR)/%.o:
$(CC) -c $(CFLAGS) -o $@ $<
$(OBJ_MJ2_DIR)/frames_to_mj2.o: frames_to_mj2.c
$(OBJ_MJ2_DIR)/mj2_to_frames.o: mj2_to_frames.c
$(OBJ_MJ2_DIR)/mj2.o: mj2.c
$(OBJ_MJ2_DIR)/mj2_convert.o: mj2_convert.c
$(OBJ_MJ2_DIR)/extract_j2k_from_mj2.o: extract_j2k_from_mj2.c
$(OBJ_MJ2_DIR)/wrap_j2k_in_mj2.o: wrap_j2k_in_mj2.c
MJ2_OBJS = $(addprefix $(OBJ_MJ2_DIR)/, mj2.o mj2_convert.o)
$(BIN_MJ2_DIR)/frames_to_mj2: $(OBJ_MJ2_DIR)/frames_to_mj2.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/frames_to_mj2 \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/frames_to_mj2.o \
$(LIB_OPENJPEG) \
-lm
$(BIN_MJ2_DIR)/mj2_to_frames: $(OBJ_MJ2_DIR)/mj2_to_frames.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/mj2_to_frames \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/mj2_to_frames.o \
$(LIB_OPENJPEG) \
-lm
$(BIN_MJ2_DIR)/extract_j2k_from_mj2: $(OBJ_MJ2_DIR)/extract_j2k_from_mj2.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/extract_j2k_from_mj2 \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/extract_j2k_from_mj2.o \
$(LIB_OPENJPEG) \
-lm
$(BIN_MJ2_DIR)/wrap_j2k_in_mj2: $(OBJ_MJ2_DIR)/wrap_j2k_in_mj2.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/wrap_j2k_in_mj2 \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/wrap_j2k_in_mj2.o \
$(LIB_OPENJPEG) \
-lm
clean:
rm -f $(BIN_MJ2_DIR)/* $(OBJ_MJ2_DIR)/*

View File

@ -1,85 +0,0 @@
#include <stdio.h>
#ifdef WIN32
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <setjmp.h>
#include "mj2.h"
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
int main(int argc, char *argv[]) {
int tnum;
unsigned int 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;
}

View File

@ -1,997 +0,0 @@
/*
* Copyright (c) 2003-2004, François-Olivier Devaux
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <openjpeg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <setjmp.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
#else
#include "compat/getopt.h"
#endif
#include "mj2_convert.h"
#define MJ2_MDAT 0x6d646174
#define JP2_JP2C 0x6a703263
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
void help_display()
{
fprintf(stdout,"HELP\n----\n\n");
fprintf(stdout,"- the -h option displays this help information on screen\n\n");
fprintf(stdout,"List of parameters for the MJ2 encoder:\n");
fprintf(stdout,"\n");
fprintf(stdout,"REMARKS:\n");
fprintf(stdout,"---------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
fprintf
(stdout,"COD and QCD never appear in the tile_header.\n");
fprintf(stdout,"\n");
fprintf
(stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
fprintf
(stdout,"color image. You need enough disk space memory (twice the original) to encode \n");
fprintf
(stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
fprintf(stdout,"\n");
fprintf(stdout,"By default:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf(stdout," * Lossless\n");
fprintf(stdout," * 1 tile\n");
fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
fprintf(stdout," * Size of code-block : 64 x 64\n");
fprintf(stdout," * Number of resolutions: 6\n");
fprintf(stdout," * No SOP marker in the codestream\n");
fprintf(stdout," * No EPH marker in the codestream\n");
fprintf(stdout," * No sub-sampling in x or y direction\n");
fprintf(stdout," * No mode switch activated\n");
fprintf(stdout," * Progression order: LRCP\n");
fprintf(stdout," * No index file\n");
fprintf(stdout," * No ROI upshifted\n");
fprintf(stdout," * No offset of the origin of the image\n");
fprintf(stdout," * No offset of the origin of the tiles\n");
fprintf(stdout," * Reversible DWT 5-3\n");
fprintf(stdout,"\n");
fprintf(stdout,"Parameters:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"Required Parameters (except with -h):\n");
fprintf
(stdout,"-i : source file (-i source.yuv) \n");
fprintf
(stdout,"-o : destination file (-o dest.mj2) \n");
fprintf
(stdout,"Optional Parameters:\n");
fprintf(stdout,"-h : display the help information \n");
fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
fprintf(stdout," - The rate specified for each quality level is the desired \n");
fprintf(stdout," compression factor.\n");
fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"-n : number of resolutions (-n 3) \n");
fprintf(stdout,"-b : size of code block (-b 32,32) \n");
fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
fprintf(stdout,"-t : size of tile (-t 512,512) \n");
fprintf
(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
fprintf
(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
fprintf
(stdout,"-SOP : write SOP marker before each packet \n");
fprintf
(stdout,"-EPH : write EPH marker after each header packet \n");
fprintf
(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
fprintf
(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
fprintf
(stdout," Indicate multiple modes by adding their values. \n");
fprintf
(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
fprintf
(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
fprintf
(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
fprintf
(stdout," for component c=%%d [%%d = 0,1,2]\n");
fprintf
(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
fprintf
(stdout,"-d : offset of the origin of the image (-d 150,300) \n");
fprintf
(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
fprintf(stdout,"-W : image width, height and the dx and dy subsampling \n");
fprintf(stdout," of the Cb and Cr components for YUV files \n");
fprintf(stdout," (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n");
fprintf(stdout,"-F : video frame rate (set to 25 by default)\n");
fprintf(stdout,"\n");
fprintf(stdout,"IMPORTANT:\n");
fprintf(stdout,"-----------\n");
fprintf(stdout,"\n");
fprintf(stdout,"The index file has the structure below:\n");
fprintf(stdout,"---------------------------------------\n");
fprintf(stdout,"\n");
fprintf(stdout,"Image_height Image_width\n");
fprintf(stdout,"progression order\n");
fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
fprintf(stdout,"Components_nb\n");
fprintf(stdout,"Layers_nb\n");
fprintf(stdout,"decomposition_levels\n");
fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
fprintf(stdout,"Main_header_end_position\n");
fprintf(stdout,"Codestream_size\n");
fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
fprintf(stdout,"MaxDisto\n");
fprintf(stdout,"TotalDisto\n\n");
}
int give_progression(char progression[4])
{
if (progression[0] == 'L' && progression[1] == 'R'
&& progression[2] == 'C' && progression[3] == 'P') {
return 0;
} else {
if (progression[0] == 'R' && progression[1] == 'L'
&& progression[2] == 'C' && progression[3] == 'P') {
return 1;
} else {
if (progression[0] == 'R' && progression[1] == 'P'
&& progression[2] == 'C' && progression[3] == 'L') {
return 2;
} else {
if (progression[0] == 'P' && progression[1] == 'C'
&& progression[2] == 'R' && progression[3] == 'L') {
return 3;
} else {
if (progression[0] == 'C' && progression[1] == 'P'
&& progression[2] == 'R' && progression[3] == 'L') {
return 4;
} else {
return -1;
}
}
}
}
}
}
double dwt_norms_97[4][10] = {
{1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
};
int floorlog2(int a)
{
int l;
for (l = 0; a > 1; l++) {
a >>= 1;
}
return l;
}
void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
{
int p, n;
p = floorlog2(stepsize) - 13;
n = 11 - floorlog2(stepsize);
*mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
*expn = numbps - p;
}
void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
{
int numbands, bandno;
numbands = 3 * tccp->numresolutions - 2;
for (bandno = 0; bandno < numbands; bandno++) {
double stepsize;
int resno, level, orient, gain;
resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
level = tccp->numresolutions - 1 - resno;
gain =
tccp->qmfbid == 0 ? 0 : (orient ==
0 ? 0 : (orient == 1
|| orient == 2 ? 1 : 2));
if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
stepsize = 1.0;
} else {
double norm = dwt_norms_97[orient][level];
stepsize = (1 << (gain + 1)) / norm;
}
encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
&tccp->stepsizes[bandno].expn,
&tccp->stepsizes[bandno].mant);
}
}
int main(int argc, char **argv)
{
int NumResolution, numD_min; /* NumResolution : number of resolution */
int Tile_arg; /* Tile_arg = 0 (not in argument) ou = 1 (in argument) */
int CSty; /* CSty : coding style */
int Prog_order; /* progression order (default LRCP) */
char progression[4];
int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
int prcw_init[J2K_MAXRLVLS]; /* Initialisation Precinct width */
int prch_init[J2K_MAXRLVLS]; /* Initialisation Precinct height */
//int prcw_init, prch_init; /* Initialisation precincts' size */
int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
int mode, value; /* Mode switch (cblk_style) */
int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
int ROI_compno, ROI_shift; /* region of interrest */
int Dim[2]; /* portion of the image coded */
int TX0, TY0; /* tile off-set */
mj2_movie_t movie;
j2k_cp_t cp, cp_init; /* cp_init is used to initialise in multiple tiles */
j2k_tcp_t *tcp, *tcp_init; /* tcp_init is used to initialise in multiple tile */
j2k_poc_t POC[32]; /* POC : used in case of Progression order change */
j2k_poc_t *tcp_poc;
j2k_tccp_t *tccp;
int i, tileno, l, j;
char *infile = 0;
char *outfile = 0;
char *index = 0;
char *s, S1, S2, S3;
char *buf;
int ir = 0;
int res_spec = 0; /* For various precinct sizes specification */
char sep;
int w; /* Width of YUV file */
int h; /* Height of YUV file */
int CbCr_subsampling_dx; /* Sample rate of YUV 4:4:4 4:2:2 or 4:2:0 */
int CbCr_subsampling_dy; /* Sample rate of YUV 4:4:4 4:2:2 or 4:2:0 */
int frame_rate; /* Video Frame Rate */
int numcomps; /* In YUV files, numcomps always considered as 3 */
int prec; /* In YUV files, precision always considered as 8 */
int x1, y1, len, jp2c_initpos, m, k, pos;
long mdat_initpos, offset;
FILE *mj2file, *yuvfile;
unsigned int sampleno;
j2k_image_t img;
/* default value */
/* ------------- */
NumResolution = 6;
CSty = 0;
cblockw_init = 64;
cblockh_init = 64;
cp.tw = 1;
cp.th = 1;
cp.index_on = 0;
Prog_order = 0;
numpocs = 0;
mode = 0;
subsampling_dx = 1;
subsampling_dy = 1;
ROI_compno = -1; /* no ROI */
ROI_shift = 0;
Dim[0] = 0;
Dim[1] = 0;
TX0 = 0;
TY0 = 0;
cp.comment = NULL;
cp.disto_alloc = 0;
cp.fixed_alloc = 0;
cp.fixed_quality = 0; //add fixed_quality
w = 352; // CIF default value
h = 288; // CIF default value
CbCr_subsampling_dx = 2; // CIF default value
CbCr_subsampling_dy = 2; // CIF default value
frame_rate = 25;
Tile_arg = 0;
cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t)); /* initialisation if only one tile */
tcp_init = &cp_init.tcps[0];
tcp_init->numlayers = 0;
cp.intermed_file = 0; // Don't store each tile in a file during processing
while (1) {
int c = getopt(argc, argv,
"i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W:F:");
if (c == -1)
break;
switch (c) {
case 'i': /* IN fill */
infile = optarg;
s = optarg;
while (*s) {
s++;
}
s--;
S3 = *s;
s--;
S2 = *s;
s--;
S1 = *s;
if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
|| (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
cp.decod_format = YUV_DFMT;
break;
}
fprintf(stderr,
"!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
S1, S2, S3);
return 1;
break;
/* ----------------------------------------------------- */
case 'o': /* OUT fill */
outfile = optarg;
while (*outfile) {
outfile++;
}
outfile--;
S3 = *outfile;
outfile--;
S2 = *outfile;
outfile--;
S1 = *outfile;
outfile = optarg;
if ((S1 == 'm' && S2 == 'j' && S3 == '2')
|| (S1 == 'M' && S2 == 'J' && S3 == '2'))
cp.cod_format = MJ2_CFMT;
else {
fprintf(stderr,
"Unknown output format image *.%c%c%c [only *.mj2]!! \n",
S1, S2, S3);
return 1;
}
break;
/* ----------------------------------------------------- */
case 'r': /* rates rates/distorsion */
s = optarg;
while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
== 1) {
tcp_init->numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
cp.disto_alloc = 1;
cp.matrice = NULL;
break;
/* ----------------------------------------------------- */
case 'q': /* add fixed_quality */
s = optarg;
while (sscanf
(s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) == 1) {
tcp_init->numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
cp.fixed_quality = 1;
cp.matrice = NULL;
break;
/* dda */
/* ----------------------------------------------------- */
case 'f': /* mod fixed_quality (before : -q) */
s = optarg;
sscanf(s, "%d", &tcp_init->numlayers);
s++;
if (tcp_init->numlayers > 9)
s++;
cp.matrice =
(int *) malloc(tcp_init->numlayers * NumResolution * 3 *
sizeof(int));
s = s + 2;
for (i = 0; i < tcp_init->numlayers; i++) {
tcp_init->rates[i] = 1;
sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
s += 2;
if (cp.matrice[i * NumResolution * 3] > 9)
s++;
cp.matrice[i * NumResolution * 3 + 1] = 0;
cp.matrice[i * NumResolution * 3 + 2] = 0;
for (j = 1; j < NumResolution; j++) {
sscanf(s, "%d,%d,%d",
&cp.matrice[i * NumResolution * 3 + j * 3 + 0],
&cp.matrice[i * NumResolution * 3 + j * 3 + 1],
&cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
s += 6;
if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
s++;
if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
s++;
if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
s++;
}
if (i < tcp_init->numlayers - 1)
s++;
}
cp.fixed_alloc = 1;
break;
/* ----------------------------------------------------- */
case 't': /* tiles */
sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
Tile_arg = 1;
break;
/* ----------------------------------------------------- */
case 'n': /* resolution */
sscanf(optarg, "%d", &NumResolution);
break;
/* ----------------------------------------------------- */
case 'c': /* precinct dimension */
s = optarg;
do {
sep = 0;
sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
&prch_init[res_spec], &sep);
CSty |= 0x01;
res_spec++;
s = strpbrk(s, "]") + 2;
} while (sep == ',');
break;
/* ----------------------------------------------------- */
case 'b': /* code-block dimension */
sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
|| cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
fprintf(stderr,
"!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'x': /* creation of index file */
index = optarg;
cp.index_on = 1;
break;
/* ----------------------------------------------------- */
case 'p': /* progression order */
s = optarg;
for (i = 0; i < 4; i++) {
progression[i] = *s;
s++;
}
Prog_order = give_progression(progression);
if (Prog_order == -1) {
fprintf(stderr,
"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 's': /* subsampling factor */
if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy)
!= 2) {
fprintf(stderr,
"'-s' sub-sampling argument error ! [-s dx,dy]\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'd': /* coordonnate of the reference grid */
if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
fprintf(stderr,
"-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'h': /* Display an help description */
help_display();
return 0;
break;
/* ----------------------------------------------------- */
case 'P': /* POC */
fprintf(stderr, "/----------------------------------\\\n");
fprintf(stderr, "| POC option not fully tested !! |\n");
fprintf(stderr, "\\----------------------------------/\n");
s = optarg;
while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
&POC[numpocs].resno0, &POC[numpocs].compno0,
&POC[numpocs].layno1, &POC[numpocs].resno1,
&POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
POC[numpocs].prg = give_progression(POC[numpocs].progorder);
/* POC[numpocs].tile; */
numpocs++;
while (*s && *s != '/') {
s++;
}
if (!*s)
break;
s++;
}
break;
/* ------------------------------------------------------ */
case 'S': /* SOP marker */
CSty |= 0x02;
break;
/* ------------------------------------------------------ */
case 'E': /* EPH marker */
CSty |= 0x04;
break;
/* ------------------------------------------------------ */
case 'M': /* Mode switch pas tous au point !! */
if (sscanf(optarg, "%d", &value) == 1) {
for (i = 0; i <= 5; i++) {
int cache = value & (1 << i);
if (cache)
mode |= (1 << i);
}
}
break;
/* ------------------------------------------------------ */
case 'R': /* ROI */
if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'T': /* Tile offset */
if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'C': /* Add a comment */
cp.comment = optarg;
break;
/* ------------------------------------------------------ */
case 'I': /* reversible or not */
ir = 1;
break;
/* ------------------------------------------------------ */
case 'W': /* Width and Height and Cb and Cr subsampling in case of YUV format files */
if (sscanf
(optarg, "%d,%d,%d,%d", &w, &h, &CbCr_subsampling_dx,
&CbCr_subsampling_dy) != 4) {
fprintf(stderr, "-W argument error");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'F': /* Video frame rate */
if (sscanf(optarg, "%d", &frame_rate) != 1) {
fprintf(stderr, "-F argument error");
return 1;
}
break;
/* ------------------------------------------------------ */
default:
return 1;
}
}
cp.tx0 = TX0;
cp.ty0 = TY0;
/* Error messages */
/* -------------- */
if (!infile || !outfile) {
fprintf(stderr,
"Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
return 1;
}
if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
&& (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
fprintf(stderr,
"Error: options -r -q and -f can not be used together !!\n");
return 1;
} // mod fixed_quality
/* if no rate entered, lossless by default */
if (tcp_init->numlayers == 0) {
tcp_init->rates[tcp_init->numlayers] = 0; //MOD antonin : losslessbug
tcp_init->numlayers++;
cp.disto_alloc = 1;
}
if (TX0 > Dim[0] || TY0 > Dim[1]) {
fprintf(stderr,
"Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
TX0, Dim[0], TY0, Dim[1]);
return 1;
}
for (i = 0; i < numpocs; i++) {
if (POC[i].prg == -1) {
fprintf(stderr,
"Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
i + 1);
}
}
/* to respect profile - 0 */
/* ---------------------- */
numD_min = 0;
x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
1) *
subsampling_dx + 1;
y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
1) *
subsampling_dy + 1;
if (Tile_arg == 1) {
cp.tw = int_ceildiv(x1 - cp.tx0, cp.tdx);
cp.th = int_ceildiv(y1 - cp.ty0, cp.tdy);
} else {
cp.tdx = x1 - cp.tx0;
cp.tdy = y1 - cp.ty0;
}
/* Initialization for PPM marker */
cp.ppm = 0;
cp.ppm_data = NULL;
cp.ppm_previous = 0;
cp.ppm_store = 0;
numcomps = 3; /* Because YUV files only have 3 components */
tcp_init->mct = 0; /* No component transform needed */
prec = 8; /* Because in YUV files, components have 8-bit depth */
/* Init the mutiple tiles */
/* ---------------------- */
cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
tcp = &cp.tcps[tileno];
tcp->numlayers = tcp_init->numlayers;
for (j = 0; j < tcp->numlayers; j++) {
if (cp.fixed_quality) // add fixed_quality
tcp->distoratio[j] = tcp_init->distoratio[j];
else
tcp->rates[j] = tcp_init->rates[j];
}
tcp->csty = CSty;
tcp->prg = Prog_order;
tcp->mct = tcp_init->mct;
tcp->ppt = 0;
tcp->ppt_data = NULL;
tcp->ppt_store = 0;
numpocs_tile = 0;
tcp->POC = 0;
if (numpocs) {
/* intialisation of POC */
tcp->POC = 1;
for (i = 0; i < numpocs; i++) {
if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
tcp_poc = &tcp->pocs[numpocs_tile];
tcp_poc->resno0 = POC[numpocs_tile].resno0;
tcp_poc->compno0 = POC[numpocs_tile].compno0;
tcp_poc->layno1 = POC[numpocs_tile].layno1;
tcp_poc->resno1 = POC[numpocs_tile].resno1;
tcp_poc->compno1 = POC[numpocs_tile].compno1;
tcp_poc->prg = POC[numpocs_tile].prg;
tcp_poc->tile = POC[numpocs_tile].tile;
numpocs_tile++;
}
}
}
tcp->numpocs = numpocs_tile;
tcp->tccps = (j2k_tccp_t *) malloc(numcomps * sizeof(j2k_tccp_t));
for (i = 0; i < numcomps; i++) {
tccp = &tcp->tccps[i];
tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */
tccp->numresolutions = NumResolution;
tccp->cblkw = int_floorlog2(cblockw_init);
tccp->cblkh = int_floorlog2(cblockh_init);
tccp->cblksty = mode;
tccp->qmfbid = ir ? 0 : 1;
tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
tccp->numgbits = 2;
if (i == ROI_compno)
tccp->roishift = ROI_shift;
else
tccp->roishift = 0;
if (CSty & J2K_CCP_CSTY_PRT) {
int p = 0;
for (j = tccp->numresolutions - 1; j >= 0; j--) {
if (p < res_spec) {
if (prcw_init[p] < 1)
tccp->prcw[j] = 1;
else
tccp->prcw[j] = int_floorlog2(prcw_init[p]);
if (prch_init[p] < 1)
tccp->prch[j] = 1;
else
tccp->prch[j] = int_floorlog2(prch_init[p]);
} else {
int size_prcw, size_prch;
size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
if (size_prcw < 1)
tccp->prcw[j] = 1;
else
tccp->prcw[j] = int_floorlog2(size_prcw);
if (size_prch < 1)
tccp->prch[j] = 1;
else
tccp->prch[j] = int_floorlog2(size_prch);
}
p++;
/*printf("\nsize precinct pour level %d : %d,%d\n", j,
tccp->prcw[j], tccp->prch[j]); */
}
} else {
for (j = 0; j < tccp->numresolutions; j++) {
tccp->prcw[j] = 15;
tccp->prch[j] = 15;
}
}
calc_explicit_stepsizes(tccp, prec);
}
}
mj2file = fopen(outfile, "wb");
if (!mj2file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
movie.tk = (mj2_tk_t *) malloc(sizeof(mj2_tk_t));
movie.num_vtk = 1;
movie.num_stk = 0;
movie.num_htk = 0;
movie.tk[0].track_type = 0; // Video Track
movie.tk[0].track_ID = 1;
movie.tk[0].Dim[0] = Dim[0];
movie.tk[0].Dim[1] = Dim[1];
movie.tk[0].w = w;
movie.tk[0].h = h;
movie.tk[0].CbCr_subsampling_dx = CbCr_subsampling_dx;
movie.tk[0].CbCr_subsampling_dy = CbCr_subsampling_dy;
movie.tk[0].sample_rate = frame_rate;
movie.tk[0].jp2_struct.numcomps = 3; // NC
jp2_init_stdjp2(&movie.tk[0].jp2_struct);
movie.tk[0].jp2_struct.w = w;
movie.tk[0].jp2_struct.h = h;
movie.tk[0].jp2_struct.bpc = 7;
movie.tk[0].jp2_struct.meth = 1;
movie.tk[0].jp2_struct.enumcs = 18; // YUV
yuvfile = fopen(infile,"rb");
if (!yuvfile) {
fprintf(stderr, "failed to open %s for reading\n",infile);
return 1;
}
movie.tk[0].num_samples = yuv_num_frames(&movie.tk[0],yuvfile);
if (!movie.tk[0].num_samples) {
fprintf(stderr,"Unable to count the number of frames in YUV input file\n");
}
// One sample per chunk
movie.tk[0].chunk = (mj2_chunk_t*) malloc(movie.tk[0].num_samples * sizeof(mj2_chunk_t));
movie.tk[0].sample = (mj2_sample_t*) malloc(movie.tk[0].num_samples * sizeof(mj2_sample_t));
if (mj2_init_stdmovie(&movie)) {
fprintf(stderr, "Error with movie initialization");
return 1;
};
// Writing JP, FTYP and MDAT boxes
buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP
// boxes won't be longer than 300 bytes
cio_init(buf , 300);
mj2_write_jp();
mj2_write_ftyp(&movie);
mdat_initpos = cio_tell();
cio_skip(4);
cio_write(MJ2_MDAT, 4);
fwrite(buf,cio_tell(),1,mj2file);
offset = cio_tell();
free(buf);
for (i = 0; i < movie.num_stk + movie.num_htk + movie.num_vtk; i++) {
if (movie.tk[i].track_type != 0) {
fprintf(stderr, "Unable to write sound or hint tracks\n");
} else {
//j2k_cp_t cp_init;
mj2_tk_t *tk;
tk = &movie.tk[i];
//tk->sample =
//(mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
tk->num_chunks = tk->num_samples;
//tk->chunk =
//(mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
fprintf(stderr, "Video Track number %d\n", i + 1);
// Copy the first tile coding parameters (tcp) to cp_init
//cp_init.tcps =
//(j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
for (l = 0; l < cp.tcps[tileno].numlayers; l++) {
cp_init.tcps[tileno].rates[l] = cp.tcps[tileno].rates[l];
//tileno = cp.tcps[tileno].rates[l];
}
}
for (sampleno = 0; sampleno < tk->num_samples; sampleno++) {
buf = (char *) malloc(cp.tdx * cp.tdy * cp.th * cp.tw * 2);
cio_init(buf, cp.tdx * cp.tdy * cp.th * cp.tw * 2);
fprintf(stderr, "Frame number %d/%d: ", sampleno + 1, tk->num_samples);
if (!yuvtoimage(yuvfile, tk, &img, sampleno, subsampling_dx, subsampling_dy)) {
fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
return 1;
}
jp2c_initpos = cio_tell();
cio_skip(4);
cio_write(JP2_JP2C, 4); // JP2C
len = j2k_encode(&img, &cp, buf, cp.tdx * cp.tdy * 2, index);
if (len ==0) {
fprintf(stderr,"Unable to encode image");
return 1;
}
cio_seek(jp2c_initpos);
cio_write(len+8,4);
for (m = 0; m < img.numcomps; m++) {
free(img.comps[m].data);
}
free(img.comps);
tk->sample[sampleno].sample_size = len+8;
tk->sample[sampleno].offset = offset;
tk->chunk[sampleno].offset = offset; // There is one sample per chunk
fwrite(buf, 1, len+8, mj2file);
offset += len+8;
free(buf);
// Copy the cp_init parameters to cp.tcps
for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
for (k = 0; k < cp.tcps[tileno].numlayers; k++) {
cp.tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k];
}
}
}
}
}
fseek(mj2file, mdat_initpos, SEEK_SET);
buf = (char*) malloc(4*sizeof(char));
cio_init(buf, 4); // Init a cio to write box length variable in a little endian way
cio_write(offset - mdat_initpos, 4);
fwrite(buf, 4, 1, mj2file);
fseek(mj2file,0,SEEK_END);
free(buf);
// Writing MOOV box
i=1;
buf = (char*) malloc (10000 * sizeof(char));
cio_init(buf , i*10000);
if (setjmp(j2k_error)) {
i++;
realloc(buf,i*10000* sizeof(char));
pos = cio_tell();
cio_init(buf , i*10000);
cio_seek(pos);
}
mj2_write_moov(&movie);
fwrite(buf,cio_tell(),1,mj2file);
// Ending program
mj2_memory_free(&movie);
free(cp_init.tcps);
if (tcp_init->numlayers > 9)
free(cp.matrice);
for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
free(cp.tcps[tileno].tccps);
free(cp.tcps);
fclose(mj2file);
free(buf);
remove("Compo0");
remove("Compo1");
remove("Compo2");
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,201 +0,0 @@
/*
* Copyright (c) 2003-2004, François-Olivier Devaux
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <j2k.h>
#include <jp2.h>
#ifndef __MJ2_H
#define __MJ2_H
typedef struct { /* Time To Sample */
int sample_count;
int sample_delta;
} mj2_tts_t;
typedef struct { /* Chunk */
int num_samples;
int sample_descr_idx;
int offset;
} mj2_chunk_t;
typedef struct { /* Sample to chunk */
int first_chunk;
int samples_per_chunk;
int sample_descr_idx;
} mj2_sampletochunk_t;
typedef struct { /* Sample */
unsigned int sample_size;
unsigned int offset;
unsigned int sample_delta;
} mj2_sample_t;
typedef struct { /* URL */
int location[4];
} mj2_url_t;
typedef struct { /* URN */
int name[2];
int location[4];
} mj2_urn_t;
typedef struct { /* Video Track Parameters */
int track_ID;
int track_type;
unsigned int creation_time;
unsigned int modification_time;
int duration;
int timescale;
int layer;
int volume;
int language;
int balance;
int maxPDUsize;
int avgPDUsize;
int maxbitrate;
int avgbitrate;
int slidingavgbitrate;
int graphicsmode;
int opcolor[3];
int num_url;
mj2_url_t *url;
int num_urn;
mj2_urn_t *urn;
int Dim[2];
int w;
int h;
int visual_w;
int visual_h;
int CbCr_subsampling_dx;
int CbCr_subsampling_dy;
// int subsampling_dx;
// int subsampling_dy;
int sample_rate;
int sample_description;
int horizresolution;
int vertresolution;
int compressorname[8];
int depth;
unsigned char fieldcount;
unsigned char fieldorder;
unsigned char or_fieldcount;
unsigned char or_fieldorder;
int num_br;
unsigned int *br;
unsigned char num_jp2x;
unsigned char *jp2xdata;
unsigned char hsub;
unsigned char vsub;
unsigned char hoff;
unsigned char voff;
int trans_matrix[9];
unsigned int num_samples; /* Number of samples */
int transorm;
int handler_type;
int name_size;
unsigned char same_sample_size;
int num_tts;
mj2_tts_t *tts; /* Time to sample */
unsigned int num_chunks;
mj2_chunk_t *chunk;
int num_samplestochunk;
mj2_sampletochunk_t *sampletochunk;
char *name;
jp2_struct_t jp2_struct;
mj2_sample_t *sample; /* Sample parameters */
} mj2_tk_t; /* Track Parameters */
typedef struct { /* Movie */
unsigned int brand;
unsigned int minversion;
int num_cl;
unsigned int *cl;
unsigned int creation_time;
unsigned int modification_time;
int timescale;
unsigned int duration;
int rate;
int num_vtk;
int num_stk;
int num_htk;
int volume;
int trans_matrix[9];
int next_tk_id;
mj2_tk_t *tk; /* Track Parameters */
} mj2_movie_t;
typedef struct {
int length;
int type;
int init_pos;
} mj2_box_t;
void mj2_write_jp();
void mj2_write_ftyp(mj2_movie_t * movie);
/*
* Use this function to initialize a standard movie with standard values
* It has one sample per chunk
*/
int mj2_init_stdmovie(mj2_movie_t * movie);
/* int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index);
*
* Encode a MJ2 movie from a yuv file
* movie: an existing mj2_movie structure (to create a standard one, use mj2_init_stdmovie
* cp: coding parameters of j2k images
* index: index file name
*/
int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index);
/* int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie,
* j2k_cp_t * cp, char *outfile);
*
* Decode a MJ2 movie to a yuv file
* src: pointer to memory where frames will be stored
* movie: a mj2_movie structure
* cp: coding parameters of j2k images
* outfile: yuv file name
*/
int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie,
j2k_cp_t * cp, char *outfile);
/*
* Free memory used to encode and decode mj2 files
*
*/
void mj2_memory_free(mj2_movie_t * movie);
int mj2_read_struct(FILE *file, mj2_movie_t * movie);
void mj2_write_moov(mj2_movie_t * movie);
#endif

View File

@ -1,16 +0,0 @@
#include "mj2.h"
#ifndef __MJ2_CONVERT_H
#define __MJ2_CONVERT_H
int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile);
int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile);
int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
int frame_num, int subsampling_dx, int subsampling_dy);
int yuv_num_frames(mj2_tk_t * tk, FILE *f);
#endif

View File

@ -1,134 +0,0 @@
#include <stdio.h>
#ifdef WIN32
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <setjmp.h>
#include "mj2.h"
#include "mj2_convert.h"
#include <openjpeg.h>
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#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];
j2k_image_t img;
j2k_cp_t cp;
int i;
cp.layer=0;
cp.reduce=0;
if (argc != 3) {
printf("Bad syntax: Usage: mj2_to_frames inputfile.mj2 outputfile.yuv\n");
printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
return 1;
}
file = fopen(argv[1], "rb");
if (!file) {
fprintf(stderr, "failed to open %s for reading\n", argv[1]);
return 1;
}
// Checking output file
outfile = fopen(argv[2], "w");
if (!file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
fclose(outfile);
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];
// Output info on first video tracl
fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
track->num_samples, track->w, track->h);
for (snum=0; snum < track->num_samples; snum++)
{
fprintf(stdout,"Frame %d: ",snum+1);
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
if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image
return 1;
if (((img.numcomps == 3) && (img.comps[0].dx == img.comps[1].dx / 2)
&& (img.comps[0].dx == img.comps[2].dx / 2 ) && (img.comps[0].dx == 1))
|| (img.numcomps == 1)) {
if (imagetoyuv(&img, &cp, argv[2])) // Convert image to YUV
return 1;
}
else if ((img.numcomps == 3) &&
(img.comps[0].dx == 1) && (img.comps[1].dx == 1)&&
(img.comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp
{
fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
sprintf(outfilename,"output_%d.bmp",snum);
if (imagetobmp(&img, &cp, outfilename)) // Convert image to YUV
return 1;
}
else {
fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
sprintf(outfilename,"output_%d.j2k",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);
}
for (i=0; i<img.numcomps; i++)
free(img.comps[i].data);
j2k_dec_release();
free(frame_codestream);
}
fclose(file);
fprintf(stdout, "%d frame(s) correctly extracted\n", snum);
mj2_memory_free(&movie);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

View File

@ -1,291 +0,0 @@
#include <stdio.h>
#ifdef WIN32
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <setjmp.h>
#include <string.h>
#include "mj2.h"
#include <cio.h>
#include <j2k.h>
#include <int.h>
#define MJ2_MJ2 0x6d6a7032
#define MJ2_MJ2S 0x6d6a3273
#define JP2_JP2C 0x6a703263
#define MJ2_MDAT 0x6d646174
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
void j2k_read_siz_marker(FILE *file, j2k_image_t *j2k_img)
{
int len,i;
char buf, buf2[2];
char *siz_buffer;
fseek(file, 0, SEEK_SET);
do {
fread(&buf,1,1, file);
if (buf==(char)0xff)
fread(&buf,1,1, file);
}
while (!(buf==(char)0x51));
fread(buf2,2,1,file); /* Lsiz */
len = ((buf2[0])<<8) + buf2[1];
siz_buffer = (char*) malloc(len * sizeof(char));
fread(siz_buffer,len, 1, file);
cio_init(siz_buffer,len);
cio_read(2); /* Rsiz (capabilities) */
j2k_img->x1 = cio_read(4); /* Xsiz */
j2k_img->y1 = cio_read(4); /* Ysiz */
j2k_img->x0 = cio_read(4); /* X0siz */
j2k_img->y0 = cio_read(4); /* Y0siz */
cio_skip(16); /* XTsiz, YTsiz, XT0siz, YT0siz */
j2k_img->numcomps = cio_read(2); /* Csiz */
j2k_img->comps =
(j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < j2k_img->numcomps; i++) {
int tmp;
tmp = cio_read(1); /* Ssiz_i */
j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
j2k_img->comps[i].sgnd = tmp >> 7;
j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */
j2k_img->comps[i].dy = cio_read(1); /* YRsiz_i */
j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */
j2k_img->comps[i].factor = 0; /* reducing factor by component */
}
free(siz_buffer);
fseek(file, 0, SEEK_SET);
}
void setparams(mj2_movie_t *movie, j2k_image_t *img) {
int i, depth_0, depth, sign;
movie->tk[0].sample_rate = 25;
movie->tk[0].w = int_ceildiv(img->x1 - img->x0, img->comps[0].dx);
movie->tk[0].h = int_ceildiv(img->y1 - img->y0, img->comps[0].dy);
mj2_init_stdmovie(movie);
movie->tk[0].depth = img->comps[0].prec;
if (img->numcomps==3) {
if ((img->comps[0].dx == 1) && (img->comps[1].dx == 1) && (img->comps[1].dx == 1))
movie->tk[0].CbCr_subsampling_dx = 1;
else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[1].dx == 2))
movie->tk[0].CbCr_subsampling_dx = 2;
else
fprintf(stderr,"Image component sizes are incoherent\n");
if ((img->comps[0].dy == 1) && (img->comps[1].dy == 1) && (img->comps[1].dy == 1))
movie->tk[0].CbCr_subsampling_dy = 1;
else if ((img->comps[0].dy == 1) && (img->comps[1].dy == 2) && (img->comps[1].dy == 2))
movie->tk[0].CbCr_subsampling_dy = 2;
else
fprintf(stderr,"Image component sizes are incoherent\n");
}
movie->tk[0].sample_rate = 25;
movie->tk[0].jp2_struct.numcomps = img->numcomps; // NC
jp2_init_stdjp2(&movie->tk[0].jp2_struct);
movie->tk[0].jp2_struct.w = int_ceildiv(img->x1 - img->x0, img->comps[0].dx);
movie->tk[0].jp2_struct.h = int_ceildiv(img->y1 - img->y0, img->comps[0].dy);
depth_0 = img->comps[0].prec - 1;
sign = img->comps[0].sgnd;
movie->tk[0].jp2_struct.bpc = depth_0 + (sign << 7);
for (i = 1; i < img->numcomps; i++) {
depth = img->comps[i].prec - 1;
sign = img->comps[i].sgnd;
if (depth_0 != depth)
movie->tk[0].jp2_struct.bpc = 255;
}
for (i = 0; i < img->numcomps; i++)
movie->tk[0].jp2_struct.comps[i].bpcc =
img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
if ((img->numcomps == 1 || img->numcomps == 3)
&& (movie->tk[0].jp2_struct.bpc != 255))
movie->tk[0].jp2_struct.meth = 1;
else
movie->tk[0].jp2_struct.meth = 2;
if (img->numcomps == 1)
movie->tk[0].jp2_struct.enumcs = 17; // Grayscale
else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 1) && (img->comps[1].dx == 1) &&
(img->comps[0].dy == 1) && (img->comps[1].dy == 1) && (img->comps[1].dy == 1))
movie->tk[0].jp2_struct.enumcs = 16; // RGB
else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[1].dx == 2) &&
(img->comps[0].dy == 1) && (img->comps[1].dy == 2) && (img->comps[1].dy == 2))
movie->tk[0].jp2_struct.enumcs = 18; // YUV
else
movie->tk[0].jp2_struct.enumcs = 0; // Unkown profile */
}
int main(int argc, char *argv[]) {
unsigned int snum;
mj2_movie_t movie;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *mj2file, *j2kfile;
char j2kfilename[50];
char *buf;
int offset, mdat_initpos;
j2k_image_t img;
int i;
if (argc != 3) {
printf("Bad syntax: Usage: MJ2_Wrapper source_location mj2_filename\n");
printf("Example: MJ2_Wrapper input/input output.mj2\n");
return 1;
}
mj2file = fopen(argv[2], "wb");
if (!mj2file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
// Initialing the movie (parameters used in the JP and FTYP boxes
movie.num_htk = 0; // No hint tracks
movie.num_stk = 0; // No sound tracks
movie.num_vtk = 1; // One video track
movie.tk = (mj2_tk_t*) malloc (sizeof(mj2_tk_t)); //Memory allocation for the video track
movie.tk[0].sample = (mj2_sample_t*) malloc (sizeof(mj2_sample_t));
movie.tk[0].chunk = (mj2_chunk_t *) malloc(sizeof(mj2_chunk_t));
movie.tk[0].track_type = 0; // Video track
movie.tk[0].track_ID = 1; // Track ID = 1
movie.brand = MJ2_MJ2; // One brand: MJ2
movie.num_cl = 2; // Two compatible brands: MJ2 and MJ2S
movie.cl = (unsigned int *) malloc(movie.num_cl * sizeof(unsigned int));
movie.cl[0] = MJ2_MJ2;
movie.cl[1] = MJ2_MJ2S;
movie.minversion = 0; // Minimum version: 0
// Writing JP, FTYP and MDAT boxes
buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP
// boxes won't be longer than 300 bytes
cio_init(buf , 300);
mj2_write_jp();
mj2_write_ftyp(&movie);
mdat_initpos = cio_tell();
cio_skip(4);
cio_write(MJ2_MDAT, 4);
fwrite(buf,cio_tell(),1,mj2file);
free(buf);
// Insert each j2k codestream in a JP2C box
snum=0;
offset = 0;
while(1)
{
sample = &movie.tk[0].sample[snum];
sprintf(j2kfilename,"%s_%d.j2k",argv[1],snum);
j2kfile = fopen(j2kfilename, "rb");
if (!j2kfile) {
if (snum==0) { // Could not open a single codestream
fprintf(stderr, "failed to open %s for reading\n",j2kfilename);
return 1;
}
else { // Tried to open a inexistant codestream
fprintf(stdout,"%d frames created\n",snum);
break;
}
}
// Calculating offset for samples and chunks
offset += cio_tell();
sample->offset = offset;
movie.tk[0].chunk[snum].offset = offset; // There will be one sample per chunk
// Calculating sample size
fseek(j2kfile,0,SEEK_END);
sample->sample_size = ftell(j2kfile) + 8; // Sample size is codestream + JP2C box header
fseek(j2kfile,0,SEEK_SET);
// Reading siz marker of j2k image for the first codestream
if (snum==0)
j2k_read_siz_marker(j2kfile, &img);
// Writing JP2C box header
frame_codestream = (unsigned char*) malloc (sample->sample_size+8);
cio_init(frame_codestream, sample->sample_size);
cio_write(sample->sample_size, 4); // Sample size
cio_write(JP2_JP2C, 4); // JP2C
// Writing codestream from J2K file to MJ2 file
fread(frame_codestream+8,sample->sample_size-8,1,j2kfile);
fwrite(frame_codestream,sample->sample_size,1,mj2file);
cio_skip(sample->sample_size-8);
// Ending loop
fclose(j2kfile);
snum++;
movie.tk[0].sample = realloc(movie.tk[0].sample, (snum+1) * sizeof(mj2_sample_t));
movie.tk[0].chunk = realloc(movie.tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t));
free(frame_codestream);
}
// Writing the MDAT box length in header
offset += cio_tell();
buf = (char*) malloc (4 * sizeof(char));
cio_init(buf,4);
cio_write(offset-mdat_initpos,4); // Write MDAT length in MDAT box header
fseek(mj2file,(long)mdat_initpos,SEEK_SET);
fwrite(buf,4,1,mj2file);
fseek(mj2file,0,SEEK_END);
free(buf);
// Setting movie parameters
movie.tk[0].num_samples=snum;
movie.tk[0].num_chunks=snum;
setparams(&movie, &img);
// Writing MOOV box
i=1;
buf = (char*) malloc (10000 * sizeof(char));
cio_init(buf , i*10000);
if (setjmp(j2k_error)) {
i++;
buf = realloc(buf,i*10000* sizeof(char));
cio_init(buf , i*10000);
}
mj2_write_moov(&movie);
fwrite(buf,cio_tell(),1,mj2file);
// Ending program
fclose(mj2file);
free(img.comps);
free(buf);
mj2_memory_free(&movie);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

20
mj2/Makefile Normal file
View File

@ -0,0 +1,20 @@
# Makefile for the MJ2 codecs of the OpenJPEG library: frames_to_mj2, mj2_to_frames, extract_j2k_from_mj2 and wrap_j2k_in_mj2
CFLAGS = -O3 -lstdc++ # -g -p -pg
all: frames_to_mj2 mj2_to_frames extract_j2k_from_mj2 wrap_j2k_in_mj2
frames_to_mj2: frames_to_mj2.c ../libopenjpeg.a
gcc $(CFLAGS) compat/getopt.c mj2_convert.c mj2.c frames_to_mj2.c -o frames_to_mj2 -L.. -lopenjpeg -I ../libopenjpeg/ -lm
mj2_to_frames: mj2_to_frames.c ../libopenjpeg.a
gcc $(CFLAGS) compat/getopt.c mj2_convert.c mj2.c mj2_to_frames.c -o mj2_to_frames -L.. -lopenjpeg -I ../libopenjpeg/ -lm
extract_j2k_from_mj2: extract_j2k_from_mj2.c ../libopenjpeg.a
gcc $(CFLAGS) mj2.c extract_j2k_from_mj2.c -o extract_j2k_from_mj2 -L.. -lopenjpeg -I ../libopenjpeg/ -lm
wrap_j2k_in_mj2: wrap_j2k_in_mj2.c ../libopenjpeg.a
gcc $(CFLAGS) mj2.c wrap_j2k_in_mj2.c -o wrap_j2k_in_mj2 -L.. -lopenjpeg -I ../libopenjpeg/ -lm
clean:
rm -f frames_to_mj2 mj2_to_frames extract_j2k_from_mj2 wrap_j2k_in_mj2

142
mj2/extract_j2k_from_mj2.c Normal file
View File

@ -0,0 +1,142 @@
/*
* Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2007, Professor Benoit Macq
* Copyright (c) 2003-2007, Francois-Olivier Devaux
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "opj_includes.h"
#include "mj2.h"
/* -------------------------------------------------------------------------- */
/**
sample error callback expecting a FILE* client object
*/
void error_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[ERROR] %s", msg);
}
/**
sample warning callback expecting a FILE* client object
*/
void warning_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[WARNING] %s", msg);
}
/**
sample debug callback expecting a FILE* client object
*/
void info_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[INFO] %s", msg);
}
/* -------------------------------------------------------------------------- */
int main(int argc, char *argv[]) {
opj_dinfo_t* dinfo;
opj_event_mgr_t event_mgr; /* event manager */
int tnum;
unsigned int snum;
opj_mj2_t *movie;
mj2_tk_t *track;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *file, *outfile;
char outfilename[50];
mj2_dparameters_t parameters;
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;
}
/*
configure the event callbacks (not required)
setting of each callback is optionnal
*/
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
/* get a MJ2 decompressor handle */
dinfo = mj2_create_decompress();
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
/* setup the decoder decoding parameters using user parameters */
movie = (opj_mj2_t*) dinfo->mj2_handle;
mj2_setup_decoder(dinfo->mj2_handle, &parameters);
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_%05d.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);
/* free remaining structures */
if(dinfo) {
mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
}
return 0;
}

811
mj2/frames_to_mj2.c Normal file
View File

@ -0,0 +1,811 @@
/*
* Copyright (c) 2003-2004, François-Olivier Devaux
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "opj_includes.h"
#include "mj2.h"
#include "mj2_convert.h"
#include "compat/getopt.h"
/**
Size of memory first allocated for MOOV box
*/
#define TEMP_BUF 10000
/* -------------------------------------------------------------------------- */
/**
sample error callback expecting a FILE* client object
*/
void error_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[ERROR] %s", msg);
}
/**
sample warning callback expecting a FILE* client object
*/
void warning_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[WARNING] %s", msg);
}
/**
sample debug callback expecting a FILE* client object
*/
void info_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[INFO] %s", msg);
}
/* -------------------------------------------------------------------------- */
void help_display()
{
fprintf(stdout,"HELP\n----\n\n");
fprintf(stdout,"- the -h option displays this help information on screen\n\n");
fprintf(stdout,"List of parameters for the MJ2 encoder:\n");
fprintf(stdout,"\n");
fprintf(stdout,"REMARKS:\n");
fprintf(stdout,"---------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
fprintf
(stdout,"COD and QCD never appear in the tile_header.\n");
fprintf(stdout,"\n");
fprintf
(stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
fprintf
(stdout,"color image. You need enough disk space memory (twice the original) to encode \n");
fprintf
(stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
fprintf(stdout,"\n");
fprintf(stdout,"By default:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf(stdout," * Lossless\n");
fprintf(stdout," * 1 tile\n");
fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
fprintf(stdout," * Size of code-block : 64 x 64\n");
fprintf(stdout," * Number of resolutions: 6\n");
fprintf(stdout," * No SOP marker in the codestream\n");
fprintf(stdout," * No EPH marker in the codestream\n");
fprintf(stdout," * No sub-sampling in x or y direction\n");
fprintf(stdout," * No mode switch activated\n");
fprintf(stdout," * Progression order: LRCP\n");
fprintf(stdout," * No index file\n");
fprintf(stdout," * No ROI upshifted\n");
fprintf(stdout," * No offset of the origin of the image\n");
fprintf(stdout," * No offset of the origin of the tiles\n");
fprintf(stdout," * Reversible DWT 5-3\n");
fprintf(stdout,"\n");
fprintf(stdout,"Parameters:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"Required Parameters (except with -h):\n");
fprintf
(stdout,"-i : source file (-i source.yuv) \n");
fprintf
(stdout,"-o : destination file (-o dest.mj2) \n");
fprintf
(stdout,"Optional Parameters:\n");
fprintf(stdout,"-h : display the help information \n");
fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
fprintf(stdout," - The rate specified for each quality level is the desired \n");
fprintf(stdout," compression factor.\n");
fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"-n : number of resolutions (-n 3) \n");
fprintf(stdout,"-b : size of code block (-b 32,32) \n");
fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
fprintf(stdout,"-t : size of tile (-t 512,512) \n");
fprintf
(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
fprintf
(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
fprintf
(stdout,"-SOP : write SOP marker before each packet \n");
fprintf
(stdout,"-EPH : write EPH marker after each header packet \n");
fprintf
(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
fprintf
(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
fprintf
(stdout," Indicate multiple modes by adding their values. \n");
fprintf
(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
fprintf
(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
fprintf
(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
fprintf
(stdout," for component c=%%d [%%d = 0,1,2]\n");
fprintf
(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
fprintf
(stdout,"-d : offset of the origin of the image (-d 150,300) \n");
fprintf
(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
fprintf(stdout,"-W : image width, height and the dx and dy subsampling \n");
fprintf(stdout," of the Cb and Cr components for YUV files \n");
fprintf(stdout," (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n");
fprintf(stdout,"-F : video frame rate (set to 25 by default)\n");
fprintf(stdout,"\n");
fprintf(stdout,"IMPORTANT:\n");
fprintf(stdout,"-----------\n");
fprintf(stdout,"\n");
fprintf(stdout,"The index file has the structure below:\n");
fprintf(stdout,"---------------------------------------\n");
fprintf(stdout,"\n");
fprintf(stdout,"Image_height Image_width\n");
fprintf(stdout,"progression order\n");
fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
fprintf(stdout,"Components_nb\n");
fprintf(stdout,"Layers_nb\n");
fprintf(stdout,"decomposition_levels\n");
fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
fprintf(stdout,"Main_header_end_position\n");
fprintf(stdout,"Codestream_size\n");
fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
fprintf(stdout,"MaxDisto\n");
fprintf(stdout,"TotalDisto\n\n");
}
int give_progression(char progression[4])
{
if (progression[0] == 'L' && progression[1] == 'R'
&& progression[2] == 'C' && progression[3] == 'P') {
return 0;
} else {
if (progression[0] == 'R' && progression[1] == 'L'
&& progression[2] == 'C' && progression[3] == 'P') {
return 1;
} else {
if (progression[0] == 'R' && progression[1] == 'P'
&& progression[2] == 'C' && progression[3] == 'L') {
return 2;
} else {
if (progression[0] == 'P' && progression[1] == 'C'
&& progression[2] == 'R' && progression[3] == 'L') {
return 3;
} else {
if (progression[0] == 'C' && progression[1] == 'P'
&& progression[2] == 'R' && progression[3] == 'L') {
return 4;
} else {
return -1;
}
}
}
}
}
}
int main(int argc, char **argv)
{
mj2_cparameters_t mj2_parameters; /* MJ2 compression parameters */
opj_cparameters_t *j2k_parameters; /* J2K compression parameters */
opj_event_mgr_t event_mgr; /* event manager */
opj_cio_t *cio;
char value;
opj_mj2_t *movie;
opj_image_t *img;
int i, j;
char *s, S1, S2, S3;
char *buf;
int x1, y1, len;
long mdat_initpos, offset;
FILE *mj2file;
int sampleno;
opj_cinfo_t* cinfo;
bool bSuccess;
int numframes;
double total_time = 0;
/* default value */
/* ------------- */
mj2_parameters.Dim[0] = 0;
mj2_parameters.Dim[1] = 0;
mj2_parameters.w = 352; // CIF default value
mj2_parameters.h = 288; // CIF default value
mj2_parameters.CbCr_subsampling_dx = 2; // CIF default value
mj2_parameters.CbCr_subsampling_dy = 2; // CIF default value
mj2_parameters.frame_rate = 25;
/*
configure the event callbacks (not required)
setting of each callback is optionnal
*/
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = NULL;
/* set J2K encoding parameters to default values */
opj_set_default_encoder_parameters(&mj2_parameters.j2k_parameters);
j2k_parameters = &mj2_parameters.j2k_parameters;
/* Create comment for codestream */
if(j2k_parameters->cp_comment == NULL) {
const char comment[] = "Created by OpenJPEG version ";
const size_t clen = strlen(comment);
const char *version = opj_version();
j2k_parameters->cp_comment = (char*)malloc(clen+strlen(version)+1);
sprintf(j2k_parameters->cp_comment,"%s%s", comment, version);
}
while (1) {
int c = getopt(argc, argv,
"i:o:r:q:f:t:n:c:b:x:p:s:d:h P:S:E:M:R:T:C:I:W:F:");
if (c == -1)
break;
switch (c) {
case 'i': /* IN fill */
{
char *infile = optarg;
s = optarg;
while (*s) {
s++;
}
s--;
S3 = *s;
s--;
S2 = *s;
s--;
S1 = *s;
if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
|| (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
mj2_parameters.decod_format = YUV_DFMT;
}
else {
fprintf(stderr,
"!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
S1, S2, S3);
return 1;
}
strncpy(mj2_parameters.infile, infile, sizeof(mj2_parameters.infile)-1);
}
break;
/* ----------------------------------------------------- */
case 'o': /* OUT fill */
{
char *outfile = optarg;
while (*outfile) {
outfile++;
}
outfile--;
S3 = *outfile;
outfile--;
S2 = *outfile;
outfile--;
S1 = *outfile;
outfile = optarg;
if ((S1 == 'm' && S2 == 'j' && S3 == '2')
|| (S1 == 'M' && S2 == 'J' && S3 == '2'))
mj2_parameters.cod_format = MJ2_CFMT;
else {
fprintf(stderr,
"Unknown output format image *.%c%c%c [only *.mj2]!! \n",
S1, S2, S3);
return 1;
}
strncpy(mj2_parameters.outfile, outfile, sizeof(mj2_parameters.outfile)-1);
}
break;
/* ----------------------------------------------------- */
case 'r': /* rates rates/distorsion */
{
float rate;
s = optarg;
while (sscanf(s, "%f", &rate) == 1) {
j2k_parameters->tcp_rates[j2k_parameters->tcp_numlayers] = rate * 2;
j2k_parameters->tcp_numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
j2k_parameters->cp_disto_alloc = 1;
}
break;
/* ----------------------------------------------------- */
case 'q': /* add fixed_quality */
s = optarg;
while (sscanf(s, "%f", &j2k_parameters->tcp_distoratio[j2k_parameters->tcp_numlayers]) == 1) {
j2k_parameters->tcp_numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
j2k_parameters->cp_fixed_quality = 1;
break;
/* dda */
/* ----------------------------------------------------- */
case 'f': /* mod fixed_quality (before : -q) */
{
int *row = NULL, *col = NULL;
int numlayers = 0, numresolution = 0, matrix_width = 0;
s = optarg;
sscanf(s, "%d", &numlayers);
s++;
if (numlayers > 9)
s++;
j2k_parameters->tcp_numlayers = numlayers;
numresolution = j2k_parameters->numresolution;
matrix_width = numresolution * 3;
j2k_parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
s = s + 2;
for (i = 0; i < numlayers; i++) {
row = &j2k_parameters->cp_matrice[i * matrix_width];
col = row;
j2k_parameters->tcp_rates[i] = 1;
sscanf(s, "%d,", &col[0]);
s += 2;
if (col[0] > 9)
s++;
col[1] = 0;
col[2] = 0;
for (j = 1; j < numresolution; j++) {
col += 3;
sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
s += 6;
if (col[0] > 9)
s++;
if (col[1] > 9)
s++;
if (col[2] > 9)
s++;
}
if (i < numlayers - 1)
s++;
}
j2k_parameters->cp_fixed_alloc = 1;
}
break;
/* ----------------------------------------------------- */
case 't': /* tiles */
sscanf(optarg, "%d,%d", &j2k_parameters->cp_tdx, &j2k_parameters->cp_tdy);
j2k_parameters->tile_size_on = true;
break;
/* ----------------------------------------------------- */
case 'n': /* resolution */
sscanf(optarg, "%d", &j2k_parameters->numresolution);
break;
/* ----------------------------------------------------- */
case 'c': /* precinct dimension */
{
char sep;
int res_spec = 0;
char *s = optarg;
do {
sep = 0;
sscanf(s, "[%d,%d]%c", &j2k_parameters->prcw_init[res_spec],
&j2k_parameters->prch_init[res_spec], &sep);
j2k_parameters->csty |= 0x01;
res_spec++;
s = strpbrk(s, "]") + 2;
}
while (sep == ',');
j2k_parameters->res_spec = res_spec;
}
break;
/* ----------------------------------------------------- */
case 'b': /* code-block dimension */
{
int cblockw_init = 0, cblockh_init = 0;
sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
|| cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
fprintf(stderr,
"!! Size of code_block error (option -b) !!\n\nRestriction :\n"
" * width*height<=4096\n * 4<=width,height<= 1024\n\n");
return 1;
}
j2k_parameters->cblockw_init = cblockw_init;
j2k_parameters->cblockh_init = cblockh_init;
}
break;
/* ----------------------------------------------------- */
case 'x': /* creation of index file */
{
char *index = optarg;
strncpy(j2k_parameters->index, index, sizeof(j2k_parameters->index)-1);
j2k_parameters->index_on = 1;
}
break;
/* ----------------------------------------------------- */
case 'p': /* progression order */
{
char progression[4];
strncpy(progression, optarg, 4);
j2k_parameters->prog_order = give_progression(progression);
if (j2k_parameters->prog_order == -1) {
fprintf(stderr, "Unrecognized progression order "
"[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 's': /* subsampling factor */
{
if (sscanf(optarg, "%d,%d", &j2k_parameters->subsampling_dx,
&j2k_parameters->subsampling_dy) != 2) {
fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'd': /* coordonnate of the reference grid */
{
if (sscanf(optarg, "%d,%d", &j2k_parameters->image_offset_x0,
&j2k_parameters->image_offset_y0) != 2) {
fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
"error !! [-d x0,y0]\n");
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'h': /* Display an help description */
help_display();
return 0;
break;
/* ----------------------------------------------------- */
case 'P': /* POC */
{
int numpocs = 0; /* number of progression order change (POC) default 0 */
opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
char *s = optarg;
POC = j2k_parameters->POC;
while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
&POC[numpocs].resno0, &POC[numpocs].compno0,
&POC[numpocs].layno1, &POC[numpocs].resno1,
&POC[numpocs].compno1, &POC[numpocs].progorder) == 7) {
POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
numpocs++;
while (*s && *s != '/') {
s++;
}
if (!*s) {
break;
}
s++;
}
j2k_parameters->numpocs = numpocs;
}
break;
/* ------------------------------------------------------ */
case 'S': /* SOP marker */
j2k_parameters->csty |= 0x02;
break;
/* ------------------------------------------------------ */
case 'E': /* EPH marker */
j2k_parameters->csty |= 0x04;
break;
/* ------------------------------------------------------ */
case 'M': /* Mode switch pas tous au point !! */
if (sscanf(optarg, "%d", &value) == 1) {
for (i = 0; i <= 5; i++) {
int cache = value & (1 << i);
if (cache)
j2k_parameters->mode |= (1 << i);
}
}
break;
/* ------------------------------------------------------ */
case 'R': /* ROI */
{
if (sscanf(optarg, "OI:c=%d,U=%d", &j2k_parameters->roi_compno,
&j2k_parameters->roi_shift) != 2) {
fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
return 1;
}
}
break;
/* ------------------------------------------------------ */
case 'T': /* Tile offset */
{
if (sscanf(optarg, "%d,%d", &j2k_parameters->cp_tx0, &j2k_parameters->cp_ty0) != 2) {
fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
return 1;
}
}
break;
/* ------------------------------------------------------ */
case 'C': /* Add a comment */
{
j2k_parameters->cp_comment = (char*)malloc(strlen(optarg) + 1);
if(j2k_parameters->cp_comment) {
strcpy(j2k_parameters->cp_comment, optarg);
}
}
break;
/* ------------------------------------------------------ */
case 'I': /* reversible or not */
{
j2k_parameters->irreversible = 1;
}
break;
/* ------------------------------------------------------ */
case 'W': /* Width and Height and Cb and Cr subsampling in case of YUV format files */
if (sscanf
(optarg, "%d,%d,%d,%d", &mj2_parameters.w, &mj2_parameters.h, &mj2_parameters.CbCr_subsampling_dx,
&mj2_parameters.CbCr_subsampling_dy) != 4) {
fprintf(stderr, "-W argument error");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'F': /* Video frame rate */
if (sscanf(optarg, "%d", &mj2_parameters.frame_rate) != 1) {
fprintf(stderr, "-F argument error");
return 1;
}
break;
/* ------------------------------------------------------ */
default:
return 1;
}
}
/* Error messages */
/* -------------- */
if (!mj2_parameters.infile || !mj2_parameters.outfile) {
fprintf(stderr,
"Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
return 1;
}
if ((j2k_parameters->cp_disto_alloc || j2k_parameters->cp_fixed_alloc || j2k_parameters->cp_fixed_quality)
&& (!(j2k_parameters->cp_disto_alloc ^ j2k_parameters->cp_fixed_alloc ^ j2k_parameters->cp_fixed_quality))) {
fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
return 1;
} /* mod fixed_quality */
/* if no rate entered, lossless by default */
if (j2k_parameters->tcp_numlayers == 0) {
j2k_parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
j2k_parameters->tcp_numlayers++;
j2k_parameters->cp_disto_alloc = 1;
}
if((j2k_parameters->cp_tx0 > j2k_parameters->image_offset_x0) || (j2k_parameters->cp_ty0 > j2k_parameters->image_offset_y0)) {
fprintf(stderr,
"Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
j2k_parameters->cp_tx0, j2k_parameters->image_offset_x0, j2k_parameters->cp_ty0, j2k_parameters->image_offset_y0);
return 1;
}
for (i = 0; i < j2k_parameters->numpocs; i++) {
if (j2k_parameters->POC[i].prg == -1) {
fprintf(stderr,
"Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
i + 1);
}
}
if (j2k_parameters->cp_tdx > mj2_parameters.Dim[0] || j2k_parameters->cp_tdy > mj2_parameters.Dim[1]) {
fprintf(stderr,
"Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
j2k_parameters->cp_tdx, mj2_parameters.Dim[0], j2k_parameters->cp_tdy, mj2_parameters.Dim[1]);
return 1;
}
/* to respect profile - 0 */
/* ---------------------- */
x1 = !mj2_parameters.Dim[0] ? (mj2_parameters.w - 1) * j2k_parameters->subsampling_dx
+ 1 : mj2_parameters.Dim[0] + (mj2_parameters.w - 1) * j2k_parameters->subsampling_dx + 1;
y1 = !mj2_parameters.Dim[1] ? (mj2_parameters.h - 1) * j2k_parameters->subsampling_dy
+ 1 : mj2_parameters.Dim[1] + (mj2_parameters.h - 1) * j2k_parameters->subsampling_dy + 1;
mj2_parameters.numcomps = 3; /* Because YUV files only have 3 components */
mj2_parameters.prec = 8; /* Because in YUV files, components have 8-bit depth */
j2k_parameters->tcp_mct = 0;
mj2file = fopen(mj2_parameters.outfile, "wb");
if (!mj2file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
/* get a MJ2 decompressor handle */
cinfo = mj2_create_compress();
movie = cinfo->mj2_handle;
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
/* setup encoder parameters */
mj2_setup_encoder(movie, &mj2_parameters);
movie->tk[0].num_samples = yuv_num_frames(&movie->tk[0],mj2_parameters.infile);
if (!movie->tk[0].num_samples) {
fprintf(stderr,"Unable to count the number of frames in YUV input file\n");
}
// One sample per chunk
movie->tk[0].chunk = (mj2_chunk_t*) malloc(movie->tk[0].num_samples * sizeof(mj2_chunk_t));
movie->tk[0].sample = (mj2_sample_t*) malloc(movie->tk[0].num_samples * sizeof(mj2_sample_t));
if (mj2_init_stdmovie(movie)) {
fprintf(stderr, "Error with movie initialization");
return 1;
};
// Writing JP, FTYP and MDAT boxes
buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP
// boxes won't be longer than 300 bytes
cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, 300);
mj2_write_jp(cio);
mj2_write_ftyp(movie, cio);
mdat_initpos = cio_tell(cio);
cio_skip(cio, 4);
cio_write(cio, MJ2_MDAT, 4);
fwrite(buf,cio_tell(cio),1,mj2file);
offset = cio_tell(cio);
opj_free(buf);
for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
if (movie->tk[i].track_type != 0) {
fprintf(stderr, "Unable to write sound or hint tracks\n");
} else {
mj2_tk_t *tk;
int buflen = 0;
tk = &movie->tk[i];
tk->num_chunks = tk->num_samples;
numframes = tk->num_samples;
fprintf(stderr, "Video Track number %d\n", i + 1);
img = mj2_image_create(tk, j2k_parameters);
buflen = 2 * (tk->w * tk->h * 8);
buf = (char *) malloc(buflen*sizeof(char));
for (sampleno = 0; sampleno < numframes; sampleno++) {
double init_time = opj_clock();
double elapsed_time;
if (!yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) {
fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
return 1;
}
/* setup the encoder parameters using the current image and user parameters */
opj_setup_encoder(cinfo, j2k_parameters, img);
cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, buflen);
cio_skip(cio, 4);
cio_write(cio, JP2_JP2C, 4); // JP2C
/* encode the image */
bSuccess = opj_encode(cinfo, cio, img, j2k_parameters->index);
if (!bSuccess) {
opj_cio_close(cio);
fprintf(stderr, "failed to encode image\n");
return 1;
}
len = cio_tell(cio) - 8;
cio_seek(cio, 0);
cio_write(cio, len+8,4);
tk->sample[sampleno].sample_size = len+8;
tk->sample[sampleno].offset = offset;
tk->chunk[sampleno].offset = offset; // There is one sample per chunk
fwrite(buf, 1, len+8, mj2file);
offset += len+8;
elapsed_time = opj_clock()-init_time;
fprintf(stderr, "Frame number %d/%d encoded in %.2f mseconds\n", sampleno + 1, numframes, elapsed_time*1000);
total_time += elapsed_time;
}
/* free buffer data */
opj_free(buf);
/* free image data */
opj_image_destroy(img);
}
}
fseek(mj2file, mdat_initpos, SEEK_SET);
buf = (char*) malloc(4*sizeof(char));
// Init a cio to write box length variable in a little endian way
cio = opj_cio_open(NULL, buf, 4);
cio_write(cio, offset - mdat_initpos, 4);
fwrite(buf, 4, 1, mj2file);
fseek(mj2file,0,SEEK_END);
opj_free(buf);
// Writing MOOV box
buf = (char*) malloc ((TEMP_BUF+numframes*20) * sizeof(char));
cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20));
mj2_write_moov(movie, cio);
fwrite(buf,cio_tell(cio),1,mj2file);
fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time);
// Ending program
fclose(mj2file);
/* free remaining compression structures */
mj2_destroy_compress(movie);
free(cinfo);
/* free user parameters structure */
if(j2k_parameters->cp_comment) free(j2k_parameters->cp_comment);
if(j2k_parameters->cp_matrice) free(j2k_parameters->cp_matrice);
opj_cio_close(cio);
return 0;
}

View File

@ -42,7 +42,7 @@ RSC=rc.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
@ -51,6 +51,7 @@ BSC32=bscmake.exe
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# SUBTRACT LINK32 /pdb:none /incremental:yes /debug
!ELSEIF "$(CFG)" == "frames_to_mj2 - Win32 Debug"
@ -66,7 +67,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /GZ /c
# ADD CPP /nologo /MT /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
@ -74,7 +75,8 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmt" /pdbtype:sept
# SUBTRACT LINK32 /pdb:none
!ENDIF
@ -90,10 +92,6 @@ LINK32=link.exe
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\compat\getopt.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# Begin Source File
@ -122,140 +120,92 @@ SOURCE=.\mj2_convert.c
# End Source File
# End Group
# End Group
# Begin Group "Libopenjpeg_097"
# Begin Group "Libopenjpeg Header files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg_097\bio.c
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\bio.h
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.c
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.h
SOURCE=..\libopenjpeg\event.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\dwt.c
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\dwt.h
SOURCE=..\libopenjpeg\image.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\fix.c
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\fix.h
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.c
SOURCE=..\libopenjpeg\j2k_lib.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.h
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.c
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.h
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.c
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.h
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jpt.c
SOURCE=..\libopenjpeg\opj_includes.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jpt.h
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mct.c
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mct.h
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mqc.c
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mqc.h
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tgt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tgt.h
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# End Target

44
mj2/frames_to_mj2.dsw Normal file
View File

@ -0,0 +1,44 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "frames_to_mj2"=".\frames_to_mj2.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name LibOpenJPEG
End Project Dependency
}}}
###############################################################################
Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View File

@ -1,194 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
***/
#include "bio.h"
#include <stdio.h>
#include <setjmp.h>
static unsigned char *bio_start; /* pointer to the start of the buffer */
static unsigned char *bio_end; /* pointer to the end of the buffer */
static unsigned char *bio_bp; /* pointer to the present position in the buffer */
static unsigned int bio_buf; /* temporary place where each byte is read or written */
static int bio_ct; /* coder : number of bits free to write // decoder : number of bits read */
extern jmp_buf j2k_error;
/*
* Number of bytes written.
*/
int bio_numbytes()
{
return bio_bp - bio_start;
}
/*
* Init encoder.
*
* bp : Output buffer
* len : Output buffer length
*/
void bio_init_enc(unsigned char *bp, int len)
{
bio_start = bp;
bio_end = bp + len;
bio_bp = bp;
bio_buf = 0;
bio_ct = 8;
}
/*
* Init decoder.
*
* bp : Input buffer
* len : Input buffer length
*/
void bio_init_dec(unsigned char *bp, int len)
{
bio_start = bp;
bio_end = bp + len;
bio_bp = bp;
bio_buf = 0;
bio_ct = 0;
}
/*
* Write byte. --> function modified to eliminate longjmp !!!
*
*/
int bio_byteout()
{
bio_buf = (bio_buf << 8) & 0xffff;
bio_ct = bio_buf == 0xff00 ? 7 : 8;
if (bio_bp >= bio_end)
return 1;
*bio_bp++ = bio_buf >> 8;
return 0;
}
/*
* Read byte. --> function modified to eliminate longjmp !!
*
*/
int bio_bytein()
{
bio_buf = (bio_buf << 8) & 0xffff;
bio_ct = bio_buf == 0xff00 ? 7 : 8;
if (bio_bp >= bio_end)
return 1;
bio_buf |= *bio_bp++;
return 0;
}
/*
* Write bit.
*
* b : Bit to write (0 or 1)
*/
void bio_putbit(int b)
{
if (bio_ct == 0) {
bio_byteout();
}
bio_ct--;
bio_buf |= b << bio_ct;
}
/*
* Read bit.
*
*/
int bio_getbit()
{
if (bio_ct == 0) {
bio_bytein();
}
bio_ct--;
return (bio_buf >> bio_ct) & 1;
}
/*
* Write bits.
*
* v : Value of bits
* n : Number of bits to write
*/
void bio_write(int v, int n)
{
int i;
for (i = n - 1; i >= 0; i--) {
bio_putbit((v >> i) & 1);
}
}
/*
* Read bits.
*
* n : Number of bits to read
*/
int bio_read(int n)
{
int i, v;
v = 0;
for (i = n - 1; i >= 0; i--) {
v += bio_getbit() << i;
}
return v;
}
/*
* Flush bits. Modified to eliminate longjmp !!
*
*/
int bio_flush()
{
bio_ct = 0;
if (bio_byteout())
return 1;
if (bio_ct == 7) {
bio_ct = 0;
if (bio_byteout())
return 1;
}
return 0;
}
/*
* Passes the ending bits (coming from flushing)
*/
int bio_inalign()
{
bio_ct = 0;
if ((bio_buf & 0xff) == 0xff) {
if (bio_bytein())
return 1;
bio_ct = 0;
}
return 0;
}

View File

@ -1,75 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __BIO_H
#define __BIO_H
/*
* Number of bytes written.
*/
int bio_numbytes();
/*
* Init encoder.
*
* bp : Output buffer
* len : Output buffer length
*/
void bio_init_enc(unsigned char *bp, int len);
/*
* Init decoder.
*
* bp : Input buffer
* len : Input buffer length
*/
void bio_init_dec(unsigned char *bp, int len);
/*
* Write bits.
*
* v : Value of bits
* n : Number of bits to write
*/
void bio_write(int v, int n);
/*
* Read bits.
*
* n : Number of bits to read
*/
int bio_read(int n);
/*
* Flush bits. Modified to eliminate longjmp !!
*/
int bio_flush();
int bio_inalign(); /* modified to eliminated longjmp !! */
#endif

View File

@ -1,179 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "cio.h"
#include <setjmp.h>
#include <memory.h>
static unsigned char *cio_start; /* pointer to the start of the stream */
static unsigned char *cio_end; /* pointer to the end of the stream */
static unsigned char *cio_bp; /* pointer to the present position */
extern jmp_buf j2k_error;
/*
* Number of bytes written.
*/
int cio_numbytes()
{
return cio_bp - cio_start;
}
/*
* Get position in byte stream.
*/
int cio_tell()
{
return cio_bp - cio_start;
}
/*
* Set position in byte stream.
*
* pos : position, in number of bytes, from the beginning of the stream
*/
void cio_seek(int pos)
{
cio_bp = cio_start + pos;
}
/*
* Number of bytes left before the end of the stream.
*/
int cio_numbytesleft()
{
return cio_end - cio_bp;
}
/*
* Get pointer to the current position in the stream.
*/
unsigned char *cio_getbp()
{
return cio_bp;
}
/*
* Initialize byte IO
*
* bp : destination/source stream
* len : length of the stream
*/
void cio_init(unsigned char *bp, int len)
{
cio_start = bp;
cio_end = bp + len;
cio_bp = bp;
}
/*
* Write a byte.
*/
void cio_byteout(unsigned char v)
{
if (cio_bp >= cio_end)
longjmp(j2k_error, 1);
*cio_bp++ = v;
}
/*
* Read a byte.
*/
unsigned char cio_bytein()
{
if (cio_bp >= cio_end)
longjmp(j2k_error, 1);
return *cio_bp++;
}
/*
* Write some bytes.
*
* v : value to write
* n : number of bytes to write
*/
void cio_write(unsigned int v, int n)
{
int i;
for (i = n - 1; i >= 0; i--) {
cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));
}
}
/*
* Read some bytes.
*
* n : number of bytes to read
*
* return : value of the n bytes read
*/
unsigned int cio_read(int n)
{
int i;
unsigned int v;
v = 0;
for (i = n - 1; i >= 0; i--) {
v += cio_bytein() << (i << 3);
}
return v;
}
/*
* Skip some bytes.
*
* n : number of bytes to skip
*/
void cio_skip(int n)
{
cio_bp += n;
}
/*
* Read n bytes, copy to buffer
*
* n : number of bytes to transfer
*/
void cio_read_to_buf(unsigned char* src_buf, int n)/* Glenn adds */
{
if (cio_bp + n > cio_end)
longjmp(j2k_error, 1);
memcpy(cio_bp, src_buf, n);
cio_bp += n;
}
/*
* Write n bytes, copy from buffer
*
* n : number of bytes to transfer
*/
void cio_write_from_buf(unsigned char* dest_buf, int n)/* Glenn adds */
{
if (cio_bp + n > cio_end)
longjmp(j2k_error, 1);
memcpy(dest_buf, cio_bp, n);
cio_bp += n;
}

View File

@ -1,107 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __CIO_H
#define __CIO_H
/*
* Number of bytes written.
*
* returns number of bytes written
*/
int cio_numbytes();
/*
* Get position in byte stream.
*
* return position in bytes
*/
int cio_tell();
/*
* Set position in byte stream.
*
* pos : position, in number of bytes, from the beginning of the stream
*/
void cio_seek(int pos);
/*
* Number of bytes left before the end of the stream.
*
* Returns the number of bytes before the end of the stream
*/
int cio_numbytesleft();
/*
* Get pointer to the current position in the stream.
*
* return : pointer to the position
*/
unsigned char *cio_getbp();
/*
* Initialize byte IO
*
* bp : destination/source stream
* len : length of the stream
*/
void cio_init(unsigned char *bp, int len);
/*
* Write some bytes.
*
* v : value to write
* n : number of bytes to write
*/
void cio_write(unsigned int v, int n);
/*
* Read some bytes.
*
* n : number of bytes to read
*
* return : value of the n bytes read
*/
unsigned int cio_read(int n);
/*
* Skip some bytes.
*
* n : number of bytes to skip
*/
void cio_skip(int n);
/*
* Read n bytes, copy to buffer
*/
void cio_read_to_buf(unsigned char* src_buf, int n);/* Glenn Pearson adds */
/*
* Write n bytes, copy from buffer
*/
void cio_write_from_buf(unsigned char* dest_buf, int n);/* Glenn Pearson adds */
#endif

View File

@ -1,540 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2004, Yannick Verschueren
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2005, Reiner Wahler
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* NOTE:
* This is a modified version of the openjpeg dwt.c file.
* Average speed improvement compared to the original file (measured on
* my own machine, a P4 running at 3.0 GHz):
* 5x3 wavelets about 2 times faster
* 9x7 wavelets about 3 times faster
* for both, encoding and decoding.
*
* The better performance is caused by doing the 1-dimensional DWT
* within a temporary buffer where the data can be accessed sequential
* for both directions, horizontal and vertical. The 2d vertical DWT was
* the major bottleneck in the former version.
*
* I have also removed the "Add Patrick" part because it is not longer
* needed.
*
* 6/6/2005
* -Ive (aka Reiner Wahler)
* mail: ive@lilysoft.com
*/
#include "dwt.h"
#include "int.h"
#include "fix.h"
#include "tcd.h"
#include <stdlib.h>
#define S(i) a[(i)*2]
#define D(i) a[(1+(i)*2)]
#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
/* new */
#define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
#define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
/* <summary> */
/* This table contains the norms of the 5-3 wavelets for different bands. */
/* </summary> */
double dwt_norms[4][10] = {
{1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
{1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
{1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
{.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
};
/* <summary> */
/* This table contains the norms of the 9-7 wavelets for different bands. */
/* </summary> */
double dwt_norms_real[4][10] = {
{1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
};
/* <summary> */
/* Forward lazy transform (horizontal). */
/* </summary> */
void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
int i;
for (i=0; i<sn; i++) b[i]=a[2*i+cas];
for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
}
/* <summary> */
/* Forward lazy transform (vertical). */
/* </summary> */
void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
int i;
for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
}
/* <summary> */
/* Inverse lazy transform (horizontal). */
/* </summary> */
void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) {
int i;
/* for (i=0; i<sn; i++) b[2*i+cas]=a[i];*/
/* for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)];*/
int* ai;
int* bi;
ai=a;
bi=b+cas;
for (i=0; i<sn; i++) {
*bi = *ai; bi+=2; ai++;
}
ai=a+sn;
bi=b+1-cas;
for (i=0; i<dn; i++) {
*bi = *ai; bi+=2; ai++;
}
}
/* <summary> */
/* Inverse lazy transform (vertical). */
/* </summary> */
void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
int i;
/* for (i=0; i<sn; i++) b[2*i+cas]=a[i*x];*/
/* for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)*x];*/
int* ai;
int* bi;
ai=a;
bi=b+cas;
for (i=0; i<sn; i++) {
*bi = *ai; bi+=2; ai+=x;
}
ai=a+(sn*x);
bi=b+1-cas;
for (i=0; i<dn; i++) {
*bi = *ai; bi+=2; ai+=x;
}
}
/* <summary> */
/* Forward 5-3 wavelet tranform in 1-D. */
/* </summary> */
void dwt_encode_1(int *a, int dn, int sn, int cas)
{
int i;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
}
} else {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(0) *= 2;
else {
for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
}
}
}
/* <summary> */
/* Inverse 5-3 wavelet tranform in 1-D. */
/* </summary> */
void dwt_decode_1(int *a, int dn, int sn, int cas)
{
int i;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
}
} else {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(0) /= 2;
else {
for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
}
}
}
/* <summary> */
/* Forward 5-3 wavelet tranform in 2-D. */
/* </summary> */
void dwt_encode(tcd_tilecomp_t * tilec)
{
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = 0; i < l; i++) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
sn = rh1;
dn = rh - rh1;
bj=(int*)malloc(rh*sizeof(int));
for (j=0; j<rw; j++) {
aj=a+j;
for (k=0; k<rh; k++) bj[k]=aj[k*w];
dwt_encode_1(bj, dn, sn, cas_col);
dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
}
free(bj);
sn = rw1;
dn = rw - rw1;
bj=(int*)malloc(rw*sizeof(int));
for (j=0; j<rh; j++) {
aj=a+j*w;
for (k=0; k<rw; k++) bj[k]=aj[k];
dwt_encode_1(bj, dn, sn, cas_row);
dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
}
free(bj);
}
}
/* <summary> */
/* Inverse 5-3 wavelet tranform in 2-D. */
/* </summary> */
void dwt_decode(tcd_tilecomp_t * tilec, int stop)
{
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = l - 1; i >= stop; i--) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
sn = rw1;
dn = rw - rw1;
bj=(int*)malloc(rw*sizeof(int));
for (j = 0; j < rh; j++) {
aj = a+j*w;
dwt_interleave_h(aj, bj, dn, sn, cas_row);
dwt_decode_1(bj, dn, sn, cas_row);
for (k = 0; k < rw; k++) aj[k] = bj[k];
}
free(bj);
sn = rh1;
dn = rh - rh1;
bj=(int*)malloc(rh*sizeof(int));
for (j = 0; j < rw; j++) {
aj = a+j;
dwt_interleave_v(aj, bj, dn, sn, w, cas_col);
dwt_decode_1(bj, dn, sn, cas_col);
for (k = 0; k < rh; k++) aj[k * w] = bj[k];
}
free(bj);
}
}
/* <summary> */
/* Get gain of 5-3 wavelet transform. */
/* </summary> */
int dwt_getgain(int orient)
{
if (orient == 0)
return 0;
if (orient == 1 || orient == 2)
return 1;
return 2;
}
/* <summary> */
/* Get norm of 5-3 wavelet. */
/* </summary> */
double dwt_getnorm(int level, int orient)
{
return dwt_norms[orient][level];
}
/* <summary> */
/* Forward 9-7 wavelet transform in 1-D. */
/* </summary> */
void dwt_encode_1_real(int *a, int dn, int sn, int cas)
{
int i;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
for (i = 0; i < sn; i++)
S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
for (i = 0; i < dn; i++)
D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
for (i = 0; i < sn; i++)
S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 5038); /*5038 */
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 6659); /*6660 */
}
} else {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 5038); /*5038 */
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 6659); /*6660 */
}
}
}
/* <summary> */
/* Inverse 9-7 wavelet transform in 1-D. */
/* </summary> */
void dwt_decode_1_real(int *a, int dn, int sn, int cas)
{
int i;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 10078); /* 10076 */
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 13318); /* 13320 */
for (i = 0; i < sn; i++)
S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);
for (i = 0; i < sn; i++)
S(i) += fix_mul(D_(i - 1) + D_(i), 434);
for (i = 0; i < dn; i++)
D(i) += fix_mul(S_(i) + S_(i + 1), 12994); /* 12993 */
}
} else {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 10078); /* 10076 */
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 13318); /* 13320 */
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 12994); /* 12993 */
}
}
}
/* <summary> */
/* Forward 9-7 wavelet transform in 2-D. */
/* </summary> */
void dwt_encode_real(tcd_tilecomp_t * tilec)
{
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = 0; i < l; i++) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
sn = rh1;
dn = rh - rh1;
bj=(int*)malloc(rh*sizeof(int));
for (j = 0; j < rw; j++) {
aj = a + j;
for (k = 0; k < rh; k++) bj[k] = aj[k*w];
dwt_encode_1_real(bj, dn, sn, cas_col);
dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
}
free(bj);
sn = rw1;
dn = rw - rw1;
bj=(int*)malloc(rw*sizeof(int));
for (j = 0; j < rh; j++) {
aj = a + j * w;
for (k = 0; k < rw; k++) bj[k] = aj[k];
dwt_encode_1_real(bj, dn, sn, cas_row);
dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
}
free(bj);
}
}
/* <summary> */
/* Inverse 9-7 wavelet transform in 2-D. */
/* </summary> */
void dwt_decode_real(tcd_tilecomp_t * tilec, int stop)
{
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = l-1; i >= stop; i--) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
cas_col = tilec->resolutions[l - i].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
cas_row = tilec->resolutions[l - i].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
sn = rw1;
dn = rw-rw1;
bj = (int*)malloc(rw * sizeof(int));
for (j = 0; j < rh; j++) {
aj = a+j*w;
dwt_interleave_h(aj, bj, dn, sn, cas_col);
dwt_decode_1_real(bj, dn, sn, cas_col);
for (k = 0; k < rw; k++) aj[k] = bj[k];
}
free(bj);
sn = rh1;
dn = rh-rh1;
bj = (int*)malloc(rh * sizeof(int));
for (j=0; j<rw; j++) {
aj = a+j;
dwt_interleave_v(aj, bj, dn, sn, w, cas_row);
dwt_decode_1_real(bj, dn, sn, cas_row);
for (k = 0; k < rh; k++) aj[k * w] = bj[k];
}
free(bj);
}
}
/* <summary> */
/* Get gain of 9-7 wavelet transform. */
/* </summary> */
int dwt_getgain_real(int orient)
{
(void)orient;
return 0;
}
/* <summary> */
/* Get norm of 9-7 wavelet. */
/* </summary> */
double dwt_getnorm_real(int level, int orient)
{
return dwt_norms_real[orient][level];
}

View File

@ -1,81 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "tcd.h"
#ifndef __DWT_H
#define __DWT_H
/*
* Apply a reversible DWT transform to a component of an image
* tilec : tile component information (present tile)
*/
/* void dwt_encode(int* a, int w, int h, int l); */
void dwt_encode(tcd_tilecomp_t * tilec);
/*
* Apply a reversible inverse DWT transform to a component of an image
* tilec : tile component information (present tile)
*/
void dwt_decode(tcd_tilecomp_t * tilec, int stop);
/*
* Get the gain of a subband for the reversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
*/
int dwt_getgain(int orient);
/*
* Get the norm of a wavelet function of a subband at a specified level for the reversible DWT
* level: level of the wavelet function
* orient: band of the wavelet function
*/
double dwt_getnorm(int level, int orient);
/*
* Apply an irreversible DWT transform to a component of an image
*/
void dwt_encode_real(tcd_tilecomp_t * tilec);
/*
* Apply an irreversible inverse DWT transform to a component of an image
*/
void dwt_decode_real(tcd_tilecomp_t * tilec, int stop);
/*
* Get the gain of a subband for the irreversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
*/
int dwt_getgain_real(int orient);
/*
* Get the norm of a wavelet function of a subband at a specified level for the irreversible DWT
* level: level of the wavelet function
* orient: band of the wavelet function
*/
double dwt_getnorm_real(int level, int orient);
#endif

View File

@ -1,63 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "fix.h"
#include <math.h> /*Add Antonin : multbug1*/
#ifdef WIN32
#define int64 __int64
#else
#define int64 long long
#endif
/*
* Multiply two fixed-precision rational numbers.
*/
/*int fix_mul(int a, int b)
{
return (int) ((int64) a * (int64) b >> 13);
}*/
/*Mod Antonin : multbug1*/
/*
int fix_mul(int a, int b)
{
double tmp= (double) ((int64) a * (int64) b);
int64 v = (int64) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));
v = (tmp<0)?-v:v;
return (int) v;
}
*/
/*doM*/
int fix_mul(int a, int b) /* Luke Lee optimized : 11/16/2004*/
{
int64 temp = (int64) a * (int64) b >> 12;
return (int) ((temp >> 1) + (temp & 1)) ;
}

View File

@ -1,33 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __FIX_H
#define __FIX_H
int fix_mul(int a, int b);
#endif

View File

@ -1,115 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "int.h"
/*
* Get the minimum of two integers.
*
* returns a if a < b else b
*/
int int_min(int a, int b)
{
return a < b ? a : b;
}
/*
* Get the maximum of two integers.
*
* returns a if a > b else b
*/
int int_max(int a, int b)
{
return a > b ? a : b;
}
/*
* Clamp an integer inside an interval.
*
* return a if (min < a < max)
* return max if (a > max)
* return min if (a < min)
*/
int int_clamp(int a, int min, int max)
{
if (a < min)
return min;
if (a > max)
return max;
return a;
}
/*
* Get absolute value of integer.
*/
int int_abs(int a)
{
return a < 0 ? -a : a;
}
/*
* Divide an integer and round upwards.
*
* a divided by b
*/
int int_ceildiv(int a, int b)
{
return (a + b - 1) / b;
}
/*
* Divide an integer by a power of 2 and round upwards.
*
* a divided by 2^b
*/
int int_ceildivpow2(int a, int b)
{
return (a + (1 << b) - 1) >> b;
}
/*
* Divide an integer by a power of 2 and round downwards.
*
* a divided by 2^b
*/
int int_floordivpow2(int a, int b)
{
return a >> b;
}
/*
* Get logarithm of an integer and round downwards.
*
* log2(a)
*/
int int_floorlog2(int a)
{
int l;
for (l = 0; a > 1; l++) {
a >>= 1;
}
return l;
}

View File

@ -1,88 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "j2k.h"
#ifndef __INT_H
#define __INT_H
/*
* Get the minimum of two integers.
*
* returns a if a < b else b
*/
int int_min(int a, int b);
/*
* Get the maximum of two integers.
*
* returns a if a > b else b
*/
int int_max(int a, int b);
/*
* Clamp an integer inside an interval.
*
* return a if (min < a < max)
* return max if (a > max)
* return min if (a < min)
*/
int int_clamp(int a, int min, int max);
/*
* Get absolute value of integer.
*/
int int_abs(int a);
/*
* Divide an integer and round upwards.
*
* a divided by b
*/
int int_ceildiv(int a, int b);
/*
* Divide an integer by a power of 2 and round upwards.
*
* a divided by 2^b
*/
LIBJ2K_API int int_ceildivpow2(int a, int b);
/*
* Divide an integer by a power of 2 and round downwards.
*
* a divided by 2^b
*/
LIBJ2K_API int int_floordivpow2(int a, int b);
/*
* Get logarithm of an integer and round downwards.
*
* log2(a)
*/
int int_floorlog2(int a);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,241 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define VERSION "0.0.8"
#if defined(_WIN32) && defined (OPENJPEGDLL)
#ifdef gdcmopenjpeg_EXPORTS /*LIBJ2K_EXPORTS*/
#define LIBJ2K_API __declspec(dllexport)
#else
#define LIBJ2K_API __declspec(dllimport)
#endif
#else
#ifdef OPENJPEGSTATIC
#define LIBJ2K_API extern
#else
#define LIBJ2K_API
#endif
#endif
#ifndef __J2K_H
#define __J2K_H
#define J2K_MAXRLVLS 33 /* Number of maximum resolution level authorized */
#define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /* Number of maximum sub-band linked to number of resolution level */
#define J2K_CFMT 0
#define JP2_CFMT 1
#define JPT_CFMT 2
#define MJ2_CFMT 3
#define PXM_DFMT 0
#define PGX_DFMT 1
#define BMP_DFMT 2
#define YUV_DFMT 3
#define J2K_CP_CSTY_PRT 0x01
#define J2K_CP_CSTY_SOP 0x02
#define J2K_CP_CSTY_EPH 0x04
#define J2K_CCP_CSTY_PRT 0x01
#define J2K_CCP_CBLKSTY_LAZY 0x01
#define J2K_CCP_CBLKSTY_RESET 0x02
#define J2K_CCP_CBLKSTY_TERMALL 0x04
#define J2K_CCP_CBLKSTY_VSC 0x08
#define J2K_CCP_CBLKSTY_PTERM 0x10
#define J2K_CCP_CBLKSTY_SEGSYM 0x20
#define J2K_CCP_QNTSTY_NOQNT 0
#define J2K_CCP_QNTSTY_SIQNT 1
#define J2K_CCP_QNTSTY_SEQNT 2
typedef struct {
int dx, dy; /* XRsiz, YRsiz */
int w, h; /* width and height of data */
int x0, y0; /* offset of the component compare to the whole image */
int prec; /* precision */
int bpp; /* deapth of image in bits */
int sgnd; /* signed */
int resno_decoded; /* number of decoded resolution */
int factor; /* number of division by 2 of the out image compare to the original size of image */
int *data; /* image-component data */
} j2k_comp_t;
typedef struct {
int x0, y0; /* XOsiz, YOsiz */
int x1, y1; /* Xsiz, Ysiz */
int numcomps; /* number of components */
int color_space; /* sRGB, Greyscale or YUV */
j2k_comp_t *comps; /* image-components */
} j2k_image_t;
typedef struct {
int expn; /* exponent */
int mant; /* mantissa */
} j2k_stepsize_t;
typedef struct {
int csty; /* coding style */
int numresolutions; /* number of resolutions */
int cblkw; /* width of code-blocks */
int cblkh; /* height of code-blocks */
int cblksty; /* code-block coding style */
int qmfbid; /* discrete wavelet transform identifier */
int qntsty; /* quantisation style */
j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantization */
int numgbits; /* number of guard bits */
int roishift; /* Region Of Interest shift */
int prcw[J2K_MAXRLVLS]; /* Precinct width */
int prch[J2K_MAXRLVLS]; /* Precinct height */
} j2k_tccp_t;
typedef struct {
int resno0, compno0;
int layno1, resno1, compno1;
int prg;
int tile;
char progorder[4];
} j2k_poc_t;
typedef struct {
int first; /* 1 : first part-tile of a tile */
int csty; /* coding style */
int prg; /* progression order */
int numlayers; /* number of layers */
int mct; /* multi-component transform identifier */
int rates[100]; /* rates of layers */
int numpocs; /* number of progression order changes */
int POC; /* Precise if a POC marker has been used O:NO, 1:YES */
j2k_poc_t pocs[32]; /* progression order changes */
unsigned char *ppt_data; /* packet header store there for futur use in t2_decode_packet */
unsigned char *ppt_data_first; /* pointer remaining on the first byte of the first header if ppt is used */
int ppt; /* If ppt == 1 --> there was a PPT marker for the present tile */
int ppt_store; /* Use in case of multiple marker PPT (number of info already store) */
int ppt_len; /* ppmbug1 */
float distoratio[100]; /* add fixed_quality */
j2k_tccp_t *tccps; /* tile-component coding parameters */
} j2k_tcp_t;
typedef struct {
int intermed_file; /* 1: Store each encoded tile one by one in the output file (for mega-Images)*/
int decod_format; /* 0: PGX, 1: PxM, 2: BMP */
int cod_format; /* 0: J2K, 1: JP2, 2: JPT */
int disto_alloc; /* Allocation by rate/distortion */
int fixed_alloc; /* Allocation by fixed layer */
int fixed_quality; /* add fixed_quality */
int reduce; /* if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
int layer; /* if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
int index_on; /* 0 = no index || 1 = index */
int tx0, ty0; /* XTOsiz, YTOsiz */
int tdx, tdy; /* XTsiz, YTsiz */
char *comment; /* comment for coding */
int tw, th; /* number of tiles in width and heigth */
int *tileno; /* ID number of the tiles present in the codestream */
int tileno_size; /* size of the vector tileno */
unsigned char *ppm_data; /* packet header store there for futur use in t2_decode_packet */
unsigned char *ppm_data_first; /* pointer remaining on the first byte of the first header if ppm is used */
int ppm; /* If ppm == 1 --> there was a PPM marker for the present tile */
int ppm_store; /* Use in case of multiple marker PPM (number of info already store) */
int ppm_previous; /* Use in case of multiple marker PPM (case on non-finished previous info) */
int ppm_len; /* ppmbug1 */
j2k_tcp_t *tcps; /* tile coding parameters */
int *matrice; /* Fixed layer */
} j2k_cp_t;
typedef struct {
int start_pos, end_pos; /* start and end position */
double disto; /* ADD for Marcela */
} info_packet; /* Index struct */
typedef struct {
double *thresh; /* value of thresh for each layer by tile cfr. Marcela */
int num_tile; /* Number of Tile */
int start_pos; /* Start position */
int end_header; /* End position of the header */
int end_pos; /* End position */
int pw[33], ph[33]; /* precinct number for each resolution level */
int pdx[33], pdy[33]; /* precinct size (in power of 2), in X and Y for each resolution level */
info_packet *packet; /* information concerning packets inside tile */
int nbpix; /* add fixed_quality */
double distotile; /* add fixed_quality */
} info_tile; /* index struct */
typedef struct {
int index_on;
double D_max; /* ADD for Marcela */
int num; /* numero of packet */
int index_write; /* writing the packet inthe index with t2_encode_packets */
int Im_w, Im_h; /* Image width and Height */
int Prog; /* progression order */
int Tile_x, Tile_y; /* Tile size in x and y */
int Tile_Ox, Tile_Oy;
int tw, th; /* Number of Tile in X and Y */
int Comp; /* Component numbers */
int Layer; /* number of layer */
int Decomposition; /* number of decomposition */
int Main_head_end; /* Main header position */
int codestream_size; /* codestream's size */
info_tile *tile; /* information concerning tiles inside image */
} info_image; /* index struct */
/*
* Encode an image into a JPEG-2000 codestream
* i: image to encode
* cp: coding parameters
* output: destination buffer or name of the output file when cp->intermed_file==1
* len: length of destination buffer
* index : index file name
*/
LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *output,
int len, char *index);
/* LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len); */
/*
* Decode an image from a JPEG-2000 codestream
* src: source buffer
* len: length of source buffer
* i: decode image
* cp: coding parameters that were used to encode the image
*/
LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp);
/*
* Decode an image form a JPT-stream (JPEG 2000, JPIP)
* src: source buffer
* len: length of source buffer
* i: decode image
* cp: coding parameters that were used to encode the image
*
*/
int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp);
LIBJ2K_API void j2k_dec_release();/*antonin*/
#endif

View File

@ -1,529 +0,0 @@
/*
* Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "j2k.h"
#include "jp2.h"
#include "cio.h"
#define JPIP_JPIP 0x6a706970
#define JP2_JP 0x6a502020
#define JP2_FTYP 0x66747970
#define JP2_JP2H 0x6a703268
#define JP2_IHDR 0x69686472
#define JP2_COLR 0x636f6c72
#define JP2_JP2C 0x6a703263
#define JP2_URL 0x75726c20
#define JP2_DBTL 0x6474626c
#define JP2_BPCC 0x62706363
#define JP2_JP2 0x6a703220
/*
*
* Read box headers
*
*/
int jp2_read_boxhdr(jp2_box_t * box)
{
box->init_pos = cio_tell();
box->length = cio_read(4);
box->type = cio_read(4);
if (box->length == 1) {
if (cio_read(4) != 0) {
fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
return 1;
}
box->length = cio_read(4);
if (box->length == 0)
box->length = cio_numbytesleft() + 12;
} else if (box->length == 0) {
box->length = cio_numbytesleft() + 8;
}
return 0;
}
/*
*
* Initialisation of a Standard JP2 structure
*/
int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
{
jp2_struct->comps =
(jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
jp2_struct->precedence = 0; /* PRECEDENCE*/
jp2_struct->approx = 0; /* APPROX*/
jp2_struct->brand = JP2_JP2; /* BR */
jp2_struct->minversion = 0; /* MinV */
jp2_struct->numcl = 1;
jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(int));
jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
jp2_struct->C = 7; /* C : Always 7*/
jp2_struct->UnkC = 0; /* UnkC, colorspace specified in colr box*/
jp2_struct->IPR = 0; /* IPR, no intellectual property*/
return 0;
}
void jp2_write_url(char *Idx_file)
{
unsigned int i;
char str[256];
jp2_box_t box;
sprintf(str, "%s", Idx_file);
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_URL, 4); /* DBTL*/
cio_write(0, 1); /* VERS*/
cio_write(0, 3); /* FLAG*/
for (i = 0; i < strlen(str); i++) {
cio_write(str[i], 1);
}
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the IHDR box
*
* Image Header box
*
*/
int jp2_read_ihdr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_IHDR != box.type) {
fprintf(stderr, "Error: Expected IHDR Marker\n");
return 1;
}
jp2_struct->h = cio_read(4); /* HEIGHT*/
jp2_struct->w = cio_read(4); /* WIDTH*/
jp2_struct->numcomps = cio_read(2); /* NC*/
jp2_struct->bpc = cio_read(1); /* BPC*/
jp2_struct->C = cio_read(1); /* C */
jp2_struct->UnkC = cio_read(1); /* UnkC*/
jp2_struct->IPR = cio_read(1); /* IPR*/
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with IHDR Box\n");
return 1;
}
return 0;
}
void jp2_write_ihdr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_IHDR, 4); /* IHDR*/
cio_write(jp2_struct->h, 4); /* HEIGHT*/
cio_write(jp2_struct->w, 4); /* WIDTH*/
cio_write(jp2_struct->numcomps, 2); /* NC*/
cio_write(jp2_struct->bpc, 1); /* BPC */
cio_write(jp2_struct->C, 1); /* C : Always 7*/
cio_write(jp2_struct->UnkC, 1); /* UnkC, colorspace unknow*/
cio_write(jp2_struct->IPR, 1); /* IPR, no intellectual property*/
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
void jp2_write_bpcc(jp2_struct_t * jp2_struct)
{
unsigned int i;
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_BPCC, 4); /* BPCC*/
for (i = 0; i < jp2_struct->numcomps; i++)
cio_write(jp2_struct->comps[i].bpcc, 1);
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
int jp2_read_bpcc(jp2_struct_t * jp2_struct)
{
unsigned int i;
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_BPCC != box.type) {
fprintf(stderr, "Error: Expected BPCC Marker\n");
return 1;
}
for (i = 0; i < jp2_struct->numcomps; i++)
jp2_struct->comps[i].bpcc = cio_read(1);
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with BPCC Box\n");
return 1;
}
return 0;
}
void jp2_write_colr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_COLR, 4); /* COLR*/
cio_write(jp2_struct->meth, 1); /* METH*/
cio_write(jp2_struct->precedence, 1); /* PRECEDENCE*/
cio_write(jp2_struct->approx, 1); /* APPROX*/
if (jp2_struct->meth == 1)
cio_write(jp2_struct->enumcs, 4); /* EnumCS*/
else
cio_write(0, 1); /* PROFILE (??)*/
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
int jp2_read_colr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
int skip_len;
jp2_read_boxhdr(&box);
do {
if (JP2_COLR != box.type) {
cio_skip(box.length - 8);
jp2_read_boxhdr(&box);
}
} while (JP2_COLR != box.type);
jp2_struct->meth = cio_read(1); /* METH*/
jp2_struct->precedence = cio_read(1); /* PRECEDENCE*/
jp2_struct->approx = cio_read(1); /* APPROX*/
if (jp2_struct->meth == 1)
jp2_struct->enumcs = cio_read(4); /* EnumCS*/
else {
/* SKIP PROFILE */
skip_len = box.init_pos + box.length - cio_tell();
if (skip_len < 0) {
fprintf(stderr, "Error with JP2H box size\n");
return 1;
}
cio_skip(box.init_pos + box.length - cio_tell());
}
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with BPCC Box\n");
return 1;
}
return 0;
}
/*
* Write the JP2H box
*
* JP2 Header box
*
*/
void jp2_write_jp2h(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);;
cio_write(JP2_JP2H, 4); /* JP2H */
jp2_write_ihdr(jp2_struct);
if (jp2_struct->bpc == 255)
jp2_write_bpcc(jp2_struct);
jp2_write_colr(jp2_struct);
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the JP2H box
*
* JP2 Header box
*
*/
int jp2_read_jp2h(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
int skip_len;
jp2_read_boxhdr(&box);
do {
if (JP2_JP2H != box.type) {
if (box.type == JP2_JP2C) {
fprintf(stderr, "Error: Expected JP2H Marker\n");
return 1;
}
cio_skip(box.length - 8);
jp2_read_boxhdr(&box);
}
} while (JP2_JP2H != box.type);
if (jp2_read_ihdr(jp2_struct))
return 1;
if (jp2_struct->bpc == 255) {
if (jp2_read_bpcc(jp2_struct))
return 1;
}
if (jp2_read_colr(jp2_struct))
return 1;
skip_len = box.init_pos + box.length - cio_tell();
if (skip_len < 0) {
fprintf(stderr, "Error with JP2H box size\n");
return 1;
}
cio_skip(box.init_pos + box.length - cio_tell());
return 0;
}
/*
* Write the FTYP box
*
* File type box
*
*/
void jp2_write_ftyp(jp2_struct_t * jp2_struct)
{
unsigned int i;
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_FTYP, 4); /* FTYP */
cio_write(jp2_struct->brand, 4); /* BR */
cio_write(jp2_struct->minversion, 4); /* MinV */
for (i = 0; i < jp2_struct->numcl; i++)
cio_write(jp2_struct->cl[i], 4); /* CL */
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the FTYP box
*
* File type box
*
*/
int jp2_read_ftyp(jp2_struct_t * jp2_struct)
{
int i;
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_FTYP != box.type) {
fprintf(stderr, "Error: Excpected FTYP Marker\n");
return 1;
}
jp2_struct->brand = cio_read(4); /* BR */
jp2_struct->minversion = cio_read(4); /* MinV */
jp2_struct->numcl = (box.length - 16) / 4;
jp2_struct->cl =
(unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
for (i = 0; i < (int) jp2_struct->numcl; i++)
jp2_struct->cl[i] = cio_read(4); /* CLi */
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with FTYP Box\n");
return 1;
}
return 0;
}
int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset,
char *j2k_codestream)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_JP2C, 4); /* JP2C*/
*j2k_codestream_offset = cio_tell();
memcpy(cio_getbp(), j2k_codestream, j2k_codestream_len);
box.length = 8 + j2k_codestream_len;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
return box.length;
}
int jp2_read_jp2c(unsigned int *j2k_codestream_len,
unsigned int *j2k_codestream_offset)
{
jp2_box_t box;
jp2_read_boxhdr(&box);
do {
if (JP2_JP2C != box.type) {
cio_skip(box.length - 8);
jp2_read_boxhdr(&box);
}
} while (JP2_JP2C != box.type);
*j2k_codestream_offset = cio_tell();
*j2k_codestream_len = box.length - 8;
return 0;
}
void jp2_write_jp()
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_JP, 4); /* JP*/
cio_write(0x0d0a870a, 4);
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the JP box
*
* JPEG 2000 signature
*
* return 1 if error else 0
*/
int jp2_read_jp()
{
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_JP != box.type) {
fprintf(stderr, "Error: Expected JP Marker\n");
return 1;
}
if (0x0d0a870a != cio_read(4)) {
fprintf(stderr, "Error with JP Marker\n");
return 1;
}
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with JP Box size\n");
return 1;
}
return 0;
}
int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
{
cio_init(src, len);
if (jp2_read_jp())
return 1;
if (jp2_read_ftyp(jp2_struct))
return 1;
if (jp2_read_jp2h(jp2_struct))
return 1;
if (jp2_read_jp2c
(&jp2_struct->j2k_codestream_len,
&jp2_struct->j2k_codestream_offset))
return 1;
return 0;
}
int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
char *output)
{
(void)output;
jp2_write_jp();
jp2_write_ftyp(jp2_struct);
jp2_write_jp2h(jp2_struct);
jp2_write_jp2c(jp2_struct->j2k_codestream_len,
&jp2_struct->j2k_codestream_offset, j2k_codestream);
return cio_tell();
}

View File

@ -1,118 +0,0 @@
/*
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __JP2_H
#define __JP2_H
#include "j2k.h"
typedef struct {
int depth;
int sgnd;
int bpcc;
} jp2_comps_t;
typedef struct {
unsigned int w;
unsigned int h;
unsigned int numcomps;
unsigned int bpc;
unsigned int C;
unsigned int UnkC;
unsigned int IPR;
unsigned int meth;
unsigned int approx;
unsigned int enumcs;
unsigned int precedence;
unsigned int brand;
unsigned int minversion;
unsigned int numcl;
unsigned int *cl;
jp2_comps_t *comps;
j2k_image_t *image;
unsigned int j2k_codestream_offset;
unsigned int j2k_codestream_len;
} jp2_struct_t;
typedef struct {
int length;
int type;
int init_pos;
} jp2_box_t;
/* int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img);
*
* Create a standard jp2_structure
* jp2_struct: the structure you are creating
* img: a j2k_image_t wich will help you to create the jp2_structure
*/
int jp2_init_stdjp2(jp2_struct_t * jp2_struct);
/* int jp2_write_jp2c(int j2k_len, int *j2k_codestream_offset, char *j2k_codestream)
*
* Write the jp2c codestream box
* j2k_len: the j2k codestream length
* j2k_codestream_offset: the function will return the j2k codestream offset
* j2k_codestream: the j2k codestream to include in jp2 file
*/
int jp2_write_jp2c(int j2k_len, int *j2k_codestream_offset, char *j2k_codestream);
/* int jp2_write_jp2h(jp2_struct_t * jp2_struct);
*
* Write the jp2h header box
* jp2_struct: the jp2 structure you are working with
*/
void jp2_write_jp2h(jp2_struct_t * jp2_struct);
/* int jp2_read_jp2h(jp2_struct_t * jp2_struct);
*
* Read the jp2h header box
* jp2_struct: the jp2 structure you are working with
*/
int jp2_read_jp2h(jp2_struct_t * jp2_struct);
/* int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
int j2k_len, char *output)
*
* Wrap a J2K codestream in a JP2 file
* jp2_struct: the jp2 structure used to create jp2 boxes
* j2k_codestream: the j2k codestream to include in jp2 file
* output: pointer to jp2 codestream that will be created
*/
int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
char *output);
/* int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct);
*
* Decode the structure of a JP2 file
* src: pointer to memory where compressed data is stored
* jp2_struct: the jp2 structure that will be created
* len: length of jp2 codestream
*/
int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len);
#endif

View File

@ -1,163 +0,0 @@
/*
* Copyright (c) 2004, Yannick Verschueren
* Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include "jpt.h"
#include "j2k.h"
#include "cio.h"
/*
* Read the information contains in VBAS [JPP/JPT stream message header]
* Store information (7 bits) in value
*
*/
unsigned int jpt_read_VBAS_info(unsigned int value)
{
unsigned char elmt;
elmt = cio_read(1);
while ((elmt >> 7) == 1) {
value = (value << 7);
value |= (elmt & 0x7f);
elmt = cio_read(1);
}
value = (value << 7);
value |= (elmt & 0x7f);
return value;
}
/*
* Initialize the value of the message header structure
*
*/
void jpt_init_Msg_Header(jpt_msg_header_struct_t * header)
{
header->Id = 0; /* In-class Identifier */
header->last_byte = 0; /* Last byte information */
header->Class_Id = 0; /* Class Identifier */
header->CSn_Id = 0; /* CSn : index identifier */
header->Msg_offset = 0; /* Message offset */
header->Msg_length = 0; /* Message length */
header->Layer_nb = 0; /* Auxiliary for JPP case */
}
/*
* Re-initialize the value of the message header structure
*
* Only parameters always present in message header
*
*/
void jpt_reinit_Msg_Header(jpt_msg_header_struct_t * header)
{
header->Id = 0; /* In-class Identifier */
header->last_byte = 0; /* Last byte information */
header->Msg_offset = 0; /* Message offset */
header->Msg_length = 0; /* Message length */
}
/*
* Read the message header for a JPP/JPT - stream
*
*/
void jpt_read_Msg_Header(jpt_msg_header_struct_t * header)
{
unsigned char elmt, Class = 0, CSn = 0;
jpt_reinit_Msg_Header(header);
/* ------------- */
/* VBAS : Bin-ID */
/* ------------- */
elmt = cio_read(1);
/* See for Class and CSn */
switch ((elmt >> 5) & 0x03) {
case 0:
fprintf(stderr, "Forbidden value encounter in message header !!\n");
break;
case 1:
Class = 0;
CSn = 0;
break;
case 2:
Class = 1;
CSn = 0;
break;
case 3:
Class = 1;
CSn = 1;
break;
default:
break;
}
/* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */
if (((elmt >> 4) & 0x01) == 1)
header->last_byte = 1;
/* In-class identifier */
header->Id |= (elmt & 0x0f);
if ((elmt >> 7) == 1)
header->Id = jpt_read_VBAS_info(header->Id);
/* ------------ */
/* VBAS : Class */
/* ------------ */
if (Class == 1) {
header->Class_Id = 0;
header->Class_Id = jpt_read_VBAS_info(header->Class_Id);
}
/* ---------- */
/* VBAS : CSn */
/* ---------- */
if (CSn == 1) {
header->CSn_Id = 0;
header->CSn_Id = jpt_read_VBAS_info(header->CSn_Id);
}
/* ----------------- */
/* VBAS : Msg_offset */
/* ----------------- */
header->Msg_offset = jpt_read_VBAS_info(header->Msg_offset);
/* ----------------- */
/* VBAS : Msg_length */
/* ----------------- */
header->Msg_length = jpt_read_VBAS_info(header->Msg_length);
/* ---------- */
/* VBAS : Aux */
/* ---------- */
if ((header->Class_Id & 0x01) == 1) {
header->Layer_nb = 0;
header->Layer_nb = jpt_read_VBAS_info(header->Layer_nb);
}
}

View File

@ -1,56 +0,0 @@
/*
* Copyright (c) 2004, Yannick Verschueren
* Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Message Header JPT_stream Structure
*
*/
typedef struct {
unsigned int Id; /* In-class Identifier */
unsigned int last_byte; /* Last byte information */
unsigned int Class_Id; /* Class Identifier */
unsigned int CSn_Id; /* CSn : index identifier */
unsigned int Msg_offset; /* Message offset */
unsigned int Msg_length; /* Message length */
unsigned int Layer_nb; /* Auxiliary for JPP case */
} jpt_msg_header_struct_t;
/*
* Initialize the value of the message header structure
*
* header : Message header structure
*
*/
void jpt_init_Msg_Header(jpt_msg_header_struct_t * header);
/*
* Read the message header for a JPP/JPT - stream
*
* header : Message header structure
*
*/
void jpt_read_Msg_Header(jpt_msg_header_struct_t * header);

View File

@ -1,134 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "mct.h"
#include "fix.h"
/* <summary> */
/* This table contains the norms of the basis function of the reversible MCT. */
/* </summary> */
double mct_norms[3] = { 1.732, .8292, .8292 };
/* <summary> */
/* This table contains the norms of the basis function of the irreversible MCT. */
/* </summary> */
double mct_norms_real[3] = { 1.732, 1.805, 1.573 };
/* <summary> */
/* Foward reversible MCT. */
/* </summary> */
void mct_encode(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int r, g, b, y, u, v;
r = c0[i];
g = c1[i];
b = c2[i];
y = (r + (g << 1) + b) >> 2;
u = b - g;
v = r - g;
c0[i] = y;
c1[i] = u;
c2[i] = v;
}
}
/* <summary> */
/* Inverse reversible MCT. */
/* </summary> */
void mct_decode(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int y, u, v, r, g, b;
y = c0[i];
u = c1[i];
v = c2[i];
g = y - ((u + v) >> 2);
r = v + g;
b = u + g;
c0[i] = r;
c1[i] = g;
c2[i] = b;
}
}
/* <summary> */
/* Get norm of basis function of reversible MCT. */
/* </summary> */
double mct_getnorm(int compno)
{
return mct_norms[compno];
}
/* <summary> */
/* Foward irreversible MCT. */
/* </summary> */
void mct_encode_real(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int r, g, b, y, u, v;
r = c0[i];
g = c1[i];
b = c2[i];
y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);
u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);
v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);
c0[i] = y;
c1[i] = u;
c2[i] = v;
}
}
/* <summary> */
/* Inverse irreversible MCT. */
/* </summary> */
void mct_decode_real(int *c0, int *c1, int *c2, int n)
{
int i;
for (i = 0; i < n; i++) {
int y, u, v, r, g, b;
y = c0[i];
u = c1[i];
v = c2[i];
r = y + fix_mul(v, 11485);
g = y - fix_mul(u, 2819) - fix_mul(v, 5850);
b = y + fix_mul(u, 14516);
c0[i] = r;
c1[i] = g;
c2[i] = b;
}
}
/* <summary> */
/* Get norm of basis function of irreversible MCT. */
/* </summary> */
double mct_getnorm_real(int compno)
{
return mct_norms_real[compno];
}

View File

@ -1,74 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __MCT_H
#define __MCT_H
/*
* Apply a reversible multi-component transform to an image
* R: samples for red component
* G: samples for green component
* B: samples blue component
* n: number of samples for each component
*/
void mct_encode(int *R, int *G, int *B, int n);
/*
* Apply a reversible multi-component inverse transform to an image
* Y: samples for luminance component
* U: samples for red chrominance component
* V: samples for blue chrominance component
* n: number of samples for each component
*/
void mct_decode(int *V, int *U, int *Y, int n);
/*
* Get norm of the basis function used for the reversible multi-component transform
* compno: number of the component (0->Y, 1->U, 2->V)
*/
double mct_getnorm(int compno);
/*
* Apply an irreversible multi-component transform to an image
* R: samples for red component
* G: samples for green component
* B: samples blue component
* n: number of samples for each component
*/
void mct_encode_real(int *c0, int *c1, int *c2, int n);
/*
* Apply an irreversible multi-component inverse transform to an image
* Y: samples for luminance component
* U: samples for red chrominance component
* V: samples for blue chrominance component
* n: number of samples for each component
*/
void mct_decode_real(int *c0, int *c1, int *c2, int n);
/*
* Get norm of the basis function used for the irreversible multi-component transform
* compno: number of the component (0->Y, 1->U, 2->V)
*/
double mct_getnorm_real(int compno);
#endif

View File

@ -1,591 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "mqc.h"
#include <stdio.h>
/* <summary> */
/* This struct defines the state of a context. */
/* </summary> */
typedef struct mqc_state_s {
unsigned int qeval; /* the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
int mps; /* the Most Probable Symbol (0 or 1) */
struct mqc_state_s *nmps; /* next state if the next encoded symbol is the MPS */
struct mqc_state_s *nlps; /* next state if the next encoded symbol is the LPS */
} mqc_state_t;
/* <summary> */
/* This array defines all the possible states for a context. */
/* </summary> */
mqc_state_t mqc_states[47 * 2] = {
{0x5601, 0, &mqc_states[2], &mqc_states[3]},
{0x5601, 1, &mqc_states[3], &mqc_states[2]},
{0x3401, 0, &mqc_states[4], &mqc_states[12]},
{0x3401, 1, &mqc_states[5], &mqc_states[13]},
{0x1801, 0, &mqc_states[6], &mqc_states[18]},
{0x1801, 1, &mqc_states[7], &mqc_states[19]},
{0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
{0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
{0x0521, 0, &mqc_states[10], &mqc_states[58]},
{0x0521, 1, &mqc_states[11], &mqc_states[59]},
{0x0221, 0, &mqc_states[76], &mqc_states[66]},
{0x0221, 1, &mqc_states[77], &mqc_states[67]},
{0x5601, 0, &mqc_states[14], &mqc_states[13]},
{0x5601, 1, &mqc_states[15], &mqc_states[12]},
{0x5401, 0, &mqc_states[16], &mqc_states[28]},
{0x5401, 1, &mqc_states[17], &mqc_states[29]},
{0x4801, 0, &mqc_states[18], &mqc_states[28]},
{0x4801, 1, &mqc_states[19], &mqc_states[29]},
{0x3801, 0, &mqc_states[20], &mqc_states[28]},
{0x3801, 1, &mqc_states[21], &mqc_states[29]},
{0x3001, 0, &mqc_states[22], &mqc_states[34]},
{0x3001, 1, &mqc_states[23], &mqc_states[35]},
{0x2401, 0, &mqc_states[24], &mqc_states[36]},
{0x2401, 1, &mqc_states[25], &mqc_states[37]},
{0x1c01, 0, &mqc_states[26], &mqc_states[40]},
{0x1c01, 1, &mqc_states[27], &mqc_states[41]},
{0x1601, 0, &mqc_states[58], &mqc_states[42]},
{0x1601, 1, &mqc_states[59], &mqc_states[43]},
{0x5601, 0, &mqc_states[30], &mqc_states[29]},
{0x5601, 1, &mqc_states[31], &mqc_states[28]},
{0x5401, 0, &mqc_states[32], &mqc_states[28]},
{0x5401, 1, &mqc_states[33], &mqc_states[29]},
{0x5101, 0, &mqc_states[34], &mqc_states[30]},
{0x5101, 1, &mqc_states[35], &mqc_states[31]},
{0x4801, 0, &mqc_states[36], &mqc_states[32]},
{0x4801, 1, &mqc_states[37], &mqc_states[33]},
{0x3801, 0, &mqc_states[38], &mqc_states[34]},
{0x3801, 1, &mqc_states[39], &mqc_states[35]},
{0x3401, 0, &mqc_states[40], &mqc_states[36]},
{0x3401, 1, &mqc_states[41], &mqc_states[37]},
{0x3001, 0, &mqc_states[42], &mqc_states[38]},
{0x3001, 1, &mqc_states[43], &mqc_states[39]},
{0x2801, 0, &mqc_states[44], &mqc_states[38]},
{0x2801, 1, &mqc_states[45], &mqc_states[39]},
{0x2401, 0, &mqc_states[46], &mqc_states[40]},
{0x2401, 1, &mqc_states[47], &mqc_states[41]},
{0x2201, 0, &mqc_states[48], &mqc_states[42]},
{0x2201, 1, &mqc_states[49], &mqc_states[43]},
{0x1c01, 0, &mqc_states[50], &mqc_states[44]},
{0x1c01, 1, &mqc_states[51], &mqc_states[45]},
{0x1801, 0, &mqc_states[52], &mqc_states[46]},
{0x1801, 1, &mqc_states[53], &mqc_states[47]},
{0x1601, 0, &mqc_states[54], &mqc_states[48]},
{0x1601, 1, &mqc_states[55], &mqc_states[49]},
{0x1401, 0, &mqc_states[56], &mqc_states[50]},
{0x1401, 1, &mqc_states[57], &mqc_states[51]},
{0x1201, 0, &mqc_states[58], &mqc_states[52]},
{0x1201, 1, &mqc_states[59], &mqc_states[53]},
{0x1101, 0, &mqc_states[60], &mqc_states[54]},
{0x1101, 1, &mqc_states[61], &mqc_states[55]},
{0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
{0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
{0x09c1, 0, &mqc_states[64], &mqc_states[58]},
{0x09c1, 1, &mqc_states[65], &mqc_states[59]},
{0x08a1, 0, &mqc_states[66], &mqc_states[60]},
{0x08a1, 1, &mqc_states[67], &mqc_states[61]},
{0x0521, 0, &mqc_states[68], &mqc_states[62]},
{0x0521, 1, &mqc_states[69], &mqc_states[63]},
{0x0441, 0, &mqc_states[70], &mqc_states[64]},
{0x0441, 1, &mqc_states[71], &mqc_states[65]},
{0x02a1, 0, &mqc_states[72], &mqc_states[66]},
{0x02a1, 1, &mqc_states[73], &mqc_states[67]},
{0x0221, 0, &mqc_states[74], &mqc_states[68]},
{0x0221, 1, &mqc_states[75], &mqc_states[69]},
{0x0141, 0, &mqc_states[76], &mqc_states[70]},
{0x0141, 1, &mqc_states[77], &mqc_states[71]},
{0x0111, 0, &mqc_states[78], &mqc_states[72]},
{0x0111, 1, &mqc_states[79], &mqc_states[73]},
{0x0085, 0, &mqc_states[80], &mqc_states[74]},
{0x0085, 1, &mqc_states[81], &mqc_states[75]},
{0x0049, 0, &mqc_states[82], &mqc_states[76]},
{0x0049, 1, &mqc_states[83], &mqc_states[77]},
{0x0025, 0, &mqc_states[84], &mqc_states[78]},
{0x0025, 1, &mqc_states[85], &mqc_states[79]},
{0x0015, 0, &mqc_states[86], &mqc_states[80]},
{0x0015, 1, &mqc_states[87], &mqc_states[81]},
{0x0009, 0, &mqc_states[88], &mqc_states[82]},
{0x0009, 1, &mqc_states[89], &mqc_states[83]},
{0x0005, 0, &mqc_states[90], &mqc_states[84]},
{0x0005, 1, &mqc_states[91], &mqc_states[85]},
{0x0001, 0, &mqc_states[90], &mqc_states[86]},
{0x0001, 1, &mqc_states[91], &mqc_states[87]},
{0x5601, 0, &mqc_states[92], &mqc_states[92]},
{0x5601, 1, &mqc_states[93], &mqc_states[93]},
};
#define MQC_NUMCTXS 32
unsigned int mqc_c;
unsigned int mqc_a;
unsigned int mqc_ct;
unsigned char *mqc_bp;
unsigned char *mqc_start;
unsigned char *mqc_end;
mqc_state_t *mqc_ctxs[MQC_NUMCTXS];
mqc_state_t **mqc_curctx;
/* <summary> */
/* Return the number of bytes already encoded. */
/* </summary> */
int mqc_numbytes()
{
return mqc_bp - mqc_start;
}
/* <summary> */
/* Output a byte, doing bit-stuffing if necessary. */
/* After a 0xff byte, the next byte must be smaller than 0x90 */
/* </summary> */
void mqc_byteout()
{
if (*mqc_bp == 0xff) {
mqc_bp++;
*mqc_bp = mqc_c >> 20;
mqc_c &= 0xfffff;
mqc_ct = 7;
} else {
if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */
mqc_bp++;
*mqc_bp = mqc_c >> 19;
mqc_c &= 0x7ffff;
mqc_ct = 8;
} else {
(*mqc_bp)++;
if (*mqc_bp == 0xff) {
mqc_c &= 0x7ffffff;
mqc_bp++;
*mqc_bp = mqc_c >> 20;
mqc_c &= 0xfffff;
mqc_ct = 7;
} else {
mqc_bp++;
*mqc_bp = mqc_c >> 19;
mqc_c &= 0x7ffff;
mqc_ct = 8;
}
}
}
}
/* <summary> */
/* Renormalize mqc_a and mqc_c while encoding, so that mqc_a stays between 0x8000 and 0x10000 */
/* </summary> */
void mqc_renorme()
{
do {
mqc_a <<= 1;
mqc_c <<= 1;
mqc_ct--;
if (mqc_ct == 0) {
mqc_byteout();
}
} while ((mqc_a & 0x8000) == 0);
}
/* <summary> */
/* Encode the most probable symbol. */
/* </summary> */
void mqc_codemps()
{
mqc_a -= (*mqc_curctx)->qeval;
if ((mqc_a & 0x8000) == 0) {
if (mqc_a < (*mqc_curctx)->qeval) {
mqc_a = (*mqc_curctx)->qeval;
} else {
mqc_c += (*mqc_curctx)->qeval;
}
*mqc_curctx = (*mqc_curctx)->nmps;
mqc_renorme();
} else {
mqc_c += (*mqc_curctx)->qeval;
}
}
/* <summary> */
/* Encode the most least symbol. */
/* </summary> */
void mqc_codelps()
{
mqc_a -= (*mqc_curctx)->qeval;
if (mqc_a < (*mqc_curctx)->qeval) {
mqc_c += (*mqc_curctx)->qeval;
} else {
mqc_a = (*mqc_curctx)->qeval;
}
*mqc_curctx = (*mqc_curctx)->nlps;
mqc_renorme();
}
/* <summary> */
/* Initialize encoder. */
/* </summary> */
/* <param name="bp">Output buffer.</param> */
void mqc_init_enc(unsigned char *bp)
{
mqc_setcurctx(0);
mqc_a = 0x8000;
mqc_c = 0;
mqc_bp = bp - 1;
mqc_ct = 12;
if (*mqc_bp == 0xff) {
mqc_ct = 13;
}
mqc_start = bp;
}
/* <summary> */
/* Set current context. */
/* </summary> */
/* <param name="ctxno">Context number.</param> */
void mqc_setcurctx(int ctxno)
{
mqc_curctx = &mqc_ctxs[ctxno];
}
/* <summary> */
/* Encode a symbol using the MQ-coder. */
/* </summary> */
/* <param name="d"> The symbol to be encoded (0 or 1).</param> */
void mqc_encode(int d)
{
if ((*mqc_curctx)->mps == d) {
mqc_codemps();
} else {
mqc_codelps();
}
}
/* <summary> */
/* Fill mqc_c with 1's for flushing */
/* </summary> */
void mqc_setbits()
{
unsigned int tempc = mqc_c + mqc_a;
mqc_c |= 0xffff;
if (mqc_c >= tempc) {
mqc_c -= 0x8000;
}
}
/* <summary> */
/* Flush encoded data. */
/* </summary> */
void mqc_flush()
{
mqc_setbits();
mqc_c <<= mqc_ct;
mqc_byteout();
mqc_c <<= mqc_ct;
mqc_byteout();
if (*mqc_bp != 0xff) {
mqc_bp++;
}
}
/* <summary> */
/* not fully implemented and tested !! */
/* BYPASS mode switch, initialization operation */
/* JPEG 2000 p 505 */
/* </summary> */
void mqc_bypass_init_enc()
{
mqc_c = 0;
mqc_ct = 8;
/*if (*mqc_bp == 0xff) {
mqc_ct = 7;
} */
}
/* <summary> */
/* not fully implemented and tested !! */
/* BYPASS mode switch, coding operation */
/* JPEG 2000 p 505 */
/* </summary> */
void mqc_bypass_enc(int d)
{
mqc_ct--;
mqc_c = mqc_c + (d << mqc_ct);
if (mqc_ct == 0) {
mqc_bp++;
*mqc_bp = mqc_c;
mqc_ct = 8;
if (*mqc_bp == 0xff) {
mqc_ct = 7;
}
mqc_c = 0;
}
}
/* <summary> */
/* not fully implemented and tested !! */
/* BYPASS mode switch, flush operation */
/* </summary> */
int mqc_bypass_flush_enc()
{
unsigned char bit_padding;
bit_padding = 0;
if (mqc_ct != 0) {
while (mqc_ct > 0) {
mqc_ct--;
mqc_c += bit_padding << mqc_ct;
bit_padding = (bit_padding + 1) & 0x01;
}
mqc_bp++;
*mqc_bp = mqc_c;
mqc_ct = 8;
mqc_c = 0;
}
return 1;
}
/* <summary> */
/* RESET mode switch */
/* </summary> */
void mqc_reset_enc()
{
mqc_resetstates();
mqc_setstate(18, 0, 46);
mqc_setstate(0, 0, 3);
mqc_setstate(1, 0, 4);
}
/* <summary> */
/* mode switch RESTART (TERMALL) */
/* </summary> */
int mqc_restart_enc()
{
int correction = 1;
/* <flush part> */
int n = 27 - 15 - mqc_ct;
mqc_c <<= mqc_ct;
while (n > 0) {
mqc_byteout();
n -= mqc_ct;
mqc_c <<= mqc_ct;
}
mqc_byteout();
return correction;
}
/* <summary> */
/* mode switch RESTART (TERMALL) reinitialisation */
/* </summary> */
void mqc_restart_init_enc()
{
/* <Re-init part> */
mqc_setcurctx(0);
mqc_a = 0x8000;
mqc_c = 0;
mqc_ct = 12;
mqc_bp--;
if (*mqc_bp == 0xff) {
mqc_ct = 13;
}
}
/* <summary> */
/* ERTERM mode switch */
/* </summary> */
void mqc_erterm_enc()
{
int k = 11 - mqc_ct + 1;
while (k > 0) {
mqc_c <<= mqc_ct;
mqc_ct = 0;
mqc_byteout();
k -= mqc_ct;
}
if (*mqc_bp != 0xff) {
mqc_byteout();
}
}
/* <summary> */
/* SEGMARK mode switch (SEGSYM) */
/* </summary> */
void mqc_segmark_enc()
{
int i;
mqc_setcurctx(18);
for (i = 1; i < 5; i++) {
mqc_encode(i % 2);
}
}
/* <summary> */
/* </summary> */
int mqc_mpsexchange()
{
int d;
if (mqc_a < (*mqc_curctx)->qeval) {
d = 1 - (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nlps;
} else {
d = (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nmps;
}
return d;
}
/* <summary> */
/* </summary> */
int mqc_lpsexchange()
{
int d;
if (mqc_a < (*mqc_curctx)->qeval) {
mqc_a = (*mqc_curctx)->qeval;
d = (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nmps;
} else {
mqc_a = (*mqc_curctx)->qeval;
d = 1 - (*mqc_curctx)->mps;
*mqc_curctx = (*mqc_curctx)->nlps;
}
return d;
}
/* <summary> */
/* Input a byte. */
/* </summary> */
void mqc_bytein()
{
if (mqc_bp != mqc_end) {
unsigned int c;
if (mqc_bp + 1 != mqc_end) {
c = *(mqc_bp + 1);
} else {
c = 0xff;
}
if (*mqc_bp == 0xff) {
if (c > 0x8f) {
mqc_c += 0xff00;
mqc_ct = 8;
} else {
mqc_bp++;
mqc_c += c << 9;
mqc_ct = 7;
}
} else {
mqc_bp++;
mqc_c += c << 8;
mqc_ct = 8;
}
} else {
mqc_c += 0xff00;
mqc_ct = 8;
}
}
/* <summary> */
/* Renormalize mqc_a and mqc_c while decoding. */
/* </summary> */
void mqc_renormd()
{
do {
if (mqc_ct == 0) {
mqc_bytein();
}
mqc_a <<= 1;
mqc_c <<= 1;
mqc_ct--;
} while (mqc_a < 0x8000);
}
/* <summary> */
/* Initialize decoder. */
/* </summary> */
void mqc_init_dec(unsigned char *bp, int len)
{
mqc_setcurctx(0);
mqc_start = bp;
mqc_end = bp + len;
mqc_bp = bp;
/*add antonin initbug1*/
if (len==0) mqc_c = 0xff << 16;
else mqc_c = *mqc_bp << 16;
/*dda*/
mqc_bytein();
mqc_c <<= 7;
mqc_ct -= 7;
mqc_a = 0x8000;
}
/* <summary> */
/* Decode a symbol. */
/* </summary> */
int mqc_decode()
{
int d;
mqc_a -= (*mqc_curctx)->qeval;
if ((mqc_c >> 16) < (*mqc_curctx)->qeval) {
d = mqc_lpsexchange();
mqc_renormd();
} else {
mqc_c -= (*mqc_curctx)->qeval << 16;
if ((mqc_a & 0x8000) == 0) {
d = mqc_mpsexchange();
mqc_renormd();
} else {
d = (*mqc_curctx)->mps;
}
}
return d;
}
/* <summary> */
/* Reset states of all contexts. */
/* </summary> */
void mqc_resetstates()
{
int i;
for (i = 0; i < MQC_NUMCTXS; i++) {
mqc_ctxs[i] = mqc_states;
}
}
/* <summary> */
/* Set the state for a context. */
/* </summary> */
/* <param name="ctxno">Context number</param> */
/* <param name="msb">Most significant bit</param> */
/* <param name="prob">Index to the probability of symbols</param> */
void mqc_setstate(int ctxno, int msb, int prob)
{
mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
}

View File

@ -1,127 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __MQC_H
#define __MQC_H
/*
* Return the number of bytes written/read since initialisation
*/
int mqc_numbytes();
/*
* Reset the states of all the context of the coder/decoder
* (each context is set to a state where 0 and 1 are more or less equiprobable)
*/
void mqc_resetstates();
/*
* Set the state of a particular context
* ctxno: number that identifies the context
* msb: the MSB of the new state of the context
* prob: number that identifies the probability of the symbols for the new state of the context
*/
void mqc_setstate(int ctxno, int msb, int prob);
/*
* Initialize the encoder
* bp: pointer to the start of the buffer where the bytes will be written
*/
void mqc_init_enc(unsigned char *bp);
/*
* Set the current context used for coding/decoding
* ctxno: number that identifies the context
*/
void mqc_setcurctx(int ctxno);
/*
* Encode a bit
* d: bit to encode (0 or 1)
*/
void mqc_encode(int d);
/*
* Flush the encoder, so that all remaining data is written
*/
void mqc_flush();
/*
* BYPASS mode switch
*/
void mqc_bypass_init_enc();
/*
* BYPASS mode switch
*/
void mqc_bypass_enc(int d);
/*
* BYPASS mode switch
*/
int mqc_bypass_flush_enc();
/*
* RESET mode switch
*/
void mqc_reset_enc();
/*
* RESTART mode switch (TERMALL)
*/
int mqc_restart_enc();
/*
* RESTART mode switch (TERMALL)
*/
void mqc_restart_init_enc();
/*
* ERTERM mode switch (PTERM)
*/
void mqc_erterm_enc();
/*
* SEGMARK mode switch (SEGSYM)
*/
void mqc_segmark_enc();
/*
* Initialize the decoder
* bp: pointer to the start of the buffer from which the bytes will be read
* len: length of the input buffer
*/
void mqc_init_dec(unsigned char *bp, int len);
/*
* Decode a bit (returns 0 or 1)
*/
int mqc_decode();
#endif

View File

@ -1,49 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __OPENJPEG_H
#define __OPENJPEG_H
#include <j2k.h>
#include <tcd.h>
#include <jp2.h>
#include <bio.h>
#include <cio.h>
#include <dwt.h>
#include <fix.h>
#include <int.h>
#include <mct.h>
#include <mqc.h>
#include <pi.h>
#include <raw.h>
#include <t1.h>
#include <t2.h>
#include <tgt.h>
#endif

View File

@ -1,557 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "pi.h"
#include "int.h"
#include <stdlib.h>
#include <stdio.h>
/* <summary>
* Create a packet iterator.
* </summary> */
pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
{
int p, q, i;
int compno, resno, pino;
int maxres = 0;
pi_iterator_t *pi;
j2k_tcp_t *tcp;
j2k_tccp_t *tccp;
tcp = &cp->tcps[tileno];
pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) *
sizeof(pi_iterator_t));
for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
p = tileno % cp->tw;
q = tileno / cp->tw;
pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
pi[pino].numcomps = img->numcomps;
pi[pino].comps =
(pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t));
for (compno = 0; compno < pi->numcomps; compno++) {
int tcx0, tcy0, tcx1, tcy1;
pi_comp_t *comp = &pi[pino].comps[compno];
tccp = &tcp->tccps[compno];
comp->dx = img->comps[compno].dx;
comp->dy = img->comps[compno].dy;
comp->numresolutions = tccp->numresolutions;
comp->resolutions =
(pi_resolution_t *) malloc(comp->numresolutions *
sizeof(pi_resolution_t));
tcx0 = int_ceildiv(pi->tx0, comp->dx);
tcy0 = int_ceildiv(pi->ty0, comp->dy);
tcx1 = int_ceildiv(pi->tx1, comp->dx);
tcy1 = int_ceildiv(pi->ty1, comp->dy);
if (comp->numresolutions > maxres) {
maxres = comp->numresolutions;
}
for (resno = 0; resno < comp->numresolutions; resno++) {
int levelno;
int rx0, ry0, rx1, ry1;
int px0, py0, px1, py1;
pi_resolution_t *res = &comp->resolutions[resno];
if (tccp->csty & J2K_CCP_CSTY_PRT) {
res->pdx = tccp->prcw[resno];
res->pdy = tccp->prch[resno];
} else {
res->pdx = 15;
res->pdy = 15;
}
levelno = comp->numresolutions - 1 - resno;
rx0 = int_ceildivpow2(tcx0, levelno);
ry0 = int_ceildivpow2(tcy0, levelno);
rx1 = int_ceildivpow2(tcx1, levelno);
ry1 = int_ceildivpow2(tcy1, levelno);
px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); /*Mod Antonin : sizebug1*/
res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); /*Mod Antonin : sizebug1*/
}
}
tccp = &tcp->tccps[0];
pi[pino].step_p = 1;
pi[pino].step_c = 100 * pi[pino].step_p;
pi[pino].step_r = img->numcomps * pi[pino].step_c;
pi[pino].step_l = maxres * pi[pino].step_r;
if (pino == 0) {
pi[pino].include =
(short int *) malloc(img->numcomps * maxres *
tcp->numlayers * 100 * sizeof(short int));
for (i = 0; i < img->numcomps * maxres * tcp->numlayers * 100; i++)
pi[pino].include[i] = 0;
}
/* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int)); */
else
pi[pino].include = pi[pino - 1].include;
if (tcp->POC == 0) {
pi[pino].first = 1;
pi[pino].poc.resno0 = 0;
pi[pino].poc.compno0 = 0;
pi[pino].poc.layno1 = tcp->numlayers;
pi[pino].poc.resno1 = maxres;
pi[pino].poc.compno1 = img->numcomps;
pi[pino].poc.prg = tcp->prg;
} else {
pi[pino].first = 1;
pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
pi[pino].poc.prg = tcp->pocs[pino].prg;
}
}
return pi;
}
/* <summary>
* Get next packet in layer-resolution-component-precinct order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_lrcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
res = &comp->resolutions[pi->resno];
goto skip;
} else {
pi->first = 0;
}
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
pi->resno++) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c + pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
return 0;
}
/* <summary>
* Get next packet in resolution-layer-component-precinct order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_rlcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
res = &comp->resolutions[pi->resno];
goto skip;
} else {
pi->first = 0;
}
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c + pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
return 0;
}
/* <summary>
* Get next packet in resolution-precinct-component-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_rpcl(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
goto skip;
} else {
int compno, resno;
pi->first = 0;
pi->dx = 0;
pi->dy = 0;
for (compno = 0; compno < pi->numcomps; compno++) {
comp = &pi->comps[compno];
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx = comp->dx *
(1 << (res->pdx + comp->numresolutions - 1 - resno));
dy = comp->dy *
(1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
}
}
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
for (pi->y = pi->ty0; pi->y < pi->ty1;
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
int levelno;
int trx0, try0;
int trx1, try1;/* Add antonin pcrl*/
int rpx, rpy;
int prci, prcj;
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
(pi->x % (comp->dx << rpx) == 0
|| (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
continue;
}
if (!
(pi->y % (comp->dy << rpy) == 0
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
/*Add Antonin : sizebug1*/
if ((res->pw==0)||(res->pw==0)) continue;
/*ddA*/
/*Add Antonin : pcrl*/
if ((trx0==trx1)||(try0==try1)) continue;
/*ddA*/
prci =
int_floordivpow2(int_ceildiv
(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv
(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
}
return 0;
}
/* <summary>
* Get next packet in precinct-component-resolution-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_pcrl(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
goto skip;
} else {
int compno, resno;
pi->first = 0;
pi->dx = 0;
pi->dy = 0;
for (compno = 0; compno < pi->numcomps; compno++) {
comp = &pi->comps[compno];
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx = comp->dx *
(1 << (res->pdx + comp->numresolutions - 1 - resno));
dy = comp->dy *
(1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
}
}
for (pi->y = pi->ty0; pi->y < pi->ty1;
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
comp = &pi->comps[pi->compno];
for (pi->resno = pi->poc.resno0;
pi->resno < int_min(pi->poc.resno1,
comp->numresolutions); pi->resno++) {
int levelno;
int trx0, try0;
int trx1, try1;/* Add antonin pcrl*/
int rpx, rpy;
int prci, prcj;
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
(pi->x % (comp->dx << rpx) == 0
|| (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
continue;
}
if (!
(pi->y % (comp->dy << rpy) == 0
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
/*Add Antonin : sizebug1*/
if ((res->pw==0)||(res->pw==0)) continue;
/*ddA*/
/*Add Antonin : pcrl*/
if ((trx0==trx1)||(try0==try1)) continue;
/*ddA*/
prci =
int_floordivpow2(int_ceildiv
(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv
(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
}
return 0;
}
/* <summary>
* Get next packet in component-precinct-resolution-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_cprl(pi_iterator_t * pi)
{
pi_comp_t *comp;
pi_resolution_t *res;
if (!pi->first) {
comp = &pi->comps[pi->compno];
goto skip;
} else {
pi->first = 0;
}
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
int resno;
comp = &pi->comps[pi->compno];
pi->dx = 0;
pi->dy = 0;
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
for (pi->y = pi->ty0; pi->y < pi->ty1;
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->resno = pi->poc.resno0;
pi->resno < int_min(pi->poc.resno1,
comp->numresolutions); pi->resno++) {
int levelno;
int trx0, try0;
int trx1, try1;/* Add antonin pcrl*/
int rpx, rpy;
int prci, prcj;
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
(pi->x % (comp->dx << rpx) == 0
|| (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
continue;
}
if (!
(pi->y % (comp->dy << rpy) == 0
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
/*Add Antonin : sizebug1*/
if ((res->pw==0)||(res->pw==0)) continue;
/*ddA*/
/*Add Antonin : pcrl*/
if ((trx0==trx1)||(try0==try1)) continue;
/*ddA*/
prci =
int_floordivpow2(int_ceildiv
(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv
(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
}
}
}
}
}
return 0;
}
/* <summary>
* Get next packet.
*
* pi: packet iterator to modify
* </summary> */
int pi_next(pi_iterator_t * pi)
{
switch (pi->poc.prg) {
case 0:
return pi_next_lrcp(pi);
case 1:
return pi_next_rlcp(pi);
case 2:
return pi_next_rpcl(pi);
case 3:
return pi_next_pcrl(pi);
case 4:
return pi_next_cprl(pi);
}
return 0;
}

View File

@ -1,72 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __PI_H
#define __PI_H
#include "j2k.h"
#include "tcd.h"
typedef struct {
int pdx, pdy;
int pw, ph;
} pi_resolution_t;
typedef struct {
int dx, dy;
int numresolutions;
pi_resolution_t *resolutions;
} pi_comp_t;
typedef struct {
short int *include; /* precise if the packet has been already used (usefull for progression order change) */
int step_l, step_r, step_c, step_p; /* different steps (layer, resolution, component, precinct) to localize the packet in the include vector */
int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */
int first; /* 0 if the first packet */
j2k_poc_t poc;
int numcomps;
pi_comp_t *comps;
int tx0, ty0, tx1, ty1;
int x, y, dx, dy;
} pi_iterator_t; /* packet iterator */
/*
* Create a packet iterator
* img: raw image for which the packets will be listed
* cp: coding paremeters
* tileno: number that identifies the tile for which to list the packets
* return value: returns a packet iterator that points to the first packet of the tile
*/
pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno);
/*
* Modify the packet iterator to point to the next packet
* pi: packet iterator to modify
* return value: returns 0 if pi pointed to the last packet or else returns 1
*/
int pi_next(pi_iterator_t * pi);
#endif

View File

@ -1,81 +0,0 @@
/*
* Copyright (c) 2002-2003, Antonin Descampe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "raw.h"
unsigned char raw_c; /* temporary buffer where bits are coded or decoded */
unsigned int raw_ct; /* number of bits already read or free to write */
unsigned int raw_lenmax; /* maximum length to decode */
unsigned int raw_len; /* length decoded */
unsigned char *raw_bp; /* pointer to the current position in the buffer */
unsigned char *raw_start; /* pointer to the start of the buffer */
unsigned char *raw_end; /* pointer to the end of the buffer */
/*
* Return the number of bytes already encoded.
*/
int raw_numbytes()
{
return raw_bp - raw_start;
}
/*
* Initialize raw-decoder.
*
* bp : pointer to the start of the buffer from which the bytes will be read
* len : length of the input buffer
*/
void raw_init_dec(unsigned char *bp, int len)
{
raw_start = bp;
raw_lenmax = len;
raw_len = 0;
raw_c = 0;
raw_ct = 0;
}
/*
* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
*/
int raw_decode()
{
int d;
if (raw_ct == 0) {
raw_ct = 8;
if (raw_len == raw_lenmax)
raw_c = 0xff;
else {
if (raw_c == 0xff)
raw_ct = 7;
raw_c = *(raw_start + raw_len);
raw_len++;
}
}
raw_ct--;
d = (raw_c >> raw_ct) & 0x01;
return d;
}

View File

@ -1,48 +0,0 @@
/*
* Copyright (c) 2002-2003, Antonin Descampe
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __RAW_H
#define __RAW_H
/*
* Return the number of bytes written/read since initialisation
*/
int raw_numbytes();
/*
* Initialize the decoder
* bp: pointer to the start of the buffer from which the bytes will be read
* len: length of the input buffer
*/
void raw_init_dec(unsigned char *bp, int len);
/*
* Decode a bit (returns 0 or 1)
*/
int raw_decode();
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,52 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __T1_H
#define __T1_H
#include "tcd.h"
#include "j2k.h"
/*
* Initialize the look-up tables of the Tier-1 coder/decoder
*/
void t1_init_luts();
/*
* Encode the code-blocks of a tile
* tile: the tile to encode
* tcp: tile coding parameters
*/
void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp);
/*
* Decode the code-blocks of a tile
* tile: the tile to encode
* tcp: tile coding parameters
*/
void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp);
#endif

View File

@ -1,707 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2004, Yannick Verschueren
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "t2.h"
#include "tcd.h"
#include "bio.h"
#include "j2k.h"
#include "pi.h"
#include "tgt.h"
#include "int.h"
#include "cio.h"
#include <stdio.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>
#define RESTART 0x04
extern jmp_buf j2k_error;
void t2_putcommacode(int n)
{
while (--n >= 0) {
bio_write(1, 1);
}
bio_write(0, 1);
}
int t2_getcommacode()
{
int n;
for (n = 0; bio_read(1); n++) {
}
return n;
}
/* <summary> */
/* Variable length code for signalling delta Zil (truncation point) */
/* <val> n : delta Zil */
/* <\summary> */
void t2_putnumpasses(int n)
{
if (n == 1) {
bio_write(0, 1);
} else if (n == 2) {
bio_write(2, 2);
} else if (n <= 5) {
bio_write(0xc | (n - 3), 4);
} else if (n <= 36) {
bio_write(0x1e0 | (n - 6), 9);
} else if (n <= 164) {
bio_write(0xff80 | (n - 37), 16);
}
}
int t2_getnumpasses()
{
int n;
if (!bio_read(1))
return 1;
if (!bio_read(1))
return 2;
if ((n = bio_read(2)) != 3)
return 3 + n;
if ((n = bio_read(5)) != 31)
return 6 + n;
return 37 + bio_read(7);
}
/*
* Encode a packet of a tile to a destination buffer
*
* Tile : the tile for which to write the packets
* tcp : the tile coding parameters
* compno : Identity of the packet --> component value
* resno : Identity of the packet --> resolution level value
* precno : Identity of the packet --> precinct value
* layno : Identity of the packet --> quality layer value
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
* tileno : number of the tile encoded
*/
int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
int resno, int precno, int layno, unsigned char *dest,
int len, info_image * info_IM, int tileno)
{
int bandno, cblkno;
unsigned char *sop = 0, *eph = 0;
tcd_tilecomp_t *tilec = &tile->comps[compno];
tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c = dest;
/* <SOP 0xff91> */
if (tcp->csty & J2K_CP_CSTY_SOP) {
sop = (unsigned char *) malloc(6 * sizeof(unsigned char));
sop[0] = 255;
sop[1] = 145;
sop[2] = 0;
sop[3] = 4;
sop[4] = (info_IM->num % 65536) / 256;
sop[5] = (info_IM->num % 65536) % 256;
memcpy(c, sop, 6);
free(sop);
c += 6;
}
/* </SOP> */
if (!layno) {
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
tgt_reset(prc->incltree);
tgt_reset(prc->imsbtree);
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
cblk->numpasses = 0;
tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
}
}
}
bio_init_enc(c, len);
bio_write(1, 1); /* Empty header bit */
/* Writing Packet header */
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
if (!cblk->numpasses && layer->numpasses) {
tgt_setvalue(prc->incltree, cblkno, layno);
}
}
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
int increment = 0;
int nump = 0;
int len = 0, passno;
/* cblk inclusion bits */
if (!cblk->numpasses) {
tgt_encode(prc->incltree, cblkno, layno + 1);
} else {
bio_write(layer->numpasses != 0, 1);
}
/* if cblk not included, go to the next cblk */
if (!layer->numpasses) {
continue;
}
/* if first instance of cblk --> zero bit-planes information */
if (!cblk->numpasses) {
cblk->numlenbits = 3;
tgt_encode(prc->imsbtree, cblkno, 999);
}
/* number of coding passes included */
t2_putnumpasses(layer->numpasses);
/* computation of the increase of the length indicator and insertion in the header */
for (passno = cblk->numpasses;
passno < cblk->numpasses + layer->numpasses; passno++) {
tcd_pass_t *pass = &cblk->passes[passno];
nump++;
len += pass->len;
if (pass->term
|| passno == (cblk->numpasses + layer->numpasses) - 1) {
increment =
int_max(increment,
int_floorlog2(len) + 1 -
(cblk->numlenbits + int_floorlog2(nump)));
len = 0;
nump = 0;
}
}
t2_putcommacode(increment);
/* computation of the new Length indicator */
cblk->numlenbits += increment;
/* insertion of the codeword segment length */
for (passno = cblk->numpasses;
passno < cblk->numpasses + layer->numpasses; passno++) {
tcd_pass_t *pass = &cblk->passes[passno];
nump++;
len += pass->len;
if (pass->term
|| passno == (cblk->numpasses + layer->numpasses) - 1) {
bio_write(len, cblk->numlenbits + int_floorlog2(nump));
len = 0;
nump = 0;
}
}
}
}
if (bio_flush())
return -999; /* modified to eliminate longjmp !! */
c += bio_numbytes();
/* <EPH 0xff92> */
if (tcp->csty & J2K_CP_CSTY_EPH) {
eph = (unsigned char *) malloc(2 * sizeof(unsigned char));
eph[0] = 255;
eph[1] = 146;
memcpy(c, eph, 2);
free(eph);
c += 2;
}
/* </EPH> */
/* Writing the packet body */
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
if (!layer->numpasses) {
continue;
}
if (c + layer->len > dest + len) {
return -999;
}
memcpy(c, layer->data, layer->len);
cblk->numpasses += layer->numpasses;
c += layer->len;
/* ADD for index Cfr. Marcela --> delta disto by packet */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
info_PK->disto += layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
}
}
return c - dest;
}
void t2_init_seg(tcd_seg_t * seg, int cblksty, int first)
{
seg->numpasses = 0;
seg->len = 0;
if (cblksty & J2K_CCP_CBLKSTY_TERMALL)
seg->maxpasses = 1;
else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
if (first)
seg->maxpasses = 10;
else
seg->maxpasses = (((seg - 1)->maxpasses == 1)
|| ((seg - 1)->maxpasses == 10)) ? 2 : 1;
} else
seg->maxpasses = 109;
}
/*
* Decode a packet of a tile from a source buffer
*
* src : the source buffer
* len : the length of the source buffer
* tile : the tile for which to write the packets
* cp : the image coding parameters
* tcp : the tile coding parameters
* compno : Identity of the packet --> component value
* resno : Identity of the packet --> resolution level value
* precno : Identity of the packet --> precinct value
* layno : Identity of the packet --> quality layer value
*/
int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
j2k_cp_t * cp, j2k_tcp_t * tcp, int compno, int resno,
int precno, int layno)
{
int bandno, cblkno;
tcd_tilecomp_t *tilec = &tile->comps[compno];
tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c = src;
unsigned char *hd = NULL;
int present;
if (layno == 0) {
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
/*Add Antonin : sizebug1*/
if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
/*ddA*/
tgt_reset(prc->incltree);
tgt_reset(prc->imsbtree);
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
cblk->numsegs = 0;
}
}
}
/* SOP markers*/
if (tcp->csty & J2K_CP_CSTY_SOP) {
if ((*c) != 0xff || (*(c + 1) != 0x91)) {
fprintf(stderr,"Warning : expected SOP marker\n");
} else {
c += 6;
}
/*TODO : check the Nsop value*/
}
/* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
This part deal with this caracteristic
step 1: Read packet header in the saved structure
step 2: Return to codestream for decoding */
if (cp->ppm == 1) { /* PPM */
hd = cp->ppm_data;
bio_init_dec(hd, cp->ppm_len); /*Mod Antonin : ppmbug1*/
} else if (tcp->ppt == 1) { /* PPT */
hd = tcp->ppt_data;
bio_init_dec(hd, tcp->ppt_len); /*Mod Antonin : ppmbug1*/
} else { /* Normal Case */
hd = c;
bio_init_dec(hd, src+len-hd);
}
present = bio_read(1);
if (!present) {
bio_inalign();
hd += bio_numbytes();
/* EPH markers*/
if (tcp->csty & J2K_CP_CSTY_EPH) {
if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
printf("Error : expected EPH marker\n");
} else {
hd += 2;
}
}
if (cp->ppm == 1) { /* PPM case */
cp->ppm_len+=cp->ppm_data-hd;
cp->ppm_data = hd;
return c - src;
}
if (tcp->ppt == 1) { /* PPT case */
tcp->ppt_len+=tcp->ppt_data-hd;
tcp->ppt_data = hd;
return c - src;
}
return hd - src;
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
/*Add Antonin : sizebug1*/
if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
/*ddA*/
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
int included, increment, n;
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg;
/* if cblk not yet included before --> inclusion tagtree */
if (!cblk->numsegs) {
included = tgt_decode(prc->incltree, cblkno, layno + 1);
/* else one bit */
} else {
included = bio_read(1);
}
/* if cblk not included */
if (!included) {
cblk->numnewpasses = 0;
continue;
}
/* if cblk not yet included --> zero-bitplane tagtree */
if (!cblk->numsegs) {
int i, numimsbs;
for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {
}
numimsbs = i - 1;
cblk->numbps = band->numbps - numimsbs;
cblk->numlenbits = 3;
}
/* number of coding passes */
cblk->numnewpasses = t2_getnumpasses();
increment = t2_getcommacode();
/* length indicator increment */
cblk->numlenbits += increment;
if (!cblk->numsegs) {
seg = &cblk->segs[0];
t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
} else {
seg = &cblk->segs[cblk->numsegs - 1];
if (seg->numpasses == seg->maxpasses) {
t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
}
}
n = cblk->numnewpasses;
do {
seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
seg->newlen =
bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));
n -= seg->numnewpasses;
if (n > 0) {
t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
}
} while (n > 0);
}
}
if (bio_inalign())
return -999;
hd += bio_numbytes();
/* EPH markers*/
if (tcp->csty & J2K_CP_CSTY_EPH) {
if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
fprintf(stderr,"Error : expected EPH marker\n");
} else {
hd += 2;
}
}
if (cp->ppm==1) {
cp->ppm_len+=cp->ppm_data-hd;
cp->ppm_data = hd;
} else if (tcp->ppt == 1) {
tcp->ppt_len+=tcp->ppt_data-hd;
tcp->ppt_data = hd;
} else {
c=hd;
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
/*Add Antonin : sizebug1*/
if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
/*ddA*/
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg;
if (!cblk->numnewpasses)
continue;
if (!cblk->numsegs) {
seg = &cblk->segs[0];
cblk->numsegs++;
cblk->len = 0;
} else {
seg = &cblk->segs[cblk->numsegs - 1];
if (seg->numpasses == seg->maxpasses) {
seg++;
cblk->numsegs++;
}
}
do {
if (c + seg->newlen > src + len) {
return -999;
}
memcpy(cblk->data + cblk->len, c, seg->newlen);
if (seg->numpasses == 0) {
seg->data = cblk->data + cblk->len;
}
c += seg->newlen;
cblk->len += seg->newlen;
seg->len += seg->newlen;
seg->numpasses += seg->numnewpasses;
cblk->numnewpasses -= seg->numnewpasses;
if (cblk->numnewpasses > 0) {
seg++;
cblk->numsegs++;
}
} while (cblk->numnewpasses > 0);
}
}
return c - src;
}
/*
* Encode the packets of a tile to a destination buffer
*
* img : the source image
* cp : the image coding parameters
* tileno : number of the tile encoded
* tile : the tile for which to write the packets
* maxlayers : maximum number of layers
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
*/
int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
tcd_tile_t * tile, int maxlayers,
unsigned char *dest, int len, info_image * info_IM)
{
unsigned char *c = dest;
int e = 0;
pi_iterator_t *pi;
int pino, compno;
pi = pi_create(img, cp, tileno);
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
while (pi_next(&pi[pino])) {
if (pi[pino].layno < maxlayers) {
e = t2_encode_packet(tile, &cp->tcps[tileno],
pi[pino].compno, pi[pino].resno,
pi[pino].precno, pi[pino].layno, c,
dest + len - c, info_IM, tileno);
if (e == -999) {
break;
} else
c += e;
/* INDEX >> */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
if (!info_IM->num) {
info_PK->start_pos = info_TL->end_header + 1;
} else {
info_PK->start_pos =
info_TL->packet[info_IM->num - 1].end_pos + 1;
}
info_PK->end_pos = info_PK->start_pos + e - 1;
}
/* << INDEX */
if ((info_IM->index_write
&& cp->tcps[tileno].csty & J2K_CP_CSTY_SOP)
|| (info_IM->index_write && info_IM->index_on)) {
info_IM->num++;
}
}
}
/* FREE space memory taken by pi */
for (compno = 0; compno < pi[pino].numcomps; compno++) {
free(pi[pino].comps[compno].resolutions);
}
free(pi[pino].comps);
}
free(pi[0].include);
free(pi);
if (e == -999)
return e;
else
return c - dest;
}
/*
* Decode the packets of a tile from a source buffer
*
* src: the source buffer
* len: length of the source buffer
* img: destination image
* cp: image coding parameters
* tileno: number that identifies the tile for which to decode the packets
* tile: tile for which to decode the packets
*/
int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp, int tileno, tcd_tile_t * tile)
{
unsigned char *c = src;
pi_iterator_t *pi;
int pino, compno, e = 0;
int n = 0;
pi = pi_create(img, cp, tileno);
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
while (pi_next(&pi[pino])) {
if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
e = t2_decode_packet(c, src + len - c, tile, cp,
&cp->tcps[tileno], pi[pino].compno,
pi[pino].resno, pi[pino].precno,
pi[pino].layno);
} else {
e = 0;
}
/* progression in resolution */
img->comps[pi[pino].compno].resno_decoded =
e > 0 ? int_max(pi[pino].resno,
img->comps[pi[pino].compno].
resno_decoded) : img->comps[pi[pino].
compno].resno_decoded;
n++;
if (e == -999) { /* ADD */
break;
} else {
c += e;
}
}
/* FREE space memory taken by pi */
for (compno = 0; compno < pi[pino].numcomps; compno++) {
free(pi[pino].comps[compno].resolutions);
}
free(pi[pino].comps);
}
free(pi[0].include);
free(pi);
if (e == -999)
return e;
else
return c - src;
}

View File

@ -1,64 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __T2_H
#define __T2_H
#include "tcd.h"
#include "j2k.h"
/*
* Encode the packets of a tile to a destination buffer
*
* img : the source image
* cp : the image coding parameters
* tileno : number of the tile encoded
* tile : the tile for which to write the packets
* maxlayers : maximum number of layers
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
*/
int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
tcd_tile_t * tile, int maxlayers,
unsigned char *dest, int len, info_image * info_IM);
/*
* Decode the packets of a tile from a source buffer
*
* src: the source buffer
* len: length of the source buffer
* img: destination image
* cp: image coding parameters
* tileno: number that identifies the tile for which to decode the packets
* tile: tile for which to decode the packets
*/
int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp, int tileno, tcd_tile_t * tile);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,183 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __TCD_H
#define __TCD_H
#include "j2k.h"
#include "tgt.h"
typedef struct {
int numpasses;
int len;
unsigned char *data;
int maxpasses;
int numnewpasses;
int newlen;
} tcd_seg_t;
typedef struct {
int rate;
double distortiondec;
int term, len;
} tcd_pass_t;
typedef struct {
int numpasses; /* Number of passes in the layer */
int len; /* len of information */
double disto; /* add for index (Cfr. Marcela) */
unsigned char *data; /* data */
} tcd_layer_t;
typedef struct {
int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
int numbps;
int numlenbits;
int len; /* length */
int numpasses; /* number of pass already done for the code-blocks */
int numnewpasses; /* number of pass added to the code-blocks */
int numsegs; /* number of segments */
tcd_seg_t segs[100]; /* segments informations */
unsigned char data[8192]; /* Data */
int numpassesinlayers; /* number of passes in the layer */
tcd_layer_t layers[100]; /* layer information */
int totalpasses; /* total number of passes */
tcd_pass_t passes[100]; /* information about the passes */
} tcd_cblk_t;
typedef struct {
int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
int cw, ch; /* number of precinct in width and heigth */
tcd_cblk_t *cblks; /* code-blocks informations */
tgt_tree_t *incltree; /* inclusion tree */
tgt_tree_t *imsbtree; /* IMSB tree */
} tcd_precinct_t;
typedef struct {
int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
int bandno;
tcd_precinct_t *precincts; /* precinct information */
int numbps;
float stepsize;
} tcd_band_t;
typedef struct {
int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
int pw, ph;
int numbands; /* number sub-band for the resolution level */
tcd_band_t bands[3]; /* subband information */
} tcd_resolution_t;
typedef struct {
int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
int numresolutions; /* number of resolutions level */
tcd_resolution_t *resolutions; /* resolutions information */
int *data; /* data of the component */
int nbpix; /* add fixed_quality */
} tcd_tilecomp_t;
typedef struct {
int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
int numcomps; /* number of components in tile */
tcd_tilecomp_t *comps; /* Components information */
int nbpix; /* add fixed_quality */
double distotile; /* add fixed_quality */
double distolayer[100]; /* add fixed_quality */
} tcd_tile_t;
typedef struct {
int tw, th; /* number of tiles in width and heigth */
tcd_tile_t *tiles; /* Tiles information */
} tcd_image_t;
/*
* Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Initialize the tile coder (allocate the memory)
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Initialize the tile decoder
* img: raw image
* cp: coding parameters
*/
void tcd_init(j2k_image_t * img, j2k_cp_t * cp);
/*
* Free the memory allocated for encoding
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Encode a tile from the raw image into a buffer, format pnm, pgm or ppm
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
* info_IM: creation of index file
*/
int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
info_image * info_IM);
/*
* Encode a tile from the raw image into a buffer, format pgx
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
* info_IM: creation of index file
*/
int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
info_image * info_IM);
/*
* Decode a tile from a buffer into a raw image
* src: source buffer
* len: length of the source buffer
* tileno: number that identifies the tile that will be decoded
*/
int tcd_decode_tile(unsigned char *src, int len, int tileno);
void tcd_dec_release();
#endif

View File

@ -1,225 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "tgt.h"
#include "bio.h"
#include <stdlib.h>
#include <stdio.h>
/* <summary> */
/* Reset tag-tree. */
/* </summary> */
void tgt_reset(tgt_tree_t * tree)
{
int i;
/* new */
if (!tree || tree == NULL)
return;
for (i = 0; i < tree->numnodes; i++) {
tree->nodes[i].value = 999;
tree->nodes[i].low = 0;
tree->nodes[i].known = 0;
}
}
/* <summary> */
/* Create tag-tree. */
/* </summary> */
tgt_tree_t *tgt_create(int numleafsh, int numleafsv)
{
int nplh[32];
int nplv[32];
tgt_node_t *node;
tgt_node_t *parentnode;
tgt_node_t *parentnode0;
tgt_tree_t *tree;
int i, j, k;
int numlvls;
int n;
tree = (tgt_tree_t *) malloc(sizeof(tgt_tree_t));
tree->numleafsh = numleafsh;
tree->numleafsv = numleafsv;
numlvls = 0;
nplh[0] = numleafsh;
nplv[0] = numleafsv;
tree->numnodes = 0;
do {
n = nplh[numlvls] * nplv[numlvls];
nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
tree->numnodes += n;
++numlvls;
} while (n > 1);
/* ADD */
if (tree->numnodes == 0) {
free(tree);
return NULL;
}
tree->nodes = (tgt_node_t *) malloc(tree->numnodes * sizeof(tgt_node_t));
node = tree->nodes;
parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];
parentnode0 = parentnode;
for (i = 0; i < numlvls - 1; ++i) {
for (j = 0; j < nplv[i]; ++j) {
k = nplh[i];
while (--k >= 0) {
node->parent = parentnode;
++node;
if (--k >= 0) {
node->parent = parentnode;
++node;
}
++parentnode;
}
if ((j & 1) || j == nplv[i] - 1) {
parentnode0 = parentnode;
} else {
parentnode = parentnode0;
parentnode0 += nplh[i];
}
}
}
node->parent = 0;
tgt_reset(tree);
return tree;
}
/* <summary> */
/* Destroy tag-tree. */
/* </summary> */
void tgt_destroy(tgt_tree_t * t)
{
free(t->nodes);
free(t);
}
/* <summary> */
/* Set the value of a leaf of the tag-tree. */
/* </summary> */
void tgt_setvalue(tgt_tree_t * tree, int leafno, int value)
{
tgt_node_t *node;
node = &tree->nodes[leafno];
while (node && node->value > value) {
node->value = value;
node = node->parent;
}
}
/* <summary> */
/* Encode the value of a leaf of the tag-tree. */
/* </summary> */
void tgt_encode(tgt_tree_t * tree, int leafno, int threshold)
{
tgt_node_t *stk[31];
tgt_node_t **stkptr;
tgt_node_t *node;
int low;
stkptr = stk;
node = &tree->nodes[leafno];
while (node->parent) {
*stkptr++ = node;
node = node->parent;
}
low = 0;
for (;;) {
if (low > node->low) {
node->low = low;
} else {
low = node->low;
}
while (low < threshold) {
if (low >= node->value) {
if (!node->known) {
bio_write(1, 1);
node->known = 1;
}
break;
}
bio_write(0, 1);
++low;
}
node->low = low;
if (stkptr == stk)
break;
node = *--stkptr;
}
}
/* <summary> */
/* Decode the value of a leaf of the tag-tree. */
/* </summary> */
int tgt_decode(tgt_tree_t * tree, int leafno, int threshold)
{
tgt_node_t *stk[31];
tgt_node_t **stkptr;
tgt_node_t *node;
int low;
stkptr = stk;
node = &tree->nodes[leafno];
while (node->parent) {
*stkptr++ = node;
node = node->parent;
}
low = 0;
for (;;) {
if (low > node->low) {
node->low = low;
} else {
low = node->low;
}
while (low < threshold && low < node->value) {
if (bio_read(1)) {
node->value = low;
} else {
++low;
}
}
node->low = low;
if (stkptr == stk) {
break;
}
node = *--stkptr;
}
return (node->value < threshold) ? 1 : 0;
}

View File

@ -1,87 +0,0 @@
/*
* Copyright (c) 2001-2002, David Janssens
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __TGT_H
#define __TGT_H
typedef struct tgt_node {
struct tgt_node *parent;
int value;
int low;
int known;
} tgt_node_t;
typedef struct {
int numleafsh;
int numleafsv;
int numnodes;
tgt_node_t *nodes;
} tgt_tree_t;
/*
* Reset a tag-tree (set all leaves to 0)
* tree: tag-tree to reset
*/
void tgt_reset(tgt_tree_t * tree);
/*
* Create a tag-tree
* numleafsh: width of the array of leafs of the tree
* numleafsv: height of the array of leafs of the tree
*/
tgt_tree_t *tgt_create(int numleafsh, int numleafsv);
/*
* Destroy a tag-tree, liberating memory
* tree: tag-tree to destroy
*/
void tgt_destroy(tgt_tree_t * tree);
/*
* Set the value of a leaf of a tag-tree
* tree: tag-tree to modify
* leafno: number that identifies the leaf to modify
* value: new value of the leaf
*/
void tgt_setvalue(tgt_tree_t * tree, int leafno, int value);
/*
* Encode the value of a leaf of the tag-tree up to a given threshold
* leafno: number that identifies the leaf to encode
* threshold: threshold to use when encoding value of the leaf
*/
void tgt_encode(tgt_tree_t * tree, int leafno, int threshold);
/*
* Decode the value of a leaf of the tag-tree up to a given threshold
* leafno: number that identifies the leaf to decode
* threshold: threshold to use when decoding value of the leaf
*/
int tgt_decode(tgt_tree_t * tree, int leafno, int threshold);
#endif

View File

@ -11,40 +11,29 @@ A non-exclusive copy of this code has been contributed to the Open JPEG project.
Except for copyright, inclusion of the code within Open JPEG for distribution and use
can be bound by the Open JPEG open-source license and disclaimer, expressed elsewhere.
*/
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include "mj2.h"
#include <openjpeg.h>
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
#include <windows.h> /* for time functions */
#include "opj_includes.h"
#include "mj2.h"
#include <time.h>
#include "meta_out.h"
#include <string.h>
jmp_buf j2k_error;
extern j2k_tcp_t j2k_default_tcp;
static BOOL notes = TRUE;
static BOOL sampletables = FALSE;
static BOOL raw = TRUE;
static BOOL derived = TRUE;
opj_tcp_t *j2k_default_tcp;
/* Forwards */
int xml_write_overall_header(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe);
int xml_write_moov(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe);
int xml_write_overall_header(FILE *file, FILE *xmlout, opj_mj2_t * movie, unsigned int sampleframe, opj_event_mgr_t *event_mgr);
int xml_write_moov(FILE *file, FILE *xmlout, opj_mj2_t * movie, unsigned int sampleframe, opj_event_mgr_t *event_mgr);
void uint_to_chars(unsigned int value, char* buf);
void xml_write_trak(FILE *file, FILE *xmlout, mj2_tk_t *track, unsigned int tnum, unsigned int sampleframe);
void xml_write_trak(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum, unsigned int sampleframe, opj_event_mgr_t *event_mgr);
void xml_write_tkhd(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum);
void xml_write_udta(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum);
void xml_write_mdia(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum);
@ -56,43 +45,41 @@ void xml_time_out(FILE* xmlout, time_t t);
void int16_to_3packedchars(short int value, char* buf);
void xml_write_moov_udta(FILE* xmlout, mj2_movie_t * movie);
void xml_write_free_and_skip(FILE* xmlout, mj2_movie_t * movie);
void xml_write_uuid(FILE* xmlout, mj2_movie_t * movie);
void xml_write_moov_udta(FILE* xmlout, opj_mj2_t * movie);
void xml_write_free_and_skip(FILE* xmlout, opj_mj2_t * movie);
void xml_write_uuid(FILE* xmlout, opj_mj2_t * movie);
int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int snum);
int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int snum, opj_event_mgr_t *event_mgr);
void xml_out_frame_siz(FILE* xmlout, j2k_image_t *img, j2k_cp_t *cp);
void xml_out_frame_cod(FILE* xmlout, j2k_tcp_t *tcp);
void xml_out_frame_coc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps); /* j2k_image_t *img); */
BOOL same_component_style(j2k_tccp_t *tccp1, j2k_tccp_t *tccp2);
void xml_out_frame_qcd(FILE* xmlout, j2k_tcp_t *tcp);
void xml_out_frame_qcc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps); /* j2k_image_t *img); */
BOOL same_component_quantization(j2k_tccp_t *tccp1, j2k_tccp_t *tccp2);
void xml_out_frame_rgn(FILE* xmlout, j2k_tcp_t *tcp, int numcomps);/* j2k_image_t *img);*/
void xml_out_frame_poc(FILE* xmlout, j2k_tcp_t *tcp);
void xml_out_frame_ppm(FILE* xmlout, j2k_cp_t *cp);
void xml_out_frame_ppt(FILE* xmlout, j2k_tcp_t *tcp);
void xml_out_frame_siz(FILE* xmlout, opj_image_t *img, opj_cp_t *cp);
void xml_out_frame_cod(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_coc(FILE* xmlout, opj_tcp_t *tcp, int numcomps); /* opj_image_t *img); */
BOOL same_component_style(opj_tccp_t *tccp1, opj_tccp_t *tccp2);
void xml_out_frame_qcd(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_qcc(FILE* xmlout, opj_tcp_t *tcp, int numcomps); /* opj_image_t *img); */
BOOL same_component_quantization(opj_tccp_t *tccp1, opj_tccp_t *tccp2);
void xml_out_frame_rgn(FILE* xmlout, opj_tcp_t *tcp, int numcomps);/* opj_image_t *img);*/
void xml_out_frame_poc(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_ppm(FILE* xmlout, opj_cp_t *cp);
void xml_out_frame_ppt(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_tlm(FILE* xmlout); /* j2k_default_tcp is passed globally */ /* NO-OP. TLM NOT SAVED IN DATA STRUCTURE */
void xml_out_frame_plm(FILE* xmlout); /* j2k_default_tcp is passed globally */ /* NO-OP. PLM NOT SAVED IN DATA STRUCTURE. opt in main; can be used in conjunction with PLT */
void xml_out_frame_plt(FILE* xmlout, j2k_tcp_t *tcp); /* NO-OP. PLM NOT SAVED IN DATA STRUCTURE. opt in main; can be used in conjunction with PLT */
void xml_out_frame_plt(FILE* xmlout, opj_tcp_t *tcp); /* NO-OP. PLM NOT SAVED IN DATA STRUCTURE. opt in main; can be used in conjunction with PLT */
void xml_out_frame_crg(FILE* xmlout); /* j2k_default_tcp is passed globally */ /* opt in main; */
void xml_out_frame_com(FILE* xmlout, j2k_tcp_t *tcp); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main; */
void xml_out_frame_com(FILE* xmlout, opj_tcp_t *tcp); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main; */
void xml_out_dump_hex(FILE* xmlout, char *data, int data_len, char* s);
void xml_out_dump_hex_and_ascii(FILE* xmlout, char *data, int data_len, char* s);
void xml_out_frame_jp2h(FILE* xmlout, jp2_struct_t *jp2_struct);
void xml_out_frame_jp2h(FILE* xmlout, opj_jp2_t *jp2_struct);
#ifdef NOTYET
/* Shown with cp, extended, as data structure... but it could be a new different one */
void xml_out_frame_jp2i(FILE* xmlout, j2k_cp_t *cp);/* IntellectualProperty 'jp2i' (no restrictions on location) */
void xml_out_frame_xml(FILE* xmlout, j2k_cp_t *cp); /* XML 'xml\040' (0x786d6c20). Can appear multiply */
void xml_out_frame_uuid(FILE* xmlout, j2k_cp_t *cp); /* UUID 'uuid' (top level only) */
void xml_out_frame_uinf(FILE* xmlout, j2k_cp_t *cp); /* UUIDInfo 'uinf', includes UUIDList 'ulst' and URL 'url\40' */
void xml_out_frame_unknown_type(FILE* xmlout, j2k_cp_t *cp);
void xml_out_frame_jp2i(FILE* xmlout, opj_cp_t *cp);/* IntellectualProperty 'jp2i' (no restrictions on location) */
void xml_out_frame_xml(FILE* xmlout, opj_cp_t *cp); /* XML 'xml\040' (0x786d6c20). Can appear multiply */
void xml_out_frame_uuid(FILE* xmlout, opj_cp_t *cp); /* UUID 'uuid' (top level only) */
void xml_out_frame_uinf(FILE* xmlout, opj_cp_t *cp); /* UUIDInfo 'uinf', includes UUIDList 'ulst' and URL 'url\40' */
void xml_out_frame_unknown_type(FILE* xmlout, opj_cp_t *cp);
#endif
/* ------------------------------------------------------------------------------------------- */
void xml_write_init(BOOL n, BOOL t, BOOL r, BOOL d)
{
/* Init file globals */
@ -102,7 +89,7 @@ void xml_write_init(BOOL n, BOOL t, BOOL r, BOOL d)
derived = d;
}
int xml_write_struct(FILE* file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe, char* stringDTD) {
int xml_write_struct(FILE* file, FILE *xmlout, opj_mj2_t * movie, unsigned int sampleframe, char* stringDTD, opj_event_mgr_t *event_mgr) {
if(stringDTD != NULL)
{
@ -116,14 +103,14 @@ int xml_write_struct(FILE* file, FILE *xmlout, mj2_movie_t * movie, unsigned int
fprintf(xmlout,"<?xml version=\"1.0\" standalone=\"yes\"?>\n");
fprintf(xmlout, "<MJ2_File>\n");
xml_write_overall_header(file, xmlout, movie, sampleframe);
xml_write_overall_header(file, xmlout, movie, sampleframe, event_mgr);
fprintf(xmlout, "</MJ2_File>");
return 0;
}
/* ------------- */
int xml_write_overall_header(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe)
int xml_write_overall_header(FILE *file, FILE *xmlout, opj_mj2_t * movie, unsigned int sampleframe, opj_event_mgr_t *event_mgr)
{
int i;
char buf[5];
@ -143,7 +130,7 @@ int xml_write_overall_header(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsi
}
fprintf(xmlout, " </CompatibilityList>\n");
fprintf(xmlout, " </FileType>\n");
xml_write_moov(file, xmlout, movie, sampleframe);
xml_write_moov(file, xmlout, movie, sampleframe, event_mgr);
// To come? <mdat> // This is the container for media data that can also be accessed through track structures,
// so is redundant, and simply not of interest as metadata
// <moof> // Allows incremental build up of movie. Probably not in Simple Profile
@ -154,7 +141,7 @@ int xml_write_overall_header(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsi
/* ------------- */
int xml_write_moov(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe)
int xml_write_moov(FILE *file, FILE *xmlout, opj_mj2_t * movie, unsigned int sampleframe, opj_event_mgr_t *event_mgr)
{
unsigned int tnum;
mj2_tk_t *track;
@ -188,10 +175,6 @@ int xml_write_moov(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int s
if(notes)
fprintf(xmlout, " <!-- Timescale defines time units in one second -->\n");
fprintf(xmlout, " <Rate>\n"); /* Rate to play presentation (default = 0x00010000) */
#define CURRENTSTRUCT
#ifdef CURRENTSTRUCT
movie->rate = movie->rate << 16;
#endif
if(notes) {
fprintf(xmlout, " <!-- Rate to play presentation is stored as fixed-point binary 16.16 value. Decimal value is approximation. -->\n");
fprintf(xmlout, " <!-- Rate is expressed relative to normal (default) value of 0x00010000 (1.0) -->\n");
@ -200,11 +183,6 @@ int xml_write_moov(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int s
fprintf(xmlout, " <AsHex>0x%08x</AsHex>\n", movie->rate);
if(derived)
fprintf(xmlout, " <AsDecimal>%12.6f</AsDecimal>\n", (double)movie->rate/(double)0x00010000);
#ifdef CURRENTSTRUCT
if(notes)
fprintf(xmlout, " <!-- Current m2j_to_metadata implementation always shows bits to right of decimal as zeroed. -->\n");
movie->rate = movie->rate >> 16;
#endif
fprintf(xmlout, " </Rate>\n");
fprintf(xmlout, " <Duration>\n");
if(raw)
@ -272,7 +250,7 @@ int xml_write_moov(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int s
track = &(movie->tk[tnum]);
// For now, output info on first video track
xml_write_trak(file, xmlout, track, tnum, sampleframe);
xml_write_trak(file, xmlout, track, tnum, sampleframe, event_mgr);
// to come: <MovieExtends mvek> // possibly not in Simple Profile
xml_write_moov_udta(xmlout, movie); /* NO OP so far */ /* <UserDataBox udta> contains <CopyrightBox cprt> */
@ -341,7 +319,7 @@ void xml_time_out(FILE* xmlout, time_t t)
/* ------------- */
void xml_write_moov_udta(FILE* xmlout, mj2_movie_t * movie) {
void xml_write_moov_udta(FILE* xmlout, opj_mj2_t * movie) {
/* Compare with xml_write_udta */
#ifdef NOTYET
/* NO-OP so far. Optional UserData 'udta' (zero or one in moov or each trak)
@ -368,7 +346,7 @@ void xml_write_moov_udta(FILE* xmlout, mj2_movie_t * movie) {
#endif
}
void xml_write_free_and_skip(FILE* xmlout, mj2_movie_t * movie) {
void xml_write_free_and_skip(FILE* xmlout, opj_mj2_t * movie) {
#ifdef NOTYET
/* NO-OP so far. There can be zero or more instances of free and/or skip
at the top level of the file. This may be a place where the user squirrel's metadata.
@ -396,7 +374,7 @@ void xml_write_free_and_skip(FILE* xmlout, mj2_movie_t * movie) {
#endif
}
void xml_write_uuid(FILE* xmlout, mj2_movie_t * movie) {
void xml_write_uuid(FILE* xmlout, opj_mj2_t * movie) {
/* Univeral Unique IDs of 16 bytes. */
#ifdef NOTYET
/* NO-OP so far. There can be zero or more instances of private uuid boxes in a file.
@ -420,7 +398,7 @@ void xml_write_uuid(FILE* xmlout, mj2_movie_t * movie) {
/* ------------- */
void xml_write_trak(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum, unsigned int sampleframe)
void xml_write_trak(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum, unsigned int sampleframe, opj_event_mgr_t *event_mgr)
{
fprintf(xmlout, " <Track BoxType=\"trak\" Instance=\"%d\">\n", tnum);
xml_write_tkhd(file, xmlout, track, tnum);
@ -442,7 +420,7 @@ void xml_write_trak(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum
// Someday maybe do a smart range scan... for (snum=0; snum < track->num_samples; snum++){
// fprintf(stdout,"Frame %d: ",snum+1);
sample = &track->sample[snum];
if(xml_out_frame(file, xmlout, sample, snum))
if(xml_out_frame(file, xmlout, sample, snum, event_mgr))
return; /* Not great error handling here */
}
}
@ -932,14 +910,29 @@ void xml_write_stbl(FILE* file, FILE* xmlout, mj2_tk_t *track, unsigned int tnum
/* ------------- */
int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int snum)
int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int snum, opj_event_mgr_t *event_mgr)
{
j2k_image_t img;
j2k_cp_t cp;
opj_dparameters_t parameters; /* decompression parameters */
opj_image_t *img;
opj_cp_t *cp;
int i;
int numcomps;
unsigned char* frame_codestream;
/* char xmloutname[50]; */
opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
opj_cio_t *cio = NULL;
opj_j2k_t *j2k;
/* JPEG 2000 compressed image data */
/* get a decoder handle */
dinfo = opj_create_decompress(CODEC_J2K);
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)dinfo, event_mgr, stderr);
/* setup the decoder decoding parameters using the current image and user parameters */
parameters.cp_limit_decoding = DECODE_ALL_BUT_PACKETS;
opj_setup_decoder(dinfo, &parameters);
frame_codestream = (unsigned char*) malloc (sample->sample_size-8); /* Skipping JP2C marker */
if(frame_codestream == NULL)
@ -947,18 +940,24 @@ int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int s
fseek(file,sample->offset+8,SEEK_SET);
fread(frame_codestream,sample->sample_size-8,1, file); /* Assuming that jp and ftyp markers size do */
/* open a byte stream */
cio = opj_cio_open((opj_common_ptr)dinfo, frame_codestream, sample->sample_size-8);
/* Decode J2K to image: */
if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) {
free(frame_codestream);
#ifndef NO_PACKETS_DECODING
for (i=0; i<img.numcomps; i++)
free(img.comps[i].data);
#endif
j2k_dec_release();
img = opj_decode(dinfo, cio);
if (!img) {
fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
opj_destroy_decompress(dinfo);
opj_cio_close(cio);
return 1;
}
numcomps = img.numcomps;
j2k = (opj_j2k_t*)dinfo->j2k_handle;
j2k_default_tcp = j2k->default_tcp;
cp = j2k->cp;
numcomps = img->numcomps;
/* Alignments: " < To help maintain xml pretty-printing */
fprintf(xmlout, " <JP2_Frame Num=\"%d\">\n", snum+1);
fprintf(xmlout, " <MainHeader>\n");
@ -966,46 +965,46 @@ int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int s
/* TO DO: A frame can be represented by two I-guess-contigious codestreams if its interleaved. */
fprintf(xmlout, " <StartOfCodestream Marker=\"SOC\" />\n");
/* "cp" stands for "coding parameter"; "tcp" is tile coding parameters, "tccp" is tile-component coding parameters */
xml_out_frame_siz(xmlout, &img, &cp); /* reqd in main */
xml_out_frame_cod(xmlout, &j2k_default_tcp); /* reqd in main */
xml_out_frame_coc(xmlout, &j2k_default_tcp, numcomps); /* opt in main, at most 1 per component */
xml_out_frame_qcd(xmlout, &j2k_default_tcp); /* reqd in main */
xml_out_frame_qcc(xmlout, &j2k_default_tcp, numcomps); /* opt in main, at most 1 per component */
xml_out_frame_rgn(xmlout, &j2k_default_tcp, numcomps); /* opt, at most 1 per component */
xml_out_frame_poc(xmlout, &j2k_default_tcp); /* opt (but reqd in main or tile for any progression order changes) */
xml_out_frame_siz(xmlout, img, cp); /* reqd in main */
xml_out_frame_cod(xmlout, j2k_default_tcp); /* reqd in main */
xml_out_frame_coc(xmlout, j2k_default_tcp, numcomps); /* opt in main, at most 1 per component */
xml_out_frame_qcd(xmlout, j2k_default_tcp); /* reqd in main */
xml_out_frame_qcc(xmlout, j2k_default_tcp, numcomps); /* opt in main, at most 1 per component */
xml_out_frame_rgn(xmlout, j2k_default_tcp, numcomps); /* opt, at most 1 per component */
xml_out_frame_poc(xmlout, j2k_default_tcp); /* opt (but reqd in main or tile for any progression order changes) */
/* Next four get j2k_default_tcp passed globally: */
#ifdef SUPPRESS_FOR_NOW
xml_out_frame_ppm(xmlout, &cp); /* opt (but either PPM or PPT [distributed in tile headers] or codestream packet header reqd) */
xml_out_frame_ppm(xmlout, cp); /* opt (but either PPM or PPT [distributed in tile headers] or codestream packet header reqd) */
#endif
xml_out_frame_tlm(xmlout); /* NO-OP. TLM NOT SAVED IN DATA STRUCTURE */ /* opt */
xml_out_frame_plm(xmlout); /* NO-OP. PLM NOT SAVED IN DATA STRUCTURE */ /* opt in main; can be used in conjunction with PLT */
xml_out_frame_crg(xmlout); /* NO-OP. CRG NOT SAVED IN DATA STRUCTURE */ /* opt in main; */
xml_out_frame_com(xmlout, &j2k_default_tcp); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main; */
xml_out_frame_com(xmlout, j2k_default_tcp); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main; */
fprintf(xmlout, " </MainHeader>\n");
/* TO DO: all the tile headers (sigh) */
fprintf(xmlout, " <TilePartHeaders Count=\"%d\">\n", cp.tileno_size); /* size of the vector tileno */
for(i = 0; i < cp.tileno_size; i++) { /* I think cp.tileno_size will be same number as (cp.tw * cp.th) or as global j2k_curtileno */
fprintf(xmlout, " <TilePartHeaders Count=\"%d\">\n", cp->tileno_size); /* size of the vector tileno */
for(i = 0; i < cp->tileno_size; i++) { /* I think cp->tileno_size will be same number as (cp->tw * cp->th) or as global j2k_curtileno */
// Standard seems to use zero-based # for tile-part.
fprintf(xmlout, " <TilePartHeader Num=\"%d\" ID=\"%d\">\n", i, cp.tileno[i]); /* ID number of the tiles present in the codestream */
fprintf(xmlout, " <TilePartHeader Num=\"%d\" ID=\"%d\">\n", i, cp->tileno[i]); /* ID number of the tiles present in the codestream */
fprintf(xmlout, " <StartOfTilePart Marker=\"SOT\" />\n");
/* All markers in tile-part headers (between SOT and SOD) are optional, unless structure requires. */
if(i == 0) {
xml_out_frame_cod(xmlout, &(cp.tcps[i])); /* No more than 1 per tile */
xml_out_frame_coc(xmlout, &(cp.tcps[i]), numcomps); /* No more than 1 per component */
xml_out_frame_qcd(xmlout, &(cp.tcps[i])); /* No more than 1 per tile */
xml_out_frame_qcc(xmlout, &(cp.tcps[i]), numcomps); /* No more than 1 per component */
xml_out_frame_rgn(xmlout, &(cp.tcps[i]), numcomps); /* No more than 1 per component */
xml_out_frame_cod(xmlout, &(cp->tcps[i])); /* No more than 1 per tile */
xml_out_frame_coc(xmlout, &(cp->tcps[i]), numcomps); /* No more than 1 per component */
xml_out_frame_qcd(xmlout, &(cp->tcps[i])); /* No more than 1 per tile */
xml_out_frame_qcc(xmlout, &(cp->tcps[i]), numcomps); /* No more than 1 per component */
xml_out_frame_rgn(xmlout, &(cp->tcps[i]), numcomps); /* No more than 1 per component */
}
xml_out_frame_poc(xmlout, &(cp.tcps[i])); /* Reqd only if any progression order changes different from main POC */
xml_out_frame_poc(xmlout, &(cp->tcps[i])); /* Reqd only if any progression order changes different from main POC */
#ifdef SUPPRESS_FOR_NOW
xml_out_frame_ppt(xmlout, &(cp.tcps[i])); /* Either PPT [distributed in tile headers] or PPM or codestream packet header reqd. */
xml_out_frame_ppt(xmlout, &(cp->tcps[i])); /* Either PPT [distributed in tile headers] or PPM or codestream packet header reqd. */
#endif
xml_out_frame_plt(xmlout, &(cp.tcps[i])); /* NO-OP. PLT NOT SAVED IN DATA STRUCTURE */ /* Can be used in conjunction with main's PLM */
xml_out_frame_com(xmlout, &(cp.tcps[i])); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */
/* j2k_tcp_t * cp.tcps; "tile coding parameters" */
/* Maybe not: fprintf(xmlout, " <>%d</>, cp.matrice[i]; */ /* Fixed layer */
xml_out_frame_plt(xmlout, &(cp->tcps[i])); /* NO-OP. PLT NOT SAVED IN DATA STRUCTURE */ /* Can be used in conjunction with main's PLM */
xml_out_frame_com(xmlout, &(cp->tcps[i])); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */
/* opj_tcp_t * cp->tcps; "tile coding parameters" */
/* Maybe not: fprintf(xmlout, " <>%d</>, cp->matrice[i]; */ /* Fixed layer */
fprintf(xmlout, " <StartOfData Marker=\"SOD\" />\n");
if(notes)
fprintf(xmlout, " <!-- Tile-part bitstream, not shown, follows tile-part header and SOD marker. -->\n");
@ -1028,14 +1027,14 @@ int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int s
/* Extra commentary: */
if(notes) {
fprintf(xmlout, " <!-- Given the number and size of components, mj2_to_frame would try to convert this -->\n");
if (((img.numcomps == 3) && (img.comps[0].dx == img.comps[1].dx / 2)
&& (img.comps[0].dx == img.comps[2].dx / 2 ) && (img.comps[0].dx == 1))
|| (img.numcomps == 1)) {
if (((img->numcomps == 3) && (img->comps[0].dx == img->comps[1].dx / 2)
&& (img->comps[0].dx == img->comps[2].dx / 2 ) && (img->comps[0].dx == 1))
|| (img->numcomps == 1)) {
fprintf(xmlout, " <!-- file to a YUV movie in the normal manner. -->\n");
}
else if ((img.numcomps == 3) &&
(img.comps[0].dx == 1) && (img.comps[1].dx == 1)&&
(img.comps[2].dx == 1)) {// If YUV 4:4:4 input --> to bmp
else if ((img->numcomps == 3) &&
(img->comps[0].dx == 1) && (img->comps[1].dx == 1)&&
(img->comps[2].dx == 1)) {// If YUV 4:4:4 input --> to bmp
fprintf(xmlout, " <!-- YUV 4:4:4 file to a series of .bmp files. -->\n");
}
else {
@ -1043,11 +1042,8 @@ int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int s
}
}
#ifndef NO_PACKETS_DECODING
for (i=0; i<img.numcomps; i++)
free(img.comps[i].data);
#endif
j2k_dec_release();
opj_destroy_decompress(dinfo);
opj_cio_close(cio);
free(frame_codestream);
return 0;
@ -1070,9 +1066,9 @@ void int16_to_3packedchars(short int value, char* buf)
/* ------------- */
void xml_out_frame_siz(FILE* xmlout, j2k_image_t *img, j2k_cp_t *cp)
void xml_out_frame_siz(FILE* xmlout, opj_image_t *img, opj_cp_t *cp)
{
j2k_comp_t *comp;
opj_image_comp_t *comp;
int i;
fprintf(xmlout, " <ImageAndFileSize Marker=\"SIZ\">\n");
@ -1137,17 +1133,17 @@ void xml_out_frame_siz(FILE* xmlout, j2k_image_t *img, j2k_cp_t *cp)
/* ------------- */
void xml_out_frame_cod(FILE* xmlout, j2k_tcp_t *tcp)
void xml_out_frame_cod(FILE* xmlout, opj_tcp_t *tcp)
{
/* Could be called with tcp = &j2k_default_tcp;
/* Or, for tile-part header, with &j2k_cp->tcps[j2k_curtileno]
/* Alignment for main:" < < < < To help maintain xml pretty-printing */
/* Alignment for tile:" < < < To help maintain xml pretty-printing */
j2k_tccp_t *tccp;
opj_tccp_t *tccp;
int i;
char spaces[13] = " "; /* 12 spaces if tilepart*/
char* s = spaces;
if(tcp == &j2k_default_tcp) {
if(tcp == j2k_default_tcp) {
s++;s++; /* shorten s to 10 spaces if main */
}
tccp = &(tcp->tccps[0]);
@ -1221,14 +1217,14 @@ void xml_out_frame_cod(FILE* xmlout, j2k_tcp_t *tcp)
/* ------------- */
void xml_out_frame_coc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps) /* Optional in main & tile-part headers */
void xml_out_frame_coc(FILE* xmlout, opj_tcp_t *tcp, int numcomps) /* Optional in main & tile-part headers */
{
/* Uses global j2k_default_tcp */
j2k_tccp_t *tccp, *firstcomp_tccp;
opj_tccp_t *tccp, *firstcomp_tccp;
int i, compno;
char spaces[13] = " "; /* 12 spaces if tilepart*/
char* s = spaces;
if(tcp == &j2k_default_tcp) {
if(tcp == j2k_default_tcp) {
s++;s++; /* shorten s to 10 spaces if main */
}
@ -1240,7 +1236,7 @@ void xml_out_frame_coc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps) /* Optional i
/* Let's pretend that [0] is the default and all others are not */
if(notes) {
fprintf(xmlout, "%s<!-- mj2_to_metadata implementation always reports component[0] as using default COD, -->\n", s);
if(tcp == &j2k_default_tcp)
if(tcp == j2k_default_tcp)
fprintf(xmlout, "%s<!-- and any other component, with main-header style values different from [0], as COC. -->\n", s);
else
fprintf(xmlout, "%s<!-- and any other component, with tile-part-header style values different from [0], as COC. -->\n", s);
@ -1314,7 +1310,7 @@ void xml_out_frame_coc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps) /* Optional i
/* ------------- */
BOOL same_component_style(j2k_tccp_t *tccp1, j2k_tccp_t *tccp2)
BOOL same_component_style(opj_tccp_t *tccp1, opj_tccp_t *tccp2)
{
int i;
@ -1340,14 +1336,14 @@ BOOL same_component_style(j2k_tccp_t *tccp1, j2k_tccp_t *tccp2)
/* ------------- */
void xml_out_frame_qcd(FILE* xmlout, j2k_tcp_t *tcp)
void xml_out_frame_qcd(FILE* xmlout, opj_tcp_t *tcp)
{
/* This code will compile only if declaration of j2k_default_tcp is changed from static (to implicit extern) in j2k.c */
j2k_tccp_t *tccp;
opj_tccp_t *tccp;
int bandno, numbands;
char spaces[13] = " "; /* 12 spaces if tilepart*/
char* s = spaces;
if(tcp == &j2k_default_tcp) {
if(tcp == j2k_default_tcp) {
s++;s++; /* shorten s to 10 spaces if main */
}
@ -1471,16 +1467,16 @@ void xml_out_frame_qcd(FILE* xmlout, j2k_tcp_t *tcp)
/* ------------- */
void xml_out_frame_qcc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps)
void xml_out_frame_qcc(FILE* xmlout, opj_tcp_t *tcp, int numcomps)
{
/* Uses global j2k_default_tcp */
/* This code will compile only if declaration of j2k_default_tcp is changed from static (to implicit extern) in j2k.c */
j2k_tccp_t *tccp, *firstcomp_tccp;
opj_tccp_t *tccp, *firstcomp_tccp;
int bandno, numbands;
int compno;
char spaces[13] = " "; /* 12 spaces if tilepart*/
char* s = spaces;
if(tcp == &j2k_default_tcp) {
if(tcp == j2k_default_tcp) {
s++;s++; /* shorten s to 10 spaces if main */
}
@ -1492,7 +1488,7 @@ void xml_out_frame_qcc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps)
/* Let's pretend that [0] is the default and all others are not */
if(notes) {
fprintf(xmlout, "%s<!-- mj2_to_metadata implementation always reports component[0] as using default QCD, -->\n", s);
if(tcp == &j2k_default_tcp)
if(tcp == j2k_default_tcp)
fprintf(xmlout, "%s<!-- and any other component, with main-header quantization values different from [0], as QCC. -->\n", s);
else
fprintf(xmlout, "%s<!-- and any other component, with tile-part-header quantization values different from [0], as QCC. -->\n", s);
@ -1505,7 +1501,7 @@ void xml_out_frame_qcc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps)
/* Compare j2k_read_qcx */
fprintf(xmlout, "%s<QuantizationComponent Marker=\"QCC\" Component=\"%d\">\n", s, compno); /* Required in main header, single occurrence */
tccp = &j2k_default_tcp.tccps[0];
tccp = &j2k_default_tcp->tccps[0];
/* Not retained or perhaps of interest: Lqcd It maybe can be calculated. */
fprintf(xmlout, "%s <Sqcc>\n", s); /* 1 byte */
if(notes)
@ -1617,7 +1613,7 @@ void xml_out_frame_qcc(FILE* xmlout, j2k_tcp_t *tcp, int numcomps)
/* ------------- */
BOOL same_component_quantization(j2k_tccp_t *tccp1, j2k_tccp_t *tccp2)
BOOL same_component_quantization(opj_tccp_t *tccp1, opj_tccp_t *tccp2)
{
int bandno, numbands;
@ -1658,13 +1654,13 @@ BOOL same_component_quantization(j2k_tccp_t *tccp1, j2k_tccp_t *tccp2)
/* ------------- */
void xml_out_frame_rgn(FILE* xmlout, j2k_tcp_t *tcp, int numcomps)
void xml_out_frame_rgn(FILE* xmlout, opj_tcp_t *tcp, int numcomps)
{
int compno, SPrgn;
/* MJ2 files can have regions of interest if hybridized with JPX Part II */
char spaces[13] = " "; /* 12 spaces if tilepart*/
char* s = spaces;
if(tcp == &j2k_default_tcp) {
if(tcp == j2k_default_tcp) {
s++;s++; /* shorten s to 10 spaces if main */
}
@ -1690,13 +1686,13 @@ void xml_out_frame_rgn(FILE* xmlout, j2k_tcp_t *tcp, int numcomps)
/* ------------- */
void xml_out_frame_poc(FILE* xmlout, j2k_tcp_t *tcp) { /* Progression Order Change */
void xml_out_frame_poc(FILE* xmlout, opj_tcp_t *tcp) { /* Progression Order Change */
/* Compare j2k_read_poc() */
int i;
j2k_poc_t *poc;
opj_poc_t *poc;
char spaces[13] = " "; /* 12 spaces if tilepart*/
char* s = spaces;
if(tcp == &j2k_default_tcp) {
if(tcp == j2k_default_tcp) {
s++;s++; /* shorten s to 10 spaces if main */
}
@ -1743,7 +1739,7 @@ void xml_out_frame_poc(FILE* xmlout, j2k_tcp_t *tcp) { /* Progression Order Chan
/* Suppress PPM and PPT since we're not showing data from the third option, namely within the codestream, and
that's evidently what frames_to_mj2 uses. And a hex dump isn't so useful anyway */
void xml_out_frame_ppm(FILE *xmlout, j2k_cp_t *cp) { /* For main header, not tile-part (which uses PPT instead). */
void xml_out_frame_ppm(FILE *xmlout, opj_cp_t *cp) { /* For main header, not tile-part (which uses PPT instead). */
/* Either the PPM or PPT is required if the packet headers are not distributed in the bit stream */
/* Use of PPM and PPT are mutually exclusive. */
/* Compare j2k_read_ppm() */
@ -1774,7 +1770,7 @@ void xml_out_frame_ppm(FILE *xmlout, j2k_cp_t *cp) { /* For main header, not til
/* ------------- */
void xml_out_frame_ppt(FILE *xmlout, j2k_tcp_t *tcp) { /* For tile-part header, not main (which uses PPM instead). */
void xml_out_frame_ppt(FILE *xmlout, opj_tcp_t *tcp) { /* For tile-part header, not main (which uses PPM instead). */
/* Either the PPM or PPT is required if the packet headers are not distributed in the bit stream */
/* Use of PPM and PPT are mutually exclusive. */
/* Compare j2k_read_ppt() */
@ -1824,7 +1820,7 @@ void xml_out_frame_plm(FILE* xmlout) { /* opt, main header only; can be used in
/* ------------- */
void xml_out_frame_plt(FILE* xmlout, j2k_tcp_t *tcp) { /* opt, tile-part headers only; can be used in conjunction with main header's PLM */
void xml_out_frame_plt(FILE* xmlout, opj_tcp_t *tcp) { /* opt, tile-part headers only; can be used in conjunction with main header's PLM */
/* NO-OP. PLT NOT SAVED IN DATA STRUCTURE */
/* Compare j2k_read_plt()... which doesn't retain anything! */
/* Tile-part header indents are 12 spaces */
@ -1879,7 +1875,7 @@ void xml_out_frame_crg(FILE* xmlout) { /* NO-OP. CRG NOT SAVED IN DATA STRUCTUR
/* ------------- */
/* Regrettably from a metadata point of view, j2k_read_com() skips over any comments in main header or tile-part-header */
void xml_out_frame_com(FILE* xmlout, j2k_tcp_t *tcp) { /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main or tile-part headers; */
void xml_out_frame_com(FILE* xmlout, opj_tcp_t *tcp) { /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main or tile-part headers; */
/* Compare j2k_read_com()... which doesn't retain anything! */
#ifdef NOTYET
char spaces[13] = " "; /* 12 spaces if tilepart*/
@ -1948,8 +1944,8 @@ void xml_out_dump_hex_and_ascii(FILE* xmlout, char *data, int data_len, char* s)
/* ------------- */
void xml_out_frame_jp2h(FILE* xmlout, jp2_struct_t *jp2_struct) { /* JP2 Header */
/* Compare jp2_read_jp2h(jp2_struct_t * jp2_struct) */
void xml_out_frame_jp2h(FILE* xmlout, opj_jp2_t *jp2_struct) { /* JP2 Header */
/* Compare jp2_read_jp2h(opj_jp2_t * jp2_struct) */
int i;
fprintf(xmlout, " <JP2Header BoxType=\"jp2h\">\n");
@ -2052,7 +2048,7 @@ void xml_out_frame_jp2h(FILE* xmlout, jp2_struct_t *jp2_struct) { /* JP2 Header
#ifdef NOTYET
IMAGE these use cp structure, extended... but we could use a new data structure instead
void xml_out_frame_jp2i(FILE* xmlout, j2k_cp_t *cp) {
void xml_out_frame_jp2i(FILE* xmlout, opj_cp_t *cp) {
/* IntellectualProperty 'jp2i' (no restrictions on location) */
int i;
IMAGE cp->jp2i, cp->jp2i_count, cp->jp2i_data (array of chars), cp->cp2i_len (array of ints)
@ -2070,7 +2066,7 @@ void xml_out_frame_jp2i(FILE* xmlout, j2k_cp_t *cp) {
}
}
void xml_out_frame_xml(FILE* xmlout, j2k_cp_t *cp) {
void xml_out_frame_xml(FILE* xmlout, opj_cp_t *cp) {
/* XML 'xml\040' (0x786d6c20). Can appear multiply, before or after jp2c codestreams */
IMAGE cp->xml, cp->xml_count, cp->xml_data (array of chars)
MAYBE WE DON'T NEED cp->xml_len (array of ints) IF WE ASSUME xml_data IS NULL-TERMINATED.
@ -2089,7 +2085,7 @@ void xml_out_frame_xml(FILE* xmlout, j2k_cp_t *cp) {
}
}
void xml_out_frame_uuid(FILE* xmlout, j2k_cp_t *cp) {
void xml_out_frame_uuid(FILE* xmlout, opj_cp_t *cp) {
/* UUID 'uuid' (top level only) */
/* Part I 1.7.2 says: may appear multiply in JP2 file, anywhere except before File Type box */
/* Part III 5.2.1 says: Private extensions shall be achieved through the 'uuid' type. */
@ -2119,7 +2115,7 @@ void xml_out_frame_uuid(FILE* xmlout, j2k_cp_t *cp) {
}
}
void xml_out_frame_uinf(FILE* xmlout, j2k_cp_t *cp) {
void xml_out_frame_uinf(FILE* xmlout, opj_cp_t *cp) {
/* UUIDInfo 'uinf', includes UUIDList 'ulst' and URL 'url\40' */
/* Part I 1.7.3 says: may appear multiply in JP2 file, anywhere at the top level except before File Type box */
/* So there may be multiple ulst's, and each can have multiple UUIDs listed (with a single URL) */
@ -2151,7 +2147,7 @@ void xml_out_frame_uinf(FILE* xmlout, j2k_cp_t *cp) {
}
IMAGE these use cp structure, extended... but we could use a new data structure instead
void xml_out_frame_unknown_type(FILE* xmlout, j2k_cp_t *cp) {
void xml_out_frame_unknown_type(FILE* xmlout, opj_cp_t *cp) {
/* Part III 5.2.1 says "Type fields not defined here are reserved. Private extensions
shall be acieved through the 'uuid' type." [This implies an unknown
type would be an error, but then...] "Boxes not explicitly defined in this standard,

View File

@ -9,5 +9,5 @@
void xml_write_init(BOOL n, BOOL t, BOOL r, BOOL d);
int xml_write_struct(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe, char* stringDTD);
int xml_write_struct(FILE *file, FILE *xmlout, opj_mj2_t * movie, unsigned int sampleframe, char* stringDTD, opj_event_mgr_t *event_mgr);

2917
mj2/mj2.c Normal file

File diff suppressed because it is too large Load Diff

391
mj2/mj2.h Normal file
View File

@ -0,0 +1,391 @@
/*
* Copyright (c) 2003-2004, François-Olivier Devaux
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __MJ2_H
#define __MJ2_H
/**
@file mj2.h
@brief The Motion JPEG 2000 file format Reader/Writer (MJ22)
*/
/** @defgroup MJ2 MJ2 - Motion JPEG 2000 file format reader/writer */
/*@{*/
#define MJ2_JP 0x6a502020
#define MJ2_FTYP 0x66747970
#define MJ2_MJ2 0x6d6a7032
#define MJ2_MJ2S 0x6d6a3273
#define MJ2_MDAT 0x6d646174
#define MJ2_MOOV 0x6d6f6f76
#define MJ2_MVHD 0x6d766864
#define MJ2_TRAK 0x7472616b
#define MJ2_TKHD 0x746b6864
#define MJ2_MDIA 0x6d646961
#define MJ2_MDHD 0x6d646864
#define MJ2_MHDR 0x6d686472
#define MJ2_HDLR 0x68646C72
#define MJ2_MINF 0x6d696e66
#define MJ2_VMHD 0x766d6864
#define MJ2_SMHD 0x736d6864
#define MJ2_HMHD 0x686d6864
#define MJ2_DINF 0x64696e66
#define MJ2_DREF 0x64726566
#define MJ2_URL 0x75726c20
#define MJ2_URN 0x75726e20
#define MJ2_STBL 0x7374626c
#define MJ2_STSD 0x73747364
#define MJ2_STTS 0x73747473
#define MJ2_STSC 0x73747363
#define MJ2_STSZ 0x7374737a
#define MJ2_STCO 0x7374636f
#define MJ2_MOOF 0x6d6f6f66
#define MJ2_FREE 0x66726565
#define MJ2_SKIP 0x736b6970
#define MJ2_JP2C 0x6a703263
#define MJ2_FIEL 0x6669656c
#define MJ2_JP2P 0x6a703270
#define MJ2_JP2X 0x6a703278
#define MJ2_JSUB 0x6a737562
#define MJ2_ORFO 0x6f72666f
#define MJ2_MVEX 0x6d766578
#define MJ2_JP2 0x6a703220
#define MJ2_J2P0 0x4a325030
/**
Decompressed format used in parameters
YUV = 0
*/
#define YUV_DFMT 0
/**
Compressed format used in parameters
MJ2 = 0
*/
#define MJ2_CFMT 0
/* ----------------------------------------------------------------------- */
/**
Time To Sample
*/
typedef struct mj2_tts {
int sample_count;
int sample_delta;
} mj2_tts_t;
/**
Chunk
*/
typedef struct mj2_chunk {
int num_samples;
int sample_descr_idx;
int offset;
} mj2_chunk_t;
/**
Sample to chunk
*/
typedef struct mj2_sampletochunk {
int first_chunk;
int samples_per_chunk;
int sample_descr_idx;
} mj2_sampletochunk_t;
/**
Sample
*/
typedef struct mj2_sample {
unsigned int sample_size;
unsigned int offset;
unsigned int sample_delta;
} mj2_sample_t;
/**
URL
*/
typedef struct mj2_url {
int location[4];
} mj2_url_t;
/**
URN
*/
typedef struct mj2_urn {
int name[2];
int location[4];
} mj2_urn_t;
/**
Video Track Parameters
*/
typedef struct mj2_tk {
/** codec context */
opj_common_ptr cinfo;
int track_ID;
int track_type;
unsigned int creation_time;
unsigned int modification_time;
int duration;
int timescale;
int layer;
int volume;
int language;
int balance;
int maxPDUsize;
int avgPDUsize;
int maxbitrate;
int avgbitrate;
int slidingavgbitrate;
int graphicsmode;
int opcolor[3];
int num_url;
mj2_url_t *url;
int num_urn;
mj2_urn_t *urn;
int Dim[2];
int w;
int h;
int visual_w;
int visual_h;
int CbCr_subsampling_dx;
int CbCr_subsampling_dy;
int sample_rate;
int sample_description;
int horizresolution;
int vertresolution;
int compressorname[8];
int depth;
unsigned char fieldcount;
unsigned char fieldorder;
unsigned char or_fieldcount;
unsigned char or_fieldorder;
int num_br;
unsigned int *br;
unsigned char num_jp2x;
unsigned char *jp2xdata;
unsigned char hsub;
unsigned char vsub;
unsigned char hoff;
unsigned char voff;
int trans_matrix[9];
/** Number of samples */
unsigned int num_samples;
int transorm;
int handler_type;
int name_size;
unsigned char same_sample_size;
int num_tts;
/** Time to sample */
mj2_tts_t *tts;
unsigned int num_chunks;
mj2_chunk_t *chunk;
int num_samplestochunk;
mj2_sampletochunk_t *sampletochunk;
char *name;
opj_jp2_t jp2_struct;
/** Sample parameters */
mj2_sample_t *sample;
} mj2_tk_t;
/**
MJ2 box
*/
typedef struct mj2_box {
int length;
int type;
int init_pos;
} mj2_box_t;
/**
MJ2 Movie
*/
typedef struct opj_mj2 {
/** codec context */
opj_common_ptr cinfo;
/** handle to the J2K codec */
opj_j2k_t *j2k;
unsigned int brand;
unsigned int minversion;
int num_cl;
unsigned int *cl;
unsigned int creation_time;
unsigned int modification_time;
int timescale;
unsigned int duration;
int rate;
int num_vtk;
int num_stk;
int num_htk;
int volume;
int trans_matrix[9];
int next_tk_id;
/** Track Parameters */
mj2_tk_t *tk;
} opj_mj2_t;
/**
Decompression parameters
*/
typedef struct mj2_dparameters {
/**@name command line encoder parameters (not used inside the library) */
/*@{*/
/** input file name */
char infile[OPJ_PATH_LEN];
/** output file name */
char outfile[OPJ_PATH_LEN];
/** J2K decompression parameters */
opj_dparameters_t j2k_parameters;
} mj2_dparameters_t;
/**
Compression parameters
*/
typedef struct mj2_cparameters {
/**@name command line encoder parameters (not used inside the library) */
/*@{*/
/** J2K compression parameters */
opj_cparameters_t j2k_parameters;
/** input file name */
char infile[OPJ_PATH_LEN];
/** output file name */
char outfile[OPJ_PATH_LEN];
/** input file format 0:MJ2 */
int decod_format;
/** output file format 0:YUV */
int cod_format;
/** Portion of the image coded */
int Dim[2];
/** YUV Frame width */
int w;
/** YUV Frame height */
int h;
/* Sample rate of YUV 4:4:4, 4:2:2 or 4:2:0 */
int CbCr_subsampling_dx;
/* Sample rate of YUV 4:4:4, 4:2:2 or 4:2:0 */
int CbCr_subsampling_dy;
/* Video Frame Rate */
int frame_rate;
/* In YUV files, numcomps always considered as 3 */
int numcomps;
/* In YUV files, precision always considered as 8 */
int prec;
} mj2_cparameters_t;
/** @name Exported functions */
/*@{*/
/* ----------------------------------------------------------------------- */
/**
Write the JP box
*/
void mj2_write_jp(opj_cio_t *cio);
/**
Write the FTYP box
@param movie MJ2 movie
@param cio Output buffer stream
*/
void mj2_write_ftyp(opj_mj2_t *movie, opj_cio_t *cio);
/**
Creates an MJ2 decompression structure
@return Returns a handle to a MJ2 decompressor if successful, returns NULL otherwise
*/
opj_dinfo_t* mj2_create_decompress();
/**
Destroy a MJ2 decompressor handle
@param movie MJ2 decompressor handle to destroy
*/
void mj2_destroy_decompress(opj_mj2_t *movie);
/**
Setup the decoder decoding parameters using user parameters.
Decoding parameters are returned in mj2->j2k->cp.
@param movie MJ2 decompressor handle
@param parameters decompression parameters
*/
void mj2_setup_decoder(opj_mj2_t *movie, mj2_dparameters_t *mj2_parameters);
/**
Decode an image from a JPEG-2000 file stream
@param movie MJ2 decompressor handle
@param cio Input buffer stream
@return Returns a decoded image if successful, returns NULL otherwise
*/
opj_image_t* mj2_decode(opj_mj2_t *movie, opj_cio_t *cio);
/**
Creates a MJ2 compression structure
@return Returns a handle to a MJ2 compressor if successful, returns NULL otherwise
*/
opj_cinfo_t* mj2_create_compress();
/**
Destroy a MJ2 compressor handle
@param movie MJ2 compressor handle to destroy
*/
void mj2_destroy_compress(opj_mj2_t *movie);
/**
Setup the encoder parameters using the current image and using user parameters.
Coding parameters are returned in mj2->j2k->cp.
@param movie MJ2 compressor handle
@param parameters compression parameters
*/
void mj2_setup_encoder(opj_mj2_t *movie, mj2_cparameters_t *parameters);
/**
Encode an image into a JPEG-2000 file stream
@param movie MJ2 compressor handle
@param cio Output buffer stream
@param image Image to encode
@param index Name of the index file if required, NULL otherwise
@return Returns true if successful, returns false otherwise
*/
bool mj2_encode(opj_mj2_t *movie, opj_cio_t *cio, opj_image_t *image, char *index);
/**
Init a Standard MJ2 movie
@param movie MJ2 Movie
@return Returns 0 if successful, returns 1 otherwise
*/
int mj2_init_stdmovie(opj_mj2_t *movie);
/**
Read the structure of an MJ2 file
@param File MJ2 input File
@param movie J2 movie structure
@return Returns 0 if successful, returns 1 otherwise
*/
int mj2_read_struct(FILE *file, opj_mj2_t *mj2);
/**
Write the the MOOV box to an output buffer stream
@param movie MJ2 movie structure
@param cio Output buffer stream
*/
void mj2_write_moov(opj_mj2_t *movie, opj_cio_t *cio);
/* ----------------------------------------------------------------------- */
/*@}*/
/*@}*/
#endif /* __MJ2_H */

View File

@ -1,13 +1,33 @@
#include <stdio.h>
#ifdef WIN32
#include <malloc.h>
#else
#include <stdlib.h>
#endif
/*
* Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2007, Professor Benoit Macq
* Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "opj_includes.h"
#include "mj2.h"
#include <j2k.h>
#include <int.h>
/* ----------------------- */
/* */
@ -17,10 +37,17 @@
/* */
/* ----------------------- */
int yuv_num_frames(mj2_tk_t * tk, FILE *f)
int yuv_num_frames(mj2_tk_t * tk, char *infile)
{
int numimages, frame_size;
long end_of_f;
FILE *f;
f = fopen(infile,"rb");
if (!f) {
fprintf(stderr, "failed to open %s for reading\n",infile);
return 1;
}
frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */
@ -35,9 +62,9 @@ int yuv_num_frames(mj2_tk_t * tk, FILE *f)
}
numimages = end_of_f / frame_size; /* Calculate number of images */
fclose(f);
return numimages;
fclose(f);
}
// -----------------------
@ -47,19 +74,49 @@ int yuv_num_frames(mj2_tk_t * tk, FILE *f)
//
// -----------------------
int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
int frame_num, int subsampling_dx, int subsampling_dy)
opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters)
{
int i, j;
opj_image_cmptparm_t cmptparm[3];
opj_image_t * img;
int i;
int numcomps = 3;
int subsampling_dx = parameters->subsampling_dx;
int subsampling_dy = parameters->subsampling_dy;
/* initialize image components */
memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
for(i = 0; i < numcomps; i++) {
cmptparm[i].prec = 8;
cmptparm[i].bpp = 8;
cmptparm[i].sgnd = 0;
cmptparm[i].dx = i ? subsampling_dx * tk->CbCr_subsampling_dx : subsampling_dx;
cmptparm[i].dy = i ? subsampling_dy * tk->CbCr_subsampling_dy : subsampling_dy;
cmptparm[i].w = tk->w;
cmptparm[i].h = tk->h;
}
/* create the image */
img = opj_image_create(numcomps, cmptparm, CLRSPC_SRGB);
return img;
}
bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile)
{
int i, compno;
int offset;
long end_of_f, position;
FILE *Compo;
int numcomps = 3;
int subsampling_dx = parameters->subsampling_dx;
int subsampling_dy = parameters->subsampling_dy;
FILE *yuvfile;
offset =
(int) ((double) (frame_num * tk->w * tk->h) *
(1.0 +
1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx *
tk->CbCr_subsampling_dy)));
yuvfile = fopen(infile,"rb");
if (!yuvfile) {
fprintf(stderr, "failed to open %s for reading\n",parameters->infile);
return false;
}
offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 +
1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy)));
fseek(yuvfile, 0, SEEK_END);
end_of_f = ftell(yuvfile);
fseek(yuvfile, sizeof(unsigned char) * offset, SEEK_SET);
@ -67,7 +124,8 @@ int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
if (position >= end_of_f) {
fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n",
frame_num);
return 0;
fclose(yuvfile);
return false;
}
img->x0 = tk->Dim[0];
@ -76,68 +134,19 @@ int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
(tk->w - 1) * subsampling_dx + 1;
img->y1 = !tk->Dim[1] ? (tk->h - 1) * subsampling_dy + 1 : tk->Dim[1] +
(tk->h - 1) * subsampling_dy + 1;
img->numcomps = 3;
img->color_space = 3;
img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) {
img->comps[i].data = (int *) malloc(sizeof(int) * tk->w * tk->h);
img->comps[i].prec = 8;
img->comps[i].bpp = 8;
img->comps[i].sgnd = 0;
if (i == 0) {
img->comps[i].dx = subsampling_dx;
img->comps[i].dy = subsampling_dy;
} else {
img->comps[i].dx = subsampling_dx * tk->CbCr_subsampling_dx;
img->comps[i].dy = subsampling_dy * tk->CbCr_subsampling_dy;
}
}
Compo = fopen("Compo0", "wb");
if (!Compo) {
fprintf(stderr, "Failed to open Compo0 for writing !\n");
}
for (i = 0; i < (tk->w * tk->h / (img->comps[0].dx * img->comps[0].dy))
for(compno = 0; compno < numcomps; compno++) {
for (i = 0; i < (tk->w * tk->h / (img->comps[compno].dx * img->comps[compno].dy))
&& !feof(yuvfile); i++) {
unsigned char y;
j = fread(&y, 1, 1, yuvfile);
fwrite(&y, 1, 1, Compo);
if (!fread(&img->comps[compno].data[i], 1, 1, yuvfile)) {
fprintf(stderr, "Error reading %s file !!\n", infile);
return false;
}
fclose(Compo);
Compo = fopen("Compo1", "wb");
if (!Compo) {
fprintf(stderr, "Failed to open Compo1 for writing !\n");
}
for (i = 0; i < (tk->w * tk->h / (img->comps[1].dx * img->comps[1].dy))
&& !feof(yuvfile); i++) {
unsigned char cb;
j = fread(&cb, sizeof(unsigned char), 1, yuvfile);
fwrite(&cb, 1, 1, Compo);
}
fclose(yuvfile);
fclose(Compo);
Compo = fopen("Compo2", "wb");
if (!Compo) {
fprintf(stderr, "Failed to open Compo2 for writing !\n");
}
for (i = 0; i < (tk->w * tk->h / (img->comps[2].dx * img->comps[2].dy))
&& !feof(yuvfile); i++) {
unsigned char cr;
j = fread(&cr, sizeof(unsigned char), 1, yuvfile);
fwrite(&cr, 1, 1, Compo);
}
fclose(Compo);
return 1;
return true;
}
@ -150,7 +159,7 @@ int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
// -----------------------
int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
bool imagetoyuv(opj_image_t * img, char *outfile)
{
FILE *f;
int i;
@ -160,18 +169,18 @@ int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
|| img->comps[1].dx != img->comps[2].dx) {
fprintf(stderr,
"Error with the input image components size: cannot create yuv file)\n");
return 1;
return false;
}
} else if (!(img->numcomps == 1)) {
fprintf(stderr,
"Error with the number of image components(must be one or three)\n");
return 1;
return false;
}
f = fopen(outfile, "a+b");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1;
return false;
}
@ -208,7 +217,7 @@ int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
}
}
fclose(f);
return 0;
return true;
}
// -----------------------
@ -218,7 +227,7 @@ int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
//
// -----------------------
int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile) {
int imagetobmp(opj_image_t * img, char *outfile) {
int w,wr,h,hr,i,pad;
FILE *f;
@ -315,8 +324,8 @@ int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile) {
}
}
fclose(f);
free(img->comps[1].data);
free(img->comps[2].data);
opj_free(img->comps[1].data);
opj_free(img->comps[2].data);
}
return 0;
}

45
mj2/mj2_convert.h Normal file
View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2003-2004, François-Olivier Devaux
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "mj2.h"
#ifndef __MJ2_CONVERT_H
#define __MJ2_CONVERT_H
int imagetoyuv(opj_image_t * img, char *outfile);
int imagetobmp(opj_image_t * img, char *outfile);
opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters);
bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile);
int yuv_num_frames(mj2_tk_t * tk, char *infile);
#endif

216
mj2/mj2_to_frames.c Normal file
View File

@ -0,0 +1,216 @@
/*
* Copyright (c) 2003-2004, François-Olivier Devaux
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "opj_includes.h"
#include "mj2.h"
#include "mj2_convert.h"
/* -------------------------------------------------------------------------- */
/**
sample error callback expecting a FILE* client object
*/
void error_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[ERROR] %s", msg);
}
/**
sample warning callback expecting a FILE* client object
*/
void warning_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[WARNING] %s", msg);
}
/**
sample debug callback expecting a FILE* client object
*/
void info_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[INFO] %s", msg);
}
/* -------------------------------------------------------------------------- */
int main(int argc, char *argv[]) {
mj2_dparameters_t mj2_parameters; /* decompression parameters */
opj_dinfo_t* dinfo;
opj_event_mgr_t event_mgr; /* event manager */
opj_cio_t *cio = NULL;
unsigned int tnum, snum;
opj_mj2_t *movie;
mj2_tk_t *track;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *file, *outfile;
char outfilename[50];
opj_image_t *img = NULL;
unsigned int max_codstrm_size = 0;
double total_time = 0;
unsigned int numframes = 0;
if (argc != 3) {
printf("Bad syntax: Usage: mj2_to_frames inputfile.mj2 outputfile.yuv\n");
printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
return 1;
}
file = fopen(argv[1], "rb");
if (!file) {
fprintf(stderr, "failed to open %s for reading\n", argv[1]);
return 1;
}
// Checking output file
outfile = fopen(argv[2], "w");
if (!file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
fclose(outfile);
/*
configure the event callbacks (not required)
setting of each callback is optionnal
*/
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = NULL;
/* get a MJ2 decompressor handle */
dinfo = mj2_create_decompress();
movie = dinfo->mj2_handle;
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
/* set J2K decoding parameters to default values */
opj_set_default_decoder_parameters(&mj2_parameters.j2k_parameters);
/* setup the decoder decoding parameters using user parameters */
mj2_setup_decoder(dinfo->mj2_handle, &mj2_parameters);
if (mj2_read_struct(file, movie)) // Creating the movie structure
return 1;
// Decode first video track
for (tnum=0; tnum < (unsigned int)(movie->num_htk + movie->num_stk + movie->num_vtk); tnum++) {
if (movie->tk[tnum].track_type == 0)
break;
}
if (movie->tk[tnum].track_type != 0) {
printf("Error. Movie does not contain any video track\n");
return 1;
}
track = &movie->tk[tnum];
// Output info on first video tracl
fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
track->num_samples, track->w, track->h);
max_codstrm_size = track->sample[0].sample_size-8;
frame_codestream = (unsigned char*) malloc(max_codstrm_size * sizeof(unsigned char));
numframes = track->num_samples;
for (snum=0; snum < numframes; snum++)
{
double init_time = opj_clock();
double elapsed_time;
sample = &track->sample[snum];
if (sample->sample_size-8 > max_codstrm_size) {
max_codstrm_size = sample->sample_size-8;
if ((frame_codestream = realloc(frame_codestream, max_codstrm_size)) == NULL) {
printf("Error reallocation memory\n");
return 1;
};
}
fseek(file,sample->offset+8,SEEK_SET);
fread(frame_codestream, sample->sample_size-8, 1, file); // Assuming that jp and ftyp markers size do
/* open a byte stream */
cio = opj_cio_open((opj_common_ptr)dinfo, frame_codestream, sample->sample_size-8);
img = opj_decode(dinfo, cio); // Decode J2K to image
if (((img->numcomps == 3) && (img->comps[0].dx == img->comps[1].dx / 2)
&& (img->comps[0].dx == img->comps[2].dx / 2 ) && (img->comps[0].dx == 1))
|| (img->numcomps == 1)) {
if (!imagetoyuv(img, argv[2])) // Convert image to YUV
return 1;
}
else if ((img->numcomps == 3) &&
(img->comps[0].dx == 1) && (img->comps[1].dx == 1)&&
(img->comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp
{
fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
sprintf(outfilename,"output_%d.bmp",snum);
if (imagetobmp(img, outfilename)) // Convert image to YUV
return 1;
}
else {
fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
sprintf(outfilename,"output_%d.j2k",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);
}
/* close the byte stream */
opj_cio_close(cio);
/* free image data structure */
opj_image_destroy(img);
elapsed_time = opj_clock()-init_time;
fprintf(stderr, "Frame number %d/%d decoded in %.2f mseconds\n", snum + 1, numframes, elapsed_time*1000);
total_time += elapsed_time;
}
free(frame_codestream);
fclose(file);
/* free remaining structures */
if(dinfo) {
mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
}
free(dinfo);
fprintf(stdout, "%d frame(s) correctly decompressed\n", snum);
fprintf(stdout,"Total decoding time: %.2f seconds (%.1f fps)\n", total_time, (float)numframes/total_time);
return 0;
}

View File

@ -42,7 +42,7 @@ RSC=rc.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
@ -63,9 +63,10 @@ LINK32=link.exe
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "mj2_to_frames___Win32_Debug0"
# PROP Intermediate_Dir "mj2_to_frames___Win32_Debug0"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /MT /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /GZ /c
# SUBTRACT CPP /X
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
@ -74,7 +75,7 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmtd" /pdbtype:sept
!ENDIF
@ -112,8 +113,6 @@ SOURCE=.\compat\getopt.c
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
@ -125,8 +124,6 @@ SOURCE=.\mj2.c
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
@ -138,8 +135,6 @@ SOURCE=.\mj2_convert.c
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
@ -151,148 +146,14 @@ SOURCE=.\mj2_to_frames.c
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
# End Group
# End Group
# Begin Group "Libopenjpeg_097"
# Begin Group "OpenJPEG Headers"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg_097\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\fix.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tgt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tgt.h
# End Source File
# End Group
# End Target
# End Project

44
mj2/mj2_to_frames.dsw Normal file
View File

@ -0,0 +1,44 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Project: "mj2_to_frames"=".\mj2_to_frames.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name LibOpenJPEG
End Project Dependency
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View File

@ -10,28 +10,41 @@ A non-exclusive copy of this code has been contributed to the Open JPEG project.
Except for copyright, inclusion of the code within Open JPEG for distribution and use
can be bound by the Open JPEG open-source license and disclaimer, expressed elsewhere.
*/
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include "opj_includes.h"
#include "mj2.h"
#include <openjpeg.h>
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
#include "mj2_to_metadata.h"
#include <string.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
#else
#include "compat/getopt.h"
#endif
/* -------------------------------------------------------------------------- */
/**
sample error callback expecting a FILE* client object
*/
void error_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[ERROR] %s", msg);
}
/**
sample warning callback expecting a FILE* client object
*/
void warning_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[WARNING] %s", msg);
}
/**
sample debug callback expecting a FILE* client object
*/
void info_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[INFO] %s", msg);
}
/* -------------------------------------------------------------------------- */
/* ------------- */
@ -96,9 +109,12 @@ void help_display()
int main(int argc, char *argv[]) {
opj_dinfo_t* dinfo;
opj_event_mgr_t event_mgr; /* event manager */
FILE *file, *xmlout;
/* char xmloutname[50]; */
mj2_movie_t movie;
opj_mj2_t *movie;
char* infile = 0;
char* outfile = 0;
@ -110,10 +126,7 @@ int main(int argc, char *argv[]) {
BOOL sampletables = FALSE;
BOOL raw = TRUE;
BOOL derived = TRUE;
#ifndef NO_PACKETS_DECODING
fprintf(stdout,"WARNING: For best performance, define NO_PACKETS_DECODING in preprocessing.\n");
#endif
mj2_dparameters_t parameters;
while (TRUE) {
/* ':' after letter means it takes an argument */
@ -257,23 +270,41 @@ int main(int argc, char *argv[]) {
}
// Leave it open
if (mj2_read_struct(file, &movie)) // Creating the movie structure
/*
configure the event callbacks (not required)
setting of each callback is optionnal
*/
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
/* get a MJ2 decompressor handle */
dinfo = mj2_create_decompress();
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
/* setup the decoder decoding parameters using user parameters */
movie = (opj_mj2_t*) dinfo->mj2_handle;
mj2_setup_decoder(dinfo->mj2_handle, &parameters);
if (mj2_read_struct(file, movie)) // Creating the movie structure
{
fclose(xmlout);
return 1;
}
xml_write_init(notes, sampletables, raw, derived);
xml_write_struct(file, xmlout, &movie, sampleframe, stringDTD);
xml_write_struct(file, xmlout, movie, sampleframe, stringDTD, &event_mgr);
fclose(xmlout);
mj2_memory_free(&movie);
fprintf(stderr,"Metadata correctly extracted to XML file \n");;
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
/* free remaining structures */
if(dinfo) {
mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
}
return 0;
}

View File

@ -39,9 +39,10 @@ RSC=rc.exe
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c
# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
@ -49,7 +50,8 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /nodefaultlib:"libcmtd"
# SUBTRACT LINK32 /pdb:none
!ELSEIF "$(CFG)" == "mj2_to_metadata - Win32 Debug"
@ -62,9 +64,10 @@ LINK32=link.exe
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "mj2_to_metadata___Win32_Debug0"
# PROP Intermediate_Dir "mj2_to_metadata___Win32_Debug0"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /FR /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
@ -72,7 +75,8 @@ BSC32=bscmake.exe
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmtd" /pdbtype:sept
# SUBTRACT LINK32 /pdb:none
!ENDIF
@ -132,141 +136,5 @@ SOURCE=.\mj2_to_metadata.c
# End Source File
# End Group
# End Group
# Begin Group "Libopenjpeg_097"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg_097\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\fix.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tgt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg_097\tgt.h
# End Source File
# End Group
# End Target
# End Project

44
mj2/mj2_to_metadata.dsw Normal file
View File

@ -0,0 +1,44 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Project: "mj2_to_metadata"=".\mj2_to_metadata.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name LibOpenJPEG
End Project Dependency
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

337
mj2/wrap_j2k_in_mj2.c Normal file
View File

@ -0,0 +1,337 @@
/*
* Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2007, Professor Benoit Macq
* Copyright (c) 2003-2007, Francois-Olivier Devaux
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "opj_includes.h"
#include "mj2.h"
/**
Size of memory first allocated for MOOV box
*/
#define TEMP_BUF 10000
/* -------------------------------------------------------------------------- */
/**
sample error callback expecting a FILE* client object
*/
void error_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[ERROR] %s", msg);
}
/**
sample warning callback expecting a FILE* client object
*/
void warning_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[WARNING] %s", msg);
}
/**
sample debug callback expecting a FILE* client object
*/
void info_callback(const char *msg, void *client_data) {
FILE *stream = (FILE*)client_data;
fprintf(stream, "[INFO] %s", msg);
}
/* -------------------------------------------------------------------------- */
static void read_siz_marker(FILE *file, opj_image_t *image)
{
int len,i;
char buf, buf2[2];
char *siz_buffer;
opj_cio_t *cio;
fseek(file, 0, SEEK_SET);
do {
fread(&buf,1,1, file);
if (buf==(char)0xff)
fread(&buf,1,1, file);
}
while (!(buf==(char)0x51));
fread(buf2,2,1,file); /* Lsiz */
len = ((buf2[0])<<8) + buf2[1];
siz_buffer = (char*) malloc(len * sizeof(char));
fread(siz_buffer,len, 1, file);
cio = opj_cio_open(NULL, siz_buffer, len);
cio_read(cio, 2); /* Rsiz (capabilities) */
image->x1 = cio_read(cio, 4); /* Xsiz */
image->y1 = cio_read(cio, 4); /* Ysiz */
image->x0 = cio_read(cio, 4); /* X0siz */
image->y0 = cio_read(cio, 4); /* Y0siz */
cio_skip(cio, 16); /* XTsiz, YTsiz, XT0siz, YT0siz */
image->numcomps = cio_read(cio,2); /* Csiz */
image->comps =
(opj_image_comp_t *) malloc(image->numcomps * sizeof(opj_image_comp_t));
for (i = 0; i < image->numcomps; i++) {
int tmp;
tmp = cio_read(cio,1); /* Ssiz_i */
image->comps[i].prec = (tmp & 0x7f) + 1;
image->comps[i].sgnd = tmp >> 7;
image->comps[i].dx = cio_read(cio,1); /* XRsiz_i */
image->comps[i].dy = cio_read(cio,1); /* YRsiz_i */
image->comps[i].resno_decoded = 0; /* number of resolution decoded */
image->comps[i].factor = 0; /* reducing factor by component */
}
fseek(file, 0, SEEK_SET);
opj_cio_close(cio);
}
static void setparams(opj_mj2_t *movie, opj_image_t *image) {
int i, depth_0, depth, sign;
movie->tk[0].sample_rate = 25;
movie->tk[0].w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
movie->tk[0].h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
mj2_init_stdmovie(movie);
movie->tk[0].depth = image->comps[0].prec;
if (image->numcomps==3) {
if ((image->comps[0].dx == 1) && (image->comps[1].dx == 1) && (image->comps[1].dx == 1))
movie->tk[0].CbCr_subsampling_dx = 1;
else if ((image->comps[0].dx == 1) && (image->comps[1].dx == 2) && (image->comps[1].dx == 2))
movie->tk[0].CbCr_subsampling_dx = 2;
else
fprintf(stderr,"Image component sizes are incoherent\n");
if ((image->comps[0].dy == 1) && (image->comps[1].dy == 1) && (image->comps[1].dy == 1))
movie->tk[0].CbCr_subsampling_dy = 1;
else if ((image->comps[0].dy == 1) && (image->comps[1].dy == 2) && (image->comps[1].dy == 2))
movie->tk[0].CbCr_subsampling_dy = 2;
else
fprintf(stderr,"Image component sizes are incoherent\n");
}
movie->tk[0].sample_rate = 25;
movie->tk[0].jp2_struct.numcomps = image->numcomps; // NC
/* Init Standard jp2 structure */
movie->tk[0].jp2_struct.comps =
(opj_jp2_comps_t *) malloc(movie->tk[0].jp2_struct.numcomps * sizeof(opj_jp2_comps_t));
movie->tk[0].jp2_struct.precedence = 0; /* PRECEDENCE*/
movie->tk[0].jp2_struct.approx = 0; /* APPROX*/
movie->tk[0].jp2_struct.brand = JP2_JP2; /* BR */
movie->tk[0].jp2_struct.minversion = 0; /* MinV */
movie->tk[0].jp2_struct.numcl = 1;
movie->tk[0].jp2_struct.cl = (unsigned int *) malloc(movie->tk[0].jp2_struct.numcl * sizeof(int));
movie->tk[0].jp2_struct.cl[0] = JP2_JP2; /* CL0 : JP2 */
movie->tk[0].jp2_struct.C = 7; /* C : Always 7*/
movie->tk[0].jp2_struct.UnkC = 0; /* UnkC, colorspace specified in colr box*/
movie->tk[0].jp2_struct.IPR = 0; /* IPR, no intellectual property*/
movie->tk[0].jp2_struct.w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
movie->tk[0].jp2_struct.h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
depth_0 = image->comps[0].prec - 1;
sign = image->comps[0].sgnd;
movie->tk[0].jp2_struct.bpc = depth_0 + (sign << 7);
for (i = 1; i < image->numcomps; i++) {
depth = image->comps[i].prec - 1;
sign = image->comps[i].sgnd;
if (depth_0 != depth)
movie->tk[0].jp2_struct.bpc = 255;
}
for (i = 0; i < image->numcomps; i++)
movie->tk[0].jp2_struct.comps[i].bpcc =
image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
if ((image->numcomps == 1 || image->numcomps == 3)
&& (movie->tk[0].jp2_struct.bpc != 255))
movie->tk[0].jp2_struct.meth = 1;
else
movie->tk[0].jp2_struct.meth = 2;
if (image->numcomps == 1)
movie->tk[0].jp2_struct.enumcs = 17; // Grayscale
else if ((image->comps[0].dx == 1) && (image->comps[1].dx == 1) && (image->comps[1].dx == 1) &&
(image->comps[0].dy == 1) && (image->comps[1].dy == 1) && (image->comps[1].dy == 1))
movie->tk[0].jp2_struct.enumcs = 16; // RGB
else if ((image->comps[0].dx == 1) && (image->comps[1].dx == 2) && (image->comps[1].dx == 2) &&
(image->comps[0].dy == 1) && (image->comps[1].dy == 2) && (image->comps[1].dy == 2))
movie->tk[0].jp2_struct.enumcs = 18; // YUV
else
movie->tk[0].jp2_struct.enumcs = 0; // Unkown profile */
}
int main(int argc, char *argv[]) {
opj_cinfo_t* cinfo;
opj_event_mgr_t event_mgr; /* event manager */
unsigned int snum;
opj_mj2_t *movie;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *mj2file, *j2kfile;
char j2kfilename[50];
char *buf;
int offset, mdat_initpos;
opj_image_t img;
opj_cio_t *cio;
mj2_cparameters_t parameters;
if (argc != 3) {
printf("Bad syntax: Usage: MJ2_Wrapper source_location mj2_filename\n");
printf("Example: MJ2_Wrapper input/input output.mj2\n");
return 1;
}
mj2file = fopen(argv[2], "wb");
if (!mj2file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
/*
configure the event callbacks (not required)
setting of each callback is optionnal
*/
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
/* get a MJ2 decompressor handle */
cinfo = mj2_create_compress();
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
/* setup the decoder encoding parameters using user parameters */
movie = (opj_mj2_t*) cinfo->mj2_handle;
mj2_setup_encoder(cinfo->mj2_handle, &parameters);
/* Writing JP, FTYP and MDAT boxes
Assuming that the JP and FTYP boxes won't be longer than 300 bytes */
buf = (char*) malloc (300 * sizeof(char));
cio = opj_cio_open(movie->cinfo, buf, 300);
mj2_write_jp(cio);
mj2_write_ftyp(movie, cio);
mdat_initpos = cio_tell(cio);
cio_skip(cio, 4);
cio_write(cio,MJ2_MDAT, 4);
fwrite(buf,cio_tell(cio),1,mj2file);
free(buf);
// Insert each j2k codestream in a JP2C box
snum=0;
offset = 0;
while(1)
{
sample = &movie->tk[0].sample[snum];
sprintf(j2kfilename,"%s_%05d.j2k",argv[1],snum);
j2kfile = fopen(j2kfilename, "rb");
if (!j2kfile) {
if (snum==0) { // Could not open a single codestream
fprintf(stderr, "failed to open %s for reading\n",j2kfilename);
return 1;
}
else { // Tried to open a inexistant codestream
fprintf(stdout,"%d frames are being added to the MJ2 file\n",snum);
break;
}
}
// Calculating offset for samples and chunks
offset += cio_tell(cio);
sample->offset = offset;
movie->tk[0].chunk[snum].offset = offset; // There will be one sample per chunk
// Calculating sample size
fseek(j2kfile,0,SEEK_END);
sample->sample_size = ftell(j2kfile) + 8; // Sample size is codestream + JP2C box header
fseek(j2kfile,0,SEEK_SET);
// Reading siz marker of j2k image for the first codestream
if (snum==0)
read_siz_marker(j2kfile, &img);
// Writing JP2C box header
frame_codestream = (unsigned char*) malloc (sample->sample_size+8);
cio = opj_cio_open(movie->cinfo, frame_codestream, sample->sample_size);
cio_write(cio,sample->sample_size, 4); // Sample size
cio_write(cio,JP2_JP2C, 4); // JP2C
// Writing codestream from J2K file to MJ2 file
fread(frame_codestream+8,sample->sample_size-8,1,j2kfile);
fwrite(frame_codestream,sample->sample_size,1,mj2file);
cio_skip(cio, sample->sample_size-8);
// Ending loop
fclose(j2kfile);
snum++;
movie->tk[0].sample = realloc(movie->tk[0].sample, (snum+1) * sizeof(mj2_sample_t));
movie->tk[0].chunk = realloc(movie->tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t));
free(frame_codestream);
}
// Writing the MDAT box length in header
offset += cio_tell(cio);
buf = (char*) malloc (4 * sizeof(char));
cio = opj_cio_open(movie->cinfo, buf, 4);
cio_write(cio,offset-mdat_initpos,4);
fseek(mj2file,(long)mdat_initpos,SEEK_SET);
fwrite(buf,4,1,mj2file);
fseek(mj2file,0,SEEK_END);
free(buf);
// Setting movie parameters
movie->tk[0].num_samples=snum;
movie->tk[0].num_chunks=snum;
setparams(movie, &img);
// Writing MOOV box
buf = (char*) malloc ((TEMP_BUF+snum*20) * sizeof(char));
cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+snum*20));
mj2_write_moov(movie, cio);
fwrite(buf,cio_tell(cio),1,mj2file);
// Ending program
fclose(mj2file);
free(img.comps);
opj_cio_close(cio);
mj2_destroy_compress(movie);
return 0;
}