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
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);
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;
/*@{*/
/* ----------------------------------------------------------------------- */
/**
+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
# PROP Use_Debug_Libraries 0\r
# PROP Output_Dir "Release"\r
# PROP Intermediate_Dir "Release"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
-# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
+# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /YX /FD /c\r
# ADD BASE RSC /l 0x809 /d "NDEBUG"\r
# ADD RSC /l 0x809 /d "NDEBUG"\r
BSC32=bscmake.exe\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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"\r
+# SUBTRACT LINK32 /pdb:none\r
\r
!ELSEIF "$(CFG)" == "MJ2_Extractor - Win32 Debug"\r
\r
# PROP Use_Debug_Libraries 1\r
# PROP Output_Dir "MJ2_Extractor___Win32_Debug"\r
# PROP Intermediate_Dir "MJ2_Extractor___Win32_Debug"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
-# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
+# 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\r
# ADD BASE RSC /l 0x809 /d "_DEBUG"\r
# ADD RSC /l 0x809 /d "_DEBUG"\r
BSC32=bscmake.exe\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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\r
+# SUBTRACT LINK32 /pdb:none\r
\r
!ENDIF \r
\r
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\cio.c\r
+SOURCE=.\extract_j2k_from_mj2.c\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=.\extract_j2k_from_mj2.c\r
+SOURCE=.\mj2.c\r
# End Source File\r
+# End Group\r
+# Begin Group "Header Files"\r
+\r
+# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jp2.c\r
+SOURCE=.\mj2.h\r
# End Source File\r
+# End Group\r
+# Begin Group "OpenJPEG Header Files"\r
+\r
+# PROP Default_Filter ""\r
# Begin Source File\r
\r
-SOURCE=.\mj2.c\r
+SOURCE=..\libopenjpeg\bio.h\r
# End Source File\r
-# End Group\r
-# Begin Group "Header Files"\r
+# Begin Source File\r
\r
-# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
+SOURCE=..\libopenjpeg\cio.h\r
+# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\cio.h\r
+SOURCE=..\libopenjpeg\dwt.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\j2k.h\r
+SOURCE=..\libopenjpeg\event.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jp2.h\r
+SOURCE=..\libopenjpeg\fix.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=.\mj2.h\r
+SOURCE=..\libopenjpeg\image.h\r
# End Source File\r
-# End Group\r
-# Begin Group "Resource Files"\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\int.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\j2k.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\j2k_lib.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\jp2.h\r
+# End Source File\r
+# Begin Source File\r
\r
-# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"\r
+SOURCE=..\libopenjpeg\jpt.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\mct.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\mqc.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\openjpeg.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\opj_includes.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\pi.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\raw.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\t1.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\t2.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\tcd.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\tgt.h\r
+# End Source File\r
# End Group\r
# End Target\r
# End Project\r
--- /dev/null
+Microsoft Developer Studio Workspace File, Format Version 6.00\r
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r
+\r
+###############################################################################\r
+\r
+Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Project: "MJ2_Extractor"=".\MJ2_Extractor.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+ Begin Project Dependency\r
+ Project_Dep_Name LibOpenJPEG\r
+ End Project Dependency\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Global:\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<3>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
# PROP Use_Debug_Libraries 0\r
# PROP Output_Dir "Release"\r
# PROP Intermediate_Dir "Release"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
-# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /c\r
# ADD BASE RSC /l 0x809 /d "NDEBUG"\r
# ADD RSC /l 0x809 /d "NDEBUG"\r
BSC32=bscmake.exe\r
# PROP Use_Debug_Libraries 1\r
# PROP Output_Dir "MJ2_Wrapper___Win32_Debug"\r
# PROP Intermediate_Dir "MJ2_Wrapper___Win32_Debug"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
-# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
+# 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\r
# ADD BASE RSC /l 0x809 /d "_DEBUG"\r
# ADD RSC /l 0x809 /d "_DEBUG"\r
BSC32=bscmake.exe\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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\r
+# SUBTRACT LINK32 /pdb:none\r
\r
!ENDIF \r
\r
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\cio.c\r
+SOURCE=.\mj2.c\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\int.c\r
+SOURCE=.\wrap_j2k_in_mj2.c\r
# End Source File\r
+# End Group\r
+# Begin Group "Header Files"\r
+\r
+# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jp2.c\r
+SOURCE=.\mj2.h\r
# End Source File\r
+# End Group\r
+# Begin Group "OpenJPEG Header Files"\r
+\r
+# PROP Default_Filter ""\r
# Begin Source File\r
\r
-SOURCE=.\mj2.c\r
+SOURCE=..\libopenjpeg\bio.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=.\wrap_j2k_in_mj2.c\r
+SOURCE=..\libopenjpeg\cio.h\r
# End Source File\r
-# End Group\r
-# Begin Group "Header Files"\r
+# Begin Source File\r
\r
-# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
+SOURCE=..\libopenjpeg\dwt.h\r
+# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\cio.h\r
+SOURCE=..\libopenjpeg\event.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\int.h\r
+SOURCE=..\libopenjpeg\fix.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\j2k.h\r
+SOURCE=..\libopenjpeg\image.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jp2.h\r
+SOURCE=..\libopenjpeg\int.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=.\mj2.h\r
+SOURCE=..\libopenjpeg\j2k.h\r
# End Source File\r
-# End Group\r
-# Begin Group "Resource Files"\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\j2k_lib.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\jp2.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\jpt.h\r
+# End Source File\r
+# Begin Source File\r
\r
-# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"\r
+SOURCE=..\libopenjpeg\mct.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\mqc.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\openjpeg.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\opj_includes.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\pi.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\raw.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\t1.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\t2.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\tcd.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=..\libopenjpeg\tgt.h\r
+# End Source File\r
# End Group\r
# End Target\r
# End Project\r
--- /dev/null
+Microsoft Developer Studio Workspace File, Format Version 6.00\r
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r
+\r
+###############################################################################\r
+\r
+Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Project: "MJ2_Wrapper"=".\MJ2_Wrapper.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+ Begin Project Dependency\r
+ Project_Dep_Name LibOpenJPEG\r
+ End Project Dependency\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Global:\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<3>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+++ /dev/null
-#
-# 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)/*
\ No newline at end of file
+++ /dev/null
-#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;
-}
\ No newline at end of file
+++ /dev/null
-/*
-* 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;
-}
-
-
+++ /dev/null
-/*
-* 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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <math.h>
-
-#include "mj2.h"
-#include <cio.h>
-
-#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
-
-/*
-*
-* Free movie structure memory
-*
-*/
-void mj2_memory_free(mj2_movie_t * movie)
-{
- int i;
- mj2_tk_t *tk=NULL;
-
- if (movie->num_cl != 0)
- free(movie->cl);
-
- for (i = 0; i < movie->num_vtk + movie->num_stk + movie->num_htk; i++) {
- tk = &movie->tk[i];
- if (tk->name_size != 0)
- free(tk->name);
- if (tk->track_type == 0) {// Video track
- if (tk->jp2_struct.comps != 0)
- free(tk->jp2_struct.comps);
- if (tk->jp2_struct.cl != 0)
- free(tk->jp2_struct.cl);
- if (tk->num_jp2x != 0)
- free(tk->jp2xdata);
-
- }
- if (tk->num_url != 0)
- free(tk->url);
- if (tk->num_urn != 0)
- free(tk->urn);
- if (tk->num_br != 0)
- free(tk->br);
- if (tk->num_tts != 0)
- free(tk->tts);
- if (tk->num_chunks != 0)
- free(tk->chunk);
- if (tk->num_samplestochunk != 0)
- free(tk->sampletochunk);
- if (tk->num_samples != 0)
- free(tk->sample);
- }
-
- free(movie->tk);
-}
-
-/*
-*
-* Read box headers
-*
-*/
-
-int mj2_read_boxhdr(mj2_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 Movie, given a simple movie structure defined by the user
-* The movie will have one sample per chunk
-*
-* Arguments: mj2_movie_t * movie
-* Several variables of "movie" must be defined in order to enable a correct execution of
-* this function:
-* - The number of tracks of each type (movie->num_vtk, movie->num_stk, movie->num_htk)
-* - The memory for each must be allocated (movie->tk)
-* - For each track:
-* The track type (tk->track_type)
-* The number of sample (tk->num_samples)
-* The sample rate (tk->sample_rate)
-*
-*/
-
-int mj2_init_stdmovie(mj2_movie_t * movie)
-{
- int i;
- unsigned int j;
- time_t ltime;
-
- movie->brand = MJ2_MJ2;
- movie->minversion = 0;
- movie->num_cl = 2;
- movie->cl =
- (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
-
- movie->cl[0] = MJ2_MJ2;
- movie->cl[1] = MJ2_MJ2S;
- time(<ime); /* Time since 1/1/70 */
- movie->creation_time = (unsigned int) ltime + 2082844800; /* Seconds between 1/1/04 and 1/1/70 */
- movie->timescale = 1000;
-
- movie->rate = 1 << 16; /* Rate to play presentation (default = 0x00010000) */
- movie->volume = 1 << 8; /* Movie volume (default = 0x0100) */
- movie->trans_matrix[0] = 0x00010000; /* Transformation matrix for video */
- movie->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
- movie->trans_matrix[2] = 0;
- movie->trans_matrix[3] = 0;
- movie->trans_matrix[4] = 0x00010000;
- movie->trans_matrix[5] = 0;
- movie->trans_matrix[6] = 0;
- movie->trans_matrix[7] = 0;
- movie->trans_matrix[8] = 0x40000000;
- movie->next_tk_id = 1;
-
- for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
- mj2_tk_t *tk = &movie->tk[i];
- movie->next_tk_id++;
- tk->jp2_struct.comps = NULL;
- tk->jp2_struct.cl = NULL;
-
- if (tk->track_type == 0) {
- if (tk->num_samples == 0)
- return 1;
-
- tk->Dim[0] = 0;
- tk->Dim[1] = 0;
-
- tk->timescale = 1000; /* Timescale = 1 ms */
-
- tk->chunk[0].num_samples = 1;
- tk->chunk[0].sample_descr_idx = 1;
-
- tk->same_sample_size = 0;
-
- tk->num_samplestochunk = 1; /* One sample per chunk */
- tk->sampletochunk =
- (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
- sizeof(mj2_sampletochunk_t));
- tk->sampletochunk[0].first_chunk = 1;
- tk->sampletochunk[0].samples_per_chunk = 1;
- tk->sampletochunk[0].sample_descr_idx = 1;
-
- if (tk->sample_rate == 0) {
- fprintf(stderr,"Error while initializing MJ2 movie: Sample rate of track %d must be different from zero\n",tk->track_ID);
- return 1;
- }
-
- for (j = 0; j < tk->num_samples; j++) {
- tk->sample[j].sample_delta = tk->timescale / tk->sample_rate;
- }
-
- tk->num_tts = 1;
- tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
- tk->tts[0].sample_count = tk->num_samples;
- tk->tts[0].sample_delta = tk->timescale / tk->sample_rate;
-
- tk->horizresolution = 0x00480000; /* Horizontal resolution (typically 72) */
- tk->vertresolution = 0x00480000; /* Vertical resolution (typically 72) */
- tk->compressorname[0] = 0x0f4d6f74; /* Compressor Name[]: Motion JPEG2000 */
- tk->compressorname[1] = 0x696f6e20;
- tk->compressorname[2] = 0x4a504547;
- tk->compressorname[3] = 0x32303030;
- tk->compressorname[4] = 0x00120000;
- tk->compressorname[5] = 0;
- tk->compressorname[6] = 0x00000042;
- tk->compressorname[7] = 0x000000DC;
- tk->num_url = 0; /* Number of URL */
- tk->num_urn = 0; /* Number of URN */
- tk->graphicsmode = 0; /* Graphicsmode */
- tk->opcolor[0] = 0; /* OpColor */
- tk->opcolor[1] = 0; /* OpColor */
- tk->opcolor[2] = 0; /* OpColor */
- tk->creation_time = movie->creation_time; /* Seconds between 1/1/04 and 1/1/70 */
- tk->language = 0; /* Language (undefined) */
- tk->layer = 0;
- tk->volume = 1 << 8; /* Movie volume (default = 0x0100) */
- tk->trans_matrix[0] = 0x00010000; /* Transformation matrix for track */
- tk->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
- tk->trans_matrix[2] = 0;
- tk->trans_matrix[3] = 0;
- tk->trans_matrix[4] = 0x00010000;
- tk->trans_matrix[5] = 0;
- tk->trans_matrix[6] = 0;
- tk->trans_matrix[7] = 0;
- tk->trans_matrix[8] = 0x40000000;
- tk->fieldcount = 1;
- tk->fieldorder = 0;
- tk->or_fieldcount = 1;
- tk->or_fieldorder = 0;
- tk->num_br = 2;
- tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
- tk->br[0] = MJ2_JP2;
- tk->br[1] = MJ2_J2P0;
- tk->num_jp2x = 0;
- tk->hsub = 2; /* 4:2:0 */
- tk->vsub = 2; /* 4:2:0 */
- tk->hoff = 0;
- tk->voff = 0;
- tk->visual_w = tk->w << 16;
- tk->visual_h = tk->h << 16;
- }
- else {
- tk->num_br = 0;
- tk->jp2xdata = NULL;
- }
- }
- return 0;
-}
-
-/*
-* Time To Sample box Decompact
-*
-*/
-void mj2_tts_decompact(mj2_tk_t * tk)
-{
- int i, j;
- tk->num_samples = 0;
- for (i = 0; i < tk->num_tts; i++) {
- tk->num_samples += tk->tts[i].sample_count;
- }
-
- tk->sample =
- (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
-
- for (i = 0; i < tk->num_tts; i++) {
- for (j = 0; j < tk->tts[i].sample_count; j++) {
- tk->sample[j].sample_delta = tk->tts[i].sample_delta;
- }
- }
-}
-
-/*
-* Sample To Chunk box Decompact
-*
-*/
-void mj2_stsc_decompact(mj2_tk_t * tk)
-{
- int j, i;
- unsigned int k;
- int sampleno=0;
-
- if (tk->num_samplestochunk == 1) {
- tk->num_chunks =
- (unsigned int) ceil((double) tk->num_samples /
- (double) tk->sampletochunk[0].samples_per_chunk);
- tk->chunk =
- (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
- for (k = 0; k < tk->num_chunks; k++) {
- tk->chunk[k].num_samples = tk->sampletochunk[0].samples_per_chunk;
- }
-
- } else {
- tk->chunk =
- (mj2_chunk_t *) malloc(tk->num_samples * sizeof(mj2_chunk_t));
- tk->num_chunks = 0;
- for (i = 0; i < tk->num_samplestochunk -1 ; i++) {
- for (j = tk->sampletochunk[i].first_chunk - 1;
- j < tk->sampletochunk[i + 1].first_chunk - 1; j++) {
- tk->chunk[j].num_samples = tk->sampletochunk[i].samples_per_chunk;
- tk->num_chunks++;
- sampleno += tk->chunk[j].num_samples;
- }
- }
- tk->num_chunks += (int)(tk->num_samples - sampleno) / tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
- for (k = tk->sampletochunk[tk->num_samplestochunk - 1].first_chunk - 1;
- k < tk->num_chunks; k++) {
- tk->chunk[k].num_samples =
- tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
- }
- tk->chunk = realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
- }
-
-}
-
-
-/*
-* Chunk offset box Decompact
-*
-*/
-void mj2_stco_decompact(mj2_tk_t * tk)
-{
- int j;
- unsigned int i;
- int k = 0;
- int intra_chunk_offset;
-
- for (i = 0; i < tk->num_chunks; i++) {
- intra_chunk_offset = 0;
- for (j = 0; j < tk->chunk[i].num_samples; j++) {
- tk->sample[k].offset = intra_chunk_offset + tk->chunk[i].offset;
- intra_chunk_offset += tk->sample[k].sample_size;
- k++;
- }
- }
-}
-
-/*
-* Write the JP box
-*
-* JP Signature box
-*
-*/
-void mj2_write_jp()
-{
- mj2_box_t box;
- box.init_pos = cio_tell();
- cio_skip(4);
-
- cio_write(MJ2_JP, 4); /* JP */
- cio_write(0x0d0a870a, 4); /* 0x0d0a870a required in a JP box */
-
- box.length = cio_tell() - box.init_pos;
- cio_seek(box.init_pos);
- cio_write(box.length, 4);
- cio_seek(box.init_pos + box.length);
-}
-
-/*
-* Read the JP box
-*
-* JPEG 2000 signature
-*
-*/
-int mj2_read_jp()
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_JP != box.type) { /* Check Marker */
- fprintf(stderr, "Error: Expected JP Marker\n");
- return 1;
- }
- if (0x0d0a870a != cio_read(4)) { /* read the 0x0d0a870a required in a JP box */
- fprintf(stderr, "Error with JP Marker\n");
- return 1;
- }
- if (cio_tell() - box.init_pos != box.length) { /* Check box length */
- fprintf(stderr, "Error with JP Box size \n");
- return 1;
- }
- return 0;
-
-}
-
-/*
-* Write the FTYP box
-*
-* File type box
-*
-*/
-void mj2_write_ftyp(mj2_movie_t * movie)
-{
- int i;
- mj2_box_t box;
- box.init_pos = cio_tell();
- cio_skip(4);
-
- cio_write(MJ2_FTYP, 4); /* FTYP */
- cio_write(movie->brand, 4); /* BR */
- cio_write(movie->minversion, 4); /* MinV */
-
- for (i = 0; i < movie->num_cl; i++)
- cio_write(movie->cl[i], 4); /* CL */
-
- box.length = cio_tell() - box.init_pos;
- cio_seek(box.init_pos);
- cio_write(box.length, 4); /* Length */
- cio_seek(box.init_pos + box.length);
-}
-
-/*
-* Read the FTYP box
-*
-* File type box
-*
-*/
-int mj2_read_ftyp(mj2_movie_t * movie)
-{
- int i;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box); /* Box Size */
- if (MJ2_FTYP != box.type) {
- fprintf(stderr, "Error: Expected FTYP Marker\n");
- return 1;
- }
-
- movie->brand = cio_read(4); /* BR */
- movie->minversion = cio_read(4); /* MinV */
- movie->num_cl = (box.length - 16) / 4;
- movie->cl =
- (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
-
- for (i = movie->num_cl - 1; i > -1; i--)
- movie->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;
-}
-
-
-/*
-* Write the STCO box
-*
-* Chunk Offset Box
-*
-*/
-void mj2_write_stco(mj2_tk_t * tk)
-{
- mj2_box_t box;
- unsigned int i;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_STCO, 4); /* STCO */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->num_chunks, 4); /* Entry Count */
-
- for (i = 0; i < tk->num_chunks; i++) {
- cio_write(tk->chunk[i].offset, 4); /* Entry offset */
- }
-
- 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 STCO box
-*
-* Chunk Offset Box
-*
-*/
-int mj2_read_stco(mj2_tk_t * tk)
-{
- unsigned int i;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box); /* Box Size */
- if (MJ2_STCO != box.type) {
- fprintf(stderr, "Error: Expected STCO Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in STCO box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in STCO box. Expected flag 0\n");
- return 1;
- }
-
-
- if (cio_read(4) != tk->num_chunks) {
- fprintf(stderr,
- "Error in STCO box: expecting same amount of entry-count as chunks \n");
- } else {
- for (i = 0; i < tk->num_chunks; i++) {
- tk->chunk[i].offset = cio_read(4); /* Entry offset */
- }
- }
-
- mj2_stco_decompact(tk);
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with STCO Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the STSZ box
-*
-* Sample size box
-*
-*/
-void mj2_write_stsz(mj2_tk_t * tk)
-{
- mj2_box_t box;
- unsigned int i;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_STSZ, 4); /* STSZ */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- if (tk->same_sample_size == 1) { /* If they all have the same size */
- cio_write(tk->sample[0].sample_size, 4); /* Size */
-
- cio_write(1, 4); /* Entry count = 1 */
- }
-
- else {
- cio_write(0, 4); /* Sample Size = 0 becase they all have different sizes */
-
- cio_write(tk->num_samples, 4); /* Sample Count */
-
- for (i = 0; i < tk->num_samples; i++) {
- cio_write(tk->sample[i].sample_size, 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 STSZ box
-*
-* Sample size box
-*
-*/
-int mj2_read_stsz(mj2_tk_t * tk)
-{
- int sample_size;
- unsigned int i;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box); /* Box Size */
- if (MJ2_STSZ != box.type) {
- fprintf(stderr, "Error: Expected STSZ Marker\n");
- return 1;
- }
-
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in STSZ box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in STSZ box. Expected flag 0\n");
- return 1;
- }
-
- sample_size = cio_read(4);
-
- if (sample_size != 0) { /* Samples do have the same size */
- tk->same_sample_size = 1;
- for (i = 0; i < tk->num_samples; i++) {
- tk->sample[i].sample_size = sample_size;
- }
- cio_skip(4); /* Sample count = 1 */
- } else {
- tk->same_sample_size = 0;
- if (tk->num_samples != cio_read(4)) { /* Sample count */
- fprintf(stderr,
- "Error in STSZ box. Expected that sample-count is number of samples in track\n");
- return 1;
- }
- for (i = 0; i < tk->num_samples; i++) {
- tk->sample[i].sample_size = cio_read(4); /* Sample Size */
- }
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with STSZ Box size\n");
- return 1;
- }
- }
- return 0;
-
-}
-
-/*
-* Write the STSC box
-*
-* Sample to Chunk
-*
-*/
-void mj2_write_stsc(mj2_tk_t * tk)
-{
- int i;
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_STSC, 4); /* STSC */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->num_samplestochunk, 4); /* Entry Count */
-
- for (i = 0; i < tk->num_samplestochunk; i++) {
- cio_write(tk->sampletochunk[i].first_chunk, 4); /* First Chunk */
- cio_write(tk->sampletochunk[i].samples_per_chunk, 4); /* Samples per chunk */
- cio_write(tk->sampletochunk[i].sample_descr_idx, 4); /* Samples description index */
- }
-
-
- 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 STSC box
-*
-* Sample to Chunk
-*
-*/
-int mj2_read_stsc(mj2_tk_t * tk)
-{
- int i;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box); /* Box Size */
- if (MJ2_STSC != box.type) {
- fprintf(stderr, "Error: Expected STSC Marker\n");
- return 1;
- }
-
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in STSC box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in STSC box. Expected flag 0\n");
- return 1;
- }
-
- tk->num_samplestochunk = cio_read(4);
-
- tk->sampletochunk =
- (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
- sizeof(mj2_sampletochunk_t));
-
-
- for (i = 0; i < tk->num_samplestochunk; i++) {
- tk->sampletochunk[i].first_chunk = cio_read(4);
- tk->sampletochunk[i].samples_per_chunk = cio_read(4);
- tk->sampletochunk[i].sample_descr_idx = cio_read(4);
- }
-
- mj2_stsc_decompact(tk); /* decompact sample to chunk box */
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with STSC Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the STTS box
-*
-* Time to Sample Box
-*
-*/
-void mj2_write_stts(mj2_tk_t * tk)
-{
-
- int i;
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_STTS, 4); /* STTS */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->num_tts, 4); /* entry_count */
- for (i = 0; i < tk->num_tts; i++) {
- cio_write(tk->tts[i].sample_count, 4); /* Sample-count */
- cio_write(tk->tts[i].sample_delta, 4); /* Sample-Delta */
- }
-
- 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 STTS box
-*
-*
-*
-*/
-int mj2_read_stts(mj2_tk_t * tk)
-{
- int i;
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_STTS != box.type) {
- fprintf(stderr, "Error: Expected STTS Marker\n");
- return 1;
- }
-
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in STTS box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in STTS box. Expected flag 0\n");
- return 1;
- }
-
- tk->num_tts = cio_read(4);
-
- tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
-
- for (i = 0; i < tk->num_tts; i++) {
- tk->tts[i].sample_count = cio_read(4);
- tk->tts[i].sample_delta = cio_read(4);
- }
-
- mj2_tts_decompact(tk);
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with STTS Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the FIEL box
-*
-* Field coding Box
-*
-*/
-void mj2_write_fiel(mj2_tk_t * tk)
-{
-
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_FIEL, 4); /* STTS */
-
- cio_write(tk->fieldcount, 1); /* Field count */
- cio_write(tk->fieldorder, 1); /* Field order */
-
-
- 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 FIEL box
-*
-* Field coding Box
-*
-*/
-int mj2_read_fiel(mj2_tk_t * tk)
-{
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_FIEL != box.type) {
- fprintf(stderr, "Error: Expected FIEL Marker\n");
- return 1;
- }
-
-
- tk->fieldcount = cio_read(1);
- tk->fieldorder = cio_read(1);
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with FIEL Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the ORFO box
-*
-* Original Format Box
-*
-*/
-void mj2_write_orfo(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_ORFO, 4);
-
- cio_write(tk->or_fieldcount, 1); /* Original Field count */
- cio_write(tk->or_fieldorder, 1); /* Original Field order */
-
-
- 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 ORFO box
-*
-* Original Format Box
-*
-*/
-int mj2_read_orfo(mj2_tk_t * tk)
-{
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_ORFO != box.type) {
- fprintf(stderr, "Error: Expected ORFO Marker\n");
- return 1;
- }
-
-
- tk->or_fieldcount = cio_read(1);
- tk->or_fieldorder = cio_read(1);
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with ORFO Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the JP2P box
-*
-* MJP2 Profile Box
-*
-*/
-void mj2_write_jp2p(mj2_tk_t * tk)
-{
-
- int i;
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_JP2P, 4);
-
- cio_write(0, 4); /* Version 0, flags =0 */
-
- for (i = 0; i < tk->num_br; i++) {
- cio_write(tk->br[i], 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 JP2P box
-*
-* MJP2 Profile Box
-*
-*/
-int mj2_read_jp2p(mj2_tk_t * tk)
-{
- int i;
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_JP2P != box.type) {
- fprintf(stderr, "Error: Expected JP2P Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in JP2P box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in JP2P box. Expected flag 0\n");
- return 1;
- }
-
-
- tk->num_br = (box.length - 12) / 4;
- tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
-
- for (i = 0; i < tk->num_br; i++) {
- tk->br[i] = cio_read(4);
- }
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with JP2P Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the JP2X box
-*
-* MJP2 Prefix Box
-*
-*/
-void mj2_write_jp2x(mj2_tk_t * tk)
-{
-
- int i;
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_JP2X, 4);
-
- for (i = 0; i < tk->num_jp2x; i++) {
- cio_write(tk->jp2xdata[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 JP2X box
-*
-* MJP2 Prefix Box
-*
-*/
-int mj2_read_jp2x(mj2_tk_t * tk)
-{
- unsigned int i;
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_JP2X != box.type) {
- fprintf(stderr, "Error: Expected JP2X Marker\n");
- return 1;
- }
-
-
- tk->num_jp2x = (box.length - 8);
- tk->jp2xdata =
- (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
-
- for (i = 0; i < tk->num_jp2x; i++) {
- tk->jp2xdata[i] = cio_read(1);
- }
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with JP2X Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the JSUB box
-*
-* MJP2 Subsampling Box
-*
-*/
-void mj2_write_jsub(mj2_tk_t * tk)
-{
-
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_JSUB, 4);
-
- cio_write(tk->hsub, 1);
- cio_write(tk->vsub, 1);
- cio_write(tk->hoff, 1);
- cio_write(tk->voff, 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 JSUB box
-*
-* MJP2 Subsampling Box
-*
-*/
-int mj2_read_jsub(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_JSUB != box.type) {
- fprintf(stderr, "Error: Expected JSUB Marker\n");
- return 1;
- }
-
- tk->hsub = cio_read(1);
- tk->vsub = cio_read(1);
- tk->hoff = cio_read(1);;
- tk->voff = cio_read(1);
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with JSUB Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the SMJ2 box
-*
-* Visual Sample Entry Description
-*
-*/
-void mj2_write_smj2(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MJ2, 4); /* MJ2 */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(1, 4);
-
- cio_write(0, 2); /* Pre-defined */
-
- cio_write(0, 2); /* Reserved */
-
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
-
- cio_write(tk->w, 2); /* Width */
- cio_write(tk->h, 2); /* Height */
-
- cio_write(tk->horizresolution, 4); /* Horizontal resolution */
- cio_write(tk->vertresolution, 4); /* Vertical resolution */
-
- cio_write(0, 4); /* Reserved */
-
- cio_write(1, 2); /* Pre-defined = 1 */
-
- cio_write(tk->compressorname[0], 4); /* Compressor Name */
- cio_write(tk->compressorname[1], 4);
- cio_write(tk->compressorname[2], 4);
- cio_write(tk->compressorname[3], 4);
- cio_write(tk->compressorname[4], 4);
- cio_write(tk->compressorname[5], 4);
- cio_write(tk->compressorname[6], 4);
- cio_write(tk->compressorname[7], 4);
-
- cio_write(tk->depth, 2); /* Depth */
-
- cio_write(0xffff, 2); /* Pre-defined = -1 */
-
- jp2_write_jp2h(&tk->jp2_struct);
-
- mj2_write_fiel(tk);
-
- if (tk->num_br != 0)
- mj2_write_jp2p(tk);
- if (tk->num_jp2x != 0)
- mj2_write_jp2x(tk);
-
- mj2_write_jsub(tk);
- mj2_write_orfo(tk);
-
- 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 SMJ2 box
-*
-* Visual Sample Entry Description
-*
-*/
-int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk)
-{
- mj2_box_t box;
- mj2_box_t box2;
- int i;
-
- mj2_read_boxhdr(&box);
-
- if (MJ2_MJ2 != box.type) {
- fprintf(stderr, "Error in SMJ2 box: Expected MJ2 Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in MJP2 box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in MJP2 box. Expected flag 0\n");
- return 1;
- }
-
- cio_skip(4);
-
- cio_skip(2); /* Pre-defined */
-
- cio_skip(2); /* Reserved */
-
- cio_skip(4); /* Pre-defined */
- cio_skip(4); /* Pre-defined */
- cio_skip(4); /* Pre-defined */
-
- tk->w = cio_read(2); /* Width */
- tk->h = cio_read(2); /* Height */
-
- tk->horizresolution = cio_read(4); /* Horizontal resolution */
- tk->vertresolution = cio_read(4); /* Vertical resolution */
-
- cio_skip(4); /* Reserved */
-
- cio_skip(2); /* Pre-defined = 1 */
-
- tk->compressorname[0] = cio_read(4); /* Compressor Name */
- tk->compressorname[1] = cio_read(4);
- tk->compressorname[2] = cio_read(4);
- tk->compressorname[3] = cio_read(4);
- tk->compressorname[4] = cio_read(4);
- tk->compressorname[5] = cio_read(4);
- tk->compressorname[6] = cio_read(4);
- tk->compressorname[7] = cio_read(4);
-
- tk->depth = cio_read(2); /* Depth */
-
- /* Init std value */
- tk->num_jp2x = 0;
- tk->fieldcount = 1;
- tk->fieldorder = 0;
- tk->or_fieldcount = 1;
- tk->or_fieldorder = 0;
-
- cio_skip(2); /* Pre-defined = -1 */
-
- if (jp2_read_jp2h(&tk->jp2_struct)) {
- fprintf(stderr, "Error with JP2H Box\n");
- return 1;
- }
-
- tk->jp2_struct.comps = (jp2_comps_t *) malloc(tk->jp2_struct.numcomps * sizeof(jp2_comps_t));
- tk->jp2_struct.cl = (int *) malloc(sizeof(int));
-
- tk->num_br = 0;
- tk->num_jp2x = 0;
-
- for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
- mj2_read_boxhdr(&box2);
- cio_seek(box2.init_pos);
- switch (box2.type) {
- case MJ2_FIEL:
- if (mj2_read_fiel(tk))
- return 1;
- break;
-
- case MJ2_JP2P:
- if (mj2_read_jp2p(tk))
- return 1;
- break;
-
- case MJ2_JP2X:
- if (mj2_read_jp2x(tk))
- return 1;
- break;
-
- case MJ2_JSUB:
- if (mj2_read_jsub(tk))
- return 1;
- break;
-
- case MJ2_ORFO:
- if (mj2_read_orfo(tk))
- return 1;
- break;
-
- default:
- fprintf(stderr, "Error with MJP2 Box size\n");
- return 1;
- break;
-
- }
- }
- return 0;
-}
-
-
-/*
-* Write the STSD box
-*
-* Sample Description
-*
-*/
-void mj2_write_stsd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_STSD, 4); /* STSD */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */
-
- if (tk->track_type == 0) {
- mj2_write_smj2(tk);
- } else if (tk->track_type == 1) {
- // Not implemented
- }
- if (tk->track_type == 2) {
- // Not implemented
- }
-
-
- 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 STSD box
-*
-* Sample Description
-*
-*/
-int mj2_read_stsd(mj2_tk_t * tk, j2k_image_t * img)
-{
- int i;
- int entry_count, len_2skip;
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
-
- if (MJ2_STSD != box.type) {
- fprintf(stderr, "Error: Expected STSD Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in STSD box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in STSD box. Expected flag 0\n");
- return 1;
- }
-
- entry_count = cio_read(4);
-
- if (tk->track_type == 0) {
- for (i = 0; i < entry_count; i++) {
- if (mj2_read_smj2(img, tk))
- return 1;
- }
- } else if (tk->track_type == 1) {
- len_2skip = cio_read(4); // Not implemented -> skipping box
- cio_skip(len_2skip - 4);
- } else if (tk->track_type == 2) {
- len_2skip = cio_read(4); // Not implemented -> skipping box
- cio_skip(len_2skip - 4);
- }
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with STSD Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the STBL box
-*
-* Sample table box box
-*
-*/
-void mj2_write_stbl(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_STBL, 4); /* STBL */
-
- mj2_write_stsd(tk);
- mj2_write_stts(tk);
- mj2_write_stsc(tk);
- mj2_write_stsz(tk);
- mj2_write_stco(tk);
-
- 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 STBL box
-*
-* Sample table box box
-*
-*/
-int mj2_read_stbl(mj2_tk_t * tk, j2k_image_t * img)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_STBL != box.type) {
- fprintf(stderr, "Error: Expected STBL Marker\n");
- return 1;
- }
-
- if (mj2_read_stsd(tk, img))
- return 1;
- if (mj2_read_stts(tk))
- return 1;
- if (mj2_read_stsc(tk))
- return 1;
- if (mj2_read_stsz(tk))
- return 1;
- if (mj2_read_stco(tk))
- return 1;
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with STBL Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the URL box
-*
-* URL box
-*
-*/
-void mj2_write_url(mj2_tk_t * tk, int url_num)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_URL, 4); /* URL */
-
- if (url_num == 0)
- cio_write(1, 4); /* Version = 0, flags = 1 because stored in same file */
- else {
- cio_write(0, 4); /* Version = 0, flags = 0 */
- cio_write(tk->url[url_num - 1].location[0], 4);
- cio_write(tk->url[url_num - 1].location[1], 4);
- cio_write(tk->url[url_num - 1].location[2], 4);
- cio_write(tk->url[url_num - 1].location[3], 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 URL box
-*
-* URL box
-*
-*/
-int mj2_read_url(mj2_tk_t * tk, int urn_num)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_URL != box.type) {
- fprintf(stderr, "Error: Expected URL Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in URL box\n");
- return 1;
- }
-
- if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
- tk->url[urn_num].location[0] = cio_read(4);
- tk->url[urn_num].location[1] = cio_read(4);
- tk->url[urn_num].location[2] = cio_read(4);
- tk->url[urn_num].location[3] = cio_read(4);
- } else {
- tk->num_url--;
- }
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with URL Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the URN box
-*
-* URN box
-*
-*/
-void mj2_write_urn(mj2_tk_t * tk, int urn_num)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_URN, 4); /* URN */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->urn[urn_num].name[0], 4);
- cio_write(tk->urn[urn_num].name[1], 4);
- cio_write(tk->urn[urn_num].name[2], 4);
- cio_write(tk->urn[urn_num].name[3], 4);
- cio_write(tk->urn[urn_num].location[0], 4);
- cio_write(tk->urn[urn_num].location[1], 4);
- cio_write(tk->urn[urn_num].location[2], 4);
- cio_write(tk->urn[urn_num].location[3], 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 URN box
-*
-* URN box
-*
-*/
-int mj2_read_urn(mj2_tk_t * tk, int urn_num)
-{
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_URN != box.type) {
- fprintf(stderr, "Error: Expected URN Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in URN box\n");
- return 1;
- }
-
- if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
- tk->urn[urn_num].name[0] = cio_read(4);
- tk->urn[urn_num].name[1] = cio_read(4);
- tk->urn[urn_num].name[2] = cio_read(4);
- tk->urn[urn_num].name[3] = cio_read(4);
- tk->urn[urn_num].location[0] = cio_read(4);
- tk->urn[urn_num].location[1] = cio_read(4);
- tk->urn[urn_num].location[2] = cio_read(4);
- tk->urn[urn_num].location[3] = cio_read(4);
- }
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with URN Box size\n");
- return 1;
- }
- return 0;
-}
-
-
-/*
-* Write the DREF box
-*
-* Data reference box
-*
-*/
-void mj2_write_dref(mj2_tk_t * tk)
-{
- int i;
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_DREF, 4); /* DREF */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- if (tk->num_url + tk->num_urn == 0) { /* Media data in same file */
- cio_write(1, 4); /* entry_count = 1 */
- mj2_write_url(tk, 0);
- } else {
- cio_write(tk->num_url + tk->num_urn, 4); /* entry_count */
-
- for (i = 0; i < tk->num_url; i++)
- mj2_write_url(tk, i + 1);
-
- for (i = 0; i < tk->num_urn; i++)
- mj2_write_urn(tk, i);
- }
-
- 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 DREF box
-*
-* Data reference box
-*
-*/
-int mj2_read_dref(mj2_tk_t * tk)
-{
-
- int i;
- int entry_count, marker;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_DREF != box.type) {
- fprintf(stderr, "Error: Expected DREF Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in DREF box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in DREF box. Expected flag 0\n");
- return 1;
- }
-
- entry_count = cio_read(4);
- tk->num_url = 0;
- tk->num_urn = 0;
-
- for (i = 0; i < entry_count; i++) {
- cio_skip(4);
- marker = cio_read(4);
- if (marker == MJ2_URL) {
- cio_skip(-8);
- tk->num_url++;
- if (mj2_read_url(tk, tk->num_url))
- return 1;
- } else if (marker == MJ2_URN) {
- cio_skip(-8);
- tk->num_urn++;
- if (mj2_read_urn(tk, tk->num_urn))
- return 1;
- } else {
- fprintf(stderr, "Error with in DREF box. Expected URN or URL box\n");
- return 1;
- }
-
- }
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with DREF Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the DINF box
-*
-* Data information box
-*
-*/
-void mj2_write_dinf(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_DINF, 4); /* DINF */
-
- mj2_write_dref(tk);
-
- 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 DINF box
-*
-* Data information box
-*
-*/
-int mj2_read_dinf(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_DINF != box.type) {
- fprintf(stderr, "Error: Expected DINF Marker\n");
- return 1;
- }
-
- if (mj2_read_dref(tk))
- return 1;
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with DINF Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the VMHD box
-*
-* Video Media information box
-*
-*/
-void mj2_write_vmhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_VMHD, 4); /* VMHD */
-
- cio_write(1, 4); /* Version = 0, flags = 1 */
-
- cio_write(tk->graphicsmode, 2);
- cio_write(tk->opcolor[0], 2);
- cio_write(tk->opcolor[1], 2);
- cio_write(tk->opcolor[2], 2);
-
- 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 VMHD box
-*
-* Video Media information box
-*
-*/
-int mj2_read_vmhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_VMHD != box.type) {
- fprintf(stderr, "Error: Expected VMHD Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
- return 1;
- }
-
- if (1 != cio_read(3)) { /* Flags = 1 */
- fprintf(stderr, "Error with flag in VMHD box. Expected flag 1\n");
- return 1;
- }
-
- tk->track_type = 0;
- tk->graphicsmode = cio_read(2);
- tk->opcolor[0] = cio_read(2);
- tk->opcolor[1] = cio_read(2);
- tk->opcolor[2] = cio_read(2);
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with VMHD Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the SMHD box
-*
-* Sound Media information box
-*
-*/
-void mj2_write_smhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_SMHD, 4); /* SMHD */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->balance, 2);
-
- cio_write(0, 2); /* Reserved */
-
- 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 SMHD box
-*
-* Sound Media information box
-*
-*/
-int mj2_read_smhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_SMHD != box.type) {
- fprintf(stderr, "Error: Expected SMHD Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in SMHD box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in SMHD box. Expected flag 0\n");
- return 1;
- }
-
- tk->track_type = 1;
- tk->balance = cio_read(2);
-
- /* Init variables to zero to avoid problems when freeeing memory
- The values will possibly be overidded when decoding the track structure */
- tk->num_br = 0;
- tk->num_url = 0;
- tk->num_urn = 0;
- tk->num_chunks = 0;
- tk->num_tts = 0;
- tk->num_samplestochunk = 0;
- tk->num_samples = 0;
-
- cio_skip(2); /* Reserved */
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with SMHD Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the HMHD box
-*
-* Hint Media information box
-*
-*/
-void mj2_write_hmhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_HMHD, 4); /* HMHD */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->maxPDUsize, 2);
- cio_write(tk->avgPDUsize, 2);
- cio_write(tk->maxbitrate, 4);
- cio_write(tk->avgbitrate, 4);
- cio_write(tk->slidingavgbitrate, 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 HMHD box
-*
-* Hint Media information box
-*
-*/
-int mj2_read_hmhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_HMHD != box.type) {
- fprintf(stderr, "Error: Expected HMHD Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in HMHD box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in HMHD box. Expected flag 0\n");
- return 1;
- }
-
- tk->track_type = 2;
- tk->maxPDUsize = cio_read(2);
- tk->avgPDUsize = cio_read(2);
- tk->maxbitrate = cio_read(4);
- tk->avgbitrate = cio_read(4);
- tk->slidingavgbitrate = cio_read(4);
-
- /* Init variables to zero to avoid problems when freeeing memory
- The values will possibly be overidded when decoding the track structure */
- tk->num_br = 0;
- tk->num_url = 0;
- tk->num_urn = 0;
- tk->num_chunks = 0;
- tk->num_tts = 0;
- tk->num_samplestochunk = 0;
- tk->num_samples = 0;
-
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with HMHD Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the MINF box
-*
-* Media information box
-*
-*/
-void mj2_write_minf(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MINF, 4); /* MINF */
-
- if (tk->track_type == 0) {
- mj2_write_vmhd(tk);
- } else if (tk->track_type == 1) {
- mj2_write_smhd(tk);
- } else if (tk->track_type == 2) {
- mj2_write_hmhd(tk);
- }
-
- mj2_write_dinf(tk);
- mj2_write_stbl(tk);
-
- 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 MINF box
-*
-* Media information box
-*
-*/
-int mj2_read_minf(mj2_tk_t * tk, j2k_image_t * img)
-{
-
- unsigned int box_type;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_MINF != box.type) {
- fprintf(stderr, "Error: Expected MINF Marker\n");
- return 1;
- }
-
- cio_skip(4);
- box_type = cio_read(4);
- cio_skip(-8);
-
- if (box_type == MJ2_VMHD) {
- if (mj2_read_vmhd(tk))
- return 1;
- } else if (box_type == MJ2_SMHD) {
- if (mj2_read_smhd(tk))
- return 1;
- } else if (box_type == MJ2_HMHD) {
- if (mj2_read_hmhd(tk))
- return 1;
- } else {
- fprintf(stderr, "Error in MINF box expected vmhd, smhd or hmhd\n");
- return 1;
- }
-
- if (mj2_read_dinf(tk))
- return 1;
-
- if (mj2_read_stbl(tk, img))
- return 1;
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with MINF Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the HDLR box
-*
-* Handler reference box
-*
-*/
-void mj2_write_hdlr(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_HDLR, 4); /* HDLR */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(0, 4); /* Predefine */
-
- tk->name = 0; /* The track name is immediately determined by the track type */
-
- if (tk->track_type == 0) {
- tk->handler_type = 0x76696465; /* Handler type: vide */
- cio_write(tk->handler_type, 4);
-
- cio_write(0, 4);
- cio_write(0, 4);
- cio_write(0, 4); /* Reserved */
-
- cio_write(0x76696465, 4);
- cio_write(0x6F206d65, 4);
- cio_write(0x64696120, 4);
- cio_write(0x74726163, 4);
- cio_write(0x6b00, 2); /* String: video media track */
- } else if (tk->track_type == 1) {
- tk->handler_type = 0x736F756E; /* Handler type: soun */
- cio_write(tk->handler_type, 4);
-
- cio_write(0, 4);
- cio_write(0, 4);
- cio_write(0, 4); /* Reserved */
-
- cio_write(0x536F756E, 4);
- cio_write(0x6400, 2); /* String: Sound */
- } else if (tk->track_type == 2) {
- tk->handler_type = 0x68696E74; /* Handler type: hint */
- cio_write(tk->handler_type, 4);
-
- cio_write(0, 4);
- cio_write(0, 4);
- cio_write(0, 4); /* Reserved */
-
- cio_write(0x48696E74, 4);
- cio_write(0, 2); /* String: Hint */
- }
-
- 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 HDLR box
-*
-* Handler reference box
-*
-*/
-int mj2_read_hdlr(mj2_tk_t * tk)
-{
- int i;
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_HDLR != box.type) {
- fprintf(stderr, "Error: Expected HDLR Marker\n");
- return 1;
- }
-
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in HDLR box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in HDLR box. Expected flag 0\n");
- return 1;
- }
-
- cio_skip(4); /* Reserved */
-
- tk->handler_type = cio_read(4);
- cio_skip(12); /* Reserved */
-
- tk->name_size = box.length - 32;
-
- tk->name = (char *) malloc(tk->name_size * sizeof(char));
- for (i = 0; i < tk->name_size; i++) {
- tk->name[i] = cio_read(1); /* Name */
- }
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with HDLR Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the MDHD box
-*
-* Media Header Box
-*
-*/
-void mj2_write_mdhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
- unsigned int i;
- time_t ltime;
- unsigned int modification_time;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MDHD, 4); /* MDHD */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- cio_write(tk->creation_time, 4); /* Creation Time */
-
- time(<ime); /* Time since 1/1/70 */
- modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
-
- cio_write(modification_time, 4); /* Modification Time */
-
- cio_write(tk->timescale, 4); /* Timescale */
-
- tk->duration = 0;
-
- for (i = 0; i < tk->num_samples; i++)
- tk->duration += tk->sample[i].sample_delta;
-
- cio_write(tk->duration, 4); /* Duration */
-
- cio_write(tk->language, 2); /* Language */
-
- cio_write(0, 2); /* Predefined */
-
- 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 MDHD box
-*
-* Media Header Box
-*
-*/
-int mj2_read_mdhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (!(MJ2_MHDR == box.type || MJ2_MDHD == box.type)) { // Kakadu writes MHDR instead of MDHD
- fprintf(stderr, "Error: Expected MDHD Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in MDHD box\n");
- return 1;
- }
-
- if (0 != cio_read(3)) { /* Flags = 0 */
- fprintf(stderr, "Error with flag in MDHD box. Expected flag 0\n");
- return 1;
- }
-
-
- tk->creation_time = cio_read(4); /* Creation Time */
-
- tk->modification_time = cio_read(4); /* Modification Time */
-
- tk->timescale = cio_read(4); /* Timescale */
-
- tk->duration = cio_read(4); /* Duration */
-
- tk->language = cio_read(2); /* Language */
-
- cio_skip(2); /* Predefined */
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with MDHD Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the MDIA box
-*
-* Media box
-*
-*/
-void mj2_write_mdia(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MDIA, 4); /* MDIA */
-
- mj2_write_mdhd(tk);
- mj2_write_hdlr(tk);
- mj2_write_minf(tk);
-
- 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 MDIA box
-*
-* Media box
-*
-*/
-int mj2_read_mdia(mj2_tk_t * tk, j2k_image_t * img)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_MDIA != box.type) {
- fprintf(stderr, "Error: Expected MDIA Marker\n");
- return 1;
- }
-
- if (mj2_read_mdhd(tk))
- return 1;
- if (mj2_read_hdlr(tk))
- return 1;
- if (mj2_read_minf(tk, img))
- return 1;
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with MDIA Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the TKHD box
-*
-* Track Header box
-*
-*/
-void mj2_write_tkhd(mj2_tk_t * tk)
-{
- mj2_box_t box;
- unsigned int i;
- time_t ltime;
-
- box.init_pos = cio_tell();
- cio_skip(4);
-
- cio_write(MJ2_TKHD, 4); /* TKHD */
-
- cio_write(3, 4); /* Version=0, flags=3 */
-
- time(<ime); /* Time since 1/1/70 */
- tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
-
- cio_write(tk->creation_time, 4); /* Creation Time */
-
- cio_write(tk->modification_time, 4); /* Modification Time */
-
- cio_write(tk->track_ID, 4); /* Track ID */
-
- cio_write(0, 4); /* Reserved */
-
- tk->duration = 0;
-
- for (i = 0; i < tk->num_samples; i++)
- tk->duration += tk->sample[i].sample_delta;
-
- cio_write(tk->duration, 4); /* Duration */
-
- cio_write(0, 4); /* Reserved */
- cio_write(0, 4); /* Reserved */
-
- cio_write(tk->layer, 2); /* Layer */
-
- cio_write(0, 2); /* Predefined */
-
- cio_write(tk->volume, 2); /* Volume */
-
- cio_write(0, 2); /* Reserved */
-
- cio_write(tk->trans_matrix[0], 4); /* Transformation matrix for track */
- cio_write(tk->trans_matrix[1], 4);
- cio_write(tk->trans_matrix[2], 4);
- cio_write(tk->trans_matrix[3], 4);
- cio_write(tk->trans_matrix[4], 4);
- cio_write(tk->trans_matrix[5], 4);
- cio_write(tk->trans_matrix[6], 4);
- cio_write(tk->trans_matrix[7], 4);
- cio_write(tk->trans_matrix[8], 4);
-
- cio_write(tk->visual_w, 4); /* Video Visual Width */
-
- cio_write(tk->visual_h, 4); /* Video Visual Height */
-
- 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 TKHD box
-*
-* Track Header box
-*
-*/
-int mj2_read_tkhd(mj2_tk_t * tk)
-{
- int flag;
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
-
- if (MJ2_TKHD != box.type) {
- fprintf(stderr, "Error: Expected TKHD Marker\n");
- return 1;
- }
-
- if (0 != cio_read(1)) { /* Version = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in TKHD box\n");
- return 1;
- }
-
- flag = cio_read(3);
-
- if (!(flag == 1 || flag == 2 || flag == 3 || flag == 4)) { /* Flags = 1,2,3 or 4 */
- fprintf(stderr,
- "Error with flag in TKHD box: Expected flag 1,2,3 or 4\n");
- return 1;
- }
-
- tk->creation_time = cio_read(4); /* Creation Time */
-
- tk->modification_time = cio_read(4); /* Modification Time */
-
- tk->track_ID = cio_read(4); /* Track ID */
-
- cio_skip(4); /* Reserved */
-
- tk->duration = cio_read(4); /* Duration */
-
- cio_skip(8); /* Reserved */
-
- tk->layer = cio_read(2); /* Layer */
-
- cio_read(2); /* Predefined */
-
- tk->volume = cio_read(2); /* Volume */
-
- cio_skip(2); /* Reserved */
-
- tk->trans_matrix[0] = cio_read(4); /* Transformation matrix for track */
- tk->trans_matrix[1] = cio_read(4);
- tk->trans_matrix[2] = cio_read(4);
- tk->trans_matrix[3] = cio_read(4);
- tk->trans_matrix[4] = cio_read(4);
- tk->trans_matrix[5] = cio_read(4);
- tk->trans_matrix[6] = cio_read(4);
- tk->trans_matrix[7] = cio_read(4);
- tk->trans_matrix[8] = cio_read(4);
-
- tk->visual_w = cio_read(4); /* Video Visual Width */
-
- tk->visual_h = cio_read(4); /* Video Visual Height */
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with TKHD Box size\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the TRAK box
-*
-* Track box
-*
-*/
-void mj2_write_trak(mj2_tk_t * tk)
-{
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
-
- cio_write(MJ2_TRAK, 4); /* TRAK */
-
- mj2_write_tkhd(tk);
- mj2_write_mdia(tk);
-
- 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 TRAK box
-*
-* Track box
-*
-*/
-int mj2_read_trak(mj2_tk_t * tk, j2k_image_t * img)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_TRAK != box.type) {
- fprintf(stderr, "Error: Expected TRAK Marker\n");
- return 1;
- }
- if (mj2_read_tkhd(tk))
- return 1;
- if (mj2_read_mdia(tk, img))
- return 1;
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with TRAK Box\n");
- return 1;
- }
- return 0;
-}
-
-/*
-* Write the MVHD box
-*
-* Movie header Box
-*
-*/
-void mj2_write_mvhd(mj2_movie_t * movie)
-{
- int i;
- mj2_box_t box;
- unsigned j;
- time_t ltime;
- int max_tk_num = 0;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MVHD, 4); /* MVHD */
-
- cio_write(0, 4); /* Version = 0, flags = 0 */
-
- time(<ime); /* Time since 1/1/70 */
- movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
-
- cio_write(movie->creation_time, 4); /* Creation Time */
-
- cio_write(movie->modification_time, 4); /* Modification Time */
-
- cio_write(movie->timescale, 4); /* Timescale */
-
- movie->duration = 0;
-
- for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
- mj2_tk_t *tk = &movie->tk[i];
-
- for (j = 0; j < tk->num_samples; j++) {
- movie->duration += tk->sample[j].sample_delta;
- }
- }
-
- cio_write(movie->duration, 4);
-
- cio_write(movie->rate, 4); /* Rate to play presentation */
-
- cio_write(movie->volume, 2); /* Volume */
-
- cio_write(0, 2); /* Reserved */
- cio_write(0, 4); /* Reserved */
- cio_write(0, 4); /* Reserved */
-
- cio_write(movie->trans_matrix[0], 4); /* Transformation matrix for video */
- cio_write(movie->trans_matrix[1], 4);
- cio_write(movie->trans_matrix[2], 4);
- cio_write(movie->trans_matrix[3], 4);
- cio_write(movie->trans_matrix[4], 4);
- cio_write(movie->trans_matrix[5], 4);
- cio_write(movie->trans_matrix[6], 4);
- cio_write(movie->trans_matrix[7], 4);
- cio_write(movie->trans_matrix[8], 4);
-
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
- cio_write(0, 4); /* Pre-defined */
-
-
- for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
- if (max_tk_num < movie->tk[i].track_ID)
- max_tk_num = movie->tk[i].track_ID;
- }
-
- movie->next_tk_id = max_tk_num + 1;
-
- cio_write(movie->next_tk_id, 4); /* ID of Next track to be added */
-
- 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 MVHD box
-*
-* Movie header Box
-*
-*/
-int mj2_read_mvhd(mj2_movie_t * movie)
-{
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_MVHD != box.type) {
- fprintf(stderr, "Error: Expected MVHD Marker\n");
- return 1;
- }
-
-
- if (0 != cio_read(4)) { /* Version = 0, flags = 0 */
- fprintf(stderr, "Error: Only Version 0 handled in MVHD box\n");
- }
-
- movie->creation_time = cio_read(4); /* Creation Time */
-
- movie->modification_time = cio_read(4); /* Modification Time */
-
- movie->timescale = cio_read(4); /* Timescale */
-
- movie->duration = cio_read(4); /* Duration */
-
- movie->rate = cio_read(4); /* Rate to play presentation */
-
- movie->volume = cio_read(2); /* Volume */
-
- cio_skip(10); /* Reserved */
-
- movie->trans_matrix[0] = cio_read(4); /* Transformation matrix for video */
- movie->trans_matrix[1] = cio_read(4);
- movie->trans_matrix[2] = cio_read(4);
- movie->trans_matrix[3] = cio_read(4);
- movie->trans_matrix[4] = cio_read(4);
- movie->trans_matrix[5] = cio_read(4);
- movie->trans_matrix[6] = cio_read(4);
- movie->trans_matrix[7] = cio_read(4);
- movie->trans_matrix[8] = cio_read(4);
-
- cio_skip(24); /* Pre-defined */
-
- movie->next_tk_id = cio_read(4); /* ID of Next track to be added */
-
- if (cio_tell() - box.init_pos != box.length) {
- fprintf(stderr, "Error with MVHD Box Size\n");
- return 1;
- }
- return 0;
-}
-
-
-/*
-* Write the MOOV box
-*
-* Movie Box
-*
-*/
-void mj2_write_moov(mj2_movie_t * movie)
-{
- int i;
- mj2_box_t box;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MOOV, 4); /* MOOV */
-
- mj2_write_mvhd(movie);
-
- for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
- mj2_write_trak(&movie->tk[i]);
- }
-
- 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 MOOV box
-*
-* Movie Box
-*
-*/
-int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img)
-{
- unsigned int i;
- mj2_box_t box;
- mj2_box_t box2;
-
- mj2_read_boxhdr(&box);
- if (MJ2_MOOV != box.type) {
- fprintf(stderr, "Error: Expected MOOV Marker\n");
- return 1;
- }
-
-
-
- if (mj2_read_mvhd(movie))
- return 1;
-
-
- movie->tk =
- (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
-
- for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
- mj2_read_boxhdr(&box2);
- if (box2.type == MJ2_TRAK) {
- cio_seek(box2.init_pos);
- if (mj2_read_trak(&movie->tk[i], img))
- return 1;
-
- if (movie->tk[i].track_type == 0) {
- movie->num_vtk++;
- } else if (movie->tk[i].track_type == 1) {
- movie->num_stk++;
- } else if (movie->tk[i].track_type == 2) {
- movie->num_htk++;
- }
- } else if (box2.type == MJ2_MVEX) {
- cio_seek(box2.init_pos);
- cio_skip(box2.length);
- i--;
- } else {
- fprintf(stderr, "Error with MOOV Box: Expected TRAK or MVEX box\n");
- return 1;
- }
- }
- return 0;
-}
-
-int mj2_read_struct(FILE *file, mj2_movie_t * movie) {
- mj2_box_t box;
- j2k_image_t img;
- char * src;
- int fsresult;
- int foffset;
-
- movie->num_vtk=0;
- movie->num_stk=0;
- movie->num_htk=0;
-
- src = (char*) malloc (300 * sizeof(char));
- fread(src,300,1, file); // Assuming that jp and ftyp markers size do
- // not exceed 300 bytes
-
- cio_init(src, 300);
-
- if (mj2_read_jp())
- return 1;
- if (mj2_read_ftyp(movie))
- return 1;
-
- fsresult = fseek(file,cio_tell(),SEEK_SET);
- if( fsresult ) {
- fprintf(stderr, "End of file reached while trying to read data after FTYP box\n" );
- return 1;
- }
-
- foffset = cio_tell();
-
- box.type = 0;
-
- fread(src,30,1,file);
- cio_init(src, 30);
- mj2_read_boxhdr(&box);
-
- while(box.type != MJ2_MOOV) {
-
- switch(box.type)
- {
- case MJ2_MDAT:
- fsresult = fseek(file,foffset+box.length,SEEK_SET);
- if( fsresult ) {
- fprintf(stderr, "End of file reached while trying to read MDAT box\n" );
- return 1;
- }
- foffset += box.length;
- break;
-
- case MJ2_MOOF:
- fsresult = fseek(file,foffset+box.length,SEEK_SET);
- if( fsresult ) {
- fprintf(stderr, "End of file reached while trying to read MOOF box\n" );
- return 1;
- }
- foffset += box.length;
- break;
- case MJ2_FREE:
- fsresult = fseek(file,foffset+box.length,SEEK_SET);
- if( fsresult ) {
- fprintf(stderr, "End of file reached while trying to read FREE box\n" );
- return 1;
- }
- foffset += box.length;
- break;
- case MJ2_SKIP:
- fsresult = fseek(file,foffset+box.length,SEEK_SET);
- if( fsresult ) {
- fprintf(stderr, "End of file reached while trying to read SKIP box\n" );
- return 1;
- }
- foffset += box.length;
- break;
- default:
- fprintf(stderr, "Unknown box in MJ2 stream\n");
- fsresult = fseek(file,foffset+box.length,SEEK_SET);
- if( fsresult ) {
- fprintf(stderr, "End of file reached while trying to read end of unknown box\n");
- return 1;
- }
- foffset += box.length;
- break;
- }
- fsresult = fread(src,8,1,file);
- if (fsresult != 1) {
- fprintf(stderr, "MOOV box not found in file\n");
- return 1;
- }
- cio_init(src, 8);
- mj2_read_boxhdr(&box);
- }
-
- fseek(file,foffset,SEEK_SET);
- src = realloc(src,box.length);
- fsresult = fread(src,box.length,1,file);
- if (fsresult != 1) {
- fprintf(stderr, "End of file reached while trying to read MOOV box\n");
- return 1;
- }
-
- cio_init(src, box.length);
-
- if (mj2_read_moov(movie, &img))
- return 1;
-
- free(src);
- return 0;
-}
+++ /dev/null
-/*
-* 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
+++ /dev/null
-#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
+++ /dev/null
-#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;
-}
+++ /dev/null
-#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;
-}
\ No newline at end of file
--- /dev/null
+# 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
--- /dev/null
+/*
+ * 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, ¶meters);
+
+ 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;
+}
--- /dev/null
+/*
+* 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;
+}
+
+
# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
-# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c\r
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /c\r
# ADD BASE RSC /l 0x809 /d "NDEBUG"\r
# ADD RSC /l 0x809 /d "NDEBUG"\r
BSC32=bscmake.exe\r
LINK32=link.exe\r
# 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\r
# 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\r
+# SUBTRACT LINK32 /pdb:none /incremental:yes /debug\r
\r
!ELSEIF "$(CFG)" == "frames_to_mj2 - Win32 Debug"\r
\r
# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
-# 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\r
+# 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\r
# ADD BASE RSC /l 0x809 /d "_DEBUG"\r
# ADD RSC /l 0x809 /d "_DEBUG"\r
BSC32=bscmake.exe\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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\r
+# SUBTRACT LINK32 /pdb:none\r
\r
!ENDIF \r
\r
# PROP Default_Filter ""\r
# Begin Source File\r
\r
-SOURCE=.\compat\getopt.h\r
-# End Source File\r
-# Begin Source File\r
-\r
SOURCE=.\mj2.h\r
# End Source File\r
# Begin Source File\r
# End Source File\r
# End Group\r
# End Group\r
-# Begin Group "Libopenjpeg_097"\r
+# Begin Group "Libopenjpeg Header files"\r
\r
# PROP Default_Filter ""\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\bio.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\bio.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\cio.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\cio.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\dwt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\dwt.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\fix.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\fix.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\int.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\int.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\j2k.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\j2k.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jp2.c\r
+SOURCE=..\libopenjpeg\bio.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jp2.h\r
+SOURCE=..\libopenjpeg\cio.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jpt.c\r
+SOURCE=..\libopenjpeg\dwt.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\jpt.h\r
+SOURCE=..\libopenjpeg\event.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\mct.c\r
+SOURCE=..\libopenjpeg\fix.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\mct.h\r
+SOURCE=..\libopenjpeg\image.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\mqc.c\r
+SOURCE=..\libopenjpeg\int.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\mqc.h\r
+SOURCE=..\libopenjpeg\j2k.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\openjpeg.h\r
+SOURCE=..\libopenjpeg\j2k_lib.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\pi.c\r
+SOURCE=..\libopenjpeg\jp2.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\pi.h\r
+SOURCE=..\libopenjpeg\jpt.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\raw.c\r
+SOURCE=..\libopenjpeg\mct.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\raw.h\r
+SOURCE=..\libopenjpeg\mqc.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\t1.c\r
+SOURCE=..\libopenjpeg\openjpeg.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\t1.h\r
+SOURCE=..\libopenjpeg\opj_includes.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\t2.c\r
+SOURCE=..\libopenjpeg\pi.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\t2.h\r
+SOURCE=..\libopenjpeg\raw.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\tcd.c\r
+SOURCE=..\libopenjpeg\t1.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\tcd.h\r
+SOURCE=..\libopenjpeg\t2.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\tgt.c\r
+SOURCE=..\libopenjpeg\tcd.h\r
# End Source File\r
# Begin Source File\r
\r
-SOURCE=..\libopenjpeg_097\tgt.h\r
+SOURCE=..\libopenjpeg\tgt.h\r
# End Source File\r
# End Group\r
# End Target\r
--- /dev/null
+Microsoft Developer Studio Workspace File, Format Version 6.00\r
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r
+\r
+###############################################################################\r
+\r
+Project: "frames_to_mj2"=".\frames_to_mj2.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+ Begin Project Dependency\r
+ Project_Dep_Name LibOpenJPEG\r
+ End Project Dependency\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Global:\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<3>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2003, Yannick Verschueren\r
- * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- *\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- ***/ \r
-\r
-#include "bio.h"\r
-#include <stdio.h>\r
-#include <setjmp.h>\r
-\r
-static unsigned char *bio_start; /* pointer to the start of the buffer */\r
-static unsigned char *bio_end; /* pointer to the end of the buffer */\r
-static unsigned char *bio_bp; /* pointer to the present position in the buffer */\r
-static unsigned int bio_buf; /* temporary place where each byte is read or written */\r
-static int bio_ct; /* coder : number of bits free to write // decoder : number of bits read */\r
-\r
-extern jmp_buf j2k_error;\r
-\r
-/*\r
- * Number of bytes written.\r
- */\r
-int bio_numbytes()\r
-{\r
- return bio_bp - bio_start;\r
-}\r
-\r
-/*\r
- * Init encoder.\r
- *\r
- * bp : Output buffer\r
- * len : Output buffer length \r
- */\r
-void bio_init_enc(unsigned char *bp, int len)\r
-{\r
- bio_start = bp;\r
- bio_end = bp + len;\r
- bio_bp = bp;\r
- bio_buf = 0;\r
- bio_ct = 8;\r
-}\r
-\r
-/*\r
- * Init decoder.\r
- * \r
- * bp : Input buffer\r
- * len : Input buffer length \r
- */\r
-void bio_init_dec(unsigned char *bp, int len)\r
-{\r
- bio_start = bp;\r
- bio_end = bp + len;\r
- bio_bp = bp;\r
- bio_buf = 0;\r
- bio_ct = 0;\r
-}\r
-\r
-/*\r
- * Write byte. --> function modified to eliminate longjmp !!! \r
- *\r
- */\r
-int bio_byteout()\r
-{\r
- bio_buf = (bio_buf << 8) & 0xffff;\r
- bio_ct = bio_buf == 0xff00 ? 7 : 8;\r
- if (bio_bp >= bio_end)\r
- return 1;\r
- *bio_bp++ = bio_buf >> 8;\r
- return 0;\r
-}\r
-\r
-/*\r
- * Read byte. --> function modified to eliminate longjmp !!\r
- *\r
- */\r
-int bio_bytein()\r
-{\r
- bio_buf = (bio_buf << 8) & 0xffff;\r
- bio_ct = bio_buf == 0xff00 ? 7 : 8;\r
- if (bio_bp >= bio_end)\r
- return 1;\r
- bio_buf |= *bio_bp++;\r
- return 0;\r
-}\r
-\r
-/*\r
- * Write bit.\r
- *\r
- * b : Bit to write (0 or 1)\r
- */\r
-void bio_putbit(int b)\r
-{\r
- if (bio_ct == 0) {\r
- bio_byteout();\r
- }\r
- bio_ct--;\r
- bio_buf |= b << bio_ct;\r
-}\r
-\r
-/*\r
- * Read bit.\r
- *\r
- */\r
-int bio_getbit()\r
-{\r
- if (bio_ct == 0) {\r
- bio_bytein();\r
- }\r
- bio_ct--;\r
- return (bio_buf >> bio_ct) & 1;\r
-}\r
-\r
-/*\r
- * Write bits.\r
- *\r
- * v : Value of bits\r
- * n : Number of bits to write \r
- */\r
-void bio_write(int v, int n)\r
-{\r
- int i;\r
- for (i = n - 1; i >= 0; i--) {\r
- bio_putbit((v >> i) & 1);\r
- }\r
-}\r
-\r
-/*\r
- * Read bits.\r
- * \r
- * n : Number of bits to read\r
- */\r
-int bio_read(int n)\r
-{\r
- int i, v;\r
- v = 0;\r
- for (i = n - 1; i >= 0; i--) {\r
- v += bio_getbit() << i;\r
- }\r
- return v;\r
-}\r
-\r
-/*\r
- * Flush bits. Modified to eliminate longjmp !!\r
- *\r
- */\r
-int bio_flush()\r
-{\r
- bio_ct = 0;\r
- if (bio_byteout())\r
- return 1;\r
- if (bio_ct == 7) {\r
- bio_ct = 0;\r
-\r
- if (bio_byteout())\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-/*\r
- * Passes the ending bits (coming from flushing)\r
- */\r
-int bio_inalign()\r
-{\r
- bio_ct = 0;\r
- if ((bio_buf & 0xff) == 0xff) {\r
- if (bio_bytein())\r
- return 1;\r
- bio_ct = 0;\r
- }\r
- return 0;\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2003, Yannick Verschueren\r
- * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __BIO_H\r
-#define __BIO_H\r
-\r
-/*\r
- * Number of bytes written.\r
- */\r
-int bio_numbytes();\r
-\r
-/*\r
- * Init encoder.\r
- *\r
- * bp : Output buffer\r
- * len : Output buffer length \r
- */\r
-void bio_init_enc(unsigned char *bp, int len);\r
-\r
-/*\r
- * Init decoder.\r
- *\r
- * bp : Input buffer\r
- * len : Input buffer length\r
- */\r
-void bio_init_dec(unsigned char *bp, int len);\r
-\r
-/*\r
- * Write bits.\r
- *\r
- * v : Value of bits\r
- * n : Number of bits to write\r
- */\r
-void bio_write(int v, int n);\r
-\r
-/*\r
- * Read bits. \r
- *\r
- * n : Number of bits to read \r
- */\r
-int bio_read(int n);\r
-\r
-/*\r
- * Flush bits. Modified to eliminate longjmp !!\r
- */\r
-int bio_flush();\r
-\r
-int bio_inalign(); /* modified to eliminated longjmp !! */\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "cio.h"\r
-#include <setjmp.h>\r
-#include <memory.h>\r
-\r
-static unsigned char *cio_start; /* pointer to the start of the stream */\r
-static unsigned char *cio_end; /* pointer to the end of the stream */\r
-static unsigned char *cio_bp; /* pointer to the present position */\r
-\r
-extern jmp_buf j2k_error;\r
-\r
-/* \r
- * Number of bytes written.\r
- */\r
-int cio_numbytes()\r
-{\r
- return cio_bp - cio_start;\r
-}\r
-\r
-/*\r
- * Get position in byte stream.\r
- */\r
-int cio_tell()\r
-{\r
- return cio_bp - cio_start;\r
-}\r
-\r
-/*\r
- * Set position in byte stream.\r
- *\r
- * pos : position, in number of bytes, from the beginning of the stream\r
- */\r
-void cio_seek(int pos)\r
-{\r
- cio_bp = cio_start + pos;\r
-}\r
-\r
-/*\r
- * Number of bytes left before the end of the stream.\r
- */\r
-int cio_numbytesleft()\r
-{\r
- return cio_end - cio_bp;\r
-}\r
-\r
-/*\r
- * Get pointer to the current position in the stream.\r
- */\r
-unsigned char *cio_getbp()\r
-{\r
- return cio_bp;\r
-}\r
-\r
-/* \r
- * Initialize byte IO\r
- *\r
- * bp : destination/source stream\r
- * len : length of the stream\r
- */\r
-void cio_init(unsigned char *bp, int len)\r
-{\r
- cio_start = bp;\r
- cio_end = bp + len;\r
- cio_bp = bp;\r
-}\r
-\r
-/*\r
- * Write a byte.\r
- */\r
-void cio_byteout(unsigned char v)\r
-{\r
- if (cio_bp >= cio_end)\r
- longjmp(j2k_error, 1);\r
- *cio_bp++ = v;\r
-\r
-}\r
-\r
-/*\r
- * Read a byte.\r
- */\r
-unsigned char cio_bytein()\r
-{\r
- if (cio_bp >= cio_end)\r
- longjmp(j2k_error, 1);\r
- return *cio_bp++;\r
-}\r
-\r
-/*\r
- * Write some bytes.\r
- *\r
- * v : value to write\r
- * n : number of bytes to write\r
- */\r
-void cio_write(unsigned int v, int n)\r
-{\r
- int i;\r
- for (i = n - 1; i >= 0; i--) {\r
- cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));\r
- }\r
-}\r
-\r
-/*\r
- * Read some bytes.\r
- *\r
- * n : number of bytes to read\r
- *\r
- * return : value of the n bytes read\r
- */\r
-unsigned int cio_read(int n)\r
-{\r
- int i;\r
- unsigned int v;\r
- v = 0;\r
- for (i = n - 1; i >= 0; i--) {\r
- v += cio_bytein() << (i << 3);\r
- }\r
- return v;\r
-}\r
-\r
-/* \r
- * Skip some bytes.\r
- *\r
- * n : number of bytes to skip\r
- */\r
-void cio_skip(int n)\r
-{\r
- cio_bp += n;\r
-}\r
-\r
-/* \r
- * Read n bytes, copy to buffer\r
- *\r
- * n : number of bytes to transfer\r
- */\r
-void cio_read_to_buf(unsigned char* src_buf, int n)/* Glenn adds */\r
-{\r
- if (cio_bp + n > cio_end)\r
- longjmp(j2k_error, 1);\r
- memcpy(cio_bp, src_buf, n);\r
- cio_bp += n;\r
-}\r
-\r
-/* \r
- * Write n bytes, copy from buffer\r
- *\r
- * n : number of bytes to transfer\r
- */\r
-void cio_write_from_buf(unsigned char* dest_buf, int n)/* Glenn adds */\r
-{\r
- if (cio_bp + n > cio_end)\r
- longjmp(j2k_error, 1);\r
- memcpy(dest_buf, cio_bp, n);\r
- cio_bp += n;\r
-}
\ No newline at end of file
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __CIO_H\r
-#define __CIO_H\r
-\r
-/* \r
- * Number of bytes written.\r
- *\r
- * returns number of bytes written\r
- */\r
-int cio_numbytes();\r
-\r
-/*\r
- * Get position in byte stream.\r
- *\r
- * return position in bytes\r
- */\r
-int cio_tell();\r
-\r
-/*\r
- * Set position in byte stream.\r
- *\r
- * pos : position, in number of bytes, from the beginning of the stream\r
- */\r
-void cio_seek(int pos);\r
-\r
-/*\r
- * Number of bytes left before the end of the stream.\r
- *\r
- * Returns the number of bytes before the end of the stream \r
- */\r
-int cio_numbytesleft();\r
-\r
-/*\r
- * Get pointer to the current position in the stream.\r
- *\r
- * return : pointer to the position\r
- */\r
-unsigned char *cio_getbp();\r
-\r
-/* \r
- * Initialize byte IO\r
- *\r
- * bp : destination/source stream\r
- * len : length of the stream\r
- */\r
-void cio_init(unsigned char *bp, int len);\r
-\r
-/*\r
- * Write some bytes.\r
- *\r
- * v : value to write\r
- * n : number of bytes to write\r
- */\r
-void cio_write(unsigned int v, int n);\r
-\r
-/*\r
- * Read some bytes.\r
- *\r
- * n : number of bytes to read\r
- *\r
- * return : value of the n bytes read\r
- */\r
-unsigned int cio_read(int n);\r
-\r
-/* \r
- * Skip some bytes.\r
- *\r
- * n : number of bytes to skip\r
- */\r
-void cio_skip(int n);\r
-\r
-/*\r
- * Read n bytes, copy to buffer\r
- */\r
-void cio_read_to_buf(unsigned char* src_buf, int n);/* Glenn Pearson adds */\r
-\r
-/*\r
- * Write n bytes, copy from buffer\r
- */\r
-void cio_write_from_buf(unsigned char* dest_buf, int n);/* Glenn Pearson adds */\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2004, Yannick Verschueren\r
- * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2005, Reiner Wahler\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-/*\r
- * NOTE:\r
- * This is a modified version of the openjpeg dwt.c file.\r
- * Average speed improvement compared to the original file (measured on\r
- * my own machine, a P4 running at 3.0 GHz):\r
- * 5x3 wavelets about 2 times faster\r
- * 9x7 wavelets about 3 times faster\r
- * for both, encoding and decoding.\r
- *\r
- * The better performance is caused by doing the 1-dimensional DWT\r
- * within a temporary buffer where the data can be accessed sequential\r
- * for both directions, horizontal and vertical. The 2d vertical DWT was\r
- * the major bottleneck in the former version.\r
- *\r
- * I have also removed the "Add Patrick" part because it is not longer\r
- * needed. \r
- *\r
- * 6/6/2005\r
- * -Ive (aka Reiner Wahler)\r
- * mail: ive@lilysoft.com\r
- */\r
-\r
-\r
-#include "dwt.h"\r
-#include "int.h"\r
-#include "fix.h"\r
-#include "tcd.h"\r
-#include <stdlib.h>\r
-\r
-#define S(i) a[(i)*2]\r
-#define D(i) a[(1+(i)*2)]\r
-#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))\r
-#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))\r
-/* new */\r
-#define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))\r
-#define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))\r
-\r
-/* <summary> */\r
-/* This table contains the norms of the 5-3 wavelets for different bands. */\r
-/* </summary> */\r
-double dwt_norms[4][10] = {\r
- {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},\r
- {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},\r
- {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},\r
- {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}\r
-};\r
-\r
-/* <summary> */\r
-/* This table contains the norms of the 9-7 wavelets for different bands. */\r
-/* </summary> */\r
-double dwt_norms_real[4][10] = {\r
- {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},\r
- {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},\r
- {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},\r
- {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}\r
-};\r
-\r
-\r
-/* <summary> */\r
-/* Forward lazy transform (horizontal). */\r
-/* </summary> */ \r
-void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {\r
- int i;\r
- for (i=0; i<sn; i++) b[i]=a[2*i+cas];\r
- for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];\r
-}\r
-\r
-/* <summary> */ \r
-/* Forward lazy transform (vertical). */\r
-/* </summary> */ \r
-void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {\r
- int i;\r
- for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];\r
- for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse lazy transform (horizontal). */\r
-/* </summary> */\r
-void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) {\r
- int i;\r
-/* for (i=0; i<sn; i++) b[2*i+cas]=a[i];*/\r
-/* for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)];*/\r
- int* ai;\r
- int* bi;\r
- ai=a;\r
- bi=b+cas;\r
- for (i=0; i<sn; i++) {\r
- *bi = *ai; bi+=2; ai++;\r
- }\r
- ai=a+sn;\r
- bi=b+1-cas;\r
- for (i=0; i<dn; i++) {\r
- *bi = *ai; bi+=2; ai++;\r
- }\r
-}\r
-\r
-/* <summary> */ \r
-/* Inverse lazy transform (vertical). */\r
-/* </summary> */ \r
-void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) {\r
- int i;\r
-/* for (i=0; i<sn; i++) b[2*i+cas]=a[i*x];*/\r
-/* for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)*x];*/\r
- int* ai;\r
- int* bi;\r
- ai=a;\r
- bi=b+cas;\r
- for (i=0; i<sn; i++) {\r
- *bi = *ai; bi+=2; ai+=x;\r
- }\r
- ai=a+(sn*x);\r
- bi=b+1-cas;\r
- for (i=0; i<dn; i++) {\r
- *bi = *ai; bi+=2; ai+=x;\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* Forward 5-3 wavelet tranform in 1-D. */\r
-/* </summary> */\r
-void dwt_encode_1(int *a, int dn, int sn, int cas)\r
-{\r
- int i;\r
-\r
- if (!cas) {\r
- if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */\r
- for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;\r
- for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;\r
- }\r
- } else {\r
- if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */\r
- S(0) *= 2;\r
- else {\r
- for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;\r
- for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;\r
- }\r
-\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse 5-3 wavelet tranform in 1-D. */\r
-/* </summary> */ \r
-void dwt_decode_1(int *a, int dn, int sn, int cas) \r
-{\r
- int i;\r
-\r
- if (!cas) {\r
- if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */\r
- for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;\r
- for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;\r
- }\r
- } else {\r
- if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */\r
- S(0) /= 2;\r
- else {\r
- for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;\r
- for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;\r
- }\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* Forward 5-3 wavelet tranform in 2-D. */\r
-/* </summary> */\r
-void dwt_encode(tcd_tilecomp_t * tilec)\r
-{\r
- int i, j, k;\r
- int* a;\r
- int* aj;\r
- int* bj;\r
- int w, l;\r
-\r
- w = tilec->x1-tilec->x0;\r
- l = tilec->numresolutions-1;\r
- a = tilec->data;\r
-\r
- for (i = 0; i < l; i++) {\r
- int rw; /* width of the resolution level computed */\r
- int rh; /* heigth of the resolution level computed */\r
- int rw1; /* width of the resolution level once lower than computed one */\r
- int rh1; /* height of the resolution level once lower than computed one */\r
- int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */\r
- int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */\r
- int dn, sn;\r
-\r
- rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;\r
- rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;\r
- rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;\r
- rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;\r
-\r
- cas_row = tilec->resolutions[l - i].x0 % 2;\r
- cas_col = tilec->resolutions[l - i].y0 % 2;\r
-\r
-\r
- sn = rh1;\r
- dn = rh - rh1;\r
- bj=(int*)malloc(rh*sizeof(int));\r
- for (j=0; j<rw; j++) {\r
- aj=a+j;\r
- for (k=0; k<rh; k++) bj[k]=aj[k*w];\r
- dwt_encode_1(bj, dn, sn, cas_col);\r
- dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);\r
- }\r
- free(bj);\r
-\r
- sn = rw1;\r
- dn = rw - rw1;\r
- bj=(int*)malloc(rw*sizeof(int));\r
- for (j=0; j<rh; j++) {\r
- aj=a+j*w;\r
- for (k=0; k<rw; k++) bj[k]=aj[k];\r
- dwt_encode_1(bj, dn, sn, cas_row);\r
- dwt_deinterleave_h(bj, aj, dn, sn, cas_row);\r
- }\r
- free(bj);\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* Inverse 5-3 wavelet tranform in 2-D. */\r
-/* </summary> */\r
-void dwt_decode(tcd_tilecomp_t * tilec, int stop)\r
-{\r
- int i, j, k;\r
- int* a;\r
- int* aj;\r
- int* bj;\r
- int w, l;\r
-\r
- w = tilec->x1-tilec->x0;\r
- l = tilec->numresolutions-1;\r
- a = tilec->data;\r
-\r
- for (i = l - 1; i >= stop; i--) {\r
- int rw; /* width of the resolution level computed */\r
- int rh; /* heigth of the resolution level computed */\r
- int rw1; /* width of the resolution level once lower than computed one */\r
- int rh1; /* height of the resolution level once lower than computed one */\r
- int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */\r
- int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */\r
- int dn, sn;\r
-\r
- rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;\r
- rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;\r
- rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;\r
- rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;\r
-\r
- cas_row = tilec->resolutions[l - i].x0 % 2;\r
- cas_col = tilec->resolutions[l - i].y0 % 2;\r
-\r
- sn = rw1;\r
- dn = rw - rw1;\r
- bj=(int*)malloc(rw*sizeof(int));\r
- for (j = 0; j < rh; j++) {\r
- aj = a+j*w;\r
- dwt_interleave_h(aj, bj, dn, sn, cas_row);\r
- dwt_decode_1(bj, dn, sn, cas_row);\r
- for (k = 0; k < rw; k++) aj[k] = bj[k];\r
- }\r
- free(bj);\r
-\r
- sn = rh1;\r
- dn = rh - rh1;\r
- bj=(int*)malloc(rh*sizeof(int));\r
- for (j = 0; j < rw; j++) {\r
- aj = a+j;\r
- dwt_interleave_v(aj, bj, dn, sn, w, cas_col);\r
- dwt_decode_1(bj, dn, sn, cas_col);\r
- for (k = 0; k < rh; k++) aj[k * w] = bj[k];\r
- }\r
- free(bj);\r
-\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* Get gain of 5-3 wavelet transform. */\r
-/* </summary> */\r
-int dwt_getgain(int orient)\r
-{\r
- if (orient == 0)\r
- return 0;\r
- if (orient == 1 || orient == 2)\r
- return 1;\r
- return 2;\r
-}\r
-\r
-/* <summary> */\r
-/* Get norm of 5-3 wavelet. */\r
-/* </summary> */\r
-double dwt_getnorm(int level, int orient)\r
-{\r
- return dwt_norms[orient][level];\r
-}\r
-\r
-/* <summary> */\r
-/* Forward 9-7 wavelet transform in 1-D. */\r
-/* </summary> */\r
-void dwt_encode_1_real(int *a, int dn, int sn, int cas)\r
-{\r
- int i;\r
- if (!cas) {\r
- if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */\r
- for (i = 0; i < dn; i++)\r
- D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);\r
- for (i = 0; i < sn; i++)\r
- S(i) -= fix_mul(D_(i - 1) + D_(i), 434);\r
- for (i = 0; i < dn; i++)\r
- D(i) += fix_mul(S_(i) + S_(i + 1), 7233);\r
- for (i = 0; i < sn; i++)\r
- S(i) += fix_mul(D_(i - 1) + D_(i), 3633);\r
- for (i = 0; i < dn; i++)\r
- D(i) = fix_mul(D(i), 5038); /*5038 */\r
- for (i = 0; i < sn; i++)\r
- S(i) = fix_mul(S(i), 6659); /*6660 */\r
- }\r
- } else {\r
- if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */\r
- for (i = 0; i < dn; i++)\r
- S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);\r
- for (i = 0; i < sn; i++)\r
- D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);\r
- for (i = 0; i < dn; i++)\r
- S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);\r
- for (i = 0; i < sn; i++)\r
- D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);\r
- for (i = 0; i < dn; i++)\r
- S(i) = fix_mul(S(i), 5038); /*5038 */\r
- for (i = 0; i < sn; i++)\r
- D(i) = fix_mul(D(i), 6659); /*6660 */\r
- }\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse 9-7 wavelet transform in 1-D. */\r
-/* </summary> */\r
-void dwt_decode_1_real(int *a, int dn, int sn, int cas)\r
-{\r
- int i;\r
- if (!cas) {\r
- if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */\r
- for (i = 0; i < sn; i++)\r
- S(i) = fix_mul(S(i), 10078); /* 10076 */\r
- for (i = 0; i < dn; i++)\r
- D(i) = fix_mul(D(i), 13318); /* 13320 */\r
- for (i = 0; i < sn; i++)\r
- S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);\r
- for (i = 0; i < dn; i++)\r
- D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);\r
- for (i = 0; i < sn; i++)\r
- S(i) += fix_mul(D_(i - 1) + D_(i), 434);\r
- for (i = 0; i < dn; i++)\r
- D(i) += fix_mul(S_(i) + S_(i + 1), 12994); /* 12993 */\r
- }\r
- } else {\r
- if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */\r
- for (i = 0; i < sn; i++)\r
- D(i) = fix_mul(D(i), 10078); /* 10076 */\r
- for (i = 0; i < dn; i++)\r
- S(i) = fix_mul(S(i), 13318); /* 13320 */\r
- for (i = 0; i < sn; i++)\r
- D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);\r
- for (i = 0; i < dn; i++)\r
- S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);\r
- for (i = 0; i < sn; i++)\r
- D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);\r
- for (i = 0; i < dn; i++)\r
- S(i) += fix_mul(DD_(i) + DD_(i - 1), 12994); /* 12993 */\r
- }\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Forward 9-7 wavelet transform in 2-D. */\r
-/* </summary> */\r
-\r
-void dwt_encode_real(tcd_tilecomp_t * tilec)\r
-{\r
- int i, j, k;\r
- int* a;\r
- int* aj;\r
- int* bj;\r
- int w, l;\r
-\r
- w = tilec->x1-tilec->x0;\r
- l = tilec->numresolutions-1;\r
- a = tilec->data;\r
-\r
- for (i = 0; i < l; i++) {\r
- int rw; /* width of the resolution level computed */\r
- int rh; /* heigth of the resolution level computed */\r
- int rw1; /* width of the resolution level once lower than computed one */\r
- int rh1; /* height of the resolution level once lower than computed one */\r
- int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */\r
- int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */\r
- int dn, sn;\r
-\r
- rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;\r
- rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;\r
- rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;\r
- rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;\r
-\r
- cas_row = tilec->resolutions[l - i].x0 % 2;\r
- cas_col = tilec->resolutions[l - i].y0 % 2;\r
-\r
- sn = rh1;\r
- dn = rh - rh1;\r
- bj=(int*)malloc(rh*sizeof(int));\r
- for (j = 0; j < rw; j++) {\r
- aj = a + j;\r
- for (k = 0; k < rh; k++) bj[k] = aj[k*w];\r
- dwt_encode_1_real(bj, dn, sn, cas_col);\r
- dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);\r
- }\r
- free(bj);\r
-\r
- sn = rw1;\r
- dn = rw - rw1;\r
- bj=(int*)malloc(rw*sizeof(int));\r
- for (j = 0; j < rh; j++) {\r
- aj = a + j * w;\r
- for (k = 0; k < rw; k++) bj[k] = aj[k];\r
- dwt_encode_1_real(bj, dn, sn, cas_row);\r
- dwt_deinterleave_h(bj, aj, dn, sn, cas_row);\r
- }\r
- free(bj);\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* Inverse 9-7 wavelet transform in 2-D. */\r
-/* </summary> */\r
-void dwt_decode_real(tcd_tilecomp_t * tilec, int stop)\r
-{\r
-\r
- int i, j, k;\r
- int* a;\r
- int* aj;\r
- int* bj;\r
- int w, l;\r
-\r
- w = tilec->x1-tilec->x0;\r
- l = tilec->numresolutions-1;\r
- a = tilec->data;\r
-\r
- for (i = l-1; i >= stop; i--) {\r
- int rw; /* width of the resolution level computed */\r
- int rh; /* heigth of the resolution level computed */\r
- int rw1; /* width of the resolution level once lower than computed one */\r
- int rh1; /* height of the resolution level once lower than computed one */\r
- int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */\r
- int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */\r
- int dn, sn;\r
-\r
- rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;\r
- rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;\r
- rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;\r
- rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;\r
-\r
- cas_col = tilec->resolutions[l - i].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */\r
- cas_row = tilec->resolutions[l - i].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */\r
-\r
- sn = rw1;\r
- dn = rw-rw1;\r
- bj = (int*)malloc(rw * sizeof(int));\r
- for (j = 0; j < rh; j++) {\r
- aj = a+j*w;\r
- dwt_interleave_h(aj, bj, dn, sn, cas_col);\r
- dwt_decode_1_real(bj, dn, sn, cas_col);\r
- for (k = 0; k < rw; k++) aj[k] = bj[k];\r
- }\r
- free(bj);\r
-\r
- sn = rh1;\r
- dn = rh-rh1;\r
- bj = (int*)malloc(rh * sizeof(int));\r
- for (j=0; j<rw; j++) {\r
- aj = a+j;\r
- dwt_interleave_v(aj, bj, dn, sn, w, cas_row);\r
- dwt_decode_1_real(bj, dn, sn, cas_row);\r
- for (k = 0; k < rh; k++) aj[k * w] = bj[k];\r
- }\r
- free(bj);\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* Get gain of 9-7 wavelet transform. */\r
-/* </summary> */\r
-int dwt_getgain_real(int orient)\r
-{\r
- (void)orient;\r
- return 0;\r
-}\r
-\r
-/* <summary> */\r
-/* Get norm of 9-7 wavelet. */\r
-/* </summary> */\r
-double dwt_getnorm_real(int level, int orient)\r
-{\r
- return dwt_norms_real[orient][level];\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "tcd.h"\r
-\r
-#ifndef __DWT_H\r
-#define __DWT_H\r
-\r
-/*\r
- * Apply a reversible DWT transform to a component of an image \r
- * tilec : tile component information (present tile)\r
- */\r
-/* void dwt_encode(int* a, int w, int h, int l); */\r
-void dwt_encode(tcd_tilecomp_t * tilec);\r
-/*\r
- * Apply a reversible inverse DWT transform to a component of an image \r
- * tilec : tile component information (present tile)\r
- */\r
-void dwt_decode(tcd_tilecomp_t * tilec, int stop);\r
-\r
-/*\r
- * Get the gain of a subband for the reversible DWT\r
- * orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)\r
- */\r
-int dwt_getgain(int orient);\r
-\r
-/*\r
- * Get the norm of a wavelet function of a subband at a specified level for the reversible DWT\r
- * level: level of the wavelet function\r
- * orient: band of the wavelet function\r
- */\r
-double dwt_getnorm(int level, int orient);\r
-\r
-/*\r
- * Apply an irreversible DWT transform to a component of an image \r
- */\r
-void dwt_encode_real(tcd_tilecomp_t * tilec);\r
-\r
-/*\r
- * Apply an irreversible inverse DWT transform to a component of an image \r
- */\r
-void dwt_decode_real(tcd_tilecomp_t * tilec, int stop);\r
-/*\r
- * Get the gain of a subband for the irreversible DWT\r
- * orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)\r
- */\r
-int dwt_getgain_real(int orient);\r
-\r
-/*\r
- * Get the norm of a wavelet function of a subband at a specified level for the irreversible DWT\r
- * level: level of the wavelet function\r
- * orient: band of the wavelet function\r
- */\r
-double dwt_getnorm_real(int level, int orient);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "fix.h"\r
-#include <math.h> /*Add Antonin : multbug1*/\r
-\r
-#ifdef WIN32\r
-#define int64 __int64\r
-#else\r
-#define int64 long long\r
-#endif\r
-\r
-/*\r
- * Multiply two fixed-precision rational numbers.\r
- */\r
-\r
-/*int fix_mul(int a, int b)\r
-{\r
- return (int) ((int64) a * (int64) b >> 13);\r
-}*/\r
-\r
-\r
-/*Mod Antonin : multbug1*/\r
-/*\r
-int fix_mul(int a, int b)\r
-{\r
- double tmp= (double) ((int64) a * (int64) b);\r
- int64 v = (int64) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));\r
- v = (tmp<0)?-v:v;\r
- return (int) v;\r
-}\r
-*/\r
-/*doM*/\r
-\r
-int fix_mul(int a, int b) /* Luke Lee optimized : 11/16/2004*/\r
-{\r
- int64 temp = (int64) a * (int64) b >> 12;\r
- return (int) ((temp >> 1) + (temp & 1)) ;\r
-}\r
-\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-\r
-#ifndef __FIX_H\r
-#define __FIX_H\r
-\r
-int fix_mul(int a, int b);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "int.h"\r
-\r
-/*\r
- * Get the minimum of two integers.\r
- *\r
- * returns a if a < b else b\r
- */\r
-int int_min(int a, int b)\r
-{\r
- return a < b ? a : b;\r
-}\r
-\r
-/*\r
- * Get the maximum of two integers.\r
- *\r
- * returns a if a > b else b\r
- */\r
-int int_max(int a, int b)\r
-{\r
- return a > b ? a : b;\r
-}\r
-\r
-/*\r
- * Clamp an integer inside an interval.\r
- *\r
- * return a if (min < a < max)\r
- * return max if (a > max)\r
- * return min if (a < min) \r
- */\r
-int int_clamp(int a, int min, int max)\r
-{\r
- if (a < min)\r
- return min;\r
- if (a > max)\r
- return max;\r
- return a;\r
-}\r
-\r
-/*\r
- * Get absolute value of integer.\r
- */\r
-int int_abs(int a)\r
-{\r
- return a < 0 ? -a : a;\r
-}\r
-\r
-/*\r
- * Divide an integer and round upwards.\r
- *\r
- * a divided by b\r
- */\r
-int int_ceildiv(int a, int b)\r
-{\r
- return (a + b - 1) / b;\r
-}\r
-\r
-/*\r
- * Divide an integer by a power of 2 and round upwards.\r
- *\r
- * a divided by 2^b\r
- */\r
-int int_ceildivpow2(int a, int b)\r
-{\r
- return (a + (1 << b) - 1) >> b;\r
-}\r
-\r
-/*\r
- * Divide an integer by a power of 2 and round downwards.\r
- *\r
- * a divided by 2^b\r
- */\r
-int int_floordivpow2(int a, int b)\r
-{\r
- return a >> b;\r
-}\r
-\r
-/*\r
- * Get logarithm of an integer and round downwards.\r
- *\r
- * log2(a)\r
- */\r
-int int_floorlog2(int a)\r
-{\r
- int l;\r
- for (l = 0; a > 1; l++) {\r
- a >>= 1;\r
- }\r
- return l;\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "j2k.h"\r
-\r
-#ifndef __INT_H\r
-#define __INT_H\r
-\r
-/*\r
- * Get the minimum of two integers.\r
- *\r
- * returns a if a < b else b\r
- */\r
-int int_min(int a, int b);\r
-\r
-/*\r
- * Get the maximum of two integers.\r
- *\r
- * returns a if a > b else b\r
- */\r
-int int_max(int a, int b);\r
-\r
-/*\r
- * Clamp an integer inside an interval.\r
- *\r
- * return a if (min < a < max)\r
- * return max if (a > max)\r
- * return min if (a < min) \r
- */\r
-int int_clamp(int a, int min, int max);\r
-\r
-/*\r
- * Get absolute value of integer.\r
- */\r
-int int_abs(int a);\r
-\r
-/*\r
- * Divide an integer and round upwards.\r
- *\r
- * a divided by b\r
- */\r
-int int_ceildiv(int a, int b);\r
-\r
-/*\r
- * Divide an integer by a power of 2 and round upwards.\r
- *\r
- * a divided by 2^b\r
- */\r
-LIBJ2K_API int int_ceildivpow2(int a, int b);\r
-\r
-/*\r
- * Divide an integer by a power of 2 and round downwards.\r
- *\r
- * a divided by 2^b\r
- */\r
-LIBJ2K_API int int_floordivpow2(int a, int b);\r
-\r
-/*\r
- * Get logarithm of an integer and round downwards.\r
- *\r
- * log2(a)\r
- */\r
-int int_floorlog2(int a);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
-* Copyright (c) 2001-2002, David Janssens\r
-* Copyright (c) 2002-2004, Yannick Verschueren\r
-* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
-* All rights reserved.\r
-*\r
-* Redistribution and use in source and binary forms, with or without\r
-* modification, are permitted provided that the following conditions\r
-* are met:\r
-* 1. Redistributions of source code must retain the above copyright\r
-* notice, this list of conditions and the following disclaimer.\r
-* 2. Redistributions in binary form must reproduce the above copyright\r
-* notice, this list of conditions and the following disclaimer in the\r
-* documentation and/or other materials provided with the distribution.\r
-*\r
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
-* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
-* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
-* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
-* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
-* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
-* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
-* POSSIBILITY OF SUCH DAMAGE.\r
-*/\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <setjmp.h>\r
-#include <math.h>\r
-\r
-#include "j2k.h"\r
-#include "cio.h"\r
-#include "tcd.h"\r
-#include "dwt.h"\r
-#include "int.h"\r
-#include "jpt.h"\r
-\r
-#define J2K_MS_SOC 0xff4f\r
-#define J2K_MS_SOT 0xff90\r
-#define J2K_MS_SOD 0xff93\r
-#define J2K_MS_EOC 0xffd9\r
-#define J2K_MS_SIZ 0xff51\r
-#define J2K_MS_COD 0xff52\r
-#define J2K_MS_COC 0xff53\r
-#define J2K_MS_RGN 0xff5e\r
-#define J2K_MS_QCD 0xff5c\r
-#define J2K_MS_QCC 0xff5d\r
-#define J2K_MS_POC 0xff5f\r
-#define J2K_MS_TLM 0xff55\r
-#define J2K_MS_PLM 0xff57\r
-#define J2K_MS_PLT 0xff58\r
-#define J2K_MS_PPM 0xff60\r
-#define J2K_MS_PPT 0xff61\r
-#define J2K_MS_SOP 0xff91\r
-#define J2K_MS_EPH 0xff92\r
-#define J2K_MS_CRG 0xff63\r
-#define J2K_MS_COM 0xff64\r
-\r
-#define J2K_STATE_MHSOC 0x0001\r
-#define J2K_STATE_MHSIZ 0x0002\r
-#define J2K_STATE_MH 0x0004\r
-#define J2K_STATE_TPHSOT 0x0008\r
-#define J2K_STATE_TPH 0x0010\r
-#define J2K_STATE_MT 0x0020\r
-#define J2K_STATE_NEOC 0x0040\r
-\r
-\r
-jmp_buf j2k_error;\r
-\r
-static int j2k_state;\r
-static int j2k_curtileno;\r
-j2k_tcp_t j2k_default_tcp;\r
-static unsigned char *j2k_eot;\r
-static int j2k_sot_start;\r
-static int pos_correction;\r
-\r
-static j2k_image_t *j2k_img;\r
-static j2k_cp_t *j2k_cp;\r
-\r
-static unsigned char **j2k_tile_data;\r
-static int *j2k_tile_len;\r
-\r
-static info_image info_IM;\r
-\r
-/* Add Patrick */\r
-void j2k_clean()\r
-{\r
- int tileno = 0;\r
-#ifndef NO_PACKETS_DECODING\r
- tcd_free_encode(j2k_img, j2k_cp, j2k_curtileno);\r
-#endif\r
- \r
- if (info_IM.index_on) {\r
- for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) {\r
- free(info_IM.tile[tileno].packet);\r
- }\r
- free(info_IM.tile);\r
- }\r
-}\r
-\r
-/* \Add Patrick */\r
-\r
-void j2k_dump_image(j2k_image_t * img)\r
-{\r
- int compno;\r
- fprintf(stdout, "image {\n");\r
- fprintf(stdout, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0,\r
- img->x1, img->y1);\r
- fprintf(stdout, " numcomps=%d\n", img->numcomps);\r
- for (compno = 0; compno < img->numcomps; compno++) {\r
- j2k_comp_t *comp = &img->comps[compno];\r
- fprintf(stdout, " comp %d {\n", compno);\r
- fprintf(stdout, " dx=%d, dy=%d\n", comp->dx, comp->dy);\r
- fprintf(stdout, " prec=%d\n", comp->prec);\r
- fprintf(stdout, " sgnd=%d\n", comp->sgnd);\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, "}\n");\r
-}\r
-\r
-void j2k_dump_cp(j2k_image_t * img, j2k_cp_t * cp)\r
-{\r
- int tileno, compno, layno, bandno, resno, numbands;\r
- fprintf(stdout, "coding parameters {\n");\r
- fprintf(stdout, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);\r
- fprintf(stdout, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);\r
- fprintf(stdout, " tw=%d, th=%d\n", cp->tw, cp->th);\r
- for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {\r
- j2k_tcp_t *tcp = &cp->tcps[tileno];\r
- fprintf(stdout, " tile %d {\n", tileno);\r
- fprintf(stdout, " csty=%x\n", tcp->csty);\r
- fprintf(stdout, " prg=%d\n", tcp->prg);\r
- fprintf(stdout, " numlayers=%d\n", tcp->numlayers);\r
- fprintf(stdout, " mct=%d\n", tcp->mct);\r
- fprintf(stdout, " rates=");\r
- for (layno = 0; layno < tcp->numlayers; layno++) {\r
- fprintf(stdout, "%d ", tcp->rates[layno]);\r
- }\r
- fprintf(stdout, "\n");\r
- for (compno = 0; compno < img->numcomps; compno++) {\r
- j2k_tccp_t *tccp = &tcp->tccps[compno];\r
- fprintf(stdout, " comp %d {\n", compno);\r
- fprintf(stdout, " csty=%x\n", tccp->csty);\r
- fprintf(stdout, " numresolutions=%d\n", tccp->numresolutions);\r
- fprintf(stdout, " cblkw=%d\n", tccp->cblkw);\r
- fprintf(stdout, " cblkh=%d\n", tccp->cblkh);\r
- fprintf(stdout, " cblksty=%x\n", tccp->cblksty);\r
- fprintf(stdout, " qmfbid=%d\n", tccp->qmfbid);\r
- fprintf(stdout, " qntsty=%d\n", tccp->qntsty);\r
- fprintf(stdout, " numgbits=%d\n", tccp->numgbits);\r
- fprintf(stdout, " roishift=%d\n", tccp->roishift);\r
- fprintf(stdout, " stepsizes=");\r
- numbands =\r
- tccp->qntsty ==\r
- J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;\r
- for (bandno = 0; bandno < numbands; bandno++) {\r
- fprintf(stdout, "(%d,%d) ", tccp->stepsizes[bandno].mant,\r
- tccp->stepsizes[bandno].expn);\r
- }\r
- fprintf(stdout, "\n");\r
- \r
- if (tccp->csty & J2K_CCP_CSTY_PRT) {\r
- fprintf(stdout, " prcw=");\r
- for (resno = 0; resno < tccp->numresolutions; resno++) {\r
- fprintf(stdout, "%d ", tccp->prcw[resno]);\r
- }\r
- fprintf(stdout, "\n");\r
- fprintf(stdout, " prch=");\r
- for (resno = 0; resno < tccp->numresolutions; resno++) {\r
- fprintf(stdout, "%d ", tccp->prch[resno]);\r
- }\r
- fprintf(stdout, "\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, "}\n");\r
-}\r
-\r
-void j2k_write_soc()\r
-{\r
- cio_write(J2K_MS_SOC, 2);\r
-}\r
-\r
-void j2k_read_soc()\r
-{\r
- j2k_state = J2K_STATE_MHSIZ;\r
-}\r
-\r
-void j2k_write_siz()\r
-{\r
- int i;\r
- int lenp, len;\r
- \r
- cio_write(J2K_MS_SIZ, 2); /* SIZ */\r
- lenp = cio_tell();\r
- cio_skip(2);\r
- cio_write(0, 2); /* Rsiz (capabilities) */\r
- cio_write(j2k_img->x1, 4); /* Xsiz */\r
- cio_write(j2k_img->y1, 4); /* Ysiz */\r
- cio_write(j2k_img->x0, 4); /* X0siz */\r
- cio_write(j2k_img->y0, 4); /* Y0siz */\r
- cio_write(j2k_cp->tdx, 4); /* XTsiz */\r
- cio_write(j2k_cp->tdy, 4); /* YTsiz */\r
- cio_write(j2k_cp->tx0, 4); /* XT0siz */\r
- cio_write(j2k_cp->ty0, 4); /* YT0siz */\r
- cio_write(j2k_img->numcomps, 2); /* Csiz */\r
- for (i = 0; i < j2k_img->numcomps; i++) {\r
- cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */\r
- cio_write(j2k_img->comps[i].dx, 1); /* XRsiz_i */\r
- cio_write(j2k_img->comps[i].dy, 1); /* YRsiz_i */\r
- }\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2); /* Lsiz */\r
- cio_seek(lenp + len);\r
- \r
-}\r
-\r
-void j2k_read_siz()\r
-{\r
- int len, i;\r
- \r
- len = cio_read(2); /* Lsiz */\r
- cio_read(2); /* Rsiz (capabilities) */\r
- j2k_img->x1 = cio_read(4); /* Xsiz */\r
- j2k_img->y1 = cio_read(4); /* Ysiz */\r
- j2k_img->x0 = cio_read(4); /* X0siz */\r
- j2k_img->y0 = cio_read(4); /* Y0siz */\r
- j2k_cp->tdx = cio_read(4); /* XTsiz */\r
- j2k_cp->tdy = cio_read(4); /* YTsiz */\r
- j2k_cp->tx0 = cio_read(4); /* XT0siz */\r
- j2k_cp->ty0 = cio_read(4); /* YT0siz */\r
- \r
- j2k_img->numcomps = cio_read(2); /* Csiz */\r
- j2k_img->comps =\r
- (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t));\r
- for (i = 0; i < j2k_img->numcomps; i++) {\r
- int tmp, w, h;\r
- tmp = cio_read(1); /* Ssiz_i */\r
- j2k_img->comps[i].prec = (tmp & 0x7f) + 1;\r
- j2k_img->comps[i].sgnd = tmp >> 7;\r
- j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */\r
- j2k_img->comps[i].dy = cio_read(1); /* YRsiz_i */\r
- w = int_ceildiv(j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx);\r
- h = int_ceildiv(j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy);\r
- j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */\r
- j2k_img->comps[i].factor = 0; /* reducing factor by component */\r
- }\r
- \r
- j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);\r
- j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);\r
- j2k_cp->tcps =\r
- (j2k_tcp_t *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(j2k_tcp_t));\r
- j2k_cp->tileno = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));\r
- j2k_cp->tileno_size = 0;\r
- \r
- for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {\r
- j2k_cp->tcps[i].POC = 0;\r
- j2k_cp->tcps[i].numpocs = 0;\r
- j2k_cp->tcps[i].first = 1;\r
- }\r
- \r
- /* Initialization for PPM marker */\r
- j2k_cp->ppm = 0;\r
- j2k_cp->ppm_data = NULL;\r
- j2k_cp->ppm_data_first = NULL;\r
- j2k_cp->ppm_previous = 0;\r
- j2k_cp->ppm_store = 0;\r
- \r
- j2k_default_tcp.tccps =\r
- (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);\r
- for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {\r
- j2k_cp->tcps[i].tccps =\r
- (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);\r
- }\r
- j2k_tile_data =\r
- (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *));\r
- j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));\r
- j2k_state = J2K_STATE_MH;\r
- \r
- \r
-}\r
-\r
-void j2k_write_com()\r
-{\r
- unsigned int i;\r
- int lenp, len;\r
- char str[256];\r
- sprintf(str, "%s", j2k_cp->comment);\r
- \r
- cio_write(J2K_MS_COM, 2);\r
- lenp = cio_tell();\r
- cio_skip(2);\r
- cio_write(0, 2);\r
- for (i = 0; i < strlen(str); i++) {\r
- cio_write(str[i], 1);\r
- }\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2);\r
- cio_seek(lenp + len);\r
- \r
-}\r
-\r
-void j2k_read_com()\r
-{\r
- int len;\r
- \r
- len = cio_read(2);\r
- cio_skip(len - 2);\r
- \r
-}\r
-\r
-void j2k_write_cox(int compno)\r
-{\r
- int i;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- tccp = &tcp->tccps[compno];\r
- \r
- cio_write(tccp->numresolutions - 1, 1); /* SPcox (D) */\r
- cio_write(tccp->cblkw - 2, 1); /* SPcox (E) */\r
- cio_write(tccp->cblkh - 2, 1); /* SPcox (F) */\r
- cio_write(tccp->cblksty, 1); /* SPcox (G) */\r
- cio_write(tccp->qmfbid, 1); /* SPcox (H) */\r
- \r
- if (tccp->csty & J2K_CCP_CSTY_PRT) {\r
- for (i = 0; i < tccp->numresolutions; i++) {\r
- cio_write(tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */\r
- }\r
- }\r
-}\r
-\r
-void j2k_read_cox(int compno)\r
-{\r
- int i;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
- tcp =\r
- j2k_state ==\r
- J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;\r
- tccp = &tcp->tccps[compno];\r
- tccp->numresolutions = cio_read(1) + 1; /* SPcox (D) */\r
- /*Check the reduce value*/\r
- j2k_cp->reduce=int_min((tccp->numresolutions)-1,j2k_cp->reduce);\r
- tccp->cblkw = cio_read(1) + 2; /* SPcox (E) */\r
- tccp->cblkh = cio_read(1) + 2; /* SPcox (F) */\r
- tccp->cblksty = cio_read(1); /* SPcox (G) */\r
- tccp->qmfbid = cio_read(1); /* SPcox (H) */\r
- if (tccp->csty & J2K_CP_CSTY_PRT) {\r
- for (i = 0; i < tccp->numresolutions; i++) {\r
- int tmp = cio_read(1); /* SPcox (I_i) */\r
- tccp->prcw[i] = tmp & 0xf;\r
- tccp->prch[i] = tmp >> 4;\r
- }\r
- }\r
-}\r
-\r
-void j2k_write_cod()\r
-{\r
- j2k_tcp_t *tcp;\r
- int lenp, len;\r
- \r
- cio_write(J2K_MS_COD, 2); /* COD */\r
- \r
- lenp = cio_tell();\r
- cio_skip(2);\r
- \r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- cio_write(tcp->csty, 1); /* Scod */\r
- cio_write(tcp->prg, 1); /* SGcod (A) */\r
- cio_write(tcp->numlayers, 2); /* SGcod (B) */\r
- cio_write(tcp->mct, 1); /* SGcod (C) */\r
- \r
- j2k_write_cox(0);\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2); /* Lcod */\r
- cio_seek(lenp + len);\r
-}\r
-\r
-void j2k_read_cod()\r
-{\r
- int len, i, pos;\r
- j2k_tcp_t *tcp;\r
- \r
- tcp =\r
- j2k_state ==\r
- J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;\r
- len = cio_read(2); /* Lcod */\r
- tcp->csty = cio_read(1); /* Scod */\r
- tcp->prg = cio_read(1); /* SGcod (A) */\r
- tcp->numlayers = cio_read(2); /* SGcod (B) */\r
- tcp->mct = cio_read(1); /* SGcod (C) */\r
- \r
- pos = cio_tell();\r
- for (i = 0; i < j2k_img->numcomps; i++) {\r
- tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;\r
- cio_seek(pos);\r
- j2k_read_cox(i);\r
- }\r
-}\r
-\r
-void j2k_write_coc(int compno)\r
-{\r
- j2k_tcp_t *tcp;\r
- int lenp, len;\r
- \r
- cio_write(J2K_MS_COC, 2); /* COC */\r
- lenp = cio_tell();\r
- cio_skip(2);\r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */\r
- cio_write(tcp->tccps[compno].csty, 1); /* Scoc */\r
- j2k_write_cox(compno);\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2); /* Lcoc */\r
- cio_seek(lenp + len);\r
-}\r
-\r
-void j2k_read_coc()\r
-{\r
- int len, compno;\r
- j2k_tcp_t *tcp;\r
- \r
- tcp =\r
- j2k_state ==\r
- J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;\r
- len = cio_read(2); /* Lcoc */\r
- compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */\r
- tcp->tccps[compno].csty = cio_read(1); /* Scoc */\r
- j2k_read_cox(compno);\r
-}\r
-\r
-void j2k_write_qcx(int compno)\r
-{\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
- int bandno, numbands;\r
- int expn, mant;\r
- \r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- tccp = &tcp->tccps[compno];\r
- \r
- cio_write(tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */\r
- numbands =\r
- tccp->qntsty ==\r
- J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;\r
- \r
- for (bandno = 0; bandno < numbands; bandno++) {\r
- expn = tccp->stepsizes[bandno].expn;\r
- mant = tccp->stepsizes[bandno].mant;\r
- \r
- if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {\r
- cio_write(expn << 3, 1); /* SPqcx_i */\r
- } else {\r
- cio_write((expn << 11) + mant, 2); /* SPqcx_i */\r
- }\r
- }\r
- \r
-}\r
-\r
-void j2k_read_qcx(int compno, int len)\r
-{\r
- int tmp;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
- int bandno, numbands;\r
- \r
- tcp =\r
- j2k_state ==\r
- J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;\r
- tccp = &tcp->tccps[compno];\r
- tmp = cio_read(1); /* Sqcx */\r
- tccp->qntsty = tmp & 0x1f;\r
- tccp->numgbits = tmp >> 5;\r
- numbands =\r
- tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty ==\r
- J2K_CCP_QNTSTY_NOQNT ?\r
- len - 1 : (len - 1) / 2);\r
- for (bandno = 0; bandno < numbands; bandno++) {\r
- int expn, mant;\r
- if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {\r
- expn = cio_read(1) >> 3; /* SPqcx_i */\r
- mant = 0;\r
- } else {\r
- tmp = cio_read(2); /* SPqcx_i */\r
- expn = tmp >> 11;\r
- mant = tmp & 0x7ff;\r
- }\r
- tccp->stepsizes[bandno].expn = expn;\r
- tccp->stepsizes[bandno].mant = mant;\r
- }\r
- \r
- \r
- \r
- /* Add Antonin : if scalar_derived -> compute other stepsizes */\r
- \r
- \r
- \r
- if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {\r
- \r
- for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {\r
- \r
- tccp->stepsizes[bandno].expn =\r
- ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) >\r
- 0) ? (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;\r
- \r
- tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;\r
- \r
- }\r
- \r
- }\r
- \r
- \r
- \r
- /* ddA */\r
-}\r
-\r
-void j2k_write_qcd()\r
-{\r
- int lenp, len;\r
- \r
- cio_write(J2K_MS_QCD, 2); /* QCD */\r
- lenp = cio_tell();\r
- cio_skip(2);\r
- j2k_write_qcx(0);\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2); /* Lqcd */\r
- cio_seek(lenp + len);\r
-}\r
-\r
-void j2k_read_qcd()\r
-{\r
- int len, i, pos;\r
- \r
- len = cio_read(2); /* Lqcd */\r
- pos = cio_tell();\r
- for (i = 0; i < j2k_img->numcomps; i++) {\r
- cio_seek(pos);\r
- j2k_read_qcx(i, len - 2);\r
- }\r
-}\r
-\r
-void j2k_write_qcc(int compno)\r
-{\r
- int lenp, len;\r
- \r
- cio_write(J2K_MS_QCC, 2); /* QCC */\r
- lenp = cio_tell();\r
- cio_skip(2);\r
- cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */\r
- j2k_write_qcx(compno);\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2); /* Lqcc */\r
- cio_seek(lenp + len);\r
-}\r
-\r
-void j2k_read_qcc()\r
-{\r
- int len, compno;\r
- \r
- len = cio_read(2); /* Lqcc */\r
- compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */\r
- j2k_read_qcx(compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2));\r
-}\r
-\r
-void j2k_write_poc()\r
-{\r
- int len, numpchgs, i;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
- \r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- tccp = &tcp->tccps[0];\r
- numpchgs = tcp->numpocs;\r
- cio_write(J2K_MS_POC, 2); /* POC */\r
- len = 2 + (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)) * numpchgs;\r
- cio_write(len, 2); /* Lpoc */\r
- for (i = 0; i < numpchgs; i++) {\r
- /* MODIF*/\r
- j2k_poc_t *poc;\r
- poc = &tcp->pocs[i];\r
- cio_write(poc->resno0, 1); /* RSpoc_i */\r
- cio_write(poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CSpoc_i */\r
- cio_write(poc->layno1, 2); /* LYEpoc_i */\r
- poc->layno1 = int_min(poc->layno1, tcp->numlayers);\r
- cio_write(poc->resno1, 1); /* REpoc_i */\r
- poc->resno1 = int_min(poc->resno1, tccp->numresolutions);\r
- cio_write(poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CEpoc_i */\r
- poc->compno1 = int_min(poc->compno1, j2k_img->numcomps);\r
- cio_write(poc->prg, 1); /* Ppoc_i */\r
- }\r
-}\r
-\r
-void j2k_read_poc()\r
-{\r
- int len, numpchgs, i, old_poc;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
- \r
- tcp =\r
- j2k_state ==\r
- J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;\r
- \r
- old_poc = tcp->POC ? tcp->numpocs + 1 : 0;\r
- tcp->POC = 1;\r
- tccp = &tcp->tccps[0];\r
- len = cio_read(2); /* Lpoc */\r
- numpchgs = (len - 2) / (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2));\r
- \r
- for (i = old_poc; i < numpchgs + old_poc; i++) {\r
- j2k_poc_t *poc;\r
- poc = &tcp->pocs[i];\r
- poc->resno0 = cio_read(1); /* RSpoc_i */\r
- poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */\r
- poc->layno1 = int_min(cio_read(2), (unsigned int) tcp->numlayers); /* LYEpoc_i */\r
- poc->resno1 = int_min(cio_read(1), (unsigned int) tccp->numresolutions); /* REpoc_i */\r
- poc->compno1 = int_min(cio_read(j2k_img->numcomps <= 256 ? 1 : 2), (unsigned int) j2k_img->numcomps); /* CEpoc_i */\r
- poc->prg = cio_read(1); /* Ppoc_i */\r
- }\r
- \r
- tcp->numpocs = numpchgs + old_poc - 1;\r
-}\r
-\r
-void j2k_read_crg()\r
-{\r
- int len, i, Xcrg_i, Ycrg_i;\r
- \r
- len = cio_read(2); /* Lcrg */\r
- for (i = 0; i < j2k_img->numcomps; i++) {\r
- Xcrg_i = cio_read(2); /* Xcrg_i */\r
- Ycrg_i = cio_read(2); /* Ycrg_i */\r
- }\r
-}\r
-\r
-void j2k_read_tlm()\r
-{\r
- int len, Ztlm, Stlm, ST, SP, tile_tlm, i;\r
- long int Ttlm_i, Ptlm_i;\r
- \r
- len = cio_read(2); /* Ltlm */\r
- Ztlm = cio_read(1); /* Ztlm */\r
- Stlm = cio_read(1); /* Stlm */\r
- ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);\r
- SP = (Stlm >> 6) & 0x01;\r
- tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);\r
- for (i = 0; i < tile_tlm; i++) {\r
- Ttlm_i = cio_read(ST); /* Ttlm_i */\r
- Ptlm_i = cio_read(SP ? 4 : 2); /* Ptlm_i */\r
- }\r
-}\r
-\r
-void j2k_read_plm()\r
-{\r
- int len, i, Zplm, Nplm, add, packet_len = 0;\r
- \r
- len = cio_read(2); /* Lplm */\r
- Zplm = cio_read(1); /* Zplm */\r
- len -= 3;\r
- while (len > 0) {\r
- Nplm = cio_read(4); /* Nplm */\r
- len -= 4;\r
- for (i = Nplm; i > 0; i--) {\r
- add = cio_read(1);\r
- len--;\r
- packet_len = (packet_len << 7) + add; /* Iplm_ij */\r
- if ((add & 0x80) == 0) {\r
- /* New packet */\r
- packet_len = 0;\r
- }\r
- if (len <= 0)\r
- break;\r
- }\r
- }\r
-}\r
-\r
-void j2k_read_plt()\r
-{\r
- int len, i, Zplt, packet_len = 0, add;\r
- \r
- len = cio_read(2); /* Lplt */\r
- Zplt = cio_read(1); /* Zplt */\r
- for (i = len - 3; i > 0; i--) {\r
- add = cio_read(1);\r
- packet_len = (packet_len << 7) + add; /* Iplt_i */\r
- if ((add & 0x80) == 0) {\r
- /* New packet */\r
- packet_len = 0;\r
- }\r
- }\r
-}\r
-\r
-void j2k_read_ppm()\r
-{\r
- int len, Z_ppm, i, j;\r
- int N_ppm;\r
- \r
- len = cio_read(2);\r
- j2k_cp->ppm = 1;\r
- \r
- Z_ppm = cio_read(1); /* Z_ppm */\r
- len -= 3;\r
- while (len > 0) {\r
- if (j2k_cp->ppm_previous == 0) {\r
- N_ppm = cio_read(4); /* N_ppm */\r
- len -= 4;\r
- } else {\r
- N_ppm = j2k_cp->ppm_previous;\r
- }\r
- \r
- j = j2k_cp->ppm_store;\r
- if (Z_ppm == 0) { /* First PPM marker */\r
- j2k_cp->ppm_data =\r
- (unsigned char *) calloc(N_ppm, sizeof(unsigned char));\r
- j2k_cp->ppm_data_first = j2k_cp->ppm_data;\r
- \r
- j2k_cp->ppm_len = N_ppm; /*Add antonin : ppmbug1*/\r
- \r
- } else { /* NON-first PPM marker */\r
- j2k_cp->ppm_data =\r
- (unsigned char *) realloc(j2k_cp->ppm_data,\r
- (N_ppm +\r
- j2k_cp->ppm_store) *\r
- sizeof(unsigned char));\r
- j2k_cp->ppm_data_first = j2k_cp->ppm_data;\r
- \r
- j2k_cp->ppm_len = N_ppm + j2k_cp->ppm_store; /*Add antonin : ppmbug1*/\r
- \r
- }\r
- \r
- for (i = N_ppm; i > 0; i--) { /* Read packet header */\r
- j2k_cp->ppm_data[j] = cio_read(1);\r
- j++;\r
- len--;\r
- if (len == 0)\r
- break; /* Case of non-finished packet header in present marker but finished in next one */\r
- }\r
- \r
- j2k_cp->ppm_previous = i - 1;\r
- j2k_cp->ppm_store = j;\r
- }\r
-}\r
-\r
-void j2k_read_ppt()\r
-{\r
- int len, Z_ppt, i, j = 0;\r
- j2k_tcp_t *tcp;\r
- \r
- len = cio_read(2);\r
- Z_ppt = cio_read(1);\r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- tcp->ppt = 1;\r
- if (Z_ppt == 0) { /* First PPT marker */\r
- tcp->ppt_data =\r
- (unsigned char *) calloc(len - 3, sizeof(unsigned char));\r
- tcp->ppt_data_first = tcp->ppt_data;\r
- tcp->ppt_store = 0;\r
- \r
- tcp->ppt_len = len - 3; /*Add antonin : ppmbug1*/\r
- } else { /* NON-first PPT marker */\r
- tcp->ppt_data =\r
- (unsigned char *) realloc(tcp->ppt_data,\r
- (len - 3 +\r
- tcp->ppt_store) * sizeof(unsigned char));\r
- tcp->ppt_data_first = tcp->ppt_data;\r
- \r
- tcp->ppt_len = len - 3 + tcp->ppt_store; /*Add antonin : ppmbug1*/\r
- \r
- }\r
- \r
- j = tcp->ppt_store;\r
- for (i = len - 3; i > 0; i--) {\r
- tcp->ppt_data[j] = cio_read(1);\r
- j++;\r
- }\r
- tcp->ppt_store = j;\r
-}\r
-\r
-void j2k_write_sot()\r
-{\r
- int lenp, len;\r
- \r
- j2k_sot_start = cio_tell();\r
- cio_write(J2K_MS_SOT, 2); /* SOT */\r
- lenp = cio_tell();\r
- cio_skip(2); /* Lsot (further) */\r
- cio_write(j2k_curtileno, 2); /* Isot */\r
- cio_skip(4); /* Psot (further in j2k_write_sod) */\r
- cio_write(0, 1); /* TPsot */\r
- cio_write(1, 1); /* TNsot */\r
- len = cio_tell() - lenp;\r
- cio_seek(lenp);\r
- cio_write(len, 2); /* Lsot */\r
- cio_seek(lenp + len);\r
-}\r
-\r
-void j2k_read_sot()\r
-{\r
- int len, tileno, totlen, partno, numparts, i;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tmp;\r
- char status = 0;\r
- \r
- len = cio_read(2);\r
- tileno = cio_read(2);\r
- \r
- if (j2k_cp->tileno_size == 0) {\r
- j2k_cp->tileno[j2k_cp->tileno_size] = tileno;\r
- j2k_cp->tileno_size++;\r
- } else {\r
- i = 0;\r
- while (i < j2k_cp->tileno_size && status == 0) {\r
- status = j2k_cp->tileno[i] == tileno ? 1 : 0;\r
- i++;\r
- }\r
- if (status == 0) {\r
- j2k_cp->tileno[j2k_cp->tileno_size] = tileno;\r
- j2k_cp->tileno_size++;\r
- }\r
- }\r
- \r
- totlen = cio_read(4);\r
- if (!totlen)\r
- totlen = cio_numbytesleft() + 8;\r
- \r
- partno = cio_read(1);\r
- numparts = cio_read(1);\r
- \r
- j2k_curtileno = tileno;\r
- j2k_eot = cio_getbp() - 12 + totlen;\r
- j2k_state = J2K_STATE_TPH;\r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- \r
- if (tcp->first == 1) {\r
- tmp = tcp->tccps;\r
- *tcp = j2k_default_tcp;\r
- \r
- /* Initialization PPT */\r
- tcp->ppt = 0;\r
- tcp->ppt_data = NULL;\r
- tcp->ppt_data_first = NULL;\r
- \r
- tcp->tccps = tmp;\r
- for (i = 0; i < j2k_img->numcomps; i++) {\r
- tcp->tccps[i] = j2k_default_tcp.tccps[i];\r
- }\r
- j2k_cp->tcps[j2k_curtileno].first = 0;\r
- }\r
-}\r
-\r
-void j2k_write_sod()\r
-{\r
- int l, layno;\r
- int totlen;\r
- j2k_tcp_t *tcp;\r
- static int j2k_sod_start;\r
- \r
- cio_write(J2K_MS_SOD, 2);\r
- if (j2k_curtileno == 0) {\r
- j2k_sod_start = cio_tell() + pos_correction;\r
- }\r
- \r
- /* INDEX >> */\r
- if (info_IM.index_on) {\r
- info_IM.tile[j2k_curtileno].end_header =\r
- cio_tell() + pos_correction - 1;\r
- }\r
- /* << INDEX */\r
- \r
- tcp = &j2k_cp->tcps[j2k_curtileno];\r
- for (layno = 0; layno < tcp->numlayers; layno++) {\r
- tcp->rates[layno] -= tcp->rates[layno] ? (j2k_sod_start / (j2k_cp->th * j2k_cp->tw)) : 0; /*Mod antonin losslessbug*/\r
- }\r
- \r
- info_IM.num = 0;\r
- if (j2k_cp->decod_format != PGX_DFMT)\r
- l = tcd_encode_tile_pxm(j2k_curtileno, cio_getbp(),\r
- cio_numbytesleft() - 2, &info_IM);\r
- else\r
- l = tcd_encode_tile_pgx(j2k_curtileno, cio_getbp(),\r
- cio_numbytesleft() - 2, &info_IM);\r
- \r
- /* Writing Psot in SOT marker */\r
- totlen = cio_tell() + l - j2k_sot_start;\r
- cio_seek(j2k_sot_start + 6);\r
- cio_write(totlen, 4);\r
- cio_seek(j2k_sot_start + totlen);\r
-}\r
-\r
-void j2k_read_sod()\r
-{\r
- int len, truncate = 0, i;\r
- unsigned char *data;\r
- \r
- len = int_min(j2k_eot - cio_getbp(), cio_numbytesleft() + 1);\r
- \r
- if (len == cio_numbytesleft() + 1)\r
- truncate = 1; /* Case of a truncate codestream */\r
- \r
- data =\r
- (unsigned char *) malloc((j2k_tile_len[j2k_curtileno] + len) *\r
- sizeof(unsigned char));\r
- for (i = 0; i < j2k_tile_len[j2k_curtileno]; i++)\r
- data[i] = j2k_tile_data[j2k_curtileno][i];\r
- for (i = 0; i < len; i++)\r
- data[i + j2k_tile_len[j2k_curtileno]] = cio_read(1);\r
- \r
- j2k_tile_len[j2k_curtileno] += len;\r
- free(j2k_tile_data[j2k_curtileno]);\r
- j2k_tile_data[j2k_curtileno] = data;\r
- data = NULL;\r
- \r
- if (!truncate)\r
- j2k_state = J2K_STATE_TPHSOT;\r
- else\r
- j2k_state = J2K_STATE_NEOC; /* RAJOUTE !! */\r
-}\r
-\r
-void j2k_write_rgn(int compno, int tileno)\r
-{\r
- j2k_tcp_t *tcp = &j2k_cp->tcps[tileno];\r
- \r
- cio_write(J2K_MS_RGN, 2); /* RGN */\r
- cio_write(j2k_img->numcomps <= 256 ? 5 : 6, 2); /* Lrgn */\r
- cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */\r
- cio_write(0, 1); /* Srgn */\r
- cio_write(tcp->tccps[compno].roishift, 1); /* SPrgn */\r
-}\r
-\r
-void j2k_read_rgn()\r
-{\r
- int len, compno, roisty;\r
- j2k_tcp_t *tcp;\r
- \r
- tcp =\r
- j2k_state ==\r
- J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;\r
- len = cio_read(2); /* Lrgn */\r
- compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */\r
- roisty = cio_read(1); /* Srgn */\r
- tcp->tccps[compno].roishift = cio_read(1); /* SPrgn */\r
-}\r
-\r
-void j2k_write_eoc()\r
-{\r
- /* fprintf(stderr, "%.8x: EOC\n", cio_tell() + pos_correction); */\r
- cio_write(J2K_MS_EOC, 2);\r
-}\r
-\r
-void j2k_read_eoc()\r
-{\r
- int i, tileno;\r
-\r
-#ifndef NO_PACKETS_DECODING \r
- tcd_init(j2k_img, j2k_cp);\r
-#endif\r
- \r
- for (i = 0; i < j2k_cp->tileno_size; i++) {\r
- tileno = j2k_cp->tileno[i];\r
-#ifndef NO_PACKETS_DECODING \r
- tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno);\r
-#endif\r
- free(j2k_tile_data[tileno]);\r
- }\r
- \r
- j2k_state = J2K_STATE_MT;\r
- longjmp(j2k_error, 1);\r
-}\r
-\r
-void j2k_read_unk()\r
-{\r
- fprintf(stderr, "warning: unknown marker\n");\r
-}\r
-\r
-LIBJ2K_API int\r
-j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *output,\r
- int len, char *index)\r
-{\r
- int tileno, compno, layno, resno, precno, pack_nb, x, y;\r
- unsigned char *dest = NULL;\r
- FILE *INDEX = NULL;\r
- FILE *f = NULL;\r
- \r
- if (setjmp(j2k_error)) {\r
- return 0;\r
- }\r
- \r
- if (cp->intermed_file == 1) {\r
- f = fopen(output, "wb");\r
- if (!f) {\r
- fprintf(stderr, "failed to open %s for writing\n", output);\r
- return 1;\r
- }\r
- dest = (unsigned char *) malloc(len);\r
- cio_init(dest, len);\r
- }\r
- \r
- j2k_img = img;\r
- j2k_cp = cp;\r
- /* j2k_dump_cp(j2k_img, j2k_cp); */\r
- \r
- /* INDEX >> */\r
- info_IM.index_on = j2k_cp->index_on;\r
- if (info_IM.index_on) {\r
- info_IM.tile =\r
- (info_tile *) malloc(j2k_cp->tw * j2k_cp->th * sizeof(info_tile));\r
- info_IM.Im_w = j2k_img->x1 - j2k_img->x0;\r
- info_IM.Im_h = j2k_img->y1 - j2k_img->y0;\r
- info_IM.Prog = (&j2k_cp->tcps[0])->prg;\r
- info_IM.tw = j2k_cp->tw;\r
- info_IM.th = j2k_cp->th;\r
- info_IM.Tile_x = j2k_cp->tdx; /* new version parser */\r
- info_IM.Tile_y = j2k_cp->tdy; /* new version parser */\r
- info_IM.Tile_Ox = j2k_cp->tx0; /* new version parser */\r
- info_IM.Tile_Oy = j2k_cp->ty0; /* new version parser */\r
- info_IM.Comp = j2k_img->numcomps;\r
- info_IM.Layer = (&j2k_cp->tcps[0])->numlayers;\r
- info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1;\r
- info_IM.D_max = 0; /* ADD Marcela */\r
- }\r
- /* << INDEX */\r
- \r
- j2k_write_soc();\r
- j2k_write_siz();\r
- j2k_write_cod();\r
- j2k_write_qcd();\r
- for (compno = 0; compno < j2k_img->numcomps; compno++) {\r
- j2k_tcp_t *tcp = &j2k_cp->tcps[0];\r
- if (tcp->tccps[compno].roishift)\r
- j2k_write_rgn(compno, 0);\r
- }\r
- if (j2k_cp->comment != NULL)\r
- j2k_write_com();\r
- \r
- if (cp->intermed_file == 1) {\r
- /* Writing the main header */\r
- pos_correction = cio_tell();\r
- fwrite(dest, 1, cio_tell(), f);\r
- }\r
- \r
- /* INDEX >> */\r
- if (info_IM.index_on) {\r
- info_IM.Main_head_end = cio_tell() - 1;\r
- }\r
- /* << INDEX */\r
- \r
- \r
- for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {\r
- fprintf(stdout, "Tile number %d / %d ", tileno + 1, cp->tw * cp->th);\r
- \r
- if (cp->intermed_file == 1) {\r
- /* new dest for each tile */\r
- free(dest);\r
- dest = (unsigned char *) malloc(len);\r
- cio_init(dest, len);\r
- }\r
- j2k_curtileno = tileno;\r
- /* initialisation before tile encoding */\r
- \r
- if (tileno == 0) {\r
- tcd_malloc_encode(j2k_img, j2k_cp, j2k_curtileno);\r
- } else {\r
- tcd_init_encode(j2k_img, j2k_cp, j2k_curtileno);\r
- }\r
- \r
- /* INDEX >> */\r
- if (info_IM.index_on) {\r
- info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno;\r
- info_IM.tile[j2k_curtileno].start_pos = cio_tell() + pos_correction;\r
- }\r
- /* << INDEX */\r
- j2k_write_sot();\r
- \r
- for (compno = 1; compno < img->numcomps; compno++) {\r
- j2k_write_coc(compno);\r
- j2k_write_qcc(compno);\r
- }\r
- \r
- if (cp->tcps[tileno].numpocs)\r
- j2k_write_poc();\r
- j2k_write_sod();\r
- \r
- /* INDEX >> */\r
- if (info_IM.index_on) {\r
- info_IM.tile[j2k_curtileno].end_pos =\r
- cio_tell() + pos_correction - 1;\r
- }\r
- /* << INDEX */\r
- \r
- /*\r
- if (tile->PPT) BAD PPT !!!\r
- {\r
- FILE *PPT_file;\r
- \r
- int i;\r
- PPT_file=fopen("PPT","rb");\r
- fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);\r
- for (i=0;i<tile->len_ppt;i++)\r
- {\r
- unsigned char elmt;\r
- fread(&elmt, 1, 1, PPT_file);\r
- fwrite(&elmt,1,1,f);\r
- }\r
- fclose(PPT_file);\r
- unlink("PPT");\r
- }\r
- */\r
- if (cp->intermed_file == 1) {\r
- fwrite(dest, 1, cio_tell(), f);\r
- pos_correction = cio_tell() + pos_correction;\r
- }\r
- }\r
- \r
- if (cp->intermed_file == 1) {\r
- free(dest);\r
- dest = (unsigned char *) malloc(len);\r
- cio_init(dest, len);\r
- }\r
- \r
- j2k_write_eoc();\r
- \r
- if (cp->intermed_file == 1) {\r
- fwrite(dest, 1, 2, f);\r
- free(dest);\r
- /* closing file *.j2k */\r
- fclose(f);\r
- }\r
- \r
- /* Creation of the index file */\r
- \r
- if (info_IM.index_on) {\r
- \r
- double DistoTotal = 0;\r
- \r
- info_IM.codestream_size = cio_tell() + pos_correction; /* Correction 14/4/03 suite rmq de Patrick */\r
- \r
- INDEX = fopen(index, "w");\r
- \r
- \r
- \r
- if (!INDEX) {\r
- \r
- fprintf(stderr, "failed to open %s for writing\n", index);\r
- \r
- return 1;\r
- \r
- }\r
- \r
- fprintf(INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h);\r
-\r
- fprintf(INDEX, "%d\n", info_IM.Prog);\r
-\r
- fprintf(INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y);\r
-\r
- fprintf(INDEX, "%d %d\n", info_IM.tw, info_IM.th);\r
-\r
- fprintf(INDEX, "%d\n", info_IM.Comp);\r
-\r
- fprintf(INDEX, "%d\n", info_IM.Layer);\r
-\r
- fprintf(INDEX, "%d\n", info_IM.Decomposition);\r
-\r
- for (resno = info_IM.Decomposition; resno >= 0; resno--) {\r
-\r
- fprintf(INDEX, "[%d,%d] ", (1 << info_IM.tile[0].pdx[resno]), (1 << info_IM.tile[0].pdx[resno])); /*based on tile 0*/\r
-\r
- }\r
-\r
- fprintf(INDEX, "\n");\r
-\r
- fprintf(INDEX, "%d\n", info_IM.Main_head_end);\r
-\r
- fprintf(INDEX, "%d\n", info_IM.codestream_size);\r
-\r
- for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) {\r
-\r
- fprintf(INDEX, "%4d %9d %9d %9d %9e %9d %9e\n",\r
- info_IM.tile[tileno].num_tile,\r
- info_IM.tile[tileno].start_pos,\r
- info_IM.tile[tileno].end_header,\r
- info_IM.tile[tileno].end_pos,\r
- info_IM.tile[tileno].distotile, info_IM.tile[tileno].nbpix,\r
- info_IM.tile[tileno].distotile / info_IM.tile[tileno].nbpix);\r
-\r
- }\r
-\r
- for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) {\r
-\r
- int start_pos, end_pos;\r
-\r
- double disto = 0;\r
-\r
- pack_nb = 0;\r
-\r
- /* fprintf(INDEX,\r
-\r
- "pkno tileno layerno resno compno precno start_pos end_pos deltaSE \n"); */\r
- \r
- if (info_IM.Prog == 0) { /* LRCP */\r
- /*fprintf(INDEX, "pack_nb tileno layno resno compno precno start_pos end_pos disto");*/\r
- for (layno = 0; layno < info_IM.Layer; layno++) {\r
- for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {\r
- for (compno = 0; compno < info_IM.Comp; compno++) {\r
- for (precno = 0;\r
- precno <\r
- info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];\r
- precno++) {\r
- start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;\r
- end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;\r
- disto = info_IM.tile[tileno].packet[pack_nb].disto;\r
- fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",\r
- pack_nb, tileno, layno, resno, compno, precno,\r
- start_pos, end_pos, disto);\r
- DistoTotal += disto;\r
- pack_nb++;\r
- }\r
- }\r
- }\r
- }\r
- } else if (info_IM.Prog == 1) { /* RLCP */\r
- /*fprintf(INDEX, "pack_nb tileno resno layno compno precno start_pos end_pos disto");*/\r
- for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {\r
- for (layno = 0; layno < info_IM.Layer; layno++) {\r
- for (compno = 0; compno < info_IM.Comp; compno++) {\r
- for (precno = 0; precno < info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno]; precno++) {\r
- start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;\r
- end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;\r
- disto = info_IM.tile[tileno].packet[pack_nb].disto;\r
- fprintf(INDEX, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",\r
- pack_nb, tileno, resno, layno, compno, precno,\r
- start_pos, end_pos, disto);\r
- DistoTotal += disto;\r
- pack_nb++;\r
- }\r
- }\r
- }\r
- }\r
- } else if (info_IM.Prog == 2) { /* RPCL */\r
- /*fprintf(INDEX, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n"); */\r
- for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {\r
- /* I suppose components have same XRsiz, YRsiz */\r
- int x0 = info_IM.Tile_Ox + tileno - (int)floor( tileno/info_IM.tw ) * info_IM.tw * info_IM.Tile_x;\r
- int y0 = info_IM.Tile_Ox + (int)floor( tileno/info_IM.tw ) * info_IM.Tile_y;\r
- int x1 = x0 + info_IM.Tile_x;\r
- int y1 = y0 + info_IM.Tile_y;\r
- for (y=y0; y<y1; y++) {\r
- for (x=x0; x<x1; x++) {\r
- for (compno = 0; compno < info_IM.Comp; compno++) {\r
- int prec_max = info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];\r
- for (precno = 0; precno < prec_max; precno++) {\r
- int pcnx = info_IM.tile[tileno].pw[resno];\r
- int pcx = (int) pow( 2, info_IM.tile[tileno].pdx[resno] + info_IM.Decomposition - resno );\r
- int pcy = (int) pow( 2, info_IM.tile[tileno].pdy[resno] + info_IM.Decomposition - resno );\r
- int precno_x = precno - (int) floor( precno/pcnx ) * pcnx;\r
- int precno_y = (int) floor( precno/pcnx ); \r
- if (precno_y*pcy == y ) {\r
- if (precno_x*pcx == x ) {\r
- for (layno = 0; layno < info_IM.Layer; layno++) {\r
- start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;\r
- end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;\r
- disto = info_IM.tile[tileno].packet[pack_nb].disto;\r
- fprintf(INDEX, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",\r
- pack_nb, tileno, resno, precno, compno, layno,\r
- start_pos, end_pos, disto); \r
- DistoTotal += disto;\r
- pack_nb++; \r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- } else if (info_IM.Prog == 3) { /* PCRL */\r
- /* I suppose components have same XRsiz, YRsiz*/ \r
- int x0 = info_IM.Tile_Ox + tileno - (int)floor( tileno/info_IM.tw ) * info_IM.tw * info_IM.Tile_x;\r
- int y0 = info_IM.Tile_Ox + (int)floor( tileno/info_IM.tw ) * info_IM.Tile_y;\r
- int x1 = x0 + info_IM.Tile_x;\r
- int y1 = y0 + info_IM.Tile_y;\r
- \r
- /*fprintf(INDEX, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n"); */\r
- for (y=y0; y<y1; y++) {\r
- for (x=x0; x<x1; x++) {\r
- for (compno = 0; compno < info_IM.Comp; compno++) {\r
- for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {\r
- int prec_max = info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];\r
- for (precno = 0; precno < prec_max; precno++) {\r
- int pcnx = info_IM.tile[tileno].pw[resno];\r
- int pcx = (int) pow( 2, info_IM.tile[tileno].pdx[resno] + info_IM.Decomposition - resno );\r
- int pcy = (int) pow( 2, info_IM.tile[tileno].pdy[resno] + info_IM.Decomposition - resno );\r
- int precno_x = precno - (int) floor( precno/pcnx ) * pcnx;\r
- int precno_y = (int) floor( precno/pcnx ); \r
- if (precno_y*pcy == y ) {\r
- if (precno_x*pcx == x ) {\r
- for (layno = 0; layno < info_IM.Layer; layno++) {\r
- start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;\r
- end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;\r
- disto = info_IM.tile[tileno].packet[pack_nb].disto;\r
- fprintf(INDEX, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",\r
- pack_nb, tileno, precno, compno, resno, layno,\r
- start_pos, end_pos, disto); \r
- DistoTotal += disto;\r
- pack_nb++; \r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- } else { /* CPRL */\r
- /*fprintf(INDEX, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n"); */\r
- for (compno = 0; compno < info_IM.Comp; compno++) {\r
- /* I suppose components have same XRsiz, YRsiz */\r
- int x0 = info_IM.Tile_Ox + tileno - (int)floor( tileno/info_IM.tw ) * info_IM.tw * info_IM.Tile_x;\r
- int y0 = info_IM.Tile_Ox + (int)floor( tileno/info_IM.tw ) * info_IM.Tile_y;\r
- int x1 = x0 + info_IM.Tile_x;\r
- int y1 = y0 + info_IM.Tile_y;\r
- for (y=y0; y<y1; y++) {\r
- for (x=x0; x<x1; x++) {\r
- for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {\r
- int prec_max = info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];\r
- for (precno = 0; precno < prec_max; precno++) {\r
- int pcnx = info_IM.tile[tileno].pw[resno];\r
- int pcx = (int) pow( 2, info_IM.tile[tileno].pdx[resno] + info_IM.Decomposition - resno );\r
- int pcy = (int) pow( 2, info_IM.tile[tileno].pdy[resno] + info_IM.Decomposition - resno );\r
- int precno_x = precno - (int) floor( precno/pcnx ) * pcnx;\r
- int precno_y = (int) floor( precno/pcnx ); \r
- if (precno_y*pcy == y ) {\r
- if (precno_x*pcx == x ) {\r
- for (layno = 0; layno < info_IM.Layer; layno++) {\r
- start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;\r
- end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;\r
- disto = info_IM.tile[tileno].packet[pack_nb].disto;\r
- fprintf(INDEX, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",\r
- pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); \r
- DistoTotal += disto;\r
- pack_nb++; \r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- } \r
- }\r
- \r
- fprintf(INDEX, "%8e\n", info_IM.D_max); /*SE max*/\r
- \r
- fprintf(INDEX, "%.8e\n", DistoTotal); /* SE totale*/\r
- \r
- fclose(INDEX);\r
- \r
- }\r
- \r
- j2k_clean();\r
- \r
- return cio_tell();\r
-}\r
-\r
-typedef struct {\r
- int id;\r
- int states;\r
- void (*handler) ();\r
-} j2k_dec_mstabent_t;\r
-\r
-j2k_dec_mstabent_t j2k_dec_mstab[] = {\r
- {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},\r
- {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},\r
- {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},\r
- {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},\r
- {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},\r
- {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},\r
- {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},\r
- {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},\r
- {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},\r
- {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},\r
- {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},\r
- {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},\r
- {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},\r
- {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},\r
- {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},\r
- {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},\r
- {J2K_MS_SOP, 0, 0},\r
- {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},\r
- {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},\r
- {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}\r
-};\r
-\r
-j2k_dec_mstabent_t *j2k_dec_mstab_lookup(int id)\r
-{\r
- j2k_dec_mstabent_t *e;\r
- for (e = j2k_dec_mstab; e->id != 0; e++) {\r
- if (e->id == id) {\r
- break;\r
- }\r
- }\r
- return e;\r
-}\r
-\r
-\r
-\r
-LIBJ2K_API int\r
-j2k_decode(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp)\r
-{\r
- \r
- if (setjmp(j2k_error)) {\r
- if (j2k_state != J2K_STATE_MT) {\r
- fprintf(stderr, "WARNING: incomplete bitstream\n");\r
- return 0;\r
- }\r
- j2k_clean();\r
- return cio_numbytes(); /* Correct way of ending j2k_decode */\r
- }\r
- \r
- j2k_img = img;\r
- \r
- j2k_cp = cp;\r
- \r
- j2k_state = J2K_STATE_MHSOC;\r
- cio_init(src, len);\r
- \r
- for (;;) {\r
- \r
- \r
- j2k_dec_mstabent_t *e;\r
- int id = cio_read(2);\r
- if (id >> 8 != 0xff) {\r
- fprintf(stderr, "%.8x: expected a marker instead of %x\n",\r
- cio_tell() - 2, id);\r
- return 0;\r
- }\r
- e = j2k_dec_mstab_lookup(id);\r
- if (!(j2k_state & e->states)) {\r
- fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id);\r
- return 0;\r
- }\r
- if (e->handler) {\r
- (*e->handler) ();\r
- }\r
- \r
- \r
- if (j2k_state == J2K_STATE_NEOC)\r
- break; /* RAJOUTE */\r
- }\r
- if (j2k_state == J2K_STATE_NEOC)\r
- j2k_read_eoc(); /* RAJOUTE */\r
- \r
- return 0;\r
-}\r
-\r
-/*\r
-* Read a JPT-stream and decode file\r
-*\r
-*/\r
-int\r
-j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,\r
- j2k_cp_t * cp)\r
-{\r
- jpt_msg_header_struct_t header;\r
- int position;\r
- \r
- if (setjmp(j2k_error)) {\r
- if (j2k_state != J2K_STATE_MT) {\r
- fprintf(stderr, "WARNING: incomplete bitstream\n");\r
- return 0;\r
- }\r
- return cio_numbytes();\r
- }\r
- \r
- j2k_img = img;\r
- \r
- j2k_cp = cp;\r
- \r
- j2k_state = J2K_STATE_MHSOC;\r
- cio_init(src, len);\r
- \r
- /* Initialize the header */\r
- jpt_init_Msg_Header(&header);\r
- /* Read the first header of the message */\r
- jpt_read_Msg_Header(&header);\r
- \r
- position = cio_tell();\r
- if (header.Class_Id != 6) { /* 6 : Main header data-bin message */\r
- fprintf(stderr,\r
- "[JPT-stream] : Expecting Main header first [class_Id %d] !\n",\r
- header.Class_Id);\r
- return 0;\r
- }\r
- \r
- for (;;) {\r
- j2k_dec_mstabent_t *e;\r
- int id;\r
- \r
- if (!cio_numbytesleft()) {\r
- j2k_read_eoc();\r
- return 0;\r
- }\r
- /* data-bin read -> need to read a new header */\r
- if ((unsigned int) (cio_tell() - position) == header.Msg_length) {\r
- jpt_read_Msg_Header(&header);\r
- position = cio_tell();\r
- if (header.Class_Id != 4) { /* 4 : Tile data-bin message */\r
- fprintf(stderr, "[JPT-stream] : Expecting Tile info !\n");\r
- return 0;\r
- }\r
- }\r
- \r
- id = cio_read(2);\r
- if (id >> 8 != 0xff) {\r
- fprintf(stderr, "%.8x: expected a marker instead of %x\n",\r
- cio_tell() - 2, id);\r
- return 0;\r
- }\r
- e = j2k_dec_mstab_lookup(id);\r
- if (!(j2k_state & e->states)) {\r
- fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id);\r
- return 0;\r
- }\r
- if (e->handler) {\r
- (*e->handler) ();\r
- }\r
- if (j2k_state == J2K_STATE_NEOC)\r
- break; /* RAJOUTE */\r
- }\r
- if (j2k_state == J2K_STATE_NEOC)\r
- j2k_read_eoc(); /* RAJOUTE */\r
- \r
- return 0;\r
-}\r
-\r
-\r
-\r
-void j2k_dec_release()\r
-\r
-{\r
- \r
- int i=0;\r
- \r
- \r
- \r
- /*tcd_dec_release();*/\r
- \r
- \r
- \r
- if (j2k_tile_len!=NULL) free(j2k_tile_len);\r
- \r
- if (j2k_tile_data!=NULL) free(j2k_tile_data);\r
- \r
- if (j2k_default_tcp.ppt_data_first!=NULL) free(j2k_default_tcp.ppt_data_first);\r
- \r
- if (j2k_default_tcp.tccps!=NULL) free(j2k_default_tcp.tccps);\r
- \r
- for (i=0;i<j2k_cp->tw*j2k_cp->th;i++) {\r
- \r
- if (j2k_cp->tcps[i].ppt_data_first!=NULL) free(j2k_cp->tcps[i].ppt_data_first);\r
- \r
- if (j2k_cp->tcps[i].tccps!=NULL) free(j2k_cp->tcps[i].tccps);\r
- \r
- }\r
- \r
- if (j2k_cp->ppm_data_first!=NULL) free(j2k_cp->ppm_data_first);\r
- \r
- if (j2k_cp->tcps!=NULL) free(j2k_cp->tcps);\r
- \r
- if (j2k_img->comps!=NULL) free(j2k_img->comps);\r
- \r
- if (j2k_cp->tileno!=NULL) free(j2k_cp->tileno);\r
- \r
-}\r
-\r
-#ifdef WIN32\r
-#include <windows.h>\r
-\r
-BOOL APIENTRY\r
-DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)\r
-{\r
- switch (ul_reason_for_call) {\r
- case DLL_PROCESS_ATTACH:\r
- case DLL_THREAD_ATTACH:\r
- case DLL_THREAD_DETACH:\r
- case DLL_PROCESS_DETACH:\r
- break;\r
- }\r
- return TRUE;\r
-}\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved. \r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#define VERSION "0.0.8"\r
-\r
-#if defined(_WIN32) && defined (OPENJPEGDLL)\r
-#ifdef gdcmopenjpeg_EXPORTS /*LIBJ2K_EXPORTS*/\r
-#define LIBJ2K_API __declspec(dllexport)\r
-#else\r
-#define LIBJ2K_API __declspec(dllimport)\r
-#endif\r
-#else\r
-#ifdef OPENJPEGSTATIC\r
-#define LIBJ2K_API extern\r
-#else\r
-#define LIBJ2K_API\r
-#endif\r
-#endif\r
-\r
-\r
-#ifndef __J2K_H\r
-#define __J2K_H\r
-\r
-#define J2K_MAXRLVLS 33 /* Number of maximum resolution level authorized */\r
-#define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /* Number of maximum sub-band linked to number of resolution level */\r
-#define J2K_CFMT 0\r
-#define JP2_CFMT 1\r
-#define JPT_CFMT 2\r
-#define MJ2_CFMT 3\r
-#define PXM_DFMT 0\r
-#define PGX_DFMT 1\r
-#define BMP_DFMT 2\r
-#define YUV_DFMT 3\r
-\r
-#define J2K_CP_CSTY_PRT 0x01\r
-#define J2K_CP_CSTY_SOP 0x02\r
-#define J2K_CP_CSTY_EPH 0x04\r
-#define J2K_CCP_CSTY_PRT 0x01\r
-#define J2K_CCP_CBLKSTY_LAZY 0x01\r
-#define J2K_CCP_CBLKSTY_RESET 0x02\r
-#define J2K_CCP_CBLKSTY_TERMALL 0x04\r
-#define J2K_CCP_CBLKSTY_VSC 0x08\r
-#define J2K_CCP_CBLKSTY_PTERM 0x10\r
-#define J2K_CCP_CBLKSTY_SEGSYM 0x20\r
-#define J2K_CCP_QNTSTY_NOQNT 0\r
-#define J2K_CCP_QNTSTY_SIQNT 1\r
-#define J2K_CCP_QNTSTY_SEQNT 2\r
-\r
-typedef struct {\r
- int dx, dy; /* XRsiz, YRsiz */\r
- int w, h; /* width and height of data */\r
- int x0, y0; /* offset of the component compare to the whole image */\r
- int prec; /* precision */\r
- int bpp; /* deapth of image in bits */\r
- int sgnd; /* signed */\r
- int resno_decoded; /* number of decoded resolution */\r
- int factor; /* number of division by 2 of the out image compare to the original size of image */\r
- int *data; /* image-component data */\r
-} j2k_comp_t;\r
-\r
-typedef struct {\r
- int x0, y0; /* XOsiz, YOsiz */\r
- int x1, y1; /* Xsiz, Ysiz */\r
- int numcomps; /* number of components */\r
- int color_space; /* sRGB, Greyscale or YUV */\r
- j2k_comp_t *comps; /* image-components */\r
-} j2k_image_t;\r
-\r
-typedef struct {\r
- int expn; /* exponent */\r
- int mant; /* mantissa */\r
-} j2k_stepsize_t;\r
-\r
-typedef struct {\r
- int csty; /* coding style */\r
- int numresolutions; /* number of resolutions */\r
- int cblkw; /* width of code-blocks */\r
- int cblkh; /* height of code-blocks */\r
- int cblksty; /* code-block coding style */\r
- int qmfbid; /* discrete wavelet transform identifier */\r
- int qntsty; /* quantisation style */\r
- j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantization */\r
- int numgbits; /* number of guard bits */\r
- int roishift; /* Region Of Interest shift */\r
- int prcw[J2K_MAXRLVLS]; /* Precinct width */\r
- int prch[J2K_MAXRLVLS]; /* Precinct height */\r
-} j2k_tccp_t;\r
-\r
-typedef struct {\r
- int resno0, compno0;\r
- int layno1, resno1, compno1;\r
- int prg;\r
- int tile;\r
- char progorder[4];\r
-} j2k_poc_t;\r
-\r
-typedef struct {\r
- int first; /* 1 : first part-tile of a tile */\r
- int csty; /* coding style */\r
- int prg; /* progression order */\r
- int numlayers; /* number of layers */\r
- int mct; /* multi-component transform identifier */\r
- int rates[100]; /* rates of layers */\r
- int numpocs; /* number of progression order changes */\r
- int POC; /* Precise if a POC marker has been used O:NO, 1:YES */\r
- j2k_poc_t pocs[32]; /* progression order changes */\r
- unsigned char *ppt_data; /* packet header store there for futur use in t2_decode_packet */\r
- unsigned char *ppt_data_first; /* pointer remaining on the first byte of the first header if ppt is used */\r
- int ppt; /* If ppt == 1 --> there was a PPT marker for the present tile */\r
- int ppt_store; /* Use in case of multiple marker PPT (number of info already store) */\r
- int ppt_len; /* ppmbug1 */\r
- float distoratio[100]; /* add fixed_quality */\r
- j2k_tccp_t *tccps; /* tile-component coding parameters */\r
-} j2k_tcp_t;\r
-\r
-typedef struct {\r
- int intermed_file; /* 1: Store each encoded tile one by one in the output file (for mega-Images)*/\r
- int decod_format; /* 0: PGX, 1: PxM, 2: BMP */\r
- int cod_format; /* 0: J2K, 1: JP2, 2: JPT */\r
- int disto_alloc; /* Allocation by rate/distortion */\r
- int fixed_alloc; /* Allocation by fixed layer */\r
- int fixed_quality; /* add fixed_quality */\r
- int reduce; /* if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */\r
- int layer; /* if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */\r
- int index_on; /* 0 = no index || 1 = index */\r
- int tx0, ty0; /* XTOsiz, YTOsiz */\r
- int tdx, tdy; /* XTsiz, YTsiz */\r
- char *comment; /* comment for coding */\r
- int tw, th; /* number of tiles in width and heigth */\r
- int *tileno; /* ID number of the tiles present in the codestream */\r
- int tileno_size; /* size of the vector tileno */\r
- unsigned char *ppm_data; /* packet header store there for futur use in t2_decode_packet */\r
- unsigned char *ppm_data_first; /* pointer remaining on the first byte of the first header if ppm is used */\r
- int ppm; /* If ppm == 1 --> there was a PPM marker for the present tile */\r
- int ppm_store; /* Use in case of multiple marker PPM (number of info already store) */\r
- int ppm_previous; /* Use in case of multiple marker PPM (case on non-finished previous info) */\r
- int ppm_len; /* ppmbug1 */\r
- j2k_tcp_t *tcps; /* tile coding parameters */\r
- int *matrice; /* Fixed layer */\r
-} j2k_cp_t;\r
-\r
-typedef struct {\r
- int start_pos, end_pos; /* start and end position */\r
- double disto; /* ADD for Marcela */\r
-} info_packet; /* Index struct */\r
-\r
-typedef struct {\r
- double *thresh; /* value of thresh for each layer by tile cfr. Marcela */\r
- int num_tile; /* Number of Tile */\r
- int start_pos; /* Start position */\r
- int end_header; /* End position of the header */\r
- int end_pos; /* End position */\r
- int pw[33], ph[33]; /* precinct number for each resolution level */\r
-\r
- int pdx[33], pdy[33]; /* precinct size (in power of 2), in X and Y for each resolution level */\r
- info_packet *packet; /* information concerning packets inside tile */\r
- int nbpix; /* add fixed_quality */\r
- double distotile; /* add fixed_quality */\r
-} info_tile; /* index struct */\r
-\r
-typedef struct {\r
- int index_on;\r
- double D_max; /* ADD for Marcela */\r
- int num; /* numero of packet */\r
- int index_write; /* writing the packet inthe index with t2_encode_packets */\r
- int Im_w, Im_h; /* Image width and Height */\r
- int Prog; /* progression order */\r
- int Tile_x, Tile_y; /* Tile size in x and y */\r
- int Tile_Ox, Tile_Oy;\r
- int tw, th; /* Number of Tile in X and Y */\r
- int Comp; /* Component numbers */\r
- int Layer; /* number of layer */\r
- int Decomposition; /* number of decomposition */\r
- int Main_head_end; /* Main header position */\r
- int codestream_size; /* codestream's size */\r
- info_tile *tile; /* information concerning tiles inside image */\r
-} info_image; /* index struct */\r
-\r
-/* \r
- * Encode an image into a JPEG-2000 codestream\r
- * i: image to encode\r
- * cp: coding parameters\r
- * output: destination buffer or name of the output file when cp->intermed_file==1\r
- * len: length of destination buffer\r
- * index : index file name\r
- */\r
-LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *output,\r
- int len, char *index);\r
-\r
-/* LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len); */\r
-/*\r
- * Decode an image from a JPEG-2000 codestream\r
- * src: source buffer\r
- * len: length of source buffer\r
- * i: decode image\r
- * cp: coding parameters that were used to encode the image\r
- */\r
-\r
-LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t * img,\r
- j2k_cp_t * cp);\r
-\r
-\r
-/*\r
- * Decode an image form a JPT-stream (JPEG 2000, JPIP)\r
- * src: source buffer\r
- * len: length of source buffer\r
- * i: decode image\r
- * cp: coding parameters that were used to encode the image\r
- *\r
- */\r
-int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,\r
- j2k_cp_t * cp);\r
-\r
-LIBJ2K_API void j2k_dec_release();/*antonin*/\r
-\r
-#endif\r
+++ /dev/null
-/*\r
-* Copyright (c) 2003-2004, Yannick Verschueren\r
-* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
-* All rights reserved.\r
-*\r
-* Redistribution and use in source and binary forms, with or without\r
-* modification, are permitted provided that the following conditions\r
-* are met:\r
-* 1. Redistributions of source code must retain the above copyright\r
-* notice, this list of conditions and the following disclaimer.\r
-* 2. Redistributions in binary form must reproduce the above copyright\r
-* notice, this list of conditions and the following disclaimer in the\r
-* documentation and/or other materials provided with the distribution.\r
-*\r
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
-* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
-* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
-* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
-* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
-* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
-* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
-* POSSIBILITY OF SUCH DAMAGE.\r
-*/\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-#include "j2k.h"\r
-#include "jp2.h"\r
-#include "cio.h"\r
-\r
-#define JPIP_JPIP 0x6a706970\r
-\r
-#define JP2_JP 0x6a502020\r
-#define JP2_FTYP 0x66747970\r
-#define JP2_JP2H 0x6a703268\r
-#define JP2_IHDR 0x69686472\r
-#define JP2_COLR 0x636f6c72\r
-#define JP2_JP2C 0x6a703263\r
-#define JP2_URL 0x75726c20\r
-#define JP2_DBTL 0x6474626c\r
-#define JP2_BPCC 0x62706363\r
-#define JP2_JP2 0x6a703220\r
-\r
-/*\r
-* \r
-* Read box headers\r
-*\r
-*/\r
-\r
-int jp2_read_boxhdr(jp2_box_t * box)\r
-{\r
- box->init_pos = cio_tell();\r
- box->length = cio_read(4);\r
- box->type = cio_read(4);\r
- if (box->length == 1) {\r
- if (cio_read(4) != 0) {\r
- fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");\r
- return 1;\r
- }\r
- box->length = cio_read(4);\r
- if (box->length == 0)\r
- box->length = cio_numbytesleft() + 12;\r
- } else if (box->length == 0) {\r
- box->length = cio_numbytesleft() + 8;\r
- }\r
- return 0;\r
-}\r
-\r
-/*\r
-* \r
-* Initialisation of a Standard JP2 structure\r
-*/\r
-\r
-int jp2_init_stdjp2(jp2_struct_t * jp2_struct)\r
-{\r
-\r
- jp2_struct->comps =\r
- (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));\r
-\r
- jp2_struct->precedence = 0; /* PRECEDENCE*/\r
- jp2_struct->approx = 0; /* APPROX*/\r
-\r
- jp2_struct->brand = JP2_JP2; /* BR */\r
- jp2_struct->minversion = 0; /* MinV */\r
- jp2_struct->numcl = 1;\r
- jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(int));\r
- jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */\r
-\r
- jp2_struct->C = 7; /* C : Always 7*/\r
- jp2_struct->UnkC = 0; /* UnkC, colorspace specified in colr box*/\r
- jp2_struct->IPR = 0; /* IPR, no intellectual property*/\r
-\r
- return 0;\r
-}\r
-\r
-\r
-void jp2_write_url(char *Idx_file)\r
-{\r
- unsigned int i;\r
- char str[256];\r
- jp2_box_t box;\r
-\r
- sprintf(str, "%s", Idx_file);\r
-\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_URL, 4); /* DBTL*/\r
- cio_write(0, 1); /* VERS*/\r
- cio_write(0, 3); /* FLAG*/\r
-\r
- for (i = 0; i < strlen(str); i++) {\r
- cio_write(str[i], 1);\r
- }\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-/*\r
-* Read the IHDR box\r
-*\r
-* Image Header box\r
-*\r
-*/\r
-int jp2_read_ihdr(jp2_struct_t * jp2_struct)\r
-{\r
- jp2_box_t box;\r
-\r
- jp2_read_boxhdr(&box);\r
- if (JP2_IHDR != box.type) {\r
- fprintf(stderr, "Error: Expected IHDR Marker\n");\r
- return 1;\r
- }\r
-\r
- jp2_struct->h = cio_read(4); /* HEIGHT*/\r
- jp2_struct->w = cio_read(4); /* WIDTH*/\r
- jp2_struct->numcomps = cio_read(2); /* NC*/\r
-\r
- jp2_struct->bpc = cio_read(1); /* BPC*/\r
-\r
- jp2_struct->C = cio_read(1); /* C */\r
- jp2_struct->UnkC = cio_read(1); /* UnkC*/\r
- jp2_struct->IPR = cio_read(1); /* IPR*/\r
-\r
- if (cio_tell() - box.init_pos != box.length) {\r
- fprintf(stderr, "Error with IHDR Box\n");\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-void jp2_write_ihdr(jp2_struct_t * jp2_struct)\r
-{\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_IHDR, 4); /* IHDR*/\r
-\r
- cio_write(jp2_struct->h, 4); /* HEIGHT*/\r
- cio_write(jp2_struct->w, 4); /* WIDTH*/\r
- cio_write(jp2_struct->numcomps, 2); /* NC*/\r
-\r
- cio_write(jp2_struct->bpc, 1); /* BPC */\r
-\r
- cio_write(jp2_struct->C, 1); /* C : Always 7*/\r
- cio_write(jp2_struct->UnkC, 1); /* UnkC, colorspace unknow*/\r
- cio_write(jp2_struct->IPR, 1); /* IPR, no intellectual property*/\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-\r
-void jp2_write_bpcc(jp2_struct_t * jp2_struct)\r
-{\r
- unsigned int i;\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_BPCC, 4); /* BPCC*/\r
-\r
- for (i = 0; i < jp2_struct->numcomps; i++)\r
- cio_write(jp2_struct->comps[i].bpcc, 1);\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-\r
-int jp2_read_bpcc(jp2_struct_t * jp2_struct)\r
-{\r
- unsigned int i;\r
- jp2_box_t box;\r
-\r
- jp2_read_boxhdr(&box);\r
- if (JP2_BPCC != box.type) {\r
- fprintf(stderr, "Error: Expected BPCC Marker\n");\r
- return 1;\r
- }\r
-\r
- for (i = 0; i < jp2_struct->numcomps; i++)\r
- jp2_struct->comps[i].bpcc = cio_read(1);\r
-\r
- if (cio_tell() - box.init_pos != box.length) {\r
- fprintf(stderr, "Error with BPCC Box\n");\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-void jp2_write_colr(jp2_struct_t * jp2_struct)\r
-{\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_COLR, 4); /* COLR*/\r
-\r
- cio_write(jp2_struct->meth, 1); /* METH*/\r
- cio_write(jp2_struct->precedence, 1); /* PRECEDENCE*/\r
- cio_write(jp2_struct->approx, 1); /* APPROX*/\r
-\r
- if (jp2_struct->meth == 1)\r
- cio_write(jp2_struct->enumcs, 4); /* EnumCS*/\r
- else\r
- cio_write(0, 1); /* PROFILE (??)*/\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-int jp2_read_colr(jp2_struct_t * jp2_struct)\r
-{\r
- jp2_box_t box;\r
- int skip_len;\r
-\r
- jp2_read_boxhdr(&box);\r
- do {\r
- if (JP2_COLR != box.type) {\r
- cio_skip(box.length - 8);\r
- jp2_read_boxhdr(&box);\r
- }\r
- } while (JP2_COLR != box.type);\r
-\r
- jp2_struct->meth = cio_read(1); /* METH*/\r
- jp2_struct->precedence = cio_read(1); /* PRECEDENCE*/\r
- jp2_struct->approx = cio_read(1); /* APPROX*/\r
-\r
- if (jp2_struct->meth == 1)\r
- jp2_struct->enumcs = cio_read(4); /* EnumCS*/\r
- else {\r
- /* SKIP PROFILE */\r
- skip_len = box.init_pos + box.length - cio_tell();\r
- if (skip_len < 0) {\r
- fprintf(stderr, "Error with JP2H box size\n");\r
- return 1;\r
- }\r
- cio_skip(box.init_pos + box.length - cio_tell());\r
- }\r
-\r
- if (cio_tell() - box.init_pos != box.length) {\r
- fprintf(stderr, "Error with BPCC Box\n");\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-/*\r
-* Write the JP2H box\r
-*\r
-* JP2 Header box\r
-*\r
-*/\r
-void jp2_write_jp2h(jp2_struct_t * jp2_struct)\r
-{\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);;\r
- cio_write(JP2_JP2H, 4); /* JP2H */\r
-\r
- jp2_write_ihdr(jp2_struct);\r
-\r
- if (jp2_struct->bpc == 255)\r
- jp2_write_bpcc(jp2_struct);\r
- jp2_write_colr(jp2_struct);\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-\r
-/*\r
-* Read the JP2H box\r
-*\r
-* JP2 Header box\r
-*\r
-*/\r
-int jp2_read_jp2h(jp2_struct_t * jp2_struct)\r
-{\r
- jp2_box_t box;\r
- int skip_len;\r
-\r
- jp2_read_boxhdr(&box);\r
- do {\r
- if (JP2_JP2H != box.type) {\r
- if (box.type == JP2_JP2C) {\r
- fprintf(stderr, "Error: Expected JP2H Marker\n");\r
- return 1;\r
- }\r
- cio_skip(box.length - 8);\r
- jp2_read_boxhdr(&box);\r
- }\r
- } while (JP2_JP2H != box.type);\r
-\r
- if (jp2_read_ihdr(jp2_struct))\r
- return 1;\r
-\r
- if (jp2_struct->bpc == 255) {\r
- if (jp2_read_bpcc(jp2_struct))\r
- return 1;\r
- }\r
-\r
- if (jp2_read_colr(jp2_struct))\r
- return 1;\r
-\r
- skip_len = box.init_pos + box.length - cio_tell();\r
- if (skip_len < 0) {\r
- fprintf(stderr, "Error with JP2H box size\n");\r
- return 1;\r
- }\r
- cio_skip(box.init_pos + box.length - cio_tell());\r
-\r
- return 0;\r
-}\r
-\r
-/*\r
-* Write the FTYP box\r
-*\r
-* File type box\r
-*\r
-*/\r
-void jp2_write_ftyp(jp2_struct_t * jp2_struct)\r
-{\r
- unsigned int i;\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_FTYP, 4); /* FTYP */\r
-\r
- cio_write(jp2_struct->brand, 4); /* BR */\r
- cio_write(jp2_struct->minversion, 4); /* MinV */\r
-\r
- for (i = 0; i < jp2_struct->numcl; i++)\r
- cio_write(jp2_struct->cl[i], 4); /* CL */\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-/*\r
-* Read the FTYP box\r
-*\r
-* File type box\r
-*\r
-*/\r
-int jp2_read_ftyp(jp2_struct_t * jp2_struct)\r
-{\r
- int i;\r
- jp2_box_t box;\r
-\r
- jp2_read_boxhdr(&box);\r
-\r
- if (JP2_FTYP != box.type) {\r
- fprintf(stderr, "Error: Excpected FTYP Marker\n");\r
- return 1;\r
- }\r
-\r
- jp2_struct->brand = cio_read(4); /* BR */\r
- jp2_struct->minversion = cio_read(4); /* MinV */\r
- jp2_struct->numcl = (box.length - 16) / 4;\r
- jp2_struct->cl =\r
- (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));\r
-\r
- for (i = 0; i < (int) jp2_struct->numcl; i++)\r
- jp2_struct->cl[i] = cio_read(4); /* CLi */\r
-\r
- if (cio_tell() - box.init_pos != box.length) {\r
- fprintf(stderr, "Error with FTYP Box\n");\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset,\r
- char *j2k_codestream)\r
-{\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_JP2C, 4); /* JP2C*/\r
-\r
- *j2k_codestream_offset = cio_tell();\r
- memcpy(cio_getbp(), j2k_codestream, j2k_codestream_len);\r
-\r
- box.length = 8 + j2k_codestream_len;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-\r
- return box.length;\r
-}\r
-\r
-\r
-int jp2_read_jp2c(unsigned int *j2k_codestream_len,\r
- unsigned int *j2k_codestream_offset)\r
-{\r
- jp2_box_t box;\r
-\r
- jp2_read_boxhdr(&box);\r
- do {\r
- if (JP2_JP2C != box.type) {\r
- cio_skip(box.length - 8);\r
- jp2_read_boxhdr(&box);\r
- }\r
- } while (JP2_JP2C != box.type);\r
-\r
- *j2k_codestream_offset = cio_tell();\r
- *j2k_codestream_len = box.length - 8;\r
-\r
- return 0;\r
-}\r
-\r
-void jp2_write_jp()\r
-{\r
- jp2_box_t box;\r
-\r
- box.init_pos = cio_tell();\r
- cio_skip(4);\r
- cio_write(JP2_JP, 4); /* JP*/\r
- cio_write(0x0d0a870a, 4);\r
-\r
- box.length = cio_tell() - box.init_pos;\r
- cio_seek(box.init_pos);\r
- cio_write(box.length, 4); /* L */\r
- cio_seek(box.init_pos + box.length);\r
-}\r
-\r
-/*\r
-* Read the JP box\r
-*\r
-* JPEG 2000 signature\r
-*\r
-* return 1 if error else 0\r
-*/\r
-int jp2_read_jp()\r
-{\r
- jp2_box_t box;\r
-\r
- jp2_read_boxhdr(&box);\r
- if (JP2_JP != box.type) {\r
- fprintf(stderr, "Error: Expected JP Marker\n");\r
- return 1;\r
- }\r
- if (0x0d0a870a != cio_read(4)) {\r
- fprintf(stderr, "Error with JP Marker\n");\r
- return 1;\r
- }\r
- if (cio_tell() - box.init_pos != box.length) {\r
- fprintf(stderr, "Error with JP Box size\n");\r
- return 1;\r
- }\r
- return 0;\r
-\r
-}\r
-\r
-\r
-int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)\r
-{\r
- cio_init(src, len);\r
-\r
- if (jp2_read_jp())\r
- return 1;\r
- if (jp2_read_ftyp(jp2_struct))\r
- return 1;\r
- if (jp2_read_jp2h(jp2_struct))\r
- return 1;\r
- if (jp2_read_jp2c\r
- (&jp2_struct->j2k_codestream_len,\r
- &jp2_struct->j2k_codestream_offset))\r
- return 1;\r
- return 0;\r
-}\r
-\r
-int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,\r
- char *output)\r
-{\r
- (void)output;\r
- jp2_write_jp();\r
- jp2_write_ftyp(jp2_struct);\r
- jp2_write_jp2h(jp2_struct);\r
-\r
- jp2_write_jp2c(jp2_struct->j2k_codestream_len,\r
- &jp2_struct->j2k_codestream_offset, j2k_codestream);\r
-\r
- return cio_tell();\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2003, Yannick Verschueren\r
- * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __JP2_H\r
-#define __JP2_H\r
-\r
-#include "j2k.h"\r
-\r
-typedef struct {\r
- int depth; \r
- int sgnd; \r
- int bpcc;\r
-} jp2_comps_t;\r
-\r
-typedef struct {\r
- unsigned int w;\r
- unsigned int h;\r
- unsigned int numcomps;\r
- unsigned int bpc;\r
- unsigned int C;\r
- unsigned int UnkC;\r
- unsigned int IPR;\r
- unsigned int meth;\r
- unsigned int approx;\r
- unsigned int enumcs;\r
- unsigned int precedence;\r
- unsigned int brand;\r
- unsigned int minversion;\r
- unsigned int numcl;\r
- unsigned int *cl;\r
- jp2_comps_t *comps;\r
- j2k_image_t *image;\r
- unsigned int j2k_codestream_offset;\r
- unsigned int j2k_codestream_len;\r
-} jp2_struct_t;\r
-\r
-typedef struct {\r
- int length;\r
- int type;\r
- int init_pos;\r
-} jp2_box_t;\r
-\r
-/* int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img); \r
- *\r
- * Create a standard jp2_structure\r
- * jp2_struct: the structure you are creating\r
- * img: a j2k_image_t wich will help you to create the jp2_structure\r
- */\r
-int jp2_init_stdjp2(jp2_struct_t * jp2_struct);\r
-\r
-/* int jp2_write_jp2c(int j2k_len, int *j2k_codestream_offset, char *j2k_codestream)\r
- *\r
- * Write the jp2c codestream box \r
- * j2k_len: the j2k codestream length\r
- * j2k_codestream_offset: the function will return the j2k codestream offset\r
- * j2k_codestream: the j2k codestream to include in jp2 file\r
- */\r
-int jp2_write_jp2c(int j2k_len, int *j2k_codestream_offset, char *j2k_codestream);\r
-\r
-/* int jp2_write_jp2h(jp2_struct_t * jp2_struct);\r
- *\r
- * Write the jp2h header box \r
- * jp2_struct: the jp2 structure you are working with\r
- */\r
-void jp2_write_jp2h(jp2_struct_t * jp2_struct);\r
-\r
-/* int jp2_read_jp2h(jp2_struct_t * jp2_struct);\r
- *\r
- * Read the jp2h header box \r
- * jp2_struct: the jp2 structure you are working with\r
- */\r
-int jp2_read_jp2h(jp2_struct_t * jp2_struct);\r
-\r
-/* int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, \r
- int j2k_len, char *output)\r
- *\r
- * Wrap a J2K codestream in a JP2 file\r
- * jp2_struct: the jp2 structure used to create jp2 boxes\r
- * j2k_codestream: the j2k codestream to include in jp2 file\r
- * output: pointer to jp2 codestream that will be created\r
- */\r
-int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, \r
- char *output);\r
-\r
-\r
-/* int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct);\r
- *\r
- * Decode the structure of a JP2 file\r
- * src: pointer to memory where compressed data is stored\r
- * jp2_struct: the jp2 structure that will be created \r
- * len: length of jp2 codestream\r
- */\r
-int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2004, Yannick Verschueren\r
- * Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-\r
-#include "jpt.h"\r
-#include "j2k.h"\r
-#include "cio.h"\r
-\r
-\r
-/*\r
- * Read the information contains in VBAS [JPP/JPT stream message header]\r
- * Store information (7 bits) in value\r
- *\r
- */\r
-unsigned int jpt_read_VBAS_info(unsigned int value)\r
-{\r
- unsigned char elmt;\r
-\r
- elmt = cio_read(1);\r
- while ((elmt >> 7) == 1) {\r
- value = (value << 7);\r
- value |= (elmt & 0x7f);\r
- elmt = cio_read(1);\r
- }\r
- value = (value << 7);\r
- value |= (elmt & 0x7f);\r
-\r
- return value;\r
-}\r
-\r
-/*\r
- * Initialize the value of the message header structure \r
- *\r
- */\r
-void jpt_init_Msg_Header(jpt_msg_header_struct_t * header)\r
-{\r
- header->Id = 0; /* In-class Identifier */\r
- header->last_byte = 0; /* Last byte information */\r
- header->Class_Id = 0; /* Class Identifier */\r
- header->CSn_Id = 0; /* CSn : index identifier */\r
- header->Msg_offset = 0; /* Message offset */\r
- header->Msg_length = 0; /* Message length */\r
- header->Layer_nb = 0; /* Auxiliary for JPP case */\r
-}\r
-\r
-/*\r
- * Re-initialize the value of the message header structure\r
- *\r
- * Only parameters always present in message header\r
- *\r
- */\r
-void jpt_reinit_Msg_Header(jpt_msg_header_struct_t * header)\r
-{\r
- header->Id = 0; /* In-class Identifier */\r
- header->last_byte = 0; /* Last byte information */\r
- header->Msg_offset = 0; /* Message offset */\r
- header->Msg_length = 0; /* Message length */\r
-}\r
-\r
-/*\r
- * Read the message header for a JPP/JPT - stream\r
- *\r
- */\r
-void jpt_read_Msg_Header(jpt_msg_header_struct_t * header)\r
-{\r
- unsigned char elmt, Class = 0, CSn = 0;\r
- jpt_reinit_Msg_Header(header);\r
-\r
- /* ------------- */\r
- /* VBAS : Bin-ID */\r
- /* ------------- */\r
- elmt = cio_read(1);\r
-\r
- /* See for Class and CSn */\r
- switch ((elmt >> 5) & 0x03) {\r
- case 0:\r
- fprintf(stderr, "Forbidden value encounter in message header !!\n");\r
- break;\r
- case 1:\r
- Class = 0;\r
- CSn = 0;\r
- break;\r
- case 2:\r
- Class = 1;\r
- CSn = 0;\r
- break;\r
- case 3:\r
- Class = 1;\r
- CSn = 1;\r
- break;\r
- default:\r
- break;\r
- }\r
-\r
- /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */\r
- if (((elmt >> 4) & 0x01) == 1)\r
- header->last_byte = 1;\r
-\r
- /* In-class identifier */\r
- header->Id |= (elmt & 0x0f);\r
- if ((elmt >> 7) == 1)\r
- header->Id = jpt_read_VBAS_info(header->Id);\r
-\r
- /* ------------ */\r
- /* VBAS : Class */\r
- /* ------------ */\r
- if (Class == 1) {\r
- header->Class_Id = 0;\r
- header->Class_Id = jpt_read_VBAS_info(header->Class_Id);\r
- }\r
-\r
- /* ---------- */\r
- /* VBAS : CSn */\r
- /* ---------- */\r
- if (CSn == 1) {\r
- header->CSn_Id = 0;\r
- header->CSn_Id = jpt_read_VBAS_info(header->CSn_Id);\r
- }\r
-\r
- /* ----------------- */\r
- /* VBAS : Msg_offset */\r
- /* ----------------- */\r
- header->Msg_offset = jpt_read_VBAS_info(header->Msg_offset);\r
-\r
- /* ----------------- */\r
- /* VBAS : Msg_length */\r
- /* ----------------- */\r
- header->Msg_length = jpt_read_VBAS_info(header->Msg_length);\r
-\r
- /* ---------- */\r
- /* VBAS : Aux */\r
- /* ---------- */\r
- if ((header->Class_Id & 0x01) == 1) {\r
- header->Layer_nb = 0;\r
- header->Layer_nb = jpt_read_VBAS_info(header->Layer_nb);\r
- }\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2004, Yannick Verschueren\r
- * Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-/*\r
- * Message Header JPT_stream Structure \r
- *\r
- */\r
-typedef struct {\r
- unsigned int Id; /* In-class Identifier */\r
- unsigned int last_byte; /* Last byte information */\r
- unsigned int Class_Id; /* Class Identifier */\r
- unsigned int CSn_Id; /* CSn : index identifier */\r
- unsigned int Msg_offset; /* Message offset */\r
- unsigned int Msg_length; /* Message length */\r
- unsigned int Layer_nb; /* Auxiliary for JPP case */\r
-} jpt_msg_header_struct_t;\r
-\r
-/*\r
- * Initialize the value of the message header structure \r
- *\r
- * header : Message header structure\r
- *\r
- */\r
-void jpt_init_Msg_Header(jpt_msg_header_struct_t * header);\r
-\r
-/*\r
- * Read the message header for a JPP/JPT - stream\r
- *\r
- * header : Message header structure\r
- *\r
- */\r
-void jpt_read_Msg_Header(jpt_msg_header_struct_t * header);\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "mct.h"\r
-#include "fix.h"\r
-\r
-/* <summary> */\r
-/* This table contains the norms of the basis function of the reversible MCT. */\r
-/* </summary> */\r
-double mct_norms[3] = { 1.732, .8292, .8292 };\r
-\r
-/* <summary> */\r
-/* This table contains the norms of the basis function of the irreversible MCT. */\r
-/* </summary> */\r
-double mct_norms_real[3] = { 1.732, 1.805, 1.573 };\r
-\r
-/* <summary> */\r
-/* Foward reversible MCT. */\r
-/* </summary> */\r
-void mct_encode(int *c0, int *c1, int *c2, int n)\r
-{\r
- int i;\r
- for (i = 0; i < n; i++) {\r
- int r, g, b, y, u, v;\r
- r = c0[i];\r
- g = c1[i];\r
- b = c2[i];\r
- y = (r + (g << 1) + b) >> 2;\r
- u = b - g;\r
- v = r - g;\r
- c0[i] = y;\r
- c1[i] = u;\r
- c2[i] = v;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse reversible MCT. */\r
-/* </summary> */\r
-void mct_decode(int *c0, int *c1, int *c2, int n)\r
-{\r
- int i;\r
- for (i = 0; i < n; i++) {\r
- int y, u, v, r, g, b;\r
- y = c0[i];\r
- u = c1[i];\r
- v = c2[i];\r
- g = y - ((u + v) >> 2);\r
- r = v + g;\r
- b = u + g;\r
- c0[i] = r;\r
- c1[i] = g;\r
- c2[i] = b;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Get norm of basis function of reversible MCT. */\r
-/* </summary> */\r
-double mct_getnorm(int compno)\r
-{\r
- return mct_norms[compno];\r
-}\r
-\r
-/* <summary> */\r
-/* Foward irreversible MCT. */\r
-/* </summary> */\r
-void mct_encode_real(int *c0, int *c1, int *c2, int n)\r
-{\r
- int i;\r
- for (i = 0; i < n; i++) {\r
- int r, g, b, y, u, v;\r
- r = c0[i];\r
- g = c1[i];\r
- b = c2[i];\r
- y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);\r
- u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);\r
- v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);\r
- c0[i] = y;\r
- c1[i] = u;\r
- c2[i] = v;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse irreversible MCT. */\r
-/* </summary> */\r
-void mct_decode_real(int *c0, int *c1, int *c2, int n)\r
-{\r
- int i;\r
- for (i = 0; i < n; i++) {\r
- int y, u, v, r, g, b;\r
- y = c0[i];\r
- u = c1[i];\r
- v = c2[i];\r
- r = y + fix_mul(v, 11485);\r
- g = y - fix_mul(u, 2819) - fix_mul(v, 5850);\r
- b = y + fix_mul(u, 14516);\r
- c0[i] = r;\r
- c1[i] = g;\r
- c2[i] = b;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Get norm of basis function of irreversible MCT. */\r
-/* </summary> */\r
-double mct_getnorm_real(int compno)\r
-{\r
- return mct_norms_real[compno];\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __MCT_H\r
-#define __MCT_H\r
-\r
-/*\r
- * Apply a reversible multi-component transform to an image\r
- * R: samples for red component\r
- * G: samples for green component\r
- * B: samples blue component\r
- * n: number of samples for each component\r
- */\r
-void mct_encode(int *R, int *G, int *B, int n);\r
-/*\r
- * Apply a reversible multi-component inverse transform to an image\r
- * Y: samples for luminance component\r
- * U: samples for red chrominance component\r
- * V: samples for blue chrominance component\r
- * n: number of samples for each component\r
- */\r
-void mct_decode(int *V, int *U, int *Y, int n);\r
-/*\r
- * Get norm of the basis function used for the reversible multi-component transform\r
- * compno: number of the component (0->Y, 1->U, 2->V)\r
- */\r
-double mct_getnorm(int compno);\r
-\r
-/*\r
- * Apply an irreversible multi-component transform to an image\r
- * R: samples for red component\r
- * G: samples for green component\r
- * B: samples blue component\r
- * n: number of samples for each component\r
- */\r
-void mct_encode_real(int *c0, int *c1, int *c2, int n);\r
-/*\r
- * Apply an irreversible multi-component inverse transform to an image\r
- * Y: samples for luminance component\r
- * U: samples for red chrominance component\r
- * V: samples for blue chrominance component\r
- * n: number of samples for each component\r
- */\r
-void mct_decode_real(int *c0, int *c1, int *c2, int n);\r
-/*\r
- * Get norm of the basis function used for the irreversible multi-component transform\r
- * compno: number of the component (0->Y, 1->U, 2->V)\r
- */\r
-double mct_getnorm_real(int compno);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "mqc.h"\r
-#include <stdio.h>\r
-\r
-/* <summary> */\r
-/* This struct defines the state of a context. */\r
-/* </summary> */\r
-typedef struct mqc_state_s {\r
- unsigned int qeval; /* the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */\r
- int mps; /* the Most Probable Symbol (0 or 1) */\r
- struct mqc_state_s *nmps; /* next state if the next encoded symbol is the MPS */\r
- struct mqc_state_s *nlps; /* next state if the next encoded symbol is the LPS */\r
-} mqc_state_t;\r
-\r
-/* <summary> */\r
-/* This array defines all the possible states for a context. */\r
-/* </summary> */\r
-mqc_state_t mqc_states[47 * 2] = {\r
- {0x5601, 0, &mqc_states[2], &mqc_states[3]},\r
- {0x5601, 1, &mqc_states[3], &mqc_states[2]},\r
- {0x3401, 0, &mqc_states[4], &mqc_states[12]},\r
- {0x3401, 1, &mqc_states[5], &mqc_states[13]},\r
- {0x1801, 0, &mqc_states[6], &mqc_states[18]},\r
- {0x1801, 1, &mqc_states[7], &mqc_states[19]},\r
- {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},\r
- {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},\r
- {0x0521, 0, &mqc_states[10], &mqc_states[58]},\r
- {0x0521, 1, &mqc_states[11], &mqc_states[59]},\r
- {0x0221, 0, &mqc_states[76], &mqc_states[66]},\r
- {0x0221, 1, &mqc_states[77], &mqc_states[67]},\r
- {0x5601, 0, &mqc_states[14], &mqc_states[13]},\r
- {0x5601, 1, &mqc_states[15], &mqc_states[12]},\r
- {0x5401, 0, &mqc_states[16], &mqc_states[28]},\r
- {0x5401, 1, &mqc_states[17], &mqc_states[29]},\r
- {0x4801, 0, &mqc_states[18], &mqc_states[28]},\r
- {0x4801, 1, &mqc_states[19], &mqc_states[29]},\r
- {0x3801, 0, &mqc_states[20], &mqc_states[28]},\r
- {0x3801, 1, &mqc_states[21], &mqc_states[29]},\r
- {0x3001, 0, &mqc_states[22], &mqc_states[34]},\r
- {0x3001, 1, &mqc_states[23], &mqc_states[35]},\r
- {0x2401, 0, &mqc_states[24], &mqc_states[36]},\r
- {0x2401, 1, &mqc_states[25], &mqc_states[37]},\r
- {0x1c01, 0, &mqc_states[26], &mqc_states[40]},\r
- {0x1c01, 1, &mqc_states[27], &mqc_states[41]},\r
- {0x1601, 0, &mqc_states[58], &mqc_states[42]},\r
- {0x1601, 1, &mqc_states[59], &mqc_states[43]},\r
- {0x5601, 0, &mqc_states[30], &mqc_states[29]},\r
- {0x5601, 1, &mqc_states[31], &mqc_states[28]},\r
- {0x5401, 0, &mqc_states[32], &mqc_states[28]},\r
- {0x5401, 1, &mqc_states[33], &mqc_states[29]},\r
- {0x5101, 0, &mqc_states[34], &mqc_states[30]},\r
- {0x5101, 1, &mqc_states[35], &mqc_states[31]},\r
- {0x4801, 0, &mqc_states[36], &mqc_states[32]},\r
- {0x4801, 1, &mqc_states[37], &mqc_states[33]},\r
- {0x3801, 0, &mqc_states[38], &mqc_states[34]},\r
- {0x3801, 1, &mqc_states[39], &mqc_states[35]},\r
- {0x3401, 0, &mqc_states[40], &mqc_states[36]},\r
- {0x3401, 1, &mqc_states[41], &mqc_states[37]},\r
- {0x3001, 0, &mqc_states[42], &mqc_states[38]},\r
- {0x3001, 1, &mqc_states[43], &mqc_states[39]},\r
- {0x2801, 0, &mqc_states[44], &mqc_states[38]},\r
- {0x2801, 1, &mqc_states[45], &mqc_states[39]},\r
- {0x2401, 0, &mqc_states[46], &mqc_states[40]},\r
- {0x2401, 1, &mqc_states[47], &mqc_states[41]},\r
- {0x2201, 0, &mqc_states[48], &mqc_states[42]},\r
- {0x2201, 1, &mqc_states[49], &mqc_states[43]},\r
- {0x1c01, 0, &mqc_states[50], &mqc_states[44]},\r
- {0x1c01, 1, &mqc_states[51], &mqc_states[45]},\r
- {0x1801, 0, &mqc_states[52], &mqc_states[46]},\r
- {0x1801, 1, &mqc_states[53], &mqc_states[47]},\r
- {0x1601, 0, &mqc_states[54], &mqc_states[48]},\r
- {0x1601, 1, &mqc_states[55], &mqc_states[49]},\r
- {0x1401, 0, &mqc_states[56], &mqc_states[50]},\r
- {0x1401, 1, &mqc_states[57], &mqc_states[51]},\r
- {0x1201, 0, &mqc_states[58], &mqc_states[52]},\r
- {0x1201, 1, &mqc_states[59], &mqc_states[53]},\r
- {0x1101, 0, &mqc_states[60], &mqc_states[54]},\r
- {0x1101, 1, &mqc_states[61], &mqc_states[55]},\r
- {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},\r
- {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},\r
- {0x09c1, 0, &mqc_states[64], &mqc_states[58]},\r
- {0x09c1, 1, &mqc_states[65], &mqc_states[59]},\r
- {0x08a1, 0, &mqc_states[66], &mqc_states[60]},\r
- {0x08a1, 1, &mqc_states[67], &mqc_states[61]},\r
- {0x0521, 0, &mqc_states[68], &mqc_states[62]},\r
- {0x0521, 1, &mqc_states[69], &mqc_states[63]},\r
- {0x0441, 0, &mqc_states[70], &mqc_states[64]},\r
- {0x0441, 1, &mqc_states[71], &mqc_states[65]},\r
- {0x02a1, 0, &mqc_states[72], &mqc_states[66]},\r
- {0x02a1, 1, &mqc_states[73], &mqc_states[67]},\r
- {0x0221, 0, &mqc_states[74], &mqc_states[68]},\r
- {0x0221, 1, &mqc_states[75], &mqc_states[69]},\r
- {0x0141, 0, &mqc_states[76], &mqc_states[70]},\r
- {0x0141, 1, &mqc_states[77], &mqc_states[71]},\r
- {0x0111, 0, &mqc_states[78], &mqc_states[72]},\r
- {0x0111, 1, &mqc_states[79], &mqc_states[73]},\r
- {0x0085, 0, &mqc_states[80], &mqc_states[74]},\r
- {0x0085, 1, &mqc_states[81], &mqc_states[75]},\r
- {0x0049, 0, &mqc_states[82], &mqc_states[76]},\r
- {0x0049, 1, &mqc_states[83], &mqc_states[77]},\r
- {0x0025, 0, &mqc_states[84], &mqc_states[78]},\r
- {0x0025, 1, &mqc_states[85], &mqc_states[79]},\r
- {0x0015, 0, &mqc_states[86], &mqc_states[80]},\r
- {0x0015, 1, &mqc_states[87], &mqc_states[81]},\r
- {0x0009, 0, &mqc_states[88], &mqc_states[82]},\r
- {0x0009, 1, &mqc_states[89], &mqc_states[83]},\r
- {0x0005, 0, &mqc_states[90], &mqc_states[84]},\r
- {0x0005, 1, &mqc_states[91], &mqc_states[85]},\r
- {0x0001, 0, &mqc_states[90], &mqc_states[86]},\r
- {0x0001, 1, &mqc_states[91], &mqc_states[87]},\r
- {0x5601, 0, &mqc_states[92], &mqc_states[92]},\r
- {0x5601, 1, &mqc_states[93], &mqc_states[93]},\r
-};\r
-\r
-#define MQC_NUMCTXS 32\r
-\r
-unsigned int mqc_c;\r
-unsigned int mqc_a;\r
-unsigned int mqc_ct;\r
-unsigned char *mqc_bp;\r
-unsigned char *mqc_start;\r
-unsigned char *mqc_end;\r
-mqc_state_t *mqc_ctxs[MQC_NUMCTXS];\r
-mqc_state_t **mqc_curctx;\r
-\r
-/* <summary> */\r
-/* Return the number of bytes already encoded. */\r
-/* </summary> */\r
-int mqc_numbytes()\r
-{\r
- return mqc_bp - mqc_start;\r
-}\r
-\r
-/* <summary> */\r
-/* Output a byte, doing bit-stuffing if necessary. */\r
-/* After a 0xff byte, the next byte must be smaller than 0x90 */\r
-/* </summary> */\r
-void mqc_byteout()\r
-{\r
- if (*mqc_bp == 0xff) {\r
- mqc_bp++;\r
- *mqc_bp = mqc_c >> 20;\r
- mqc_c &= 0xfffff;\r
- mqc_ct = 7;\r
- } else {\r
- if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */\r
- mqc_bp++;\r
- *mqc_bp = mqc_c >> 19;\r
- mqc_c &= 0x7ffff;\r
- mqc_ct = 8;\r
- } else {\r
- (*mqc_bp)++;\r
- if (*mqc_bp == 0xff) {\r
- mqc_c &= 0x7ffffff;\r
- mqc_bp++;\r
- *mqc_bp = mqc_c >> 20;\r
- mqc_c &= 0xfffff;\r
- mqc_ct = 7;\r
- } else {\r
- mqc_bp++;\r
- *mqc_bp = mqc_c >> 19;\r
- mqc_c &= 0x7ffff;\r
- mqc_ct = 8;\r
- }\r
- }\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Renormalize mqc_a and mqc_c while encoding, so that mqc_a stays between 0x8000 and 0x10000 */\r
-/* </summary> */\r
-void mqc_renorme()\r
-{\r
- do {\r
- mqc_a <<= 1;\r
- mqc_c <<= 1;\r
- mqc_ct--;\r
- if (mqc_ct == 0) {\r
- mqc_byteout();\r
- }\r
- } while ((mqc_a & 0x8000) == 0);\r
-}\r
-\r
-/* <summary> */\r
-/* Encode the most probable symbol. */\r
-/* </summary> */\r
-void mqc_codemps()\r
-{\r
- mqc_a -= (*mqc_curctx)->qeval;\r
- if ((mqc_a & 0x8000) == 0) {\r
- if (mqc_a < (*mqc_curctx)->qeval) {\r
- mqc_a = (*mqc_curctx)->qeval;\r
- } else {\r
- mqc_c += (*mqc_curctx)->qeval;\r
- }\r
- *mqc_curctx = (*mqc_curctx)->nmps;\r
- mqc_renorme();\r
- } else {\r
- mqc_c += (*mqc_curctx)->qeval;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Encode the most least symbol. */\r
-/* </summary> */\r
-void mqc_codelps()\r
-{\r
- mqc_a -= (*mqc_curctx)->qeval;\r
- if (mqc_a < (*mqc_curctx)->qeval) {\r
- mqc_c += (*mqc_curctx)->qeval;\r
- } else {\r
- mqc_a = (*mqc_curctx)->qeval;\r
- }\r
- *mqc_curctx = (*mqc_curctx)->nlps;\r
- mqc_renorme();\r
-}\r
-\r
-/* <summary> */\r
-/* Initialize encoder. */\r
-/* </summary> */\r
-/* <param name="bp">Output buffer.</param> */\r
-void mqc_init_enc(unsigned char *bp)\r
-{\r
- mqc_setcurctx(0);\r
- mqc_a = 0x8000;\r
- mqc_c = 0;\r
- mqc_bp = bp - 1;\r
- mqc_ct = 12;\r
- if (*mqc_bp == 0xff) {\r
- mqc_ct = 13;\r
- }\r
- mqc_start = bp;\r
-}\r
-\r
-/* <summary> */\r
-/* Set current context. */\r
-/* </summary> */\r
-/* <param name="ctxno">Context number.</param> */\r
-void mqc_setcurctx(int ctxno)\r
-{\r
- mqc_curctx = &mqc_ctxs[ctxno];\r
-}\r
-\r
-/* <summary> */\r
-/* Encode a symbol using the MQ-coder. */\r
-/* </summary> */\r
-/* <param name="d"> The symbol to be encoded (0 or 1).</param> */\r
-void mqc_encode(int d)\r
-{\r
- if ((*mqc_curctx)->mps == d) {\r
- mqc_codemps();\r
- } else {\r
- mqc_codelps();\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Fill mqc_c with 1's for flushing */\r
-/* </summary> */\r
-void mqc_setbits()\r
-{\r
- unsigned int tempc = mqc_c + mqc_a;\r
- mqc_c |= 0xffff;\r
- if (mqc_c >= tempc) {\r
- mqc_c -= 0x8000;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Flush encoded data. */\r
-/* </summary> */\r
-void mqc_flush()\r
-{\r
- mqc_setbits();\r
- mqc_c <<= mqc_ct;\r
- mqc_byteout();\r
- mqc_c <<= mqc_ct;\r
- mqc_byteout();\r
-\r
- if (*mqc_bp != 0xff) {\r
- mqc_bp++;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* not fully implemented and tested !! */\r
-/* BYPASS mode switch, initialization operation */\r
-/* JPEG 2000 p 505 */\r
-/* </summary> */\r
-void mqc_bypass_init_enc()\r
-{\r
- mqc_c = 0;\r
- mqc_ct = 8;\r
- /*if (*mqc_bp == 0xff) {\r
- mqc_ct = 7;\r
- } */\r
-}\r
-\r
-/* <summary> */\r
-/* not fully implemented and tested !! */\r
-/* BYPASS mode switch, coding operation */\r
-/* JPEG 2000 p 505 */\r
-/* </summary> */\r
-void mqc_bypass_enc(int d)\r
-{\r
- mqc_ct--;\r
- mqc_c = mqc_c + (d << mqc_ct);\r
- if (mqc_ct == 0) {\r
- mqc_bp++;\r
- *mqc_bp = mqc_c;\r
- mqc_ct = 8;\r
- if (*mqc_bp == 0xff) {\r
- mqc_ct = 7;\r
- }\r
- mqc_c = 0;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* not fully implemented and tested !! */\r
-/* BYPASS mode switch, flush operation */\r
-/* </summary> */\r
-int mqc_bypass_flush_enc()\r
-{\r
- unsigned char bit_padding;\r
-\r
- bit_padding = 0;\r
-\r
- if (mqc_ct != 0) {\r
- while (mqc_ct > 0) {\r
- mqc_ct--;\r
- mqc_c += bit_padding << mqc_ct;\r
- bit_padding = (bit_padding + 1) & 0x01;\r
- }\r
- mqc_bp++;\r
- *mqc_bp = mqc_c;\r
- mqc_ct = 8;\r
- mqc_c = 0;\r
- }\r
-\r
- return 1;\r
-}\r
-\r
-/* <summary> */\r
-/* RESET mode switch */\r
-/* </summary> */\r
-void mqc_reset_enc()\r
-{\r
- mqc_resetstates();\r
- mqc_setstate(18, 0, 46);\r
- mqc_setstate(0, 0, 3);\r
- mqc_setstate(1, 0, 4);\r
-}\r
-\r
-/* <summary> */\r
-/* mode switch RESTART (TERMALL) */\r
-/* </summary> */\r
-int mqc_restart_enc()\r
-{\r
- int correction = 1;\r
-\r
- /* <flush part> */\r
- int n = 27 - 15 - mqc_ct;\r
- mqc_c <<= mqc_ct;\r
- while (n > 0) {\r
- mqc_byteout();\r
- n -= mqc_ct;\r
- mqc_c <<= mqc_ct;\r
- }\r
- mqc_byteout();\r
-\r
- return correction;\r
-}\r
-\r
-/* <summary> */\r
-/* mode switch RESTART (TERMALL) reinitialisation */\r
-/* </summary> */\r
-void mqc_restart_init_enc()\r
-{\r
- /* <Re-init part> */\r
- mqc_setcurctx(0);\r
- mqc_a = 0x8000;\r
- mqc_c = 0;\r
- mqc_ct = 12;\r
- mqc_bp--;\r
- if (*mqc_bp == 0xff) {\r
- mqc_ct = 13;\r
- }\r
-}\r
-\r
-\r
-/* <summary> */\r
-/* ERTERM mode switch */\r
-/* </summary> */\r
-void mqc_erterm_enc()\r
-{\r
- int k = 11 - mqc_ct + 1;\r
-\r
- while (k > 0) {\r
- mqc_c <<= mqc_ct;\r
- mqc_ct = 0;\r
- mqc_byteout();\r
- k -= mqc_ct;\r
- }\r
-\r
- if (*mqc_bp != 0xff) {\r
- mqc_byteout();\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* SEGMARK mode switch (SEGSYM) */\r
-/* </summary> */\r
-void mqc_segmark_enc()\r
-{\r
- int i;\r
- mqc_setcurctx(18);\r
-\r
- for (i = 1; i < 5; i++) {\r
- mqc_encode(i % 2);\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* </summary> */\r
-int mqc_mpsexchange()\r
-{\r
- int d;\r
- if (mqc_a < (*mqc_curctx)->qeval) {\r
- d = 1 - (*mqc_curctx)->mps;\r
- *mqc_curctx = (*mqc_curctx)->nlps;\r
- } else {\r
- d = (*mqc_curctx)->mps;\r
- *mqc_curctx = (*mqc_curctx)->nmps;\r
- }\r
- return d;\r
-}\r
-\r
-/* <summary> */\r
-/* </summary> */\r
-int mqc_lpsexchange()\r
-{\r
- int d;\r
- if (mqc_a < (*mqc_curctx)->qeval) {\r
- mqc_a = (*mqc_curctx)->qeval;\r
- d = (*mqc_curctx)->mps;\r
- *mqc_curctx = (*mqc_curctx)->nmps;\r
- } else {\r
- mqc_a = (*mqc_curctx)->qeval;\r
- d = 1 - (*mqc_curctx)->mps;\r
- *mqc_curctx = (*mqc_curctx)->nlps;\r
- }\r
- return d;\r
-}\r
-\r
-/* <summary> */\r
-/* Input a byte. */\r
-/* </summary> */\r
-void mqc_bytein()\r
-{\r
- if (mqc_bp != mqc_end) {\r
- unsigned int c;\r
- if (mqc_bp + 1 != mqc_end) {\r
- c = *(mqc_bp + 1);\r
- } else {\r
- c = 0xff;\r
- }\r
- if (*mqc_bp == 0xff) {\r
- if (c > 0x8f) {\r
- mqc_c += 0xff00;\r
- mqc_ct = 8;\r
- } else {\r
- mqc_bp++;\r
- mqc_c += c << 9;\r
- mqc_ct = 7;\r
- }\r
- } else {\r
- mqc_bp++;\r
- mqc_c += c << 8;\r
- mqc_ct = 8;\r
- }\r
- } else {\r
- mqc_c += 0xff00;\r
- mqc_ct = 8;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Renormalize mqc_a and mqc_c while decoding. */\r
-/* </summary> */\r
-void mqc_renormd()\r
-{\r
- do {\r
- if (mqc_ct == 0) {\r
- mqc_bytein();\r
- }\r
- mqc_a <<= 1;\r
- mqc_c <<= 1;\r
- mqc_ct--;\r
- } while (mqc_a < 0x8000);\r
-}\r
-\r
-/* <summary> */\r
-/* Initialize decoder. */\r
-/* </summary> */\r
-void mqc_init_dec(unsigned char *bp, int len)\r
-{\r
- mqc_setcurctx(0);\r
- mqc_start = bp;\r
- mqc_end = bp + len;\r
- mqc_bp = bp;\r
- /*add antonin initbug1*/\r
- if (len==0) mqc_c = 0xff << 16;\r
- else mqc_c = *mqc_bp << 16;\r
- /*dda*/\r
- mqc_bytein();\r
- mqc_c <<= 7;\r
- mqc_ct -= 7;\r
- mqc_a = 0x8000;\r
-}\r
-\r
-/* <summary> */\r
-/* Decode a symbol. */\r
-/* </summary> */\r
-int mqc_decode()\r
-{\r
- int d;\r
- mqc_a -= (*mqc_curctx)->qeval;\r
- if ((mqc_c >> 16) < (*mqc_curctx)->qeval) {\r
- d = mqc_lpsexchange();\r
- mqc_renormd();\r
- } else {\r
- mqc_c -= (*mqc_curctx)->qeval << 16;\r
- if ((mqc_a & 0x8000) == 0) {\r
- d = mqc_mpsexchange();\r
- mqc_renormd();\r
- } else {\r
- d = (*mqc_curctx)->mps;\r
- }\r
- }\r
- return d;\r
-}\r
-\r
-/* <summary> */\r
-/* Reset states of all contexts. */\r
-/* </summary> */\r
-void mqc_resetstates()\r
-{\r
- int i;\r
- for (i = 0; i < MQC_NUMCTXS; i++) {\r
- mqc_ctxs[i] = mqc_states;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Set the state for a context. */\r
-/* </summary> */\r
-/* <param name="ctxno">Context number</param> */\r
-/* <param name="msb">Most significant bit</param> */\r
-/* <param name="prob">Index to the probability of symbols</param> */\r
-void mqc_setstate(int ctxno, int msb, int prob)\r
-{\r
- mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)];\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __MQC_H\r
-#define __MQC_H\r
-\r
-/*\r
- * Return the number of bytes written/read since initialisation\r
- */\r
-int mqc_numbytes();\r
-\r
-/*\r
- * Reset the states of all the context of the coder/decoder\r
- * (each context is set to a state where 0 and 1 are more or less equiprobable)\r
- */\r
-void mqc_resetstates();\r
-\r
-/*\r
- * Set the state of a particular context\r
- * ctxno: number that identifies the context\r
- * msb: the MSB of the new state of the context\r
- * prob: number that identifies the probability of the symbols for the new state of the context\r
- */\r
-void mqc_setstate(int ctxno, int msb, int prob);\r
-\r
-/*\r
- * Initialize the encoder\r
- * bp: pointer to the start of the buffer where the bytes will be written\r
- */\r
-void mqc_init_enc(unsigned char *bp);\r
-\r
-/*\r
- * Set the current context used for coding/decoding\r
- * ctxno: number that identifies the context\r
- */\r
-void mqc_setcurctx(int ctxno);\r
-\r
-/*\r
- * Encode a bit\r
- * d: bit to encode (0 or 1)\r
- */\r
-void mqc_encode(int d);\r
-\r
-/*\r
- * Flush the encoder, so that all remaining data is written\r
- */\r
-void mqc_flush();\r
-\r
-/*\r
- * BYPASS mode switch\r
- */\r
-void mqc_bypass_init_enc();\r
-\r
-/*\r
- * BYPASS mode switch\r
- */\r
-void mqc_bypass_enc(int d);\r
-\r
-/*\r
- * BYPASS mode switch\r
- */\r
-int mqc_bypass_flush_enc();\r
-\r
-/*\r
- * RESET mode switch\r
- */\r
-void mqc_reset_enc();\r
-\r
-/*\r
- * RESTART mode switch (TERMALL)\r
- */\r
-int mqc_restart_enc();\r
-\r
-/*\r
- * RESTART mode switch (TERMALL)\r
- */\r
-void mqc_restart_init_enc();\r
-\r
-/*\r
- * ERTERM mode switch (PTERM)\r
- */\r
-void mqc_erterm_enc();\r
-\r
-/*\r
- * SEGMARK mode switch (SEGSYM)\r
- */\r
-void mqc_segmark_enc();\r
-\r
-\r
-/*\r
- * Initialize the decoder\r
- * bp: pointer to the start of the buffer from which the bytes will be read\r
- * len: length of the input buffer\r
- */\r
-void mqc_init_dec(unsigned char *bp, int len);\r
-\r
-/*\r
- * Decode a bit (returns 0 or 1)\r
- */\r
-int mqc_decode();\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2003, Yannick Verschueren\r
- * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __OPENJPEG_H\r
-#define __OPENJPEG_H\r
-\r
-#include <j2k.h>\r
-#include <tcd.h>\r
-#include <jp2.h>\r
-\r
-#include <bio.h>\r
-#include <cio.h>\r
-#include <dwt.h>\r
-#include <fix.h>\r
-#include <int.h>\r
-#include <mct.h>\r
-#include <mqc.h>\r
-#include <pi.h>\r
-#include <raw.h>\r
-#include <t1.h>\r
-#include <t2.h>\r
-#include <tgt.h>\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2003-2004, Yannick Verschueren\r
- * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "pi.h"\r
-#include "int.h"\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-\r
-/* <summary>\r
- * Create a packet iterator.\r
- * </summary> */\r
-pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)\r
-{\r
- int p, q, i;\r
- int compno, resno, pino;\r
- int maxres = 0;\r
- pi_iterator_t *pi;\r
- j2k_tcp_t *tcp;\r
- j2k_tccp_t *tccp;\r
-\r
- tcp = &cp->tcps[tileno];\r
- pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) *\r
- sizeof(pi_iterator_t));\r
-\r
- for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */\r
- p = tileno % cp->tw;\r
- q = tileno / cp->tw;\r
-\r
- pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0);\r
- pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0);\r
- pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);\r
- pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);\r
- pi[pino].numcomps = img->numcomps;\r
- pi[pino].comps =\r
- (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t));\r
-\r
- for (compno = 0; compno < pi->numcomps; compno++) {\r
- int tcx0, tcy0, tcx1, tcy1;\r
- pi_comp_t *comp = &pi[pino].comps[compno];\r
- tccp = &tcp->tccps[compno];\r
- comp->dx = img->comps[compno].dx;\r
- comp->dy = img->comps[compno].dy;\r
- comp->numresolutions = tccp->numresolutions;\r
- comp->resolutions =\r
- (pi_resolution_t *) malloc(comp->numresolutions *\r
- sizeof(pi_resolution_t));\r
- tcx0 = int_ceildiv(pi->tx0, comp->dx);\r
- tcy0 = int_ceildiv(pi->ty0, comp->dy);\r
- tcx1 = int_ceildiv(pi->tx1, comp->dx);\r
- tcy1 = int_ceildiv(pi->ty1, comp->dy);\r
- if (comp->numresolutions > maxres) {\r
- maxres = comp->numresolutions;\r
- }\r
- for (resno = 0; resno < comp->numresolutions; resno++) {\r
- int levelno;\r
- int rx0, ry0, rx1, ry1;\r
- int px0, py0, px1, py1;\r
- pi_resolution_t *res = &comp->resolutions[resno];\r
- if (tccp->csty & J2K_CCP_CSTY_PRT) {\r
- res->pdx = tccp->prcw[resno];\r
- res->pdy = tccp->prch[resno];\r
- } else {\r
- res->pdx = 15;\r
- res->pdy = 15;\r
- }\r
- levelno = comp->numresolutions - 1 - resno;\r
- rx0 = int_ceildivpow2(tcx0, levelno);\r
- ry0 = int_ceildivpow2(tcy0, levelno);\r
- rx1 = int_ceildivpow2(tcx1, levelno);\r
- ry1 = int_ceildivpow2(tcy1, levelno);\r
- px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;\r
- py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;\r
- px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;\r
- py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;\r
- res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); /*Mod Antonin : sizebug1*/\r
- res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); /*Mod Antonin : sizebug1*/\r
- }\r
- }\r
-\r
- tccp = &tcp->tccps[0];\r
- pi[pino].step_p = 1;\r
- pi[pino].step_c = 100 * pi[pino].step_p;\r
- pi[pino].step_r = img->numcomps * pi[pino].step_c;\r
- pi[pino].step_l = maxres * pi[pino].step_r;\r
-\r
- if (pino == 0) {\r
- pi[pino].include =\r
- (short int *) malloc(img->numcomps * maxres *\r
- tcp->numlayers * 100 * sizeof(short int));\r
- for (i = 0; i < img->numcomps * maxres * tcp->numlayers * 100; i++)\r
- pi[pino].include[i] = 0;\r
- }\r
- /* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int)); */\r
- else\r
- pi[pino].include = pi[pino - 1].include;\r
-\r
- if (tcp->POC == 0) {\r
- pi[pino].first = 1;\r
- pi[pino].poc.resno0 = 0;\r
- pi[pino].poc.compno0 = 0;\r
- pi[pino].poc.layno1 = tcp->numlayers;\r
- pi[pino].poc.resno1 = maxres;\r
- pi[pino].poc.compno1 = img->numcomps;\r
- pi[pino].poc.prg = tcp->prg;\r
- } else {\r
- pi[pino].first = 1;\r
- pi[pino].poc.resno0 = tcp->pocs[pino].resno0;\r
- pi[pino].poc.compno0 = tcp->pocs[pino].compno0;\r
- pi[pino].poc.layno1 = tcp->pocs[pino].layno1;\r
- pi[pino].poc.resno1 = tcp->pocs[pino].resno1;\r
- pi[pino].poc.compno1 = tcp->pocs[pino].compno1;\r
- pi[pino].poc.prg = tcp->pocs[pino].prg;\r
- }\r
- }\r
- return pi;\r
-}\r
-\r
-/* <summary>\r
- * Get next packet in layer-resolution-component-precinct order.\r
- * \r
- * pi: packet iterator to modify\r
- * </summary> */\r
-int pi_next_lrcp(pi_iterator_t * pi)\r
-{\r
- pi_comp_t *comp;\r
- pi_resolution_t *res;\r
-\r
- if (!pi->first) {\r
- comp = &pi->comps[pi->compno];\r
- res = &comp->resolutions[pi->resno];\r
- goto skip;\r
- } else {\r
- pi->first = 0;\r
- }\r
- for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
- for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;\r
- pi->resno++) {\r
- for (pi->compno = pi->poc.compno0;\r
- pi->compno < pi->poc.compno1; pi->compno++) {\r
- comp = &pi->comps[pi->compno];\r
- if (pi->resno >= comp->numresolutions) {\r
- continue;\r
- }\r
- res = &comp->resolutions[pi->resno];\r
- for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {\r
- if (!pi->\r
- include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c + pi->precno * pi->step_p]) {\r
- pi->include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p] = 1;\r
- return 1;\r
- }\r
- skip:;\r
- }\r
- }\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-/* <summary>\r
- * Get next packet in resolution-layer-component-precinct order.\r
- *\r
- * pi: packet iterator to modify\r
- * </summary> */\r
-int pi_next_rlcp(pi_iterator_t * pi)\r
-{\r
- pi_comp_t *comp;\r
- pi_resolution_t *res;\r
- if (!pi->first) {\r
- comp = &pi->comps[pi->compno];\r
- res = &comp->resolutions[pi->resno];\r
- goto skip;\r
- } else {\r
- pi->first = 0;\r
- }\r
- for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {\r
- for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
- for (pi->compno = pi->poc.compno0;\r
- pi->compno < pi->poc.compno1; pi->compno++) {\r
- comp = &pi->comps[pi->compno];\r
- if (pi->resno >= comp->numresolutions) {\r
- continue;\r
- }\r
- res = &comp->resolutions[pi->resno];\r
- for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {\r
- if (!pi->\r
- include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c + pi->precno * pi->step_p]) {\r
- pi->include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p] = 1;\r
- return 1;\r
- }\r
- skip:;\r
- }\r
- }\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-/* <summary>\r
- * Get next packet in resolution-precinct-component-layer order.\r
- *\r
- * pi: packet iterator to modify\r
- * </summary> */\r
-int pi_next_rpcl(pi_iterator_t * pi)\r
-{\r
- pi_comp_t *comp;\r
- pi_resolution_t *res;\r
- if (!pi->first) {\r
- goto skip;\r
- } else {\r
- int compno, resno;\r
- pi->first = 0;\r
- pi->dx = 0;\r
- pi->dy = 0;\r
- for (compno = 0; compno < pi->numcomps; compno++) {\r
- comp = &pi->comps[compno];\r
- for (resno = 0; resno < comp->numresolutions; resno++) {\r
- int dx, dy;\r
- res = &comp->resolutions[resno];\r
- dx = comp->dx *\r
- (1 << (res->pdx + comp->numresolutions - 1 - resno));\r
- dy = comp->dy *\r
- (1 << (res->pdy + comp->numresolutions - 1 - resno));\r
- pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);\r
- pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);\r
- }\r
- }\r
- }\r
- for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {\r
- for (pi->y = pi->ty0; pi->y < pi->ty1;\r
- pi->y += pi->dy - (pi->y % pi->dy)) {\r
- for (pi->x = pi->tx0; pi->x < pi->tx1;\r
- pi->x += pi->dx - (pi->x % pi->dx)) {\r
- for (pi->compno = pi->poc.compno0;\r
- pi->compno < pi->poc.compno1; pi->compno++) {\r
- int levelno;\r
- int trx0, try0;\r
- int trx1, try1;/* Add antonin pcrl*/\r
- int rpx, rpy;\r
- int prci, prcj;\r
- comp = &pi->comps[pi->compno];\r
- if (pi->resno >= comp->numresolutions) {\r
- continue;\r
- }\r
- res = &comp->resolutions[pi->resno];\r
- levelno = comp->numresolutions - 1 - pi->resno;\r
- trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);\r
- try0 = int_ceildiv(pi->ty0, comp->dy << levelno);\r
- trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/\r
- try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/\r
- rpx = res->pdx + levelno;\r
- rpy = res->pdy + levelno;\r
- if (!\r
- (pi->x % (comp->dx << rpx) == 0\r
- || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {\r
- continue;\r
- }\r
- if (!\r
- (pi->y % (comp->dy << rpy) == 0\r
- || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {\r
- continue;\r
- }\r
-\r
- /*Add Antonin : sizebug1*/\r
- if ((res->pw==0)||(res->pw==0)) continue;\r
- /*ddA*/\r
-\r
- /*Add Antonin : pcrl*/\r
- if ((trx0==trx1)||(try0==try1)) continue;\r
- /*ddA*/\r
-\r
- prci =\r
- int_floordivpow2(int_ceildiv\r
- (pi->x, comp->dx << levelno),\r
- res->pdx) - int_floordivpow2(trx0, res->pdx);\r
- prcj =\r
- int_floordivpow2(int_ceildiv\r
- (pi->y, comp->dy << levelno),\r
- res->pdy) - int_floordivpow2(try0, res->pdy);\r
- pi->precno = prci + prcj * res->pw;\r
- for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
- if (!pi->\r
- include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p]) {\r
- pi->include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p] = 1;\r
- return 1;\r
- }\r
- skip:;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-/* <summary>\r
- * Get next packet in precinct-component-resolution-layer order.\r
- *\r
- * pi: packet iterator to modify\r
- * </summary> */\r
-int pi_next_pcrl(pi_iterator_t * pi)\r
-{\r
- pi_comp_t *comp;\r
- pi_resolution_t *res;\r
- if (!pi->first) {\r
- comp = &pi->comps[pi->compno];\r
- goto skip;\r
- } else {\r
- int compno, resno;\r
- pi->first = 0;\r
- pi->dx = 0;\r
- pi->dy = 0;\r
- for (compno = 0; compno < pi->numcomps; compno++) {\r
- comp = &pi->comps[compno];\r
- for (resno = 0; resno < comp->numresolutions; resno++) {\r
- int dx, dy;\r
- res = &comp->resolutions[resno];\r
- dx = comp->dx *\r
- (1 << (res->pdx + comp->numresolutions - 1 - resno));\r
- dy = comp->dy *\r
- (1 << (res->pdy + comp->numresolutions - 1 - resno));\r
- pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);\r
- pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);\r
- }\r
- }\r
- }\r
- for (pi->y = pi->ty0; pi->y < pi->ty1;\r
- pi->y += pi->dy - (pi->y % pi->dy)) {\r
- for (pi->x = pi->tx0; pi->x < pi->tx1;\r
- pi->x += pi->dx - (pi->x % pi->dx)) {\r
- for (pi->compno = pi->poc.compno0;\r
- pi->compno < pi->poc.compno1; pi->compno++) {\r
- comp = &pi->comps[pi->compno];\r
- for (pi->resno = pi->poc.resno0;\r
- pi->resno < int_min(pi->poc.resno1,\r
- comp->numresolutions); pi->resno++) {\r
- int levelno;\r
- int trx0, try0;\r
- int trx1, try1;/* Add antonin pcrl*/\r
- int rpx, rpy;\r
- int prci, prcj;\r
- res = &comp->resolutions[pi->resno];\r
- levelno = comp->numresolutions - 1 - pi->resno;\r
- trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);\r
- try0 = int_ceildiv(pi->ty0, comp->dy << levelno);\r
- trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/\r
- try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/\r
- rpx = res->pdx + levelno;\r
- rpy = res->pdy + levelno;\r
- if (!\r
- (pi->x % (comp->dx << rpx) == 0\r
- || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {\r
- continue;\r
- }\r
- if (!\r
- (pi->y % (comp->dy << rpy) == 0\r
- || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {\r
- continue;\r
- }\r
- \r
- /*Add Antonin : sizebug1*/\r
- if ((res->pw==0)||(res->pw==0)) continue;\r
- /*ddA*/\r
-\r
- /*Add Antonin : pcrl*/\r
- if ((trx0==trx1)||(try0==try1)) continue;\r
- /*ddA*/\r
- \r
- prci =\r
- int_floordivpow2(int_ceildiv\r
- (pi->x, comp->dx << levelno),\r
- res->pdx) - int_floordivpow2(trx0, res->pdx);\r
- prcj =\r
- int_floordivpow2(int_ceildiv\r
- (pi->y, comp->dy << levelno),\r
- res->pdy) - int_floordivpow2(try0, res->pdy);\r
- pi->precno = prci + prcj * res->pw;\r
- for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
- if (!pi->\r
- include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p]) {\r
- pi->include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p] = 1;\r
- return 1;\r
- }\r
-skip:;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-/* <summary>\r
- * Get next packet in component-precinct-resolution-layer order.\r
- *\r
- * pi: packet iterator to modify\r
- * </summary> */\r
-int pi_next_cprl(pi_iterator_t * pi)\r
-{\r
- pi_comp_t *comp;\r
- pi_resolution_t *res;\r
- if (!pi->first) {\r
- comp = &pi->comps[pi->compno];\r
- goto skip;\r
- } else {\r
- pi->first = 0;\r
- }\r
- for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;\r
- pi->compno++) {\r
- int resno;\r
- comp = &pi->comps[pi->compno];\r
- pi->dx = 0;\r
- pi->dy = 0;\r
- for (resno = 0; resno < comp->numresolutions; resno++) {\r
- int dx, dy;\r
- res = &comp->resolutions[resno];\r
- dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));\r
- dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));\r
- pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);\r
- pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);\r
- }\r
- for (pi->y = pi->ty0; pi->y < pi->ty1;\r
- pi->y += pi->dy - (pi->y % pi->dy)) {\r
- for (pi->x = pi->tx0; pi->x < pi->tx1;\r
- pi->x += pi->dx - (pi->x % pi->dx)) {\r
- for (pi->resno = pi->poc.resno0;\r
- pi->resno < int_min(pi->poc.resno1,\r
- comp->numresolutions); pi->resno++) {\r
- int levelno;\r
- int trx0, try0;\r
- int trx1, try1;/* Add antonin pcrl*/\r
- int rpx, rpy;\r
- int prci, prcj;\r
- res = &comp->resolutions[pi->resno];\r
- levelno = comp->numresolutions - 1 - pi->resno;\r
- trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);\r
- try0 = int_ceildiv(pi->ty0, comp->dy << levelno);\r
- trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/\r
- try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/\r
- rpx = res->pdx + levelno;\r
- rpy = res->pdy + levelno;\r
- if (!\r
- (pi->x % (comp->dx << rpx) == 0\r
- || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {\r
- continue;\r
- }\r
- if (!\r
- (pi->y % (comp->dy << rpy) == 0\r
- || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {\r
- continue;\r
- }\r
-\r
- /*Add Antonin : sizebug1*/\r
- if ((res->pw==0)||(res->pw==0)) continue;\r
- /*ddA*/\r
-\r
- /*Add Antonin : pcrl*/\r
- if ((trx0==trx1)||(try0==try1)) continue;\r
- /*ddA*/\r
-\r
- prci =\r
- int_floordivpow2(int_ceildiv\r
- (pi->x, comp->dx << levelno),\r
- res->pdx) - int_floordivpow2(trx0, res->pdx);\r
- prcj =\r
- int_floordivpow2(int_ceildiv\r
- (pi->y, comp->dy << levelno),\r
- res->pdy) - int_floordivpow2(try0, res->pdy);\r
- pi->precno = prci + prcj * res->pw;\r
- for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
- if (!pi->\r
- include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p]) {\r
- pi->include[pi->layno * pi->step_l +\r
- pi->resno * pi->step_r +\r
- pi->compno * pi->step_c +\r
- pi->precno * pi->step_p] = 1;\r
- return 1;\r
- }\r
- skip:;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-/* <summary>\r
- * Get next packet.\r
- *\r
- * pi: packet iterator to modify\r
- * </summary> */\r
-int pi_next(pi_iterator_t * pi)\r
-{\r
- switch (pi->poc.prg) {\r
- case 0:\r
- return pi_next_lrcp(pi);\r
- case 1:\r
- return pi_next_rlcp(pi);\r
- case 2:\r
- return pi_next_rpcl(pi);\r
- case 3:\r
- return pi_next_pcrl(pi);\r
- case 4:\r
- return pi_next_cprl(pi);\r
- }\r
- return 0;\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __PI_H\r
-#define __PI_H\r
-\r
-#include "j2k.h"\r
-#include "tcd.h"\r
-\r
-typedef struct {\r
- int pdx, pdy;\r
- int pw, ph;\r
-} pi_resolution_t;\r
-\r
-typedef struct {\r
- int dx, dy;\r
- int numresolutions;\r
- pi_resolution_t *resolutions;\r
-} pi_comp_t;\r
-\r
-typedef struct {\r
- short int *include; /* precise if the packet has been already used (usefull for progression order change) */\r
- int step_l, step_r, step_c, step_p; /* different steps (layer, resolution, component, precinct) to localize the packet in the include vector */\r
- int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */\r
- int first; /* 0 if the first packet */\r
- j2k_poc_t poc;\r
- int numcomps;\r
- pi_comp_t *comps;\r
- int tx0, ty0, tx1, ty1;\r
- int x, y, dx, dy;\r
-} pi_iterator_t; /* packet iterator */\r
-\r
-/*\r
- * Create a packet iterator\r
- * img: raw image for which the packets will be listed\r
- * cp: coding paremeters\r
- * tileno: number that identifies the tile for which to list the packets\r
- * return value: returns a packet iterator that points to the first packet of the tile\r
- */\r
-pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno);\r
-\r
-/* \r
- * Modify the packet iterator to point to the next packet\r
- * pi: packet iterator to modify\r
- * return value: returns 0 if pi pointed to the last packet or else returns 1 \r
- */\r
-int pi_next(pi_iterator_t * pi);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2002-2003, Antonin Descampe\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "raw.h"\r
-\r
-\r
-unsigned char raw_c; /* temporary buffer where bits are coded or decoded */\r
-unsigned int raw_ct; /* number of bits already read or free to write */\r
-unsigned int raw_lenmax; /* maximum length to decode */\r
-unsigned int raw_len; /* length decoded */\r
-unsigned char *raw_bp; /* pointer to the current position in the buffer */\r
-unsigned char *raw_start; /* pointer to the start of the buffer */\r
-unsigned char *raw_end; /* pointer to the end of the buffer */\r
-\r
-/*\r
- * Return the number of bytes already encoded.\r
- */\r
-int raw_numbytes()\r
-{\r
- return raw_bp - raw_start;\r
-}\r
-\r
-/*\r
- * Initialize raw-decoder.\r
- *\r
- * bp : pointer to the start of the buffer from which the bytes will be read\r
- * len : length of the input buffer\r
- */\r
-void raw_init_dec(unsigned char *bp, int len)\r
-{\r
- raw_start = bp;\r
- raw_lenmax = len;\r
- raw_len = 0;\r
- raw_c = 0;\r
- raw_ct = 0;\r
-}\r
-\r
-/*\r
- * Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN\r
- */\r
-int raw_decode()\r
-{\r
- int d;\r
- if (raw_ct == 0) {\r
- raw_ct = 8;\r
- if (raw_len == raw_lenmax)\r
- raw_c = 0xff;\r
- else {\r
- if (raw_c == 0xff)\r
- raw_ct = 7;\r
- raw_c = *(raw_start + raw_len);\r
- raw_len++;\r
- }\r
- }\r
- raw_ct--;\r
- d = (raw_c >> raw_ct) & 0x01;\r
- return d;\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2002-2003, Antonin Descampe\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __RAW_H\r
-#define __RAW_H\r
-\r
-/*\r
- * Return the number of bytes written/read since initialisation\r
- */\r
-int raw_numbytes();\r
-\r
-/*\r
- * Initialize the decoder\r
- * bp: pointer to the start of the buffer from which the bytes will be read\r
- * len: length of the input buffer\r
- */\r
-void raw_init_dec(unsigned char *bp, int len);\r
-\r
-/*\r
- * Decode a bit (returns 0 or 1)\r
- */\r
-int raw_decode();\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "t1.h"\r
-#include "j2k.h"\r
-#include "mqc.h"\r
-#include "raw.h" /* Antonin */\r
-#include "int.h"\r
-#include "mct.h"\r
-#include "dwt.h"\r
-#include "fix.h"\r
-#include <stdio.h>\r
-#include <math.h>\r
-#include <stdlib.h>\r
-\r
-#define T1_MAXCBLKW 1024\r
-#define T1_MAXCBLKH 1024\r
-\r
-#define T1_SIG_NE 0x0001\r
-#define T1_SIG_SE 0x0002\r
-#define T1_SIG_SW 0x0004\r
-#define T1_SIG_NW 0x0008\r
-#define T1_SIG_N 0x0010\r
-#define T1_SIG_E 0x0020\r
-#define T1_SIG_S 0x0040\r
-#define T1_SIG_W 0x0080\r
-#define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)\r
-#define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)\r
-\r
-#define T1_SGN_N 0x0100\r
-#define T1_SGN_E 0x0200\r
-#define T1_SGN_S 0x0400\r
-#define T1_SGN_W 0x0800\r
-#define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)\r
-\r
-#define T1_SIG 0x1000\r
-#define T1_REFINE 0x2000\r
-#define T1_VISIT 0x4000\r
-\r
-#define T1_NUMCTXS_AGG 1\r
-#define T1_NUMCTXS_ZC 9\r
-#define T1_NUMCTXS_MAG 3\r
-#define T1_NUMCTXS_SC 5\r
-#define T1_NUMCTXS_UNI 1\r
-\r
-#define T1_CTXNO_AGG 0\r
-#define T1_CTXNO_ZC (T1_CTXNO_AGG+T1_NUMCTXS_AGG)\r
-#define T1_CTXNO_MAG (T1_CTXNO_ZC+T1_NUMCTXS_ZC)\r
-#define T1_CTXNO_SC (T1_CTXNO_MAG+T1_NUMCTXS_MAG)\r
-#define T1_CTXNO_UNI (T1_CTXNO_SC+T1_NUMCTXS_SC)\r
-#define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)\r
-\r
-#define T1_NMSEDEC_BITS 7\r
-#define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)\r
-\r
-/* add TONY */\r
-#define T1_TYPE_MQ 0\r
-#define T1_TYPE_RAW 1\r
-/* dda */\r
-\r
-static int t1_lut_ctxno_zc[1024];\r
-static int t1_lut_ctxno_sc[256];\r
-static int t1_lut_ctxno_mag[4096];\r
-static int t1_lut_spb[256];\r
-static int t1_lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];\r
-static int t1_lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];\r
-static int t1_lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];\r
-static int t1_lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];\r
-\r
-static int t1_data[T1_MAXCBLKH][T1_MAXCBLKW];\r
-static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2];\r
-\r
-int t1_getctxno_zc(int f, int orient)\r
-{\r
- return t1_lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];\r
-}\r
-\r
-int t1_getctxno_sc(int f)\r
-{\r
- return t1_lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];\r
-}\r
-\r
-int t1_getctxno_mag(int f)\r
-{\r
- return t1_lut_ctxno_mag[(f & T1_SIG_OTH) |\r
- (((f & T1_REFINE) != 0) << 11)];\r
-}\r
-\r
-int t1_getspb(int f)\r
-{\r
- return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];\r
-}\r
-\r
-int t1_getnmsedec_sig(int x, int bitpos)\r
-{\r
- if (bitpos > T1_NMSEDEC_FRACBITS)\r
- return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &\r
- ((1 << T1_NMSEDEC_BITS) - 1)];\r
- else\r
- return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-}\r
-\r
-int t1_getnmsedec_ref(int x, int bitpos)\r
-{\r
- if (bitpos > T1_NMSEDEC_FRACBITS)\r
- return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &\r
- ((1 << T1_NMSEDEC_BITS) - 1)];\r
- else\r
- return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-}\r
-\r
-void t1_updateflags(int *fp, int s)\r
-{\r
- int *np = fp - (T1_MAXCBLKW + 2);\r
- int *sp = fp + (T1_MAXCBLKW + 2);\r
- np[-1] |= T1_SIG_SE;\r
- np[1] |= T1_SIG_SW;\r
- sp[-1] |= T1_SIG_NE;\r
- sp[1] |= T1_SIG_NW;\r
- *np |= T1_SIG_S;\r
- *sp |= T1_SIG_N;\r
- fp[-1] |= T1_SIG_E;\r
- fp[1] |= T1_SIG_W;\r
- if (s) {\r
- *np |= T1_SGN_S;\r
- *sp |= T1_SGN_N;\r
- fp[-1] |= T1_SGN_E;\r
- fp[1] |= T1_SGN_W;\r
- }\r
-}\r
-\r
-void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,\r
- int *nmsedec, char type, int vsc)\r
-{\r
- int v, flag;\r
- flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))\r
- : (*fp);\r
- if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {\r
- v = int_abs(*dp) & one ? 1 : 0;\r
- if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */\r
- mqc_setcurctx(t1_getctxno_zc(flag, orient)); /* ESSAI */\r
- mqc_bypass_enc(v);\r
- } else {\r
- mqc_setcurctx(t1_getctxno_zc(flag, orient));\r
- mqc_encode(v);\r
- }\r
- if (v) {\r
- v = *dp < 0 ? 1 : 0;\r
- *nmsedec +=\r
- t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
- if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */\r
- mqc_setcurctx(t1_getctxno_sc(flag)); /* ESSAI */\r
- mqc_bypass_enc(v);\r
- } else {\r
- mqc_setcurctx(t1_getctxno_sc(flag));\r
- mqc_encode(v ^ t1_getspb(flag));\r
- }\r
- t1_updateflags(fp, v);\r
- *fp |= T1_SIG;\r
- }\r
- *fp |= T1_VISIT;\r
- }\r
-}\r
-\r
-\r
-\r
-void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,\r
- char type, int vsc)\r
-{\r
- int v, flag;\r
- flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))\r
- : (*fp);\r
- if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {\r
- if (type == T1_TYPE_RAW) {\r
- if (raw_decode()) {\r
- v = raw_decode(); /* ESSAI */\r
- *dp = v ? -oneplushalf : oneplushalf;\r
- t1_updateflags(fp, v);\r
- *fp |= T1_SIG;\r
- }\r
- } else {\r
- mqc_setcurctx(t1_getctxno_zc(flag, orient));\r
- if (mqc_decode()) {\r
- mqc_setcurctx(t1_getctxno_sc(flag));\r
- v = mqc_decode() ^ t1_getspb(flag);\r
- *dp = v ? -oneplushalf : oneplushalf;\r
- t1_updateflags(fp, v);\r
- *fp |= T1_SIG;\r
- }\r
- }\r
- *fp |= T1_VISIT;\r
- }\r
-} /* VSC and BYPASS by Antonin */\r
-\r
-void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,\r
- char type, int cblksty)\r
-{\r
- int i, j, k, one, vsc;\r
- *nmsedec = 0;\r
- one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
- for (k = 0; k < h; k += 4) {\r
- for (i = 0; i < w; i++) {\r
- for (j = k; j < k + 4 && j < h; j++) {\r
- vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)\r
- && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
- t1_enc_sigpass_step(&t1_flags[1 + j][1 + i],\r
- &t1_data[j][i], orient, bpno, one,\r
- nmsedec, type, vsc);\r
- }\r
- }\r
- }\r
-}\r
-\r
-void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,\r
- int cblksty)\r
-{\r
- int i, j, k, one, half, oneplushalf, vsc;\r
- one = 1 << bpno;\r
- half = one >> 1;\r
- oneplushalf = one | half;\r
- for (k = 0; k < h; k += 4) {\r
- for (i = 0; i < w; i++) {\r
- for (j = k; j < k + 4 && j < h; j++) {\r
- vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)\r
- && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
- t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],\r
- &t1_data[j][i], \r
-\r
- orient, \r
-\r
- oneplushalf,\r
- type, vsc);\r
- }\r
- }\r
- }\r
-} /* VSC and BYPASS by Antonin */\r
-\r
-void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,\r
- char type, int vsc)\r
-{\r
- int v, flag;\r
- flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))\r
- : (*fp);\r
- if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {\r
- *nmsedec +=\r
- t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
- v = int_abs(*dp) & one ? 1 : 0;\r
- if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */\r
- mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */\r
- mqc_bypass_enc(v);\r
- } else {\r
- mqc_setcurctx(t1_getctxno_mag(flag));\r
- mqc_encode(v);\r
- }\r
- *fp |= T1_REFINE;\r
- }\r
-}\r
-\r
-\r
-\r
-void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,\r
- char type, int vsc)\r
-{\r
- int v, t, flag;\r
- flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))\r
- : (*fp);\r
- if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {\r
- if (type == T1_TYPE_RAW) {\r
- mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */\r
- v = raw_decode();\r
- } else {\r
- mqc_setcurctx(t1_getctxno_mag(flag));\r
- v = mqc_decode();\r
- }\r
- t = v ? poshalf : neghalf;\r
- *dp += *dp < 0 ? -t : t;\r
- *fp |= T1_REFINE;\r
- }\r
-} /* VSC and BYPASS by Antonin */\r
-\r
-void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,\r
- int cblksty)\r
-{\r
- int i, j, k, one, vsc;\r
- *nmsedec = 0;\r
- one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
- for (k = 0; k < h; k += 4) {\r
- for (i = 0; i < w; i++) {\r
- for (j = k; j < k + 4 && j < h; j++) {\r
- vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)\r
- && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
- t1_enc_refpass_step(&t1_flags[1 + j][1 + i],\r
- &t1_data[j][i], bpno, one, nmsedec, type, vsc);\r
- }\r
- }\r
- }\r
-}\r
-\r
-void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)\r
-{\r
- int i, j, k, one, poshalf, neghalf;\r
- int vsc;\r
- one = 1 << bpno;\r
- poshalf = one >> 1;\r
- neghalf = bpno > 0 ? -poshalf : -1;\r
- for (k = 0; k < h; k += 4) {\r
- for (i = 0; i < w; i++) {\r
- for (j = k; j < k + 4 && j < h; j++) {\r
- vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)\r
- && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
- t1_dec_refpass_step(&t1_flags[1 + j][1 + i],\r
- &t1_data[j][i], \r
-\r
- poshalf, \r
-\r
- neghalf, \r
-\r
- type, vsc);\r
- }\r
- }\r
- }\r
-} /* VSC and BYPASS by Antonin */\r
-\r
-void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,\r
- int *nmsedec, int partial, int vsc)\r
-{\r
- int v, flag;\r
- flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))\r
- : (*fp);\r
- if (partial)\r
- goto label_partial;\r
- if (!(*fp & (T1_SIG | T1_VISIT))) {\r
- mqc_setcurctx(t1_getctxno_zc(flag, orient));\r
- v = int_abs(*dp) & one ? 1 : 0;\r
- mqc_encode(v);\r
- if (v) {\r
- label_partial:\r
- *nmsedec +=\r
- t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
- mqc_setcurctx(t1_getctxno_sc(flag));\r
- v = *dp < 0 ? 1 : 0;\r
- mqc_encode(v ^ t1_getspb(flag));\r
- t1_updateflags(fp, v);\r
- *fp |= T1_SIG;\r
- }\r
- }\r
- *fp &= ~T1_VISIT;\r
-}\r
-\r
-\r
-\r
-\r
-void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,\r
- int partial, int vsc)\r
-{\r
- int v, flag;\r
- flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))\r
- : (*fp);\r
- if (partial)\r
- goto label_partial;\r
- if (!(flag & (T1_SIG | T1_VISIT))) {\r
- mqc_setcurctx(t1_getctxno_zc(flag, orient));\r
- if (mqc_decode()) {\r
- label_partial:\r
- mqc_setcurctx(t1_getctxno_sc(flag));\r
- v = mqc_decode() ^ t1_getspb(flag);\r
- *dp = v ? -oneplushalf : oneplushalf;\r
- t1_updateflags(fp, v);\r
- *fp |= T1_SIG;\r
- }\r
- }\r
- *fp &= ~T1_VISIT;\r
-} /* VSC and BYPASS by Antonin */\r
-\r
-void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,\r
- int cblksty)\r
-{\r
- int i, j, k, one, agg, runlen, vsc;\r
- *nmsedec = 0;\r
- one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
- for (k = 0; k < h; k += 4) {\r
- for (i = 0; i < w; i++) {\r
- if (k + 3 < h) {\r
- if (cblksty & J2K_CCP_CBLKSTY_VSC) {\r
- agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
- || t1_flags[1 + k + 1][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || t1_flags[1 + k + 2][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || (t1_flags[1 + k + 3][1 + i] &\r
- (~\r
- (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |\r
- T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));\r
- } else {\r
- agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
- || t1_flags[1 + k + 1][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || t1_flags[1 + k + 2][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || t1_flags[1 + k + 3][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH));\r
- }\r
- } else {\r
- agg = 0;\r
- }\r
- if (agg) {\r
- for (runlen = 0; runlen < 4; runlen++) {\r
- if (int_abs(t1_data[k + runlen][i]) & one)\r
- break;\r
- }\r
- mqc_setcurctx(T1_CTXNO_AGG);\r
- mqc_encode(runlen != 4);\r
- if (runlen == 4) {\r
- continue;\r
- }\r
- mqc_setcurctx(T1_CTXNO_UNI);\r
- mqc_encode(runlen >> 1);\r
- mqc_encode(runlen & 1);\r
- } else {\r
- runlen = 0;\r
- }\r
- for (j = k + runlen; j < k + 4 && j < h; j++) {\r
- vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)\r
- && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
- t1_enc_clnpass_step(&t1_flags[1 + j][1 + i],\r
- &t1_data[j][i], orient, bpno, one,\r
- nmsedec, agg && (j == k + runlen), vsc);\r
- }\r
- }\r
- }\r
-}\r
-\r
-void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)\r
-{\r
- int i, j, k, one, half, oneplushalf, agg, runlen, vsc;\r
- int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;\r
- one = 1 << bpno;\r
- half = one >> 1;\r
- oneplushalf = one | half;\r
- for (k = 0; k < h; k += 4) {\r
- for (i = 0; i < w; i++) {\r
- if (k + 3 < h) {\r
- if (cblksty & J2K_CCP_CBLKSTY_VSC) {\r
- agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
- || t1_flags[1 + k + 1][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || t1_flags[1 + k + 2][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || (t1_flags[1 + k + 3][1 + i] &\r
- (~\r
- (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |\r
- T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));\r
- } else {\r
- agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
- || t1_flags[1 + k + 1][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || t1_flags[1 + k + 2][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH)\r
- || t1_flags[1 + k + 3][1 +\r
- i] & (T1_SIG | T1_VISIT |\r
- T1_SIG_OTH));\r
- }\r
- } else {\r
- agg = 0;\r
- }\r
- if (agg) {\r
- mqc_setcurctx(T1_CTXNO_AGG);\r
- if (!mqc_decode()) {\r
- continue;\r
- }\r
- mqc_setcurctx(T1_CTXNO_UNI);\r
- runlen = mqc_decode();\r
- runlen = (runlen << 1) | mqc_decode();\r
- } else {\r
- runlen = 0;\r
- }\r
- for (j = k + runlen; j < k + 4 && j < h; j++) {\r
- vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)\r
- && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
- t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],\r
- &t1_data[j][i], \r
-\r
- orient, \r
-\r
- oneplushalf,\r
- agg && (j == k + runlen), vsc);\r
- }\r
- }\r
- }\r
- if (segsym) {\r
- int v = 0;\r
- mqc_setcurctx(T1_CTXNO_UNI);\r
- v = mqc_decode();\r
- v = (v << 1) | mqc_decode();\r
- v = (v << 1) | mqc_decode();\r
- v = (v << 1) | mqc_decode();\r
- /* if (v!=0xa) \r
- {\r
- fprintf(stderr, "warning: bad segmentation symbol %x\n",v);\r
- } */\r
- }\r
-} /* VSC and BYPASS by Antonin */\r
-\r
-double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, float stepsize, int numcomps) /*mod fixed_quality*/\r
-{\r
- double w1, w2, wmsedec;\r
- if (qmfbid == 1) {\r
- w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;\r
- w2 = dwt_getnorm(level, orient);\r
- } else { /* if (qmfbid == 0) */\r
- w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;\r
- w2 = dwt_getnorm_real(level, orient);\r
- }\r
- wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);\r
- wmsedec *= wmsedec * nmsedec / 8192.0;\r
- return wmsedec;\r
-}\r
-\r
-void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, float stepsize, int cblksty, int numcomps, tcd_tile_t * tile) /*mod fixed_quality*/\r
-{\r
- int i, j;\r
- int w, h;\r
- int passno;\r
- int bpno, passtype;\r
- int max;\r
- int nmsedec;\r
- double cumwmsedec = 0;\r
- char type = T1_TYPE_MQ;\r
-\r
- w = cblk->x1 - cblk->x0;\r
- h = cblk->y1 - cblk->y0;\r
-\r
- max = 0;\r
- for (j = 0; j < h; j++) {\r
- for (i = 0; i < w; i++) {\r
- max = int_max(max, int_abs(t1_data[j][i]));\r
- }\r
- }\r
-\r
- cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;\r
-\r
- /* Changed by Dmitry Kolyadin */\r
- for (i = 0; i <= w; i++)\r
- for (j = 0; j <= h; j++){\r
- t1_flags[j][i] = 0;\r
- }\r
-\r
- bpno = cblk->numbps - 1;\r
- passtype = 2;\r
-\r
- mqc_resetstates();\r
- mqc_setstate(T1_CTXNO_UNI, 0, 46);\r
- mqc_setstate(T1_CTXNO_AGG, 0, 3);\r
- mqc_setstate(T1_CTXNO_ZC, 0, 4);\r
- mqc_init_enc(cblk->data);\r
-\r
- for (passno = 0; bpno >= 0; passno++) {\r
- tcd_pass_t *pass = &cblk->passes[passno];\r
- int correction = 3;\r
- type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)\r
- && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :\r
- T1_TYPE_MQ;\r
-\r
- switch (passtype) {\r
- case 0:\r
- t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);\r
- break;\r
- case 1:\r
- t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);\r
- break;\r
- case 2:\r
- t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);\r
- /* code switch SEGMARK (i.e. SEGSYM) */\r
- if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)\r
- mqc_segmark_enc();\r
- break;\r
- }\r
-\r
- cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); /*mod fixed_quality*/\r
- tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); /*add antonin quality*/\r
-\r
-\r
- /* Code switch "RESTART" (i.e. TERMALL) */\r
- if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)\r
- && !((passtype == 2) && (bpno - 1 < 0))) {\r
- if (type == T1_TYPE_RAW) {\r
- mqc_flush();\r
- correction = 1;\r
- /* correction = mqc_bypass_flush_enc(); */\r
- } else { /* correction = mqc_restart_enc(); */\r
- mqc_flush();\r
- correction = 1;\r
- }\r
- pass->term = 1;\r
- } else {\r
- if (((bpno < (cblk->numbps - 4) && (passtype > 0))\r
- || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))\r
- && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {\r
- if (type == T1_TYPE_RAW) {\r
- mqc_flush();\r
- correction = 1;\r
- /* correction = mqc_bypass_flush_enc(); */\r
- } else { /* correction = mqc_restart_enc(); */\r
- mqc_flush();\r
- correction = 1;\r
- }\r
- pass->term = 1;\r
- } else {\r
- pass->term = 0;\r
- }\r
- }\r
-\r
- if (++passtype == 3) {\r
- passtype = 0;\r
- bpno--;\r
- }\r
-\r
- if (pass->term && bpno > 0) {\r
- type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)\r
- && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :\r
- T1_TYPE_MQ;\r
- if (type == T1_TYPE_RAW)\r
- mqc_bypass_init_enc();\r
- else\r
- mqc_restart_init_enc();\r
- }\r
-\r
- pass->distortiondec = cumwmsedec;\r
- pass->rate = mqc_numbytes() + correction; /* FIXME */\r
- pass->len =\r
- pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);\r
-\r
- /* Code-switch "RESET" */\r
- if (cblksty & J2K_CCP_CBLKSTY_RESET)\r
- mqc_reset_enc();\r
- }\r
-\r
- /* Code switch "ERTERM" (i.e. PTERM) */\r
- if (cblksty & J2K_CCP_CBLKSTY_PTERM)\r
- mqc_erterm_enc();\r
- else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))\r
- mqc_flush();\r
-\r
- cblk->totalpasses = passno;\r
-}\r
-\r
-void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,\r
- int cblksty)\r
-{\r
- int i, j, w, h;\r
- int bpno, passtype;\r
- int segno, passno;\r
- char type = T1_TYPE_MQ; /*BYPASS mode*/\r
- \r
- w = cblk->x1 - cblk->x0;\r
- h = cblk->y1 - cblk->y0;\r
- \r
- /* Changed by Dmitry Kolyadin */\r
- for (j = 0; j <= h; j++){\r
- for (i = 0; i <= w; i++) {\r
- t1_flags[j][i] = 0;\r
- }\r
- }\r
- \r
- /* Changed by Dmitry Kolyadin */\r
- for (i = 0; i < w; i++) {\r
- for (j = 0; j < h; j++){\r
- t1_data[j][i] = 0;\r
- }\r
- }\r
- \r
- bpno = roishift + cblk->numbps - 1;\r
- passtype = 2;\r
-\r
- mqc_resetstates();\r
- mqc_setstate(T1_CTXNO_UNI, 0, 46);\r
- mqc_setstate(T1_CTXNO_AGG, 0, 3);\r
- mqc_setstate(T1_CTXNO_ZC, 0, 4);\r
-\r
- for (segno = 0; segno < cblk->numsegs; segno++) {\r
- tcd_seg_t *seg = &cblk->segs[segno];\r
- \r
- /* Add BYPASS mode */\r
- type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)\r
- && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :\r
- T1_TYPE_MQ;\r
- if (type == T1_TYPE_RAW) \r
- raw_init_dec(seg->data, seg->len);\r
- else\r
- mqc_init_dec(seg->data, seg->len);\r
- /* ddA */\r
- \r
- for (passno = 0; passno < seg->numpasses; passno++) {\r
- switch (passtype) {\r
- case 0:\r
- t1_dec_sigpass(w, h, bpno+1, orient, type, cblksty);\r
- break;\r
- case 1:\r
- t1_dec_refpass(w, h, bpno+1, type, cblksty);\r
- break;\r
- case 2:\r
- t1_dec_clnpass(w, h, bpno+1, orient, cblksty);\r
- break;\r
- }\r
- \r
- if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {\r
- mqc_resetstates();\r
-\r
- mqc_setstate(T1_CTXNO_UNI, 0, 46);\r
-\r
- mqc_setstate(T1_CTXNO_AGG, 0, 3);\r
-\r
- mqc_setstate(T1_CTXNO_ZC, 0, 4);\r
-\r
- }\r
- \r
- if (++passtype == 3) {\r
- passtype = 0;\r
- bpno--;\r
- }\r
- }\r
- }\r
-}\r
-\r
-void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)\r
-{\r
- int compno, resno, bandno, precno, cblkno;\r
- int x, y, i, j, orient;\r
- tcd_tilecomp_t *tilec;\r
- tcd_resolution_t *res;\r
- tcd_band_t *band;\r
- tcd_precinct_t *prc;\r
- tcd_cblk_t *cblk;\r
-\r
- tile->distotile = 0; /*add fixed_quality*/\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tilec = &tile->comps[compno];\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- res = &tilec->resolutions[resno];\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- band = &res->bands[bandno];\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- cblk = &prc->cblks[cblkno];\r
-\r
- if (band->bandno == 0) {\r
- x = cblk->x0 - band->x0;\r
- y = cblk->y0 - band->y0;\r
- } else if (band->bandno == 1) {\r
- tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
- x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
- y = cblk->y0 - band->y0;\r
- } else if (band->bandno == 2) {\r
- tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
- x = cblk->x0 - band->x0;\r
- y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
- } else { /* if (band->bandno == 3) */\r
- tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
- x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
- y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
- }\r
-\r
- if (tcp->tccps[compno].qmfbid == 1) {\r
-\r
- for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
- for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
- t1_data[j][i] =\r
- tilec->data[(x + i) +\r
- (y + j) * (tilec->x1 -\r
- tilec->\r
- x0)] << T1_NMSEDEC_FRACBITS;\r
- }\r
- }\r
- } else if (tcp->tccps[compno].qmfbid == 0) {\r
- for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
- for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
- t1_data[j][i] =\r
- fix_mul(tilec->\r
- data[x + i +\r
- (y + j) * (tilec->x1 -\r
- tilec->\r
- x0)],\r
- 8192 * 8192 /\r
- ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);\r
- }\r
- }\r
- }\r
- orient = band->bandno; /* FIXME */\r
- if (orient == 2) {\r
- orient = 1;\r
- } else if (orient == 1) {\r
- orient = 2;\r
- }\r
- t1_encode_cblk(cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile); /*mod fixed_quality*/\r
- } /* cblkno */\r
- } /* precno */\r
- } /* bandno */\r
- } /* resno */\r
- } /* compo */\r
-}\r
-\r
-\r
-void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)\r
-{\r
- int compno, resno, bandno, precno, cblkno;\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- int x, y, i, j, orient;\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- orient = band->bandno; /* FIXME */\r
- if (orient == 2)\r
- orient = 1;\r
- else if (orient == 1)\r
- orient = 2;\r
- t1_decode_cblk(cblk, orient,\r
- tcp->tccps[compno].roishift,\r
- tcp->tccps[compno].cblksty);\r
- if (band->bandno == 0) {\r
- x = cblk->x0 - band->x0;\r
- y = cblk->y0 - band->y0;\r
- } else if (band->bandno == 1) {\r
- tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
- x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
- y = cblk->y0 - band->y0;\r
- } else if (band->bandno == 2) {\r
- tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
- x = cblk->x0 - band->x0;\r
- y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
- } else { /* if (band->bandno == 3) */\r
- tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
- x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
- y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
- }\r
-\r
- if (tcp->tccps[compno].roishift) {\r
- int thresh, val, mag;\r
- thresh = 1 << tcp->tccps[compno].roishift;\r
- for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
- for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
- val = t1_data[j][i];\r
- mag = int_abs(val);\r
- if (mag >= thresh) {\r
- mag >>= tcp->tccps[compno].roishift;\r
- t1_data[j][i] = val < 0 ? -mag : mag;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if (tcp->tccps[compno].qmfbid == 1) {\r
- for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
- for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
- int tmp=t1_data[j][i];\r
- if (tmp>>1==0) tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;\r
- else tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);\r
- }\r
- }\r
- } else { /* if (tcp->tccps[compno].qmfbid == 0) */\r
-\r
- for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
- for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
- double tmp=t1_data[j][i] * band->stepsize * 4096.0;\r
- int tmp2;\r
- if (t1_data[j][i]>>1 == 0) {\r
- tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;\r
- } else {\r
- tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);\r
- tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2); \r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
-}\r
-\r
-int t1_init_ctxno_zc(int f, int orient)\r
-{\r
- int h, v, d, n, t, hv;\r
- n = 0;\r
- h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);\r
- v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);\r
- d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=\r
- 0) + ((f & T1_SIG_SE) !=\r
- 0) + ((f & T1_SIG_SW) != 0);\r
- switch (orient) {\r
- case 2:\r
- t = h;\r
- h = v;\r
- v = t;\r
- case 0:\r
- case 1:\r
- if (!h) {\r
- if (!v) {\r
- if (!d)\r
- n = 0;\r
- else if (d == 1)\r
- n = 1;\r
- else\r
- n = 2;\r
- } else if (v == 1)\r
- n = 3;\r
- else\r
- n = 4;\r
- } else if (h == 1) {\r
- if (!v) {\r
- if (!d)\r
- n = 5;\r
- else\r
- n = 6;\r
- } else\r
- n = 7;\r
- } else\r
- n = 8;\r
- break;\r
- case 3:\r
- hv = h + v;\r
- if (!d) {\r
- if (!hv)\r
- n = 0;\r
- else if (hv == 1)\r
- n = 1;\r
- else\r
- n = 2;\r
- } else if (d == 1) {\r
- if (!hv)\r
- n = 3;\r
- else if (hv == 1)\r
- n = 4;\r
- else\r
- n = 5;\r
- } else if (d == 2) {\r
- if (!hv)\r
- n = 6;\r
- else\r
- n = 7;\r
- } else\r
- n = 8;\r
- break;\r
- }\r
- return T1_CTXNO_ZC + n;\r
-}\r
-\r
-int t1_init_ctxno_sc(int f)\r
-{\r
- int hc, vc, n;\r
- n = 0;\r
- hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
- T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),\r
- 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
- (T1_SIG_E | T1_SGN_E)) +\r
- ((f & (T1_SIG_W | T1_SGN_W)) ==\r
- (T1_SIG_W | T1_SGN_W)), 1);\r
- vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
- T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),\r
- 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
- (T1_SIG_N | T1_SGN_N)) +\r
- ((f & (T1_SIG_S | T1_SGN_S)) ==\r
- (T1_SIG_S | T1_SGN_S)), 1);\r
- if (hc < 0) {\r
- hc = -hc;\r
- vc = -vc;\r
- }\r
- if (!hc) {\r
- if (vc == -1)\r
- n = 1;\r
- else if (!vc)\r
- n = 0;\r
- else\r
- n = 1;\r
- } else if (hc == 1) {\r
- if (vc == -1)\r
- n = 2;\r
- else if (!vc)\r
- n = 3;\r
- else\r
- n = 4;\r
- }\r
- return T1_CTXNO_SC + n;\r
-}\r
-\r
-int t1_init_ctxno_mag(int f)\r
-{\r
- int n;\r
- if (!(f & T1_REFINE))\r
- n = (f & (T1_SIG_OTH)) ? 1 : 0;\r
- else\r
- n = 2;\r
- return T1_CTXNO_MAG + n;\r
-}\r
-\r
-int t1_init_spb(int f)\r
-{\r
- int hc, vc, n;\r
- hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
- T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),\r
- 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
- (T1_SIG_E | T1_SGN_E)) +\r
- ((f & (T1_SIG_W | T1_SGN_W)) ==\r
- (T1_SIG_W | T1_SGN_W)), 1);\r
- vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
- T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),\r
- 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
- (T1_SIG_N | T1_SGN_N)) +\r
- ((f & (T1_SIG_S | T1_SGN_S)) ==\r
- (T1_SIG_S | T1_SGN_S)), 1);\r
- if (!hc && !vc)\r
- n = 0;\r
- else\r
- n = (!(hc > 0 || (!hc && vc > 0)));\r
- return n;\r
-}\r
-\r
-void t1_init_luts()\r
-{\r
- int i, j;\r
- double u, v, t;\r
- for (j = 0; j < 4; j++) {\r
- for (i = 0; i < 256; ++i) {\r
- t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);\r
- }\r
- }\r
- for (i = 0; i < 256; i++) {\r
- t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);\r
- }\r
- for (j = 0; j < 2; j++) {\r
- for (i = 0; i < 2048; ++i) {\r
- t1_lut_ctxno_mag[(j << 11) + i] =\r
- t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);\r
- }\r
- }\r
- for (i = 0; i < 256; ++i) {\r
- t1_lut_spb[i] = t1_init_spb(i << 4);\r
- }\r
- /* FIXME FIXME FIXME */\r
- /* fprintf(stdout,"nmsedec luts:\n"); */\r
- for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {\r
- t = i / pow(2, T1_NMSEDEC_FRACBITS);\r
- u = t;\r
- v = t - 1.5;\r
- t1_lut_nmsedec_sig[i] =\r
- int_max(0,\r
- (int) (floor\r
- ((u * u - v * v) * pow(2,\r
- T1_NMSEDEC_FRACBITS) +\r
- 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
- t1_lut_nmsedec_sig0[i] =\r
- int_max(0,\r
- (int) (floor\r
- ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +\r
- 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
- u = t - 1.0;\r
- if (i & (1 << (T1_NMSEDEC_BITS - 1))) {\r
- v = t - 1.5;\r
- } else {\r
- v = t - 0.5;\r
- }\r
- t1_lut_nmsedec_ref[i] =\r
- int_max(0,\r
- (int) (floor\r
- ((u * u - v * v) * pow(2,\r
- T1_NMSEDEC_FRACBITS) +\r
- 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
- t1_lut_nmsedec_ref0[i] =\r
- int_max(0,\r
- (int) (floor\r
- ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +\r
- 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
- }\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __T1_H\r
-#define __T1_H\r
-\r
-#include "tcd.h"\r
-#include "j2k.h"\r
-\r
-/*\r
- * Initialize the look-up tables of the Tier-1 coder/decoder\r
- */\r
-void t1_init_luts();\r
-\r
-/*\r
- * Encode the code-blocks of a tile\r
- * tile: the tile to encode\r
- * tcp: tile coding parameters\r
- */\r
-void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp);\r
-\r
-/*\r
- * Decode the code-blocks of a tile\r
- * tile: the tile to encode\r
- * tcp: tile coding parameters\r
- */\r
-void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2004, Yannick Verschueren\r
- * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "t2.h"\r
-#include "tcd.h"\r
-#include "bio.h"\r
-#include "j2k.h"\r
-#include "pi.h"\r
-#include "tgt.h"\r
-#include "int.h"\r
-#include "cio.h"\r
-#include <stdio.h>\r
-#include <setjmp.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-\r
-#define RESTART 0x04\r
-\r
-extern jmp_buf j2k_error;\r
-\r
-void t2_putcommacode(int n)\r
-{\r
- while (--n >= 0) {\r
- bio_write(1, 1);\r
- }\r
- bio_write(0, 1);\r
-}\r
-\r
-int t2_getcommacode()\r
-{\r
- int n;\r
- for (n = 0; bio_read(1); n++) {\r
- }\r
- return n;\r
-}\r
-\r
-/* <summary> */\r
-/* Variable length code for signalling delta Zil (truncation point) */\r
-/* <val> n : delta Zil */\r
-/* <\summary> */\r
-void t2_putnumpasses(int n)\r
-{\r
- if (n == 1) {\r
- bio_write(0, 1);\r
- } else if (n == 2) {\r
- bio_write(2, 2);\r
- } else if (n <= 5) {\r
- bio_write(0xc | (n - 3), 4);\r
- } else if (n <= 36) {\r
- bio_write(0x1e0 | (n - 6), 9);\r
- } else if (n <= 164) {\r
- bio_write(0xff80 | (n - 37), 16);\r
- }\r
-}\r
-\r
-int t2_getnumpasses()\r
-{\r
- int n;\r
- if (!bio_read(1))\r
- return 1;\r
- if (!bio_read(1))\r
- return 2;\r
- if ((n = bio_read(2)) != 3)\r
- return 3 + n;\r
- if ((n = bio_read(5)) != 31)\r
- return 6 + n;\r
- return 37 + bio_read(7);\r
-}\r
-\r
-/*\r
- * Encode a packet of a tile to a destination buffer\r
- *\r
- * Tile : the tile for which to write the packets\r
- * tcp : the tile coding parameters\r
- * compno : Identity of the packet --> component value\r
- * resno : Identity of the packet --> resolution level value\r
- * precno : Identity of the packet --> precinct value\r
- * layno : Identity of the packet --> quality layer value\r
- * dest : the destination buffer\r
- * len : the length of the destination buffer\r
- * info_IM : structure to create an index file\r
- * tileno : number of the tile encoded\r
-*/\r
-int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,\r
- int resno, int precno, int layno, unsigned char *dest,\r
- int len, info_image * info_IM, int tileno)\r
-{\r
- int bandno, cblkno;\r
- unsigned char *sop = 0, *eph = 0;\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- unsigned char *c = dest;\r
-\r
- /* <SOP 0xff91> */\r
- if (tcp->csty & J2K_CP_CSTY_SOP) {\r
- sop = (unsigned char *) malloc(6 * sizeof(unsigned char));\r
- sop[0] = 255;\r
- sop[1] = 145;\r
- sop[2] = 0;\r
- sop[3] = 4;\r
- sop[4] = (info_IM->num % 65536) / 256;\r
- sop[5] = (info_IM->num % 65536) % 256;\r
- memcpy(c, sop, 6);\r
- free(sop);\r
- c += 6;\r
- }\r
- /* </SOP> */\r
-\r
- if (!layno) {\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- tgt_reset(prc->incltree);\r
- tgt_reset(prc->imsbtree);\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- cblk->numpasses = 0;\r
- tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);\r
- }\r
- }\r
- }\r
-\r
- bio_init_enc(c, len);\r
- bio_write(1, 1); /* Empty header bit */\r
-\r
- /* Writing Packet header */\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_layer_t *layer = &cblk->layers[layno];\r
- if (!cblk->numpasses && layer->numpasses) {\r
- tgt_setvalue(prc->incltree, cblkno, layno);\r
- }\r
- }\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_layer_t *layer = &cblk->layers[layno];\r
- int increment = 0;\r
- int nump = 0;\r
- int len = 0, passno;\r
- /* cblk inclusion bits */\r
- if (!cblk->numpasses) {\r
- tgt_encode(prc->incltree, cblkno, layno + 1);\r
- } else {\r
- bio_write(layer->numpasses != 0, 1);\r
- }\r
- /* if cblk not included, go to the next cblk */\r
- if (!layer->numpasses) {\r
- continue;\r
- }\r
- /* if first instance of cblk --> zero bit-planes information */\r
- if (!cblk->numpasses) {\r
- cblk->numlenbits = 3;\r
- tgt_encode(prc->imsbtree, cblkno, 999);\r
- }\r
- /* number of coding passes included */\r
- t2_putnumpasses(layer->numpasses);\r
-\r
- /* computation of the increase of the length indicator and insertion in the header */\r
- for (passno = cblk->numpasses;\r
- passno < cblk->numpasses + layer->numpasses; passno++) {\r
- tcd_pass_t *pass = &cblk->passes[passno];\r
- nump++;\r
- len += pass->len;\r
- if (pass->term\r
- || passno == (cblk->numpasses + layer->numpasses) - 1) {\r
- increment =\r
- int_max(increment,\r
- int_floorlog2(len) + 1 -\r
- (cblk->numlenbits + int_floorlog2(nump)));\r
- len = 0;\r
- nump = 0;\r
- }\r
- }\r
- t2_putcommacode(increment);\r
- /* computation of the new Length indicator */\r
- cblk->numlenbits += increment;\r
- /* insertion of the codeword segment length */\r
-\r
- for (passno = cblk->numpasses;\r
- passno < cblk->numpasses + layer->numpasses; passno++) {\r
- tcd_pass_t *pass = &cblk->passes[passno];\r
- nump++;\r
- len += pass->len;\r
- if (pass->term\r
- || passno == (cblk->numpasses + layer->numpasses) - 1) {\r
- bio_write(len, cblk->numlenbits + int_floorlog2(nump));\r
- len = 0;\r
- nump = 0;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if (bio_flush())\r
- return -999; /* modified to eliminate longjmp !! */\r
-\r
- c += bio_numbytes();\r
-\r
- /* <EPH 0xff92> */\r
- if (tcp->csty & J2K_CP_CSTY_EPH) {\r
- eph = (unsigned char *) malloc(2 * sizeof(unsigned char));\r
- eph[0] = 255;\r
- eph[1] = 146;\r
- memcpy(c, eph, 2);\r
- free(eph);\r
- c += 2;\r
- }\r
- /* </EPH> */\r
-\r
- /* Writing the packet body */\r
-\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_layer_t *layer = &cblk->layers[layno];\r
- if (!layer->numpasses) {\r
- continue;\r
- }\r
- if (c + layer->len > dest + len) {\r
- return -999;\r
- }\r
-\r
- memcpy(c, layer->data, layer->len);\r
- cblk->numpasses += layer->numpasses;\r
- c += layer->len;\r
- /* ADD for index Cfr. Marcela --> delta disto by packet */\r
- if (info_IM->index_write && info_IM->index_on) {\r
- info_tile *info_TL = &info_IM->tile[tileno];\r
- info_packet *info_PK = &info_TL->packet[info_IM->num];\r
- info_PK->disto += layer->disto;\r
- if (info_IM->D_max < info_PK->disto)\r
- info_IM->D_max = info_PK->disto;\r
- } /* </ADD> */\r
- }\r
- }\r
- return c - dest;\r
-}\r
-\r
-void t2_init_seg(tcd_seg_t * seg, int cblksty, int first)\r
-{\r
- seg->numpasses = 0;\r
- seg->len = 0;\r
- if (cblksty & J2K_CCP_CBLKSTY_TERMALL)\r
- seg->maxpasses = 1;\r
- else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {\r
- if (first)\r
- seg->maxpasses = 10;\r
- else\r
- seg->maxpasses = (((seg - 1)->maxpasses == 1)\r
- || ((seg - 1)->maxpasses == 10)) ? 2 : 1;\r
- } else\r
- seg->maxpasses = 109;\r
-}\r
-\r
-/* \r
- * Decode a packet of a tile from a source buffer\r
- *\r
- * src : the source buffer\r
- * len : the length of the source buffer\r
- * tile : the tile for which to write the packets\r
- * cp : the image coding parameters\r
- * tcp : the tile coding parameters\r
- * compno : Identity of the packet --> component value\r
- * resno : Identity of the packet --> resolution level value\r
- * precno : Identity of the packet --> precinct value\r
- * layno : Identity of the packet --> quality layer value\r
- */\r
-int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,\r
- j2k_cp_t * cp, j2k_tcp_t * tcp, int compno, int resno,\r
- int precno, int layno)\r
-{\r
- int bandno, cblkno;\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- unsigned char *c = src;\r
-\r
- unsigned char *hd = NULL;\r
- int present;\r
-\r
- if (layno == 0) {\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
- \r
-\r
- /*Add Antonin : sizebug1*/\r
-\r
- if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;\r
-\r
- /*ddA*/\r
-\r
- \r
- tgt_reset(prc->incltree);\r
- tgt_reset(prc->imsbtree);\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- cblk->numsegs = 0;\r
- }\r
- }\r
- }\r
-\r
- \r
-\r
- /* SOP markers*/\r
-\r
- if (tcp->csty & J2K_CP_CSTY_SOP) {\r
-\r
- if ((*c) != 0xff || (*(c + 1) != 0x91)) {\r
-\r
- fprintf(stderr,"Warning : expected SOP marker\n");\r
-\r
- } else {\r
-\r
- c += 6;\r
-\r
- }\r
-\r
- /*TODO : check the Nsop value*/\r
-\r
- }\r
-\r
- /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker\r
- This part deal with this caracteristic\r
- step 1: Read packet header in the saved structure\r
- step 2: Return to codestream for decoding */\r
-\r
-\r
- if (cp->ppm == 1) { /* PPM */\r
- hd = cp->ppm_data;\r
- bio_init_dec(hd, cp->ppm_len); /*Mod Antonin : ppmbug1*/\r
- } else if (tcp->ppt == 1) { /* PPT */\r
- hd = tcp->ppt_data;\r
- bio_init_dec(hd, tcp->ppt_len); /*Mod Antonin : ppmbug1*/\r
- } else { /* Normal Case */\r
-\r
- hd = c;\r
-\r
- bio_init_dec(hd, src+len-hd);\r
-\r
- }\r
- \r
- present = bio_read(1);\r
-\r
- if (!present) {\r
- bio_inalign();\r
-\r
- hd += bio_numbytes();\r
-\r
-\r
-\r
- /* EPH markers*/\r
-\r
- if (tcp->csty & J2K_CP_CSTY_EPH) {\r
-\r
- if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {\r
-\r
- printf("Error : expected EPH marker\n");\r
-\r
- } else {\r
-\r
- hd += 2;\r
-\r
- }\r
-\r
- }\r
-\r
- if (cp->ppm == 1) { /* PPM case */\r
-\r
- cp->ppm_len+=cp->ppm_data-hd;\r
- cp->ppm_data = hd;\r
- return c - src;\r
- }\r
- if (tcp->ppt == 1) { /* PPT case */\r
-\r
- tcp->ppt_len+=tcp->ppt_data-hd;\r
- tcp->ppt_data = hd;\r
- return c - src;\r
- }\r
-\r
-\r
- return hd - src;\r
- }\r
-\r
-\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
-\r
-\r
- /*Add Antonin : sizebug1*/\r
-\r
- if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;\r
-\r
- /*ddA*/\r
-\r
-\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- int included, increment, n;\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_seg_t *seg;\r
- /* if cblk not yet included before --> inclusion tagtree */\r
- if (!cblk->numsegs) {\r
- included = tgt_decode(prc->incltree, cblkno, layno + 1);\r
- /* else one bit */\r
- } else {\r
- included = bio_read(1);\r
- }\r
- /* if cblk not included */\r
- if (!included) {\r
- cblk->numnewpasses = 0;\r
- continue;\r
- }\r
- /* if cblk not yet included --> zero-bitplane tagtree */\r
- if (!cblk->numsegs) {\r
- int i, numimsbs;\r
- for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {\r
- }\r
- numimsbs = i - 1;\r
- cblk->numbps = band->numbps - numimsbs;\r
- cblk->numlenbits = 3;\r
- }\r
- /* number of coding passes */\r
- cblk->numnewpasses = t2_getnumpasses();\r
- increment = t2_getcommacode();\r
- /* length indicator increment */\r
- cblk->numlenbits += increment;\r
- if (!cblk->numsegs) {\r
- seg = &cblk->segs[0];\r
- t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);\r
- } else {\r
- seg = &cblk->segs[cblk->numsegs - 1];\r
- if (seg->numpasses == seg->maxpasses) {\r
- t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);\r
- }\r
- }\r
- n = cblk->numnewpasses;\r
-\r
- do {\r
- seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);\r
- seg->newlen =\r
- bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));\r
- n -= seg->numnewpasses;\r
- if (n > 0) {\r
- t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);\r
- }\r
- } while (n > 0);\r
- }\r
- }\r
- if (bio_inalign())\r
- return -999;\r
-\r
- hd += bio_numbytes();\r
-\r
-\r
- /* EPH markers*/\r
- if (tcp->csty & J2K_CP_CSTY_EPH) {\r
- if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {\r
- fprintf(stderr,"Error : expected EPH marker\n");\r
- } else {\r
- hd += 2;\r
-\r
- }\r
- }\r
-\r
-\r
-\r
- if (cp->ppm==1) {\r
-\r
- cp->ppm_len+=cp->ppm_data-hd;\r
-\r
- cp->ppm_data = hd;\r
-\r
- } else if (tcp->ppt == 1) {\r
-\r
- tcp->ppt_len+=tcp->ppt_data-hd;\r
-\r
- tcp->ppt_data = hd;\r
-\r
- } else {\r
-\r
- c=hd;\r
-\r
- }\r
-\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
-\r
-\r
- /*Add Antonin : sizebug1*/\r
-\r
- if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;\r
-\r
- /*ddA*/\r
-\r
-\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_seg_t *seg;\r
- if (!cblk->numnewpasses)\r
- continue;\r
- if (!cblk->numsegs) {\r
- seg = &cblk->segs[0];\r
-\r
- cblk->numsegs++;\r
- cblk->len = 0;\r
- } else {\r
- seg = &cblk->segs[cblk->numsegs - 1];\r
- if (seg->numpasses == seg->maxpasses) {\r
- seg++;\r
- cblk->numsegs++;\r
- }\r
- }\r
- do {\r
- if (c + seg->newlen > src + len) {\r
- return -999;\r
- }\r
-\r
- memcpy(cblk->data + cblk->len, c, seg->newlen);\r
- if (seg->numpasses == 0) {\r
- seg->data = cblk->data + cblk->len;\r
- }\r
- c += seg->newlen;\r
- cblk->len += seg->newlen;\r
- seg->len += seg->newlen;\r
- seg->numpasses += seg->numnewpasses;\r
- cblk->numnewpasses -= seg->numnewpasses;\r
- if (cblk->numnewpasses > 0) {\r
- seg++;\r
- cblk->numsegs++;\r
- }\r
- } while (cblk->numnewpasses > 0);\r
- }\r
- }\r
-\r
- return c - src;\r
-}\r
-\r
-\r
-\r
-/*\r
- * Encode the packets of a tile to a destination buffer\r
- *\r
- * img : the source image\r
- * cp : the image coding parameters\r
- * tileno : number of the tile encoded\r
- * tile : the tile for which to write the packets\r
- * maxlayers : maximum number of layers\r
- * dest : the destination buffer\r
- * len : the length of the destination buffer\r
- * info_IM : structure to create an index file\r
- */\r
-int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,\r
- tcd_tile_t * tile, int maxlayers,\r
- unsigned char *dest, int len, info_image * info_IM)\r
-{\r
- unsigned char *c = dest;\r
- int e = 0;\r
- pi_iterator_t *pi;\r
- int pino, compno;\r
-\r
- pi = pi_create(img, cp, tileno);\r
-\r
- for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {\r
- while (pi_next(&pi[pino])) {\r
- if (pi[pino].layno < maxlayers) {\r
- e = t2_encode_packet(tile, &cp->tcps[tileno],\r
- pi[pino].compno, pi[pino].resno,\r
- pi[pino].precno, pi[pino].layno, c,\r
- dest + len - c, info_IM, tileno);\r
- if (e == -999) {\r
- break;\r
- } else\r
- c += e;\r
- /* INDEX >> */\r
- if (info_IM->index_write && info_IM->index_on) {\r
- info_tile *info_TL = &info_IM->tile[tileno];\r
- info_packet *info_PK = &info_TL->packet[info_IM->num];\r
- if (!info_IM->num) {\r
- info_PK->start_pos = info_TL->end_header + 1;\r
- } else {\r
- info_PK->start_pos =\r
- info_TL->packet[info_IM->num - 1].end_pos + 1;\r
- }\r
- info_PK->end_pos = info_PK->start_pos + e - 1;\r
-\r
- }\r
- /* << INDEX */\r
- if ((info_IM->index_write\r
- && cp->tcps[tileno].csty & J2K_CP_CSTY_SOP)\r
- || (info_IM->index_write && info_IM->index_on)) {\r
- info_IM->num++;\r
- }\r
- }\r
-\r
- }\r
-\r
- /* FREE space memory taken by pi */\r
- for (compno = 0; compno < pi[pino].numcomps; compno++) {\r
- free(pi[pino].comps[compno].resolutions);\r
- }\r
- free(pi[pino].comps);\r
- }\r
- free(pi[0].include);\r
- free(pi);\r
- if (e == -999)\r
- return e;\r
- else\r
- return c - dest;\r
-}\r
-\r
-\r
-\r
-/*\r
- * Decode the packets of a tile from a source buffer\r
- *\r
- * src: the source buffer\r
- * len: length of the source buffer\r
- * img: destination image\r
- * cp: image coding parameters\r
- * tileno: number that identifies the tile for which to decode the packets\r
- * tile: tile for which to decode the packets\r
- */\r
-int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,\r
- j2k_cp_t * cp, int tileno, tcd_tile_t * tile)\r
-{\r
- unsigned char *c = src;\r
- pi_iterator_t *pi;\r
- int pino, compno, e = 0;\r
- int n = 0;\r
-\r
- pi = pi_create(img, cp, tileno);\r
-\r
- for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {\r
- while (pi_next(&pi[pino])) {\r
- \r
- if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {\r
- e = t2_decode_packet(c, src + len - c, tile, cp,\r
- &cp->tcps[tileno], pi[pino].compno,\r
- pi[pino].resno, pi[pino].precno,\r
- pi[pino].layno);\r
- } else {\r
- e = 0;\r
- }\r
- \r
- /* progression in resolution */\r
- img->comps[pi[pino].compno].resno_decoded =\r
- e > 0 ? int_max(pi[pino].resno,\r
- img->comps[pi[pino].compno].\r
- resno_decoded) : img->comps[pi[pino].\r
- compno].resno_decoded;\r
- n++;\r
- \r
- if (e == -999) { /* ADD */\r
- break;\r
- } else {\r
- c += e;\r
- }\r
- }\r
-\r
- /* FREE space memory taken by pi */\r
- for (compno = 0; compno < pi[pino].numcomps; compno++) {\r
- free(pi[pino].comps[compno].resolutions);\r
- }\r
- free(pi[pino].comps);\r
- }\r
- free(pi[0].include);\r
- free(pi);\r
-\r
- if (e == -999)\r
- return e;\r
- else\r
- return c - src;\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __T2_H\r
-#define __T2_H\r
-\r
-#include "tcd.h"\r
-#include "j2k.h"\r
-\r
-/*\r
- * Encode the packets of a tile to a destination buffer\r
- *\r
- * img : the source image\r
- * cp : the image coding parameters\r
- * tileno : number of the tile encoded\r
- * tile : the tile for which to write the packets\r
- * maxlayers : maximum number of layers\r
- * dest : the destination buffer\r
- * len : the length of the destination buffer\r
- * info_IM : structure to create an index file\r
- */\r
-int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,\r
- tcd_tile_t * tile, int maxlayers,\r
- unsigned char *dest, int len, info_image * info_IM);\r
-\r
-/*\r
- * Decode the packets of a tile from a source buffer\r
- *\r
- * src: the source buffer\r
- * len: length of the source buffer\r
- * img: destination image\r
- * cp: image coding parameters\r
- * tileno: number that identifies the tile for which to decode the packets\r
- * tile: tile for which to decode the packets\r
- */\r
-int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,\r
- j2k_cp_t * cp, int tileno, tcd_tile_t * tile);\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * Copyright (c) 2002-2004, Yannick Verschueren\r
- * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "tcd.h"\r
-#include "int.h"\r
-#include "t1.h"\r
-#include "t2.h"\r
-#include "dwt.h"\r
-#include "mct.h"\r
-#include <setjmp.h>\r
-#include <float.h>\r
-#include <stdio.h>\r
-#include <time.h>\r
-#include <math.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-static tcd_image_t tcd_image;\r
-\r
-static j2k_image_t *tcd_img;\r
-static j2k_cp_t *tcd_cp;\r
-\r
-static tcd_tile_t *tcd_tile;\r
-static j2k_tcp_t *tcd_tcp;\r
-static int tcd_tileno;\r
-\r
-static tcd_tile_t *tile;\r
-static tcd_tilecomp_t *tilec;\r
-static tcd_resolution_t *res;\r
-static tcd_band_t *band;\r
-static tcd_precinct_t *prc;\r
-static tcd_cblk_t *cblk;\r
-\r
-extern jmp_buf j2k_error;\r
-\r
-void tcd_dump(tcd_image_t * img, int curtileno)\r
-{\r
- int tileno, compno, resno, bandno, precno, cblkno;\r
- (void)curtileno;\r
- fprintf(stdout, "image {\n");\r
- fprintf(stdout, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw,\r
- img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1);\r
- for (tileno = 0; tileno < img->th * img->tw; tileno++) {\r
- tcd_tile_t *tile = &tcd_image.tiles[tileno];\r
- fprintf(stdout, " tile {\n");\r
- fprintf(stdout, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",\r
- tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- fprintf(stdout, " tilec {\n");\r
- fprintf(stdout,\r
- " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",\r
- tilec->x0, tilec->y0, tilec->x1, tilec->y1,\r
- tilec->numresolutions);\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- fprintf(stdout, "\n res {\n");\r
- fprintf(stdout,\r
- " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",\r
- res->x0, res->y0, res->x1, res->y1, res->pw, res->ph,\r
- res->numbands);\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- fprintf(stdout, " band {\n");\r
- fprintf(stdout,\r
- " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",\r
- band->x0, band->y0, band->x1, band->y1,\r
- band->stepsize, band->numbps);\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- tcd_precinct_t *prec = &band->precincts[precno];\r
- fprintf(stdout, " prec {\n");\r
- fprintf(stdout,\r
- " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",\r
- prec->x0, prec->y0, prec->x1, prec->y1,\r
- prec->cw, prec->ch);\r
- for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prec->cblks[cblkno];\r
- fprintf(stdout, " cblk {\n");\r
- fprintf(stdout,\r
- " x0=%d, y0=%d, x1=%d, y1=%d\n",\r
- cblk->x0, cblk->y0, cblk->x1, cblk->y1);\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, " }\n");\r
- }\r
- fprintf(stdout, "}\n");\r
-}\r
-\r
-void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)\r
-{\r
- int tileno, compno, resno, bandno, precno, cblkno;\r
- tcd_img = img;\r
- tcd_cp = cp;\r
- tcd_image.tw = cp->tw;\r
- tcd_image.th = cp->th;\r
- tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));\r
-\r
- for (tileno = 0; tileno < 1; tileno++) {\r
- j2k_tcp_t *tcp = &cp->tcps[curtileno];\r
- int j;\r
- /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */\r
- int p = curtileno % cp->tw; /* si numerotation matricielle .. */\r
- int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */\r
- /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */\r
- tile = tcd_image.tiles;\r
- /* 4 borders of the tile rescale on the image if necessary */\r
- tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);\r
- tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);\r
- tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);\r
- tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);\r
- tile->numcomps = img->numcomps;\r
- /* tile->PPT=img->PPT; */\r
- /* Modification of the RATE >> */\r
- for (j = 0; j < tcp->numlayers; j++) {\r
- tcp->rates[j] = tcp->rates[j] ? int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; /*Mod antonin losslessbug*/\r
- if (tcp->rates[j]) {\r
- if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {\r
- tcp->rates[j] = tcp->rates[j - 1] + 20;\r
- } else {\r
- if (!j && tcp->rates[j] < 30)\r
- tcp->rates[j] = 30;\r
- }\r
- }\r
- }\r
- /* << Modification of the RATE */\r
-\r
- tile->comps =\r
- (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- j2k_tccp_t *tccp = &tcp->tccps[compno];\r
- /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */\r
- tilec = &tile->comps[compno];\r
- /* border of each tile component (global) */\r
- tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);\r
-\r
- tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);\r
- tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);\r
- tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);\r
-\r
- tilec->data =\r
- (int *) malloc((tilec->x1 - tilec->x0) *\r
- (tilec->y1 - tilec->y0) * sizeof(int));\r
- tilec->numresolutions = tccp->numresolutions;\r
-\r
- tilec->resolutions =\r
- (tcd_resolution_t *) malloc(tilec->numresolutions *\r
- sizeof(tcd_resolution_t));\r
-\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- int pdx, pdy;\r
- int levelno = tilec->numresolutions - 1 - resno;\r
- int tlprcxstart, tlprcystart, brprcxend, brprcyend;\r
- int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;\r
- int cbgwidthexpn, cbgheightexpn;\r
- int cblkwidthexpn, cblkheightexpn;\r
- /* tcd_resolution_t *res=&tilec->resolutions[resno]; */\r
-\r
- res = &tilec->resolutions[resno];\r
-\r
- /* border for each resolution level (global) */\r
- res->x0 = int_ceildivpow2(tilec->x0, levelno);\r
- res->y0 = int_ceildivpow2(tilec->y0, levelno);\r
- res->x1 = int_ceildivpow2(tilec->x1, levelno);\r
- res->y1 = int_ceildivpow2(tilec->y1, levelno);\r
-\r
- res->numbands = resno == 0 ? 1 : 3;\r
- /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */\r
- if (tccp->csty & J2K_CCP_CSTY_PRT) {\r
- pdx = tccp->prcw[resno];\r
- pdy = tccp->prch[resno];\r
- } else {\r
- pdx = 15;\r
- pdy = 15;\r
- }\r
- /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */\r
- tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;\r
- tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;\r
- brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;\r
- brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;\r
-\r
- res->pw = (brprcxend - tlprcxstart) >> pdx;\r
- res->ph = (brprcyend - tlprcystart) >> pdy;\r
-\r
- if (resno == 0) {\r
- tlcbgxstart = tlprcxstart;\r
- tlcbgystart = tlprcystart;\r
- brcbgxend = brprcxend;\r
- brcbgyend = brprcyend;\r
- cbgwidthexpn = pdx;\r
- cbgheightexpn = pdy;\r
- } else {\r
- tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);\r
- tlcbgystart = int_ceildivpow2(tlprcystart, 1);\r
- brcbgxend = int_ceildivpow2(brprcxend, 1);\r
- brcbgyend = int_ceildivpow2(brprcyend, 1);\r
- cbgwidthexpn = pdx - 1;\r
- cbgheightexpn = pdy - 1;\r
- }\r
-\r
- cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);\r
- cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);\r
-\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- int x0b, y0b, i;\r
- int gain, numbps;\r
- j2k_stepsize_t *ss;\r
- band = &res->bands[bandno];\r
- band->bandno = resno == 0 ? 0 : bandno + 1;\r
- x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;\r
- y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;\r
-\r
- if (band->bandno == 0) {\r
- /* band border (global) */\r
- band->x0 = int_ceildivpow2(tilec->x0, levelno);\r
- band->y0 = int_ceildivpow2(tilec->y0, levelno);\r
- band->x1 = int_ceildivpow2(tilec->x1, levelno);\r
- band->y1 = int_ceildivpow2(tilec->y1, levelno);\r
- } else {\r
- /* band border (global) */\r
- band->x0 =\r
- int_ceildivpow2(tilec->x0 -\r
- (1 << levelno) * x0b, levelno + 1);\r
- band->y0 =\r
- int_ceildivpow2(tilec->y0 -\r
- (1 << levelno) * y0b, levelno + 1);\r
- band->x1 =\r
- int_ceildivpow2(tilec->x1 -\r
- (1 << levelno) * x0b, levelno + 1);\r
- band->y1 =\r
- int_ceildivpow2(tilec->y1 -\r
- (1 << levelno) * y0b, levelno + 1);\r
-\r
- }\r
-\r
- ss = &tccp->stepsizes[resno ==\r
- 0 ? 0 : 3 * (resno - 1) + bandno + 1];\r
- gain =\r
- tccp->qmfbid ==\r
- 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);\r
- numbps = img->comps[compno].prec + gain;\r
- band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));\r
- band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */\r
-\r
- band->precincts =\r
- (tcd_precinct_t *) malloc(3 * res->pw * res->ph *\r
- sizeof(tcd_precinct_t));\r
-\r
- for (i = 0; i < res->pw * res->ph * 3; i++) {\r
- band->precincts[i].imsbtree = NULL;\r
- band->precincts[i].incltree = NULL;\r
- }\r
-\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;\r
- int cbgxstart =\r
- tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);\r
- int cbgystart =\r
- tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);\r
- int cbgxend = cbgxstart + (1 << cbgwidthexpn);\r
- int cbgyend = cbgystart + (1 << cbgheightexpn);\r
- /* tcd_precinct_t *prc=&band->precincts[precno]; */\r
- prc = &band->precincts[precno];\r
- /* precinct size (global) */\r
- prc->x0 = int_max(cbgxstart, band->x0);\r
- prc->y0 = int_max(cbgystart, band->y0);\r
- prc->x1 = int_min(cbgxend, band->x1);\r
- prc->y1 = int_min(cbgyend, band->y1);\r
-\r
- tlcblkxstart =\r
- int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;\r
- tlcblkystart =\r
- int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;\r
- brcblkxend =\r
- int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;\r
- brcblkyend =\r
- int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;\r
- prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\r
- prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;\r
-\r
- prc->cblks =\r
- (tcd_cblk_t *) malloc((prc->cw * prc->ch) *\r
- sizeof(tcd_cblk_t));\r
- prc->incltree = tgt_create(prc->cw, prc->ch);\r
- prc->imsbtree = tgt_create(prc->cw, prc->ch);\r
-\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- int cblkxstart =\r
- tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);\r
- int cblkystart =\r
- tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);\r
- int cblkxend = cblkxstart + (1 << cblkwidthexpn);\r
- int cblkyend = cblkystart + (1 << cblkheightexpn);\r
-\r
- cblk = &prc->cblks[cblkno];\r
- /* code-block size (global) */\r
- cblk->x0 = int_max(cblkxstart, prc->x0);\r
- cblk->y0 = int_max(cblkystart, prc->y0);\r
- cblk->x1 = int_min(cblkxend, prc->x1);\r
- cblk->y1 = int_min(cblkyend, prc->y1);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- /* tcd_dump(&tcd_image,curtileno); */\r
-}\r
-\r
-void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)\r
-{\r
- int tileno, compno, resno, bandno, precno;\r
- (void)curtileno;\r
- tcd_img = img;\r
- tcd_cp = cp;\r
- tcd_image.tw = cp->tw;\r
- tcd_image.th = cp->th;\r
- for (tileno = 0; tileno < 1; tileno++) {\r
- /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */\r
- tile = tcd_image.tiles;\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tilec = &tile->comps[compno];\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- res = &tilec->resolutions[resno];\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- band = &res->bands[bandno];\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- prc = &band->precincts[precno];\r
-\r
- if (prc->incltree != NULL)\r
- tgt_destroy(prc->incltree);\r
- if (prc->imsbtree != NULL)\r
- tgt_destroy(prc->imsbtree);\r
- free(prc->cblks);\r
- } /* for (precno */\r
- free(band->precincts);\r
- } /* for (bandno */\r
- } /* for (resno */\r
- free(tilec->resolutions);\r
- } /* for (compno */\r
- free(tile->comps);\r
- } /* for (tileno */\r
- free(tcd_image.tiles);\r
-}\r
-\r
-void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)\r
-{\r
- int tileno, compno, resno, bandno, precno, cblkno;\r
-\r
- for (tileno = 0; tileno < 1; tileno++) {\r
- j2k_tcp_t *tcp = &cp->tcps[curtileno];\r
- int j;\r
- /* int previous_x0, previous_x1, previous_y0, previous_y1;*/\r
- /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */\r
- int p = curtileno % cp->tw;\r
- int q = curtileno / cp->tw;\r
- tile = tcd_image.tiles;\r
-\r
- /* 4 borders of the tile rescale on the image if necessary */\r
- tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);\r
- tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);\r
- tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);\r
- tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);\r
-\r
- tile->numcomps = img->numcomps;\r
- /* tile->PPT=img->PPT; */\r
- /* Modification of the RATE >> */\r
- for (j = 0; j < tcp->numlayers; j++) {\r
- tcp->rates[j] = tcp->rates[j] ? int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; /*Mod antonin losslessbug*/\r
- if (tcp->rates[j]) {\r
- if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {\r
- tcp->rates[j] = tcp->rates[j - 1] + 20;\r
- } else {\r
- if (!j && tcp->rates[j] < 30)\r
- tcp->rates[j] = 30;\r
- }\r
- }\r
- }\r
- /* << Modification of the RATE */\r
- /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- j2k_tccp_t *tccp = &tcp->tccps[compno];\r
- /* int realloc_op; */\r
-\r
- tilec = &tile->comps[compno];\r
- /* border of each tile component (global) */\r
- tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);\r
- tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);\r
- tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);\r
- tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);\r
-\r
- tilec->data =\r
- (int *) malloc((tilec->x1 - tilec->x0) *\r
- (tilec->y1 - tilec->y0) * sizeof(int));\r
- tilec->numresolutions = tccp->numresolutions;\r
- /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- int pdx, pdy;\r
- int levelno = tilec->numresolutions - 1 - resno;\r
- int tlprcxstart, tlprcystart, brprcxend, brprcyend;\r
- int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;\r
- int cbgwidthexpn, cbgheightexpn;\r
- int cblkwidthexpn, cblkheightexpn;\r
-\r
- res = &tilec->resolutions[resno];\r
- /* border for each resolution level (global) */\r
- res->x0 = int_ceildivpow2(tilec->x0, levelno);\r
- res->y0 = int_ceildivpow2(tilec->y0, levelno);\r
- res->x1 = int_ceildivpow2(tilec->x1, levelno);\r
- res->y1 = int_ceildivpow2(tilec->y1, levelno);\r
-\r
- res->numbands = resno == 0 ? 1 : 3;\r
- /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */\r
- if (tccp->csty & J2K_CCP_CSTY_PRT) {\r
- pdx = tccp->prcw[resno];\r
- pdy = tccp->prch[resno];\r
- } else {\r
- pdx = 15;\r
- pdy = 15;\r
- }\r
- /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */\r
- tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;\r
- tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;\r
- brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;\r
- brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;\r
-\r
- res->pw = (brprcxend - tlprcxstart) >> pdx;\r
- res->ph = (brprcyend - tlprcystart) >> pdy;\r
-\r
- if (resno == 0) {\r
- tlcbgxstart = tlprcxstart;\r
- tlcbgystart = tlprcystart;\r
- brcbgxend = brprcxend;\r
- brcbgyend = brprcyend;\r
- cbgwidthexpn = pdx;\r
- cbgheightexpn = pdy;\r
- } else {\r
- tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);\r
- tlcbgystart = int_ceildivpow2(tlprcystart, 1);\r
- brcbgxend = int_ceildivpow2(brprcxend, 1);\r
- brcbgyend = int_ceildivpow2(brprcyend, 1);\r
- cbgwidthexpn = pdx - 1;\r
- cbgheightexpn = pdy - 1;\r
- }\r
-\r
- cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);\r
- cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);\r
-\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- int x0b, y0b;\r
- int gain, numbps;\r
- j2k_stepsize_t *ss;\r
- band = &res->bands[bandno];\r
- band->bandno = resno == 0 ? 0 : bandno + 1;\r
- x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;\r
- y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;\r
-\r
- if (band->bandno == 0) {\r
- /* band border */\r
- band->x0 = int_ceildivpow2(tilec->x0, levelno);\r
- band->y0 = int_ceildivpow2(tilec->y0, levelno);\r
- band->x1 = int_ceildivpow2(tilec->x1, levelno);\r
- band->y1 = int_ceildivpow2(tilec->y1, levelno);\r
- } else {\r
- band->x0 =\r
- int_ceildivpow2(tilec->x0 -\r
- (1 << levelno) * x0b, levelno + 1);\r
- band->y0 =\r
- int_ceildivpow2(tilec->y0 -\r
- (1 << levelno) * y0b, levelno + 1);\r
- band->x1 =\r
- int_ceildivpow2(tilec->x1 -\r
- (1 << levelno) * x0b, levelno + 1);\r
- band->y1 =\r
- int_ceildivpow2(tilec->y1 -\r
- (1 << levelno) * y0b, levelno + 1);\r
- }\r
-\r
- ss = &tccp->stepsizes[resno ==\r
- 0 ? 0 : 3 * (resno - 1) + bandno + 1];\r
- gain =\r
- tccp->qmfbid ==\r
- 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);\r
- numbps = img->comps[compno].prec + gain;\r
- band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));\r
- band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */\r
-\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;\r
- int cbgxstart =\r
- tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);\r
- int cbgystart =\r
- tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);\r
- int cbgxend = cbgxstart + (1 << cbgwidthexpn);\r
- int cbgyend = cbgystart + (1 << cbgheightexpn);\r
-\r
- prc = &band->precincts[precno];\r
- /* precinct size (global) */\r
- prc->x0 = int_max(cbgxstart, band->x0);\r
- prc->y0 = int_max(cbgystart, band->y0);\r
- prc->x1 = int_min(cbgxend, band->x1);\r
- prc->y1 = int_min(cbgyend, band->y1);\r
-\r
- tlcblkxstart =\r
- int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;\r
- tlcblkystart =\r
- int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;\r
- brcblkxend =\r
- int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;\r
- brcblkyend =\r
- int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;\r
- prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\r
- prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;\r
-\r
- free(prc->cblks);\r
- prc->cblks =\r
- (tcd_cblk_t *) malloc(prc->cw * prc->ch *\r
- sizeof(tcd_cblk_t));\r
-\r
- if (prc->incltree != NULL)\r
- tgt_destroy(prc->incltree);\r
- if (prc->imsbtree != NULL)\r
- tgt_destroy(prc->imsbtree);\r
-\r
- prc->incltree = tgt_create(prc->cw, prc->ch);\r
- prc->imsbtree = tgt_create(prc->cw, prc->ch);\r
-\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- int cblkxstart =\r
- tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);\r
- int cblkystart =\r
- tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);\r
- int cblkxend = cblkxstart + (1 << cblkwidthexpn);\r
- int cblkyend = cblkystart + (1 << cblkheightexpn);\r
- cblk = &prc->cblks[cblkno];\r
-\r
- /* code-block size (global) */\r
- cblk->x0 = int_max(cblkxstart, prc->x0);\r
- cblk->y0 = int_max(cblkystart, prc->y0);\r
- cblk->x1 = int_min(cblkxend, prc->x1);\r
- cblk->y1 = int_min(cblkyend, prc->y1);\r
-\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- /* tcd_dump(&tcd_image,0); */\r
-}\r
-\r
-void tcd_init(j2k_image_t * img, j2k_cp_t * cp)\r
-{\r
- int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;\r
- unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;\r
- tcd_img = img;\r
- tcd_cp = cp;\r
- tcd_image.tw = cp->tw;\r
- tcd_image.th = cp->th;\r
- tcd_image.tiles =\r
- (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));\r
-\r
- /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {\r
- j2k_tcp_t *tcp = &cp->tcps[tileno];\r
- tcd_tile_t *tile = &tcd_image.tiles[tileno]; */\r
-\r
- for (i = 0; i < cp->tileno_size; i++) {\r
- j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];\r
- tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];\r
- tileno = cp->tileno[i];\r
-\r
-\r
- /* int previous_x0, previous_x1, previous_y0, previous_y1;*/\r
- /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */\r
- p = tileno % cp->tw; /* si numerotation matricielle .. */\r
- q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */\r
-\r
- /* 4 borders of the tile rescale on the image if necessary */\r
- tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);\r
- tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);\r
- tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);\r
- tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);\r
-\r
- tile->numcomps = img->numcomps;\r
- tile->comps =\r
- (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- j2k_tccp_t *tccp = &tcp->tccps[compno];\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- /* border of each tile component (global) */\r
- tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);\r
- tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);\r
- tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);\r
- tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);\r
-\r
- tilec->data =\r
- (int *) malloc((tilec->x1 - tilec->x0) *\r
- (tilec->y1 - tilec->y0) * sizeof(int));\r
- tilec->numresolutions = tccp->numresolutions;\r
- tilec->resolutions =\r
- (tcd_resolution_t *) malloc(tilec->numresolutions *\r
- sizeof(tcd_resolution_t));\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- int pdx, pdy;\r
- int levelno = tilec->numresolutions - 1 - resno;\r
- int tlprcxstart, tlprcystart, brprcxend, brprcyend;\r
- int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;\r
- int cbgwidthexpn, cbgheightexpn;\r
- int cblkwidthexpn, cblkheightexpn;\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
-\r
- /* border for each resolution level (global) */\r
- res->x0 = int_ceildivpow2(tilec->x0, levelno);\r
- res->y0 = int_ceildivpow2(tilec->y0, levelno);\r
- res->x1 = int_ceildivpow2(tilec->x1, levelno);\r
- res->y1 = int_ceildivpow2(tilec->y1, levelno);\r
-\r
- res->numbands = resno == 0 ? 1 : 3;\r
- /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */\r
- if (tccp->csty & J2K_CCP_CSTY_PRT) {\r
- pdx = tccp->prcw[resno];\r
- pdy = tccp->prch[resno];\r
- } else {\r
- pdx = 15;\r
- pdy = 15;\r
- }\r
- /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */\r
- tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;\r
- tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;\r
- brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;\r
- brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;\r
- res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); /* Mod Antonin : sizebug1*/\r
- res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); /* Mod Antonin : sizebug1*/\r
-\r
- if (resno == 0) {\r
- tlcbgxstart = tlprcxstart;\r
- tlcbgystart = tlprcystart;\r
- brcbgxend = brprcxend;\r
- brcbgyend = brprcyend;\r
- cbgwidthexpn = pdx;\r
- cbgheightexpn = pdy;\r
- } else {\r
- tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);\r
- tlcbgystart = int_ceildivpow2(tlprcystart, 1);\r
- brcbgxend = int_ceildivpow2(brprcxend, 1);\r
- brcbgyend = int_ceildivpow2(brprcyend, 1);\r
- cbgwidthexpn = pdx - 1;\r
- cbgheightexpn = pdy - 1;\r
- }\r
-\r
- cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);\r
- cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);\r
-\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- int x0b, y0b;\r
- int gain, numbps;\r
- j2k_stepsize_t *ss;\r
- tcd_band_t *band = &res->bands[bandno];\r
- band->bandno = resno == 0 ? 0 : bandno + 1;\r
- x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;\r
- y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;\r
-\r
- if (band->bandno == 0) {\r
- /* band border (global) */\r
- band->x0 = int_ceildivpow2(tilec->x0, levelno);\r
- band->y0 = int_ceildivpow2(tilec->y0, levelno);\r
- band->x1 = int_ceildivpow2(tilec->x1, levelno);\r
- band->y1 = int_ceildivpow2(tilec->y1, levelno);\r
- } else {\r
- /* band border (global) */\r
- band->x0 =\r
- int_ceildivpow2(tilec->x0 -\r
- (1 << levelno) * x0b, levelno + 1);\r
- band->y0 =\r
- int_ceildivpow2(tilec->y0 -\r
- (1 << levelno) * y0b, levelno + 1);\r
- band->x1 =\r
- int_ceildivpow2(tilec->x1 -\r
- (1 << levelno) * x0b, levelno + 1);\r
- band->y1 =\r
- int_ceildivpow2(tilec->y1 -\r
- (1 << levelno) * y0b, levelno + 1);\r
- }\r
-\r
- ss = &tccp->stepsizes[resno ==\r
- 0 ? 0 : 3 * (resno - 1) + bandno + 1];\r
- gain =\r
- tccp->qmfbid ==\r
- 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);\r
- numbps = img->comps[compno].prec + gain;\r
- band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));\r
- band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */\r
-\r
- band->precincts =\r
- (tcd_precinct_t *) malloc(res->pw * res->ph *\r
- sizeof(tcd_precinct_t));\r
-\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;\r
- int cbgxstart =\r
- tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);\r
- int cbgystart =\r
- tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);\r
- int cbgxend = cbgxstart + (1 << cbgwidthexpn);\r
- int cbgyend = cbgystart + (1 << cbgheightexpn);\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- /* precinct size (global) */\r
- prc->x0 = int_max(cbgxstart, band->x0);\r
- prc->y0 = int_max(cbgystart, band->y0);\r
- prc->x1 = int_min(cbgxend, band->x1);\r
- prc->y1 = int_min(cbgyend, band->y1);\r
-\r
- tlcblkxstart =\r
- int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;\r
- tlcblkystart =\r
- int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;\r
- brcblkxend =\r
- int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;\r
- brcblkyend =\r
- int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;\r
- prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\r
- prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;\r
-\r
- prc->cblks =\r
- (tcd_cblk_t *) malloc(prc->cw * prc->ch *\r
- sizeof(tcd_cblk_t));\r
-\r
- prc->incltree = tgt_create(prc->cw, prc->ch);\r
- prc->imsbtree = tgt_create(prc->cw, prc->ch);\r
-\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- int cblkxstart =\r
- tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);\r
- int cblkystart =\r
- tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);\r
- int cblkxend = cblkxstart + (1 << cblkwidthexpn);\r
- int cblkyend = cblkystart + (1 << cblkheightexpn);\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- /* code-block size (global) */\r
- cblk->x0 = int_max(cblkxstart, prc->x0);\r
- cblk->y0 = int_max(cblkystart, prc->y0);\r
- cblk->x1 = int_min(cblkxend, prc->x1);\r
- cblk->y1 = int_min(cblkyend, prc->y1);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- /*tcd_dump(&tcd_image,0);*/\r
-\r
-\r
- /* Allocate place to store the data decoded = final image */\r
- /* Place limited by the tile really present in the codestream */\r
-\r
-\r
- for (i = 0; i < img->numcomps; i++) {\r
- for (j = 0; j < cp->tileno_size; j++) {\r
- tileno = cp->tileno[j];\r
- x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,\r
- (unsigned int) \r
- tcd_image.\r
- tiles\r
- [tileno].\r
- comps\r
- [i].x0);\r
- y0 =\r
- j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,\r
- (unsigned int) \r
- tcd_image.\r
- tiles\r
- [tileno].\r
- comps[i].\r
- y0);\r
- x1 =\r
- j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,\r
- (unsigned int) \r
- tcd_image.\r
- tiles\r
- [tileno].\r
- comps[i].\r
- x1);\r
- y1 =\r
- j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,\r
- (unsigned int) \r
- tcd_image.\r
- tiles\r
- [tileno].\r
- comps[i].\r
- y1);\r
- }\r
-\r
- w = x1 - x0;\r
-\r
- h = y1 - y0;\r
- img->comps[i].data = (int *) calloc(w * h, sizeof(int));\r
- img->comps[i].w = w;\r
- img->comps[i].h = h;\r
- img->comps[i].x0 = x0;\r
- img->comps[i].y0 = y0;\r
- }\r
-}\r
-\r
-void tcd_makelayer_fixed(int layno, int final)\r
-{\r
- int compno, resno, bandno, precno, cblkno;\r
- int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];*/\r
- int matrice[10][10][3];\r
- int i, j, k;\r
-\r
- /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */\r
-\r
- for (compno = 0; compno < tcd_tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];\r
- for (i = 0; i < tcd_tcp->numlayers; i++) {\r
- for (j = 0; j < tilec->numresolutions; j++) {\r
- for (k = 0; k < 3; k++) {\r
- matrice[i][j][k] =\r
- (int) (tcd_cp->\r
- matrice[i * tilec->numresolutions * 3 +\r
- j * 3 +\r
- k] *\r
- (float) (tcd_img->comps[compno].prec / 16.0));\r
- }\r
- }\r
- }\r
-\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_layer_t *layer = &cblk->layers[layno];\r
- int n;\r
- int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */\r
- /* Correction of the matrix of coefficient to include the IMSB information */\r
-\r
- if (layno == 0) {\r
- value = matrice[layno][resno][bandno];\r
- if (imsb >= value)\r
- value = 0;\r
- else\r
- value -= imsb;\r
- } else {\r
- value =\r
- matrice[layno][resno][bandno] -\r
- matrice[layno - 1][resno][bandno];\r
- if (imsb >= matrice[layno - 1][resno][bandno]) {\r
- value -= (imsb - matrice[layno - 1][resno][bandno]);\r
- if (value < 0)\r
- value = 0;\r
- }\r
- }\r
-\r
- if (layno == 0)\r
- cblk->numpassesinlayers = 0;\r
-\r
- n = cblk->numpassesinlayers;\r
- if (cblk->numpassesinlayers == 0) {\r
- if (value != 0)\r
- n = 3 * value - 2 + cblk->numpassesinlayers;\r
- else\r
- n = cblk->numpassesinlayers;\r
- } else\r
- n = 3 * value + cblk->numpassesinlayers;\r
-\r
- layer->numpasses = n - cblk->numpassesinlayers;\r
-\r
- if (!layer->numpasses)\r
- continue;\r
-\r
- if (cblk->numpassesinlayers == 0) {\r
- layer->len = cblk->passes[n - 1].rate;\r
- layer->data = cblk->data;\r
- } else {\r
- layer->len =\r
- cblk->passes[n - 1].rate -\r
- cblk->passes[cblk->numpassesinlayers - 1].rate;\r
- layer->data =\r
- cblk->data +\r
- cblk->passes[cblk->numpassesinlayers - 1].rate;\r
- }\r
- if (final)\r
- cblk->numpassesinlayers = n;\r
- }\r
- }\r
- }\r
- }\r
- }\r
-}\r
-\r
-void tcd_rateallocate_fixed()\r
-{\r
- int layno;\r
-\r
- for (layno = 0; layno < tcd_tcp->numlayers; layno++) {\r
- tcd_makelayer_fixed(layno, 1);\r
- }\r
-}\r
-\r
-void tcd_makelayer(int layno, double thresh, int final)\r
-{\r
- int compno, resno, bandno, precno, cblkno, passno;\r
-\r
- tcd_tile->distolayer[layno] = 0; /*add fixed_quality*/\r
-\r
- for (compno = 0; compno < tcd_tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- tcd_layer_t *layer = &cblk->layers[layno];\r
- int n;\r
-\r
- if (layno == 0) {\r
- cblk->numpassesinlayers = 0;\r
- }\r
- n = cblk->numpassesinlayers;\r
- for (passno = cblk->numpassesinlayers;\r
- passno < cblk->totalpasses; passno++) {\r
- int dr;\r
- double dd;\r
- tcd_pass_t *pass = &cblk->passes[passno];\r
- if (n == 0) {\r
- dr = pass->rate;\r
- dd = pass->distortiondec;\r
- } else {\r
- dr = pass->rate - cblk->passes[n - 1].rate;\r
- dd = pass->distortiondec - cblk->passes[n -\r
- 1].distortiondec;\r
- }\r
- if (!dr) {\r
- if (dd)\r
- n = passno + 1;\r
- continue;\r
- }\r
-\r
- if (dd / dr >= thresh)\r
- n = passno + 1;\r
- }\r
- layer->numpasses = n - cblk->numpassesinlayers;\r
-\r
- if (!layer->numpasses) {\r
- layer->disto = 0;\r
- continue;\r
- }\r
-\r
- if (cblk->numpassesinlayers == 0) {\r
- layer->len = cblk->passes[n - 1].rate;\r
- layer->data = cblk->data;\r
- layer->disto = cblk->passes[n - 1].distortiondec;\r
- } else {\r
- layer->len = cblk->passes[n - 1].rate -\r
- cblk->passes[cblk->numpassesinlayers - 1].rate;\r
- layer->data =\r
- cblk->data +\r
- cblk->passes[cblk->numpassesinlayers - 1].rate;\r
- layer->disto =\r
- cblk->passes[n - 1].distortiondec -\r
- cblk->passes[cblk->numpassesinlayers - 1].distortiondec;\r
- }\r
-\r
- tcd_tile->distolayer[layno] += layer->disto; /*add fixed_quality*/\r
-\r
- if (final)\r
- cblk->numpassesinlayers = n;\r
- }\r
- }\r
- }\r
- }\r
- }\r
-}\r
-\r
-void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)\r
-{\r
- int compno, resno, bandno, precno, cblkno, passno, layno;\r
- double min, max;\r
- double cumdisto[100]; /*add fixed_quality*/\r
- const double K = 1; /* 1.1; //add fixed_quality*/\r
-\r
- double maxSE = 0;\r
- min = DBL_MAX;\r
- max = 0;\r
-\r
- tcd_tile->nbpix = 0; /*add fixed_quality*/\r
-\r
- for (compno = 0; compno < tcd_tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];\r
-\r
- tilec->nbpix = 0;\r
- for (resno = 0; resno < tilec->numresolutions; resno++) {\r
- tcd_resolution_t *res = &tilec->resolutions[resno];\r
- for (bandno = 0; bandno < res->numbands; bandno++) {\r
- tcd_band_t *band = &res->bands[bandno];\r
- for (precno = 0; precno < res->pw * res->ph; precno++) {\r
- tcd_precinct_t *prc = &band->precincts[precno];\r
- for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {\r
- tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
- for (passno = 0; passno < cblk->totalpasses; passno++) {\r
- tcd_pass_t *pass = &cblk->passes[passno];\r
- int dr;\r
- double dd, rdslope;\r
- if (passno == 0) {\r
- dr = pass->rate;\r
- dd = pass->distortiondec;\r
- } else {\r
- dr = pass->rate - cblk->passes[passno - 1].rate;\r
- dd = pass->distortiondec -\r
- cblk->passes[passno - 1].distortiondec;\r
- }\r
- if (dr == 0) {\r
- continue;\r
- }\r
-\r
- rdslope = dd / dr;\r
-\r
- if (rdslope < min) {\r
- min = rdslope;\r
- }\r
- if (rdslope > max) {\r
- max = rdslope;\r
- }\r
- } /* passno */\r
-\r
- tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); /*add fixed_quality*/\r
-\r
- tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); /*add fixed_quality*/\r
-\r
- } /* cbklno */\r
- } /* precno */\r
- } /* bandno */\r
- } /* resno */\r
- maxSE += (((double)(1 << tcd_img->comps[compno].prec) - 1.0) * ((double)(1 << tcd_img->comps[compno].prec) -1.0)) * ((double)(tilec->nbpix));\r
- } /* compno */\r
-\r
- /* add antonin index */\r
- if (info_IM->index_on) {\r
- info_tile *info_TL = &info_IM->tile[tcd_tileno];\r
- info_TL->nbpix = tcd_tile->nbpix;\r
- info_TL->distotile = tcd_tile->distotile;\r
- info_TL->thresh =\r
- (double *) malloc(tcd_tcp->numlayers * sizeof(double));\r
- }\r
- /* dda */\r
-\r
- for (layno = 0; layno < tcd_tcp->numlayers; layno++) {\r
- volatile double lo = min;\r
- volatile double hi = max;\r
- volatile int success = 0;\r
- volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len; /*Mod antonin losslessbug*/\r
- volatile double goodthresh;\r
- volatile int i;\r
- double distotarget; /*add fixed_quality*/\r
-\r
- distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); /* add fixed_quality*/\r
- \r
- if ((tcd_tcp->rates[layno]) || (tcd_cp->disto_alloc==0)) {\r
- for (i = 0; i < 32; i++) {\r
- volatile double thresh = (lo + hi) / 2;\r
- int l = 0;\r
- double distoachieved = 0; /* add fixed_quality*/\r
-\r
- tcd_makelayer(layno, thresh, 0);\r
-\r
- if (tcd_cp->fixed_quality) { /* add fixed_quality*/\r
- distoachieved =\r
- layno ==\r
- 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +\r
- tcd_tile->distolayer[layno];\r
- if (distoachieved < distotarget) {\r
- hi = thresh;\r
- continue;\r
- }\r
- lo = thresh;\r
- } else {\r
- l =\r
- t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,\r
- layno + 1, dest, maxlen, info_IM);\r
- /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */\r
- if (l == -999) {\r
- lo = thresh;\r
- continue;\r
- }\r
- hi = thresh;\r
- }\r
-\r
- success = 1;\r
- goodthresh = thresh;\r
- }\r
- } else {\r
- success = 1;\r
- goodthresh = min;\r
- }\r
-\r
- if (!success) {\r
- longjmp(j2k_error, 1);\r
- }\r
-\r
- if (info_IM->index_on) { /* Threshold for Marcela Index */\r
- info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;\r
- }\r
- tcd_makelayer(layno, goodthresh, 1);\r
-\r
- cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; /* add fixed_quality*/\r
- }\r
-}\r
-\r
-int\r
-tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,\r
- info_image * info_IM)\r
-{\r
- int compno;\r
- int l, i, npck=0;\r
- clock_t time7;\r
- tcd_tile_t *tile;\r
- j2k_tcp_t *tcp = &tcd_cp->tcps[0];\r
- j2k_tccp_t *tccp = &tcp->tccps[0];\r
- \r
- tcd_tileno = tileno;\r
- tcd_tile = tcd_image.tiles;\r
- tcd_tcp = &tcd_cp->tcps[tileno];\r
- tile = tcd_tile;\r
- /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */\r
- if (info_IM->index_on) {\r
- tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /*Based on Component 0*/\r
- \r
- for (i = 0; i < tilec_idx->numresolutions; i++) {\r
- \r
- tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];\r
- \r
- info_IM->tile[tileno].pw[i] = res_idx->pw;\r
- info_IM->tile[tileno].ph[i] = res_idx->ph;\r
- \r
- npck+=res_idx->pw * res_idx->ph;\r
- \r
- info_IM->tile[tileno].pdx[i] = tccp->prcw[i];\r
- info_IM->tile[tileno].pdy[i] = tccp->prch[i];\r
- \r
- }\r
- info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));\r
- }\r
- /* << INDEX */\r
-\r
-/*---------------TILE-------------------*/\r
-\r
- time7 = clock();\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- FILE *src;\r
- char tmp[256];\r
- int k;\r
- unsigned char elmt;\r
- int i, j;\r
- int tw, w;\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- int adjust =\r
- tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].\r
- prec - 1);\r
- int offset_x, offset_y;\r
-\r
- offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);\r
- offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);\r
- tw = tilec->x1 - tilec->x0;\r
- w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);\r
- sprintf(tmp, "Compo%d", compno); /* component file */\r
- src = fopen(tmp, "rb");\r
- if (!src) {\r
- fprintf(stderr, "failed to open %s for reading\n", tmp);\r
- return 1;\r
- }\r
-\r
- /* read the Compo file to extract data of the tile */\r
- k = 0;\r
- fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,\r
- SEEK_SET);\r
- k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;\r
- for (j = tilec->y0; j < tilec->y1; j++) {\r
- for (i = tilec->x0; i < tilec->x1; i++) {\r
- if (tcd_tcp->tccps[compno].qmfbid == 1) {\r
- elmt = fgetc(src);\r
- tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =\r
- elmt - adjust;\r
- k++;\r
- } else if (tcd_tcp->tccps[compno].qmfbid == 0) {\r
- elmt = fgetc(src);\r
- tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =\r
- (elmt - adjust) << 13;\r
- k++;\r
- }\r
- }\r
- fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,\r
- SEEK_CUR);\r
- k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;\r
-\r
- }\r
- fclose(src);\r
- }\r
-\r
-/*----------------MCT-------------------*/\r
-\r
- if (tcd_tcp->mct) {\r
- if (tcd_tcp->tccps[0].qmfbid == 0) {\r
- mct_encode_real(tile->comps[0].data, tile->comps[1].data,\r
- tile->comps[2].data,\r
- (tile->comps[0].x1 -\r
- tile->comps[0].x0) * (tile->comps[0].y1 -\r
- tile->comps[0].y0));\r
- } else {\r
- mct_encode(tile->comps[0].data, tile->comps[1].data,\r
- tile->comps[2].data,\r
- (tile->comps[0].x1 -\r
- tile->comps[0].x0) * (tile->comps[0].y1 -\r
- tile->comps[0].y0));\r
- }\r
- }\r
-/*----------------DWT---------------------*/\r
-\r
-/* mod Ive*/\r
-for (compno = 0; compno < tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- if (tcd_tcp->tccps[compno].qmfbid == 1) {\r
- dwt_encode(tilec);\r
- } else if (tcd_tcp->tccps[compno].qmfbid == 0) {\r
- dwt_encode_real(tilec);\r
- }\r
-}\r
-/* /mod Ive*/\r
-/*------------------TIER1-----------------*/\r
-\r
- t1_init_luts();\r
- t1_encode_cblks(tile, tcd_tcp);\r
-\r
-/*-----------RATE-ALLOCATE------------------*/\r
- info_IM->index_write = 0; /* INDEX */\r
-\r
- if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) /* mod fixed_quality*/\r
- /* Normal Rate/distortion allocation */\r
- tcd_rateallocate(dest, len, info_IM);\r
- else\r
- /* Fixed layer allocation */\r
- tcd_rateallocate_fixed();\r
-\r
-/*--------------TIER2------------------*/\r
- info_IM->index_write = 1; /* INDEX */\r
- l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,\r
- tcd_tcp->numlayers, dest, len, info_IM);\r
-/*---------------CLEAN-------------------*/\r
-\r
- time7 = clock() - time7;\r
- fprintf(stdout,"total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,\r
- (time7 % (int)CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);\r
-\r
- /* cleaning memory */\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tilec = &tile->comps[compno];\r
- free(tilec->data);\r
- }\r
-\r
- return l;\r
-}\r
-\r
-int\r
-tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,\r
- info_image * info_IM)\r
-{\r
- int compno;\r
- int l, i, npck=0;\r
- clock_t time;\r
- tcd_tile_t *tile;\r
- j2k_tcp_t *tcp = &tcd_cp->tcps[0];\r
- j2k_tccp_t *tccp = &tcp->tccps[0];\r
- \r
- tcd_tileno = tileno;\r
- tcd_tile = tcd_image.tiles;\r
- tcd_tcp = &tcd_cp->tcps[tileno];\r
- tile = tcd_tile;\r
- /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */\r
- if (info_IM->index_on) {\r
- tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /*Based on Component 0*/\r
- \r
- for (i = 0; i < tilec_idx->numresolutions; i++) {\r
- \r
- tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];\r
- \r
- info_IM->tile[tileno].pw[i] = res_idx->pw;\r
- info_IM->tile[tileno].ph[i] = res_idx->ph;\r
- \r
- npck+=res_idx->pw * res_idx->ph;\r
- \r
- info_IM->tile[tileno].pdx[i] = tccp->prcw[i];\r
- info_IM->tile[tileno].pdy[i] = tccp->prch[i];\r
- \r
- }\r
- info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));\r
- }\r
- /* << INDEX */\r
-/*---------------TILE-------------------*/\r
- time = clock();\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- FILE *src;\r
- char tmp[256];\r
- int k;\r
- int elmt;\r
- int i, j;\r
- int tw, w;\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- int adjust =\r
- tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].\r
- prec - 1);\r
- int offset_x, offset_y;\r
-\r
- offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);\r
- offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);\r
- tw = tilec->x1 - tilec->x0;\r
- w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);\r
- sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */\r
- src = fopen(tmp, "rb");\r
- if (!src) {\r
- fprintf(stderr, "failed to open %s for reading\n", tmp);\r
- return 1;\r
- }\r
- /* Extract data from bandtile file limited to the current tile */\r
- k = 0;\r
- while (k < tilec->x0 - offset_x) {\r
- k++;\r
- fscanf(src, "%d", &elmt);\r
- }\r
-\r
- for (j = 0; j < tilec->y1 - tilec->y0; j++) {\r
- for (i = tilec->x0; i < tilec->x1; i++) {\r
- if (tcd_tcp->tccps[compno].qmfbid == 1) {\r
- fscanf(src, "%d", &elmt);\r
- tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;\r
- k++;\r
- } else if (tcd_tcp->tccps[compno].qmfbid == 0) {\r
- fscanf(src, "%d", &elmt);\r
- tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;\r
- k++;\r
- }\r
- }\r
- while (k < tilec->x0 - offset_x + (j + 1) * w) {\r
- k++;\r
- fscanf(src, "%d", &elmt);\r
- }\r
- }\r
- fclose(src);\r
- }\r
-\r
-/*----------------MCT-------------------*/\r
-\r
- if (tcd_tcp->mct) {\r
- if (tcd_tcp->tccps[0].qmfbid == 0) {\r
- mct_encode_real(tile->comps[0].data, tile->comps[1].data,\r
- tile->comps[2].data,\r
- (tile->comps[0].x1 -\r
- tile->comps[0].x0) * (tile->comps[0].y1 -\r
- tile->comps[0].y0));\r
- } else {\r
- mct_encode(tile->comps[0].data, tile->comps[1].data,\r
- tile->comps[2].data,\r
- (tile->comps[0].x1 -\r
- tile->comps[0].x0) * (tile->comps[0].y1 -\r
- tile->comps[0].y0));\r
- }\r
- }\r
-\r
-/*----------------DWT---------------------*/\r
-\r
-/* mod Ive*/\r
-for (compno = 0; compno < tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- if (tcd_tcp->tccps[compno].qmfbid == 1) {\r
- dwt_encode(tilec);\r
- } else if (tcd_tcp->tccps[compno].qmfbid == 0) {\r
- dwt_encode_real(tilec);\r
- }\r
-}\r
-/* /mod Ive*/\r
-\r
-/*------------------TIER1-----------------*/\r
-\r
- t1_init_luts();\r
- t1_encode_cblks(tile, tcd_tcp);\r
-\r
-/*-----------RATE-ALLOCATE------------------*/\r
-\r
- info_IM->index_write = 0; /* INDEX */\r
-\r
- if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) /* mod fixed_quality*/\r
-\r
- /* Normal Rate/distortion allocation */\r
-\r
- tcd_rateallocate(dest, len, info_IM);\r
-\r
- else\r
- /* Fixed layer allocation */\r
-\r
- tcd_rateallocate_fixed();\r
-\r
-/*--------------TIER2------------------*/\r
- info_IM->index_write = 1; /* INDEX */\r
-\r
- l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,\r
- tcd_tcp->numlayers, dest, len, info_IM);\r
-\r
- /*---------------CLEAN-------------------*/\r
- time = clock() - time;\r
- fprintf(stdout,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,\r
- (time % (int)CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tilec = &tile->comps[compno];\r
- free(tilec->data);\r
- }\r
-\r
- return l;\r
-}\r
-\r
-\r
-int tcd_decode_tile(unsigned char *src, int len, int tileno)\r
-{\r
- int l;\r
- int compno;\r
- int eof = 0;\r
- clock_t time;\r
- tcd_tile_t *tile;\r
-\r
- tcd_tileno = tileno;\r
- tcd_tile = &tcd_image.tiles[tileno];\r
- tcd_tcp = &tcd_cp->tcps[tileno];\r
- tile = tcd_tile;\r
-\r
- time = clock();\r
-\r
- fprintf(stdout, "Tile %d of %d decoded in ", tileno + 1,\r
- tcd_cp->tw * tcd_cp->th);\r
-\r
- /*--------------TIER2------------------*/\r
-\r
- l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);\r
-\r
- if (l == -999) {\r
- eof = 1;\r
- fprintf(stderr, "tcd_decode: incomplete bistream\n");\r
- }\r
-\r
- /*------------------TIER1-----------------*/\r
- t1_init_luts();\r
- t1_decode_cblks(tile, tcd_tcp);\r
-\r
- /*----------------DWT---------------------*/\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- if (tcd_cp->reduce != 0) {\r
- tcd_img->comps[compno].resno_decoded =\r
- tile->comps[compno].numresolutions - tcd_cp->reduce - 1;\r
- }\r
-\r
-\r
- /* mod Ive */\r
- if (tcd_tcp->tccps[compno].qmfbid == 1) {\r
- dwt_decode(tilec, \r
- tilec->numresolutions - 1 - \r
- tcd_img->comps[compno].resno_decoded);\r
- } else {\r
- dwt_decode_real(tilec,\r
- tilec->numresolutions - 1 -\r
- tcd_img->comps[compno].resno_decoded);\r
- }\r
- /* /mod Ive*/\r
- \r
- if (tile->comps[compno].numresolutions > 0)\r
- tcd_img->comps[compno].factor =\r
- tile->comps[compno].numresolutions -\r
- (tcd_img->comps[compno].resno_decoded + 1);\r
- }\r
-\r
- /*----------------MCT-------------------*/\r
-\r
- if (tcd_tcp->mct) {\r
- if (tcd_tcp->tccps[0].qmfbid == 1) {\r
- mct_decode(tile->comps[0].data, tile->comps[1].data,\r
- tile->comps[2].data,\r
- (tile->comps[0].x1 -\r
- tile->comps[0].x0) * (tile->comps[0].y1 -\r
- tile->comps[0].y0));\r
- } else {\r
- mct_decode_real(tile->comps[0].data, tile->comps[1].data,\r
- tile->comps[2].data,\r
- (tile->comps[0].x1 -\r
- tile->comps[0].x0) * (tile->comps[0].y1 -\r
- tile->comps[0].y0));\r
- }\r
- }\r
-\r
- /*---------------TILE-------------------*/\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- tcd_tilecomp_t *tilec = &tile->comps[compno];\r
- tcd_resolution_t *res =\r
- &tilec->resolutions[tcd_img->comps[compno].resno_decoded];\r
- int adjust =\r
- tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].\r
- prec - 1);\r
- int min =\r
- tcd_img->comps[compno].\r
- sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;\r
- int max =\r
- tcd_img->comps[compno].\r
- sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -\r
- 1 : (1 << tcd_img->comps[compno].prec) - 1;\r
-\r
- int tw = tilec->x1 - tilec->x0;\r
- int w = tcd_img->comps[compno].w;\r
-\r
- int i, j;\r
- int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,\r
- tcd_img->comps[compno].factor);\r
- int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,\r
- tcd_img->comps[compno].factor);\r
-\r
- for (j = res->y0; j < res->y1; j++) {\r
- for (i = res->x0; i < res->x1; i++) {\r
-\r
- int v;\r
- double tmp = (tilec->data[i - res->x0 + (j - res->y0) * tw])/8192.0;\r
- int tmp2;\r
- \r
- if (tcd_tcp->tccps[compno].qmfbid == 1) {\r
- v = tilec->data[i - res->x0 + (j - res->y0) * tw];\r
- } else {\r
- tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);\r
- v = ((tmp<0)?-tmp2:tmp2);\r
- }\r
- \r
- v += adjust;\r
-\r
- tcd_img->comps[compno].data[(i - offset_x) +\r
- (j - offset_y) * w] =\r
- int_clamp(v, min, max);\r
- }\r
- }\r
- }\r
-\r
- time = clock() - time;\r
- fprintf(stdout, "%ld.%.3ld s\n", time / CLOCKS_PER_SEC,\r
- (time % (int)CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);\r
-\r
-\r
-\r
- for (compno = 0; compno < tile->numcomps; compno++) {\r
- free(tcd_image.tiles[tileno].comps[compno].data);\r
- }\r
-\r
- if (eof) {\r
- longjmp(j2k_error, 1);\r
- }\r
-\r
- return l;\r
-}\r
-\r
-\r
-\r
-void tcd_dec_release()\r
-\r
-{\r
-\r
- int tileno,compno,resno,bandno,precno;\r
-\r
- for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {\r
-\r
- tcd_tile_t tile=tcd_image.tiles[tileno];\r
-\r
- for (compno=0;compno<tile.numcomps;compno++) {\r
-\r
- tcd_tilecomp_t tilec=tile.comps[compno];\r
-\r
- for (resno=0;resno<tilec.numresolutions;resno++) {\r
-\r
- tcd_resolution_t res=tilec.resolutions[resno];\r
-\r
- for (bandno=0;bandno<res.numbands;bandno++) {\r
-\r
- tcd_band_t band=res.bands[bandno];\r
-\r
- for (precno=0;precno<res.ph*res.pw;precno++) {\r
-\r
- tcd_precinct_t prec=band.precincts[precno];\r
-\r
- if (prec.cblks!=NULL) free(prec.cblks);\r
-\r
- if (prec.imsbtree!=NULL) free(prec.imsbtree);\r
-\r
- if (prec.incltree!=NULL) free(prec.incltree);\r
-\r
- }\r
-\r
- if (band.precincts!=NULL) free(band.precincts);\r
-\r
- }\r
-\r
- }\r
-\r
- if (tilec.resolutions!=NULL) free(tilec.resolutions);\r
-\r
- }\r
-\r
- if (tile.comps!=NULL) free(tile.comps);\r
-\r
- }\r
-\r
- if (tcd_image.tiles!=NULL) free(tcd_image.tiles);\r
-\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens \r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __TCD_H\r
-#define __TCD_H\r
-\r
-#include "j2k.h"\r
-#include "tgt.h"\r
-\r
-typedef struct {\r
- int numpasses;\r
- int len;\r
- unsigned char *data;\r
- int maxpasses;\r
- int numnewpasses;\r
- int newlen;\r
-} tcd_seg_t;\r
-\r
-typedef struct {\r
- int rate;\r
- double distortiondec;\r
- int term, len;\r
-} tcd_pass_t;\r
-\r
-typedef struct {\r
- int numpasses; /* Number of passes in the layer */\r
- int len; /* len of information */\r
- double disto; /* add for index (Cfr. Marcela) */\r
- unsigned char *data; /* data */\r
-} tcd_layer_t;\r
-\r
-typedef struct {\r
- int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */\r
- int numbps;\r
- int numlenbits;\r
- int len; /* length */\r
- int numpasses; /* number of pass already done for the code-blocks */\r
- int numnewpasses; /* number of pass added to the code-blocks */\r
- int numsegs; /* number of segments */\r
- tcd_seg_t segs[100]; /* segments informations */\r
- unsigned char data[8192]; /* Data */\r
- int numpassesinlayers; /* number of passes in the layer */\r
- tcd_layer_t layers[100]; /* layer information */\r
- int totalpasses; /* total number of passes */\r
- tcd_pass_t passes[100]; /* information about the passes */\r
-} tcd_cblk_t;\r
-\r
-typedef struct {\r
- int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */\r
- int cw, ch; /* number of precinct in width and heigth */\r
- tcd_cblk_t *cblks; /* code-blocks informations */\r
- tgt_tree_t *incltree; /* inclusion tree */\r
- tgt_tree_t *imsbtree; /* IMSB tree */\r
-} tcd_precinct_t;\r
-\r
-typedef struct {\r
- int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */\r
- int bandno;\r
- tcd_precinct_t *precincts; /* precinct information */\r
- int numbps;\r
- float stepsize;\r
-} tcd_band_t;\r
-\r
-typedef struct {\r
- int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */\r
- int pw, ph;\r
- int numbands; /* number sub-band for the resolution level */\r
- tcd_band_t bands[3]; /* subband information */\r
-} tcd_resolution_t;\r
-\r
-typedef struct {\r
- int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */\r
- int numresolutions; /* number of resolutions level */\r
- tcd_resolution_t *resolutions; /* resolutions information */\r
- int *data; /* data of the component */\r
- int nbpix; /* add fixed_quality */\r
-} tcd_tilecomp_t;\r
-\r
-typedef struct {\r
- int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */\r
- int numcomps; /* number of components in tile */\r
- tcd_tilecomp_t *comps; /* Components information */\r
- int nbpix; /* add fixed_quality */\r
- double distotile; /* add fixed_quality */\r
- double distolayer[100]; /* add fixed_quality */\r
-} tcd_tile_t;\r
-\r
-typedef struct {\r
- int tw, th; /* number of tiles in width and heigth */\r
- tcd_tile_t *tiles; /* Tiles information */\r
-} tcd_image_t;\r
-\r
-/*\r
- * Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)\r
- * img: raw image\r
- * cp: coding parameters\r
- * curtileno : number that identifies the tile that will be encoded\r
- */\r
-void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);\r
-\r
-\r
-/*\r
- * Initialize the tile coder (allocate the memory)\r
- * img: raw image\r
- * cp: coding parameters\r
- * curtileno : number that identifies the tile that will be encoded\r
- */\r
-void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);\r
-\r
-\r
-/*\r
- * Initialize the tile decoder\r
- * img: raw image\r
- * cp: coding parameters\r
- */\r
-void tcd_init(j2k_image_t * img, j2k_cp_t * cp);\r
-\r
-\r
-/*\r
- * Free the memory allocated for encoding\r
- * img: raw image\r
- * cp: coding parameters\r
- * curtileno : number that identifies the tile that will be encoded\r
- */\r
-void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);\r
-\r
-/*\r
- * Encode a tile from the raw image into a buffer, format pnm, pgm or ppm\r
- * tileno: number that identifies one of the tiles to be encoded\r
- * dest: destination buffer\r
- * len: length of destination buffer\r
- * info_IM: creation of index file\r
- */\r
-int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,\r
- info_image * info_IM);\r
-\r
-\r
-/*\r
- * Encode a tile from the raw image into a buffer, format pgx\r
- * tileno: number that identifies one of the tiles to be encoded\r
- * dest: destination buffer\r
- * len: length of destination buffer\r
- * info_IM: creation of index file\r
- */\r
-int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,\r
- info_image * info_IM);\r
-\r
-/*\r
- * Decode a tile from a buffer into a raw image\r
- * src: source buffer\r
- * len: length of the source buffer\r
- * tileno: number that identifies the tile that will be decoded\r
- */\r
-int tcd_decode_tile(unsigned char *src, int len, int tileno);\r
-\r
-void tcd_dec_release();\r
-\r
-#endif\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "tgt.h"\r
-#include "bio.h"\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-\r
-/* <summary> */\r
-/* Reset tag-tree. */\r
-/* </summary> */\r
-void tgt_reset(tgt_tree_t * tree)\r
-{\r
- int i;\r
- /* new */\r
- if (!tree || tree == NULL)\r
- return;\r
-\r
- for (i = 0; i < tree->numnodes; i++) {\r
- tree->nodes[i].value = 999;\r
- tree->nodes[i].low = 0;\r
- tree->nodes[i].known = 0;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Create tag-tree. */\r
-/* </summary> */\r
-tgt_tree_t *tgt_create(int numleafsh, int numleafsv)\r
-{\r
- int nplh[32];\r
- int nplv[32];\r
- tgt_node_t *node;\r
- tgt_node_t *parentnode;\r
- tgt_node_t *parentnode0;\r
- tgt_tree_t *tree;\r
- int i, j, k;\r
- int numlvls;\r
- int n;\r
-\r
- tree = (tgt_tree_t *) malloc(sizeof(tgt_tree_t));\r
- tree->numleafsh = numleafsh;\r
- tree->numleafsv = numleafsv;\r
-\r
- numlvls = 0;\r
- nplh[0] = numleafsh;\r
- nplv[0] = numleafsv;\r
- tree->numnodes = 0;\r
- do {\r
- n = nplh[numlvls] * nplv[numlvls];\r
- nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;\r
- nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;\r
- tree->numnodes += n;\r
- ++numlvls;\r
- } while (n > 1);\r
-\r
- /* ADD */\r
- if (tree->numnodes == 0) {\r
- free(tree);\r
- return NULL;\r
- }\r
-\r
- tree->nodes = (tgt_node_t *) malloc(tree->numnodes * sizeof(tgt_node_t));\r
-\r
- node = tree->nodes;\r
- parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];\r
- parentnode0 = parentnode;\r
-\r
- for (i = 0; i < numlvls - 1; ++i) {\r
- for (j = 0; j < nplv[i]; ++j) {\r
- k = nplh[i];\r
- while (--k >= 0) {\r
- node->parent = parentnode;\r
- ++node;\r
- if (--k >= 0) {\r
- node->parent = parentnode;\r
- ++node;\r
- }\r
- ++parentnode;\r
- }\r
- if ((j & 1) || j == nplv[i] - 1) {\r
- parentnode0 = parentnode;\r
- } else {\r
- parentnode = parentnode0;\r
- parentnode0 += nplh[i];\r
- }\r
- }\r
- }\r
- node->parent = 0;\r
-\r
- tgt_reset(tree);\r
-\r
- return tree;\r
-}\r
-\r
-/* <summary> */\r
-/* Destroy tag-tree. */\r
-/* </summary> */\r
-void tgt_destroy(tgt_tree_t * t)\r
-{\r
- free(t->nodes);\r
- free(t);\r
-}\r
-\r
-/* <summary> */\r
-/* Set the value of a leaf of the tag-tree. */\r
-/* </summary> */\r
-void tgt_setvalue(tgt_tree_t * tree, int leafno, int value)\r
-{\r
- tgt_node_t *node;\r
- node = &tree->nodes[leafno];\r
- while (node && node->value > value) {\r
- node->value = value;\r
- node = node->parent;\r
- }\r
-}\r
-\r
-/* <summary> */\r
-/* Encode the value of a leaf of the tag-tree. */\r
-/* </summary> */\r
-void tgt_encode(tgt_tree_t * tree, int leafno, int threshold)\r
-{\r
- tgt_node_t *stk[31];\r
- tgt_node_t **stkptr;\r
- tgt_node_t *node;\r
- int low;\r
-\r
- stkptr = stk;\r
- node = &tree->nodes[leafno];\r
- while (node->parent) {\r
- *stkptr++ = node;\r
- node = node->parent;\r
- }\r
-\r
- low = 0;\r
- for (;;) {\r
- if (low > node->low) {\r
- node->low = low;\r
- } else {\r
- low = node->low;\r
- }\r
-\r
- while (low < threshold) {\r
- if (low >= node->value) {\r
- if (!node->known) {\r
- bio_write(1, 1);\r
- node->known = 1;\r
- }\r
- break;\r
- }\r
- bio_write(0, 1);\r
- ++low;\r
- }\r
-\r
- node->low = low;\r
- if (stkptr == stk)\r
- break;\r
- node = *--stkptr;\r
- }\r
-\r
-}\r
-\r
-/* <summary> */\r
-/* Decode the value of a leaf of the tag-tree. */\r
-/* </summary> */\r
-int tgt_decode(tgt_tree_t * tree, int leafno, int threshold)\r
-{\r
- tgt_node_t *stk[31];\r
- tgt_node_t **stkptr;\r
- tgt_node_t *node;\r
- int low;\r
-\r
- stkptr = stk;\r
- node = &tree->nodes[leafno];\r
- while (node->parent) {\r
- *stkptr++ = node;\r
- node = node->parent;\r
- }\r
-\r
- low = 0;\r
- for (;;) {\r
- if (low > node->low) {\r
- node->low = low;\r
- } else {\r
- low = node->low;\r
- }\r
- while (low < threshold && low < node->value) {\r
- if (bio_read(1)) {\r
- node->value = low;\r
- } else {\r
- ++low;\r
- }\r
- }\r
- node->low = low;\r
- if (stkptr == stk) {\r
- break;\r
- }\r
- node = *--stkptr;\r
- }\r
-\r
- return (node->value < threshold) ? 1 : 0;\r
-}\r
+++ /dev/null
-/*\r
- * Copyright (c) 2001-2002, David Janssens\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __TGT_H\r
-#define __TGT_H\r
-\r
-typedef struct tgt_node {\r
- struct tgt_node *parent;\r
- int value;\r
- int low;\r
- int known;\r
-} tgt_node_t;\r
-\r
-typedef struct {\r
- int numleafsh;\r
- int numleafsv;\r
- int numnodes;\r
- tgt_node_t *nodes;\r
-} tgt_tree_t;\r
-\r
-\r
-\r
-/*\r
- * Reset a tag-tree (set all leaves to 0)\r
- * tree: tag-tree to reset\r
- */\r
-void tgt_reset(tgt_tree_t * tree);\r
-\r
-/*\r
- * Create a tag-tree\r
- * numleafsh: width of the array of leafs of the tree\r
- * numleafsv: height of the array of leafs of the tree\r
- */\r
-tgt_tree_t *tgt_create(int numleafsh, int numleafsv);\r
-\r
-/*\r
- * Destroy a tag-tree, liberating memory\r
- * tree: tag-tree to destroy\r
- */\r
-void tgt_destroy(tgt_tree_t * tree);\r
-\r
-/*\r
- * Set the value of a leaf of a tag-tree\r
- * tree: tag-tree to modify\r
- * leafno: number that identifies the leaf to modify\r
- * value: new value of the leaf\r
- */\r
-void tgt_setvalue(tgt_tree_t * tree, int leafno, int value);\r
-\r
-/*\r
- * Encode the value of a leaf of the tag-tree up to a given threshold\r
- * leafno: number that identifies the leaf to encode\r
- * threshold: threshold to use when encoding value of the leaf\r
- */\r
-void tgt_encode(tgt_tree_t * tree, int leafno, int threshold);\r
-\r
-/*\r
- * Decode the value of a leaf of the tag-tree up to a given threshold\r
- * leafno: number that identifies the leaf to decode\r
- * threshold: threshold to use when decoding value of the leaf\r
- */\r
-int tgt_decode(tgt_tree_t * tree, int leafno, int threshold);\r
-\r
-#endif\r
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>
+#include <windows.h> /* for time functions */
-//MEMORY LEAK
-#ifdef _DEBUG
-#define _CRTDBG_MAP_ALLOC
-#include <stdlib.h> // Must be included first
-#include <crtdbg.h>
-#endif
-//MEM
+#include "opj_includes.h"
+#include "mj2.h"
-#include <windows.h> /* for time functions */
#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);
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);
-
-int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample, unsigned int snum);
-
-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_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, opj_event_mgr_t *event_mgr);
+
+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 */
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)
{
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];
}
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
/* ------------- */
-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;
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");
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)
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> */
/* ------------- */
-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)
#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.
#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.
/* ------------- */
-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);
// 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 */
}
}
/* ------------- */
-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, ¶meters);
frame_codestream = (unsigned char*) malloc (sample->sample_size-8); /* Skipping JP2C marker */
if(frame_codestream == NULL)
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();
- return 1;
- }
+ 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;
+ }
+
+ 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 */
+ numcomps = img->numcomps;
+ /* Alignments: " < To help maintain xml pretty-printing */
fprintf(xmlout, " <JP2_Frame Num=\"%d\">\n", snum+1);
fprintf(xmlout, " <MainHeader>\n");
/* There can be multiple codestreams; a particular image is entirely within a single codestream */
/* 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");
/* 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 {
}
}
-#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;
/* ------------- */
-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");
/* ------------- */
-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]);
/* ------------- */
-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 */
}
/* 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);
/* ------------- */
-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;
/* ------------- */
-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 */
}
/* ------------- */
-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 */
}
/* 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);
/* 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)
/* ------------- */
-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;
/* ------------- */
-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 */
}
/* ------------- */
-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 */
}
/* 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() */
/* ------------- */
-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() */
/* ------------- */
-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 */
/* ------------- */
/* 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*/
/* ------------- */
-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");
#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)
}
}
-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.
}
}
-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. */
}
}
-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) */
}
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,
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);
--- /dev/null
+/*
+ * 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"
+
+/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Read box headers
+@param cinfo Codec context info
+@param cio Input stream
+@param box
+@return Returns true if successful, returns false otherwise
+*/
+static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
+
+/*
+*
+* Read box headers
+*
+*/
+
+int mj2_read_boxhdr(mj2_box_t * box, opj_cio_t *cio)
+{
+ box->init_pos = cio_tell(cio);
+ box->length = cio_read(cio, 4);
+ box->type = cio_read(cio, 4);
+ if (box->length == 1) {
+ if (cio_read(cio, 4) != 0) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Cannot handle box sizes higher than 2^32\n");
+ return 1;
+ };
+ box->length = cio_read(cio, 4);
+ if (box->length == 0)
+ box->length = cio_numbytesleft(cio) + 12;
+ }
+ else if (box->length == 0) {
+ box->length = cio_numbytesleft(cio) + 8;
+ }
+ return 0;
+}
+
+/*
+*
+* Initialisation of a Standard Movie, given a simple movie structure defined by the user
+* The movie will have one sample per chunk
+*
+* Arguments: opj_mj2_t * movie
+* Several variables of "movie" must be defined in order to enable a correct execution of
+* this function:
+* - The number of tracks of each type (movie->num_vtk, movie->num_stk, movie->num_htk)
+* - The memory for each must be allocated (movie->tk)
+* - For each track:
+* The track type (tk->track_type)
+* The number of sample (tk->num_samples)
+* The sample rate (tk->sample_rate)
+*
+*/
+
+int mj2_init_stdmovie(opj_mj2_t * movie)
+{
+ int i;
+ unsigned int j;
+ time_t ltime;
+
+ movie->brand = MJ2_MJ2;
+ movie->minversion = 0;
+ movie->num_cl = 2;
+ movie->cl =
+ (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
+
+ movie->cl[0] = MJ2_MJ2;
+ movie->cl[1] = MJ2_MJ2S;
+ time(<ime); /* Time since 1/1/70 */
+ movie->creation_time = (unsigned int) ltime + 2082844800; /* Seconds between 1/1/04 and 1/1/70 */
+ movie->timescale = 1000;
+
+ movie->rate = 1 << 16; /* Rate to play presentation (default = 0x00010000) */
+ movie->volume = 1 << 8; /* Movie volume (default = 0x0100) */
+ movie->trans_matrix[0] = 0x00010000; /* Transformation matrix for video */
+ movie->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
+ movie->trans_matrix[2] = 0;
+ movie->trans_matrix[3] = 0;
+ movie->trans_matrix[4] = 0x00010000;
+ movie->trans_matrix[5] = 0;
+ movie->trans_matrix[6] = 0;
+ movie->trans_matrix[7] = 0;
+ movie->trans_matrix[8] = 0x40000000;
+ movie->next_tk_id = 1;
+
+ for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
+ mj2_tk_t *tk = &movie->tk[i];
+ movie->next_tk_id++;
+ tk->jp2_struct.comps = NULL;
+ tk->jp2_struct.cl = NULL;
+
+ if (tk->track_type == 0) {
+ if (tk->num_samples == 0)
+ return 1;
+
+ tk->Dim[0] = 0;
+ tk->Dim[1] = 0;
+
+ tk->timescale = 1000; /* Timescale = 1 ms */
+
+ tk->chunk[0].num_samples = 1;
+ tk->chunk[0].sample_descr_idx = 1;
+
+ tk->same_sample_size = 0;
+
+ tk->num_samplestochunk = 1; /* One sample per chunk */
+ tk->sampletochunk =
+ (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
+ sizeof(mj2_sampletochunk_t));
+ tk->sampletochunk[0].first_chunk = 1;
+ tk->sampletochunk[0].samples_per_chunk = 1;
+ tk->sampletochunk[0].sample_descr_idx = 1;
+
+ if (tk->sample_rate == 0) {
+ opj_event_msg(tk->cinfo, EVT_ERROR,
+ "Error while initializing MJ2 movie: Sample rate of track %d must be different from zero\n",
+ tk->track_ID);
+ return 1;
+ }
+
+ for (j = 0; j < tk->num_samples; j++) {
+ tk->sample[j].sample_delta = tk->timescale / tk->sample_rate;
+ }
+
+ tk->num_tts = 1;
+ tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
+ tk->tts[0].sample_count = tk->num_samples;
+ tk->tts[0].sample_delta = tk->timescale / tk->sample_rate;
+
+ tk->horizresolution = 0x00480000; /* Horizontal resolution (typically 72) */
+ tk->vertresolution = 0x00480000; /* Vertical resolution (typically 72) */
+ tk->compressorname[0] = 0x0f4d6f74; /* Compressor Name[]: Motion JPEG2000 */
+ tk->compressorname[1] = 0x696f6e20;
+ tk->compressorname[2] = 0x4a504547;
+ tk->compressorname[3] = 0x32303030;
+ tk->compressorname[4] = 0x00120000;
+ tk->compressorname[5] = 0;
+ tk->compressorname[6] = 0x00000042;
+ tk->compressorname[7] = 0x000000DC;
+ tk->num_url = 0; /* Number of URL */
+ tk->num_urn = 0; /* Number of URN */
+ tk->graphicsmode = 0; /* Graphicsmode */
+ tk->opcolor[0] = 0; /* OpColor */
+ tk->opcolor[1] = 0; /* OpColor */
+ tk->opcolor[2] = 0; /* OpColor */
+ tk->creation_time = movie->creation_time; /* Seconds between 1/1/04 and 1/1/70 */
+ tk->language = 0; /* Language (undefined) */
+ tk->layer = 0;
+ tk->volume = 1 << 8; /* Movie volume (default = 0x0100) */
+ tk->trans_matrix[0] = 0x00010000; /* Transformation matrix for track */
+ tk->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
+ tk->trans_matrix[2] = 0;
+ tk->trans_matrix[3] = 0;
+ tk->trans_matrix[4] = 0x00010000;
+ tk->trans_matrix[5] = 0;
+ tk->trans_matrix[6] = 0;
+ tk->trans_matrix[7] = 0;
+ tk->trans_matrix[8] = 0x40000000;
+ tk->fieldcount = 1;
+ tk->fieldorder = 0;
+ tk->or_fieldcount = 1;
+ tk->or_fieldorder = 0;
+ tk->num_br = 2;
+ tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
+ tk->br[0] = MJ2_JP2;
+ tk->br[1] = MJ2_J2P0;
+ tk->num_jp2x = 0;
+ tk->hsub = 2; /* 4:2:0 */
+ tk->vsub = 2; /* 4:2:0 */
+ tk->hoff = 0;
+ tk->voff = 0;
+ tk->visual_w = tk->w << 16;
+ tk->visual_h = tk->h << 16;
+ }
+ else {
+ tk->num_br = 0;
+ tk->jp2xdata = NULL;
+ }
+ }
+ return 0;
+}
+
+/*
+* Time To Sample box Decompact
+*
+*/
+void mj2_tts_decompact(mj2_tk_t * tk)
+{
+ int i, j;
+ tk->num_samples = 0;
+ for (i = 0; i < tk->num_tts; i++) {
+ tk->num_samples += tk->tts[i].sample_count;
+ }
+
+ tk->sample =
+ (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
+
+ for (i = 0; i < tk->num_tts; i++) {
+ for (j = 0; j < tk->tts[i].sample_count; j++) {
+ tk->sample[j].sample_delta = tk->tts[i].sample_delta;
+ }
+ }
+}
+
+/*
+* Sample To Chunk box Decompact
+*
+*/
+void mj2_stsc_decompact(mj2_tk_t * tk)
+{
+ int j, i;
+ unsigned int k;
+ int sampleno=0;
+
+ if (tk->num_samplestochunk == 1) {
+ tk->num_chunks =
+ (unsigned int) ceil((double) tk->num_samples /
+ (double) tk->sampletochunk[0].samples_per_chunk);
+ tk->chunk =
+ (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
+ for (k = 0; k < tk->num_chunks; k++) {
+ tk->chunk[k].num_samples = tk->sampletochunk[0].samples_per_chunk;
+ }
+
+ } else {
+ tk->chunk =
+ (mj2_chunk_t *) malloc(tk->num_samples * sizeof(mj2_chunk_t));
+ tk->num_chunks = 0;
+ for (i = 0; i < tk->num_samplestochunk -1 ; i++) {
+ for (j = tk->sampletochunk[i].first_chunk - 1;
+ j < tk->sampletochunk[i + 1].first_chunk - 1; j++) {
+ tk->chunk[j].num_samples = tk->sampletochunk[i].samples_per_chunk;
+ tk->num_chunks++;
+ sampleno += tk->chunk[j].num_samples;
+ }
+ }
+ tk->num_chunks += (int)(tk->num_samples - sampleno) / tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
+ for (k = tk->sampletochunk[tk->num_samplestochunk - 1].first_chunk - 1;
+ k < tk->num_chunks; k++) {
+ tk->chunk[k].num_samples =
+ tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
+ }
+ tk->chunk = realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
+ }
+
+}
+
+
+/*
+* Chunk offset box Decompact
+*
+*/
+void mj2_stco_decompact(mj2_tk_t * tk)
+{
+ int j;
+ unsigned int i;
+ int k = 0;
+ int intra_chunk_offset;
+
+ for (i = 0; i < tk->num_chunks; i++) {
+ intra_chunk_offset = 0;
+ for (j = 0; j < tk->chunk[i].num_samples; j++) {
+ tk->sample[k].offset = intra_chunk_offset + tk->chunk[i].offset;
+ intra_chunk_offset += tk->sample[k].sample_size;
+ k++;
+ }
+ }
+}
+
+/*
+* Write the JP box
+*
+* JP Signature box
+*
+*/
+void mj2_write_jp(opj_cio_t *cio)
+{
+ mj2_box_t box;
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+
+ cio_write(cio, MJ2_JP, 4); /* JP */
+ cio_write(cio, 0x0d0a870a, 4); /* 0x0d0a870a required in a JP box */
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4);
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the JP box
+*
+* JPEG 2000 signature
+*
+*/
+int mj2_read_jp(opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_JP != box.type) { /* Check Marker */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected JP Marker\n");
+ return 1;
+ }
+ if (0x0d0a870a != cio_read(cio, 4)) { /* read the 0x0d0a870a required in a JP box */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with JP Marker\n");
+ return 1;
+ }
+ if (cio_tell(cio) - box.init_pos != box.length) { /* Check box length */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with JP Box size \n");
+ return 1;
+ }
+ return 0;
+
+}
+
+/*
+* Write the FTYP box
+*
+* File type box
+*
+*/
+void mj2_write_ftyp(opj_mj2_t * movie, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+
+ cio_write(cio, MJ2_FTYP, 4); /* FTYP */
+ cio_write(cio, movie->brand, 4); /* BR */
+ cio_write(cio, movie->minversion, 4); /* MinV */
+
+ for (i = 0; i < movie->num_cl; i++)
+ cio_write(cio, movie->cl[i], 4); /* CL */
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* Length */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the FTYP box
+*
+* File type box
+*
+*/
+int mj2_read_ftyp(opj_mj2_t * movie, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio); /* Box Size */
+ if (MJ2_FTYP != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected FTYP Marker\n");
+ return 1;
+ }
+
+ movie->brand = cio_read(cio, 4); /* BR */
+ movie->minversion = cio_read(cio, 4); /* MinV */
+ movie->num_cl = (box.length - 16) / 4;
+ movie->cl =
+ (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
+
+ for (i = movie->num_cl - 1; i > -1; i--)
+ movie->cl[i] = cio_read(cio, 4); /* CLi */
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with FTYP Box\n");
+ return 1;
+ }
+ return 0;
+}
+
+
+/*
+* Write the STCO box
+*
+* Chunk Offset Box
+*
+*/
+void mj2_write_stco(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+ unsigned int i;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_STCO, 4); /* STCO */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->num_chunks, 4); /* Entry Count */
+
+ for (i = 0; i < tk->num_chunks; i++) {
+ cio_write(cio, tk->chunk[i].offset, 4); /* Entry offset */
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the STCO box
+*
+* Chunk Offset Box
+*
+*/
+int mj2_read_stco(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ unsigned int i;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio); /* Box Size */
+ if (MJ2_STCO != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected STCO Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in STCO box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in STCO box. Expected flag 0\n");
+ return 1;
+ }
+
+
+ if (cio_read(cio, 4) != tk->num_chunks) {
+ opj_event_msg(cio->cinfo, EVT_ERROR,
+ "Error in STCO box: expecting same amount of entry-count as chunks \n");
+ } else {
+ for (i = 0; i < tk->num_chunks; i++) {
+ tk->chunk[i].offset = cio_read(cio, 4); /* Entry offset */
+ }
+ }
+
+ mj2_stco_decompact(tk);
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with STCO Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the STSZ box
+*
+* Sample size box
+*
+*/
+void mj2_write_stsz(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+ unsigned int i;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_STSZ, 4); /* STSZ */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ if (tk->same_sample_size == 1) { /* If they all have the same size */
+ cio_write(cio, tk->sample[0].sample_size, 4); /* Size */
+
+ cio_write(cio, 1, 4); /* Entry count = 1 */
+ }
+
+ else {
+ cio_write(cio, 0, 4); /* Sample Size = 0 becase they all have different sizes */
+
+ cio_write(cio, tk->num_samples, 4); /* Sample Count */
+
+ for (i = 0; i < tk->num_samples; i++) {
+ cio_write(cio, tk->sample[i].sample_size, 4);
+ }
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the STSZ box
+*
+* Sample size box
+*
+*/
+int mj2_read_stsz(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int sample_size;
+ unsigned int i;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio); /* Box Size */
+ if (MJ2_STSZ != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected STSZ Marker\n");
+ return 1;
+ }
+
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in STSZ box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in STSZ box. Expected flag 0\n");
+ return 1;
+ }
+
+ sample_size = cio_read(cio, 4);
+
+ if (sample_size != 0) { /* Samples do have the same size */
+ tk->same_sample_size = 1;
+ for (i = 0; i < tk->num_samples; i++) {
+ tk->sample[i].sample_size = sample_size;
+ }
+ cio_skip(cio,4); /* Sample count = 1 */
+ } else {
+ tk->same_sample_size = 0;
+ if (tk->num_samples != cio_read(cio, 4)) { /* Sample count */
+ opj_event_msg(cio->cinfo, EVT_ERROR,
+ "Error in STSZ box. Expected that sample-count is number of samples in track\n");
+ return 1;
+ }
+ for (i = 0; i < tk->num_samples; i++) {
+ tk->sample[i].sample_size = cio_read(cio, 4); /* Sample Size */
+ }
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with STSZ Box size\n");
+ return 1;
+ }
+ }
+ return 0;
+
+}
+
+/*
+* Write the STSC box
+*
+* Sample to Chunk
+*
+*/
+void mj2_write_stsc(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_STSC, 4); /* STSC */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->num_samplestochunk, 4); /* Entry Count */
+
+ for (i = 0; i < tk->num_samplestochunk; i++) {
+ cio_write(cio, tk->sampletochunk[i].first_chunk, 4); /* First Chunk */
+ cio_write(cio, tk->sampletochunk[i].samples_per_chunk, 4); /* Samples per chunk */
+ cio_write(cio, tk->sampletochunk[i].sample_descr_idx, 4); /* Samples description index */
+ }
+
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the STSC box
+*
+* Sample to Chunk
+*
+*/
+int mj2_read_stsc(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio); /* Box Size */
+ if (MJ2_STSC != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected STSC Marker\n");
+ return 1;
+ }
+
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in STSC box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in STSC box. Expected flag 0\n");
+ return 1;
+ }
+
+ tk->num_samplestochunk = cio_read(cio, 4);
+
+ tk->sampletochunk =
+ (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
+ sizeof(mj2_sampletochunk_t));
+
+
+ for (i = 0; i < tk->num_samplestochunk; i++) {
+ tk->sampletochunk[i].first_chunk = cio_read(cio, 4);
+ tk->sampletochunk[i].samples_per_chunk = cio_read(cio, 4);
+ tk->sampletochunk[i].sample_descr_idx = cio_read(cio, 4);
+ }
+
+ mj2_stsc_decompact(tk); /* decompact sample to chunk box */
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with STSC Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the STTS box
+*
+* Time to Sample Box
+*
+*/
+void mj2_write_stts(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ int i;
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_STTS, 4); /* STTS */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->num_tts, 4); /* entry_count */
+ for (i = 0; i < tk->num_tts; i++) {
+ cio_write(cio, tk->tts[i].sample_count, 4); /* Sample-count */
+ cio_write(cio, tk->tts[i].sample_delta, 4); /* Sample-Delta */
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the STTS box
+*
+*
+*
+*/
+int mj2_read_stts(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int i;
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_STTS != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected STTS Marker\n");
+ return 1;
+ }
+
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in STTS box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in STTS box. Expected flag 0\n");
+ return 1;
+ }
+
+ tk->num_tts = cio_read(cio, 4);
+
+ tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
+
+ for (i = 0; i < tk->num_tts; i++) {
+ tk->tts[i].sample_count = cio_read(cio, 4);
+ tk->tts[i].sample_delta = cio_read(cio, 4);
+ }
+
+ mj2_tts_decompact(tk);
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with STTS Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the FIEL box
+*
+* Field coding Box
+*
+*/
+void mj2_write_fiel(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_FIEL, 4); /* STTS */
+
+ cio_write(cio, tk->fieldcount, 1); /* Field count */
+ cio_write(cio, tk->fieldorder, 1); /* Field order */
+
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the FIEL box
+*
+* Field coding Box
+*
+*/
+int mj2_read_fiel(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_FIEL != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected FIEL Marker\n");
+ return 1;
+ }
+
+
+ tk->fieldcount = cio_read(cio, 1);
+ tk->fieldorder = cio_read(cio, 1);
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with FIEL Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the ORFO box
+*
+* Original Format Box
+*
+*/
+void mj2_write_orfo(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_ORFO, 4);
+
+ cio_write(cio, tk->or_fieldcount, 1); /* Original Field count */
+ cio_write(cio, tk->or_fieldorder, 1); /* Original Field order */
+
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the ORFO box
+*
+* Original Format Box
+*
+*/
+int mj2_read_orfo(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_ORFO != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected ORFO Marker\n");
+ return 1;
+ }
+
+
+ tk->or_fieldcount = cio_read(cio, 1);
+ tk->or_fieldorder = cio_read(cio, 1);
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with ORFO Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the JP2P box
+*
+* MJP2 Profile Box
+*
+*/
+void mj2_write_jp2p(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ int i;
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_JP2P, 4);
+
+ cio_write(cio, 0, 4); /* Version 0, flags =0 */
+
+ for (i = 0; i < tk->num_br; i++) {
+ cio_write(cio, tk->br[i], 4);
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the JP2P box
+*
+* MJP2 Profile Box
+*
+*/
+int mj2_read_jp2p(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int i;
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_JP2P != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected JP2P Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in JP2P box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in JP2P box. Expected flag 0\n");
+ return 1;
+ }
+
+
+ tk->num_br = (box.length - 12) / 4;
+ tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
+
+ for (i = 0; i < tk->num_br; i++) {
+ tk->br[i] = cio_read(cio, 4);
+ }
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with JP2P Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the JP2X box
+*
+* MJP2 Prefix Box
+*
+*/
+void mj2_write_jp2x(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ int i;
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_JP2X, 4);
+
+ for (i = 0; i < tk->num_jp2x; i++) {
+ cio_write(cio, tk->jp2xdata[i], 1);
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the JP2X box
+*
+* MJP2 Prefix Box
+*
+*/
+int mj2_read_jp2x(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ unsigned int i;
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_JP2X != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected JP2X Marker\n");
+ return 1;
+ }
+
+
+ tk->num_jp2x = (box.length - 8);
+ tk->jp2xdata =
+ (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
+
+ for (i = 0; i < tk->num_jp2x; i++) {
+ tk->jp2xdata[i] = cio_read(cio, 1);
+ }
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with JP2X Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the JSUB box
+*
+* MJP2 Subsampling Box
+*
+*/
+void mj2_write_jsub(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_JSUB, 4);
+
+ cio_write(cio, tk->hsub, 1);
+ cio_write(cio, tk->vsub, 1);
+ cio_write(cio, tk->hoff, 1);
+ cio_write(cio, tk->voff, 1);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the JSUB box
+*
+* MJP2 Subsampling Box
+*
+*/
+int mj2_read_jsub(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_JSUB != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected JSUB Marker\n");
+ return 1;
+ }
+
+ tk->hsub = cio_read(cio, 1);
+ tk->vsub = cio_read(cio, 1);
+ tk->hoff = cio_read(cio, 1);;
+ tk->voff = cio_read(cio, 1);
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with JSUB Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the SMJ2 box
+*
+* Visual Sample Entry Description
+*
+*/
+void mj2_write_smj2(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_MJ2, 4); /* MJ2 */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, 1, 4);
+
+ cio_write(cio, 0, 2); /* Pre-defined */
+
+ cio_write(cio, 0, 2); /* Reserved */
+
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+
+ cio_write(cio, tk->w, 2); /* Width */
+ cio_write(cio, tk->h, 2); /* Height */
+
+ cio_write(cio, tk->horizresolution, 4); /* Horizontal resolution */
+ cio_write(cio, tk->vertresolution, 4); /* Vertical resolution */
+
+ cio_write(cio, 0, 4); /* Reserved */
+
+ cio_write(cio, 1, 2); /* Pre-defined = 1 */
+
+ cio_write(cio, tk->compressorname[0], 4); /* Compressor Name */
+ cio_write(cio, tk->compressorname[1], 4);
+ cio_write(cio, tk->compressorname[2], 4);
+ cio_write(cio, tk->compressorname[3], 4);
+ cio_write(cio, tk->compressorname[4], 4);
+ cio_write(cio, tk->compressorname[5], 4);
+ cio_write(cio, tk->compressorname[6], 4);
+ cio_write(cio, tk->compressorname[7], 4);
+
+ cio_write(cio, tk->depth, 2); /* Depth */
+
+ cio_write(cio, 0xffff, 2); /* Pre-defined = -1 */
+
+ jp2_write_jp2h(&tk->jp2_struct, cio);
+
+ mj2_write_fiel(tk, cio);
+
+ if (tk->num_br != 0)
+ mj2_write_jp2p(tk, cio);
+ if (tk->num_jp2x != 0)
+ mj2_write_jp2x(tk, cio);
+
+ mj2_write_jsub(tk, cio);
+ mj2_write_orfo(tk, cio);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the SMJ2 box
+*
+* Visual Sample Entry Description
+*
+*/
+int mj2_read_smj2(opj_image_t * img, mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+ mj2_box_t box2;
+ int i;
+
+ mj2_read_boxhdr(&box, cio);
+
+ if (MJ2_MJ2 != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error in SMJ2 box: Expected MJ2 Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in MJP2 box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in MJP2 box. Expected flag 0\n");
+ return 1;
+ }
+
+ cio_skip(cio,4);
+
+ cio_skip(cio,2); /* Pre-defined */
+
+ cio_skip(cio,2); /* Reserved */
+
+ cio_skip(cio,4); /* Pre-defined */
+ cio_skip(cio,4); /* Pre-defined */
+ cio_skip(cio,4); /* Pre-defined */
+
+ tk->w = cio_read(cio, 2); /* Width */
+ tk->h = cio_read(cio, 2); /* Height */
+
+ tk->horizresolution = cio_read(cio, 4); /* Horizontal resolution */
+ tk->vertresolution = cio_read(cio, 4); /* Vertical resolution */
+
+ cio_skip(cio,4); /* Reserved */
+
+ cio_skip(cio,2); /* Pre-defined = 1 */
+
+ tk->compressorname[0] = cio_read(cio, 4); /* Compressor Name */
+ tk->compressorname[1] = cio_read(cio, 4);
+ tk->compressorname[2] = cio_read(cio, 4);
+ tk->compressorname[3] = cio_read(cio, 4);
+ tk->compressorname[4] = cio_read(cio, 4);
+ tk->compressorname[5] = cio_read(cio, 4);
+ tk->compressorname[6] = cio_read(cio, 4);
+ tk->compressorname[7] = cio_read(cio, 4);
+
+ tk->depth = cio_read(cio, 2); /* Depth */
+
+ /* Init std value */
+ tk->num_jp2x = 0;
+ tk->fieldcount = 1;
+ tk->fieldorder = 0;
+ tk->or_fieldcount = 1;
+ tk->or_fieldorder = 0;
+
+ cio_skip(cio,2); /* Pre-defined = -1 */
+
+ if (!jp2_read_jp2h(&tk->jp2_struct, cio)) {
+ opj_event_msg(tk->cinfo, EVT_ERROR, "Error reading JP2H Box\n");
+ return 1;
+ }
+
+ tk->jp2_struct.comps = (opj_jp2_comps_t *) malloc(tk->jp2_struct.numcomps * sizeof(opj_jp2_comps_t));
+ tk->jp2_struct.cl = (int *) malloc(sizeof(int));
+
+ tk->num_br = 0;
+ tk->num_jp2x = 0;
+
+ for (i = 0; cio_tell(cio) - box.init_pos < box.length; i++) {
+ mj2_read_boxhdr(&box2, cio);
+ cio_seek(cio, box2.init_pos);
+ switch (box2.type) {
+ case MJ2_FIEL:
+ if (mj2_read_fiel(tk, cio))
+ return 1;
+ break;
+
+ case MJ2_JP2P:
+ if (mj2_read_jp2p(tk, cio))
+ return 1;
+ break;
+
+ case MJ2_JP2X:
+ if (mj2_read_jp2x(tk, cio))
+ return 1;
+ break;
+
+ case MJ2_JSUB:
+ if (mj2_read_jsub(tk, cio))
+ return 1;
+ break;
+
+ case MJ2_ORFO:
+ if (mj2_read_orfo(tk, cio))
+ return 1;
+ break;
+
+ default:
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with MJP2 Box size\n");
+ return 1;
+ break;
+
+ }
+ }
+ return 0;
+}
+
+
+/*
+* Write the STSD box
+*
+* Sample Description
+*
+*/
+void mj2_write_stsd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_STSD, 4); /* STSD */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, 1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */
+
+ if (tk->track_type == 0) {
+ mj2_write_smj2(tk, cio);
+ } else if (tk->track_type == 1) {
+ // Not implemented
+ }
+ if (tk->track_type == 2) {
+ // Not implemented
+ }
+
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the STSD box
+*
+* Sample Description
+*
+*/
+int mj2_read_stsd(mj2_tk_t * tk, opj_image_t * img, opj_cio_t *cio)
+{
+ int i;
+ int entry_count, len_2skip;
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+
+ if (MJ2_STSD != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected STSD Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in STSD box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in STSD box. Expected flag 0\n");
+ return 1;
+ }
+
+ entry_count = cio_read(cio, 4);
+
+ if (tk->track_type == 0) {
+ for (i = 0; i < entry_count; i++) {
+ if (mj2_read_smj2(img, tk, cio))
+ return 1;
+ }
+ } else if (tk->track_type == 1) {
+ len_2skip = cio_read(cio, 4); // Not implemented -> skipping box
+ cio_skip(cio,len_2skip - 4);
+ } else if (tk->track_type == 2) {
+ len_2skip = cio_read(cio, 4); // Not implemented -> skipping box
+ cio_skip(cio,len_2skip - 4);
+ }
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with STSD Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the STBL box
+*
+* Sample table box box
+*
+*/
+void mj2_write_stbl(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_STBL, 4); /* STBL */
+
+ mj2_write_stsd(tk, cio);
+ mj2_write_stts(tk, cio);
+ mj2_write_stsc(tk, cio);
+ mj2_write_stsz(tk, cio);
+ mj2_write_stco(tk, cio);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the STBL box
+*
+* Sample table box box
+*
+*/
+int mj2_read_stbl(mj2_tk_t * tk, opj_image_t * img, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_STBL != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected STBL Marker\n");
+ return 1;
+ }
+
+ if (mj2_read_stsd(tk, img, cio))
+ return 1;
+ if (mj2_read_stts(tk, cio))
+ return 1;
+ if (mj2_read_stsc(tk, cio))
+ return 1;
+ if (mj2_read_stsz(tk, cio))
+ return 1;
+ if (mj2_read_stco(tk, cio))
+ return 1;
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with STBL Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the URL box
+*
+* URL box
+*
+*/
+void mj2_write_url(mj2_tk_t * tk, int url_num, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_URL, 4); /* URL */
+
+ if (url_num == 0)
+ cio_write(cio, 1, 4); /* Version = 0, flags = 1 because stored in same file */
+ else {
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+ cio_write(cio, tk->url[url_num - 1].location[0], 4);
+ cio_write(cio, tk->url[url_num - 1].location[1], 4);
+ cio_write(cio, tk->url[url_num - 1].location[2], 4);
+ cio_write(cio, tk->url[url_num - 1].location[3], 4);
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the URL box
+*
+* URL box
+*
+*/
+int mj2_read_url(mj2_tk_t * tk, int urn_num, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_URL != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected URL Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in URL box\n");
+ return 1;
+ }
+
+ if (1 != cio_read(cio, 3)) { /* If flags = 1 --> media data in file */
+ tk->url[urn_num].location[0] = cio_read(cio, 4);
+ tk->url[urn_num].location[1] = cio_read(cio, 4);
+ tk->url[urn_num].location[2] = cio_read(cio, 4);
+ tk->url[urn_num].location[3] = cio_read(cio, 4);
+ } else {
+ tk->num_url--;
+ }
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with URL Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the URN box
+*
+* URN box
+*
+*/
+void mj2_write_urn(mj2_tk_t * tk, int urn_num, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_URN, 4); /* URN */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->urn[urn_num].name[0], 4);
+ cio_write(cio, tk->urn[urn_num].name[1], 4);
+ cio_write(cio, tk->urn[urn_num].name[2], 4);
+ cio_write(cio, tk->urn[urn_num].name[3], 4);
+ cio_write(cio, tk->urn[urn_num].location[0], 4);
+ cio_write(cio, tk->urn[urn_num].location[1], 4);
+ cio_write(cio, tk->urn[urn_num].location[2], 4);
+ cio_write(cio, tk->urn[urn_num].location[3], 4);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the URN box
+*
+* URN box
+*
+*/
+int mj2_read_urn(mj2_tk_t * tk, int urn_num, opj_cio_t *cio)
+{
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_URN != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected URN Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in URN box\n");
+ return 1;
+ }
+
+ if (1 != cio_read(cio, 3)) { /* If flags = 1 --> media data in file */
+ tk->urn[urn_num].name[0] = cio_read(cio, 4);
+ tk->urn[urn_num].name[1] = cio_read(cio, 4);
+ tk->urn[urn_num].name[2] = cio_read(cio, 4);
+ tk->urn[urn_num].name[3] = cio_read(cio, 4);
+ tk->urn[urn_num].location[0] = cio_read(cio, 4);
+ tk->urn[urn_num].location[1] = cio_read(cio, 4);
+ tk->urn[urn_num].location[2] = cio_read(cio, 4);
+ tk->urn[urn_num].location[3] = cio_read(cio, 4);
+ }
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with URN Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+
+/*
+* Write the DREF box
+*
+* Data reference box
+*
+*/
+void mj2_write_dref(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_DREF, 4); /* DREF */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ if (tk->num_url + tk->num_urn == 0) { /* Media data in same file */
+ cio_write(cio, 1, 4); /* entry_count = 1 */
+ mj2_write_url(tk, 0, cio);
+ } else {
+ cio_write(cio, tk->num_url + tk->num_urn, 4); /* entry_count */
+
+ for (i = 0; i < tk->num_url; i++)
+ mj2_write_url(tk, i + 1, cio);
+
+ for (i = 0; i < tk->num_urn; i++)
+ mj2_write_urn(tk, i, cio);
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the DREF box
+*
+* Data reference box
+*
+*/
+int mj2_read_dref(mj2_tk_t * tk, opj_cio_t *cio)
+{
+
+ int i;
+ int entry_count, marker;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_DREF != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected DREF Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in DREF box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in DREF box. Expected flag 0\n");
+ return 1;
+ }
+
+ entry_count = cio_read(cio, 4);
+ tk->num_url = 0;
+ tk->num_urn = 0;
+
+ for (i = 0; i < entry_count; i++) {
+ cio_skip(cio,4);
+ marker = cio_read(cio, 4);
+ if (marker == MJ2_URL) {
+ cio_skip(cio,-8);
+ tk->num_url++;
+ if (mj2_read_url(tk, tk->num_url, cio))
+ return 1;
+ } else if (marker == MJ2_URN) {
+ cio_skip(cio,-8);
+ tk->num_urn++;
+ if (mj2_read_urn(tk, tk->num_urn, cio))
+ return 1;
+ } else {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with in DREF box. Expected URN or URL box\n");
+ return 1;
+ }
+
+ }
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with DREF Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the DINF box
+*
+* Data information box
+*
+*/
+void mj2_write_dinf(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_DINF, 4); /* DINF */
+
+ mj2_write_dref(tk, cio);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the DINF box
+*
+* Data information box
+*
+*/
+int mj2_read_dinf(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_DINF != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected DINF Marker\n");
+ return 1;
+ }
+
+ if (mj2_read_dref(tk, cio))
+ return 1;
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with DINF Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the VMHD box
+*
+* Video Media information box
+*
+*/
+void mj2_write_vmhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_VMHD, 4); /* VMHD */
+
+ cio_write(cio, 1, 4); /* Version = 0, flags = 1 */
+
+ cio_write(cio, tk->graphicsmode, 2);
+ cio_write(cio, tk->opcolor[0], 2);
+ cio_write(cio, tk->opcolor[1], 2);
+ cio_write(cio, tk->opcolor[2], 2);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the VMHD box
+*
+* Video Media information box
+*
+*/
+int mj2_read_vmhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_VMHD != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected VMHD Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in VMHD box\n");
+ return 1;
+ }
+
+ if (1 != cio_read(cio, 3)) { /* Flags = 1 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in VMHD box. Expected flag 1\n");
+ return 1;
+ }
+
+ tk->track_type = 0;
+ tk->graphicsmode = cio_read(cio, 2);
+ tk->opcolor[0] = cio_read(cio, 2);
+ tk->opcolor[1] = cio_read(cio, 2);
+ tk->opcolor[2] = cio_read(cio, 2);
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with VMHD Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the SMHD box
+*
+* Sound Media information box
+*
+*/
+void mj2_write_smhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_SMHD, 4); /* SMHD */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->balance, 2);
+
+ cio_write(cio, 0, 2); /* Reserved */
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the SMHD box
+*
+* Sound Media information box
+*
+*/
+int mj2_read_smhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_SMHD != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected SMHD Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in SMHD box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in SMHD box. Expected flag 0\n");
+ return 1;
+ }
+
+ tk->track_type = 1;
+ tk->balance = cio_read(cio, 2);
+
+ /* Init variables to zero to avoid problems when freeeing memory
+ The values will possibly be overidded when decoding the track structure */
+ tk->num_br = 0;
+ tk->num_url = 0;
+ tk->num_urn = 0;
+ tk->num_chunks = 0;
+ tk->num_tts = 0;
+ tk->num_samplestochunk = 0;
+ tk->num_samples = 0;
+
+ cio_skip(cio,2); /* Reserved */
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with SMHD Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the HMHD box
+*
+* Hint Media information box
+*
+*/
+void mj2_write_hmhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_HMHD, 4); /* HMHD */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->maxPDUsize, 2);
+ cio_write(cio, tk->avgPDUsize, 2);
+ cio_write(cio, tk->maxbitrate, 4);
+ cio_write(cio, tk->avgbitrate, 4);
+ cio_write(cio, tk->slidingavgbitrate, 4);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the HMHD box
+*
+* Hint Media information box
+*
+*/
+int mj2_read_hmhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_HMHD != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected HMHD Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in HMHD box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in HMHD box. Expected flag 0\n");
+ return 1;
+ }
+
+ tk->track_type = 2;
+ tk->maxPDUsize = cio_read(cio, 2);
+ tk->avgPDUsize = cio_read(cio, 2);
+ tk->maxbitrate = cio_read(cio, 4);
+ tk->avgbitrate = cio_read(cio, 4);
+ tk->slidingavgbitrate = cio_read(cio, 4);
+
+ /* Init variables to zero to avoid problems when freeeing memory
+ The values will possibly be overidded when decoding the track structure */
+ tk->num_br = 0;
+ tk->num_url = 0;
+ tk->num_urn = 0;
+ tk->num_chunks = 0;
+ tk->num_tts = 0;
+ tk->num_samplestochunk = 0;
+ tk->num_samples = 0;
+
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with HMHD Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the MINF box
+*
+* Media information box
+*
+*/
+void mj2_write_minf(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_MINF, 4); /* MINF */
+
+ if (tk->track_type == 0) {
+ mj2_write_vmhd(tk, cio);
+ } else if (tk->track_type == 1) {
+ mj2_write_smhd(tk, cio);
+ } else if (tk->track_type == 2) {
+ mj2_write_hmhd(tk, cio);
+ }
+
+ mj2_write_dinf(tk, cio);
+ mj2_write_stbl(tk, cio);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the MINF box
+*
+* Media information box
+*
+*/
+int mj2_read_minf(mj2_tk_t * tk, opj_image_t * img, opj_cio_t *cio)
+{
+
+ unsigned int box_type;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_MINF != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected MINF Marker\n");
+ return 1;
+ }
+
+ cio_skip(cio,4);
+ box_type = cio_read(cio, 4);
+ cio_skip(cio,-8);
+
+ if (box_type == MJ2_VMHD) {
+ if (mj2_read_vmhd(tk, cio))
+ return 1;
+ } else if (box_type == MJ2_SMHD) {
+ if (mj2_read_smhd(tk, cio))
+ return 1;
+ } else if (box_type == MJ2_HMHD) {
+ if (mj2_read_hmhd(tk, cio))
+ return 1;
+ } else {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error in MINF box expected vmhd, smhd or hmhd\n");
+ return 1;
+ }
+
+ if (mj2_read_dinf(tk, cio))
+ return 1;
+
+ if (mj2_read_stbl(tk, img, cio))
+ return 1;
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with MINF Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the HDLR box
+*
+* Handler reference box
+*
+*/
+void mj2_write_hdlr(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_HDLR, 4); /* HDLR */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, 0, 4); /* Predefine */
+
+ tk->name = 0; /* The track name is immediately determined by the track type */
+
+ if (tk->track_type == 0) {
+ tk->handler_type = 0x76696465; /* Handler type: vide */
+ cio_write(cio, tk->handler_type, 4);
+
+ cio_write(cio, 0, 4);
+ cio_write(cio, 0, 4);
+ cio_write(cio, 0, 4); /* Reserved */
+
+ cio_write(cio, 0x76696465, 4);
+ cio_write(cio, 0x6F206d65, 4);
+ cio_write(cio, 0x64696120, 4);
+ cio_write(cio, 0x74726163, 4);
+ cio_write(cio, 0x6b00, 2); /* String: video media track */
+ } else if (tk->track_type == 1) {
+ tk->handler_type = 0x736F756E; /* Handler type: soun */
+ cio_write(cio, tk->handler_type, 4);
+
+ cio_write(cio, 0, 4);
+ cio_write(cio, 0, 4);
+ cio_write(cio, 0, 4); /* Reserved */
+
+ cio_write(cio, 0x536F756E, 4);
+ cio_write(cio, 0x6400, 2); /* String: Sound */
+ } else if (tk->track_type == 2) {
+ tk->handler_type = 0x68696E74; /* Handler type: hint */
+ cio_write(cio, tk->handler_type, 4);
+
+ cio_write(cio, 0, 4);
+ cio_write(cio, 0, 4);
+ cio_write(cio, 0, 4); /* Reserved */
+
+ cio_write(cio, 0x48696E74, 4);
+ cio_write(cio, 0, 2); /* String: Hint */
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the HDLR box
+*
+* Handler reference box
+*
+*/
+int mj2_read_hdlr(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_HDLR != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected HDLR Marker\n");
+ return 1;
+ }
+
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in HDLR box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in HDLR box. Expected flag 0\n");
+ return 1;
+ }
+
+ cio_skip(cio,4); /* Reserved */
+
+ tk->handler_type = cio_read(cio, 4);
+ cio_skip(cio,12); /* Reserved */
+
+ tk->name_size = box.length - 32;
+
+ tk->name = (char *) malloc(tk->name_size * sizeof(char));
+ for (i = 0; i < tk->name_size; i++) {
+ tk->name[i] = cio_read(cio, 1); /* Name */
+ }
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with HDLR Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the MDHD box
+*
+* Media Header Box
+*
+*/
+void mj2_write_mdhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+ unsigned int i;
+ time_t ltime;
+ unsigned int modification_time;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_MDHD, 4); /* MDHD */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ cio_write(cio, tk->creation_time, 4); /* Creation Time */
+
+ time(<ime); /* Time since 1/1/70 */
+ modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
+
+ cio_write(cio, modification_time, 4); /* Modification Time */
+
+ cio_write(cio, tk->timescale, 4); /* Timescale */
+
+ tk->duration = 0;
+
+ for (i = 0; i < tk->num_samples; i++)
+ tk->duration += tk->sample[i].sample_delta;
+
+ cio_write(cio, tk->duration, 4); /* Duration */
+
+ cio_write(cio, tk->language, 2); /* Language */
+
+ cio_write(cio, 0, 2); /* Predefined */
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the MDHD box
+*
+* Media Header Box
+*
+*/
+int mj2_read_mdhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (!(MJ2_MHDR == box.type || MJ2_MDHD == box.type)) { // Kakadu writes MHDR instead of MDHD
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected MDHD Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in MDHD box\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 3)) { /* Flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with flag in MDHD box. Expected flag 0\n");
+ return 1;
+ }
+
+
+ tk->creation_time = cio_read(cio, 4); /* Creation Time */
+
+ tk->modification_time = cio_read(cio, 4); /* Modification Time */
+
+ tk->timescale = cio_read(cio, 4); /* Timescale */
+
+ tk->duration = cio_read(cio, 4); /* Duration */
+
+ tk->language = cio_read(cio, 2); /* Language */
+
+ cio_skip(cio,2); /* Predefined */
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with MDHD Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the MDIA box
+*
+* Media box
+*
+*/
+void mj2_write_mdia(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_MDIA, 4); /* MDIA */
+
+ mj2_write_mdhd(tk, cio);
+ mj2_write_hdlr(tk, cio);
+ mj2_write_minf(tk, cio);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the MDIA box
+*
+* Media box
+*
+*/
+int mj2_read_mdia(mj2_tk_t * tk, opj_image_t * img, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_MDIA != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected MDIA Marker\n");
+ return 1;
+ }
+
+ if (mj2_read_mdhd(tk, cio))
+ return 1;
+ if (mj2_read_hdlr(tk, cio))
+ return 1;
+ if (mj2_read_minf(tk, img, cio))
+ return 1;
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with MDIA Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the TKHD box
+*
+* Track Header box
+*
+*/
+void mj2_write_tkhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+ unsigned int i;
+ time_t ltime;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+
+ cio_write(cio, MJ2_TKHD, 4); /* TKHD */
+
+ cio_write(cio, 3, 4); /* Version=0, flags=3 */
+
+ time(<ime); /* Time since 1/1/70 */
+ tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
+
+ cio_write(cio, tk->creation_time, 4); /* Creation Time */
+
+ cio_write(cio, tk->modification_time, 4); /* Modification Time */
+
+ cio_write(cio, tk->track_ID, 4); /* Track ID */
+
+ cio_write(cio, 0, 4); /* Reserved */
+
+ tk->duration = 0;
+
+ for (i = 0; i < tk->num_samples; i++)
+ tk->duration += tk->sample[i].sample_delta;
+
+ cio_write(cio, tk->duration, 4); /* Duration */
+
+ cio_write(cio, 0, 4); /* Reserved */
+ cio_write(cio, 0, 4); /* Reserved */
+
+ cio_write(cio, tk->layer, 2); /* Layer */
+
+ cio_write(cio, 0, 2); /* Predefined */
+
+ cio_write(cio, tk->volume, 2); /* Volume */
+
+ cio_write(cio, 0, 2); /* Reserved */
+
+ cio_write(cio, tk->trans_matrix[0], 4); /* Transformation matrix for track */
+ cio_write(cio, tk->trans_matrix[1], 4);
+ cio_write(cio, tk->trans_matrix[2], 4);
+ cio_write(cio, tk->trans_matrix[3], 4);
+ cio_write(cio, tk->trans_matrix[4], 4);
+ cio_write(cio, tk->trans_matrix[5], 4);
+ cio_write(cio, tk->trans_matrix[6], 4);
+ cio_write(cio, tk->trans_matrix[7], 4);
+ cio_write(cio, tk->trans_matrix[8], 4);
+
+ cio_write(cio, tk->visual_w, 4); /* Video Visual Width */
+
+ cio_write(cio, tk->visual_h, 4); /* Video Visual Height */
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the TKHD box
+*
+* Track Header box
+*
+*/
+int mj2_read_tkhd(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ int flag;
+
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+
+ if (MJ2_TKHD != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected TKHD Marker\n");
+ return 1;
+ }
+
+ if (0 != cio_read(cio, 1)) { /* Version = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in TKHD box\n");
+ return 1;
+ }
+
+ flag = cio_read(cio, 3);
+
+ if (!(flag == 1 || flag == 2 || flag == 3 || flag == 4)) { /* Flags = 1,2,3 or 4 */
+ opj_event_msg(cio->cinfo, EVT_ERROR,
+ "Error with flag in TKHD box: Expected flag 1,2,3 or 4\n");
+ return 1;
+ }
+
+ tk->creation_time = cio_read(cio, 4); /* Creation Time */
+
+ tk->modification_time = cio_read(cio, 4); /* Modification Time */
+
+ tk->track_ID = cio_read(cio, 4); /* Track ID */
+
+ cio_skip(cio,4); /* Reserved */
+
+ tk->duration = cio_read(cio, 4); /* Duration */
+
+ cio_skip(cio,8); /* Reserved */
+
+ tk->layer = cio_read(cio, 2); /* Layer */
+
+ cio_read(cio, 2); /* Predefined */
+
+ tk->volume = cio_read(cio, 2); /* Volume */
+
+ cio_skip(cio,2); /* Reserved */
+
+ tk->trans_matrix[0] = cio_read(cio, 4); /* Transformation matrix for track */
+ tk->trans_matrix[1] = cio_read(cio, 4);
+ tk->trans_matrix[2] = cio_read(cio, 4);
+ tk->trans_matrix[3] = cio_read(cio, 4);
+ tk->trans_matrix[4] = cio_read(cio, 4);
+ tk->trans_matrix[5] = cio_read(cio, 4);
+ tk->trans_matrix[6] = cio_read(cio, 4);
+ tk->trans_matrix[7] = cio_read(cio, 4);
+ tk->trans_matrix[8] = cio_read(cio, 4);
+
+ tk->visual_w = cio_read(cio, 4); /* Video Visual Width */
+
+ tk->visual_h = cio_read(cio, 4); /* Video Visual Height */
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with TKHD Box size\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the TRAK box
+*
+* Track box
+*
+*/
+void mj2_write_trak(mj2_tk_t * tk, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+
+ cio_write(cio, MJ2_TRAK, 4); /* TRAK */
+
+ mj2_write_tkhd(tk, cio);
+ mj2_write_mdia(tk, cio);
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the TRAK box
+*
+* Track box
+*
+*/
+int mj2_read_trak(mj2_tk_t * tk, opj_image_t * img, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_TRAK != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected TRAK Marker\n");
+ return 1;
+ }
+ if (mj2_read_tkhd(tk, cio))
+ return 1;
+ if (mj2_read_mdia(tk, img, cio))
+ return 1;
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with TRAK Box\n");
+ return 1;
+ }
+ return 0;
+}
+
+/*
+* Write the MVHD box
+*
+* Movie header Box
+*
+*/
+void mj2_write_mvhd(opj_mj2_t * movie, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+ unsigned j;
+ time_t ltime;
+ int max_tk_num = 0;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_MVHD, 4); /* MVHD */
+
+ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
+
+ time(<ime); /* Time since 1/1/70 */
+ movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
+
+ cio_write(cio, movie->creation_time, 4); /* Creation Time */
+
+ cio_write(cio, movie->modification_time, 4); /* Modification Time */
+
+ cio_write(cio, movie->timescale, 4); /* Timescale */
+
+ movie->duration = 0;
+
+ for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
+ mj2_tk_t *tk = &movie->tk[i];
+
+ for (j = 0; j < tk->num_samples; j++) {
+ movie->duration += tk->sample[j].sample_delta;
+ }
+ }
+
+ cio_write(cio, movie->duration, 4);
+
+ cio_write(cio, movie->rate, 4); /* Rate to play presentation */
+
+ cio_write(cio, movie->volume, 2); /* Volume */
+
+ cio_write(cio, 0, 2); /* Reserved */
+ cio_write(cio, 0, 4); /* Reserved */
+ cio_write(cio, 0, 4); /* Reserved */
+
+ cio_write(cio, movie->trans_matrix[0], 4); /* Transformation matrix for video */
+ cio_write(cio, movie->trans_matrix[1], 4);
+ cio_write(cio, movie->trans_matrix[2], 4);
+ cio_write(cio, movie->trans_matrix[3], 4);
+ cio_write(cio, movie->trans_matrix[4], 4);
+ cio_write(cio, movie->trans_matrix[5], 4);
+ cio_write(cio, movie->trans_matrix[6], 4);
+ cio_write(cio, movie->trans_matrix[7], 4);
+ cio_write(cio, movie->trans_matrix[8], 4);
+
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+ cio_write(cio, 0, 4); /* Pre-defined */
+
+
+ for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
+ if (max_tk_num < movie->tk[i].track_ID)
+ max_tk_num = movie->tk[i].track_ID;
+ }
+
+ movie->next_tk_id = max_tk_num + 1;
+
+ cio_write(cio, movie->next_tk_id, 4); /* ID of Next track to be added */
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the MVHD box
+*
+* Movie header Box
+*
+*/
+int mj2_read_mvhd(opj_mj2_t * movie, opj_cio_t *cio)
+{
+ mj2_box_t box;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_MVHD != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected MVHD Marker\n");
+ return 1;
+ }
+
+
+ if (0 != cio_read(cio, 4)) { /* Version = 0, flags = 0 */
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Only Version 0 handled in MVHD box\n");
+ }
+
+ movie->creation_time = cio_read(cio, 4); /* Creation Time */
+
+ movie->modification_time = cio_read(cio, 4); /* Modification Time */
+
+ movie->timescale = cio_read(cio, 4); /* Timescale */
+
+ movie->duration = cio_read(cio, 4); /* Duration */
+
+ movie->rate = cio_read(cio, 4); /* Rate to play presentation */
+
+ movie->volume = cio_read(cio, 2); /* Volume */
+
+ cio_skip(cio,10); /* Reserved */
+
+ movie->trans_matrix[0] = cio_read(cio, 4); /* Transformation matrix for video */
+ movie->trans_matrix[1] = cio_read(cio, 4);
+ movie->trans_matrix[2] = cio_read(cio, 4);
+ movie->trans_matrix[3] = cio_read(cio, 4);
+ movie->trans_matrix[4] = cio_read(cio, 4);
+ movie->trans_matrix[5] = cio_read(cio, 4);
+ movie->trans_matrix[6] = cio_read(cio, 4);
+ movie->trans_matrix[7] = cio_read(cio, 4);
+ movie->trans_matrix[8] = cio_read(cio, 4);
+
+ cio_skip(cio,24); /* Pre-defined */
+
+ movie->next_tk_id = cio_read(cio, 4); /* ID of Next track to be added */
+
+ if (cio_tell(cio) - box.init_pos != box.length) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with MVHD Box Size\n");
+ return 1;
+ }
+ return 0;
+}
+
+
+/*
+* Write the MOOV box
+*
+* Movie Box
+*
+*/
+void mj2_write_moov(opj_mj2_t * movie, opj_cio_t *cio)
+{
+ int i;
+ mj2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio,4);
+ cio_write(cio, MJ2_MOOV, 4); /* MOOV */
+
+ mj2_write_mvhd(movie, cio);
+
+ for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
+ mj2_write_trak(&movie->tk[i], cio);
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+
+/*
+* Read the MOOV box
+*
+* Movie Box
+*
+*/
+int mj2_read_moov(opj_mj2_t * movie, opj_image_t * img, opj_cio_t *cio)
+{
+ unsigned int i;
+ mj2_box_t box;
+ mj2_box_t box2;
+
+ mj2_read_boxhdr(&box, cio);
+ if (MJ2_MOOV != box.type) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error: Expected MOOV Marker\n");
+ return 1;
+ }
+
+ if (mj2_read_mvhd(movie, cio))
+ return 1;
+
+ movie->tk =
+ (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
+
+ for (i = 0; cio_tell(cio) - box.init_pos < box.length; i++) {
+ mj2_tk_t *tk = &movie->tk[i];
+ tk->cinfo = movie->cinfo;
+ mj2_read_boxhdr(&box2, cio);
+ if (box2.type == MJ2_TRAK) {
+ cio_seek(cio, box2.init_pos);
+ if (mj2_read_trak(tk, img, cio))
+ return 1;
+
+ if (tk->track_type == 0) {
+ movie->num_vtk++;
+ } else if (tk->track_type == 1) {
+ movie->num_stk++;
+ } else if (tk->track_type == 2) {
+ movie->num_htk++;
+ }
+ } else if (box2.type == MJ2_MVEX) {
+ cio_seek(cio, box2.init_pos);
+ cio_skip(cio,box2.length);
+ i--;
+ } else {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Error with MOOV Box: Expected TRAK or MVEX box\n");
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int mj2_read_struct(FILE *file, opj_mj2_t *movie) {
+ mj2_box_t box;
+ opj_image_t img;
+ char * src;
+ int fsresult;
+ int foffset;
+ opj_cio_t *cio;
+
+ /* open a byte stream for reading */
+ src = (char*) malloc (300 * sizeof(char));
+
+ /* Assuming that jp and ftyp markers size do
+ not exceed 300 bytes */
+ fread(src,300,1, file);
+
+ cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, 300);
+
+ if (mj2_read_jp(cio))
+ return 1;
+ if (mj2_read_ftyp(movie, cio))
+ return 1;
+
+ fsresult = fseek(file,cio_tell(cio),SEEK_SET);
+ if( fsresult ) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read data after FTYP box\n" );
+ return 1;
+ }
+
+ foffset = cio_tell(cio);
+
+ box.type = 0;
+
+ fread(src,30,1,file);
+ cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, 300);
+ mj2_read_boxhdr(&box, cio);
+
+ while(box.type != MJ2_MOOV) {
+
+ switch(box.type)
+ {
+ case MJ2_MDAT:
+ fsresult = fseek(file,foffset+box.length,SEEK_SET);
+ if( fsresult ) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read MDAT box\n" );
+ return 1;
+ }
+ foffset += box.length;
+ break;
+
+ case MJ2_MOOF:
+ fsresult = fseek(file,foffset+box.length,SEEK_SET);
+ if( fsresult ) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read MOOF box\n" );
+ return 1;
+ }
+ foffset += box.length;
+ break;
+ case MJ2_FREE:
+ fsresult = fseek(file,foffset+box.length,SEEK_SET);
+ if( fsresult ) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read FREE box\n" );
+ return 1;
+ }
+ foffset += box.length;
+ break;
+ case MJ2_SKIP:
+ fsresult = fseek(file,foffset+box.length,SEEK_SET);
+ if( fsresult ) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read SKIP box\n" );
+ return 1;
+ }
+ foffset += box.length;
+ break;
+ default:
+ opj_event_msg(cio->cinfo, EVT_ERROR, "Unknown box in MJ2 stream\n");
+ fsresult = fseek(file,foffset+box.length,SEEK_SET);
+ if( fsresult ) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read end of unknown box\n");
+ return 1;
+ }
+ foffset += box.length;
+ break;
+ }
+ fsresult = fread(src,8,1,file);
+ if (fsresult != 1) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "MOOV box not found in file\n");
+ return 1;
+ }
+ cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, 8);
+ mj2_read_boxhdr(&box, cio);
+ }
+
+ fseek(file,foffset,SEEK_SET);
+ src = realloc(src,box.length);
+ fsresult = fread(src,box.length,1,file);
+ if (fsresult != 1) {
+ opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read MOOV box\n");
+ return 1;
+ }
+
+ cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, box.length);
+
+ if (mj2_read_moov(movie, &img, cio))
+ return 1;
+
+ free(src);
+ return 0;
+}
+
+/* ----------------------------------------------------------------------- */
+/* MJ2 decoder interface */
+/* ----------------------------------------------------------------------- */
+
+opj_dinfo_t* mj2_create_decompress() {
+ opj_mj2_t* mj2;
+ opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t));
+ if(!dinfo) return NULL;
+
+ dinfo->is_decompressor = true;
+
+ mj2 = (opj_mj2_t*)opj_malloc(sizeof(opj_mj2_t));
+ dinfo->mj2_handle = mj2;
+ if(mj2) {
+ mj2->cinfo = (opj_common_ptr)dinfo;
+ }
+ mj2->j2k = j2k_create_decompress((opj_common_ptr)dinfo);
+ dinfo->j2k_handle = mj2->j2k;
+
+ return dinfo;
+}
+
+void mj2_setup_decoder(opj_mj2_t *movie, mj2_dparameters_t *mj2_parameters) {
+ movie->num_vtk=0;
+ movie->num_stk=0;
+ movie->num_htk=0;
+ /* setup the J2K decoder parameters */
+ j2k_setup_decoder(movie->cinfo->j2k_handle, &mj2_parameters->j2k_parameters);
+}
+
+void mj2_destroy_decompress(opj_mj2_t *movie) {
+ if(movie) {
+ int i;
+ mj2_tk_t *tk=NULL;
+
+ if (movie->cinfo->j2k_handle) {
+ j2k_destroy_compress(movie->j2k);
+ }
+
+ if (movie->num_cl != 0)
+ opj_free(movie->cl);
+
+ for (i = 0; i < movie->num_vtk + movie->num_stk + movie->num_htk; i++) {
+ tk = &movie->tk[i];
+ if (tk->name_size != 0)
+ opj_free(tk->name);
+ if (tk->track_type == 0) {// Video track
+ if (tk->jp2_struct.comps != 0)
+ opj_free(tk->jp2_struct.comps);
+ if (tk->jp2_struct.cl != 0)
+ opj_free(tk->jp2_struct.cl);
+ if (tk->num_jp2x != 0)
+ opj_free(tk->jp2xdata);
+
+ }
+ if (tk->num_url != 0)
+ opj_free(tk->url);
+ if (tk->num_urn != 0)
+ opj_free(tk->urn);
+ if (tk->num_br != 0)
+ opj_free(tk->br);
+ if (tk->num_tts != 0)
+ opj_free(tk->tts);
+ if (tk->num_chunks != 0)
+ opj_free(tk->chunk);
+ if (tk->num_samplestochunk != 0)
+ opj_free(tk->sampletochunk);
+ if (tk->num_samples != 0)
+ opj_free(tk->sample);
+ }
+
+ opj_free(movie->tk);
+ }
+ opj_free(movie);
+}
+
+/* ----------------------------------------------------------------------- */
+/* MJ2 encoder interface */
+/* ----------------------------------------------------------------------- */
+
+
+opj_cinfo_t* mj2_create_compress() {
+ opj_mj2_t* mj2;
+ opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
+ if(!cinfo) return NULL;
+
+ mj2 = (opj_mj2_t*)opj_malloc(sizeof(opj_mj2_t));
+ cinfo->mj2_handle = mj2;
+ if(mj2) {
+ mj2->cinfo = (opj_common_ptr)cinfo;
+ }
+
+ mj2->j2k = j2k_create_compress(mj2->cinfo);
+ cinfo->j2k_handle = mj2->j2k;
+
+ return cinfo;
+}
+
+void mj2_setup_encoder(opj_mj2_t *movie, mj2_cparameters_t *parameters) {
+ if(movie && parameters) {
+ opj_jp2_t *jp2_struct;
+
+ movie->num_htk = 0; // No hint tracks
+ movie->num_stk = 0; // No sound tracks
+ movie->num_vtk = 1; // One video track
+
+ 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
+
+ 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->tk[0].Dim[0] = parameters->Dim[0];
+ movie->tk[0].Dim[1] = parameters->Dim[1];
+ movie->tk[0].w = parameters->w;
+ movie->tk[0].h = parameters->h;
+ movie->tk[0].CbCr_subsampling_dx = parameters->CbCr_subsampling_dx;
+ movie->tk[0].CbCr_subsampling_dy = parameters->CbCr_subsampling_dy;
+ movie->tk[0].sample_rate = parameters->frame_rate;
+
+ jp2_struct = &movie->tk[0].jp2_struct;
+ jp2_struct->numcomps = 3; // NC
+ jp2_struct->comps =
+ (opj_jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(opj_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*/
+ jp2_struct->w = parameters->w;
+ jp2_struct->h = parameters->h;
+ jp2_struct->bpc = 7;
+ jp2_struct->meth = 1;
+ jp2_struct->enumcs = 18; // YUV
+ }
+}
+
+void mj2_destroy_compress(opj_mj2_t *movie) {
+ if(movie) {
+ int i;
+ mj2_tk_t *tk=NULL;
+
+ if (movie->cinfo->j2k_handle) {
+ j2k_destroy_compress(movie->j2k);
+ }
+
+ if (movie->num_cl != 0)
+ opj_free(movie->cl);
+
+ for (i = 0; i < movie->num_vtk + movie->num_stk + movie->num_htk; i++) {
+ tk = &movie->tk[i];
+ if (tk->name_size != 0)
+ opj_free(tk->name);
+ if (tk->track_type == 0) {// Video track
+ if (tk->jp2_struct.comps != 0)
+ opj_free(tk->jp2_struct.comps);
+ if (tk->jp2_struct.cl != 0)
+ opj_free(tk->jp2_struct.cl);
+ if (tk->num_jp2x != 0)
+ opj_free(tk->jp2xdata);
+
+ }
+ if (tk->num_url != 0)
+ opj_free(tk->url);
+ if (tk->num_urn != 0)
+ opj_free(tk->urn);
+ if (tk->num_br != 0)
+ opj_free(tk->br);
+ if (tk->num_tts != 0)
+ opj_free(tk->tts);
+ if (tk->num_chunks != 0)
+ opj_free(tk->chunk);
+ if (tk->num_samplestochunk != 0)
+ opj_free(tk->sampletochunk);
+ if (tk->num_samples != 0)
+ opj_free(tk->sample);
+ }
+
+ opj_free(movie->tk);
+ }
+ opj_free(movie);
+}
--- /dev/null
+/*
+* 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 */
-#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>
/* ----------------------- */
/* */
/* */
/* ----------------------- */
-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 */
-
+
fseek(f, 0, SEEK_END);
end_of_f = ftell(f); /* Calculate file size */
-
+
if (end_of_f < frame_size) {
fprintf(stderr,
- "YUV does not contains any frame of %d x %d size\n", tk->w,
- tk->h);
+ "YUV does not contains any frame of %d x %d size\n", tk->w,
+ tk->h);
return 0;
}
-
+
numimages = end_of_f / frame_size; /* Calculate number of images */
+ fclose(f);
return numimages;
- fclose(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)
+{
+ 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, j;
+ 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;
+
+ 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)));
+ 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);
position = ftell(yuvfile);
if (position >= end_of_f) {
fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n",
- frame_num);
- return 0;
+ frame_num);
+ fclose(yuvfile);
+ return false;
}
-
+
img->x0 = tk->Dim[0];
img->y0 = tk->Dim[1];
img->x1 = !tk->Dim[0] ? (tk->w - 1) * subsampling_dx + 1 : tk->Dim[0] +
(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))
- && !feof(yuvfile); i++) {
- unsigned char y;
- j = fread(&y, 1, 1, yuvfile);
- fwrite(&y, 1, 1, Compo);
- }
-
- 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(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;
+
+ 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++) {
+ if (!fread(&img->comps[compno].data[i], 1, 1, yuvfile)) {
+ fprintf(stderr, "Error reading %s file !!\n", infile);
+ return false;
+ }
+ }
+ }
+ fclose(yuvfile);
+
+ return true;
}
// -----------------------
-int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
+bool imagetoyuv(opj_image_t * img, char *outfile)
{
FILE *f;
int i;
if (img->comps[0].dx != img->comps[1].dx / 2
|| img->comps[1].dx != img->comps[2].dx) {
fprintf(stderr,
- "Error with the input image components size: cannot create yuv file)\n");
- return 1;
+ "Error with the input image components size: cannot create yuv file)\n");
+ 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;
}
}
}
fclose(f);
- return 0;
+ return true;
}
// -----------------------
//
// -----------------------
-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;
fprintf(f, "%c%c%c", B, G, R);
if ((i + 1) % wr == 0) {
- for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
- fprintf(f, "%c", 0);
+ for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
+ fprintf(f, "%c", 0);
}
}
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;
}
--- /dev/null
+/*
+* 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
--- /dev/null
+/*
+* 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;
+}
# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
-# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /FR /YX /FD /c\r
# ADD BASE RSC /l 0x809 /d "NDEBUG"\r
# ADD RSC /l 0x809 /d "NDEBUG"\r
BSC32=bscmake.exe\r
# PROP Use_Debug_Libraries 1\r
# PROP Output_Dir "mj2_to_frames___Win32_Debug0"\r
# PROP Intermediate_Dir "mj2_to_frames___Win32_Debug0"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
-# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg_097" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
+# 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\r
# SUBTRACT CPP /X\r
# ADD BASE RSC /l 0x809 /d "_DEBUG"\r
# ADD RSC /l 0x809 /d "_DEBUG"\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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\r
\r
!ENDIF \r
\r
\r
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"\r
\r
-# ADD CPP /I "../libopenjpeg"\r
-\r
!ENDIF \r
\r
# End Source File\r
\r
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"\r
\r
-# ADD CPP /I "../libopenjpeg"\r
-\r
!ENDIF \r
\r
# End Source File\r
\r
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"\r
\r
-# ADD CPP /I "../libopenjpeg"\r
-\r
!ENDIF \r
\r
# End Source File\r
\r
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"\r
\r
-# ADD CPP /I "../libopenjpeg"\r
-\r
!ENDIF \r
\r
# End Source File\r
# End Group\r
# End Group\r
-# Begin Group "Libopenjpeg_097"\r
+# Begin Group "OpenJPEG Headers"\r
\r
# PROP Default_Filter ""\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\bio.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\bio.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\cio.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\cio.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\dwt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\dwt.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\fix.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\fix.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\int.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\int.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\j2k.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\j2k.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jp2.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jp2.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jpt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jpt.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mct.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mct.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mqc.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mqc.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\openjpeg.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\pi.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\pi.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\raw.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\raw.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t1.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t1.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t2.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t2.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tcd.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tcd.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tgt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tgt.h\r
-# End Source File\r
# End Group\r
# End Target\r
# End Project\r
--- /dev/null
+Microsoft Developer Studio Workspace File, Format Version 6.00\r
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r
+\r
+###############################################################################\r
+\r
+Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Project: "mj2_to_frames"=".\mj2_to_frames.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+ Begin Project Dependency\r
+ Project_Dep_Name LibOpenJPEG\r
+ End Project Dependency\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Global:\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<3>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
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);
+}
+
+/* -------------------------------------------------------------------------- */
+
+
/* ------------- */
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;
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 */
}
// 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, ¶meters);
+
+ 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;
}
# PROP Use_Debug_Libraries 0\r
# PROP Output_Dir "Release"\r
# PROP Intermediate_Dir "Release"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c\r
-# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg_097" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c\r
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "OPJ_STATIC" /YX /FD /c\r
# ADD BASE RSC /l 0x809 /d "NDEBUG"\r
# ADD RSC /l 0x809 /d "NDEBUG"\r
BSC32=bscmake.exe\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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"\r
+# SUBTRACT LINK32 /pdb:none\r
\r
!ELSEIF "$(CFG)" == "mj2_to_metadata - Win32 Debug"\r
\r
# PROP Use_Debug_Libraries 1\r
# PROP Output_Dir "mj2_to_metadata___Win32_Debug0"\r
# PROP Intermediate_Dir "mj2_to_metadata___Win32_Debug0"\r
+# PROP Ignore_Export_Lib 0\r
# PROP Target_Dir ""\r
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c\r
-# 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\r
+# 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\r
# ADD BASE RSC /l 0x809 /d "_DEBUG"\r
# ADD RSC /l 0x809 /d "_DEBUG"\r
BSC32=bscmake.exe\r
# ADD BSC32 /nologo\r
LINK32=link.exe\r
# 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\r
-# 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\r
+# 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\r
+# SUBTRACT LINK32 /pdb:none\r
\r
!ENDIF \r
\r
# End Source File\r
# End Group\r
# End Group\r
-# Begin Group "Libopenjpeg_097"\r
-\r
-# PROP Default_Filter ""\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\bio.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\bio.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\cio.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\cio.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\dwt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\dwt.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\fix.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\fix.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\int.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\int.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\j2k.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\j2k.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jp2.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jp2.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jpt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\jpt.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mct.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mct.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mqc.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\mqc.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\openjpeg.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\pi.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\pi.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\raw.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\raw.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t1.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t1.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t2.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\t2.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tcd.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tcd.h\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tgt.c\r
-# End Source File\r
-# Begin Source File\r
-\r
-SOURCE=..\libopenjpeg_097\tgt.h\r
-# End Source File\r
-# End Group\r
# End Target\r
# End Project\r
--- /dev/null
+Microsoft Developer Studio Workspace File, Format Version 6.00\r
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r
+\r
+###############################################################################\r
+\r
+Project: "LibOpenJPEG"="..\LibOpenJPEG.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Project: "mj2_to_metadata"=".\mj2_to_metadata.dsp" - Package Owner=<4>\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<4>\r
+{{{\r
+ Begin Project Dependency\r
+ Project_Dep_Name LibOpenJPEG\r
+ End Project Dependency\r
+}}}\r
+\r
+###############################################################################\r
+\r
+Global:\r
+\r
+Package=<5>\r
+{{{\r
+}}}\r
+\r
+Package=<3>\r
+{{{\r
+}}}\r
+\r
+###############################################################################\r
+\r
--- /dev/null
+/*
+ * 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, ¶meters);
+
+
+ /* 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;
+}