diff --git a/.gitignore b/.gitignore index 029173001c..b57db50e78 100644 --- a/.gitignore +++ b/.gitignore @@ -218,3 +218,14 @@ benchmarks/torch_data # API docs api_docs/ +cpp/.ext +docs/*.iml +docs/.venv/ +docs/env/ +docs/site/ +/docs/api-js/ +docs/*.DS_Store +docs/test.env +docs/.cache/ +python/build/* +python/MANIFEST.in diff --git a/cpp/3rd_party/CMakeLists.txt b/cpp/3rd_party/CMakeLists.txt new file mode 100644 index 0000000000..ef50301808 --- /dev/null +++ b/cpp/3rd_party/CMakeLists.txt @@ -0,0 +1,11 @@ +set(TIFF_LIBRARY tiff CACHE INTERNAL "") + +add_subdirectory(libtiff) +add_subdirectory(openjpeg) +add_subdirectory(sql-parser) +add_subdirectory(rnifti) +add_subdirectory(json) + +set(TIFF_INCLUDE_DIR "${${TIFF_LIBRARY}_SOURCE_DIR}" "${${TIFF_LIBRARY}_BINARY_DIR}" CACHE INTERNAL "") +set(3RD_PARTY_LIBS ${TIFF_LIBRARY} openjp2 hsql json PARENT_SCOPE) +set(3RD_PARTY_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/openjpeg/openjp2 ${CMAKE_CURRENT_SOURCE_DIR}/rnifti ${CMAKE_CURRENT_BINARY_DIR}/libtiff PARENT_SCOPE) diff --git a/cpp/3rd_party/cblas.h b/cpp/3rd_party/cblas.h new file mode 100644 index 0000000000..7f4fae6109 --- /dev/null +++ b/cpp/3rd_party/cblas.h @@ -0,0 +1,369 @@ +#ifndef CBLAS_H +#define CBLAS_H + +#include + +#ifdef __cplusplus +extern "C" { + /* Assume C declarations for C++ */ +#endif /* __cplusplus */ + +#define CBLAS_INDEX size_t + +#ifndef BFLOAT16 +#include +typedef uint16_t bfloat16; +#endif + + +typedef enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER; +typedef enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113, CblasConjNoTrans=114} CBLAS_TRANSPOSE; +typedef enum CBLAS_UPLO {CblasUpper=121, CblasLower=122} CBLAS_UPLO; +typedef enum CBLAS_DIAG {CblasNonUnit=131, CblasUnit=132} CBLAS_DIAG; +typedef enum CBLAS_SIDE {CblasLeft=141, CblasRight=142} CBLAS_SIDE; +typedef CBLAS_ORDER CBLAS_LAYOUT; + +float cblas_sdsdot(const int n, const float alpha, const float *x, const int incx, const float *y, const int incy); +double cblas_dsdot (const int n, const float *x, const int incx, const float *y, const int incy); +float cblas_sdot(const int n, const float *x, const int incx, const float *y, const int incy); +double cblas_ddot(const int n, const double *x, const int incx, const double *y, const int incy); + +void cblas_cdotu_sub(const int n, const void *x, const int incx, const void *y, const int incy, void *ret); +void cblas_cdotc_sub(const int n, const void *x, const int incx, const void *y, const int incy, void *ret); +void cblas_zdotu_sub(const int n, const void *x, const int incx, const void *y, const int incy, void *ret); +void cblas_zdotc_sub(const int n, const void *x, const int incx, const void *y, const int incy, void *ret); + +float cblas_sasum (const int n, const float *x, const int incx); +double cblas_dasum (const int n, const double *x, const int incx); +float cblas_scasum(const int n, const void *x, const int incx); +double cblas_dzasum(const int n, const void *x, const int incx); + +float cblas_ssum (const int n, const float *x, const int incx); +double cblas_dsum (const int n, const double *x, const int incx); +float cblas_scsum(const int n, const void *x, const int incx); +double cblas_dzsum(const int n, const void *x, const int incx); + +float cblas_snrm2 (const int N, const float *X, const int incX); +double cblas_dnrm2 (const int N, const double *X, const int incX); +float cblas_scnrm2(const int N, const void *X, const int incX); +double cblas_dznrm2(const int N, const void *X, const int incX); + +CBLAS_INDEX cblas_isamax(const int n, const float *x, const int incx); +CBLAS_INDEX cblas_idamax(const int n, const double *x, const int incx); +CBLAS_INDEX cblas_icamax(const int n, const void *x, const int incx); +CBLAS_INDEX cblas_izamax(const int n, const void *x, const int incx); + +CBLAS_INDEX cblas_isamin(const int n, const float *x, const int incx); +CBLAS_INDEX cblas_idamin(const int n, const double *x, const int incx); +CBLAS_INDEX cblas_icamin(const int n, const void *x, const int incx); +CBLAS_INDEX cblas_izamin(const int n, const void *x, const int incx); + +CBLAS_INDEX cblas_ismax(const int n, const float *x, const int incx); +CBLAS_INDEX cblas_idmax(const int n, const double *x, const int incx); +CBLAS_INDEX cblas_icmax(const int n, const void *x, const int incx); +CBLAS_INDEX cblas_izmax(const int n, const void *x, const int incx); + +CBLAS_INDEX cblas_ismin(const int n, const float *x, const int incx); +CBLAS_INDEX cblas_idmin(const int n, const double *x, const int incx); +CBLAS_INDEX cblas_icmin(const int n, const void *x, const int incx); +CBLAS_INDEX cblas_izmin(const int n, const void *x, const int incx); + +void cblas_saxpy(const int n, const float alpha, const float *x, const int incx, float *y, const int incy); +void cblas_daxpy(const int n, const double alpha, const double *x, const int incx, double *y, const int incy); +void cblas_caxpy(const int n, const void *alpha, const void *x, const int incx, void *y, const int incy); +void cblas_zaxpy(const int n, const void *alpha, const void *x, const int incx, void *y, const int incy); + +void cblas_scopy(const int n, const float *x, const int incx, float *y, const int incy); +void cblas_dcopy(const int n, const double *x, const int incx, double *y, const int incy); +void cblas_ccopy(const int n, const void *x, const int incx, void *y, const int incy); +void cblas_zcopy(const int n, const void *x, const int incx, void *y, const int incy); + +void cblas_sswap(const int n, float *x, const int incx, float *y, const int incy); +void cblas_dswap(const int n, double *x, const int incx, double *y, const int incy); +void cblas_cswap(const int n, void *x, const int incx, void *y, const int incy); +void cblas_zswap(const int n, void *x, const int incx, void *y, const int incy); + +void cblas_srot(const int N, float *X, const int incX, float *Y, const int incY, const float c, const float s); +void cblas_drot(const int N, double *X, const int incX, double *Y, const int incY, const double c, const double s); +void cblas_csrot(const int n, const void *x, const int incx, void *y, const int incY, const float c, const float s); +void cblas_zdrot(const int n, const void *x, const int incx, void *y, const int incY, const double c, const double s); + +void cblas_srotg(float *a, float *b, float *c, float *s); +void cblas_drotg(double *a, double *b, double *c, double *s); +void cblas_crotg(void *a, void *b, float *c, void *s); +void cblas_zrotg(void *a, void *b, double *c, void *s); + + +void cblas_srotm(const int N, float *X, const int incX, float *Y, const int incY, const float *P); +void cblas_drotm(const int N, double *X, const int incX, double *Y, const int incY, const double *P); + +void cblas_srotmg(float *d1, float *d2, float *b1, const float b2, float *P); +void cblas_drotmg(double *d1, double *d2, double *b1, const double b2, double *P); + +void cblas_sscal(const int N, const float alpha, float *X, const int incX); +void cblas_dscal(const int N, const double alpha, double *X, const int incX); +void cblas_cscal(const int N, const void *alpha, void *X, const int incX); +void cblas_zscal(const int N, const void *alpha, void *X, const int incX); +void cblas_csscal(const int N, const float alpha, void *X, const int incX); +void cblas_zdscal(const int N, const double alpha, void *X, const int incX); + +void cblas_sgemv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE trans, const int m, const int n, + const float alpha, const float *a, const int lda, const float *x, const int incx, const float beta, float *y, const int incy); +void cblas_dgemv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE trans, const int m, const int n, + const double alpha, const double *a, const int lda, const double *x, const int incx, const double beta, double *y, const int incy); +void cblas_cgemv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE trans, const int m, const int n, + const void *alpha, const void *a, const int lda, const void *x, const int incx, const void *beta, void *y, const int incy); +void cblas_zgemv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE trans, const int m, const int n, + const void *alpha, const void *a, const int lda, const void *x, const int incx, const void *beta, void *y, const int incy); + +void cblas_sger (const enum CBLAS_ORDER order, const int M, const int N, const float alpha, const float *X, const int incX, const float *Y, const int incY, float *A, const int lda); +void cblas_dger (const enum CBLAS_ORDER order, const int M, const int N, const double alpha, const double *X, const int incX, const double *Y, const int incY, double *A, const int lda); +void cblas_cgeru(const enum CBLAS_ORDER order, const int M, const int N, const void *alpha, const void *X, const int incX, const void *Y, const int incY, void *A, const int lda); +void cblas_cgerc(const enum CBLAS_ORDER order, const int M, const int N, const void *alpha, const void *X, const int incX, const void *Y, const int incY, void *A, const int lda); +void cblas_zgeru(const enum CBLAS_ORDER order, const int M, const int N, const void *alpha, const void *X, const int incX, const void *Y, const int incY, void *A, const int lda); +void cblas_zgerc(const enum CBLAS_ORDER order, const int M, const int N, const void *alpha, const void *X, const int incX, const void *Y, const int incY, void *A, const int lda); + +void cblas_strsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const float *A, const int lda, float *X, const int incX); +void cblas_dtrsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const double *A, const int lda, double *X, const int incX); +void cblas_ctrsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const void *A, const int lda, void *X, const int incX); +void cblas_ztrsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const void *A, const int lda, void *X, const int incX); + +void cblas_strmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const float *A, const int lda, float *X, const int incX); +void cblas_dtrmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const double *A, const int lda, double *X, const int incX); +void cblas_ctrmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const void *A, const int lda, void *X, const int incX); +void cblas_ztrmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int N, const void *A, const int lda, void *X, const int incX); + +void cblas_ssyr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const float *X, const int incX, float *A, const int lda); +void cblas_dsyr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const double *X, const int incX, double *A, const int lda); +void cblas_cher(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const void *X, const int incX, void *A, const int lda); +void cblas_zher(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const void *X, const int incX, void *A, const int lda); + +void cblas_ssyr2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo,const int N, const float alpha, const float *X, + const int incX, const float *Y, const int incY, float *A, const int lda); +void cblas_dsyr2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const double *X, + const int incX, const double *Y, const int incY, double *A, const int lda); +void cblas_cher2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const void *alpha, const void *X, const int incX, + const void *Y, const int incY, void *A, const int lda); +void cblas_zher2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const void *alpha, const void *X, const int incX, + const void *Y, const int incY, void *A, const int lda); + +void cblas_sgbmv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE TransA, const int M, const int N, + const int KL, const int KU, const float alpha, const float *A, const int lda, const float *X, const int incX, const float beta, float *Y, const int incY); +void cblas_dgbmv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE TransA, const int M, const int N, + const int KL, const int KU, const double alpha, const double *A, const int lda, const double *X, const int incX, const double beta, double *Y, const int incY); +void cblas_cgbmv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE TransA, const int M, const int N, + const int KL, const int KU, const void *alpha, const void *A, const int lda, const void *X, const int incX, const void *beta, void *Y, const int incY); +void cblas_zgbmv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE TransA, const int M, const int N, + const int KL, const int KU, const void *alpha, const void *A, const int lda, const void *X, const int incX, const void *beta, void *Y, const int incY); + +void cblas_ssbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const int K, const float alpha, const float *A, + const int lda, const float *X, const int incX, const float beta, float *Y, const int incY); +void cblas_dsbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const int K, const double alpha, const double *A, + const int lda, const double *X, const int incX, const double beta, double *Y, const int incY); + + +void cblas_stbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const float *A, const int lda, float *X, const int incX); +void cblas_dtbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const double *A, const int lda, double *X, const int incX); +void cblas_ctbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const void *A, const int lda, void *X, const int incX); +void cblas_ztbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const void *A, const int lda, void *X, const int incX); + +void cblas_stbsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const float *A, const int lda, float *X, const int incX); +void cblas_dtbsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const double *A, const int lda, double *X, const int incX); +void cblas_ctbsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const void *A, const int lda, void *X, const int incX); +void cblas_ztbsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const int K, const void *A, const int lda, void *X, const int incX); + +void cblas_stpmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const float *Ap, float *X, const int incX); +void cblas_dtpmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const double *Ap, double *X, const int incX); +void cblas_ctpmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const void *Ap, void *X, const int incX); +void cblas_ztpmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const void *Ap, void *X, const int incX); + +void cblas_stpsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const float *Ap, float *X, const int incX); +void cblas_dtpsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const double *Ap, double *X, const int incX); +void cblas_ctpsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const void *Ap, void *X, const int incX); +void cblas_ztpsv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, + const int N, const void *Ap, void *X, const int incX); + +void cblas_ssymv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const float *A, + const int lda, const float *X, const int incX, const float beta, float *Y, const int incY); +void cblas_dsymv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const double *A, + const int lda, const double *X, const int incX, const double beta, double *Y, const int incY); +void cblas_chemv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const void *alpha, const void *A, + const int lda, const void *X, const int incX, const void *beta, void *Y, const int incY); +void cblas_zhemv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const void *alpha, const void *A, + const int lda, const void *X, const int incX, const void *beta, void *Y, const int incY); + + +void cblas_sspmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const float *Ap, + const float *X, const int incX, const float beta, float *Y, const int incY); +void cblas_dspmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const double *Ap, + const double *X, const int incX, const double beta, double *Y, const int incY); + +void cblas_sspr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const float *X, const int incX, float *Ap); +void cblas_dspr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const double *X, const int incX, double *Ap); + +void cblas_chpr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const void *X, const int incX, void *A); +void cblas_zhpr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const void *X,const int incX, void *A); + +void cblas_sspr2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const float alpha, const float *X, const int incX, const float *Y, const int incY, float *A); +void cblas_dspr2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const double alpha, const double *X, const int incX, const double *Y, const int incY, double *A); +void cblas_chpr2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const void *alpha, const void *X, const int incX, const void *Y, const int incY, void *Ap); +void cblas_zhpr2(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const void *alpha, const void *X, const int incX, const void *Y, const int incY, void *Ap); + +void cblas_chbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *X, const int incX, const void *beta, void *Y, const int incY); +void cblas_zhbmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *X, const int incX, const void *beta, void *Y, const int incY); + +void cblas_chpmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, + const void *alpha, const void *Ap, const void *X, const int incX, const void *beta, void *Y, const int incY); +void cblas_zhpmv(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo, const int N, + const void *alpha, const void *Ap, const void *X, const int incX, const void *beta, void *Y, const int incY); + +void cblas_sgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const float alpha, const float *A, const int lda, const float *B, const int ldb, const float beta, float *C, const int ldc); +void cblas_dgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const double alpha, const double *A, const int lda, const double *B, const int ldb, const double beta, double *C, const int ldc); +void cblas_cgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); +void cblas_cgemm3m(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); +void cblas_zgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); +void cblas_zgemm3m(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); + + +void cblas_ssymm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const int M, const int N, + const float alpha, const float *A, const int lda, const float *B, const int ldb, const float beta, float *C, const int ldc); +void cblas_dsymm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const int M, const int N, + const double alpha, const double *A, const int lda, const double *B, const int ldb, const double beta, double *C, const int ldc); +void cblas_csymm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const int M, const int N, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); +void cblas_zsymm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const int M, const int N, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); + +void cblas_ssyrk(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const float alpha, const float *A, const int lda, const float beta, float *C, const int ldc); +void cblas_dsyrk(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const double alpha, const double *A, const int lda, const double beta, double *C, const int ldc); +void cblas_csyrk(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const void *alpha, const void *A, const int lda, const void *beta, void *C, const int ldc); +void cblas_zsyrk(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const void *alpha, const void *A, const int lda, const void *beta, void *C, const int ldc); + +void cblas_ssyr2k(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const float alpha, const float *A, const int lda, const float *B, const int ldb, const float beta, float *C, const int ldc); +void cblas_dsyr2k(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const double alpha, const double *A, const int lda, const double *B, const int ldb, const double beta, double *C, const int ldc); +void cblas_csyr2k(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); +void cblas_zsyr2k(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, + const int N, const int K, const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); + +void cblas_strmm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const float alpha, const float *A, const int lda, float *B, const int ldb); +void cblas_dtrmm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const double alpha, const double *A, const int lda, double *B, const int ldb); +void cblas_ctrmm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const void *alpha, const void *A, const int lda, void *B, const int ldb); +void cblas_ztrmm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const void *alpha, const void *A, const int lda, void *B, const int ldb); + +void cblas_strsm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const float alpha, const float *A, const int lda, float *B, const int ldb); +void cblas_dtrsm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const double alpha, const double *A, const int lda, double *B, const int ldb); +void cblas_ctrsm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const void *alpha, const void *A, const int lda, void *B, const int ldb); +void cblas_ztrsm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, + const enum CBLAS_DIAG Diag, const int M, const int N, const void *alpha, const void *A, const int lda, void *B, const int ldb); + +void cblas_chemm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const int M, const int N, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); +void cblas_zhemm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const int M, const int N, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const void *beta, void *C, const int ldc); + +void cblas_cherk(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, const int N, const int K, + const float alpha, const void *A, const int lda, const float beta, void *C, const int ldc); +void cblas_zherk(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, const int N, const int K, + const double alpha, const void *A, const int lda, const double beta, void *C, const int ldc); + +void cblas_cher2k(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const float beta, void *C, const int ldc); +void cblas_zher2k(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE Trans, const int N, const int K, + const void *alpha, const void *A, const int lda, const void *B, const int ldb, const double beta, void *C, const int ldc); + +void cblas_xerbla(int p, const char *rout, const char *form, ...); + +/*** BLAS extensions ***/ + +void cblas_saxpby(const int n, const float alpha, const float *x, const int incx,const float beta, float *y, const int incy); + +void cblas_daxpby(const int n, const double alpha, const double *x, const int incx,const double beta, double *y, const int incy); + +void cblas_caxpby(const int n, const void *alpha, const void *x, const int incx,const void *beta, void *y, const int incy); + +void cblas_zaxpby(const int n, const void *alpha, const void *x, const int incx,const void *beta, void *y, const int incy); + +void cblas_somatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const float calpha, const float *a, + const int clda, float *b, const int cldb); +void cblas_domatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const double calpha, const double *a, + const int clda, double *b, const int cldb); +void cblas_comatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const float* calpha, const float* a, + const int clda, float*b, const int cldb); +void cblas_zomatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const double* calpha, const double* a, + const int clda, double *b, const int cldb); + +void cblas_simatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const float calpha, float *a, + const int clda, const int cldb); +void cblas_dimatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const double calpha, double *a, + const int clda, const int cldb); +void cblas_cimatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const float* calpha, float* a, + const int clda, const int cldb); +void cblas_zimatcopy(const enum CBLAS_ORDER CORDER, const enum CBLAS_TRANSPOSE CTRANS, const int crows, const int ccols, const double* calpha, double* a, + const int clda, const int cldb); + +void cblas_sgeadd(const enum CBLAS_ORDER CORDER,const int crows, const int ccols, const float calpha, float *a, const int clda, const float cbeta, + float *c, const int cldc); +void cblas_dgeadd(const enum CBLAS_ORDER CORDER,const int crows, const int ccols, const double calpha, double *a, const int clda, const double cbeta, + double *c, const int cldc); +void cblas_cgeadd(const enum CBLAS_ORDER CORDER,const int crows, const int ccols, const float *calpha, float *a, const int clda, const float *cbeta, + float *c, const int cldc); +void cblas_zgeadd(const enum CBLAS_ORDER CORDER,const int crows, const int ccols, const double *calpha, double *a, const int clda, const double *cbeta, + double *c, const int cldc); + +/*** BFLOAT16 and INT8 extensions ***/ +/* convert float array to BFLOAT16 array by rounding */ +void cblas_sbstobf16(const int n, const float *in, const int incin, bfloat16 *out, const int incout); +/* convert double array to BFLOAT16 array by rounding */ +void cblas_sbdtobf16(const int n, const double *in, const int incin, bfloat16 *out, const int incout); +/* convert BFLOAT16 array to float array */ +void cblas_sbf16tos(const int n, const bfloat16 *in, const int incin, float *out, const int incout); +/* convert BFLOAT16 array to double array */ +void cblas_dbf16tod(const int n, const bfloat16 *in, const int incin, double *out, const int incout); +/* dot production of BFLOAT16 input arrays, and output as float */ +float cblas_sbdot(const int n, const bfloat16 *x, const int incx, const bfloat16 *y, const int incy); +void cblas_sbgemv(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE trans, const int m, const int n, const float alpha, const bfloat16 *a, const int lda, const bfloat16 *x, const int incx, const float beta, float *y, const int incy); + +void cblas_sbgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, + const float alpha, const bfloat16 *A, const int lda, const bfloat16 *B, const int ldb, const float beta, float *C, const int ldc); +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/cpp/3rd_party/happly.h b/cpp/3rd_party/happly.h new file mode 100644 index 0000000000..4ee8c7146e --- /dev/null +++ b/cpp/3rd_party/happly.h @@ -0,0 +1,2017 @@ +#pragma once + +/* A header-only implementation of the .ply file format. + * https://github.com/nmwsharp/happly + * By Nicholas Sharp - nsharp@cs.cmu.edu + * + * Version 2, July 20, 2019 + */ + +/* +MIT License + +Copyright (c) 2018 Nick Sharp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + + +// clang-format off +/* + + === Changelog === + + Significant changes to the file recorded here. + + - Version 5 (Aug 22, 2020) Minor: skip blank lines before properties in ASCII files + - Version 4 (Sep 11, 2019) Change internal list format to be flat. Other small perf fixes and cleanup. + - Version 3 (Aug 1, 2019) Add support for big endian and obj_info + - Version 2 (July 20, 2019) Catch exceptions by const reference. + - Version 1 (undated) Initial version. Unnamed changes before version numbering. + +*/ +// clang-format on + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// General namespace wrapping all Happly things. +namespace happly { + +// Enum specifying binary or ASCII filetypes. Binary can be little-endian +// (default) or big endian. +enum class DataFormat { ASCII, Binary, BinaryBigEndian }; + +// Type name strings +// clang-format off +template std::string typeName() { return "unknown"; } +template<> inline std::string typeName() { return "char"; } +template<> inline std::string typeName() { return "uchar"; } +template<> inline std::string typeName() { return "short"; } +template<> inline std::string typeName() { return "ushort"; } +template<> inline std::string typeName() { return "int"; } +template<> inline std::string typeName() { return "uint"; } +template<> inline std::string typeName() { return "float"; } +template<> inline std::string typeName() { return "double"; } + +// Template hackery that makes getProperty() and friends pretty while automatically picking up smaller types +namespace { + +// A pointer for the equivalent/smaller equivalent of a type (eg. when a double is requested a float works too, etc) +// long int is intentionally absent to avoid platform confusion +template struct TypeChain { bool hasChildType = false; typedef T type; }; +template <> struct TypeChain { bool hasChildType = true; typedef int32_t type; }; +template <> struct TypeChain { bool hasChildType = true; typedef int16_t type; }; +template <> struct TypeChain { bool hasChildType = true; typedef int8_t type; }; +template <> struct TypeChain { bool hasChildType = true; typedef uint32_t type; }; +template <> struct TypeChain { bool hasChildType = true; typedef uint16_t type; }; +template <> struct TypeChain { bool hasChildType = true; typedef uint8_t type; }; +template <> struct TypeChain { bool hasChildType = true; typedef float type; }; + +template struct CanonicalName { typedef T type; }; +template <> struct CanonicalName { typedef int8_t type; }; +template <> struct CanonicalName { typedef uint8_t type; }; +template <> struct CanonicalName { typedef std::conditional::type, int>::value, uint32_t, uint64_t>::type type; }; + +// Used to change behavior of >> for 8bit ints, which does not do what we want. +template struct SerializeType { typedef T type; }; +template <> struct SerializeType { typedef int32_t type; }; +template <> struct SerializeType< int8_t> { typedef int32_t type; }; + +// Give address only if types are same (used below when conditionally copying data) +// last int/char arg is to resolve ambiguous overloads, just always pass 0 and the int version will be preferred +template +S* addressIfSame(T&, char) { + throw std::runtime_error("tried to take address for types that are not same"); + return nullptr;} +template +S* addressIfSame(S& t, int) {return &t;} + +// clang-format on +} // namespace + +/** + * @brief A generic property, which is associated with some element. Can be plain Property or a ListProperty, of some + * type. Generally, the user should not need to interact with these directly, but they are exposed in case someone + * wants to get clever. + */ +class Property { + +public: + /** + * @brief Create a new Property with the given name. + * + * @param name_ + */ + Property(const std::string& name_) : name(name_){}; + virtual ~Property(){}; + + std::string name; + + /** + * @brief Reserve memory. + * + * @param capacity Expected number of elements. + */ + virtual void reserve(size_t capacity) = 0; + + /** + * @brief (ASCII reading) Parse out the next value of this property from a list of tokens. + * + * @param tokens The list of property tokens for the element. + * @param currEntry Index in to tokens, updated after this property is read. + */ + virtual void parseNext(const std::vector& tokens, size_t& currEntry) = 0; + + /** + * @brief (binary reading) Copy the next value of this property from a stream of bits. + * + * @param stream Stream to read from. + */ + virtual void readNext(std::istream& stream) = 0; + + /** + * @brief (binary reading) Copy the next value of this property from a stream of bits. + * + * @param stream Stream to read from. + */ + virtual void readNextBigEndian(std::istream& stream) = 0; + + /** + * @brief (reading) Write a header entry for this property. + * + * @param outStream Stream to write to. + */ + virtual void writeHeader(std::ostream& outStream) = 0; + + /** + * @brief (ASCII writing) write this property for some element to a stream in plaintext + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataASCII(std::ostream& outStream, size_t iElement) = 0; + + /** + * @brief (binary writing) copy the bits of this property for some element to a stream + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataBinary(std::ostream& outStream, size_t iElement) = 0; + + /** + * @brief (binary writing) copy the bits of this property for some element to a stream + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataBinaryBigEndian(std::ostream& outStream, size_t iElement) = 0; + + /** + * @brief Number of element entries for this property + * + * @return + */ + virtual size_t size() = 0; + + /** + * @brief A string naming the type of the property + * + * @return + */ + virtual std::string propertyTypeName() = 0; +}; + +namespace { + +/** + * Check if the platform is little endian. + * (not foolproof, but will work on most platforms) + * + * @return true if little endian + */ +bool isLittleEndian() { + int32_t oneVal = 0x1; + char* numPtr = (char*)&oneVal; + return (numPtr[0] == 1); +} + +/** + * Swap endianness. + * + * @param value Value to swap. + * + * @return Swapped value. + */ +template +T swapEndian(T val) { + char* bytes = reinterpret_cast(&val); + for (unsigned int i = 0; i < sizeof(val) / 2; i++) { + std::swap(bytes[sizeof(val) - 1 - i], bytes[i]); + } + return val; +} + +// The following specializations for single-byte types are used to avoid compiler warnings. +template <> int8_t swapEndian(int8_t val) { return val; } +template <> uint8_t swapEndian(uint8_t val) { return val; } + + +// Unpack flattened list from the convention used in TypedListProperty +template +std::vector> unflattenList(const std::vector& flatList, const std::vector flatListStarts) { + size_t outerCount = flatListStarts.size() - 1; + + // Put the output here + std::vector> outLists(outerCount); + + if (outerCount == 0) { + return outLists; // quick out for empty + } + + // Copy each sublist + for (size_t iOuter = 0; iOuter < outerCount; iOuter++) { + size_t iFlatStart = flatListStarts[iOuter]; + size_t iFlatEnd = flatListStarts[iOuter + 1]; + outLists[iOuter].insert(outLists[iOuter].begin(), flatList.begin() + iFlatStart, flatList.begin() + iFlatEnd); + } + + return outLists; +} + + +}; // namespace + + +/** + * @brief A property which takes a single value (not a list). + */ +template +class TypedProperty : public Property { + +public: + /** + * @brief Create a new Property with the given name. + * + * @param name_ + */ + TypedProperty(const std::string& name_) : Property(name_) { + if (typeName() == "unknown") { + // TODO should really be a compile-time error + throw std::runtime_error("Attempted property type does not match any type defined by the .ply format."); + } + }; + + /** + * @brief Create a new property and initialize with data. + * + * @param name_ + * @param data_ + */ + TypedProperty(const std::string& name_, const std::vector& data_) : Property(name_), data(data_) { + if (typeName() == "unknown") { + throw std::runtime_error("Attempted property type does not match any type defined by the .ply format."); + } + }; + + virtual ~TypedProperty() override{}; + + /** + * @brief Reserve memory. + * + * @param capacity Expected number of elements. + */ + virtual void reserve(size_t capacity) override { data.reserve(capacity); } + + /** + * @brief (ASCII reading) Parse out the next value of this property from a list of tokens. + * + * @param tokens The list of property tokens for the element. + * @param currEntry Index in to tokens, updated after this property is read. + */ + virtual void parseNext(const std::vector& tokens, size_t& currEntry) override { + data.emplace_back(); + std::istringstream iss(tokens[currEntry]); + typename SerializeType::type tmp; // usually the same type as T + iss >> tmp; + data.back() = tmp; + currEntry++; + }; + + /** + * @brief (binary reading) Copy the next value of this property from a stream of bits. + * + * @param stream Stream to read from. + */ + virtual void readNext(std::istream& stream) override { + data.emplace_back(); + stream.read((char*)&data.back(), sizeof(T)); + } + + /** + * @brief (binary reading) Copy the next value of this property from a stream of bits. + * + * @param stream Stream to read from. + */ + virtual void readNextBigEndian(std::istream& stream) override { + data.emplace_back(); + stream.read((char*)&data.back(), sizeof(T)); + data.back() = swapEndian(data.back()); + } + + /** + * @brief (reading) Write a header entry for this property. + * + * @param outStream Stream to write to. + */ + virtual void writeHeader(std::ostream& outStream) override { + outStream << "property " << typeName() << " " << name << "\n"; + } + + /** + * @brief (ASCII writing) write this property for some element to a stream in plaintext + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataASCII(std::ostream& outStream, size_t iElement) override { + outStream.precision(std::numeric_limits::max_digits10); + outStream << static_cast::type>(data[iElement]); // case is usually a no-op + } + + /** + * @brief (binary writing) copy the bits of this property for some element to a stream + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataBinary(std::ostream& outStream, size_t iElement) override { + outStream.write((char*)&data[iElement], sizeof(T)); + } + + /** + * @brief (binary writing) copy the bits of this property for some element to a stream + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataBinaryBigEndian(std::ostream& outStream, size_t iElement) override { + auto value = swapEndian(data[iElement]); + outStream.write((char*)&value, sizeof(T)); + } + + /** + * @brief Number of element entries for this property + * + * @return + */ + virtual size_t size() override { return data.size(); } + + + /** + * @brief A string naming the type of the property + * + * @return + */ + virtual std::string propertyTypeName() override { return typeName(); } + + /** + * @brief The actual data contained in the property + */ + std::vector data; +}; + + +/** + * @brief A property which is a list of value (eg, 3 doubles). Note that lists are always variable length per-element. + */ +template +class TypedListProperty : public Property { + +public: + /** + * @brief Create a new Property with the given name. + * + * @param name_ + */ + TypedListProperty(const std::string& name_, int listCountBytes_) : Property(name_), listCountBytes(listCountBytes_) { + if (typeName() == "unknown") { + throw std::runtime_error("Attempted property type does not match any type defined by the .ply format."); + } + + flattenedIndexStart.push_back(0); + }; + + /** + * @brief Create a new property and initialize with data + * + * @param name_ + * @param data_ + */ + TypedListProperty(const std::string& name_, const std::vector>& data_) : Property(name_) { + if (typeName() == "unknown") { + throw std::runtime_error("Attempted property type does not match any type defined by the .ply format."); + } + + // Populate list with data + flattenedIndexStart.push_back(0); + for (const std::vector& vec : data_) { + for (const T& val : vec) { + flattenedData.emplace_back(val); + } + flattenedIndexStart.push_back(flattenedData.size()); + } + }; + + virtual ~TypedListProperty() override{}; + + /** + * @brief Reserve memory. + * + * @param capacity Expected number of elements. + */ + virtual void reserve(size_t capacity) override { + flattenedData.reserve(3 * capacity); // optimize for triangle meshes + flattenedIndexStart.reserve(capacity + 1); + } + + /** + * @brief (ASCII reading) Parse out the next value of this property from a list of tokens. + * + * @param tokens The list of property tokens for the element. + * @param currEntry Index in to tokens, updated after this property is read. + */ + virtual void parseNext(const std::vector& tokens, size_t& currEntry) override { + + std::istringstream iss(tokens[currEntry]); + size_t count; + iss >> count; + currEntry++; + + size_t currSize = flattenedData.size(); + size_t afterSize = currSize + count; + flattenedData.resize(afterSize); + for (size_t iFlat = currSize; iFlat < afterSize; iFlat++) { + std::istringstream iss(tokens[currEntry]); + typename SerializeType::type tmp; // usually the same type as T + iss >> tmp; + flattenedData[iFlat] = tmp; + currEntry++; + } + flattenedIndexStart.emplace_back(afterSize); + } + + /** + * @brief (binary reading) Copy the next value of this property from a stream of bits. + * + * @param stream Stream to read from. + */ + virtual void readNext(std::istream& stream) override { + + // Read the size of the list + size_t count = 0; + stream.read(((char*)&count), listCountBytes); + + // Read list elements + size_t currSize = flattenedData.size(); + size_t afterSize = currSize + count; + flattenedData.resize(afterSize); + if (count > 0) { + stream.read((char*)&flattenedData[currSize], count * sizeof(T)); + } + flattenedIndexStart.emplace_back(afterSize); + } + + /** + * @brief (binary reading) Copy the next value of this property from a stream of bits. + * + * @param stream Stream to read from. + */ + virtual void readNextBigEndian(std::istream& stream) override { + + // Read the size of the list + size_t count = 0; + stream.read(((char*)&count), listCountBytes); + if (listCountBytes == 8) { + count = (size_t)swapEndian((uint64_t)count); + } else if (listCountBytes == 4) { + count = (size_t)swapEndian((uint32_t)count); + } else if (listCountBytes == 2) { + count = (size_t)swapEndian((uint16_t)count); + } + + // Read list elements + size_t currSize = flattenedData.size(); + size_t afterSize = currSize + count; + flattenedData.resize(afterSize); + if (count > 0) { + stream.read((char*)&flattenedData[currSize], count * sizeof(T)); + } + flattenedIndexStart.emplace_back(afterSize); + + // Swap endian order of list elements + for (size_t iFlat = currSize; iFlat < afterSize; iFlat++) { + flattenedData[iFlat] = swapEndian(flattenedData[iFlat]); + } + } + + /** + * @brief (reading) Write a header entry for this property. Note that we already use "uchar" for the list count type. + * + * @param outStream Stream to write to. + */ + virtual void writeHeader(std::ostream& outStream) override { + // NOTE: We ALWAYS use uchar as the list count output type + outStream << "property list uchar " << typeName() << " " << name << "\n"; + } + + /** + * @brief (ASCII writing) write this property for some element to a stream in plaintext + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataASCII(std::ostream& outStream, size_t iElement) override { + size_t dataStart = flattenedIndexStart[iElement]; + size_t dataEnd = flattenedIndexStart[iElement + 1]; + + // Get the number of list elements as a uchar, and ensure the value fits + size_t dataCount = dataEnd - dataStart; + if (dataCount > std::numeric_limits::max()) { + throw std::runtime_error( + "List property has an element with more entries than fit in a uchar. See note in README."); + } + + outStream << dataCount; + outStream.precision(std::numeric_limits::max_digits10); + for (size_t iFlat = dataStart; iFlat < dataEnd; iFlat++) { + outStream << " " << static_cast::type>(flattenedData[iFlat]); // cast is usually a no-op + } + } + + /** + * @brief (binary writing) copy the bits of this property for some element to a stream + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataBinary(std::ostream& outStream, size_t iElement) override { + size_t dataStart = flattenedIndexStart[iElement]; + size_t dataEnd = flattenedIndexStart[iElement + 1]; + + // Get the number of list elements as a uchar, and ensure the value fits + size_t dataCount = dataEnd - dataStart; + if (dataCount > std::numeric_limits::max()) { + throw std::runtime_error( + "List property has an element with more entries than fit in a uchar. See note in README."); + } + uint8_t count = static_cast(dataCount); + + outStream.write((char*)&count, sizeof(uint8_t)); + outStream.write((char*)&flattenedData[dataStart], count * sizeof(T)); + } + + /** + * @brief (binary writing) copy the bits of this property for some element to a stream + * + * @param outStream Stream to write to. + * @param iElement index of the element to write. + */ + virtual void writeDataBinaryBigEndian(std::ostream& outStream, size_t iElement) override { + size_t dataStart = flattenedIndexStart[iElement]; + size_t dataEnd = flattenedIndexStart[iElement + 1]; + + // Get the number of list elements as a uchar, and ensure the value fits + size_t dataCount = dataEnd - dataStart; + if (dataCount > std::numeric_limits::max()) { + throw std::runtime_error( + "List property has an element with more entries than fit in a uchar. See note in README."); + } + uint8_t count = static_cast(dataCount); + + outStream.write((char*)&count, sizeof(uint8_t)); + for (size_t iFlat = dataStart; iFlat < dataEnd; iFlat++) { + T value = swapEndian(flattenedData[iFlat]); + outStream.write((char*)&value, sizeof(T)); + } + } + + /** + * @brief Number of element entries for this property + * + * @return + */ + virtual size_t size() override { return flattenedIndexStart.size() - 1; } + + + /** + * @brief A string naming the type of the property + * + * @return + */ + virtual std::string propertyTypeName() override { return typeName(); } + + /** + * @brief The (flattened) data for the property, as formed by concatenating all of the individual element lists + * together. + */ + std::vector flattenedData; + + /** + * @brief Indices in to flattenedData. The i'th element gives the index in to flattenedData where the element's data + * begins. A final entry is included which is the length of flattenedData. Size is N_elem + 1. + */ + std::vector flattenedIndexStart; + + /** + * @brief The number of bytes used to store the count for lists of data. + */ + int listCountBytes = -1; +}; + + +/** + * @brief Helper function to construct a new property of the appropriate type. + * + * @param name The name of the property to construct. + * @param typeStr A string naming the type according to the format. + * @param isList Is this a plain property, or a list property? + * @param listCountTypeStr If a list property, the type of the count varible. + * + * @return A new Property with the proper type. + */ +inline std::unique_ptr createPropertyWithType(const std::string& name, const std::string& typeStr, + bool isList, const std::string& listCountTypeStr) { + + // == Figure out how many bytes the list count field has, if this is a list type + // Note: some files seem to use signed types here, we read the width but always parse as if unsigned + int listCountBytes = -1; + if (isList) { + if (listCountTypeStr == "uchar" || listCountTypeStr == "uint8" || listCountTypeStr == "char" || + listCountTypeStr == "int8") { + listCountBytes = 1; + } else if (listCountTypeStr == "ushort" || listCountTypeStr == "uint16" || listCountTypeStr == "short" || + listCountTypeStr == "int16") { + listCountBytes = 2; + } else if (listCountTypeStr == "uint" || listCountTypeStr == "uint32" || listCountTypeStr == "int" || + listCountTypeStr == "int32") { + listCountBytes = 4; + } else { + throw std::runtime_error("Unrecognized list count type: " + listCountTypeStr); + } + } + + // = Unsigned int + + // 8 bit unsigned + if (typeStr == "uchar" || typeStr == "uint8") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // 16 bit unsigned + else if (typeStr == "ushort" || typeStr == "uint16") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // 32 bit unsigned + else if (typeStr == "uint" || typeStr == "uint32") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // = Signed int + + // 8 bit signed + if (typeStr == "char" || typeStr == "int8") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // 16 bit signed + else if (typeStr == "short" || typeStr == "int16") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // 32 bit signed + else if (typeStr == "int" || typeStr == "int32") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // = Float + + // 32 bit float + else if (typeStr == "float" || typeStr == "float32") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + // 64 bit float + else if (typeStr == "double" || typeStr == "float64") { + if (isList) { + return std::unique_ptr(new TypedListProperty(name, listCountBytes)); + } else { + return std::unique_ptr(new TypedProperty(name)); + } + } + + else { + throw std::runtime_error("Data type: " + typeStr + " cannot be mapped to .ply format"); + } +} + +/** + * @brief An element (more properly an element type) in the .ply object. Tracks the name of the elemnt type (eg, + * "vertices"), the number of elements of that type (eg, 1244), and any properties associated with that element (eg, + * "position", "color"). + */ +class Element { + +public: + /** + * @brief Create a new element type. + * + * @param name_ Name of the element type (eg, "vertices") + * @param count_ Number of instances of this element. + */ + Element(const std::string& name_, size_t count_) : name(name_), count(count_) {} + + std::string name; + size_t count; + std::vector> properties; + + /** + * @brief Check if a property exists. + * + * @param target The name of the property to get. + * + * @return Whether the target property exists. + */ + bool hasProperty(const std::string& target) { + for (std::unique_ptr& prop : properties) { + if (prop->name == target) { + return true; + } + } + return false; + } + + /** + * @brief Check if a property exists with the requested type. + * + * @tparam T The type of the property + * @param target The name of the property to get. + * + * @return Whether the target property exists. + */ + template + bool hasPropertyType(const std::string& target) { + for (std::unique_ptr& prop : properties) { + if (prop->name == target) { + TypedProperty* castedProp = dynamic_cast*>(prop.get()); + if (castedProp) { + return true; + } + return false; + } + } + return false; + } + + /** + * @brief A list of the names of all properties + * + * @return Property names + */ + std::vector getPropertyNames() { + std::vector names; + for (std::unique_ptr& p : properties) { + names.push_back(p->name); + } + return names; + } + + /** + * @brief Low-level method to get a pointer to a property. Users probably don't need to call this. + * + * @param target The name of the property to get. + * + * @return A (unique_ptr) pointer to the property. + */ + std::unique_ptr& getPropertyPtr(const std::string& target) { + for (std::unique_ptr& prop : properties) { + if (prop->name == target) { + return prop; + } + } + throw std::runtime_error("PLY parser: element " + name + " does not have property " + target); + } + + /** + * @brief Add a new (plain, not list) property for this element type. + * + * @tparam T The type of the property + * @param propertyName The name of the property + * @param data The data for the property. Must have the same length as the number of elements. + */ + template + void addProperty(const std::string& propertyName, const std::vector& data) { + + if (data.size() != count) { + throw std::runtime_error("PLY write: new property " + propertyName + " has size which does not match element"); + } + + // If there is already some property with this name, remove it + for (size_t i = 0; i < properties.size(); i++) { + if (properties[i]->name == propertyName) { + properties.erase(properties.begin() + i); + i--; + } + } + + // Copy to canonical type. Often a no-op, but takes care of standardizing widths across platforms. + std::vector::type> canonicalVec(data.begin(), data.end()); + + properties.push_back( + std::unique_ptr(new TypedProperty::type>(propertyName, canonicalVec))); + } + + /** + * @brief Add a new list property for this element type. + * + * @tparam T The type of the property (eg, "double" for a list of doubles) + * @param propertyName The name of the property + * @param data The data for the property. Outer vector must have the same length as the number of elements. + */ + template + void addListProperty(const std::string& propertyName, const std::vector>& data) { + + if (data.size() != count) { + throw std::runtime_error("PLY write: new property " + propertyName + " has size which does not match element"); + } + + // If there is already some property with this name, remove it + for (size_t i = 0; i < properties.size(); i++) { + if (properties[i]->name == propertyName) { + properties.erase(properties.begin() + i); + i--; + } + } + + // Copy to canonical type. Often a no-op, but takes care of standardizing widths across platforms. + std::vector::type>> canonicalListVec; + for (const std::vector& subList : data) { + canonicalListVec.emplace_back(subList.begin(), subList.end()); + } + + properties.push_back(std::unique_ptr( + new TypedListProperty::type>(propertyName, canonicalListVec))); + } + + /** + * @brief Get a vector of a data from a property for this element. Automatically promotes to larger types. Throws if + * requested data is unavailable. + * + * @tparam T The type of data requested + * @param propertyName The name of the property to get. + * + * @return The data. + */ + template + std::vector getProperty(const std::string& propertyName) { + + // Find the property + std::unique_ptr& prop = getPropertyPtr(propertyName); + + // Get a copy of the data with auto-promoting type magic + return getDataFromPropertyRecursive(prop.get()); + } + + /** + * @brief Get a vector of a data from a property for this element. Unlike getProperty(), only returns if the ply + * record contains a type that matches T exactly. Throws if * requested data is unavailable. + * + * @tparam T The type of data requested + * @param propertyName The name of the property to get. + * + * @return The data. + */ + template + std::vector getPropertyType(const std::string& propertyName) { + + // Find the property + std::unique_ptr& prop = getPropertyPtr(propertyName); + TypedProperty* castedProp = dynamic_cast*>(prop.get()); + if (castedProp) { + return castedProp->data; + } + + // No match, failure + throw std::runtime_error("PLY parser: property " + prop->name + " is not of type type " + typeName() + + ". Has type " + prop->propertyTypeName()); + } + + /** + * @brief Get a vector of lists of data from a property for this element. Automatically promotes to larger types. + * Throws if requested data is unavailable. + * + * @tparam T The type of data requested + * @param propertyName The name of the property to get. + * + * @return The data. + */ + template + std::vector> getListProperty(const std::string& propertyName) { + + // Find the property + std::unique_ptr& prop = getPropertyPtr(propertyName); + + // Get a copy of the data with auto-promoting type magic + return getDataFromListPropertyRecursive(prop.get()); + } + + /** + * @brief Get a vector of a data from a property for this element. Unlike getProperty(), only returns if the ply + * record contains a type that matches T exactly. Throws if * requested data is unavailable. + * + * @tparam T The type of data requested + * @param propertyName The name of the property to get. + * + * @return The data. + */ + template + std::vector> getListPropertyType(const std::string& propertyName) { + + // Find the property + std::unique_ptr& prop = getPropertyPtr(propertyName); + TypedListProperty* castedProp = dynamic_cast*>(prop.get()); + if (castedProp) { + return unflattenList(castedProp->flattenedData, castedProp->flattenedIndexStart); + } + + // No match, failure + throw std::runtime_error("PLY parser: list property " + prop->name + " is not of type " + typeName() + + ". Has type " + prop->propertyTypeName()); + } + + + /** + * @brief Get a vector of lists of data from a property for this element. Automatically promotes to larger types. + * Unlike getListProperty(), this method will additionally convert between types of different sign (eg, requesting and + * int32 would get data from a uint32); doing so naively converts between signed and unsigned types. This is typically + * useful for data representing indices, which might be stored as signed or unsigned numbers. + * + * @tparam T The type of data requested + * @param propertyName The name of the property to get. + * + * @return The data. + */ + template + std::vector> getListPropertyAnySign(const std::string& propertyName) { + + // Find the property + std::unique_ptr& prop = getPropertyPtr(propertyName); + + // Get a copy of the data with auto-promoting type magic + try { + // First, try the usual approach, looking for a version of the property with the same signed-ness and possibly + // smaller size + return getDataFromListPropertyRecursive(prop.get()); + } catch (const std::runtime_error& orig_e) { + + // If the usual approach fails, look for a version with opposite signed-ness + try { + + // This type has the oppopsite signeness as the input type + typedef typename CanonicalName::type Tcan; + typedef typename std::conditional::value, typename std::make_unsigned::type, + typename std::make_signed::type>::type OppsignType; + + return getDataFromListPropertyRecursive(prop.get()); + + } catch (const std::runtime_error&) { + throw orig_e; + } + + throw orig_e; + } + } + + + /** + * @brief Performs sanity checks on the element, throwing if any fail. + */ + void validate() { + + // Make sure no properties have duplicate names, and no names have whitespace + for (size_t iP = 0; iP < properties.size(); iP++) { + for (char c : properties[iP]->name) { + if (std::isspace(c)) { + throw std::runtime_error("Ply validate: illegal whitespace in name " + properties[iP]->name); + } + } + for (size_t jP = iP + 1; jP < properties.size(); jP++) { + if (properties[iP]->name == properties[jP]->name) { + throw std::runtime_error("Ply validate: multiple properties with name " + properties[iP]->name); + } + } + } + + // Make sure all properties have right length + for (size_t iP = 0; iP < properties.size(); iP++) { + if (properties[iP]->size() != count) { + throw std::runtime_error("Ply validate: property has wrong size. " + properties[iP]->name + + " does not match element size."); + } + } + } + + /** + * @brief Writes out this element's information to the file header. + * + * @param outStream The stream to use. + */ + void writeHeader(std::ostream& outStream) { + + outStream << "element " << name << " " << count << "\n"; + + for (std::unique_ptr& p : properties) { + p->writeHeader(outStream); + } + } + + /** + * @brief (ASCII writing) Writes out all of the data for every element of this element type to the stream, including + * all contained properties. + * + * @param outStream The stream to write to. + */ + void writeDataASCII(std::ostream& outStream) { + // Question: what is the proper output for an element with no properties? Here, we write a blank line, so there is + // one line per element no matter what. + for (size_t iE = 0; iE < count; iE++) { + for (size_t iP = 0; iP < properties.size(); iP++) { + properties[iP]->writeDataASCII(outStream, iE); + if (iP < properties.size() - 1) { + outStream << " "; + } + } + outStream << "\n"; + } + } + + + /** + * @brief (binary writing) Writes out all of the data for every element of this element type to the stream, including + * all contained properties. + * + * @param outStream The stream to write to. + */ + void writeDataBinary(std::ostream& outStream) { + for (size_t iE = 0; iE < count; iE++) { + for (size_t iP = 0; iP < properties.size(); iP++) { + properties[iP]->writeDataBinary(outStream, iE); + } + } + } + + + /** + * @brief (binary writing) Writes out all of the data for every element of this element type to the stream, including + * all contained properties. + * + * @param outStream The stream to write to. + */ + void writeDataBinaryBigEndian(std::ostream& outStream) { + for (size_t iE = 0; iE < count; iE++) { + for (size_t iP = 0; iP < properties.size(); iP++) { + properties[iP]->writeDataBinaryBigEndian(outStream, iE); + } + } + } + + + /** + * @brief Helper function which does the hard work to implement type promotion for data getters. Throws if type + * conversion fails. + * + * @tparam D The desired output type + * @tparam T The current attempt for the actual type of the property + * @param prop The property to get (does not delete nor share pointer) + * + * @return The data, with the requested type + */ + template + std::vector getDataFromPropertyRecursive(Property* prop) { + + typedef typename CanonicalName::type Tcan; + + { // Try to return data of type D from a property of type T + TypedProperty* castedProp = dynamic_cast*>(prop); + if (castedProp) { + // Succeeded, return a buffer of the data (copy while converting type) + std::vector castedVec; + castedVec.reserve(castedProp->data.size()); + for (Tcan& v : castedProp->data) { + castedVec.push_back(static_cast(v)); + } + return castedVec; + } + } + + TypeChain chainType; + if (chainType.hasChildType) { + return getDataFromPropertyRecursive::type>(prop); + } else { + // No smaller type to try, failure + throw std::runtime_error("PLY parser: property " + prop->name + " cannot be coerced to requested type " + + typeName() + ". Has type " + prop->propertyTypeName()); + } + } + + + /** + * @brief Helper function which does the hard work to implement type promotion for list data getters. Throws if type + * conversion fails. + * + * @tparam D The desired output type + * @tparam T The current attempt for the actual type of the property + * @param prop The property to get (does not delete nor share pointer) + * + * @return The data, with the requested type + */ + template + std::vector> getDataFromListPropertyRecursive(Property* prop) { + typedef typename CanonicalName::type Tcan; + + TypedListProperty* castedProp = dynamic_cast*>(prop); + if (castedProp) { + // Succeeded, return a buffer of the data (copy while converting type) + + // Convert to flat buffer of new type + std::vector* castedFlatVec = nullptr; + std::vector castedFlatVecCopy; // we _might_ make a copy here, depending on is_same below + + if (std::is_same, std::vector>::value) { + // just use the array we already have + castedFlatVec = addressIfSame>(castedProp->flattenedData, 0 /* dummy arg to disambiguate */); + } else { + // make a copy + castedFlatVecCopy.reserve(castedProp->flattenedData.size()); + for (Tcan& v : castedProp->flattenedData) { + castedFlatVecCopy.push_back(static_cast(v)); + } + castedFlatVec = &castedFlatVecCopy; + } + + // Unflatten and return + return unflattenList(*castedFlatVec, castedProp->flattenedIndexStart); + } + + TypeChain chainType; + if (chainType.hasChildType) { + return getDataFromListPropertyRecursive::type>(prop); + } else { + // No smaller type to try, failure + throw std::runtime_error("PLY parser: list property " + prop->name + + " cannot be coerced to requested type list " + typeName() + ". Has type list " + + prop->propertyTypeName()); + } + } +}; + + +// Some string helpers +namespace { + +inline std::string trimSpaces(const std::string& input) { + size_t start = 0; + while (start < input.size() && input[start] == ' ') start++; + size_t end = input.size(); + while (end > start && (input[end - 1] == ' ' || input[end - 1] == '\n' || input[end - 1] == '\r')) end--; + return input.substr(start, end - start); +} + +inline std::vector tokenSplit(const std::string& input) { + std::vector result; + size_t curr = 0; + size_t found = 0; + while ((found = input.find_first_of(' ', curr)) != std::string::npos) { + std::string token = input.substr(curr, found - curr); + token = trimSpaces(token); + if (token.size() > 0) { + result.push_back(token); + } + curr = found + 1; + } + std::string token = input.substr(curr); + token = trimSpaces(token); + if (token.size() > 0) { + result.push_back(token); + } + + return result; +} + +inline bool startsWith(const std::string& input, const std::string& query) { + return input.compare(0, query.length(), query) == 0; +} +}; // namespace + + +/** + * @brief Primary class; represents a set of data in the .ply format. + */ +class PLYData { + +public: + /** + * @brief Create an empty PLYData object. + */ + PLYData(){}; + + /** + * @brief Initialize a PLYData by reading from a file. Throws if any failures occur. + * + * @param filename The file to read from. + * @param verbose If true, print useful info about the file to stdout + */ + PLYData(const std::string& filename, bool verbose = false) { + + using std::cout; + using std::endl; + using std::string; + using std::vector; + + if (verbose) cout << "PLY parser: Reading ply file: " << filename << endl; + + // Open a file in binary always, in case it turns out to have binary data. + std::ifstream inStream(filename, std::ios::binary); + if (inStream.fail()) { + throw std::runtime_error("PLY parser: Could not open file " + filename); + } + + parsePLY(inStream, verbose); + + if (verbose) { + cout << " - Finished parsing file." << endl; + } + } + + /** + * @brief Initialize a PLYData by reading from a stringstream. Throws if any failures occur. + * + * @param inStream The stringstream to read from. + * @param verbose If true, print useful info about the file to stdout + */ + PLYData(std::istream& inStream, bool verbose = false) { + + using std::cout; + using std::endl; + + if (verbose) cout << "PLY parser: Reading ply file from stream" << endl; + + parsePLY(inStream, verbose); + + if (verbose) { + cout << " - Finished parsing stream." << endl; + } + } + + /** + * @brief Perform sanity checks on the file, throwing if any fail. + */ + void validate() { + + for (size_t iE = 0; iE < elements.size(); iE++) { + for (char c : elements[iE].name) { + if (std::isspace(c)) { + throw std::runtime_error("Ply validate: illegal whitespace in element name " + elements[iE].name); + } + } + for (size_t jE = iE + 1; jE < elements.size(); jE++) { + if (elements[iE].name == elements[jE].name) { + throw std::runtime_error("Ply validate: duplcate element name " + elements[iE].name); + } + } + } + + // Do a quick validation sanity check + for (Element& e : elements) { + e.validate(); + } + } + + /** + * @brief Write this data to a .ply file. + * + * @param filename The file to write to. + * @param format The format to use (binary or ascii?) + */ + void write(const std::string& filename, DataFormat format = DataFormat::ASCII) { + outputDataFormat = format; + + validate(); + + // Open stream for writing + std::ofstream outStream(filename, std::ios::out | std::ios::binary); + if (!outStream.good()) { + throw std::runtime_error("Ply writer: Could not open output file " + filename + " for writing"); + } + + writePLY(outStream); + } + + /** + * @brief Write this data to an output stream + * + * @param outStream The output stream to write to. + * @param format The format to use (binary or ascii?) + */ + void write(std::ostream& outStream, DataFormat format = DataFormat::ASCII) { + outputDataFormat = format; + + validate(); + + writePLY(outStream); + } + + /** + * @brief Get an element type by name ("vertices") + * + * @param target The name of the element type to get + * + * @return A reference to the element type. + */ + Element& getElement(const std::string& target) { + for (Element& e : elements) { + if (e.name == target) return e; + } + throw std::runtime_error("PLY parser: no element with name: " + target); + } + + + /** + * @brief Check if an element type exists + * + * @param target The name to check for. + * + * @return True if exists. + */ + bool hasElement(const std::string& target) { + for (Element& e : elements) { + if (e.name == target) return true; + } + return false; + } + + + /** + * @brief A list of the names of all elements + * + * @return Element names + */ + std::vector getElementNames() { + std::vector names; + for (Element& e : elements) { + names.push_back(e.name); + } + return names; + } + + + /** + * @brief Add a new element type to the object + * + * @param name The name of the new element type ("vertices"). + * @param count The number of elements of this type. + */ + void addElement(const std::string& name, size_t count) { elements.emplace_back(name, count); } + + // === Common-case helpers + + + /** + * @brief Common-case helper get mesh vertex positions + * + * @param vertexElementName The element name to use (default: "vertex") + * + * @return A vector of vertex positions. + */ + std::vector> getVertexPositions(const std::string& vertexElementName = "vertex") { + + std::vector xPos = getElement(vertexElementName).getProperty("x"); + std::vector yPos = getElement(vertexElementName).getProperty("y"); + std::vector zPos = getElement(vertexElementName).getProperty("z"); + + std::vector> result(xPos.size()); + for (size_t i = 0; i < result.size(); i++) { + result[i][0] = xPos[i]; + result[i][1] = yPos[i]; + result[i][2] = zPos[i]; + } + + return result; + } + + /** + * @brief Common-case helper get mesh vertex colors + * + * @param vertexElementName The element name to use (default: "vertex") + * + * @return A vector of vertex colors (unsigned chars [0,255]). + */ + std::vector> getVertexColors(const std::string& vertexElementName = "vertex") { + + std::vector r = getElement(vertexElementName).getProperty("red"); + std::vector g = getElement(vertexElementName).getProperty("green"); + std::vector b = getElement(vertexElementName).getProperty("blue"); + + std::vector> result(r.size()); + for (size_t i = 0; i < result.size(); i++) { + result[i][0] = r[i]; + result[i][1] = g[i]; + result[i][2] = b[i]; + } + + return result; + } + + /** + * @brief Common-case helper to get face indices for a mesh. If not template type is given, size_t is used. Naively + * converts to requested signedness, which may lead to unexpected values if an unsigned type is used and file contains + * negative values. + * + * @return The indices into the vertex elements for each face. Usually 0-based, though there are no formal rules. + */ + template + std::vector> getFaceIndices() { + + for (const std::string& f : std::vector{"face"}) { + for (const std::string& p : std::vector{"vertex_indices", "vertex_index"}) { + try { + return getElement(f).getListPropertyAnySign(p); + } catch (const std::runtime_error&) { + // that's fine + } + } + } + throw std::runtime_error("PLY parser: could not find face vertex indices attribute under any common name."); + } + + + /** + * @brief Common-case helper set mesh vertex positons. Creates vertex element, if necessary. + * + * @param vertexPositions A vector of vertex positions + */ + void addVertexPositions(std::vector>& vertexPositions) { + + std::string vertexName = "vertex"; + size_t N = vertexPositions.size(); + + // Create the element + if (!hasElement(vertexName)) { + addElement(vertexName, N); + } + + // De-interleave + std::vector xPos(N); + std::vector yPos(N); + std::vector zPos(N); + for (size_t i = 0; i < vertexPositions.size(); i++) { + xPos[i] = vertexPositions[i][0]; + yPos[i] = vertexPositions[i][1]; + zPos[i] = vertexPositions[i][2]; + } + + // Store + getElement(vertexName).addProperty("x", xPos); + getElement(vertexName).addProperty("y", yPos); + getElement(vertexName).addProperty("z", zPos); + } + + /** + * @brief Common-case helper set mesh vertex colors. Creates a vertex element, if necessary. + * + * @param colors A vector of vertex colors (unsigned chars [0,255]). + */ + void addVertexColors(std::vector>& colors) { + + std::string vertexName = "vertex"; + size_t N = colors.size(); + + // Create the element + if (!hasElement(vertexName)) { + addElement(vertexName, N); + } + + // De-interleave + std::vector r(N); + std::vector g(N); + std::vector b(N); + for (size_t i = 0; i < colors.size(); i++) { + r[i] = colors[i][0]; + g[i] = colors[i][1]; + b[i] = colors[i][2]; + } + + // Store + getElement(vertexName).addProperty("red", r); + getElement(vertexName).addProperty("green", g); + getElement(vertexName).addProperty("blue", b); + } + + /** + * @brief Common-case helper set mesh vertex colors. Creates a vertex element, if necessary. + * + * @param colors A vector of vertex colors as floating point [0,1] values. Internally converted to [0,255] chars. + */ + void addVertexColors(std::vector>& colors) { + + std::string vertexName = "vertex"; + size_t N = colors.size(); + + // Create the element + if (!hasElement(vertexName)) { + addElement(vertexName, N); + } + + auto toChar = [](double v) { + if (v < 0.0) v = 0.0; + if (v > 1.0) v = 1.0; + return static_cast(v * 255.); + }; + + // De-interleave + std::vector r(N); + std::vector g(N); + std::vector b(N); + for (size_t i = 0; i < colors.size(); i++) { + r[i] = toChar(colors[i][0]); + g[i] = toChar(colors[i][1]); + b[i] = toChar(colors[i][2]); + } + + // Store + getElement(vertexName).addProperty("red", r); + getElement(vertexName).addProperty("green", g); + getElement(vertexName).addProperty("blue", b); + } + + + /** + * @brief Common-case helper to set face indices. Creates a face element if needed. The input type will be casted to a + * 32 bit integer of the same signedness. + * + * @param indices The indices into the vertex list around each face. + */ + template + void addFaceIndices(std::vector>& indices) { + + std::string faceName = "face"; + size_t N = indices.size(); + + // Create the element + if (!hasElement(faceName)) { + addElement(faceName, N); + } + + // Cast to 32 bit + typedef typename std::conditional::value, int32_t, uint32_t>::type IndType; + std::vector> intInds; + for (std::vector& l : indices) { + std::vector thisInds; + for (T& val : l) { + IndType valConverted = static_cast(val); + if (valConverted != val) { + throw std::runtime_error("Index value " + std::to_string(val) + + " could not be converted to a .ply integer without loss of data. Note that .ply " + "only supports 32-bit ints."); + } + thisInds.push_back(valConverted); + } + intInds.push_back(thisInds); + } + + // Store + getElement(faceName).addListProperty("vertex_indices", intInds); + } + + + /** + * @brief Comments for the file. When writing, each entry will be written as a sequential comment line. + */ + std::vector comments; + + + /** + * @brief obj_info comments for the file. When writing, each entry will be written as a sequential comment line. + */ + std::vector objInfoComments; + +private: + std::vector elements; + const int majorVersion = 1; // I'll buy you a drink if these ever get bumped + const int minorVersion = 0; + + DataFormat inputDataFormat = DataFormat::ASCII; // set when reading from a file + DataFormat outputDataFormat = DataFormat::ASCII; // option for writing files + + + // === Reading === + + /** + * @brief Parse a PLY file from an input stream + * + * @param inStream + * @param verbose + */ + void parsePLY(std::istream& inStream, bool verbose) { + + // == Process the header + parseHeader(inStream, verbose); + + + // === Parse data from a binary file + if (inputDataFormat == DataFormat::Binary) { + parseBinary(inStream, verbose); + } + // === Parse data from an binary file + else if (inputDataFormat == DataFormat::BinaryBigEndian) { + parseBinaryBigEndian(inStream, verbose); + } + // === Parse data from an ASCII file + else if (inputDataFormat == DataFormat::ASCII) { + parseASCII(inStream, verbose); + } + } + + /** + * @brief Read the header for a file + * + * @param inStream + * @param verbose + */ + void parseHeader(std::istream& inStream, bool verbose) { + + using std::cout; + using std::endl; + using std::string; + using std::vector; + + // First two lines are predetermined + { // First line is magic constant + string plyLine; + std::getline(inStream, plyLine); + if (trimSpaces(plyLine) != "ply") { + throw std::runtime_error("PLY parser: File does not appear to be ply file. First line should be 'ply'"); + } + } + + { // second line is version + string styleLine; + std::getline(inStream, styleLine); + vector tokens = tokenSplit(styleLine); + if (tokens.size() != 3) throw std::runtime_error("PLY parser: bad format line"); + std::string formatStr = tokens[0]; + std::string typeStr = tokens[1]; + std::string versionStr = tokens[2]; + + // "format" + if (formatStr != "format") throw std::runtime_error("PLY parser: bad format line"); + + // ascii/binary + if (typeStr == "ascii") { + inputDataFormat = DataFormat::ASCII; + if (verbose) cout << " - Type: ascii" << endl; + } else if (typeStr == "binary_little_endian") { + inputDataFormat = DataFormat::Binary; + if (verbose) cout << " - Type: binary" << endl; + } else if (typeStr == "binary_big_endian") { + inputDataFormat = DataFormat::BinaryBigEndian; + if (verbose) cout << " - Type: binary big endian" << endl; + } else { + throw std::runtime_error("PLY parser: bad format line"); + } + + // version + if (versionStr != "1.0") { + throw std::runtime_error("PLY parser: encountered file with version != 1.0. Don't know how to parse that"); + } + if (verbose) cout << " - Version: " << versionStr << endl; + } + + // Consume header line by line + while (inStream.good()) { + string line; + std::getline(inStream, line); + + // Parse a comment + if (startsWith(line, "comment")) { + string comment = line.substr(8); + if (verbose) cout << " - Comment: " << comment << endl; + comments.push_back(comment); + continue; + } + + // Parse an obj_info comment + if (startsWith(line, "obj_info")) { + string infoComment = line.substr(9); + if (verbose) cout << " - obj_info: " << infoComment << endl; + objInfoComments.push_back(infoComment); + continue; + } + + // Parse an element + else if (startsWith(line, "element")) { + vector tokens = tokenSplit(line); + if (tokens.size() != 3) throw std::runtime_error("PLY parser: Invalid element line"); + string name = tokens[1]; + size_t count; + std::istringstream iss(tokens[2]); + iss >> count; + elements.emplace_back(name, count); + if (verbose) cout << " - Found element: " << name << " (count = " << count << ")" << endl; + continue; + } + + // Parse a property list + else if (startsWith(line, "property list")) { + vector tokens = tokenSplit(line); + if (tokens.size() != 5) throw std::runtime_error("PLY parser: Invalid property list line"); + if (elements.size() == 0) throw std::runtime_error("PLY parser: Found property list without previous element"); + string countType = tokens[2]; + string type = tokens[3]; + string name = tokens[4]; + elements.back().properties.push_back(createPropertyWithType(name, type, true, countType)); + if (verbose) + cout << " - Found list property: " << name << " (count type = " << countType << ", data type = " << type + << ")" << endl; + continue; + } + + // Parse a property + else if (startsWith(line, "property")) { + vector tokens = tokenSplit(line); + if (tokens.size() != 3) throw std::runtime_error("PLY parser: Invalid property line"); + if (elements.size() == 0) throw std::runtime_error("PLY parser: Found property without previous element"); + string type = tokens[1]; + string name = tokens[2]; + elements.back().properties.push_back(createPropertyWithType(name, type, false, "")); + if (verbose) cout << " - Found property: " << name << " (type = " << type << ")" << endl; + continue; + } + + // Parse end of header + else if (startsWith(line, "end_header")) { + break; + } + + // Error! + else { + throw std::runtime_error("Unrecognized header line: " + line); + } + } + } + + /** + * @brief Read the actual data for a file, in ASCII + * + * @param inStream + * @param verbose + */ + void parseASCII(std::istream& inStream, bool verbose) { + + using std::string; + using std::vector; + + // Read all elements + for (Element& elem : elements) { + + if (verbose) { + std::cout << " - Processing element: " << elem.name << std::endl; + } + + for (size_t iP = 0; iP < elem.properties.size(); iP++) { + elem.properties[iP]->reserve(elem.count); + } + for (size_t iEntry = 0; iEntry < elem.count; iEntry++) { + + string line; + std::getline(inStream, line); + + // Some .ply files seem to include empty lines before the start of property data (though this is not specified + // in the format description). We attempt to recover and parse such files by skipping any empty lines. + if (!elem.properties.empty()) { // if the element has no properties, the line _should_ be blank, presumably + while (line.empty()) { // skip lines until we hit something nonempty + std::getline(inStream, line); + } + } + + vector tokens = tokenSplit(line); + size_t iTok = 0; + for (size_t iP = 0; iP < elem.properties.size(); iP++) { + elem.properties[iP]->parseNext(tokens, iTok); + } + } + } + } + + /** + * @brief Read the actual data for a file, in binary. + * + * @param inStream + * @param verbose + */ + void parseBinary(std::istream& inStream, bool verbose) { + + if (!isLittleEndian()) { + throw std::runtime_error("binary reading assumes little endian system"); + } + + using std::string; + using std::vector; + + // Read all elements + for (Element& elem : elements) { + + if (verbose) { + std::cout << " - Processing element: " << elem.name << std::endl; + } + + for (size_t iP = 0; iP < elem.properties.size(); iP++) { + elem.properties[iP]->reserve(elem.count); + } + for (size_t iEntry = 0; iEntry < elem.count; iEntry++) { + for (size_t iP = 0; iP < elem.properties.size(); iP++) { + elem.properties[iP]->readNext(inStream); + } + } + } + } + + /** + * @brief Read the actual data for a file, in binary. + * + * @param inStream + * @param verbose + */ + void parseBinaryBigEndian(std::istream& inStream, bool verbose) { + + if (!isLittleEndian()) { + throw std::runtime_error("binary reading assumes little endian system"); + } + + using std::string; + using std::vector; + + // Read all elements + for (Element& elem : elements) { + + if (verbose) { + std::cout << " - Processing element: " << elem.name << std::endl; + } + + for (size_t iP = 0; iP < elem.properties.size(); iP++) { + elem.properties[iP]->reserve(elem.count); + } + for (size_t iEntry = 0; iEntry < elem.count; iEntry++) { + for (size_t iP = 0; iP < elem.properties.size(); iP++) { + elem.properties[iP]->readNextBigEndian(inStream); + } + } + } + } + + // === Writing === + + + /** + * @brief write a PLY file to an output stream + * + * @param outStream + */ + void writePLY(std::ostream& outStream) { + + writeHeader(outStream); + + // Write all elements + for (Element& e : elements) { + if (outputDataFormat == DataFormat::Binary) { + if (!isLittleEndian()) { + throw std::runtime_error("binary writing assumes little endian system"); + } + e.writeDataBinary(outStream); + } else if (outputDataFormat == DataFormat::BinaryBigEndian) { + if (!isLittleEndian()) { + throw std::runtime_error("binary writing assumes little endian system"); + } + e.writeDataBinaryBigEndian(outStream); + } else if (outputDataFormat == DataFormat::ASCII) { + e.writeDataASCII(outStream); + } + } + } + + + /** + * @brief Write out a header for a file + * + * @param outStream + */ + void writeHeader(std::ostream& outStream) { + + // Magic line + outStream << "ply\n"; + + // Type line + outStream << "format "; + if (outputDataFormat == DataFormat::Binary) { + outStream << "binary_little_endian "; + } else if (outputDataFormat == DataFormat::BinaryBigEndian) { + outStream << "binary_big_endian "; + } else if (outputDataFormat == DataFormat::ASCII) { + outStream << "ascii "; + } + + // Version number + outStream << majorVersion << "." << minorVersion << "\n"; + + // Write comments + bool hasHapplyComment = false; + std::string happlyComment = "Written with hapPLY (https://github.com/nmwsharp/happly)"; + for (const std::string& comment : comments) { + if (comment == happlyComment) hasHapplyComment = true; + outStream << "comment " << comment << "\n"; + } + if (!hasHapplyComment) { + outStream << "comment " << happlyComment << "\n"; + } + + // Write obj_info comments + for (const std::string& comment : objInfoComments) { + outStream << "obj_info " << comment << "\n"; + } + + // Write elements (and their properties) + for (Element& e : elements) { + e.writeHeader(outStream); + } + + // End header + outStream << "end_header\n"; + } +}; + +} // namespace happly diff --git a/cpp/3rd_party/json/CMakeLists.txt b/cpp/3rd_party/json/CMakeLists.txt new file mode 100644 index 0000000000..4179e15c6d --- /dev/null +++ b/cpp/3rd_party/json/CMakeLists.txt @@ -0,0 +1,5 @@ +project(json) + +file(GLOB_RECURSE SOURCES "*.cpp" "*.c" "*.hpp") + +add_library(json ${SOURCES}) \ No newline at end of file diff --git a/cpp/3rd_party/json/comparision.hpp b/cpp/3rd_party/json/comparision.hpp new file mode 100644 index 0000000000..cf8d5f9122 --- /dev/null +++ b/cpp/3rd_party/json/comparision.hpp @@ -0,0 +1,157 @@ +#pragma once + +#include +#include +#include + +#include + +// order: null < boolean < number < object < array < string < binary +static constexpr std::array json_kind_order_ = {{ + 0, // null + 1, // boolean + 2, // number + 2, // unsigned + 2, // float + 5, // string + 4, // array + 3, // object + } +}; + +namespace boost { + +namespace json { + +// Less than operators +template +inline bool operator<(const boost::json::value& lhs, const T& rhs) +{ + return lhs < boost::json::value_from(rhs); +} + +template +inline bool operator<(const T& lhs, const boost::json::value& rhs) +{ + return boost::json::value_from(lhs) < rhs; +} + +inline bool operator<(const boost::json::value& lhs, const boost::json::value& rhs) +{ + auto lkind_idx = static_cast(lhs.kind()); + auto rkind_idx = static_cast(rhs.kind()); + if (lkind_idx == rkind_idx || (lhs.is_number() && rhs.is_number())) { + if (lhs.is_null()) { + return false; + } else if (lhs.is_bool()) { + return lhs.as_bool() < rhs.is_bool(); + } else if (lhs.is_string()) { + return lhs.as_string() < rhs.as_string(); + } else if (lhs.is_number()) { + if (lhs.is_int64() && rhs.is_int64()) { + return lhs.as_int64() < rhs.as_int64(); + } else if (lhs.is_double() && rhs.is_double()) { + return lhs.as_double() < rhs.as_double(); + } else if (lhs.is_int64() && rhs.is_double()) { + return static_cast(lhs.as_int64()) < rhs.as_double(); + } else if (lhs.is_double() && rhs.is_int64()) { + return lhs.as_double() < static_cast(rhs.as_int64()); + } else if (lhs.is_int64() && rhs.is_uint64()) { + return lhs.as_int64() < static_cast(rhs.as_uint64()); + } else if (lhs.is_uint64() && rhs.is_int64()) { + return static_cast(lhs.as_uint64()) < rhs.as_int64(); + } else if (lhs.is_uint64() && rhs.is_double()) { + return static_cast(lhs.as_uint64()) < rhs.as_double(); + } else if (lhs.is_double() && rhs.is_uint64()) { + return lhs.as_double() < static_cast(rhs.as_uint64()); + } else if (lhs.is_uint64() && rhs.is_uint64()) { + return lhs.as_uint64() < rhs.as_uint64(); + } + } + } + return lkind_idx < json_kind_order_.size() && rkind_idx < json_kind_order_.size() && json_kind_order_[lkind_idx] < json_kind_order_[rkind_idx]; +} + +// Greater than operators +template +inline bool operator>(const boost::json::value& lhs, const T& rhs) +{ + return rhs < lhs; +} + +template +inline bool operator>(const T& lhs, const boost::json::value& rhs) +{ + return rhs < lhs; +} + +inline bool operator>(const boost::json::value& lhs, const boost::json::value& rhs) +{ + return rhs < lhs; +} + +// Less than or equal operators +template +inline bool operator<=(const boost::json::value& lhs, const T& rhs) +{ + return !(rhs < lhs); +} + +template +inline bool operator<=(const T& lhs, const boost::json::value& rhs) +{ + return !(rhs < lhs); +} + +inline bool operator<=(const boost::json::value& lhs, const boost::json::value& rhs) +{ + return !(rhs < lhs); +} + +// Greater than or equal operators +template +inline bool operator>=(const boost::json::value& lhs, const T& rhs) +{ + return !(lhs < rhs); +} + +template +inline bool operator>=(const T& lhs, const boost::json::value& rhs) +{ + return !(lhs < rhs); +} + +inline bool operator>=(const boost::json::value& lhs, const boost::json::value& rhs) +{ + return (lhs > rhs) || (lhs == rhs); +} + +// Equality operators +template +inline bool operator==(const boost::json::value& lhs, const T& rhs) +{ + return lhs == boost::json::value_from(rhs); +} + +template +inline bool operator==(const T& lhs, const boost::json::value& rhs) +{ + return rhs == lhs; +} + +// Inequality operators +template +inline bool operator!=(const T& lhs, const boost::json::value& rhs) +{ + return boost::json::value_from(lhs) != rhs; +} + +template +inline bool operator!=(const boost::json::value& lhs, const T& rhs) +{ + return rhs != lhs; +} + +} + +} // namespace boost diff --git a/cpp/3rd_party/json/src.cpp b/cpp/3rd_party/json/src.cpp new file mode 100644 index 0000000000..ce2a6f95b9 --- /dev/null +++ b/cpp/3rd_party/json/src.cpp @@ -0,0 +1,13 @@ +/** + * @file src.cpp + * @brief the file here is used to include the boost json library and treat it as a single unit and header only library. + * @note There should not be added any other code in this file. + * @ref https://github.com/boostorg/json#header-only + * @version 0.1 + * @date 2024-04-26 + * + * @copyright Copyright (c) 2024 + * + */ + +#include diff --git a/cpp/3rd_party/libtiff/CMakeLists.txt b/cpp/3rd_party/libtiff/CMakeLists.txt new file mode 100644 index 0000000000..f4f9945e87 --- /dev/null +++ b/cpp/3rd_party/libtiff/CMakeLists.txt @@ -0,0 +1,419 @@ +# ---------------------------------------------------------------------------- +# CMake file for libtiff. See root CMakeLists.txt +# +# ---------------------------------------------------------------------------- +project(${TIFF_LIBRARY}) + +include(CheckCSourceCompiles) +include(CheckFunctionExists) +include(CheckIncludeFile) +include(CheckTypeSize) + +# Find libm, if available +find_library(M_LIBRARY m) + +set(CMAKE_EXTRA_INCLUDE_FILES_SAVE ${CMAKE_EXTRA_INCLUDE_FILES}) +set(CMAKE_EXTRA_INCLUDE_FILES ${CMAKE_EXTRA_INCLUDE_FILES} "stddef.h") +set(CMAKE_EXTRA_INCLUDE_FILES ${CMAKE_EXTRA_INCLUDE_FILES_SAVE}) + +check_type_size("size_t" SIZEOF_SIZE_T) +check_type_size("ptrdiff_t" SIZEOF_PTRDIFF_T) +check_type_size("signed short" SIZEOF_SIGNED_SHORT) +check_type_size("unsigned short" SIZEOF_UNSIGNED_SHORT) +check_type_size("signed int" SIZEOF_SIGNED_INT) +check_type_size("unsigned int" SIZEOF_UNSIGNED_INT) +check_type_size("signed long" SIZEOF_SIGNED_LONG) +check_type_size("unsigned long" SIZEOF_UNSIGNED_LONG) +check_type_size("signed long long" SIZEOF_SIGNED_LONG_LONG) +check_type_size("unsigned long long" SIZEOF_UNSIGNED_LONG_LONG) +check_type_size("unsigned char *" SIZEOF_UNSIGNED_CHAR_P) + +set(TIFF_INT8_T "signed char") +set(TIFF_UINT8_T "unsigned char") + +set(TIFF_INT16_T "signed short") +set(TIFF_UINT16_T "unsigned short") + +string(TOLOWER ${CMAKE_SYSTEM_NAME} AL_SYSTEM) + +if("${AL_SYSTEM}" STREQUAL "emscripten") + set(TIFF_INT32_T "signed int") + set(TIFF_INT32_FORMAT "%d") + + set(TIFF_UINT32_T "unsigned int") + set(TIFF_UINT32_FORMAT "%u") + + set(TIFF_INT64_T "signed long") + set(TIFF_INT64_FORMAT "%ld") + + set(TIFF_UINT64_T "unsigned long") + set(TIFF_UINT64_FORMAT "%lu") + + set(TIFF_SIZE_T "unsigned long") + set(TIFF_SIZE_FORMAT "%lu") + set(TIFF_SSIZE_T "signed long") + + set(TIFF_SSIZE_FORMAT "%ld") + + set(TIFF_PTRDIFF_T "ptrdiff_t") + set(TIFF_PTRDIFF_FORMAT "%ld") + + set(SIZEOF_SIZE_T 4) + set(SIZEOF_PTRDIFF_T 4) + set(SIZEOF_SIGNED_SHORT 2) + set(SIZEOF_UNSIGNED_SHORT 2) + set(SIZEOF_SIGNED_INT 4) + set(SIZEOF_UNSIGNED_INT 4) + set(SIZEOF_SIGNED_LONG 4) + set(SIZEOF_UNSIGNED_LONG 4) + set(SIZEOF_SIGNED_LONG_LONG 8) + set(SIZEOF_UNSIGNED_LONG_LONG 8) + set(SIZEOF_UNSIGNED_CHAR_P 4) + message(STATUS "LEVON Configured libtiff for Emscripten target " , ${SIZEOF_SIZE_T}, ${SIZEOF_PTRDIFF_T}, ${SIZEOF_UNSIGNED_LONG}) + +else() + if(SIZEOF_SIGNED_INT EQUAL 4) + set(TIFF_INT32_T "signed int") + set(TIFF_INT32_FORMAT "%d") + elseif(SIZEOF_SIGNED_LONG EQUAL 4) + set(TIFF_INT32_T "signed long") + set(TIFF_INT32_FORMAT "%ld") + endif() + + if(SIZEOF_UNSIGNED_INT EQUAL 4) + set(TIFF_UINT32_T "unsigned int") + set(TIFF_UINT32_FORMAT "%u") + elseif(SIZEOF_UNSIGNED_LONG EQUAL 4) + set(TIFF_UINT32_T "unsigned long") + set(TIFF_UINT32_FORMAT "%lu") + endif() + + if(SIZEOF_SIGNED_LONG EQUAL 8) + set(TIFF_INT64_T "signed long") + set(TIFF_INT64_FORMAT "%ld") + elseif(SIZEOF_SIGNED_LONG_LONG EQUAL 8) + set(TIFF_INT64_T "signed long long") + + if(MINGW) + set(TIFF_INT64_FORMAT "%I64d") + else() + set(TIFF_INT64_FORMAT "%lld") + endif() + endif() + + if(SIZEOF_UNSIGNED_LONG EQUAL 8) + set(TIFF_UINT64_T "unsigned long") + set(TIFF_UINT64_FORMAT "%lu") + elseif(SIZEOF_UNSIGNED_LONG_LONG EQUAL 8) + set(TIFF_UINT64_T "unsigned long long") + + if(MINGW) + set(TIFF_UINT64_FORMAT "%I64u") + else() + set(TIFF_UINT64_FORMAT "%llu") + endif() + endif() + + if(SIZEOF_UNSIGNED_INT EQUAL SIZEOF_SIZE_T) + set(TIFF_SIZE_T "uint32_t") + set(TIFF_SIZE_FORMAT "%u") + set(TIFF_SSIZE_T "int32_t") + set(TIFF_SSIZE_FORMAT "%d") + elseif(SIZEOF_UNSIGNED_LONG EQUAL SIZEOF_SIZE_T) + set(TIFF_SIZE_T "uint64_t") + set(TIFF_SIZE_FORMAT "%lu") + set(TIFF_SSIZE_T "int64_t") + set(TIFF_SSIZE_FORMAT "%ld") + elseif(SIZEOF_UNSIGNED_LONG_LONG EQUAL SIZEOF_SIZE_T) + set(TIFF_SIZE_T "uint64_t") + + if(MINGW) + set(TIFF_SIZE_FORMAT "%I64u") + set(TIFF_SSIZE_FORMAT "%I64d") + else() + set(TIFF_SIZE_FORMAT "%llu") + set(TIFF_SSIZE_FORMAT "%lld") + endif() + endif() + + if(SIZEOF_SIGNED_INT EQUAL SIZEOF_UNSIGNED_CHAR_P) + elseif(SIZEOF_SIGNED_LONG EQUAL SIZEOF_UNSIGNED_CHAR_P) + elseif(SIZEOF_SIGNED_LONG_LONG EQUAL SIZEOF_UNSIGNED_CHAR_P) + set(TIFF_SSIZE_T "signed long long") + + if(MINGW) + else() + endif() + endif() + + if(NOT SIZEOF_PTRDIFF_T) + set(TIFF_PTRDIFF_T "${TIFF_SSIZE_T}") + set(TIFF_PTRDIFF_FORMAT "${SSIZE_FORMAT}") + else() + set(TIFF_PTRDIFF_T "ptrdiff_t") + set(TIFF_PTRDIFF_FORMAT "%ld") + endif() +endif() + +# Check functions +if(NOT MSVC) + set(CMAKE_REQUIRED_LIBRARIES_SAVE ${CMAKE_REQUIRED_LIBRARIES}) + set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${M_LIBRARY}) + set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_SAVE}) +endif() + +# Check for headers +include(CheckIncludeFile) +check_include_file("fcntl.h" HAVE_FCNTL_H) +check_include_file("io.h" HAVE_IO_H) +check_include_file("unistd.h" HAVE_UNISTD_H) + +# CPU bit order +set(fillorder FILLORDER_MSB2LSB) + +if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i.*86.*" OR + CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "amd64.*" OR + + # AMD64 on Windows + CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64" OR + CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64.*") + set(fillorder FILLORDER_LSB2MSB) +endif() + +set(HOST_FILLORDER ${fillorder} CACHE STRING "Native CPU bit order") +mark_as_advanced(HOST_FILLORDER) + +if(bigendian) + set(bigendian ON) +else() + set(bigendian OFF) +endif() + +set(HOST_BIG_ENDIAN ${bigendian} CACHE STRING "Native CPU bit order") +mark_as_advanced(HOST_BIG_ENDIAN) + +if(HOST_BIG_ENDIAN) + set(HOST_BIG_ENDIAN 1) +else() + set(HOST_BIG_ENDIAN 0) +endif() + +if(HOST_BIG_ENDIAN) + add_definitions(-DWORDS_BIGENDIAN) +endif() + +# IEEE floating point +set(HAVE_IEEEFP 1 CACHE STRING "IEEE floating point is available") +mark_as_advanced(HAVE_IEEEFP) + +# Large file support +if(UNIX OR MINGW) + if(ANDROID AND(ANDROID_NATIVE_API_LEVEL LESS 21) AND CV_GCC) + # Android NDK build problem: 'mmap' issue with GCC and API<21 + else() + # This might not catch every possibility catered for by + # AC_SYS_LARGEFILE. + add_definitions(-D_FILE_OFFSET_BITS=64) + set(FILE_OFFSET_BITS 64) + endif() +endif() + +# Documentation install directory (default to cmake project docdir) +set(LIBTIFF_DOCDIR "${CMAKE_INSTALL_FULL_DOCDIR}") + +# Options to enable and disable internal codecs +option(ccitt "support for CCITT Group 3 & 4 algorithms" ON) +set(CCITT_SUPPORT ${ccitt}) + +option(packbits "support for Macintosh PackBits algorithm" ON) +set(PACKBITS_SUPPORT ${packbits}) + +option(lzw "support for LZW algorithm" ON) +set(LZW_SUPPORT ${lzw}) + +option(thunder "support for ThunderScan 4-bit RLE algorithm" ON) +set(THUNDER_SUPPORT ${thunder}) + +option(next "support for NeXT 2-bit RLE algorithm" ON) +set(NEXT_SUPPORT ${next}) + +option(logluv "support for LogLuv high dynamic range algorithm" ON) +set(LOGLUV_SUPPORT ${logluv}) + +# Option for Microsoft Document Imaging +option(mdi "support for Microsoft Document Imaging" ON) +set(MDI_SUPPORT ${mdi}) + +# ZLIB +set(ZLIB_SUPPORT 0) + +if(ZLIB_LIBRARY) + set(ZLIB_SUPPORT 1) +endif() + +set(ZIP_SUPPORT ${ZLIB_SUPPORT}) + +set(PIXARLOG_SUPPORT FALSE) + +# JPEG +set(JPEG_SUPPORT FALSE) + +if(HAVE_JPEG) + set(JPEG_SUPPORT TRUE) + + if(TARGET ${JPEG_LIBRARY} AND DEFINED ${JPEG_LIBRARY}_BINARY_DIR) + include_directories("${${JPEG_LIBRARY}_BINARY_DIR}") + endif() + + include_directories(${JPEG_INCLUDE_DIR}) +endif() + +set(OJPEG_SUPPORT FALSE) + +set(JBIG_SUPPORT 0) +set(LZMA_SUPPORT 0) +set(JPEG12_FOUND FALSE) +set(STRIPCHOP_DEFAULT) +set(STRIP_SIZE_DEFAULT 8192) + +# Win32 IO +set(win32_io FALSE) + +if(WIN32 AND NOT WINRT) + set(win32_io TRUE) +endif() + +set(USE_WIN32_FILEIO ${win32_io} CACHE BOOL "Use win32 IO system (Microsoft Windows only)") + +if(USE_WIN32_FILEIO) + set(USE_WIN32_FILEIO TRUE) +else() + set(USE_WIN32_FILEIO FALSE) +endif() + +# Orthogonal features +set(SUBIFD_SUPPORT 1) +set(DEFAULT_EXTRASAMPLE_AS_ALPHA 1) +set(CHECK_JPEG_YCBCR_SUBSAMPLING 1) + +if(JPEG_INCLUDE_DIR) + list(APPEND TIFF_INCLUDES ${JPEG_INCLUDE_DIR}) +endif() + +if(JPEG12_INCLUDE_DIR) + list(APPEND TIFF_INCLUDES ${JPEG12_INCLUDE_DIR}) +endif() + +if(JBIG_INCLUDE_DIR) + list(APPEND TIFF_INCLUDES ${JBIG_INCLUDE_DIR}) +endif() + +if(LIBLZMA_INCLUDE_DIRS) + list(APPEND TIFF_INCLUDES ${LIBLZMA_INCLUDE_DIRS}) +endif() + +# Libraries required by libtiff +set(TIFF_LIBRARY_DEPS) + +if(M_LIBRARY) + list(APPEND TIFF_LIBRARY_DEPS ${M_LIBRARY}) +endif() + +if(ZLIB_LIBRARIES) + list(APPEND TIFF_LIBRARY_DEPS ${ZLIB_LIBRARIES}) +endif() + +if(JPEG_LIBRARIES) + list(APPEND TIFF_LIBRARY_DEPS ${JPEG_LIBRARIES}) +endif() + +if(JPEG12_LIBRARIES) + list(APPEND TIFF_LIBRARY_DEPS ${JPEG12_LIBRARIES}) +endif() + +if(JBIG_LIBRARIES) + list(APPEND TIFF_LIBRARY_DEPS ${JBIG_LIBRARIES}) +endif() + +if(LIBLZMA_LIBRARIES) + list(APPEND TIFF_LIBRARY_DEPS ${LIBLZMA_LIBRARIES}) +endif() + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tif_config.h.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/tif_config.h" + @ONLY) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tiffconf.h.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/tiffconf.h" + @ONLY) + +include_directories("${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" ${ZLIB_INCLUDE_DIRS}) + +set(lib_srcs + tif_aux.c + tif_close.c + tif_codec.c + tif_color.c + tif_compress.c + tif_dir.c + tif_dirinfo.c + tif_dirread.c + tif_dirwrite.c + tif_dumpmode.c + tif_error.c + tif_extension.c + tif_fax3.c + tif_fax3sm.c + tif_flush.c + tif_getimage.c + tif_hash_set.c + tif_jbig.c + tif_jpeg_12.c + tif_jpeg.c + tif_luv.c + tif_lzma.c + tif_lzw.c + tif_next.c + tif_ojpeg.c + tif_open.c + tif_packbits.c + tif_pixarlog.c + tif_predict.c + tif_print.c + tif_read.c + tif_strip.c + tif_swab.c + tif_thunder.c + tif_tile.c + tif_version.c + tif_warning.c + tif_webp.c + tif_write.c + tif_zip.c + tif_zstd.c + + # Skip C++ stream API (tif_stream.cxx) - not needed for our use case and has deprecated type issues + t4.h + tif_dir.h + tif_fax3.h + tif_hash_set.h + tiff.h + tiffio.h + tiffiop.h + tiffvers.h + tif_predict.h + uvcode.h + tiffio.hxx + "${CMAKE_CURRENT_BINARY_DIR}/tif_config.h" + "${CMAKE_CURRENT_BINARY_DIR}/tiffconf.h" +) + +if(WIN32 AND NOT WINRT) + list(APPEND lib_srcs tif_win32.c) +else() + list(APPEND lib_srcs tif_unix.c) +endif() + +add_library(${TIFF_LIBRARY} STATIC ${lib_srcs}) +target_compile_options(${TIFF_LIBRARY} PRIVATE "-w") +target_compile_definitions(${TIFF_LIBRARY} PRIVATE TIFF_DISABLE_DEPRECATED) +target_link_libraries(${TIFF_LIBRARY} PUBLIC ${ZLIB_LIBRARIES}) \ No newline at end of file diff --git a/cpp/3rd_party/libtiff/COPYRIGHT b/cpp/3rd_party/libtiff/COPYRIGHT new file mode 100644 index 0000000000..8282186151 --- /dev/null +++ b/cpp/3rd_party/libtiff/COPYRIGHT @@ -0,0 +1,21 @@ +Copyright (c) 1988-1997 Sam Leffler +Copyright (c) 1991-1997 Silicon Graphics, Inc. + +Permission to use, copy, modify, distribute, and sell this software and +its documentation for any purpose is hereby granted without fee, provided +that (i) the above copyright notices and this permission notice appear in +all copies of the software and related documentation, and (ii) the names of +Sam Leffler and Silicon Graphics may not be used in any advertising or +publicity relating to the software without the specific, prior written +permission of Sam Leffler and Silicon Graphics. + +THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, +EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY +WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + +IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR +ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF +LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. diff --git a/cpp/3rd_party/libtiff/libport.h b/cpp/3rd_party/libtiff/libport.h new file mode 100644 index 0000000000..8351414102 --- /dev/null +++ b/cpp/3rd_party/libtiff/libport.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2009 Frank Warmerdam + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _LIBPORT_ +#define _LIBPORT_ + +#if defined(HAVE_CONFIG_H) +# include +#endif + +int getopt(int argc, char * const argv[], const char *optstring); +extern char *optarg; +extern int opterr; +extern int optind; +extern int optopt; + +int strcasecmp(const char *s1, const char *s2); + +#endif /* ndef _LIBPORT_ */ diff --git a/cpp/3rd_party/libtiff/mkspans.c b/cpp/3rd_party/libtiff/mkspans.c new file mode 100644 index 0000000000..24c63ab7f6 --- /dev/null +++ b/cpp/3rd_party/libtiff/mkspans.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 1991-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include +#include + +/* + * Hack program to construct tables used to find + * runs of zeros and ones in Group 3 Fax encoding. + */ + +dumparray(name, runs) char *name; +unsigned char runs[256]; +{ + int i; + char *sep; + printf("static unsigned char %s[256] = {\n", name); + sep = " "; + for (i = 0; i < 256; i++) + { + printf("%s%d", sep, runs[i]); + if (((i + 1) % 16) == 0) + { + printf(", /* 0x%02x - 0x%02x */\n", i - 15, i); + sep = " "; + } + else + sep = ", "; + } + printf("\n};\n"); +} + +main() +{ + unsigned char runs[2][256]; + + memset(runs[0], 0, 256 * sizeof(char)); + memset(runs[1], 0, 256 * sizeof(char)); + { + register int run, runlen, i; + runlen = 1; + for (run = 0x80; run != 0xff; run = (run >> 1) | 0x80) + { + for (i = run - 1; i >= 0; i--) + { + runs[1][run | i] = runlen; + runs[0][(~(run | i)) & 0xff] = runlen; + } + runlen++; + } + runs[1][0xff] = runs[0][0] = 8; + } + dumparray("bruns", runs[0]); + dumparray("wruns", runs[1]); +} diff --git a/cpp/3rd_party/libtiff/snprintf.c b/cpp/3rd_party/libtiff/snprintf.c new file mode 100644 index 0000000000..3542ab759b --- /dev/null +++ b/cpp/3rd_party/libtiff/snprintf.c @@ -0,0 +1,42 @@ +/** + * Workaround for lack of snprintf(3) in Visual Studio. See + * http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010/8712996#8712996 + * It's a trivial wrapper around the builtin _vsnprintf_s and + * _vscprintf functions. + */ + +#ifdef _MSC_VER + +#include +#include +#include "libport.h" + +int _TIFF_vsnprintf_f(char* str, size_t size, const char* format, va_list ap) +{ + int count = -1; + + if (size != 0) +#if _MSC_VER <= 1310 + count = _vsnprintf(str, size, format, ap); +#else + count = _vsnprintf_s(str, size, _TRUNCATE, format, ap); +#endif + if (count == -1) + count = _vscprintf(format, ap); + + return count; +} + +int _TIFF_snprintf_f(char* str, size_t size, const char* format, ...) +{ + int count; + va_list ap; + + va_start(ap, format); + count = vsnprintf(str, size, format, ap); + va_end(ap); + + return count; +} + +#endif // _MSC_VER diff --git a/cpp/3rd_party/libtiff/t4.h b/cpp/3rd_party/libtiff/t4.h new file mode 100644 index 0000000000..f933d4a336 --- /dev/null +++ b/cpp/3rd_party/libtiff/t4.h @@ -0,0 +1,284 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _T4_ +#define _T4_ +/* + * CCITT T.4 1D Huffman runlength codes and + * related definitions. Given the small sizes + * of these tables it does not seem + * worthwhile to make code & length 8 bits. + */ +typedef struct tableentry +{ + unsigned short length; /* bit length of g3 code */ + unsigned short code; /* g3 code */ + short runlen; /* run length in bits */ +} tableentry; + +#define EOL 0x001 /* EOL code value - 0000 0000 0000 1 */ + +/* status values returned instead of a run length */ +#define G3CODE_EOL -1 /* NB: ACT_EOL - ACT_WRUNT */ +#define G3CODE_INVALID -2 /* NB: ACT_INVALID - ACT_WRUNT */ +#define G3CODE_EOF -3 /* end of input data */ +#define G3CODE_INCOMP -4 /* incomplete run code */ + +/* + * Note that these tables are ordered such that the + * index into the table is known to be either the + * run length, or (run length / 64) + a fixed offset. + * + * NB: The G3CODE_INVALID entries are only used + * during state generation (see mkg3states.c). + */ +#ifdef G3CODES +const tableentry TIFFFaxWhiteCodes[] = { + {8, 0x35, 0}, /* 0011 0101 */ + {6, 0x7, 1}, /* 0001 11 */ + {4, 0x7, 2}, /* 0111 */ + {4, 0x8, 3}, /* 1000 */ + {4, 0xB, 4}, /* 1011 */ + {4, 0xC, 5}, /* 1100 */ + {4, 0xE, 6}, /* 1110 */ + {4, 0xF, 7}, /* 1111 */ + {5, 0x13, 8}, /* 1001 1 */ + {5, 0x14, 9}, /* 1010 0 */ + {5, 0x7, 10}, /* 0011 1 */ + {5, 0x8, 11}, /* 0100 0 */ + {6, 0x8, 12}, /* 0010 00 */ + {6, 0x3, 13}, /* 0000 11 */ + {6, 0x34, 14}, /* 1101 00 */ + {6, 0x35, 15}, /* 1101 01 */ + {6, 0x2A, 16}, /* 1010 10 */ + {6, 0x2B, 17}, /* 1010 11 */ + {7, 0x27, 18}, /* 0100 111 */ + {7, 0xC, 19}, /* 0001 100 */ + {7, 0x8, 20}, /* 0001 000 */ + {7, 0x17, 21}, /* 0010 111 */ + {7, 0x3, 22}, /* 0000 011 */ + {7, 0x4, 23}, /* 0000 100 */ + {7, 0x28, 24}, /* 0101 000 */ + {7, 0x2B, 25}, /* 0101 011 */ + {7, 0x13, 26}, /* 0010 011 */ + {7, 0x24, 27}, /* 0100 100 */ + {7, 0x18, 28}, /* 0011 000 */ + {8, 0x2, 29}, /* 0000 0010 */ + {8, 0x3, 30}, /* 0000 0011 */ + {8, 0x1A, 31}, /* 0001 1010 */ + {8, 0x1B, 32}, /* 0001 1011 */ + {8, 0x12, 33}, /* 0001 0010 */ + {8, 0x13, 34}, /* 0001 0011 */ + {8, 0x14, 35}, /* 0001 0100 */ + {8, 0x15, 36}, /* 0001 0101 */ + {8, 0x16, 37}, /* 0001 0110 */ + {8, 0x17, 38}, /* 0001 0111 */ + {8, 0x28, 39}, /* 0010 1000 */ + {8, 0x29, 40}, /* 0010 1001 */ + {8, 0x2A, 41}, /* 0010 1010 */ + {8, 0x2B, 42}, /* 0010 1011 */ + {8, 0x2C, 43}, /* 0010 1100 */ + {8, 0x2D, 44}, /* 0010 1101 */ + {8, 0x4, 45}, /* 0000 0100 */ + {8, 0x5, 46}, /* 0000 0101 */ + {8, 0xA, 47}, /* 0000 1010 */ + {8, 0xB, 48}, /* 0000 1011 */ + {8, 0x52, 49}, /* 0101 0010 */ + {8, 0x53, 50}, /* 0101 0011 */ + {8, 0x54, 51}, /* 0101 0100 */ + {8, 0x55, 52}, /* 0101 0101 */ + {8, 0x24, 53}, /* 0010 0100 */ + {8, 0x25, 54}, /* 0010 0101 */ + {8, 0x58, 55}, /* 0101 1000 */ + {8, 0x59, 56}, /* 0101 1001 */ + {8, 0x5A, 57}, /* 0101 1010 */ + {8, 0x5B, 58}, /* 0101 1011 */ + {8, 0x4A, 59}, /* 0100 1010 */ + {8, 0x4B, 60}, /* 0100 1011 */ + {8, 0x32, 61}, /* 0011 0010 */ + {8, 0x33, 62}, /* 0011 0011 */ + {8, 0x34, 63}, /* 0011 0100 */ + {5, 0x1B, 64}, /* 1101 1 */ + {5, 0x12, 128}, /* 1001 0 */ + {6, 0x17, 192}, /* 0101 11 */ + {7, 0x37, 256}, /* 0110 111 */ + {8, 0x36, 320}, /* 0011 0110 */ + {8, 0x37, 384}, /* 0011 0111 */ + {8, 0x64, 448}, /* 0110 0100 */ + {8, 0x65, 512}, /* 0110 0101 */ + {8, 0x68, 576}, /* 0110 1000 */ + {8, 0x67, 640}, /* 0110 0111 */ + {9, 0xCC, 704}, /* 0110 0110 0 */ + {9, 0xCD, 768}, /* 0110 0110 1 */ + {9, 0xD2, 832}, /* 0110 1001 0 */ + {9, 0xD3, 896}, /* 0110 1001 1 */ + {9, 0xD4, 960}, /* 0110 1010 0 */ + {9, 0xD5, 1024}, /* 0110 1010 1 */ + {9, 0xD6, 1088}, /* 0110 1011 0 */ + {9, 0xD7, 1152}, /* 0110 1011 1 */ + {9, 0xD8, 1216}, /* 0110 1100 0 */ + {9, 0xD9, 1280}, /* 0110 1100 1 */ + {9, 0xDA, 1344}, /* 0110 1101 0 */ + {9, 0xDB, 1408}, /* 0110 1101 1 */ + {9, 0x98, 1472}, /* 0100 1100 0 */ + {9, 0x99, 1536}, /* 0100 1100 1 */ + {9, 0x9A, 1600}, /* 0100 1101 0 */ + {6, 0x18, 1664}, /* 0110 00 */ + {9, 0x9B, 1728}, /* 0100 1101 1 */ + {11, 0x8, 1792}, /* 0000 0001 000 */ + {11, 0xC, 1856}, /* 0000 0001 100 */ + {11, 0xD, 1920}, /* 0000 0001 101 */ + {12, 0x12, 1984}, /* 0000 0001 0010 */ + {12, 0x13, 2048}, /* 0000 0001 0011 */ + {12, 0x14, 2112}, /* 0000 0001 0100 */ + {12, 0x15, 2176}, /* 0000 0001 0101 */ + {12, 0x16, 2240}, /* 0000 0001 0110 */ + {12, 0x17, 2304}, /* 0000 0001 0111 */ + {12, 0x1C, 2368}, /* 0000 0001 1100 */ + {12, 0x1D, 2432}, /* 0000 0001 1101 */ + {12, 0x1E, 2496}, /* 0000 0001 1110 */ + {12, 0x1F, 2560}, /* 0000 0001 1111 */ + {12, 0x1, G3CODE_EOL}, /* 0000 0000 0001 */ + {9, 0x1, G3CODE_INVALID}, /* 0000 0000 1 */ + {10, 0x1, G3CODE_INVALID}, /* 0000 0000 01 */ + {11, 0x1, G3CODE_INVALID}, /* 0000 0000 001 */ + {12, 0x0, G3CODE_INVALID}, /* 0000 0000 0000 */ +}; + +const tableentry TIFFFaxBlackCodes[] = { + {10, 0x37, 0}, /* 0000 1101 11 */ + {3, 0x2, 1}, /* 010 */ + {2, 0x3, 2}, /* 11 */ + {2, 0x2, 3}, /* 10 */ + {3, 0x3, 4}, /* 011 */ + {4, 0x3, 5}, /* 0011 */ + {4, 0x2, 6}, /* 0010 */ + {5, 0x3, 7}, /* 0001 1 */ + {6, 0x5, 8}, /* 0001 01 */ + {6, 0x4, 9}, /* 0001 00 */ + {7, 0x4, 10}, /* 0000 100 */ + {7, 0x5, 11}, /* 0000 101 */ + {7, 0x7, 12}, /* 0000 111 */ + {8, 0x4, 13}, /* 0000 0100 */ + {8, 0x7, 14}, /* 0000 0111 */ + {9, 0x18, 15}, /* 0000 1100 0 */ + {10, 0x17, 16}, /* 0000 0101 11 */ + {10, 0x18, 17}, /* 0000 0110 00 */ + {10, 0x8, 18}, /* 0000 0010 00 */ + {11, 0x67, 19}, /* 0000 1100 111 */ + {11, 0x68, 20}, /* 0000 1101 000 */ + {11, 0x6C, 21}, /* 0000 1101 100 */ + {11, 0x37, 22}, /* 0000 0110 111 */ + {11, 0x28, 23}, /* 0000 0101 000 */ + {11, 0x17, 24}, /* 0000 0010 111 */ + {11, 0x18, 25}, /* 0000 0011 000 */ + {12, 0xCA, 26}, /* 0000 1100 1010 */ + {12, 0xCB, 27}, /* 0000 1100 1011 */ + {12, 0xCC, 28}, /* 0000 1100 1100 */ + {12, 0xCD, 29}, /* 0000 1100 1101 */ + {12, 0x68, 30}, /* 0000 0110 1000 */ + {12, 0x69, 31}, /* 0000 0110 1001 */ + {12, 0x6A, 32}, /* 0000 0110 1010 */ + {12, 0x6B, 33}, /* 0000 0110 1011 */ + {12, 0xD2, 34}, /* 0000 1101 0010 */ + {12, 0xD3, 35}, /* 0000 1101 0011 */ + {12, 0xD4, 36}, /* 0000 1101 0100 */ + {12, 0xD5, 37}, /* 0000 1101 0101 */ + {12, 0xD6, 38}, /* 0000 1101 0110 */ + {12, 0xD7, 39}, /* 0000 1101 0111 */ + {12, 0x6C, 40}, /* 0000 0110 1100 */ + {12, 0x6D, 41}, /* 0000 0110 1101 */ + {12, 0xDA, 42}, /* 0000 1101 1010 */ + {12, 0xDB, 43}, /* 0000 1101 1011 */ + {12, 0x54, 44}, /* 0000 0101 0100 */ + {12, 0x55, 45}, /* 0000 0101 0101 */ + {12, 0x56, 46}, /* 0000 0101 0110 */ + {12, 0x57, 47}, /* 0000 0101 0111 */ + {12, 0x64, 48}, /* 0000 0110 0100 */ + {12, 0x65, 49}, /* 0000 0110 0101 */ + {12, 0x52, 50}, /* 0000 0101 0010 */ + {12, 0x53, 51}, /* 0000 0101 0011 */ + {12, 0x24, 52}, /* 0000 0010 0100 */ + {12, 0x37, 53}, /* 0000 0011 0111 */ + {12, 0x38, 54}, /* 0000 0011 1000 */ + {12, 0x27, 55}, /* 0000 0010 0111 */ + {12, 0x28, 56}, /* 0000 0010 1000 */ + {12, 0x58, 57}, /* 0000 0101 1000 */ + {12, 0x59, 58}, /* 0000 0101 1001 */ + {12, 0x2B, 59}, /* 0000 0010 1011 */ + {12, 0x2C, 60}, /* 0000 0010 1100 */ + {12, 0x5A, 61}, /* 0000 0101 1010 */ + {12, 0x66, 62}, /* 0000 0110 0110 */ + {12, 0x67, 63}, /* 0000 0110 0111 */ + {10, 0xF, 64}, /* 0000 0011 11 */ + {12, 0xC8, 128}, /* 0000 1100 1000 */ + {12, 0xC9, 192}, /* 0000 1100 1001 */ + {12, 0x5B, 256}, /* 0000 0101 1011 */ + {12, 0x33, 320}, /* 0000 0011 0011 */ + {12, 0x34, 384}, /* 0000 0011 0100 */ + {12, 0x35, 448}, /* 0000 0011 0101 */ + {13, 0x6C, 512}, /* 0000 0011 0110 0 */ + {13, 0x6D, 576}, /* 0000 0011 0110 1 */ + {13, 0x4A, 640}, /* 0000 0010 0101 0 */ + {13, 0x4B, 704}, /* 0000 0010 0101 1 */ + {13, 0x4C, 768}, /* 0000 0010 0110 0 */ + {13, 0x4D, 832}, /* 0000 0010 0110 1 */ + {13, 0x72, 896}, /* 0000 0011 1001 0 */ + {13, 0x73, 960}, /* 0000 0011 1001 1 */ + {13, 0x74, 1024}, /* 0000 0011 1010 0 */ + {13, 0x75, 1088}, /* 0000 0011 1010 1 */ + {13, 0x76, 1152}, /* 0000 0011 1011 0 */ + {13, 0x77, 1216}, /* 0000 0011 1011 1 */ + {13, 0x52, 1280}, /* 0000 0010 1001 0 */ + {13, 0x53, 1344}, /* 0000 0010 1001 1 */ + {13, 0x54, 1408}, /* 0000 0010 1010 0 */ + {13, 0x55, 1472}, /* 0000 0010 1010 1 */ + {13, 0x5A, 1536}, /* 0000 0010 1101 0 */ + {13, 0x5B, 1600}, /* 0000 0010 1101 1 */ + {13, 0x64, 1664}, /* 0000 0011 0010 0 */ + {13, 0x65, 1728}, /* 0000 0011 0010 1 */ + {11, 0x8, 1792}, /* 0000 0001 000 */ + {11, 0xC, 1856}, /* 0000 0001 100 */ + {11, 0xD, 1920}, /* 0000 0001 101 */ + {12, 0x12, 1984}, /* 0000 0001 0010 */ + {12, 0x13, 2048}, /* 0000 0001 0011 */ + {12, 0x14, 2112}, /* 0000 0001 0100 */ + {12, 0x15, 2176}, /* 0000 0001 0101 */ + {12, 0x16, 2240}, /* 0000 0001 0110 */ + {12, 0x17, 2304}, /* 0000 0001 0111 */ + {12, 0x1C, 2368}, /* 0000 0001 1100 */ + {12, 0x1D, 2432}, /* 0000 0001 1101 */ + {12, 0x1E, 2496}, /* 0000 0001 1110 */ + {12, 0x1F, 2560}, /* 0000 0001 1111 */ + {12, 0x1, G3CODE_EOL}, /* 0000 0000 0001 */ + {9, 0x1, G3CODE_INVALID}, /* 0000 0000 1 */ + {10, 0x1, G3CODE_INVALID}, /* 0000 0000 01 */ + {11, 0x1, G3CODE_INVALID}, /* 0000 0000 001 */ + {12, 0x0, G3CODE_INVALID}, /* 0000 0000 0000 */ +}; +#else +extern const tableentry TIFFFaxWhiteCodes[]; +extern const tableentry TIFFFaxBlackCodes[]; +#endif +#endif /* _T4_ */ diff --git a/cpp/3rd_party/libtiff/tif_aux.c b/cpp/3rd_party/libtiff/tif_aux.c new file mode 100644 index 0000000000..31b240070c --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_aux.c @@ -0,0 +1,414 @@ +/* + * Copyright (c) 1991-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Auxiliary Support Routines. + */ +#include "tif_predict.h" +#include "tiffiop.h" +#include +#include + +uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, + const char *where) +{ + if (second && first > UINT32_MAX / second) + { + TIFFErrorExtR(tif, where, "Integer overflow in %s", where); + return 0; + } + + return first * second; +} + +uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, + const char *where) +{ + if (second && first > UINT64_MAX / second) + { + TIFFErrorExtR(tif, where, "Integer overflow in %s", where); + return 0; + } + + return first * second; +} + +tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, + const char *where) +{ + if (first <= 0 || second <= 0) + { + if (tif != NULL && where != NULL) + { + TIFFErrorExtR(tif, where, + "Invalid argument to _TIFFMultiplySSize() in %s", + where); + } + return 0; + } + + if (first > TIFF_TMSIZE_T_MAX / second) + { + if (tif != NULL && where != NULL) + { + TIFFErrorExtR(tif, where, "Integer overflow in %s", where); + } + return 0; + } + return first * second; +} + +tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module) +{ + if (val > (uint64_t)TIFF_TMSIZE_T_MAX) + { + if (tif != NULL && module != NULL) + { + TIFFErrorExtR(tif, module, "Integer overflow"); + } + return 0; + } + return (tmsize_t)val; +} + +void *_TIFFCheckRealloc(TIFF *tif, void *buffer, tmsize_t nmemb, + tmsize_t elem_size, const char *what) +{ + void *cp = NULL; + tmsize_t count = _TIFFMultiplySSize(tif, nmemb, elem_size, NULL); + /* + * Check for integer overflow. + */ + if (count != 0) + { + cp = _TIFFreallocExt(tif, buffer, count); + } + + if (cp == NULL) + { + TIFFErrorExtR(tif, tif->tif_name, + "Failed to allocate memory for %s " + "(%" TIFF_SSIZE_FORMAT " elements of %" TIFF_SSIZE_FORMAT + " bytes each)", + what, nmemb, elem_size); + } + + return cp; +} + +void *_TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, + const char *what) +{ + return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what); +} + +static int TIFFDefaultTransferFunction(TIFF *tif, TIFFDirectory *td) +{ + uint16_t **tf = td->td_transferfunction; + tmsize_t i, n, nbytes; + + tf[0] = tf[1] = tf[2] = 0; + // Do not try to generate a default TransferFunction beyond 24 bits. + // This otherwise leads to insane amounts, resulting in denial of service + // See https://github.com/OSGeo/gdal/issues/10875 + if (td->td_bitspersample > 24) + return 0; + + n = ((tmsize_t)1) << td->td_bitspersample; + nbytes = n * sizeof(uint16_t); + tf[0] = (uint16_t *)_TIFFmallocExt(tif, nbytes); + if (tf[0] == NULL) + return 0; + tf[0][0] = 0; + for (i = 1; i < n; i++) + { + double t = (double)i / ((double)n - 1.); + tf[0][i] = (uint16_t)floor(65535. * pow(t, 2.2) + .5); + } + + if (td->td_samplesperpixel - td->td_extrasamples > 1) + { + tf[1] = (uint16_t *)_TIFFmallocExt(tif, nbytes); + if (tf[1] == NULL) + goto bad; + _TIFFmemcpy(tf[1], tf[0], nbytes); + tf[2] = (uint16_t *)_TIFFmallocExt(tif, nbytes); + if (tf[2] == NULL) + goto bad; + _TIFFmemcpy(tf[2], tf[0], nbytes); + } + return 1; + +bad: + if (tf[0]) + _TIFFfreeExt(tif, tf[0]); + if (tf[1]) + _TIFFfreeExt(tif, tf[1]); + if (tf[2]) + _TIFFfreeExt(tif, tf[2]); + tf[0] = tf[1] = tf[2] = 0; + return 0; +} + +static int TIFFDefaultRefBlackWhite(TIFF *tif, TIFFDirectory *td) +{ + int i; + + td->td_refblackwhite = (float *)_TIFFmallocExt(tif, 6 * sizeof(float)); + if (td->td_refblackwhite == NULL) + return 0; + if (td->td_photometric == PHOTOMETRIC_YCBCR) + { + /* + * YCbCr (Class Y) images must have the ReferenceBlackWhite + * tag set. Fix the broken images, which lacks that tag. + */ + td->td_refblackwhite[0] = 0.0F; + td->td_refblackwhite[1] = td->td_refblackwhite[3] = + td->td_refblackwhite[5] = 255.0F; + td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F; + } + else + { + /* + * Assume RGB (Class R) + */ + for (i = 0; i < 3; i++) + { + td->td_refblackwhite[2 * i + 0] = 0; + td->td_refblackwhite[2 * i + 1] = + (float)((1L << td->td_bitspersample) - 1L); + } + } + return 1; +} + +/* + * Like TIFFGetField, but return any default + * value if the tag is not present in the directory. + * + * NB: We use the value in the directory, rather than + * explicit values so that defaults exist only one + * place in the library -- in TIFFDefaultDirectory. + */ +int TIFFVGetFieldDefaulted(TIFF *tif, uint32_t tag, va_list ap) +{ + TIFFDirectory *td = &tif->tif_dir; + + if (TIFFVGetField(tif, tag, ap)) + return (1); + switch (tag) + { + case TIFFTAG_SUBFILETYPE: + *va_arg(ap, uint32_t *) = td->td_subfiletype; + return (1); + case TIFFTAG_BITSPERSAMPLE: + *va_arg(ap, uint16_t *) = td->td_bitspersample; + return (1); + case TIFFTAG_THRESHHOLDING: + *va_arg(ap, uint16_t *) = td->td_threshholding; + return (1); + case TIFFTAG_FILLORDER: + *va_arg(ap, uint16_t *) = td->td_fillorder; + return (1); + case TIFFTAG_ORIENTATION: + *va_arg(ap, uint16_t *) = td->td_orientation; + return (1); + case TIFFTAG_SAMPLESPERPIXEL: + *va_arg(ap, uint16_t *) = td->td_samplesperpixel; + return (1); + case TIFFTAG_ROWSPERSTRIP: + *va_arg(ap, uint32_t *) = td->td_rowsperstrip; + return (1); + case TIFFTAG_MINSAMPLEVALUE: + *va_arg(ap, uint16_t *) = td->td_minsamplevalue; + return (1); + case TIFFTAG_MAXSAMPLEVALUE: + { + uint16_t maxsamplevalue; + /* td_bitspersample=1 is always set in TIFFDefaultDirectory(). + * Therefore, td_maxsamplevalue has to be re-calculated in + * TIFFGetFieldDefaulted(). */ + if (td->td_bitspersample > 0) + { + /* This shift operation into a uint16_t limits the value to + * 65535 even if td_bitspersamle is > 16 */ + if (td->td_bitspersample <= 16) + { + maxsamplevalue = (1 << td->td_bitspersample) - + 1; /* 2**(BitsPerSample) - 1 */ + } + else + { + maxsamplevalue = 65535; + } + } + else + { + maxsamplevalue = 0; + } + *va_arg(ap, uint16_t *) = maxsamplevalue; + return (1); + } + case TIFFTAG_PLANARCONFIG: + *va_arg(ap, uint16_t *) = td->td_planarconfig; + return (1); + case TIFFTAG_RESOLUTIONUNIT: + *va_arg(ap, uint16_t *) = td->td_resolutionunit; + return (1); + case TIFFTAG_PREDICTOR: + { + TIFFPredictorState *sp = (TIFFPredictorState *)tif->tif_data; + if (sp == NULL) + { + TIFFErrorExtR( + tif, tif->tif_name, + "Cannot get \"Predictor\" tag as plugin is not configured"); + *va_arg(ap, uint16_t *) = 0; + return 0; + } + *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor; + return 1; + } + case TIFFTAG_DOTRANGE: + *va_arg(ap, uint16_t *) = 0; + *va_arg(ap, uint16_t *) = (1 << td->td_bitspersample) - 1; + return (1); + case TIFFTAG_INKSET: + *va_arg(ap, uint16_t *) = INKSET_CMYK; + return 1; + case TIFFTAG_NUMBEROFINKS: + *va_arg(ap, uint16_t *) = 4; + return (1); + case TIFFTAG_EXTRASAMPLES: + *va_arg(ap, uint16_t *) = td->td_extrasamples; + *va_arg(ap, const uint16_t **) = td->td_sampleinfo; + return (1); + case TIFFTAG_MATTEING: + *va_arg(ap, uint16_t *) = + (td->td_extrasamples == 1 && + td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); + return (1); + case TIFFTAG_TILEDEPTH: + *va_arg(ap, uint32_t *) = td->td_tiledepth; + return (1); + case TIFFTAG_DATATYPE: + *va_arg(ap, uint16_t *) = td->td_sampleformat - 1; + return (1); + case TIFFTAG_SAMPLEFORMAT: + *va_arg(ap, uint16_t *) = td->td_sampleformat; + return (1); + case TIFFTAG_IMAGEDEPTH: + *va_arg(ap, uint32_t *) = td->td_imagedepth; + return (1); + case TIFFTAG_YCBCRCOEFFICIENTS: + { + /* defaults are from CCIR Recommendation 601-1 */ + static const float ycbcrcoeffs[] = {0.299f, 0.587f, 0.114f}; + *va_arg(ap, const float **) = ycbcrcoeffs; + return 1; + } + case TIFFTAG_YCBCRSUBSAMPLING: + *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0]; + *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1]; + return (1); + case TIFFTAG_YCBCRPOSITIONING: + *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning; + return (1); + case TIFFTAG_WHITEPOINT: + { + /* TIFF 6.0 specification tells that it is no default + value for the WhitePoint, but AdobePhotoshop TIFF + Technical Note tells that it should be CIE D50. */ + static const float whitepoint[] = { + D50_X0 / (D50_X0 + D50_Y0 + D50_Z0), + D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0)}; + *va_arg(ap, const float **) = whitepoint; + return 1; + } + case TIFFTAG_TRANSFERFUNCTION: + if (!td->td_transferfunction[0] && + !TIFFDefaultTransferFunction(tif, td)) + { + TIFFErrorExtR(tif, tif->tif_name, + "No space for \"TransferFunction\" tag"); + return (0); + } + *va_arg(ap, const uint16_t **) = td->td_transferfunction[0]; + if (td->td_samplesperpixel - td->td_extrasamples > 1) + { + *va_arg(ap, const uint16_t **) = td->td_transferfunction[1]; + *va_arg(ap, const uint16_t **) = td->td_transferfunction[2]; + } + return (1); + case TIFFTAG_REFERENCEBLACKWHITE: + if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(tif, td)) + return (0); + *va_arg(ap, const float **) = td->td_refblackwhite; + return (1); + } + return 0; +} + +/* + * Like TIFFGetField, but return any default + * value if the tag is not present in the directory. + */ +int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag, ...) +{ + int ok; + va_list ap; + + va_start(ap, tag); + ok = TIFFVGetFieldDefaulted(tif, tag, ap); + va_end(ap); + return (ok); +} + +float _TIFFClampDoubleToFloat(double val) +{ + if (val > FLT_MAX) + return FLT_MAX; + if (val < -FLT_MAX) + return -FLT_MAX; + return (float)val; +} + +uint32_t _TIFFClampDoubleToUInt32(double val) +{ + if (val < 0) + return 0; + if (val > 0xFFFFFFFFU || val != val) + return 0xFFFFFFFFU; + return (uint32_t)val; +} + +int _TIFFSeekOK(TIFF *tif, toff_t off) +{ + /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */ + /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */ + return off <= (~(uint64_t)0) / 2 && TIFFSeekFile(tif, off, SEEK_SET) == off; +} diff --git a/cpp/3rd_party/libtiff/tif_close.c b/cpp/3rd_party/libtiff/tif_close.c new file mode 100644 index 0000000000..d498010740 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_close.c @@ -0,0 +1,165 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + */ +#include "tiffiop.h" +#include + +/************************************************************************/ +/* TIFFCleanup() */ +/************************************************************************/ + +/** + * Auxiliary function to free the TIFF structure. Given structure will be + * completely freed, so you should save opened file handle and pointer + * to the close procedure in external variables before calling + * _TIFFCleanup(), if you will need these ones to close the file. + * + * @param tif A TIFF pointer. + */ + +void TIFFCleanup(TIFF *tif) +{ + /* + * Flush buffered data and directory (if dirty). + */ + if (tif->tif_mode != O_RDONLY) + TIFFFlush(tif); + TIFFFreeDirectory(tif); + + _TIFFCleanupIFDOffsetAndNumberMaps(tif); + + /* + * Clean up client info links. + */ + while (tif->tif_clientinfo) + { + TIFFClientInfoLink *psLink = tif->tif_clientinfo; + + tif->tif_clientinfo = psLink->next; + _TIFFfreeExt(tif, psLink->name); + _TIFFfreeExt(tif, psLink); + } + + if (tif->tif_rawdata && (tif->tif_flags & TIFF_MYBUFFER)) + _TIFFfreeExt(tif, tif->tif_rawdata); + if (isMapped(tif)) + TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size); + + /* + * Clean up custom fields. + */ + if (tif->tif_fields && tif->tif_nfields > 0) + { + uint32_t i; + + for (i = 0; i < tif->tif_nfields; i++) + { + TIFFField *fld = tif->tif_fields[i]; + if (fld->field_name != NULL) + { + if (fld->field_bit == FIELD_CUSTOM && + /* caution: tif_fields[i] must not be the beginning of a + * fields-array. Otherwise the following tags are also freed + * with the first free(). + */ + TIFFFieldIsAnonymous(fld)) + { + _TIFFfreeExt(tif, fld->field_name); + _TIFFfreeExt(tif, fld); + } + } + } + + _TIFFfreeExt(tif, tif->tif_fields); + } + + if (tif->tif_nfieldscompat > 0) + { + uint32_t i; + + for (i = 0; i < tif->tif_nfieldscompat; i++) + { + if (tif->tif_fieldscompat[i].allocated_size) + _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields); + } + _TIFFfreeExt(tif, tif->tif_fieldscompat); + } + + if (tif->tif_cur_cumulated_mem_alloc != 0) + { + TIFFErrorExtR(tif, "TIFFCleanup", + "tif_cur_cumulated_mem_alloc = %" PRIu64 " whereas it " + "should be 0", + (uint64_t)tif->tif_cur_cumulated_mem_alloc); + } + + _TIFFfreeExt(NULL, tif); +} + +/************************************************************************/ +/* _TIFFCleanupIFDOffsetAndNumberMaps() */ +/************************************************************************/ + +void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif) +{ + if (tif->tif_map_dir_offset_to_number) + { + TIFFHashSetDestroy(tif->tif_map_dir_offset_to_number); + tif->tif_map_dir_offset_to_number = NULL; + } + if (tif->tif_map_dir_number_to_offset) + { + TIFFHashSetDestroy(tif->tif_map_dir_number_to_offset); + tif->tif_map_dir_number_to_offset = NULL; + } +} + +/************************************************************************/ +/* TIFFClose() */ +/************************************************************************/ + +/** + * Close a previously opened TIFF file. + * + * TIFFClose closes a file that was previously opened with TIFFOpen(). + * Any buffered data are flushed to the file, including the contents of + * the current directory (if modified); and all resources are reclaimed. + * + * @param tif A TIFF pointer. + */ + +void TIFFClose(TIFF *tif) +{ + if (tif != NULL) + { + TIFFCloseProc closeproc = tif->tif_closeproc; + thandle_t fd = tif->tif_clientdata; + + TIFFCleanup(tif); + (void)(*closeproc)(fd); + } +} diff --git a/cpp/3rd_party/libtiff/tif_codec.c b/cpp/3rd_party/libtiff/tif_codec.c new file mode 100644 index 0000000000..d499b63a58 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_codec.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library + * + * Builtin Compression Scheme Configuration Support. + */ +#include "tiffiop.h" + +static int NotConfigured(TIFF *, int); + +#ifndef LZW_SUPPORT +#define TIFFInitLZW NotConfigured +#endif +#ifndef PACKBITS_SUPPORT +#define TIFFInitPackBits NotConfigured +#endif +#ifndef THUNDER_SUPPORT +#define TIFFInitThunderScan NotConfigured +#endif +#ifndef NEXT_SUPPORT +#define TIFFInitNeXT NotConfigured +#endif +#ifndef JPEG_SUPPORT +#define TIFFInitJPEG NotConfigured +#endif +#ifndef OJPEG_SUPPORT +#define TIFFInitOJPEG NotConfigured +#endif +#ifndef CCITT_SUPPORT +#define TIFFInitCCITTRLE NotConfigured +#define TIFFInitCCITTRLEW NotConfigured +#define TIFFInitCCITTFax3 NotConfigured +#define TIFFInitCCITTFax4 NotConfigured +#endif +#ifndef JBIG_SUPPORT +#define TIFFInitJBIG NotConfigured +#endif +#ifndef ZIP_SUPPORT +#define TIFFInitZIP NotConfigured +#endif +#ifndef PIXARLOG_SUPPORT +#define TIFFInitPixarLog NotConfigured +#endif +#ifndef LOGLUV_SUPPORT +#define TIFFInitSGILog NotConfigured +#endif +#ifndef LERC_SUPPORT +#define TIFFInitLERC NotConfigured +#endif +#ifndef LZMA_SUPPORT +#define TIFFInitLZMA NotConfigured +#endif +#ifndef ZSTD_SUPPORT +#define TIFFInitZSTD NotConfigured +#endif +#ifndef WEBP_SUPPORT +#define TIFFInitWebP NotConfigured +#endif + +/* + * Compression schemes statically built into the library. + */ +const TIFFCodec _TIFFBuiltinCODECS[] = { + {"None", COMPRESSION_NONE, TIFFInitDumpMode}, + {"LZW", COMPRESSION_LZW, TIFFInitLZW}, + {"PackBits", COMPRESSION_PACKBITS, TIFFInitPackBits}, + {"ThunderScan", COMPRESSION_THUNDERSCAN, TIFFInitThunderScan}, + {"NeXT", COMPRESSION_NEXT, TIFFInitNeXT}, + {"JPEG", COMPRESSION_JPEG, TIFFInitJPEG}, + {"Old-style JPEG", COMPRESSION_OJPEG, TIFFInitOJPEG}, + {"CCITT RLE", COMPRESSION_CCITTRLE, TIFFInitCCITTRLE}, + {"CCITT RLE/W", COMPRESSION_CCITTRLEW, TIFFInitCCITTRLEW}, + {"CCITT Group 3", COMPRESSION_CCITTFAX3, TIFFInitCCITTFax3}, + {"CCITT Group 4", COMPRESSION_CCITTFAX4, TIFFInitCCITTFax4}, + {"ISO JBIG", COMPRESSION_JBIG, TIFFInitJBIG}, + {"Deflate", COMPRESSION_DEFLATE, TIFFInitZIP}, + {"AdobeDeflate", COMPRESSION_ADOBE_DEFLATE, TIFFInitZIP}, + {"PixarLog", COMPRESSION_PIXARLOG, TIFFInitPixarLog}, + {"SGILog", COMPRESSION_SGILOG, TIFFInitSGILog}, + {"SGILog24", COMPRESSION_SGILOG24, TIFFInitSGILog}, + {"LZMA", COMPRESSION_LZMA, TIFFInitLZMA}, + {"ZSTD", COMPRESSION_ZSTD, TIFFInitZSTD}, + {"WEBP", COMPRESSION_WEBP, TIFFInitWebP}, + {"LERC", COMPRESSION_LERC, TIFFInitLERC}, + {NULL, 0, NULL}}; + +static int _notConfigured(TIFF *tif) +{ + const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression); + char compression_code[20]; + + snprintf(compression_code, sizeof(compression_code), "%" PRIu16, + tif->tif_dir.td_compression); + TIFFErrorExtR(tif, tif->tif_name, + "%s compression support is not configured", + c ? c->name : compression_code); + return (0); +} + +static int NotConfigured(TIFF *tif, int scheme) +{ + (void)scheme; + + tif->tif_fixuptags = _notConfigured; + tif->tif_decodestatus = FALSE; + tif->tif_setupdecode = _notConfigured; + tif->tif_encodestatus = FALSE; + tif->tif_setupencode = _notConfigured; + return (1); +} + +/************************************************************************/ +/* TIFFIsCODECConfigured() */ +/************************************************************************/ + +/** + * Check whether we have working codec for the specific coding scheme. + * + * @return returns 1 if the codec is configured and working. Otherwise + * 0 will be returned. + */ + +int TIFFIsCODECConfigured(uint16_t scheme) +{ + const TIFFCodec *codec = TIFFFindCODEC(scheme); + + if (codec == NULL) + { + return 0; + } + if (codec->init == NULL) + { + return 0; + } + if (codec->init != NotConfigured) + { + return 1; + } + return 0; +} diff --git a/cpp/3rd_party/libtiff/tif_color.c b/cpp/3rd_party/libtiff/tif_color.c new file mode 100644 index 0000000000..a52fdacba5 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_color.c @@ -0,0 +1,322 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * CIE L*a*b* to CIE XYZ and CIE XYZ to RGB conversion routines are taken + * from the VIPS library (http://www.vips.ecs.soton.ac.uk) with + * the permission of John Cupitt, the VIPS author. + */ + +/* + * TIFF Library. + * + * Color space conversion routines. + */ + +#include "tiffiop.h" +#include + +/* + * Convert color value from the CIE L*a*b* 1976 space to CIE XYZ. + */ +void TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, int32_t b, + float *X, float *Y, float *Z) +{ + TIFFCIELab16ToXYZ(cielab, l * 257, a * 256, b * 256, X, Y, Z); +} + +/* + * For CIELab encoded in 16 bits, L is an unsigned integer range [0,65535]. + * The a* and b* components are signed integers range [-32768,32767]. The 16 + * bit chrominance values are encoded as 256 times the 1976 CIE a* and b* + * values + */ +void TIFFCIELab16ToXYZ(TIFFCIELabToRGB *cielab, uint32_t l, int32_t a, + int32_t b, float *X, float *Y, float *Z) +{ + float L = (float)l * 100.0F / 65535.0F; + float cby, tmp; + + if (L < 8.856F) + { + *Y = (L * cielab->Y0) / 903.292F; + cby = 7.787F * (*Y / cielab->Y0) + 16.0F / 116.0F; + } + else + { + cby = (L + 16.0F) / 116.0F; + *Y = cielab->Y0 * cby * cby * cby; + } + + tmp = (float)a / 256.0F / 500.0F + cby; + if (tmp < 0.2069F) + *X = cielab->X0 * (tmp - 0.13793F) / 7.787F; + else + *X = cielab->X0 * tmp * tmp * tmp; + + tmp = cby - (float)b / 256.0F / 200.0F; + if (tmp < 0.2069F) + *Z = cielab->Z0 * (tmp - 0.13793F) / 7.787F; + else + *Z = cielab->Z0 * tmp * tmp * tmp; +} + +#define RINT(R) ((uint32_t)((R) > 0 ? ((R) + 0.5) : ((R)-0.5))) +/* + * Convert color value from the XYZ space to RGB. + */ +void TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z, + uint32_t *r, uint32_t *g, uint32_t *b) +{ + size_t i; + float Yr, Yg, Yb; + float *matrix = &cielab->display.d_mat[0][0]; + + /* Multiply through the matrix to get luminosity values. */ + Yr = matrix[0] * X + matrix[1] * Y + matrix[2] * Z; + Yg = matrix[3] * X + matrix[4] * Y + matrix[5] * Z; + Yb = matrix[6] * X + matrix[7] * Y + matrix[8] * Z; + + /* Clip input */ + Yr = TIFFmax(Yr, cielab->display.d_Y0R); + Yg = TIFFmax(Yg, cielab->display.d_Y0G); + Yb = TIFFmax(Yb, cielab->display.d_Y0B); + + /* Avoid overflow in case of wrong input values */ + Yr = TIFFmin(Yr, cielab->display.d_YCR); + Yg = TIFFmin(Yg, cielab->display.d_YCG); + Yb = TIFFmin(Yb, cielab->display.d_YCB); + + /* Turn luminosity to colour value. */ + i = (size_t)((Yr - cielab->display.d_Y0R) / cielab->rstep); + i = TIFFmin((size_t)cielab->range, i); + *r = RINT(cielab->Yr2r[i]); + + i = (size_t)((Yg - cielab->display.d_Y0G) / cielab->gstep); + i = TIFFmin((size_t)cielab->range, i); + *g = RINT(cielab->Yg2g[i]); + + i = (size_t)((Yb - cielab->display.d_Y0B) / cielab->bstep); + i = TIFFmin((size_t)cielab->range, i); + *b = RINT(cielab->Yb2b[i]); + + /* Clip output. */ + *r = TIFFmin(*r, cielab->display.d_Vrwr); + *g = TIFFmin(*g, cielab->display.d_Vrwg); + *b = TIFFmin(*b, cielab->display.d_Vrwb); +} +#undef RINT + +/* + * Allocate conversion state structures and make look_up tables for + * the Yr,Yb,Yg <=> r,g,b conversions. + */ +int TIFFCIELabToRGBInit(TIFFCIELabToRGB *cielab, const TIFFDisplay *display, + float *refWhite) +{ + size_t i; + double dfGamma; + + cielab->range = CIELABTORGB_TABLE_RANGE; + + _TIFFmemcpy(&cielab->display, display, sizeof(TIFFDisplay)); + + /* Red */ + dfGamma = 1.0 / cielab->display.d_gammaR; + cielab->rstep = + (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range; + for (i = 0; i <= (size_t)cielab->range; i++) + { + cielab->Yr2r[i] = cielab->display.d_Vrwr * + ((float)pow((double)i / cielab->range, dfGamma)); + } + + /* Green */ + dfGamma = 1.0 / cielab->display.d_gammaG; + cielab->gstep = + (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range; + for (i = 0; i <= (size_t)cielab->range; i++) + { + cielab->Yg2g[i] = cielab->display.d_Vrwg * + ((float)pow((double)i / cielab->range, dfGamma)); + } + + /* Blue */ + dfGamma = 1.0 / cielab->display.d_gammaB; + cielab->bstep = + (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range; + for (i = 0; i <= (size_t)cielab->range; i++) + { + cielab->Yb2b[i] = cielab->display.d_Vrwb * + ((float)pow((double)i / cielab->range, dfGamma)); + } + + /* Init reference white point */ + cielab->X0 = refWhite[0]; + cielab->Y0 = refWhite[1]; + cielab->Z0 = refWhite[2]; + + return 0; +} + +/* + * Convert color value from the YCbCr space to RGB. + * The colorspace conversion algorithm comes from the IJG v5a code; + * see below for more information on how it works. + */ +#define SHIFT 16 +#define FIX(x) ((int32_t)((x) * (1L << SHIFT) + 0.5)) +#define ONE_HALF ((int32_t)(1 << (SHIFT - 1))) +#define Code2V(c, RB, RW, CR) \ + ((((c) - (int32_t)(RB)) * (float)(CR)) / \ + (float)(((RW) - (RB) != 0) ? ((RW) - (RB)) : 1)) +/* !((f)>=(min)) written that way to deal with NaN */ +#define CLAMP(f, min, max) \ + ((!((f) >= (min))) ? (min) : (f) > (max) ? (max) : (f)) +#define HICLAMP(f, max) ((f) > (max) ? (max) : (f)) + +void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32_t Y, int32_t Cb, int32_t Cr, + uint32_t *r, uint32_t *g, uint32_t *b) +{ + int32_t i; + + /* XXX: Only 8-bit YCbCr input supported for now */ + Y = HICLAMP(Y, 255); + Cb = CLAMP(Cb, 0, 255); + Cr = CLAMP(Cr, 0, 255); + + i = ycbcr->Y_tab[Y] + ycbcr->Cr_r_tab[Cr]; + *r = CLAMP(i, 0, 255); + i = ycbcr->Y_tab[Y] + + (int)((ycbcr->Cb_g_tab[Cb] + ycbcr->Cr_g_tab[Cr]) >> SHIFT); + *g = CLAMP(i, 0, 255); + i = ycbcr->Y_tab[Y] + ycbcr->Cb_b_tab[Cb]; + *b = CLAMP(i, 0, 255); +} + +/* Clamp function for sanitization purposes. Normally clamping should not */ +/* occur for well behaved chroma and refBlackWhite coefficients */ +static float CLAMPw(float v, float vmin, float vmax) +{ + if (v < vmin) + { + /* printf("%f clamped to %f\n", v, vmin); */ + return vmin; + } + if (v > vmax) + { + /* printf("%f clamped to %f\n", v, vmax); */ + return vmax; + } + return v; +} + +/* + * Initialize the YCbCr->RGB conversion tables. The conversion + * is done according to the 6.0 spec: + * + * R = Y + Cr*(2 - 2*LumaRed) + * B = Y + Cb*(2 - 2*LumaBlue) + * G = Y + * - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen + * - LumaRed*Cr*(2-2*LumaRed)/LumaGreen + * + * To avoid floating point arithmetic the fractional constants that + * come out of the equations are represented as fixed point values + * in the range 0...2^16. We also eliminate multiplications by + * pre-calculating possible values indexed by Cb and Cr (this code + * assumes conversion is being done for 8-bit samples). + */ +int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *ycbcr, float *luma, float *refBlackWhite) +{ + TIFFRGBValue *clamptab; + int i; + +#define LumaRed luma[0] +#define LumaGreen luma[1] +#define LumaBlue luma[2] + + clamptab = + (TIFFRGBValue *)((uint8_t *)ycbcr + + TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long))); + _TIFFmemset(clamptab, 0, 256); /* v < 0 => 0 */ + ycbcr->clamptab = (clamptab += 256); + for (i = 0; i < 256; i++) + clamptab[i] = (TIFFRGBValue)i; + _TIFFmemset(clamptab + 256, 255, 2 * 256); /* v > 255 => 255 */ + ycbcr->Cr_r_tab = (int *)(clamptab + 3 * 256); + ycbcr->Cb_b_tab = ycbcr->Cr_r_tab + 256; + ycbcr->Cr_g_tab = (int32_t *)(ycbcr->Cb_b_tab + 256); + ycbcr->Cb_g_tab = ycbcr->Cr_g_tab + 256; + ycbcr->Y_tab = ycbcr->Cb_g_tab + 256; + + { + float f1 = 2 - 2 * LumaRed; + int32_t D1 = FIX(CLAMP(f1, 0.0F, 2.0F)); + float f2 = LumaRed * f1 / LumaGreen; + int32_t D2 = -FIX(CLAMP(f2, 0.0F, 2.0F)); + float f3 = 2 - 2 * LumaBlue; + int32_t D3 = FIX(CLAMP(f3, 0.0F, 2.0F)); + float f4 = LumaBlue * f3 / LumaGreen; + int32_t D4 = -FIX(CLAMP(f4, 0.0F, 2.0F)); + int x; + +#undef LumaBlue +#undef LumaGreen +#undef LumaRed + + /* + * i is the actual input pixel value in the range 0..255 + * Cb and Cr values are in the range -128..127 (actually + * they are in a range defined by the ReferenceBlackWhite + * tag) so there is some range shifting to do here when + * constructing tables indexed by the raw pixel data. + */ + for (i = 0, x = -128; i < 256; i++, x++) + { + int32_t Cr = (int32_t)CLAMPw(Code2V(x, refBlackWhite[4] - 128.0F, + refBlackWhite[5] - 128.0F, 127), + -128.0F * 32, 128.0F * 32); + int32_t Cb = (int32_t)CLAMPw(Code2V(x, refBlackWhite[2] - 128.0F, + refBlackWhite[3] - 128.0F, 127), + -128.0F * 32, 128.0F * 32); + + ycbcr->Cr_r_tab[i] = (int32_t)((D1 * Cr + ONE_HALF) >> SHIFT); + ycbcr->Cb_b_tab[i] = (int32_t)((D3 * Cb + ONE_HALF) >> SHIFT); + ycbcr->Cr_g_tab[i] = D2 * Cr; + ycbcr->Cb_g_tab[i] = D4 * Cb + ONE_HALF; + ycbcr->Y_tab[i] = (int32_t)CLAMPw( + Code2V(x + 128, refBlackWhite[0], refBlackWhite[1], 255), + -128.0F * 32, 128.0F * 32); + } + } + + return 0; +} +#undef HICLAMP +#undef CLAMP +#undef Code2V +#undef SHIFT +#undef ONE_HALF +#undef FIX diff --git a/cpp/3rd_party/libtiff/tif_compress.c b/cpp/3rd_party/libtiff/tif_compress.c new file mode 100644 index 0000000000..c6e17d3e11 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_compress.c @@ -0,0 +1,310 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library + * + * Compression Scheme Configuration Support. + */ +#include "tiffiop.h" + +static int TIFFNoEncode(TIFF *tif, const char *method) +{ + const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression); + + if (c) + { + TIFFErrorExtR(tif, tif->tif_name, "%s %s encoding is not implemented", + c->name, method); + } + else + { + TIFFErrorExtR(tif, tif->tif_name, + "Compression scheme %" PRIu16 + " %s encoding is not implemented", + tif->tif_dir.td_compression, method); + } + return (-1); +} + +int _TIFFNoRowEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)pp; + (void)cc; + (void)s; + return (TIFFNoEncode(tif, "scanline")); +} + +int _TIFFNoStripEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)pp; + (void)cc; + (void)s; + return (TIFFNoEncode(tif, "strip")); +} + +int _TIFFNoTileEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)pp; + (void)cc; + (void)s; + return (TIFFNoEncode(tif, "tile")); +} + +static int TIFFNoDecode(TIFF *tif, const char *method) +{ + const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression); + + if (c) + TIFFErrorExtR(tif, tif->tif_name, "%s %s decoding is not implemented", + c->name, method); + else + TIFFErrorExtR(tif, tif->tif_name, + "Compression scheme %" PRIu16 + " %s decoding is not implemented", + tif->tif_dir.td_compression, method); + return (0); +} + +static int _TIFFNoFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +int _TIFFNoRowDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)pp; + (void)cc; + (void)s; + return (TIFFNoDecode(tif, "scanline")); +} + +int _TIFFNoStripDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)pp; + (void)cc; + (void)s; + return (TIFFNoDecode(tif, "strip")); +} + +int _TIFFNoTileDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)pp; + (void)cc; + (void)s; + return (TIFFNoDecode(tif, "tile")); +} + +int _TIFFNoSeek(TIFF *tif, uint32_t off) +{ + (void)off; + TIFFErrorExtR(tif, tif->tif_name, + "Compression algorithm does not support random access"); + return (0); +} + +int _TIFFNoPreCode(TIFF *tif, uint16_t s) +{ + (void)tif; + (void)s; + return (1); +} + +static int _TIFFtrue(TIFF *tif) +{ + (void)tif; + return (1); +} +static void _TIFFvoid(TIFF *tif) { (void)tif; } + +void _TIFFSetDefaultCompressionState(TIFF *tif) +{ + tif->tif_fixuptags = _TIFFNoFixupTags; + tif->tif_decodestatus = TRUE; + tif->tif_setupdecode = _TIFFtrue; + tif->tif_predecode = _TIFFNoPreCode; + tif->tif_decoderow = _TIFFNoRowDecode; + tif->tif_decodestrip = _TIFFNoStripDecode; + tif->tif_decodetile = _TIFFNoTileDecode; + tif->tif_encodestatus = TRUE; + tif->tif_setupencode = _TIFFtrue; + tif->tif_preencode = _TIFFNoPreCode; + tif->tif_postencode = _TIFFtrue; + tif->tif_encoderow = _TIFFNoRowEncode; + tif->tif_encodestrip = _TIFFNoStripEncode; + tif->tif_encodetile = _TIFFNoTileEncode; + tif->tif_close = _TIFFvoid; + tif->tif_seek = _TIFFNoSeek; + tif->tif_cleanup = _TIFFvoid; + tif->tif_defstripsize = _TIFFDefaultStripSize; + tif->tif_deftilesize = _TIFFDefaultTileSize; + tif->tif_flags &= ~(TIFF_NOBITREV | TIFF_NOREADRAW); +} + +int TIFFSetCompressionScheme(TIFF *tif, int scheme) +{ + const TIFFCodec *c = TIFFFindCODEC((uint16_t)scheme); + + _TIFFSetDefaultCompressionState(tif); + /* + * Don't treat an unknown compression scheme as an error. + * This permits applications to open files with data that + * the library does not have builtin support for, but which + * may still be meaningful. + */ + return (c ? (*c->init)(tif, scheme) : 1); +} + +/* + * Other compression schemes may be registered. Registered + * schemes can also override the builtin versions provided + * by this library. + */ +typedef struct _codec +{ + struct _codec *next; + TIFFCodec *info; +} codec_t; +static codec_t *registeredCODECS = NULL; + +const TIFFCodec *TIFFFindCODEC(uint16_t scheme) +{ + const TIFFCodec *c; + codec_t *cd; + + for (cd = registeredCODECS; cd; cd = cd->next) + if (cd->info->scheme == scheme) + return ((const TIFFCodec *)cd->info); + for (c = _TIFFBuiltinCODECS; c->name; c++) + if (c->scheme == scheme) + return (c); + return ((const TIFFCodec *)0); +} + +TIFFCodec *TIFFRegisterCODEC(uint16_t scheme, const char *name, + TIFFInitMethod init) +{ + codec_t *cd = (codec_t *)_TIFFmallocExt( + NULL, + (tmsize_t)(sizeof(codec_t) + sizeof(TIFFCodec) + strlen(name) + 1)); + + if (cd != NULL) + { + cd->info = (TIFFCodec *)((uint8_t *)cd + sizeof(codec_t)); + cd->info->name = (char *)((uint8_t *)cd->info + sizeof(TIFFCodec)); + strcpy(cd->info->name, name); + cd->info->scheme = scheme; + cd->info->init = init; + cd->next = registeredCODECS; + registeredCODECS = cd; + } + else + { + TIFFErrorExt(0, "TIFFRegisterCODEC", + "No space to register compression scheme %s", name); + return NULL; + } + return (cd->info); +} + +void TIFFUnRegisterCODEC(TIFFCodec *c) +{ + codec_t *cd; + codec_t **pcd; + + for (pcd = ®isteredCODECS; (cd = *pcd) != NULL; pcd = &cd->next) + if (cd->info == c) + { + *pcd = cd->next; + _TIFFfreeExt(NULL, cd); + return; + } + TIFFErrorExt(0, "TIFFUnRegisterCODEC", + "Cannot remove compression scheme %s; not registered", + c->name); +} + +/************************************************************************/ +/* TIFFGetConfisuredCODECs() */ +/************************************************************************/ + +/** + * Get list of configured codecs, both built-in and registered by user. + * Caller is responsible to free this structure. + * + * @return returns array of TIFFCodec records (the last record should be NULL) + * or NULL if function failed. + */ + +TIFFCodec *TIFFGetConfiguredCODECs() +{ + int i = 1; + codec_t *cd; + const TIFFCodec *c; + TIFFCodec *codecs = NULL; + TIFFCodec *new_codecs; + + for (cd = registeredCODECS; cd; cd = cd->next) + { + new_codecs = + (TIFFCodec *)_TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec)); + if (!new_codecs) + { + _TIFFfreeExt(NULL, codecs); + return NULL; + } + codecs = new_codecs; + _TIFFmemcpy(codecs + i - 1, cd->info, sizeof(TIFFCodec)); + i++; + } + for (c = _TIFFBuiltinCODECS; c->name; c++) + { + if (TIFFIsCODECConfigured(c->scheme)) + { + new_codecs = (TIFFCodec *)_TIFFreallocExt(NULL, codecs, + i * sizeof(TIFFCodec)); + if (!new_codecs) + { + _TIFFfreeExt(NULL, codecs); + return NULL; + } + codecs = new_codecs; + _TIFFmemcpy(codecs + i - 1, (const void *)c, sizeof(TIFFCodec)); + i++; + } + } + + new_codecs = + (TIFFCodec *)_TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec)); + if (!new_codecs) + { + _TIFFfreeExt(NULL, codecs); + return NULL; + } + codecs = new_codecs; + _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec)); + + return codecs; +} diff --git a/cpp/3rd_party/libtiff/tif_config.h.cmake.in b/cpp/3rd_party/libtiff/tif_config.h.cmake.in new file mode 100644 index 0000000000..e48a6a1629 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_config.h.cmake.in @@ -0,0 +1,176 @@ +/* libtiff/tif_config.h.cmake.in. Not generated, but originated from autoheader. */ +/* This file must be kept up-to-date with needed substitutions from libtiff/tif_config.h.in. */ + +#ifdef HAVE_FCNTL_H +#include +#endif + +#ifdef HAVE_IO_H +#include +#endif + +/* Support CCITT Group 3 & 4 algorithms */ +#cmakedefine CCITT_SUPPORT 1 + +/* Pick up YCbCr subsampling info from the JPEG data stream to support files + lacking the tag (default enabled). */ +#cmakedefine CHECK_JPEG_YCBCR_SUBSAMPLING 1 + +/* enable partial strip reading for large strips (experimental) */ +#cmakedefine CHUNKY_STRIP_READ_SUPPORT 1 + +/* Support C++ stream API (requires C++ compiler) */ +#cmakedefine CXX_SUPPORT 1 + +/* enable deferred strip/tile offset/size loading (experimental) */ +#cmakedefine DEFER_STRILE_LOAD 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_GLUT_GLUT_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_GL_GLUT_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_GL_GLU_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_GL_GL_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_FCNTL_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_IO_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_UNISTD_H 1 + +/* Define to 1 if you have the `jbg_newlen' function. */ +#cmakedefine HAVE_JBG_NEWLEN 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_OPENGL_GLU_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_OPENGL_GL_H 1 + +/* Define to 1 if you have the `strtoul' function. */ +#cmakedefine HAVE_STRTOUL 1 + +/* 8/12 bit libjpeg dual mode enabled */ +#cmakedefine JPEG_DUAL_MODE_8_12 1 + +/* 12bit libjpeg primary include file with path */ +#define LIBJPEG_12_PATH @LIBJPEG_12_PATH@ + +/* Support LZMA2 compression */ +#cmakedefine LZMA_SUPPORT 1 + +/* Support ZSTD compression */ +#cmakedefine ZSTD_SUPPORT 1 + +/* Support WEBP compression */ +#cmakedefine WEBP_SUPPORT 1 + +/* Name of package */ +#define PACKAGE "@PACKAGE_NAME@" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "@PACKAGE_BUGREPORT@" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "@PACKAGE_NAME@" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "@PACKAGE_STRING@" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "@PACKAGE_TARNAME@" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "@PACKAGE_URL@" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "@PACKAGE_VERSION@" + +/* The size of `signed int', as computed by sizeof. */ +#define SIZEOF_SIGNED_INT @SIZEOF_SIGNED_INT@ + +/* The size of `signed long', as computed by sizeof. */ +#define SIZEOF_SIGNED_LONG @SIZEOF_SIGNED_LONG@ + +/* The size of `signed long long', as computed by sizeof. */ +#define SIZEOF_SIGNED_LONG_LONG @SIZEOF_SIGNED_LONG_LONG@ + +/* The size of `unsigned char *', as computed by sizeof. */ +#define SIZEOF_UNSIGNED_CHAR_P @SIZEOF_UNSIGNED_CHAR_P@ + +/* The size of `unsigned int', as computed by sizeof. */ +#define SIZEOF_UNSIGNED_INT @SIZEOF_UNSIGNED_INT@ + +/* The size of `unsigned long', as computed by sizeof. */ +#define SIZEOF_UNSIGNED_LONG @SIZEOF_UNSIGNED_LONG@ + +/* The size of `unsigned long long', as computed by sizeof. */ +#define SIZEOF_UNSIGNED_LONG_LONG @SIZEOF_UNSIGNED_LONG_LONG@ + +/* The size of `unsigned short', as computed by sizeof. */ +#define SIZEOF_UNSIGNED_SHORT @SIZEOF_UNSIGNED_SHORT@ + +/* Default size of the strip in bytes (when strip chopping enabled) */ +#define STRIP_SIZE_DEFAULT @STRIP_SIZE_DEFAULT@ + +/* Signed 32-bit type formatter */ +#define TIFF_INT32_FORMAT "@TIFF_INT32_FORMAT@" + +/* Signed 64-bit type formatter */ +#define TIFF_INT64_FORMAT "@TIFF_INT64_FORMAT@" + +/* Pointer difference type formatter */ +#define TIFF_PTRDIFF_FORMAT "@TIFF_PTRDIFF_FORMAT@" + +/* Unsigned size type formatter */ +#define TIFF_SIZE_FORMAT "@TIFF_SIZE_FORMAT@" + +/* Signed size type formatter */ +#define TIFF_SSIZE_FORMAT "@TIFF_SSIZE_FORMAT@" + +/* Unsigned 32-bit type formatter */ +#define TIFF_UINT32_FORMAT "@TIFF_UINT32_FORMAT@" + +/* Unsigned 64-bit type formatter */ +#define TIFF_UINT64_FORMAT "@TIFF_UINT64_FORMAT@" + +/* Unsigned 8-bit type */ +#define TIFF_UINT8_T @TIFF_UINT8_T@ + +/* Define to 1 if you can safely include both and . */ +#undef TIME_WITH_SYS_TIME + +/* Define to 1 if your declares `struct tm'. */ +#cmakedefine TM_IN_SYS_TIME 1 + +/* define to use win32 IO system */ +#cmakedefine USE_WIN32_FILEIO 1 + +/* Version number of package */ +#define VERSION "@PACKAGE_VERSION@" + +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#if defined AC_APPLE_UNIVERSAL_BUILD +# if defined __BIG_ENDIAN__ +# define WORDS_BIGENDIAN 1 +# endif +#else +# ifndef WORDS_BIGENDIAN +# undef WORDS_BIGENDIAN +# endif +#endif + +/* Number of bits in a file offset, on hosts where this is settable. */ +#define _FILE_OFFSET_BITS @FILE_OFFSET_BITS@ + +/* The size of `size_t', as computed by sizeof. */ +#define SIZEOF_SIZE_T @SIZEOF_SIZE_T@ diff --git a/cpp/3rd_party/libtiff/tif_dir.c b/cpp/3rd_party/libtiff/tif_dir.c new file mode 100644 index 0000000000..7421c738db --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_dir.c @@ -0,0 +1,2366 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Directory Tag Get & Set Routines. + * (and also some miscellaneous stuff) + */ +#include "tiffiop.h" +#include /*--: for Rational2Double */ +#include + +/* + * These are used in the backwards compatibility code... + */ +#define DATATYPE_VOID 0 /* !untyped data */ +#define DATATYPE_INT 1 /* !signed integer data */ +#define DATATYPE_UINT 2 /* !unsigned integer data */ +#define DATATYPE_IEEEFP 3 /* !IEEE floating point data */ + +static void setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb, + size_t elem_size) +{ + if (*vpp) + { + _TIFFfreeExt(tif, *vpp); + *vpp = 0; + } + if (vp) + { + tmsize_t bytes = _TIFFMultiplySSize(NULL, nmemb, elem_size, NULL); + if (bytes) + *vpp = (void *)_TIFFmallocExt(tif, bytes); + if (*vpp) + _TIFFmemcpy(*vpp, vp, bytes); + } +} +void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n) +{ + setByteArray(NULL, vpp, vp, n, 1); +} +void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n) +{ + setByteArray(tif, vpp, vp, n, 1); +} + +static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n) +{ + setByteArray(tif, (void **)cpp, cp, n, 1); +} + +void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n) +{ + setByteArray(NULL, (void **)wpp, wp, n, sizeof(uint16_t)); +} +void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp, + uint32_t n) +{ + setByteArray(tif, (void **)wpp, wp, n, sizeof(uint16_t)); +} + +void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n) +{ + setByteArray(NULL, (void **)lpp, lp, n, sizeof(uint32_t)); +} +void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp, + uint32_t n) +{ + setByteArray(tif, (void **)lpp, lp, n, sizeof(uint32_t)); +} + +static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp, + uint32_t n) +{ + setByteArray(tif, (void **)lpp, lp, n, sizeof(uint64_t)); +} + +void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n) +{ + setByteArray(NULL, (void **)fpp, fp, n, sizeof(float)); +} +void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n) +{ + setByteArray(tif, (void **)fpp, fp, n, sizeof(float)); +} + +void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n) +{ + setByteArray(NULL, (void **)dpp, dp, n, sizeof(double)); +} +void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp, + uint32_t n) +{ + setByteArray(tif, (void **)dpp, dp, n, sizeof(double)); +} + +static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value, + size_t nmemb) +{ + if (*vpp) + _TIFFfreeExt(tif, *vpp); + *vpp = _TIFFmallocExt(tif, nmemb * sizeof(double)); + if (*vpp) + { + while (nmemb--) + ((double *)*vpp)[nmemb] = value; + } +} + +/* + * Install extra samples information. + */ +static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v) +{ +/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */ +#define EXTRASAMPLE_COREL_UNASSALPHA 999 + + uint16_t *va; + uint32_t i; + TIFFDirectory *td = &tif->tif_dir; + static const char module[] = "setExtraSamples"; + + *v = (uint16_t)va_arg(ap, uint16_vap); + if ((uint16_t)*v > td->td_samplesperpixel) + return 0; + va = va_arg(ap, uint16_t *); + if (*v > 0 && va == NULL) /* typically missing param */ + return 0; + for (i = 0; i < *v; i++) + { + if (va[i] > EXTRASAMPLE_UNASSALPHA) + { + /* + * XXX: Corel Draw is known to produce incorrect + * ExtraSamples tags which must be patched here if we + * want to be able to open some of the damaged TIFF + * files: + */ + if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA) + va[i] = EXTRASAMPLE_UNASSALPHA; + else + return 0; + } + } + + if (td->td_transferfunction[0] != NULL && + (td->td_samplesperpixel - *v > 1) && + !(td->td_samplesperpixel - td->td_extrasamples > 1)) + { + TIFFWarningExtR(tif, module, + "ExtraSamples tag value is changing, " + "but TransferFunction was read with a different value. " + "Canceling it"); + TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION); + _TIFFfreeExt(tif, td->td_transferfunction[0]); + td->td_transferfunction[0] = NULL; + } + + td->td_extrasamples = (uint16_t)*v; + _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, va, td->td_extrasamples); + return 1; + +#undef EXTRASAMPLE_COREL_UNASSALPHA +} + +/* + * Count ink names separated by \0. Returns + * zero if the ink names are not as expected. + */ +static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s) +{ + uint16_t i = 0; + + if (slen > 0) + { + const char *ep = s + slen; + const char *cp = s; + do + { + for (; cp < ep && *cp != '\0'; cp++) + { + } + if (cp >= ep) + goto bad; + cp++; /* skip \0 */ + i++; + } while (cp < ep); + return (i); + } +bad: + TIFFErrorExtR(tif, "TIFFSetField", + "%s: Invalid InkNames value; no null at given buffer end " + "location %" PRIu32 ", after %" PRIu16 " ink", + tif->tif_name, slen, i); + return (0); +} + +static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "_TIFFVSetField"; + + TIFFDirectory *td = &tif->tif_dir; + int status = 1; + uint32_t v32, v; + double dblval; + char *s; + const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); + uint32_t standard_tag = tag; + if (fip == NULL) /* cannot happen since OkToChangeTag() already checks it */ + return 0; + /* + * We want to force the custom code to be used for custom + * fields even if the tag happens to match a well known + * one - important for reinterpreted handling of standard + * tag values in custom directories (i.e. EXIF) + */ + if (fip->field_bit == FIELD_CUSTOM) + { + standard_tag = 0; + } + + switch (standard_tag) + { + case TIFFTAG_SUBFILETYPE: + td->td_subfiletype = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_IMAGEWIDTH: + td->td_imagewidth = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_IMAGELENGTH: + td->td_imagelength = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_BITSPERSAMPLE: + td->td_bitspersample = (uint16_t)va_arg(ap, uint16_vap); + /* + * If the data require post-decoding processing to byte-swap + * samples, set it up here. Note that since tags are required + * to be ordered, compression code can override this behavior + * in the setup method if it wants to roll the post decoding + * work in with its normal work. + */ + if (tif->tif_flags & TIFF_SWAB) + { + if (td->td_bitspersample == 8) + tif->tif_postdecode = _TIFFNoPostDecode; + else if (td->td_bitspersample == 16) + tif->tif_postdecode = _TIFFSwab16BitData; + else if (td->td_bitspersample == 24) + tif->tif_postdecode = _TIFFSwab24BitData; + else if (td->td_bitspersample == 32) + tif->tif_postdecode = _TIFFSwab32BitData; + else if (td->td_bitspersample == 64) + tif->tif_postdecode = _TIFFSwab64BitData; + else if (td->td_bitspersample == 128) /* two 64's */ + tif->tif_postdecode = _TIFFSwab64BitData; + } + break; + case TIFFTAG_COMPRESSION: + v = (uint16_t)va_arg(ap, uint16_vap); + /* + * If we're changing the compression scheme, notify the + * previous module so that it can cleanup any state it's + * setup. + */ + if (TIFFFieldSet(tif, FIELD_COMPRESSION)) + { + if ((uint32_t)td->td_compression == v) + break; + (*tif->tif_cleanup)(tif); + tif->tif_flags &= ~TIFF_CODERSETUP; + } + /* + * Setup new compression routine state. + */ + if ((status = TIFFSetCompressionScheme(tif, v)) != 0) + td->td_compression = (uint16_t)v; + else + status = 0; + break; + case TIFFTAG_PHOTOMETRIC: + td->td_photometric = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_THRESHHOLDING: + td->td_threshholding = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_FILLORDER: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB) + goto badvalue; + td->td_fillorder = (uint16_t)v; + break; + case TIFFTAG_ORIENTATION: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) + goto badvalue; + else + td->td_orientation = (uint16_t)v; + break; + case TIFFTAG_SAMPLESPERPIXEL: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v == 0) + goto badvalue; + if (v != td->td_samplesperpixel) + { + /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */ + if (td->td_sminsamplevalue != NULL) + { + TIFFWarningExtR(tif, module, + "SamplesPerPixel tag value is changing, " + "but SMinSampleValue tag was read with a " + "different value. Canceling it"); + TIFFClrFieldBit(tif, FIELD_SMINSAMPLEVALUE); + _TIFFfreeExt(tif, td->td_sminsamplevalue); + td->td_sminsamplevalue = NULL; + } + if (td->td_smaxsamplevalue != NULL) + { + TIFFWarningExtR(tif, module, + "SamplesPerPixel tag value is changing, " + "but SMaxSampleValue tag was read with a " + "different value. Canceling it"); + TIFFClrFieldBit(tif, FIELD_SMAXSAMPLEVALUE); + _TIFFfreeExt(tif, td->td_smaxsamplevalue); + td->td_smaxsamplevalue = NULL; + } + /* Test if 3 transfer functions instead of just one are now + needed See http://bugzilla.maptools.org/show_bug.cgi?id=2820 + */ + if (td->td_transferfunction[0] != NULL && + (v - td->td_extrasamples > 1) && + !(td->td_samplesperpixel - td->td_extrasamples > 1)) + { + TIFFWarningExtR(tif, module, + "SamplesPerPixel tag value is changing, " + "but TransferFunction was read with a " + "different value. Canceling it"); + TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION); + _TIFFfreeExt(tif, td->td_transferfunction[0]); + td->td_transferfunction[0] = NULL; + } + } + td->td_samplesperpixel = (uint16_t)v; + break; + case TIFFTAG_ROWSPERSTRIP: + v32 = (uint32_t)va_arg(ap, uint32_t); + if (v32 == 0) + goto badvalue32; + td->td_rowsperstrip = v32; + if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) + { + td->td_tilelength = v32; + td->td_tilewidth = td->td_imagewidth; + } + break; + case TIFFTAG_MINSAMPLEVALUE: + td->td_minsamplevalue = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_MAXSAMPLEVALUE: + td->td_maxsamplevalue = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_SMINSAMPLEVALUE: + if (tif->tif_flags & TIFF_PERSAMPLE) + _TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue, + va_arg(ap, double *), + td->td_samplesperpixel); + else + setDoubleArrayOneValue(tif, &td->td_sminsamplevalue, + va_arg(ap, double), + td->td_samplesperpixel); + break; + case TIFFTAG_SMAXSAMPLEVALUE: + if (tif->tif_flags & TIFF_PERSAMPLE) + _TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue, + va_arg(ap, double *), + td->td_samplesperpixel); + else + setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue, + va_arg(ap, double), + td->td_samplesperpixel); + break; + case TIFFTAG_XRESOLUTION: + dblval = va_arg(ap, double); + if (dblval != dblval || dblval < 0) + goto badvaluedouble; + td->td_xresolution = _TIFFClampDoubleToFloat(dblval); + break; + case TIFFTAG_YRESOLUTION: + dblval = va_arg(ap, double); + if (dblval != dblval || dblval < 0) + goto badvaluedouble; + td->td_yresolution = _TIFFClampDoubleToFloat(dblval); + break; + case TIFFTAG_PLANARCONFIG: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE) + goto badvalue; + td->td_planarconfig = (uint16_t)v; + break; + case TIFFTAG_XPOSITION: + td->td_xposition = _TIFFClampDoubleToFloat(va_arg(ap, double)); + break; + case TIFFTAG_YPOSITION: + td->td_yposition = _TIFFClampDoubleToFloat(va_arg(ap, double)); + break; + case TIFFTAG_RESOLUTIONUNIT: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v) + goto badvalue; + td->td_resolutionunit = (uint16_t)v; + break; + case TIFFTAG_PAGENUMBER: + td->td_pagenumber[0] = (uint16_t)va_arg(ap, uint16_vap); + td->td_pagenumber[1] = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_HALFTONEHINTS: + td->td_halftonehints[0] = (uint16_t)va_arg(ap, uint16_vap); + td->td_halftonehints[1] = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_COLORMAP: + v32 = (uint32_t)(1L << td->td_bitspersample); + _TIFFsetShortArrayExt(tif, &td->td_colormap[0], + va_arg(ap, uint16_t *), v32); + _TIFFsetShortArrayExt(tif, &td->td_colormap[1], + va_arg(ap, uint16_t *), v32); + _TIFFsetShortArrayExt(tif, &td->td_colormap[2], + va_arg(ap, uint16_t *), v32); + break; + case TIFFTAG_EXTRASAMPLES: + if (!setExtraSamples(tif, ap, &v)) + goto badvalue; + break; + case TIFFTAG_MATTEING: + td->td_extrasamples = (((uint16_t)va_arg(ap, uint16_vap)) != 0); + if (td->td_extrasamples) + { + uint16_t sv = EXTRASAMPLE_ASSOCALPHA; + _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1); + } + break; + case TIFFTAG_TILEWIDTH: + v32 = (uint32_t)va_arg(ap, uint32_t); + if (v32 % 16) + { + if (tif->tif_mode != O_RDONLY) + goto badvalue32; + TIFFWarningExtR( + tif, tif->tif_name, + "Nonstandard tile width %" PRIu32 ", convert file", v32); + } + td->td_tilewidth = v32; + tif->tif_flags |= TIFF_ISTILED; + break; + case TIFFTAG_TILELENGTH: + v32 = (uint32_t)va_arg(ap, uint32_t); + if (v32 % 16) + { + if (tif->tif_mode != O_RDONLY) + goto badvalue32; + TIFFWarningExtR( + tif, tif->tif_name, + "Nonstandard tile length %" PRIu32 ", convert file", v32); + } + td->td_tilelength = v32; + tif->tif_flags |= TIFF_ISTILED; + break; + case TIFFTAG_TILEDEPTH: + v32 = (uint32_t)va_arg(ap, uint32_t); + if (v32 == 0) + goto badvalue32; + td->td_tiledepth = v32; + break; + case TIFFTAG_DATATYPE: + v = (uint16_t)va_arg(ap, uint16_vap); + switch (v) + { + case DATATYPE_VOID: + v = SAMPLEFORMAT_VOID; + break; + case DATATYPE_INT: + v = SAMPLEFORMAT_INT; + break; + case DATATYPE_UINT: + v = SAMPLEFORMAT_UINT; + break; + case DATATYPE_IEEEFP: + v = SAMPLEFORMAT_IEEEFP; + break; + default: + goto badvalue; + } + td->td_sampleformat = (uint16_t)v; + break; + case TIFFTAG_SAMPLEFORMAT: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v) + goto badvalue; + td->td_sampleformat = (uint16_t)v; + + /* Try to fix up the SWAB function for complex data. */ + if (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT && + td->td_bitspersample == 32 && + tif->tif_postdecode == _TIFFSwab32BitData) + tif->tif_postdecode = _TIFFSwab16BitData; + else if ((td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT || + td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) && + td->td_bitspersample == 64 && + tif->tif_postdecode == _TIFFSwab64BitData) + tif->tif_postdecode = _TIFFSwab32BitData; + break; + case TIFFTAG_IMAGEDEPTH: + td->td_imagedepth = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_SUBIFD: + if ((tif->tif_flags & TIFF_INSUBIFD) == 0) + { + td->td_nsubifd = (uint16_t)va_arg(ap, uint16_vap); + _TIFFsetLong8Array(tif, &td->td_subifd, + (uint64_t *)va_arg(ap, uint64_t *), + (uint32_t)td->td_nsubifd); + } + else + { + TIFFErrorExtR(tif, module, "%s: Sorry, cannot nest SubIFDs", + tif->tif_name); + status = 0; + } + break; + case TIFFTAG_YCBCRPOSITIONING: + td->td_ycbcrpositioning = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_YCBCRSUBSAMPLING: + td->td_ycbcrsubsampling[0] = (uint16_t)va_arg(ap, uint16_vap); + td->td_ycbcrsubsampling[1] = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_TRANSFERFUNCTION: + { + uint32_t i; + v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1; + for (i = 0; i < v; i++) + _TIFFsetShortArrayExt(tif, &td->td_transferfunction[i], + va_arg(ap, uint16_t *), + 1U << td->td_bitspersample); + break; + } + case TIFFTAG_REFERENCEBLACKWHITE: + /* XXX should check for null range */ + _TIFFsetFloatArrayExt(tif, &td->td_refblackwhite, + va_arg(ap, float *), 6); + break; + case TIFFTAG_INKNAMES: + { + v = (uint16_t)va_arg(ap, uint16_vap); + s = va_arg(ap, char *); + uint16_t ninksinstring; + ninksinstring = countInkNamesString(tif, v, s); + status = ninksinstring > 0; + if (ninksinstring > 0) + { + _TIFFsetNString(tif, &td->td_inknames, s, v); + td->td_inknameslen = v; + /* Set NumberOfInks to the value ninksinstring */ + if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS)) + { + if (td->td_numberofinks != ninksinstring) + { + TIFFErrorExtR( + tif, module, + "Warning %s; Tag %s:\n Value %" PRIu16 + " of NumberOfInks is different from the number of " + "inks %" PRIu16 + ".\n -> NumberOfInks value adapted to %" PRIu16 "", + tif->tif_name, fip->field_name, td->td_numberofinks, + ninksinstring, ninksinstring); + td->td_numberofinks = ninksinstring; + } + } + else + { + td->td_numberofinks = ninksinstring; + TIFFSetFieldBit(tif, FIELD_NUMBEROFINKS); + } + if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) + { + if (td->td_numberofinks != td->td_samplesperpixel) + { + TIFFErrorExtR(tif, module, + "Warning %s; Tag %s:\n Value %" PRIu16 + " of NumberOfInks is different from the " + "SamplesPerPixel value %" PRIu16 "", + tif->tif_name, fip->field_name, + td->td_numberofinks, + td->td_samplesperpixel); + } + } + } + } + break; + case TIFFTAG_NUMBEROFINKS: + v = (uint16_t)va_arg(ap, uint16_vap); + /* If InkNames already set also NumberOfInks is set accordingly and + * should be equal */ + if (TIFFFieldSet(tif, FIELD_INKNAMES)) + { + if (v != td->td_numberofinks) + { + TIFFErrorExtR( + tif, module, + "Error %s; Tag %s:\n It is not possible to set the " + "value %" PRIu32 + " for NumberOfInks\n which is different from the " + "number of inks in the InkNames tag (%" PRIu16 ")", + tif->tif_name, fip->field_name, v, td->td_numberofinks); + /* Do not set / overwrite number of inks already set by + * InkNames case accordingly. */ + status = 0; + } + } + else + { + td->td_numberofinks = (uint16_t)v; + if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) + { + if (td->td_numberofinks != td->td_samplesperpixel) + { + TIFFErrorExtR(tif, module, + "Warning %s; Tag %s:\n Value %" PRIu32 + " of NumberOfInks is different from the " + "SamplesPerPixel value %" PRIu16 "", + tif->tif_name, fip->field_name, v, + td->td_samplesperpixel); + } + } + } + break; + case TIFFTAG_PERSAMPLE: + v = (uint16_t)va_arg(ap, uint16_vap); + if (v == PERSAMPLE_MULTI) + tif->tif_flags |= TIFF_PERSAMPLE; + else + tif->tif_flags &= ~TIFF_PERSAMPLE; + break; + default: + { + TIFFTagValue *tv; + int tv_size, iCustom; + + /* + * This can happen if multiple images are open with different + * codecs which have private tags. The global tag information + * table may then have tags that are valid for one file but not + * the other. If the client tries to set a tag that is not valid + * for the image's codec then we'll arrive here. This + * happens, for example, when tiffcp is used to convert between + * compression schemes and codec-specific tags are blindly copied. + * + * This also happens when a FIELD_IGNORE tag is written. + */ + if (fip->field_bit == FIELD_IGNORE) + { + TIFFErrorExtR( + tif, module, + "%s: Ignored %stag \"%s\" (not supported by libtiff)", + tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", + fip->field_name); + status = 0; + break; + } + if (fip->field_bit != FIELD_CUSTOM) + { + TIFFErrorExtR( + tif, module, + "%s: Invalid %stag \"%s\" (not supported by codec)", + tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", + fip->field_name); + status = 0; + break; + } + + /* + * Find the existing entry for this custom value. + */ + tv = NULL; + for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) + { + if (td->td_customValues[iCustom].info->field_tag == tag) + { + tv = td->td_customValues + iCustom; + if (tv->value != NULL) + { + _TIFFfreeExt(tif, tv->value); + tv->value = NULL; + } + break; + } + } + + /* + * Grow the custom list if the entry was not found. + */ + if (tv == NULL) + { + TIFFTagValue *new_customValues; + + new_customValues = (TIFFTagValue *)_TIFFreallocExt( + tif, td->td_customValues, + sizeof(TIFFTagValue) * (td->td_customValueCount + 1)); + if (!new_customValues) + { + TIFFErrorExtR(tif, module, + "%s: Failed to allocate space for list of " + "custom values", + tif->tif_name); + status = 0; + goto end; + } + + td->td_customValueCount++; + td->td_customValues = new_customValues; + + tv = td->td_customValues + (td->td_customValueCount - 1); + tv->info = fip; + tv->value = NULL; + tv->count = 0; + } + + /* + * Set custom value ... save a copy of the custom tag value. + */ + /*--: Rational2Double: For Rationals evaluate "set_get_field_type" + * to determine internal storage size. */ + tv_size = TIFFFieldSetGetSize(fip); + if (tv_size == 0) + { + status = 0; + TIFFErrorExtR(tif, module, "%s: Bad field type %d for \"%s\"", + tif->tif_name, fip->field_type, fip->field_name); + goto end; + } + + if (fip->field_type == TIFF_ASCII) + { + uint32_t ma; + const char *mb; + if (fip->field_passcount) + { + assert(fip->field_writecount == TIFF_VARIABLE2); + ma = (uint32_t)va_arg(ap, uint32_t); + mb = (const char *)va_arg(ap, const char *); + } + else + { + mb = (const char *)va_arg(ap, const char *); + size_t len = strlen(mb) + 1; + if (len >= 0x80000000U) + { + status = 0; + TIFFErrorExtR(tif, module, + "%s: Too long string value for \"%s\". " + "Maximum supported is 2147483647 bytes", + tif->tif_name, fip->field_name); + goto end; + } + ma = (uint32_t)len; + } + tv->count = ma; + setByteArray(tif, &tv->value, mb, ma, 1); + } + else + { + if (fip->field_passcount) + { + if (fip->field_writecount == TIFF_VARIABLE2) + tv->count = (uint32_t)va_arg(ap, uint32_t); + else + tv->count = (int)va_arg(ap, int); + } + else if (fip->field_writecount == TIFF_VARIABLE || + fip->field_writecount == TIFF_VARIABLE2) + tv->count = 1; + else if (fip->field_writecount == TIFF_SPP) + tv->count = td->td_samplesperpixel; + else + tv->count = fip->field_writecount; + + if (tv->count == 0) + { + TIFFWarningExtR(tif, module, + "%s: Null count for \"%s\" (type " + "%d, writecount %d, passcount %d)", + tif->tif_name, fip->field_name, + fip->field_type, fip->field_writecount, + fip->field_passcount); + break; + } + + tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size, + "custom tag binary object"); + if (!tv->value) + { + status = 0; + goto end; + } + + if (fip->field_tag == TIFFTAG_DOTRANGE && + strcmp(fip->field_name, "DotRange") == 0) + { + /* TODO: This is an evil exception and should not have been + handled this way ... likely best if we move it into + the directory structure with an explicit field in + libtiff 4.1 and assign it a FIELD_ value */ + uint16_t v2[2]; + v2[0] = (uint16_t)va_arg(ap, int); + v2[1] = (uint16_t)va_arg(ap, int); + _TIFFmemcpy(tv->value, &v2, 4); + } + + else if (fip->field_passcount || + fip->field_writecount == TIFF_VARIABLE || + fip->field_writecount == TIFF_VARIABLE2 || + fip->field_writecount == TIFF_SPP || tv->count > 1) + { + /*--: Rational2Double: For Rationals tv_size is set above to + * 4 or 8 according to fip->set_get_field_type! */ + _TIFFmemcpy(tv->value, va_arg(ap, void *), + tv->count * tv_size); + /* Test here for too big values for LONG8, SLONG8 in + * ClassicTIFF and delete custom field from custom list */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (tv->info->field_type == TIFF_LONG8) + { + uint64_t *pui64 = (uint64_t *)tv->value; + for (int i = 0; i < tv->count; i++) + { + if (pui64[i] > 0xffffffffu) + { + TIFFErrorExtR( + tif, module, + "%s: Bad LONG8 value %" PRIu64 + " at %d. array position for \"%s\" tag " + "%d in ClassicTIFF. Tag won't be " + "written to file", + tif->tif_name, pui64[i], i, + fip->field_name, tag); + goto badvalueifd8long8; + } + } + } + else if (tv->info->field_type == TIFF_SLONG8) + { + int64_t *pi64 = (int64_t *)tv->value; + for (int i = 0; i < tv->count; i++) + { + if (pi64[i] > 2147483647 || + pi64[i] < (-2147483647 - 1)) + { + TIFFErrorExtR( + tif, module, + "%s: Bad SLONG8 value %" PRIi64 + " at %d. array position for \"%s\" tag " + "%d in ClassicTIFF. Tag won't be " + "written to file", + tif->tif_name, pi64[i], i, + fip->field_name, tag); + goto badvalueifd8long8; + } + } + } + } + } + else + { + char *val = (char *)tv->value; + assert(tv->count == 1); + + switch (fip->field_type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + { + uint8_t v2 = (uint8_t)va_arg(ap, int); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_SBYTE: + { + int8_t v2 = (int8_t)va_arg(ap, int); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_SHORT: + { + uint16_t v2 = (uint16_t)va_arg(ap, int); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_SSHORT: + { + int16_t v2 = (int16_t)va_arg(ap, int); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_LONG: + case TIFF_IFD: + { + uint32_t v2 = va_arg(ap, uint32_t); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_SLONG: + { + int32_t v2 = va_arg(ap, int32_t); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_LONG8: + case TIFF_IFD8: + { + uint64_t v2 = va_arg(ap, uint64_t); + _TIFFmemcpy(val, &v2, tv_size); + /* Test here for too big values for ClassicTIFF and + * delete custom field from custom list */ + if (!(tif->tif_flags & TIFF_BIGTIFF) && + (v2 > 0xffffffffu)) + { + TIFFErrorExtR( + tif, module, + "%s: Bad LONG8 or IFD8 value %" PRIu64 + " for \"%s\" tag %d in ClassicTIFF. Tag " + "won't be written to file", + tif->tif_name, v2, fip->field_name, tag); + goto badvalueifd8long8; + } + } + break; + case TIFF_SLONG8: + { + int64_t v2 = va_arg(ap, int64_t); + _TIFFmemcpy(val, &v2, tv_size); + /* Test here for too big values for ClassicTIFF and + * delete custom field from custom list */ + if (!(tif->tif_flags & TIFF_BIGTIFF) && + ((v2 > 2147483647) || (v2 < (-2147483647 - 1)))) + { + TIFFErrorExtR( + tif, module, + "%s: Bad SLONG8 value %" PRIi64 + " for \"%s\" tag %d in ClassicTIFF. Tag " + "won't be written to file", + tif->tif_name, v2, fip->field_name, tag); + goto badvalueifd8long8; + } + } + break; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + /*-- Rational2Double: For Rationals tv_size is set + * above to 4 or 8 according to + * fip->set_get_field_type! + */ + { + if (tv_size == 8) + { + double v2 = va_arg(ap, double); + _TIFFmemcpy(val, &v2, tv_size); + } + else + { + /*-- default should be tv_size == 4 */ + float v3 = (float)va_arg(ap, double); + _TIFFmemcpy(val, &v3, tv_size); + /*-- ToDo: After Testing, this should be + * removed and tv_size==4 should be set as + * default. */ + if (tv_size != 4) + { + TIFFErrorExtR(tif, module, + "Rational2Double: " + ".set_get_field_type " + "in not 4 but %d", + tv_size); + } + } + } + break; + case TIFF_FLOAT: + { + float v2 = + _TIFFClampDoubleToFloat(va_arg(ap, double)); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + case TIFF_DOUBLE: + { + double v2 = va_arg(ap, double); + _TIFFmemcpy(val, &v2, tv_size); + } + break; + default: + _TIFFmemset(val, 0, tv_size); + status = 0; + break; + } + } + } + } + } + if (status) + { + const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); + if (fip2) + TIFFSetFieldBit(tif, fip2->field_bit); + tif->tif_flags |= TIFF_DIRTYDIRECT; + } + +end: + va_end(ap); + return (status); +badvalue: +{ + const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); + TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag", + tif->tif_name, v, fip2 ? fip2->field_name : "Unknown"); + va_end(ap); +} + return (0); +badvalue32: +{ + const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); + TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag", + tif->tif_name, v32, fip2 ? fip2->field_name : "Unknown"); + va_end(ap); +} + return (0); +badvaluedouble: +{ + const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); + TIFFErrorExtR(tif, module, "%s: Bad value %f for \"%s\" tag", tif->tif_name, + dblval, fip2 ? fip2->field_name : "Unknown"); + va_end(ap); +} + return (0); +badvalueifd8long8: +{ + /* Error message issued already above. */ + TIFFTagValue *tv2 = NULL; + int iCustom2, iC2; + /* Find the existing entry for this custom value. */ + for (iCustom2 = 0; iCustom2 < td->td_customValueCount; iCustom2++) + { + if (td->td_customValues[iCustom2].info->field_tag == tag) + { + tv2 = td->td_customValues + (iCustom2); + break; + } + } + if (tv2 != NULL) + { + /* Remove custom field from custom list */ + if (tv2->value != NULL) + { + _TIFFfreeExt(tif, tv2->value); + tv2->value = NULL; + } + /* Shorten list and close gap in customValues list. + * Re-allocation of td_customValues not necessary here. */ + td->td_customValueCount--; + for (iC2 = iCustom2; iC2 < td->td_customValueCount; iC2++) + { + td->td_customValues[iC2] = td->td_customValues[iC2 + 1]; + } + } + else + { + assert(0); + } + va_end(ap); +} + return (0); +} /*-- _TIFFVSetField() --*/ + +/* + * Return 1/0 according to whether or not + * it is permissible to set the tag's value. + * Note that we allow ImageLength to be changed + * so that we can append and extend to images. + * Any other tag may not be altered once writing + * has commenced, unless its value has no effect + * on the format of the data that is written. + */ +static int OkToChangeTag(TIFF *tif, uint32_t tag) +{ + const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); + if (!fip) + { /* unknown tag */ + TIFFErrorExtR(tif, "TIFFSetField", "%s: Unknown %stag %" PRIu32, + tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag); + return (0); + } + if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) && + !fip->field_oktochange) + { + /* + * Consult info table to see if tag can be changed + * after we've started writing. We only allow changes + * to those tags that don't/shouldn't affect the + * compression and/or format of the data. + */ + TIFFErrorExtR(tif, "TIFFSetField", + "%s: Cannot modify tag \"%s\" while writing", + tif->tif_name, fip->field_name); + return (0); + } + return (1); +} + +/* + * Record the value of a field in the + * internal directory structure. The + * field will be written to the file + * when/if the directory structure is + * updated. + */ +int TIFFSetField(TIFF *tif, uint32_t tag, ...) +{ + va_list ap; + int status; + + va_start(ap, tag); + status = TIFFVSetField(tif, tag, ap); + va_end(ap); + return (status); +} + +/* + * Clear the contents of the field in the internal structure. + */ +int TIFFUnsetField(TIFF *tif, uint32_t tag) +{ + const TIFFField *fip = TIFFFieldWithTag(tif, tag); + TIFFDirectory *td = &tif->tif_dir; + + if (!fip) + return 0; + + if (fip->field_bit != FIELD_CUSTOM) + TIFFClrFieldBit(tif, fip->field_bit); + else + { + TIFFTagValue *tv = NULL; + int i; + + for (i = 0; i < td->td_customValueCount; i++) + { + + tv = td->td_customValues + i; + if (tv->info->field_tag == tag) + break; + } + + if (i < td->td_customValueCount) + { + _TIFFfreeExt(tif, tv->value); + for (; i < td->td_customValueCount - 1; i++) + { + td->td_customValues[i] = td->td_customValues[i + 1]; + } + td->td_customValueCount--; + } + } + + tif->tif_flags |= TIFF_DIRTYDIRECT; + + return (1); +} + +/* + * Like TIFFSetField, but taking a varargs + * parameter list. This routine is useful + * for building higher-level interfaces on + * top of the library. + */ +int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + return OkToChangeTag(tif, tag) + ? (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) + : 0; +} + +static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + TIFFDirectory *td = &tif->tif_dir; + int ret_val = 1; + uint32_t standard_tag = tag; + const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); + if (fip == NULL) /* cannot happen since TIFFGetField() already checks it */ + return 0; + + /* + * We want to force the custom code to be used for custom + * fields even if the tag happens to match a well known + * one - important for reinterpreted handling of standard + * tag values in custom directories (i.e. EXIF) + */ + if (fip->field_bit == FIELD_CUSTOM) + { + standard_tag = 0; + } + + switch (standard_tag) + { + case TIFFTAG_SUBFILETYPE: + *va_arg(ap, uint32_t *) = td->td_subfiletype; + break; + case TIFFTAG_IMAGEWIDTH: + *va_arg(ap, uint32_t *) = td->td_imagewidth; + break; + case TIFFTAG_IMAGELENGTH: + *va_arg(ap, uint32_t *) = td->td_imagelength; + break; + case TIFFTAG_BITSPERSAMPLE: + *va_arg(ap, uint16_t *) = td->td_bitspersample; + break; + case TIFFTAG_COMPRESSION: + *va_arg(ap, uint16_t *) = td->td_compression; + break; + case TIFFTAG_PHOTOMETRIC: + *va_arg(ap, uint16_t *) = td->td_photometric; + break; + case TIFFTAG_THRESHHOLDING: + *va_arg(ap, uint16_t *) = td->td_threshholding; + break; + case TIFFTAG_FILLORDER: + *va_arg(ap, uint16_t *) = td->td_fillorder; + break; + case TIFFTAG_ORIENTATION: + *va_arg(ap, uint16_t *) = td->td_orientation; + break; + case TIFFTAG_SAMPLESPERPIXEL: + *va_arg(ap, uint16_t *) = td->td_samplesperpixel; + break; + case TIFFTAG_ROWSPERSTRIP: + *va_arg(ap, uint32_t *) = td->td_rowsperstrip; + break; + case TIFFTAG_MINSAMPLEVALUE: + *va_arg(ap, uint16_t *) = td->td_minsamplevalue; + break; + case TIFFTAG_MAXSAMPLEVALUE: + *va_arg(ap, uint16_t *) = td->td_maxsamplevalue; + break; + case TIFFTAG_SMINSAMPLEVALUE: + if (tif->tif_flags & TIFF_PERSAMPLE) + *va_arg(ap, double **) = td->td_sminsamplevalue; + else + { + /* libtiff historically treats this as a single value. */ + uint16_t i; + double v = td->td_sminsamplevalue[0]; + for (i = 1; i < td->td_samplesperpixel; ++i) + if (td->td_sminsamplevalue[i] < v) + v = td->td_sminsamplevalue[i]; + *va_arg(ap, double *) = v; + } + break; + case TIFFTAG_SMAXSAMPLEVALUE: + if (tif->tif_flags & TIFF_PERSAMPLE) + *va_arg(ap, double **) = td->td_smaxsamplevalue; + else + { + /* libtiff historically treats this as a single value. */ + uint16_t i; + double v = td->td_smaxsamplevalue[0]; + for (i = 1; i < td->td_samplesperpixel; ++i) + if (td->td_smaxsamplevalue[i] > v) + v = td->td_smaxsamplevalue[i]; + *va_arg(ap, double *) = v; + } + break; + case TIFFTAG_XRESOLUTION: + *va_arg(ap, float *) = td->td_xresolution; + break; + case TIFFTAG_YRESOLUTION: + *va_arg(ap, float *) = td->td_yresolution; + break; + case TIFFTAG_PLANARCONFIG: + *va_arg(ap, uint16_t *) = td->td_planarconfig; + break; + case TIFFTAG_XPOSITION: + *va_arg(ap, float *) = td->td_xposition; + break; + case TIFFTAG_YPOSITION: + *va_arg(ap, float *) = td->td_yposition; + break; + case TIFFTAG_RESOLUTIONUNIT: + *va_arg(ap, uint16_t *) = td->td_resolutionunit; + break; + case TIFFTAG_PAGENUMBER: + *va_arg(ap, uint16_t *) = td->td_pagenumber[0]; + *va_arg(ap, uint16_t *) = td->td_pagenumber[1]; + break; + case TIFFTAG_HALFTONEHINTS: + *va_arg(ap, uint16_t *) = td->td_halftonehints[0]; + *va_arg(ap, uint16_t *) = td->td_halftonehints[1]; + break; + case TIFFTAG_COLORMAP: + *va_arg(ap, const uint16_t **) = td->td_colormap[0]; + *va_arg(ap, const uint16_t **) = td->td_colormap[1]; + *va_arg(ap, const uint16_t **) = td->td_colormap[2]; + break; + case TIFFTAG_STRIPOFFSETS: + case TIFFTAG_TILEOFFSETS: + _TIFFFillStriles(tif); + *va_arg(ap, const uint64_t **) = td->td_stripoffset_p; + if (td->td_stripoffset_p == NULL) + ret_val = 0; + break; + case TIFFTAG_STRIPBYTECOUNTS: + case TIFFTAG_TILEBYTECOUNTS: + _TIFFFillStriles(tif); + *va_arg(ap, const uint64_t **) = td->td_stripbytecount_p; + if (td->td_stripbytecount_p == NULL) + ret_val = 0; + break; + case TIFFTAG_MATTEING: + *va_arg(ap, uint16_t *) = + (td->td_extrasamples == 1 && + td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); + break; + case TIFFTAG_EXTRASAMPLES: + *va_arg(ap, uint16_t *) = td->td_extrasamples; + *va_arg(ap, const uint16_t **) = td->td_sampleinfo; + break; + case TIFFTAG_TILEWIDTH: + *va_arg(ap, uint32_t *) = td->td_tilewidth; + break; + case TIFFTAG_TILELENGTH: + *va_arg(ap, uint32_t *) = td->td_tilelength; + break; + case TIFFTAG_TILEDEPTH: + *va_arg(ap, uint32_t *) = td->td_tiledepth; + break; + case TIFFTAG_DATATYPE: + switch (td->td_sampleformat) + { + case SAMPLEFORMAT_UINT: + *va_arg(ap, uint16_t *) = DATATYPE_UINT; + break; + case SAMPLEFORMAT_INT: + *va_arg(ap, uint16_t *) = DATATYPE_INT; + break; + case SAMPLEFORMAT_IEEEFP: + *va_arg(ap, uint16_t *) = DATATYPE_IEEEFP; + break; + case SAMPLEFORMAT_VOID: + *va_arg(ap, uint16_t *) = DATATYPE_VOID; + break; + } + break; + case TIFFTAG_SAMPLEFORMAT: + *va_arg(ap, uint16_t *) = td->td_sampleformat; + break; + case TIFFTAG_IMAGEDEPTH: + *va_arg(ap, uint32_t *) = td->td_imagedepth; + break; + case TIFFTAG_SUBIFD: + *va_arg(ap, uint16_t *) = td->td_nsubifd; + *va_arg(ap, const uint64_t **) = td->td_subifd; + break; + case TIFFTAG_YCBCRPOSITIONING: + *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning; + break; + case TIFFTAG_YCBCRSUBSAMPLING: + *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0]; + *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1]; + break; + case TIFFTAG_TRANSFERFUNCTION: + *va_arg(ap, const uint16_t **) = td->td_transferfunction[0]; + if (td->td_samplesperpixel - td->td_extrasamples > 1) + { + *va_arg(ap, const uint16_t **) = td->td_transferfunction[1]; + *va_arg(ap, const uint16_t **) = td->td_transferfunction[2]; + } + else + { + *va_arg(ap, const uint16_t **) = NULL; + *va_arg(ap, const uint16_t **) = NULL; + } + break; + case TIFFTAG_REFERENCEBLACKWHITE: + *va_arg(ap, const float **) = td->td_refblackwhite; + break; + case TIFFTAG_INKNAMES: + *va_arg(ap, const char **) = td->td_inknames; + break; + case TIFFTAG_NUMBEROFINKS: + *va_arg(ap, uint16_t *) = td->td_numberofinks; + break; + default: + { + int i; + + /* + * This can happen if multiple images are open + * with different codecs which have private + * tags. The global tag information table may + * then have tags that are valid for one file + * but not the other. If the client tries to + * get a tag that is not valid for the image's + * codec then we'll arrive here. + */ + if (fip->field_bit != FIELD_CUSTOM) + { + TIFFErrorExtR(tif, "_TIFFVGetField", + "%s: Invalid %stag \"%s\" " + "(not supported by codec)", + tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", + fip->field_name); + ret_val = 0; + break; + } + + /* + * Do we have a custom value? + */ + ret_val = 0; + for (i = 0; i < td->td_customValueCount; i++) + { + TIFFTagValue *tv = td->td_customValues + i; + + if (tv->info->field_tag != tag) + continue; + + if (fip->field_passcount) + { + if (fip->field_readcount == TIFF_VARIABLE2) + *va_arg(ap, uint32_t *) = (uint32_t)tv->count; + else /* Assume TIFF_VARIABLE */ + *va_arg(ap, uint16_t *) = (uint16_t)tv->count; + *va_arg(ap, const void **) = tv->value; + ret_val = 1; + } + else if (fip->field_tag == TIFFTAG_DOTRANGE && + strcmp(fip->field_name, "DotRange") == 0) + { + /* TODO: This is an evil exception and should not have been + handled this way ... likely best if we move it into + the directory structure with an explicit field in + libtiff 4.1 and assign it a FIELD_ value */ + *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[0]; + *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[1]; + ret_val = 1; + } + else + { + if (fip->field_type == TIFF_ASCII || + fip->field_readcount == TIFF_VARIABLE || + fip->field_readcount == TIFF_VARIABLE2 || + fip->field_readcount == TIFF_SPP || tv->count > 1) + { + *va_arg(ap, void **) = tv->value; + ret_val = 1; + } + else + { + char *val = (char *)tv->value; + assert(tv->count == 1); + switch (fip->field_type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + *va_arg(ap, uint8_t *) = *(uint8_t *)val; + ret_val = 1; + break; + case TIFF_SBYTE: + *va_arg(ap, int8_t *) = *(int8_t *)val; + ret_val = 1; + break; + case TIFF_SHORT: + *va_arg(ap, uint16_t *) = *(uint16_t *)val; + ret_val = 1; + break; + case TIFF_SSHORT: + *va_arg(ap, int16_t *) = *(int16_t *)val; + ret_val = 1; + break; + case TIFF_LONG: + case TIFF_IFD: + *va_arg(ap, uint32_t *) = *(uint32_t *)val; + ret_val = 1; + break; + case TIFF_SLONG: + *va_arg(ap, int32_t *) = *(int32_t *)val; + ret_val = 1; + break; + case TIFF_LONG8: + case TIFF_IFD8: + *va_arg(ap, uint64_t *) = *(uint64_t *)val; + ret_val = 1; + break; + case TIFF_SLONG8: + *va_arg(ap, int64_t *) = *(int64_t *)val; + ret_val = 1; + break; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + { + /*-- Rational2Double: For Rationals evaluate + * "set_get_field_type" to determine internal + * storage size and return value size. */ + int tv_size = TIFFFieldSetGetSize(fip); + if (tv_size == 8) + { + *va_arg(ap, double *) = *(double *)val; + ret_val = 1; + } + else + { + /*-- default should be tv_size == 4 */ + *va_arg(ap, float *) = *(float *)val; + ret_val = 1; + /*-- ToDo: After Testing, this should be + * removed and tv_size==4 should be set as + * default. */ + if (tv_size != 4) + { + TIFFErrorExtR(tif, "_TIFFVGetField", + "Rational2Double: " + ".set_get_field_type " + "in not 4 but %d", + tv_size); + } + } + } + break; + case TIFF_FLOAT: + *va_arg(ap, float *) = *(float *)val; + ret_val = 1; + break; + case TIFF_DOUBLE: + *va_arg(ap, double *) = *(double *)val; + ret_val = 1; + break; + default: + ret_val = 0; + break; + } + } + } + break; + } + } + } + return (ret_val); +} + +/* + * Return the value of a field in the + * internal directory structure. + */ +int TIFFGetField(TIFF *tif, uint32_t tag, ...) +{ + int status; + va_list ap; + + va_start(ap, tag); + status = TIFFVGetField(tif, tag, ap); + va_end(ap); + return (status); +} + +/* + * Like TIFFGetField, but taking a varargs + * parameter list. This routine is useful + * for building higher-level interfaces on + * top of the library. + */ +int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); + return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) + ? (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) + : 0); +} + +#define CleanupField(member) \ + { \ + if (td->member) \ + { \ + _TIFFfreeExt(tif, td->member); \ + td->member = 0; \ + } \ + } + +/* + * Release storage associated with a directory. + */ +void TIFFFreeDirectory(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + int i; + + (*tif->tif_cleanup)(tif); + _TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset)); + CleanupField(td_sminsamplevalue); + CleanupField(td_smaxsamplevalue); + CleanupField(td_colormap[0]); + CleanupField(td_colormap[1]); + CleanupField(td_colormap[2]); + CleanupField(td_sampleinfo); + CleanupField(td_subifd); + CleanupField(td_inknames); + CleanupField(td_refblackwhite); + CleanupField(td_transferfunction[0]); + CleanupField(td_transferfunction[1]); + CleanupField(td_transferfunction[2]); + CleanupField(td_stripoffset_p); + CleanupField(td_stripbytecount_p); + td->td_stripoffsetbyteallocsize = 0; + TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING); + TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING); + + /* Cleanup custom tag values */ + for (i = 0; i < td->td_customValueCount; i++) + { + if (td->td_customValues[i].value) + _TIFFfreeExt(tif, td->td_customValues[i].value); + } + + td->td_customValueCount = 0; + CleanupField(td_customValues); + + _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry)); + _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry)); + + /* Reset some internal parameters for IFD data size checking. */ + tif->tif_dir.td_dirdatasize_read = 0; + tif->tif_dir.td_dirdatasize_write = 0; + if (tif->tif_dir.td_dirdatasize_offsets != NULL) + { + _TIFFfreeExt(tif, tif->tif_dir.td_dirdatasize_offsets); + tif->tif_dir.td_dirdatasize_offsets = NULL; + tif->tif_dir.td_dirdatasize_Noffsets = 0; + } + tif->tif_dir.td_iswrittentofile = FALSE; +} +#undef CleanupField + +/* + * Client Tag extension support (from Niles Ritter). + */ +static TIFFExtendProc _TIFFextender = (TIFFExtendProc)NULL; + +TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc extender) +{ + TIFFExtendProc prev = _TIFFextender; + _TIFFextender = extender; + return (prev); +} + +/* + * Setup for a new directory. Should we automatically call + * TIFFWriteDirectory() if the current one is dirty? + * + * The newly created directory will not exist on the file till + * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called. + */ +int TIFFCreateDirectory(TIFF *tif) +{ + /* Free previously allocated memory and setup default values. */ + TIFFFreeDirectory(tif); + TIFFDefaultDirectory(tif); + tif->tif_diroff = 0; + tif->tif_nextdiroff = 0; + tif->tif_curoff = 0; + tif->tif_row = (uint32_t)-1; + tif->tif_curstrip = (uint32_t)-1; + tif->tif_dir.td_iswrittentofile = FALSE; + + return 0; +} + +int TIFFCreateCustomDirectory(TIFF *tif, const TIFFFieldArray *infoarray) +{ + /* Free previously allocated memory and setup default values. */ + TIFFFreeDirectory(tif); + TIFFDefaultDirectory(tif); + + /* + * Reset the field definitions to match the application provided list. + * Hopefully TIFFDefaultDirectory() won't have done anything irreversible + * based on it's assumption this is an image directory. + */ + _TIFFSetupFields(tif, infoarray); + + tif->tif_diroff = 0; + tif->tif_nextdiroff = 0; + tif->tif_curoff = 0; + tif->tif_row = (uint32_t)-1; + tif->tif_curstrip = (uint32_t)-1; + /* invalidate directory index */ + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + /* invalidate IFD loop lists */ + _TIFFCleanupIFDOffsetAndNumberMaps(tif); + /* To be able to return from SubIFD or custom-IFD to main-IFD */ + tif->tif_setdirectory_force_absolute = TRUE; + + return 0; +} + +int TIFFCreateEXIFDirectory(TIFF *tif) +{ + const TIFFFieldArray *exifFieldArray; + exifFieldArray = _TIFFGetExifFields(); + return TIFFCreateCustomDirectory(tif, exifFieldArray); +} + +/* + * Creates the EXIF GPS custom directory + */ +int TIFFCreateGPSDirectory(TIFF *tif) +{ + const TIFFFieldArray *gpsFieldArray; + gpsFieldArray = _TIFFGetGpsFields(); + return TIFFCreateCustomDirectory(tif, gpsFieldArray); +} + +/* + * Setup a default directory structure. + */ +int TIFFDefaultDirectory(TIFF *tif) +{ + register TIFFDirectory *td = &tif->tif_dir; + const TIFFFieldArray *tiffFieldArray; + + tiffFieldArray = _TIFFGetFields(); + _TIFFSetupFields(tif, tiffFieldArray); + + _TIFFmemset(td, 0, sizeof(*td)); + td->td_fillorder = FILLORDER_MSB2LSB; + td->td_bitspersample = 1; + td->td_threshholding = THRESHHOLD_BILEVEL; + td->td_orientation = ORIENTATION_TOPLEFT; + td->td_samplesperpixel = 1; + td->td_rowsperstrip = (uint32_t)-1; + td->td_tilewidth = 0; + td->td_tilelength = 0; + td->td_tiledepth = 1; +#ifdef STRIPBYTECOUNTSORTED_UNUSED + td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */ +#endif + td->td_resolutionunit = RESUNIT_INCH; + td->td_sampleformat = SAMPLEFORMAT_UINT; + td->td_imagedepth = 1; + td->td_ycbcrsubsampling[0] = 2; + td->td_ycbcrsubsampling[1] = 2; + td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED; + tif->tif_postdecode = _TIFFNoPostDecode; + tif->tif_foundfield = NULL; + tif->tif_tagmethods.vsetfield = _TIFFVSetField; + tif->tif_tagmethods.vgetfield = _TIFFVGetField; + tif->tif_tagmethods.printdir = NULL; + /* additional default values */ + td->td_planarconfig = PLANARCONFIG_CONTIG; + td->td_compression = COMPRESSION_NONE; + td->td_subfiletype = 0; + td->td_minsamplevalue = 0; + /* td_bitspersample=1 is always set in TIFFDefaultDirectory(). + * Therefore, td_maxsamplevalue has to be re-calculated in + * TIFFGetFieldDefaulted(). */ + td->td_maxsamplevalue = 1; /* Default for td_bitspersample=1 */ + td->td_extrasamples = 0; + td->td_sampleinfo = NULL; + + /* + * Give client code a chance to install their own + * tag extensions & methods, prior to compression overloads, + * but do some prior cleanup first. + * (http://trac.osgeo.org/gdal/ticket/5054) + */ + if (tif->tif_nfieldscompat > 0) + { + uint32_t i; + + for (i = 0; i < tif->tif_nfieldscompat; i++) + { + if (tif->tif_fieldscompat[i].allocated_size) + _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields); + } + _TIFFfreeExt(tif, tif->tif_fieldscompat); + tif->tif_nfieldscompat = 0; + tif->tif_fieldscompat = NULL; + } + if (_TIFFextender) + (*_TIFFextender)(tif); + (void)TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); + /* + * NB: The directory is marked dirty as a result of setting + * up the default compression scheme. However, this really + * isn't correct -- we want TIFF_DIRTYDIRECT to be set only + * if the user does something. We could just do the setup + * by hand, but it seems better to use the normal mechanism + * (i.e. TIFFSetField). + */ + tif->tif_flags &= ~TIFF_DIRTYDIRECT; + + /* + * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19 + * we clear the ISTILED flag when setting up a new directory. + * Should we also be clearing stuff like INSUBIFD? + */ + tif->tif_flags &= ~TIFF_ISTILED; + + return (1); +} + +static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off, + tdir_t *nextdirnum) +{ + static const char module[] = "TIFFAdvanceDirectory"; + + /* Add this directory to the directory list, if not already in. */ + if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff)) + { + TIFFErrorExtR(tif, module, + "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64 + ") might cause an IFD loop", + *nextdirnum, *nextdiroff, *nextdiroff); + *nextdiroff = 0; + *nextdirnum = 0; + return (0); + } + + if (isMapped(tif)) + { + uint64_t poff = *nextdiroff; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + tmsize_t poffa, poffb, poffc, poffd; + uint16_t dircount; + uint32_t nextdir32; + poffa = (tmsize_t)poff; + poffb = poffa + sizeof(uint16_t); + if (((uint64_t)poffa != poff) || (poffb < poffa) || + (poffb < (tmsize_t)sizeof(uint16_t)) || (poffb > tif->tif_size)) + { + TIFFErrorExtR(tif, module, + "%s:%d: %s: Error fetching directory count", + __FILE__, __LINE__, tif->tif_name); + *nextdiroff = 0; + return (0); + } + _TIFFmemcpy(&dircount, tif->tif_base + poffa, sizeof(uint16_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount); + poffc = poffb + dircount * 12; + poffd = poffc + sizeof(uint32_t); + if ((poffc < poffb) || (poffc < dircount * 12) || (poffd < poffc) || + (poffd < (tmsize_t)sizeof(uint32_t)) || (poffd > tif->tif_size)) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + if (off != NULL) + *off = (uint64_t)poffc; + _TIFFmemcpy(&nextdir32, tif->tif_base + poffc, sizeof(uint32_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextdir32); + *nextdiroff = nextdir32; + } + else + { + tmsize_t poffa, poffb, poffc, poffd; + uint64_t dircount64; + uint16_t dircount16; + if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint64_t)) + { + TIFFErrorExtR(tif, module, + "%s:%d: %s: Error fetching directory count", + __FILE__, __LINE__, tif->tif_name); + return (0); + } + poffa = (tmsize_t)poff; + poffb = poffa + sizeof(uint64_t); + if (poffb > tif->tif_size) + { + TIFFErrorExtR(tif, module, + "%s:%d: %s: Error fetching directory count", + __FILE__, __LINE__, tif->tif_name); + return (0); + } + _TIFFmemcpy(&dircount64, tif->tif_base + poffa, sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + if (dircount64 > 0xFFFF) + { + TIFFErrorExtR(tif, module, + "Sanity check on directory count failed"); + return (0); + } + dircount16 = (uint16_t)dircount64; + if (poffb > TIFF_TMSIZE_T_MAX - (tmsize_t)(dircount16 * 20) - + (tmsize_t)sizeof(uint64_t)) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + poffc = poffb + dircount16 * 20; + poffd = poffc + sizeof(uint64_t); + if (poffd > tif->tif_size) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + if (off != NULL) + *off = (uint64_t)poffc; + _TIFFmemcpy(nextdiroff, tif->tif_base + poffc, sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(nextdiroff); + } + } + else + { + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint16_t dircount; + uint32_t nextdir32; + if (!SeekOK(tif, *nextdiroff) || + !ReadOK(tif, &dircount, sizeof(uint16_t))) + { + TIFFErrorExtR(tif, module, + "%s:%d: %s: Error fetching directory count", + __FILE__, __LINE__, tif->tif_name); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount); + if (off != NULL) + *off = TIFFSeekFile(tif, dircount * 12, SEEK_CUR); + else + (void)TIFFSeekFile(tif, dircount * 12, SEEK_CUR); + if (!ReadOK(tif, &nextdir32, sizeof(uint32_t))) + { + TIFFErrorExtR(tif, module, "%s: Error fetching directory link", + tif->tif_name); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextdir32); + *nextdiroff = nextdir32; + } + else + { + uint64_t dircount64; + uint16_t dircount16; + if (!SeekOK(tif, *nextdiroff) || + !ReadOK(tif, &dircount64, sizeof(uint64_t))) + { + TIFFErrorExtR(tif, module, + "%s:%d: %s: Error fetching directory count", + __FILE__, __LINE__, tif->tif_name); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + if (dircount64 > 0xFFFF) + { + TIFFErrorExtR(tif, module, + "%s:%d: %s: Error fetching directory count", + __FILE__, __LINE__, tif->tif_name); + return (0); + } + dircount16 = (uint16_t)dircount64; + if (off != NULL) + *off = TIFFSeekFile(tif, dircount16 * 20, SEEK_CUR); + else + (void)TIFFSeekFile(tif, dircount16 * 20, SEEK_CUR); + if (!ReadOK(tif, nextdiroff, sizeof(uint64_t))) + { + TIFFErrorExtR(tif, module, "%s: Error fetching directory link", + tif->tif_name); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(nextdiroff); + } + } + if (*nextdiroff != 0) + { + (*nextdirnum)++; + /* Check next directory for IFD looping and if so, set it as last + * directory. */ + if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff)) + { + TIFFWarningExtR( + tif, module, + "the next directory %u at offset 0x%" PRIx64 " (%" PRIu64 + ") might be an IFD loop. Treating directory %d as " + "last directory", + *nextdirnum, *nextdiroff, *nextdiroff, (int)(*nextdirnum) - 1); + *nextdiroff = 0; + (*nextdirnum)--; + } + } + return (1); +} + +/* + * Count the number of directories in a file. + */ +tdir_t TIFFNumberOfDirectories(TIFF *tif) +{ + uint64_t nextdiroff; + tdir_t nextdirnum; + tdir_t n; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + nextdiroff = tif->tif_header.classic.tiff_diroff; + else + nextdiroff = tif->tif_header.big.tiff_diroff; + nextdirnum = 0; + n = 0; + while (nextdiroff != 0 && + TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum)) + { + ++n; + } + /* Update number of main-IFDs in file. */ + tif->tif_curdircount = n; + return (n); +} + +/* + * Set the n-th directory as the current directory. + * NB: Directories are numbered starting at 0. + */ +int TIFFSetDirectory(TIFF *tif, tdir_t dirn) +{ + uint64_t nextdiroff; + tdir_t nextdirnum = 0; + tdir_t n; + + if (tif->tif_setdirectory_force_absolute) + { + /* tif_setdirectory_force_absolute=1 will force parsing the main IFD + * chain from the beginning, thus IFD directory list needs to be cleared + * from possible SubIFD offsets. + */ + _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */ + } + + /* Even faster path, if offset is available within IFD loop hash list. */ + if (!tif->tif_setdirectory_force_absolute && + _TIFFGetOffsetFromDirNumber(tif, dirn, &nextdiroff)) + { + /* Set parameters for following TIFFReadDirectory() below. */ + tif->tif_nextdiroff = nextdiroff; + tif->tif_curdir = dirn; + /* Reset to relative stepping */ + tif->tif_setdirectory_force_absolute = FALSE; + } + else + { + + /* Fast path when we just advance relative to the current directory: + * start at the current dir offset and continue to seek from there. + * Check special cases when relative is not allowed: + * - jump back from SubIFD or custom directory + * - right after TIFFWriteDirectory() jump back to that directory + * using TIFFSetDirectory() */ + const int relative = (dirn >= tif->tif_curdir) && + (tif->tif_diroff != 0) && + !tif->tif_setdirectory_force_absolute; + + if (relative) + { + nextdiroff = tif->tif_diroff; + dirn -= tif->tif_curdir; + nextdirnum = tif->tif_curdir; + } + else if (!(tif->tif_flags & TIFF_BIGTIFF)) + nextdiroff = tif->tif_header.classic.tiff_diroff; + else + nextdiroff = tif->tif_header.big.tiff_diroff; + + /* Reset to relative stepping */ + tif->tif_setdirectory_force_absolute = FALSE; + + for (n = dirn; n > 0 && nextdiroff != 0; n--) + if (!TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum)) + return (0); + /* If the n-th directory could not be reached (does not exist), + * return here without touching anything further. */ + if (nextdiroff == 0 || n > 0) + return (0); + + tif->tif_nextdiroff = nextdiroff; + + /* Set curdir to the actual directory index. */ + if (relative) + tif->tif_curdir += dirn - n; + else + tif->tif_curdir = dirn - n; + } + + /* The -1 decrement is because TIFFReadDirectory will increment + * tif_curdir after successfully reading the directory. */ + if (tif->tif_curdir == 0) + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + else + tif->tif_curdir--; + + tdir_t curdir = tif->tif_curdir; + + int retval = TIFFReadDirectory(tif); + + if (!retval && tif->tif_curdir == curdir) + { + /* If tif_curdir has not be incremented, TIFFFetchDirectory() in + * TIFFReadDirectory() has failed and tif_curdir shall be set + * specifically. */ + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + } + return (retval); +} + +/* + * Set the current directory to be the directory + * located at the specified file offset. This interface + * is used mainly to access directories linked with + * the SubIFD tag (e.g. thumbnail images). + */ +int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff) +{ + /* Match nextdiroff and curdir for consistent IFD-loop checking. + * Only with TIFFSetSubDirectory() the IFD list can be corrupted with + * invalid offsets within the main IFD tree. In the case of several subIFDs + * of a main image, there are two possibilities that are not even mutually + * exclusive. a.) The subIFD tag contains an array with all offsets of the + * subIFDs. b.) The SubIFDs are concatenated with their NextIFD parameters. + * (refer to + * https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf.) + */ + int retval; + uint32_t curdir = 0; + int8_t probablySubIFD = 0; + if (diroff == 0) + { + /* Special case to set tif_diroff=0, which is done in + * TIFFReadDirectory() below to indicate that the currently read IFD is + * treated as a new, fresh IFD. */ + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + tif->tif_dir.td_iswrittentofile = FALSE; + } + else + { + if (!_TIFFGetDirNumberFromOffset(tif, diroff, &curdir)) + { + /* Non-existing offsets might point to a SubIFD or invalid IFD.*/ + probablySubIFD = 1; + } + /* -1 because TIFFReadDirectory() will increment tif_curdir. */ + if (curdir >= 1) + tif->tif_curdir = curdir - 1; + else + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + } + curdir = tif->tif_curdir; + + tif->tif_nextdiroff = diroff; + retval = TIFFReadDirectory(tif); + + /* tif_curdir is incremented in TIFFReadDirectory(), but if it has not been + * incremented, TIFFFetchDirectory() has failed there and tif_curdir shall + * be set specifically. */ + if (!retval && diroff != 0 && tif->tif_curdir == curdir) + { + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + } + + if (probablySubIFD) + { + if (retval) + { + /* Reset IFD list to start new one for SubIFD chain and also start + * SubIFD chain with tif_curdir=0 for IFD loop checking. */ + /* invalidate IFD loop lists */ + _TIFFCleanupIFDOffsetAndNumberMaps(tif); + tif->tif_curdir = 0; /* first directory of new chain */ + /* add this offset to new IFD list */ + retval = _TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, diroff); + } + /* To be able to return from SubIFD or custom-IFD to main-IFD */ + tif->tif_setdirectory_force_absolute = TRUE; + } + + return (retval); +} + +/* + * Return file offset of the current directory. + */ +uint64_t TIFFCurrentDirOffset(TIFF *tif) { return (tif->tif_diroff); } + +/* + * Return an indication of whether or not we are + * at the last directory in the file. + */ +int TIFFLastDirectory(TIFF *tif) { return (tif->tif_nextdiroff == 0); } + +/* + * Unlink the specified directory from the directory chain. + * Note: First directory starts with number dirn=1. + * This is different to TIFFSetDirectory() where the first directory starts with + * zero. + */ +int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn) +{ + static const char module[] = "TIFFUnlinkDirectory"; + uint64_t nextdir; + tdir_t nextdirnum; + uint64_t off; + tdir_t n; + + if (tif->tif_mode == O_RDONLY) + { + TIFFErrorExtR(tif, module, + "Can not unlink directory in read-only file"); + return (0); + } + if (dirn == 0) + { + TIFFErrorExtR(tif, module, + "For TIFFUnlinkDirectory() first directory starts with " + "number 1 and not 0"); + return (0); + } + /* + * Go to the directory before the one we want + * to unlink and nab the offset of the link + * field we'll need to patch. + */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + nextdir = tif->tif_header.classic.tiff_diroff; + off = 4; + } + else + { + nextdir = tif->tif_header.big.tiff_diroff; + off = 8; + } + nextdirnum = 0; /* First directory is dirn=0 */ + + for (n = dirn - 1; n > 0; n--) + { + if (nextdir == 0) + { + TIFFErrorExtR(tif, module, "Directory %u does not exist", dirn); + return (0); + } + if (!TIFFAdvanceDirectory(tif, &nextdir, &off, &nextdirnum)) + return (0); + } + /* + * Advance to the directory to be unlinked and fetch + * the offset of the directory that follows. + */ + if (!TIFFAdvanceDirectory(tif, &nextdir, NULL, &nextdirnum)) + return (0); + /* + * Go back and patch the link field of the preceding + * directory to point to the offset of the directory + * that follows. + */ + (void)TIFFSeekFile(tif, off, SEEK_SET); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t nextdir32; + nextdir32 = (uint32_t)nextdir; + assert((uint64_t)nextdir32 == nextdir); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextdir32); + if (!WriteOK(tif, &nextdir32, sizeof(uint32_t))) + { + TIFFErrorExtR(tif, module, "Error writing directory link"); + return (0); + } + } + else + { + /* Need local swap because nextdir has to be used unswapped below. */ + uint64_t nextdir64 = nextdir; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&nextdir64); + if (!WriteOK(tif, &nextdir64, sizeof(uint64_t))) + { + TIFFErrorExtR(tif, module, "Error writing directory link"); + return (0); + } + } + + /* For dirn=1 (first directory) also update the libtiff internal + * base offset variables. */ + if (dirn == 1) + { + if (!(tif->tif_flags & TIFF_BIGTIFF)) + tif->tif_header.classic.tiff_diroff = (uint32_t)nextdir; + else + tif->tif_header.big.tiff_diroff = nextdir; + } + + /* + * Leave directory state setup safely. We don't have + * facilities for doing inserting and removing directories, + * so it's safest to just invalidate everything. This + * means that the caller can only append to the directory + * chain. + */ + if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) + { + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_rawdata = NULL; + tif->tif_rawcc = 0; + tif->tif_rawcp = NULL; + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = 0; + } + tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP | TIFF_POSTENCODE | + TIFF_BUF4WRITE); + TIFFFreeDirectory(tif); + TIFFDefaultDirectory(tif); + tif->tif_diroff = 0; /* force link on next write */ + tif->tif_nextdiroff = 0; /* next write must be at end */ + tif->tif_lastdiroff = 0; /* will be updated on next link */ + tif->tif_curoff = 0; + tif->tif_row = (uint32_t)-1; + tif->tif_curstrip = (uint32_t)-1; + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; + if (tif->tif_curdircount > 0) + tif->tif_curdircount--; + else + tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER; + _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */ + return (1); +} diff --git a/cpp/3rd_party/libtiff/tif_dir.h b/cpp/3rd_party/libtiff/tif_dir.h new file mode 100644 index 0000000000..44270f7097 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_dir.h @@ -0,0 +1,365 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _TIFFDIR_ +#define _TIFFDIR_ + +#include "tiff.h" +#include "tiffio.h" + +/* + * ``Library-private'' Directory-related Definitions. + */ + +typedef struct +{ + const TIFFField *info; + int count; + void *value; +} TIFFTagValue; + +/* + * TIFF Image File Directories are comprised of a table of field + * descriptors of the form shown below. The table is sorted in + * ascending order by tag. The values associated with each entry are + * disjoint and may appear anywhere in the file (so long as they are + * placed on a word boundary). + * + * If the value is 4 bytes or less, in ClassicTIFF, or 8 bytes or less in + * BigTIFF, then it is placed in the offset field to save space. If so, + * it is left-justified in the offset field. + */ +typedef struct +{ + uint16_t tdir_tag; /* see below */ + uint16_t tdir_type; /* data type; see below */ + uint64_t tdir_count; /* number of items; length in spec */ + union + { + uint16_t toff_short; + uint32_t toff_long; + uint64_t toff_long8; + } tdir_offset; /* either offset or the data itself if fits */ + uint8_t tdir_ignore; /* flag status to ignore tag when parsing tags in + tif_dirread.c */ +} TIFFDirEntry; + +typedef struct +{ + uint64_t offset; + uint64_t length; +} TIFFEntryOffsetAndLength; /* auxiliary for evaluating size of IFD data */ + +/* + * Internal format of a TIFF directory entry. + */ +typedef struct +{ +#define FIELDSET_ITEMS 4 + /* bit vector of fields that are set */ + uint32_t td_fieldsset[FIELDSET_ITEMS]; + + uint32_t td_imagewidth, td_imagelength, td_imagedepth; + uint32_t td_tilewidth, td_tilelength, td_tiledepth; + uint32_t td_subfiletype; + uint16_t td_bitspersample; + uint16_t td_sampleformat; + uint16_t td_compression; + uint16_t td_photometric; + uint16_t td_threshholding; + uint16_t td_fillorder; + uint16_t td_orientation; + uint16_t td_samplesperpixel; + uint32_t td_rowsperstrip; + uint16_t td_minsamplevalue, td_maxsamplevalue; + double *td_sminsamplevalue; + double *td_smaxsamplevalue; + float td_xresolution, td_yresolution; + uint16_t td_resolutionunit; + uint16_t td_planarconfig; + float td_xposition, td_yposition; + uint16_t td_pagenumber[2]; + uint16_t *td_colormap[3]; + uint16_t td_halftonehints[2]; + uint16_t td_extrasamples; + uint16_t *td_sampleinfo; + /* even though the name is misleading, td_stripsperimage is the number + * of striles (=strips or tiles) per plane, and td_nstrips the total + * number of striles */ + uint32_t td_stripsperimage; + uint32_t td_nstrips; /* size of offset & bytecount arrays */ + uint64_t + *td_stripoffset_p; /* should be accessed with TIFFGetStrileOffset */ + uint64_t *td_stripbytecount_p; /* should be accessed with + TIFFGetStrileByteCount */ + uint32_t + td_stripoffsetbyteallocsize; /* number of elements currently allocated + for td_stripoffset/td_stripbytecount. + Only used if TIFF_LAZYSTRILELOAD is set + */ +#ifdef STRIPBYTECOUNTSORTED_UNUSED + int td_stripbytecountsorted; /* is the bytecount array sorted ascending? */ +#endif + /* Be aware that the parameters of td_stripoffset_entry and + * td_stripbytecount_entry are swapped but tdir_offset is not + * and has to be swapped when used. */ + TIFFDirEntry td_stripoffset_entry; /* for deferred loading */ + TIFFDirEntry td_stripbytecount_entry; /* for deferred loading */ + uint16_t td_nsubifd; + uint64_t *td_subifd; + /* YCbCr parameters */ + uint16_t td_ycbcrsubsampling[2]; + uint16_t td_ycbcrpositioning; + /* Colorimetry parameters */ + uint16_t *td_transferfunction[3]; + float *td_refblackwhite; + /* CMYK parameters */ + int td_inknameslen; + char *td_inknames; + uint16_t td_numberofinks; /* number of inks in InkNames string */ + + int td_customValueCount; + TIFFTagValue *td_customValues; + + unsigned char + td_deferstrilearraywriting; /* see TIFFDeferStrileArrayWriting() */ + + unsigned char + td_iswrittentofile; /* indicates if current IFD is present on file */ + + /* LibTIFF writes all data that does not fit into the IFD entries directly + * after the IFD tag entry part. When reading, only the IFD data directly + * and continuously behind the IFD tags is taken into account for the IFD + * data size.*/ + uint64_t td_dirdatasize_write; /* auxiliary for evaluating size of IFD data + to be written */ + uint64_t td_dirdatasize_read; /* auxiliary for evaluating size of IFD data + read from file */ + uint32_t td_dirdatasize_Noffsets; /* auxiliary counter for + tif_dir.td_dirdatasize_offsets array */ + TIFFEntryOffsetAndLength + *td_dirdatasize_offsets; /* auxiliary array for all offsets of IFD tag + entries with data outside the IFD tag + entries. */ +} TIFFDirectory; + +/* + * Field flags used to indicate fields that have been set in a directory, and + * to reference fields when manipulating a directory. + */ + +/* + * FIELD_IGNORE is used to signify tags that are to be processed but otherwise + * ignored. This permits antiquated tags to be quietly read and discarded. + * Note that a bit *is* allocated for ignored tags; this is understood by the + * directory reading logic which uses this fact to avoid special-case handling + */ +#define FIELD_IGNORE 0 + +/* multi-item fields */ +#define FIELD_IMAGEDIMENSIONS 1 +#define FIELD_TILEDIMENSIONS 2 +#define FIELD_RESOLUTION 3 +#define FIELD_POSITION 4 + +/* single-item fields */ +#define FIELD_SUBFILETYPE 5 +#define FIELD_BITSPERSAMPLE 6 +#define FIELD_COMPRESSION 7 +#define FIELD_PHOTOMETRIC 8 +#define FIELD_THRESHHOLDING 9 +#define FIELD_FILLORDER 10 +#define FIELD_ORIENTATION 15 +#define FIELD_SAMPLESPERPIXEL 16 +#define FIELD_ROWSPERSTRIP 17 +#define FIELD_MINSAMPLEVALUE 18 +#define FIELD_MAXSAMPLEVALUE 19 +#define FIELD_PLANARCONFIG 20 +#define FIELD_RESOLUTIONUNIT 22 +#define FIELD_PAGENUMBER 23 +#define FIELD_STRIPBYTECOUNTS 24 +#define FIELD_STRIPOFFSETS 25 +#define FIELD_COLORMAP 26 +#define FIELD_EXTRASAMPLES 31 +#define FIELD_SAMPLEFORMAT 32 +#define FIELD_SMINSAMPLEVALUE 33 +#define FIELD_SMAXSAMPLEVALUE 34 +#define FIELD_IMAGEDEPTH 35 +#define FIELD_TILEDEPTH 36 +#define FIELD_HALFTONEHINTS 37 +#define FIELD_YCBCRSUBSAMPLING 39 +#define FIELD_YCBCRPOSITIONING 40 +#define FIELD_REFBLACKWHITE 41 +#define FIELD_TRANSFERFUNCTION 44 +#define FIELD_INKNAMES 46 +#define FIELD_SUBIFD 49 +#define FIELD_NUMBEROFINKS 50 +/* FIELD_CUSTOM (see tiffio.h) 65 */ +/* end of support for well-known tags; codec-private tags follow */ +#define FIELD_CODEC 66 /* base of codec-private tags */ + +/* + * Pseudo-tags don't normally need field bits since they are not written to an + * output file (by definition). The library also has express logic to always + * query a codec for a pseudo-tag so allocating a field bit for one is a + * waste. If codec wants to promote the notion of a pseudo-tag being ``set'' + * or ``unset'' then it can do using internal state flags without polluting + * the field bit space defined for real tags. + */ +#define FIELD_PSEUDO 0 + +#define FIELD_LAST (32 * FIELDSET_ITEMS - 1) + +#define BITn(n) (((uint32_t)1L) << ((n)&0x1f)) +#define BITFIELDn(tif, n) ((tif)->tif_dir.td_fieldsset[(n) / 32]) +#define TIFFFieldSet(tif, field) (BITFIELDn(tif, field) & BITn(field)) +#define TIFFSetFieldBit(tif, field) (BITFIELDn(tif, field) |= BITn(field)) +#define TIFFClrFieldBit(tif, field) (BITFIELDn(tif, field) &= ~BITn(field)) + +#define FieldSet(fields, f) (fields[(f) / 32] & BITn(f)) +#define ResetFieldBit(fields, f) (fields[(f) / 32] &= ~BITn(f)) + +typedef enum +{ + TIFF_SETGET_UNDEFINED = 0, + TIFF_SETGET_ASCII = 1, + TIFF_SETGET_UINT8 = 2, + TIFF_SETGET_SINT8 = 3, + TIFF_SETGET_UINT16 = 4, + TIFF_SETGET_SINT16 = 5, + TIFF_SETGET_UINT32 = 6, + TIFF_SETGET_SINT32 = 7, + TIFF_SETGET_UINT64 = 8, + TIFF_SETGET_SINT64 = 9, + TIFF_SETGET_FLOAT = 10, + TIFF_SETGET_DOUBLE = 11, + TIFF_SETGET_IFD8 = 12, + TIFF_SETGET_INT = 13, + TIFF_SETGET_UINT16_PAIR = 14, + TIFF_SETGET_C0_ASCII = 15, + TIFF_SETGET_C0_UINT8 = 16, + TIFF_SETGET_C0_SINT8 = 17, + TIFF_SETGET_C0_UINT16 = 18, + TIFF_SETGET_C0_SINT16 = 19, + TIFF_SETGET_C0_UINT32 = 20, + TIFF_SETGET_C0_SINT32 = 21, + TIFF_SETGET_C0_UINT64 = 22, + TIFF_SETGET_C0_SINT64 = 23, + TIFF_SETGET_C0_FLOAT = 24, + TIFF_SETGET_C0_DOUBLE = 25, + TIFF_SETGET_C0_IFD8 = 26, + TIFF_SETGET_C16_ASCII = 27, + TIFF_SETGET_C16_UINT8 = 28, + TIFF_SETGET_C16_SINT8 = 29, + TIFF_SETGET_C16_UINT16 = 30, + TIFF_SETGET_C16_SINT16 = 31, + TIFF_SETGET_C16_UINT32 = 32, + TIFF_SETGET_C16_SINT32 = 33, + TIFF_SETGET_C16_UINT64 = 34, + TIFF_SETGET_C16_SINT64 = 35, + TIFF_SETGET_C16_FLOAT = 36, + TIFF_SETGET_C16_DOUBLE = 37, + TIFF_SETGET_C16_IFD8 = 38, + TIFF_SETGET_C32_ASCII = 39, + TIFF_SETGET_C32_UINT8 = 40, + TIFF_SETGET_C32_SINT8 = 41, + TIFF_SETGET_C32_UINT16 = 42, + TIFF_SETGET_C32_SINT16 = 43, + TIFF_SETGET_C32_UINT32 = 44, + TIFF_SETGET_C32_SINT32 = 45, + TIFF_SETGET_C32_UINT64 = 46, + TIFF_SETGET_C32_SINT64 = 47, + TIFF_SETGET_C32_FLOAT = 48, + TIFF_SETGET_C32_DOUBLE = 49, + TIFF_SETGET_C32_IFD8 = 50, + TIFF_SETGET_OTHER = 51 +} TIFFSetGetFieldType; + +#if defined(__cplusplus) +extern "C" +{ +#endif + + extern const TIFFFieldArray *_TIFFGetFields(void); + extern const TIFFFieldArray *_TIFFGetExifFields(void); + extern const TIFFFieldArray *_TIFFGetGpsFields(void); + extern void _TIFFSetupFields(TIFF *tif, const TIFFFieldArray *infoarray); + extern void _TIFFPrintFieldInfo(TIFF *, FILE *); + + extern int _TIFFFillStriles(TIFF *); + + typedef enum + { + tfiatImage, + tfiatExif, + tfiatGps, /* EXIF-GPS fields array type */ + tfiatOther + } TIFFFieldArrayType; + + struct _TIFFFieldArray + { + TIFFFieldArrayType type; /* array type, will be used to determine if IFD + is image and such */ + uint32_t allocated_size; /* 0 if array is constant, other if modified by + future definition extension support */ + uint32_t count; /* number of elements in fields array */ + TIFFField *fields; /* actual field info */ + }; + + struct _TIFFField + { + uint32_t field_tag; /* field's tag */ + short field_readcount; /* read count/TIFF_VARIABLE/TIFF_SPP */ + short field_writecount; /* write count/TIFF_VARIABLE */ + TIFFDataType field_type; /* type of associated data */ + uint32_t field_anonymous; /* if true, this is a unknown / + anonymous tag */ + TIFFSetGetFieldType set_get_field_type; /* type to be passed to + TIFFSetField, TIFFGetField */ + unsigned short field_bit; /* bit in fieldsset bit vector */ + unsigned char field_oktochange; /* if true, can change while writing */ + unsigned char field_passcount; /* if true, pass dir count on set */ + char *field_name; /* ASCII name */ + TIFFFieldArray *field_subfields; /* if field points to child ifds, child + ifd field definition array */ + }; + + extern int _TIFFMergeFields(TIFF *, const TIFFField[], uint32_t); + extern const TIFFField *_TIFFFindOrRegisterField(TIFF *, uint32_t, + TIFFDataType); + extern TIFFField *_TIFFCreateAnonField(TIFF *, uint32_t, TIFFDataType); + extern int _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag); + extern int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, + uint64_t diroff); + extern int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, + tdir_t *dirn); + extern int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, + uint64_t *diroff); + extern int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, + uint64_t diroff); + +#if defined(__cplusplus) +} +#endif +#endif /* _TIFFDIR_ */ diff --git a/cpp/3rd_party/libtiff/tif_dirinfo.c b/cpp/3rd_party/libtiff/tif_dirinfo.c new file mode 100644 index 0000000000..c038497cf4 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_dirinfo.c @@ -0,0 +1,1364 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Core Directory Tag Support. + */ +#include "tiffiop.h" +#include + +/* + * NOTE: THIS ARRAY IS ASSUMED TO BE SORTED BY TAG. + * + * NOTE: The second field (field_readcount) and third field (field_writecount) + * sometimes use the values TIFF_VARIABLE (-1), TIFF_VARIABLE2 (-3) + * and TIFF_SPP (-2). The macros should be used but would throw off + * the formatting of the code, so please interpret the -1, -2 and -3 + * values accordingly. + */ + +/* const object should be initialized */ +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4132) +#endif +static const TIFFFieldArray tiffFieldArray; +static const TIFFFieldArray exifFieldArray; +static const TIFFFieldArray gpsFieldArray; +#ifdef _MSC_VER +#pragma warning(pop) +#endif +/*--: Rational2Double: -- + * The Rational2Double upgraded libtiff functionality allows the definition and + * achievement of true double-precision accuracy for TIFF tags of RATIONAL type + * and field_bit=FIELD_CUSTOM using the set_get_field_type = TIFF_SETGET_DOUBLE. + * Unfortunately, that changes the old implemented interface for TIFFGetField(). + * In order to keep the old TIFFGetField() interface behavior those tags have to + * be redefined with set_get_field_type = TIFF_SETGET_FLOAT! + * + * Rational custom arrays are already defined as _Cxx_FLOAT, thus can stay. + * + */ + +/* clang-format off */ /* for better readability of tag comments */ +static const TIFFField tiffFields[] = { + {TIFFTAG_SUBFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_SUBFILETYPE, 1, 0, "SubfileType", NULL}, + {TIFFTAG_OSUBFILETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "OldSubfileType", NULL}, + {TIFFTAG_IMAGEWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_IMAGEDIMENSIONS, 0, 0, "ImageWidth", NULL}, + {TIFFTAG_IMAGELENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_IMAGEDIMENSIONS, 1, 0, "ImageLength", NULL}, + {TIFFTAG_BITSPERSAMPLE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_BITSPERSAMPLE, 0, 0, "BitsPerSample", NULL}, + {TIFFTAG_COMPRESSION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_COMPRESSION, 0, 0, "Compression", NULL}, + {TIFFTAG_PHOTOMETRIC, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_PHOTOMETRIC, 0, 0, "PhotometricInterpretation", NULL}, + {TIFFTAG_THRESHHOLDING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_THRESHHOLDING, 1, 0, "Threshholding", NULL}, + {TIFFTAG_CELLWIDTH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CellWidth", NULL}, + {TIFFTAG_CELLLENGTH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CellLength", NULL}, + {TIFFTAG_FILLORDER, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_FILLORDER, 0, 0, "FillOrder", NULL}, + {TIFFTAG_DOCUMENTNAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DocumentName", NULL}, + {TIFFTAG_IMAGEDESCRIPTION, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "ImageDescription", NULL}, + {TIFFTAG_MAKE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Make", NULL}, + {TIFFTAG_MODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Model", NULL}, + {TIFFTAG_STRIPOFFSETS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, FIELD_STRIPOFFSETS, 0, 0, "StripOffsets", NULL}, + {TIFFTAG_ORIENTATION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_ORIENTATION, 0, 0, "Orientation", NULL}, + {TIFFTAG_SAMPLESPERPIXEL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_SAMPLESPERPIXEL, 0, 0, "SamplesPerPixel", NULL}, + {TIFFTAG_ROWSPERSTRIP, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_ROWSPERSTRIP, 0, 0, "RowsPerStrip", NULL}, + {TIFFTAG_STRIPBYTECOUNTS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, FIELD_STRIPBYTECOUNTS, 0, 0, "StripByteCounts", NULL}, + {TIFFTAG_MINSAMPLEVALUE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_MINSAMPLEVALUE, 1, 0, "MinSampleValue", NULL}, + {TIFFTAG_MAXSAMPLEVALUE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_MAXSAMPLEVALUE, 1, 0, "MaxSampleValue", NULL}, + {TIFFTAG_XRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_RESOLUTION, 1, 0, "XResolution", NULL}, + {TIFFTAG_YRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_RESOLUTION, 1, 0, "YResolution", NULL}, + {TIFFTAG_PLANARCONFIG, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_PLANARCONFIG, 0, 0, "PlanarConfiguration", NULL}, + {TIFFTAG_PAGENAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "PageName", NULL}, + {TIFFTAG_XPOSITION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_POSITION, 1, 0, "XPosition", NULL}, + {TIFFTAG_YPOSITION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_POSITION, 1, 0, "YPosition", NULL}, + {TIFFTAG_FREEOFFSETS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 0, 0, "FreeOffsets", NULL}, + {TIFFTAG_FREEBYTECOUNTS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 0, 0, "FreeByteCounts", NULL}, + {TIFFTAG_GRAYRESPONSEUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "GrayResponseUnit", NULL}, + {TIFFTAG_GRAYRESPONSECURVE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "GrayResponseCurve", NULL}, + {TIFFTAG_RESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_RESOLUTIONUNIT, 1, 0, "ResolutionUnit", NULL}, + {TIFFTAG_PAGENUMBER, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, FIELD_PAGENUMBER, 1, 0, "PageNumber", NULL}, + {TIFFTAG_COLORRESPONSEUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "ColorResponseUnit", NULL}, + {TIFFTAG_TRANSFERFUNCTION, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_OTHER, FIELD_TRANSFERFUNCTION, 1, 0, "TransferFunction", NULL}, + {TIFFTAG_SOFTWARE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Software", NULL}, + {TIFFTAG_DATETIME, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DateTime", NULL}, + {TIFFTAG_ARTIST, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Artist", NULL}, + {TIFFTAG_HOSTCOMPUTER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "HostComputer", NULL}, + {TIFFTAG_WHITEPOINT, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "WhitePoint", NULL}, + {TIFFTAG_PRIMARYCHROMATICITIES, 6, 6, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "PrimaryChromaticities", NULL}, + {TIFFTAG_COLORMAP, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_OTHER, FIELD_COLORMAP, 1, 0, "ColorMap", NULL}, + {TIFFTAG_HALFTONEHINTS, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, FIELD_HALFTONEHINTS, 1, 0, "HalftoneHints", NULL}, + {TIFFTAG_TILEWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_TILEDIMENSIONS, 0, 0, "TileWidth", NULL}, + {TIFFTAG_TILELENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_TILEDIMENSIONS, 0, 0, "TileLength", NULL}, + {TIFFTAG_TILEOFFSETS, -1, 1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, FIELD_STRIPOFFSETS, 0, 0, "TileOffsets", NULL}, + {TIFFTAG_TILEBYTECOUNTS, -1, 1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, FIELD_STRIPBYTECOUNTS, 0, 0, "TileByteCounts", NULL}, + {TIFFTAG_SUBIFD, -1, -1, TIFF_IFD8, 0, TIFF_SETGET_C16_IFD8, FIELD_SUBIFD, 1, 1, "SubIFD", (TIFFFieldArray *)&tiffFieldArray}, + {TIFFTAG_INKSET, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 0, 0, "InkSet", NULL}, + {TIFFTAG_INKNAMES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_C16_ASCII, FIELD_INKNAMES, 1, 1, "InkNames", NULL}, + {TIFFTAG_NUMBEROFINKS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_NUMBEROFINKS, 1, 0, "NumberOfInks", NULL}, + {TIFFTAG_DOTRANGE, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, FIELD_CUSTOM, 0, 0, "DotRange", NULL}, + {TIFFTAG_TARGETPRINTER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "TargetPrinter", NULL}, + {TIFFTAG_EXTRASAMPLES, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_EXTRASAMPLES, 0, 1, "ExtraSamples", NULL}, + {TIFFTAG_SAMPLEFORMAT, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_SAMPLEFORMAT, 0, 0, "SampleFormat", NULL}, + {TIFFTAG_SMINSAMPLEVALUE, -2, -1, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, FIELD_SMINSAMPLEVALUE, 1, 0, "SMinSampleValue", NULL}, + {TIFFTAG_SMAXSAMPLEVALUE, -2, -1, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, FIELD_SMAXSAMPLEVALUE, 1, 0, "SMaxSampleValue", NULL}, + {TIFFTAG_CLIPPATH, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 0, 1, "ClipPath", NULL}, + {TIFFTAG_XCLIPPATHUNITS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 0, 0, "XClipPathUnits", NULL}, + {TIFFTAG_YCLIPPATHUNITS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 0, 0, "YClipPathUnits", NULL}, + {TIFFTAG_YCBCRCOEFFICIENTS, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 0, 0, "YCbCrCoefficients", NULL}, + {TIFFTAG_YCBCRSUBSAMPLING, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, FIELD_YCBCRSUBSAMPLING, 0, 0, "YCbCrSubsampling", NULL}, + {TIFFTAG_YCBCRPOSITIONING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_YCBCRPOSITIONING, 0, 0, "YCbCrPositioning", NULL}, + {TIFFTAG_REFERENCEBLACKWHITE, 6, 6, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_REFBLACKWHITE, 1, 0, "ReferenceBlackWhite", NULL}, + {TIFFTAG_XMLPACKET, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "XMLPacket", NULL}, + /* begin SGI tags */ + {TIFFTAG_MATTEING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_EXTRASAMPLES, 0, 0, "Matteing", NULL}, + {TIFFTAG_DATATYPE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_SAMPLEFORMAT, 0, 0, "DataType", NULL}, + {TIFFTAG_IMAGEDEPTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_IMAGEDEPTH, 0, 0, "ImageDepth", NULL}, + {TIFFTAG_TILEDEPTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_TILEDEPTH, 0, 0, "TileDepth", NULL}, + /* end SGI tags */ + /* begin Pixar tags */ + {TIFFTAG_PIXAR_IMAGEFULLWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ImageFullWidth", NULL}, + {TIFFTAG_PIXAR_IMAGEFULLLENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ImageFullLength", NULL}, + {TIFFTAG_PIXAR_TEXTUREFORMAT, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "TextureFormat", NULL}, + {TIFFTAG_PIXAR_WRAPMODES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "TextureWrapModes", NULL}, + {TIFFTAG_PIXAR_FOVCOT, 1, 1, TIFF_FLOAT, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "FieldOfViewCotangent", NULL}, + {TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN, 16, 16, TIFF_FLOAT, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "MatrixWorldToScreen", NULL}, + {TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA, 16, 16, TIFF_FLOAT, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "MatrixWorldToCamera", NULL}, + {TIFFTAG_COPYRIGHT, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Copyright", NULL}, + /* end Pixar tags */ + {TIFFTAG_RICHTIFFIPTC, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "RichTIFFIPTC", NULL}, + {TIFFTAG_PHOTOSHOP, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "Photoshop", NULL}, + /*--: EXIFIFD and GPSIFD specified as TIFF_LONG by Aware-Systems and not TIFF_IFD8 as in original LibTiff. However, for IFD-like tags, + * libtiff uses the data type TIFF_IFD8 in tiffFields[]-tag definition combined with a special handling procedure in order to write either + * a 32-bit value and the TIFF_IFD type-id into ClassicTIFF files or a 64-bit value and the TIFF_IFD8 type-id into BigTIFF files. */ + {TIFFTAG_EXIFIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, FIELD_CUSTOM, 1, 0, "EXIFIFDOffset", (TIFFFieldArray *)&exifFieldArray}, + {TIFFTAG_ICCPROFILE, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "ICC Profile", NULL}, + {TIFFTAG_GPSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, FIELD_CUSTOM, 1, 0, "GPSIFDOffset", (TIFFFieldArray *)&gpsFieldArray}, + {TIFFTAG_FAXRECVPARAMS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, TRUE, FALSE, "FaxRecvParams", NULL}, + {TIFFTAG_FAXSUBADDRESS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, TRUE, FALSE, "FaxSubAddress", NULL}, + {TIFFTAG_FAXRECVTIME, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, TRUE, FALSE, "FaxRecvTime", NULL}, + {TIFFTAG_FAXDCS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, TRUE, FALSE, "FaxDcs", NULL}, + {TIFFTAG_STONITS, 1, 1, TIFF_DOUBLE, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 0, 0, "StoNits", NULL}, + {TIFFTAG_IMAGESOURCEDATA, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "Adobe Photoshop Document Data Block", NULL}, + {TIFFTAG_INTEROPERABILITYIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, FIELD_CUSTOM, 0, 0, "InteroperabilityIFDOffset", NULL}, + /* begin DNG tags */ + {TIFFTAG_DNGVERSION, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "DNGVersion", NULL}, + {TIFFTAG_DNGBACKWARDVERSION, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "DNGBackwardVersion", NULL}, + {TIFFTAG_UNIQUECAMERAMODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "UniqueCameraModel", NULL}, + {TIFFTAG_LOCALIZEDCAMERAMODEL, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "LocalizedCameraModel", NULL}, + {TIFFTAG_CFAPLANECOLOR, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "CFAPlaneColor", NULL}, + {TIFFTAG_CFALAYOUT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CFALayout", NULL}, + {TIFFTAG_LINEARIZATIONTABLE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_CUSTOM, 1, 1, "LinearizationTable", NULL}, + {TIFFTAG_BLACKLEVELREPEATDIM, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, FIELD_CUSTOM, 1, 0, "BlackLevelRepeatDim", NULL}, + {TIFFTAG_BLACKLEVEL, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "BlackLevel", NULL}, + {TIFFTAG_BLACKLEVELDELTAH, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "BlackLevelDeltaH", NULL}, + {TIFFTAG_BLACKLEVELDELTAV, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "BlackLevelDeltaV", NULL}, + {TIFFTAG_WHITELEVEL, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, FIELD_CUSTOM, 1, 1, "WhiteLevel", NULL}, + {TIFFTAG_DEFAULTSCALE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "DefaultScale", NULL}, + {TIFFTAG_BESTQUALITYSCALE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "BestQualityScale", NULL}, + {TIFFTAG_DEFAULTCROPORIGIN, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "DefaultCropOrigin", NULL}, + {TIFFTAG_DEFAULTCROPSIZE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "DefaultCropSize", NULL}, + {TIFFTAG_COLORMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ColorMatrix1", NULL}, + {TIFFTAG_COLORMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ColorMatrix2", NULL}, + {TIFFTAG_CAMERACALIBRATION1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "CameraCalibration1", NULL}, + {TIFFTAG_CAMERACALIBRATION2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "CameraCalibration2", NULL}, + {TIFFTAG_REDUCTIONMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ReductionMatrix1", NULL}, + {TIFFTAG_REDUCTIONMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ReductionMatrix2", NULL}, + {TIFFTAG_ANALOGBALANCE, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "AnalogBalance", NULL}, + {TIFFTAG_ASSHOTNEUTRAL, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "AsShotNeutral", NULL}, + {TIFFTAG_ASSHOTWHITEXY, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "AsShotWhiteXY", NULL}, + {TIFFTAG_BASELINEEXPOSURE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "BaselineExposure", NULL}, + {TIFFTAG_BASELINENOISE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "BaselineNoise", NULL}, + {TIFFTAG_BASELINESHARPNESS, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "BaselineSharpness", NULL}, + {TIFFTAG_BAYERGREENSPLIT, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "BayerGreenSplit", NULL}, + {TIFFTAG_LINEARRESPONSELIMIT, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "LinearResponseLimit", NULL}, + {TIFFTAG_CAMERASERIALNUMBER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "CameraSerialNumber", NULL}, + {TIFFTAG_LENSINFO, 4, 4, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "LensInfo", NULL}, + {TIFFTAG_CHROMABLURRADIUS, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ChromaBlurRadius", NULL}, + {TIFFTAG_ANTIALIASSTRENGTH, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "AntiAliasStrength", NULL}, + {TIFFTAG_SHADOWSCALE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ShadowScale", NULL}, + {TIFFTAG_DNGPRIVATEDATA, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "DNGPrivateData", NULL}, + {TIFFTAG_MAKERNOTESAFETY, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "MakerNoteSafety", NULL}, + {TIFFTAG_CALIBRATIONILLUMINANT1, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CalibrationIlluminant1", NULL}, + {TIFFTAG_CALIBRATIONILLUMINANT2, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CalibrationIlluminant2", NULL}, + {TIFFTAG_RAWDATAUNIQUEID, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "RawDataUniqueID", NULL}, + {TIFFTAG_ORIGINALRAWFILENAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "OriginalRawFileName", NULL}, + {TIFFTAG_ORIGINALRAWFILEDATA, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "OriginalRawFileData", NULL}, + {TIFFTAG_ACTIVEAREA, 4, 4, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "ActiveArea", NULL}, + {TIFFTAG_MASKEDAREAS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, FIELD_CUSTOM, 1, 1, "MaskedAreas", NULL}, + {TIFFTAG_ASSHOTICCPROFILE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "AsShotICCProfile", NULL}, + {TIFFTAG_ASSHOTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "AsShotPreProfileMatrix", NULL}, + {TIFFTAG_CURRENTICCPROFILE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "CurrentICCProfile", NULL}, + {TIFFTAG_CURRENTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "CurrentPreProfileMatrix", NULL}, + {TIFFTAG_PERSAMPLE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "PerSample", NULL}, +#if 0 + /* begin DNG 1.2.0.0 tags */ + {TIFFTAG_COLORIMETRICREFERENCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "ColorimetricReference", NULL}, + {TIFFTAG_CAMERACALIBRATIONSIGNATURE, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "CameraCalibrationSignature", NULL}, + {TIFFTAG_PROFILECALIBRATIONSIGNATURE, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "ProfileCalibrationSignature", NULL}, + {TIFFTAG_EXTRACAMERAPROFILES, -1, -1, TIFF_IFD8, 0, TIFF_SETGET_C16_IFD8, FIELD_CUSTOM, 1, 1, "ExtraCameraProfiles", NULL}, + {TIFFTAG_ASSHOTPROFILENAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "AsShotProfileName", NULL}, + {TIFFTAG_NOISEREDUCTIONAPPLIED, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "NoiseReductionApplied", NULL}, + {TIFFTAG_PROFILENAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "ProfileName", NULL}, + {TIFFTAG_PROFILEHUESATMAPDIMS, 3, 3, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "ProfileHueSatMapDims", NULL}, + {TIFFTAG_PROFILEHUESATMAPDATA1, -1, -1, TIFF_FLOAT, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ProfileHueSatMapData1", NULL}, + {TIFFTAG_PROFILEHUESATMAPDATA2, -1, -1, TIFF_FLOAT, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ProfileHueSatMapData2", NULL}, + {TIFFTAG_PROFILETONECURVE, -1, -1, TIFF_FLOAT, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ProfileToneCurve", NULL}, + {TIFFTAG_PROFILEEMBEDPOLICY, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ProfileEmbedPolicy", NULL}, + {TIFFTAG_PROFILECOPYRIGHT, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "ProfileCopyright", NULL}, + {TIFFTAG_FORWARDMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ForwardMatrix1", NULL}, + {TIFFTAG_FORWARDMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ForwardMatrix2", NULL}, + {TIFFTAG_PREVIEWAPPLICATIONNAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "PreviewApplicationName", NULL}, + {TIFFTAG_PREVIEWAPPLICATIONVERSION, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "PreviewApplicationVersion", NULL}, + {TIFFTAG_PREVIEWSETTINGSNAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "PreviewSettingsName", NULL}, + {TIFFTAG_PREVIEWSETTINGSDIGEST, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "PreviewSettingsDigest", NULL}, + {TIFFTAG_PREVIEWCOLORSPACE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "PreviewColorSpace", NULL}, + {TIFFTAG_PREVIEWDATETIME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "PreviewDateTime", NULL}, + {TIFFTAG_RAWIMAGEDIGEST, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "RawImageDigest", NULL}, + {TIFFTAG_ORIGINALRAWFILEDIGEST, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "OriginalRawFileDigest", NULL}, + {TIFFTAG_SUBTILEBLOCKSIZE, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "SubTileBlockSize", NULL}, + {TIFFTAG_ROWINTERLEAVEFACTOR, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "RowInterleaveFactor", NULL}, + {TIFFTAG_PROFILELOOKTABLEDIMS, 3, 3, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "ProfileLookTableDims", NULL}, + {TIFFTAG_PROFILELOOKTABLEDATA, -1, -1, TIFF_FLOAT, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ProfileLookTableData", NULL}, + /* begin DNG 1.3.0.0 tags */ + {TIFFTAG_OPCODELIST1, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "OpcodeList1", NULL}, + {TIFFTAG_OPCODELIST2, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "OpcodeList2", NULL}, + {TIFFTAG_OPCODELIST3, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "OpcodeList3", NULL}, + {TIFFTAG_NOISEPROFILE, -1, -1, TIFF_DOUBLE, 0, TIFF_SETGET_C16_DOUBLE, FIELD_CUSTOM, 1, 1, "NoiseProfile", NULL}, + /* begin DNG 1.4.0.0 tags */ + {TIFFTAG_DEFAULTUSERCROP, 4, 4, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "DefaultUserCrop", NULL}, + {TIFFTAG_DEFAULTBLACKRENDER, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "DefaultBlackRender", NULL}, + {TIFFTAG_BASELINEEXPOSUREOFFSET, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "BaselineExposureOffset", NULL}, + {TIFFTAG_PROFILELOOKTABLEENCODING, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ProfileLookTableEncoding", NULL}, + {TIFFTAG_PROFILEHUESATMAPENCODING, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ProfileHueSatMapEncoding", NULL}, + {TIFFTAG_ORIGINALDEFAULTFINALSIZE, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "OriginalDefaultFinalSize", NULL}, + {TIFFTAG_ORIGINALBESTQUALITYFINALSIZE, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "OriginalBestQualityFinalSize", NULL}, + {TIFFTAG_ORIGINALDEFAULTCROPSIZE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "OriginalDefaultCropSize", NULL}, /* could also be rational */ + {TIFFTAG_NEWRAWIMAGEDIGEST, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "NewRawImageDigest", NULL}, + {TIFFTAG_RAWTOPREVIEWGAIN, 1, 1, TIFF_DOUBLE, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "RawToPreviewGain", NULL}, + /* begin DNG 1.5.0.0 tags */ + {TIFFTAG_DEPTHFORMAT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "DepthFormat", NULL}, + {TIFFTAG_DEPTHNEAR, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "DepthNear", NULL}, + {TIFFTAG_DEPTHFAR, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "DepthFar", NULL}, + {TIFFTAG_DEPTHUNITS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "DepthUnits", NULL}, + {TIFFTAG_DEPTHMEASURETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "DepthMeasureType", NULL}, + {TIFFTAG_ENHANCEPARAMS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "EnhanceParams", NULL}, + /* begin DNG 1.6.0.0 tags */ + {TIFFTAG_PROFILEGAINTABLEMAP, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "ProfileGainTableMap", NULL}, + {TIFFTAG_SEMANTICNAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SemanticName", NULL}, + {TIFFTAG_SEMANTICINSTANCEID, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SemanticInstanceID", NULL}, + {TIFFTAG_MASKSUBAREA, 4, 4, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "MaskSubArea", NULL}, + {TIFFTAG_RGBTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "RGBTables", NULL}, + {TIFFTAG_CALIBRATIONILLUMINANT3, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CalibrationIlluminant3", NULL}, + {TIFFTAG_COLORMATRIX3, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ColorMatrix3", NULL}, + {TIFFTAG_CAMERACALIBRATION3, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "CameraCalibration3", NULL}, + {TIFFTAG_REDUCTIONMATRIX3, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ReductionMatrix3", NULL}, + {TIFFTAG_PROFILEHUESATMAPDATA3, -1, -1, TIFF_FLOAT, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ProfileHueSatMapData3", NULL}, + {TIFFTAG_FORWARDMATRIX3, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "ForwardMatrix3", NULL}, + {TIFFTAG_ILLUMINANTDATA1, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "IlluminantData1", NULL}, + {TIFFTAG_ILLUMINANTDATA2, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "IlluminantData2", NULL}, + {TIFFTAG_ILLUMINANTDATA3, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "IlluminantData3", NULL}, + /* end DNG tags */ +#endif + /* begin TIFF/EP tags */ + {TIFFTAG_EP_CFAREPEATPATTERNDIM, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, FIELD_CUSTOM, 1, 0, "EP CFARepeatPatternDim", NULL}, + {TIFFTAG_EP_CFAPATTERN, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "EP CFAPattern", NULL}, +#if 0 + /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII. + * LibTiff defines it as ASCII and converts RATIONAL to an ASCII string. */ + {TIFFTAG_EP_BATTERYLEVEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "EP BatteryLevel", NULL}, + {TIFFTAG_EP_INTERLACE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP Interlace", NULL}, + /* TIFFTAG_EP_IPTC_NAA and TIFFTAG_RICHTIFFIPTC share the same tag number (33723) + * LibTIFF type is UNDEFINED or BYTE, but often times incorrectly specified as LONG, because TIFF/EP (ISO/DIS 12234-2) specifies type LONG or ASCII. */ + {TIFFTAG_EP_TIMEZONEOFFSET, -1, -1, TIFF_SSHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_CUSTOM, 1, 1, "EP TimeZoneOffset", NULL}, + {TIFFTAG_EP_SELFTIMERMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP SelfTimerMode", NULL}, + {TIFFTAG_EP_FLASHENERGY, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP FlashEnergy", NULL}, + {TIFFTAG_EP_SPATIALFREQUENCYRESPONSE, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "EP SpatialFrequencyResponse", NULL}, + {TIFFTAG_EP_NOISE, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "EP Noise", NULL}, + {TIFFTAG_EP_FOCALPLANEXRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "EP FocalPlaneXResolution", NULL}, + {TIFFTAG_EP_FOCALPLANEYRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "EP FocalPlaneYResolution", NULL}, + {TIFFTAG_EP_FOCALPLANERESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP FocalPlaneResolutionUnit", NULL}, + {TIFFTAG_EP_IMAGENUMBER, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "EP ImageNumber", NULL}, /* or SHORT */ + {TIFFTAG_EP_SECURITYCLASSIFICATION, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "EP SecurityClassification", NULL}, + {TIFFTAG_EP_IMAGEHISTORY, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "EP ImageHistory", NULL}, + {TIFFTAG_EP_EXPOSUREINDEX, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP ExposureIndex", NULL}, + {TIFFTAG_EP_STANDARDID, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "EP StandardId", NULL}, + {TIFFTAG_EP_SENSINGMETHOD, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP SensingMethod", NULL}, + /* TIFF/EP tags equivalent to EXIF tags, sometimes defined differently. */ + {TIFFTAG_EP_EXPOSURETIME, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP ExposureTime", NULL}, /*N=1 or 2 */ + {TIFFTAG_EP_FNUMBER, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP FNumber", NULL}, + {TIFFTAG_EP_EXPOSUREPROGRAM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP ExposureProgram", NULL}, + {TIFFTAG_EP_SPECTRALSENSITIVITY, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "EP SpectralSensitivity", NULL}, + {TIFFTAG_EP_ISOSPEEDRATINGS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP ISOSpeedRatings", NULL}, + {TIFFTAG_EP_OECF, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, FIELD_CUSTOM, 1, 1, "EP OptoelectricConversionFactor", NULL}, + {TIFFTAG_EP_DATETIMEORIGINAL, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "EP DateTimeOriginal", NULL}, + {TIFFTAG_EP_COMPRESSEDBITSPERPIXEL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "EP CompressedBitsPerPixel", NULL}, + {TIFFTAG_EP_SHUTTERSPEEDVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "EP ShutterSpeedValue", NULL}, + {TIFFTAG_EP_APERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "EP ApertureValue", NULL}, + {TIFFTAG_EP_BRIGHTNESSVALUE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP BrightnessValue", NULL}, + {TIFFTAG_EP_EXPOSUREBIASVALUE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP ExposureBiasValue", NULL}, /*N=1 or 2 */ + {TIFFTAG_EP_MAXAPERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "EP MaxApertureValue", NULL}, + {TIFFTAG_EP_SUBJECTDISTANCE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP SubjectDistance", NULL}, + {TIFFTAG_EP_METERINGMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP MeteringMode", NULL}, + {TIFFTAG_EP_LIGHTSOURCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP LightSource", NULL}, + {TIFFTAG_EP_FLASH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "EP Flash", NULL}, + {TIFFTAG_EP_FOCALLENGTH, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "EP FocalLength", NULL}, + {TIFFTAG_EP_SUBJECTLOCATION, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_CUSTOM, 1, 1, "EP SubjectLocation", NULL}, + /* end TIFF/EP tags */ +#endif + /* begin TIFF/FX tags */ + {TIFFTAG_INDEXED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "Indexed", NULL}, + {TIFFTAG_GLOBALPARAMETERSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, FIELD_CUSTOM, 1, 0, "GlobalParametersIFD", NULL}, + {TIFFTAG_PROFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ProfileType", NULL}, + {TIFFTAG_FAXPROFILE, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, FIELD_CUSTOM, 1, 0, "FaxProfile", NULL}, + {TIFFTAG_CODINGMETHODS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "CodingMethods", NULL}, + {TIFFTAG_VERSIONYEAR, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "VersionYear", NULL}, + {TIFFTAG_MODENUMBER, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, FIELD_CUSTOM, 1, 0, "ModeNumber", NULL}, + {TIFFTAG_DECODE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, FIELD_CUSTOM, 1, 1, "Decode", NULL}, + {TIFFTAG_IMAGEBASECOLOR, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_CUSTOM, 1, 1, "ImageBaseColor", NULL}, + {TIFFTAG_T82OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "T82Options", NULL}, + {TIFFTAG_STRIPROWCOUNTS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, FIELD_CUSTOM, 1, 1, "StripRowCounts", NULL}, + {TIFFTAG_IMAGELAYER, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, FIELD_CUSTOM, 1, 0, "ImageLayer", NULL}, + /* end TIFF/FX tags */ + /* begin pseudo tags */ +}; + +/* + * EXIF tags (Version 2.31, July 2016 plus version 2.32 May 2019) + */ +static const TIFFField exifFields[] = { + {EXIFTAG_EXPOSURETIME, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ExposureTime", NULL}, + {EXIFTAG_FNUMBER, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "FNumber", NULL}, + {EXIFTAG_EXPOSUREPROGRAM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "ExposureProgram", NULL}, + {EXIFTAG_SPECTRALSENSITIVITY, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SpectralSensitivity", NULL}, + /* After EXIF 2.2.1 ISOSpeedRatings is named PhotographicSensitivity. In addition, while "Count=Any", only 1 count should be used. */ + {EXIFTAG_ISOSPEEDRATINGS, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_CUSTOM, 1, 1, "ISOSpeedRatings", NULL}, + {EXIFTAG_OECF, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "OptoelectricConversionFactor", NULL}, + {EXIFTAG_SENSITIVITYTYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "SensitivityType", NULL}, + {EXIFTAG_STANDARDOUTPUTSENSITIVITY, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "StandardOutputSensitivity", NULL}, + {EXIFTAG_RECOMMENDEDEXPOSUREINDEX, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "RecommendedExposureIndex", NULL}, + {EXIFTAG_ISOSPEED, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ISOSpeed", NULL}, + {EXIFTAG_ISOSPEEDLATITUDEYYY, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ISOSpeedLatitudeyyy", NULL}, + {EXIFTAG_ISOSPEEDLATITUDEZZZ, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "ISOSpeedLatitudezzz", NULL}, + {EXIFTAG_EXIFVERSION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "ExifVersion", NULL}, + {EXIFTAG_DATETIMEORIGINAL, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DateTimeOriginal", NULL}, + {EXIFTAG_DATETIMEDIGITIZED, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DateTimeDigitized", NULL}, + {EXIFTAG_OFFSETTIME, 7, 7, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "OffsetTime", NULL}, + {EXIFTAG_OFFSETTIMEORIGINAL, 7, 7, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "OffsetTimeOriginal", NULL}, + {EXIFTAG_OFFSETTIMEDIGITIZED, 7, 7, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "OffsetTimeDigitized", NULL}, + {EXIFTAG_COMPONENTSCONFIGURATION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "ComponentsConfiguration", NULL}, + {EXIFTAG_COMPRESSEDBITSPERPIXEL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "CompressedBitsPerPixel", NULL}, + {EXIFTAG_SHUTTERSPEEDVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ShutterSpeedValue", NULL}, + {EXIFTAG_APERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ApertureValue", NULL}, + {EXIFTAG_BRIGHTNESSVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "BrightnessValue", NULL}, + {EXIFTAG_EXPOSUREBIASVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ExposureBiasValue", NULL}, + {EXIFTAG_MAXAPERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "MaxApertureValue", NULL}, + /*--: EXIFTAG_SUBJECTDISTANCE: LibTiff returns value of "-1" if numerator equals 4294967295 (0xFFFFFFFF) to indicate infinite distance! + * However, there are two other EXIF tags where numerator indicates a special value and six other cases where the denominator indicates special values, + * which are not treated within LibTiff!! */ + {EXIFTAG_SUBJECTDISTANCE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "SubjectDistance", NULL}, + {EXIFTAG_METERINGMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "MeteringMode", NULL}, + {EXIFTAG_LIGHTSOURCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "LightSource", NULL}, + {EXIFTAG_FLASH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "Flash", NULL}, + {EXIFTAG_FOCALLENGTH, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "FocalLength", NULL}, + {EXIFTAG_SUBJECTAREA, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, FIELD_CUSTOM, 1, 1, "SubjectArea", NULL}, + {EXIFTAG_MAKERNOTE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "MakerNote", NULL}, + {EXIFTAG_USERCOMMENT, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "UserComment", NULL}, + {EXIFTAG_SUBSECTIME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SubSecTime", NULL}, + {EXIFTAG_SUBSECTIMEORIGINAL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SubSecTimeOriginal", NULL}, + {EXIFTAG_SUBSECTIMEDIGITIZED, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SubSecTimeDigitized", NULL}, + {EXIFTAG_TEMPERATURE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "Temperature", NULL}, + {EXIFTAG_HUMIDITY, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "Humidity", NULL}, + {EXIFTAG_PRESSURE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "Pressure", NULL}, + {EXIFTAG_WATERDEPTH, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "WaterDepth", NULL}, + {EXIFTAG_ACCELERATION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "Acceleration", NULL}, + {EXIFTAG_CAMERAELEVATIONANGLE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "CameraElevationAngle", NULL}, + {EXIFTAG_FLASHPIXVERSION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "FlashpixVersion", NULL}, + {EXIFTAG_COLORSPACE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "ColorSpace", NULL}, + {EXIFTAG_PIXELXDIMENSION, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "PixelXDimension", NULL}, + {EXIFTAG_PIXELYDIMENSION, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, FIELD_CUSTOM, 1, 0, "PixelYDimension", NULL}, + {EXIFTAG_RELATEDSOUNDFILE, 13, 13, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "RelatedSoundFile", NULL}, + {EXIFTAG_FLASHENERGY, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "FlashEnergy", NULL}, + {EXIFTAG_SPATIALFREQUENCYRESPONSE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "SpatialFrequencyResponse", NULL}, + {EXIFTAG_FOCALPLANEXRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "FocalPlaneXResolution", NULL}, + {EXIFTAG_FOCALPLANEYRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "FocalPlaneYResolution", NULL}, + {EXIFTAG_FOCALPLANERESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "FocalPlaneResolutionUnit", NULL}, + {EXIFTAG_SUBJECTLOCATION, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, FIELD_CUSTOM, 1, 0, "SubjectLocation", NULL}, + {EXIFTAG_EXPOSUREINDEX, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "ExposureIndex", NULL}, + {EXIFTAG_SENSINGMETHOD, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "SensingMethod", NULL}, + {EXIFTAG_FILESOURCE, 1, 1, TIFF_UNDEFINED, 0, TIFF_SETGET_UINT8, FIELD_CUSTOM, 1, 0, "FileSource", NULL}, + {EXIFTAG_SCENETYPE, 1, 1, TIFF_UNDEFINED, 0, TIFF_SETGET_UINT8, FIELD_CUSTOM, 1, 0, "SceneType", NULL}, + {EXIFTAG_CFAPATTERN, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "CFAPattern", NULL}, + {EXIFTAG_CUSTOMRENDERED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CustomRendered", NULL}, + {EXIFTAG_EXPOSUREMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "ExposureMode", NULL}, + {EXIFTAG_WHITEBALANCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "WhiteBalance", NULL}, + {EXIFTAG_DIGITALZOOMRATIO, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "DigitalZoomRatio", NULL}, + {EXIFTAG_FOCALLENGTHIN35MMFILM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "FocalLengthIn35mmFilm", NULL}, + {EXIFTAG_SCENECAPTURETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "SceneCaptureType", NULL}, + {EXIFTAG_GAINCONTROL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "GainControl", NULL}, + {EXIFTAG_CONTRAST, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "Contrast", NULL}, + {EXIFTAG_SATURATION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "Saturation", NULL}, + {EXIFTAG_SHARPNESS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "Sharpness", NULL}, + {EXIFTAG_DEVICESETTINGDESCRIPTION, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "DeviceSettingDescription", NULL}, + {EXIFTAG_SUBJECTDISTANCERANGE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "SubjectDistanceRange", NULL}, + {EXIFTAG_IMAGEUNIQUEID, 33, 33, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "ImageUniqueID", NULL}, + {EXIFTAG_CAMERAOWNERNAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "CameraOwnerName", NULL}, + {EXIFTAG_BODYSERIALNUMBER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "BodySerialNumber", NULL}, + {EXIFTAG_LENSSPECIFICATION, 4, 4, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, FIELD_CUSTOM, 1, 0, "LensSpecification", NULL}, + {EXIFTAG_LENSMAKE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "LensMake", NULL}, + {EXIFTAG_LENSMODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "LensModel", NULL}, + {EXIFTAG_LENSSERIALNUMBER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "LensSerialNumber", NULL}, + {EXIFTAG_GAMMA, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_FLOAT, FIELD_CUSTOM, 1, 0, "Gamma", NULL}, + {EXIFTAG_COMPOSITEIMAGE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "CompositeImage", NULL}, + {EXIFTAG_SOURCEIMAGENUMBEROFCOMPOSITEIMAGE, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, FIELD_CUSTOM, 1, 0, "SourceImageNumberOfCompositeImage", NULL}, + {EXIFTAG_SOURCEEXPOSURETIMESOFCOMPOSITEIMAGE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, + "SourceExposureTimesOfCompositeImage", NULL}}; +/* + * EXIF-GPS tags (Version 2.31, July 2016; nothing changed for version 2.32 May + * 2019) + */ + +static const TIFFField gpsFields[] = { + /* For the GPS tag definitions in gpsFields[] the standard definition for Rationals is TIFF_SETGET_DOUBLE and TIFF_SETGET_C0_FLOAT. + *-- ATTENTION: After the upgrade with Rational2Double, the GPSTAG values can now be written and also read in double precision! + * In order to achieve double precision for GPS tags: Standard definitions for GPSTAG is kept to TIFF_SETGET_DOUBLE + * and TIFF_SETGET_C0_FLOAT is changed to TIFF_SETGET_C0_DOUBLE. + */ + {GPSTAG_VERSIONID, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, FIELD_CUSTOM, 1, 0, "VersionID", NULL}, + {GPSTAG_LATITUDEREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "LatitudeRef", NULL}, + {GPSTAG_LATITUDE, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_DOUBLE, FIELD_CUSTOM, 1, 0, "Latitude", NULL}, + {GPSTAG_LONGITUDEREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "LongitudeRef", NULL}, + {GPSTAG_LONGITUDE, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_DOUBLE, FIELD_CUSTOM, 1, 0, "Longitude", NULL}, + {GPSTAG_ALTITUDEREF, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, FIELD_CUSTOM, 1, 0, "AltitudeRef", NULL}, + {GPSTAG_ALTITUDE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "Altitude", NULL}, + {GPSTAG_TIMESTAMP, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_DOUBLE, FIELD_CUSTOM, 1, 0, "TimeStamp", NULL}, + {GPSTAG_SATELLITES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Satellites", NULL}, + {GPSTAG_STATUS, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "Status", NULL}, + {GPSTAG_MEASUREMODE, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "MeasureMode", NULL}, + {GPSTAG_DOP, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "DOP", NULL}, + {GPSTAG_SPEEDREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "SpeedRef", NULL}, + {GPSTAG_SPEED, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "Speed", NULL}, + {GPSTAG_TRACKREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "TrackRef", NULL}, + {GPSTAG_TRACK, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "Track", NULL}, + {GPSTAG_IMGDIRECTIONREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "ImgDirectionRef", NULL}, + {GPSTAG_IMGDIRECTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "ImgDirection", NULL}, + {GPSTAG_MAPDATUM, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "MapDatum", NULL}, + {GPSTAG_DESTLATITUDEREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DestLatitudeRef", NULL}, + {GPSTAG_DESTLATITUDE, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_DOUBLE, FIELD_CUSTOM, 1, 0, "DestLatitude", NULL}, + {GPSTAG_DESTLONGITUDEREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DestLongitudeRef", NULL}, + {GPSTAG_DESTLONGITUDE, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_DOUBLE, FIELD_CUSTOM, 1, 0, "DestLongitude", NULL}, + {GPSTAG_DESTBEARINGREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DestBearingRef", NULL}, + {GPSTAG_DESTBEARING, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "DestBearing", NULL}, + {GPSTAG_DESTDISTANCEREF, 2, 2, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DestDistanceRef", NULL}, + {GPSTAG_DESTDISTANCE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "DestDistance", NULL}, + {GPSTAG_PROCESSINGMETHOD, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "ProcessingMethod", NULL}, + {GPSTAG_AREAINFORMATION, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, FIELD_CUSTOM, 1, 1, "AreaInformation", NULL}, + {GPSTAG_DATESTAMP, 11, 11, TIFF_ASCII, 0, TIFF_SETGET_ASCII, FIELD_CUSTOM, 1, 0, "DateStamp", NULL}, + {GPSTAG_DIFFERENTIAL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, FIELD_CUSTOM, 1, 0, "Differential", NULL}, + {GPSTAG_GPSHPOSITIONINGERROR, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, FIELD_CUSTOM, 1, 0, "HorizontalPositioningError", NULL}}; +/* clang-format on */ /* was off for better readability of tag comments */ + +static const TIFFFieldArray tiffFieldArray = { + tfiatImage, 0, TIFFArrayCount(tiffFields), (TIFFField *)tiffFields}; +static const TIFFFieldArray exifFieldArray = { + tfiatExif, 0, TIFFArrayCount(exifFields), (TIFFField *)exifFields}; +static const TIFFFieldArray gpsFieldArray = { + tfiatGps, 0, TIFFArrayCount(gpsFields), (TIFFField *)gpsFields}; + +/* + * We have our own local lfind() equivalent to avoid subtle differences + * in types passed to lfind() on different systems. + */ + +static void *td_lfind(const void *key, const void *base, size_t *nmemb, + size_t size, int (*compar)(const void *, const void *)) +{ + char *element, *end; + + end = (char *)base + *nmemb * size; + for (element = (char *)base; element < end; element += size) + if (!compar(key, element)) /* key found */ + return element; + + return NULL; +} + +const TIFFFieldArray *_TIFFGetFields(void) { return (&tiffFieldArray); } + +const TIFFFieldArray *_TIFFGetExifFields(void) { return (&exifFieldArray); } + +const TIFFFieldArray *_TIFFGetGpsFields(void) { return (&gpsFieldArray); } + +void _TIFFSetupFields(TIFF *tif, const TIFFFieldArray *fieldarray) +{ + if (tif->tif_fields && tif->tif_nfields > 0) + { + uint32_t i; + + for (i = 0; i < tif->tif_nfields; i++) + { + TIFFField *fld = tif->tif_fields[i]; + if (fld->field_name != NULL) + { + if (fld->field_bit == FIELD_CUSTOM && TIFFFieldIsAnonymous(fld)) + { + _TIFFfreeExt(tif, fld->field_name); + /* caution: tif_fields[i] must not be the beginning of a + * fields-array. Otherwise the following tags are also freed + * with the first free(). + */ + _TIFFfreeExt(tif, fld); + } + } + } + + _TIFFfreeExt(tif, tif->tif_fields); + tif->tif_fields = NULL; + tif->tif_nfields = 0; + } + if (!_TIFFMergeFields(tif, fieldarray->fields, fieldarray->count)) + { + TIFFErrorExtR(tif, "_TIFFSetupFields", "Setting up field info failed"); + } +} + +static int tagCompare(const void *a, const void *b) +{ + const TIFFField *ta = *(const TIFFField **)a; + const TIFFField *tb = *(const TIFFField **)b; + /* NB: be careful of return values for 16-bit platforms */ + if (ta->field_tag != tb->field_tag) + return (int)ta->field_tag - (int)tb->field_tag; + else + return (ta->field_type == TIFF_ANY) + ? 0 + : ((int)tb->field_type - (int)ta->field_type); +} + +static int tagNameCompare(const void *a, const void *b) +{ + const TIFFField *ta = *(const TIFFField **)a; + const TIFFField *tb = *(const TIFFField **)b; + int ret = strcmp(ta->field_name, tb->field_name); + + if (ret) + return ret; + else + return (ta->field_type == TIFF_ANY) + ? 0 + : ((int)tb->field_type - (int)ta->field_type); +} + +int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n) +{ + static const char module[] = "_TIFFMergeFields"; + static const char reason[] = "for fields array"; + /* TIFFField** tp; */ + uint32_t i; + + tif->tif_foundfield = NULL; + + if (tif->tif_fields && tif->tif_nfields > 0) + { + tif->tif_fields = (TIFFField **)_TIFFCheckRealloc( + tif, tif->tif_fields, (tif->tif_nfields + n), sizeof(TIFFField *), + reason); + } + else + { + tif->tif_fields = + (TIFFField **)_TIFFCheckMalloc(tif, n, sizeof(TIFFField *), reason); + } + if (!tif->tif_fields) + { + TIFFErrorExtR(tif, module, "Failed to allocate fields array"); + return 0; + } + + /* tp = tif->tif_fields + tif->tif_nfields; */ + for (i = 0; i < n; i++) + { + const TIFFField *fip = TIFFFindField(tif, info[i].field_tag, TIFF_ANY); + + /* only add definitions that aren't already present */ + if (!fip) + { + tif->tif_fields[tif->tif_nfields] = (TIFFField *)(info + i); + tif->tif_nfields++; + } + } + + /* Sort the field info by tag number */ + qsort(tif->tif_fields, tif->tif_nfields, sizeof(TIFFField *), tagCompare); + + return n; +} + +void _TIFFPrintFieldInfo(TIFF *tif, FILE *fd) +{ + uint32_t i; + + fprintf(fd, "%s: \n", tif->tif_name); + for (i = 0; i < tif->tif_nfields; i++) + { + const TIFFField *fip = tif->tif_fields[i]; + fprintf(fd, "field[%2d] %5lu, %2d, %2d, %d, %2d, %5s, %5s, %s\n", + (int)i, (unsigned long)fip->field_tag, fip->field_readcount, + fip->field_writecount, fip->field_type, fip->field_bit, + fip->field_oktochange ? "TRUE" : "FALSE", + fip->field_passcount ? "TRUE" : "FALSE", fip->field_name); + } +} + +/* + * Return size of TIFFDataType within TIFF-file in bytes + */ +int TIFFDataWidth(TIFFDataType type) +{ + switch (type) + { + case 0: /* nothing */ + case TIFF_BYTE: + case TIFF_ASCII: + case TIFF_SBYTE: + case TIFF_UNDEFINED: + return 1; + case TIFF_SHORT: + case TIFF_SSHORT: + return 2; + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_FLOAT: + case TIFF_IFD: + return 4; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_DOUBLE: + case TIFF_LONG8: + case TIFF_SLONG8: + case TIFF_IFD8: + return 8; + default: + return 0; /* will return 0 for unknown types */ + } +} + +/* + * Return internal storage size of TIFFSetGetFieldType in bytes. + * TIFFSetField() and TIFFGetField() have to provide the parameter accordingly. + * Replaces internal functions _TIFFDataSize() and _TIFFSetGetFieldSize() + * with now extern available function TIFFFieldSetGetSize(). + */ +int TIFFFieldSetGetSize(const TIFFField *fip) +{ + /* + * TIFFSetField() and TIFFGetField() must provide the parameter accordingly + * to the definition of "set_get_field_type" of the tag definition in + * dir_info.c. This function returns the data size for that purpose. + * + * Furthermore, this data size is also used for the internal storage, + * even for TIFF_RATIONAL values for FIELD_CUSTOM, which are stored + * internally as 4-byte float, but some of them should be stored internally + * as 8-byte double, depending on the "set_get_field_type" _FLOAT_ or + * _DOUBLE_. + */ + if (fip == NULL) + return 0; + + switch (fip->set_get_field_type) + { + case TIFF_SETGET_UNDEFINED: + case TIFF_SETGET_ASCII: + case TIFF_SETGET_C0_ASCII: + case TIFF_SETGET_C16_ASCII: + case TIFF_SETGET_C32_ASCII: + case TIFF_SETGET_OTHER: + return 1; + case TIFF_SETGET_UINT8: + case TIFF_SETGET_SINT8: + case TIFF_SETGET_C0_UINT8: + case TIFF_SETGET_C0_SINT8: + case TIFF_SETGET_C16_UINT8: + case TIFF_SETGET_C16_SINT8: + case TIFF_SETGET_C32_UINT8: + case TIFF_SETGET_C32_SINT8: + return 1; + case TIFF_SETGET_UINT16: + case TIFF_SETGET_SINT16: + case TIFF_SETGET_C0_UINT16: + case TIFF_SETGET_C0_SINT16: + case TIFF_SETGET_C16_UINT16: + case TIFF_SETGET_C16_SINT16: + case TIFF_SETGET_C32_UINT16: + case TIFF_SETGET_C32_SINT16: + return 2; + case TIFF_SETGET_INT: + case TIFF_SETGET_UINT32: + case TIFF_SETGET_SINT32: + case TIFF_SETGET_FLOAT: + case TIFF_SETGET_UINT16_PAIR: + case TIFF_SETGET_C0_UINT32: + case TIFF_SETGET_C0_SINT32: + case TIFF_SETGET_C0_FLOAT: + case TIFF_SETGET_C16_UINT32: + case TIFF_SETGET_C16_SINT32: + case TIFF_SETGET_C16_FLOAT: + case TIFF_SETGET_C32_UINT32: + case TIFF_SETGET_C32_SINT32: + case TIFF_SETGET_C32_FLOAT: + return 4; + case TIFF_SETGET_UINT64: + case TIFF_SETGET_SINT64: + case TIFF_SETGET_DOUBLE: + case TIFF_SETGET_IFD8: + case TIFF_SETGET_C0_UINT64: + case TIFF_SETGET_C0_SINT64: + case TIFF_SETGET_C0_DOUBLE: + case TIFF_SETGET_C0_IFD8: + case TIFF_SETGET_C16_UINT64: + case TIFF_SETGET_C16_SINT64: + case TIFF_SETGET_C16_DOUBLE: + case TIFF_SETGET_C16_IFD8: + case TIFF_SETGET_C32_UINT64: + case TIFF_SETGET_C32_SINT64: + case TIFF_SETGET_C32_DOUBLE: + case TIFF_SETGET_C32_IFD8: + return 8; + default: + return 0; + } +} /*-- TIFFFieldSetGetSize() --- */ + +/* + * Return size of count parameter of TIFFSetField() and TIFFGetField() + * and also if it is required: 0=none, 2=uint16_t, 4=uint32_t + */ +int TIFFFieldSetGetCountSize(const TIFFField *fip) +{ + if (fip == NULL) + return 0; + + switch (fip->set_get_field_type) + { + case TIFF_SETGET_C16_ASCII: + case TIFF_SETGET_C16_UINT8: + case TIFF_SETGET_C16_SINT8: + case TIFF_SETGET_C16_UINT16: + case TIFF_SETGET_C16_SINT16: + case TIFF_SETGET_C16_UINT32: + case TIFF_SETGET_C16_SINT32: + case TIFF_SETGET_C16_FLOAT: + case TIFF_SETGET_C16_UINT64: + case TIFF_SETGET_C16_SINT64: + case TIFF_SETGET_C16_DOUBLE: + case TIFF_SETGET_C16_IFD8: + return 2; + case TIFF_SETGET_C32_ASCII: + case TIFF_SETGET_C32_UINT8: + case TIFF_SETGET_C32_SINT8: + case TIFF_SETGET_C32_UINT16: + case TIFF_SETGET_C32_SINT16: + case TIFF_SETGET_C32_UINT32: + case TIFF_SETGET_C32_SINT32: + case TIFF_SETGET_C32_FLOAT: + case TIFF_SETGET_C32_UINT64: + case TIFF_SETGET_C32_SINT64: + case TIFF_SETGET_C32_DOUBLE: + case TIFF_SETGET_C32_IFD8: + return 4; + default: + return 0; + } +} /*-- TIFFFieldSetGetCountSize() --- */ + +const TIFFField *TIFFFindField(TIFF *tif, uint32_t tag, TIFFDataType dt) +{ + TIFFField key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0, 0, NULL, NULL}; + TIFFField *pkey = &key; + const TIFFField **ret; + if (tif->tif_foundfield && tif->tif_foundfield->field_tag == tag && + (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type)) + return tif->tif_foundfield; + + /* If we are invoked with no field information, then just return. */ + if (!tif->tif_fields) + return NULL; + + /* NB: use sorted search (e.g. binary search) */ + + key.field_tag = tag; + key.field_type = dt; + + ret = (const TIFFField **)bsearch(&pkey, tif->tif_fields, tif->tif_nfields, + sizeof(TIFFField *), tagCompare); + return tif->tif_foundfield = (ret ? *ret : NULL); +} + +static const TIFFField *_TIFFFindFieldByName(TIFF *tif, const char *field_name, + TIFFDataType dt) +{ + TIFFField key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0, 0, NULL, NULL}; + TIFFField *pkey = &key; + const TIFFField **ret; + if (tif->tif_foundfield && + streq(tif->tif_foundfield->field_name, field_name) && + (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type)) + return (tif->tif_foundfield); + + /* If we are invoked with no field information, then just return. */ + if (!tif->tif_fields) + return NULL; + + /* NB: use linear search since list is sorted by key#, not name */ + + key.field_name = (char *)field_name; + key.field_type = dt; + + ret = + (const TIFFField **)td_lfind(&pkey, tif->tif_fields, &tif->tif_nfields, + sizeof(TIFFField *), tagNameCompare); + + return tif->tif_foundfield = (ret ? *ret : NULL); +} + +const TIFFField *TIFFFieldWithTag(TIFF *tif, uint32_t tag) +{ + const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); + if (!fip) + { + TIFFWarningExtR(tif, "TIFFFieldWithTag", "Warning, unknown tag 0x%x", + (unsigned int)tag); + } + return (fip); +} + +const TIFFField *TIFFFieldWithName(TIFF *tif, const char *field_name) +{ + const TIFFField *fip = _TIFFFindFieldByName(tif, field_name, TIFF_ANY); + if (!fip) + { + TIFFWarningExtR(tif, "TIFFFieldWithName", "Warning, unknown tag %s", + field_name); + } + return (fip); +} + +uint32_t TIFFFieldTag(const TIFFField *fip) { return fip->field_tag; } + +const char *TIFFFieldName(const TIFFField *fip) { return fip->field_name; } + +TIFFDataType TIFFFieldDataType(const TIFFField *fip) { return fip->field_type; } + +int TIFFFieldPassCount(const TIFFField *fip) { return fip->field_passcount; } + +int TIFFFieldReadCount(const TIFFField *fip) { return fip->field_readcount; } + +int TIFFFieldWriteCount(const TIFFField *fip) { return fip->field_writecount; } + +int TIFFFieldIsAnonymous(const TIFFField *fip) { return fip->field_anonymous; } + +const TIFFField *_TIFFFindOrRegisterField(TIFF *tif, uint32_t tag, + TIFFDataType dt) + +{ + const TIFFField *fld; + + fld = TIFFFindField(tif, tag, dt); + if (fld == NULL) + { + fld = _TIFFCreateAnonField(tif, tag, dt); + if (fld == NULL || !_TIFFMergeFields(tif, fld, 1)) + return NULL; + } + + return fld; +} + +TIFFField *_TIFFCreateAnonField(TIFF *tif, uint32_t tag, + TIFFDataType field_type) +{ + TIFFField *fld; + (void)tif; + + fld = (TIFFField *)_TIFFmallocExt(tif, sizeof(TIFFField)); + if (fld == NULL) + return NULL; + _TIFFmemset(fld, 0, sizeof(TIFFField)); + + fld->field_tag = tag; + fld->field_readcount = TIFF_VARIABLE2; + fld->field_writecount = TIFF_VARIABLE2; + fld->field_type = field_type; + fld->field_anonymous = + 1; /* indicate that this is an anonymous / unknown tag */ + switch (field_type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + fld->set_get_field_type = TIFF_SETGET_C32_UINT8; + break; + case TIFF_ASCII: + fld->set_get_field_type = TIFF_SETGET_C32_ASCII; + break; + case TIFF_SHORT: + fld->set_get_field_type = TIFF_SETGET_C32_UINT16; + break; + case TIFF_LONG: + fld->set_get_field_type = TIFF_SETGET_C32_UINT32; + break; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + fld->set_get_field_type = TIFF_SETGET_C32_FLOAT; + break; + case TIFF_SBYTE: + fld->set_get_field_type = TIFF_SETGET_C32_SINT8; + break; + case TIFF_SSHORT: + fld->set_get_field_type = TIFF_SETGET_C32_SINT16; + break; + case TIFF_SLONG: + fld->set_get_field_type = TIFF_SETGET_C32_SINT32; + break; + case TIFF_DOUBLE: + fld->set_get_field_type = TIFF_SETGET_C32_DOUBLE; + break; + case TIFF_IFD: + case TIFF_IFD8: + fld->set_get_field_type = TIFF_SETGET_C32_IFD8; + break; + case TIFF_LONG8: + fld->set_get_field_type = TIFF_SETGET_C32_UINT64; + break; + case TIFF_SLONG8: + fld->set_get_field_type = TIFF_SETGET_C32_SINT64; + break; + default: + fld->set_get_field_type = TIFF_SETGET_UNDEFINED; + break; + } + fld->field_bit = FIELD_CUSTOM; + fld->field_oktochange = TRUE; + fld->field_passcount = TRUE; + fld->field_name = (char *)_TIFFmallocExt(tif, 32); + if (fld->field_name == NULL) + { + _TIFFfreeExt(tif, fld); + return NULL; + } + fld->field_subfields = NULL; + + /* + * note that this name is a special sign to TIFFClose() and + * _TIFFSetupFields() to free the field + * Update: + * This special sign is replaced by fld->field_anonymous flag. + */ + (void)snprintf(fld->field_name, 32, "Tag %d", (int)tag); + + return fld; +} + +/**************************************************************************** + * O B S O L E T E D I N T E R F A C E S + * + * Don't use this stuff in your applications, it may be removed in the future + * libtiff versions. + ****************************************************************************/ + +static TIFFSetGetFieldType _TIFFSetGetType(TIFFDataType type, short count, + unsigned char passcount) +{ + if (type == TIFF_ASCII && count == TIFF_VARIABLE && passcount == 0) + return TIFF_SETGET_ASCII; + + else if (count == 1 && passcount == 0) + { + switch (type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + return TIFF_SETGET_UINT8; + case TIFF_ASCII: + return TIFF_SETGET_ASCII; + case TIFF_SHORT: + return TIFF_SETGET_UINT16; + case TIFF_LONG: + return TIFF_SETGET_UINT32; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + return TIFF_SETGET_FLOAT; + case TIFF_SBYTE: + return TIFF_SETGET_SINT8; + case TIFF_SSHORT: + return TIFF_SETGET_SINT16; + case TIFF_SLONG: + return TIFF_SETGET_SINT32; + case TIFF_DOUBLE: + return TIFF_SETGET_DOUBLE; + case TIFF_IFD: + case TIFF_IFD8: + return TIFF_SETGET_IFD8; + case TIFF_LONG8: + return TIFF_SETGET_UINT64; + case TIFF_SLONG8: + return TIFF_SETGET_SINT64; + default: + return TIFF_SETGET_UNDEFINED; + } + } + + else if (count >= 1 && passcount == 0) + { + switch (type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + return TIFF_SETGET_C0_UINT8; + case TIFF_ASCII: + return TIFF_SETGET_C0_ASCII; + case TIFF_SHORT: + return TIFF_SETGET_C0_UINT16; + case TIFF_LONG: + return TIFF_SETGET_C0_UINT32; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + return TIFF_SETGET_C0_FLOAT; + case TIFF_SBYTE: + return TIFF_SETGET_C0_SINT8; + case TIFF_SSHORT: + return TIFF_SETGET_C0_SINT16; + case TIFF_SLONG: + return TIFF_SETGET_C0_SINT32; + case TIFF_DOUBLE: + return TIFF_SETGET_C0_DOUBLE; + case TIFF_IFD: + case TIFF_IFD8: + return TIFF_SETGET_C0_IFD8; + case TIFF_LONG8: + return TIFF_SETGET_C0_UINT64; + case TIFF_SLONG8: + return TIFF_SETGET_C0_SINT64; + default: + return TIFF_SETGET_UNDEFINED; + } + } + + else if (count == TIFF_VARIABLE && passcount == 1) + { + switch (type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + return TIFF_SETGET_C16_UINT8; + case TIFF_ASCII: + return TIFF_SETGET_C16_ASCII; + case TIFF_SHORT: + return TIFF_SETGET_C16_UINT16; + case TIFF_LONG: + return TIFF_SETGET_C16_UINT32; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + return TIFF_SETGET_C16_FLOAT; + case TIFF_SBYTE: + return TIFF_SETGET_C16_SINT8; + case TIFF_SSHORT: + return TIFF_SETGET_C16_SINT16; + case TIFF_SLONG: + return TIFF_SETGET_C16_SINT32; + case TIFF_DOUBLE: + return TIFF_SETGET_C16_DOUBLE; + case TIFF_IFD: + case TIFF_IFD8: + return TIFF_SETGET_C16_IFD8; + case TIFF_LONG8: + return TIFF_SETGET_C16_UINT64; + case TIFF_SLONG8: + return TIFF_SETGET_C16_SINT64; + default: + return TIFF_SETGET_UNDEFINED; + } + } + + else if (count == TIFF_VARIABLE2 && passcount == 1) + { + switch (type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: + return TIFF_SETGET_C32_UINT8; + case TIFF_ASCII: + return TIFF_SETGET_C32_ASCII; + case TIFF_SHORT: + return TIFF_SETGET_C32_UINT16; + case TIFF_LONG: + return TIFF_SETGET_C32_UINT32; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + return TIFF_SETGET_C32_FLOAT; + case TIFF_SBYTE: + return TIFF_SETGET_C32_SINT8; + case TIFF_SSHORT: + return TIFF_SETGET_C32_SINT16; + case TIFF_SLONG: + return TIFF_SETGET_C32_SINT32; + case TIFF_DOUBLE: + return TIFF_SETGET_C32_DOUBLE; + case TIFF_IFD: + case TIFF_IFD8: + return TIFF_SETGET_C32_IFD8; + case TIFF_LONG8: + return TIFF_SETGET_C32_UINT64; + case TIFF_SLONG8: + return TIFF_SETGET_C32_SINT64; + default: + return TIFF_SETGET_UNDEFINED; + } + } + + return TIFF_SETGET_UNDEFINED; +} + +int TIFFMergeFieldInfo(TIFF *tif, const TIFFFieldInfo info[], uint32_t n) +{ + static const char module[] = "TIFFMergeFieldInfo"; + static const char reason[] = "for fields array"; + TIFFField *tp; + size_t nfields; + uint32_t i; + + if (tif->tif_nfieldscompat > 0) + { + tif->tif_fieldscompat = (TIFFFieldArray *)_TIFFCheckRealloc( + tif, tif->tif_fieldscompat, tif->tif_nfieldscompat + 1, + sizeof(TIFFFieldArray), reason); + } + else + { + tif->tif_fieldscompat = (TIFFFieldArray *)_TIFFCheckMalloc( + tif, 1, sizeof(TIFFFieldArray), reason); + } + if (!tif->tif_fieldscompat) + { + TIFFErrorExtR(tif, module, "Failed to allocate fields array"); + return -1; + } + nfields = tif->tif_nfieldscompat++; + + tif->tif_fieldscompat[nfields].type = tfiatOther; + tif->tif_fieldscompat[nfields].allocated_size = n; + tif->tif_fieldscompat[nfields].count = n; + tif->tif_fieldscompat[nfields].fields = + (TIFFField *)_TIFFCheckMalloc(tif, n, sizeof(TIFFField), reason); + if (!tif->tif_fieldscompat[nfields].fields) + { + TIFFErrorExtR(tif, module, "Failed to allocate fields array"); + return -1; + } + + tp = tif->tif_fieldscompat[nfields].fields; + for (i = 0; i < n; i++) + { + tp->field_tag = info[i].field_tag; + if (info[i].field_readcount < TIFF_VARIABLE2 || + info[i].field_writecount < TIFF_VARIABLE2) + { + /* The fields (field_readcount) and (field_writecount) may use the + * values TIFF_VARIABLE (-1), TIFF_SPP (-2), TIFF_VARIABLE2 (-3). */ + TIFFErrorExtR( + tif, module, + "The value of field_readcount %d and field_writecount %d " + "must be greater than or equal to -3.", + info[i].field_readcount, info[i].field_writecount); + return -1; + } + if ((info[i].field_readcount == 0 || info[i].field_writecount == 0) && + info[i].field_bit != FIELD_IGNORE) + { + /* The fields (field_readcount) and (field_writecount) may only + be zero for pseudo_tags or ignored tags. */ + TIFFErrorExtR( + tif, module, + "The value of field_readcount %d and field_writecount %d " + "may only be zero for field_bit = 0 (i.e. ignored tags).", + info[i].field_readcount, info[i].field_writecount); + return -1; + } + tp->field_readcount = info[i].field_readcount; + tp->field_writecount = info[i].field_writecount; + tp->field_type = info[i].field_type; + tp->field_anonymous = 0; + tp->set_get_field_type = + _TIFFSetGetType(info[i].field_type, info[i].field_writecount, + info[i].field_passcount); + tp->field_bit = info[i].field_bit; + tp->field_oktochange = info[i].field_oktochange; + tp->field_passcount = info[i].field_passcount; + /* Define an empty static string to be passed as field_name where a NULL + * pointer is passed in. Otherwise, this will lead to buffer overflow + * furtheron. */ + if (info[i].field_name == NULL) + { + static const char *string_static_empty = ""; + tp->field_name = (char *)string_static_empty; + } + else + { + tp->field_name = info[i].field_name; + } + tp->field_subfields = NULL; + tp++; + } + + if (!_TIFFMergeFields(tif, tif->tif_fieldscompat[nfields].fields, n)) + { + TIFFErrorExtR(tif, module, "Setting up field info failed"); + return -1; + } + + return 0; +} + +int _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag) +{ + /* Filter out non-codec specific tags */ + switch (tag) + { + /* Shared tags */ + case TIFFTAG_PREDICTOR: + /* JPEG tags */ + case TIFFTAG_JPEGTABLES: + /* OJPEG tags */ + case TIFFTAG_JPEGIFOFFSET: + case TIFFTAG_JPEGIFBYTECOUNT: + case TIFFTAG_JPEGQTABLES: + case TIFFTAG_JPEGDCTABLES: + case TIFFTAG_JPEGACTABLES: + case TIFFTAG_JPEGPROC: + case TIFFTAG_JPEGRESTARTINTERVAL: + /* CCITT* */ + case TIFFTAG_BADFAXLINES: + case TIFFTAG_CLEANFAXDATA: + case TIFFTAG_CONSECUTIVEBADFAXLINES: + case TIFFTAG_GROUP3OPTIONS: + case TIFFTAG_GROUP4OPTIONS: + /* LERC */ + case TIFFTAG_LERC_PARAMETERS: + break; + default: + return 1; + } + if (!TIFFIsCODECConfigured(tif->tif_dir.td_compression)) + { + return 0; + } + /* Check if codec specific tags are allowed for the current + * compression scheme (codec) */ + switch (tif->tif_dir.td_compression) + { + case COMPRESSION_LZW: + if (tag == TIFFTAG_PREDICTOR) + return 1; + break; + case COMPRESSION_PACKBITS: + /* No codec-specific tags */ + break; + case COMPRESSION_THUNDERSCAN: + /* No codec-specific tags */ + break; + case COMPRESSION_NEXT: + /* No codec-specific tags */ + break; + case COMPRESSION_JPEG: + if (tag == TIFFTAG_JPEGTABLES) + return 1; + break; + case COMPRESSION_OJPEG: + switch (tag) + { + case TIFFTAG_JPEGIFOFFSET: + case TIFFTAG_JPEGIFBYTECOUNT: + case TIFFTAG_JPEGQTABLES: + case TIFFTAG_JPEGDCTABLES: + case TIFFTAG_JPEGACTABLES: + case TIFFTAG_JPEGPROC: + case TIFFTAG_JPEGRESTARTINTERVAL: + return 1; + } + break; + case COMPRESSION_CCITTRLE: + case COMPRESSION_CCITTRLEW: + case COMPRESSION_CCITTFAX3: + case COMPRESSION_CCITTFAX4: + switch (tag) + { + case TIFFTAG_BADFAXLINES: + case TIFFTAG_CLEANFAXDATA: + case TIFFTAG_CONSECUTIVEBADFAXLINES: + return 1; + case TIFFTAG_GROUP3OPTIONS: + if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3) + return 1; + break; + case TIFFTAG_GROUP4OPTIONS: + if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) + return 1; + break; + } + break; + case COMPRESSION_JBIG: + /* No codec-specific tags */ + break; + case COMPRESSION_DEFLATE: + case COMPRESSION_ADOBE_DEFLATE: + if (tag == TIFFTAG_PREDICTOR) + return 1; + break; + case COMPRESSION_PIXARLOG: + if (tag == TIFFTAG_PREDICTOR) + return 1; + break; + case COMPRESSION_SGILOG: + case COMPRESSION_SGILOG24: + /* No codec-specific tags */ + break; + case COMPRESSION_LZMA: + if (tag == TIFFTAG_PREDICTOR) + return 1; + break; + case COMPRESSION_ZSTD: + if (tag == TIFFTAG_PREDICTOR) + return 1; + break; + case COMPRESSION_LERC: + if (tag == TIFFTAG_LERC_PARAMETERS) + return 1; + break; + } + return 0; +} diff --git a/cpp/3rd_party/libtiff/tif_dirread.c b/cpp/3rd_party/libtiff/tif_dirread.c new file mode 100644 index 0000000000..7f0aeb1544 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_dirread.c @@ -0,0 +1,8447 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Directory Read Support Routines. + */ + +/* Suggested pending improvements: + * - add a field 'field_info' to the TIFFDirEntry structure, and set that with + * the pointer to the appropriate TIFFField structure early on in + * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup. + */ + +#include "tiffconf.h" +#include "tiffiop.h" +#include +#include +#include +#include + +#define FAILED_FII ((uint32_t)-1) + +#ifdef HAVE_IEEEFP +#define TIFFCvtIEEEFloatToNative(tif, n, fp) +#define TIFFCvtIEEEDoubleToNative(tif, n, dp) +#else +/* If your machine does not support IEEE floating point then you will need to + * add support to tif_machdep.c to convert between the native format and + * IEEE format. */ +extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *); +extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *); +#endif + +enum TIFFReadDirEntryErr +{ + TIFFReadDirEntryErrOk = 0, + TIFFReadDirEntryErrCount = 1, + TIFFReadDirEntryErrType = 2, + TIFFReadDirEntryErrIo = 3, + TIFFReadDirEntryErrRange = 4, + TIFFReadDirEntryErrPsdif = 5, + TIFFReadDirEntryErrSizesan = 6, + TIFFReadDirEntryErrAlloc = 7, +}; + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count, + uint32_t desttypesize, void **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry, + uint16_t *value); + +static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry, + uint8_t *value); +static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry, + int8_t *value); +static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry, + uint16_t *value); +static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry, + int16_t *value); +static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry, + uint32_t *value); +static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry, + int32_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, + uint64_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, + int64_t *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry, + double *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry, + double *value); +static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry, + float *value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value); +#if 0 +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry, + TIFFRational_t *value); +#endif +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSbyte(int8_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteShort(uint16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSshort(int16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteLong(uint32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSlong(int32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteLong8(uint64_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSlong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSbyte(int8_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSshort(int16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortLong(uint32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSlong(int32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortLong8(uint64_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSlong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortShort(uint16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortLong(uint32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortSlong(int32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSbyte(int8_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSshort(int16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSlong(int32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongLong8(uint64_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSlong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlongLong(uint32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Slong(int32_t value); +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value); + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value); + +static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset, + tmsize_t size, void *dest); +static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err, + const char *module, const char *tagname, + int recover); + +static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir, + uint16_t dircount); +static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir, + uint16_t dircount, + uint16_t tagid); +static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid, + uint32_t *fii); + +static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir, + uint16_t dircount); +static void MissingRequired(TIFF *, const char *); +static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t); +static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff, + TIFFDirEntry **pdir, uint64_t *nextdiroff); +static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover); +static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips, + uint64_t **lpp); +static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *); +static void ChopUpSingleUncompressedStrip(TIFF *); +static void TryChopUpUncompressedBigTiff(TIFF *); +static uint64_t TIFFReadUInt64(const uint8_t *value); +static int _TIFFGetMaxColorChannels(uint16_t photometric); + +static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount); + +typedef union _UInt64Aligned_t +{ + double d; + uint64_t l; + uint32_t i[2]; + uint16_t s[4]; + uint8_t c[8]; +} UInt64Aligned_t; + +/* + Unaligned safe copy of a uint64_t value from an octet array. +*/ +static uint64_t TIFFReadUInt64(const uint8_t *value) +{ + UInt64Aligned_t result; + + result.c[0] = value[0]; + result.c[1] = value[1]; + result.c[2] = value[2]; + result.c[3] = value[3]; + result.c[4] = value[4]; + result.c[5] = value[5]; + result.c[6] = value[6]; + result.c[7] = value[7]; + + return result.l; +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with + field_readcount==1 */ + TIFFReadDirEntryCheckedByte(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeByteSbyte(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeByteShort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeByteSshort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeByteLong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeByteSlong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeByteLong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeByteSlong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint8_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with + field_readcount==1 */ + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSbyteByte(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + TIFFReadDirEntryCheckedSbyte(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSbyteShort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSbyteSshort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSbyteLong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSbyteSlong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSbyteLong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSbyteSlong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int8_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntrySbyte() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeShortSbyte(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + TIFFReadDirEntryCheckedShort(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeShortSshort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeShortLong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeShortSlong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeShortLong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeShortSlong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntryShort() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (int16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + *value = (int16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSshortShort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + TIFFReadDirEntryCheckedSshort(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSshortLong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSshortSlong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSshortLong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int16_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSshortSlong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int16_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntrySshort() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeLongSbyte(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeLongSshort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + TIFFReadDirEntryCheckedLong(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeLongSlong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeLongLong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeLongSlong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint32_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntryLong() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeSlongLong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + TIFFReadDirEntryCheckedSlong(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSlongLong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSlongSlong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int32_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntrySlong() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeLong8Sbyte(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeLong8Sshort(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + err = TIFFReadDirEntryCheckRangeLong8Slong(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + err = TIFFReadDirEntryCheckedLong8(tif, direntry, value); + return (err); + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeLong8Slong8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntryLong8() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + err = TIFFReadDirEntryCheckRangeSlong8Long8(m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (int64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value); + return (err); + default: + return (TIFFReadDirEntryErrType); + } +} /*-- TIFFReadDirEntrySlong8() --*/ + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_RATIONAL: + { + double m; + err = TIFFReadDirEntryCheckedRational(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SRATIONAL: + { + double m; + err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_FLOAT: + TIFFReadDirEntryCheckedFloat(tif, direntry, value); + return (TIFFReadDirEntryErrOk); + case TIFF_DOUBLE: + { + double m; + err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + if ((m > FLT_MAX) || (m < -FLT_MAX)) + return (TIFFReadDirEntryErrRange); + *value = (float)m; + return (TIFFReadDirEntryErrOk); + } + default: + return (TIFFReadDirEntryErrType); + } +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t m; + TIFFReadDirEntryCheckedByte(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + { + int8_t m; + TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SHORT: + { + uint16_t m; + TIFFReadDirEntryCheckedShort(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + { + int16_t m; + TIFFReadDirEntryCheckedSshort(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + { + int32_t m; + TIFFReadDirEntryCheckedSlong(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + { + uint64_t m; + err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + { + int64_t m; + err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk) + return (err); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_RATIONAL: + err = TIFFReadDirEntryCheckedRational(tif, direntry, value); + return (err); + case TIFF_SRATIONAL: + err = TIFFReadDirEntryCheckedSrational(tif, direntry, value); + return (err); + case TIFF_FLOAT: + { + float m; + TIFFReadDirEntryCheckedFloat(tif, direntry, &m); + *value = (double)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_DOUBLE: + err = TIFFReadDirEntryCheckedDouble(tif, direntry, value); + return (err); + default: + return (TIFFReadDirEntryErrType); + } +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value) +{ + enum TIFFReadDirEntryErr err; + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + switch (direntry->tdir_type) + { + case TIFF_LONG: + case TIFF_IFD: + { + uint32_t m; + TIFFReadDirEntryCheckedLong(tif, direntry, &m); + *value = (uint64_t)m; + return (TIFFReadDirEntryErrOk); + } + case TIFF_LONG8: + case TIFF_IFD8: + err = TIFFReadDirEntryCheckedLong8(tif, direntry, value); + return (err); + default: + return (TIFFReadDirEntryErrType); + } +} + +#define INITIAL_THRESHOLD (1024 * 1024) +#define THRESHOLD_MULTIPLIER 10 +#define MAX_THRESHOLD \ + (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \ + INITIAL_THRESHOLD) + +static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif, + uint64_t offset, + tmsize_t size, + void **pdest) +{ +#if SIZEOF_SIZE_T == 8 + tmsize_t threshold = INITIAL_THRESHOLD; +#endif + tmsize_t already_read = 0; + + assert(!isMapped(tif)); + + if (!SeekOK(tif, offset)) + return (TIFFReadDirEntryErrIo); + + /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */ + /* so as to avoid allocating too much memory in case the file is too */ + /* short. We could ask for the file size, but this might be */ + /* expensive with some I/O layers (think of reading a gzipped file) */ + /* Restrict to 64 bit processes, so as to avoid reallocs() */ + /* on 32 bit processes where virtual memory is scarce. */ + while (already_read < size) + { + void *new_dest; + tmsize_t bytes_read; + tmsize_t to_read = size - already_read; +#if SIZEOF_SIZE_T == 8 + if (to_read >= threshold && threshold < MAX_THRESHOLD) + { + to_read = threshold; + threshold *= THRESHOLD_MULTIPLIER; + } +#endif + + new_dest = + (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read); + if (new_dest == NULL) + { + TIFFErrorExtR(tif, tif->tif_name, + "Failed to allocate memory for %s " + "(%" TIFF_SSIZE_FORMAT + " elements of %" TIFF_SSIZE_FORMAT " bytes each)", + "TIFFReadDirEntryArray", (tmsize_t)1, + already_read + to_read); + return TIFFReadDirEntryErrAlloc; + } + *pdest = new_dest; + + bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read); + already_read += bytes_read; + if (bytes_read != to_read) + { + return TIFFReadDirEntryErrIo; + } + } + return TIFFReadDirEntryErrOk; +} + +/* Caution: if raising that value, make sure int32 / uint32 overflows can't + * occur elsewhere */ +#define MAX_SIZE_TAG_DATA 2147483647U + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry, + uint32_t *count, uint32_t desttypesize, + void **value, uint64_t maxcount) +{ + int typesize; + uint32_t datasize; + void *data; + uint64_t target_count64; + int original_datasize_clamped; + typesize = TIFFDataWidth(direntry->tdir_type); + + target_count64 = + (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count; + + if ((target_count64 == 0) || (typesize == 0)) + { + *value = 0; + return (TIFFReadDirEntryErrOk); + } + (void)desttypesize; + + /* We just want to know if the original tag size is more than 4 bytes + * (classic TIFF) or 8 bytes (BigTIFF) + */ + original_datasize_clamped = + ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) * + typesize; + + /* + * As a sanity check, make sure we have no more than a 2GB tag array + * in either the current data type or the dest data type. This also + * avoids problems with overflow of tmsize_t on 32bit systems. + */ + if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64) + return (TIFFReadDirEntryErrSizesan); + if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64) + return (TIFFReadDirEntryErrSizesan); + + *count = (uint32_t)target_count64; + datasize = (*count) * typesize; + assert((tmsize_t)datasize > 0); + + if (datasize > 100 * 1024 * 1024) + { + /* Before allocating a huge amount of memory for corrupted files, check + * if size of requested memory is not greater than file size. + */ + const uint64_t filesize = TIFFGetFileSize(tif); + if (datasize > filesize) + { + TIFFWarningExtR(tif, "ReadDirEntryArray", + "Requested memory size for tag %d (0x%x) %" PRIu32 + " is greater than filesize %" PRIu64 + ". Memory not allocated, tag not read", + direntry->tdir_tag, direntry->tdir_tag, datasize, + filesize); + return (TIFFReadDirEntryErrAlloc); + } + } + + if (isMapped(tif) && datasize > (uint64_t)tif->tif_size) + return TIFFReadDirEntryErrIo; + + if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) || + (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4))) + { + data = NULL; + } + else + { + data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray"); + if (data == 0) + return (TIFFReadDirEntryErrAlloc); + } + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + /* Only the condition on original_datasize_clamped. The second + * one is implied, but Coverity Scan cannot see it. */ + if (original_datasize_clamped <= 4 && datasize <= 4) + _TIFFmemcpy(data, &direntry->tdir_offset, datasize); + else + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + if (isMapped(tif)) + err = TIFFReadDirEntryData(tif, (uint64_t)offset, + (tmsize_t)datasize, data); + else + err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, + (tmsize_t)datasize, &data); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + } + } + else + { + /* See above comment for the Classic TIFF case */ + if (original_datasize_clamped <= 8 && datasize <= 8) + _TIFFmemcpy(data, &direntry->tdir_offset, datasize); + else + { + enum TIFFReadDirEntryErr err; + uint64_t offset = direntry->tdir_offset.toff_long8; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&offset); + if (isMapped(tif)) + err = TIFFReadDirEntryData(tif, (uint64_t)offset, + (tmsize_t)datasize, data); + else + err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, + (tmsize_t)datasize, &data); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + } + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count, + uint32_t desttypesize, void **value) +{ + return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize, + value, ~((uint64_t)0)); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + uint8_t *data; + switch (direntry->tdir_type) + { + case TIFF_ASCII: + case TIFF_UNDEFINED: + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_ASCII: + case TIFF_UNDEFINED: + case TIFF_BYTE: + *value = (uint8_t *)origdata; + return (TIFFReadDirEntryErrOk); + case TIFF_SBYTE: + { + int8_t *m; + uint32_t n; + m = (int8_t *)origdata; + for (n = 0; n < count; n++) + { + err = TIFFReadDirEntryCheckRangeByteSbyte(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (uint8_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + } + data = (uint8_t *)_TIFFmallocExt(tif, count); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_SHORT: + { + uint16_t *ma; + uint8_t *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + err = TIFFReadDirEntryCheckRangeByteShort(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint8_t)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + uint8_t *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + err = TIFFReadDirEntryCheckRangeByteSshort(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint8_t)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + uint8_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + err = TIFFReadDirEntryCheckRangeByteLong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint8_t)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + uint8_t *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + err = TIFFReadDirEntryCheckRangeByteSlong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint8_t)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + uint8_t *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + err = TIFFReadDirEntryCheckRangeByteLong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint8_t)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + uint8_t *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + err = TIFFReadDirEntryCheckRangeByteSlong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint8_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + int8_t *data; + switch (direntry->tdir_type) + { + case TIFF_UNDEFINED: + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_UNDEFINED: + case TIFF_BYTE: + { + uint8_t *m; + uint32_t n; + m = (uint8_t *)origdata; + for (n = 0; n < count; n++) + { + err = TIFFReadDirEntryCheckRangeSbyteByte(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (int8_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SBYTE: + *value = (int8_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + data = (int8_t *)_TIFFmallocExt(tif, count); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_SHORT: + { + uint16_t *ma; + int8_t *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + err = TIFFReadDirEntryCheckRangeSbyteShort(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int8_t)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + int8_t *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int8_t)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + int8_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + err = TIFFReadDirEntryCheckRangeSbyteLong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int8_t)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + int8_t *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int8_t)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + int8_t *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int8_t)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + int8_t *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int8_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + uint16_t *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_SHORT: + *value = (uint16_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfShort(*value, count); + return (TIFFReadDirEntryErrOk); + case TIFF_SSHORT: + { + int16_t *m; + uint32_t n; + m = (int16_t *)origdata; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)m); + err = TIFFReadDirEntryCheckRangeShortSshort(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (uint16_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + } + data = (uint16_t *)_TIFFmallocExt(tif, count * 2); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + uint16_t *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (uint16_t)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + uint16_t *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + err = TIFFReadDirEntryCheckRangeShortSbyte(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint16_t)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + uint16_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + err = TIFFReadDirEntryCheckRangeShortLong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint16_t)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + uint16_t *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + err = TIFFReadDirEntryCheckRangeShortSlong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint16_t)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + uint16_t *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + err = TIFFReadDirEntryCheckRangeShortLong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint16_t)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + uint16_t *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + err = TIFFReadDirEntryCheckRangeShortSlong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint16_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + int16_t *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_SHORT: + { + uint16_t *m; + uint32_t n; + m = (uint16_t *)origdata; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(m); + err = TIFFReadDirEntryCheckRangeSshortShort(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (int16_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SSHORT: + *value = (int16_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfShort((uint16_t *)(*value), count); + return (TIFFReadDirEntryErrOk); + } + data = (int16_t *)_TIFFmallocExt(tif, count * 2); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + int16_t *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (int16_t)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + int16_t *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (int16_t)(*ma++); + } + break; + case TIFF_LONG: + { + uint32_t *ma; + int16_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + err = TIFFReadDirEntryCheckRangeSshortLong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int16_t)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + int16_t *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + err = TIFFReadDirEntryCheckRangeSshortSlong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int16_t)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + int16_t *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + err = TIFFReadDirEntryCheckRangeSshortLong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int16_t)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + int16_t *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int16_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + uint32_t *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_LONG: + *value = (uint32_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(*value, count); + return (TIFFReadDirEntryErrOk); + case TIFF_SLONG: + { + int32_t *m; + uint32_t n; + m = (int32_t *)origdata; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)m); + err = TIFFReadDirEntryCheckRangeLongSlong(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (uint32_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + } + data = (uint32_t *)_TIFFmallocExt(tif, count * 4); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + uint32_t *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (uint32_t)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + uint32_t *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + err = TIFFReadDirEntryCheckRangeLongSbyte(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint32_t)(*ma++); + } + } + break; + case TIFF_SHORT: + { + uint16_t *ma; + uint32_t *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + *mb++ = (uint32_t)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + uint32_t *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + err = TIFFReadDirEntryCheckRangeLongSshort(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint32_t)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + uint32_t *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + err = TIFFReadDirEntryCheckRangeLongLong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint32_t)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + uint32_t *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + err = TIFFReadDirEntryCheckRangeLongSlong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint32_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + int32_t *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_LONG: + { + uint32_t *m; + uint32_t n; + m = (uint32_t *)origdata; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)m); + err = TIFFReadDirEntryCheckRangeSlongLong(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (int32_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG: + *value = (int32_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)(*value), count); + return (TIFFReadDirEntryErrOk); + } + data = (int32_t *)_TIFFmallocExt(tif, count * 4); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + int32_t *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (int32_t)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + int32_t *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (int32_t)(*ma++); + } + break; + case TIFF_SHORT: + { + uint16_t *ma; + int32_t *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + *mb++ = (int32_t)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + int32_t *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + *mb++ = (int32_t)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + int32_t *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + err = TIFFReadDirEntryCheckRangeSlongLong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int32_t)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + int32_t *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (int32_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry, + uint64_t **value, uint64_t maxcount) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + uint64_t *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata, + maxcount); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_LONG8: + *value = (uint64_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8(*value, count); + return (TIFFReadDirEntryErrOk); + case TIFF_SLONG8: + { + int64_t *m; + uint32_t n; + m = (int64_t *)origdata; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)m); + err = TIFFReadDirEntryCheckRangeLong8Slong8(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (uint64_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + } + data = (uint64_t *)_TIFFmallocExt(tif, count * 8); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + uint64_t *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (uint64_t)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + uint64_t *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint64_t)(*ma++); + } + } + break; + case TIFF_SHORT: + { + uint16_t *ma; + uint64_t *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + *mb++ = (uint64_t)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + uint64_t *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint64_t)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + uint64_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + *mb++ = (uint64_t)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + uint64_t *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + err = TIFFReadDirEntryCheckRangeLong8Slong(*ma); + if (err != TIFFReadDirEntryErrOk) + break; + *mb++ = (uint64_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, data); + return (err); + } + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value) +{ + return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value, + ~((uint64_t)0)); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + int64_t *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_LONG8: + { + uint64_t *m; + uint32_t n; + m = (uint64_t *)origdata; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(m); + err = TIFFReadDirEntryCheckRangeSlong8Long8(*m); + if (err != TIFFReadDirEntryErrOk) + { + _TIFFfreeExt(tif, origdata); + return (err); + } + m++; + } + *value = (int64_t *)origdata; + return (TIFFReadDirEntryErrOk); + } + case TIFF_SLONG8: + *value = (int64_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8((uint64_t *)(*value), count); + return (TIFFReadDirEntryErrOk); + } + data = (int64_t *)_TIFFmallocExt(tif, count * 8); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + int64_t *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (int64_t)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + int64_t *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (int64_t)(*ma++); + } + break; + case TIFF_SHORT: + { + uint16_t *ma; + int64_t *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + *mb++ = (int64_t)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + int64_t *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + *mb++ = (int64_t)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + int64_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + *mb++ = (int64_t)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + int64_t *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + *mb++ = (int64_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + float *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + case TIFF_DOUBLE: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_FLOAT: + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)origdata, count); + TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata); + *value = (float *)origdata; + return (TIFFReadDirEntryErrOk); + } + data = (float *)_TIFFmallocExt(tif, count * sizeof(float)); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + float *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (float)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + float *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (float)(*ma++); + } + break; + case TIFF_SHORT: + { + uint16_t *ma; + float *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + *mb++ = (float)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + float *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + *mb++ = (float)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + float *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + *mb++ = (float)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + float *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + *mb++ = (float)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + float *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + *mb++ = (float)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + float *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + *mb++ = (float)(*ma++); + } + } + break; + case TIFF_RATIONAL: + { + uint32_t *ma; + uint32_t maa; + uint32_t mab; + float *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + maa = *ma++; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + mab = *ma++; + if (mab == 0) + *mb++ = 0.0; + else + *mb++ = (float)maa / (float)mab; + } + } + break; + case TIFF_SRATIONAL: + { + uint32_t *ma; + int32_t maa; + uint32_t mab; + float *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + maa = *(int32_t *)ma; + ma++; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + mab = *ma++; + if (mab == 0) + *mb++ = 0.0; + else + *mb++ = (float)maa / (float)mab; + } + } + break; + case TIFF_DOUBLE: + { + double *ma; + float *mb; + uint32_t n; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8((uint64_t *)origdata, count); + TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata); + ma = (double *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + double val = *ma++; + if (val > FLT_MAX) + val = FLT_MAX; + else if (val < -FLT_MAX) + val = -FLT_MAX; + *mb++ = (float)val; + } + } + break; + } + _TIFFfreeExt(tif, origdata); + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + double *data; + switch (direntry->tdir_type) + { + case TIFF_BYTE: + case TIFF_SBYTE: + case TIFF_SHORT: + case TIFF_SSHORT: + case TIFF_LONG: + case TIFF_SLONG: + case TIFF_LONG8: + case TIFF_SLONG8: + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_FLOAT: + case TIFF_DOUBLE: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_DOUBLE: + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8((uint64_t *)origdata, count); + TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata); + *value = (double *)origdata; + return (TIFFReadDirEntryErrOk); + } + data = (double *)_TIFFmallocExt(tif, count * sizeof(double)); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_BYTE: + { + uint8_t *ma; + double *mb; + uint32_t n; + ma = (uint8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (double)(*ma++); + } + break; + case TIFF_SBYTE: + { + int8_t *ma; + double *mb; + uint32_t n; + ma = (int8_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (double)(*ma++); + } + break; + case TIFF_SHORT: + { + uint16_t *ma; + double *mb; + uint32_t n; + ma = (uint16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(ma); + *mb++ = (double)(*ma++); + } + } + break; + case TIFF_SSHORT: + { + int16_t *ma; + double *mb; + uint32_t n; + ma = (int16_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + *mb++ = (double)(*ma++); + } + } + break; + case TIFF_LONG: + { + uint32_t *ma; + double *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + *mb++ = (double)(*ma++); + } + } + break; + case TIFF_SLONG: + { + int32_t *ma; + double *mb; + uint32_t n; + ma = (int32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + *mb++ = (double)(*ma++); + } + } + break; + case TIFF_LONG8: + { + uint64_t *ma; + double *mb; + uint32_t n; + ma = (uint64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(ma); + *mb++ = (double)(*ma++); + } + } + break; + case TIFF_SLONG8: + { + int64_t *ma; + double *mb; + uint32_t n; + ma = (int64_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + *mb++ = (double)(*ma++); + } + } + break; + case TIFF_RATIONAL: + { + uint32_t *ma; + uint32_t maa; + uint32_t mab; + double *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + maa = *ma++; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + mab = *ma++; + if (mab == 0) + *mb++ = 0.0; + else + *mb++ = (double)maa / (double)mab; + } + } + break; + case TIFF_SRATIONAL: + { + uint32_t *ma; + int32_t maa; + uint32_t mab; + double *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + maa = *(int32_t *)ma; + ma++; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + mab = *ma++; + if (mab == 0) + *mb++ = 0.0; + else + *mb++ = (double)maa / (double)mab; + } + } + break; + case TIFF_FLOAT: + { + float *ma; + double *mb; + uint32_t n; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)origdata, count); + TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata); + ma = (float *)origdata; + mb = data; + for (n = 0; n < count; n++) + *mb++ = (double)(*ma++); + } + break; + } + _TIFFfreeExt(tif, origdata); + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value) +{ + enum TIFFReadDirEntryErr err; + uint32_t count; + void *origdata; + uint64_t *data; + switch (direntry->tdir_type) + { + case TIFF_LONG: + case TIFF_LONG8: + case TIFF_IFD: + case TIFF_IFD8: + break; + default: + return (TIFFReadDirEntryErrType); + } + err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + *value = 0; + return (err); + } + switch (direntry->tdir_type) + { + case TIFF_LONG8: + case TIFF_IFD8: + *value = (uint64_t *)origdata; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8(*value, count); + return (TIFFReadDirEntryErrOk); + } + data = (uint64_t *)_TIFFmallocExt(tif, count * 8); + if (data == 0) + { + _TIFFfreeExt(tif, origdata); + return (TIFFReadDirEntryErrAlloc); + } + switch (direntry->tdir_type) + { + case TIFF_LONG: + case TIFF_IFD: + { + uint32_t *ma; + uint64_t *mb; + uint32_t n; + ma = (uint32_t *)origdata; + mb = data; + for (n = 0; n < count; n++) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(ma); + *mb++ = (uint64_t)(*ma++); + } + } + break; + } + _TIFFfreeExt(tif, origdata); + *value = data; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry, + uint16_t *value) +{ + enum TIFFReadDirEntryErr err; + uint16_t *m; + uint16_t *na; + uint16_t nb; + if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel) + return (TIFFReadDirEntryErrCount); + err = TIFFReadDirEntryShortArray(tif, direntry, &m); + if (err != TIFFReadDirEntryErrOk || m == NULL) + return (err); + na = m; + nb = tif->tif_dir.td_samplesperpixel; + *value = *na++; + nb--; + while (nb > 0) + { + if (*na++ != *value) + { + err = TIFFReadDirEntryErrPsdif; + break; + } + nb--; + } + _TIFFfreeExt(tif, m); + return (err); +} + +static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry, + uint8_t *value) +{ + (void)tif; + *value = *(uint8_t *)(&direntry->tdir_offset); +} + +static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry, + int8_t *value) +{ + (void)tif; + *value = *(int8_t *)(&direntry->tdir_offset); +} + +static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry, + uint16_t *value) +{ + *value = direntry->tdir_offset.toff_short; + /* *value=*(uint16_t*)(&direntry->tdir_offset); */ + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(value); +} + +static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry, + int16_t *value) +{ + *value = *(int16_t *)(&direntry->tdir_offset); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)value); +} + +static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry, + uint32_t *value) +{ + *value = *(uint32_t *)(&direntry->tdir_offset); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(value); +} + +static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry, + int32_t *value) +{ + *value = *(int32_t *)(&direntry->tdir_offset); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)value); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value) +{ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, value); + if (err != TIFFReadDirEntryErrOk) + return (err); + } + else + *value = direntry->tdir_offset.toff_long8; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(value); + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value) +{ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, value); + if (err != TIFFReadDirEntryErrOk) + return (err); + } + else + *value = *(int64_t *)(&direntry->tdir_offset); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)value); + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry, + double *value) +{ + UInt64Aligned_t m; + + assert(sizeof(double) == 8); + assert(sizeof(uint64_t) == 8); + assert(sizeof(uint32_t) == 4); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, m.i); + if (err != TIFFReadDirEntryErrOk) + return (err); + } + else + m.l = direntry->tdir_offset.toff_long8; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(m.i, 2); + /* Not completely sure what we should do when m.i[1]==0, but some */ + /* sanitizers do not like division by 0.0: */ + /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */ + if (m.i[0] == 0 || m.i[1] == 0) + *value = 0.0; + else + *value = (double)m.i[0] / (double)m.i[1]; + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry, + double *value) +{ + UInt64Aligned_t m; + assert(sizeof(double) == 8); + assert(sizeof(uint64_t) == 8); + assert(sizeof(int32_t) == 4); + assert(sizeof(uint32_t) == 4); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, m.i); + if (err != TIFFReadDirEntryErrOk) + return (err); + } + else + m.l = direntry->tdir_offset.toff_long8; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(m.i, 2); + /* Not completely sure what we should do when m.i[1]==0, but some */ + /* sanitizers do not like division by 0.0: */ + /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */ + if ((int32_t)m.i[0] == 0 || m.i[1] == 0) + *value = 0.0; + else + *value = (double)((int32_t)m.i[0]) / (double)m.i[1]; + return (TIFFReadDirEntryErrOk); +} + +#if 0 +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry, + TIFFRational_t *value) +{ /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals) + directly --*/ + UInt64Aligned_t m; + + assert(sizeof(double) == 8); + assert(sizeof(uint64_t) == 8); + assert(sizeof(uint32_t) == 4); + + if (direntry->tdir_count != 1) + return (TIFFReadDirEntryErrCount); + + if (direntry->tdir_type != TIFF_RATIONAL && + direntry->tdir_type != TIFF_SRATIONAL) + return (TIFFReadDirEntryErrType); + + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, m.i); + if (err != TIFFReadDirEntryErrOk) + return (err); + } + else + { + m.l = direntry->tdir_offset.toff_long8; + } + + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(m.i, 2); + + value->uNum = m.i[0]; + value->uDenom = m.i[1]; + return (TIFFReadDirEntryErrOk); +} /*-- TIFFReadDirEntryCheckedRationalDirect() --*/ +#endif + +static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry, + float *value) +{ + union + { + float f; + uint32_t i; + } float_union; + assert(sizeof(float) == 4); + assert(sizeof(uint32_t) == 4); + assert(sizeof(float_union) == 4); + float_union.i = *(uint32_t *)(&direntry->tdir_offset); + *value = float_union.f; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)value); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value) +{ + assert(sizeof(double) == 8); + assert(sizeof(uint64_t) == 8); + assert(sizeof(UInt64Aligned_t) == 8); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + enum TIFFReadDirEntryErr err; + uint32_t offset = direntry->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, value); + if (err != TIFFReadDirEntryErrOk) + return (err); + } + else + { + UInt64Aligned_t uint64_union; + uint64_union.l = direntry->tdir_offset.toff_long8; + *value = uint64_union.d; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)value); + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSbyte(int8_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteShort(uint16_t value) +{ + if (value > 0xFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSshort(int16_t value) +{ + if ((value < 0) || (value > 0xFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteLong(uint32_t value) +{ + if (value > 0xFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSlong(int32_t value) +{ + if ((value < 0) || (value > 0xFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteLong8(uint64_t value) +{ + if (value > 0xFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeByteSlong8(int64_t value) +{ + if ((value < 0) || (value > 0xFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value) +{ + if (value > 0x7F) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value) +{ + if (value > 0x7F) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value) +{ + if ((value < -0x80) || (value > 0x7F)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value) +{ + if (value > 0x7F) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value) +{ + if ((value < -0x80) || (value > 0x7F)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value) +{ + if (value > 0x7F) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value) +{ + if ((value < -0x80) || (value > 0x7F)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSbyte(int8_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSshort(int16_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortLong(uint32_t value) +{ + if (value > 0xFFFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSlong(int32_t value) +{ + if ((value < 0) || (value > 0xFFFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortLong8(uint64_t value) +{ + if (value > 0xFFFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeShortSlong8(int64_t value) +{ + if ((value < 0) || (value > 0xFFFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortShort(uint16_t value) +{ + if (value > 0x7FFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortLong(uint32_t value) +{ + if (value > 0x7FFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortSlong(int32_t value) +{ + if ((value < -0x8000) || (value > 0x7FFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value) +{ + if (value > 0x7FFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value) +{ + if ((value < -0x8000) || (value > 0x7FFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSbyte(int8_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSshort(int16_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSlong(int32_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongLong8(uint64_t value) +{ + if (value > UINT32_MAX) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLongSlong8(int64_t value) +{ + if ((value < 0) || (value > (int64_t)UINT32_MAX)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlongLong(uint32_t value) +{ + if (value > 0x7FFFFFFFUL) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +/* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */ +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value) +{ + if (value > 0x7FFFFFFF) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +/* Check that the 8-byte signed value can fit in a 4-byte signed range */ +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value) +{ + if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF)) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Slong(int32_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value) +{ + if (value < 0) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr +TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value) +{ + if (value > INT64_MAX) + return (TIFFReadDirEntryErrRange); + else + return (TIFFReadDirEntryErrOk); +} + +static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset, + tmsize_t size, void *dest) +{ + assert(size > 0); + if (!isMapped(tif)) + { + if (!SeekOK(tif, offset)) + return (TIFFReadDirEntryErrIo); + if (!ReadOK(tif, dest, size)) + return (TIFFReadDirEntryErrIo); + } + else + { + size_t ma, mb; + ma = (size_t)offset; + if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size) + { + return TIFFReadDirEntryErrIo; + } + mb = ma + size; + if (mb > (uint64_t)tif->tif_size) + return (TIFFReadDirEntryErrIo); + _TIFFmemcpy(dest, tif->tif_base + ma, size); + } + return (TIFFReadDirEntryErrOk); +} + +static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err, + const char *module, const char *tagname, + int recover) +{ + if (!recover) + { + switch (err) + { + case TIFFReadDirEntryErrCount: + TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"", + tagname); + break; + case TIFFReadDirEntryErrType: + TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"", + tagname); + break; + case TIFFReadDirEntryErrIo: + TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"", + tagname); + break; + case TIFFReadDirEntryErrRange: + TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"", + tagname); + break; + case TIFFReadDirEntryErrPsdif: + TIFFErrorExtR( + tif, module, + "Cannot handle different values per sample for \"%s\"", + tagname); + break; + case TIFFReadDirEntryErrSizesan: + TIFFErrorExtR(tif, module, + "Sanity check on size of \"%s\" value failed", + tagname); + break; + case TIFFReadDirEntryErrAlloc: + TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"", + tagname); + break; + default: + assert(0); /* we should never get here */ + break; + } + } + else + { + switch (err) + { + case TIFFReadDirEntryErrCount: + TIFFWarningExtR(tif, module, + "Incorrect count for \"%s\"; tag ignored", + tagname); + break; + case TIFFReadDirEntryErrType: + TIFFWarningExtR(tif, module, + "Incompatible type for \"%s\"; tag ignored", + tagname); + break; + case TIFFReadDirEntryErrIo: + TIFFWarningExtR( + tif, module, + "IO error during reading of \"%s\"; tag ignored", tagname); + break; + case TIFFReadDirEntryErrRange: + TIFFWarningExtR(tif, module, + "Incorrect value for \"%s\"; tag ignored", + tagname); + break; + case TIFFReadDirEntryErrPsdif: + TIFFWarningExtR(tif, module, + "Cannot handle different values per sample for " + "\"%s\"; tag ignored", + tagname); + break; + case TIFFReadDirEntryErrSizesan: + TIFFWarningExtR( + tif, module, + "Sanity check on size of \"%s\" value failed; tag ignored", + tagname); + break; + case TIFFReadDirEntryErrAlloc: + TIFFWarningExtR(tif, module, + "Out of memory reading of \"%s\"; tag ignored", + tagname); + break; + default: + assert(0); /* we should never get here */ + break; + } + } +} + +/* + * Return the maximum number of color channels specified for a given photometric + * type. 0 is returned if photometric type isn't supported or no default value + * is defined by the specification. + */ +static int _TIFFGetMaxColorChannels(uint16_t photometric) +{ + switch (photometric) + { + case PHOTOMETRIC_PALETTE: + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + return 1; + case PHOTOMETRIC_YCBCR: + case PHOTOMETRIC_RGB: + case PHOTOMETRIC_CIELAB: + case PHOTOMETRIC_LOGLUV: + case PHOTOMETRIC_ITULAB: + case PHOTOMETRIC_ICCLAB: + return 3; + case PHOTOMETRIC_SEPARATED: + case PHOTOMETRIC_MASK: + return 4; + case PHOTOMETRIC_LOGL: + case PHOTOMETRIC_CFA: + default: + return 0; + } +} + +static int ByteCountLooksBad(TIFF *tif) +{ + /* + * Assume we have wrong StripByteCount value (in case + * of single strip) in following cases: + * - it is equal to zero along with StripOffset; + * - it is larger than file itself (in case of uncompressed + * image); + * - it is smaller than the size of the bytes per row + * multiplied on the number of rows. The last case should + * not be checked in the case of writing new image, + * because we may do not know the exact strip size + * until the whole image will be written and directory + * dumped out. + */ + uint64_t bytecount = TIFFGetStrileByteCount(tif, 0); + uint64_t offset = TIFFGetStrileOffset(tif, 0); + uint64_t filesize; + + if (offset == 0) + return 0; + if (bytecount == 0) + return 1; + if (tif->tif_dir.td_compression != COMPRESSION_NONE) + return 0; + filesize = TIFFGetFileSize(tif); + if (offset <= filesize && bytecount > filesize - offset) + return 1; + if (tif->tif_mode == O_RDONLY) + { + uint64_t scanlinesize = TIFFScanlineSize64(tif); + if (tif->tif_dir.td_imagelength > 0 && + scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength) + { + return 1; + } + if (bytecount < scanlinesize * tif->tif_dir.td_imagelength) + return 1; + } + return 0; +} + +/* + * To evaluate the IFD data size when reading, save the offset and data size of + * all data that does not fit into the IFD entries themselves. + */ +static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp) +{ + const uint64_t data_width = TIFFDataWidth(dp->tdir_type); + if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width) + { + TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading", + "Too large IFD data size"); + return false; + } + const uint64_t datalength = dp->tdir_count * data_width; + if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U)) + { + if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength) + { + TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading", + "Too large IFD data size"); + return false; + } + tif->tif_dir.td_dirdatasize_read += datalength; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + /* The offset of TIFFDirEntry are not swapped when read in. That has + * to be done when used. */ + uint32_t offset = dp->tdir_offset.toff_long; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + tif->tif_dir + .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets] + .offset = (uint64_t)offset; + } + else + { + tif->tif_dir + .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets] + .offset = dp->tdir_offset.toff_long8; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8( + &tif->tif_dir + .td_dirdatasize_offsets[tif->tif_dir + .td_dirdatasize_Noffsets] + .offset); + } + tif->tif_dir + .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets] + .length = datalength; + tif->tif_dir.td_dirdatasize_Noffsets++; + } + return true; +} + +/* + * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries. + */ +static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b) +{ + const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a; + const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b; + /* Compare offsets */ + if (ta->offset > tb->offset) + return 1; + else if (ta->offset < tb->offset) + return -1; + else + return 0; +} + +/* + * Determine the IFD data size after reading an IFD from the file that can be + * overwritten and saving it in tif_dir.td_dirdatasize_read. This data size + * includes the IFD entries themselves as well as the data that does not fit + * directly into the IFD entries but is located directly after the IFD entries + * in the file. + */ +static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount) +{ + /* IFD data size is only needed if file-writing is enabled. + * This also avoids the seek() to EOF to determine the file size, which + * causes the stdin-streaming-friendly mode of libtiff for GDAL to fail. */ + if (tif->tif_mode == O_RDONLY) + return; + + /* Sort TIFFEntryOffsetAndLength array in ascending order. */ + qsort(tif->tif_dir.td_dirdatasize_offsets, + tif->tif_dir.td_dirdatasize_Noffsets, + sizeof(TIFFEntryOffsetAndLength), cmpTIFFEntryOffsetAndLength); + + /* Get offset of end of IFD entry space. */ + uint64_t IFDendoffset; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + IFDendoffset = tif->tif_diroff + 2 + dircount * 12 + 4; + else + IFDendoffset = tif->tif_diroff + 8 + dircount * 20 + 8; + + /* Check which offsets are right behind IFD entries. However, LibTIFF + * increments the writing address for every external data to an even offset. + * Thus gaps of 1 byte can occur. */ + uint64_t size = 0; + uint64_t offset; + uint32_t i; + for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++) + { + offset = tif->tif_dir.td_dirdatasize_offsets[i].offset; + if (offset == IFDendoffset) + { + size += tif->tif_dir.td_dirdatasize_offsets[i].length; + IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length; + } + else if (offset == IFDendoffset + 1) + { + /* Add gap byte after previous IFD data set. */ + size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1; + IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length; + } + else + { + /* Further data is no more continuously after IFD */ + break; + } + } + /* Check for gap byte of some easy cases. This should cover 90% of cases. + * Otherwise, IFD will be re-written even it might be safely overwritten. */ + if (tif->tif_nextdiroff != 0) + { + if (tif->tif_nextdiroff == IFDendoffset + 1) + size++; + } + else + { + /* Check for IFD data ends at EOF. Then IFD can always be safely + * overwritten. */ + offset = TIFFSeekFile(tif, 0, SEEK_END); + if (offset == IFDendoffset) + { + tif->tif_dir.td_dirdatasize_read = UINT64_MAX; + return; + } + } + + /* Finally, add the size of the IFD tag entries themselves. */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + tif->tif_dir.td_dirdatasize_read = 2 + dircount * 12 + 4 + size; + else + tif->tif_dir.td_dirdatasize_read = 8 + dircount * 20 + 8 + size; +} /*-- CalcFinalIFDdatasizeReading() --*/ + +/* + * Read the next TIFF directory from a file and convert it to the internal + * format. We read directories sequentially. + */ +int TIFFReadDirectory(TIFF *tif) +{ + static const char module[] = "TIFFReadDirectory"; + TIFFDirEntry *dir; + uint16_t dircount; + TIFFDirEntry *dp; + uint16_t di; + const TIFFField *fip; + uint32_t fii = FAILED_FII; + toff_t nextdiroff; + int bitspersample_read = FALSE; + int color_channels; + + if (tif->tif_nextdiroff == 0) + { + /* In this special case, tif_diroff needs also to be set to 0. + * This is behind the last IFD, thus no checking or reading necessary. + */ + tif->tif_diroff = tif->tif_nextdiroff; + return 0; + } + + nextdiroff = tif->tif_nextdiroff; + /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL + * reading of the directory. Otherwise, invalid IFD offsets could corrupt + * the IFD list. */ + if (!_TIFFCheckDirNumberAndOffset(tif, + tif->tif_curdir == + TIFF_NON_EXISTENT_DIR_NUMBER + ? 0 + : tif->tif_curdir + 1, + nextdiroff)) + { + return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT + IFDs) */ + } + dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff); + if (!dircount) + { + TIFFErrorExtR(tif, module, + "Failed to read directory at offset %" PRIu64, + nextdiroff); + return 0; + } + /* Set global values after a valid directory has been fetched. + * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the + * beginning. */ + if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER) + tif->tif_curdir = 0; + else + tif->tif_curdir++; + + TIFFReadDirectoryCheckOrder(tif, dir, dircount); + + /* + * Mark duplicates of any tag to be ignored (bugzilla 1994) + * to avoid certain pathological problems. + */ + { + TIFFDirEntry *ma; + uint16_t mb; + for (ma = dir, mb = 0; mb < dircount; ma++, mb++) + { + TIFFDirEntry *na; + uint16_t nb; + for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++) + { + if (ma->tdir_tag == na->tdir_tag) + { + na->tdir_ignore = TRUE; + } + } + } + } + + tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */ + tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */ + tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS; + + /* free any old stuff and reinit */ + TIFFFreeDirectory(tif); + TIFFDefaultDirectory(tif); + + /* After setup a fresh directory indicate that now active IFD is also + * present on file, even if its entries could not be read successfully + * below. */ + tif->tif_dir.td_iswrittentofile = TRUE; + + /* Allocate arrays for offset values outside IFD entry for IFD data size + * checking. Note: Counter are reset within TIFFFreeDirectory(). */ + tif->tif_dir.td_dirdatasize_offsets = + (TIFFEntryOffsetAndLength *)_TIFFmallocExt( + tif, dircount * sizeof(TIFFEntryOffsetAndLength)); + if (tif->tif_dir.td_dirdatasize_offsets == NULL) + { + TIFFErrorExtR( + tif, module, + "Failed to allocate memory for counting IFD data size at reading"); + goto bad; + } + /* + * Electronic Arts writes gray-scale TIFF files + * without a PlanarConfiguration directory entry. + * Thus we setup a default value here, even though + * the TIFF spec says there is no default value. + * After PlanarConfiguration is preset in TIFFDefaultDirectory() + * the following setting is not needed, but does not harm either. + */ + TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + /* + * Setup default value and then make a pass over + * the fields to check type and tag information, + * and to extract info required to size data + * structures. A second pass is made afterwards + * to read in everything not taken in the first pass. + * But we must process the Compression tag first + * in order to merge in codec-private tag definitions (otherwise + * we may get complaints about unknown tags). However, the + * Compression tag may be dependent on the SamplesPerPixel + * tag value because older TIFF specs permitted Compression + * to be written as a SamplesPerPixel-count tag entry. + * Thus if we don't first figure out the correct SamplesPerPixel + * tag value then we may end up ignoring the Compression tag + * value because it has an incorrect count value (if the + * true value of SamplesPerPixel is not 1). + */ + dp = + TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL); + if (dp) + { + if (!TIFFFetchNormalTag(tif, dp, 0)) + goto bad; + dp->tdir_ignore = TRUE; + } + dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION); + if (dp) + { + /* + * The 5.0 spec says the Compression tag has one value, while + * earlier specs say it has one value per sample. Because of + * this, we accept the tag if one value is supplied with either + * count. + */ + uint16_t value; + enum TIFFReadDirEntryErr err; + err = TIFFReadDirEntryShort(tif, dp, &value); + if (err == TIFFReadDirEntryErrCount) + err = TIFFReadDirEntryPersampleShort(tif, dp, &value); + if (err != TIFFReadDirEntryErrOk) + { + TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0); + goto bad; + } + if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value)) + goto bad; + dp->tdir_ignore = TRUE; + } + else + { + if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE)) + goto bad; + } + /* + * First real pass over the directory. + */ + for (di = 0, dp = dir; di < dircount; di++, dp++) + { + if (!dp->tdir_ignore) + { + TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); + if (fii == FAILED_FII) + { + if (tif->tif_warn_about_unknown_tags) + { + TIFFWarningExtR(tif, module, + "Unknown field with tag %" PRIu16 + " (0x%" PRIx16 ") encountered", + dp->tdir_tag, dp->tdir_tag); + } + /* the following knowingly leaks the + anonymous field structure */ + const TIFFField *fld = _TIFFCreateAnonField( + tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type); + if (fld == NULL || !_TIFFMergeFields(tif, fld, 1)) + { + TIFFWarningExtR( + tif, module, + "Registering anonymous field with tag %" PRIu16 + " (0x%" PRIx16 ") failed", + dp->tdir_tag, dp->tdir_tag); + dp->tdir_ignore = TRUE; + } + else + { + TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); + assert(fii != FAILED_FII); + } + } + } + if (!dp->tdir_ignore) + { + fip = tif->tif_fields[fii]; + if (fip->field_bit == FIELD_IGNORE) + dp->tdir_ignore = TRUE; + else + { + switch (dp->tdir_tag) + { + case TIFFTAG_STRIPOFFSETS: + case TIFFTAG_STRIPBYTECOUNTS: + case TIFFTAG_TILEOFFSETS: + case TIFFTAG_TILEBYTECOUNTS: + TIFFSetFieldBit(tif, fip->field_bit); + break; + case TIFFTAG_IMAGEWIDTH: + case TIFFTAG_IMAGELENGTH: + case TIFFTAG_IMAGEDEPTH: + case TIFFTAG_TILELENGTH: + case TIFFTAG_TILEWIDTH: + case TIFFTAG_TILEDEPTH: + case TIFFTAG_PLANARCONFIG: + case TIFFTAG_ROWSPERSTRIP: + case TIFFTAG_EXTRASAMPLES: + if (!TIFFFetchNormalTag(tif, dp, 0)) + goto bad; + dp->tdir_ignore = TRUE; + break; + default: + if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag)) + dp->tdir_ignore = TRUE; + break; + } + } + } + } + /* + * XXX: OJPEG hack. + * If a) compression is OJPEG, b) planarconfig tag says it's separate, + * c) strip offsets/bytecounts tag are both present and + * d) both contain exactly one value, then we consistently find + * that the buggy implementation of the buggy compression scheme + * matches contig planarconfig best. So we 'fix-up' the tag here + */ + if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) && + (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)) + { + if (!_TIFFFillStriles(tif)) + goto bad; + dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, + TIFFTAG_STRIPOFFSETS); + if ((dp != 0) && (dp->tdir_count == 1)) + { + dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, + TIFFTAG_STRIPBYTECOUNTS); + if ((dp != 0) && (dp->tdir_count == 1)) + { + tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG; + TIFFWarningExtR(tif, module, + "Planarconfig tag value assumed incorrect, " + "assuming data is contig instead of chunky"); + } + } + } + /* + * Allocate directory structure and setup defaults. + */ + if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) + { + MissingRequired(tif, "ImageLength"); + goto bad; + } + + /* + * Second pass: extract other information. + */ + for (di = 0, dp = dir; di < dircount; di++, dp++) + { + if (!dp->tdir_ignore) + { + switch (dp->tdir_tag) + { + case TIFFTAG_MINSAMPLEVALUE: + case TIFFTAG_MAXSAMPLEVALUE: + case TIFFTAG_BITSPERSAMPLE: + case TIFFTAG_DATATYPE: + case TIFFTAG_SAMPLEFORMAT: + /* + * The MinSampleValue, MaxSampleValue, BitsPerSample + * DataType and SampleFormat tags are supposed to be + * written as one value/sample, but some vendors + * incorrectly write one value only -- so we accept + * that as well (yuck). Other vendors write correct + * value for NumberOfSamples, but incorrect one for + * BitsPerSample and friends, and we will read this + * too. + */ + { + uint16_t value; + enum TIFFReadDirEntryErr err; + err = TIFFReadDirEntryShort(tif, dp, &value); + if (!EvaluateIFDdatasizeReading(tif, dp)) + goto bad; + if (err == TIFFReadDirEntryErrCount) + err = + TIFFReadDirEntryPersampleShort(tif, dp, &value); + if (err != TIFFReadDirEntryErrOk) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFReadDirEntryOutputErr( + tif, err, module, + fip ? fip->field_name : "unknown tagname", 0); + goto bad; + } + if (!TIFFSetField(tif, dp->tdir_tag, value)) + goto bad; + if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE) + bitspersample_read = TRUE; + } + break; + case TIFFTAG_SMINSAMPLEVALUE: + case TIFFTAG_SMAXSAMPLEVALUE: + { + + double *data = NULL; + enum TIFFReadDirEntryErr err; + uint32_t saved_flags; + int m; + if (dp->tdir_count != + (uint64_t)tif->tif_dir.td_samplesperpixel) + err = TIFFReadDirEntryErrCount; + else + err = TIFFReadDirEntryDoubleArray(tif, dp, &data); + if (!EvaluateIFDdatasizeReading(tif, dp)) + goto bad; + if (err != TIFFReadDirEntryErrOk) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFReadDirEntryOutputErr( + tif, err, module, + fip ? fip->field_name : "unknown tagname", 0); + goto bad; + } + saved_flags = tif->tif_flags; + tif->tif_flags |= TIFF_PERSAMPLE; + m = TIFFSetField(tif, dp->tdir_tag, data); + tif->tif_flags = saved_flags; + _TIFFfreeExt(tif, data); + if (!m) + goto bad; + } + break; + case TIFFTAG_STRIPOFFSETS: + case TIFFTAG_TILEOFFSETS: + { + switch (dp->tdir_type) + { + case TIFF_SHORT: + case TIFF_LONG: + case TIFF_LONG8: + break; + default: + /* Warn except if directory typically created with + * TIFFDeferStrileArrayWriting() */ + if (!(tif->tif_mode == O_RDWR && + dp->tdir_count == 0 && dp->tdir_type == 0 && + dp->tdir_offset.toff_long8 == 0)) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFWarningExtR( + tif, module, "Invalid data type for tag %s", + fip ? fip->field_name : "unknown tagname"); + } + break; + } + _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp, + sizeof(TIFFDirEntry)); + if (!EvaluateIFDdatasizeReading(tif, dp)) + goto bad; + } + break; + case TIFFTAG_STRIPBYTECOUNTS: + case TIFFTAG_TILEBYTECOUNTS: + { + switch (dp->tdir_type) + { + case TIFF_SHORT: + case TIFF_LONG: + case TIFF_LONG8: + break; + default: + /* Warn except if directory typically created with + * TIFFDeferStrileArrayWriting() */ + if (!(tif->tif_mode == O_RDWR && + dp->tdir_count == 0 && dp->tdir_type == 0 && + dp->tdir_offset.toff_long8 == 0)) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFWarningExtR( + tif, module, "Invalid data type for tag %s", + fip ? fip->field_name : "unknown tagname"); + } + break; + } + _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp, + sizeof(TIFFDirEntry)); + if (!EvaluateIFDdatasizeReading(tif, dp)) + goto bad; + } + break; + case TIFFTAG_COLORMAP: + case TIFFTAG_TRANSFERFUNCTION: + { + enum TIFFReadDirEntryErr err; + uint32_t countpersample; + uint32_t countrequired; + uint32_t incrementpersample; + uint16_t *value = NULL; + /* It would be dangerous to instantiate those tag values */ + /* since if td_bitspersample has not yet been read (due to + */ + /* unordered tags), it could be read afterwards with a */ + /* values greater than the default one (1), which may cause + */ + /* crashes in user code */ + if (!bitspersample_read) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFWarningExtR( + tif, module, + "Ignoring %s since BitsPerSample tag not found", + fip ? fip->field_name : "unknown tagname"); + continue; + } + /* ColorMap or TransferFunction for high bit */ + /* depths do not make much sense and could be */ + /* used as a denial of service vector */ + if (tif->tif_dir.td_bitspersample > 24) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFWarningExtR( + tif, module, + "Ignoring %s because BitsPerSample=%" PRIu16 ">24", + fip ? fip->field_name : "unknown tagname", + tif->tif_dir.td_bitspersample); + continue; + } + countpersample = (1U << tif->tif_dir.td_bitspersample); + if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) && + (dp->tdir_count == (uint64_t)countpersample)) + { + countrequired = countpersample; + incrementpersample = 0; + } + else + { + countrequired = 3 * countpersample; + incrementpersample = countpersample; + } + if (dp->tdir_count != (uint64_t)countrequired) + err = TIFFReadDirEntryErrCount; + else + err = TIFFReadDirEntryShortArray(tif, dp, &value); + if (!EvaluateIFDdatasizeReading(tif, dp)) + goto bad; + if (err != TIFFReadDirEntryErrOk) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFReadDirEntryOutputErr( + tif, err, module, + fip ? fip->field_name : "unknown tagname", 1); + } + else + { + TIFFSetField(tif, dp->tdir_tag, value, + value + incrementpersample, + value + 2 * incrementpersample); + _TIFFfreeExt(tif, value); + } + } + break; + /* BEGIN REV 4.0 COMPATIBILITY */ + case TIFFTAG_OSUBFILETYPE: + { + uint16_t valueo; + uint32_t value; + if (TIFFReadDirEntryShort(tif, dp, &valueo) == + TIFFReadDirEntryErrOk) + { + switch (valueo) + { + case OFILETYPE_REDUCEDIMAGE: + value = FILETYPE_REDUCEDIMAGE; + break; + case OFILETYPE_PAGE: + value = FILETYPE_PAGE; + break; + default: + value = 0; + break; + } + if (value != 0) + TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value); + } + } + break; + /* END REV 4.0 COMPATIBILITY */ +#if 0 + case TIFFTAG_EP_BATTERYLEVEL: + /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII. + * LibTiff defines it as ASCII and converts RATIONAL to an + * ASCII string. */ + switch (dp->tdir_type) + { + case TIFF_RATIONAL: + { + /* Read rational and convert to ASCII*/ + enum TIFFReadDirEntryErr err; + TIFFRational_t rValue; + err = TIFFReadDirEntryCheckedRationalDirect( + tif, dp, &rValue); + if (err != TIFFReadDirEntryErrOk) + { + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFReadDirEntryOutputErr( + tif, err, module, + fip ? fip->field_name : "unknown tagname", + 1); + } + else + { + char szAux[32]; + snprintf(szAux, sizeof(szAux) - 1, "%d/%d", + rValue.uNum, rValue.uDenom); + TIFFSetField(tif, dp->tdir_tag, szAux); + } + } + break; + case TIFF_ASCII: + (void)TIFFFetchNormalTag(tif, dp, TRUE); + break; + default: + fip = TIFFFieldWithTag(tif, dp->tdir_tag); + TIFFWarningExtR(tif, module, + "Invalid data type for tag %s. " + "ASCII or RATIONAL expected", + fip ? fip->field_name + : "unknown tagname"); + break; + } + break; +#endif + default: + (void)TIFFFetchNormalTag(tif, dp, TRUE); + break; + } /* -- switch (dp->tdir_tag) -- */ + } /* -- if (!dp->tdir_ignore) */ + } /* -- for-loop -- */ + + /* Evaluate final IFD data size. */ + CalcFinalIFDdatasizeReading(tif, dircount); + + /* + * OJPEG hack: + * - If a) compression is OJPEG, and b) photometric tag is missing, + * then we consistently find that photometric should be YCbCr + * - If a) compression is OJPEG, and b) photometric tag says it's RGB, + * then we consistently find that the buggy implementation of the + * buggy compression scheme matches photometric YCbCr instead. + * - If a) compression is OJPEG, and b) bitspersample tag is missing, + * then we consistently find bitspersample should be 8. + * - If a) compression is OJPEG, b) samplesperpixel tag is missing, + * and c) photometric is RGB or YCbCr, then we consistently find + * samplesperpixel should be 3 + * - If a) compression is OJPEG, b) samplesperpixel tag is missing, + * and c) photometric is MINISWHITE or MINISBLACK, then we consistently + * find samplesperpixel should be 3 + */ + if (tif->tif_dir.td_compression == COMPRESSION_OJPEG) + { + if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC)) + { + TIFFWarningExtR( + tif, module, + "Photometric tag is missing, assuming data is YCbCr"); + if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR)) + goto bad; + } + else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB) + { + tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR; + TIFFWarningExtR(tif, module, + "Photometric tag value assumed incorrect, " + "assuming data is YCbCr instead of RGB"); + } + if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE)) + { + TIFFWarningExtR( + tif, module, + "BitsPerSample tag is missing, assuming 8 bits per sample"); + if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) + goto bad; + } + if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) + { + if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB) + { + TIFFWarningExtR(tif, module, + "SamplesPerPixel tag is missing, " + "assuming correct SamplesPerPixel value is 3"); + if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) + goto bad; + } + if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) + { + TIFFWarningExtR(tif, module, + "SamplesPerPixel tag is missing, " + "applying correct SamplesPerPixel value of 3"); + if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) + goto bad; + } + else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) || + (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK)) + { + /* + * SamplesPerPixel tag is missing, but is not required + * by spec. Assume correct SamplesPerPixel value of 1. + */ + if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1)) + goto bad; + } + } + } + + /* + * Setup appropriate structures (by strip or by tile) + * We do that only after the above OJPEG hack which alters SamplesPerPixel + * and thus influences the number of strips in the separate planarconfig. + */ + if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) + { + tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif); + tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth; + tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip; + tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth; + tif->tif_flags &= ~TIFF_ISTILED; + } + else + { + tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif); + tif->tif_flags |= TIFF_ISTILED; + } + if (!tif->tif_dir.td_nstrips) + { + TIFFErrorExtR(tif, module, "Cannot handle zero number of %s", + isTiled(tif) ? "tiles" : "strips"); + goto bad; + } + tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips; + if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE) + tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel; + if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) + { +#ifdef OJPEG_SUPPORT + if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) && + (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1)) + { + /* + * XXX: OJPEG hack. + * If a) compression is OJPEG, b) it's not a tiled TIFF, + * and c) the number of strips is 1, + * then we tolerate the absence of stripoffsets tag, + * because, presumably, all required data is in the + * JpegInterchangeFormat stream. + */ + TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS); + } + else +#endif + { + MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets"); + goto bad; + } + } + + if (tif->tif_mode == O_RDWR && + tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && + tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) + { + /* Directory typically created with TIFFDeferStrileArrayWriting() */ + TIFFSetupStrips(tif); + } + else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD)) + { + if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0) + { + if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry), + tif->tif_dir.td_nstrips, + &tif->tif_dir.td_stripoffset_p)) + { + goto bad; + } + } + if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0) + { + if (!TIFFFetchStripThing( + tif, &(tif->tif_dir.td_stripbytecount_entry), + tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p)) + { + goto bad; + } + } + } + + /* + * Make sure all non-color channels are extrasamples. + * If it's not the case, define them as such. + */ + color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric); + if (color_channels && + tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > + color_channels) + { + uint16_t old_extrasamples; + uint16_t *new_sampleinfo; + + TIFFWarningExtR( + tif, module, + "Sum of Photometric type-related " + "color channels and ExtraSamples doesn't match SamplesPerPixel. " + "Defining non-color channels as ExtraSamples."); + + old_extrasamples = tif->tif_dir.td_extrasamples; + tif->tif_dir.td_extrasamples = + (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels); + + // sampleinfo should contain information relative to these new extra + // samples + new_sampleinfo = (uint16_t *)_TIFFcallocExt( + tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t)); + if (!new_sampleinfo) + { + TIFFErrorExtR(tif, module, + "Failed to allocate memory for " + "temporary new sampleinfo array " + "(%" PRIu16 " 16 bit elements)", + tif->tif_dir.td_extrasamples); + goto bad; + } + + if (old_extrasamples > 0) + memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, + old_extrasamples * sizeof(uint16_t)); + _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo, + tif->tif_dir.td_extrasamples); + _TIFFfreeExt(tif, new_sampleinfo); + } + + /* + * Verify Palette image has a Colormap. + */ + if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE && + !TIFFFieldSet(tif, FIELD_COLORMAP)) + { + if (tif->tif_dir.td_bitspersample >= 8 && + tif->tif_dir.td_samplesperpixel == 3) + tif->tif_dir.td_photometric = PHOTOMETRIC_RGB; + else if (tif->tif_dir.td_bitspersample >= 8) + tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK; + else + { + MissingRequired(tif, "Colormap"); + goto bad; + } + } + /* + * OJPEG hack: + * We do no further messing with strip/tile offsets/bytecounts in OJPEG + * TIFFs + */ + if (tif->tif_dir.td_compression != COMPRESSION_OJPEG) + { + /* + * Attempt to deal with a missing StripByteCounts tag. + */ + if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) + { + /* + * Some manufacturers violate the spec by not giving + * the size of the strips. In this case, assume there + * is one uncompressed strip of data. + */ + if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG && + tif->tif_dir.td_nstrips > 1) || + (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE && + tif->tif_dir.td_nstrips != + (uint32_t)tif->tif_dir.td_samplesperpixel)) + { + MissingRequired(tif, "StripByteCounts"); + goto bad; + } + TIFFWarningExtR( + tif, module, + "TIFF directory is missing required " + "\"StripByteCounts\" field, calculating from imagelength"); + if (EstimateStripByteCounts(tif, dir, dircount) < 0) + goto bad; + } + else if (tif->tif_dir.td_nstrips == 1 && + !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif)) + { + /* + * XXX: Plexus (and others) sometimes give a value of + * zero for a tag when they don't know what the + * correct value is! Try and handle the simple case + * of estimating the size of a one strip image. + */ + TIFFWarningExtR(tif, module, + "Bogus \"StripByteCounts\" field, ignoring and " + "calculating from imagelength"); + if (EstimateStripByteCounts(tif, dir, dircount) < 0) + goto bad; + } + else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && + tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG && + tif->tif_dir.td_nstrips > 2 && + tif->tif_dir.td_compression == COMPRESSION_NONE && + TIFFGetStrileByteCount(tif, 0) != + TIFFGetStrileByteCount(tif, 1) && + TIFFGetStrileByteCount(tif, 0) != 0 && + TIFFGetStrileByteCount(tif, 1) != 0) + { + /* + * XXX: Some vendors fill StripByteCount array with + * absolutely wrong values (it can be equal to + * StripOffset array, for example). Catch this case + * here. + * + * We avoid this check if deferring strile loading + * as it would always force us to load the strip/tile + * information. + */ + TIFFWarningExtR(tif, module, + "Wrong \"StripByteCounts\" field, ignoring and " + "calculating from imagelength"); + if (EstimateStripByteCounts(tif, dir, dircount) < 0) + goto bad; + } + } + if (dir) + { + _TIFFfreeExt(tif, dir); + dir = NULL; + } + if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) + { + if (tif->tif_dir.td_bitspersample >= 16) + tif->tif_dir.td_maxsamplevalue = 0xFFFF; + else + tif->tif_dir.td_maxsamplevalue = + (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1); + } + +#ifdef STRIPBYTECOUNTSORTED_UNUSED + /* + * XXX: We can optimize checking for the strip bounds using the sorted + * bytecounts array. See also comments for TIFFAppendToStrip() + * function in tif_write.c. + */ + if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) + { + uint32_t strip; + + tif->tif_dir.td_stripbytecountsorted = 1; + for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) + { + if (TIFFGetStrileOffset(tif, strip - 1) > + TIFFGetStrileOffset(tif, strip)) + { + tif->tif_dir.td_stripbytecountsorted = 0; + break; + } + } + } +#endif + + /* + * An opportunity for compression mode dependent tag fixup + */ + (*tif->tif_fixuptags)(tif); + + /* + * Some manufacturers make life difficult by writing + * large amounts of uncompressed data as a single strip. + * This is contrary to the recommendations of the spec. + * The following makes an attempt at breaking such images + * into strips closer to the recommended 8k bytes. A + * side effect, however, is that the RowsPerStrip tag + * value may be changed. + */ + if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) && + (tif->tif_dir.td_nstrips == 1) && + (tif->tif_dir.td_compression == COMPRESSION_NONE) && + ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP)) + { + ChopUpSingleUncompressedStrip(tif); + } + + /* There are also uncompressed striped files with strips larger than */ + /* 2 GB, which make them unfriendly with a lot of code. If possible, */ + /* try to expose smaller "virtual" strips. */ + if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG && + tif->tif_dir.td_compression == COMPRESSION_NONE && + (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP && + TIFFStripSize64(tif) > 0x7FFFFFFFUL) + { + TryChopUpUncompressedBigTiff(tif); + } + + /* + * Clear the dirty directory flag. + */ + tif->tif_flags &= ~TIFF_DIRTYDIRECT; + tif->tif_flags &= ~TIFF_DIRTYSTRIP; + + /* + * Reinitialize i/o since we are starting on a new directory. + */ + tif->tif_row = (uint32_t)-1; + tif->tif_curstrip = (uint32_t)-1; + tif->tif_col = (uint32_t)-1; + tif->tif_curtile = (uint32_t)-1; + tif->tif_tilesize = (tmsize_t)-1; + + tif->tif_scanlinesize = TIFFScanlineSize(tif); + if (!tif->tif_scanlinesize) + { + TIFFErrorExtR(tif, module, "Cannot handle zero scanline size"); + return (0); + } + + if (isTiled(tif)) + { + tif->tif_tilesize = TIFFTileSize(tif); + if (!tif->tif_tilesize) + { + TIFFErrorExtR(tif, module, "Cannot handle zero tile size"); + return (0); + } + } + else + { + if (!TIFFStripSize(tif)) + { + TIFFErrorExtR(tif, module, "Cannot handle zero strip size"); + return (0); + } + } + return (1); +bad: + if (dir) + _TIFFfreeExt(tif, dir); + return (0); +} /*-- TIFFReadDirectory() --*/ + +static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir, + uint16_t dircount) +{ + static const char module[] = "TIFFReadDirectoryCheckOrder"; + uint32_t m; + uint16_t n; + TIFFDirEntry *o; + m = 0; + for (n = 0, o = dir; n < dircount; n++, o++) + { + if (o->tdir_tag < m) + { + TIFFWarningExtR(tif, module, + "Invalid TIFF directory; tags are not sorted in " + "ascending order"); + break; + } + m = o->tdir_tag + 1; + } +} + +static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir, + uint16_t dircount, + uint16_t tagid) +{ + TIFFDirEntry *m; + uint16_t n; + (void)tif; + for (m = dir, n = 0; n < dircount; m++, n++) + { + if (m->tdir_tag == tagid) + return (m); + } + return (0); +} + +static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid, + uint32_t *fii) +{ + int32_t ma, mb, mc; + ma = -1; + mc = (int32_t)tif->tif_nfields; + while (1) + { + if (ma + 1 == mc) + { + *fii = FAILED_FII; + return; + } + mb = (ma + mc) / 2; + if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid) + break; + if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid) + ma = mb; + else + mc = mb; + } + while (1) + { + if (mb == 0) + break; + if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid) + break; + mb--; + } + *fii = mb; +} + +/* + * Read custom directory from the arbitrary offset. + * The code is very similar to TIFFReadDirectory(). + */ +int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff, + const TIFFFieldArray *infoarray) +{ + static const char module[] = "TIFFReadCustomDirectory"; + TIFFDirEntry *dir; + uint16_t dircount; + TIFFDirEntry *dp; + uint16_t di; + const TIFFField *fip; + uint32_t fii; + + assert(infoarray != NULL); + dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL); + if (!dircount) + { + TIFFErrorExtR(tif, module, + "Failed to read custom directory at offset %" PRIu64, + diroff); + return 0; + } + TIFFReadDirectoryCheckOrder(tif, dir, dircount); + + /* + * Mark duplicates of any tag to be ignored (bugzilla 1994) + * to avoid certain pathological problems. + */ + { + TIFFDirEntry *ma; + uint16_t mb; + for (ma = dir, mb = 0; mb < dircount; ma++, mb++) + { + TIFFDirEntry *na; + uint16_t nb; + for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++) + { + if (ma->tdir_tag == na->tdir_tag) + { + na->tdir_ignore = TRUE; + } + } + } + } + + /* Free any old stuff and reinit. */ + TIFFFreeDirectory(tif); + /* Even if custom directories do not need the default settings of a standard + * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e. + * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to + * be initialized, which is done in TIFFDefaultDirectory(). + * After that, the field array for the custom tags needs to be setup again. + */ + TIFFDefaultDirectory(tif); + _TIFFSetupFields(tif, infoarray); + + /* Allocate arrays for offset values outside IFD entry for IFD data size + * checking. Note: Counter are reset within TIFFFreeDirectory(). */ + tif->tif_dir.td_dirdatasize_offsets = + (TIFFEntryOffsetAndLength *)_TIFFmallocExt( + tif, dircount * sizeof(TIFFEntryOffsetAndLength)); + if (tif->tif_dir.td_dirdatasize_offsets == NULL) + { + TIFFErrorExtR( + tif, module, + "Failed to allocate memory for counting IFD data size at reading"); + if (dir) + _TIFFfreeExt(tif, dir); + return 0; + } + + for (di = 0, dp = dir; di < dircount; di++, dp++) + { + TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); + if (fii == FAILED_FII) + { + if (tif->tif_warn_about_unknown_tags) + { + TIFFWarningExtR(tif, module, + "Unknown field with tag %" PRIu16 " (0x%" PRIx16 + ") encountered", + dp->tdir_tag, dp->tdir_tag); + } + const TIFFField *fld = _TIFFCreateAnonField( + tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type); + if (fld == NULL || !_TIFFMergeFields(tif, fld, 1)) + { + if (tif->tif_warn_about_unknown_tags) + { + TIFFWarningExtR( + tif, module, + "Registering anonymous field with tag %" PRIu16 + " (0x%" PRIx16 ") failed", + dp->tdir_tag, dp->tdir_tag); + } + dp->tdir_ignore = TRUE; + } + else + { + TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); + assert(fii != FAILED_FII); + } + } + if (!dp->tdir_ignore) + { + fip = tif->tif_fields[fii]; + if (fip->field_bit == FIELD_IGNORE) + dp->tdir_ignore = TRUE; + else + { + /* check data type */ + while ((fip->field_type != TIFF_ANY) && + (fip->field_type != dp->tdir_type)) + { + fii++; + if ((fii == tif->tif_nfields) || + (tif->tif_fields[fii]->field_tag != + (uint32_t)dp->tdir_tag)) + { + fii = 0xFFFF; + break; + } + fip = tif->tif_fields[fii]; + } + if (fii == 0xFFFF) + { + TIFFWarningExtR(tif, module, + "Wrong data type %" PRIu16 + " for \"%s\"; tag ignored", + dp->tdir_type, fip->field_name); + dp->tdir_ignore = TRUE; + } + else + { + /* check count if known in advance */ + if ((fip->field_readcount != TIFF_VARIABLE) && + (fip->field_readcount != TIFF_VARIABLE2)) + { + uint32_t expected; + if (fip->field_readcount == TIFF_SPP) + expected = + (uint32_t)tif->tif_dir.td_samplesperpixel; + else + expected = (uint32_t)fip->field_readcount; + if (!CheckDirCount(tif, dp, expected)) + dp->tdir_ignore = TRUE; + } + } + } + if (!dp->tdir_ignore) + { + switch (dp->tdir_tag) + { + case EXIFTAG_SUBJECTDISTANCE: + if (!TIFFFieldIsAnonymous(fip)) + { + /* should only be called on a Exif directory */ + /* when exifFields[] is active */ + (void)TIFFFetchSubjectDistance(tif, dp); + } + else + { + (void)TIFFFetchNormalTag(tif, dp, TRUE); + } + break; + default: + (void)TIFFFetchNormalTag(tif, dp, TRUE); + break; + } + } /*-- if (!dp->tdir_ignore) */ + } + } + /* Evaluate final IFD data size. */ + CalcFinalIFDdatasizeReading(tif, dircount); + + /* To be able to return from SubIFD or custom-IFD to main-IFD */ + tif->tif_setdirectory_force_absolute = TRUE; + if (dir) + _TIFFfreeExt(tif, dir); + return 1; +} + +/* + * EXIF is important special case of custom IFD, so we have a special + * function to read it. + */ +int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff) +{ + return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields()); +} + +/* + *--: EXIF-GPS custom directory reading as another special case of custom IFD. + */ +int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff) +{ + return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields()); +} + +static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir, + uint16_t dircount) +{ + static const char module[] = "EstimateStripByteCounts"; + + TIFFDirEntry *dp; + TIFFDirectory *td = &tif->tif_dir; + uint32_t strip; + + /* Do not try to load stripbytecount as we will compute it */ + if (!_TIFFFillStrilesInternal(tif, 0)) + return -1; + + const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t); + uint64_t filesize = 0; + if (allocsize > 100 * 1024 * 1024) + { + /* Before allocating a huge amount of memory for corrupted files, check + * if size of requested memory is not greater than file size. */ + filesize = TIFFGetFileSize(tif); + if (allocsize > filesize) + { + TIFFWarningExtR( + tif, module, + "Requested memory size for StripByteCounts of %" PRIu64 + " is greater than filesize %" PRIu64 ". Memory not allocated", + allocsize, filesize); + return -1; + } + } + + if (td->td_stripbytecount_p) + _TIFFfreeExt(tif, td->td_stripbytecount_p); + td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc( + tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array"); + if (td->td_stripbytecount_p == NULL) + return -1; + + if (td->td_compression != COMPRESSION_NONE) + { + uint64_t space; + uint16_t n; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4; + else + space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8; + /* calculate amount of space used by indirect values */ + for (dp = dir, n = dircount; n > 0; n--, dp++) + { + uint32_t typewidth; + uint64_t datasize; + typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type); + if (typewidth == 0) + { + TIFFErrorExtR( + tif, module, + "Cannot determine size of unknown tag type %" PRIu16, + dp->tdir_type); + return -1; + } + if (dp->tdir_count > UINT64_MAX / typewidth) + return -1; + datasize = (uint64_t)typewidth * dp->tdir_count; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (datasize <= 4) + datasize = 0; + } + else + { + if (datasize <= 8) + datasize = 0; + } + if (space > UINT64_MAX - datasize) + return -1; + space += datasize; + } + if (filesize == 0) + filesize = TIFFGetFileSize(tif); + if (filesize < space) + /* we should perhaps return in error ? */ + space = filesize; + else + space = filesize - space; + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + space /= td->td_samplesperpixel; + for (strip = 0; strip < td->td_nstrips; strip++) + td->td_stripbytecount_p[strip] = space; + /* + * This gross hack handles the case were the offset to + * the last strip is past the place where we think the strip + * should begin. Since a strip of data must be contiguous, + * it's safe to assume that we've overestimated the amount + * of data in the strip and trim this number back accordingly. + */ + strip--; + if (td->td_stripoffset_p[strip] > + UINT64_MAX - td->td_stripbytecount_p[strip]) + return -1; + if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] > + filesize) + { + if (td->td_stripoffset_p[strip] >= filesize) + { + /* Not sure what we should in that case... */ + td->td_stripbytecount_p[strip] = 0; + } + else + { + td->td_stripbytecount_p[strip] = + filesize - td->td_stripoffset_p[strip]; + } + } + } + else if (isTiled(tif)) + { + uint64_t bytespertile = TIFFTileSize64(tif); + + for (strip = 0; strip < td->td_nstrips; strip++) + td->td_stripbytecount_p[strip] = bytespertile; + } + else + { + uint64_t rowbytes = TIFFScanlineSize64(tif); + uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage; + for (strip = 0; strip < td->td_nstrips; strip++) + { + if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes) + return -1; + td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip; + } + } + TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); + if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) + td->td_rowsperstrip = td->td_imagelength; + return 1; +} + +static void MissingRequired(TIFF *tif, const char *tagname) +{ + static const char module[] = "MissingRequired"; + + TIFFErrorExtR(tif, module, + "TIFF directory is missing required \"%s\" field", tagname); +} + +static unsigned long hashFuncOffsetToNumber(const void *elt) +{ + const TIFFOffsetAndDirNumber *offsetAndDirNumber = + (const TIFFOffsetAndDirNumber *)elt; + const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^ + ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU); + return hash; +} + +static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2) +{ + const TIFFOffsetAndDirNumber *offsetAndDirNumber1 = + (const TIFFOffsetAndDirNumber *)elt1; + const TIFFOffsetAndDirNumber *offsetAndDirNumber2 = + (const TIFFOffsetAndDirNumber *)elt2; + return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset; +} + +static unsigned long hashFuncNumberToOffset(const void *elt) +{ + const TIFFOffsetAndDirNumber *offsetAndDirNumber = + (const TIFFOffsetAndDirNumber *)elt; + return offsetAndDirNumber->dirNumber; +} + +static bool equalFuncNumberToOffset(const void *elt1, const void *elt2) +{ + const TIFFOffsetAndDirNumber *offsetAndDirNumber1 = + (const TIFFOffsetAndDirNumber *)elt1; + const TIFFOffsetAndDirNumber *offsetAndDirNumber2 = + (const TIFFOffsetAndDirNumber *)elt2; + return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber; +} + +/* + * Check the directory number and offset against the list of already seen + * directory numbers and offsets. This is a trick to prevent IFD looping. + * The one can create TIFF file with looped directory pointers. We will + * maintain a list of already seen directories and check every IFD offset + * and its IFD number against that list. However, the offset of an IFD number + * can change - e.g. when writing updates to file. + * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered, + * or an error has occurred. + */ +int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff) +{ + if (diroff == 0) /* no more directories */ + return 0; + + if (tif->tif_map_dir_offset_to_number == NULL) + { + tif->tif_map_dir_offset_to_number = TIFFHashSetNew( + hashFuncOffsetToNumber, equalFuncOffsetToNumber, free); + if (tif->tif_map_dir_offset_to_number == NULL) + { + TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", + "Not enough memory"); + return 1; + } + } + + if (tif->tif_map_dir_number_to_offset == NULL) + { + /* No free callback for this map, as it shares the same items as + * tif->tif_map_dir_offset_to_number. */ + tif->tif_map_dir_number_to_offset = TIFFHashSetNew( + hashFuncNumberToOffset, equalFuncNumberToOffset, NULL); + if (tif->tif_map_dir_number_to_offset == NULL) + { + TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", + "Not enough memory"); + return 1; + } + } + + /* Check if offset is already in the list: + * - yes: check, if offset is at the same IFD number - if not, it is an IFD + * loop + * - no: add to list or update offset at that IFD number + */ + TIFFOffsetAndDirNumber entry; + entry.offset = diroff; + entry.dirNumber = dirn; + + TIFFOffsetAndDirNumber *foundEntry = + (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_offset_to_number, &entry); + if (foundEntry) + { + if (foundEntry->dirNumber == dirn) + { + return 1; + } + else + { + TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset", + "TIFF directory %d has IFD looping to directory %u " + "at offset 0x%" PRIx64 " (%" PRIu64 ")", + (int)dirn - 1, foundEntry->dirNumber, diroff, + diroff); + return 0; + } + } + + /* Check if offset of an IFD has been changed and update offset of that IFD + * number. */ + foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_number_to_offset, &entry); + if (foundEntry) + { + if (foundEntry->offset != diroff) + { + TIFFOffsetAndDirNumber entryOld; + entryOld.offset = foundEntry->offset; + entryOld.dirNumber = dirn; + /* We must remove first from tif_map_dir_number_to_offset as the */ + /* entry is owned (and thus freed) by */ + /* tif_map_dir_offset_to_number */ + TIFFOffsetAndDirNumber *foundEntryOld = + (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_number_to_offset, &entryOld); + if (foundEntryOld) + { + TIFFHashSetRemove(tif->tif_map_dir_number_to_offset, + foundEntryOld); + } + foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_offset_to_number, &entryOld); + if (foundEntryOld) + { + TIFFHashSetRemove(tif->tif_map_dir_offset_to_number, + foundEntryOld); + } + + TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc( + sizeof(TIFFOffsetAndDirNumber)); + if (entryPtr == NULL) + { + return 0; + } + + /* Add IFD offset and dirn to IFD directory list */ + *entryPtr = entry; + + if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr)) + { + TIFFErrorExtR( + tif, "_TIFFCheckDirNumberAndOffset", + "Insertion in tif_map_dir_offset_to_number failed"); + return 0; + } + if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr)) + { + TIFFErrorExtR( + tif, "_TIFFCheckDirNumberAndOffset", + "Insertion in tif_map_dir_number_to_offset failed"); + return 0; + } + } + return 1; + } + + /* Arbitrary (hopefully big enough) limit */ + if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >= + TIFF_MAX_DIR_COUNT) + { + TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", + "Cannot handle more than %u TIFF directories", + TIFF_MAX_DIR_COUNT); + return 0; + } + + TIFFOffsetAndDirNumber *entryPtr = + (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber)); + if (entryPtr == NULL) + { + TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", + "malloc(sizeof(TIFFOffsetAndDirNumber)) failed"); + return 0; + } + + /* Add IFD offset and dirn to IFD directory list */ + *entryPtr = entry; + + if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr)) + { + TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", + "Insertion in tif_map_dir_offset_to_number failed"); + return 0; + } + if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr)) + { + TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", + "Insertion in tif_map_dir_number_to_offset failed"); + return 0; + } + + return 1; +} /* --- _TIFFCheckDirNumberAndOffset() ---*/ + +/* + * Retrieve the matching IFD directory number of a given IFD offset + * from the list of directories already seen. + * Returns 1 if the offset was in the list and the directory number + * can be returned. + * Otherwise returns 0 or if an error occurred. + */ +int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn) +{ + if (diroff == 0) /* no more directories */ + return 0; + + /* Check if offset is already in the list and return matching directory + * number. Otherwise update IFD list using TIFFNumberOfDirectories() and + * search again in IFD list. + */ + if (tif->tif_map_dir_offset_to_number == NULL) + return 0; + TIFFOffsetAndDirNumber entry; + entry.offset = diroff; + entry.dirNumber = 0; /* not used */ + + TIFFOffsetAndDirNumber *foundEntry = + (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_offset_to_number, &entry); + if (foundEntry) + { + *dirn = foundEntry->dirNumber; + return 1; + } + + /* This updates the directory list for all main-IFDs in the file. */ + TIFFNumberOfDirectories(tif); + + foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_offset_to_number, &entry); + if (foundEntry) + { + *dirn = foundEntry->dirNumber; + return 1; + } + + return 0; +} /*--- _TIFFGetDirNumberFromOffset() ---*/ + +/* + * Retrieve the matching IFD directory offset of a given IFD number + * from the list of directories already seen. + * Returns 1 if the offset was in the list of already seen IFDs and the + * directory offset can be returned. The directory list is not updated. + * Otherwise returns 0 or if an error occurred. + */ +int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff) +{ + + if (tif->tif_map_dir_number_to_offset == NULL) + return 0; + TIFFOffsetAndDirNumber entry; + entry.offset = 0; /* not used */ + entry.dirNumber = dirn; + + TIFFOffsetAndDirNumber *foundEntry = + (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_number_to_offset, &entry); + if (foundEntry) + { + *diroff = foundEntry->offset; + return 1; + } + + return 0; +} /*--- _TIFFGetOffsetFromDirNumber() ---*/ + +/* + * Remove an entry from the directory list of already seen directories + * by directory offset. + * If an entry is to be removed from the list, it is also okay if the entry + * is not in the list or the list does not exist. + */ +int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff) +{ + if (tif->tif_map_dir_offset_to_number == NULL) + return 1; + + TIFFOffsetAndDirNumber entryOld; + entryOld.offset = diroff; + entryOld.dirNumber = 0; + /* We must remove first from tif_map_dir_number_to_offset as the + * entry is owned (and thus freed) by tif_map_dir_offset_to_number. + * However, we need firstly to find the directory number from offset. */ + + TIFFOffsetAndDirNumber *foundEntryOldOff = + (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_offset_to_number, &entryOld); + if (foundEntryOldOff) + { + entryOld.dirNumber = foundEntryOldOff->dirNumber; + if (tif->tif_map_dir_number_to_offset != NULL) + { + TIFFOffsetAndDirNumber *foundEntryOldDir = + (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( + tif->tif_map_dir_number_to_offset, &entryOld); + if (foundEntryOldDir) + { + TIFFHashSetRemove(tif->tif_map_dir_number_to_offset, + foundEntryOldDir); + TIFFHashSetRemove(tif->tif_map_dir_offset_to_number, + foundEntryOldOff); + return 1; + } + } + else + { + TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset", + "Unexpectedly tif_map_dir_number_to_offset is " + "missing but tif_map_dir_offset_to_number exists."); + return 0; + } + } + return 1; +} /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/ + +/* + * Check the count field of a directory entry against a known value. The + * caller is expected to skip/ignore the tag if there is a mismatch. + */ +static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count) +{ + if ((uint64_t)count > dir->tdir_count) + { + const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); + TIFFWarningExtR(tif, tif->tif_name, + "incorrect count for field \"%s\" (%" PRIu64 + ", expecting %" PRIu32 "); tag ignored", + fip ? fip->field_name : "unknown tagname", + dir->tdir_count, count); + return (0); + } + else if ((uint64_t)count < dir->tdir_count) + { + const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); + TIFFWarningExtR(tif, tif->tif_name, + "incorrect count for field \"%s\" (%" PRIu64 + ", expecting %" PRIu32 "); tag trimmed", + fip ? fip->field_name : "unknown tagname", + dir->tdir_count, count); + dir->tdir_count = count; + return (1); + } + return (1); +} + +/* + * Read IFD structure from the specified offset. If the pointer to + * nextdiroff variable has been specified, read it too. Function returns a + * number of fields in the directory or 0 if failed. + */ +static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff, + TIFFDirEntry **pdir, uint64_t *nextdiroff) +{ + static const char module[] = "TIFFFetchDirectory"; + + void *origdir; + uint16_t dircount16; + uint32_t dirsize; + TIFFDirEntry *dir; + uint8_t *ma; + TIFFDirEntry *mb; + uint16_t n; + + assert(pdir); + + tif->tif_diroff = diroff; + if (nextdiroff) + *nextdiroff = 0; + if (!isMapped(tif)) + { + if (!SeekOK(tif, tif->tif_diroff)) + { + TIFFErrorExtR(tif, module, + "%s: Seek error accessing TIFF directory", + tif->tif_name); + return 0; + } + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (!ReadOK(tif, &dircount16, sizeof(uint16_t))) + { + TIFFErrorExtR(tif, module, + "%s: Can not read TIFF directory count", + tif->tif_name); + return 0; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount16); + if (dircount16 > 4096) + { + TIFFErrorExtR(tif, module, + "Sanity check on directory count failed, this is " + "probably not a valid IFD offset"); + return 0; + } + dirsize = 12; + } + else + { + uint64_t dircount64; + if (!ReadOK(tif, &dircount64, sizeof(uint64_t))) + { + TIFFErrorExtR(tif, module, + "%s: Can not read TIFF directory count", + tif->tif_name); + return 0; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + if (dircount64 > 4096) + { + TIFFErrorExtR(tif, module, + "Sanity check on directory count failed, this is " + "probably not a valid IFD offset"); + return 0; + } + dircount16 = (uint16_t)dircount64; + dirsize = 20; + } + origdir = _TIFFCheckMalloc(tif, dircount16, dirsize, + "to read TIFF directory"); + if (origdir == NULL) + return 0; + if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize))) + { + TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory", + tif->tif_name); + _TIFFfreeExt(tif, origdir); + return 0; + } + /* + * Read offset to next directory for sequential scans if + * needed. + */ + if (nextdiroff) + { + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t nextdiroff32; + if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t))) + nextdiroff32 = 0; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextdiroff32); + *nextdiroff = nextdiroff32; + } + else + { + if (!ReadOK(tif, nextdiroff, sizeof(uint64_t))) + *nextdiroff = 0; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(nextdiroff); + } + } + } + else + { + tmsize_t m; + tmsize_t off; + if (tif->tif_diroff > (uint64_t)INT64_MAX) + { + TIFFErrorExtR(tif, module, "Can not read TIFF directory count"); + return (0); + } + off = (tmsize_t)tif->tif_diroff; + + /* + * Check for integer overflow when validating the dir_off, + * otherwise a very high offset may cause an OOB read and + * crash the client. Make two comparisons instead of + * + * off + sizeof(uint16_t) > tif->tif_size + * + * to avoid overflow. + */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + m = off + sizeof(uint16_t); + if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) || + (m > tif->tif_size)) + { + TIFFErrorExtR(tif, module, "Can not read TIFF directory count"); + return 0; + } + else + { + _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t)); + } + off += sizeof(uint16_t); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount16); + if (dircount16 > 4096) + { + TIFFErrorExtR(tif, module, + "Sanity check on directory count failed, this is " + "probably not a valid IFD offset"); + return 0; + } + dirsize = 12; + } + else + { + uint64_t dircount64; + m = off + sizeof(uint64_t); + if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) || + (m > tif->tif_size)) + { + TIFFErrorExtR(tif, module, "Can not read TIFF directory count"); + return 0; + } + else + { + _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t)); + } + off += sizeof(uint64_t); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + if (dircount64 > 4096) + { + TIFFErrorExtR(tif, module, + "Sanity check on directory count failed, this is " + "probably not a valid IFD offset"); + return 0; + } + dircount16 = (uint16_t)dircount64; + dirsize = 20; + } + if (dircount16 == 0) + { + TIFFErrorExtR(tif, module, + "Sanity check on directory count failed, zero tag " + "directories not supported"); + return 0; + } + /* Before allocating a huge amount of memory for corrupted files, check + * if size of requested memory is not greater than file size. */ + uint64_t filesize = TIFFGetFileSize(tif); + uint64_t allocsize = (uint64_t)dircount16 * dirsize; + if (allocsize > filesize) + { + TIFFWarningExtR( + tif, module, + "Requested memory size for TIFF directory of %" PRIu64 + " is greater than filesize %" PRIu64 + ". Memory not allocated, TIFF directory not read", + allocsize, filesize); + return 0; + } + origdir = _TIFFCheckMalloc(tif, dircount16, dirsize, + "to read TIFF directory"); + if (origdir == NULL) + return 0; + m = off + dircount16 * dirsize; + if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) || + (m > tif->tif_size)) + { + TIFFErrorExtR(tif, module, "Can not read TIFF directory"); + _TIFFfreeExt(tif, origdir); + return 0; + } + else + { + _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize); + } + if (nextdiroff) + { + off += dircount16 * dirsize; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t nextdiroff32; + m = off + sizeof(uint32_t); + if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) || + (m > tif->tif_size)) + nextdiroff32 = 0; + else + _TIFFmemcpy(&nextdiroff32, tif->tif_base + off, + sizeof(uint32_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextdiroff32); + *nextdiroff = nextdiroff32; + } + else + { + m = off + sizeof(uint64_t); + if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) || + (m > tif->tif_size)) + *nextdiroff = 0; + else + _TIFFmemcpy(nextdiroff, tif->tif_base + off, + sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(nextdiroff); + } + } + } + /* No check against filesize needed here because "dir" should have same size + * than "origdir" checked above. */ + dir = (TIFFDirEntry *)_TIFFCheckMalloc( + tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory"); + if (dir == 0) + { + _TIFFfreeExt(tif, origdir); + return 0; + } + ma = (uint8_t *)origdir; + mb = dir; + for (n = 0; n < dircount16; n++) + { + mb->tdir_ignore = FALSE; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + mb->tdir_tag = *(uint16_t *)ma; + ma += sizeof(uint16_t); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)ma); + mb->tdir_type = *(uint16_t *)ma; + ma += sizeof(uint16_t); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)ma); + mb->tdir_count = (uint64_t)(*(uint32_t *)ma); + ma += sizeof(uint32_t); + mb->tdir_offset.toff_long8 = 0; + *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma; + ma += sizeof(uint32_t); + } + else + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)ma); + mb->tdir_count = TIFFReadUInt64(ma); + ma += sizeof(uint64_t); + mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma); + ma += sizeof(uint64_t); + } + mb++; + } + _TIFFfreeExt(tif, origdir); + *pdir = dir; + return dircount16; +} + +/* + * Fetch a tag that is not handled by special case code. + */ +static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover) +{ + static const char module[] = "TIFFFetchNormalTag"; + enum TIFFReadDirEntryErr err; + uint32_t fii; + const TIFFField *fip = NULL; + TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); + if (fii == FAILED_FII) + { + TIFFErrorExtR(tif, "TIFFFetchNormalTag", + "No definition found for tag %" PRIu16, dp->tdir_tag); + return 0; + } + fip = tif->tif_fields[fii]; + assert(fip != NULL); /* should not happen */ + assert(fip->set_get_field_type != + TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with + this in specialized code */ + assert(fip->set_get_field_type != + TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only + the case for pseudo-tags */ + err = TIFFReadDirEntryErrOk; + switch (fip->set_get_field_type) + { + case TIFF_SETGET_UNDEFINED: + TIFFErrorExtR( + tif, "TIFFFetchNormalTag", + "Defined set_get_field_type of custom tag %u (%s) is " + "TIFF_SETGET_UNDEFINED and thus tag is not read from file", + fip->field_tag, fip->field_name); + break; + case TIFF_SETGET_ASCII: + { + uint8_t *data; + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryByteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + size_t mb = 0; + int n; + if (data != NULL) + { + if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0) + { + /* optimization: if data is known to be 0 terminated, we + * can use strlen() */ + mb = strlen((const char *)data); + } + else + { + /* general case. equivalent to non-portable */ + /* mb = strnlen((const char*)data, + * (uint32_t)dp->tdir_count); */ + uint8_t *ma = data; + while (mb < (uint32_t)dp->tdir_count) + { + if (*ma == 0) + break; + ma++; + mb++; + } + } + } + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != NULL) + _TIFFfreeExt(tif, data); + return (0); + } + if (mb + 1 < (uint32_t)dp->tdir_count) + TIFFWarningExtR( + tif, module, + "ASCII value for tag \"%s\" contains null byte in " + "value; value incorrectly truncated during reading due " + "to implementation limitations", + fip->field_name); + else if (mb + 1 > (uint32_t)dp->tdir_count) + { + TIFFWarningExtR(tif, module, + "ASCII value for tag \"%s\" does not end " + "in null byte. Forcing it to be null", + fip->field_name); + /* TIFFReadDirEntryArrayWithLimit() ensures this can't be + * larger than MAX_SIZE_TAG_DATA */ + assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1); + uint8_t *o = + _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1); + if (o == NULL) + { + if (data != NULL) + _TIFFfreeExt(tif, data); + return (0); + } + if (dp->tdir_count > 0) + { + _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count); + } + o[(uint32_t)dp->tdir_count] = 0; + if (data != 0) + _TIFFfreeExt(tif, data); + data = o; + } + n = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!n) + return (0); + } + } + break; + case TIFF_SETGET_UINT8: + { + uint8_t data = 0; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryByte(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_SINT8: + { + int8_t data = 0; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntrySbyte(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_UINT16: + { + uint16_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryShort(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_SINT16: + { + int16_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntrySshort(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_UINT32: + { + uint32_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryLong(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_SINT32: + { + int32_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntrySlong(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_UINT64: + { + uint64_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryLong8(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + return 0; + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_SINT64: + { + int64_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntrySlong8(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + return 0; + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_FLOAT: + { + float data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryFloat(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + return 0; + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_DOUBLE: + { + double data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryDouble(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + return 0; + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_IFD8: + { + uint64_t data; + assert(fip->field_readcount == 1); + assert(fip->field_passcount == 0); + err = TIFFReadDirEntryIfd8(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + return 0; + if (!TIFFSetField(tif, dp->tdir_tag, data)) + return (0); + } + } + break; + case TIFF_SETGET_UINT16_PAIR: + { + uint16_t *data; + assert(fip->field_readcount == 2); + assert(fip->field_passcount == 0); + if (dp->tdir_count != 2) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected 2, " + "got %" PRIu64, + fip->field_name, dp->tdir_count); + return (0); + } + err = TIFFReadDirEntryShortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + int m; + assert(data); /* avoid CLang static Analyzer false positive */ + m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]); + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C0_UINT8: + { + uint8_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntryByteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_SINT8: + { + int8_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntrySbyteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_UINT16: + { + uint16_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntryShortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_SINT16: + { + int16_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntrySshortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_UINT32: + { + uint32_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntryLongArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_SINT32: + { + int32_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntrySlongArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_UINT64: + { + uint64_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntryLong8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_SINT64: + { + int64_t *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntrySlong8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C0_FLOAT: + { + float *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntryFloatArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read + * into Double-Arrays. */ + case TIFF_SETGET_C0_DOUBLE: + { + double *data; + assert(fip->field_readcount >= 1); + assert(fip->field_passcount == 0); + if (dp->tdir_count != (uint64_t)fip->field_readcount) + { + TIFFWarningExtR(tif, module, + "incorrect count for field \"%s\", expected " + "%d, got %" PRIu64, + fip->field_name, (int)fip->field_readcount, + dp->tdir_count); + return (0); + } + else + { + err = TIFFReadDirEntryDoubleArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_ASCII: + { + uint8_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryByteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + if (data != 0 && dp->tdir_count > 0 && + data[dp->tdir_count - 1] != '\0') + { + TIFFWarningExtR(tif, module, + "ASCII value for ASCII array tag " + "\"%s\" does not end in null " + "byte. Forcing it to be null", + fip->field_name); + /* Enlarge buffer and add terminating null. */ + uint8_t *o = + _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1); + if (o == NULL) + { + if (data != NULL) + _TIFFfreeExt(tif, data); + return (0); + } + if (dp->tdir_count > 0) + { + _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count); + } + o[(uint32_t)dp->tdir_count] = 0; + dp->tdir_count++; /* Increment for added null. */ + if (data != 0) + _TIFFfreeExt(tif, data); + data = o; + } + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_UINT8: + { + uint8_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryByteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_SINT8: + { + int8_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntrySbyteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_UINT16: + { + uint16_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryShortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_SINT16: + { + int16_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntrySshortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_UINT32: + { + uint32_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryLongArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_SINT32: + { + int32_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntrySlongArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_UINT64: + { + uint64_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryLong8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_SINT64: + { + int64_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntrySlong8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_FLOAT: + { + float *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryFloatArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_DOUBLE: + { + double *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryDoubleArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C16_IFD8: + { + uint64_t *data; + assert(fip->field_readcount == TIFF_VARIABLE); + assert(fip->field_passcount == 1); + if (dp->tdir_count > 0xFFFF) + err = TIFFReadDirEntryErrCount; + else + { + err = TIFFReadDirEntryIfd8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, + (uint16_t)(dp->tdir_count), data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + } + break; + case TIFF_SETGET_C32_ASCII: + { + uint8_t *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryByteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + if (data != 0 && dp->tdir_count > 0 && + data[dp->tdir_count - 1] != '\0') + { + TIFFWarningExtR( + tif, module, + "ASCII value for ASCII array tag \"%s\" does not end " + "in null byte. Forcing it to be null", + fip->field_name); + /* Enlarge buffer and add terminating null. */ + uint8_t *o = + _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1); + if (o == NULL) + { + if (data != NULL) + _TIFFfreeExt(tif, data); + return (0); + } + if (dp->tdir_count > 0) + { + _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count); + } + o[(uint32_t)dp->tdir_count] = 0; + dp->tdir_count++; /* Increment for added null. */ + if (data != 0) + _TIFFfreeExt(tif, data); + data = o; + } + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_UINT8: + { + uint8_t *data; + uint32_t count = 0; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + if (fip->field_tag == TIFFTAG_RICHTIFFIPTC && + dp->tdir_type == TIFF_LONG) + { + /* Adobe's software (wrongly) writes RichTIFFIPTC tag with + * data type LONG instead of UNDEFINED. Work around this + * frequently found issue */ + void *origdata; + err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata); + if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) + { + data = NULL; + } + else + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)origdata, count); + data = (uint8_t *)origdata; + count = (uint32_t)(count * 4); + } + } + else + { + err = TIFFReadDirEntryByteArray(tif, dp, &data); + count = (uint32_t)(dp->tdir_count); + } + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, count, data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_SINT8: + { + int8_t *data = NULL; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntrySbyteArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_UINT16: + { + uint16_t *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryShortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_SINT16: + { + int16_t *data = NULL; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntrySshortArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_UINT32: + { + uint32_t *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryLongArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_SINT32: + { + int32_t *data = NULL; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntrySlongArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_UINT64: + { + uint64_t *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryLong8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_SINT64: + { + int64_t *data = NULL; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntrySlong8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_FLOAT: + { + float *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryFloatArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_DOUBLE: + { + double *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryDoubleArray(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + case TIFF_SETGET_C32_IFD8: + { + uint64_t *data; + assert(fip->field_readcount == TIFF_VARIABLE2); + assert(fip->field_passcount == 1); + err = TIFFReadDirEntryIfd8Array(tif, dp, &data); + if (err == TIFFReadDirEntryErrOk) + { + if (!EvaluateIFDdatasizeReading(tif, dp)) + { + if (data != 0) + _TIFFfreeExt(tif, data); + return 0; + } + int m; + m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), + data); + if (data != 0) + _TIFFfreeExt(tif, data); + if (!m) + return (0); + } + } + break; + default: + assert(0); /* we should never get here */ + break; + } + if (err != TIFFReadDirEntryErrOk) + { + TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover); + return (0); + } + return (1); +} + +/* + * Fetch a set of offsets or lengths. + * While this routine says "strips", in fact it's also used for tiles. + */ +static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips, + uint64_t **lpp) +{ + static const char module[] = "TIFFFetchStripThing"; + enum TIFFReadDirEntryErr err; + uint64_t *data; + err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips); + if (err != TIFFReadDirEntryErrOk) + { + const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); + TIFFReadDirEntryOutputErr(tif, err, module, + fip ? fip->field_name : "unknown tagname", 0); + return (0); + } + if (dir->tdir_count < (uint64_t)nstrips) + { + uint64_t *resizeddata; + const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); + const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT"); + uint32_t max_nstrips = 1000000; + if (pszMax) + max_nstrips = (uint32_t)atoi(pszMax); + TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module, + fip ? fip->field_name : "unknown tagname", + (nstrips <= max_nstrips)); + + if (nstrips > max_nstrips) + { + _TIFFfreeExt(tif, data); + return (0); + } + + const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t); + if (allocsize > 100 * 1024 * 1024) + { + /* Before allocating a huge amount of memory for corrupted files, + * check if size of requested memory is not greater than file size. + */ + const uint64_t filesize = TIFFGetFileSize(tif); + if (allocsize > filesize) + { + TIFFWarningExtR( + tif, module, + "Requested memory size for StripArray of %" PRIu64 + " is greater than filesize %" PRIu64 + ". Memory not allocated", + allocsize, filesize); + _TIFFfreeExt(tif, data); + return (0); + } + } + resizeddata = (uint64_t *)_TIFFCheckMalloc( + tif, nstrips, sizeof(uint64_t), "for strip array"); + if (resizeddata == 0) + { + _TIFFfreeExt(tif, data); + return (0); + } + if (dir->tdir_count) + _TIFFmemcpy(resizeddata, data, + (uint32_t)dir->tdir_count * sizeof(uint64_t)); + _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0, + (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t)); + _TIFFfreeExt(tif, data); + data = resizeddata; + } + *lpp = data; + return (1); +} + +/* + * Fetch and set the SubjectDistance EXIF tag. + */ +static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir) +{ + static const char module[] = "TIFFFetchSubjectDistance"; + enum TIFFReadDirEntryErr err; + UInt64Aligned_t m; + m.l = 0; + assert(sizeof(double) == 8); + assert(sizeof(uint64_t) == 8); + assert(sizeof(uint32_t) == 4); + if (dir->tdir_count != 1) + err = TIFFReadDirEntryErrCount; + else if (dir->tdir_type != TIFF_RATIONAL) + err = TIFFReadDirEntryErrType; + else + { + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t offset; + offset = *(uint32_t *)(&dir->tdir_offset); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&offset); + err = TIFFReadDirEntryData(tif, offset, 8, m.i); + } + else + { + m.l = dir->tdir_offset.toff_long8; + err = TIFFReadDirEntryErrOk; + } + } + if (err == TIFFReadDirEntryErrOk) + { + double n; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(m.i, 2); + if (m.i[0] == 0) + n = 0.0; + else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0) + /* + * XXX: Numerator 0xFFFFFFFF means that we have infinite + * distance. Indicate that with a negative floating point + * SubjectDistance value. + */ + n = -1.0; + else + n = (double)m.i[0] / (double)m.i[1]; + return (TIFFSetField(tif, dir->tdir_tag, n)); + } + else + { + TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE); + return (0); + } +} + +static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips, + uint64_t stripbytes, + uint32_t rowsperstrip) +{ + TIFFDirectory *td = &tif->tif_dir; + uint64_t bytecount; + uint64_t offset; + uint64_t last_offset; + uint64_t last_bytecount; + uint32_t i; + uint64_t *newcounts; + uint64_t *newoffsets; + + offset = TIFFGetStrileOffset(tif, 0); + last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1); + last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1); + if (last_offset > UINT64_MAX - last_bytecount || + last_offset + last_bytecount < offset) + { + return; + } + bytecount = last_offset + last_bytecount - offset; + + /* Before allocating a huge amount of memory for corrupted files, check if + * size of StripByteCount and StripOffset tags is not greater than + * file size. + */ + const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2; + if (allocsize > 100 * 1024 * 1024) + { + const uint64_t filesize = TIFFGetFileSize(tif); + if (allocsize > filesize) + { + TIFFWarningExtR(tif, "allocChoppedUpStripArrays", + "Requested memory size for StripByteCount and " + "StripOffsets %" PRIu64 + " is greater than filesize %" PRIu64 + ". Memory not allocated", + allocsize, filesize); + return; + } + } + + newcounts = + (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t), + "for chopped \"StripByteCounts\" array"); + newoffsets = (uint64_t *)_TIFFCheckMalloc( + tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array"); + if (newcounts == NULL || newoffsets == NULL) + { + /* + * Unable to allocate new strip information, give up and use + * the original one strip information. + */ + if (newcounts != NULL) + _TIFFfreeExt(tif, newcounts); + if (newoffsets != NULL) + _TIFFfreeExt(tif, newoffsets); + return; + } + + /* + * Fill the strip information arrays with new bytecounts and offsets + * that reflect the broken-up format. + */ + for (i = 0; i < nstrips; i++) + { + if (stripbytes > bytecount) + stripbytes = bytecount; + newcounts[i] = stripbytes; + newoffsets[i] = stripbytes ? offset : 0; + offset += stripbytes; + bytecount -= stripbytes; + } + + /* + * Replace old single strip info with multi-strip info. + */ + td->td_stripsperimage = td->td_nstrips = nstrips; + TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip); + + _TIFFfreeExt(tif, td->td_stripbytecount_p); + _TIFFfreeExt(tif, td->td_stripoffset_p); + td->td_stripbytecount_p = newcounts; + td->td_stripoffset_p = newoffsets; +#ifdef STRIPBYTECOUNTSORTED_UNUSED + td->td_stripbytecountsorted = 1; +#endif + tif->tif_flags |= TIFF_CHOPPEDUPARRAYS; +} + +/* + * Replace a single strip (tile) of uncompressed data by multiple strips + * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for + * dealing with large images or for dealing with machines with a limited + * amount memory. + */ +static void ChopUpSingleUncompressedStrip(TIFF *tif) +{ + register TIFFDirectory *td = &tif->tif_dir; + uint64_t bytecount; + uint64_t offset; + uint32_t rowblock; + uint64_t rowblockbytes; + uint64_t stripbytes; + uint32_t nstrips; + uint32_t rowsperstrip; + + bytecount = TIFFGetStrileByteCount(tif, 0); + /* On a newly created file, just re-opened to be filled, we */ + /* don't want strip chop to trigger as it is going to cause issues */ + /* later ( StripOffsets and StripByteCounts improperly filled) . */ + if (bytecount == 0 && tif->tif_mode != O_RDONLY) + return; + offset = TIFFGetStrileByteCount(tif, 0); + assert(td->td_planarconfig == PLANARCONFIG_CONTIG); + if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif))) + rowblock = td->td_ycbcrsubsampling[1]; + else + rowblock = 1; + rowblockbytes = TIFFVTileSize64(tif, rowblock); + /* + * Make the rows hold at least one scanline, but fill specified amount + * of data if possible. + */ + if (rowblockbytes > STRIP_SIZE_DEFAULT) + { + stripbytes = rowblockbytes; + rowsperstrip = rowblock; + } + else if (rowblockbytes > 0) + { + uint32_t rowblocksperstrip; + rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes); + rowsperstrip = rowblocksperstrip * rowblock; + stripbytes = rowblocksperstrip * rowblockbytes; + } + else + return; + + /* + * never increase the number of rows per strip + */ + if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0) + return; + nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip); + if (nstrips == 0) + return; + + /* If we are going to allocate a lot of memory, make sure that the */ + /* file is as big as needed */ + if (tif->tif_mode == O_RDONLY && nstrips > 1000000 && + (offset >= TIFFGetFileSize(tif) || + stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1))) + { + return; + } + + allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip); +} + +/* + * Replace a file with contiguous strips > 2 GB of uncompressed data by + * multiple smaller strips. This is useful for + * dealing with large images or for dealing with machines with a limited + * amount memory. + */ +static void TryChopUpUncompressedBigTiff(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + uint32_t rowblock; + uint64_t rowblockbytes; + uint32_t i; + uint64_t stripsize; + uint32_t rowblocksperstrip; + uint32_t rowsperstrip; + uint64_t stripbytes; + uint32_t nstrips; + + stripsize = TIFFStripSize64(tif); + + assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG); + assert(tif->tif_dir.td_compression == COMPRESSION_NONE); + assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == + TIFF_STRIPCHOP); + assert(stripsize > 0x7FFFFFFFUL); + + /* On a newly created file, just re-opened to be filled, we */ + /* don't want strip chop to trigger as it is going to cause issues */ + /* later ( StripOffsets and StripByteCounts improperly filled) . */ + if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY) + return; + + if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif))) + rowblock = td->td_ycbcrsubsampling[1]; + else + rowblock = 1; + rowblockbytes = TIFFVStripSize64(tif, rowblock); + if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL) + { + /* In case of file with gigantic width */ + return; + } + + /* Check that the strips are contiguous and of the expected size */ + for (i = 0; i < td->td_nstrips; i++) + { + if (i == td->td_nstrips - 1) + { + if (TIFFGetStrileByteCount(tif, i) < + TIFFVStripSize64(tif, + td->td_imagelength - i * td->td_rowsperstrip)) + { + return; + } + } + else + { + if (TIFFGetStrileByteCount(tif, i) != stripsize) + { + return; + } + if (i > 0 && TIFFGetStrileOffset(tif, i) != + TIFFGetStrileOffset(tif, i - 1) + + TIFFGetStrileByteCount(tif, i - 1)) + { + return; + } + } + } + + /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */ + rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes); + if (rowblocksperstrip == 0) + rowblocksperstrip = 1; + rowsperstrip = rowblocksperstrip * rowblock; + stripbytes = rowblocksperstrip * rowblockbytes; + assert(stripbytes <= 0x7FFFFFFFUL); + + if (rowsperstrip == 0) + return; + nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip); + if (nstrips == 0) + return; + + /* If we are going to allocate a lot of memory, make sure that the */ + /* file is as big as needed */ + if (tif->tif_mode == O_RDONLY && nstrips > 1000000) + { + uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1); + uint64_t filesize = TIFFGetFileSize(tif); + uint64_t last_bytecount = + TIFFGetStrileByteCount(tif, td->td_nstrips - 1); + if (last_offset > filesize || last_bytecount > filesize - last_offset) + { + return; + } + } + + allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip); +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b) +{ + return a + b; +} + +/* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around + * strip/tile of number strile. Also fetch the neighbouring values using a + * 4096 byte page size. + */ +static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent, + int strile, uint64_t *panVals) +{ + static const char module[] = "_TIFFPartialReadStripArray"; +#define IO_CACHE_PAGE_SIZE 4096 + + size_t sizeofval; + const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0; + int sizeofvalint; + uint64_t nBaseOffset; + uint64_t nOffset; + uint64_t nOffsetStartPage; + uint64_t nOffsetEndPage; + tmsize_t nToRead; + tmsize_t nRead; + uint64_t nLastStripOffset; + int iStartBefore; + int i; + const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize; + unsigned char buffer[2 * IO_CACHE_PAGE_SIZE]; + + assert(dirent->tdir_count > 4); + + if (dirent->tdir_type == TIFF_SHORT) + { + sizeofval = sizeof(uint16_t); + } + else if (dirent->tdir_type == TIFF_LONG) + { + sizeofval = sizeof(uint32_t); + } + else if (dirent->tdir_type == TIFF_LONG8) + { + sizeofval = sizeof(uint64_t); + } + else if (dirent->tdir_type == TIFF_SLONG8) + { + /* Non conformant but used by some images as in */ + /* https://github.com/OSGeo/gdal/issues/2165 */ + sizeofval = sizeof(int64_t); + } + else + { + TIFFErrorExtR(tif, module, + "Invalid type for [Strip|Tile][Offset/ByteCount] tag"); + panVals[strile] = 0; + return 0; + } + sizeofvalint = (int)(sizeofval); + + if (tif->tif_flags & TIFF_BIGTIFF) + { + uint64_t offset = dirent->tdir_offset.toff_long8; + if (bSwab) + TIFFSwabLong8(&offset); + nBaseOffset = offset; + } + else + { + uint32_t offset = dirent->tdir_offset.toff_long; + if (bSwab) + TIFFSwabLong(&offset); + nBaseOffset = offset; + } + /* To avoid later unsigned integer overflows */ + if (nBaseOffset > (uint64_t)INT64_MAX) + { + TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d", + strile); + panVals[strile] = 0; + return 0; + } + nOffset = nBaseOffset + sizeofval * strile; + nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE; + nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE; + + if (nOffset + sizeofval > nOffsetEndPage) + nOffsetEndPage += IO_CACHE_PAGE_SIZE; +#undef IO_CACHE_PAGE_SIZE + + nLastStripOffset = nBaseOffset + arraySize * sizeofval; + if (nLastStripOffset < nOffsetEndPage) + nOffsetEndPage = nLastStripOffset; + if (nOffsetStartPage >= nOffsetEndPage) + { + TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d", + strile); + panVals[strile] = 0; + return 0; + } + if (!SeekOK(tif, nOffsetStartPage)) + { + panVals[strile] = 0; + return 0; + } + + nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage); + nRead = TIFFReadFile(tif, buffer, nToRead); + if (nRead < nToRead) + { + TIFFErrorExtR(tif, module, + "Cannot read offset/size for strile around ~%d", strile); + return 0; + } + iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval); + if (strile + iStartBefore < 0) + iStartBefore = -strile; + for (i = iStartBefore; + (uint32_t)(strile + i) < arraySize && + _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <= + nOffsetEndPage; + ++i) + { + if (dirent->tdir_type == TIFF_SHORT) + { + uint16_t val; + memcpy(&val, + buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, + sizeof(val)); + if (bSwab) + TIFFSwabShort(&val); + panVals[strile + i] = val; + } + else if (dirent->tdir_type == TIFF_LONG) + { + uint32_t val; + memcpy(&val, + buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, + sizeof(val)); + if (bSwab) + TIFFSwabLong(&val); + panVals[strile + i] = val; + } + else if (dirent->tdir_type == TIFF_LONG8) + { + uint64_t val; + memcpy(&val, + buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, + sizeof(val)); + if (bSwab) + TIFFSwabLong8(&val); + panVals[strile + i] = val; + } + else /* if( dirent->tdir_type == TIFF_SLONG8 ) */ + { + /* Non conformant data type */ + int64_t val; + memcpy(&val, + buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, + sizeof(val)); + if (bSwab) + TIFFSwabLong8((uint64_t *)&val); + panVals[strile + i] = (uint64_t)val; + } + } + return 1; +} + +static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile, + TIFFDirEntry *dirent, uint64_t **parray) +{ + static const char module[] = "_TIFFFetchStrileValue"; + TIFFDirectory *td = &tif->tif_dir; + if (strile >= dirent->tdir_count) + { + return 0; + } + if (strile >= td->td_stripoffsetbyteallocsize) + { + uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize; + uint32_t nStripArrayAllocNew; + uint64_t nArraySize64; + size_t nArraySize; + uint64_t *offsetArray; + uint64_t *bytecountArray; + + if (strile > 1000000) + { + uint64_t filesize = TIFFGetFileSize(tif); + /* Avoid excessive memory allocation attempt */ + /* For such a big blockid we need at least a TIFF_LONG per strile */ + /* for the offset array. */ + if (strile > filesize / sizeof(uint32_t)) + { + TIFFErrorExtR(tif, module, "File too short"); + return 0; + } + } + + if (td->td_stripoffsetbyteallocsize == 0 && + td->td_nstrips < 1024 * 1024) + { + nStripArrayAllocNew = td->td_nstrips; + } + else + { +#define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b)) + nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U); + if (nStripArrayAllocNew < 0xFFFFFFFFU / 2) + nStripArrayAllocNew *= 2; + nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips); + } + assert(strile < nStripArrayAllocNew); + nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew; + nArraySize = (size_t)(nArraySize64); +#if SIZEOF_SIZE_T == 4 + if (nArraySize != nArraySize64) + { + TIFFErrorExtR(tif, module, + "Cannot allocate strip offset and bytecount arrays"); + return 0; + } +#endif + offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p, + nArraySize)); + bytecountArray = (uint64_t *)(_TIFFreallocExt( + tif, td->td_stripbytecount_p, nArraySize)); + if (offsetArray) + td->td_stripoffset_p = offsetArray; + if (bytecountArray) + td->td_stripbytecount_p = bytecountArray; + if (offsetArray && bytecountArray) + { + td->td_stripoffsetbyteallocsize = nStripArrayAllocNew; + /* Initialize new entries to ~0 / -1 */ + /* coverity[overrun-buffer-arg] */ + memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF, + (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * + sizeof(uint64_t)); + /* coverity[overrun-buffer-arg] */ + memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF, + (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * + sizeof(uint64_t)); + } + else + { + TIFFErrorExtR(tif, module, + "Cannot allocate strip offset and bytecount arrays"); + _TIFFfreeExt(tif, td->td_stripoffset_p); + td->td_stripoffset_p = NULL; + _TIFFfreeExt(tif, td->td_stripbytecount_p); + td->td_stripbytecount_p = NULL; + td->td_stripoffsetbyteallocsize = 0; + } + } + if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize) + return 0; + + if (~((*parray)[strile]) == 0) + { + if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray)) + { + (*parray)[strile] = 0; + return 0; + } + } + + return 1; +} + +static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile, + TIFFDirEntry *dirent, + uint64_t **parray, + int *pbErr) +{ + TIFFDirectory *td = &tif->tif_dir; + if (pbErr) + *pbErr = 0; + if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) && + !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS)) + { + if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD) || + /* If the values may fit in the toff_long/toff_long8 member */ + /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */ + dirent->tdir_count <= 4) + { + if (!_TIFFFillStriles(tif)) + { + if (pbErr) + *pbErr = 1; + /* Do not return, as we want this function to always */ + /* return the same value if called several times with */ + /* the same arguments */ + } + } + else + { + if (!_TIFFFetchStrileValue(tif, strile, dirent, parray)) + { + if (pbErr) + *pbErr = 1; + return 0; + } + } + } + if (*parray == NULL || strile >= td->td_nstrips) + { + if (pbErr) + *pbErr = 1; + return 0; + } + return (*parray)[strile]; +} + +/* Return the value of the TileOffsets/StripOffsets array for the specified + * tile/strile */ +uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile) +{ + return TIFFGetStrileOffsetWithErr(tif, strile, NULL); +} + +/* Return the value of the TileOffsets/StripOffsets array for the specified + * tile/strile */ +uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr) +{ + TIFFDirectory *td = &tif->tif_dir; + return _TIFFGetStrileOffsetOrByteCountValue(tif, strile, + &(td->td_stripoffset_entry), + &(td->td_stripoffset_p), pbErr); +} + +/* Return the value of the TileByteCounts/StripByteCounts array for the + * specified tile/strile */ +uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile) +{ + return TIFFGetStrileByteCountWithErr(tif, strile, NULL); +} + +/* Return the value of the TileByteCounts/StripByteCounts array for the + * specified tile/strile */ +uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr) +{ + TIFFDirectory *td = &tif->tif_dir; + return _TIFFGetStrileOffsetOrByteCountValue( + tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p), + pbErr); +} + +int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); } + +static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount) +{ + register TIFFDirectory *td = &tif->tif_dir; + int return_value = 1; + + /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */ + if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) || + (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0) + return 1; + + if (tif->tif_flags & TIFF_LAZYSTRILELOAD) + { + /* In case of lazy loading, reload completely the arrays */ + _TIFFfreeExt(tif, td->td_stripoffset_p); + _TIFFfreeExt(tif, td->td_stripbytecount_p); + td->td_stripoffset_p = NULL; + td->td_stripbytecount_p = NULL; + td->td_stripoffsetbyteallocsize = 0; + tif->tif_flags &= ~TIFF_LAZYSTRILELOAD; + } + + /* If stripoffset array is already loaded, exit with success */ + if (td->td_stripoffset_p != NULL) + return 1; + + /* If tdir_count was canceled, then we already got there, but in error */ + if (td->td_stripoffset_entry.tdir_count == 0) + return 0; + + if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips, + &td->td_stripoffset_p)) + { + return_value = 0; + } + + if (loadStripByteCount && + !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry), + td->td_nstrips, &td->td_stripbytecount_p)) + { + return_value = 0; + } + + _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry)); + _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry)); + +#ifdef STRIPBYTECOUNTSORTED_UNUSED + if (tif->tif_dir.td_nstrips > 1 && return_value == 1) + { + uint32_t strip; + + tif->tif_dir.td_stripbytecountsorted = 1; + for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) + { + if (tif->tif_dir.td_stripoffset_p[strip - 1] > + tif->tif_dir.td_stripoffset_p[strip]) + { + tif->tif_dir.td_stripbytecountsorted = 0; + break; + } + } + } +#endif + + return return_value; +} diff --git a/cpp/3rd_party/libtiff/tif_dirwrite.c b/cpp/3rd_party/libtiff/tif_dirwrite.c new file mode 100644 index 0000000000..bc5ada0f2b --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_dirwrite.c @@ -0,0 +1,3824 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Directory Write Support Routines. + */ +#include "tiffiop.h" +#include /*--: for Rational2Double */ +#include /*--: for Rational2Double */ + +#ifdef HAVE_IEEEFP +#define TIFFCvtNativeToIEEEFloat(tif, n, fp) +#define TIFFCvtNativeToIEEEDouble(tif, n, dp) +#else +/* If your machine does not support IEEE floating point then you will need to + * add support to tif_machdep.c to convert between the native format and + * IEEE format. */ +extern void TIFFCvtNativeToIEEEFloat(TIFF *tif, uint32_t n, float *fp); +extern void TIFFCvtNativeToIEEEDouble(TIFF *tif, uint32_t n, double *dp); +#endif + +static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone, + uint64_t *pdiroff); + +static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + double *value); + +static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, char *value); +static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint8_t *value); +static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint8_t *value); +static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int8_t *value); +static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t value); +static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint16_t *value); +static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t value); +static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int16_t *value); +static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t value); +static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint32_t *value); +static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int32_t *value); +static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint64_t *value); +static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int64_t *value); +static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + double value); +static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, float *value); +static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, float *value); +static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, float *value); +static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, double *value); +static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint32_t *value); +static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t value); +static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint64_t *value); +static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint64_t *value); +static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir); +static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir); +static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir); + +static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, char *value); +static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + uint8_t *value); +static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint8_t *value); +static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int8_t *value); +static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t value); +static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint16_t *value); +static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int16_t *value); +static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t value); +static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint32_t *value); +static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int32_t *value); +static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint64_t *value); +static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int64_t *value); +static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + double value); +static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + float *value); +static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + float *value); + +/*--: Rational2Double: New functions to support true double-precision for custom + * rational tag types. */ +static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + double *value); +static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + double *value); +static int +TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, double *value); +static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray( + TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, + double *value); +static void DoubleToRational(double value, uint32_t *num, uint32_t *denom); +static void DoubleToSrational(double value, int32_t *num, int32_t *denom); + +static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + float *value); +static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + double *value); +static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, + uint32_t *value); +static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint64_t *value); + +static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t datatype, uint32_t count, + uint32_t datalength, void *data); + +static int TIFFLinkDirectory(TIFF *); + +/* + * Write the contents of the current directory + * to the specified file. This routine doesn't + * handle overwriting a directory with auxiliary + * storage that's been changed. + */ +int TIFFWriteDirectory(TIFF *tif) +{ + return TIFFWriteDirectorySec(tif, TRUE, TRUE, NULL); +} + +/* + * This is an advanced writing function that must be used in a particular + * sequence, and generally together with TIFFForceStrileArrayWriting(), + * to make its intended effect. Its aim is to modify the location + * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file. + * More precisely, when TIFFWriteCheck() will be called, the tag entries for + * those arrays will be written with type = count = offset = 0 as a temporary + * value. + * + * Its effect is only valid for the current directory, and before + * TIFFWriteDirectory() is first called, and will be reset when + * changing directory. + * + * The typical sequence of calls is: + * TIFFOpen() + * [ TIFFCreateDirectory(tif) ] + * Set fields with calls to TIFFSetField(tif, ...) + * TIFFDeferStrileArrayWriting(tif) + * TIFFWriteCheck(tif, ...) + * TIFFWriteDirectory(tif) + * ... potentially create other directories and come back to the above directory + * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file + * + * Returns 1 in case of success, 0 otherwise. + */ +int TIFFDeferStrileArrayWriting(TIFF *tif) +{ + static const char module[] = "TIFFDeferStrileArrayWriting"; + if (tif->tif_mode == O_RDONLY) + { + TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode"); + return 0; + } + if (tif->tif_diroff != 0) + { + TIFFErrorExtR(tif, module, "Directory has already been written"); + return 0; + } + + tif->tif_dir.td_deferstrilearraywriting = TRUE; + return 1; +} + +/* + * Similar to TIFFWriteDirectory(), writes the directory out + * but leaves all data structures in memory so that it can be + * written again. This will make a partially written TIFF file + * readable before it is successfully completed/closed. + */ +int TIFFCheckpointDirectory(TIFF *tif) +{ + int rc; + /* Setup the strips arrays, if they haven't already been. */ + if (tif->tif_dir.td_stripoffset_p == NULL) + (void)TIFFSetupStrips(tif); + rc = TIFFWriteDirectorySec(tif, TRUE, FALSE, NULL); + (void)TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END)); + return rc; +} + +int TIFFWriteCustomDirectory(TIFF *tif, uint64_t *pdiroff) +{ + return TIFFWriteDirectorySec(tif, FALSE, FALSE, pdiroff); +} + +/* + * Similar to TIFFWriteDirectorySec(), but if the directory has already + * been written once, it is relocated to the end of the file, in case it + * has changed in size. Note that this will result in the loss of the + * previously used directory space. + */ + +static int TIFFRewriteDirectorySec(TIFF *tif, int isimage, int imagedone, + uint64_t *pdiroff) +{ + static const char module[] = "TIFFRewriteDirectory"; + + /* We don't need to do anything special if it hasn't been written. */ + if (tif->tif_diroff == 0) + return TIFFWriteDirectory(tif); + + /* + * Find and zero the pointer to this directory, so that TIFFLinkDirectory + * will cause it to be added after this directories current pre-link. + */ + uint64_t torewritediroff = tif->tif_diroff; + + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff) + { + tif->tif_header.classic.tiff_diroff = 0; + tif->tif_diroff = 0; + + TIFFSeekFile(tif, 4, SEEK_SET); + if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff), 4)) + { + TIFFErrorExtR(tif, tif->tif_name, "Error updating TIFF header"); + return (0); + } + } + else if (tif->tif_diroff > 0xFFFFFFFFU) + { + TIFFErrorExtR(tif, module, + "tif->tif_diroff exceeds 32 bit range allowed for " + "Classic TIFF"); + return (0); + } + else + { + uint32_t nextdir; + nextdir = tif->tif_header.classic.tiff_diroff; + while (1) + { + uint16_t dircount; + uint32_t nextnextdir; + + if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, 2)) + { + TIFFErrorExtR(tif, module, + "Error fetching directory count"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount); + (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, SEEK_SET); + if (!ReadOK(tif, &nextnextdir, 4)) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextnextdir); + if (nextnextdir == tif->tif_diroff) + { + uint32_t m; + m = 0; + (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, + SEEK_SET); + if (!WriteOK(tif, &m, 4)) + { + TIFFErrorExtR(tif, module, + "Error writing directory link"); + return (0); + } + tif->tif_diroff = 0; + /* Force a full-traversal to reach the zeroed pointer */ + tif->tif_lastdiroff = 0; + break; + } + nextdir = nextnextdir; + } + } + /* Remove skipped offset from IFD loop directory list. */ + _TIFFRemoveEntryFromDirectoryListByOffset(tif, torewritediroff); + } + else + { + if (tif->tif_header.big.tiff_diroff == tif->tif_diroff) + { + tif->tif_header.big.tiff_diroff = 0; + tif->tif_diroff = 0; + + TIFFSeekFile(tif, 8, SEEK_SET); + if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff), 8)) + { + TIFFErrorExtR(tif, tif->tif_name, "Error updating TIFF header"); + return (0); + } + } + else + { + uint64_t nextdir; + nextdir = tif->tif_header.big.tiff_diroff; + while (1) + { + uint64_t dircount64; + uint16_t dircount; + uint64_t nextnextdir; + + if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount64, 8)) + { + TIFFErrorExtR(tif, module, + "Error fetching directory count"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + if (dircount64 > 0xFFFF) + { + TIFFErrorExtR(tif, module, + "Sanity check on tag count failed, likely " + "corrupt TIFF"); + return (0); + } + dircount = (uint16_t)dircount64; + (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, SEEK_SET); + if (!ReadOK(tif, &nextnextdir, 8)) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&nextnextdir); + if (nextnextdir == tif->tif_diroff) + { + uint64_t m; + m = 0; + (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, + SEEK_SET); + if (!WriteOK(tif, &m, 8)) + { + TIFFErrorExtR(tif, module, + "Error writing directory link"); + return (0); + } + tif->tif_diroff = 0; + /* Force a full-traversal to reach the zeroed pointer */ + tif->tif_lastdiroff = 0; + break; + } + nextdir = nextnextdir; + } + } + /* Remove skipped offset from IFD loop directory list. */ + _TIFFRemoveEntryFromDirectoryListByOffset(tif, torewritediroff); + } + + /* + * Now use TIFFWriteDirectorySec() normally. + */ + return TIFFWriteDirectorySec(tif, isimage, imagedone, pdiroff); +} /*-- TIFFRewriteDirectorySec() --*/ + +/* + * Similar to TIFFWriteDirectory(), but if the directory has already + * been written once, it is relocated to the end of the file, in case it + * has changed in size. Note that this will result in the loss of the + * previously used directory space. + */ +int TIFFRewriteDirectory(TIFF *tif) +{ + return TIFFRewriteDirectorySec(tif, TRUE, TRUE, NULL); +} + +static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone, + uint64_t *pdiroff) +{ + static const char module[] = "TIFFWriteDirectorySec"; + uint32_t ndir; + TIFFDirEntry *dir; + uint32_t dirsize; + void *dirmem; + uint32_t m; + if (tif->tif_mode == O_RDONLY) + return (1); + + _TIFFFillStriles(tif); + + /* + * Clear write state so that subsequent images with + * different characteristics get the right buffers + * setup for them. + */ + if (imagedone) + { + if (tif->tif_flags & TIFF_POSTENCODE) + { + tif->tif_flags &= ~TIFF_POSTENCODE; + if (!(*tif->tif_postencode)(tif)) + { + TIFFErrorExtR(tif, module, + "Error post-encoding before directory write"); + return (0); + } + } + (*tif->tif_close)(tif); /* shutdown encoder */ + /* + * Flush any data that might have been written + * by the compression close+cleanup routines. But + * be careful not to write stuff if we didn't add data + * in the previous steps as the "rawcc" data may well be + * a previously read tile/strip in mixed read/write mode. + */ + if (tif->tif_rawcc > 0 && (tif->tif_flags & TIFF_BEENWRITING) != 0) + { + if (!TIFFFlushData1(tif)) + { + TIFFErrorExtR(tif, module, + "Error flushing data before directory write"); + return (0); + } + } + if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) + { + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_rawdata = NULL; + tif->tif_rawcp = NULL; + tif->tif_rawcc = 0; + tif->tif_rawdatasize = 0; + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = 0; + } + tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP); + } + + if (TIFFFieldSet(tif, FIELD_COMPRESSION) && + (tif->tif_dir.td_compression == COMPRESSION_DEFLATE)) + { + TIFFWarningExtR(tif, module, + "Creating TIFF with legacy Deflate codec identifier, " + "COMPRESSION_ADOBE_DEFLATE is more widely supported"); + } + dir = NULL; + dirmem = NULL; + dirsize = 0; + while (1) + { + /* The first loop only determines "ndir" and uses TIFFLinkDirectory() to + * set the offset at which the IFD is to be written to the file. + * The second loop writes IFD entries to the file. */ + ndir = 0; + if (dir == NULL) + tif->tif_dir.td_dirdatasize_write = 0; + if (isimage) + { + /*-- Step 1: Process named tags for an image with FIELD bits + * associated. --*/ + if (TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) + { + if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir, + TIFFTAG_IMAGEWIDTH, + tif->tif_dir.td_imagewidth)) + goto bad; + if (!TIFFWriteDirectoryTagShortLong( + tif, &ndir, dir, TIFFTAG_IMAGELENGTH, + tif->tif_dir.td_imagelength)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) + { + if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir, + TIFFTAG_TILEWIDTH, + tif->tif_dir.td_tilewidth)) + goto bad; + if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir, + TIFFTAG_TILELENGTH, + tif->tif_dir.td_tilelength)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_RESOLUTION)) + { + if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir, + TIFFTAG_XRESOLUTION, + tif->tif_dir.td_xresolution)) + goto bad; + if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir, + TIFFTAG_YRESOLUTION, + tif->tif_dir.td_yresolution)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_POSITION)) + { + if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir, + TIFFTAG_XPOSITION, + tif->tif_dir.td_xposition)) + goto bad; + if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir, + TIFFTAG_YPOSITION, + tif->tif_dir.td_yposition)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_SUBFILETYPE)) + { + if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir, + TIFFTAG_SUBFILETYPE, + tif->tif_dir.td_subfiletype)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_BITSPERSAMPLE)) + { + if (!TIFFWriteDirectoryTagShortPerSample( + tif, &ndir, dir, TIFFTAG_BITSPERSAMPLE, + tif->tif_dir.td_bitspersample)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_COMPRESSION)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_COMPRESSION, + tif->tif_dir.td_compression)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_PHOTOMETRIC)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_PHOTOMETRIC, + tif->tif_dir.td_photometric)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_THRESHHOLDING)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_THRESHHOLDING, + tif->tif_dir.td_threshholding)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_FILLORDER)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_FILLORDER, + tif->tif_dir.td_fillorder)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_ORIENTATION)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_ORIENTATION, + tif->tif_dir.td_orientation)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) + { + if (!TIFFWriteDirectoryTagShort( + tif, &ndir, dir, TIFFTAG_SAMPLESPERPIXEL, + tif->tif_dir.td_samplesperpixel)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) + { + if (!TIFFWriteDirectoryTagShortLong( + tif, &ndir, dir, TIFFTAG_ROWSPERSTRIP, + tif->tif_dir.td_rowsperstrip)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_MINSAMPLEVALUE)) + { + if (!TIFFWriteDirectoryTagShortPerSample( + tif, &ndir, dir, TIFFTAG_MINSAMPLEVALUE, + tif->tif_dir.td_minsamplevalue)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) + { + if (!TIFFWriteDirectoryTagShortPerSample( + tif, &ndir, dir, TIFFTAG_MAXSAMPLEVALUE, + tif->tif_dir.td_maxsamplevalue)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_PLANARCONFIG)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_PLANARCONFIG, + tif->tif_dir.td_planarconfig)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_RESOLUTIONUNIT)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_RESOLUTIONUNIT, + tif->tif_dir.td_resolutionunit)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_PAGENUMBER)) + { + if (!TIFFWriteDirectoryTagShortArray( + tif, &ndir, dir, TIFFTAG_PAGENUMBER, 2, + &tif->tif_dir.td_pagenumber[0])) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) + { + if (!isTiled(tif)) + { + if (!TIFFWriteDirectoryTagLongLong8Array( + tif, &ndir, dir, TIFFTAG_STRIPBYTECOUNTS, + tif->tif_dir.td_nstrips, + tif->tif_dir.td_stripbytecount_p)) + goto bad; + } + else + { + if (!TIFFWriteDirectoryTagLongLong8Array( + tif, &ndir, dir, TIFFTAG_TILEBYTECOUNTS, + tif->tif_dir.td_nstrips, + tif->tif_dir.td_stripbytecount_p)) + goto bad; + } + } + if (TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) + { + if (!isTiled(tif)) + { + /* td_stripoffset_p might be NULL in an odd OJPEG case. See + * tif_dirread.c around line 3634. + * XXX: OJPEG hack. + * If a) compression is OJPEG, b) it's not a tiled TIFF, + * and c) the number of strips is 1, + * then we tolerate the absence of stripoffsets tag, + * because, presumably, all required data is in the + * JpegInterchangeFormat stream. + * We can get here when using tiffset on such a file. + * See http://bugzilla.maptools.org/show_bug.cgi?id=2500 + */ + if (tif->tif_dir.td_stripoffset_p != NULL && + !TIFFWriteDirectoryTagLongLong8Array( + tif, &ndir, dir, TIFFTAG_STRIPOFFSETS, + tif->tif_dir.td_nstrips, + tif->tif_dir.td_stripoffset_p)) + goto bad; + } + else + { + if (!TIFFWriteDirectoryTagLongLong8Array( + tif, &ndir, dir, TIFFTAG_TILEOFFSETS, + tif->tif_dir.td_nstrips, + tif->tif_dir.td_stripoffset_p)) + goto bad; + } + } + if (TIFFFieldSet(tif, FIELD_COLORMAP)) + { + if (!TIFFWriteDirectoryTagColormap(tif, &ndir, dir)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_EXTRASAMPLES)) + { + if (tif->tif_dir.td_extrasamples) + { + uint16_t na; + uint16_t *nb; + TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &na, &nb); + if (!TIFFWriteDirectoryTagShortArray( + tif, &ndir, dir, TIFFTAG_EXTRASAMPLES, na, nb)) + goto bad; + } + } + if (TIFFFieldSet(tif, FIELD_SAMPLEFORMAT)) + { + if (!TIFFWriteDirectoryTagShortPerSample( + tif, &ndir, dir, TIFFTAG_SAMPLEFORMAT, + tif->tif_dir.td_sampleformat)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_SMINSAMPLEVALUE)) + { + if (!TIFFWriteDirectoryTagSampleformatArray( + tif, &ndir, dir, TIFFTAG_SMINSAMPLEVALUE, + tif->tif_dir.td_samplesperpixel, + tif->tif_dir.td_sminsamplevalue)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_SMAXSAMPLEVALUE)) + { + if (!TIFFWriteDirectoryTagSampleformatArray( + tif, &ndir, dir, TIFFTAG_SMAXSAMPLEVALUE, + tif->tif_dir.td_samplesperpixel, + tif->tif_dir.td_smaxsamplevalue)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_IMAGEDEPTH)) + { + if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir, + TIFFTAG_IMAGEDEPTH, + tif->tif_dir.td_imagedepth)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_TILEDEPTH)) + { + if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir, + TIFFTAG_TILEDEPTH, + tif->tif_dir.td_tiledepth)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_HALFTONEHINTS)) + { + if (!TIFFWriteDirectoryTagShortArray( + tif, &ndir, dir, TIFFTAG_HALFTONEHINTS, 2, + &tif->tif_dir.td_halftonehints[0])) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_YCBCRSUBSAMPLING)) + { + if (!TIFFWriteDirectoryTagShortArray( + tif, &ndir, dir, TIFFTAG_YCBCRSUBSAMPLING, 2, + &tif->tif_dir.td_ycbcrsubsampling[0])) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_YCBCRPOSITIONING)) + { + if (!TIFFWriteDirectoryTagShort( + tif, &ndir, dir, TIFFTAG_YCBCRPOSITIONING, + tif->tif_dir.td_ycbcrpositioning)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_REFBLACKWHITE)) + { + if (!TIFFWriteDirectoryTagRationalArray( + tif, &ndir, dir, TIFFTAG_REFERENCEBLACKWHITE, 6, + tif->tif_dir.td_refblackwhite)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_TRANSFERFUNCTION)) + { + if (!TIFFWriteDirectoryTagTransferfunction(tif, &ndir, dir)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_INKNAMES)) + { + if (!TIFFWriteDirectoryTagAscii( + tif, &ndir, dir, TIFFTAG_INKNAMES, + tif->tif_dir.td_inknameslen, tif->tif_dir.td_inknames)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS)) + { + if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, + TIFFTAG_NUMBEROFINKS, + tif->tif_dir.td_numberofinks)) + goto bad; + } + if (TIFFFieldSet(tif, FIELD_SUBIFD)) + { + if (!TIFFWriteDirectoryTagSubifd(tif, &ndir, dir)) + goto bad; + } + /*-- Step 2: Process named tags for an image with FIELD bits + added by a codec. + Attention: There is only code for some field_types, + which are actually used by current codecs. --*/ + { + uint32_t n; + for (n = 0; n < tif->tif_nfields; n++) + { + const TIFFField *o; + o = tif->tif_fields[n]; + if ((o->field_bit >= FIELD_CODEC) && + (TIFFFieldSet(tif, o->field_bit))) + { + switch (o->set_get_field_type) + { + case TIFF_SETGET_ASCII: + { + uint32_t pa; + char *pb; + assert(o->field_type == TIFF_ASCII); + assert(o->field_readcount == TIFF_VARIABLE); + assert(o->field_passcount == 0); + TIFFGetField(tif, o->field_tag, &pb); + pa = (uint32_t)(strlen(pb) + 1); + if (!TIFFWriteDirectoryTagAscii( + tif, &ndir, dir, (uint16_t)o->field_tag, + pa, pb)) + goto bad; + } + break; + case TIFF_SETGET_UINT16: + { + uint16_t p; + assert(o->field_type == TIFF_SHORT); + assert(o->field_readcount == 1); + assert(o->field_passcount == 0); + TIFFGetField(tif, o->field_tag, &p); + if (!TIFFWriteDirectoryTagShort( + tif, &ndir, dir, (uint16_t)o->field_tag, + p)) + goto bad; + } + break; + case TIFF_SETGET_UINT32: + { + uint32_t p; + assert(o->field_type == TIFF_LONG); + assert(o->field_readcount == 1); + assert(o->field_passcount == 0); + TIFFGetField(tif, o->field_tag, &p); + if (!TIFFWriteDirectoryTagLong( + tif, &ndir, dir, (uint16_t)o->field_tag, + p)) + goto bad; + } + break; + case TIFF_SETGET_C32_UINT8: + { + uint32_t pa; + void *pb; + assert(o->field_type == TIFF_UNDEFINED); + assert(o->field_readcount == TIFF_VARIABLE2); + assert(o->field_passcount == 1); + TIFFGetField(tif, o->field_tag, &pa, &pb); + if (!TIFFWriteDirectoryTagUndefinedArray( + tif, &ndir, dir, (uint16_t)o->field_tag, + pa, pb)) + goto bad; + } + break; + default: + TIFFErrorExtR( + tif, module, + "Cannot write tag %" PRIu32 " (%s)", + TIFFFieldTag(o), + o->field_name ? o->field_name : "unknown"); + goto bad; + } + } + } + } + } + /*-- Step 3: Process custom tags without FIELD bit for an image + * or for custom IFDs (e.g. EXIF) with !isimage. --*/ + for (m = 0; m < (uint32_t)(tif->tif_dir.td_customValueCount); m++) + { + uint16_t tag = + (uint16_t)tif->tif_dir.td_customValues[m].info->field_tag; + uint32_t count = tif->tif_dir.td_customValues[m].count; + switch (tif->tif_dir.td_customValues[m].info->field_type) + { + case TIFF_ASCII: + if (!TIFFWriteDirectoryTagAscii( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_UNDEFINED: + if (!TIFFWriteDirectoryTagUndefinedArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_BYTE: + if (!TIFFWriteDirectoryTagByteArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_SBYTE: + if (!TIFFWriteDirectoryTagSbyteArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_SHORT: + if (!TIFFWriteDirectoryTagShortArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_SSHORT: + if (!TIFFWriteDirectoryTagSshortArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_LONG: + if (!TIFFWriteDirectoryTagLongArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_SLONG: + if (!TIFFWriteDirectoryTagSlongArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_LONG8: + if (!TIFFWriteDirectoryTagLong8Array( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_SLONG8: + if (!TIFFWriteDirectoryTagSlong8Array( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_RATIONAL: + { + /*-- Rational2Double: For Rationals evaluate + * "set_get_field_type" to determine internal storage size. + */ + int tv_size; + tv_size = TIFFFieldSetGetSize( + tif->tif_dir.td_customValues[m].info); + if (tv_size == 8) + { + if (!TIFFWriteDirectoryTagRationalDoubleArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + } + else + { + /*-- default should be tv_size == 4 */ + if (!TIFFWriteDirectoryTagRationalArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + /*-- ToDo: After Testing, this should be removed and + * tv_size==4 should be set as default. */ + if (tv_size != 4) + { + TIFFErrorExtR( + tif, "TIFFLib: _TIFFWriteDirectorySec()", + "Rational2Double: .set_get_field_type is " + "not 4 but %d", + tv_size); + } + } + } + break; + case TIFF_SRATIONAL: + { + /*-- Rational2Double: For Rationals evaluate + * "set_get_field_type" to determine internal storage size. + */ + int tv_size; + tv_size = TIFFFieldSetGetSize( + tif->tif_dir.td_customValues[m].info); + if (tv_size == 8) + { + if (!TIFFWriteDirectoryTagSrationalDoubleArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + } + else + { + /*-- default should be tv_size == 4 */ + if (!TIFFWriteDirectoryTagSrationalArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + /*-- ToDo: After Testing, this should be removed and + * tv_size==4 should be set as default. */ + if (tv_size != 4) + { + TIFFErrorExtR( + tif, "TIFFLib: _TIFFWriteDirectorySec()", + "Rational2Double: .set_get_field_type is " + "not 4 but %d", + tv_size); + } + } + } + break; + case TIFF_FLOAT: + if (!TIFFWriteDirectoryTagFloatArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_DOUBLE: + if (!TIFFWriteDirectoryTagDoubleArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_IFD: + if (!TIFFWriteDirectoryTagIfdArray( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + case TIFF_IFD8: + if (!TIFFWriteDirectoryTagIfdIfd8Array( + tif, &ndir, dir, tag, count, + tif->tif_dir.td_customValues[m].value)) + goto bad; + break; + default: + assert(0); /* we should never get here */ + break; + } + } + /* "break" if IFD has been written above in second pass.*/ + if (dir != NULL) + break; + + /* Evaluate IFD data size: Finally, add the size of the IFD tag entries + * themselves. */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + tif->tif_dir.td_dirdatasize_write += 2 + ndir * 12 + 4; + else + tif->tif_dir.td_dirdatasize_write += 8 + ndir * 20 + 8; + + /* Setup a new directory within first pass. */ + dir = _TIFFmallocExt(tif, ndir * sizeof(TIFFDirEntry)); + if (dir == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + goto bad; + } + if (isimage) + { + /* Check, weather the IFD to be written is new or an already written + * IFD can be overwritten or needs to be re-written to a different + * location in the file because the IFD is extended with additional + * tags or the IFD data size is increased. + * - tif_diroff == 0, if a new directory has to be linked. + * - tif_diroff != 0, IFD has been re-read from file and will be + * overwritten or re-written. + */ + if (tif->tif_diroff == 0) + { + if (!TIFFLinkDirectory(tif)) + goto bad; + } + else if (tif->tif_dir.td_dirdatasize_write > + tif->tif_dir.td_dirdatasize_read) + { + if (dir != NULL) + { + _TIFFfreeExt(tif, dir); + dir = NULL; + } + if (!TIFFRewriteDirectorySec(tif, isimage, imagedone, pdiroff)) + goto bad; + return (1); + } + } + else + { + /* For !isimage, which means custom-IFD like EXIFIFD or + * checkpointing an IFD, determine whether to overwrite or append at + * the end of the file. + */ + if (!((tif->tif_dir.td_dirdatasize_read > 0) && + (tif->tif_dir.td_dirdatasize_write <= + tif->tif_dir.td_dirdatasize_read))) + { + /* Append at end of file and increment to an even offset. */ + tif->tif_diroff = + (TIFFSeekFile(tif, 0, SEEK_END) + 1) & (~((toff_t)1)); + } + } + /* Return IFD offset */ + if (pdiroff != NULL) + *pdiroff = tif->tif_diroff; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + dirsize = 2 + ndir * 12 + 4; + else + dirsize = 8 + ndir * 20 + 8; + /* Append IFD data stright after the IFD tag entries. + * Data that does not fit into an IFD tag entry is written to the file + * in the second pass of the while loop. That offset is stored in "dir". + */ + tif->tif_dataoff = tif->tif_diroff + dirsize; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + tif->tif_dataoff = (uint32_t)tif->tif_dataoff; + if ((tif->tif_dataoff < tif->tif_diroff) || + (tif->tif_dataoff < (uint64_t)dirsize)) + { + TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); + goto bad; + } + if (tif->tif_dataoff & 1) + tif->tif_dataoff++; + } /* while() */ + if (isimage) + { + /* For SubIFDs remember offset of SubIFD tag within main IFD. + * However, might be already done in TIFFWriteDirectoryTagSubifd() if + * there are more than one SubIFD. */ + if (TIFFFieldSet(tif, FIELD_SUBIFD) && (tif->tif_subifdoff == 0)) + { + uint32_t na; + TIFFDirEntry *nb; + for (na = 0, nb = dir;; na++, nb++) + { + if (na == ndir) + { + TIFFErrorExtR(tif, module, "Cannot find SubIFD tag"); + goto bad; + } + if (nb->tdir_tag == TIFFTAG_SUBIFD) + break; + } + if (!(tif->tif_flags & TIFF_BIGTIFF)) + tif->tif_subifdoff = tif->tif_diroff + 2 + na * 12 + 8; + else + tif->tif_subifdoff = tif->tif_diroff + 8 + na * 20 + 12; + } + } + /* Copy/swab IFD entries from "dir" into "dirmem", + * which is then written to file. */ + dirmem = _TIFFmallocExt(tif, dirsize); + if (dirmem == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + goto bad; + } + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint8_t *n; + uint32_t nTmp; + TIFFDirEntry *o; + n = dirmem; + *(uint16_t *)n = (uint16_t)ndir; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)n); + n += 2; + o = dir; + for (m = 0; m < ndir; m++) + { + *(uint16_t *)n = o->tdir_tag; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)n); + n += 2; + *(uint16_t *)n = o->tdir_type; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)n); + n += 2; + nTmp = (uint32_t)o->tdir_count; + _TIFFmemcpy(n, &nTmp, 4); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)n); + n += 4; + /* This is correct. The data has been */ + /* swabbed previously in TIFFWriteDirectoryTagData */ + _TIFFmemcpy(n, &o->tdir_offset, 4); + n += 4; + o++; + } + nTmp = (uint32_t)tif->tif_nextdiroff; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nTmp); + _TIFFmemcpy(n, &nTmp, 4); + } + else + { + uint8_t *n; + TIFFDirEntry *o; + n = dirmem; + *(uint64_t *)n = ndir; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)n); + n += 8; + o = dir; + for (m = 0; m < ndir; m++) + { + *(uint16_t *)n = o->tdir_tag; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)n); + n += 2; + *(uint16_t *)n = o->tdir_type; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)n); + n += 2; + _TIFFmemcpy(n, &o->tdir_count, 8); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)n); + n += 8; + _TIFFmemcpy(n, &o->tdir_offset, 8); + n += 8; + o++; + } + _TIFFmemcpy(n, &tif->tif_nextdiroff, 8); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)n); + } + _TIFFfreeExt(tif, dir); + dir = NULL; + if (!SeekOK(tif, tif->tif_diroff)) + { + TIFFErrorExtR(tif, module, + "IO error writing directory at seek to offset"); + goto bad; + } + if (!WriteOK(tif, dirmem, (tmsize_t)dirsize)) + { + TIFFErrorExtR(tif, module, "IO error writing directory"); + goto bad; + } + _TIFFfreeExt(tif, dirmem); + + /* Increment tif_curdir if IFD wasn't already written to file and no error + * occurred during IFD writing above. */ + if (isimage && !tif->tif_dir.td_iswrittentofile) + { + if (!((tif->tif_flags & TIFF_INSUBIFD) && + !(TIFFFieldSet(tif, FIELD_SUBIFD)))) + { + /*-- Normal main-IFD case --*/ + if (tif->tif_curdircount != TIFF_NON_EXISTENT_DIR_NUMBER) + { + tif->tif_curdir = tif->tif_curdircount; + } + else + { + /*ToDo SU: NEW_IFD_CURDIR_INCREMENTING: Delete this + * unexpected case after some testing time. */ + /* Attention: tif->tif_curdircount is already set within + * TIFFNumberOfDirectories() */ + tif->tif_curdircount = TIFFNumberOfDirectories(tif); + tif->tif_curdir = tif->tif_curdircount; + TIFFErrorExtR( + tif, module, + "tif_curdircount is TIFF_NON_EXISTENT_DIR_NUMBER, " + "not expected !! Line %d", + __LINE__); + goto bad; + } + } + else + { + /*-- SubIFD case -- */ + /* tif_curdir is always set to 0 for all SubIFDs. */ + tif->tif_curdir = 0; + } + } + /* Increment tif_curdircount only if main-IFD of an image was not already + * present on file. */ + /* Check in combination with (... && !(TIFFFieldSet(tif, FIELD_SUBIFD))) + * is necessary here because TIFF_INSUBIFD was already set above for the + * next SubIFD when this main-IFD (with FIELD_SUBIFD) is currently being + * written. */ + if (isimage && !tif->tif_dir.td_iswrittentofile && + !((tif->tif_flags & TIFF_INSUBIFD) && + !(TIFFFieldSet(tif, FIELD_SUBIFD)))) + tif->tif_curdircount++; + + tif->tif_dir.td_iswrittentofile = TRUE; + + /* Reset SubIFD writing stage after last SubIFD has been written. */ + if (imagedone && (tif->tif_flags & TIFF_INSUBIFD) && tif->tif_nsubifd == 0) + tif->tif_flags &= ~TIFF_INSUBIFD; + + /* Add or update this directory to the IFD list. */ + if (!_TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, tif->tif_diroff)) + { + TIFFErrorExtR(tif, module, + "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64 + ") might cause an IFD loop", + tif->tif_curdir, tif->tif_diroff, tif->tif_diroff); + } + + if (imagedone) + { + TIFFFreeDirectory(tif); + tif->tif_flags &= ~TIFF_DIRTYDIRECT; + tif->tif_flags &= ~TIFF_DIRTYSTRIP; + /* Reset directory-related state for subsequent directories. */ + TIFFCreateDirectory(tif); + } + else + { + /* IFD is only checkpointed to file (or a custom IFD like EXIF is + * written), thus set IFD data size written to file. */ + tif->tif_dir.td_dirdatasize_read = tif->tif_dir.td_dirdatasize_write; + } + return (1); +bad: + if (dir != NULL) + _TIFFfreeExt(tif, dir); + if (dirmem != NULL) + _TIFFfreeExt(tif, dirmem); + return (0); +} + +static int8_t TIFFClampDoubleToInt8(double val) +{ + if (val > 127) + return 127; + if (val < -128 || val != val) + return -128; + return (int8_t)val; +} + +static int16_t TIFFClampDoubleToInt16(double val) +{ + if (val > 32767) + return 32767; + if (val < -32768 || val != val) + return -32768; + return (int16_t)val; +} + +static int32_t TIFFClampDoubleToInt32(double val) +{ + if (val > 0x7FFFFFFF) + return 0x7FFFFFFF; + if (val < -0x7FFFFFFF - 1 || val != val) + return -0x7FFFFFFF - 1; + return (int32_t)val; +} + +static uint8_t TIFFClampDoubleToUInt8(double val) +{ + if (val < 0) + return 0; + if (val > 255 || val != val) + return 255; + return (uint8_t)val; +} + +static uint16_t TIFFClampDoubleToUInt16(double val) +{ + if (val < 0) + return 0; + if (val > 65535 || val != val) + return 65535; + return (uint16_t)val; +} + +static uint32_t TIFFClampDoubleToUInt32(double val) +{ + if (val < 0) + return 0; + if (val > 0xFFFFFFFFU || val != val) + return 0xFFFFFFFFU; + return (uint32_t)val; +} + +static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + double *value) +{ + static const char module[] = "TIFFWriteDirectoryTagSampleformatArray"; + void *conv; + uint32_t i; + int ok; + conv = _TIFFmallocExt(tif, count * sizeof(double)); + if (conv == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + + switch (tif->tif_dir.td_sampleformat) + { + case SAMPLEFORMAT_IEEEFP: + if (tif->tif_dir.td_bitspersample <= 32) + { + for (i = 0; i < count; ++i) + ((float *)conv)[i] = _TIFFClampDoubleToFloat(value[i]); + ok = TIFFWriteDirectoryTagFloatArray(tif, ndir, dir, tag, count, + (float *)conv); + } + else + { + ok = TIFFWriteDirectoryTagDoubleArray(tif, ndir, dir, tag, + count, value); + } + break; + case SAMPLEFORMAT_INT: + if (tif->tif_dir.td_bitspersample <= 8) + { + for (i = 0; i < count; ++i) + ((int8_t *)conv)[i] = TIFFClampDoubleToInt8(value[i]); + ok = TIFFWriteDirectoryTagSbyteArray(tif, ndir, dir, tag, count, + (int8_t *)conv); + } + else if (tif->tif_dir.td_bitspersample <= 16) + { + for (i = 0; i < count; ++i) + ((int16_t *)conv)[i] = TIFFClampDoubleToInt16(value[i]); + ok = TIFFWriteDirectoryTagSshortArray(tif, ndir, dir, tag, + count, (int16_t *)conv); + } + else + { + for (i = 0; i < count; ++i) + ((int32_t *)conv)[i] = TIFFClampDoubleToInt32(value[i]); + ok = TIFFWriteDirectoryTagSlongArray(tif, ndir, dir, tag, count, + (int32_t *)conv); + } + break; + case SAMPLEFORMAT_UINT: + if (tif->tif_dir.td_bitspersample <= 8) + { + for (i = 0; i < count; ++i) + ((uint8_t *)conv)[i] = TIFFClampDoubleToUInt8(value[i]); + ok = TIFFWriteDirectoryTagByteArray(tif, ndir, dir, tag, count, + (uint8_t *)conv); + } + else if (tif->tif_dir.td_bitspersample <= 16) + { + for (i = 0; i < count; ++i) + ((uint16_t *)conv)[i] = TIFFClampDoubleToUInt16(value[i]); + ok = TIFFWriteDirectoryTagShortArray(tif, ndir, dir, tag, count, + (uint16_t *)conv); + } + else + { + for (i = 0; i < count; ++i) + ((uint32_t *)conv)[i] = TIFFClampDoubleToUInt32(value[i]); + ok = TIFFWriteDirectoryTagLongArray(tif, ndir, dir, tag, count, + (uint32_t *)conv); + } + break; + default: + ok = 0; + } + + _TIFFfreeExt(tif, conv); + return (ok); +} + +static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, char *value) +{ + return ( + TIFFWriteDirectoryTagCheckedAscii(tif, ndir, dir, tag, count, value)); +} + +static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint8_t *value) +{ + return (TIFFWriteDirectoryTagCheckedUndefinedArray(tif, ndir, dir, tag, + count, value)); +} + +static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint8_t *value) +{ + return (TIFFWriteDirectoryTagCheckedByteArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int8_t *value) +{ + return (TIFFWriteDirectoryTagCheckedSbyteArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t value) +{ + return (TIFFWriteDirectoryTagCheckedShort(tif, ndir, dir, tag, value)); +} + +static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint16_t *value) +{ + return (TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t value) +{ + static const char module[] = "TIFFWriteDirectoryTagShortPerSample"; + uint16_t *m; + uint16_t *na; + uint16_t nb; + int o; + if (dir == NULL) + { + /* only evaluate IFD data size and inc. ndir */ + return (TIFFWriteDirectoryTagCheckedShortArray( + tif, ndir, dir, tag, tif->tif_dir.td_samplesperpixel, NULL)); + } + m = _TIFFmallocExt(tif, tif->tif_dir.td_samplesperpixel * sizeof(uint16_t)); + if (m == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + for (na = m, nb = 0; nb < tif->tif_dir.td_samplesperpixel; na++, nb++) + *na = value; + o = TIFFWriteDirectoryTagCheckedShortArray( + tif, ndir, dir, tag, tif->tif_dir.td_samplesperpixel, m); + _TIFFfreeExt(tif, m); + return (o); +} + +static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int16_t *value) +{ + return (TIFFWriteDirectoryTagCheckedSshortArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t value) +{ + return (TIFFWriteDirectoryTagCheckedLong(tif, ndir, dir, tag, value)); +} + +static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint32_t *value) +{ + return (TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int32_t *value) +{ + return (TIFFWriteDirectoryTagCheckedSlongArray(tif, ndir, dir, tag, count, + value)); +} + +/************************************************************************/ +/* TIFFWriteDirectoryTagLong8Array() */ +/* */ +/* Write either Long8 or Long array depending on file type. */ +/************************************************************************/ +static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint64_t *value) +{ + static const char module[] = "TIFFWriteDirectoryTagLong8Array"; + uint64_t *ma; + uint32_t mb; + uint32_t *p; + uint32_t *q; + int o; + + /* is this just a counting pass? */ + if (dir == NULL) + { + /* only evaluate IFD data size and inc. ndir */ + return (TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag, + count, value)); + } + + /* We always write Long8 for BigTIFF, no checking needed. */ + if (tif->tif_flags & TIFF_BIGTIFF) + return (TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag, + count, value)); + + /* + ** For classic tiff we want to verify everything is in range for long + ** and convert to long format. + */ + p = _TIFFmallocExt(tif, count * sizeof(uint32_t)); + if (p == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + + for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++) + { + if (*ma > 0xFFFFFFFF) + { + TIFFErrorExtR(tif, module, + "Attempt to write unsigned long value %" PRIu64 + " larger than 0xFFFFFFFF for tag %d in Classic TIFF " + "file. TIFF file writing aborted", + *ma, tag); + _TIFFfreeExt(tif, p); + return (0); + } + *q = (uint32_t)(*ma); + } + + o = TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count, p); + _TIFFfreeExt(tif, p); + + return (o); +} + +/************************************************************************/ +/* TIFFWriteDirectoryTagSlong8Array() */ +/* */ +/* Write either SLong8 or SLong array depending on file type. */ +/************************************************************************/ +static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, int64_t *value) +{ + static const char module[] = "TIFFWriteDirectoryTagSlong8Array"; + int64_t *ma; + uint32_t mb; + int32_t *p; + int32_t *q; + int o; + + /* is this just a counting pass? */ + if (dir == NULL) + { + /* only evaluate IFD data size and inc. ndir */ + return (TIFFWriteDirectoryTagCheckedSlong8Array(tif, ndir, dir, tag, + count, value)); + } + /* We always write SLong8 for BigTIFF, no checking needed. */ + if (tif->tif_flags & TIFF_BIGTIFF) + return (TIFFWriteDirectoryTagCheckedSlong8Array(tif, ndir, dir, tag, + count, value)); + + /* + ** For classic tiff we want to verify everything is in range for signed-long + ** and convert to signed-long format. + */ + p = _TIFFmallocExt(tif, count * sizeof(uint32_t)); + if (p == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + + for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++) + { + if (*ma > (2147483647)) + { + TIFFErrorExtR(tif, module, + "Attempt to write signed long value %" PRIi64 + " larger than 0x7FFFFFFF (2147483647) for tag %d in " + "Classic TIFF file. TIFF writing to file aborted", + *ma, tag); + _TIFFfreeExt(tif, p); + return (0); + } + else if (*ma < (-2147483647 - 1)) + { + TIFFErrorExtR(tif, module, + "Attempt to write signed long value %" PRIi64 + " smaller than 0x80000000 (-2147483648) for tag %d " + "in Classic TIFF file. TIFF writing to file aborted", + *ma, tag); + _TIFFfreeExt(tif, p); + return (0); + } + *q = (int32_t)(*ma); + } + + o = TIFFWriteDirectoryTagCheckedSlongArray(tif, ndir, dir, tag, count, p); + _TIFFfreeExt(tif, p); + + return (o); +} + +static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + double value) +{ + return (TIFFWriteDirectoryTagCheckedRational(tif, ndir, dir, tag, value)); +} + +static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, float *value) +{ + return (TIFFWriteDirectoryTagCheckedRationalArray(tif, ndir, dir, tag, + count, value)); +} + +static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, float *value) +{ + return (TIFFWriteDirectoryTagCheckedSrationalArray(tif, ndir, dir, tag, + count, value)); +} + +/*-- Rational2Double: additional write functions */ +static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + double *value) +{ + return (TIFFWriteDirectoryTagCheckedRationalDoubleArray(tif, ndir, dir, tag, + count, value)); +} + +static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + double *value) +{ + return (TIFFWriteDirectoryTagCheckedSrationalDoubleArray( + tif, ndir, dir, tag, count, value)); +} + +static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, float *value) +{ + return (TIFFWriteDirectoryTagCheckedFloatArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, double *value) +{ + return (TIFFWriteDirectoryTagCheckedDoubleArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint32_t *value) +{ + return (TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, tag, count, + value)); +} + +static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t value) +{ + if (value <= 0xFFFF) + return (TIFFWriteDirectoryTagCheckedShort(tif, ndir, dir, tag, + (uint16_t)value)); + else + return (TIFFWriteDirectoryTagCheckedLong(tif, ndir, dir, tag, value)); +} + +static int _WriteAsType(TIFF *tif, uint64_t strile_size, + uint64_t uncompressed_threshold) +{ + const uint16_t compression = tif->tif_dir.td_compression; + if (compression == COMPRESSION_NONE) + { + return strile_size > uncompressed_threshold; + } + else if (compression == COMPRESSION_JPEG || + compression == COMPRESSION_LZW || + compression == COMPRESSION_ADOBE_DEFLATE || + compression == COMPRESSION_DEFLATE || + compression == COMPRESSION_LZMA || + compression == COMPRESSION_LERC || + compression == COMPRESSION_ZSTD || + compression == COMPRESSION_WEBP || compression == COMPRESSION_JXL) + { + /* For a few select compression types, we assume that in the worst */ + /* case the compressed size will be 10 times the uncompressed size. */ + /* This is overly pessismistic ! */ + return strile_size >= uncompressed_threshold / 10; + } + return 1; +} + +static int WriteAsLong8(TIFF *tif, uint64_t strile_size) +{ + return _WriteAsType(tif, strile_size, 0xFFFFFFFFU); +} + +static int WriteAsLong4(TIFF *tif, uint64_t strile_size) +{ + return _WriteAsType(tif, strile_size, 0xFFFFU); +} + +/************************************************************************/ +/* TIFFWriteDirectoryTagLongLong8Array() */ +/* */ +/* Write out LONG8 array and write a SHORT/LONG/LONG8 depending */ +/* on strile size and Classic/BigTIFF mode. */ +/************************************************************************/ + +static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint64_t *value) +{ + static const char module[] = "TIFFWriteDirectoryTagLongLong8Array"; + int o; + int write_aslong4; + + if (tif->tif_dir.td_deferstrilearraywriting) + { + if (dir == NULL) + { + /* This is just a counting pass to count IFD entries. + * For deferstrilearraywriting no extra bytes will be written + * into IFD space. */ + (*ndir)++; + return 1; + } + return TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_NOTYPE, 0, 0, + NULL); + } + + if (tif->tif_flags & TIFF_BIGTIFF) + { + int write_aslong8 = 1; + /* In the case of ByteCounts array, we may be able to write them on LONG + * if the strip/tilesize is not too big. Also do that for count > 1 in + * the case someone would want to create a single-strip file with a + * growing height, in which case using LONG8 will be safer. */ + if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS) + { + write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif)); + } + else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS) + { + write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif)); + } + if (write_aslong8) + { + return TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag, + count, value); + } + } + + write_aslong4 = 1; + if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS) + { + write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif)); + } + else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS) + { + write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif)); + } + if (write_aslong4) + { + /* + ** For classic tiff we want to verify everything is in range for LONG + ** and convert to long format. + */ + + uint32_t *p = _TIFFmallocExt(tif, count * sizeof(uint32_t)); + uint32_t *q; + uint64_t *ma; + uint32_t mb; + + if (p == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + + for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++) + { + if (*ma > 0xFFFFFFFF) + { + TIFFErrorExtR(tif, module, + "Attempt to write value larger than 0xFFFFFFFF " + "in LONG array."); + _TIFFfreeExt(tif, p); + return (0); + } + *q = (uint32_t)(*ma); + } + + o = TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count, + p); + _TIFFfreeExt(tif, p); + } + else + { + uint16_t *p = _TIFFmallocExt(tif, count * sizeof(uint16_t)); + uint16_t *q; + uint64_t *ma; + uint32_t mb; + + if (p == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + + for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++) + { + if (*ma > 0xFFFF) + { + /* Should not happen normally given the check we did before */ + TIFFErrorExtR(tif, module, + "Attempt to write value larger than 0xFFFF in " + "SHORT array."); + _TIFFfreeExt(tif, p); + return (0); + } + *q = (uint16_t)(*ma); + } + + o = TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, tag, count, + p); + _TIFFfreeExt(tif, p); + } + + return (o); +} + +/************************************************************************/ +/* TIFFWriteDirectoryTagIfdIfd8Array() */ +/* */ +/* Write either IFD8 or IFD array depending on file type. */ +/************************************************************************/ + +static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint64_t *value) +{ + static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array"; + uint64_t *ma; + uint32_t mb; + uint32_t *p; + uint32_t *q; + int o; + + /* We always write IFD8 for BigTIFF, no checking needed. */ + if (tif->tif_flags & TIFF_BIGTIFF) + return TIFFWriteDirectoryTagCheckedIfd8Array(tif, ndir, dir, tag, count, + value); + + /* + ** For classic tiff we want to verify everything is in range for IFD + ** and convert to long format. + */ + + p = _TIFFmallocExt(tif, count * sizeof(uint32_t)); + if (p == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + + for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++) + { + if (*ma > 0xFFFFFFFF) + { + TIFFErrorExtR(tif, module, + "Attempt to write value larger than 0xFFFFFFFF in " + "Classic TIFF file."); + _TIFFfreeExt(tif, p); + return (0); + } + *q = (uint32_t)(*ma); + } + + o = TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, tag, count, p); + _TIFFfreeExt(tif, p); + + return (o); +} + +/* + * Auxiliary function to determine the IFD data size to be written to the file. + * The IFD data size is finally the size of the IFD tag entries plus the IFD + * data that is written directly after the IFD tag entries. + */ +static void EvaluateIFDdatasizeWrite(TIFF *tif, uint32_t count, + uint32_t typesize, uint32_t *ndir) +{ + uint64_t datalength = (uint64_t)count * typesize; + if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U)) + { + /* LibTIFF increments write address to an even offset, thus datalength + * written is also incremented. */ + if (datalength & 1) + datalength++; + tif->tif_dir.td_dirdatasize_write += datalength; + } + (*ndir)++; +} + +static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir) +{ + static const char module[] = "TIFFWriteDirectoryTagColormap"; + uint32_t m; + uint16_t *n; + int o; + m = (1 << tif->tif_dir.td_bitspersample); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, 3 * m, sizeof(uint16_t), ndir); + return 1; + } + + n = _TIFFmallocExt(tif, 3 * m * sizeof(uint16_t)); + if (n == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + _TIFFmemcpy(&n[0], tif->tif_dir.td_colormap[0], m * sizeof(uint16_t)); + _TIFFmemcpy(&n[m], tif->tif_dir.td_colormap[1], m * sizeof(uint16_t)); + _TIFFmemcpy(&n[2 * m], tif->tif_dir.td_colormap[2], m * sizeof(uint16_t)); + o = TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, TIFFTAG_COLORMAP, + 3 * m, n); + _TIFFfreeExt(tif, n); + return (o); +} + +static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir) +{ + static const char module[] = "TIFFWriteDirectoryTagTransferfunction"; + uint32_t m; + uint16_t n; + uint16_t *o; + int p; + /* TIFFTAG_TRANSFERFUNCTION expects (1 or 3) pointer to arrays with + * (1 << BitsPerSample) * uint16_t values. + */ + m = (1 << tif->tif_dir.td_bitspersample); + /* clang-format off */ + n = (tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples) > 1 ? 3 : 1; + /* clang-format on */ + + /* Check for proper number of transferfunctions */ + for (int i = 0; i < n; i++) + { + if (tif->tif_dir.td_transferfunction[i] == NULL) + { + TIFFWarningExtR(tif, module, + "Too few TransferFunctions provided. Tag " + "not written to file"); + return (1); /* Not an error; only tag is not written. */ + } + } + /* + * Check if the table can be written as a single column, + * or if it must be written as 3 columns. Note that we + * write a 3-column tag if there are 2 samples/pixel and + * a single column of data won't suffice--hmm. + */ + if (n == 3) + { + if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0], + tif->tif_dir.td_transferfunction[2], + m * sizeof(uint16_t)) && + !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0], + tif->tif_dir.td_transferfunction[1], + m * sizeof(uint16_t))) + n = 1; + } + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, n * m, 2, ndir); + return 1; + } + + o = _TIFFmallocExt(tif, n * m * sizeof(uint16_t)); + if (o == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + _TIFFmemcpy(&o[0], tif->tif_dir.td_transferfunction[0], + m * sizeof(uint16_t)); + if (n > 1) + _TIFFmemcpy(&o[m], tif->tif_dir.td_transferfunction[1], + m * sizeof(uint16_t)); + if (n > 2) + _TIFFmemcpy(&o[2 * m], tif->tif_dir.td_transferfunction[2], + m * sizeof(uint16_t)); + p = TIFFWriteDirectoryTagCheckedShortArray( + tif, ndir, dir, TIFFTAG_TRANSFERFUNCTION, n * m, o); + _TIFFfreeExt(tif, o); + return (p); +} + +static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir) +{ + static const char module[] = "TIFFWriteDirectoryTagSubifd"; + uint64_t m; + int n; + if (tif->tif_dir.td_nsubifd == 0) + return (1); + m = tif->tif_dataoff; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t *o; + uint64_t *pa; + uint32_t *pb; + uint16_t p; + o = _TIFFmallocExt(tif, tif->tif_dir.td_nsubifd * sizeof(uint32_t)); + if (o == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + pa = tif->tif_dir.td_subifd; + pb = o; + for (p = 0; p < tif->tif_dir.td_nsubifd; p++) + { + assert(pa != 0); + + /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which + * is illegal) */ + if (*pa > 0xFFFFFFFFUL) + { + TIFFErrorExtR(tif, module, "Illegal value for SubIFD tag"); + _TIFFfreeExt(tif, o); + return (0); + } + *pb++ = (uint32_t)(*pa++); + } + n = TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, TIFFTAG_SUBIFD, + tif->tif_dir.td_nsubifd, o); + _TIFFfreeExt(tif, o); + } + else + n = TIFFWriteDirectoryTagCheckedIfd8Array( + tif, ndir, dir, TIFFTAG_SUBIFD, tif->tif_dir.td_nsubifd, + tif->tif_dir.td_subifd); + + if (dir == NULL) + /* Just have evaluated IFD data size and incremented ndir + * above in sub-functions. */ + return (n); + + if (!n) + return (0); + /* + * Total hack: if this directory includes a SubIFD + * tag then force the next directories to be + * written as ``sub directories'' of this one. This + * is used to write things like thumbnails and + * image masks that one wants to keep out of the + * normal directory linkage access mechanism. + */ + tif->tif_flags |= TIFF_INSUBIFD; + tif->tif_nsubifd = tif->tif_dir.td_nsubifd; + if (tif->tif_dir.td_nsubifd == 1) + tif->tif_subifdoff = 0; + else + tif->tif_subifdoff = m; + return (1); +} + +static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, char *value) +{ + assert(sizeof(char) == 1); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 1, ndir); + return 1; + } + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_ASCII, count, + count, value)); +} + +static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + uint8_t *value) +{ + assert(sizeof(uint8_t) == 1); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 1, ndir); + return 1; + } + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_UNDEFINED, + count, count, value)); +} + +static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint8_t *value) +{ + assert(sizeof(uint8_t) == 1); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 1, ndir); + return 1; + } + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_BYTE, count, + count, value)); +} + +static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int8_t *value) +{ + assert(sizeof(int8_t) == 1); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 1, ndir); + return 1; + } + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SBYTE, count, + count, value)); +} + +static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t value) +{ + uint16_t m; + assert(sizeof(uint16_t) == 2); + if (dir == NULL) + { + /* No additional data to IFD data size just increment ndir. */ + (*ndir)++; + return 1; + } + m = value; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&m); + return ( + TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SHORT, 1, 2, &m)); +} + +static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint16_t *value) +{ + assert(count < 0x80000000); + assert(sizeof(uint16_t) == 2); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 2, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfShort(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SHORT, count, + count * 2, value)); +} + +static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int16_t *value) +{ + assert(count < 0x80000000); + assert(sizeof(int16_t) == 2); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 2, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfShort((uint16_t *)value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SSHORT, count, + count * 2, value)); +} + +static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t value) +{ + uint32_t m; + assert(sizeof(uint32_t) == 4); + if (dir == NULL) + { + /* No additional data to IFD data size just increment ndir. */ + (*ndir)++; + return 1; + } + m = value; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&m); + return ( + TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG, 1, 4, &m)); +} + +static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint32_t *value) +{ + assert(count < 0x40000000); + assert(sizeof(uint32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 4, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG, count, + count * 4, value)); +} + +static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int32_t *value) +{ + assert(count < 0x40000000); + assert(sizeof(int32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 4, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SLONG, count, + count * 4, value)); +} + +static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint64_t *value) +{ + assert(count < 0x20000000); + assert(sizeof(uint64_t) == 8); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + TIFFErrorExtR(tif, "TIFFWriteDirectoryTagCheckedLong8Array", + "LONG8 not allowed for ClassicTIFF"); + return (0); + } + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 8, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG8, count, + count * 8, value)); +} + +static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + int64_t *value) +{ + assert(count < 0x20000000); + assert(sizeof(int64_t) == 8); + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + TIFFErrorExtR(tif, "TIFFWriteDirectoryTagCheckedSlong8Array", + "SLONG8 not allowed for ClassicTIFF"); + return (0); + } + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 8, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8((uint64_t *)value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SLONG8, count, + count * 8, value)); +} + +static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + double value) +{ + static const char module[] = "TIFFWriteDirectoryTagCheckedRational"; + uint32_t m[2]; + assert(sizeof(uint32_t) == 4); + if (value < 0) + { + TIFFErrorExtR(tif, module, "Negative value is illegal"); + return 0; + } + else if (value != value) + { + TIFFErrorExtR(tif, module, "Not-a-number value is illegal"); + return 0; + } + + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + tif->tif_dir.td_dirdatasize_write += + (tif->tif_flags & TIFF_BIGTIFF) ? 0 : 0x8U; + (*ndir)++; + return 1; + } + + DoubleToRational(value, &m[0], &m[1]); + + if (tif->tif_flags & TIFF_SWAB) + { + TIFFSwabLong(&m[0]); + TIFFSwabLong(&m[1]); + } + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, 1, 8, + &m[0])); +} + +static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + float *value) +{ + static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray"; + uint32_t *m; + float *na; + uint32_t *nb; + uint32_t nc; + int o; + assert(sizeof(uint32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(uint32_t), ndir); + return 1; + } + m = _TIFFmallocExt(tif, count * 2 * sizeof(uint32_t)); + if (m == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++) + { + DoubleToRational(*na, &nb[0], &nb[1]); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(m, count * 2); + o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, count, + count * 8, &m[0]); + _TIFFfreeExt(tif, m); + return (o); +} + +static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, + uint32_t count, + float *value) +{ + static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray"; + int32_t *m; + float *na; + int32_t *nb; + uint32_t nc; + int o; + assert(sizeof(int32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(int32_t), ndir); + return 1; + } + m = _TIFFmallocExt(tif, count * 2 * sizeof(int32_t)); + if (m == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++) + { + DoubleToSrational(*na, &nb[0], &nb[1]); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)m, count * 2); + o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SRATIONAL, count, + count * 8, &m[0]); + _TIFFfreeExt(tif, m); + return (o); +} + +/*-- Rational2Double: additional write functions for double arrays */ +static int +TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, double *value) +{ + static const char module[] = + "TIFFWriteDirectoryTagCheckedRationalDoubleArray"; + uint32_t *m; + double *na; + uint32_t *nb; + uint32_t nc; + int o; + assert(sizeof(uint32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(uint32_t), ndir); + return 1; + } + m = _TIFFmallocExt(tif, count * 2 * sizeof(uint32_t)); + if (m == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++) + { + DoubleToRational(*na, &nb[0], &nb[1]); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(m, count * 2); + o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, count, + count * 8, &m[0]); + _TIFFfreeExt(tif, m); + return (o); +} /*-- TIFFWriteDirectoryTagCheckedRationalDoubleArray() ------- */ + +static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray( + TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, + double *value) +{ + static const char module[] = + "TIFFWriteDirectoryTagCheckedSrationalDoubleArray"; + int32_t *m; + double *na; + int32_t *nb; + uint32_t nc; + int o; + assert(sizeof(int32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(int32_t), ndir); + return 1; + } + m = _TIFFmallocExt(tif, count * 2 * sizeof(int32_t)); + if (m == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++) + { + DoubleToSrational(*na, &nb[0], &nb[1]); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong((uint32_t *)m, count * 2); + o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SRATIONAL, count, + count * 8, &m[0]); + _TIFFfreeExt(tif, m); + return (o); +} /*--- TIFFWriteDirectoryTagCheckedSrationalDoubleArray() -------- */ + +/** ----- Rational2Double: Double To Rational Conversion +---------------------------------------------------------- +* There is a mathematical theorem to convert real numbers into a rational +(integer fraction) number. +* This is called "continuous fraction" which uses the Euclidean algorithm to +find the greatest common divisor (GCD). +* (ref. e.g. https://de.wikipedia.org/wiki/Kettenbruch or +https://en.wikipedia.org/wiki/Continued_fraction +* https://en.wikipedia.org/wiki/Euclidean_algorithm) +* The following functions implement the +* - ToRationalEuclideanGCD() auxiliary function which mainly +implements euclidean GCD +* - DoubleToRational() conversion function for un-signed +rationals +* - DoubleToSrational() conversion function for signed rationals +------------------------------------------------------------------------------------------------------------------*/ + +/**---- ToRationalEuclideanGCD() ----------------------------------------- +* Calculates the rational fractional of a double input value +* using the Euclidean algorithm to find the greatest common divisor (GCD) +------------------------------------------------------------------------*/ +static void ToRationalEuclideanGCD(double value, int blnUseSignedRange, + int blnUseSmallRange, uint64_t *ullNum, + uint64_t *ullDenom) +{ + /* Internally, the integer variables can be bigger than the external ones, + * as long as the result will fit into the external variable size. + */ + uint64_t numSum[3] = {0, 1, 0}, denomSum[3] = {1, 0, 0}; + uint64_t aux, bigNum, bigDenom; + uint64_t returnLimit; + int i; + uint64_t nMax; + double fMax; + unsigned long maxDenom; + /*-- nMax and fMax defines the initial accuracy of the starting fractional, + * or better, the highest used integer numbers used within the starting + * fractional (bigNum/bigDenom). There are two approaches, which can + * accidentally lead to different accuracies just depending on the value. + * Therefore, blnUseSmallRange steers this behavior. + * For long long nMax = ((9223372036854775807-1)/2); for long nMax = + * ((2147483647-1)/2); + */ + if (blnUseSmallRange) + { + nMax = (uint64_t)((2147483647 - 1) / 2); /* for ULONG range */ + } + else + { + nMax = ((9223372036854775807 - 1) / 2); /* for ULLONG range */ + } + fMax = (double)nMax; + + /*-- For the Euclidean GCD define the denominator range, so that it stays + * within size of unsigned long variables. maxDenom should be LONG_MAX for + * negative values and ULONG_MAX for positive ones. Also the final returned + * value of ullNum and ullDenom is limited according to signed- or + * unsigned-range. + */ + if (blnUseSignedRange) + { + maxDenom = 2147483647UL; /*LONG_MAX = 0x7FFFFFFFUL*/ + returnLimit = maxDenom; + } + else + { + maxDenom = 0xFFFFFFFFUL; /*ULONG_MAX = 0xFFFFFFFFUL*/ + returnLimit = maxDenom; + } + + /*-- First generate a rational fraction (bigNum/bigDenom) which represents + *the value as a rational number with the highest accuracy. Therefore, + *uint64_t (uint64_t) is needed. This rational fraction is then reduced + *using the Euclidean algorithm to find the greatest common divisor (GCD). + * bigNum = big numinator of value without fraction (or cut residual + *fraction) bigDenom = big denominator of value + *-- Break-criteria so that uint64_t cast to "bigNum" introduces no error + *and bigDenom has no overflow, and stop with enlargement of fraction when + *the double-value of it reaches an integer number without fractional part. + */ + bigDenom = 1; + while ((value != floor(value)) && (value < fMax) && (bigDenom < nMax)) + { + bigDenom <<= 1; + value *= 2; + } + bigNum = (uint64_t)value; + + /*-- Start Euclidean algorithm to find the greatest common divisor (GCD) -- + */ +#define MAX_ITERATIONS 64 + for (i = 0; i < MAX_ITERATIONS; i++) + { + uint64_t val; + /* if bigDenom is not zero, calculate integer part of fraction. */ + if (bigDenom == 0) + { + break; + } + val = bigNum / bigDenom; + + /* Set bigDenom to reminder of bigNum/bigDenom and bigNum to previous + * denominator bigDenom. */ + aux = bigNum; + bigNum = bigDenom; + bigDenom = aux % bigDenom; + + /* calculate next denominator and check for its given maximum */ + aux = val; + if (denomSum[1] * val + denomSum[0] >= maxDenom) + { + aux = (maxDenom - denomSum[0]) / denomSum[1]; + if (aux * 2 >= val || denomSum[1] >= maxDenom) + i = (MAX_ITERATIONS + + 1); /* exit but execute rest of for-loop */ + else + break; + } + /* calculate next numerator to numSum2 and save previous one to numSum0; + * numSum1 just copy of numSum2. */ + numSum[2] = aux * numSum[1] + numSum[0]; + numSum[0] = numSum[1]; + numSum[1] = numSum[2]; + /* calculate next denominator to denomSum2 and save previous one to + * denomSum0; denomSum1 just copy of denomSum2. */ + denomSum[2] = aux * denomSum[1] + denomSum[0]; + denomSum[0] = denomSum[1]; + denomSum[1] = denomSum[2]; + } + + /*-- Check and adapt for final variable size and return values; reduces + * internal accuracy; denominator is kept in ULONG-range with maxDenom -- */ + while (numSum[1] > returnLimit || denomSum[1] > returnLimit) + { + numSum[1] = numSum[1] / 2; + denomSum[1] = denomSum[1] / 2; + } + + /* return values */ + *ullNum = numSum[1]; + *ullDenom = denomSum[1]; + +} /*-- ToRationalEuclideanGCD() -------------- */ + +/**---- DoubleToRational() ----------------------------------------------- +* Calculates the rational fractional of a double input value +* for UN-SIGNED rationals, +* using the Euclidean algorithm to find the greatest common divisor (GCD) +------------------------------------------------------------------------*/ +static void DoubleToRational(double value, uint32_t *num, uint32_t *denom) +{ + /*---- UN-SIGNED RATIONAL ---- */ + double dblDiff, dblDiff2; + uint64_t ullNum, ullDenom, ullNum2, ullDenom2; + + /*-- Check for negative values. If so it is an error. */ + /* Test written that way to catch NaN */ + if (!(value >= 0)) + { + *num = *denom = 0; + TIFFErrorExt(0, "TIFFLib: DoubleToRational()", + " Negative Value for Unsigned Rational given."); + return; + } + + /*-- Check for too big numbers (> ULONG_MAX) -- */ + if (value > 0xFFFFFFFFUL) + { + *num = 0xFFFFFFFFU; + *denom = 0; + return; + } + /*-- Check for easy integer numbers -- */ + if (value == (uint32_t)(value)) + { + *num = (uint32_t)value; + *denom = 1; + return; + } + /*-- Check for too small numbers for "unsigned long" type rationals -- */ + if (value < 1.0 / (double)0xFFFFFFFFUL) + { + *num = 0; + *denom = 0xFFFFFFFFU; + return; + } + + /*-- There are two approaches using the Euclidean algorithm, + * which can accidentally lead to different accuracies just depending on + * the value. Try both and define which one was better. + */ + ToRationalEuclideanGCD(value, FALSE, FALSE, &ullNum, &ullDenom); + ToRationalEuclideanGCD(value, FALSE, TRUE, &ullNum2, &ullDenom2); + /*-- Double-Check, that returned values fit into ULONG :*/ + if (ullNum > 0xFFFFFFFFUL || ullDenom > 0xFFFFFFFFUL || + ullNum2 > 0xFFFFFFFFUL || ullDenom2 > 0xFFFFFFFFUL) + { + TIFFErrorExt(0, "TIFFLib: DoubleToRational()", + " Num or Denom exceeds ULONG: val=%14.6f, num=%12" PRIu64 + ", denom=%12" PRIu64 " | num2=%12" PRIu64 + ", denom2=%12" PRIu64 "", + value, ullNum, ullDenom, ullNum2, ullDenom2); + assert(0); + } + + /* Check, which one has higher accuracy and take that. */ + dblDiff = fabs(value - ((double)ullNum / (double)ullDenom)); + dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2)); + if (dblDiff < dblDiff2) + { + *num = (uint32_t)ullNum; + *denom = (uint32_t)ullDenom; + } + else + { + *num = (uint32_t)ullNum2; + *denom = (uint32_t)ullDenom2; + } +} /*-- DoubleToRational() -------------- */ + +/**---- DoubleToSrational() ----------------------------------------------- +* Calculates the rational fractional of a double input value +* for SIGNED rationals, +* using the Euclidean algorithm to find the greatest common divisor (GCD) +------------------------------------------------------------------------*/ +static void DoubleToSrational(double value, int32_t *num, int32_t *denom) +{ + /*---- SIGNED RATIONAL ----*/ + int neg = 1; + double dblDiff, dblDiff2; + uint64_t ullNum, ullDenom, ullNum2, ullDenom2; + + /*-- Check for negative values and use then the positive one for internal + * calculations, but take the sign into account before returning. */ + if (value < 0) + { + neg = -1; + value = -value; + } + + /*-- Check for too big numbers (> LONG_MAX) -- */ + if (value > 0x7FFFFFFFL) + { + *num = 0x7FFFFFFFL; + *denom = 0; + return; + } + /*-- Check for easy numbers -- */ + if (value == (int32_t)(value)) + { + *num = (int32_t)(neg * value); + *denom = 1; + return; + } + /*-- Check for too small numbers for "long" type rationals -- */ + if (value < 1.0 / (double)0x7FFFFFFFL) + { + *num = 0; + *denom = 0x7FFFFFFFL; + return; + } + + /*-- There are two approaches using the Euclidean algorithm, + * which can accidentally lead to different accuracies just depending on + * the value. Try both and define which one was better. Furthermore, set + * behavior of ToRationalEuclideanGCD() to the range of signed-long. + */ + ToRationalEuclideanGCD(value, TRUE, FALSE, &ullNum, &ullDenom); + ToRationalEuclideanGCD(value, TRUE, TRUE, &ullNum2, &ullDenom2); + /*-- Double-Check, that returned values fit into LONG :*/ + if (ullNum > 0x7FFFFFFFL || ullDenom > 0x7FFFFFFFL || + ullNum2 > 0x7FFFFFFFL || ullDenom2 > 0x7FFFFFFFL) + { + TIFFErrorExt(0, "TIFFLib: DoubleToSrational()", + " Num or Denom exceeds LONG: val=%14.6f, num=%12" PRIu64 + ", denom=%12" PRIu64 " | num2=%12" PRIu64 + ", denom2=%12" PRIu64 "", + neg * value, ullNum, ullDenom, ullNum2, ullDenom2); + assert(0); + } + + /* Check, which one has higher accuracy and take that. */ + dblDiff = fabs(value - ((double)ullNum / (double)ullDenom)); + dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2)); + if (dblDiff < dblDiff2) + { + *num = (int32_t)(neg * (long)ullNum); + *denom = (int32_t)ullDenom; + } + else + { + *num = (int32_t)(neg * (long)ullNum2); + *denom = (int32_t)ullDenom2; + } +} /*-- DoubleToSrational() --------------*/ + +static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + float *value) +{ + assert(count < 0x40000000); + assert(sizeof(float) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 4, ndir); + return 1; + } + TIFFCvtNativeToIEEEFloat(tif, count, value); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfFloat(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_FLOAT, count, + count * 4, value)); +} + +static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + double *value) +{ + assert(count < 0x20000000); + assert(sizeof(double) == 8); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 8, ndir); + return 1; + } + TIFFCvtNativeToIEEEDouble(tif, count, value); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfDouble(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_DOUBLE, count, + count * 8, value)); +} + +static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint32_t count, uint32_t *value) +{ + assert(count < 0x40000000); + assert(sizeof(uint32_t) == 4); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 4, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_IFD, count, + count * 4, value)); +} + +static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, + uint16_t tag, uint32_t count, + uint64_t *value) +{ + assert(count < 0x20000000); + assert(sizeof(uint64_t) == 8); + assert(tif->tif_flags & TIFF_BIGTIFF); + if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */ + { + EvaluateIFDdatasizeWrite(tif, count, 8, ndir); + return 1; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfLong8(value, count); + return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_IFD8, count, + count * 8, value)); +} + +static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir, + TIFFDirEntry *dir, uint16_t tag, + uint16_t datatype, uint32_t count, + uint32_t datalength, void *data) +{ + static const char module[] = "TIFFWriteDirectoryTagData"; + uint32_t m; + m = 0; + while (m < (*ndir)) + { + assert(dir[m].tdir_tag != tag); + if (dir[m].tdir_tag > tag) + break; + m++; + } + if (m < (*ndir)) + { + uint32_t n; + for (n = *ndir; n > m; n--) + dir[n] = dir[n - 1]; + } + dir[m].tdir_tag = tag; + dir[m].tdir_type = datatype; + dir[m].tdir_count = count; + dir[m].tdir_offset.toff_long8 = 0; + if (datalength <= ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U)) + { + if (data && datalength) + { + _TIFFmemcpy(&dir[m].tdir_offset, data, datalength); + } + } + else + { + uint64_t na, nb; + na = tif->tif_dataoff; + nb = na + datalength; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + nb = (uint32_t)nb; + if ((nb < na) || (nb < datalength)) + { + TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); + return (0); + } + if (!SeekOK(tif, na)) + { + TIFFErrorExtR(tif, module, "IO error writing tag data"); + return (0); + } + if (datalength >= 0x80000000UL) + { + TIFFErrorExtR(tif, module, + "libtiff does not allow writing more than 2147483647 " + "bytes in a tag"); + return (0); + } + if (!WriteOK(tif, data, (tmsize_t)datalength)) + { + TIFFErrorExtR(tif, module, "IO error writing tag data"); + return (0); + } + tif->tif_dataoff = nb; + if (tif->tif_dataoff & 1) + tif->tif_dataoff++; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t o; + o = (uint32_t)na; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&o); + _TIFFmemcpy(&dir[m].tdir_offset, &o, 4); + } + else + { + dir[m].tdir_offset.toff_long8 = na; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dir[m].tdir_offset.toff_long8); + } + } + (*ndir)++; + return (1); +} + +/* + * Link the current directory into the directory chain for the file. + */ +static int TIFFLinkDirectory(TIFF *tif) +{ + static const char module[] = "TIFFLinkDirectory"; + + tif->tif_diroff = (TIFFSeekFile(tif, 0, SEEK_END) + 1) & (~((toff_t)1)); + + /* + * Handle SubIFDs + */ + if (tif->tif_flags & TIFF_INSUBIFD) + { + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t m; + m = (uint32_t)tif->tif_diroff; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&m); + (void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET); + if (!WriteOK(tif, &m, 4)) + { + TIFFErrorExtR(tif, module, + "Error writing SubIFD directory link"); + return (0); + } + + /* + * Advance to the next SubIFD or, if this is + * the last one configured, reverting back to the + * normal directory linkage is done in TIFFWriteDirectorySec() + * by tif->tif_flags &= ~TIFF_INSUBIFD;. + */ + if (--tif->tif_nsubifd) + tif->tif_subifdoff += 4; + return (1); + } + else + { + uint64_t m; + m = tif->tif_diroff; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&m); + (void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET); + if (!WriteOK(tif, &m, 8)) + { + TIFFErrorExtR(tif, module, + "Error writing SubIFD directory link"); + return (0); + } + + /* + * Advance to the next SubIFD or, if this is + * the last one configured, reverting back to the + * normal directory linkage is done in TIFFWriteDirectorySec() + * by tif->tif_flags &= ~TIFF_INSUBIFD;. + */ + if (--tif->tif_nsubifd) + tif->tif_subifdoff += 8; + return (1); + } + } + + /* + * Handle main-IFDs + */ + tdir_t ndir = 1; /* count current number of main-IFDs */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t m; + uint32_t nextdir; + m = (uint32_t)(tif->tif_diroff); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&m); + if (tif->tif_header.classic.tiff_diroff == 0) + { + /* + * First directory, overwrite offset in header. + */ + tif->tif_header.classic.tiff_diroff = (uint32_t)tif->tif_diroff; + tif->tif_lastdiroff = tif->tif_diroff; + (void)TIFFSeekFile(tif, 4, SEEK_SET); + if (!WriteOK(tif, &m, 4)) + { + TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header"); + return (0); + } + if (!tif->tif_dir.td_iswrittentofile) + tif->tif_curdircount = 0; + return (1); + } + /* + * Not the first directory, search to the last and append. + */ + tdir_t dirn = 0; + if (tif->tif_lastdiroff != 0 && + _TIFFGetDirNumberFromOffset(tif, tif->tif_lastdiroff, &dirn)) + { + /* Start searching from the lastely written IFD. Thus get its IFD + * number. */ + nextdir = (uint32_t)tif->tif_lastdiroff; + ndir = dirn + 1; + } + else + { + nextdir = tif->tif_header.classic.tiff_diroff; + ndir = 1; /* start searching from the first IFD */ + } + + while (1) + { + uint16_t dircount; + uint32_t nextnextdir; + + if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, 2)) + { + TIFFErrorExtR(tif, module, "Error fetching directory count"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount); + (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, SEEK_SET); + if (!ReadOK(tif, &nextnextdir, 4)) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&nextnextdir); + if (nextnextdir == 0) + { + (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, SEEK_SET); + if (!WriteOK(tif, &m, 4)) + { + TIFFErrorExtR(tif, module, "Error writing directory link"); + return (0); + } + tif->tif_lastdiroff = tif->tif_diroff; + break; + } + nextdir = nextnextdir; + ndir++; + } + } + else + { + /*- BigTIFF -*/ + uint64_t m; + uint64_t nextdir; + m = tif->tif_diroff; + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&m); + if (tif->tif_header.big.tiff_diroff == 0) + { + /* + * First directory, overwrite offset in header. + */ + tif->tif_header.big.tiff_diroff = tif->tif_diroff; + tif->tif_lastdiroff = tif->tif_diroff; + (void)TIFFSeekFile(tif, 8, SEEK_SET); + if (!WriteOK(tif, &m, 8)) + { + TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header"); + return (0); + } + if (!tif->tif_dir.td_iswrittentofile) + tif->tif_curdircount = 0; + return (1); + } + /* + * Not the first directory, search to the last and append. + */ + tdir_t dirn = 0; + if (tif->tif_lastdiroff != 0 && + _TIFFGetDirNumberFromOffset(tif, tif->tif_lastdiroff, &dirn)) + { + /* Start searching from the lastely written IFD. Thus get its IFD + * number. */ + nextdir = tif->tif_lastdiroff; + ndir = dirn + 1; + } + else + { + nextdir = tif->tif_header.big.tiff_diroff; + ndir = 1; /* start searching from the first IFD */ + } + while (1) + { + uint64_t dircount64; + uint16_t dircount; + uint64_t nextnextdir; + + if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount64, 8)) + { + TIFFErrorExtR(tif, module, "Error fetching directory count"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + if (dircount64 > 0xFFFF) + { + TIFFErrorExtR(tif, module, + "Sanity check on tag count failed, " + "likely corrupt TIFF"); + return (0); + } + dircount = (uint16_t)dircount64; + (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, SEEK_SET); + if (!ReadOK(tif, &nextnextdir, 8)) + { + TIFFErrorExtR(tif, module, "Error fetching directory link"); + return (0); + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&nextnextdir); + if (nextnextdir == 0) + { + (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, SEEK_SET); + if (!WriteOK(tif, &m, 8)) + { + TIFFErrorExtR(tif, module, "Error writing directory link"); + return (0); + } + tif->tif_lastdiroff = tif->tif_diroff; + break; + } + nextdir = nextnextdir; + ndir++; + } + } + /* Offset of next IFD is written to file. + * Update number of main-IFDs in file. + * However, tif_curdircount shall count only newly written main-IFDs with + * entries and not only number of linked offsets! Thus, tif_curdircount is + * incremented at the end of TIFFWriteDirectorySec(). + * TIFF_NON_EXISTENT_DIR_NUMBER means 'dont know number of IFDs' + * 0 means 'empty file opened for writing, but no IFD written yet' */ + if (!tif->tif_dir.td_iswrittentofile && !(tif->tif_flags & TIFF_INSUBIFD)) + { + tif->tif_curdircount = ndir; + } + return (1); +} + +/************************************************************************/ +/* TIFFRewriteField() */ +/* */ +/* Rewrite a field in the directory on disk without regard to */ +/* updating the TIFF directory structure in memory. Currently */ +/* only supported for field that already exist in the on-disk */ +/* directory. Mainly used for updating stripoffset / */ +/* stripbytecount values after the directory is already on */ +/* disk. */ +/* */ +/* Returns zero on failure, and one on success. */ +/************************************************************************/ + +int _TIFFRewriteField(TIFF *tif, uint16_t tag, TIFFDataType in_datatype, + tmsize_t count, void *data) +{ + static const char module[] = "TIFFResetField"; + /* const TIFFField* fip = NULL; */ + uint16_t dircount; + tmsize_t dirsize; + uint8_t direntry_raw[20]; + uint16_t entry_tag = 0; + uint16_t entry_type = 0; + uint64_t entry_count = 0; + uint64_t entry_offset = 0; + int value_in_entry = 0; + uint64_t read_offset; + uint8_t *buf_to_write = NULL; + TIFFDataType datatype; + + /* -------------------------------------------------------------------- */ + /* Find field definition. */ + /* -------------------------------------------------------------------- */ + /*fip =*/TIFFFindField(tif, tag, TIFF_ANY); + + /* -------------------------------------------------------------------- */ + /* Do some checking this is a straight forward case. */ + /* -------------------------------------------------------------------- */ + if (isMapped(tif)) + { + TIFFErrorExtR(tif, module, + "Memory mapped files not currently supported for " + "this operation."); + return 0; + } + + if (tif->tif_diroff == 0) + { + TIFFErrorExtR( + tif, module, + "Attempt to reset field on directory not already on disk."); + return 0; + } + + /* -------------------------------------------------------------------- */ + /* Read the directory entry count. */ + /* -------------------------------------------------------------------- */ + if (!SeekOK(tif, tif->tif_diroff)) + { + TIFFErrorExtR(tif, module, "%s: Seek error accessing TIFF directory", + tif->tif_name); + return 0; + } + + read_offset = tif->tif_diroff; + + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (!ReadOK(tif, &dircount, sizeof(uint16_t))) + { + TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory count", + tif->tif_name); + return 0; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&dircount); + dirsize = 12; + read_offset += 2; + } + else + { + uint64_t dircount64; + if (!ReadOK(tif, &dircount64, sizeof(uint64_t))) + { + TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory count", + tif->tif_name); + return 0; + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&dircount64); + dircount = (uint16_t)dircount64; + dirsize = 20; + read_offset += 8; + } + + /* -------------------------------------------------------------------- */ + /* Read through directory to find target tag. */ + /* -------------------------------------------------------------------- */ + while (dircount > 0) + { + if (!ReadOK(tif, direntry_raw, dirsize)) + { + TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory entry.", + tif->tif_name); + return 0; + } + + memcpy(&entry_tag, direntry_raw + 0, sizeof(uint16_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&entry_tag); + + if (entry_tag == tag) + break; + + read_offset += dirsize; + } + + if (entry_tag != tag) + { + TIFFErrorExtR(tif, module, "%s: Could not find tag %" PRIu16 ".", + tif->tif_name, tag); + return 0; + } + + /* -------------------------------------------------------------------- */ + /* Extract the type, count and offset for this entry. */ + /* -------------------------------------------------------------------- */ + memcpy(&entry_type, direntry_raw + 2, sizeof(uint16_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&entry_type); + + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t value; + + memcpy(&value, direntry_raw + 4, sizeof(uint32_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&value); + entry_count = value; + + memcpy(&value, direntry_raw + 8, sizeof(uint32_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&value); + entry_offset = value; + } + else + { + memcpy(&entry_count, direntry_raw + 4, sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&entry_count); + + memcpy(&entry_offset, direntry_raw + 12, sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8(&entry_offset); + } + + /* -------------------------------------------------------------------- */ + /* When a dummy tag was written due to TIFFDeferStrileArrayWriting() */ + /* -------------------------------------------------------------------- */ + if (entry_offset == 0 && entry_count == 0 && entry_type == 0) + { + if (tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS) + { + entry_type = + (tif->tif_flags & TIFF_BIGTIFF) ? TIFF_LONG8 : TIFF_LONG; + } + else + { + int write_aslong8 = 1; + if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS) + { + write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif)); + } + else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS) + { + write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif)); + } + if (write_aslong8) + { + entry_type = TIFF_LONG8; + } + else + { + int write_aslong4 = 1; + if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS) + { + write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif)); + } + else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS) + { + write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif)); + } + if (write_aslong4) + { + entry_type = TIFF_LONG; + } + else + { + entry_type = TIFF_SHORT; + } + } + } + } + + /* -------------------------------------------------------------------- */ + /* What data type do we want to write this as? */ + /* -------------------------------------------------------------------- */ + if (TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags & TIFF_BIGTIFF)) + { + if (in_datatype == TIFF_LONG8) + datatype = entry_type == TIFF_SHORT ? TIFF_SHORT : TIFF_LONG; + else if (in_datatype == TIFF_SLONG8) + datatype = TIFF_SLONG; + else if (in_datatype == TIFF_IFD8) + datatype = TIFF_IFD; + else + datatype = in_datatype; + } + else + { + if (in_datatype == TIFF_LONG8 && + (entry_type == TIFF_SHORT || entry_type == TIFF_LONG || + entry_type == TIFF_LONG8)) + datatype = entry_type; + else if (in_datatype == TIFF_SLONG8 && + (entry_type == TIFF_SLONG || entry_type == TIFF_SLONG8)) + datatype = entry_type; + else if (in_datatype == TIFF_IFD8 && + (entry_type == TIFF_IFD || entry_type == TIFF_IFD8)) + datatype = entry_type; + else + datatype = in_datatype; + } + + /* -------------------------------------------------------------------- */ + /* Prepare buffer of actual data to write. This includes */ + /* swabbing as needed. */ + /* -------------------------------------------------------------------- */ + buf_to_write = (uint8_t *)_TIFFCheckMalloc( + tif, count, TIFFDataWidth(datatype), "for field buffer."); + if (!buf_to_write) + return 0; + + if (datatype == in_datatype) + memcpy(buf_to_write, data, count * TIFFDataWidth(datatype)); + else if (datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8) + { + tmsize_t i; + + for (i = 0; i < count; i++) + { + ((int32_t *)buf_to_write)[i] = (int32_t)((int64_t *)data)[i]; + if ((int64_t)((int32_t *)buf_to_write)[i] != ((int64_t *)data)[i]) + { + _TIFFfreeExt(tif, buf_to_write); + TIFFErrorExtR(tif, module, + "Value exceeds 32bit range of output type."); + return 0; + } + } + } + else if ((datatype == TIFF_LONG && in_datatype == TIFF_LONG8) || + (datatype == TIFF_IFD && in_datatype == TIFF_IFD8)) + { + tmsize_t i; + + for (i = 0; i < count; i++) + { + ((uint32_t *)buf_to_write)[i] = (uint32_t)((uint64_t *)data)[i]; + if ((uint64_t)((uint32_t *)buf_to_write)[i] != + ((uint64_t *)data)[i]) + { + _TIFFfreeExt(tif, buf_to_write); + TIFFErrorExtR(tif, module, + "Value exceeds 32bit range of output type."); + return 0; + } + } + } + else if (datatype == TIFF_SHORT && in_datatype == TIFF_LONG8) + { + tmsize_t i; + + for (i = 0; i < count; i++) + { + ((uint16_t *)buf_to_write)[i] = (uint16_t)((uint64_t *)data)[i]; + if ((uint64_t)((uint16_t *)buf_to_write)[i] != + ((uint64_t *)data)[i]) + { + _TIFFfreeExt(tif, buf_to_write); + TIFFErrorExtR(tif, module, + "Value exceeds 16bit range of output type."); + return 0; + } + } + } + else + { + TIFFErrorExtR(tif, module, "Unhandled type conversion."); + return 0; + } + + if (TIFFDataWidth(datatype) > 1 && (tif->tif_flags & TIFF_SWAB)) + { + if (TIFFDataWidth(datatype) == 2) + TIFFSwabArrayOfShort((uint16_t *)buf_to_write, count); + else if (TIFFDataWidth(datatype) == 4) + TIFFSwabArrayOfLong((uint32_t *)buf_to_write, count); + else if (TIFFDataWidth(datatype) == 8) + TIFFSwabArrayOfLong8((uint64_t *)buf_to_write, count); + } + + /* -------------------------------------------------------------------- */ + /* Is this a value that fits into the directory entry? */ + /* -------------------------------------------------------------------- */ + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + if (TIFFDataWidth(datatype) * count <= 4) + { + entry_offset = read_offset + 8; + value_in_entry = 1; + } + } + else + { + if (TIFFDataWidth(datatype) * count <= 8) + { + entry_offset = read_offset + 12; + value_in_entry = 1; + } + } + + if ((tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS) && + tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0) + { + tif->tif_dir.td_stripoffset_entry.tdir_type = datatype; + tif->tif_dir.td_stripoffset_entry.tdir_count = count; + } + else if ((tag == TIFFTAG_TILEBYTECOUNTS || + tag == TIFFTAG_STRIPBYTECOUNTS) && + tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) + { + tif->tif_dir.td_stripbytecount_entry.tdir_type = datatype; + tif->tif_dir.td_stripbytecount_entry.tdir_count = count; + } + + /* -------------------------------------------------------------------- */ + /* If the tag type, and count match, then we just write it out */ + /* over the old values without altering the directory entry at */ + /* all. */ + /* -------------------------------------------------------------------- */ + if (entry_count == (uint64_t)count && entry_type == (uint16_t)datatype) + { + if (!SeekOK(tif, entry_offset)) + { + _TIFFfreeExt(tif, buf_to_write); + TIFFErrorExtR(tif, module, + "%s: Seek error accessing TIFF directory", + tif->tif_name); + return 0; + } + if (!WriteOK(tif, buf_to_write, count * TIFFDataWidth(datatype))) + { + _TIFFfreeExt(tif, buf_to_write); + TIFFErrorExtR(tif, module, "Error writing directory link"); + return (0); + } + + _TIFFfreeExt(tif, buf_to_write); + return 1; + } + + /* -------------------------------------------------------------------- */ + /* Otherwise, we write the new tag data at the end of the file. */ + /* -------------------------------------------------------------------- */ + if (!value_in_entry) + { + entry_offset = TIFFSeekFile(tif, 0, SEEK_END); + + if (!WriteOK(tif, buf_to_write, count * TIFFDataWidth(datatype))) + { + _TIFFfreeExt(tif, buf_to_write); + TIFFErrorExtR(tif, module, "Error writing directory link"); + return (0); + } + } + else + { + if (count * TIFFDataWidth(datatype) == 4) + { + uint32_t value; + memcpy(&value, buf_to_write, count * TIFFDataWidth(datatype)); + entry_offset = value; + } + else + { + memcpy(&entry_offset, buf_to_write, + count * TIFFDataWidth(datatype)); + } + } + + _TIFFfreeExt(tif, buf_to_write); + buf_to_write = 0; + + /* -------------------------------------------------------------------- */ + /* Adjust the directory entry. */ + /* -------------------------------------------------------------------- */ + entry_type = datatype; + entry_count = (uint64_t)count; + memcpy(direntry_raw + 2, &entry_type, sizeof(uint16_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort((uint16_t *)(direntry_raw + 2)); + + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + uint32_t value; + + value = (uint32_t)entry_count; + memcpy(direntry_raw + 4, &value, sizeof(uint32_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)(direntry_raw + 4)); + + value = (uint32_t)entry_offset; + memcpy(direntry_raw + 8, &value, sizeof(uint32_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong((uint32_t *)(direntry_raw + 8)); + } + else + { + memcpy(direntry_raw + 4, &entry_count, sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)(direntry_raw + 4)); + + memcpy(direntry_raw + 12, &entry_offset, sizeof(uint64_t)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong8((uint64_t *)(direntry_raw + 12)); + } + + /* -------------------------------------------------------------------- */ + /* Write the directory entry out to disk. */ + /* -------------------------------------------------------------------- */ + if (!SeekOK(tif, read_offset)) + { + TIFFErrorExtR(tif, module, "%s: Seek error accessing TIFF directory", + tif->tif_name); + return 0; + } + + if (!WriteOK(tif, direntry_raw, dirsize)) + { + TIFFErrorExtR(tif, module, "%s: Can not write TIFF directory entry.", + tif->tif_name); + return 0; + } + + return 1; +} diff --git a/cpp/3rd_party/libtiff/tif_dumpmode.c b/cpp/3rd_party/libtiff/tif_dumpmode.c new file mode 100644 index 0000000000..267d5d2d7a --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_dumpmode.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * "Null" Compression Algorithm Support. + */ +#include "tiffiop.h" + +static int DumpFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +/* + * Encode a hunk of pixels. + */ +static int DumpModeEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s) +{ + (void)s; + while (cc > 0) + { + tmsize_t n; + + n = cc; + if (tif->tif_rawcc + n > tif->tif_rawdatasize) + n = tif->tif_rawdatasize - tif->tif_rawcc; + + assert(n > 0); + + /* + * Avoid copy if client has setup raw + * data buffer to avoid extra copy. + */ + if (tif->tif_rawcp != pp) + _TIFFmemcpy(tif->tif_rawcp, pp, n); + tif->tif_rawcp += n; + tif->tif_rawcc += n; + pp += n; + cc -= n; + if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif)) + return (0); + } + return (1); +} + +/* + * Decode a hunk of pixels. + */ +static int DumpModeDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + static const char module[] = "DumpModeDecode"; + (void)s; + if (tif->tif_rawcc < cc) + { + TIFFErrorExtR(tif, module, + "Not enough data for scanline %" PRIu32 + ", expected a request for at most %" TIFF_SSIZE_FORMAT + " bytes, got a request for %" TIFF_SSIZE_FORMAT " bytes", + tif->tif_row, tif->tif_rawcc, cc); + return (0); + } + /* + * Avoid copy if client has setup raw + * data buffer to avoid extra copy. + */ + if (tif->tif_rawcp != buf) + _TIFFmemcpy(buf, tif->tif_rawcp, cc); + tif->tif_rawcp += cc; + tif->tif_rawcc -= cc; + return (1); +} + +/* + * Seek forwards nrows in the current strip. + */ +static int DumpModeSeek(TIFF *tif, uint32_t nrows) +{ + tif->tif_rawcp += nrows * tif->tif_scanlinesize; + tif->tif_rawcc -= nrows * tif->tif_scanlinesize; + return (1); +} + +/* + * Initialize dump mode. + */ +int TIFFInitDumpMode(TIFF *tif, int scheme) +{ + (void)scheme; + tif->tif_fixuptags = DumpFixupTags; + tif->tif_decoderow = DumpModeDecode; + tif->tif_decodestrip = DumpModeDecode; + tif->tif_decodetile = DumpModeDecode; + tif->tif_encoderow = DumpModeEncode; + tif->tif_encodestrip = DumpModeEncode; + tif->tif_encodetile = DumpModeEncode; + tif->tif_seek = DumpModeSeek; + return (1); +} diff --git a/cpp/3rd_party/libtiff/tif_error.c b/cpp/3rd_party/libtiff/tif_error.c new file mode 100644 index 0000000000..ac0b9c373a --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_error.c @@ -0,0 +1,132 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + */ +#include "tiffiop.h" + +TIFFErrorHandlerExt _TIFFerrorHandlerExt = NULL; + +TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler handler) +{ + TIFFErrorHandler prev = _TIFFerrorHandler; + _TIFFerrorHandler = handler; + return (prev); +} + +TIFFErrorHandlerExt TIFFSetErrorHandlerExt(TIFFErrorHandlerExt handler) +{ + TIFFErrorHandlerExt prev = _TIFFerrorHandlerExt; + _TIFFerrorHandlerExt = handler; + return (prev); +} + +void TIFFError(const char *module, const char *fmt, ...) +{ + va_list ap; + if (_TIFFerrorHandler) + { + va_start(ap, fmt); + (*_TIFFerrorHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFerrorHandlerExt) + { + va_start(ap, fmt); + (*_TIFFerrorHandlerExt)(0, module, fmt, ap); + va_end(ap); + } +} + +void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt, ...) +{ + va_list ap; + if (_TIFFerrorHandler) + { + va_start(ap, fmt); + (*_TIFFerrorHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFerrorHandlerExt) + { + va_start(ap, fmt); + (*_TIFFerrorHandlerExt)(fd, module, fmt, ap); + va_end(ap); + } +} + +void _TIFFErrorEarly(TIFFOpenOptions *opts, thandle_t clientdata, + const char *module, const char *fmt, ...) +{ + va_list ap; + if (opts && opts->errorhandler) + { + va_start(ap, fmt); + int stop = opts->errorhandler(NULL, opts->errorhandler_user_data, + module, fmt, ap); + va_end(ap); + if (stop) + return; + } + if (_TIFFerrorHandler) + { + va_start(ap, fmt); + (*_TIFFerrorHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFerrorHandlerExt) + { + va_start(ap, fmt); + (*_TIFFerrorHandlerExt)(clientdata, module, fmt, ap); + va_end(ap); + } +} + +void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt, ...) +{ + va_list ap; + if (tif && tif->tif_errorhandler) + { + va_start(ap, fmt); + int stop = (*tif->tif_errorhandler)( + tif, tif->tif_errorhandler_user_data, module, fmt, ap); + va_end(ap); + if (stop) + return; + } + if (_TIFFerrorHandler) + { + va_start(ap, fmt); + (*_TIFFerrorHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFerrorHandlerExt) + { + va_start(ap, fmt); + (*_TIFFerrorHandlerExt)(tif ? tif->tif_clientdata : NULL, module, fmt, + ap); + va_end(ap); + } +} diff --git a/cpp/3rd_party/libtiff/tif_extension.c b/cpp/3rd_party/libtiff/tif_extension.c new file mode 100644 index 0000000000..1a09e987a5 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_extension.c @@ -0,0 +1,110 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Various routines support external extension of the tag set, and other + * application extension capabilities. + */ + +#include "tiffiop.h" + +int TIFFGetTagListCount(TIFF *tif) + +{ + TIFFDirectory *td = &tif->tif_dir; + + return td->td_customValueCount; +} + +uint32_t TIFFGetTagListEntry(TIFF *tif, int tag_index) + +{ + TIFFDirectory *td = &tif->tif_dir; + + if (tag_index < 0 || tag_index >= td->td_customValueCount) + return (uint32_t)(-1); + else + return td->td_customValues[tag_index].info->field_tag; +} + +/* +** This provides read/write access to the TIFFTagMethods within the TIFF +** structure to application code without giving access to the private +** TIFF structure. +*/ +TIFFTagMethods *TIFFAccessTagMethods(TIFF *tif) + +{ + return &(tif->tif_tagmethods); +} + +void *TIFFGetClientInfo(TIFF *tif, const char *name) + +{ + TIFFClientInfoLink *psLink = tif->tif_clientinfo; + + while (psLink != NULL && strcmp(psLink->name, name) != 0) + psLink = psLink->next; + + if (psLink != NULL) + return psLink->data; + else + return NULL; +} + +void TIFFSetClientInfo(TIFF *tif, void *data, const char *name) + +{ + TIFFClientInfoLink *psLink = tif->tif_clientinfo; + + /* + ** Do we have an existing link with this name? If so, just + ** set it. + */ + while (psLink != NULL && strcmp(psLink->name, name) != 0) + psLink = psLink->next; + + if (psLink != NULL) + { + psLink->data = data; + return; + } + + /* + ** Create a new link. + */ + + psLink = + (TIFFClientInfoLink *)_TIFFmallocExt(tif, sizeof(TIFFClientInfoLink)); + assert(psLink != NULL); + psLink->next = tif->tif_clientinfo; + psLink->name = (char *)_TIFFmallocExt(tif, (tmsize_t)(strlen(name) + 1)); + assert(psLink->name != NULL); + strcpy(psLink->name, name); + psLink->data = data; + + tif->tif_clientinfo = psLink; +} diff --git a/cpp/3rd_party/libtiff/tif_fax3.c b/cpp/3rd_party/libtiff/tif_fax3.c new file mode 100644 index 0000000000..34e1c7882f --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_fax3.c @@ -0,0 +1,1844 @@ +/* + * Copyright (c) 1990-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef CCITT_SUPPORT +/* + * TIFF Library. + * + * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support. + * + * This file contains support for decoding and encoding TIFF + * compression algorithms 2, 3, 4, and 32771. + * + * Decoder support is derived, with permission, from the code + * in Frank Cringle's viewfax program; + * Copyright (C) 1990, 1995 Frank D. Cringle. + */ +#include "tif_fax3.h" +#define G3CODES +#include "t4.h" +#include + +#ifndef EOF_REACHED_COUNT_THRESHOLD +/* Arbitrary threshold to avoid corrupted single-strip files with extremely + * large imageheight to cause apparently endless looping, such as in + * https://gitlab.com/libtiff/libtiff/-/issues/583 + */ +#define EOF_REACHED_COUNT_THRESHOLD 8192 +#endif + +/* + * Compression+decompression state blocks are + * derived from this ``base state'' block. + */ +typedef struct +{ + int rw_mode; /* O_RDONLY for decode, else encode */ + int mode; /* operating mode */ + tmsize_t rowbytes; /* bytes in a decoded scanline */ + uint32_t rowpixels; /* pixels in a scanline */ + + uint16_t cleanfaxdata; /* CleanFaxData tag */ + uint32_t badfaxrun; /* BadFaxRun tag */ + uint32_t badfaxlines; /* BadFaxLines tag */ + uint32_t groupoptions; /* Group 3/4 options tag */ + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ + TIFFPrintMethod printdir; /* super-class method */ +} Fax3BaseState; +#define Fax3State(tif) ((Fax3BaseState *)(tif)->tif_data) + +typedef enum +{ + G3_1D, + G3_2D +} Ttag; +typedef struct +{ + Fax3BaseState b; + + /* Decoder state info */ + const unsigned char *bitmap; /* bit reversal table */ + uint32_t data; /* current i/o byte/word */ + int bit; /* current i/o bit in byte */ + int EOLcnt; /* count of EOL codes recognized */ + int eofReachedCount; /* number of times decode has been called with + EOF already reached */ + int eolReachedCount; /* number of times decode has been called with + EOL already reached */ + int unexpectedReachedCount; /* number of times decode has been called with + "unexpedted" already reached */ + TIFFFaxFillFunc fill; /* fill routine */ + uint32_t *runs; /* b&w runs for current/previous row */ + uint32_t nruns; /* size of the refruns / curruns arrays */ + uint32_t *refruns; /* runs for reference line */ + uint32_t *curruns; /* runs for current line */ + + /* Encoder state info */ + Ttag tag; /* encoding state */ + unsigned char *refline; /* reference line for 2d decoding */ + int k; /* #rows left that can be 2d encoded */ + int maxk; /* max #rows that can be 2d encoded */ + + int line; +} Fax3CodecState; +#define DecoderState(tif) ((Fax3CodecState *)Fax3State(tif)) +#define EncoderState(tif) ((Fax3CodecState *)Fax3State(tif)) + +#define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING) +#define isAligned(p, t) ((((size_t)(p)) & (sizeof(t) - 1)) == 0) + +/* + * Group 3 and Group 4 Decoding. + */ + +/* + * These macros glue the TIFF library state to + * the state expected by Frank's decoder. + */ +#define DECLARE_STATE(tif, sp, mod) \ + static const char module[] = mod; \ + Fax3CodecState *sp = DecoderState(tif); \ + int a0; /* reference element */ \ + int lastx = sp->b.rowpixels; /* last element in row */ \ + uint32_t BitAcc; /* bit accumulator */ \ + int BitsAvail; /* # valid bits in BitAcc */ \ + int RunLength; /* length of current run */ \ + unsigned char *cp; /* next byte of input data */ \ + unsigned char *ep; /* end of input data */ \ + uint32_t *pa; /* place to stuff next run */ \ + uint32_t *thisrun; /* current row's run array */ \ + int EOLcnt; /* # EOL codes recognized */ \ + const unsigned char *bitmap = sp->bitmap; /* input data bit reverser */ \ + const TIFFFaxTabEnt *TabEnt + +#define DECLARE_STATE_2D(tif, sp, mod) \ + DECLARE_STATE(tif, sp, mod); \ + int b1; /* next change on prev line */ \ + uint32_t \ + *pb /* next run in reference line */ /* \ + * Load any state that may be \ + * changed during decoding. \ + */ +#define CACHE_STATE(tif, sp) \ + do \ + { \ + BitAcc = sp->data; \ + BitsAvail = sp->bit; \ + EOLcnt = sp->EOLcnt; \ + cp = (unsigned char *)tif->tif_rawcp; \ + ep = cp + tif->tif_rawcc; \ + } while (0) +/* + * Save state possibly changed during decoding. + */ +#define UNCACHE_STATE(tif, sp) \ + do \ + { \ + sp->bit = BitsAvail; \ + sp->data = BitAcc; \ + sp->EOLcnt = EOLcnt; \ + tif->tif_rawcc -= (tmsize_t)((uint8_t *)cp - tif->tif_rawcp); \ + tif->tif_rawcp = (uint8_t *)cp; \ + } while (0) + +/* + * Setup state for decoding a strip. + */ +static int Fax3PreDecode(TIFF *tif, uint16_t s) +{ + Fax3CodecState *sp = DecoderState(tif); + + (void)s; + assert(sp != NULL); + sp->bit = 0; /* force initial read */ + sp->data = 0; + sp->EOLcnt = 0; /* force initial scan for EOL */ + sp->eofReachedCount = 0; + sp->eolReachedCount = 0; + sp->unexpectedReachedCount = 0; + /* + * Decoder assumes lsb-to-msb bit order. Note that we select + * this here rather than in Fax3SetupState so that viewers can + * hold the image open, fiddle with the FillOrder tag value, + * and then re-decode the image. Otherwise they'd need to close + * and open the image to get the state reset. + */ + sp->bitmap = + TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB); + sp->curruns = sp->runs; + if (sp->refruns) + { /* init reference line to white */ + sp->refruns = sp->runs + sp->nruns; + sp->refruns[0] = (uint32_t)sp->b.rowpixels; + sp->refruns[1] = 0; + } + sp->line = 0; + return (1); +} + +/* + * Routine for handling various errors/conditions. + * Note how they are "glued into the decoder" by + * overriding the definitions used by the decoder. + */ + +static void Fax3Unexpected(const char *module, TIFF *tif, uint32_t line, + uint32_t a0) +{ + TIFFErrorExtR(tif, module, + "Bad code word at line %" PRIu32 " of %s %" PRIu32 + " (x %" PRIu32 ")", + line, isTiled(tif) ? "tile" : "strip", + (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); +} +#define unexpected(table, a0) \ + do \ + { \ + Fax3Unexpected(module, tif, sp->line, a0); \ + ++sp->unexpectedReachedCount; \ + } while (0) + +static void Fax3Extension(const char *module, TIFF *tif, uint32_t line, + uint32_t a0) +{ + TIFFErrorExtR(tif, module, + "Uncompressed data (not supported) at line %" PRIu32 + " of %s %" PRIu32 " (x %" PRIu32 ")", + line, isTiled(tif) ? "tile" : "strip", + (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); +} +#define extension(a0) Fax3Extension(module, tif, sp->line, a0) + +static void Fax3BadLength(const char *module, TIFF *tif, uint32_t line, + uint32_t a0, uint32_t lastx) +{ + TIFFWarningExtR(tif, module, + "%s at line %" PRIu32 " of %s %" PRIu32 " (got %" PRIu32 + ", expected %" PRIu32 ")", + a0 < lastx ? "Premature EOL" : "Line length mismatch", line, + isTiled(tif) ? "tile" : "strip", + (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0, + lastx); +} +#define badlength(a0, lastx) \ + do \ + { \ + Fax3BadLength(module, tif, sp->line, a0, lastx); \ + ++sp->eolReachedCount; \ + } while (0) + +static void Fax3PrematureEOF(const char *module, TIFF *tif, uint32_t line, + uint32_t a0) +{ + TIFFWarningExtR(tif, module, + "Premature EOF at line %" PRIu32 " of %s %" PRIu32 + " (x %" PRIu32 ")", + line, isTiled(tif) ? "tile" : "strip", + (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); +} +#define prematureEOF(a0) \ + do \ + { \ + Fax3PrematureEOF(module, tif, sp->line, a0); \ + ++sp->eofReachedCount; \ + } while (0) + +static void Fax3TryG3WithoutEOL(const char *module, TIFF *tif, uint32_t line, + uint32_t a0) +{ + TIFFWarningExtR( + tif, module, + "Try to decode (read) fax Group 3 data without EOL at line %" PRIu32 + " of %s %" PRIu32 " (x %" PRIu32 "). Please check result", + line, isTiled(tif) ? "tile" : "strip", + (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); +} +#define tryG3WithoutEOL(a0) \ + do \ + { \ + Fax3TryG3WithoutEOL(module, tif, sp->line, a0); \ + } while (0) + +#define Nop + +static int CheckReachedCounters(TIFF *tif, const char *module, + Fax3CodecState *sp) +{ + if (sp->eofReachedCount >= EOF_REACHED_COUNT_THRESHOLD) + { + TIFFErrorExtR(tif, module, + "End of file (EOF) has already been reached %d times " + "within that %s.", + sp->eofReachedCount, isTiled(tif) ? "tile" : "strip"); + return (-1); + } + if (sp->eolReachedCount >= EOF_REACHED_COUNT_THRESHOLD) + { + TIFFErrorExtR(tif, module, + "Bad line length (EOL) has already been reached %d times " + "within that %s", + sp->eolReachedCount, isTiled(tif) ? "tile" : "strip"); + return (-1); + } + if (sp->unexpectedReachedCount >= EOF_REACHED_COUNT_THRESHOLD) + { + TIFFErrorExtR(tif, module, + "Bad code word (unexpected) has already been reached %d " + "times within that %s", + sp->unexpectedReachedCount, + isTiled(tif) ? "tile" : "strip"); + return (-1); + } + return (0); +} + +/** + * Decode the requested amount of G3 1D-encoded data. + * @param buf destination buffer + * @param occ available bytes in destination buffer + * @param s number of planes (ignored) + * @returns 1 for success, -1 in case of error + */ +static int Fax3Decode1D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) +{ + DECLARE_STATE(tif, sp, "Fax3Decode1D"); + (void)s; + if (occ % sp->b.rowbytes) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return (-1); + } + if (CheckReachedCounters(tif, module, sp)) + return (-1); +RETRY_WITHOUT_EOL_1D: + CACHE_STATE(tif, sp); + thisrun = sp->curruns; + while (occ > 0) + { + a0 = 0; + RunLength = 0; + pa = thisrun; +#ifdef FAX3_DEBUG + printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); + printf("-------------------- %" PRIu32 "\n", tif->tif_row); + fflush(stdout); +#endif + SYNC_EOL(EOF1D, RETRY_WITHOUT_EOL_1D); + EXPAND1D(EOF1Da); + (*sp->fill)(buf, thisrun, pa, lastx); + buf += sp->b.rowbytes; + occ -= sp->b.rowbytes; + sp->line++; + continue; + EOF1D: /* premature EOF */ + CLEANUP_RUNS(); + EOF1Da: /* premature EOF */ + (*sp->fill)(buf, thisrun, pa, lastx); + UNCACHE_STATE(tif, sp); + return (-1); + } + UNCACHE_STATE(tif, sp); + return (1); +} + +#define SWAP(t, a, b) \ + { \ + t x; \ + x = (a); \ + (a) = (b); \ + (b) = x; \ + } +/* + * Decode the requested amount of G3 2D-encoded data. + */ +static int Fax3Decode2D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) +{ + DECLARE_STATE_2D(tif, sp, "Fax3Decode2D"); + int is1D; /* current line is 1d/2d-encoded */ + (void)s; + if (occ % sp->b.rowbytes) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return (-1); + } + if (CheckReachedCounters(tif, module, sp)) + return (-1); +RETRY_WITHOUT_EOL_2D: + CACHE_STATE(tif, sp); + while (occ > 0) + { + a0 = 0; + RunLength = 0; + pa = thisrun = sp->curruns; +#ifdef FAX3_DEBUG + printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d EOLcnt = %d", BitAcc, + BitsAvail, EOLcnt); +#endif + SYNC_EOL(EOF2D, RETRY_WITHOUT_EOL_2D); + NeedBits8(1, EOF2D); + is1D = GetBits(1); /* 1D/2D-encoding tag bit */ + ClrBits(1); +#ifdef FAX3_DEBUG + printf(" %s\n-------------------- %" PRIu32 "\n", is1D ? "1D" : "2D", + tif->tif_row); + fflush(stdout); +#endif + pb = sp->refruns; + b1 = *pb++; + if (is1D) + EXPAND1D(EOF2Da); + else + EXPAND2D(EOF2Da); + (*sp->fill)(buf, thisrun, pa, lastx); + if (pa < thisrun + sp->nruns) + { + SETVALUE(0); /* imaginary change for reference */ + } + SWAP(uint32_t *, sp->curruns, sp->refruns); + buf += sp->b.rowbytes; + occ -= sp->b.rowbytes; + sp->line++; + continue; + EOF2D: /* premature EOF */ + CLEANUP_RUNS(); + EOF2Da: /* premature EOF */ + (*sp->fill)(buf, thisrun, pa, lastx); + UNCACHE_STATE(tif, sp); + return (-1); + } + UNCACHE_STATE(tif, sp); + return (1); +} +#undef SWAP + +#define FILL(n, cp) \ + for (int32_t ifill = 0; ifill < (n); ++ifill) \ + { \ + (cp)[ifill] = 0xff; \ + } \ + (cp) += (n); + +#define ZERO(n, cp) \ + for (int32_t izero = 0; izero < (n); ++izero) \ + { \ + (cp)[izero] = 0; \ + } \ + (cp) += (n); + +/* + * Bit-fill a row according to the white/black + * runs generated during G3/G4 decoding. + */ +void _TIFFFax3fillruns(unsigned char *buf, uint32_t *runs, uint32_t *erun, + uint32_t lastx) +{ + static const unsigned char _fillmasks[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, + 0xf8, 0xfc, 0xfe, 0xff}; + unsigned char *cp; + uint32_t x, bx, run; + int32_t n, nw; + int64_t *lp; + + if ((erun - runs) & 1) + *erun++ = 0; + x = 0; + for (; runs < erun; runs += 2) + { + run = runs[0]; + if (x + run > lastx || run > lastx) + run = runs[0] = (uint32_t)(lastx - x); + if (run) + { + cp = buf + (x >> 3); + bx = x & 7; + if (run > 8 - bx) + { + if (bx) + { /* align to byte boundary */ + *cp++ &= 0xff << (8 - bx); + run -= 8 - bx; + } + if ((n = run >> 3) != 0) + { /* multiple bytes to fill */ + if ((n / sizeof(int64_t)) > 1) + { + /* + * Align to int64_tword boundary and fill. + */ + for (; n && !isAligned(cp, int64_t); n--) + *cp++ = 0x00; + lp = (int64_t *)cp; + nw = (int32_t)(n / sizeof(int64_t)); + n -= nw * sizeof(int64_t); + do + { + *lp++ = 0L; + } while (--nw); + cp = (unsigned char *)lp; + } + ZERO(n, cp); + run &= 7; + } + if (run) + cp[0] &= 0xff >> run; + } + else + cp[0] &= ~(_fillmasks[run] >> bx); + x += runs[0]; + } + run = runs[1]; + if (x + run > lastx || run > lastx) + run = runs[1] = lastx - x; + if (run) + { + cp = buf + (x >> 3); + bx = x & 7; + if (run > 8 - bx) + { + if (bx) + { /* align to byte boundary */ + *cp++ |= 0xff >> bx; + run -= 8 - bx; + } + if ((n = run >> 3) != 0) + { /* multiple bytes to fill */ + if ((n / sizeof(int64_t)) > 1) + { + /* + * Align to int64_t boundary and fill. + */ + for (; n && !isAligned(cp, int64_t); n--) + *cp++ = 0xff; + lp = (int64_t *)cp; + nw = (int32_t)(n / sizeof(int64_t)); + n -= nw * sizeof(int64_t); + do + { + *lp++ = -1L; + } while (--nw); + cp = (unsigned char *)lp; + } + FILL(n, cp); + run &= 7; + } + /* Explicit 0xff masking to make icc -check=conversions happy */ + if (run) + cp[0] = (unsigned char)((cp[0] | (0xff00 >> run)) & 0xff); + } + else + cp[0] |= _fillmasks[run] >> bx; + x += runs[1]; + } + } + assert(x == lastx); +} +#undef ZERO +#undef FILL + +static int Fax3FixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +/* + * Setup G3/G4-related compression/decompression state + * before data is processed. This routine is called once + * per image -- it sets up different state based on whether + * or not decoding or encoding is being done and whether + * 1D- or 2D-encoded data is involved. + */ +static int Fax3SetupState(TIFF *tif) +{ + static const char module[] = "Fax3SetupState"; + TIFFDirectory *td = &tif->tif_dir; + Fax3BaseState *sp = Fax3State(tif); + int needsRefLine; + Fax3CodecState *dsp = (Fax3CodecState *)Fax3State(tif); + tmsize_t rowbytes; + uint32_t rowpixels; + + if (td->td_bitspersample != 1) + { + TIFFErrorExtR(tif, module, + "Bits/sample must be 1 for Group 3/4 encoding/decoding"); + return (0); + } + if (td->td_samplesperpixel != 1 && + td->td_planarconfig != PLANARCONFIG_SEPARATE) + { + TIFFErrorExtR( + tif, module, + "Samples/pixel shall be 1 for Group 3/4 encoding/decoding, " + "or PlanarConfiguration must be set to Separate."); + return 0; + } + /* + * Calculate the scanline/tile widths. + */ + if (isTiled(tif)) + { + rowbytes = TIFFTileRowSize(tif); + rowpixels = td->td_tilewidth; + } + else + { + rowbytes = TIFFScanlineSize(tif); + rowpixels = td->td_imagewidth; + } + if ((int64_t)rowbytes < ((int64_t)rowpixels + 7) / 8) + { + TIFFErrorExtR(tif, module, + "Inconsistent number of bytes per row : rowbytes=%" PRId64 + " rowpixels=%" PRIu32, + (int64_t)rowbytes, rowpixels); + return (0); + } + sp->rowbytes = rowbytes; + sp->rowpixels = rowpixels; + /* + * Allocate any additional space required for decoding/encoding. + */ + needsRefLine = ((sp->groupoptions & GROUP3OPT_2DENCODING) || + td->td_compression == COMPRESSION_CCITTFAX4); + + /* + Assure that allocation computations do not overflow. + + TIFFroundup and TIFFSafeMultiply return zero on integer overflow + */ + if (dsp->runs != NULL) + { + _TIFFfreeExt(tif, dsp->runs); + dsp->runs = (uint32_t *)NULL; + } + dsp->nruns = TIFFroundup_32(rowpixels + 1, 32); + if (needsRefLine) + { + dsp->nruns = TIFFSafeMultiply(uint32_t, dsp->nruns, 2); + } + if ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32_t, dsp->nruns, 2) == 0)) + { + TIFFErrorExtR(tif, tif->tif_name, + "Row pixels integer overflow (rowpixels %" PRIu32 ")", + rowpixels); + return (0); + } + dsp->runs = (uint32_t *)_TIFFCheckMalloc( + tif, TIFFSafeMultiply(uint32_t, dsp->nruns, 2), sizeof(uint32_t), + "for Group 3/4 run arrays"); + if (dsp->runs == NULL) + return (0); + memset(dsp->runs, 0, + TIFFSafeMultiply(uint32_t, dsp->nruns, 2) * sizeof(uint32_t)); + dsp->curruns = dsp->runs; + if (needsRefLine) + dsp->refruns = dsp->runs + dsp->nruns; + else + dsp->refruns = NULL; + if (td->td_compression == COMPRESSION_CCITTFAX3 && is2DEncoding(dsp)) + { /* NB: default is 1D routine */ + tif->tif_decoderow = Fax3Decode2D; + tif->tif_decodestrip = Fax3Decode2D; + tif->tif_decodetile = Fax3Decode2D; + } + + if (needsRefLine) + { /* 2d encoding */ + Fax3CodecState *esp = EncoderState(tif); + /* + * 2d encoding requires a scanline + * buffer for the ``reference line''; the + * scanline against which delta encoding + * is referenced. The reference line must + * be initialized to be ``white'' (done elsewhere). + */ + if (esp->refline != NULL) + { + _TIFFfreeExt(tif, esp->refline); + } + esp->refline = (unsigned char *)_TIFFmallocExt(tif, rowbytes); + if (esp->refline == NULL) + { + TIFFErrorExtR(tif, module, "No space for Group 3/4 reference line"); + return (0); + } + } + else /* 1d encoding */ + EncoderState(tif)->refline = NULL; + + return (1); +} + +/* + * CCITT Group 3 FAX Encoding. + */ + +#define Fax3FlushBits(tif, sp) \ + { \ + if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \ + { \ + if (!TIFFFlushData1(tif)) \ + return 0; \ + } \ + *(tif)->tif_rawcp++ = (uint8_t)(sp)->data; \ + (tif)->tif_rawcc++; \ + (sp)->data = 0, (sp)->bit = 8; \ + } +#define _FlushBits(tif) \ + { \ + if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \ + { \ + if (!TIFFFlushData1(tif)) \ + return 0; \ + } \ + *(tif)->tif_rawcp++ = (uint8_t)data; \ + (tif)->tif_rawcc++; \ + data = 0, bit = 8; \ + } +static const int _msbmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, + 0x1f, 0x3f, 0x7f, 0xff}; +#define _PutBits(tif, bits, length) \ + { \ + while (length > bit) \ + { \ + data |= bits >> (length - bit); \ + length -= bit; \ + _FlushBits(tif); \ + } \ + assert(length < 9); \ + data |= (bits & _msbmask[length]) << (bit - length); \ + bit -= length; \ + if (bit == 0) \ + _FlushBits(tif); \ + } + +/* + * Write a variable-length bit-value to + * the output stream. Values are + * assumed to be at most 16 bits. + */ +static int Fax3PutBits(TIFF *tif, unsigned int bits, unsigned int length) +{ + Fax3CodecState *sp = EncoderState(tif); + unsigned int bit = sp->bit; + int data = sp->data; + + _PutBits(tif, bits, length); + + sp->data = data; + sp->bit = bit; + return 1; +} + +/* + * Write a code to the output stream. + */ +#define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length) + +#ifdef FAX3_DEBUG +#define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B") +#define DEBUG_PRINT(what, len) \ + { \ + int t; \ + printf("%08" PRIX32 "/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), \ + len); \ + for (t = length - 1; t >= 0; t--) \ + putchar(code & (1 << t) ? '1' : '0'); \ + putchar('\n'); \ + } +#endif + +/* + * Write the sequence of codes that describes + * the specified span of zero's or one's. The + * appropriate table that holds the make-up and + * terminating codes is supplied. + */ +static int putspan(TIFF *tif, int32_t span, const tableentry *tab) +{ + Fax3CodecState *sp = EncoderState(tif); + unsigned int bit = sp->bit; + int data = sp->data; + unsigned int code, length; + + while (span >= 2624) + { + const tableentry *te = &tab[63 + (2560 >> 6)]; + code = te->code; + length = te->length; +#ifdef FAX3_DEBUG + DEBUG_PRINT("MakeUp", te->runlen); +#endif + _PutBits(tif, code, length); + span -= te->runlen; + } + if (span >= 64) + { + const tableentry *te = &tab[63 + (span >> 6)]; + assert(te->runlen == 64 * (span >> 6)); + code = te->code; + length = te->length; +#ifdef FAX3_DEBUG + DEBUG_PRINT("MakeUp", te->runlen); +#endif + _PutBits(tif, code, length); + span -= te->runlen; + } + code = tab[span].code; + length = tab[span].length; +#ifdef FAX3_DEBUG + DEBUG_PRINT(" Term", tab[span].runlen); +#endif + _PutBits(tif, code, length); + + sp->data = data; + sp->bit = bit; + + return 1; +} + +/* + * Write an EOL code to the output stream. The zero-fill + * logic for byte-aligning encoded scanlines is handled + * here. We also handle writing the tag bit for the next + * scanline when doing 2d encoding. + */ +static int Fax3PutEOL(TIFF *tif) +{ + Fax3CodecState *sp = EncoderState(tif); + unsigned int bit = sp->bit; + int data = sp->data; + unsigned int code, length, tparm; + + if (sp->b.groupoptions & GROUP3OPT_FILLBITS) + { + /* + * Force bit alignment so EOL will terminate on + * a byte boundary. That is, force the bit alignment + * to 16-12 = 4 before putting out the EOL code. + */ + int align = 8 - 4; + if (align != sp->bit) + { + if (align > sp->bit) + align = sp->bit + (8 - align); + else + align = sp->bit - align; + tparm = align; + _PutBits(tif, 0, tparm); + } + } + code = EOL; + length = 12; + if (is2DEncoding(sp)) + { + code = (code << 1) | (sp->tag == G3_1D); + length++; + } + _PutBits(tif, code, length); + + sp->data = data; + sp->bit = bit; + + return 1; +} + +/* + * Reset encoding state at the start of a strip. + */ +static int Fax3PreEncode(TIFF *tif, uint16_t s) +{ + Fax3CodecState *sp = EncoderState(tif); + + (void)s; + assert(sp != NULL); + sp->bit = 8; + sp->data = 0; + sp->tag = G3_1D; + /* + * This is necessary for Group 4; otherwise it isn't + * needed because the first scanline of each strip ends + * up being copied into the refline. + */ + if (sp->refline) + _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes); + if (is2DEncoding(sp)) + { + float res = tif->tif_dir.td_yresolution; + /* + * The CCITT spec says that when doing 2d encoding, you + * should only do it on K consecutive scanlines, where K + * depends on the resolution of the image being encoded + * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory + * code initializes td_yresolution to 0, this code will + * select a K of 2 unless the YResolution tag is set + * appropriately. (Note also that we fudge a little here + * and use 150 lpi to avoid problems with units conversion.) + */ + if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER) + res *= 2.54f; /* convert to inches */ + sp->maxk = (res > 150 ? 4 : 2); + sp->k = sp->maxk - 1; + } + else + sp->k = sp->maxk = 0; + sp->line = 0; + return (1); +} + +static const unsigned char zeroruns[256] = { + 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */ +}; +static const unsigned char oneruns[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */ + 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */ +}; + +/* + * Find a span of ones or zeros using the supplied + * table. The ``base'' of the bit string is supplied + * along with the start+end bit indices. + */ +static inline int32_t find0span(unsigned char *bp, int32_t bs, int32_t be) +{ + int32_t bits = be - bs; + int32_t n, span; + + bp += bs >> 3; + /* + * Check partial byte on lhs. + */ + if (bits > 0 && (n = (bs & 7)) != 0) + { + span = zeroruns[(*bp << n) & 0xff]; + if (span > 8 - n) /* table value too generous */ + span = 8 - n; + if (span > bits) /* constrain span to bit range */ + span = bits; + if (n + span < 8) /* doesn't extend to edge of byte */ + return (span); + bits -= span; + bp++; + } + else + span = 0; + if (bits >= (int32_t)(2 * 8 * sizeof(int64_t))) + { + int64_t *lp; + /* + * Align to int64_t boundary and check int64_t words. + */ + while (!isAligned(bp, int64_t)) + { + if (*bp != 0x00) + return (span + zeroruns[*bp]); + span += 8; + bits -= 8; + bp++; + } + lp = (int64_t *)bp; + while ((bits >= (int32_t)(8 * sizeof(int64_t))) && (0 == *lp)) + { + span += 8 * sizeof(int64_t); + bits -= 8 * sizeof(int64_t); + lp++; + } + bp = (unsigned char *)lp; + } + /* + * Scan full bytes for all 0's. + */ + while (bits >= 8) + { + if (*bp != 0x00) /* end of run */ + return (span + zeroruns[*bp]); + span += 8; + bits -= 8; + bp++; + } + /* + * Check partial byte on rhs. + */ + if (bits > 0) + { + n = zeroruns[*bp]; + span += (n > bits ? bits : n); + } + return (span); +} + +static inline int32_t find1span(unsigned char *bp, int32_t bs, int32_t be) +{ + int32_t bits = be - bs; + int32_t n, span; + + bp += bs >> 3; + /* + * Check partial byte on lhs. + */ + if (bits > 0 && (n = (bs & 7)) != 0) + { + span = oneruns[(*bp << n) & 0xff]; + if (span > 8 - n) /* table value too generous */ + span = 8 - n; + if (span > bits) /* constrain span to bit range */ + span = bits; + if (n + span < 8) /* doesn't extend to edge of byte */ + return (span); + bits -= span; + bp++; + } + else + span = 0; + if (bits >= (int32_t)(2 * 8 * sizeof(int64_t))) + { + int64_t *lp; + /* + * Align to int64_t boundary and check int64_t words. + */ + while (!isAligned(bp, int64_t)) + { + if (*bp != 0xff) + return (span + oneruns[*bp]); + span += 8; + bits -= 8; + bp++; + } + lp = (int64_t *)bp; + while ((bits >= (int32_t)(8 * sizeof(int64_t))) && + (~((uint64_t)0) == (uint64_t)*lp)) + { + span += 8 * sizeof(int64_t); + bits -= 8 * sizeof(int64_t); + lp++; + } + bp = (unsigned char *)lp; + } + /* + * Scan full bytes for all 1's. + */ + while (bits >= 8) + { + if (*bp != 0xff) /* end of run */ + return (span + oneruns[*bp]); + span += 8; + bits -= 8; + bp++; + } + /* + * Check partial byte on rhs. + */ + if (bits > 0) + { + n = oneruns[*bp]; + span += (n > bits ? bits : n); + } + return (span); +} + +/* + * Return the offset of the next bit in the range + * [bs..be] that is different from the specified + * color. The end, be, is returned if no such bit + * exists. + */ +#define finddiff(_cp, _bs, _be, _color) \ + (_bs + (_color ? find1span(_cp, _bs, _be) : find0span(_cp, _bs, _be))) +/* + * Like finddiff, but also check the starting bit + * against the end in case start > end. + */ +#define finddiff2(_cp, _bs, _be, _color) \ + (_bs < _be ? finddiff(_cp, _bs, _be, _color) : _be) + +/* + * 1d-encode a row of pixels. The encoding is + * a sequence of all-white or all-black spans + * of pixels encoded with Huffman codes. + */ +static int Fax3Encode1DRow(TIFF *tif, unsigned char *bp, uint32_t bits) +{ + Fax3CodecState *sp = EncoderState(tif); + int32_t span; + uint32_t bs = 0; + + for (;;) + { + span = find0span(bp, bs, bits); /* white span */ + if (!putspan(tif, span, TIFFFaxWhiteCodes)) + return 0; + bs += span; + if (bs >= bits) + break; + span = find1span(bp, bs, bits); /* black span */ + if (!putspan(tif, span, TIFFFaxBlackCodes)) + return 0; + bs += span; + if (bs >= bits) + break; + } + if (sp->b.mode & (FAXMODE_BYTEALIGN | FAXMODE_WORDALIGN)) + { + if (sp->bit != 8) /* byte-align */ + Fax3FlushBits(tif, sp); + if ((sp->b.mode & FAXMODE_WORDALIGN) && + !isAligned(tif->tif_rawcp, uint16_t)) + Fax3FlushBits(tif, sp); + } + return (1); +} + +static const tableentry horizcode = {3, 0x1, 0}; /* 001 */ +static const tableentry passcode = {4, 0x1, 0}; /* 0001 */ +static const tableentry vcodes[7] = { + {7, 0x03, 0}, /* 0000 011 */ + {6, 0x03, 0}, /* 0000 11 */ + {3, 0x03, 0}, /* 011 */ + {1, 0x1, 0}, /* 1 */ + {3, 0x2, 0}, /* 010 */ + {6, 0x02, 0}, /* 0000 10 */ + {7, 0x02, 0} /* 0000 010 */ +}; + +/* + * 2d-encode a row of pixels. Consult the CCITT + * documentation for the algorithm. + */ +static int Fax3Encode2DRow(TIFF *tif, unsigned char *bp, unsigned char *rp, + uint32_t bits) +{ +#define PIXEL(buf, ix) ((((buf)[(ix) >> 3]) >> (7 - ((ix)&7))) & 1) + uint32_t a0 = 0; + uint32_t a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0)); + uint32_t b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0)); + uint32_t a2, b2; + + for (;;) + { + b2 = finddiff2(rp, b1, bits, PIXEL(rp, b1)); + if (b2 >= a1) + { + /* Naive computation triggers + * -fsanitize=undefined,unsigned-integer-overflow */ + /* although it is correct unless the difference between both is < 31 + * bit */ + /* int32_t d = b1 - a1; */ + int32_t d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32_t)(b1 - a1) + : (b1 < a1 && a1 - b1 <= 3U) ? -(int32_t)(a1 - b1) + : 0x7FFFFFFF; + if (!(-3 <= d && d <= 3)) + { /* horizontal mode */ + a2 = finddiff2(bp, a1, bits, PIXEL(bp, a1)); + if (!putcode(tif, &horizcode)) + return 0; + if (a0 + a1 == 0 || PIXEL(bp, a0) == 0) + { + if (!putspan(tif, a1 - a0, TIFFFaxWhiteCodes)) + return 0; + if (!putspan(tif, a2 - a1, TIFFFaxBlackCodes)) + return 0; + } + else + { + if (!putspan(tif, a1 - a0, TIFFFaxBlackCodes)) + return 0; + if (!putspan(tif, a2 - a1, TIFFFaxWhiteCodes)) + return 0; + } + a0 = a2; + } + else + { /* vertical mode */ + if (!putcode(tif, &vcodes[d + 3])) + return 0; + a0 = a1; + } + } + else + { /* pass mode */ + if (!putcode(tif, &passcode)) + return 0; + a0 = b2; + } + if (a0 >= bits) + break; + a1 = finddiff(bp, a0, bits, PIXEL(bp, a0)); + b1 = finddiff(rp, a0, bits, !PIXEL(bp, a0)); + b1 = finddiff(rp, b1, bits, PIXEL(bp, a0)); + } + return (1); +#undef PIXEL +} + +/* + * Encode a buffer of pixels. + */ +static int Fax3Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "Fax3Encode"; + Fax3CodecState *sp = EncoderState(tif); + (void)s; + if (cc % sp->b.rowbytes) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written"); + return (0); + } + while (cc > 0) + { + if ((sp->b.mode & FAXMODE_NOEOL) == 0) + { + if (!Fax3PutEOL(tif)) + return 0; + } + if (is2DEncoding(sp)) + { + if (sp->tag == G3_1D) + { + if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels)) + return (0); + sp->tag = G3_2D; + } + else + { + if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels)) + return (0); + sp->k--; + } + if (sp->k == 0) + { + sp->tag = G3_1D; + sp->k = sp->maxk - 1; + } + else + _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes); + } + else + { + if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels)) + return (0); + } + bp += sp->b.rowbytes; + cc -= sp->b.rowbytes; + } + return (1); +} + +static int Fax3PostEncode(TIFF *tif) +{ + Fax3CodecState *sp = EncoderState(tif); + + if (sp->bit != 8) + Fax3FlushBits(tif, sp); + return (1); +} + +static int _Fax3Close(TIFF *tif) +{ + if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp) + { + Fax3CodecState *sp = EncoderState(tif); + unsigned int code = EOL; + unsigned int length = 12; + int i; + + if (is2DEncoding(sp)) + { + code = (code << 1) | (sp->tag == G3_1D); + length++; + } + for (i = 0; i < 6; i++) + Fax3PutBits(tif, code, length); + Fax3FlushBits(tif, sp); + } + return 1; +} + +static void Fax3Close(TIFF *tif) { _Fax3Close(tif); } + +static void Fax3Cleanup(TIFF *tif) +{ + Fax3CodecState *sp = DecoderState(tif); + + assert(sp != 0); + + tif->tif_tagmethods.vgetfield = sp->b.vgetparent; + tif->tif_tagmethods.vsetfield = sp->b.vsetparent; + tif->tif_tagmethods.printdir = sp->b.printdir; + + if (sp->runs) + _TIFFfreeExt(tif, sp->runs); + if (sp->refline) + _TIFFfreeExt(tif, sp->refline); + + _TIFFfreeExt(tif, tif->tif_data); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +#define FIELD_BADFAXLINES (FIELD_CODEC + 0) +#define FIELD_CLEANFAXDATA (FIELD_CODEC + 1) +#define FIELD_BADFAXRUN (FIELD_CODEC + 2) + +#define FIELD_OPTIONS (FIELD_CODEC + 7) + +static const TIFFField faxFields[] = { + {TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, FALSE, + FALSE, "FaxMode", NULL}, + {TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, FIELD_PSEUDO, + FALSE, FALSE, "FaxFillFunc", NULL}, + {TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, + FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL}, + {TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, + FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL}, + {TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, + FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", NULL}}; +static const TIFFField fax3Fields[] = { + {TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, + FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL}, +}; +static const TIFFField fax4Fields[] = { + {TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, + FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL}, +}; + +static int Fax3VSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + Fax3BaseState *sp = Fax3State(tif); + const TIFFField *fip; + + assert(sp != 0); + assert(sp->vsetparent != 0); + + switch (tag) + { + case TIFFTAG_FAXMODE: + sp->mode = (int)va_arg(ap, int); + return 1; /* NB: pseudo tag */ + case TIFFTAG_FAXFILLFUNC: + DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc); + return 1; /* NB: pseudo tag */ + case TIFFTAG_GROUP3OPTIONS: + /* XXX: avoid reading options if compression mismatches. */ + if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3) + sp->groupoptions = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_GROUP4OPTIONS: + /* XXX: avoid reading options if compression mismatches. */ + if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) + sp->groupoptions = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_BADFAXLINES: + sp->badfaxlines = (uint32_t)va_arg(ap, uint32_t); + break; + case TIFFTAG_CLEANFAXDATA: + sp->cleanfaxdata = (uint16_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_CONSECUTIVEBADFAXLINES: + sp->badfaxrun = (uint32_t)va_arg(ap, uint32_t); + break; + default: + return (*sp->vsetparent)(tif, tag, ap); + } + + if ((fip = TIFFFieldWithTag(tif, tag)) != NULL) + TIFFSetFieldBit(tif, fip->field_bit); + else + return 0; + + tif->tif_flags |= TIFF_DIRTYDIRECT; + return 1; +} + +static int Fax3VGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + Fax3BaseState *sp = Fax3State(tif); + + assert(sp != 0); + + switch (tag) + { + case TIFFTAG_FAXMODE: + *va_arg(ap, int *) = sp->mode; + break; + case TIFFTAG_FAXFILLFUNC: + *va_arg(ap, TIFFFaxFillFunc *) = DecoderState(tif)->fill; + break; + case TIFFTAG_GROUP3OPTIONS: + case TIFFTAG_GROUP4OPTIONS: + *va_arg(ap, uint32_t *) = sp->groupoptions; + break; + case TIFFTAG_BADFAXLINES: + *va_arg(ap, uint32_t *) = sp->badfaxlines; + break; + case TIFFTAG_CLEANFAXDATA: + *va_arg(ap, uint16_t *) = sp->cleanfaxdata; + break; + case TIFFTAG_CONSECUTIVEBADFAXLINES: + *va_arg(ap, uint32_t *) = sp->badfaxrun; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return (1); +} + +static void Fax3PrintDir(TIFF *tif, FILE *fd, long flags) +{ + Fax3BaseState *sp = Fax3State(tif); + + assert(sp != 0); + + (void)flags; + if (TIFFFieldSet(tif, FIELD_OPTIONS)) + { + const char *sep = " "; + if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) + { + fprintf(fd, " Group 4 Options:"); + if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED) + fprintf(fd, "%suncompressed data", sep); + } + else + { + + fprintf(fd, " Group 3 Options:"); + if (sp->groupoptions & GROUP3OPT_2DENCODING) + { + fprintf(fd, "%s2-d encoding", sep); + sep = "+"; + } + if (sp->groupoptions & GROUP3OPT_FILLBITS) + { + fprintf(fd, "%sEOL padding", sep); + sep = "+"; + } + if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED) + fprintf(fd, "%suncompressed data", sep); + } + fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n", sp->groupoptions, + sp->groupoptions); + } + if (TIFFFieldSet(tif, FIELD_CLEANFAXDATA)) + { + fprintf(fd, " Fax Data:"); + switch (sp->cleanfaxdata) + { + case CLEANFAXDATA_CLEAN: + fprintf(fd, " clean"); + break; + case CLEANFAXDATA_REGENERATED: + fprintf(fd, " receiver regenerated"); + break; + case CLEANFAXDATA_UNCLEAN: + fprintf(fd, " uncorrected errors"); + break; + } + fprintf(fd, " (%" PRIu16 " = 0x%" PRIx16 ")\n", sp->cleanfaxdata, + sp->cleanfaxdata); + } + if (TIFFFieldSet(tif, FIELD_BADFAXLINES)) + fprintf(fd, " Bad Fax Lines: %" PRIu32 "\n", sp->badfaxlines); + if (TIFFFieldSet(tif, FIELD_BADFAXRUN)) + fprintf(fd, " Consecutive Bad Fax Lines: %" PRIu32 "\n", + sp->badfaxrun); + if (sp->printdir) + (*sp->printdir)(tif, fd, flags); +} + +static int InitCCITTFax3(TIFF *tif) +{ + static const char module[] = "InitCCITTFax3"; + Fax3BaseState *sp; + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields))) + { + TIFFErrorExtR(tif, "InitCCITTFax3", + "Merging common CCITT Fax codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(Fax3CodecState)); + + if (tif->tif_data == NULL) + { + TIFFErrorExtR(tif, module, "No space for state block"); + return (0); + } + _TIFFmemset(tif->tif_data, 0, sizeof(Fax3CodecState)); + + sp = Fax3State(tif); + sp->rw_mode = tif->tif_mode; + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */ + sp->printdir = tif->tif_tagmethods.printdir; + tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */ + sp->groupoptions = 0; + + if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */ + tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */ + DecoderState(tif)->runs = NULL; + TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns); + EncoderState(tif)->refline = NULL; + + /* + * Install codec methods. + */ + tif->tif_fixuptags = Fax3FixupTags; + tif->tif_setupdecode = Fax3SetupState; + tif->tif_predecode = Fax3PreDecode; + tif->tif_decoderow = Fax3Decode1D; + tif->tif_decodestrip = Fax3Decode1D; + tif->tif_decodetile = Fax3Decode1D; + tif->tif_setupencode = Fax3SetupState; + tif->tif_preencode = Fax3PreEncode; + tif->tif_postencode = Fax3PostEncode; + tif->tif_encoderow = Fax3Encode; + tif->tif_encodestrip = Fax3Encode; + tif->tif_encodetile = Fax3Encode; + tif->tif_close = Fax3Close; + tif->tif_cleanup = Fax3Cleanup; + + return (1); +} + +int TIFFInitCCITTFax3(TIFF *tif, int scheme) +{ + (void)scheme; + if (InitCCITTFax3(tif)) + { + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, fax3Fields, TIFFArrayCount(fax3Fields))) + { + TIFFErrorExtR(tif, "TIFFInitCCITTFax3", + "Merging CCITT Fax 3 codec-specific tags failed"); + return 0; + } + + /* + * The default format is Class/F-style w/o RTC. + */ + return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF); + } + else + return 01; +} + +/* + * CCITT Group 4 (T.6) Facsimile-compatible + * Compression Scheme Support. + */ + +#define SWAP(t, a, b) \ + { \ + t x; \ + x = (a); \ + (a) = (b); \ + (b) = x; \ + } +/* + * Decode the requested amount of G4-encoded data. + */ +static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) +{ + DECLARE_STATE_2D(tif, sp, "Fax4Decode"); + (void)s; + if (occ % sp->b.rowbytes) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return (-1); + } + if (CheckReachedCounters(tif, module, sp)) + return (-1); + CACHE_STATE(tif, sp); + int start = sp->line; + while (occ > 0) + { + a0 = 0; + RunLength = 0; + pa = thisrun = sp->curruns; + pb = sp->refruns; + b1 = *pb++; +#ifdef FAX3_DEBUG + printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); + printf("-------------------- %d\n", tif->tif_row); + fflush(stdout); +#endif + EXPAND2D(EOFG4); + if (EOLcnt) + goto EOFG4; + if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */ + { + TIFFErrorExtR(tif, module, + "Buffer overrun detected : %" TIFF_SSIZE_FORMAT + " bytes available, %d bits needed", + occ, lastx); + return -1; + } + (*sp->fill)(buf, thisrun, pa, lastx); + SETVALUE(0); /* imaginary change for reference */ + SWAP(uint32_t *, sp->curruns, sp->refruns); + buf += sp->b.rowbytes; + occ -= sp->b.rowbytes; + sp->line++; + continue; + EOFG4: + NeedBits16(13, BADG4); + BADG4: +#ifdef FAX3_DEBUG + if (GetBits(13) != 0x1001) + fputs("Bad EOFB\n", stderr); +#endif + ClrBits(13); + if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */ + { + TIFFErrorExtR(tif, module, + "Buffer overrun detected : %" TIFF_SSIZE_FORMAT + " bytes available, %d bits needed", + occ, lastx); + return -1; + } + (*sp->fill)(buf, thisrun, pa, lastx); + UNCACHE_STATE(tif, sp); + return (sp->line != start + ? 1 + : -1); /* don't error on badly-terminated strips */ + } + UNCACHE_STATE(tif, sp); + return (1); +} +#undef SWAP + +/* + * Encode the requested amount of data. + */ +static int Fax4Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "Fax4Encode"; + Fax3CodecState *sp = EncoderState(tif); + (void)s; + if (cc % sp->b.rowbytes) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written"); + return (0); + } + while (cc > 0) + { + if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels)) + return (0); + _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes); + bp += sp->b.rowbytes; + cc -= sp->b.rowbytes; + } + return (1); +} + +static int Fax4PostEncode(TIFF *tif) +{ + Fax3CodecState *sp = EncoderState(tif); + + /* terminate strip w/ EOFB */ + Fax3PutBits(tif, EOL, 12); + Fax3PutBits(tif, EOL, 12); + if (sp->bit != 8) + Fax3FlushBits(tif, sp); + return (1); +} + +int TIFFInitCCITTFax4(TIFF *tif, int scheme) +{ + (void)scheme; + if (InitCCITTFax3(tif)) + { /* reuse G3 support */ + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, fax4Fields, TIFFArrayCount(fax4Fields))) + { + TIFFErrorExtR(tif, "TIFFInitCCITTFax4", + "Merging CCITT Fax 4 codec-specific tags failed"); + return 0; + } + + tif->tif_decoderow = Fax4Decode; + tif->tif_decodestrip = Fax4Decode; + tif->tif_decodetile = Fax4Decode; + tif->tif_encoderow = Fax4Encode; + tif->tif_encodestrip = Fax4Encode; + tif->tif_encodetile = Fax4Encode; + tif->tif_postencode = Fax4PostEncode; + /* + * Suppress RTC at the end of each strip. + */ + return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC); + } + else + return (0); +} + +/* + * CCITT Group 3 1-D Modified Huffman RLE Compression Support. + * (Compression algorithms 2 and 32771) + */ + +/* + * Decode the requested amount of RLE-encoded data. + */ +static int Fax3DecodeRLE(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) +{ + DECLARE_STATE(tif, sp, "Fax3DecodeRLE"); + int mode = sp->b.mode; + (void)s; + if (occ % sp->b.rowbytes) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return (-1); + } + if (CheckReachedCounters(tif, module, sp)) + return (-1); + CACHE_STATE(tif, sp); + thisrun = sp->curruns; + while (occ > 0) + { + a0 = 0; + RunLength = 0; + pa = thisrun; +#ifdef FAX3_DEBUG + printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); + printf("-------------------- %" PRIu32 "\n", tif->tif_row); + fflush(stdout); +#endif + EXPAND1D(EOFRLE); + (*sp->fill)(buf, thisrun, pa, lastx); + /* + * Cleanup at the end of the row. + */ + if (mode & FAXMODE_BYTEALIGN) + { + int n = BitsAvail - (BitsAvail & ~7); + ClrBits(n); + } + else if (mode & FAXMODE_WORDALIGN) + { + int n = BitsAvail - (BitsAvail & ~15); + ClrBits(n); + if (BitsAvail == 0 && !isAligned(cp, uint16_t)) + cp++; + } + buf += sp->b.rowbytes; + occ -= sp->b.rowbytes; + sp->line++; + continue; + EOFRLE: /* premature EOF */ + (*sp->fill)(buf, thisrun, pa, lastx); + UNCACHE_STATE(tif, sp); + return (-1); + } + UNCACHE_STATE(tif, sp); + return (1); +} + +int TIFFInitCCITTRLE(TIFF *tif, int scheme) +{ + (void)scheme; + if (InitCCITTFax3(tif)) + { /* reuse G3 support */ + tif->tif_decoderow = Fax3DecodeRLE; + tif->tif_decodestrip = Fax3DecodeRLE; + tif->tif_decodetile = Fax3DecodeRLE; + /* + * Suppress RTC+EOLs when encoding and byte-align data. + */ + return TIFFSetField(tif, TIFFTAG_FAXMODE, + FAXMODE_NORTC | FAXMODE_NOEOL | FAXMODE_BYTEALIGN); + } + else + return (0); +} + +int TIFFInitCCITTRLEW(TIFF *tif, int scheme) +{ + (void)scheme; + if (InitCCITTFax3(tif)) + { /* reuse G3 support */ + tif->tif_decoderow = Fax3DecodeRLE; + tif->tif_decodestrip = Fax3DecodeRLE; + tif->tif_decodetile = Fax3DecodeRLE; + /* + * Suppress RTC+EOLs when encoding and word-align data. + */ + return TIFFSetField(tif, TIFFTAG_FAXMODE, + FAXMODE_NORTC | FAXMODE_NOEOL | FAXMODE_WORDALIGN); + } + else + return (0); +} +#endif /* CCITT_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_fax3.h b/cpp/3rd_party/libtiff/tif_fax3.h new file mode 100644 index 0000000000..c3100ce2c6 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_fax3.h @@ -0,0 +1,657 @@ +/* + * Copyright (c) 1990-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _FAX3_ +#define _FAX3_ +/* + * TIFF Library. + * + * CCITT Group 3 (T.4) and Group 4 (T.6) Decompression Support. + * + * Decoder support is derived, with permission, from the code + * in Frank Cringle's viewfax program; + * Copyright (C) 1990, 1995 Frank D. Cringle. + */ +#include "tiff.h" + +/* + * To override the default routine used to image decoded + * spans one can use the pseudo tag TIFFTAG_FAXFILLFUNC. + * The routine must have the type signature given below; + * for example: + * + * fillruns(unsigned char* buf, uint32_t* runs, uint32_t* erun, uint32_t lastx) + * + * where buf is place to set the bits, runs is the array of b&w run + * lengths (white then black), erun is the last run in the array, and + * lastx is the width of the row in pixels. Fill routines can assume + * the run array has room for at least lastx runs and can overwrite + * data in the run array as needed (e.g. to append zero runs to bring + * the count up to a nice multiple). + */ +typedef void (*TIFFFaxFillFunc)(unsigned char *, uint32_t *, uint32_t *, + uint32_t); + +/* + * The default run filler; made external for other decoders. + */ +#if defined(__cplusplus) +extern "C" +{ +#endif + extern void _TIFFFax3fillruns(unsigned char *, uint32_t *, uint32_t *, + uint32_t); +#if defined(__cplusplus) +} +#endif + +/* finite state machine codes */ +#define S_Null 0 +#define S_Pass 1 +#define S_Horiz 2 +#define S_V0 3 +#define S_VR 4 +#define S_VL 5 +#define S_Ext 6 +#define S_TermW 7 +#define S_TermB 8 +#define S_MakeUpW 9 +#define S_MakeUpB 10 +#define S_MakeUp 11 +#define S_EOL 12 + +/* WARNING: do not change the layout of this structure as the HylaFAX software + */ +/* really depends on it. See http://bugzilla.maptools.org/show_bug.cgi?id=2636 + */ +typedef struct +{ /* state table entry */ + unsigned char State; /* see above */ + unsigned char Width; /* width of code in bits */ + uint32_t Param; /* unsigned 32-bit run length in bits (holds on 16 bit + actually, but cannot be changed. See above warning) */ +} TIFFFaxTabEnt; + +extern const TIFFFaxTabEnt TIFFFaxMainTable[]; +extern const TIFFFaxTabEnt TIFFFaxWhiteTable[]; +extern const TIFFFaxTabEnt TIFFFaxBlackTable[]; + +/* + * The following macros define the majority of the G3/G4 decoder + * algorithm using the state tables defined elsewhere. To build + * a decoder you need some setup code and some glue code. Note + * that you may also need/want to change the way the NeedBits* + * macros get input data if, for example, you know the data to be + * decoded is properly aligned and oriented (doing so before running + * the decoder can be a big performance win). + * + * Consult the decoder in the TIFF library for an idea of what you + * need to define and setup to make use of these definitions. + * + * NB: to enable a debugging version of these macros define FAX3_DEBUG + * before including this file. Trace output goes to stdout. + */ + +#ifndef EndOfData +#define EndOfData() (cp >= ep) +#endif +/* + * Need <=8 or <=16 bits of input data. Unlike viewfax we + * cannot use/assume a word-aligned, properly bit swizzled + * input data set because data may come from an arbitrarily + * aligned, read-only source such as a memory-mapped file. + * Note also that the viewfax decoder does not check for + * running off the end of the input data buffer. This is + * possible for G3-encoded data because it prescans the input + * data to count EOL markers, but can cause problems for G4 + * data. In any event, we don't prescan and must watch for + * running out of data since we can't permit the library to + * scan past the end of the input data buffer. + * + * Finally, note that we must handle remaindered data at the end + * of a strip specially. The coder asks for a fixed number of + * bits when scanning for the next code. This may be more bits + * than are actually present in the data stream. If we appear + * to run out of data but still have some number of valid bits + * remaining then we makeup the requested amount with zeros and + * return successfully. If the returned data is incorrect then + * we should be called again and get a premature EOF error; + * otherwise we should get the right answer. + */ +#ifndef NeedBits8 +#define NeedBits8(n, eoflab) \ + do \ + { \ + if (BitsAvail < (n)) \ + { \ + if (EndOfData()) \ + { \ + if (BitsAvail == 0) /* no valid bits */ \ + goto eoflab; \ + BitsAvail = (n); /* pad with zeros */ \ + } \ + else \ + { \ + BitAcc |= ((uint32_t)bitmap[*cp++]) << BitsAvail; \ + BitsAvail += 8; \ + } \ + } \ + } while (0) +#endif +#ifndef NeedBits16 +#define NeedBits16(n, eoflab) \ + do \ + { \ + if (BitsAvail < (n)) \ + { \ + if (EndOfData()) \ + { \ + if (BitsAvail == 0) /* no valid bits */ \ + goto eoflab; \ + BitsAvail = (n); /* pad with zeros */ \ + } \ + else \ + { \ + BitAcc |= ((uint32_t)bitmap[*cp++]) << BitsAvail; \ + if ((BitsAvail += 8) < (n)) \ + { \ + if (EndOfData()) \ + { \ + /* NB: we know BitsAvail is non-zero here */ \ + BitsAvail = (n); /* pad with zeros */ \ + } \ + else \ + { \ + BitAcc |= ((uint32_t)bitmap[*cp++]) << BitsAvail; \ + BitsAvail += 8; \ + } \ + } \ + } \ + } \ + } while (0) +#endif +#define GetBits(n) (BitAcc & ((1 << (n)) - 1)) +#define ClrBits(n) \ + do \ + { \ + BitsAvail -= (n); \ + BitAcc >>= (n); \ + } while (0) + +#ifdef FAX3_DEBUG +static const char *StateNames[] = { + "Null ", "Pass ", "Horiz ", "V0 ", "VR ", "VL ", "Ext ", + "TermW ", "TermB ", "MakeUpW", "MakeUpB", "MakeUp ", "EOL ", +}; +#define DEBUG_SHOW putchar(BitAcc & (1 << t) ? '1' : '0') +#define LOOKUP8(wid, tab, eoflab) \ + do \ + { \ + int t; \ + NeedBits8(wid, eoflab); \ + TabEnt = tab + GetBits(wid); \ + printf("%08lX/%d: %s%5d\t", (long)BitAcc, BitsAvail, \ + StateNames[TabEnt->State], TabEnt->Param); \ + for (t = 0; t < TabEnt->Width; t++) \ + DEBUG_SHOW; \ + putchar('\n'); \ + fflush(stdout); \ + ClrBits(TabEnt->Width); \ + } while (0) +#define LOOKUP16(wid, tab, eoflab) \ + do \ + { \ + int t; \ + NeedBits16(wid, eoflab); \ + TabEnt = tab + GetBits(wid); \ + printf("%08lX/%d: %s%5d\t", (long)BitAcc, BitsAvail, \ + StateNames[TabEnt->State], TabEnt->Param); \ + for (t = 0; t < TabEnt->Width; t++) \ + DEBUG_SHOW; \ + putchar('\n'); \ + fflush(stdout); \ + ClrBits(TabEnt->Width); \ + } while (0) + +#define SETVALUE(x) \ + do \ + { \ + *pa++ = RunLength + (x); \ + printf("SETVALUE: %d\t%d\n", RunLength + (x), a0); \ + a0 += x; \ + RunLength = 0; \ + } while (0) +#else +#define LOOKUP8(wid, tab, eoflab) \ + do \ + { \ + NeedBits8(wid, eoflab); \ + TabEnt = tab + GetBits(wid); \ + ClrBits(TabEnt->Width); \ + } while (0) +#define LOOKUP16(wid, tab, eoflab) \ + do \ + { \ + NeedBits16(wid, eoflab); \ + TabEnt = tab + GetBits(wid); \ + ClrBits(TabEnt->Width); \ + } while (0) + +/* + * Append a run to the run length array for the + * current row and reset decoding state. + */ +#define SETVALUE(x) \ + do \ + { \ + if (pa >= thisrun + sp->nruns) \ + { \ + TIFFErrorExtR(tif, module, "Buffer overflow at line %u of %s %u", \ + sp->line, isTiled(tif) ? "tile" : "strip", \ + isTiled(tif) ? tif->tif_curtile \ + : tif->tif_curstrip); \ + return (-1); \ + } \ + *pa++ = RunLength + (x); \ + a0 += (x); \ + RunLength = 0; \ + } while (0) +#endif + +/* + * Synchronize input decoding at the start of each + * row by scanning for an EOL (if appropriate) and + * skipping any trash data that might be present + * after a decoding error. Note that the decoding + * done elsewhere that recognizes an EOL only consumes + * 11 consecutive zero bits. This means that if EOLcnt + * is non-zero then we still need to scan for the final flag + * bit that is part of the EOL code. + */ +#define SYNC_EOL(eoflab, retrywithouteol) \ + do \ + { \ + if (!(sp->b.mode & FAXMODE_NOEOL)) /* skip EOL, if not present */ \ + { \ + if (EOLcnt == 0) \ + { \ + for (;;) \ + { \ + NeedBits16(11, eoflab); \ + if (GetBits(11) == 0) \ + break; /* EOL found */ \ + ClrBits(1); \ + } \ + } \ + /* Now move after EOL or detect missing EOL. */ \ + for (;;) \ + { \ + NeedBits8(8, noEOLFound); \ + if (GetBits(8)) \ + break; \ + ClrBits(8); \ + } \ + while (GetBits(1) == 0) \ + ClrBits(1); \ + ClrBits(1); /* EOL bit */ \ + EOLcnt = 0; /* reset EOL counter/flag */ \ + break; /* existing EOL skipped, leave macro */ \ + noEOLFound: \ + sp->b.mode |= FAXMODE_NOEOL; \ + tryG3WithoutEOL(a0); \ + goto retrywithouteol; \ + } \ + } while (0) + +/* + * Cleanup the array of runs after decoding a row. + * We adjust final runs to insure the user buffer is not + * overwritten and/or undecoded area is white filled. + */ +#define CLEANUP_RUNS() \ + do \ + { \ + if (RunLength) \ + SETVALUE(0); \ + if (a0 != lastx) \ + { \ + badlength(a0, lastx); \ + while (a0 > lastx && pa > thisrun) \ + a0 -= *--pa; \ + if (a0 < lastx) \ + { \ + if (a0 < 0) \ + a0 = 0; \ + if ((pa - thisrun) & 1) \ + SETVALUE(0); \ + SETVALUE(lastx - a0); \ + } \ + else if (a0 > lastx) \ + { \ + SETVALUE(lastx); \ + SETVALUE(0); \ + } \ + } \ + } while (0) + +/* + * Decode a line of 1D-encoded data. + * + * The line expanders are written as macros so that they can be reused + * but still have direct access to the local variables of the "calling" + * function. + * + * Note that unlike the original version we have to explicitly test for + * a0 >= lastx after each black/white run is decoded. This is because + * the original code depended on the input data being zero-padded to + * insure the decoder recognized an EOL before running out of data. + */ +#define EXPAND1D(eoflab) \ + do \ + { \ + for (;;) \ + { \ + for (;;) \ + { \ + LOOKUP16(12, TIFFFaxWhiteTable, eof1d); \ + switch (TabEnt->State) \ + { \ + case S_EOL: \ + EOLcnt = 1; \ + goto done1d; \ + case S_TermW: \ + SETVALUE(TabEnt->Param); \ + goto doneWhite1d; \ + case S_MakeUpW: \ + case S_MakeUp: \ + a0 += TabEnt->Param; \ + RunLength += TabEnt->Param; \ + break; \ + default: \ + unexpected("WhiteTable", a0); \ + goto done1d; \ + } \ + } \ + doneWhite1d: \ + if (a0 >= lastx) \ + goto done1d; \ + for (;;) \ + { \ + LOOKUP16(13, TIFFFaxBlackTable, eof1d); \ + switch (TabEnt->State) \ + { \ + case S_EOL: \ + EOLcnt = 1; \ + goto done1d; \ + case S_TermB: \ + SETVALUE(TabEnt->Param); \ + goto doneBlack1d; \ + case S_MakeUpB: \ + case S_MakeUp: \ + a0 += TabEnt->Param; \ + RunLength += TabEnt->Param; \ + break; \ + default: \ + unexpected("BlackTable", a0); \ + goto done1d; \ + } \ + } \ + doneBlack1d: \ + if (a0 >= lastx) \ + goto done1d; \ + if (*(pa - 1) == 0 && *(pa - 2) == 0) \ + pa -= 2; \ + } \ + eof1d: \ + prematureEOF(a0); \ + CLEANUP_RUNS(); \ + goto eoflab; \ + done1d: \ + CLEANUP_RUNS(); \ + } while (0) + +/* + * Update the value of b1 using the array + * of runs for the reference line. + */ +#define CHECK_b1 \ + do \ + { \ + if (pa != thisrun) \ + while (b1 <= a0 && b1 < lastx) \ + { \ + if (pb + 1 >= sp->refruns + sp->nruns) \ + { \ + TIFFErrorExtR( \ + tif, module, "Buffer overflow at line %u of %s %u", \ + sp->line, isTiled(tif) ? "tile" : "strip", \ + isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip); \ + return (-1); \ + } \ + b1 += pb[0] + pb[1]; \ + pb += 2; \ + } \ + } while (0) + +/* + * Expand a row of 2D-encoded data. + */ +#define EXPAND2D(eoflab) \ + do \ + { \ + while (a0 < lastx) \ + { \ + if (pa >= thisrun + sp->nruns) \ + { \ + TIFFErrorExtR( \ + tif, module, "Buffer overflow at line %u of %s %u", \ + sp->line, isTiled(tif) ? "tile" : "strip", \ + isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip); \ + return (-1); \ + } \ + LOOKUP8(7, TIFFFaxMainTable, eof2d); \ + switch (TabEnt->State) \ + { \ + case S_Pass: \ + CHECK_b1; \ + if (pb + 1 >= sp->refruns + sp->nruns) \ + { \ + TIFFErrorExtR(tif, module, \ + "Buffer overflow at line %u of %s %u", \ + sp->line, \ + isTiled(tif) ? "tile" : "strip", \ + isTiled(tif) ? tif->tif_curtile \ + : tif->tif_curstrip); \ + return (-1); \ + } \ + b1 += *pb++; \ + RunLength += b1 - a0; \ + a0 = b1; \ + b1 += *pb++; \ + break; \ + case S_Horiz: \ + if ((pa - thisrun) & 1) \ + { \ + for (;;) \ + { /* black first */ \ + LOOKUP16(13, TIFFFaxBlackTable, eof2d); \ + switch (TabEnt->State) \ + { \ + case S_TermB: \ + SETVALUE(TabEnt->Param); \ + goto doneWhite2da; \ + case S_MakeUpB: \ + case S_MakeUp: \ + a0 += TabEnt->Param; \ + RunLength += TabEnt->Param; \ + break; \ + default: \ + goto badBlack2d; \ + } \ + } \ + doneWhite2da:; \ + for (;;) \ + { /* then white */ \ + LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \ + switch (TabEnt->State) \ + { \ + case S_TermW: \ + SETVALUE(TabEnt->Param); \ + goto doneBlack2da; \ + case S_MakeUpW: \ + case S_MakeUp: \ + a0 += TabEnt->Param; \ + RunLength += TabEnt->Param; \ + break; \ + default: \ + goto badWhite2d; \ + } \ + } \ + doneBlack2da:; \ + } \ + else \ + { \ + for (;;) \ + { /* white first */ \ + LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \ + switch (TabEnt->State) \ + { \ + case S_TermW: \ + SETVALUE(TabEnt->Param); \ + goto doneWhite2db; \ + case S_MakeUpW: \ + case S_MakeUp: \ + a0 += TabEnt->Param; \ + RunLength += TabEnt->Param; \ + break; \ + default: \ + goto badWhite2d; \ + } \ + } \ + doneWhite2db:; \ + for (;;) \ + { /* then black */ \ + LOOKUP16(13, TIFFFaxBlackTable, eof2d); \ + switch (TabEnt->State) \ + { \ + case S_TermB: \ + SETVALUE(TabEnt->Param); \ + goto doneBlack2db; \ + case S_MakeUpB: \ + case S_MakeUp: \ + a0 += TabEnt->Param; \ + RunLength += TabEnt->Param; \ + break; \ + default: \ + goto badBlack2d; \ + } \ + } \ + doneBlack2db:; \ + } \ + CHECK_b1; \ + break; \ + case S_V0: \ + CHECK_b1; \ + SETVALUE(b1 - a0); \ + if (pb >= sp->refruns + sp->nruns) \ + { \ + TIFFErrorExtR(tif, module, \ + "Buffer overflow at line %u of %s %u", \ + sp->line, \ + isTiled(tif) ? "tile" : "strip", \ + isTiled(tif) ? tif->tif_curtile \ + : tif->tif_curstrip); \ + return (-1); \ + } \ + b1 += *pb++; \ + break; \ + case S_VR: \ + CHECK_b1; \ + SETVALUE(b1 - a0 + TabEnt->Param); \ + if (pb >= sp->refruns + sp->nruns) \ + { \ + TIFFErrorExtR(tif, module, \ + "Buffer overflow at line %u of %s %u", \ + sp->line, \ + isTiled(tif) ? "tile" : "strip", \ + isTiled(tif) ? tif->tif_curtile \ + : tif->tif_curstrip); \ + return (-1); \ + } \ + b1 += *pb++; \ + break; \ + case S_VL: \ + CHECK_b1; \ + if (b1 < (int)(a0 + TabEnt->Param)) \ + { \ + unexpected("VL", a0); \ + goto eol2d; \ + } \ + SETVALUE(b1 - a0 - TabEnt->Param); \ + b1 -= *--pb; \ + break; \ + case S_Ext: \ + *pa++ = lastx - a0; \ + extension(a0); \ + goto eol2d; \ + case S_EOL: \ + *pa++ = lastx - a0; \ + NeedBits8(4, eof2d); \ + if (GetBits(4)) \ + unexpected("EOL", a0); \ + ClrBits(4); \ + EOLcnt = 1; \ + goto eol2d; \ + default: \ + badMain2d: \ + unexpected("MainTable", a0); \ + goto eol2d; \ + badBlack2d: \ + unexpected("BlackTable", a0); \ + goto eol2d; \ + badWhite2d: \ + unexpected("WhiteTable", a0); \ + goto eol2d; \ + eof2d: \ + prematureEOF(a0); \ + CLEANUP_RUNS(); \ + goto eoflab; \ + } \ + } \ + if (RunLength) \ + { \ + if (RunLength + a0 < lastx) \ + { \ + /* expect a final V0 */ \ + NeedBits8(1, eof2d); \ + if (!GetBits(1)) \ + goto badMain2d; \ + ClrBits(1); \ + } \ + SETVALUE(0); \ + } \ + eol2d: \ + CLEANUP_RUNS(); \ + } while (0) +#endif /* _FAX3_ */ diff --git a/cpp/3rd_party/libtiff/tif_fax3sm.c b/cpp/3rd_party/libtiff/tif_fax3sm.c new file mode 100644 index 0000000000..ba2fc532e8 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_fax3sm.c @@ -0,0 +1,1261 @@ +/* WARNING, this file was automatically generated by the + mkg3states program */ +#include +#include "tiff.h" +#include "tif_fax3.h" + const TIFFFaxTabEnt TIFFFaxMainTable[128] = { +{12,7,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0}, +{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{5,6,2},{3,1,0},{5,3,1},{3,1,0}, +{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0}, +{4,3,1},{3,1,0},{5,7,3},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0}, +{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{4,6,2},{3,1,0}, +{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0}, +{2,3,0},{3,1,0},{4,3,1},{3,1,0},{6,7,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0}, +{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0}, +{5,6,2},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0}, +{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0},{4,7,3},{3,1,0},{5,3,1},{3,1,0}, +{2,3,0},{3,1,0},{4,3,1},{3,1,0},{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0}, +{4,3,1},{3,1,0},{4,6,2},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0}, +{1,4,0},{3,1,0},{5,3,1},{3,1,0},{2,3,0},{3,1,0},{4,3,1},{3,1,0} +}; + const TIFFFaxTabEnt TIFFFaxWhiteTable[4096] = { +{12,11,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6}, +{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7}, +{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8}, +{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1792},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16}, +{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128}, +{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5}, +{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3}, +{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4}, +{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6}, +{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3}, +{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15}, +{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17}, +{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128}, +{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5}, +{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6}, +{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1856},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7}, +{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8}, +{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5}, +{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14}, +{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16}, +{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128}, +{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5}, +{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3}, +{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9}, +{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4}, +{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6}, +{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3}, +{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15}, +{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17}, +{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{11,12,2112},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6}, +{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7}, +{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8}, +{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16}, +{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128}, +{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5}, +{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3}, +{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2368},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4}, +{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6}, +{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3}, +{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15}, +{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17}, +{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128}, +{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5}, +{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6}, +{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7}, +{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8}, +{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5}, +{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14}, +{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16}, +{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128}, +{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5}, +{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{11,12,1984},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3}, +{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9}, +{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4}, +{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6}, +{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3}, +{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15}, +{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17}, +{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6}, +{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7}, +{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8}, +{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1920},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16}, +{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128}, +{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5}, +{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3}, +{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4}, +{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6}, +{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3}, +{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15}, +{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17}, +{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128}, +{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5}, +{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6}, +{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2240},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7}, +{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8}, +{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5}, +{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14}, +{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16}, +{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128}, +{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5}, +{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3}, +{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9}, +{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4}, +{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6}, +{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3}, +{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15}, +{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17}, +{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{11,12,2496},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6}, +{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7}, +{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8}, +{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{12,11,0},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16}, +{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128}, +{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5}, +{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3}, +{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1792},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4}, +{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6}, +{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3}, +{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15}, +{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17}, +{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128}, +{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5}, +{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6}, +{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7}, +{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8}, +{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5}, +{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14}, +{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16}, +{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128}, +{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5}, +{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{11,11,1856},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3}, +{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9}, +{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4}, +{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6}, +{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3}, +{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15}, +{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17}, +{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6}, +{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7}, +{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6},{7,8,31},{7,5,8}, +{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2176},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16}, +{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128}, +{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1600},{7,4,5}, +{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3}, +{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4}, +{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6}, +{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3}, +{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15}, +{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5},{7,8,43},{7,6,17}, +{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128}, +{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5}, +{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,768},{7,4,6}, +{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2432},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7}, +{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6},{7,7,19},{7,5,8}, +{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5}, +{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17},{9,9,1408},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14}, +{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16}, +{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128}, +{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5}, +{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3}, +{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9}, +{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4}, +{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,960},{7,4,6}, +{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3}, +{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15}, +{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17}, +{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{11,12,2048},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6}, +{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7}, +{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6},{7,8,32},{7,5,8}, +{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16}, +{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128}, +{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1536},{7,4,5}, +{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3}, +{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,11,1920},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4}, +{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,896},{7,4,6}, +{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3}, +{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15}, +{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5},{7,8,44},{7,6,17}, +{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128}, +{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5}, +{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6}, +{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7}, +{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8}, +{7,8,55},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5}, +{7,8,53},{7,5,9},{9,8,448},{7,4,6},{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1472},{7,4,5},{7,8,43},{7,6,17},{9,9,1216},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14}, +{7,8,61},{7,4,4},{7,4,2},{7,4,7},{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16}, +{9,9,960},{7,4,6},{7,8,31},{7,5,8},{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,9,704},{7,4,6},{7,8,37},{9,5,128}, +{7,7,25},{7,6,15},{9,8,320},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5}, +{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{11,12,2304},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,7,20},{9,5,128},{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3}, +{7,7,27},{7,4,5},{7,8,40},{7,6,16},{9,9,832},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9}, +{9,8,512},{7,4,6},{7,8,36},{9,5,128},{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{9,9,1600},{7,4,5},{7,8,44},{7,6,17},{9,9,1344},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5}, +{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4}, +{7,4,2},{7,4,7},{7,8,48},{7,4,3},{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1088},{7,4,6}, +{7,8,32},{7,5,8},{7,8,58},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3}, +{7,5,11},{7,4,5},{7,7,26},{7,5,9},{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15}, +{9,8,384},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17}, +{9,7,256},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{0,0,0},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128}, +{7,7,24},{7,6,14},{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5}, +{7,8,39},{7,6,16},{9,8,576},{7,4,6},{7,7,19},{7,5,8},{7,8,55},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,45},{7,4,3},{7,5,11},{7,4,5},{7,8,53},{7,5,9},{9,8,448},{7,4,6}, +{7,8,35},{9,5,128},{7,8,51},{7,6,15},{7,8,63},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3}, +{9,9,1536},{7,4,5},{7,8,43},{7,6,17},{9,9,1280},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,8,29},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9}, +{9,6,1664},{7,4,6},{7,8,33},{9,5,128},{7,8,49},{7,6,14},{7,8,61},{7,4,4},{7,4,2},{7,4,7}, +{7,8,47},{7,4,3},{7,8,59},{7,4,5},{7,8,41},{7,6,16},{9,9,1024},{7,4,6},{7,8,31},{7,5,8}, +{7,8,57},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5}, +{7,7,26},{7,5,9},{9,9,768},{7,4,6},{7,8,37},{9,5,128},{7,7,25},{7,6,15},{9,8,320},{7,4,4}, +{7,4,2},{7,4,7},{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6}, +{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7},{11,12,2560},{7,4,3}, +{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6},{7,7,20},{9,5,128},{7,7,24},{7,6,14}, +{7,7,28},{7,4,4},{7,4,2},{7,4,7},{7,7,23},{7,4,3},{7,7,27},{7,4,5},{7,8,40},{7,6,16}, +{9,9,896},{7,4,6},{7,7,19},{7,5,8},{7,8,56},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7}, +{7,8,46},{7,4,3},{7,5,11},{7,4,5},{7,8,54},{7,5,9},{9,8,512},{7,4,6},{7,8,36},{9,5,128}, +{7,8,52},{7,6,15},{7,8,0},{7,4,4},{7,4,2},{7,4,7},{7,6,13},{7,4,3},{9,9,1728},{7,4,5}, +{7,8,44},{7,6,17},{9,9,1408},{7,4,6},{7,6,1},{7,5,8},{9,6,192},{9,5,64},{7,5,10},{7,4,4}, +{7,4,2},{7,4,7},{7,8,30},{7,4,3},{7,5,11},{7,4,5},{7,6,12},{7,5,9},{9,6,1664},{7,4,6}, +{7,8,34},{9,5,128},{7,8,50},{7,6,14},{7,8,62},{7,4,4},{7,4,2},{7,4,7},{7,8,48},{7,4,3}, +{7,8,60},{7,4,5},{7,8,42},{7,6,16},{9,9,1152},{7,4,6},{7,8,32},{7,5,8},{7,8,58},{9,5,64}, +{7,5,10},{7,4,4},{7,4,2},{7,4,7},{7,7,22},{7,4,3},{7,5,11},{7,4,5},{7,7,26},{7,5,9}, +{9,8,640},{7,4,6},{7,8,38},{9,5,128},{7,7,25},{7,6,15},{9,8,384},{7,4,4},{7,4,2},{7,4,7}, +{7,6,13},{7,4,3},{7,7,18},{7,4,5},{7,7,21},{7,6,17},{9,7,256},{7,4,6},{7,6,1},{7,5,8}, +{9,6,192},{9,5,64},{7,5,10},{7,4,4},{7,4,2},{7,4,7} +}; + const TIFFFaxTabEnt TIFFFaxBlackTable[8192] = { +{12,11,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1792},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,11,23},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,20},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,11,25},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,128},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,56},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,30},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1856},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,57},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,11,21},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,54},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,52},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,48},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{11,12,2112},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,44},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,36},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,384},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,28},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,60},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,40},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2368},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{11,12,1984},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,50},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,34},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1664},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,26},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1408},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,32},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1920},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,61},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,42},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{10,13,1024},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,13,768},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,62},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2240},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,46},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,38},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,512},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,11,19},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,24},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,22},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{11,12,2496},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{12,11,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1792},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,23},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,20},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,11,25},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{10,12,192},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1280},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,31},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{11,11,1856},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,58},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,11,21},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,896},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,640},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,49},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2176},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,45},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,37},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{10,12,448},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,29},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,13,1536},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,41},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2432},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{11,12,2048},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,51},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,35},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,320},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,27},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,59},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,33},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1920},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,256},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,43},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,13,1152},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,55},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,63},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{11,12,2304},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,47},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,39},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,53},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,19},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,24},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,22},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2560},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{12,11,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1792},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,23},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,11,20},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,25},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,12,128},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,56},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,30},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{11,11,1856},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,57},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,21},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,54},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,52},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,48},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2112},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,44},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,36},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,12,384},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,28},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,60},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,40},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{11,12,2368},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,1984},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,50},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,34},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{10,13,1728},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,26},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,13,1472},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,32},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1920},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,61},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,42},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1088},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,832},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,62},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{11,12,2240},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,46},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,38},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,576},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,19},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,11,24},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,22},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2496},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{12,11,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,18},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{11,11,1792},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,23},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,11,20},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,25},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,192},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1344},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,31},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,11,1856},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,58},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,21},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{10,13,960},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,13,704},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,49},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2176},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,45},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,37},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,448},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,29},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1600},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,41},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{11,12,2432},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,18},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,17},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2048},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,51},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,35},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{10,12,320},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,27},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,59},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,33},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{11,11,1920},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,12,256},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,43},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,13,1216},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{0,0,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,8,13},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,9,15},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,55},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,63},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2304},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,12,47},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,12,39},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,12,53},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,12},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{0,0,0},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,8,13},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,11,19},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,11,24},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,11,22},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{11,12,2560},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,7,10},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,10,16},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2},{8,10,0},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2}, +{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{10,10,64},{8,2,3}, +{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,9},{8,2,3},{8,3,1},{8,2,2}, +{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,11},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3}, +{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2}, +{8,8,14},{8,2,3},{8,3,1},{8,2,2},{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,6,8},{8,2,3}, +{8,3,1},{8,2,2},{8,4,5},{8,2,3},{8,3,4},{8,2,2},{8,7,12},{8,2,3},{8,3,1},{8,2,2}, +{8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3}, +{8,3,4},{8,2,2} +}; +/* + * Local Variables: + * mode: c + * c-basic-offset: 8 + * fill-column: 78 + * End: + */ diff --git a/cpp/3rd_party/libtiff/tif_flush.c b/cpp/3rd_party/libtiff/tif_flush.c new file mode 100644 index 0000000000..ff9c1e247a --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_flush.c @@ -0,0 +1,157 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + */ +#include "tiffiop.h" + +int TIFFFlush(TIFF *tif) +{ + if (tif->tif_mode == O_RDONLY) + return 1; + + if (!TIFFFlushData(tif)) + return (0); + + /* In update (r+) mode we try to detect the case where + only the strip/tile map has been altered, and we try to + rewrite only that portion of the directory without + making any other changes */ + + if ((tif->tif_flags & TIFF_DIRTYSTRIP) && + !(tif->tif_flags & TIFF_DIRTYDIRECT) && tif->tif_mode == O_RDWR) + { + if (TIFFForceStrileArrayWriting(tif)) + return 1; + } + + if ((tif->tif_flags & (TIFF_DIRTYDIRECT | TIFF_DIRTYSTRIP)) && + !TIFFRewriteDirectory(tif)) + return (0); + + return (1); +} + +/* + * This is an advanced writing function that must be used in a particular + * sequence, and together with TIFFDeferStrileArrayWriting(), + * to make its intended effect. Its aim is to force the writing of + * the [Strip/Tile][Offsets/ByteCounts] arrays at the end of the file, when + * they have not yet been rewritten. + * + * The typical sequence of calls is: + * TIFFOpen() + * [ TIFFCreateDirectory(tif) ] + * Set fields with calls to TIFFSetField(tif, ...) + * TIFFDeferStrileArrayWriting(tif) + * TIFFWriteCheck(tif, ...) + * TIFFWriteDirectory(tif) + * ... potentially create other directories and come back to the above directory + * TIFFForceStrileArrayWriting(tif) + * + * Returns 1 in case of success, 0 otherwise. + */ +int TIFFForceStrileArrayWriting(TIFF *tif) +{ + static const char module[] = "TIFFForceStrileArrayWriting"; + const int isTiled = TIFFIsTiled(tif); + + if (tif->tif_mode == O_RDONLY) + { + TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode"); + return 0; + } + if (tif->tif_diroff == 0) + { + TIFFErrorExtR(tif, module, "Directory has not yet been written"); + return 0; + } + if ((tif->tif_flags & TIFF_DIRTYDIRECT) != 0) + { + TIFFErrorExtR(tif, module, + "Directory has changes other than the strile arrays. " + "TIFFRewriteDirectory() should be called instead"); + return 0; + } + + if (!(tif->tif_flags & TIFF_DIRTYSTRIP)) + { + if (!(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && + tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)) + { + TIFFErrorExtR(tif, module, + "Function not called together with " + "TIFFDeferStrileArrayWriting()"); + return 0; + } + + if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) + return 0; + } + + if (_TIFFRewriteField(tif, + isTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS, + TIFF_LONG8, tif->tif_dir.td_nstrips, + tif->tif_dir.td_stripoffset_p) && + _TIFFRewriteField( + tif, isTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS, + TIFF_LONG8, tif->tif_dir.td_nstrips, + tif->tif_dir.td_stripbytecount_p)) + { + tif->tif_flags &= ~TIFF_DIRTYSTRIP; + tif->tif_flags &= ~TIFF_BEENWRITING; + return 1; + } + + return 0; +} + +/* + * Flush buffered data to the file. + * + * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING + * is not set, so that TIFFFlush() will proceed to write out the directory. + * The documentation says returning 1 is an error indicator, but not having + * been writing isn't exactly a an error. Hopefully this doesn't cause + * problems for other people. + */ +int TIFFFlushData(TIFF *tif) +{ + if ((tif->tif_flags & TIFF_BEENWRITING) == 0) + return (1); + if (tif->tif_flags & TIFF_POSTENCODE) + { + tif->tif_flags &= ~TIFF_POSTENCODE; + if (!(*tif->tif_postencode)(tif)) + return (0); + } + return (TIFFFlushData1(tif)); +} diff --git a/cpp/3rd_party/libtiff/tif_getimage.c b/cpp/3rd_party/libtiff/tif_getimage.c new file mode 100644 index 0000000000..9d4259ef83 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_getimage.c @@ -0,0 +1,3535 @@ +/* + * Copyright (c) 1991-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library + * + * Read and return a packed RGBA image. + */ +#include "tiffiop.h" +#include +#include + +static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); +static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); +static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); +static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); +static int PickContigCase(TIFFRGBAImage *); +static int PickSeparateCase(TIFFRGBAImage *); + +static int BuildMapUaToAa(TIFFRGBAImage *img); +static int BuildMapBitdepth16To8(TIFFRGBAImage *img); + +static const char photoTag[] = "PhotometricInterpretation"; + +/* + * Helper constants used in Orientation tag handling + */ +#define FLIP_VERTICALLY 0x01 +#define FLIP_HORIZONTALLY 0x02 + +#define EMSG_BUF_SIZE 1024 + +/* + * Color conversion constants. We will define display types here. + */ + +static const TIFFDisplay display_sRGB = { + {/* XYZ -> luminance matrix */ + {3.2410F, -1.5374F, -0.4986F}, + {-0.9692F, 1.8760F, 0.0416F}, + {0.0556F, -0.2040F, 1.0570F}}, + 100.0F, + 100.0F, + 100.0F, /* Light o/p for reference white */ + 255, + 255, + 255, /* Pixel values for ref. white */ + 1.0F, + 1.0F, + 1.0F, /* Residual light o/p for black pixel */ + 2.4F, + 2.4F, + 2.4F, /* Gamma values for the three guns */ +}; + +/* + * Check the image to see if TIFFReadRGBAImage can deal with it. + * 1/0 is returned according to whether or not the image can + * be handled. If 0 is returned, emsg contains the reason + * why it is being rejected. + */ +int TIFFRGBAImageOK(TIFF *tif, char emsg[EMSG_BUF_SIZE]) +{ + TIFFDirectory *td = &tif->tif_dir; + uint16_t photometric; + int colorchannels; + + if (!tif->tif_decodestatus) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, requested compression method is not configured"); + return (0); + } + switch (td->td_bitspersample) + { + case 1: + case 2: + case 4: + case 8: + case 16: + break; + default: + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle images with %" PRIu16 + "-bit samples", + td->td_bitspersample); + return (0); + } + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP) + { + snprintf( + emsg, EMSG_BUF_SIZE, + "Sorry, can not handle images with IEEE floating-point samples"); + return (0); + } + colorchannels = td->td_samplesperpixel - td->td_extrasamples; + if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) + { + switch (colorchannels) + { + case 1: + photometric = PHOTOMETRIC_MINISBLACK; + break; + case 3: + photometric = PHOTOMETRIC_RGB; + break; + default: + snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag", + photoTag); + return (0); + } + } + switch (photometric) + { + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + case PHOTOMETRIC_PALETTE: + if (td->td_planarconfig == PLANARCONFIG_CONTIG && + td->td_samplesperpixel != 1 && td->td_bitspersample < 8) + { + snprintf( + emsg, EMSG_BUF_SIZE, + "Sorry, can not handle contiguous data with %s=%" PRIu16 + ", " + "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "", + photoTag, photometric, "Samples/pixel", + td->td_samplesperpixel, td->td_bitspersample); + return (0); + } + /* + * We should likely validate that any extra samples are either + * to be ignored, or are alpha, and if alpha we should try to use + * them. But for now we won't bother with this. + */ + break; + case PHOTOMETRIC_YCBCR: + /* + * TODO: if at all meaningful and useful, make more complete + * support check here, or better still, refactor to let supporting + * code decide whether there is support and what meaningful + * error to return + */ + break; + case PHOTOMETRIC_RGB: + if (colorchannels < 3) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle RGB image with %s=%d", + "Color channels", colorchannels); + return (0); + } + break; + case PHOTOMETRIC_SEPARATED: + { + uint16_t inkset; + TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset); + if (inkset != INKSET_CMYK) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle separated image with %s=%d", + "InkSet", inkset); + return 0; + } + if (td->td_samplesperpixel < 4) + { + snprintf( + emsg, EMSG_BUF_SIZE, + "Sorry, can not handle separated image with %s=%" PRIu16, + "Samples/pixel", td->td_samplesperpixel); + return 0; + } + break; + } + case PHOTOMETRIC_LOGL: + if (td->td_compression != COMPRESSION_SGILOG) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, LogL data must have %s=%d", "Compression", + COMPRESSION_SGILOG); + return (0); + } + break; + case PHOTOMETRIC_LOGLUV: + if (td->td_compression != COMPRESSION_SGILOG && + td->td_compression != COMPRESSION_SGILOG24) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, LogLuv data must have %s=%d or %d", + "Compression", COMPRESSION_SGILOG, + COMPRESSION_SGILOG24); + return (0); + } + if (td->td_planarconfig != PLANARCONFIG_CONTIG) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle LogLuv images with %s=%" PRIu16, + "Planarconfiguration", td->td_planarconfig); + return (0); + } + if (td->td_samplesperpixel != 3 || colorchannels != 3) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle image with %s=%" PRIu16 + ", %s=%d", + "Samples/pixel", td->td_samplesperpixel, + "colorchannels", colorchannels); + return 0; + } + break; + case PHOTOMETRIC_CIELAB: + if (td->td_samplesperpixel != 3 || colorchannels != 3 || + (td->td_bitspersample != 8 && td->td_bitspersample != 16)) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle image with %s=%" PRIu16 + ", %s=%d and %s=%" PRIu16, + "Samples/pixel", td->td_samplesperpixel, + "colorchannels", colorchannels, "Bits/sample", + td->td_bitspersample); + return 0; + } + break; + default: + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle image with %s=%" PRIu16, photoTag, + photometric); + return (0); + } + return (1); +} + +void TIFFRGBAImageEnd(TIFFRGBAImage *img) +{ + if (img->Map) + { + _TIFFfreeExt(img->tif, img->Map); + img->Map = NULL; + } + if (img->BWmap) + { + _TIFFfreeExt(img->tif, img->BWmap); + img->BWmap = NULL; + } + if (img->PALmap) + { + _TIFFfreeExt(img->tif, img->PALmap); + img->PALmap = NULL; + } + if (img->ycbcr) + { + _TIFFfreeExt(img->tif, img->ycbcr); + img->ycbcr = NULL; + } + if (img->cielab) + { + _TIFFfreeExt(img->tif, img->cielab); + img->cielab = NULL; + } + if (img->UaToAa) + { + _TIFFfreeExt(img->tif, img->UaToAa); + img->UaToAa = NULL; + } + if (img->Bitdepth16To8) + { + _TIFFfreeExt(img->tif, img->Bitdepth16To8); + img->Bitdepth16To8 = NULL; + } + + if (img->redcmap) + { + _TIFFfreeExt(img->tif, img->redcmap); + _TIFFfreeExt(img->tif, img->greencmap); + _TIFFfreeExt(img->tif, img->bluecmap); + img->redcmap = img->greencmap = img->bluecmap = NULL; + } +} + +static int isCCITTCompression(TIFF *tif) +{ + uint16_t compress; + TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress); + return (compress == COMPRESSION_CCITTFAX3 || + compress == COMPRESSION_CCITTFAX4 || + compress == COMPRESSION_CCITTRLE || + compress == COMPRESSION_CCITTRLEW); +} + +int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop, + char emsg[EMSG_BUF_SIZE]) +{ + uint16_t *sampleinfo; + uint16_t extrasamples; + uint16_t planarconfig; + uint16_t compress; + int colorchannels; + uint16_t *red_orig, *green_orig, *blue_orig; + int n_color; + + if (!TIFFRGBAImageOK(tif, emsg)) + return 0; + + /* Initialize to normal values */ + img->row_offset = 0; + img->col_offset = 0; + img->redcmap = NULL; + img->greencmap = NULL; + img->bluecmap = NULL; + img->Map = NULL; + img->BWmap = NULL; + img->PALmap = NULL; + img->ycbcr = NULL; + img->cielab = NULL; + img->UaToAa = NULL; + img->Bitdepth16To8 = NULL; + img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */ + + img->tif = tif; + img->stoponerr = stop; + TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample); + switch (img->bitspersample) + { + case 1: + case 2: + case 4: + case 8: + case 16: + break; + default: + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle images with %" PRIu16 + "-bit samples", + img->bitspersample); + goto fail_return; + } + img->alpha = 0; + TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel); + TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, + &sampleinfo); + if (extrasamples >= 1) + { + switch (sampleinfo[0]) + { + case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without + */ + if (img->samplesperpixel > + 3) /* correct info about alpha channel */ + img->alpha = EXTRASAMPLE_ASSOCALPHA; + break; + case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */ + case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */ + img->alpha = sampleinfo[0]; + break; + } + } + +#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA + if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) + img->photometric = PHOTOMETRIC_MINISWHITE; + + if (extrasamples == 0 && img->samplesperpixel == 4 && + img->photometric == PHOTOMETRIC_RGB) + { + img->alpha = EXTRASAMPLE_ASSOCALPHA; + extrasamples = 1; + } +#endif + + colorchannels = img->samplesperpixel - extrasamples; + TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress); + TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig); + if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) + { + switch (colorchannels) + { + case 1: + if (isCCITTCompression(tif)) + img->photometric = PHOTOMETRIC_MINISWHITE; + else + img->photometric = PHOTOMETRIC_MINISBLACK; + break; + case 3: + img->photometric = PHOTOMETRIC_RGB; + break; + default: + snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag", + photoTag); + goto fail_return; + } + } + switch (img->photometric) + { + case PHOTOMETRIC_PALETTE: + if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig, + &blue_orig)) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Missing required \"Colormap\" tag"); + goto fail_return; + } + + /* copy the colormaps so we can modify them */ + n_color = (1U << img->bitspersample); + img->redcmap = + (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color); + img->greencmap = + (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color); + img->bluecmap = + (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color); + if (!img->redcmap || !img->greencmap || !img->bluecmap) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Out of memory for colormap copy"); + goto fail_return; + } + + _TIFFmemcpy(img->redcmap, red_orig, n_color * 2); + _TIFFmemcpy(img->greencmap, green_orig, n_color * 2); + _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2); + + /* fall through... */ + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + if (planarconfig == PLANARCONFIG_CONTIG && + img->samplesperpixel != 1 && img->bitspersample < 8) + { + snprintf( + emsg, EMSG_BUF_SIZE, + "Sorry, can not handle contiguous data with %s=%" PRIu16 + ", " + "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16, + photoTag, img->photometric, "Samples/pixel", + img->samplesperpixel, img->bitspersample); + goto fail_return; + } + break; + case PHOTOMETRIC_YCBCR: + /* It would probably be nice to have a reality check here. */ + if (planarconfig == PLANARCONFIG_CONTIG) + /* can rely on libjpeg to convert to RGB */ + /* XXX should restore current state on exit */ + switch (compress) + { + case COMPRESSION_JPEG: + /* + * TODO: when complete tests verify complete + * desubsampling and YCbCr handling, remove use of + * TIFFTAG_JPEGCOLORMODE in favor of tif_getimage.c + * native handling + */ + TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, + JPEGCOLORMODE_RGB); + img->photometric = PHOTOMETRIC_RGB; + break; + default: + /* do nothing */; + break; + } + /* + * TODO: if at all meaningful and useful, make more complete + * support check here, or better still, refactor to let supporting + * code decide whether there is support and what meaningful + * error to return + */ + break; + case PHOTOMETRIC_RGB: + if (colorchannels < 3) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle RGB image with %s=%d", + "Color channels", colorchannels); + goto fail_return; + } + break; + case PHOTOMETRIC_SEPARATED: + { + uint16_t inkset; + TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset); + if (inkset != INKSET_CMYK) + { + snprintf( + emsg, EMSG_BUF_SIZE, + "Sorry, can not handle separated image with %s=%" PRIu16, + "InkSet", inkset); + goto fail_return; + } + if (img->samplesperpixel < 4) + { + snprintf( + emsg, EMSG_BUF_SIZE, + "Sorry, can not handle separated image with %s=%" PRIu16, + "Samples/pixel", img->samplesperpixel); + goto fail_return; + } + } + break; + case PHOTOMETRIC_LOGL: + if (compress != COMPRESSION_SGILOG) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, LogL data must have %s=%d", "Compression", + COMPRESSION_SGILOG); + goto fail_return; + } + TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT); + img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */ + img->bitspersample = 8; + break; + case PHOTOMETRIC_LOGLUV: + if (compress != COMPRESSION_SGILOG && + compress != COMPRESSION_SGILOG24) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, LogLuv data must have %s=%d or %d", + "Compression", COMPRESSION_SGILOG, + COMPRESSION_SGILOG24); + goto fail_return; + } + if (planarconfig != PLANARCONFIG_CONTIG) + { + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle LogLuv images with %s=%" PRIu16, + "Planarconfiguration", planarconfig); + return (0); + } + TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT); + img->photometric = PHOTOMETRIC_RGB; /* little white lie */ + img->bitspersample = 8; + break; + case PHOTOMETRIC_CIELAB: + break; + default: + snprintf(emsg, EMSG_BUF_SIZE, + "Sorry, can not handle image with %s=%" PRIu16, photoTag, + img->photometric); + goto fail_return; + } + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height); + TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation); + img->isContig = + !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1); + if (img->isContig) + { + if (!PickContigCase(img)) + { + snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image"); + goto fail_return; + } + } + else + { + if (!PickSeparateCase(img)) + { + snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image"); + goto fail_return; + } + } + return 1; + +fail_return: + TIFFRGBAImageEnd(img); + return 0; +} + +int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, + uint32_t h) +{ + if (img->get == NULL) + { + TIFFErrorExtR(img->tif, TIFFFileName(img->tif), + "No \"get\" routine setup"); + return (0); + } + if (img->put.any == NULL) + { + TIFFErrorExtR( + img->tif, TIFFFileName(img->tif), + "No \"put\" routine setupl; probably can not handle image format"); + return (0); + } + /* Verify raster height against image height. + * Width is checked in img->get() function individually. */ + if (0 <= img->row_offset && (uint32_t)img->row_offset < img->height) + { + uint32_t hx = img->height - img->row_offset; + if (h > hx) + { + /* Adapt parameters to read only available lines and put image + * at the bottom of the raster. */ + raster += (size_t)(h - hx) * w; + h = hx; + } + } + else + { + TIFFErrorExtR(img->tif, TIFFFileName(img->tif), + "Error in TIFFRGBAImageGet: row offset %d exceeds " + "image height %d", + img->row_offset, img->height); + return 0; + } + return (*img->get)(img, raster, w, h); +} + +/* + * Read the specified image into an ABGR-format rastertaking in account + * specified orientation. + */ +int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight, + uint32_t *raster, int orientation, int stop) +{ + char emsg[EMSG_BUF_SIZE] = ""; + TIFFRGBAImage img; + int ok; + + if (TIFFRGBAImageBegin(&img, tif, stop, emsg)) + { + img.req_orientation = (uint16_t)orientation; + ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight); + TIFFRGBAImageEnd(&img); + } + else + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg); + ok = 0; + } + return (ok); +} + +/* + * Read the specified image into an ABGR-format raster. Use bottom left + * origin for raster by default. + */ +int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight, + uint32_t *raster, int stop) +{ + return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster, + ORIENTATION_BOTLEFT, stop); +} + +static int setorientation(TIFFRGBAImage *img) +{ + switch (img->orientation) + { + case ORIENTATION_TOPLEFT: + case ORIENTATION_LEFTTOP: + if (img->req_orientation == ORIENTATION_TOPRIGHT || + img->req_orientation == ORIENTATION_RIGHTTOP) + return FLIP_HORIZONTALLY; + else if (img->req_orientation == ORIENTATION_BOTRIGHT || + img->req_orientation == ORIENTATION_RIGHTBOT) + return FLIP_HORIZONTALLY | FLIP_VERTICALLY; + else if (img->req_orientation == ORIENTATION_BOTLEFT || + img->req_orientation == ORIENTATION_LEFTBOT) + return FLIP_VERTICALLY; + else + return 0; + case ORIENTATION_TOPRIGHT: + case ORIENTATION_RIGHTTOP: + if (img->req_orientation == ORIENTATION_TOPLEFT || + img->req_orientation == ORIENTATION_LEFTTOP) + return FLIP_HORIZONTALLY; + else if (img->req_orientation == ORIENTATION_BOTRIGHT || + img->req_orientation == ORIENTATION_RIGHTBOT) + return FLIP_VERTICALLY; + else if (img->req_orientation == ORIENTATION_BOTLEFT || + img->req_orientation == ORIENTATION_LEFTBOT) + return FLIP_HORIZONTALLY | FLIP_VERTICALLY; + else + return 0; + case ORIENTATION_BOTRIGHT: + case ORIENTATION_RIGHTBOT: + if (img->req_orientation == ORIENTATION_TOPLEFT || + img->req_orientation == ORIENTATION_LEFTTOP) + return FLIP_HORIZONTALLY | FLIP_VERTICALLY; + else if (img->req_orientation == ORIENTATION_TOPRIGHT || + img->req_orientation == ORIENTATION_RIGHTTOP) + return FLIP_VERTICALLY; + else if (img->req_orientation == ORIENTATION_BOTLEFT || + img->req_orientation == ORIENTATION_LEFTBOT) + return FLIP_HORIZONTALLY; + else + return 0; + case ORIENTATION_BOTLEFT: + case ORIENTATION_LEFTBOT: + if (img->req_orientation == ORIENTATION_TOPLEFT || + img->req_orientation == ORIENTATION_LEFTTOP) + return FLIP_VERTICALLY; + else if (img->req_orientation == ORIENTATION_TOPRIGHT || + img->req_orientation == ORIENTATION_RIGHTTOP) + return FLIP_HORIZONTALLY | FLIP_VERTICALLY; + else if (img->req_orientation == ORIENTATION_BOTRIGHT || + img->req_orientation == ORIENTATION_RIGHTBOT) + return FLIP_HORIZONTALLY; + else + return 0; + default: /* NOTREACHED */ + return 0; + } +} + +/* + * Get an tile-organized image that has + * PlanarConfiguration contiguous if SamplesPerPixel > 1 + * or + * SamplesPerPixel == 1 + */ +static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, + uint32_t h) +{ + TIFF *tif = img->tif; + tileContigRoutine put = img->put.contig; + uint32_t col, row, y, rowstoread; + tmsize_t pos; + uint32_t tw, th; + unsigned char *buf = NULL; + int32_t fromskew, toskew; + uint32_t nrow; + int ret = 1, flip; + uint32_t this_tw, tocol; + int32_t this_toskew, leftmost_toskew; + int32_t leftmost_fromskew; + uint32_t leftmost_tw; + tmsize_t bufsize; + + /* If the raster is smaller than the image, + * or if there is a col_offset, adapt the samples to be copied per row. */ + uint32_t wmin; + + if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width) + { + wmin = TIFFmin(w, img->width - img->col_offset); + } + else + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Error in gtTileContig: column offset %d exceeds " + "image width %d", + img->col_offset, img->width); + return 0; + } + bufsize = TIFFTileSize(tif); + if (bufsize == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer"); + return (0); + } + + TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw); + TIFFGetField(tif, TIFFTAG_TILELENGTH, &th); + + flip = setorientation(img); + if (flip & FLIP_VERTICALLY) + { + if (((int64_t)tw + w) > INT_MAX) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", + "unsupported tile size (too wide)"); + return (0); + } + y = h - 1; + toskew = -(int32_t)(tw + w); + } + else + { + if (tw > ((int64_t)INT_MAX + w)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", + "unsupported tile size (too wide)"); + return (0); + } + y = 0; + toskew = -(int32_t)(tw - w); + } + + if (tw == 0 || th == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero"); + return (0); + } + + /* + * Leftmost tile is clipped on left side if col_offset > 0. + */ + leftmost_fromskew = img->col_offset % tw; + leftmost_tw = tw - leftmost_fromskew; + int64_t skew_i64 = (int64_t)toskew + leftmost_fromskew; + if (skew_i64 > INT_MAX || skew_i64 < INT_MIN) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew", + skew_i64); + return (0); + } + leftmost_toskew = (int32_t)skew_i64; + for (row = 0; ret != 0 && row < h; row += nrow) + { + rowstoread = th - (row + img->row_offset) % th; + nrow = (row + rowstoread > h ? h - row : rowstoread); + fromskew = leftmost_fromskew; + this_tw = leftmost_tw; + this_toskew = leftmost_toskew; + tocol = 0; + col = img->col_offset; + /* wmin: only write imagewidth if raster is bigger. */ + while (tocol < wmin) + { + if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col, + row + img->row_offset, 0, + 0) == (tmsize_t)(-1) && + (buf == NULL || img->stoponerr)) + { + ret = 0; + break; + } + pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) + + ((tmsize_t)fromskew * img->samplesperpixel); + if (tocol + this_tw > wmin) + { + /* + * Rightmost tile is clipped on right side. + */ + fromskew = tw - (wmin - tocol); + this_tw = tw - fromskew; + this_toskew = toskew + fromskew; + } + tmsize_t roffset = (tmsize_t)y * w + tocol; + (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew, + this_toskew, buf + pos); + tocol += this_tw; + col += this_tw; + /* + * After the leftmost tile, tiles are no longer clipped on left + * side. + */ + fromskew = 0; + this_tw = tw; + this_toskew = toskew; + } + + y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); + } + _TIFFfreeExt(img->tif, buf); + + if (flip & FLIP_HORIZONTALLY) + { + uint32_t line; + + for (line = 0; line < h; line++) + { + uint32_t *left = raster + (line * w); + /* Use wmin to only flip horizontally data in place and not complete + * raster-row. */ + uint32_t *right = left + wmin - 1; + + while (left < right) + { + uint32_t temp = *left; + *left = *right; + *right = temp; + left++; + right--; + } + } + } + + return (ret); +} + +/* + * Get an tile-organized image that has + * SamplesPerPixel > 1 + * PlanarConfiguration separated + * We assume that all such images are RGB. + */ +static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, + uint32_t h) +{ + TIFF *tif = img->tif; + tileSeparateRoutine put = img->put.separate; + uint32_t col, row, y, rowstoread; + tmsize_t pos; + uint32_t tw, th; + unsigned char *buf = NULL; + unsigned char *p0 = NULL; + unsigned char *p1 = NULL; + unsigned char *p2 = NULL; + unsigned char *pa = NULL; + tmsize_t tilesize; + tmsize_t bufsize; + int32_t fromskew, toskew; + int alpha = img->alpha; + uint32_t nrow; + int ret = 1, flip; + uint16_t colorchannels; + uint32_t this_tw, tocol; + int32_t this_toskew, leftmost_toskew; + int32_t leftmost_fromskew; + uint32_t leftmost_tw; + + /* If the raster is smaller than the image, + * or if there is a col_offset, adapt the samples to be copied per row. */ + uint32_t wmin; + if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width) + { + wmin = TIFFmin(w, img->width - img->col_offset); + } + else + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Error in gtTileSeparate: column offset %d exceeds " + "image width %d", + img->col_offset, img->width); + return 0; + } + + tilesize = TIFFTileSize(tif); + bufsize = + _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate"); + if (bufsize == 0) + { + return (0); + } + + TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw); + TIFFGetField(tif, TIFFTAG_TILELENGTH, &th); + + flip = setorientation(img); + if (flip & FLIP_VERTICALLY) + { + if (((int64_t)tw + w) > INT_MAX) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", + "unsupported tile size (too wide)"); + return (0); + } + y = h - 1; + toskew = -(int32_t)(tw + w); + } + else + { + if (tw > ((int64_t)INT_MAX + w)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", + "unsupported tile size (too wide)"); + return (0); + } + y = 0; + toskew = -(int32_t)(tw - w); + } + + switch (img->photometric) + { + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + case PHOTOMETRIC_PALETTE: + colorchannels = 1; + break; + + default: + colorchannels = 3; + break; + } + + if (tw == 0 || th == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero"); + return (0); + } + + /* + * Leftmost tile is clipped on left side if col_offset > 0. + */ + leftmost_fromskew = img->col_offset % tw; + leftmost_tw = tw - leftmost_fromskew; + int64_t skew_i64 = (int64_t)toskew + leftmost_fromskew; + if (skew_i64 > INT_MAX || skew_i64 < INT_MIN) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew", + skew_i64); + return (0); + } + leftmost_toskew = (int32_t)skew_i64; + for (row = 0; ret != 0 && row < h; row += nrow) + { + rowstoread = th - (row + img->row_offset) % th; + nrow = (row + rowstoread > h ? h - row : rowstoread); + fromskew = leftmost_fromskew; + this_tw = leftmost_tw; + this_toskew = leftmost_toskew; + tocol = 0; + col = img->col_offset; + /* wmin: only write imagewidth if raster is bigger. */ + while (tocol < wmin) + { + if (buf == NULL) + { + if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, + col, row + img->row_offset, 0, + 0) == (tmsize_t)(-1) && + (buf == NULL || img->stoponerr)) + { + ret = 0; + break; + } + p0 = buf; + if (colorchannels == 1) + { + p2 = p1 = p0; + pa = (alpha ? (p0 + 3 * tilesize) : NULL); + } + else + { + p1 = p0 + tilesize; + p2 = p1 + tilesize; + pa = (alpha ? (p2 + tilesize) : NULL); + } + } + else if (TIFFReadTile(tif, p0, col, row + img->row_offset, 0, 0) == + (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + if (colorchannels > 1 && + TIFFReadTile(tif, p1, col, row + img->row_offset, 0, 1) == + (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + if (colorchannels > 1 && + TIFFReadTile(tif, p2, col, row + img->row_offset, 0, 2) == + (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + if (alpha && + TIFFReadTile(tif, pa, col, row + img->row_offset, 0, + colorchannels) == (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + + /* For SEPARATE the pos-offset is per sample and should not be + * multiplied by img->samplesperpixel. */ + pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) + + (tmsize_t)fromskew; + if (tocol + this_tw > wmin) + { + /* + * Rightmost tile is clipped on right side. + */ + fromskew = tw - (wmin - tocol); + this_tw = tw - fromskew; + this_toskew = toskew + fromskew; + } + tmsize_t roffset = (tmsize_t)y * w + tocol; + (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew, + this_toskew, p0 + pos, p1 + pos, p2 + pos, + (alpha ? (pa + pos) : NULL)); + tocol += this_tw; + col += this_tw; + /* + * After the leftmost tile, tiles are no longer clipped on left + * side. + */ + fromskew = 0; + this_tw = tw; + this_toskew = toskew; + } + + y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); + } + + if (flip & FLIP_HORIZONTALLY) + { + uint32_t line; + + for (line = 0; line < h; line++) + { + uint32_t *left = raster + (line * w); + /* Use wmin to only flip horizontally data in place and not complete + * raster-row. */ + uint32_t *right = left + wmin - 1; + + while (left < right) + { + uint32_t temp = *left; + *left = *right; + *right = temp; + left++; + right--; + } + } + } + + _TIFFfreeExt(img->tif, buf); + return (ret); +} + +/* + * Get a strip-organized image that has + * PlanarConfiguration contiguous if SamplesPerPixel > 1 + * or + * SamplesPerPixel == 1 + */ +static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, + uint32_t h) +{ + TIFF *tif = img->tif; + tileContigRoutine put = img->put.contig; + uint32_t row, y, nrow, nrowsub, rowstoread; + tmsize_t pos; + unsigned char *buf = NULL; + uint32_t rowsperstrip; + uint16_t subsamplinghor, subsamplingver; + uint32_t imagewidth = img->width; + tmsize_t scanline; + /* fromskew, toskew are the increments within the input image or the raster + * from the end of a line to the start of the next line to read or write. */ + int32_t fromskew, toskew; + int ret = 1, flip; + tmsize_t maxstripsize; + + /* If the raster is smaller than the image, + * or if there is a col_offset, adapt the samples to be copied per row. */ + uint32_t wmin; + if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth) + { + wmin = TIFFmin(w, imagewidth - img->col_offset); + } + else + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Error in gtStripContig: column offset %d exceeds " + "image width %d", + img->col_offset, imagewidth); + return 0; + } + + TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, + &subsamplingver); + if (subsamplingver == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Invalid vertical YCbCr subsampling"); + return (0); + } + + maxstripsize = TIFFStripSize(tif); + + flip = setorientation(img); + if (flip & FLIP_VERTICALLY) + { + if (w > INT_MAX / 2) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow"); + return (0); + } + y = h - 1; + /* Skew back to the raster row before the currently written row + * -> one raster width plus copied image pixels. */ + toskew = -(int32_t)(w + wmin); + } + else + { + y = 0; + /* Skew forward to the end of the raster width of the row currently + * copied. */ + toskew = w - wmin; + } + + TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); + if (rowsperstrip == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero"); + return (0); + } + + scanline = TIFFScanlineSize(tif); + fromskew = (w < imagewidth ? imagewidth - w : 0); + for (row = 0; row < h; row += nrow) + { + uint32_t temp; + rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip; + nrow = (row + rowstoread > h ? h - row : rowstoread); + nrowsub = nrow; + if ((nrowsub % subsamplingver) != 0) + nrowsub += subsamplingver - nrowsub % subsamplingver; + temp = (row + img->row_offset) % rowsperstrip + nrowsub; + if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Integer overflow in gtStripContig"); + return 0; + } + if (_TIFFReadEncodedStripAndAllocBuffer( + tif, TIFFComputeStrip(tif, row + img->row_offset, 0), + (void **)(&buf), maxstripsize, + temp * scanline) == (tmsize_t)(-1) && + (buf == NULL || img->stoponerr)) + { + ret = 0; + break; + } + + pos = ((row + img->row_offset) % rowsperstrip) * scanline + + ((tmsize_t)img->col_offset * img->samplesperpixel); + tmsize_t roffset = (tmsize_t)y * w; + (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew, + buf + pos); + y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); + } + + if (flip & FLIP_HORIZONTALLY) + { + /* Flips the complete raster matrix horizontally. If raster width is + * larger than image width, data are moved horizontally to the right + * side. + * Use wmin to only flip data in place. */ + uint32_t line; + + for (line = 0; line < h; line++) + { + uint32_t *left = raster + (line * w); + /* Use wmin to only flip horizontally data in place and not complete + * raster-row. */ + uint32_t *right = left + wmin - 1; + + while (left < right) + { + uint32_t temp = *left; + *left = *right; + *right = temp; + left++; + right--; + } + } + } + + _TIFFfreeExt(img->tif, buf); + return (ret); +} + +/* + * Get a strip-organized image with + * SamplesPerPixel > 1 + * PlanarConfiguration separated + * We assume that all such images are RGB. + */ +static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, + uint32_t h) +{ + TIFF *tif = img->tif; + tileSeparateRoutine put = img->put.separate; + unsigned char *buf = NULL; + unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL; + uint32_t row, y, nrow, rowstoread; + tmsize_t pos; + tmsize_t scanline; + uint32_t rowsperstrip, offset_row; + uint32_t imagewidth = img->width; + tmsize_t stripsize; + tmsize_t bufsize; + int32_t fromskew, toskew; + int alpha = img->alpha; + int ret = 1, flip; + uint16_t colorchannels; + + /* If the raster is smaller than the image, + * or if there is a col_offset, adapt the samples to be copied per row. */ + uint32_t wmin; + if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth) + { + wmin = TIFFmin(w, imagewidth - img->col_offset); + } + else + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Error in gtStripSeparate: column offset %d exceeds " + "image width %d", + img->col_offset, imagewidth); + return 0; + } + + stripsize = TIFFStripSize(tif); + bufsize = + _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate"); + if (bufsize == 0) + { + return (0); + } + + flip = setorientation(img); + if (flip & FLIP_VERTICALLY) + { + if (w > INT_MAX / 2) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow"); + return (0); + } + y = h - 1; + /* Skew back to the raster row before the currently written row + * -> one raster width plus one image width. */ + toskew = -(int32_t)(w + wmin); + } + else + { + y = 0; + /* Skew forward to the end of the raster width of the row currently + * written. */ + toskew = w - wmin; + } + + switch (img->photometric) + { + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + case PHOTOMETRIC_PALETTE: + colorchannels = 1; + break; + + default: + colorchannels = 3; + break; + } + + TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); + if (rowsperstrip == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero"); + return (0); + } + + scanline = TIFFScanlineSize(tif); + fromskew = (w < imagewidth ? imagewidth - w : 0); + for (row = 0; row < h; row += nrow) + { + uint32_t temp; + rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip; + nrow = (row + rowstoread > h ? h - row : rowstoread); + offset_row = row + img->row_offset; + temp = (row + img->row_offset) % rowsperstrip + nrow; + if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Integer overflow in gtStripSeparate"); + return 0; + } + if (buf == NULL) + { + if (_TIFFReadEncodedStripAndAllocBuffer( + tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf, + bufsize, temp * scanline) == (tmsize_t)(-1) && + (buf == NULL || img->stoponerr)) + { + ret = 0; + break; + } + p0 = buf; + if (colorchannels == 1) + { + p2 = p1 = p0; + pa = (alpha ? (p0 + 3 * stripsize) : NULL); + } + else + { + p1 = p0 + stripsize; + p2 = p1 + stripsize; + pa = (alpha ? (p2 + stripsize) : NULL); + } + } + else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0), + p0, temp * scanline) == (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + if (colorchannels > 1 && + TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1, + temp * scanline) == (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + if (colorchannels > 1 && + TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2, + temp * scanline) == (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + if (alpha) + { + if (TIFFReadEncodedStrip( + tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa, + temp * scanline) == (tmsize_t)(-1) && + img->stoponerr) + { + ret = 0; + break; + } + } + + /* For SEPARATE the pos-offset is per sample and should not be + * multiplied by img->samplesperpixel. */ + pos = ((row + img->row_offset) % rowsperstrip) * scanline + + (tmsize_t)img->col_offset; + tmsize_t roffset = (tmsize_t)y * w; + (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew, + p0 + pos, p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL)); + y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); + } + + if (flip & FLIP_HORIZONTALLY) + { + uint32_t line; + + for (line = 0; line < h; line++) + { + uint32_t *left = raster + (line * w); + /* Use wmin to only flip horizontally data in place and not complete + * raster-row. */ + uint32_t *right = left + wmin - 1; + + while (left < right) + { + uint32_t temp = *left; + *left = *right; + *right = temp; + left++; + right--; + } + } + } + + _TIFFfreeExt(img->tif, buf); + return (ret); +} + +/* + * The following routines move decoded data returned + * from the TIFF library into rasters filled with packed + * ABGR pixels (i.e. suitable for passing to lrecwrite.) + * + * The routines have been created according to the most + * important cases and optimized. PickContigCase and + * PickSeparateCase analyze the parameters and select + * the appropriate "get" and "put" routine to use. + */ +#define REPEAT8(op) \ + REPEAT4(op); \ + REPEAT4(op) +#define REPEAT4(op) \ + REPEAT2(op); \ + REPEAT2(op) +#define REPEAT2(op) \ + op; \ + op +#define CASE8(x, op) \ + switch (x) \ + { \ + case 7: \ + op; /*-fallthrough*/ \ + case 6: \ + op; /*-fallthrough*/ \ + case 5: \ + op; /*-fallthrough*/ \ + case 4: \ + op; /*-fallthrough*/ \ + case 3: \ + op; /*-fallthrough*/ \ + case 2: \ + op; /*-fallthrough*/ \ + case 1: \ + op; \ + } +#define CASE4(x, op) \ + switch (x) \ + { \ + case 3: \ + op; /*-fallthrough*/ \ + case 2: \ + op; /*-fallthrough*/ \ + case 1: \ + op; \ + } +#define NOP + +#define UNROLL8(w, op1, op2) \ + { \ + uint32_t _x; \ + for (_x = w; _x >= 8; _x -= 8) \ + { \ + op1; \ + REPEAT8(op2); \ + } \ + if (_x > 0) \ + { \ + op1; \ + CASE8(_x, op2); \ + } \ + } +#define UNROLL4(w, op1, op2) \ + { \ + uint32_t _x; \ + for (_x = w; _x >= 4; _x -= 4) \ + { \ + op1; \ + REPEAT4(op2); \ + } \ + if (_x > 0) \ + { \ + op1; \ + CASE4(_x, op2); \ + } \ + } +#define UNROLL2(w, op1, op2) \ + { \ + uint32_t _x; \ + for (_x = w; _x >= 2; _x -= 2) \ + { \ + op1; \ + REPEAT2(op2); \ + } \ + if (_x) \ + { \ + op1; \ + op2; \ + } \ + } + +#define SKEW(r, g, b, skew) \ + { \ + r += skew; \ + g += skew; \ + b += skew; \ + } +#define SKEW4(r, g, b, a, skew) \ + { \ + r += skew; \ + g += skew; \ + b += skew; \ + a += skew; \ + } + +#define A1 (((uint32_t)0xffL) << 24) +#define PACK(r, g, b) \ + ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1) +#define PACK4(r, g, b, a) \ + ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | \ + ((uint32_t)(a) << 24)) +#define W2B(v) (((v) >> 8) & 0xff) +/* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */ +#define PACKW(r, g, b) \ + ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | A1) +#define PACKW4(r, g, b, a) \ + ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | \ + ((uint32_t)W2B(a) << 24)) + +#define DECLAREContigPutFunc(name) \ + static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \ + uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \ + unsigned char *pp) + +/* + * 8-bit palette => colormap/RGB + */ +DECLAREContigPutFunc(put8bitcmaptile) +{ + uint32_t **PALmap = img->PALmap; + int samplesperpixel = img->samplesperpixel; + + (void)y; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + *cp++ = PALmap[*pp][0]; + pp += samplesperpixel; + } + cp += toskew; + pp += fromskew; + } +} + +/* + * 4-bit palette => colormap/RGB + */ +DECLAREContigPutFunc(put4bitcmaptile) +{ + uint32_t **PALmap = img->PALmap; + + (void)x; + (void)y; + fromskew /= 2; + for (; h > 0; --h) + { + uint32_t *bw; + UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++); + cp += toskew; + pp += fromskew; + } +} + +/* + * 2-bit palette => colormap/RGB + */ +DECLAREContigPutFunc(put2bitcmaptile) +{ + uint32_t **PALmap = img->PALmap; + + (void)x; + (void)y; + fromskew /= 4; + for (; h > 0; --h) + { + uint32_t *bw; + UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++); + cp += toskew; + pp += fromskew; + } +} + +/* + * 1-bit palette => colormap/RGB + */ +DECLAREContigPutFunc(put1bitcmaptile) +{ + uint32_t **PALmap = img->PALmap; + + (void)x; + (void)y; + fromskew /= 8; + for (; h > 0; --h) + { + uint32_t *bw; + UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++); + cp += toskew; + pp += fromskew; + } +} + +/* + * 8-bit greyscale => colormap/RGB + */ +DECLAREContigPutFunc(putgreytile) +{ + int samplesperpixel = img->samplesperpixel; + uint32_t **BWmap = img->BWmap; + + (void)y; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + *cp++ = BWmap[*pp][0]; + pp += samplesperpixel; + } + cp += toskew; + pp += fromskew; + } +} + +/* + * 8-bit greyscale with associated alpha => colormap/RGBA + */ +DECLAREContigPutFunc(putagreytile) +{ + int samplesperpixel = img->samplesperpixel; + uint32_t **BWmap = img->BWmap; + + (void)y; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + *cp++ = BWmap[*pp][0] & ((uint32_t) * (pp + 1) << 24 | ~A1); + pp += samplesperpixel; + } + cp += toskew; + pp += fromskew; + } +} + +/* + * 16-bit greyscale => colormap/RGB + */ +DECLAREContigPutFunc(put16bitbwtile) +{ + int samplesperpixel = img->samplesperpixel; + uint32_t **BWmap = img->BWmap; + + (void)y; + for (; h > 0; --h) + { + uint16_t *wp = (uint16_t *)pp; + + for (x = w; x > 0; --x) + { + /* use high order byte of 16bit value */ + + *cp++ = BWmap[*wp >> 8][0]; + pp += 2 * samplesperpixel; + wp += samplesperpixel; + } + cp += toskew; + pp += fromskew; + } +} + +/* + * 1-bit bilevel => colormap/RGB + */ +DECLAREContigPutFunc(put1bitbwtile) +{ + uint32_t **BWmap = img->BWmap; + + (void)x; + (void)y; + fromskew /= 8; + for (; h > 0; --h) + { + uint32_t *bw; + UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++); + cp += toskew; + pp += fromskew; + } +} + +/* + * 2-bit greyscale => colormap/RGB + */ +DECLAREContigPutFunc(put2bitbwtile) +{ + uint32_t **BWmap = img->BWmap; + + (void)x; + (void)y; + fromskew /= 4; + for (; h > 0; --h) + { + uint32_t *bw; + UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++); + cp += toskew; + pp += fromskew; + } +} + +/* + * 4-bit greyscale => colormap/RGB + */ +DECLAREContigPutFunc(put4bitbwtile) +{ + uint32_t **BWmap = img->BWmap; + + (void)x; + (void)y; + fromskew /= 2; + for (; h > 0; --h) + { + uint32_t *bw; + UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++); + cp += toskew; + pp += fromskew; + } +} + +/* + * 8-bit packed samples, no Map => RGB + */ +DECLAREContigPutFunc(putRGBcontig8bittile) +{ + int samplesperpixel = img->samplesperpixel; + + (void)x; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]); + pp += samplesperpixel); + cp += toskew; + pp += fromskew; + } +} + +/* + * 8-bit packed samples => RGBA w/ associated alpha + * (known to have Map == NULL) + */ +DECLAREContigPutFunc(putRGBAAcontig8bittile) +{ + int samplesperpixel = img->samplesperpixel; + + (void)x; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]); + pp += samplesperpixel); + cp += toskew; + pp += fromskew; + } +} + +/* + * 8-bit packed samples => RGBA w/ unassociated alpha + * (known to have Map == NULL) + */ +DECLAREContigPutFunc(putRGBUAcontig8bittile) +{ + int samplesperpixel = img->samplesperpixel; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + uint32_t r, g, b, a; + uint8_t *m; + for (x = w; x > 0; --x) + { + a = pp[3]; + m = img->UaToAa + ((size_t)a << 8); + r = m[pp[0]]; + g = m[pp[1]]; + b = m[pp[2]]; + *cp++ = PACK4(r, g, b, a); + pp += samplesperpixel; + } + cp += toskew; + pp += fromskew; + } +} + +/* + * 16-bit packed samples => RGB + */ +DECLAREContigPutFunc(putRGBcontig16bittile) +{ + int samplesperpixel = img->samplesperpixel; + uint16_t *wp = (uint16_t *)pp; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]], + img->Bitdepth16To8[wp[2]]); + wp += samplesperpixel; + } + cp += toskew; + wp += fromskew; + } +} + +/* + * 16-bit packed samples => RGBA w/ associated alpha + * (known to have Map == NULL) + */ +DECLAREContigPutFunc(putRGBAAcontig16bittile) +{ + int samplesperpixel = img->samplesperpixel; + uint16_t *wp = (uint16_t *)pp; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]], + img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]); + wp += samplesperpixel; + } + cp += toskew; + wp += fromskew; + } +} + +/* + * 16-bit packed samples => RGBA w/ unassociated alpha + * (known to have Map == NULL) + */ +DECLAREContigPutFunc(putRGBUAcontig16bittile) +{ + int samplesperpixel = img->samplesperpixel; + uint16_t *wp = (uint16_t *)pp; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + uint32_t r, g, b, a; + uint8_t *m; + for (x = w; x > 0; --x) + { + a = img->Bitdepth16To8[wp[3]]; + m = img->UaToAa + ((size_t)a << 8); + r = m[img->Bitdepth16To8[wp[0]]]; + g = m[img->Bitdepth16To8[wp[1]]]; + b = m[img->Bitdepth16To8[wp[2]]]; + *cp++ = PACK4(r, g, b, a); + wp += samplesperpixel; + } + cp += toskew; + wp += fromskew; + } +} + +/* + * 8-bit packed CMYK samples w/o Map => RGB + * + * NB: The conversion of CMYK->RGB is *very* crude. + */ +DECLAREContigPutFunc(putRGBcontig8bitCMYKtile) +{ + int samplesperpixel = img->samplesperpixel; + uint16_t r, g, b, k; + + (void)x; + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + UNROLL8(w, NOP, k = 255 - pp[3]; r = (k * (255 - pp[0])) / 255; + g = (k * (255 - pp[1])) / 255; b = (k * (255 - pp[2])) / 255; + *cp++ = PACK(r, g, b); pp += samplesperpixel); + cp += toskew; + pp += fromskew; + } +} + +/* + * 8-bit packed CMYK samples w/Map => RGB + * + * NB: The conversion of CMYK->RGB is *very* crude. + */ +DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile) +{ + int samplesperpixel = img->samplesperpixel; + TIFFRGBValue *Map = img->Map; + uint16_t r, g, b, k; + + (void)y; + fromskew *= samplesperpixel; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + k = 255 - pp[3]; + r = (k * (255 - pp[0])) / 255; + g = (k * (255 - pp[1])) / 255; + b = (k * (255 - pp[2])) / 255; + *cp++ = PACK(Map[r], Map[g], Map[b]); + pp += samplesperpixel; + } + pp += fromskew; + cp += toskew; + } +} + +#define DECLARESepPutFunc(name) \ + static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \ + uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \ + unsigned char *r, unsigned char *g, unsigned char *b, \ + unsigned char *a) + +/* + * 8-bit unpacked samples => RGB + */ +DECLARESepPutFunc(putRGBseparate8bittile) +{ + (void)img; + (void)x; + (void)y; + (void)a; + for (; h > 0; --h) + { + UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++)); + SKEW(r, g, b, fromskew); + cp += toskew; + } +} + +/* + * 8-bit unpacked samples => RGBA w/ associated alpha + */ +DECLARESepPutFunc(putRGBAAseparate8bittile) +{ + (void)img; + (void)x; + (void)y; + for (; h > 0; --h) + { + UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++)); + SKEW4(r, g, b, a, fromskew); + cp += toskew; + } +} + +/* + * 8-bit unpacked CMYK samples => RGBA + */ +DECLARESepPutFunc(putCMYKseparate8bittile) +{ + (void)img; + (void)y; + for (; h > 0; --h) + { + uint32_t rv, gv, bv, kv; + for (x = w; x > 0; --x) + { + kv = 255 - *a++; + rv = (kv * (255 - *r++)) / 255; + gv = (kv * (255 - *g++)) / 255; + bv = (kv * (255 - *b++)) / 255; + *cp++ = PACK4(rv, gv, bv, 255); + } + SKEW4(r, g, b, a, fromskew); + cp += toskew; + } +} + +/* + * 8-bit unpacked samples => RGBA w/ unassociated alpha + */ +DECLARESepPutFunc(putRGBUAseparate8bittile) +{ + (void)img; + (void)y; + for (; h > 0; --h) + { + uint32_t rv, gv, bv, av; + uint8_t *m; + for (x = w; x > 0; --x) + { + av = *a++; + m = img->UaToAa + ((size_t)av << 8); + rv = m[*r++]; + gv = m[*g++]; + bv = m[*b++]; + *cp++ = PACK4(rv, gv, bv, av); + } + SKEW4(r, g, b, a, fromskew); + cp += toskew; + } +} + +/* + * 16-bit unpacked samples => RGB + */ +DECLARESepPutFunc(putRGBseparate16bittile) +{ + uint16_t *wr = (uint16_t *)r; + uint16_t *wg = (uint16_t *)g; + uint16_t *wb = (uint16_t *)b; + (void)img; + (void)y; + (void)a; + for (; h > 0; --h) + { + for (x = 0; x < w; x++) + *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++], + img->Bitdepth16To8[*wb++]); + SKEW(wr, wg, wb, fromskew); + cp += toskew; + } +} + +/* + * 16-bit unpacked samples => RGBA w/ associated alpha + */ +DECLARESepPutFunc(putRGBAAseparate16bittile) +{ + uint16_t *wr = (uint16_t *)r; + uint16_t *wg = (uint16_t *)g; + uint16_t *wb = (uint16_t *)b; + uint16_t *wa = (uint16_t *)a; + (void)img; + (void)y; + for (; h > 0; --h) + { + for (x = 0; x < w; x++) + *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++], + img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]); + SKEW4(wr, wg, wb, wa, fromskew); + cp += toskew; + } +} + +/* + * 16-bit unpacked samples => RGBA w/ unassociated alpha + */ +DECLARESepPutFunc(putRGBUAseparate16bittile) +{ + uint16_t *wr = (uint16_t *)r; + uint16_t *wg = (uint16_t *)g; + uint16_t *wb = (uint16_t *)b; + uint16_t *wa = (uint16_t *)a; + (void)img; + (void)y; + for (; h > 0; --h) + { + uint32_t r2, g2, b2, a2; + uint8_t *m; + for (x = w; x > 0; --x) + { + a2 = img->Bitdepth16To8[*wa++]; + m = img->UaToAa + ((size_t)a2 << 8); + r2 = m[img->Bitdepth16To8[*wr++]]; + g2 = m[img->Bitdepth16To8[*wg++]]; + b2 = m[img->Bitdepth16To8[*wb++]]; + *cp++ = PACK4(r2, g2, b2, a2); + } + SKEW4(wr, wg, wb, wa, fromskew); + cp += toskew; + } +} + +/* + * 8-bit packed CIE L*a*b 1976 samples => RGB + */ +DECLAREContigPutFunc(putcontig8bitCIELab8) +{ + float X, Y, Z; + uint32_t r, g, b; + (void)y; + fromskew *= 3; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0], + (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z); + TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b); + *cp++ = PACK(r, g, b); + pp += 3; + } + cp += toskew; + pp += fromskew; + } +} + +/* + * 16-bit packed CIE L*a*b 1976 samples => RGB + */ +DECLAREContigPutFunc(putcontig8bitCIELab16) +{ + float X, Y, Z; + uint32_t r, g, b; + uint16_t *wp = (uint16_t *)pp; + (void)y; + fromskew *= 3; + for (; h > 0; --h) + { + for (x = w; x > 0; --x) + { + TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1], + (int16_t)wp[2], &X, &Y, &Z); + TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b); + *cp++ = PACK(r, g, b); + wp += 3; + } + cp += toskew; + wp += fromskew; + } +} + +/* + * YCbCr -> RGB conversion and packing routines. + */ + +#define YCbCrtoRGB(dst, Y) \ + { \ + uint32_t r, g, b; \ + TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \ + dst = PACK(r, g, b); \ + } + +/* + * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr44tile) +{ + uint32_t *cp1 = cp + w + toskew; + uint32_t *cp2 = cp1 + w + toskew; + uint32_t *cp3 = cp2 + w + toskew; + int32_t incr = 3 * w + 4 * toskew; + + (void)y; + /* adjust fromskew */ + fromskew = (fromskew / 4) * (4 * 2 + 2); + if ((h & 3) == 0 && (w & 3) == 0) + { + for (; h >= 4; h -= 4) + { + x = w >> 2; + do + { + int32_t Cb = pp[16]; + int32_t Cr = pp[17]; + + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp[1], pp[1]); + YCbCrtoRGB(cp[2], pp[2]); + YCbCrtoRGB(cp[3], pp[3]); + YCbCrtoRGB(cp1[0], pp[4]); + YCbCrtoRGB(cp1[1], pp[5]); + YCbCrtoRGB(cp1[2], pp[6]); + YCbCrtoRGB(cp1[3], pp[7]); + YCbCrtoRGB(cp2[0], pp[8]); + YCbCrtoRGB(cp2[1], pp[9]); + YCbCrtoRGB(cp2[2], pp[10]); + YCbCrtoRGB(cp2[3], pp[11]); + YCbCrtoRGB(cp3[0], pp[12]); + YCbCrtoRGB(cp3[1], pp[13]); + YCbCrtoRGB(cp3[2], pp[14]); + YCbCrtoRGB(cp3[3], pp[15]); + + cp += 4; + cp1 += 4; + cp2 += 4; + cp3 += 4; + pp += 18; + } while (--x); + cp += incr; + cp1 += incr; + cp2 += incr; + cp3 += incr; + pp += fromskew; + } + } + else + { + while (h > 0) + { + for (x = w; x > 0;) + { + int32_t Cb = pp[16]; + int32_t Cr = pp[17]; + switch (x) + { + default: + switch (h) + { + default: + YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */ + case 3: + YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */ + case 2: + YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + case 3: + switch (h) + { + default: + YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */ + case 3: + YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */ + case 2: + YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + case 2: + switch (h) + { + default: + YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */ + case 3: + YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */ + case 2: + YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + case 1: + switch (h) + { + default: + YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */ + case 3: + YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */ + case 2: + YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + } + if (x < 4) + { + cp += x; + cp1 += x; + cp2 += x; + cp3 += x; + x = 0; + } + else + { + cp += 4; + cp1 += 4; + cp2 += 4; + cp3 += 4; + x -= 4; + } + pp += 18; + } + if (h <= 4) + break; + h -= 4; + cp += incr; + cp1 += incr; + cp2 += incr; + cp3 += incr; + pp += fromskew; + } + } +} + +/* + * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr42tile) +{ + uint32_t *cp1 = cp + w + toskew; + int32_t incr = 2 * toskew + w; + + (void)y; + fromskew = (fromskew / 4) * (4 * 2 + 2); + if ((w & 3) == 0 && (h & 1) == 0) + { + for (; h >= 2; h -= 2) + { + x = w >> 2; + do + { + int32_t Cb = pp[8]; + int32_t Cr = pp[9]; + + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp[1], pp[1]); + YCbCrtoRGB(cp[2], pp[2]); + YCbCrtoRGB(cp[3], pp[3]); + YCbCrtoRGB(cp1[0], pp[4]); + YCbCrtoRGB(cp1[1], pp[5]); + YCbCrtoRGB(cp1[2], pp[6]); + YCbCrtoRGB(cp1[3], pp[7]); + + cp += 4; + cp1 += 4; + pp += 10; + } while (--x); + cp += incr; + cp1 += incr; + pp += fromskew; + } + } + else + { + while (h > 0) + { + for (x = w; x > 0;) + { + int32_t Cb = pp[8]; + int32_t Cr = pp[9]; + switch (x) + { + default: + switch (h) + { + default: + YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + case 3: + switch (h) + { + default: + YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + case 2: + switch (h) + { + default: + YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + case 1: + switch (h) + { + default: + YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */ + case 1: + YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */ + } /* FALLTHROUGH */ + } + if (x < 4) + { + cp += x; + cp1 += x; + x = 0; + } + else + { + cp += 4; + cp1 += 4; + x -= 4; + } + pp += 10; + } + if (h <= 2) + break; + h -= 2; + cp += incr; + cp1 += incr; + pp += fromskew; + } + } +} + +/* + * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr41tile) +{ + (void)y; + fromskew = (fromskew / 4) * (4 * 1 + 2); + do + { + x = w >> 2; + while (x > 0) + { + int32_t Cb = pp[4]; + int32_t Cr = pp[5]; + + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp[1], pp[1]); + YCbCrtoRGB(cp[2], pp[2]); + YCbCrtoRGB(cp[3], pp[3]); + + cp += 4; + pp += 6; + x--; + } + + if ((w & 3) != 0) + { + int32_t Cb = pp[4]; + int32_t Cr = pp[5]; + + switch ((w & 3)) + { + case 3: + YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/ + case 2: + YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/ + case 1: + YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/ + case 0: + break; + } + + cp += (w & 3); + pp += 6; + } + + cp += toskew; + pp += fromskew; + } while (--h); +} + +/* + * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr22tile) +{ + uint32_t *cp2; + int32_t incr = 2 * toskew + w; + (void)y; + fromskew = (fromskew / 2) * (2 * 2 + 2); + cp2 = cp + w + toskew; + while (h >= 2) + { + x = w; + while (x >= 2) + { + uint32_t Cb = pp[4]; + uint32_t Cr = pp[5]; + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp[1], pp[1]); + YCbCrtoRGB(cp2[0], pp[2]); + YCbCrtoRGB(cp2[1], pp[3]); + cp += 2; + cp2 += 2; + pp += 6; + x -= 2; + } + if (x == 1) + { + uint32_t Cb = pp[4]; + uint32_t Cr = pp[5]; + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp2[0], pp[2]); + cp++; + cp2++; + pp += 6; + } + cp += incr; + cp2 += incr; + pp += fromskew; + h -= 2; + } + if (h == 1) + { + x = w; + while (x >= 2) + { + uint32_t Cb = pp[4]; + uint32_t Cr = pp[5]; + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp[1], pp[1]); + cp += 2; + cp2 += 2; + pp += 6; + x -= 2; + } + if (x == 1) + { + uint32_t Cb = pp[4]; + uint32_t Cr = pp[5]; + YCbCrtoRGB(cp[0], pp[0]); + } + } +} + +/* + * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr21tile) +{ + (void)y; + fromskew = (fromskew / 2) * (2 * 1 + 2); + do + { + x = w >> 1; + while (x > 0) + { + int32_t Cb = pp[2]; + int32_t Cr = pp[3]; + + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp[1], pp[1]); + + cp += 2; + pp += 4; + x--; + } + + if ((w & 1) != 0) + { + int32_t Cb = pp[2]; + int32_t Cr = pp[3]; + + YCbCrtoRGB(cp[0], pp[0]); + + cp += 1; + pp += 4; + } + + cp += toskew; + pp += fromskew; + } while (--h); +} + +/* + * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr12tile) +{ + uint32_t *cp2; + int32_t incr = 2 * toskew + w; + (void)y; + fromskew = (fromskew / 1) * (1 * 2 + 2); + cp2 = cp + w + toskew; + while (h >= 2) + { + x = w; + do + { + uint32_t Cb = pp[2]; + uint32_t Cr = pp[3]; + YCbCrtoRGB(cp[0], pp[0]); + YCbCrtoRGB(cp2[0], pp[1]); + cp++; + cp2++; + pp += 4; + } while (--x); + cp += incr; + cp2 += incr; + pp += fromskew; + h -= 2; + } + if (h == 1) + { + x = w; + do + { + uint32_t Cb = pp[2]; + uint32_t Cr = pp[3]; + YCbCrtoRGB(cp[0], pp[0]); + cp++; + pp += 4; + } while (--x); + } +} + +/* + * 8-bit packed YCbCr samples w/ no subsampling => RGB + */ +DECLAREContigPutFunc(putcontig8bitYCbCr11tile) +{ + (void)y; + fromskew = (fromskew / 1) * (1 * 1 + 2); + do + { + x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */ + do + { + int32_t Cb = pp[1]; + int32_t Cr = pp[2]; + + YCbCrtoRGB(*cp++, pp[0]); + + pp += 3; + } while (--x); + cp += toskew; + pp += fromskew; + } while (--h); +} + +/* + * 8-bit packed YCbCr samples w/ no subsampling => RGB + */ +DECLARESepPutFunc(putseparate8bitYCbCr11tile) +{ + (void)y; + (void)a; + /* TODO: naming of input vars is still off, change obfuscating declaration + * inside define, or resolve obfuscation */ + for (; h > 0; --h) + { + x = w; + do + { + uint32_t dr, dg, db; + TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db); + *cp++ = PACK(dr, dg, db); + } while (--x); + SKEW(r, g, b, fromskew); + cp += toskew; + } +} +#undef YCbCrtoRGB + +static int isInRefBlackWhiteRange(float f) +{ + return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF; +} + +static int initYCbCrConversion(TIFFRGBAImage *img) +{ + static const char module[] = "initYCbCrConversion"; + + float *luma, *refBlackWhite; + + if (img->ycbcr == NULL) + { + img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt( + img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) + + 4 * 256 * sizeof(TIFFRGBValue) + + 2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t)); + if (img->ycbcr == NULL) + { + TIFFErrorExtR(img->tif, module, + "No space for YCbCr->RGB conversion state"); + return (0); + } + } + + TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma); + TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE, + &refBlackWhite); + + /* Do some validation to avoid later issues. Detect NaN for now */ + /* and also if lumaGreen is zero since we divide by it later */ + if (luma[0] != luma[0] || luma[1] != luma[1] || luma[1] == 0.0 || + luma[2] != luma[2]) + { + TIFFErrorExtR(img->tif, module, + "Invalid values for YCbCrCoefficients tag"); + return (0); + } + + if (!isInRefBlackWhiteRange(refBlackWhite[0]) || + !isInRefBlackWhiteRange(refBlackWhite[1]) || + !isInRefBlackWhiteRange(refBlackWhite[2]) || + !isInRefBlackWhiteRange(refBlackWhite[3]) || + !isInRefBlackWhiteRange(refBlackWhite[4]) || + !isInRefBlackWhiteRange(refBlackWhite[5])) + { + TIFFErrorExtR(img->tif, module, + "Invalid values for ReferenceBlackWhite tag"); + return (0); + } + + if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0) + return (0); + return (1); +} + +static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img) +{ + static const char module[] = "initCIELabConversion"; + + float *whitePoint; + float refWhite[3]; + + TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint); + if (whitePoint[1] == 0.0f) + { + TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag."); + return NULL; + } + + if (!img->cielab) + { + img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt( + img->tif, sizeof(TIFFCIELabToRGB)); + if (!img->cielab) + { + TIFFErrorExtR(img->tif, module, + "No space for CIE L*a*b*->RGB conversion state."); + return NULL; + } + } + + refWhite[1] = 100.0F; + refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1]; + refWhite[2] = + (1.0F - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1]; + if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) + { + TIFFErrorExtR(img->tif, module, + "Failed to initialize CIE L*a*b*->RGB conversion state."); + _TIFFfreeExt(img->tif, img->cielab); + return NULL; + } + + if (img->bitspersample == 8) + return putcontig8bitCIELab8; + else if (img->bitspersample == 16) + return putcontig8bitCIELab16; + return NULL; +} + +/* + * Greyscale images with less than 8 bits/sample are handled + * with a table to avoid lots of shifts and masks. The table + * is setup so that put*bwtile (below) can retrieve 8/bitspersample + * pixel values simply by indexing into the table with one + * number. + */ +static int makebwmap(TIFFRGBAImage *img) +{ + TIFFRGBValue *Map = img->Map; + int bitspersample = img->bitspersample; + int nsamples = 8 / bitspersample; + int i; + uint32_t *p; + + if (nsamples == 0) + nsamples = 1; + + img->BWmap = (uint32_t **)_TIFFmallocExt( + img->tif, + 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t))); + if (img->BWmap == NULL) + { + TIFFErrorExtR(img->tif, TIFFFileName(img->tif), + "No space for B&W mapping table"); + return (0); + } + p = (uint32_t *)(img->BWmap + 256); + for (i = 0; i < 256; i++) + { + TIFFRGBValue c; + img->BWmap[i] = p; + switch (bitspersample) + { +#define GREY(x) \ + c = Map[x]; \ + *p++ = PACK(c, c, c); + case 1: + GREY(i >> 7); + GREY((i >> 6) & 1); + GREY((i >> 5) & 1); + GREY((i >> 4) & 1); + GREY((i >> 3) & 1); + GREY((i >> 2) & 1); + GREY((i >> 1) & 1); + GREY(i & 1); + break; + case 2: + GREY(i >> 6); + GREY((i >> 4) & 3); + GREY((i >> 2) & 3); + GREY(i & 3); + break; + case 4: + GREY(i >> 4); + GREY(i & 0xf); + break; + case 8: + case 16: + GREY(i); + break; + } +#undef GREY + } + return (1); +} + +/* + * Construct a mapping table to convert from the range + * of the data samples to [0,255] --for display. This + * process also handles inverting B&W images when needed. + */ +static int setupMap(TIFFRGBAImage *img) +{ + int32_t x, range; + + range = (int32_t)((1L << img->bitspersample) - 1); + + /* treat 16 bit the same as eight bit */ + if (img->bitspersample == 16) + range = (int32_t)255; + + img->Map = (TIFFRGBValue *)_TIFFmallocExt( + img->tif, (range + 1) * sizeof(TIFFRGBValue)); + if (img->Map == NULL) + { + TIFFErrorExtR(img->tif, TIFFFileName(img->tif), + "No space for photometric conversion table"); + return (0); + } + if (img->photometric == PHOTOMETRIC_MINISWHITE) + { + for (x = 0; x <= range; x++) + img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range); + } + else + { + for (x = 0; x <= range; x++) + img->Map[x] = (TIFFRGBValue)((x * 255) / range); + } + if (img->bitspersample <= 16 && + (img->photometric == PHOTOMETRIC_MINISBLACK || + img->photometric == PHOTOMETRIC_MINISWHITE)) + { + /* + * Use photometric mapping table to construct + * unpacking tables for samples <= 8 bits. + */ + if (!makebwmap(img)) + return (0); + /* no longer need Map, free it */ + _TIFFfreeExt(img->tif, img->Map); + img->Map = NULL; + } + return (1); +} + +static int checkcmap(TIFFRGBAImage *img) +{ + uint16_t *r = img->redcmap; + uint16_t *g = img->greencmap; + uint16_t *b = img->bluecmap; + long n = 1L << img->bitspersample; + + while (n-- > 0) + if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256) + return (16); + return (8); +} + +static void cvtcmap(TIFFRGBAImage *img) +{ + uint16_t *r = img->redcmap; + uint16_t *g = img->greencmap; + uint16_t *b = img->bluecmap; + long i; + + for (i = (1L << img->bitspersample) - 1; i >= 0; i--) + { +#define CVT(x) ((uint16_t)((x) >> 8)) + r[i] = CVT(r[i]); + g[i] = CVT(g[i]); + b[i] = CVT(b[i]); +#undef CVT + } +} + +/* + * Palette images with <= 8 bits/sample are handled + * with a table to avoid lots of shifts and masks. The table + * is setup so that put*cmaptile (below) can retrieve 8/bitspersample + * pixel values simply by indexing into the table with one + * number. + */ +static int makecmap(TIFFRGBAImage *img) +{ + int bitspersample = img->bitspersample; + int nsamples = 8 / bitspersample; + uint16_t *r = img->redcmap; + uint16_t *g = img->greencmap; + uint16_t *b = img->bluecmap; + uint32_t *p; + int i; + + img->PALmap = (uint32_t **)_TIFFmallocExt( + img->tif, + 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t))); + if (img->PALmap == NULL) + { + TIFFErrorExtR(img->tif, TIFFFileName(img->tif), + "No space for Palette mapping table"); + return (0); + } + p = (uint32_t *)(img->PALmap + 256); + for (i = 0; i < 256; i++) + { + TIFFRGBValue c; + img->PALmap[i] = p; +#define CMAP(x) \ + c = (TIFFRGBValue)x; \ + *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff); + switch (bitspersample) + { + case 1: + CMAP(i >> 7); + CMAP((i >> 6) & 1); + CMAP((i >> 5) & 1); + CMAP((i >> 4) & 1); + CMAP((i >> 3) & 1); + CMAP((i >> 2) & 1); + CMAP((i >> 1) & 1); + CMAP(i & 1); + break; + case 2: + CMAP(i >> 6); + CMAP((i >> 4) & 3); + CMAP((i >> 2) & 3); + CMAP(i & 3); + break; + case 4: + CMAP(i >> 4); + CMAP(i & 0xf); + break; + case 8: + CMAP(i); + break; + } +#undef CMAP + } + return (1); +} + +/* + * Construct any mapping table used + * by the associated put routine. + */ +static int buildMap(TIFFRGBAImage *img) +{ + switch (img->photometric) + { + case PHOTOMETRIC_RGB: + case PHOTOMETRIC_YCBCR: + case PHOTOMETRIC_SEPARATED: + if (img->bitspersample == 8) + break; + /* fall through... */ + case PHOTOMETRIC_MINISBLACK: + case PHOTOMETRIC_MINISWHITE: + if (!setupMap(img)) + return (0); + break; + case PHOTOMETRIC_PALETTE: + /* + * Convert 16-bit colormap to 8-bit (unless it looks + * like an old-style 8-bit colormap). + */ + if (checkcmap(img) == 16) + cvtcmap(img); + else + TIFFWarningExtR(img->tif, TIFFFileName(img->tif), + "Assuming 8-bit colormap"); + /* + * Use mapping table and colormap to construct + * unpacking tables for samples < 8 bits. + */ + if (img->bitspersample <= 8 && !makecmap(img)) + return (0); + break; + } + return (1); +} + +/* + * Select the appropriate conversion routine for packed data. + */ +static int PickContigCase(TIFFRGBAImage *img) +{ + img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig; + img->put.contig = NULL; + switch (img->photometric) + { + case PHOTOMETRIC_RGB: + switch (img->bitspersample) + { + case 8: + if (img->alpha == EXTRASAMPLE_ASSOCALPHA && + img->samplesperpixel >= 4) + img->put.contig = putRGBAAcontig8bittile; + else if (img->alpha == EXTRASAMPLE_UNASSALPHA && + img->samplesperpixel >= 4) + { + if (BuildMapUaToAa(img)) + img->put.contig = putRGBUAcontig8bittile; + } + else if (img->samplesperpixel >= 3) + img->put.contig = putRGBcontig8bittile; + break; + case 16: + if (img->alpha == EXTRASAMPLE_ASSOCALPHA && + img->samplesperpixel >= 4) + { + if (BuildMapBitdepth16To8(img)) + img->put.contig = putRGBAAcontig16bittile; + } + else if (img->alpha == EXTRASAMPLE_UNASSALPHA && + img->samplesperpixel >= 4) + { + if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img)) + img->put.contig = putRGBUAcontig16bittile; + } + else if (img->samplesperpixel >= 3) + { + if (BuildMapBitdepth16To8(img)) + img->put.contig = putRGBcontig16bittile; + } + break; + } + break; + case PHOTOMETRIC_SEPARATED: + if (img->samplesperpixel >= 4 && buildMap(img)) + { + if (img->bitspersample == 8) + { + if (!img->Map) + img->put.contig = putRGBcontig8bitCMYKtile; + else + img->put.contig = putRGBcontig8bitCMYKMaptile; + } + } + break; + case PHOTOMETRIC_PALETTE: + if (buildMap(img)) + { + switch (img->bitspersample) + { + case 8: + img->put.contig = put8bitcmaptile; + break; + case 4: + img->put.contig = put4bitcmaptile; + break; + case 2: + img->put.contig = put2bitcmaptile; + break; + case 1: + img->put.contig = put1bitcmaptile; + break; + } + } + break; + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + if (buildMap(img)) + { + switch (img->bitspersample) + { + case 16: + img->put.contig = put16bitbwtile; + break; + case 8: + if (img->alpha && img->samplesperpixel == 2) + img->put.contig = putagreytile; + else + img->put.contig = putgreytile; + break; + case 4: + img->put.contig = put4bitbwtile; + break; + case 2: + img->put.contig = put2bitbwtile; + break; + case 1: + img->put.contig = put1bitbwtile; + break; + } + } + break; + case PHOTOMETRIC_YCBCR: + if ((img->bitspersample == 8) && (img->samplesperpixel == 3)) + { + if (initYCbCrConversion(img) != 0) + { + /* + * The 6.0 spec says that subsampling must be + * one of 1, 2, or 4, and that vertical subsampling + * must always be <= horizontal subsampling; so + * there are only a few possibilities and we just + * enumerate the cases. + * Joris: added support for the [1,2] case, nonetheless, to + * accommodate some OJPEG files + */ + uint16_t SubsamplingHor; + uint16_t SubsamplingVer; + TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, + &SubsamplingHor, &SubsamplingVer); + switch ((SubsamplingHor << 4) | SubsamplingVer) + { + case 0x44: + img->put.contig = putcontig8bitYCbCr44tile; + break; + case 0x42: + img->put.contig = putcontig8bitYCbCr42tile; + break; + case 0x41: + img->put.contig = putcontig8bitYCbCr41tile; + break; + case 0x22: + img->put.contig = putcontig8bitYCbCr22tile; + break; + case 0x21: + img->put.contig = putcontig8bitYCbCr21tile; + break; + case 0x12: + img->put.contig = putcontig8bitYCbCr12tile; + break; + case 0x11: + img->put.contig = putcontig8bitYCbCr11tile; + break; + } + } + } + break; + case PHOTOMETRIC_CIELAB: + if (img->samplesperpixel == 3 && buildMap(img)) + { + if (img->bitspersample == 8 || img->bitspersample == 16) + img->put.contig = initCIELabConversion(img); + break; + } + } + return ((img->get != NULL) && (img->put.contig != NULL)); +} + +/* + * Select the appropriate conversion routine for unpacked data. + * + * NB: we assume that unpacked single channel data is directed + * to the "packed routines. + */ +static int PickSeparateCase(TIFFRGBAImage *img) +{ + img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate; + img->put.separate = NULL; + switch (img->photometric) + { + case PHOTOMETRIC_MINISWHITE: + case PHOTOMETRIC_MINISBLACK: + /* greyscale images processed pretty much as RGB by gtTileSeparate + */ + case PHOTOMETRIC_RGB: + switch (img->bitspersample) + { + case 8: + if (img->alpha == EXTRASAMPLE_ASSOCALPHA) + img->put.separate = putRGBAAseparate8bittile; + else if (img->alpha == EXTRASAMPLE_UNASSALPHA) + { + if (BuildMapUaToAa(img)) + img->put.separate = putRGBUAseparate8bittile; + } + else + img->put.separate = putRGBseparate8bittile; + break; + case 16: + if (img->alpha == EXTRASAMPLE_ASSOCALPHA) + { + if (BuildMapBitdepth16To8(img)) + img->put.separate = putRGBAAseparate16bittile; + } + else if (img->alpha == EXTRASAMPLE_UNASSALPHA) + { + if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img)) + img->put.separate = putRGBUAseparate16bittile; + } + else + { + if (BuildMapBitdepth16To8(img)) + img->put.separate = putRGBseparate16bittile; + } + break; + } + break; + case PHOTOMETRIC_SEPARATED: + if (img->bitspersample == 8 && img->samplesperpixel == 4) + { + /* Not alpha, but seems like the only way to get 4th band */ + img->alpha = 1; + img->put.separate = putCMYKseparate8bittile; + } + break; + case PHOTOMETRIC_YCBCR: + if ((img->bitspersample == 8) && (img->samplesperpixel == 3)) + { + if (initYCbCrConversion(img) != 0) + { + uint16_t hs, vs; + TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, + &hs, &vs); + switch ((hs << 4) | vs) + { + case 0x11: + img->put.separate = putseparate8bitYCbCr11tile; + break; + /* TODO: add other cases here */ + } + } + } + break; + } + return ((img->get != NULL) && (img->put.separate != NULL)); +} + +static int BuildMapUaToAa(TIFFRGBAImage *img) +{ + static const char module[] = "BuildMapUaToAa"; + uint8_t *m; + uint16_t na, nv; + assert(img->UaToAa == NULL); + img->UaToAa = _TIFFmallocExt(img->tif, 65536); + if (img->UaToAa == NULL) + { + TIFFErrorExtR(img->tif, module, "Out of memory"); + return (0); + } + m = img->UaToAa; + for (na = 0; na < 256; na++) + { + for (nv = 0; nv < 256; nv++) + *m++ = (uint8_t)((nv * na + 127) / 255); + } + return (1); +} + +static int BuildMapBitdepth16To8(TIFFRGBAImage *img) +{ + static const char module[] = "BuildMapBitdepth16To8"; + uint8_t *m; + uint32_t n; + assert(img->Bitdepth16To8 == NULL); + img->Bitdepth16To8 = _TIFFmallocExt(img->tif, 65536); + if (img->Bitdepth16To8 == NULL) + { + TIFFErrorExtR(img->tif, module, "Out of memory"); + return (0); + } + m = img->Bitdepth16To8; + for (n = 0; n < 65536; n++) + *m++ = (uint8_t)((n + 128) / 257); + return (1); +} + +/* + * Read a whole strip off data from the file, and convert to RGBA form. + * If this is the last strip, then it will only contain the portion of + * the strip that is actually within the image space. The result is + * organized in bottom to top form. + */ + +int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster) + +{ + return TIFFReadRGBAStripExt(tif, row, raster, 0); +} + +int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster, + int stop_on_error) + +{ + char emsg[EMSG_BUF_SIZE] = ""; + TIFFRGBAImage img; + int ok; + uint32_t rowsperstrip, rows_to_read; + + if (TIFFIsTiled(tif)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Can't use TIFFReadRGBAStrip() with tiled file."); + return (0); + } + + TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); + + if (rowsperstrip == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero"); + return (0); + } + + if ((row % rowsperstrip) != 0) + { + TIFFErrorExtR( + tif, TIFFFileName(tif), + "Row passed to TIFFReadRGBAStrip() must be first in a strip."); + return (0); + } + + if (TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg)) + { + if (row >= img.height) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Invalid row passed to TIFFReadRGBAStrip()."); + TIFFRGBAImageEnd(&img); + return (0); + } + + img.row_offset = row; + img.col_offset = 0; + + if (row + rowsperstrip > img.height) + rows_to_read = img.height - row; + else + rows_to_read = rowsperstrip; + + ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read); + + TIFFRGBAImageEnd(&img); + } + else + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg); + ok = 0; + } + + return (ok); +} + +/* + * Read a whole tile off data from the file, and convert to RGBA form. + * The returned RGBA data is organized from bottom to top of tile, + * and may include zeroed areas if the tile extends off the image. + */ + +int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster) + +{ + return TIFFReadRGBATileExt(tif, col, row, raster, 0); +} + +int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster, + int stop_on_error) +{ + char emsg[EMSG_BUF_SIZE] = ""; + TIFFRGBAImage img; + int ok; + uint32_t tile_xsize, tile_ysize; + uint32_t read_xsize, read_ysize; + uint32_t i_row; + + /* + * Verify that our request is legal - on a tile file, and on a + * tile boundary. + */ + + if (!TIFFIsTiled(tif)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Can't use TIFFReadRGBATile() with striped file."); + return (0); + } + + TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize); + TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize); + if (tile_xsize == 0 || tile_ysize == 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "tile_xsize or tile_ysize is zero"); + return (0); + } + + if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Row/col passed to TIFFReadRGBATile() must be top" + "left corner of a tile."); + return (0); + } + + /* + * Setup the RGBA reader. + */ + + if (!TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg)) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg); + return (0); + } + + if (col >= img.width || row >= img.height) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Invalid row/col passed to TIFFReadRGBATile()."); + TIFFRGBAImageEnd(&img); + return (0); + } + + /* + * The TIFFRGBAImageGet() function doesn't allow us to get off the + * edge of the image, even to fill an otherwise valid tile. So we + * figure out how much we can read, and fix up the tile buffer to + * a full tile configuration afterwards. + */ + + if (row + tile_ysize > img.height) + read_ysize = img.height - row; + else + read_ysize = tile_ysize; + + if (col + tile_xsize > img.width) + read_xsize = img.width - col; + else + read_xsize = tile_xsize; + + /* + * Read the chunk of imagery. + */ + + img.row_offset = row; + img.col_offset = col; + + ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize); + + TIFFRGBAImageEnd(&img); + + /* + * If our read was incomplete we will need to fix up the tile by + * shifting the data around as if a full tile of data is being returned. + * + * This is all the more complicated because the image is organized in + * bottom to top format. + */ + + if (read_xsize == tile_xsize && read_ysize == tile_ysize) + return (ok); + + for (i_row = 0; i_row < read_ysize; i_row++) + { + memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, + raster + (size_t)(read_ysize - i_row - 1) * read_xsize, + read_xsize * sizeof(uint32_t)); + _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize + + read_xsize, + 0, sizeof(uint32_t) * (tile_xsize - read_xsize)); + } + + for (i_row = read_ysize; i_row < tile_ysize; i_row++) + { + _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0, + sizeof(uint32_t) * tile_xsize); + } + + return (ok); +} diff --git a/cpp/3rd_party/libtiff/tif_hash_set.c b/cpp/3rd_party/libtiff/tif_hash_set.c new file mode 100644 index 0000000000..81dea3fcf2 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_hash_set.c @@ -0,0 +1,603 @@ +/********************************************************************** + * + * Name: tif_hash_set.c + * Purpose: Hash set functions. + * Author: Even Rouault, + * + ********************************************************************** + * Copyright (c) 2008-2009, Even Rouault + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + ****************************************************************************/ + +#include "tif_config.h" + +#include "tif_hash_set.h" + +#include +#include +#include +#include +#include + +/** List element structure. */ +typedef struct _TIFFList TIFFList; + +/** List element structure. */ +struct _TIFFList +{ + /*! Pointer to the data object. Should be allocated and freed by the + * caller. + * */ + void *pData; + /*! Pointer to the next element in list. NULL, if current element is the + * last one. + */ + struct _TIFFList *psNext; +}; + +struct _TIFFHashSet +{ + TIFFHashSetHashFunc fnHashFunc; + TIFFHashSetEqualFunc fnEqualFunc; + TIFFHashSetFreeEltFunc fnFreeEltFunc; + TIFFList **tabList; + int nSize; + int nIndiceAllocatedSize; + int nAllocatedSize; + TIFFList *psRecyclingList; + int nRecyclingListSize; + bool bRehash; +#ifdef HASH_DEBUG + int nCollisions; +#endif +}; + +static const int anPrimes[] = { + 53, 97, 193, 389, 769, 1543, 3079, + 6151, 12289, 24593, 49157, 98317, 196613, 393241, + 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, + 100663319, 201326611, 402653189, 805306457, 1610612741}; + +/************************************************************************/ +/* TIFFHashSetHashPointer() */ +/************************************************************************/ + +/** + * Hash function for an arbitrary pointer + * + * @param elt the arbitrary pointer to hash + * + * @return the hash value of the pointer + */ + +static unsigned long TIFFHashSetHashPointer(const void *elt) +{ + return (unsigned long)(uintptr_t)((void *)(elt)); +} + +/************************************************************************/ +/* TIFFHashSetEqualPointer() */ +/************************************************************************/ + +/** + * Equality function for arbitrary pointers + * + * @param elt1 the first arbitrary pointer to compare + * @param elt2 the second arbitrary pointer to compare + * + * @return true if the pointers are equal + */ + +static bool TIFFHashSetEqualPointer(const void *elt1, const void *elt2) +{ + return elt1 == elt2; +} + +/************************************************************************/ +/* TIFFHashSetNew() */ +/************************************************************************/ + +/** + * Creates a new hash set + * + * The hash function must return a hash value for the elements to insert. + * If fnHashFunc is NULL, TIFFHashSetHashPointer will be used. + * + * The equal function must return if two elements are equal. + * If fnEqualFunc is NULL, TIFFHashSetEqualPointer will be used. + * + * The free function is used to free elements inserted in the hash set, + * when the hash set is destroyed, when elements are removed or replaced. + * If fnFreeEltFunc is NULL, elements inserted into the hash set will not be + * freed. + * + * @param fnHashFunc hash function. May be NULL. + * @param fnEqualFunc equal function. May be NULL. + * @param fnFreeEltFunc element free function. May be NULL. + * + * @return a new hash set + */ + +TIFFHashSet *TIFFHashSetNew(TIFFHashSetHashFunc fnHashFunc, + TIFFHashSetEqualFunc fnEqualFunc, + TIFFHashSetFreeEltFunc fnFreeEltFunc) +{ + TIFFHashSet *set = (TIFFHashSet *)malloc(sizeof(TIFFHashSet)); + if (set == NULL) + return NULL; + set->fnHashFunc = fnHashFunc ? fnHashFunc : TIFFHashSetHashPointer; + set->fnEqualFunc = fnEqualFunc ? fnEqualFunc : TIFFHashSetEqualPointer; + set->fnFreeEltFunc = fnFreeEltFunc; + set->nSize = 0; + set->tabList = (TIFFList **)(calloc(53, sizeof(TIFFList *))); + if (set->tabList == NULL) + { + free(set); + return NULL; + } + set->nIndiceAllocatedSize = 0; + set->nAllocatedSize = 53; + set->psRecyclingList = NULL; + set->nRecyclingListSize = 0; + set->bRehash = false; +#ifdef HASH_DEBUG + set->nCollisions = 0; +#endif + return set; +} + +/************************************************************************/ +/* TIFFHashSetSize() */ +/************************************************************************/ + +/** + * Returns the number of elements inserted in the hash set + * + * Note: this is not the internal size of the hash set + * + * @param set the hash set + * + * @return the number of elements in the hash set + */ + +int TIFFHashSetSize(const TIFFHashSet *set) +{ + assert(set != NULL); + return set->nSize; +} + +/************************************************************************/ +/* TIFFHashSetGetNewListElt() */ +/************************************************************************/ + +static TIFFList *TIFFHashSetGetNewListElt(TIFFHashSet *set) +{ + if (set->psRecyclingList) + { + TIFFList *psRet = set->psRecyclingList; + psRet->pData = NULL; + set->nRecyclingListSize--; + set->psRecyclingList = psRet->psNext; + return psRet; + } + + return (TIFFList *)malloc(sizeof(TIFFList)); +} + +/************************************************************************/ +/* TIFFHashSetReturnListElt() */ +/************************************************************************/ + +static void TIFFHashSetReturnListElt(TIFFHashSet *set, TIFFList *psList) +{ + if (set->nRecyclingListSize < 128) + { + psList->psNext = set->psRecyclingList; + set->psRecyclingList = psList; + set->nRecyclingListSize++; + } + else + { + free(psList); + } +} + +/************************************************************************/ +/* TIFFHashSetClearInternal() */ +/************************************************************************/ + +static void TIFFHashSetClearInternal(TIFFHashSet *set, bool bFinalize) +{ + assert(set != NULL); + for (int i = 0; i < set->nAllocatedSize; i++) + { + TIFFList *cur = set->tabList[i]; + while (cur) + { + if (set->fnFreeEltFunc) + set->fnFreeEltFunc(cur->pData); + TIFFList *psNext = cur->psNext; + if (bFinalize) + free(cur); + else + TIFFHashSetReturnListElt(set, cur); + cur = psNext; + } + set->tabList[i] = NULL; + } + set->bRehash = false; +} + +/************************************************************************/ +/* TIFFListDestroy() */ +/************************************************************************/ + +/** + * Destroy a list. Caller responsible for freeing data objects contained in + * list elements. + * + * @param psList pointer to list head. + * + */ + +static void TIFFListDestroy(TIFFList *psList) +{ + TIFFList *psCurrent = psList; + + while (psCurrent) + { + TIFFList *const psNext = psCurrent->psNext; + free(psCurrent); + psCurrent = psNext; + } +} + +/************************************************************************/ +/* TIFFHashSetDestroy() */ +/************************************************************************/ + +/** + * Destroys an allocated hash set. + * + * This function also frees the elements if a free function was + * provided at the creation of the hash set. + * + * @param set the hash set + */ + +void TIFFHashSetDestroy(TIFFHashSet *set) +{ + if (set) + { + TIFFHashSetClearInternal(set, true); + free(set->tabList); + TIFFListDestroy(set->psRecyclingList); + free(set); + } +} + +#ifdef notused +/************************************************************************/ +/* TIFFHashSetClear() */ +/************************************************************************/ + +/** + * Clear all elements from a hash set. + * + * This function also frees the elements if a free function was + * provided at the creation of the hash set. + * + * @param set the hash set + */ + +void TIFFHashSetClear(TIFFHashSet *set) +{ + TIFFHashSetClearInternal(set, false); + set->nIndiceAllocatedSize = 0; + set->nAllocatedSize = 53; +#ifdef HASH_DEBUG + set->nCollisions = 0; +#endif + set->nSize = 0; +} + +/************************************************************************/ +/* TIFFHashSetForeach() */ +/************************************************************************/ + +/** + * Walk through the hash set and runs the provided function on all the + * elements + * + * This function is provided the user_data argument of TIFFHashSetForeach. + * It must return true to go on the walk through the hash set, or FALSE to + * make it stop. + * + * Note : the structure of the hash set must *NOT* be modified during the + * walk. + * + * @param set the hash set. + * @param fnIterFunc the function called on each element. + * @param user_data the user data provided to the function. + */ + +void TIFFHashSetForeach(TIFFHashSet *set, TIFFHashSetIterEltFunc fnIterFunc, + void *user_data) +{ + assert(set != NULL); + if (!fnIterFunc) + return; + + for (int i = 0; i < set->nAllocatedSize; i++) + { + TIFFList *cur = set->tabList[i]; + while (cur) + { + if (!fnIterFunc(cur->pData, user_data)) + return; + + cur = cur->psNext; + } + } +} +#endif + +/************************************************************************/ +/* TIFFHashSetRehash() */ +/************************************************************************/ + +static bool TIFFHashSetRehash(TIFFHashSet *set) +{ + int nNewAllocatedSize = anPrimes[set->nIndiceAllocatedSize]; + TIFFList **newTabList = + (TIFFList **)(calloc(nNewAllocatedSize, sizeof(TIFFList *))); + if (newTabList == NULL) + return false; +#ifdef HASH_DEBUG + TIFFDebug("TIFFHASH", + "hashSet=%p, nSize=%d, nCollisions=%d, " + "fCollisionRate=%.02f", + set, set->nSize, set->nCollisions, + set->nCollisions * 100.0 / set->nSize); + set->nCollisions = 0; +#endif + for (int i = 0; i < set->nAllocatedSize; i++) + { + TIFFList *cur = set->tabList[i]; + while (cur) + { + const unsigned long nNewHashVal = + set->fnHashFunc(cur->pData) % nNewAllocatedSize; +#ifdef HASH_DEBUG + if (newTabList[nNewHashVal]) + set->nCollisions++; +#endif + TIFFList *psNext = cur->psNext; + cur->psNext = newTabList[nNewHashVal]; + newTabList[nNewHashVal] = cur; + cur = psNext; + } + } + free(set->tabList); + set->tabList = newTabList; + set->nAllocatedSize = nNewAllocatedSize; + set->bRehash = false; + return true; +} + +/************************************************************************/ +/* TIFFHashSetFindPtr() */ +/************************************************************************/ + +static void **TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt) +{ + const unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize; + TIFFList *cur = set->tabList[nHashVal]; + while (cur) + { + if (set->fnEqualFunc(cur->pData, elt)) + return &cur->pData; + cur = cur->psNext; + } + return NULL; +} + +/************************************************************************/ +/* TIFFHashSetInsert() */ +/************************************************************************/ + +/** + * Inserts an element into a hash set. + * + * If the element was already inserted in the hash set, the previous + * element is replaced by the new element. If a free function was provided, + * it is used to free the previously inserted element + * + * @param set the hash set + * @param elt the new element to insert in the hash set + * + * @return true if success. If false is returned, elt has not been inserted, + * but TIFFHashSetInsert() will have run the free function if provided. + */ + +bool TIFFHashSetInsert(TIFFHashSet *set, void *elt) +{ + assert(set != NULL); + void **pElt = TIFFHashSetFindPtr(set, elt); + if (pElt) + { + if (set->fnFreeEltFunc) + set->fnFreeEltFunc(*pElt); + + *pElt = elt; + return true; + } + + if (set->nSize >= 2 * set->nAllocatedSize / 3 || + (set->bRehash && set->nIndiceAllocatedSize > 0 && + set->nSize <= set->nAllocatedSize / 2)) + { + set->nIndiceAllocatedSize++; + if (!TIFFHashSetRehash(set)) + { + set->nIndiceAllocatedSize--; + if (set->fnFreeEltFunc) + set->fnFreeEltFunc(elt); + return false; + } + } + + const unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize; +#ifdef HASH_DEBUG + if (set->tabList[nHashVal]) + set->nCollisions++; +#endif + + TIFFList *new_elt = TIFFHashSetGetNewListElt(set); + if (new_elt == NULL) + { + if (set->fnFreeEltFunc) + set->fnFreeEltFunc(elt); + return false; + } + new_elt->pData = elt; + new_elt->psNext = set->tabList[nHashVal]; + set->tabList[nHashVal] = new_elt; + set->nSize++; + + return true; +} + +/************************************************************************/ +/* TIFFHashSetLookup() */ +/************************************************************************/ + +/** + * Returns the element found in the hash set corresponding to the element to + * look up The element must not be modified. + * + * @param set the hash set + * @param elt the element to look up in the hash set + * + * @return the element found in the hash set or NULL + */ + +void *TIFFHashSetLookup(TIFFHashSet *set, const void *elt) +{ + assert(set != NULL); + void **pElt = TIFFHashSetFindPtr(set, elt); + if (pElt) + return *pElt; + + return NULL; +} + +/************************************************************************/ +/* TIFFHashSetRemoveInternal() */ +/************************************************************************/ + +static bool TIFFHashSetRemoveInternal(TIFFHashSet *set, const void *elt, + bool bDeferRehash) +{ + assert(set != NULL); + if (set->nIndiceAllocatedSize > 0 && set->nSize <= set->nAllocatedSize / 2) + { + set->nIndiceAllocatedSize--; + if (bDeferRehash) + set->bRehash = true; + else + { + if (!TIFFHashSetRehash(set)) + { + set->nIndiceAllocatedSize++; + return false; + } + } + } + + int nHashVal = (int)(set->fnHashFunc(elt) % set->nAllocatedSize); + TIFFList *cur = set->tabList[nHashVal]; + TIFFList *prev = NULL; + while (cur) + { + if (set->fnEqualFunc(cur->pData, elt)) + { + if (prev) + prev->psNext = cur->psNext; + else + set->tabList[nHashVal] = cur->psNext; + + if (set->fnFreeEltFunc) + set->fnFreeEltFunc(cur->pData); + + TIFFHashSetReturnListElt(set, cur); +#ifdef HASH_DEBUG + if (set->tabList[nHashVal]) + set->nCollisions--; +#endif + set->nSize--; + return true; + } + prev = cur; + cur = cur->psNext; + } + return false; +} + +/************************************************************************/ +/* TIFFHashSetRemove() */ +/************************************************************************/ + +/** + * Removes an element from a hash set + * + * @param set the hash set + * @param elt the new element to remove from the hash set + * + * @return true if the element was in the hash set + */ + +bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt) +{ + return TIFFHashSetRemoveInternal(set, elt, false); +} + +#ifdef notused +/************************************************************************/ +/* TIFFHashSetRemoveDeferRehash() */ +/************************************************************************/ + +/** + * Removes an element from a hash set. + * + * This will defer potential rehashing of the set to later calls to + * TIFFHashSetInsert() or TIFFHashSetRemove(). + * + * @param set the hash set + * @param elt the new element to remove from the hash set + * + * @return true if the element was in the hash set + */ + +bool TIFFHashSetRemoveDeferRehash(TIFFHashSet *set, const void *elt) +{ + return TIFFHashSetRemoveInternal(set, elt, true); +} +#endif diff --git a/cpp/3rd_party/libtiff/tif_hash_set.h b/cpp/3rd_party/libtiff/tif_hash_set.h new file mode 100644 index 0000000000..f60e2c675e --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_hash_set.h @@ -0,0 +1,100 @@ +/********************************************************************** + * $Id$ + * + * Name: tif_hash_set.h + * Project: TIFF - Common Portability Library + * Purpose: Hash set functions. + * Author: Even Rouault, + * + ********************************************************************** + * Copyright (c) 2008-2009, Even Rouault + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + ****************************************************************************/ + +#ifndef TIFF_HASH_SET_H_INCLUDED +#define TIFF_HASH_SET_H_INCLUDED + +#include + +/** + * \file tif_hash_set.h + * + * Hash set implementation. + * + * An hash set is a data structure that holds elements that are unique + * according to a comparison function. Operations on the hash set, such as + * insertion, removal or lookup, are supposed to be fast if an efficient + * "hash" function is provided. + */ + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Types */ + + /** Opaque type for a hash set */ + typedef struct _TIFFHashSet TIFFHashSet; + + /** TIFFHashSetHashFunc */ + typedef unsigned long (*TIFFHashSetHashFunc)(const void *elt); + + /** TIFFHashSetEqualFunc */ + typedef bool (*TIFFHashSetEqualFunc)(const void *elt1, const void *elt2); + + /** TIFFHashSetFreeEltFunc */ + typedef void (*TIFFHashSetFreeEltFunc)(void *elt); + + /* Functions */ + + TIFFHashSet *TIFFHashSetNew(TIFFHashSetHashFunc fnHashFunc, + TIFFHashSetEqualFunc fnEqualFunc, + TIFFHashSetFreeEltFunc fnFreeEltFunc); + + void TIFFHashSetDestroy(TIFFHashSet *set); + + int TIFFHashSetSize(const TIFFHashSet *set); + +#ifdef notused + void TIFFHashSetClear(TIFFHashSet *set); + + /** TIFFHashSetIterEltFunc */ + typedef int (*TIFFHashSetIterEltFunc)(void *elt, void *user_data); + + void TIFFHashSetForeach(TIFFHashSet *set, TIFFHashSetIterEltFunc fnIterFunc, + void *user_data); +#endif + + bool TIFFHashSetInsert(TIFFHashSet *set, void *elt); + + void *TIFFHashSetLookup(TIFFHashSet *set, const void *elt); + + bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt); + +#ifdef notused + bool TIFFHashSetRemoveDeferRehash(TIFFHashSet *set, const void *elt); +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* TIFF_HASH_SET_H_INCLUDED */ diff --git a/cpp/3rd_party/libtiff/tif_jbig.c b/cpp/3rd_party/libtiff/tif_jbig.c new file mode 100644 index 0000000000..bb0de9e6e1 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_jbig.c @@ -0,0 +1,233 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * JBIG Compression Algorithm Support. + * Contributed by Lee Howard + * + */ + +#include "tiffiop.h" + +#ifdef JBIG_SUPPORT +#include "jbig.h" + +static int JBIGSetupDecode(TIFF *tif) +{ + if (TIFFNumberOfStrips(tif) != 1) + { + TIFFErrorExtR(tif, "JBIG", + "Multistrip images not supported in decoder"); + return 0; + } + + return 1; +} + +static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s) +{ + struct jbg_dec_state decoder; + int decodeStatus = 0; + unsigned char *pImage = NULL; + unsigned long decodedSize; + (void)s; + + if (isFillOrder(tif, tif->tif_dir.td_fillorder)) + { + TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc); + } + + jbg_dec_init(&decoder); + +#if defined(HAVE_JBG_NEWLEN) + jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc); + /* + * I do not check the return status of jbg_newlen because even if this + * function fails it does not necessarily mean that decoding the image + * will fail. It is generally only needed for received fax images + * that do not contain the actual length of the image in the BIE + * header. I do not log when an error occurs because that will cause + * problems when converting JBIG encoded TIFF's to + * PostScript. As long as the actual image length is contained in the + * BIE header jbg_dec_in should succeed. + */ +#endif /* HAVE_JBG_NEWLEN */ + + decodeStatus = jbg_dec_in(&decoder, (unsigned char *)tif->tif_rawcp, + (size_t)tif->tif_rawcc, NULL); + if (JBG_EOK != decodeStatus) + { + /* + * XXX: JBG_EN constant was defined in pre-2.0 releases of the + * JBIG-KIT. Since the 2.0 the error reporting functions were + * changed. We will handle both cases here. + */ + TIFFErrorExtR(tif, "JBIG", "Error (%d) decoding: %s", decodeStatus, +#if defined(JBG_EN) + jbg_strerror(decodeStatus, JBG_EN) +#else + jbg_strerror(decodeStatus) +#endif + ); + memset(buffer, 0, (size_t)size); + jbg_dec_free(&decoder); + return 0; + } + + decodedSize = jbg_dec_getsize(&decoder); + if ((tmsize_t)decodedSize < size) + { + memset(buffer + decodedSize, 0, (size_t)(size - decodedSize)); + TIFFWarningExtR(tif, "JBIG", + "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT + " requested", + decodedSize, size); + } + else if ((tmsize_t)decodedSize > size) + { + TIFFErrorExtR(tif, "JBIG", + "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT + " were requested", + decodedSize, size); + jbg_dec_free(&decoder); + return 0; + } + pImage = jbg_dec_getimage(&decoder, 0); + _TIFFmemcpy(buffer, pImage, decodedSize); + jbg_dec_free(&decoder); + + tif->tif_rawcp += tif->tif_rawcc; + tif->tif_rawcc = 0; + + return 1; +} + +static int JBIGSetupEncode(TIFF *tif) +{ + if (TIFFNumberOfStrips(tif) != 1) + { + TIFFErrorExtR(tif, "JBIG", + "Multistrip images not supported in encoder"); + return 0; + } + + return 1; +} + +static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc, + uint16_t s) +{ + (void)s; + while (cc > 0) + { + tmsize_t n = (tmsize_t)cc; + + if (tif->tif_rawcc + n > tif->tif_rawdatasize) + { + n = tif->tif_rawdatasize - tif->tif_rawcc; + } + + assert(n > 0); + _TIFFmemcpy(tif->tif_rawcp, pp, n); + tif->tif_rawcp += n; + tif->tif_rawcc += n; + pp += n; + cc -= (size_t)n; + if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif)) + { + return (-1); + } + } + + return (1); +} + +static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData) +{ + TIFF *tif = (TIFF *)userData; + + if (isFillOrder(tif, tif->tif_dir.td_fillorder)) + { + TIFFReverseBits(buffer, (tmsize_t)len); + } + + JBIGCopyEncodedData(tif, buffer, len, 0); +} + +static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s) +{ + TIFFDirectory *dir = &tif->tif_dir; + struct jbg_enc_state encoder; + + (void)size, (void)s; + + jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer, + JBIGOutputBie, tif); + /* + * jbg_enc_out does the "real" encoding. As data is encoded, + * JBIGOutputBie is called, which writes the data to the directory. + */ + jbg_enc_out(&encoder); + jbg_enc_free(&encoder); + + return 1; +} + +int TIFFInitJBIG(TIFF *tif, int scheme) +{ + (void)scheme; + assert(scheme == COMPRESSION_JBIG); + + /* + * These flags are set so the JBIG Codec can control when to reverse + * bits and when not to and to allow the jbig decoder and bit reverser + * to write to memory when necessary. + */ + tif->tif_flags |= TIFF_NOBITREV; + tif->tif_flags &= ~TIFF_MAPPED; + /* We may have read from a previous IFD and thus set TIFF_BUFFERMMAP and + * cleared TIFF_MYBUFFER. It is necessary to restore them to their initial + * value to be consistent with the state of a non-memory mapped file. + */ + if (tif->tif_flags & TIFF_BUFFERMMAP) + { + tif->tif_rawdata = NULL; + tif->tif_rawdatasize = 0; + tif->tif_flags &= ~TIFF_BUFFERMMAP; + tif->tif_flags |= TIFF_MYBUFFER; + } + + /* Setup the function pointers for encode, decode, and cleanup. */ + tif->tif_setupdecode = JBIGSetupDecode; + tif->tif_decodestrip = JBIGDecode; + + tif->tif_setupencode = JBIGSetupEncode; + tif->tif_encodestrip = JBIGEncode; + + return 1; +} + +#endif /* JBIG_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_jpeg.c b/cpp/3rd_party/libtiff/tif_jpeg.c new file mode 100644 index 0000000000..aba5f99b7c --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_jpeg.c @@ -0,0 +1,2916 @@ +/* + * Copyright (c) 1994-1997 Sam Leffler + * Copyright (c) 1994-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRALEAN + +#include "tiffiop.h" +#include + +#ifdef JPEG_SUPPORT + +/* + * TIFF Library + * + * JPEG Compression support per TIFF Technical Note #2 + * (*not* per the original TIFF 6.0 spec). + * + * This file is simply an interface to the libjpeg library written by + * the Independent JPEG Group. You need release 5 or later of the IJG + * code, which you can find on the Internet at ftp.uu.net:/graphics/jpeg/. + * + * Contributed by Tom Lane . + */ +#include + +/* Settings that are independent of libjpeg ABI. Used when reinitializing the */ +/* JPEGState from libjpegs 8 bit to libjpeg 12 bits, which have potentially */ +/* different ABI */ +typedef struct +{ + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ + TIFFPrintMethod printdir; /* super-class method */ + TIFFStripMethod defsparent; /* super-class method */ + TIFFTileMethod deftparent; /* super-class method */ + + /* pseudo-tag fields */ + void *jpegtables; /* JPEGTables tag value, or NULL */ + uint32_t jpegtables_length; /* number of bytes in same */ + int jpegquality; /* Compression quality level */ + int jpegcolormode; /* Auto RGB<=>YCbCr convert? */ + int jpegtablesmode; /* What to put in JPEGTables */ + + int ycbcrsampling_fetched; + int max_allowed_scan_number; + int has_warned_about_progressive_mode; +} JPEGOtherSettings; + +int TIFFFillStrip(TIFF *tif, uint32_t strip); +int TIFFFillTile(TIFF *tif, uint32_t tile); +int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings, + int scheme, int is_encode); +int TIFFJPEGIsFullStripRequired_12(TIFF *tif); + +#include "jerror.h" +#include "jpeglib.h" + +/* Do optional compile-time version check */ +#if defined(EXPECTED_JPEG_LIB_VERSION) && !defined(LIBJPEG_12_PATH) +#if EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION +#error EXPECTED_JPEG_LIB_VERSION != JPEG_LIB_VERSION +#endif +#endif + +/* + * Do we want to do special processing suitable for when JSAMPLE is a + * 16bit value? + */ + +/* HAVE_JPEGTURBO_DUAL_MODE_8_12 is defined for libjpeg-turbo >= 3.0 which + * adds a dual-mode 8/12 bit API in the same library. + * (note: libjpeg-turbo 2.2 was actually released as 3.0) + */ + +#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) +#define JPEG_DUAL_MODE_8_12 +/* Start by undefining BITS_IN_JSAMPLE which is always set to 8 in libjpeg-turbo + * >= 3.0 Cf + * https://github.com/libjpeg-turbo/libjpeg-turbo/commit/8b9bc4b9635a2a047fb23ebe70c9acd728d3f99b + */ +#undef BITS_IN_JSAMPLE +/* libjpeg-turbo >= 3.0 adds J12xxxx datatypes for the 12-bit mode. */ +#if defined(FROM_TIF_JPEG_12) +#define BITS_IN_JSAMPLE 12 +#define TIFF_JSAMPLE J12SAMPLE +#define TIFF_JSAMPARRAY J12SAMPARRAY +#define TIFF_JSAMPIMAGE J12SAMPIMAGE +#define TIFF_JSAMPROW J12SAMPROW +#else +#define BITS_IN_JSAMPLE 8 +#define TIFF_JSAMPLE JSAMPLE +#define TIFF_JSAMPARRAY JSAMPARRAY +#define TIFF_JSAMPIMAGE JSAMPIMAGE +#define TIFF_JSAMPROW JSAMPROW +#endif +#else +#define TIFF_JSAMPLE JSAMPLE +#define TIFF_JSAMPARRAY JSAMPARRAY +#define TIFF_JSAMPIMAGE JSAMPIMAGE +#define TIFF_JSAMPROW JSAMPROW +#endif + +#if defined(JPEG_LIB_MK1) +#define JPEG_LIB_MK1_OR_12BIT 1 +#elif BITS_IN_JSAMPLE == 12 +#define JPEG_LIB_MK1_OR_12BIT 1 +#endif + +/* + * We are using width_in_blocks which is supposed to be private to + * libjpeg. Unfortunately, the libjpeg delivered with Cygwin has + * renamed this member to width_in_data_units. Since the header has + * also renamed a define, use that unique define name in order to + * detect the problem header and adjust to suit. + */ +#if defined(D_MAX_DATA_UNITS_IN_MCU) +#define width_in_blocks width_in_data_units +#endif + +/* + * On some machines it may be worthwhile to use _setjmp or sigsetjmp + * in place of plain setjmp. These macros will make it easier. + */ +#define SETJMP(jbuf) setjmp(jbuf) +#define LONGJMP(jbuf, code) longjmp(jbuf, code) +#define JMP_BUF jmp_buf + +#ifndef TIFF_jpeg_destination_mgr_defined +#define TIFF_jpeg_destination_mgr_defined +typedef struct jpeg_destination_mgr jpeg_destination_mgr; +#endif + +#ifndef TIFF_jpeg_source_mgr_defined +#define TIFF_jpeg_source_mgr_defined +typedef struct jpeg_source_mgr jpeg_source_mgr; +#endif + +#ifndef TIFF_jpeg_error_mgr_defined +#define TIFF_jpeg_error_mgr_defined +typedef struct jpeg_error_mgr jpeg_error_mgr; +#endif + +/* + * State block for each open TIFF file using + * libjpeg to do JPEG compression/decompression. + * + * libjpeg's visible state is either a jpeg_compress_struct + * or jpeg_decompress_struct depending on which way we + * are going. comm can be used to refer to the fields + * which are common to both. + * + * NB: cinfo is required to be the first member of JPEGState, + * so we can safely cast JPEGState* -> jpeg_xxx_struct* + * and vice versa! + */ +typedef struct +{ + union + { + struct jpeg_compress_struct c; + struct jpeg_decompress_struct d; + struct jpeg_common_struct comm; + } cinfo; /* NB: must be first */ + int cinfo_initialized; + + jpeg_error_mgr err; /* libjpeg error manager */ + JMP_BUF exit_jmpbuf; /* for catching libjpeg failures */ + + struct jpeg_progress_mgr progress; + /* + * The following two members could be a union, but + * they're small enough that it's not worth the effort. + */ + jpeg_destination_mgr dest; /* data dest for compression */ + jpeg_source_mgr src; /* data source for decompression */ + /* private state */ + TIFF *tif; /* back link needed by some code */ + uint16_t photometric; /* copy of PhotometricInterpretation */ + uint16_t h_sampling; /* luminance sampling factors */ + uint16_t v_sampling; + tmsize_t bytesperline; /* decompressed bytes per scanline */ + /* pointers to intermediate buffers when processing downsampled data */ + TIFF_JSAMPARRAY ds_buffer[MAX_COMPONENTS]; + int scancount; /* number of "scanlines" accumulated */ + int samplesperclump; + + JPEGOtherSettings otherSettings; + + int encode_raw_error; +} JPEGState; + +#define JState(tif) ((JPEGState *)(tif)->tif_data) + +static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); +static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); +static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); +static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); +static int JPEGInitializeLibJPEG(TIFF *tif, int decode); +static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); + +#define FIELD_JPEGTABLES (FIELD_CODEC + 0) + +static const TIFFField jpegFields[] = { + {TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, + FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL}, + {TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + TRUE, FALSE, "", NULL}, + {TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + FALSE, FALSE, "", NULL}, + {TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + FALSE, FALSE, "", NULL}}; + +/* + * libjpeg interface layer. + * + * We use setjmp/longjmp to return control to libtiff + * when a fatal error is encountered within the JPEG + * library. We also direct libjpeg error and warning + * messages through the appropriate libtiff handlers. + */ + +/* + * Error handling routines (these replace corresponding + * IJG routines from jerror.c). These are used for both + * compression and decompression. + */ +static void TIFFjpeg_error_exit(j_common_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ + char buffer[JMSG_LENGTH_MAX]; + + (*cinfo->err->format_message)(cinfo, buffer); + TIFFErrorExtR(sp->tif, "JPEGLib", "%s", + buffer); /* display the error message */ + jpeg_abort(cinfo); /* clean up libjpeg state */ + LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ +} + +/* + * This routine is invoked only for warning messages, + * since error_exit does its own thing and trace_level + * is never set > 0. + */ +static void TIFFjpeg_output_message(j_common_ptr cinfo) +{ + char buffer[JMSG_LENGTH_MAX]; + + (*cinfo->err->format_message)(cinfo, buffer); + TIFFWarningExtR(((JPEGState *)cinfo)->tif, "JPEGLib", "%s", buffer); +} + +/* Avoid the risk of denial-of-service on crafted JPEGs with an insane */ +/* number of scans. */ +/* See + * http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf + */ +static void TIFFjpeg_progress_monitor(j_common_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; /* NB: cinfo assumed first */ + if (cinfo->is_decompressor) + { + const int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number; + if (scan_no >= sp->otherSettings.max_allowed_scan_number) + { + TIFFErrorExtR( + ((JPEGState *)cinfo)->tif, "TIFFjpeg_progress_monitor", + "Scan number %d exceeds maximum scans (%d). This limit " + "can be raised through the " + "LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER " + "environment variable.", + scan_no, sp->otherSettings.max_allowed_scan_number); + + jpeg_abort(cinfo); /* clean up libjpeg state */ + LONGJMP(sp->exit_jmpbuf, 1); /* return to libtiff caller */ + } + } +} + +/* + * Interface routines. This layer of routines exists + * primarily to limit side-effects from using setjmp. + * Also, normal/error returns are converted into return + * values per libtiff practice. + */ +#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op)) +#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op), 1)) + +static int TIFFjpeg_create_compress(JPEGState *sp) +{ + /* initialize JPEG error handling */ + sp->cinfo.c.err = jpeg_std_error(&sp->err); + sp->err.error_exit = TIFFjpeg_error_exit; + sp->err.output_message = TIFFjpeg_output_message; + + /* set client_data to avoid UMR warning from tools like Purify */ + sp->cinfo.c.client_data = NULL; + + return CALLVJPEG(sp, jpeg_create_compress(&sp->cinfo.c)); +} + +static int TIFFjpeg_create_decompress(JPEGState *sp) +{ + /* initialize JPEG error handling */ + sp->cinfo.d.err = jpeg_std_error(&sp->err); + sp->err.error_exit = TIFFjpeg_error_exit; + sp->err.output_message = TIFFjpeg_output_message; + + /* set client_data to avoid UMR warning from tools like Purify */ + sp->cinfo.d.client_data = NULL; + + return CALLVJPEG(sp, jpeg_create_decompress(&sp->cinfo.d)); +} + +static int TIFFjpeg_set_defaults(JPEGState *sp) +{ + return CALLVJPEG(sp, jpeg_set_defaults(&sp->cinfo.c)); +} + +static int TIFFjpeg_set_colorspace(JPEGState *sp, J_COLOR_SPACE colorspace) +{ + return CALLVJPEG(sp, jpeg_set_colorspace(&sp->cinfo.c, colorspace)); +} + +static int TIFFjpeg_set_quality(JPEGState *sp, int quality, + boolean force_baseline) +{ + return CALLVJPEG(sp, + jpeg_set_quality(&sp->cinfo.c, quality, force_baseline)); +} + +static int TIFFjpeg_suppress_tables(JPEGState *sp, boolean suppress) +{ + return CALLVJPEG(sp, jpeg_suppress_tables(&sp->cinfo.c, suppress)); +} + +static int TIFFjpeg_start_compress(JPEGState *sp, boolean write_all_tables) +{ + return CALLVJPEG(sp, jpeg_start_compress(&sp->cinfo.c, write_all_tables)); +} + +static int TIFFjpeg_write_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines, + int num_lines) +{ +#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 + return CALLJPEG(sp, -1, + (int)jpeg12_write_scanlines(&sp->cinfo.c, scanlines, + (JDIMENSION)num_lines)); +#else + return CALLJPEG(sp, -1, + (int)jpeg_write_scanlines(&sp->cinfo.c, scanlines, + (JDIMENSION)num_lines)); +#endif +} + +static int TIFFjpeg_write_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data, + int num_lines) +{ +#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 + return CALLJPEG( + sp, -1, + (int)jpeg12_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines)); +#else + return CALLJPEG( + sp, -1, + (int)jpeg_write_raw_data(&sp->cinfo.c, data, (JDIMENSION)num_lines)); +#endif +} + +static int TIFFjpeg_finish_compress(JPEGState *sp) +{ + return CALLVJPEG(sp, jpeg_finish_compress(&sp->cinfo.c)); +} + +static int TIFFjpeg_write_tables(JPEGState *sp) +{ + return CALLVJPEG(sp, jpeg_write_tables(&sp->cinfo.c)); +} + +static int TIFFjpeg_read_header(JPEGState *sp, boolean require_image) +{ + return CALLJPEG(sp, -1, jpeg_read_header(&sp->cinfo.d, require_image)); +} + +static int TIFFjpeg_has_multiple_scans(JPEGState *sp) +{ + return CALLJPEG(sp, 0, jpeg_has_multiple_scans(&sp->cinfo.d)); +} + +static int TIFFjpeg_start_decompress(JPEGState *sp) +{ + const char *sz_max_allowed_scan_number; + /* progress monitor */ + sp->cinfo.d.progress = &sp->progress; + sp->progress.progress_monitor = TIFFjpeg_progress_monitor; + sp->otherSettings.max_allowed_scan_number = 100; + sz_max_allowed_scan_number = getenv("LIBTIFF_JPEG_MAX_ALLOWED_SCAN_NUMBER"); + if (sz_max_allowed_scan_number) + sp->otherSettings.max_allowed_scan_number = + atoi(sz_max_allowed_scan_number); + + return CALLVJPEG(sp, jpeg_start_decompress(&sp->cinfo.d)); +} + +static int TIFFjpeg_read_scanlines(JPEGState *sp, TIFF_JSAMPARRAY scanlines, + int max_lines) +{ +#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 + return CALLJPEG(sp, -1, + (int)jpeg12_read_scanlines(&sp->cinfo.d, scanlines, + (JDIMENSION)max_lines)); +#else + return CALLJPEG(sp, -1, + (int)jpeg_read_scanlines(&sp->cinfo.d, scanlines, + (JDIMENSION)max_lines)); +#endif +} + +static int TIFFjpeg_read_raw_data(JPEGState *sp, TIFF_JSAMPIMAGE data, + int max_lines) +{ +#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) && BITS_IN_JSAMPLE == 12 + return CALLJPEG( + sp, -1, + (int)jpeg12_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines)); +#else + return CALLJPEG( + sp, -1, + (int)jpeg_read_raw_data(&sp->cinfo.d, data, (JDIMENSION)max_lines)); +#endif +} + +static int TIFFjpeg_finish_decompress(JPEGState *sp) +{ + return CALLJPEG(sp, -1, (int)jpeg_finish_decompress(&sp->cinfo.d)); +} + +static int TIFFjpeg_abort(JPEGState *sp) +{ + return CALLVJPEG(sp, jpeg_abort(&sp->cinfo.comm)); +} + +static int TIFFjpeg_destroy(JPEGState *sp) +{ + return CALLVJPEG(sp, jpeg_destroy(&sp->cinfo.comm)); +} + +static JSAMPARRAY TIFFjpeg_alloc_sarray(JPEGState *sp, int pool_id, + JDIMENSION samplesperrow, + JDIMENSION numrows) +{ + return CALLJPEG(sp, (JSAMPARRAY)NULL, + (*sp->cinfo.comm.mem->alloc_sarray)( + &sp->cinfo.comm, pool_id, samplesperrow, numrows)); +} + +/* + * JPEG library destination data manager. + * These routines direct compressed data from libjpeg into the + * libtiff output buffer. + */ + +static void std_init_destination(j_compress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + TIFF *tif = sp->tif; + + sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata; + sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize; +} + +static boolean std_empty_output_buffer(j_compress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + TIFF *tif = sp->tif; + + /* the entire buffer has been filled */ + tif->tif_rawcc = tif->tif_rawdatasize; + +#ifdef IPPJ_HUFF + /* + * The Intel IPP performance library does not necessarily fill up + * the whole output buffer on each pass, so only dump out the parts + * that have been filled. + * http://trac.osgeo.org/gdal/wiki/JpegIPP + */ + if (sp->dest.free_in_buffer >= 0) + { + tif->tif_rawcc = tif->tif_rawdatasize - sp->dest.free_in_buffer; + } +#endif + + if (!TIFFFlushData1(tif)) + return FALSE; + sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata; + sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize; + + return (TRUE); +} + +static void std_term_destination(j_compress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + TIFF *tif = sp->tif; + + tif->tif_rawcp = (uint8_t *)sp->dest.next_output_byte; + tif->tif_rawcc = tif->tif_rawdatasize - (tmsize_t)sp->dest.free_in_buffer; + /* NB: libtiff does the final buffer flush */ +} + +static void TIFFjpeg_data_dest(JPEGState *sp, TIFF *tif) +{ + (void)tif; + sp->cinfo.c.dest = &sp->dest; + sp->dest.init_destination = std_init_destination; + sp->dest.empty_output_buffer = std_empty_output_buffer; + sp->dest.term_destination = std_term_destination; +} + +/* + * Alternate destination manager for outputting to JPEGTables field. + */ + +static void tables_init_destination(j_compress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + + /* while building, otherSettings.jpegtables_length is allocated buffer size + */ + sp->dest.next_output_byte = (JOCTET *)sp->otherSettings.jpegtables; + sp->dest.free_in_buffer = (size_t)sp->otherSettings.jpegtables_length; +} + +static boolean tables_empty_output_buffer(j_compress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + void *newbuf; + + /* the entire buffer has been filled; enlarge it by 1000 bytes */ + newbuf = + _TIFFreallocExt(sp->tif, (void *)sp->otherSettings.jpegtables, + (tmsize_t)(sp->otherSettings.jpegtables_length + 1000)); + if (newbuf == NULL) + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100); + sp->dest.next_output_byte = + (JOCTET *)newbuf + sp->otherSettings.jpegtables_length; + sp->dest.free_in_buffer = (size_t)1000; + sp->otherSettings.jpegtables = newbuf; + sp->otherSettings.jpegtables_length += 1000; + return (TRUE); +} + +static void tables_term_destination(j_compress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + + /* set tables length to number of bytes actually emitted */ + sp->otherSettings.jpegtables_length -= (uint32_t)sp->dest.free_in_buffer; +} + +static int TIFFjpeg_tables_dest(JPEGState *sp, TIFF *tif) +{ + (void)tif; + /* + * Allocate a working buffer for building tables. + * Initial size is 1000 bytes, which is usually adequate. + */ + if (sp->otherSettings.jpegtables) + _TIFFfreeExt(tif, sp->otherSettings.jpegtables); + sp->otherSettings.jpegtables_length = 1000; + sp->otherSettings.jpegtables = (void *)_TIFFmallocExt( + tif, (tmsize_t)sp->otherSettings.jpegtables_length); + if (sp->otherSettings.jpegtables == NULL) + { + sp->otherSettings.jpegtables_length = 0; + TIFFErrorExtR(sp->tif, "TIFFjpeg_tables_dest", + "No space for JPEGTables"); + return (0); + } + sp->cinfo.c.dest = &sp->dest; + sp->dest.init_destination = tables_init_destination; + sp->dest.empty_output_buffer = tables_empty_output_buffer; + sp->dest.term_destination = tables_term_destination; + return (1); +} + +/* + * JPEG library source data manager. + * These routines supply compressed data to libjpeg. + */ + +static void std_init_source(j_decompress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + TIFF *tif = sp->tif; + + sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata; + sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; +} + +static boolean std_fill_input_buffer(j_decompress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + static const JOCTET dummy_EOI[2] = {0xFF, JPEG_EOI}; + +#ifdef IPPJ_HUFF + /* + * The Intel IPP performance library does not necessarily read the whole + * input buffer in one pass, so it is possible to get here with data + * yet to read. + * + * We just return without doing anything, until the entire buffer has + * been read. + * http://trac.osgeo.org/gdal/wiki/JpegIPP + */ + if (sp->src.bytes_in_buffer > 0) + { + return (TRUE); + } +#endif + + /* + * Normally the whole strip/tile is read and so we don't need to do + * a fill. In the case of CHUNKY_STRIP_READ_SUPPORT we might not have + * all the data, but the rawdata is refreshed between scanlines and + * we push this into the io machinery in JPEGDecode(). + * http://trac.osgeo.org/gdal/ticket/3894 + */ + + WARNMS(cinfo, JWRN_JPEG_EOF); + /* insert a fake EOI marker */ + sp->src.next_input_byte = dummy_EOI; + sp->src.bytes_in_buffer = 2; + return (TRUE); +} + +static void std_skip_input_data(j_decompress_ptr cinfo, long num_bytes) +{ + JPEGState *sp = (JPEGState *)cinfo; + + if (num_bytes > 0) + { + if ((size_t)num_bytes > sp->src.bytes_in_buffer) + { + /* oops, buffer overrun */ + (void)std_fill_input_buffer(cinfo); + } + else + { + sp->src.next_input_byte += (size_t)num_bytes; + sp->src.bytes_in_buffer -= (size_t)num_bytes; + } + } +} + +static void std_term_source(j_decompress_ptr cinfo) +{ + /* No work necessary here */ + (void)cinfo; +} + +static void TIFFjpeg_data_src(JPEGState *sp) +{ + sp->cinfo.d.src = &sp->src; + sp->src.init_source = std_init_source; + sp->src.fill_input_buffer = std_fill_input_buffer; + sp->src.skip_input_data = std_skip_input_data; + sp->src.resync_to_restart = jpeg_resync_to_restart; + sp->src.term_source = std_term_source; + sp->src.bytes_in_buffer = 0; /* for safety */ + sp->src.next_input_byte = NULL; +} + +/* + * Alternate source manager for reading from JPEGTables. + * We can share all the code except for the init routine. + */ + +static void tables_init_source(j_decompress_ptr cinfo) +{ + JPEGState *sp = (JPEGState *)cinfo; + + sp->src.next_input_byte = (const JOCTET *)sp->otherSettings.jpegtables; + sp->src.bytes_in_buffer = (size_t)sp->otherSettings.jpegtables_length; +} + +static void TIFFjpeg_tables_src(JPEGState *sp) +{ + TIFFjpeg_data_src(sp); + sp->src.init_source = tables_init_source; +} + +/* + * Allocate downsampled-data buffers needed for downsampled I/O. + * We use values computed in jpeg_start_compress or jpeg_start_decompress. + * We use libjpeg's allocator so that buffers will be released automatically + * when done with strip/tile. + * This is also a handy place to compute samplesperclump, bytesperline. + */ +static int alloc_downsampled_buffers(TIFF *tif, jpeg_component_info *comp_info, + int num_components) +{ + JPEGState *sp = JState(tif); + int ci; + jpeg_component_info *compptr; + TIFF_JSAMPARRAY buf; + int samples_per_clump = 0; + + for (ci = 0, compptr = comp_info; ci < num_components; ci++, compptr++) + { + samples_per_clump += compptr->h_samp_factor * compptr->v_samp_factor; + buf = (TIFF_JSAMPARRAY)TIFFjpeg_alloc_sarray( + sp, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE, + (JDIMENSION)(compptr->v_samp_factor * DCTSIZE)); + if (buf == NULL) + return (0); + sp->ds_buffer[ci] = buf; + } + sp->samplesperclump = samples_per_clump; + return (1); +} + +/* + * JPEG Decoding. + */ + +#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING + +#define JPEG_MARKER_SOF0 0xC0 +#define JPEG_MARKER_SOF1 0xC1 +#define JPEG_MARKER_SOF2 0xC2 +#define JPEG_MARKER_SOF9 0xC9 +#define JPEG_MARKER_SOF10 0xCA +#define JPEG_MARKER_DHT 0xC4 +#define JPEG_MARKER_SOI 0xD8 +#define JPEG_MARKER_SOS 0xDA +#define JPEG_MARKER_DQT 0xDB +#define JPEG_MARKER_DRI 0xDD +#define JPEG_MARKER_APP0 0xE0 +#define JPEG_MARKER_COM 0xFE +struct JPEGFixupTagsSubsamplingData +{ + TIFF *tif; + void *buffer; + uint32_t buffersize; + uint8_t *buffercurrentbyte; + uint32_t bufferbytesleft; + uint64_t fileoffset; + uint64_t filebytesleft; + uint8_t filepositioned; +}; +static void JPEGFixupTagsSubsampling(TIFF *tif); +static int +JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data); +static int +JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data, + uint8_t *result); +static int +JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data, + uint16_t *result); +static void +JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data, + uint16_t skiplength); + +#endif + +static int JPEGFixupTags(TIFF *tif) +{ +#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING + JPEGState *sp = JState(tif); + if ((tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) && + (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) && + (tif->tif_dir.td_samplesperpixel == 3) && + !sp->otherSettings.ycbcrsampling_fetched) + JPEGFixupTagsSubsampling(tif); +#endif + + return (1); +} + +#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING + +static void JPEGFixupTagsSubsampling(TIFF *tif) +{ + /* + * Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in + * the TIFF tags, but still use non-default (2,2) values within the jpeg + * data stream itself. In order for TIFF applications to work properly + * - for instance to get the strip buffer size right - it is imperative + * that the subsampling be available before we start reading the image + * data normally. This function will attempt to analyze the first strip in + * order to get the sampling values from the jpeg data stream. + * + * Note that JPEGPreDeocode() will produce a fairly loud warning when the + * discovered sampling does not match the default sampling (2,2) or whatever + * was actually in the tiff tags. + * + * See the bug in bugzilla for details: + * + * http://bugzilla.remotesensing.org/show_bug.cgi?id=168 + * + * Frank Warmerdam, July 2002 + * Joris Van Damme, May 2007 + */ + static const char module[] = "JPEGFixupTagsSubsampling"; + struct JPEGFixupTagsSubsamplingData m; + uint64_t fileoffset = TIFFGetStrileOffset(tif, 0); + + if (fileoffset == 0) + { + /* Do not even try to check if the first strip/tile does not + yet exist, as occurs when GDAL has created a new NULL file + for instance. */ + return; + } + + m.tif = tif; + m.buffersize = 2048; + m.buffer = _TIFFmallocExt(tif, m.buffersize); + if (m.buffer == NULL) + { + TIFFWarningExtR(tif, module, + "Unable to allocate memory for auto-correcting of " + "subsampling values; auto-correcting skipped"); + return; + } + m.buffercurrentbyte = NULL; + m.bufferbytesleft = 0; + m.fileoffset = fileoffset; + m.filepositioned = 0; + m.filebytesleft = TIFFGetStrileByteCount(tif, 0); + if (!JPEGFixupTagsSubsamplingSec(&m)) + TIFFWarningExtR( + tif, module, + "Unable to auto-correct subsampling values, likely corrupt JPEG " + "compressed data in first strip/tile; auto-correcting skipped"); + _TIFFfreeExt(tif, m.buffer); +} + +static int +JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData *data) +{ + static const char module[] = "JPEGFixupTagsSubsamplingSec"; + uint8_t m; + while (1) + { + while (1) + { + if (!JPEGFixupTagsSubsamplingReadByte(data, &m)) + return (0); + if (m == 255) + break; + } + while (1) + { + if (!JPEGFixupTagsSubsamplingReadByte(data, &m)) + return (0); + if (m != 255) + break; + } + switch (m) + { + case JPEG_MARKER_SOI: + /* this type of marker has no data and should be skipped */ + break; + case JPEG_MARKER_COM: + case JPEG_MARKER_APP0: + case JPEG_MARKER_APP0 + 1: + case JPEG_MARKER_APP0 + 2: + case JPEG_MARKER_APP0 + 3: + case JPEG_MARKER_APP0 + 4: + case JPEG_MARKER_APP0 + 5: + case JPEG_MARKER_APP0 + 6: + case JPEG_MARKER_APP0 + 7: + case JPEG_MARKER_APP0 + 8: + case JPEG_MARKER_APP0 + 9: + case JPEG_MARKER_APP0 + 10: + case JPEG_MARKER_APP0 + 11: + case JPEG_MARKER_APP0 + 12: + case JPEG_MARKER_APP0 + 13: + case JPEG_MARKER_APP0 + 14: + case JPEG_MARKER_APP0 + 15: + case JPEG_MARKER_DQT: + case JPEG_MARKER_SOS: + case JPEG_MARKER_DHT: + case JPEG_MARKER_DRI: + /* this type of marker has data, but it has no use to us and + * should be skipped */ + { + uint16_t n; + if (!JPEGFixupTagsSubsamplingReadWord(data, &n)) + return (0); + if (n < 2) + return (0); + n -= 2; + if (n > 0) + JPEGFixupTagsSubsamplingSkip(data, n); + } + break; + case JPEG_MARKER_SOF0: /* Baseline sequential Huffman */ + case JPEG_MARKER_SOF1: /* Extended sequential Huffman */ + case JPEG_MARKER_SOF2: /* Progressive Huffman: normally not allowed + by TechNote, but that doesn't hurt + supporting it */ + case JPEG_MARKER_SOF9: /* Extended sequential arithmetic */ + case JPEG_MARKER_SOF10: /* Progressive arithmetic: normally not + allowed by TechNote, but that doesn't + hurt supporting it */ + /* this marker contains the subsampling factors we're scanning + * for */ + { + uint16_t n; + uint16_t o; + uint8_t p; + uint8_t ph, pv; + if (!JPEGFixupTagsSubsamplingReadWord(data, &n)) + return (0); + if (n != 8 + data->tif->tif_dir.td_samplesperpixel * 3) + return (0); + JPEGFixupTagsSubsamplingSkip(data, 7); + if (!JPEGFixupTagsSubsamplingReadByte(data, &p)) + return (0); + ph = (p >> 4); + pv = (p & 15); + JPEGFixupTagsSubsamplingSkip(data, 1); + for (o = 1; o < data->tif->tif_dir.td_samplesperpixel; o++) + { + JPEGFixupTagsSubsamplingSkip(data, 1); + if (!JPEGFixupTagsSubsamplingReadByte(data, &p)) + return (0); + if (p != 0x11) + { + TIFFWarningExtR(data->tif, module, + "Subsampling values inside JPEG " + "compressed data " + "have no TIFF equivalent, " + "auto-correction of TIFF " + "subsampling values failed"); + return (1); + } + JPEGFixupTagsSubsamplingSkip(data, 1); + } + if (((ph != 1) && (ph != 2) && (ph != 4)) || + ((pv != 1) && (pv != 2) && (pv != 4))) + { + TIFFWarningExtR(data->tif, module, + "Subsampling values inside JPEG " + "compressed data have no TIFF " + "equivalent, auto-correction of TIFF " + "subsampling values failed"); + return (1); + } + if ((ph != data->tif->tif_dir.td_ycbcrsubsampling[0]) || + (pv != data->tif->tif_dir.td_ycbcrsubsampling[1])) + { + TIFFWarningExtR( + data->tif, module, + "Auto-corrected former TIFF subsampling values " + "[%" PRIu16 ",%" PRIu16 + "] to match subsampling values inside JPEG " + "compressed data [%" PRIu8 ",%" PRIu8 "]", + data->tif->tif_dir.td_ycbcrsubsampling[0], + data->tif->tif_dir.td_ycbcrsubsampling[1], ph, pv); + data->tif->tif_dir.td_ycbcrsubsampling[0] = ph; + data->tif->tif_dir.td_ycbcrsubsampling[1] = pv; + } + } + return (1); + default: + return (0); + } + } +} + +static int +JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData *data, + uint8_t *result) +{ + if (data->bufferbytesleft == 0) + { + uint32_t m; + if (data->filebytesleft == 0) + return (0); + if (!data->filepositioned) + { + if (TIFFSeekFile(data->tif, data->fileoffset, SEEK_SET) == + (toff_t)-1) + { + return 0; + } + data->filepositioned = 1; + } + m = data->buffersize; + if ((uint64_t)m > data->filebytesleft) + m = (uint32_t)data->filebytesleft; + assert(m < 0x80000000UL); + if (TIFFReadFile(data->tif, data->buffer, (tmsize_t)m) != (tmsize_t)m) + return (0); + data->buffercurrentbyte = data->buffer; + data->bufferbytesleft = m; + data->fileoffset += m; + data->filebytesleft -= m; + } + *result = *data->buffercurrentbyte; + data->buffercurrentbyte++; + data->bufferbytesleft--; + return (1); +} + +static int +JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData *data, + uint16_t *result) +{ + uint8_t ma; + uint8_t mb; + if (!JPEGFixupTagsSubsamplingReadByte(data, &ma)) + return (0); + if (!JPEGFixupTagsSubsamplingReadByte(data, &mb)) + return (0); + *result = (ma << 8) | mb; + return (1); +} + +static void +JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData *data, + uint16_t skiplength) +{ + if ((uint32_t)skiplength <= data->bufferbytesleft) + { + data->buffercurrentbyte += skiplength; + data->bufferbytesleft -= skiplength; + } + else + { + uint16_t m; + m = (uint16_t)(skiplength - data->bufferbytesleft); + if (m <= data->filebytesleft) + { + data->bufferbytesleft = 0; + data->fileoffset += m; + data->filebytesleft -= m; + data->filepositioned = 0; + } + else + { + data->bufferbytesleft = 0; + data->filebytesleft = 0; + } + } +} + +#endif + +static int JPEGSetupDecode(TIFF *tif) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + +#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) + if (tif->tif_dir.td_bitspersample == 12) + { + /* We pass a pointer to a copy of otherSettings, since */ + /* TIFFReInitJPEG_12() will clear sp */ + JPEGOtherSettings savedOtherSettings = sp->otherSettings; + return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 0); + } +#endif + + JPEGInitializeLibJPEG(tif, TRUE); + + assert(sp != NULL); + assert(sp->cinfo.comm.is_decompressor); + + /* Read JPEGTables if it is present */ + if (TIFFFieldSet(tif, FIELD_JPEGTABLES)) + { + TIFFjpeg_tables_src(sp); + if (TIFFjpeg_read_header(sp, FALSE) != JPEG_HEADER_TABLES_ONLY) + { + TIFFErrorExtR(tif, "JPEGSetupDecode", "Bogus JPEGTables field"); + return (0); + } + } + + /* Grab parameters that are same for all strips/tiles */ + sp->photometric = td->td_photometric; + switch (sp->photometric) + { + case PHOTOMETRIC_YCBCR: + sp->h_sampling = td->td_ycbcrsubsampling[0]; + sp->v_sampling = td->td_ycbcrsubsampling[1]; + break; + default: + /* TIFF 6.0 forbids subsampling of all other color spaces */ + sp->h_sampling = 1; + sp->v_sampling = 1; + break; + } + + /* Set up for reading normal data */ + TIFFjpeg_data_src(sp); + tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */ + return (1); +} + +/* Returns 1 if the full strip should be read, even when doing scanline per */ +/* scanline decoding. This happens when the JPEG stream uses multiple scans. */ +/* Currently only called in CHUNKY_STRIP_READ_SUPPORT mode through */ +/* scanline interface. */ +/* Only reads tif->tif_dir.td_bitspersample, tif->tif_rawdata and */ +/* tif->tif_rawcc members. */ +/* Can be called independently of the usual setup/predecode/decode states */ +int TIFFJPEGIsFullStripRequired(TIFF *tif) +{ + int ret; + JPEGState state; + +#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) + if (tif->tif_dir.td_bitspersample == 12) + return TIFFJPEGIsFullStripRequired_12(tif); +#endif + + memset(&state, 0, sizeof(JPEGState)); + state.tif = tif; + + TIFFjpeg_create_decompress(&state); + + TIFFjpeg_data_src(&state); + + if (TIFFjpeg_read_header(&state, TRUE) != JPEG_HEADER_OK) + { + TIFFjpeg_destroy(&state); + return (0); + } + ret = TIFFjpeg_has_multiple_scans(&state); + + TIFFjpeg_destroy(&state); + + return ret; +} + +/* + * Set up for decoding a strip or tile. + */ +/*ARGSUSED*/ static int JPEGPreDecode(TIFF *tif, uint16_t s) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + static const char module[] = "JPEGPreDecode"; + uint32_t segment_width, segment_height; + int downsampled_output; + int ci; + + assert(sp != NULL); + + if (sp->cinfo.comm.is_decompressor == 0) + { + tif->tif_setupdecode(tif); + } + + assert(sp->cinfo.comm.is_decompressor); + /* + * Reset decoder state from any previous strip/tile, + * in case application didn't read the whole strip. + */ + if (!TIFFjpeg_abort(sp)) + return (0); + /* + * Read the header for this strip/tile. + */ + + if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK) + return (0); + + tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; + tif->tif_rawcc = sp->src.bytes_in_buffer; + + /* + * Check image parameters and set decompression parameters. + */ + if (isTiled(tif)) + { + segment_width = td->td_tilewidth; + segment_height = td->td_tilelength; + sp->bytesperline = TIFFTileRowSize(tif); + } + else + { + segment_width = td->td_imagewidth; + segment_height = td->td_imagelength - tif->tif_row; + if (segment_height > td->td_rowsperstrip) + segment_height = td->td_rowsperstrip; + sp->bytesperline = TIFFScanlineSize(tif); + } + if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) + { + /* + * For PC 2, scale down the expected strip/tile size + * to match a downsampled component + */ + if (sp->h_sampling == 0 || sp->v_sampling == 0) + { + TIFFErrorExtR(tif, module, + "JPEG horizontal or vertical sampling is zero"); + return (0); + } + segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); + segment_height = TIFFhowmany_32(segment_height, sp->v_sampling); + } + if (sp->cinfo.d.image_width < segment_width || + sp->cinfo.d.image_height < segment_height) + { + TIFFWarningExtR(tif, module, + "Improper JPEG strip/tile size, " + "expected %" PRIu32 "x%" PRIu32 ", got %ux%u", + segment_width, segment_height, sp->cinfo.d.image_width, + sp->cinfo.d.image_height); + } + if (sp->cinfo.d.image_width == segment_width && + sp->cinfo.d.image_height > segment_height && + tif->tif_row + segment_height == td->td_imagelength && !isTiled(tif)) + { + /* Some files have a last strip, that should be truncated, */ + /* but their JPEG codestream has still the maximum strip */ + /* height. Warn about this as this is non compliant, but */ + /* we can safely recover from that. */ + TIFFWarningExtR(tif, module, + "JPEG strip size exceeds expected dimensions," + " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", + segment_width, segment_height, sp->cinfo.d.image_width, + sp->cinfo.d.image_height); + } + else if (sp->cinfo.d.image_width > segment_width || + sp->cinfo.d.image_height > segment_height) + { + /* + * This case could be dangerous, if the strip or tile size has + * been reported as less than the amount of data jpeg will + * return, some potential security issues arise. Catch this + * case and error out. + */ + TIFFErrorExtR(tif, module, + "JPEG strip/tile size exceeds expected dimensions," + " expected %" PRIu32 "x%" PRIu32 ", got %ux%u", + segment_width, segment_height, sp->cinfo.d.image_width, + sp->cinfo.d.image_height); + return (0); + } + if (sp->cinfo.d.num_components != + (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel + : 1)) + { + TIFFErrorExtR(tif, module, "Improper JPEG component count"); + return (0); + } +#ifdef JPEG_LIB_MK1 + if (12 != td->td_bitspersample && 8 != td->td_bitspersample) + { + TIFFErrorExtR(tif, module, "Improper JPEG data precision"); + return (0); + } + sp->cinfo.d.data_precision = td->td_bitspersample; + sp->cinfo.d.bits_in_jsample = td->td_bitspersample; +#else + if (sp->cinfo.d.data_precision != td->td_bitspersample) + { + TIFFErrorExtR(tif, module, "Improper JPEG data precision"); + return (0); + } +#endif + + if (sp->cinfo.d.progressive_mode && + !sp->otherSettings.has_warned_about_progressive_mode) + { + TIFFWarningExtR(tif, module, + "The JPEG strip/tile is encoded with progressive mode, " + "which is normally not legal for JPEG-in-TIFF.\n" + "libtiff should be able to decode it, but it might " + "cause compatibility issues with other readers"); + sp->otherSettings.has_warned_about_progressive_mode = TRUE; + } + + /* In some cases, libjpeg needs to allocate a lot of memory */ + /* http://www.libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf + */ + if (TIFFjpeg_has_multiple_scans(sp)) + { + /* In this case libjpeg will need to allocate memory or backing */ + /* store for all coefficients */ + /* See call to jinit_d_coef_controller() from master_selection() */ + /* in libjpeg */ + + /* 1 MB for regular libjpeg usage */ + toff_t nRequiredMemory = 1024 * 1024; + + for (ci = 0; ci < sp->cinfo.d.num_components; ci++) + { + const jpeg_component_info *compptr = &(sp->cinfo.d.comp_info[ci]); + if (compptr->h_samp_factor > 0 && compptr->v_samp_factor > 0) + { + nRequiredMemory += + (toff_t)(((compptr->width_in_blocks + + compptr->h_samp_factor - 1) / + compptr->h_samp_factor)) * + ((compptr->height_in_blocks + compptr->v_samp_factor - 1) / + compptr->v_samp_factor) * + sizeof(JBLOCK); + } + } + + if (sp->cinfo.d.mem->max_memory_to_use > 0 && + nRequiredMemory > (toff_t)(sp->cinfo.d.mem->max_memory_to_use) && + getenv("LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC") == NULL) + { + TIFFErrorExtR( + tif, module, + "Reading this image would require libjpeg to allocate " + "at least %" PRIu64 " bytes. " + "This is disabled since above the %ld threshold. " + "You may override this restriction by defining the " + "LIBTIFF_ALLOW_LARGE_LIBJPEG_MEM_ALLOC environment variable, " + "or setting the JPEGMEM environment variable to a value " + "greater " + "or equal to '%" PRIu64 "M'", + nRequiredMemory, sp->cinfo.d.mem->max_memory_to_use, + (nRequiredMemory + 1000000u - 1u) / 1000000u); + return 0; + } + } + + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + /* Component 0 should have expected sampling factors */ + if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling || + sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) + { + TIFFErrorExtR(tif, module, + "Improper JPEG sampling factors %d,%d\n" + "Apparently should be %" PRIu16 ",%" PRIu16 ".", + sp->cinfo.d.comp_info[0].h_samp_factor, + sp->cinfo.d.comp_info[0].v_samp_factor, + sp->h_sampling, sp->v_sampling); + return (0); + } + /* Rest should have sampling factors 1,1 */ + for (ci = 1; ci < sp->cinfo.d.num_components; ci++) + { + if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 || + sp->cinfo.d.comp_info[ci].v_samp_factor != 1) + { + TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); + return (0); + } + } + } + else + { + /* PC 2's single component should have sampling factors 1,1 */ + if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 || + sp->cinfo.d.comp_info[0].v_samp_factor != 1) + { + TIFFErrorExtR(tif, module, "Improper JPEG sampling factors"); + return (0); + } + } + downsampled_output = FALSE; + if (td->td_planarconfig == PLANARCONFIG_CONTIG && + sp->photometric == PHOTOMETRIC_YCBCR && + sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) + { + /* Convert YCbCr to RGB */ + sp->cinfo.d.jpeg_color_space = JCS_YCbCr; + sp->cinfo.d.out_color_space = JCS_RGB; + } + else + { + /* Suppress colorspace handling */ + sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN; + sp->cinfo.d.out_color_space = JCS_UNKNOWN; + if (td->td_planarconfig == PLANARCONFIG_CONTIG && + (sp->h_sampling != 1 || sp->v_sampling != 1)) + downsampled_output = TRUE; + /* XXX what about up-sampling? */ + } + if (downsampled_output) + { + /* Need to use raw-data interface to libjpeg */ + sp->cinfo.d.raw_data_out = TRUE; +#if JPEG_LIB_VERSION >= 70 + sp->cinfo.d.do_fancy_upsampling = FALSE; +#endif /* JPEG_LIB_VERSION >= 70 */ + tif->tif_decoderow = DecodeRowError; + tif->tif_decodestrip = JPEGDecodeRaw; + tif->tif_decodetile = JPEGDecodeRaw; + } + else + { + /* Use normal interface to libjpeg */ + sp->cinfo.d.raw_data_out = FALSE; + tif->tif_decoderow = JPEGDecode; + tif->tif_decodestrip = JPEGDecode; + tif->tif_decodetile = JPEGDecode; + } + /* Start JPEG decompressor */ + if (!TIFFjpeg_start_decompress(sp)) + return (0); + /* Allocate downsampled-data buffers if needed */ + if (downsampled_output) + { + if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info, + sp->cinfo.d.num_components)) + return (0); + sp->scancount = DCTSIZE; /* mark buffer empty */ + } + return (1); +} + +/* + * Decode a chunk of pixels. + * "Standard" case: returned data is not downsampled. + */ +#if !JPEG_LIB_MK1_OR_12BIT +static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + JPEGState *sp = JState(tif); + tmsize_t nrows; + (void)s; + + /* + ** Update available information, buffer may have been refilled + ** between decode requests + */ + sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp; + sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; + + if (sp->bytesperline == 0) + { + memset(buf, 0, (size_t)cc); + return 0; + } + + nrows = cc / sp->bytesperline; + if (cc % sp->bytesperline) + TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read"); + + if (nrows > (tmsize_t)sp->cinfo.d.image_height) + nrows = sp->cinfo.d.image_height; + + /* data is expected to be read in multiples of a scanline */ + if (nrows) + { + do + { + /* + * In the libjpeg6b-9a 8bit case. We read directly into + * the TIFF buffer. + */ + JSAMPROW bufptr = (JSAMPROW)buf; + + if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1) + { + memset(buf, 0, (size_t)cc); + return (0); + } + + ++tif->tif_row; + buf += sp->bytesperline; + cc -= sp->bytesperline; + } while (--nrows > 0); + } + + /* Update information on consumed data */ + tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; + tif->tif_rawcc = sp->src.bytes_in_buffer; + + /* Close down the decompressor if we've finished the strip or tile. */ + return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height || + TIFFjpeg_finish_decompress(sp); +} +#endif /* !JPEG_LIB_MK1_OR_12BIT */ + +#if JPEG_LIB_MK1_OR_12BIT +/*ARGSUSED*/ static int JPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, + uint16_t s) +{ + JPEGState *sp = JState(tif); + tmsize_t nrows; + (void)s; + + /* + ** Update available information, buffer may have been refilled + ** between decode requests + */ + sp->src.next_input_byte = (const JOCTET *)tif->tif_rawcp; + sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc; + + if (sp->bytesperline == 0) + { + memset(buf, 0, (size_t)cc); + return 0; + } + + nrows = cc / sp->bytesperline; + if (cc % sp->bytesperline) + TIFFWarningExtR(tif, tif->tif_name, "fractional scanline not read"); + + if (nrows > (tmsize_t)sp->cinfo.d.image_height) + nrows = sp->cinfo.d.image_height; + + /* data is expected to be read in multiples of a scanline */ + if (nrows) + { + TIFF_JSAMPROW line_work_buf = NULL; + + /* + * For 6B, only use temporary buffer for 12 bit imagery. + * For Mk1 always use it. + */ + if (sp->cinfo.d.data_precision == 12) + { + line_work_buf = (TIFF_JSAMPROW)_TIFFmallocExt( + tif, sizeof(short) * sp->cinfo.d.output_width * + sp->cinfo.d.num_components); + } + + do + { + if (line_work_buf != NULL) + { + /* + * In the MK1 case, we always read into a 16bit + * buffer, and then pack down to 12bit or 8bit. + * In 6B case we only read into 16 bit buffer + * for 12bit data, which we need to repack. + */ + if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1) + { + memset(buf, 0, (size_t)cc); + return (0); + } + + if (sp->cinfo.d.data_precision == 12) + { + int value_pairs = (sp->cinfo.d.output_width * + sp->cinfo.d.num_components) / + 2; + int iPair; + + for (iPair = 0; iPair < value_pairs; iPair++) + { + unsigned char *out_ptr = + ((unsigned char *)buf) + iPair * 3; + TIFF_JSAMPLE *in_ptr = line_work_buf + iPair * 2; + + out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4); + out_ptr[1] = + (unsigned char)(((in_ptr[0] & 0xf) << 4) | + ((in_ptr[1] & 0xf00) >> 8)); + out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0)); + } + } + else if (sp->cinfo.d.data_precision == 8) + { + int value_count = + (sp->cinfo.d.output_width * sp->cinfo.d.num_components); + int iValue; + + for (iValue = 0; iValue < value_count; iValue++) + { + ((unsigned char *)buf)[iValue] = + line_work_buf[iValue] & 0xff; + } + } + } + + ++tif->tif_row; + buf += sp->bytesperline; + cc -= sp->bytesperline; + } while (--nrows > 0); + + if (line_work_buf != NULL) + _TIFFfreeExt(tif, line_work_buf); + } + + /* Update information on consumed data */ + tif->tif_rawcp = (uint8_t *)sp->src.next_input_byte; + tif->tif_rawcc = sp->src.bytes_in_buffer; + + /* Close down the decompressor if we've finished the strip or tile. */ + return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height || + TIFFjpeg_finish_decompress(sp); +} +#endif /* JPEG_LIB_MK1_OR_12BIT */ + +/*ARGSUSED*/ static int DecodeRowError(TIFF *tif, uint8_t *buf, tmsize_t cc, + uint16_t s) + +{ + (void)buf; + (void)cc; + (void)s; + + TIFFErrorExtR( + tif, "TIFFReadScanline", + "scanline oriented access is not supported for downsampled JPEG " + "compressed images, consider enabling TIFFTAG_JPEGCOLORMODE as " + "JPEGCOLORMODE_RGB."); + return 0; +} + +/* + * Decode a chunk of pixels. + * Returned data is downsampled per sampling factors. + */ +/*ARGSUSED*/ static int JPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, + uint16_t s) +{ + JPEGState *sp = JState(tif); + tmsize_t nrows; + TIFFDirectory *td = &tif->tif_dir; + (void)s; + + nrows = sp->cinfo.d.image_height; + /* For last strip, limit number of rows to its truncated height */ + /* even if the codestream height is larger (which is not compliant, */ + /* but that we tolerate) */ + if ((uint32_t)nrows > td->td_imagelength - tif->tif_row && !isTiled(tif)) + nrows = td->td_imagelength - tif->tif_row; + +#if defined(JPEG_LIB_MK1_OR_12BIT) + unsigned short *tmpbuf = NULL; +#endif + + /* data is expected to be read in multiples of a scanline */ + if (nrows != 0) + { + + /* Cb,Cr both have sampling factors 1, so this is correct */ + JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width; + int samples_per_clump = sp->samplesperclump; + +#if defined(JPEG_LIB_MK1_OR_12BIT) + tmpbuf = _TIFFmallocExt(tif, sizeof(unsigned short) * + sp->cinfo.d.output_width * + sp->cinfo.d.num_components); + if (tmpbuf == NULL) + { + TIFFErrorExtR(tif, "JPEGDecodeRaw", "Out of memory"); + return 0; + } +#endif + + do + { + jpeg_component_info *compptr; + int ci, clumpoffset; + + if (cc < sp->bytesperline) + { + TIFFErrorExtR( + tif, "JPEGDecodeRaw", + "application buffer not large enough for all data."); + goto error; + } + + /* Reload downsampled-data buffer if needed */ + if (sp->scancount >= DCTSIZE) + { + int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE; + if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n) + goto error; + sp->scancount = 0; + } + /* + * Fastest way to unseparate data is to make one pass + * over the scanline for each row of each component. + */ + clumpoffset = 0; /* first sample in clump */ + for (ci = 0, compptr = sp->cinfo.d.comp_info; + ci < sp->cinfo.d.num_components; ci++, compptr++) + { + int hsamp = compptr->h_samp_factor; + int vsamp = compptr->v_samp_factor; + int ypos; + + for (ypos = 0; ypos < vsamp; ypos++) + { + TIFF_JSAMPLE *inptr = + sp->ds_buffer[ci][sp->scancount * vsamp + ypos]; + JDIMENSION nclump; +#if defined(JPEG_LIB_MK1_OR_12BIT) + TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)tmpbuf + clumpoffset; +#else + TIFF_JSAMPLE *outptr = (TIFF_JSAMPLE *)buf + clumpoffset; + if (cc < (tmsize_t)(clumpoffset + + (tmsize_t)samples_per_clump * + (clumps_per_line - 1) + + hsamp)) + { + TIFFErrorExtR( + tif, "JPEGDecodeRaw", + "application buffer not large enough for all data, " + "possible subsampling issue"); + goto error; + } +#endif + + if (hsamp == 1) + { + /* fast path for at least Cb and Cr */ + for (nclump = clumps_per_line; nclump-- > 0;) + { + outptr[0] = *inptr++; + outptr += samples_per_clump; + } + } + else + { + int xpos; + + /* general case */ + for (nclump = clumps_per_line; nclump-- > 0;) + { + for (xpos = 0; xpos < hsamp; xpos++) + outptr[xpos] = *inptr++; + outptr += samples_per_clump; + } + } + clumpoffset += hsamp; + } + } + +#if defined(JPEG_LIB_MK1_OR_12BIT) + { + if (sp->cinfo.d.data_precision == 8) + { + int i = 0; + int len = + sp->cinfo.d.output_width * sp->cinfo.d.num_components; + for (i = 0; i < len; i++) + { + ((unsigned char *)buf)[i] = tmpbuf[i] & 0xff; + } + } + else + { /* 12-bit */ + int value_pairs = (sp->cinfo.d.output_width * + sp->cinfo.d.num_components) / + 2; + int iPair; + for (iPair = 0; iPair < value_pairs; iPair++) + { + unsigned char *out_ptr = + ((unsigned char *)buf) + iPair * 3; + TIFF_JSAMPLE *in_ptr = + (TIFF_JSAMPLE *)(tmpbuf + iPair * 2); + out_ptr[0] = (unsigned char)((in_ptr[0] & 0xff0) >> 4); + out_ptr[1] = + (unsigned char)(((in_ptr[0] & 0xf) << 4) | + ((in_ptr[1] & 0xf00) >> 8)); + out_ptr[2] = (unsigned char)(((in_ptr[1] & 0xff) >> 0)); + } + } + } +#endif + + sp->scancount++; + tif->tif_row += sp->v_sampling; + + buf += sp->bytesperline; + cc -= sp->bytesperline; + + nrows -= sp->v_sampling; + } while (nrows > 0); + +#if defined(JPEG_LIB_MK1_OR_12BIT) + _TIFFfreeExt(tif, tmpbuf); +#endif + } + + /* Close down the decompressor if done. */ + return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height || + TIFFjpeg_finish_decompress(sp); + +error: +#if defined(JPEG_LIB_MK1_OR_12BIT) + _TIFFfreeExt(tif, tmpbuf); +#endif + return 0; +} + +/* + * JPEG Encoding. + */ + +static void unsuppress_quant_table(JPEGState *sp, int tblno) +{ + JQUANT_TBL *qtbl; + + if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL) + qtbl->sent_table = FALSE; +} + +static void suppress_quant_table(JPEGState *sp, int tblno) +{ + JQUANT_TBL *qtbl; + + if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL) + qtbl->sent_table = TRUE; +} + +static void unsuppress_huff_table(JPEGState *sp, int tblno) +{ + JHUFF_TBL *htbl; + + if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL) + htbl->sent_table = FALSE; + if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL) + htbl->sent_table = FALSE; +} + +static void suppress_huff_table(JPEGState *sp, int tblno) +{ + JHUFF_TBL *htbl; + + if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL) + htbl->sent_table = TRUE; + if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL) + htbl->sent_table = TRUE; +} + +static int prepare_JPEGTables(TIFF *tif) +{ + JPEGState *sp = JState(tif); + + /* Initialize quant tables for current quality setting */ + if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE)) + return (0); + /* Mark only the tables we want for output */ + /* NB: chrominance tables are currently used only with YCbCr */ + if (!TIFFjpeg_suppress_tables(sp, TRUE)) + return (0); + if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT) + { + unsuppress_quant_table(sp, 0); + if (sp->photometric == PHOTOMETRIC_YCBCR) + unsuppress_quant_table(sp, 1); + } + if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) + { + unsuppress_huff_table(sp, 0); + if (sp->photometric == PHOTOMETRIC_YCBCR) + unsuppress_huff_table(sp, 1); + } + /* Direct libjpeg output into otherSettings.jpegtables */ + if (!TIFFjpeg_tables_dest(sp, tif)) + return (0); + /* Emit tables-only datastream */ + if (!TIFFjpeg_write_tables(sp)) + return (0); + + return (1); +} + +#if defined(JPEG_LIB_VERSION_MAJOR) && \ + (JPEG_LIB_VERSION_MAJOR > 9 || \ + (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4)) +/* This is a modified version of std_huff_tables() from jcparam.c + * in libjpeg-9d because it no longer initializes default Huffman + * tables in jpeg_set_defaults(). */ +static void TIFF_std_huff_tables(j_compress_ptr cinfo) +{ + + if (cinfo->dc_huff_tbl_ptrs[0] == NULL) + { + (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 0); + } + if (cinfo->ac_huff_tbl_ptrs[0] == NULL) + { + (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 0); + } + if (cinfo->dc_huff_tbl_ptrs[1] == NULL) + { + (void)jpeg_std_huff_table((j_common_ptr)cinfo, TRUE, 1); + } + if (cinfo->ac_huff_tbl_ptrs[1] == NULL) + { + (void)jpeg_std_huff_table((j_common_ptr)cinfo, FALSE, 1); + } +} +#endif + +static int JPEGSetupEncode(TIFF *tif) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + static const char module[] = "JPEGSetupEncode"; + +#if defined(JPEG_DUAL_MODE_8_12) && !defined(FROM_TIF_JPEG_12) + if (tif->tif_dir.td_bitspersample == 12) + { + /* We pass a pointer to a copy of otherSettings, since */ + /* TIFFReInitJPEG_12() will clear sp */ + JPEGOtherSettings savedOtherSettings = sp->otherSettings; + return TIFFReInitJPEG_12(tif, &savedOtherSettings, COMPRESSION_JPEG, 1); + } +#endif + + JPEGInitializeLibJPEG(tif, FALSE); + + assert(sp != NULL); + assert(!sp->cinfo.comm.is_decompressor); + + sp->photometric = td->td_photometric; + + /* + * Initialize all JPEG parameters to default values. + * Note that jpeg_set_defaults needs legal values for + * in_color_space and input_components. + */ + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + sp->cinfo.c.input_components = td->td_samplesperpixel; + if (sp->photometric == PHOTOMETRIC_YCBCR) + { + if (sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) + { + sp->cinfo.c.in_color_space = JCS_RGB; + } + else + { + sp->cinfo.c.in_color_space = JCS_YCbCr; + } + } + else + { + if ((td->td_photometric == PHOTOMETRIC_MINISWHITE || + td->td_photometric == PHOTOMETRIC_MINISBLACK) && + td->td_samplesperpixel == 1) + sp->cinfo.c.in_color_space = JCS_GRAYSCALE; + else if (td->td_photometric == PHOTOMETRIC_RGB && + td->td_samplesperpixel == 3) + sp->cinfo.c.in_color_space = JCS_RGB; + else if (td->td_photometric == PHOTOMETRIC_SEPARATED && + td->td_samplesperpixel == 4) + sp->cinfo.c.in_color_space = JCS_CMYK; + else + sp->cinfo.c.in_color_space = JCS_UNKNOWN; + } + } + else + { + sp->cinfo.c.input_components = 1; + sp->cinfo.c.in_color_space = JCS_UNKNOWN; + } + if (!TIFFjpeg_set_defaults(sp)) + return (0); + + /* mozjpeg by default enables progressive JPEG, which is illegal in + * JPEG-in-TIFF */ + /* So explicitly disable it. */ + if (sp->cinfo.c.num_scans != 0 && + (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0) + { + /* it has been found that mozjpeg could create corrupt strips/tiles */ + /* in non optimize_coding mode. */ + TIFFWarningExtR( + tif, module, + "mozjpeg library likely detected. Disable emission of " + "Huffman tables in JpegTables tag, and use optimize_coding " + "to avoid potential issues"); + sp->otherSettings.jpegtablesmode &= ~JPEGTABLESMODE_HUFF; + } + sp->cinfo.c.num_scans = 0; + sp->cinfo.c.scan_info = NULL; + + /* Set per-file parameters */ + switch (sp->photometric) + { + case PHOTOMETRIC_YCBCR: + sp->h_sampling = td->td_ycbcrsubsampling[0]; + sp->v_sampling = td->td_ycbcrsubsampling[1]; + if (sp->h_sampling == 0 || sp->v_sampling == 0) + { + TIFFErrorExtR(tif, module, + "Invalig horizontal/vertical sampling value"); + return (0); + } + if (td->td_bitspersample > 16) + { + TIFFErrorExtR(tif, module, + "BitsPerSample %" PRIu16 " not allowed for JPEG", + td->td_bitspersample); + return (0); + } + + /* + * A ReferenceBlackWhite field *must* be present since the + * default value is inappropriate for YCbCr. Fill in the + * proper value if application didn't set it. + */ + { + float *ref; + if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &ref)) + { + float refbw[6]; + long top = 1L << td->td_bitspersample; + refbw[0] = 0; + refbw[1] = (float)(top - 1L); + refbw[2] = (float)(top >> 1); + refbw[3] = refbw[1]; + refbw[4] = refbw[2]; + refbw[5] = refbw[1]; + TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw); + } + } + break; + case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */ + case PHOTOMETRIC_MASK: + TIFFErrorExtR(tif, module, + "PhotometricInterpretation %" PRIu16 + " not allowed for JPEG", + sp->photometric); + return (0); + default: + /* TIFF 6.0 forbids subsampling of all other color spaces */ + sp->h_sampling = 1; + sp->v_sampling = 1; + break; + } + + /* Verify miscellaneous parameters */ + + /* + * This would need work if libtiff ever supports different + * depths for different components, or if libjpeg ever supports + * run-time selection of depth. Neither is imminent. + */ +#ifdef JPEG_LIB_MK1 + /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */ + if (td->td_bitspersample != 8 && td->td_bitspersample != 12) +#else + if (td->td_bitspersample != BITS_IN_JSAMPLE) +#endif + { + TIFFErrorExtR(tif, module, + "BitsPerSample %" PRIu16 " not allowed for JPEG", + td->td_bitspersample); + return (0); + } + sp->cinfo.c.data_precision = td->td_bitspersample; +#ifdef JPEG_LIB_MK1 + sp->cinfo.c.bits_in_jsample = td->td_bitspersample; +#endif + if (isTiled(tif)) + { + if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) + { + TIFFErrorExtR(tif, module, + "JPEG tile height must be multiple of %" PRIu32, + (uint32_t)(sp->v_sampling * DCTSIZE)); + return (0); + } + if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) + { + TIFFErrorExtR(tif, module, + "JPEG tile width must be multiple of %" PRIu32, + (uint32_t)(sp->h_sampling * DCTSIZE)); + return (0); + } + } + else + { + if (td->td_rowsperstrip < td->td_imagelength && + (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) + { + TIFFErrorExtR(tif, module, + "RowsPerStrip must be multiple of %" PRIu32 + " for JPEG", + (uint32_t)(sp->v_sampling * DCTSIZE)); + return (0); + } + } + + /* Create a JPEGTables field if appropriate */ + if (sp->otherSettings.jpegtablesmode & + (JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF)) + { + if (sp->otherSettings.jpegtables == NULL || + memcmp(sp->otherSettings.jpegtables, "\0\0\0\0\0\0\0\0\0", 8) == 0) + { +#if defined(JPEG_LIB_VERSION_MAJOR) && \ + (JPEG_LIB_VERSION_MAJOR > 9 || \ + (JPEG_LIB_VERSION_MAJOR == 9 && JPEG_LIB_VERSION_MINOR >= 4)) + if ((sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) != 0 && + (sp->cinfo.c.dc_huff_tbl_ptrs[0] == NULL || + sp->cinfo.c.dc_huff_tbl_ptrs[1] == NULL || + sp->cinfo.c.ac_huff_tbl_ptrs[0] == NULL || + sp->cinfo.c.ac_huff_tbl_ptrs[1] == NULL)) + { + /* libjpeg-9d no longer initializes default Huffman tables in */ + /* jpeg_set_defaults() */ + TIFF_std_huff_tables(&sp->cinfo.c); + } +#endif + + if (!prepare_JPEGTables(tif)) + return (0); + /* Mark the field present */ + /* Can't use TIFFSetField since BEENWRITING is already set! */ + tif->tif_flags |= TIFF_DIRTYDIRECT; + TIFFSetFieldBit(tif, FIELD_JPEGTABLES); + } + } + else + { + /* We do not support application-supplied JPEGTables, */ + /* so mark the field not present */ + TIFFClrFieldBit(tif, FIELD_JPEGTABLES); + } + + /* Direct libjpeg output to libtiff's output buffer */ + TIFFjpeg_data_dest(sp, tif); + + return (1); +} + +/* + * Set encoding state at the start of a strip or tile. + */ +static int JPEGPreEncode(TIFF *tif, uint16_t s) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + static const char module[] = "JPEGPreEncode"; + uint32_t segment_width, segment_height; + int downsampled_input; + + assert(sp != NULL); + + if (sp->cinfo.comm.is_decompressor == 1) + { + tif->tif_setupencode(tif); + } + + assert(!sp->cinfo.comm.is_decompressor); + /* + * Set encoding parameters for this strip/tile. + */ + if (isTiled(tif)) + { + segment_width = td->td_tilewidth; + segment_height = td->td_tilelength; + sp->bytesperline = TIFFTileRowSize(tif); + } + else + { + segment_width = td->td_imagewidth; + segment_height = td->td_imagelength - tif->tif_row; + if (segment_height > td->td_rowsperstrip) + segment_height = td->td_rowsperstrip; + sp->bytesperline = TIFFScanlineSize(tif); + } + if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) + { + /* for PC 2, scale down the strip/tile size + * to match a downsampled component + */ + if (sp->h_sampling == 0 || sp->v_sampling == 0) + { + TIFFErrorExtR(tif, module, + "JPEG horizontal or vertical sampling is zero"); + return (0); + } + segment_width = TIFFhowmany_32(segment_width, sp->h_sampling); + segment_height = TIFFhowmany_32(segment_height, sp->v_sampling); + } + if (segment_width > (uint32_t)JPEG_MAX_DIMENSION || + segment_height > (uint32_t)JPEG_MAX_DIMENSION) + { + TIFFErrorExtR(tif, module, + "Strip/tile too large for JPEG. Maximum dimension is %d", + (int)JPEG_MAX_DIMENSION); + return (0); + } + sp->cinfo.c.image_width = segment_width; + sp->cinfo.c.image_height = segment_height; + downsampled_input = FALSE; + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + sp->cinfo.c.input_components = td->td_samplesperpixel; + if (sp->photometric == PHOTOMETRIC_YCBCR) + { + if (sp->otherSettings.jpegcolormode != JPEGCOLORMODE_RGB) + { + if (sp->h_sampling != 1 || sp->v_sampling != 1) + downsampled_input = TRUE; + } + if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr)) + return (0); + /* + * Set Y sampling factors; + * we assume jpeg_set_colorspace() set the rest to 1 + */ + sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling; + sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling; + } + else + { + if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space)) + return (0); + /* jpeg_set_colorspace set all sampling factors to 1 */ + } + } + else + { + if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN)) + return (0); + sp->cinfo.c.comp_info[0].component_id = s; + /* jpeg_set_colorspace() set sampling factors to 1 */ + if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) + { + sp->cinfo.c.comp_info[0].quant_tbl_no = 1; + sp->cinfo.c.comp_info[0].dc_tbl_no = 1; + sp->cinfo.c.comp_info[0].ac_tbl_no = 1; + } + } + /* ensure libjpeg won't write any extraneous markers */ + sp->cinfo.c.write_JFIF_header = FALSE; + sp->cinfo.c.write_Adobe_marker = FALSE; + /* set up table handling correctly */ + /* calling TIFFjpeg_set_quality() causes quantization tables to be flagged + */ + /* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT + */ + /* mode, so we must manually suppress them. However TIFFjpeg_set_quality() + */ + /* should really be called when dealing with files with directories with */ + /* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */ + if (!TIFFjpeg_set_quality(sp, sp->otherSettings.jpegquality, FALSE)) + return (0); + if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_QUANT) + { + suppress_quant_table(sp, 0); + suppress_quant_table(sp, 1); + } + else + { + unsuppress_quant_table(sp, 0); + unsuppress_quant_table(sp, 1); + } + if (sp->otherSettings.jpegtablesmode & JPEGTABLESMODE_HUFF) + { + /* Explicit suppression is only needed if we did not go through the */ + /* prepare_JPEGTables() code path, which may be the case if updating */ + /* an existing file */ + suppress_huff_table(sp, 0); + suppress_huff_table(sp, 1); + sp->cinfo.c.optimize_coding = FALSE; + } + else + sp->cinfo.c.optimize_coding = TRUE; + if (downsampled_input) + { + /* Need to use raw-data interface to libjpeg */ + sp->cinfo.c.raw_data_in = TRUE; + tif->tif_encoderow = JPEGEncodeRaw; + tif->tif_encodestrip = JPEGEncodeRaw; + tif->tif_encodetile = JPEGEncodeRaw; + } + else + { + /* Use normal interface to libjpeg */ + sp->cinfo.c.raw_data_in = FALSE; + tif->tif_encoderow = JPEGEncode; + tif->tif_encodestrip = JPEGEncode; + tif->tif_encodetile = JPEGEncode; + } + /* Start JPEG compressor */ + if (!TIFFjpeg_start_compress(sp, FALSE)) + return (0); + /* Allocate downsampled-data buffers if needed */ + if (downsampled_input) + { + if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info, + sp->cinfo.c.num_components)) + return (0); + } + sp->scancount = 0; + sp->encode_raw_error = FALSE; + + return (1); +} + +/* + * Encode a chunk of pixels. + * "Standard" case: incoming data is not downsampled. + */ +static int JPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + JPEGState *sp = JState(tif); + tmsize_t nrows; + TIFF_JSAMPROW bufptr[1]; + short *line16 = NULL; + int line16_count = 0; + + (void)s; + assert(sp != NULL); + /* data is expected to be supplied in multiples of a scanline */ + nrows = cc / sp->bytesperline; + if (cc % sp->bytesperline) + TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded"); + + /* The last strip will be limited to image size */ + if (!isTiled(tif) && tif->tif_row + nrows > tif->tif_dir.td_imagelength) + nrows = tif->tif_dir.td_imagelength - tif->tif_row; + + if (sp->cinfo.c.data_precision == 12) + { + line16_count = (int)((sp->bytesperline * 2) / 3); + line16 = (short *)_TIFFmallocExt(tif, sizeof(short) * line16_count); + if (!line16) + { + TIFFErrorExtR(tif, "JPEGEncode", "Failed to allocate memory"); + + return 0; + } + } + + while (nrows-- > 0) + { + + if (sp->cinfo.c.data_precision == 12) + { + + int value_pairs = line16_count / 2; + int iPair; + + bufptr[0] = (TIFF_JSAMPROW)line16; + + for (iPair = 0; iPair < value_pairs; iPair++) + { + unsigned char *in_ptr = ((unsigned char *)buf) + iPair * 3; + TIFF_JSAMPLE *out_ptr = (TIFF_JSAMPLE *)(line16 + iPair * 2); + + out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4); + out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2]; + } + } + else + { + bufptr[0] = (TIFF_JSAMPROW)buf; + } + if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1) + return (0); + if (nrows > 0) + tif->tif_row++; + buf += sp->bytesperline; + } + + if (sp->cinfo.c.data_precision == 12) + { + _TIFFfreeExt(tif, line16); + } + + return (1); +} + +/* + * Encode a chunk of pixels. + * Incoming data is expected to be downsampled per sampling factors. + */ +static int JPEGEncodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + JPEGState *sp = JState(tif); + TIFF_JSAMPLE *inptr; + TIFF_JSAMPLE *outptr; + tmsize_t nrows; + JDIMENSION clumps_per_line, nclump; + int clumpoffset, ci, xpos, ypos; + jpeg_component_info *compptr; + int samples_per_clump = sp->samplesperclump; + tmsize_t bytesperclumpline; + + (void)s; + assert(sp != NULL); + + if (sp->encode_raw_error) + { + TIFFErrorExtR(tif, tif->tif_name, "JPEGEncodeRaw() already failed"); + return 0; + } + + /* data is expected to be supplied in multiples of a clumpline */ + /* a clumpline is equivalent to v_sampling desubsampled scanlines */ + /* TODO: the following calculation of bytesperclumpline, should substitute + * calculation of sp->bytesperline, except that it is per v_sampling lines + */ + bytesperclumpline = + ((((tmsize_t)sp->cinfo.c.image_width + sp->h_sampling - 1) / + sp->h_sampling) * + ((tmsize_t)sp->h_sampling * sp->v_sampling + 2) * + sp->cinfo.c.data_precision + + 7) / + 8; + + nrows = (cc / bytesperclumpline) * sp->v_sampling; + if (cc % bytesperclumpline) + TIFFWarningExtR(tif, tif->tif_name, "fractional scanline discarded"); + + /* Cb,Cr both have sampling factors 1, so this is correct */ + clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width; + + while (nrows > 0) + { + /* + * Fastest way to separate the data is to make one pass + * over the scanline for each row of each component. + */ + clumpoffset = 0; /* first sample in clump */ + for (ci = 0, compptr = sp->cinfo.c.comp_info; + ci < sp->cinfo.c.num_components; ci++, compptr++) + { + int hsamp = compptr->h_samp_factor; + int vsamp = compptr->v_samp_factor; + int padding = (int)(compptr->width_in_blocks * DCTSIZE - + clumps_per_line * hsamp); + for (ypos = 0; ypos < vsamp; ypos++) + { + inptr = ((TIFF_JSAMPLE *)buf) + clumpoffset; + outptr = sp->ds_buffer[ci][sp->scancount * vsamp + ypos]; + if (hsamp == 1) + { + /* fast path for at least Cb and Cr */ + for (nclump = clumps_per_line; nclump-- > 0;) + { + *outptr++ = inptr[0]; + inptr += samples_per_clump; + } + } + else + { + /* general case */ + for (nclump = clumps_per_line; nclump-- > 0;) + { + for (xpos = 0; xpos < hsamp; xpos++) + *outptr++ = inptr[xpos]; + inptr += samples_per_clump; + } + } + /* pad each scanline as needed */ + for (xpos = 0; xpos < padding; xpos++) + { + *outptr = outptr[-1]; + outptr++; + } + clumpoffset += hsamp; + } + } + sp->scancount++; + if (sp->scancount >= DCTSIZE) + { + int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; + if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) + { + sp->encode_raw_error = TRUE; + return (0); + } + sp->scancount = 0; + } + tif->tif_row += sp->v_sampling; + buf += bytesperclumpline; + nrows -= sp->v_sampling; + } + return (1); +} + +/* + * Finish up at the end of a strip or tile. + */ +static int JPEGPostEncode(TIFF *tif) +{ + JPEGState *sp = JState(tif); + + if (sp->scancount > 0) + { + /* + * Need to emit a partial bufferload of downsampled data. + * Pad the data vertically. + */ + int ci, ypos, n; + jpeg_component_info *compptr; + + for (ci = 0, compptr = sp->cinfo.c.comp_info; + ci < sp->cinfo.c.num_components; ci++, compptr++) + { + int vsamp = compptr->v_samp_factor; + tmsize_t row_width = + compptr->width_in_blocks * DCTSIZE * sizeof(JSAMPLE); + for (ypos = sp->scancount * vsamp; ypos < DCTSIZE * vsamp; ypos++) + { + _TIFFmemcpy((void *)sp->ds_buffer[ci][ypos], + (void *)sp->ds_buffer[ci][ypos - 1], row_width); + } + } + n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; + if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) + return (0); + } + + return (TIFFjpeg_finish_compress(JState(tif))); +} + +static void JPEGCleanup(TIFF *tif) +{ + JPEGState *sp = JState(tif); + + assert(sp != 0); + + tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent; + tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent; + tif->tif_tagmethods.printdir = sp->otherSettings.printdir; + if (sp->cinfo_initialized) + TIFFjpeg_destroy(sp); /* release libjpeg resources */ + if (sp->otherSettings.jpegtables) /* tag value */ + _TIFFfreeExt(tif, sp->otherSettings.jpegtables); + _TIFFfreeExt(tif, tif->tif_data); /* release local state */ + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static void JPEGResetUpsampled(TIFF *tif) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + + /* + * Mark whether returned data is up-sampled or not so TIFFStripSize + * and TIFFTileSize return values that reflect the true amount of + * data. + */ + tif->tif_flags &= ~TIFF_UPSAMPLED; + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + if (td->td_photometric == PHOTOMETRIC_YCBCR && + sp->otherSettings.jpegcolormode == JPEGCOLORMODE_RGB) + { + tif->tif_flags |= TIFF_UPSAMPLED; + } + else + { +#ifdef notdef + if (td->td_ycbcrsubsampling[0] != 1 || + td->td_ycbcrsubsampling[1] != 1) + ; /* XXX what about up-sampling? */ +#endif + } + } + + /* + * Must recalculate cached tile size in case sampling state changed. + * Should we really be doing this now if image size isn't set? + */ + if (tif->tif_tilesize > 0) + tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1); + if (tif->tif_scanlinesize > 0) + tif->tif_scanlinesize = TIFFScanlineSize(tif); +} + +static int JPEGVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + JPEGState *sp = JState(tif); + const TIFFField *fip; + uint32_t v32; + + assert(sp != NULL); + + switch (tag) + { + case TIFFTAG_JPEGTABLES: + v32 = (uint32_t)va_arg(ap, uint32_t); + if (v32 == 0) + { + /* XXX */ + return (0); + } + _TIFFsetByteArrayExt(tif, &sp->otherSettings.jpegtables, + va_arg(ap, void *), v32); + sp->otherSettings.jpegtables_length = v32; + TIFFSetFieldBit(tif, FIELD_JPEGTABLES); + break; + case TIFFTAG_JPEGQUALITY: + sp->otherSettings.jpegquality = (int)va_arg(ap, int); + return (1); /* pseudo tag */ + case TIFFTAG_JPEGCOLORMODE: + sp->otherSettings.jpegcolormode = (int)va_arg(ap, int); + JPEGResetUpsampled(tif); + return (1); /* pseudo tag */ + case TIFFTAG_PHOTOMETRIC: + { + int ret_value = (*sp->otherSettings.vsetparent)(tif, tag, ap); + JPEGResetUpsampled(tif); + return ret_value; + } + case TIFFTAG_JPEGTABLESMODE: + sp->otherSettings.jpegtablesmode = (int)va_arg(ap, int); + return (1); /* pseudo tag */ + case TIFFTAG_YCBCRSUBSAMPLING: + /* mark the fact that we have a real ycbcrsubsampling! */ + sp->otherSettings.ycbcrsampling_fetched = 1; + /* should we be recomputing upsampling info here? */ + return (*sp->otherSettings.vsetparent)(tif, tag, ap); + default: + return (*sp->otherSettings.vsetparent)(tif, tag, ap); + } + + if ((fip = TIFFFieldWithTag(tif, tag)) != NULL) + { + TIFFSetFieldBit(tif, fip->field_bit); + } + else + { + return (0); + } + + tif->tif_flags |= TIFF_DIRTYDIRECT; + return (1); +} + +static int JPEGVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + JPEGState *sp = JState(tif); + + assert(sp != NULL); + + switch (tag) + { + case TIFFTAG_JPEGTABLES: + *va_arg(ap, uint32_t *) = sp->otherSettings.jpegtables_length; + *va_arg(ap, const void **) = sp->otherSettings.jpegtables; + break; + case TIFFTAG_JPEGQUALITY: + *va_arg(ap, int *) = sp->otherSettings.jpegquality; + break; + case TIFFTAG_JPEGCOLORMODE: + *va_arg(ap, int *) = sp->otherSettings.jpegcolormode; + break; + case TIFFTAG_JPEGTABLESMODE: + *va_arg(ap, int *) = sp->otherSettings.jpegtablesmode; + break; + default: + return (*sp->otherSettings.vgetparent)(tif, tag, ap); + } + return (1); +} + +static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags) +{ + JPEGState *sp = JState(tif); + + assert(sp != NULL); + (void)flags; + + if (sp != NULL) + { + if (TIFFFieldSet(tif, FIELD_JPEGTABLES)) + fprintf(fd, " JPEG Tables: (%" PRIu32 " bytes)\n", + sp->otherSettings.jpegtables_length); + if (sp->otherSettings.printdir) + (*sp->otherSettings.printdir)(tif, fd, flags); + } +} + +static uint32_t JPEGDefaultStripSize(TIFF *tif, uint32_t s) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + + s = (*sp->otherSettings.defsparent)(tif, s); + if (s < td->td_imagelength) + s = TIFFroundup_32(s, td->td_ycbcrsubsampling[1] * DCTSIZE); + return (s); +} + +static void JPEGDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th) +{ + JPEGState *sp = JState(tif); + TIFFDirectory *td = &tif->tif_dir; + + (*sp->otherSettings.deftparent)(tif, tw, th); + *tw = TIFFroundup_32(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE); + *th = TIFFroundup_32(*th, td->td_ycbcrsubsampling[1] * DCTSIZE); +} + +/* + * The JPEG library initialized used to be done in TIFFInitJPEG(), but + * now that we allow a TIFF file to be opened in update mode it is necessary + * to have some way of deciding whether compression or decompression is + * desired other than looking at tif->tif_mode. We accomplish this by + * examining {TILE/STRIP}BYTECOUNTS to see if there is a non-zero entry. + * If so, we assume decompression is desired. + * + * This is tricky, because TIFFInitJPEG() is called while the directory is + * being read, and generally speaking the BYTECOUNTS tag won't have been read + * at that point. So we try to defer jpeg library initialization till we + * do have that tag ... basically any access that might require the compressor + * or decompressor that occurs after the reading of the directory. + * + * In an ideal world compressors or decompressors would be setup + * at the point where a single tile or strip was accessed (for read or write) + * so that stuff like update of missing tiles, or replacement of tiles could + * be done. However, we aren't trying to crack that nut just yet ... + * + * NFW, Feb 3rd, 2003. + */ + +static int JPEGInitializeLibJPEG(TIFF *tif, int decompress) +{ + JPEGState *sp = JState(tif); + + if (sp->cinfo_initialized) + { + if (!decompress && sp->cinfo.comm.is_decompressor) + TIFFjpeg_destroy(sp); + else if (decompress && !sp->cinfo.comm.is_decompressor) + TIFFjpeg_destroy(sp); + else + return 1; + + sp->cinfo_initialized = 0; + } + + /* + * Initialize libjpeg. + */ + if (decompress) + { + if (!TIFFjpeg_create_decompress(sp)) + return (0); + } + else + { + if (!TIFFjpeg_create_compress(sp)) + return (0); +#ifndef TIFF_JPEG_MAX_MEMORY_TO_USE +#define TIFF_JPEG_MAX_MEMORY_TO_USE (10 * 1024 * 1024) +#endif + /* libjpeg turbo 1.5.2 honours max_memory_to_use, but has no backing */ + /* store implementation, so better not set max_memory_to_use ourselves. + */ + /* See https://github.com/libjpeg-turbo/libjpeg-turbo/issues/162 */ + if (sp->cinfo.c.mem->max_memory_to_use > 0) + { + /* This is to address bug related in ticket GDAL #1795. */ + if (getenv("JPEGMEM") == NULL) + { + /* Increase the max memory usable. This helps when creating + * files */ + /* with "big" tile, without using libjpeg temporary files. */ + /* For example a 512x512 tile with 3 bands */ + /* requires 1.5 MB which is above libjpeg 1MB default */ + if (sp->cinfo.c.mem->max_memory_to_use < + TIFF_JPEG_MAX_MEMORY_TO_USE) + sp->cinfo.c.mem->max_memory_to_use = + TIFF_JPEG_MAX_MEMORY_TO_USE; + } + } + } + + sp->cinfo_initialized = TRUE; + + return 1; +} + +/* Common to tif_jpeg.c and tif_jpeg_12.c */ +static void TIFFInitJPEGCommon(TIFF *tif) +{ + JPEGState *sp; + + sp = JState(tif); + sp->tif = tif; /* back link */ + + /* Default values for codec-specific fields */ + sp->otherSettings.jpegtables = NULL; + sp->otherSettings.jpegtables_length = 0; + sp->otherSettings.jpegquality = 75; /* Default IJG quality */ + sp->otherSettings.jpegcolormode = JPEGCOLORMODE_RAW; + sp->otherSettings.jpegtablesmode = + JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF; + sp->otherSettings.ycbcrsampling_fetched = 0; + + tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */ + tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */ + tif->tif_tagmethods.printdir = JPEGPrintDir; /* hook for codec tags */ + + /* + * Install codec methods. + */ + tif->tif_fixuptags = JPEGFixupTags; + tif->tif_setupdecode = JPEGSetupDecode; + tif->tif_predecode = JPEGPreDecode; + tif->tif_decoderow = JPEGDecode; + tif->tif_decodestrip = JPEGDecode; + tif->tif_decodetile = JPEGDecode; + tif->tif_setupencode = JPEGSetupEncode; + tif->tif_preencode = JPEGPreEncode; + tif->tif_postencode = JPEGPostEncode; + tif->tif_encoderow = JPEGEncode; + tif->tif_encodestrip = JPEGEncode; + tif->tif_encodetile = JPEGEncode; + tif->tif_cleanup = JPEGCleanup; + + tif->tif_defstripsize = JPEGDefaultStripSize; + tif->tif_deftilesize = JPEGDefaultTileSize; + tif->tif_flags |= TIFF_NOBITREV; /* no bit reversal, please */ + sp->cinfo_initialized = FALSE; +} + +int TIFFInitJPEG(TIFF *tif, int scheme) +{ + JPEGState *sp; + + (void)scheme; + assert(scheme == COMPRESSION_JPEG); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, jpegFields, TIFFArrayCount(jpegFields))) + { + TIFFErrorExtR(tif, "TIFFInitJPEG", + "Merging JPEG codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(JPEGState)); + + if (tif->tif_data == NULL) + { + TIFFErrorExtR(tif, "TIFFInitJPEG", "No space for JPEG state block"); + return 0; + } + _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState)); + + sp = JState(tif); + /* + * Override parent get/set field methods. + */ + sp->otherSettings.vgetparent = tif->tif_tagmethods.vgetfield; + sp->otherSettings.vsetparent = tif->tif_tagmethods.vsetfield; + sp->otherSettings.printdir = tif->tif_tagmethods.printdir; + + sp->otherSettings.defsparent = tif->tif_defstripsize; + sp->otherSettings.deftparent = tif->tif_deftilesize; + + TIFFInitJPEGCommon(tif); + + /* + ** Create a JPEGTables field if no directory has yet been created. + ** We do this just to ensure that sufficient space is reserved for + ** the JPEGTables field. It will be properly created the right + ** size later. + */ + if (tif->tif_diroff == 0) + { +#define SIZE_OF_JPEGTABLES 2000 + /* + The following line assumes incorrectly that all JPEG-in-TIFF files will + have a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags + to be written when the JPEG data is placed with TIFFWriteRawStrip. The + field bit should be set, anyway, later when actual JPEGTABLES header is + generated, so removing it here hopefully is harmless. + TIFFSetFieldBit(tif, FIELD_JPEGTABLES); + */ + sp->otherSettings.jpegtables_length = SIZE_OF_JPEGTABLES; + sp->otherSettings.jpegtables = + (void *)_TIFFmallocExt(tif, sp->otherSettings.jpegtables_length); + if (sp->otherSettings.jpegtables) + { + _TIFFmemset(sp->otherSettings.jpegtables, 0, SIZE_OF_JPEGTABLES); + } + else + { + TIFFErrorExtR(tif, "TIFFInitJPEG", + "Failed to allocate memory for JPEG tables"); + return 0; + } +#undef SIZE_OF_JPEGTABLES + } + return 1; +} +#endif /* JPEG_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_jpeg_12.c b/cpp/3rd_party/libtiff/tif_jpeg_12.c new file mode 100644 index 0000000000..406e1cfc31 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_jpeg_12.c @@ -0,0 +1,63 @@ + +#include "tiffiop.h" + +#if defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) +#define JPEG_DUAL_MODE_8_12 +#endif + +#if defined(JPEG_DUAL_MODE_8_12) + +#define FROM_TIF_JPEG_12 + +#ifdef TIFFInitJPEG +#undef TIFFInitJPEG +#endif +#define TIFFInitJPEG TIFFInitJPEG_12 + +#ifdef TIFFJPEGIsFullStripRequired +#undef TIFFJPEGIsFullStripRequired +#endif +#define TIFFJPEGIsFullStripRequired TIFFJPEGIsFullStripRequired_12 + +int TIFFInitJPEG_12(TIFF *tif, int scheme); + +#if !defined(HAVE_JPEGTURBO_DUAL_MODE_8_12) +#include LIBJPEG_12_PATH +#endif + +#include "tif_jpeg.c" + +int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings, + int scheme, int is_encode) +{ + JPEGState *sp; + uint8_t *new_tif_data; + + (void)scheme; + assert(scheme == COMPRESSION_JPEG); + + new_tif_data = + (uint8_t *)_TIFFreallocExt(tif, tif->tif_data, sizeof(JPEGState)); + + if (new_tif_data == NULL) + { + TIFFErrorExtR(tif, "TIFFReInitJPEG_12", + "No space for JPEG state block"); + return 0; + } + + tif->tif_data = new_tif_data; + _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState)); + + TIFFInitJPEGCommon(tif); + + sp = JState(tif); + sp->otherSettings = *otherSettings; + + if (is_encode) + return JPEGSetupEncode(tif); + else + return JPEGSetupDecode(tif); +} + +#endif /* defined(JPEG_DUAL_MODE_8_12) */ diff --git a/cpp/3rd_party/libtiff/tif_lerc.c b/cpp/3rd_party/libtiff/tif_lerc.c new file mode 100644 index 0000000000..76d1a7b440 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_lerc.c @@ -0,0 +1,1545 @@ +/* + * Copyright (c) 2018, Even Rouault + * Author: + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef LERC_SUPPORT +/* + * TIFF Library. + * + * LERC Compression Support + * + */ + +#include "Lerc_c_api.h" +#include "zlib.h" +#ifdef ZSTD_SUPPORT +#include "zstd.h" +#endif + +#if LIBDEFLATE_SUPPORT +#include "libdeflate.h" +#endif +#define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12 + +#include + +#define LSTATE_INIT_DECODE 0x01 +#define LSTATE_INIT_ENCODE 0x02 + +#ifndef LERC_AT_LEAST_VERSION +#define LERC_AT_LEAST_VERSION(maj, min, patch) 0 +#endif + +/* + * State block for each open TIFF file using LERC compression/decompression. + */ +typedef struct +{ + double maxzerror; /* max z error */ + int lerc_version; + int additional_compression; + int zstd_compress_level; /* zstd */ + int zipquality; /* deflate */ + int state; /* state flags */ + + uint32_t segment_width; + uint32_t segment_height; + + unsigned int uncompressed_size; + unsigned int uncompressed_alloc; + uint8_t *uncompressed_buffer; + unsigned int uncompressed_offset; + + uint8_t *uncompressed_buffer_multiband; + unsigned int uncompressed_buffer_multiband_alloc; + + unsigned int mask_size; + uint8_t *mask_buffer; + + unsigned int compressed_size; + void *compressed_buffer; + +#if LIBDEFLATE_SUPPORT + struct libdeflate_decompressor *libdeflate_dec; + struct libdeflate_compressor *libdeflate_enc; +#endif + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ +} LERCState; + +#define GetLERCState(tif) ((LERCState *)(tif)->tif_data) +#define LERCDecoderState(tif) GetLERCState(tif) +#define LERCEncoderState(tif) GetLERCState(tif) + +static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); + +static int LERCFixupTags(TIFF *tif) +{ + (void)tif; + return 1; +} + +static int LERCSetupDecode(TIFF *tif) +{ + LERCState *sp = LERCDecoderState(tif); + + assert(sp != NULL); + + /* if we were last encoding, terminate this mode */ + if (sp->state & LSTATE_INIT_ENCODE) + { + sp->state = 0; + } + + sp->state |= LSTATE_INIT_DECODE; + return 1; +} + +static int GetLercDataType(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + static const char module[] = "GetLercDataType"; + + if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 8) + { + return 0; + } + + if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 8) + { + return 1; + } + + if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 16) + { + return 2; + } + + if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 16) + { + return 3; + } + + if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 32) + { + return 4; + } + + if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 32) + { + return 5; + } + + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + td->td_bitspersample == 32) + { + return 6; + } + + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + td->td_bitspersample == 64) + { + return 7; + } + + TIFFErrorExtR( + tif, module, + "Unsupported combination of SampleFormat and td_bitspersample"); + return -1; +} + +static int SetupBuffers(TIFF *tif, LERCState *sp, const char *module) +{ + TIFFDirectory *td = &tif->tif_dir; + uint64_t new_size_64; + uint64_t new_alloc_64; + unsigned int new_size; + unsigned int new_alloc; + + sp->uncompressed_offset = 0; + + if (isTiled(tif)) + { + sp->segment_width = td->td_tilewidth; + sp->segment_height = td->td_tilelength; + } + else + { + sp->segment_width = td->td_imagewidth; + sp->segment_height = td->td_imagelength - tif->tif_row; + if (sp->segment_height > td->td_rowsperstrip) + sp->segment_height = td->td_rowsperstrip; + } + + new_size_64 = (uint64_t)sp->segment_width * sp->segment_height * + (td->td_bitspersample / 8); + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + new_size_64 *= td->td_samplesperpixel; + } + + new_size = (unsigned int)new_size_64; + sp->uncompressed_size = new_size; + + /* add some margin as we are going to use it also to store deflate/zstd + * compressed data. We also need extra margin when writing very small + * rasters with one mask per band. */ + new_alloc_64 = 256 + new_size_64 + new_size_64 / 3; +#ifdef ZSTD_SUPPORT + { + size_t zstd_max = ZSTD_compressBound((size_t)new_size_64); + if (new_alloc_64 < zstd_max) + { + new_alloc_64 = zstd_max; + } + } +#endif + new_alloc = (unsigned int)new_alloc_64; + if (new_alloc != new_alloc_64) + { + TIFFErrorExtR(tif, module, "Too large uncompressed strip/tile"); + _TIFFfreeExt(tif, sp->uncompressed_buffer); + sp->uncompressed_buffer = NULL; + sp->uncompressed_alloc = 0; + return 0; + } + + if (sp->uncompressed_alloc < new_alloc) + { + _TIFFfreeExt(tif, sp->uncompressed_buffer); + sp->uncompressed_buffer = (uint8_t *)_TIFFmallocExt(tif, new_alloc); + if (!sp->uncompressed_buffer) + { + TIFFErrorExtR(tif, module, "Cannot allocate buffer"); + _TIFFfreeExt(tif, sp->uncompressed_buffer); + sp->uncompressed_buffer = NULL; + sp->uncompressed_alloc = 0; + return 0; + } + sp->uncompressed_alloc = new_alloc; + } + + if ((td->td_planarconfig == PLANARCONFIG_CONTIG && + td->td_extrasamples > 0 && + td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA && + GetLercDataType(tif) == 1) || + (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + (td->td_bitspersample == 32 || td->td_bitspersample == 64))) + { + unsigned int mask_size = sp->segment_width * sp->segment_height; +#if LERC_AT_LEAST_VERSION(3, 0, 0) + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + td->td_planarconfig == PLANARCONFIG_CONTIG) + { + /* We may need one mask per band */ + mask_size *= td->td_samplesperpixel; + } +#endif + if (sp->mask_size < mask_size) + { + void *mask_buffer = + _TIFFreallocExt(tif, sp->mask_buffer, mask_size); + if (mask_buffer == NULL) + { + TIFFErrorExtR(tif, module, "Cannot allocate buffer"); + sp->mask_size = 0; + _TIFFfreeExt(tif, sp->uncompressed_buffer); + sp->uncompressed_buffer = NULL; + sp->uncompressed_alloc = 0; + return 0; + } + sp->mask_buffer = (uint8_t *)mask_buffer; + sp->mask_size = mask_size; + } + } + + return 1; +} + +/* + * Setup state for decoding a strip. + */ +static int LERCPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "LERCPreDecode"; + lerc_status lerc_ret; + TIFFDirectory *td = &tif->tif_dir; + LERCState *sp = LERCDecoderState(tif); + int lerc_data_type; + unsigned int infoArray[9]; + unsigned nomask_bands = td->td_samplesperpixel; + int ndims; + int use_mask = 0; + uint8_t *lerc_data = tif->tif_rawcp; + unsigned int lerc_data_size = (unsigned int)tif->tif_rawcc; + + (void)s; + assert(sp != NULL); + if (sp->state != LSTATE_INIT_DECODE) + tif->tif_setupdecode(tif); + + lerc_data_type = GetLercDataType(tif); + if (lerc_data_type < 0) + return 0; + + if (!SetupBuffers(tif, sp, module)) + return 0; + + if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE) + { + if (sp->compressed_size < sp->uncompressed_alloc) + { + _TIFFfreeExt(tif, sp->compressed_buffer); + sp->compressed_buffer = _TIFFmallocExt(tif, sp->uncompressed_alloc); + if (!sp->compressed_buffer) + { + sp->compressed_size = 0; + return 0; + } + sp->compressed_size = sp->uncompressed_alloc; + } + } + + if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE) + { +#if LIBDEFLATE_SUPPORT + enum libdeflate_result res; + size_t lerc_data_sizet = 0; + if (sp->libdeflate_dec == NULL) + { + sp->libdeflate_dec = libdeflate_alloc_decompressor(); + if (sp->libdeflate_dec == NULL) + { + TIFFErrorExtR(tif, module, "Cannot allocate decompressor"); + return 0; + } + } + + res = libdeflate_zlib_decompress( + sp->libdeflate_dec, tif->tif_rawcp, (size_t)tif->tif_rawcc, + sp->compressed_buffer, sp->compressed_size, &lerc_data_sizet); + if (res != LIBDEFLATE_SUCCESS) + { + TIFFErrorExtR(tif, module, "Decoding error at scanline %lu", + (unsigned long)tif->tif_row); + return 0; + } + assert(lerc_data_sizet == (unsigned int)lerc_data_sizet); + lerc_data = (uint8_t *)sp->compressed_buffer; + lerc_data_size = (unsigned int)lerc_data_sizet; +#else + z_stream strm; + int zlib_ret; + + memset(&strm, 0, sizeof(strm)); + strm.zalloc = NULL; + strm.zfree = NULL; + strm.opaque = NULL; + zlib_ret = inflateInit(&strm); + if (zlib_ret != Z_OK) + { + TIFFErrorExtR(tif, module, "inflateInit() failed"); + inflateEnd(&strm); + return 0; + } + + strm.avail_in = (uInt)tif->tif_rawcc; + strm.next_in = tif->tif_rawcp; + strm.avail_out = sp->compressed_size; + strm.next_out = (Bytef *)sp->compressed_buffer; + zlib_ret = inflate(&strm, Z_FINISH); + if (zlib_ret != Z_STREAM_END && zlib_ret != Z_OK) + { + TIFFErrorExtR(tif, module, "inflate() failed"); + inflateEnd(&strm); + return 0; + } + lerc_data = (uint8_t *)sp->compressed_buffer; + lerc_data_size = sp->compressed_size - strm.avail_out; + inflateEnd(&strm); +#endif + } + else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD) + { +#ifdef ZSTD_SUPPORT + size_t zstd_ret; + + zstd_ret = ZSTD_decompress(sp->compressed_buffer, sp->compressed_size, + tif->tif_rawcp, tif->tif_rawcc); + if (ZSTD_isError(zstd_ret)) + { + TIFFErrorExtR(tif, module, "Error in ZSTD_decompress(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + + lerc_data = (uint8_t *)sp->compressed_buffer; + lerc_data_size = (unsigned int)zstd_ret; +#else + TIFFErrorExtR(tif, module, "ZSTD support missing"); + return 0; +#endif + } + else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE) + { + TIFFErrorExtR(tif, module, "Unhandled additional compression"); + return 0; + } + + lerc_ret = + lerc_getBlobInfo(lerc_data, lerc_data_size, infoArray, NULL, 9, 0); + if (lerc_ret != 0) + { + TIFFErrorExtR(tif, module, "lerc_getBlobInfo() failed"); + return 0; + } + + /* If the configuration is compatible of a LERC mask, and that the */ + /* LERC info has dim == samplesperpixel - 1, then there is a LERC */ + /* mask. */ + if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 && + td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA && + GetLercDataType(tif) == 1 && + infoArray[2] == td->td_samplesperpixel - 1U) + { + use_mask = 1; + nomask_bands--; + } + else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP) + { + use_mask = 1; + } + + ndims = td->td_planarconfig == PLANARCONFIG_CONTIG ? nomask_bands : 1; + + /* Info returned in infoArray is { version, dataType, nDim/nDepth, nCols, + nRows, nBands, nValidPixels, blobSize, + and starting with liblerc 3.0 nRequestedMasks } */ + if (infoArray[0] != (unsigned)sp->lerc_version) + { + TIFFWarningExtR(tif, module, + "Unexpected version number: %d. Expected: %d", + infoArray[0], sp->lerc_version); + } + if (infoArray[1] != (unsigned)lerc_data_type) + { + TIFFErrorExtR(tif, module, "Unexpected dataType: %d. Expected: %d", + infoArray[1], lerc_data_type); + return 0; + } + + const unsigned nFoundDims = infoArray[2]; +#if LERC_AT_LEAST_VERSION(3, 0, 0) + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + td->td_planarconfig == PLANARCONFIG_CONTIG && + td->td_samplesperpixel > 1) + { + if (nFoundDims != 1 && nFoundDims != (unsigned)ndims) + { + TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: 1 or %d", + nFoundDims, ndims); + return 0; + } + } + else +#endif + if (nFoundDims != (unsigned)ndims) + { + TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: %d", + nFoundDims, ndims); + return 0; + } + + if (infoArray[3] != sp->segment_width) + { + TIFFErrorExtR(tif, module, "Unexpected nCols: %d. Expected: %du", + infoArray[3], sp->segment_width); + return 0; + } + if (infoArray[4] != sp->segment_height) + { + TIFFErrorExtR(tif, module, "Unexpected nRows: %d. Expected: %u", + infoArray[4], sp->segment_height); + return 0; + } + + const unsigned nFoundBands = infoArray[5]; + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + td->td_planarconfig == PLANARCONFIG_CONTIG && + td->td_samplesperpixel > 1 && nFoundDims == 1) + { +#if !LERC_AT_LEAST_VERSION(3, 0, 0) + if (nFoundBands == td->td_samplesperpixel) + { + TIFFErrorExtR( + tif, module, + "Unexpected nBands: %d. This file may have been generated with " + "a liblerc version >= 3.0, with one mask per band, and is not " + "supported by this older version of liblerc", + nFoundBands); + return 0; + } +#endif + if (nFoundBands != td->td_samplesperpixel) + { + TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d", + nFoundBands, td->td_samplesperpixel); + return 0; + } + } + else if (nFoundBands != 1) + { + TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d", + nFoundBands, 1); + return 0; + } + + if (infoArray[7] != lerc_data_size) + { + TIFFErrorExtR(tif, module, "Unexpected blobSize: %d. Expected: %u", + infoArray[7], lerc_data_size); + return 0; + } + + int nRequestedMasks = use_mask ? 1 : 0; +#if LERC_AT_LEAST_VERSION(3, 0, 0) + const int nFoundMasks = infoArray[8]; + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + td->td_planarconfig == PLANARCONFIG_CONTIG && + td->td_samplesperpixel > 1 && nFoundDims == 1) + { + if (nFoundMasks != 0 && nFoundMasks != td->td_samplesperpixel) + { + TIFFErrorExtR(tif, module, + "Unexpected nFoundMasks: %d. Expected: 0 or %d", + nFoundMasks, td->td_samplesperpixel); + return 0; + } + nRequestedMasks = nFoundMasks; + } + else + { + if (nFoundMasks != 0 && nFoundMasks != 1) + { + TIFFErrorExtR(tif, module, + "Unexpected nFoundMasks: %d. Expected: 0 or 1", + nFoundMasks); + return 0; + } + } + if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && nFoundMasks == 0) + { + nRequestedMasks = 0; + use_mask = 0; + } +#endif + + const unsigned nb_pixels = sp->segment_width * sp->segment_height; + +#if LERC_AT_LEAST_VERSION(3, 0, 0) + if (nRequestedMasks > 1) + { + unsigned int num_bytes_needed = + nb_pixels * td->td_samplesperpixel * (td->td_bitspersample / 8); + if (sp->uncompressed_buffer_multiband_alloc < num_bytes_needed) + { + _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband); + sp->uncompressed_buffer_multiband = + (uint8_t *)_TIFFmallocExt(tif, num_bytes_needed); + if (!sp->uncompressed_buffer_multiband) + { + sp->uncompressed_buffer_multiband_alloc = 0; + return 0; + } + sp->uncompressed_buffer_multiband_alloc = num_bytes_needed; + } + lerc_ret = lerc_decode(lerc_data, lerc_data_size, nRequestedMasks, + sp->mask_buffer, nFoundDims, sp->segment_width, + sp->segment_height, nFoundBands, lerc_data_type, + sp->uncompressed_buffer_multiband); + } + else +#endif + { + lerc_ret = + lerc_decode(lerc_data, lerc_data_size, +#if LERC_AT_LEAST_VERSION(3, 0, 0) + nRequestedMasks, +#endif + use_mask ? sp->mask_buffer : NULL, nFoundDims, + sp->segment_width, sp->segment_height, nFoundBands, + lerc_data_type, sp->uncompressed_buffer); + } + if (lerc_ret != 0) + { + TIFFErrorExtR(tif, module, "lerc_decode() failed"); + return 0; + } + + /* Interleave alpha mask with other samples. */ + if (use_mask && GetLercDataType(tif) == 1) + { + unsigned src_stride = + (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8); + unsigned dst_stride = + td->td_samplesperpixel * (td->td_bitspersample / 8); + unsigned i = sp->segment_width * sp->segment_height; + /* Operate from end to begin to be able to move in place */ + while (i > 0 && i > nomask_bands) + { + i--; + sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel - + 1] = 255 * sp->mask_buffer[i]; + memcpy(sp->uncompressed_buffer + i * dst_stride, + sp->uncompressed_buffer + i * src_stride, src_stride); + } + /* First pixels must use memmove due to overlapping areas */ + while (i > 0) + { + i--; + sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel - + 1] = 255 * sp->mask_buffer[i]; + memmove(sp->uncompressed_buffer + i * dst_stride, + sp->uncompressed_buffer + i * src_stride, src_stride); + } + } + else if (use_mask && td->td_sampleformat == SAMPLEFORMAT_IEEEFP) + { + unsigned i; +#if WORDS_BIGENDIAN + const unsigned char nan_bytes[] = {0x7f, 0xc0, 0, 0}; +#else + const unsigned char nan_bytes[] = {0, 0, 0xc0, 0x7f}; +#endif + float nan_float32; + memcpy(&nan_float32, nan_bytes, 4); + + if (td->td_planarconfig == PLANARCONFIG_SEPARATE || + td->td_samplesperpixel == 1) + { + if (td->td_bitspersample == 32) + { + for (i = 0; i < nb_pixels; i++) + { + if (sp->mask_buffer[i] == 0) + ((float *)sp->uncompressed_buffer)[i] = nan_float32; + } + } + else + { + const double nan_float64 = nan_float32; + for (i = 0; i < nb_pixels; i++) + { + if (sp->mask_buffer[i] == 0) + ((double *)sp->uncompressed_buffer)[i] = nan_float64; + } + } + } + else if (nRequestedMasks == 1) + { + assert(nFoundDims == td->td_samplesperpixel); + assert(nFoundBands == 1); + + unsigned k = 0; + if (td->td_bitspersample == 32) + { + for (i = 0; i < nb_pixels; i++) + { + for (int j = 0; j < td->td_samplesperpixel; j++) + { + if (sp->mask_buffer[i] == 0) + ((float *)sp->uncompressed_buffer)[k] = nan_float32; + ++k; + } + } + } + else + { + const double nan_float64 = nan_float32; + for (i = 0; i < nb_pixels; i++) + { + for (int j = 0; j < td->td_samplesperpixel; j++) + { + if (sp->mask_buffer[i] == 0) + ((double *)sp->uncompressed_buffer)[k] = + nan_float64; + ++k; + } + } + } + } +#if LERC_AT_LEAST_VERSION(3, 0, 0) + else + { + assert(nRequestedMasks == td->td_samplesperpixel); + assert(nFoundDims == 1); + assert(nFoundBands == td->td_samplesperpixel); + + unsigned k = 0; + if (td->td_bitspersample == 32) + { + for (i = 0; i < nb_pixels; i++) + { + for (int j = 0; j < td->td_samplesperpixel; j++) + { + if (sp->mask_buffer[i + j * nb_pixels] == 0) + ((float *)sp->uncompressed_buffer)[k] = nan_float32; + else + ((float *)sp->uncompressed_buffer)[k] = + ((float *)sp->uncompressed_buffer_multiband) + [i + j * nb_pixels]; + ++k; + } + } + } + else + { + const double nan_float64 = nan_float32; + for (i = 0; i < nb_pixels; i++) + { + for (int j = 0; j < td->td_samplesperpixel; j++) + { + if (sp->mask_buffer[i + j * nb_pixels] == 0) + ((double *)sp->uncompressed_buffer)[k] = + nan_float64; + else + ((double *)sp->uncompressed_buffer)[k] = + ((double *)sp->uncompressed_buffer_multiband) + [i + j * nb_pixels]; + ++k; + } + } + } + } +#endif + } + + return 1; +} + +/* + * Decode a strip, tile or scanline. + */ +static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "LERCDecode"; + LERCState *sp = LERCDecoderState(tif); + + (void)s; + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_DECODE); + + if (sp->uncompressed_buffer == NULL) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, "Uncompressed buffer not allocated"); + return 0; + } + + if ((uint64_t)sp->uncompressed_offset + (uint64_t)occ > + sp->uncompressed_size) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, "Too many bytes read"); + return 0; + } + + memcpy(op, sp->uncompressed_buffer + sp->uncompressed_offset, occ); + sp->uncompressed_offset += (unsigned)occ; + + return 1; +} + +#ifndef LERC_READ_ONLY + +static int LERCSetupEncode(TIFF *tif) +{ + LERCState *sp = LERCEncoderState(tif); + + assert(sp != NULL); + if (sp->state & LSTATE_INIT_DECODE) + { + sp->state = 0; + } + + sp->state |= LSTATE_INIT_ENCODE; + + return 1; +} + +/* + * Reset encoding state at the start of a strip. + */ +static int LERCPreEncode(TIFF *tif, uint16_t s) +{ + static const char module[] = "LERCPreEncode"; + LERCState *sp = LERCEncoderState(tif); + int lerc_data_type; + + (void)s; + assert(sp != NULL); + if (sp->state != LSTATE_INIT_ENCODE) + tif->tif_setupencode(tif); + + lerc_data_type = GetLercDataType(tif); + if (lerc_data_type < 0) + return 0; + + if (!SetupBuffers(tif, sp, module)) + return 0; + + return 1; +} + +/* + * Encode a chunk of pixels. + */ +static int LERCEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "LERCEncode"; + LERCState *sp = LERCEncoderState(tif); + + (void)s; + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_ENCODE); + + if ((uint64_t)sp->uncompressed_offset + (uint64_t)cc > + sp->uncompressed_size) + { + TIFFErrorExtR(tif, module, "Too many bytes written"); + return 0; + } + + memcpy(sp->uncompressed_buffer + sp->uncompressed_offset, bp, cc); + sp->uncompressed_offset += (unsigned)cc; + + return 1; +} + +/* + * Finish off an encoded strip by flushing it. + */ +static int LERCPostEncode(TIFF *tif) +{ + lerc_status lerc_ret; + static const char module[] = "LERCPostEncode"; + LERCState *sp = LERCEncoderState(tif); + unsigned int numBytesWritten = 0; + TIFFDirectory *td = &tif->tif_dir; + int use_mask = 0; + unsigned dst_nbands = td->td_samplesperpixel; + + if (sp->uncompressed_offset != sp->uncompressed_size) + { + TIFFErrorExtR(tif, module, "Unexpected number of bytes in the buffer"); + return 0; + } + + int mask_count = 1; + const unsigned nb_pixels = sp->segment_width * sp->segment_height; + + /* Extract alpha mask (if containing only 0 and 255 values, */ + /* and compact array of regular bands */ + if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 && + td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA && + GetLercDataType(tif) == 1) + { + const unsigned dst_stride = + (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8); + const unsigned src_stride = + td->td_samplesperpixel * (td->td_bitspersample / 8); + unsigned i = 0; + + use_mask = 1; + for (i = 0; i < nb_pixels; i++) + { + int v = sp->uncompressed_buffer[i * src_stride + + td->td_samplesperpixel - 1]; + if (v != 0 && v != 255) + { + use_mask = 0; + break; + } + } + + if (use_mask) + { + dst_nbands--; + /* First pixels must use memmove due to overlapping areas */ + for (i = 0; i < dst_nbands && i < nb_pixels; i++) + { + memmove(sp->uncompressed_buffer + i * dst_stride, + sp->uncompressed_buffer + i * src_stride, dst_stride); + sp->mask_buffer[i] = + sp->uncompressed_buffer[i * src_stride + + td->td_samplesperpixel - 1]; + } + for (; i < nb_pixels; i++) + { + memcpy(sp->uncompressed_buffer + i * dst_stride, + sp->uncompressed_buffer + i * src_stride, dst_stride); + sp->mask_buffer[i] = + sp->uncompressed_buffer[i * src_stride + + td->td_samplesperpixel - 1]; + } + } + } + else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && + (td->td_bitspersample == 32 || td->td_bitspersample == 64)) + { + /* Check for NaN values */ + unsigned i; + if (td->td_bitspersample == 32) + { + if (td->td_planarconfig == PLANARCONFIG_CONTIG && dst_nbands > 1) + { + unsigned k = 0; + for (i = 0; i < nb_pixels; i++) + { + int count_nan = 0; + for (int j = 0; j < td->td_samplesperpixel; ++j) + { + const float val = ((float *)sp->uncompressed_buffer)[k]; + ++k; + if (val != val) + { + ++count_nan; + } + } + if (count_nan > 0) + { + use_mask = 1; + if (count_nan < td->td_samplesperpixel) + { + mask_count = td->td_samplesperpixel; + break; + } + } + } + } + else + { + for (i = 0; i < nb_pixels; i++) + { + const float val = ((float *)sp->uncompressed_buffer)[i]; + if (val != val) + { + use_mask = 1; + break; + } + } + } + } + else + { + if (td->td_planarconfig == PLANARCONFIG_CONTIG && dst_nbands > 1) + { + unsigned k = 0; + for (i = 0; i < nb_pixels; i++) + { + int count_nan = 0; + for (int j = 0; j < td->td_samplesperpixel; ++j) + { + const double val = + ((double *)sp->uncompressed_buffer)[k]; + ++k; + if (val != val) + { + ++count_nan; + } + } + if (count_nan > 0) + { + use_mask = 1; + if (count_nan < td->td_samplesperpixel) + { + mask_count = td->td_samplesperpixel; + break; + } + } + } + } + else + { + for (i = 0; i < nb_pixels; i++) + { + const double val = ((double *)sp->uncompressed_buffer)[i]; + if (val != val) + { + use_mask = 1; + break; + } + } + } + } + + if (use_mask) + { + if (mask_count > 1) + { +#if LERC_AT_LEAST_VERSION(3, 0, 0) + unsigned int num_bytes_needed = + nb_pixels * dst_nbands * (td->td_bitspersample / 8); + if (sp->uncompressed_buffer_multiband_alloc < num_bytes_needed) + { + _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband); + sp->uncompressed_buffer_multiband = + (uint8_t *)_TIFFmallocExt(tif, num_bytes_needed); + if (!sp->uncompressed_buffer_multiband) + { + sp->uncompressed_buffer_multiband_alloc = 0; + return 0; + } + sp->uncompressed_buffer_multiband_alloc = num_bytes_needed; + } + + unsigned k = 0; + if (td->td_bitspersample == 32) + { + for (i = 0; i < nb_pixels; i++) + { + for (int j = 0; j < td->td_samplesperpixel; ++j) + { + const float val = + ((float *)sp->uncompressed_buffer)[k]; + ((float *)sp->uncompressed_buffer_multiband) + [i + j * nb_pixels] = val; + ++k; + sp->mask_buffer[i + j * nb_pixels] = + (val == val) ? 255 : 0; + } + } + } + else + { + for (i = 0; i < nb_pixels; i++) + { + for (int j = 0; j < td->td_samplesperpixel; ++j) + { + const double val = + ((double *)sp->uncompressed_buffer)[k]; + ((double *)sp->uncompressed_buffer_multiband) + [i + j * nb_pixels] = val; + ++k; + sp->mask_buffer[i + j * nb_pixels] = + (val == val) ? 255 : 0; + } + } + } +#else + TIFFErrorExtR(tif, module, + "lerc_encode() would need to create one mask per " + "sample, but this requires liblerc >= 3.0"); + return 0; +#endif + } + else if (td->td_planarconfig == PLANARCONFIG_CONTIG && + dst_nbands > 1) + { + if (td->td_bitspersample == 32) + { + for (i = 0; i < nb_pixels; i++) + { + const float val = + ((float *)sp->uncompressed_buffer)[i * dst_nbands]; + sp->mask_buffer[i] = (val == val) ? 255 : 0; + } + } + else + { + for (i = 0; i < nb_pixels; i++) + { + const double val = + ((double *)sp->uncompressed_buffer)[i * dst_nbands]; + sp->mask_buffer[i] = (val == val) ? 255 : 0; + } + } + } + else + { + if (td->td_bitspersample == 32) + { + for (i = 0; i < nb_pixels; i++) + { + const float val = ((float *)sp->uncompressed_buffer)[i]; + sp->mask_buffer[i] = (val == val) ? 255 : 0; + } + } + else + { + for (i = 0; i < nb_pixels; i++) + { + const double val = + ((double *)sp->uncompressed_buffer)[i]; + sp->mask_buffer[i] = (val == val) ? 255 : 0; + } + } + } + } + } + + unsigned int estimated_compressed_size = sp->uncompressed_alloc; +#if LERC_AT_LEAST_VERSION(3, 0, 0) + if (mask_count > 1) + { + estimated_compressed_size += nb_pixels * mask_count / 8; + } +#endif + + if (sp->compressed_size < estimated_compressed_size) + { + _TIFFfreeExt(tif, sp->compressed_buffer); + sp->compressed_buffer = _TIFFmallocExt(tif, estimated_compressed_size); + if (!sp->compressed_buffer) + { + sp->compressed_size = 0; + return 0; + } + sp->compressed_size = estimated_compressed_size; + } + +#if LERC_AT_LEAST_VERSION(3, 0, 0) + if (mask_count > 1) + { + lerc_ret = lerc_encodeForVersion( + sp->uncompressed_buffer_multiband, sp->lerc_version, + GetLercDataType(tif), 1, sp->segment_width, sp->segment_height, + dst_nbands, dst_nbands, sp->mask_buffer, sp->maxzerror, + (unsigned char *)sp->compressed_buffer, sp->compressed_size, + &numBytesWritten); + } + else +#endif + { + lerc_ret = lerc_encodeForVersion( + sp->uncompressed_buffer, sp->lerc_version, GetLercDataType(tif), + td->td_planarconfig == PLANARCONFIG_CONTIG ? dst_nbands : 1, + sp->segment_width, sp->segment_height, 1, +#if LERC_AT_LEAST_VERSION(3, 0, 0) + use_mask ? 1 : 0, +#endif + use_mask ? sp->mask_buffer : NULL, sp->maxzerror, + (unsigned char *)sp->compressed_buffer, sp->compressed_size, + &numBytesWritten); + } + if (lerc_ret != 0) + { + TIFFErrorExtR(tif, module, "lerc_encode() failed"); + return 0; + } + assert(numBytesWritten < estimated_compressed_size); + + if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE) + { +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_enc == NULL) + { + /* To get results as good as zlib, we ask for an extra */ + /* level of compression */ + sp->libdeflate_enc = libdeflate_alloc_compressor( + sp->zipquality == Z_DEFAULT_COMPRESSION ? 7 + : sp->zipquality >= 6 && sp->zipquality <= 9 + ? sp->zipquality + 1 + : sp->zipquality); + if (sp->libdeflate_enc == NULL) + { + TIFFErrorExtR(tif, module, "Cannot allocate compressor"); + return 0; + } + } + + /* Should not happen normally */ + if (libdeflate_zlib_compress_bound( + sp->libdeflate_enc, numBytesWritten) > sp->uncompressed_alloc) + { + TIFFErrorExtR(tif, module, + "Output buffer for libdeflate too small"); + return 0; + } + + tif->tif_rawcc = libdeflate_zlib_compress( + sp->libdeflate_enc, sp->compressed_buffer, numBytesWritten, + sp->uncompressed_buffer, sp->uncompressed_alloc); + + if (tif->tif_rawcc == 0) + { + TIFFErrorExtR(tif, module, "Encoder error at scanline %lu", + (unsigned long)tif->tif_row); + return 0; + } +#else + z_stream strm; + int zlib_ret; + int cappedQuality = sp->zipquality; + if (cappedQuality > Z_BEST_COMPRESSION) + cappedQuality = Z_BEST_COMPRESSION; + + memset(&strm, 0, sizeof(strm)); + strm.zalloc = NULL; + strm.zfree = NULL; + strm.opaque = NULL; + zlib_ret = deflateInit(&strm, cappedQuality); + if (zlib_ret != Z_OK) + { + TIFFErrorExtR(tif, module, "deflateInit() failed"); + return 0; + } + + strm.avail_in = numBytesWritten; + strm.next_in = sp->compressed_buffer; + strm.avail_out = sp->uncompressed_alloc; + strm.next_out = sp->uncompressed_buffer; + zlib_ret = deflate(&strm, Z_FINISH); + if (zlib_ret == Z_STREAM_END) + { + tif->tif_rawcc = sp->uncompressed_alloc - strm.avail_out; + } + deflateEnd(&strm); + if (zlib_ret != Z_STREAM_END) + { + TIFFErrorExtR(tif, module, "deflate() failed"); + return 0; + } +#endif + { + int ret; + uint8_t *tif_rawdata_backup = tif->tif_rawdata; + tif->tif_rawdata = sp->uncompressed_buffer; + ret = TIFFFlushData1(tif); + tif->tif_rawdata = tif_rawdata_backup; + if (!ret) + { + return 0; + } + } + } + else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD) + { +#ifdef ZSTD_SUPPORT + size_t zstd_ret = ZSTD_compress( + sp->uncompressed_buffer, sp->uncompressed_alloc, + sp->compressed_buffer, numBytesWritten, sp->zstd_compress_level); + if (ZSTD_isError(zstd_ret)) + { + TIFFErrorExtR(tif, module, "Error in ZSTD_compress(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + + { + int ret; + uint8_t *tif_rawdata_backup = tif->tif_rawdata; + tif->tif_rawdata = sp->uncompressed_buffer; + tif->tif_rawcc = zstd_ret; + ret = TIFFFlushData1(tif); + tif->tif_rawdata = tif_rawdata_backup; + if (!ret) + { + return 0; + } + } +#else + TIFFErrorExtR(tif, module, "ZSTD support missing"); + return 0; +#endif + } + else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE) + { + TIFFErrorExtR(tif, module, "Unhandled additional compression"); + return 0; + } + else + { + int ret; + uint8_t *tif_rawdata_backup = tif->tif_rawdata; + tif->tif_rawdata = (uint8_t *)sp->compressed_buffer; + tif->tif_rawcc = numBytesWritten; + ret = TIFFFlushData1(tif); + tif->tif_rawdata = tif_rawdata_backup; + if (!ret) + return 0; + } + + return 1; +} + +#endif /* LERC_READ_ONLY */ + +static void LERCCleanup(TIFF *tif) +{ + LERCState *sp = GetLERCState(tif); + + assert(sp != NULL); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + _TIFFfreeExt(tif, sp->uncompressed_buffer); + _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband); + _TIFFfreeExt(tif, sp->compressed_buffer); + _TIFFfreeExt(tif, sp->mask_buffer); + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_dec) + libdeflate_free_decompressor(sp->libdeflate_dec); + if (sp->libdeflate_enc) + libdeflate_free_compressor(sp->libdeflate_enc); +#endif + + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static const TIFFField LERCFields[] = { + {TIFFTAG_LERC_PARAMETERS, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG, 0, + TIFF_SETGET_C32_UINT32, FIELD_CUSTOM, FALSE, TRUE, + (char *)"LercParameters", NULL}, + {TIFFTAG_LERC_MAXZERROR, 0, 0, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, + FIELD_PSEUDO, TRUE, FALSE, (char *)"LercMaximumError", NULL}, + {TIFFTAG_LERC_VERSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32, FIELD_PSEUDO, + FALSE, FALSE, (char *)"LercVersion", NULL}, + {TIFFTAG_LERC_ADD_COMPRESSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32, + FIELD_PSEUDO, FALSE, FALSE, (char *)"LercAdditionalCompression", NULL}, + {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE, + FALSE, (char *)"ZSTD zstd_compress_level", NULL}, + {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE, + FALSE, (char *)"", NULL}, +}; + +static int LERCVSetFieldBase(TIFF *tif, uint32_t tag, ...) +{ + LERCState *sp = GetLERCState(tif); + int ret; + va_list ap; + va_start(ap, tag); + ret = (*sp->vsetparent)(tif, tag, ap); + va_end(ap); + return ret; +} + +static int LERCVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "LERCVSetField"; + LERCState *sp = GetLERCState(tif); + + switch (tag) + { + case TIFFTAG_LERC_PARAMETERS: + { + uint32_t count = va_arg(ap, int); + int *params = va_arg(ap, int *); + if (count < 2) + { + TIFFErrorExtR(tif, module, + "Invalid count for LercParameters: %u", count); + return 0; + } + sp->lerc_version = params[0]; + sp->additional_compression = params[1]; + return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, count, + params); + } + case TIFFTAG_LERC_MAXZERROR: + sp->maxzerror = va_arg(ap, double); + return 1; + case TIFFTAG_LERC_VERSION: + { + int params[2] = {0, 0}; + int version = va_arg(ap, int); + if (version != LERC_VERSION_2_4) + { + TIFFErrorExtR(tif, module, "Invalid value for LercVersion: %d", + version); + return 0; + } + sp->lerc_version = version; + params[0] = sp->lerc_version; + params[1] = sp->additional_compression; + return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params); + } + case TIFFTAG_LERC_ADD_COMPRESSION: + { + int params[2] = {0, 0}; + int additional_compression = va_arg(ap, int); +#ifndef ZSTD_SUPPORT + if (additional_compression == LERC_ADD_COMPRESSION_ZSTD) + { + TIFFErrorExtR(tif, module, + "LERC_ZSTD requested, but ZSTD not available"); + return 0; + } +#endif + if (additional_compression != LERC_ADD_COMPRESSION_NONE && + additional_compression != LERC_ADD_COMPRESSION_DEFLATE && + additional_compression != LERC_ADD_COMPRESSION_ZSTD) + { + TIFFErrorExtR(tif, module, + "Invalid value for LercAdditionalCompression: %d", + additional_compression); + return 0; + } + sp->additional_compression = additional_compression; + params[0] = sp->lerc_version; + params[1] = sp->additional_compression; + return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params); + } +#ifdef ZSTD_SUPPORT + case TIFFTAG_ZSTD_LEVEL: + { + sp->zstd_compress_level = (int)va_arg(ap, int); + if (sp->zstd_compress_level <= 0 || + sp->zstd_compress_level > ZSTD_maxCLevel()) + { + TIFFWarningExtR(tif, module, + "ZSTD_LEVEL should be between 1 and %d", + ZSTD_maxCLevel()); + } + return 1; + } +#endif + case TIFFTAG_ZIPQUALITY: + { + sp->zipquality = (int)va_arg(ap, int); + if (sp->zipquality < Z_DEFAULT_COMPRESSION || + sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL) + { + TIFFErrorExtR( + tif, module, + "Invalid ZipQuality value. Should be in [-1,%d] range", + LIBDEFLATE_MAX_COMPRESSION_LEVEL); + return 0; + } + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_enc) + { + libdeflate_free_compressor(sp->libdeflate_enc); + sp->libdeflate_enc = NULL; + } +#endif + + return (1); + } + default: + return (*sp->vsetparent)(tif, tag, ap); + } + /*NOTREACHED*/ +} + +static int LERCVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + LERCState *sp = GetLERCState(tif); + + switch (tag) + { + case TIFFTAG_LERC_MAXZERROR: + *va_arg(ap, double *) = sp->maxzerror; + break; + case TIFFTAG_LERC_VERSION: + *va_arg(ap, int *) = sp->lerc_version; + break; + case TIFFTAG_LERC_ADD_COMPRESSION: + *va_arg(ap, int *) = sp->additional_compression; + break; + case TIFFTAG_ZSTD_LEVEL: + *va_arg(ap, int *) = sp->zstd_compress_level; + break; + case TIFFTAG_ZIPQUALITY: + *va_arg(ap, int *) = sp->zipquality; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return 1; +} + +int TIFFInitLERC(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitLERC"; + LERCState *sp; + + (void)scheme; + assert(scheme == COMPRESSION_LERC); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, LERCFields, TIFFArrayCount(LERCFields))) + { + TIFFErrorExtR(tif, module, "Merging LERC codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, 1, sizeof(LERCState)); + if (tif->tif_data == NULL) + goto bad; + sp = GetLERCState(tif); + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = LERCVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = LERCVSetField; /* hook for codec tags */ + + /* + * Install codec methods. + */ + tif->tif_fixuptags = LERCFixupTags; + tif->tif_setupdecode = LERCSetupDecode; + tif->tif_predecode = LERCPreDecode; + tif->tif_decoderow = LERCDecode; + tif->tif_decodestrip = LERCDecode; + tif->tif_decodetile = LERCDecode; +#ifndef LERC_READ_ONLY + tif->tif_setupencode = LERCSetupEncode; + tif->tif_preencode = LERCPreEncode; + tif->tif_postencode = LERCPostEncode; + tif->tif_encoderow = LERCEncode; + tif->tif_encodestrip = LERCEncode; + tif->tif_encodetile = LERCEncode; +#endif + tif->tif_cleanup = LERCCleanup; + + /* Default values for codec-specific fields */ + TIFFSetField(tif, TIFFTAG_LERC_VERSION, LERC_VERSION_2_4); + TIFFSetField(tif, TIFFTAG_LERC_ADD_COMPRESSION, LERC_ADD_COMPRESSION_NONE); + sp->maxzerror = 0.0; + sp->zstd_compress_level = 9; /* default comp. level */ + sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */ + sp->state = 0; + + return 1; +bad: + TIFFErrorExtR(tif, module, "No space for LERC state block"); + return 0; +} +#endif /* LERC_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_luv.c b/cpp/3rd_party/libtiff/tif_luv.c new file mode 100644 index 0000000000..ecc3225265 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_luv.c @@ -0,0 +1,1855 @@ +/* + * Copyright (c) 1997 Greg Ward Larson + * Copyright (c) 1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any + * advertising or publicity relating to the software without the specific, + * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE + * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef LOGLUV_SUPPORT + +/* + * TIFF Library. + * LogLuv compression support for high dynamic range images. + * + * Contributed by Greg Larson. + * + * LogLuv image support uses the TIFF library to store 16 or 10-bit + * log luminance values with 8 bits each of u and v or a 14-bit index. + * + * The codec can take as input and produce as output 32-bit IEEE float values + * as well as 16-bit integer values. A 16-bit luminance is interpreted + * as a sign bit followed by a 15-bit integer that is converted + * to and from a linear magnitude using the transformation: + * + * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit + * + * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real + * + * The actual conversion to world luminance units in candelas per sq. meter + * requires an additional multiplier, which is stored in the TIFFTAG_STONITS. + * This value is usually set such that a reasonable exposure comes from + * clamping decoded luminances above 1 to 1 in the displayed image. + * + * The 16-bit values for u and v may be converted to real values by dividing + * each by 32768. (This allows for negative values, which aren't useful as + * far as we know, but are left in case of future improvements in human + * color vision.) + * + * Conversion from (u,v), which is actually the CIE (u',v') system for + * you color scientists, is accomplished by the following transformation: + * + * u = 4*x / (-2*x + 12*y + 3) + * v = 9*y / (-2*x + 12*y + 3) + * + * x = 9*u / (6*u - 16*v + 12) + * y = 4*v / (6*u - 16*v + 12) + * + * This process is greatly simplified by passing 32-bit IEEE floats + * for each of three CIE XYZ coordinates. The codec then takes care + * of conversion to and from LogLuv, though the application is still + * responsible for interpreting the TIFFTAG_STONITS calibration factor. + * + * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white + * point of (x,y)=(1/3,1/3). However, most color systems assume some other + * white point, such as D65, and an absolute color conversion to XYZ then + * to another color space with a different white point may introduce an + * unwanted color cast to the image. It is often desirable, therefore, to + * perform a white point conversion that maps the input white to [1 1 1] + * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT + * tag value. A decoder that demands absolute color calibration may use + * this white point tag to get back the original colors, but usually it + * will be ignored and the new white point will be used instead that + * matches the output color space. + * + * Pixel information is compressed into one of two basic encodings, depending + * on the setting of the compression tag, which is one of COMPRESSION_SGILOG + * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is + * stored as: + * + * 1 15 + * |-+---------------| + * + * COMPRESSION_SGILOG color data is stored as: + * + * 1 15 8 8 + * |-+---------------|--------+--------| + * S Le ue ve + * + * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as: + * + * 10 14 + * |----------|--------------| + * Le' Ce + * + * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is + * encoded as an index for optimal color resolution. The 10 log bits are + * defined by the following conversions: + * + * L = 2^((Le'+.5)/64 - 12) # real from 10-bit + * + * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real + * + * The 10 bits of the smaller format may be converted into the 15 bits of + * the larger format by multiplying by 4 and adding 13314. Obviously, + * a smaller range of magnitudes is covered (about 5 orders of magnitude + * instead of 38), and the lack of a sign bit means that negative luminances + * are not allowed. (Well, they aren't allowed in the real world, either, + * but they are useful for certain types of image processing.) + * + * The desired user format is controlled by the setting the internal + * pseudo tag TIFFTAG_SGILOGDATAFMT to one of: + * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values + * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v + * Raw data i/o is also possible using: + * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel + * In addition, the following decoding is provided for ease of display: + * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values + * + * For grayscale images, we provide the following data formats: + * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values + * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance + * SGILOGDATAFMT_8BIT = 8-bit gray monitor values + * + * Note that the COMPRESSION_SGILOG applies a simple run-length encoding + * scheme by separating the logL, u and v bytes for each row and applying + * a PackBits type of compression. Since the 24-bit encoding is not + * adaptive, the 32-bit color format takes less space in many cases. + * + * Further control is provided over the conversion from higher-resolution + * formats to final encoded values through the pseudo tag + * TIFFTAG_SGILOGENCODE: + * SGILOGENCODE_NODITHER = do not dither encoded values + * SGILOGENCODE_RANDITHER = apply random dithering during encoding + * + * The default value of this tag is SGILOGENCODE_NODITHER for + * COMPRESSION_SGILOG to maximize run-length encoding and + * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn + * quantization errors into noise. + */ + +#include +#include +#include +#include +#include + +/* + * State block for each open TIFF + * file using LogLuv compression/decompression. + */ +typedef struct logLuvState LogLuvState; + +struct logLuvState +{ + int encoder_state; /* 1 if encoder correctly initialized */ + int user_datafmt; /* user data format */ + int encode_meth; /* encoding method */ + int pixel_size; /* bytes per pixel */ + + uint8_t *tbuf; /* translation buffer */ + tmsize_t tbuflen; /* buffer length */ + void (*tfunc)(LogLuvState *, uint8_t *, tmsize_t); + + TIFFVSetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ +}; + +#define DecoderState(tif) ((LogLuvState *)(tif)->tif_data) +#define EncoderState(tif) ((LogLuvState *)(tif)->tif_data) + +#define SGILOGDATAFMT_UNKNOWN -1 + +#define MINRUN 4 /* minimum run length */ + +/* + * Decode a string of 16-bit gray pixels. + */ +static int LogL16Decode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "LogL16Decode"; + LogLuvState *sp = DecoderState(tif); + int shft; + tmsize_t i; + tmsize_t npixels; + unsigned char *bp; + int16_t *tp; + int16_t b; + tmsize_t cc; + int rc; + + (void)s; + assert(s == 0); + assert(sp != NULL); + + npixels = occ / sp->pixel_size; + + if (sp->user_datafmt == SGILOGDATAFMT_16BIT) + tp = (int16_t *)op; + else + { + if (sp->tbuflen < npixels) + { + TIFFErrorExtR(tif, module, "Translation buffer too short"); + return (0); + } + tp = (int16_t *)sp->tbuf; + } + _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0])); + + bp = (unsigned char *)tif->tif_rawcp; + cc = tif->tif_rawcc; + /* get each byte string */ + for (shft = 8; shft >= 0; shft -= 8) + { + for (i = 0; i < npixels && cc > 0;) + { + if (*bp >= 128) + { /* run */ + if (cc < 2) + break; + rc = *bp++ + (2 - 128); + b = (int16_t)(*bp++ << shft); + cc -= 2; + while (rc-- && i < npixels) + tp[i++] |= b; + } + else + { /* non-run */ + rc = *bp++; /* nul is noop */ + while (--cc && rc-- && i < npixels) + tp[i++] |= (int16_t)*bp++ << shft; + } + } + if (i != npixels) + { + TIFFErrorExtR(tif, module, + "Not enough data at row %" PRIu32 + " (short %" TIFF_SSIZE_FORMAT " pixels)", + tif->tif_row, npixels - i); + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + return (0); + } + } + (*sp->tfunc)(sp, op, npixels); + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + return (1); +} + +/* + * Decode a string of 24-bit pixels. + */ +static int LogLuvDecode24(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "LogLuvDecode24"; + LogLuvState *sp = DecoderState(tif); + tmsize_t cc; + tmsize_t i; + tmsize_t npixels; + unsigned char *bp; + uint32_t *tp; + + (void)s; + assert(s == 0); + assert(sp != NULL); + + npixels = occ / sp->pixel_size; + + if (sp->user_datafmt == SGILOGDATAFMT_RAW) + tp = (uint32_t *)op; + else + { + if (sp->tbuflen < npixels) + { + TIFFErrorExtR(tif, module, "Translation buffer too short"); + return (0); + } + tp = (uint32_t *)sp->tbuf; + } + /* copy to array of uint32_t */ + bp = (unsigned char *)tif->tif_rawcp; + cc = tif->tif_rawcc; + for (i = 0; i < npixels && cc >= 3; i++) + { + tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2]; + bp += 3; + cc -= 3; + } + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + if (i != npixels) + { + TIFFErrorExtR(tif, module, + "Not enough data at row %" PRIu32 + " (short %" TIFF_SSIZE_FORMAT " pixels)", + tif->tif_row, npixels - i); + return (0); + } + (*sp->tfunc)(sp, op, npixels); + return (1); +} + +/* + * Decode a string of 32-bit pixels. + */ +static int LogLuvDecode32(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "LogLuvDecode32"; + LogLuvState *sp; + int shft; + tmsize_t i; + tmsize_t npixels; + unsigned char *bp; + uint32_t *tp; + uint32_t b; + tmsize_t cc; + int rc; + + (void)s; + assert(s == 0); + sp = DecoderState(tif); + assert(sp != NULL); + + npixels = occ / sp->pixel_size; + + if (sp->user_datafmt == SGILOGDATAFMT_RAW) + tp = (uint32_t *)op; + else + { + if (sp->tbuflen < npixels) + { + TIFFErrorExtR(tif, module, "Translation buffer too short"); + return (0); + } + tp = (uint32_t *)sp->tbuf; + } + _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0])); + + bp = (unsigned char *)tif->tif_rawcp; + cc = tif->tif_rawcc; + /* get each byte string */ + for (shft = 24; shft >= 0; shft -= 8) + { + for (i = 0; i < npixels && cc > 0;) + { + if (*bp >= 128) + { /* run */ + if (cc < 2) + break; + rc = *bp++ + (2 - 128); + b = (uint32_t)*bp++ << shft; + cc -= 2; + while (rc-- && i < npixels) + tp[i++] |= b; + } + else + { /* non-run */ + rc = *bp++; /* nul is noop */ + while (--cc && rc-- && i < npixels) + tp[i++] |= (uint32_t)*bp++ << shft; + } + } + if (i != npixels) + { + TIFFErrorExtR(tif, module, + "Not enough data at row %" PRIu32 + " (short %" TIFF_SSIZE_FORMAT " pixels)", + tif->tif_row, npixels - i); + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + return (0); + } + } + (*sp->tfunc)(sp, op, npixels); + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + return (1); +} + +/* + * Decode a strip of pixels. We break it into rows to + * maintain synchrony with the encode algorithm, which + * is row by row. + */ +static int LogLuvDecodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + tmsize_t rowlen = TIFFScanlineSize(tif); + + if (rowlen == 0) + return 0; + + assert(cc % rowlen == 0); + while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) + { + bp += rowlen; + cc -= rowlen; + } + return (cc == 0); +} + +/* + * Decode a tile of pixels. We break it into rows to + * maintain synchrony with the encode algorithm, which + * is row by row. + */ +static int LogLuvDecodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + tmsize_t rowlen = TIFFTileRowSize(tif); + + if (rowlen == 0) + return 0; + + assert(cc % rowlen == 0); + while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) + { + bp += rowlen; + cc -= rowlen; + } + return (cc == 0); +} + +/* + * Encode a row of 16-bit pixels. + */ +static int LogL16Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "LogL16Encode"; + LogLuvState *sp = EncoderState(tif); + int shft; + tmsize_t i; + tmsize_t j; + tmsize_t npixels; + uint8_t *op; + int16_t *tp; + int16_t b; + tmsize_t occ; + int rc = 0, mask; + tmsize_t beg; + + (void)s; + assert(s == 0); + assert(sp != NULL); + npixels = cc / sp->pixel_size; + + if (sp->user_datafmt == SGILOGDATAFMT_16BIT) + tp = (int16_t *)bp; + else + { + tp = (int16_t *)sp->tbuf; + if (sp->tbuflen < npixels) + { + TIFFErrorExtR(tif, module, "Translation buffer too short"); + return (0); + } + (*sp->tfunc)(sp, bp, npixels); + } + /* compress each byte string */ + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + for (shft = 8; shft >= 0; shft -= 8) + { + for (i = 0; i < npixels; i += rc) + { + if (occ < 4) + { + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + } + mask = 0xff << shft; /* find next run */ + for (beg = i; beg < npixels; beg += rc) + { + b = (int16_t)(tp[beg] & mask); + rc = 1; + while (rc < 127 + 2 && beg + rc < npixels && + (tp[beg + rc] & mask) == b) + rc++; + if (rc >= MINRUN) + break; /* long enough */ + } + if (beg - i > 1 && beg - i < MINRUN) + { + b = (int16_t)(tp[i] & mask); /*check short run */ + j = i + 1; + while ((tp[j++] & mask) == b) + if (j == beg) + { + *op++ = (uint8_t)(128 - 2 + j - i); + *op++ = (uint8_t)(b >> shft); + occ -= 2; + i = beg; + break; + } + } + while (i < beg) + { /* write out non-run */ + if ((j = beg - i) > 127) + j = 127; + if (occ < j + 3) + { + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + } + *op++ = (uint8_t)j; + occ--; + while (j--) + { + *op++ = (uint8_t)(tp[i++] >> shft & 0xff); + occ--; + } + } + if (rc >= MINRUN) + { /* write out run */ + *op++ = (uint8_t)(128 - 2 + rc); + *op++ = (uint8_t)(tp[beg] >> shft & 0xff); + occ -= 2; + } + else + rc = 0; + } + } + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + + return (1); +} + +/* + * Encode a row of 24-bit pixels. + */ +static int LogLuvEncode24(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "LogLuvEncode24"; + LogLuvState *sp = EncoderState(tif); + tmsize_t i; + tmsize_t npixels; + tmsize_t occ; + uint8_t *op; + uint32_t *tp; + + (void)s; + assert(s == 0); + assert(sp != NULL); + npixels = cc / sp->pixel_size; + + if (sp->user_datafmt == SGILOGDATAFMT_RAW) + tp = (uint32_t *)bp; + else + { + tp = (uint32_t *)sp->tbuf; + if (sp->tbuflen < npixels) + { + TIFFErrorExtR(tif, module, "Translation buffer too short"); + return (0); + } + (*sp->tfunc)(sp, bp, npixels); + } + /* write out encoded pixels */ + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + for (i = npixels; i--;) + { + if (occ < 3) + { + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + } + *op++ = (uint8_t)(*tp >> 16); + *op++ = (uint8_t)(*tp >> 8 & 0xff); + *op++ = (uint8_t)(*tp++ & 0xff); + occ -= 3; + } + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + + return (1); +} + +/* + * Encode a row of 32-bit pixels. + */ +static int LogLuvEncode32(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "LogLuvEncode32"; + LogLuvState *sp = EncoderState(tif); + int shft; + tmsize_t i; + tmsize_t j; + tmsize_t npixels; + uint8_t *op; + uint32_t *tp; + uint32_t b; + tmsize_t occ; + int rc = 0; + tmsize_t beg; + + (void)s; + assert(s == 0); + assert(sp != NULL); + + npixels = cc / sp->pixel_size; + + if (sp->user_datafmt == SGILOGDATAFMT_RAW) + tp = (uint32_t *)bp; + else + { + tp = (uint32_t *)sp->tbuf; + if (sp->tbuflen < npixels) + { + TIFFErrorExtR(tif, module, "Translation buffer too short"); + return (0); + } + (*sp->tfunc)(sp, bp, npixels); + } + /* compress each byte string */ + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + for (shft = 24; shft >= 0; shft -= 8) + { + const uint32_t mask = 0xffU << shft; /* find next run */ + for (i = 0; i < npixels; i += rc) + { + if (occ < 4) + { + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + } + for (beg = i; beg < npixels; beg += rc) + { + b = tp[beg] & mask; + rc = 1; + while (rc < 127 + 2 && beg + rc < npixels && + (tp[beg + rc] & mask) == b) + rc++; + if (rc >= MINRUN) + break; /* long enough */ + } + if (beg - i > 1 && beg - i < MINRUN) + { + b = tp[i] & mask; /* check short run */ + j = i + 1; + while ((tp[j++] & mask) == b) + if (j == beg) + { + *op++ = (uint8_t)(128 - 2 + j - i); + *op++ = (uint8_t)(b >> shft); + occ -= 2; + i = beg; + break; + } + } + while (i < beg) + { /* write out non-run */ + if ((j = beg - i) > 127) + j = 127; + if (occ < j + 3) + { + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + occ = tif->tif_rawdatasize - tif->tif_rawcc; + } + *op++ = (uint8_t)j; + occ--; + while (j--) + { + *op++ = (uint8_t)(tp[i++] >> shft & 0xff); + occ--; + } + } + if (rc >= MINRUN) + { /* write out run */ + *op++ = (uint8_t)(128 - 2 + rc); + *op++ = (uint8_t)(tp[beg] >> shft & 0xff); + occ -= 2; + } + else + rc = 0; + } + } + tif->tif_rawcp = op; + tif->tif_rawcc = tif->tif_rawdatasize - occ; + + return (1); +} + +/* + * Encode a strip of pixels. We break it into rows to + * avoid encoding runs across row boundaries. + */ +static int LogLuvEncodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + tmsize_t rowlen = TIFFScanlineSize(tif); + + if (rowlen == 0) + return 0; + + assert(cc % rowlen == 0); + while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) + { + bp += rowlen; + cc -= rowlen; + } + return (cc == 0); +} + +/* + * Encode a tile of pixels. We break it into rows to + * avoid encoding runs across row boundaries. + */ +static int LogLuvEncodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + tmsize_t rowlen = TIFFTileRowSize(tif); + + if (rowlen == 0) + return 0; + + assert(cc % rowlen == 0); + while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) + { + bp += rowlen; + cc -= rowlen; + } + return (cc == 0); +} + +/* + * Encode/Decode functions for converting to and from user formats. + */ + +#include "uvcode.h" + +#ifndef UVSCALE +#define U_NEU 0.210526316 +#define V_NEU 0.473684211 +#define UVSCALE 410. +#endif + +#ifndef M_LN2 +#define M_LN2 0.69314718055994530942 +#endif +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif +#undef log2 /* Conflict with C'99 function */ +#define log2(x) ((1. / M_LN2) * log(x)) +#undef exp2 /* Conflict with C'99 function */ +#define exp2(x) exp(M_LN2 *(x)) + +#define TIFF_RAND_MAX 32767 + +// From POSIX.1-2001 as an example of an implementation of rand() +static uint32_t _TIFFRand() +{ + static uint32_t nCounter = 0; + if (!nCounter) + nCounter = (uint32_t)(time(NULL) & UINT32_MAX); + ++nCounter; + uint32_t nCounterLocal = + (uint32_t)(((uint64_t)(nCounter)*1103515245U + 12345U) & UINT32_MAX); + nCounter = nCounterLocal; + return (nCounterLocal / 65536U) % (TIFF_RAND_MAX + 1); +}; + +static int tiff_itrunc(double x, int m) +{ + if (m == SGILOGENCODE_NODITHER) + return (int)x; + return (int)(x + _TIFFRand() * (1. / TIFF_RAND_MAX) - .5); +} + +#if !LOGLUV_PUBLIC +static +#endif + double + LogL16toY(int p16) /* compute luminance from 16-bit LogL */ +{ + int Le = p16 & 0x7fff; + double Y; + + if (!Le) + return (0.); + Y = exp(M_LN2 / 256. * (Le + .5) - M_LN2 * 64.); + return (!(p16 & 0x8000) ? Y : -Y); +} + +#if !LOGLUV_PUBLIC +static +#endif + int + LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */ +{ + if (Y >= 1.8371976e19) + return (0x7fff); + if (Y <= -1.8371976e19) + return (0xffff); + if (Y > 5.4136769e-20) + return tiff_itrunc(256. * (log2(Y) + 64.), em); + if (Y < -5.4136769e-20) + return (~0x7fff | tiff_itrunc(256. * (log2(-Y) + 64.), em)); + return (0); +} + +static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + int16_t *l16 = (int16_t *)sp->tbuf; + float *yp = (float *)op; + + while (n-- > 0) + *yp++ = (float)LogL16toY(*l16++); +} + +static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + int16_t *l16 = (int16_t *)sp->tbuf; + uint8_t *gp = (uint8_t *)op; + + while (n-- > 0) + { + double Y = LogL16toY(*l16++); + *gp++ = (uint8_t)((Y <= 0.) ? 0 + : (Y >= 1.) ? 255 + : (int)(256. * sqrt(Y))); + } +} + +static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + int16_t *l16 = (int16_t *)sp->tbuf; + float *yp = (float *)op; + + while (n-- > 0) + *l16++ = (int16_t)(LogL16fromY(*yp++, sp->encode_meth)); +} + +#if !LOGLUV_PUBLIC +static +#endif + void + XYZtoRGB24(float *xyz, uint8_t *rgb) +{ + double r, g, b; + /* assume CCIR-709 primaries */ + r = 2.690 * xyz[0] + -1.276 * xyz[1] + -0.414 * xyz[2]; + g = -1.022 * xyz[0] + 1.978 * xyz[1] + 0.044 * xyz[2]; + b = 0.061 * xyz[0] + -0.224 * xyz[1] + 1.163 * xyz[2]; + /* assume 2.0 gamma for speed */ + /* could use integer sqrt approx., but this is probably faster */ + rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r))); + rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g))); + rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b))); +} + +#if !LOGLUV_PUBLIC +static +#endif + double + LogL10toY(int p10) /* compute luminance from 10-bit LogL */ +{ + if (p10 == 0) + return (0.); + return (exp(M_LN2 / 64. * (p10 + .5) - M_LN2 * 12.)); +} + +#if !LOGLUV_PUBLIC +static +#endif + int + LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */ +{ + if (Y >= 15.742) + return (0x3ff); + else if (Y <= .00024283) + return (0); + else + return tiff_itrunc(64. * (log2(Y) + 12.), em); +} + +#define NANGLES 100 +#define uv2ang(u, v) \ + ((NANGLES * .499999999 / M_PI) * atan2((v)-V_NEU, (u)-U_NEU) + .5 * NANGLES) + +static int oog_encode(double u, double v) /* encode out-of-gamut chroma */ +{ + static int oog_table[NANGLES]; + static int initialized = 0; + register int i; + + if (!initialized) + { /* set up perimeter table */ + double eps[NANGLES], ua, va, ang, epsa; + int ui, vi, ustep; + for (i = NANGLES; i--;) + eps[i] = 2.; + for (vi = UV_NVS; vi--;) + { + va = UV_VSTART + (vi + .5) * UV_SQSIZ; + ustep = uv_row[vi].nus - 1; + if (vi == UV_NVS - 1 || vi == 0 || ustep <= 0) + ustep = 1; + for (ui = uv_row[vi].nus - 1; ui >= 0; ui -= ustep) + { + ua = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ; + ang = uv2ang(ua, va); + i = (int)ang; + epsa = fabs(ang - (i + .5)); + if (epsa < eps[i]) + { + oog_table[i] = uv_row[vi].ncum + ui; + eps[i] = epsa; + } + } + } + for (i = NANGLES; i--;) /* fill any holes */ + if (eps[i] > 1.5) + { + int i1, i2; + for (i1 = 1; i1 < NANGLES / 2; i1++) + if (eps[(i + i1) % NANGLES] < 1.5) + break; + for (i2 = 1; i2 < NANGLES / 2; i2++) + if (eps[(i + NANGLES - i2) % NANGLES] < 1.5) + break; + if (i1 < i2) + oog_table[i] = oog_table[(i + i1) % NANGLES]; + else + oog_table[i] = oog_table[(i + NANGLES - i2) % NANGLES]; + } + initialized = 1; + } + i = (int)uv2ang(u, v); /* look up hue angle */ + return (oog_table[i]); +} + +#undef uv2ang +#undef NANGLES + +#if !LOGLUV_PUBLIC +static +#endif + int + uv_encode(double u, double v, int em) /* encode (u',v') coordinates */ +{ + unsigned int vi; + int ui; + + /* check for NaN */ + if (u != u || v != v) + { + u = U_NEU; + v = V_NEU; + } + + if (v < UV_VSTART) + return oog_encode(u, v); + vi = tiff_itrunc((v - UV_VSTART) * (1. / UV_SQSIZ), em); + if (vi >= UV_NVS) + return oog_encode(u, v); + if (u < uv_row[vi].ustart) + return oog_encode(u, v); + ui = tiff_itrunc((u - uv_row[vi].ustart) * (1. / UV_SQSIZ), em); + if (ui >= uv_row[vi].nus) + return oog_encode(u, v); + + return (uv_row[vi].ncum + ui); +} + +#if !LOGLUV_PUBLIC +static +#endif + int + uv_decode(double *up, double *vp, int c) /* decode (u',v') index */ +{ + unsigned int upper, lower; + int ui; + unsigned int vi; + + if (c < 0 || c >= UV_NDIVS) + return (-1); + lower = 0; /* binary search */ + upper = UV_NVS; + while (upper - lower > 1) + { + vi = (lower + upper) >> 1; + ui = c - uv_row[vi].ncum; + if (ui > 0) + lower = vi; + else if (ui < 0) + upper = vi; + else + { + lower = vi; + break; + } + } + vi = lower; + ui = c - uv_row[vi].ncum; + *up = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ; + *vp = UV_VSTART + (vi + .5) * UV_SQSIZ; + return (0); +} + +#if !LOGLUV_PUBLIC +static +#endif + void + LogLuv24toXYZ(uint32_t p, float *XYZ) +{ + int Ce; + double L, u, v, s, x, y; + /* decode luminance */ + L = LogL10toY(p >> 14 & 0x3ff); + if (L <= 0.) + { + XYZ[0] = XYZ[1] = XYZ[2] = 0.; + return; + } + /* decode color */ + Ce = p & 0x3fff; + if (uv_decode(&u, &v, Ce) < 0) + { + u = U_NEU; + v = V_NEU; + } + s = 1. / (6. * u - 16. * v + 12.); + x = 9. * u * s; + y = 4. * v * s; + /* convert to XYZ */ + XYZ[0] = (float)(x / y * L); + XYZ[1] = (float)L; + XYZ[2] = (float)((1. - x - y) / y * L); +} + +#if !LOGLUV_PUBLIC +static +#endif + uint32_t + LogLuv24fromXYZ(float *XYZ, int em) +{ + int Le, Ce; + double u, v, s; + /* encode luminance */ + Le = LogL10fromY(XYZ[1], em); + /* encode color */ + s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2]; + if (!Le || s <= 0.) + { + u = U_NEU; + v = V_NEU; + } + else + { + u = 4. * XYZ[0] / s; + v = 9. * XYZ[1] / s; + } + Ce = uv_encode(u, v, em); + if (Ce < 0) /* never happens */ + Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); + /* combine encodings */ + return (Le << 14 | Ce); +} + +static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + float *xyz = (float *)op; + + while (n-- > 0) + { + LogLuv24toXYZ(*luv, xyz); + xyz += 3; + luv++; + } +} + +static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + int16_t *luv3 = (int16_t *)op; + + while (n-- > 0) + { + double u, v; + + *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314); + if (uv_decode(&u, &v, *luv & 0x3fff) < 0) + { + u = U_NEU; + v = V_NEU; + } + *luv3++ = (int16_t)(u * (1L << 15)); + *luv3++ = (int16_t)(v * (1L << 15)); + luv++; + } +} + +static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + uint8_t *rgb = (uint8_t *)op; + + while (n-- > 0) + { + float xyz[3]; + + LogLuv24toXYZ(*luv++, xyz); + XYZtoRGB24(xyz, rgb); + rgb += 3; + } +} + +static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + float *xyz = (float *)op; + + while (n-- > 0) + { + *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth); + xyz += 3; + } +} + +static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + int16_t *luv3 = (int16_t *)op; + + while (n-- > 0) + { + int Le, Ce; + + if (luv3[0] <= 0) + Le = 0; + else if (luv3[0] >= (1 << 12) + 3314) + Le = (1 << 10) - 1; + else if (sp->encode_meth == SGILOGENCODE_NODITHER) + Le = (luv3[0] - 3314) >> 2; + else + Le = tiff_itrunc(.25 * (luv3[0] - 3314.), sp->encode_meth); + + Ce = uv_encode((luv3[1] + .5) / (1 << 15), (luv3[2] + .5) / (1 << 15), + sp->encode_meth); + if (Ce < 0) /* never happens */ + Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); + *luv++ = (uint32_t)Le << 14 | Ce; + luv3 += 3; + } +} + +#if !LOGLUV_PUBLIC +static +#endif + void + LogLuv32toXYZ(uint32_t p, float *XYZ) +{ + double L, u, v, s, x, y; + /* decode luminance */ + L = LogL16toY((int)p >> 16); + if (L <= 0.) + { + XYZ[0] = XYZ[1] = XYZ[2] = 0.; + return; + } + /* decode color */ + u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5); + v = 1. / UVSCALE * ((p & 0xff) + .5); + s = 1. / (6. * u - 16. * v + 12.); + x = 9. * u * s; + y = 4. * v * s; + /* convert to XYZ */ + XYZ[0] = (float)(x / y * L); + XYZ[1] = (float)L; + XYZ[2] = (float)((1. - x - y) / y * L); +} + +#if !LOGLUV_PUBLIC +static +#endif + uint32_t + LogLuv32fromXYZ(float *XYZ, int em) +{ + unsigned int Le, ue, ve; + double u, v, s; + /* encode luminance */ + Le = (unsigned int)LogL16fromY(XYZ[1], em); + /* encode color */ + s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2]; + if (!Le || s <= 0.) + { + u = U_NEU; + v = V_NEU; + } + else + { + u = 4. * XYZ[0] / s; + v = 9. * XYZ[1] / s; + } + if (u <= 0.) + ue = 0; + else + ue = tiff_itrunc(UVSCALE * u, em); + if (ue > 255) + ue = 255; + if (v <= 0.) + ve = 0; + else + ve = tiff_itrunc(UVSCALE * v, em); + if (ve > 255) + ve = 255; + /* combine encodings */ + return (Le << 16 | ue << 8 | ve); +} + +static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + float *xyz = (float *)op; + + while (n-- > 0) + { + LogLuv32toXYZ(*luv++, xyz); + xyz += 3; + } +} + +static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + int16_t *luv3 = (int16_t *)op; + + while (n-- > 0) + { + double u, v; + + *luv3++ = (int16_t)(*luv >> 16); + u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5); + v = 1. / UVSCALE * ((*luv & 0xff) + .5); + *luv3++ = (int16_t)(u * (1L << 15)); + *luv3++ = (int16_t)(v * (1L << 15)); + luv++; + } +} + +static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + uint8_t *rgb = (uint8_t *)op; + + while (n-- > 0) + { + float xyz[3]; + + LogLuv32toXYZ(*luv++, xyz); + XYZtoRGB24(xyz, rgb); + rgb += 3; + } +} + +static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + float *xyz = (float *)op; + + while (n-- > 0) + { + *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth); + xyz += 3; + } +} + +static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + uint32_t *luv = (uint32_t *)sp->tbuf; + int16_t *luv3 = (int16_t *)op; + + if (sp->encode_meth == SGILOGENCODE_NODITHER) + { + while (n-- > 0) + { + *luv++ = (uint32_t)luv3[0] << 16 | + (luv3[1] * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) | + (luv3[2] * (uint32_t)(UVSCALE + .5) >> 15 & 0xff); + luv3 += 3; + } + return; + } + while (n-- > 0) + { + *luv++ = + (uint32_t)luv3[0] << 16 | + (tiff_itrunc(luv3[1] * (UVSCALE / (1 << 15)), sp->encode_meth) + << 8 & + 0xff00) | + (tiff_itrunc(luv3[2] * (UVSCALE / (1 << 15)), sp->encode_meth) & + 0xff); + luv3 += 3; + } +} + +static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n) +{ + (void)sp; + (void)op; + (void)n; +} + +static int LogL16GuessDataFmt(TIFFDirectory *td) +{ +#define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f)) + switch ( + PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat)) + { + case PACK(1, 32, SAMPLEFORMAT_IEEEFP): + return (SGILOGDATAFMT_FLOAT); + case PACK(1, 16, SAMPLEFORMAT_VOID): + case PACK(1, 16, SAMPLEFORMAT_INT): + case PACK(1, 16, SAMPLEFORMAT_UINT): + return (SGILOGDATAFMT_16BIT); + case PACK(1, 8, SAMPLEFORMAT_VOID): + case PACK(1, 8, SAMPLEFORMAT_UINT): + return (SGILOGDATAFMT_8BIT); + } +#undef PACK + return (SGILOGDATAFMT_UNKNOWN); +} + +static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2) +{ + return _TIFFMultiplySSize(NULL, m1, m2, NULL); +} + +static int LogL16InitState(TIFF *tif) +{ + static const char module[] = "LogL16InitState"; + TIFFDirectory *td = &tif->tif_dir; + LogLuvState *sp = DecoderState(tif); + + assert(sp != NULL); + assert(td->td_photometric == PHOTOMETRIC_LOGL); + + if (td->td_samplesperpixel != 1) + { + TIFFErrorExtR(tif, module, + "Sorry, can not handle LogL image with %s=%" PRIu16, + "Samples/pixel", td->td_samplesperpixel); + return 0; + } + + /* for some reason, we can't do this in TIFFInitLogL16 */ + if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN) + sp->user_datafmt = LogL16GuessDataFmt(td); + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->pixel_size = sizeof(float); + break; + case SGILOGDATAFMT_16BIT: + sp->pixel_size = sizeof(int16_t); + break; + case SGILOGDATAFMT_8BIT: + sp->pixel_size = sizeof(uint8_t); + break; + default: + TIFFErrorExtR(tif, module, + "No support for converting user data format to LogL"); + return (0); + } + if (isTiled(tif)) + sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength); + else if (td->td_rowsperstrip < td->td_imagelength) + sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip); + else + sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength); + if (multiply_ms(sp->tbuflen, sizeof(int16_t)) == 0 || + (sp->tbuf = (uint8_t *)_TIFFmallocExt( + tif, sp->tbuflen * sizeof(int16_t))) == NULL) + { + TIFFErrorExtR(tif, module, "No space for SGILog translation buffer"); + return (0); + } + return (1); +} + +static int LogLuvGuessDataFmt(TIFFDirectory *td) +{ + int guess; + + /* + * If the user didn't tell us their datafmt, + * take our best guess from the bitspersample. + */ +#define PACK(a, b) (((a) << 3) | (b)) + switch (PACK(td->td_bitspersample, td->td_sampleformat)) + { + case PACK(32, SAMPLEFORMAT_IEEEFP): + guess = SGILOGDATAFMT_FLOAT; + break; + case PACK(32, SAMPLEFORMAT_VOID): + case PACK(32, SAMPLEFORMAT_UINT): + case PACK(32, SAMPLEFORMAT_INT): + guess = SGILOGDATAFMT_RAW; + break; + case PACK(16, SAMPLEFORMAT_VOID): + case PACK(16, SAMPLEFORMAT_INT): + case PACK(16, SAMPLEFORMAT_UINT): + guess = SGILOGDATAFMT_16BIT; + break; + case PACK(8, SAMPLEFORMAT_VOID): + case PACK(8, SAMPLEFORMAT_UINT): + guess = SGILOGDATAFMT_8BIT; + break; + default: + guess = SGILOGDATAFMT_UNKNOWN; + break; +#undef PACK + } + /* + * Double-check samples per pixel. + */ + switch (td->td_samplesperpixel) + { + case 1: + if (guess != SGILOGDATAFMT_RAW) + guess = SGILOGDATAFMT_UNKNOWN; + break; + case 3: + if (guess == SGILOGDATAFMT_RAW) + guess = SGILOGDATAFMT_UNKNOWN; + break; + default: + guess = SGILOGDATAFMT_UNKNOWN; + break; + } + return (guess); +} + +static int LogLuvInitState(TIFF *tif) +{ + static const char module[] = "LogLuvInitState"; + TIFFDirectory *td = &tif->tif_dir; + LogLuvState *sp = DecoderState(tif); + + assert(sp != NULL); + assert(td->td_photometric == PHOTOMETRIC_LOGLUV); + + /* for some reason, we can't do this in TIFFInitLogLuv */ + if (td->td_planarconfig != PLANARCONFIG_CONTIG) + { + TIFFErrorExtR(tif, module, + "SGILog compression cannot handle non-contiguous data"); + return (0); + } + if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN) + sp->user_datafmt = LogLuvGuessDataFmt(td); + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->pixel_size = 3 * sizeof(float); + break; + case SGILOGDATAFMT_16BIT: + sp->pixel_size = 3 * sizeof(int16_t); + break; + case SGILOGDATAFMT_RAW: + sp->pixel_size = sizeof(uint32_t); + break; + case SGILOGDATAFMT_8BIT: + sp->pixel_size = 3 * sizeof(uint8_t); + break; + default: + TIFFErrorExtR( + tif, module, + "No support for converting user data format to LogLuv"); + return (0); + } + if (isTiled(tif)) + sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength); + else if (td->td_rowsperstrip < td->td_imagelength) + sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip); + else + sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength); + if (multiply_ms(sp->tbuflen, sizeof(uint32_t)) == 0 || + (sp->tbuf = (uint8_t *)_TIFFmallocExt( + tif, sp->tbuflen * sizeof(uint32_t))) == NULL) + { + TIFFErrorExtR(tif, module, "No space for SGILog translation buffer"); + return (0); + } + return (1); +} + +static int LogLuvFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +static int LogLuvSetupDecode(TIFF *tif) +{ + static const char module[] = "LogLuvSetupDecode"; + LogLuvState *sp = DecoderState(tif); + TIFFDirectory *td = &tif->tif_dir; + + tif->tif_postdecode = _TIFFNoPostDecode; + switch (td->td_photometric) + { + case PHOTOMETRIC_LOGLUV: + if (!LogLuvInitState(tif)) + break; + if (td->td_compression == COMPRESSION_SGILOG24) + { + tif->tif_decoderow = LogLuvDecode24; + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->tfunc = Luv24toXYZ; + break; + case SGILOGDATAFMT_16BIT: + sp->tfunc = Luv24toLuv48; + break; + case SGILOGDATAFMT_8BIT: + sp->tfunc = Luv24toRGB; + break; + } + } + else + { + tif->tif_decoderow = LogLuvDecode32; + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->tfunc = Luv32toXYZ; + break; + case SGILOGDATAFMT_16BIT: + sp->tfunc = Luv32toLuv48; + break; + case SGILOGDATAFMT_8BIT: + sp->tfunc = Luv32toRGB; + break; + } + } + return (1); + case PHOTOMETRIC_LOGL: + if (!LogL16InitState(tif)) + break; + tif->tif_decoderow = LogL16Decode; + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->tfunc = L16toY; + break; + case SGILOGDATAFMT_8BIT: + sp->tfunc = L16toGry; + break; + } + return (1); + default: + TIFFErrorExtR(tif, module, + "Inappropriate photometric interpretation %" PRIu16 + " for SGILog compression; %s", + td->td_photometric, "must be either LogLUV or LogL"); + break; + } + return (0); +} + +static int LogLuvSetupEncode(TIFF *tif) +{ + static const char module[] = "LogLuvSetupEncode"; + LogLuvState *sp = EncoderState(tif); + TIFFDirectory *td = &tif->tif_dir; + + switch (td->td_photometric) + { + case PHOTOMETRIC_LOGLUV: + if (!LogLuvInitState(tif)) + return (0); + if (td->td_compression == COMPRESSION_SGILOG24) + { + tif->tif_encoderow = LogLuvEncode24; + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->tfunc = Luv24fromXYZ; + break; + case SGILOGDATAFMT_16BIT: + sp->tfunc = Luv24fromLuv48; + break; + case SGILOGDATAFMT_RAW: + break; + default: + goto notsupported; + } + } + else + { + tif->tif_encoderow = LogLuvEncode32; + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->tfunc = Luv32fromXYZ; + break; + case SGILOGDATAFMT_16BIT: + sp->tfunc = Luv32fromLuv48; + break; + case SGILOGDATAFMT_RAW: + break; + default: + goto notsupported; + } + } + break; + case PHOTOMETRIC_LOGL: + if (!LogL16InitState(tif)) + return (0); + tif->tif_encoderow = LogL16Encode; + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + sp->tfunc = L16fromY; + break; + case SGILOGDATAFMT_16BIT: + break; + default: + goto notsupported; + } + break; + default: + TIFFErrorExtR(tif, module, + "Inappropriate photometric interpretation %" PRIu16 + " for SGILog compression; %s", + td->td_photometric, "must be either LogLUV or LogL"); + return (0); + } + sp->encoder_state = 1; + return (1); +notsupported: + TIFFErrorExtR(tif, module, + "SGILog compression supported only for %s, or raw data", + td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv"); + return (0); +} + +static void LogLuvClose(TIFF *tif) +{ + LogLuvState *sp = (LogLuvState *)tif->tif_data; + TIFFDirectory *td = &tif->tif_dir; + + assert(sp != 0); + /* + * For consistency, we always want to write out the same + * bitspersample and sampleformat for our TIFF file, + * regardless of the data format being used by the application. + * Since this routine is called after tags have been set but + * before they have been recorded in the file, we reset them here. + * Note: this is really a nasty approach. See PixarLogClose + */ + if (sp->encoder_state) + { + /* See PixarLogClose. Might avoid issues with tags whose size depends + * on those below, but not completely sure this is enough. */ + td->td_samplesperpixel = + (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3; + td->td_bitspersample = 16; + td->td_sampleformat = SAMPLEFORMAT_INT; + } +} + +static void LogLuvCleanup(TIFF *tif) +{ + LogLuvState *sp = (LogLuvState *)tif->tif_data; + + assert(sp != 0); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + if (sp->tbuf) + _TIFFfreeExt(tif, sp->tbuf); + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "LogLuvVSetField"; + LogLuvState *sp = DecoderState(tif); + int bps, fmt; + + switch (tag) + { + case TIFFTAG_SGILOGDATAFMT: + sp->user_datafmt = (int)va_arg(ap, int); + /* + * Tweak the TIFF header so that the rest of libtiff knows what + * size of data will be passed between app and library, and + * assume that the app knows what it is doing and is not + * confused by these header manipulations... + */ + switch (sp->user_datafmt) + { + case SGILOGDATAFMT_FLOAT: + bps = 32; + fmt = SAMPLEFORMAT_IEEEFP; + break; + case SGILOGDATAFMT_16BIT: + bps = 16; + fmt = SAMPLEFORMAT_INT; + break; + case SGILOGDATAFMT_RAW: + bps = 32; + fmt = SAMPLEFORMAT_UINT; + TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); + break; + case SGILOGDATAFMT_8BIT: + bps = 8; + fmt = SAMPLEFORMAT_UINT; + break; + default: + TIFFErrorExtR( + tif, tif->tif_name, + "Unknown data format %d for LogLuv compression", + sp->user_datafmt); + return (0); + } + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); + TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt); + /* + * Must recalculate sizes should bits/sample change. + */ + tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1; + tif->tif_scanlinesize = TIFFScanlineSize(tif); + return (1); + case TIFFTAG_SGILOGENCODE: + sp->encode_meth = (int)va_arg(ap, int); + if (sp->encode_meth != SGILOGENCODE_NODITHER && + sp->encode_meth != SGILOGENCODE_RANDITHER) + { + TIFFErrorExtR(tif, module, + "Unknown encoding %d for LogLuv compression", + sp->encode_meth); + return (0); + } + return (1); + default: + return (*sp->vsetparent)(tif, tag, ap); + } +} + +static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + LogLuvState *sp = (LogLuvState *)tif->tif_data; + + switch (tag) + { + case TIFFTAG_SGILOGDATAFMT: + *va_arg(ap, int *) = sp->user_datafmt; + return (1); + default: + return (*sp->vgetparent)(tif, tag, ap); + } +} + +static const TIFFField LogLuvFields[] = { + {TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + TRUE, FALSE, "SGILogDataFmt", NULL}, + {TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + TRUE, FALSE, "SGILogEncode", NULL}}; + +int TIFFInitSGILog(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitSGILog"; + LogLuvState *sp; + + assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, LogLuvFields, TIFFArrayCount(LogLuvFields))) + { + TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState)); + if (tif->tif_data == NULL) + goto bad; + sp = (LogLuvState *)tif->tif_data; + _TIFFmemset((void *)sp, 0, sizeof(*sp)); + sp->user_datafmt = SGILOGDATAFMT_UNKNOWN; + sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER + : SGILOGENCODE_NODITHER; + sp->tfunc = _logLuvNop; + + /* + * Install codec methods. + * NB: tif_decoderow & tif_encoderow are filled + * in at setup time. + */ + tif->tif_fixuptags = LogLuvFixupTags; + tif->tif_setupdecode = LogLuvSetupDecode; + tif->tif_decodestrip = LogLuvDecodeStrip; + tif->tif_decodetile = LogLuvDecodeTile; + tif->tif_setupencode = LogLuvSetupEncode; + tif->tif_encodestrip = LogLuvEncodeStrip; + tif->tif_encodetile = LogLuvEncodeTile; + tif->tif_close = LogLuvClose; + tif->tif_cleanup = LogLuvCleanup; + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */ + + return (1); +bad: + TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block", + tif->tif_name); + return (0); +} +#endif /* LOGLUV_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_lzma.c b/cpp/3rd_party/libtiff/tif_lzma.c new file mode 100644 index 0000000000..9834ae2c2c --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_lzma.c @@ -0,0 +1,541 @@ +/* + * Copyright (c) 2010, Andrey Kiselev + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef LZMA_SUPPORT +/* + * TIFF Library. + * + * LZMA2 Compression Support + * + * You need an LZMA2 SDK to link with. See http://tukaani.org/xz/ for details. + * + * The codec is derived from ZLIB codec (tif_zip.c). + */ + +#include "lzma.h" +#include "tif_predict.h" + +#include + +/* + * State block for each open TIFF file using LZMA2 compression/decompression. + */ +typedef struct +{ + TIFFPredictorState predict; + int read_error; /* whether a read error has occurred, and which should cause + further reads in the same strip/tile to be aborted */ + lzma_stream stream; + lzma_filter filters[LZMA_FILTERS_MAX + 1]; + lzma_options_delta opt_delta; /* delta filter options */ + lzma_options_lzma opt_lzma; /* LZMA2 filter options */ + int preset; /* compression level */ + lzma_check check; /* type of the integrity check */ + int state; /* state flags */ +#define LSTATE_INIT_DECODE 0x01 +#define LSTATE_INIT_ENCODE 0x02 + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ +} LZMAState; + +#define GetLZMAState(tif) ((LZMAState *)(tif)->tif_data) +#define LZMADecoderState(tif) GetLZMAState(tif) +#define LZMAEncoderState(tif) GetLZMAState(tif) + +static int LZMAEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); +static int LZMADecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); + +static const char *LZMAStrerror(lzma_ret ret) +{ + switch (ret) + { + case LZMA_OK: + return "operation completed successfully"; + case LZMA_STREAM_END: + return "end of stream was reached"; + case LZMA_NO_CHECK: + return "input stream has no integrity check"; + case LZMA_UNSUPPORTED_CHECK: + return "cannot calculate the integrity check"; + case LZMA_GET_CHECK: + return "integrity check type is now available"; + case LZMA_MEM_ERROR: + return "cannot allocate memory"; + case LZMA_MEMLIMIT_ERROR: + return "memory usage limit was reached"; + case LZMA_FORMAT_ERROR: + return "file format not recognized"; + case LZMA_OPTIONS_ERROR: + return "invalid or unsupported options"; + case LZMA_DATA_ERROR: + return "data is corrupt"; + case LZMA_BUF_ERROR: + return "no progress is possible (stream is truncated or corrupt)"; + case LZMA_PROG_ERROR: + return "programming error"; + default: + return "unidentified liblzma error"; + } +} + +static int LZMAFixupTags(TIFF *tif) +{ + (void)tif; + return 1; +} + +static int LZMASetupDecode(TIFF *tif) +{ + LZMAState *sp = LZMADecoderState(tif); + + assert(sp != NULL); + + /* if we were last encoding, terminate this mode */ + if (sp->state & LSTATE_INIT_ENCODE) + { + lzma_end(&sp->stream); + sp->state = 0; + } + + sp->state |= LSTATE_INIT_DECODE; + return 1; +} + +/* + * Setup state for decoding a strip. + */ +static int LZMAPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "LZMAPreDecode"; + LZMAState *sp = LZMADecoderState(tif); + lzma_ret ret; + + (void)s; + assert(sp != NULL); + + if ((sp->state & LSTATE_INIT_DECODE) == 0) + tif->tif_setupdecode(tif); + + sp->stream.next_in = tif->tif_rawdata; + sp->stream.avail_in = (size_t)tif->tif_rawcc; + if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc) + { + TIFFErrorExtR(tif, module, + "Liblzma cannot deal with buffers this size"); + return 0; + } + + /* + * Disable memory limit when decoding. UINT64_MAX is a flag to disable + * the limit, we are passing (uint64_t)-1 which should be the same. + */ + ret = lzma_stream_decoder(&sp->stream, (uint64_t)-1, 0); + if (ret != LZMA_OK) + { + TIFFErrorExtR(tif, module, "Error initializing the stream decoder, %s", + LZMAStrerror(ret)); + return 0; + } + + sp->read_error = 0; + + return 1; +} + +static int LZMADecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "LZMADecode"; + LZMAState *sp = LZMADecoderState(tif); + + (void)s; + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_DECODE); + + if (sp->read_error) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, + "LZMADecode: Scanline %" PRIu32 " cannot be read due to " + "previous error", + tif->tif_row); + return 0; + } + + sp->stream.next_in = tif->tif_rawcp; + sp->stream.avail_in = (size_t)tif->tif_rawcc; + + sp->stream.next_out = op; + sp->stream.avail_out = (size_t)occ; + if ((tmsize_t)sp->stream.avail_out != occ) + { + // read_error not set here as this is a usage issue that can be + // recovered in a following call. + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, + "Liblzma cannot deal with buffers this size"); + return 0; + } + + do + { + /* + * Save the current stream state to properly recover from the + * decoding errors later. + */ + const uint8_t *next_in = sp->stream.next_in; + size_t avail_in = sp->stream.avail_in; + + lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN); + if (ret == LZMA_STREAM_END) + break; + if (ret == LZMA_MEMLIMIT_ERROR) + { + lzma_ret r = + lzma_stream_decoder(&sp->stream, lzma_memusage(&sp->stream), 0); + if (r != LZMA_OK) + { + sp->read_error = 1; + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, + "Error initializing the stream decoder, %s", + LZMAStrerror(r)); + break; + } + sp->stream.next_in = next_in; + sp->stream.avail_in = avail_in; + continue; + } + if (ret != LZMA_OK) + { + TIFFErrorExtR(tif, module, + "Decoding error at scanline %" PRIu32 ", %s", + tif->tif_row, LZMAStrerror(ret)); + break; + } + } while (sp->stream.avail_out > 0); + if (sp->stream.avail_out != 0) + { + sp->read_error = 1; + memset(sp->stream.next_out, 0, sp->stream.avail_out); + TIFFErrorExtR(tif, module, + "Not enough data at scanline %" PRIu32 + " (short %" TIFF_SIZE_FORMAT " bytes)", + tif->tif_row, sp->stream.avail_out); + return 0; + } + + tif->tif_rawcp = (uint8_t *)sp->stream.next_in; /* cast away const */ + tif->tif_rawcc = sp->stream.avail_in; + + return 1; +} + +static int LZMASetupEncode(TIFF *tif) +{ + LZMAState *sp = LZMAEncoderState(tif); + + assert(sp != NULL); + if (sp->state & LSTATE_INIT_DECODE) + { + lzma_end(&sp->stream); + sp->state = 0; + } + + sp->state |= LSTATE_INIT_ENCODE; + return 1; +} + +/* + * Reset encoding state at the start of a strip. + */ +static int LZMAPreEncode(TIFF *tif, uint16_t s) +{ + static const char module[] = "LZMAPreEncode"; + LZMAState *sp = LZMAEncoderState(tif); + lzma_ret ret; + + (void)s; + assert(sp != NULL); + if (sp->state != LSTATE_INIT_ENCODE) + tif->tif_setupencode(tif); + + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = (size_t)tif->tif_rawdatasize; + if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) + { + TIFFErrorExtR(tif, module, + "Liblzma cannot deal with buffers this size"); + return 0; + } + ret = lzma_stream_encoder(&sp->stream, sp->filters, sp->check); + if (ret != LZMA_OK) + { + TIFFErrorExtR(tif, module, "Error in lzma_stream_encoder(): %s", + LZMAStrerror(ret)); + return 0; + } + return 1; +} + +/* + * Encode a chunk of pixels. + */ +static int LZMAEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "LZMAEncode"; + LZMAState *sp = LZMAEncoderState(tif); + + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_ENCODE); + + (void)s; + sp->stream.next_in = bp; + sp->stream.avail_in = (size_t)cc; + if ((tmsize_t)sp->stream.avail_in != cc) + { + TIFFErrorExtR(tif, module, + "Liblzma cannot deal with buffers this size"); + return 0; + } + do + { + lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN); + if (ret != LZMA_OK) + { + TIFFErrorExtR(tif, module, + "Encoding error at scanline %" PRIu32 ", %s", + tif->tif_row, LZMAStrerror(ret)); + return 0; + } + if (sp->stream.avail_out == 0) + { + tif->tif_rawcc = tif->tif_rawdatasize; + if (!TIFFFlushData1(tif)) + return 0; + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = + (size_t) + tif->tif_rawdatasize; /* this is a safe typecast, as check + is made already in LZMAPreEncode */ + } + } while (sp->stream.avail_in > 0); + return 1; +} + +/* + * Finish off an encoded strip by flushing the last + * string and tacking on an End Of Information code. + */ +static int LZMAPostEncode(TIFF *tif) +{ + static const char module[] = "LZMAPostEncode"; + LZMAState *sp = LZMAEncoderState(tif); + lzma_ret ret; + + sp->stream.avail_in = 0; + do + { + ret = lzma_code(&sp->stream, LZMA_FINISH); + switch (ret) + { + case LZMA_STREAM_END: + case LZMA_OK: + if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) + { + tif->tif_rawcc = + tif->tif_rawdatasize - sp->stream.avail_out; + if (!TIFFFlushData1(tif)) + return 0; + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = + (size_t) + tif->tif_rawdatasize; /* this is a safe typecast, as + check is made already in + ZIPPreEncode */ + } + break; + default: + TIFFErrorExtR(tif, module, "Liblzma error: %s", + LZMAStrerror(ret)); + return 0; + } + } while (ret != LZMA_STREAM_END); + return 1; +} + +static void LZMACleanup(TIFF *tif) +{ + LZMAState *sp = GetLZMAState(tif); + + assert(sp != 0); + + (void)TIFFPredictorCleanup(tif); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + if (sp->state) + { + lzma_end(&sp->stream); + sp->state = 0; + } + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static int LZMAVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "LZMAVSetField"; + LZMAState *sp = GetLZMAState(tif); + + switch (tag) + { + case TIFFTAG_LZMAPRESET: + sp->preset = (int)va_arg(ap, int); + lzma_lzma_preset(&sp->opt_lzma, sp->preset); + if (sp->state & LSTATE_INIT_ENCODE) + { + lzma_ret ret = + lzma_stream_encoder(&sp->stream, sp->filters, sp->check); + if (ret != LZMA_OK) + { + TIFFErrorExtR(tif, module, "Liblzma error: %s", + LZMAStrerror(ret)); + } + } + return 1; + default: + return (*sp->vsetparent)(tif, tag, ap); + } + /*NOTREACHED*/ +} + +static int LZMAVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + LZMAState *sp = GetLZMAState(tif); + + switch (tag) + { + case TIFFTAG_LZMAPRESET: + *va_arg(ap, int *) = sp->preset; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return 1; +} + +static const TIFFField lzmaFields[] = { + {TIFFTAG_LZMAPRESET, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE, + FALSE, "LZMA2 Compression Preset", NULL}, +}; + +int TIFFInitLZMA(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitLZMA"; + LZMAState *sp; + lzma_stream tmp_stream = LZMA_STREAM_INIT; + + (void)scheme; + assert(scheme == COMPRESSION_LZMA); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, lzmaFields, TIFFArrayCount(lzmaFields))) + { + TIFFErrorExtR(tif, module, "Merging LZMA2 codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZMAState)); + if (tif->tif_data == NULL) + goto bad; + sp = GetLZMAState(tif); + memcpy(&sp->stream, &tmp_stream, sizeof(lzma_stream)); + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = LZMAVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = LZMAVSetField; /* hook for codec tags */ + + /* Default values for codec-specific fields */ + sp->preset = LZMA_PRESET_DEFAULT; /* default comp. level */ + sp->check = LZMA_CHECK_NONE; + sp->state = 0; + + /* Data filters. So far we are using delta and LZMA2 filters only. */ + sp->opt_delta.type = LZMA_DELTA_TYPE_BYTE; + /* + * The sample size in bytes seems to be reasonable distance for delta + * filter. + */ + sp->opt_delta.dist = (tif->tif_dir.td_bitspersample % 8) + ? 1 + : tif->tif_dir.td_bitspersample / 8; + sp->filters[0].id = LZMA_FILTER_DELTA; + sp->filters[0].options = &sp->opt_delta; + + lzma_lzma_preset(&sp->opt_lzma, sp->preset); + sp->filters[1].id = LZMA_FILTER_LZMA2; + sp->filters[1].options = &sp->opt_lzma; + + sp->filters[2].id = LZMA_VLI_UNKNOWN; + sp->filters[2].options = NULL; + + /* + * Install codec methods. + */ + tif->tif_fixuptags = LZMAFixupTags; + tif->tif_setupdecode = LZMASetupDecode; + tif->tif_predecode = LZMAPreDecode; + tif->tif_decoderow = LZMADecode; + tif->tif_decodestrip = LZMADecode; + tif->tif_decodetile = LZMADecode; + tif->tif_setupencode = LZMASetupEncode; + tif->tif_preencode = LZMAPreEncode; + tif->tif_postencode = LZMAPostEncode; + tif->tif_encoderow = LZMAEncode; + tif->tif_encodestrip = LZMAEncode; + tif->tif_encodetile = LZMAEncode; + tif->tif_cleanup = LZMACleanup; + /* + * Setup predictor setup. + */ + (void)TIFFPredictorInit(tif); + return 1; +bad: + TIFFErrorExtR(tif, module, "No space for LZMA2 state block"); + return 0; +} +#endif /* LZMA_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_lzw.c b/cpp/3rd_party/libtiff/tif_lzw.c new file mode 100644 index 0000000000..4bf845b393 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_lzw.c @@ -0,0 +1,1464 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * Copyright (c) 2022 Even Rouault + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef LZW_SUPPORT +/* + * TIFF Library. + * Rev 5.0 Lempel-Ziv & Welch Compression Support + * + * This code is derived from the compress program whose code is + * derived from software contributed to Berkeley by James A. Woods, + * derived from original work by Spencer Thomas and Joseph Orost. + * + * The original Berkeley copyright notice appears below in its entirety. + */ +#include "tif_predict.h" + +#include +#include +#include + +/* Select the plausible largest natural integer type for the architecture */ +#define SIZEOF_WORDTYPE SIZEOF_SIZE_T +typedef size_t WordType; + +/* + * NB: The 5.0 spec describes a different algorithm than Aldus + * implements. Specifically, Aldus does code length transitions + * one code earlier than should be done (for real LZW). + * Earlier versions of this library implemented the correct + * LZW algorithm, but emitted codes in a bit order opposite + * to the TIFF spec. Thus, to maintain compatibility w/ Aldus + * we interpret MSB-LSB ordered codes to be images written w/ + * old versions of this library, but otherwise adhere to the + * Aldus "off by one" algorithm. + * + * Future revisions to the TIFF spec are expected to "clarify this issue". + */ +#define LZW_COMPAT /* include backwards compatibility code */ + +#define MAXCODE(n) ((1L << (n)) - 1) +/* + * The TIFF spec specifies that encoded bit + * strings range from 9 to 12 bits. + */ +#define BITS_MIN 9 /* start with 9 bits */ +#define BITS_MAX 12 /* max of 12 bit strings */ +/* predefined codes */ +#define CODE_CLEAR 256 /* code to clear string table */ +#define CODE_EOI 257 /* end-of-information code */ +#define CODE_FIRST 258 /* first free code entry */ +#define CODE_MAX MAXCODE(BITS_MAX) +#define HSIZE 9001L /* 91% occupancy */ +#define HSHIFT (13 - 8) +#ifdef LZW_COMPAT +/* NB: +1024 is for compatibility with old files */ +#define CSIZE (MAXCODE(BITS_MAX) + 1024L) +#else +#define CSIZE (MAXCODE(BITS_MAX) + 1L) +#endif + +/* + * State block for each open TIFF file using LZW + * compression/decompression. Note that the predictor + * state block must be first in this data structure. + */ +typedef struct +{ + TIFFPredictorState predict; /* predictor super class */ + + unsigned short nbits; /* # of bits/code */ + unsigned short maxcode; /* maximum code for lzw_nbits */ + unsigned short free_ent; /* next free entry in hash table */ + WordType nextdata; /* next bits of i/o */ + long nextbits; /* # of valid bits in lzw_nextdata */ + + int rw_mode; /* preserve rw_mode from init */ +} LZWBaseState; + +#define lzw_nbits base.nbits +#define lzw_maxcode base.maxcode +#define lzw_free_ent base.free_ent +#define lzw_nextdata base.nextdata +#define lzw_nextbits base.nextbits + +/* + * Encoding-specific state. + */ +typedef uint16_t hcode_t; /* codes fit in 16 bits */ +typedef struct +{ + long hash; + hcode_t code; +} hash_t; + +/* + * Decoding-specific state. + */ +typedef struct code_ent +{ + struct code_ent *next; + unsigned short length; /* string len, including this token */ + /* firstchar should be placed immediately before value in this structure */ + unsigned char firstchar; /* first token of string */ + unsigned char value; /* data value */ + bool repeated; +} code_t; + +typedef int (*decodeFunc)(TIFF *, uint8_t *, tmsize_t, uint16_t); + +typedef struct +{ + LZWBaseState base; + + /* Decoding specific data */ + long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */ + tmsize_t dec_restart; /* restart count */ + uint64_t dec_bitsleft; /* available bits in raw data */ + tmsize_t old_tif_rawcc; /* value of tif_rawcc at the end of the previous + TIFLZWDecode() call */ + decodeFunc dec_decode; /* regular or backwards compatible */ + code_t *dec_codep; /* current recognized code */ + code_t *dec_oldcodep; /* previously recognized code */ + code_t *dec_free_entp; /* next free entry */ + code_t *dec_maxcodep; /* max available entry */ + code_t *dec_codetab; /* kept separate for small machines */ + int read_error; /* whether a read error has occurred, and which should cause + further reads in the same strip/tile to be aborted */ + + /* Encoding specific data */ + int enc_oldcode; /* last code encountered */ + tmsize_t enc_checkpoint; /* point at which to clear table */ +#define CHECK_GAP 10000 /* enc_ratio check interval */ + tmsize_t enc_ratio; /* current compression ratio */ + tmsize_t enc_incount; /* (input) data bytes encoded */ + tmsize_t enc_outcount; /* encoded (output) bytes */ + uint8_t *enc_rawlimit; /* bound on tif_rawdata buffer */ + hash_t *enc_hashtab; /* kept separate for small machines */ +} LZWCodecState; + +#define LZWState(tif) ((LZWBaseState *)(tif)->tif_data) +#define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif)) +#define LZWEncoderState(tif) ((LZWCodecState *)LZWState(tif)) + +static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s); +#ifdef LZW_COMPAT +static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s); +#endif + +/* + * LZW Decoder. + */ + +static int LZWFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +static int LZWSetupDecode(TIFF *tif) +{ + static const char module[] = "LZWSetupDecode"; + LZWCodecState *sp = LZWDecoderState(tif); + int code; + + if (sp == NULL) + { + /* + * Allocate state block so tag methods have storage to record + * values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState)); + if (tif->tif_data == NULL) + { + TIFFErrorExtR(tif, module, "No space for LZW state block"); + return (0); + } + + sp = LZWDecoderState(tif); + sp->dec_codetab = NULL; + sp->dec_decode = NULL; + + /* + * Setup predictor setup. + */ + (void)TIFFPredictorInit(tif); + } + + if (sp->dec_codetab == NULL) + { + sp->dec_codetab = (code_t *)_TIFFmallocExt(tif, CSIZE * sizeof(code_t)); + if (sp->dec_codetab == NULL) + { + TIFFErrorExtR(tif, module, "No space for LZW code table"); + return (0); + } + /* + * Pre-load the table. + */ + code = 255; + do + { + sp->dec_codetab[code].firstchar = (unsigned char)code; + sp->dec_codetab[code].value = (unsigned char)code; + sp->dec_codetab[code].repeated = true; + sp->dec_codetab[code].length = 1; + sp->dec_codetab[code].next = NULL; + } while (code--); + /* + * Zero-out the unused entries */ + /* Silence false positive */ + /* coverity[overrun-buffer-arg] */ + memset(&sp->dec_codetab[CODE_CLEAR], 0, + (CODE_FIRST - CODE_CLEAR) * sizeof(code_t)); + } + return (1); +} + +/* + * Setup state for decoding a strip. + */ +static int LZWPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "LZWPreDecode"; + LZWCodecState *sp = LZWDecoderState(tif); + + (void)s; + assert(sp != NULL); + if (sp->dec_codetab == NULL) + { + tif->tif_setupdecode(tif); + if (sp->dec_codetab == NULL) + return (0); + } + + /* + * Check for old bit-reversed codes. + */ + if (tif->tif_rawcc >= 2 && tif->tif_rawdata[0] == 0 && + (tif->tif_rawdata[1] & 0x1)) + { +#ifdef LZW_COMPAT + if (!sp->dec_decode) + { + TIFFWarningExtR(tif, module, "Old-style LZW codes, convert file"); + /* + * Override default decoding methods with + * ones that deal with the old coding. + * Otherwise the predictor versions set + * above will call the compatibility routines + * through the dec_decode method. + */ + tif->tif_decoderow = LZWDecodeCompat; + tif->tif_decodestrip = LZWDecodeCompat; + tif->tif_decodetile = LZWDecodeCompat; + /* + * If doing horizontal differencing, must + * re-setup the predictor logic since we + * switched the basic decoder methods... + */ + (*tif->tif_setupdecode)(tif); + sp->dec_decode = LZWDecodeCompat; + } + sp->lzw_maxcode = MAXCODE(BITS_MIN); +#else /* !LZW_COMPAT */ + if (!sp->dec_decode) + { + TIFFErrorExtR(tif, module, "Old-style LZW codes not supported"); + sp->dec_decode = LZWDecode; + } + return (0); +#endif /* !LZW_COMPAT */ + } + else + { + sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1; + sp->dec_decode = LZWDecode; + } + sp->lzw_nbits = BITS_MIN; + sp->lzw_nextbits = 0; + sp->lzw_nextdata = 0; + + sp->dec_restart = 0; + sp->dec_nbitsmask = MAXCODE(BITS_MIN); + sp->dec_bitsleft = 0; + sp->old_tif_rawcc = 0; + sp->dec_free_entp = sp->dec_codetab - 1; // + CODE_FIRST; + /* + * Zero entries that are not yet filled in. We do + * this to guard against bogus input data that causes + * us to index into undefined entries. If you can + * come up with a way to safely bounds-check input codes + * while decoding then you can remove this operation. + */ + sp->dec_oldcodep = &sp->dec_codetab[0]; + sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1]; + sp->read_error = 0; + return (1); +} + +/* + * Decode a "hunk of data". + */ + +/* Get the next 32 or 64-bit from the input data */ +#ifdef WORDS_BIGENDIAN +#define GetNextData(nextdata, bp) memcpy(&nextdata, bp, sizeof(nextdata)) +#elif SIZEOF_WORDTYPE == 8 +#if defined(_M_X64) +#define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp)) +#elif defined(__GNUC__) +#define GetNextData(nextdata, bp) \ + memcpy(&nextdata, bp, sizeof(nextdata)); \ + nextdata = __builtin_bswap64(nextdata) +#else +#define GetNextData(nextdata, bp) \ + nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \ + (((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \ + (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \ + (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7])) +#endif +#elif SIZEOF_WORDTYPE == 4 +#if defined(_M_X86) +#define GetNextData(nextdata, bp) \ + nextdata = _byteswap_ulong(*(unsigned long *)(bp)) +#elif defined(__GNUC__) +#define GetNextData(nextdata, bp) \ + memcpy(&nextdata, bp, sizeof(nextdata)); \ + nextdata = __builtin_bswap32(nextdata) +#else +#define GetNextData(nextdata, bp) \ + nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \ + (((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3])) +#endif +#else +#error "Unhandled SIZEOF_WORDTYPE" +#endif + +#define GetNextCodeLZW() \ + do \ + { \ + nextbits -= nbits; \ + if (nextbits < 0) \ + { \ + if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \ + { \ + unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \ + GetNextData(nextdata, bp); \ + bp += SIZEOF_WORDTYPE; \ + nextbits += 8 * SIZEOF_WORDTYPE; \ + dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \ + code = (WordType)((codetmp | (nextdata >> nextbits)) & \ + nbitsmask); \ + break; \ + } \ + else \ + { \ + if (dec_bitsleft < 8) \ + { \ + goto no_eoi; \ + } \ + nextdata = (nextdata << 8) | *(bp)++; \ + nextbits += 8; \ + dec_bitsleft -= 8; \ + if (nextbits < 0) \ + { \ + if (dec_bitsleft < 8) \ + { \ + goto no_eoi; \ + } \ + nextdata = (nextdata << 8) | *(bp)++; \ + nextbits += 8; \ + dec_bitsleft -= 8; \ + } \ + } \ + } \ + code = (WordType)((nextdata >> nextbits) & nbitsmask); \ + } while (0) + +static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s) +{ + static const char module[] = "LZWDecode"; + LZWCodecState *sp = LZWDecoderState(tif); + uint8_t *op = (uint8_t *)op0; + tmsize_t occ = occ0; + uint8_t *bp; + long nbits, nextbits, nbitsmask; + WordType nextdata; + code_t *free_entp, *maxcodep, *oldcodep; + + (void)s; + assert(sp != NULL); + assert(sp->dec_codetab != NULL); + + if (sp->read_error) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, + "LZWDecode: Scanline %" PRIu32 " cannot be read due to " + "previous error", + tif->tif_row); + return 0; + } + + /* + * Restart interrupted output operation. + */ + if (sp->dec_restart) + { + tmsize_t residue; + + code_t *codep = sp->dec_codep; + residue = codep->length - sp->dec_restart; + if (residue > occ) + { + /* + * Residue from previous decode is sufficient + * to satisfy decode request. Skip to the + * start of the decoded string, place decoded + * values in the output buffer, and return. + */ + sp->dec_restart += occ; + do + { + codep = codep->next; + } while (--residue > occ && codep); + if (codep) + { + uint8_t *tp = op + occ; + do + { + *--tp = codep->value; + codep = codep->next; + } while (--occ && codep); + } + return (1); + } + /* + * Residue satisfies only part of the decode request. + */ + op += residue; + occ -= residue; + uint8_t *tp = op; + do + { + *--tp = codep->value; + codep = codep->next; + } while (--residue && codep); + sp->dec_restart = 0; + } + + bp = (uint8_t *)tif->tif_rawcp; + sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3); + uint64_t dec_bitsleft = sp->dec_bitsleft; + nbits = sp->lzw_nbits; + nextdata = sp->lzw_nextdata; + nextbits = sp->lzw_nextbits; + nbitsmask = sp->dec_nbitsmask; + oldcodep = sp->dec_oldcodep; + free_entp = sp->dec_free_entp; + maxcodep = sp->dec_maxcodep; + code_t *const dec_codetab = sp->dec_codetab; + code_t *codep; + + if (occ == 0) + { + goto after_loop; + } + +begin: +{ + WordType code; + GetNextCodeLZW(); + codep = dec_codetab + code; + if (code >= CODE_FIRST) + goto code_above_or_equal_to_258; + if (code < 256) + goto code_below_256; + if (code == CODE_EOI) + goto after_loop; + goto code_clear; + +code_below_256: +{ + if (codep > free_entp) + goto error_code; + free_entp->next = oldcodep; + free_entp->firstchar = oldcodep->firstchar; + free_entp->length = oldcodep->length + 1; + free_entp->value = (uint8_t)code; + free_entp->repeated = + (bool)(oldcodep->repeated & (oldcodep->value == code)); + if (++free_entp > maxcodep) + { + if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */ + nbits = BITS_MAX; + nbitsmask = MAXCODE(nbits); + maxcodep = dec_codetab + nbitsmask - 1; + if (free_entp >= &dec_codetab[CSIZE]) + { + /* At that point, the next valid states are either EOI or a */ + /* CODE_CLEAR. If a regular code is read, at the next */ + /* attempt at registering a new entry, we will error out */ + /* due to setting free_entp before any valid code */ + free_entp = dec_codetab - 1; + } + } + oldcodep = codep; + *op++ = (uint8_t)code; + occ--; + if (occ == 0) + goto after_loop; + goto begin; +} + +code_above_or_equal_to_258: +{ + /* + * Add the new entry to the code table. + */ + + if (codep >= free_entp) + { + if (codep != free_entp) + goto error_code; + free_entp->value = oldcodep->firstchar; + } + else + { + free_entp->value = codep->firstchar; + } + free_entp->repeated = + (bool)(oldcodep->repeated & (oldcodep->value == free_entp->value)); + free_entp->next = oldcodep; + + free_entp->firstchar = oldcodep->firstchar; + free_entp->length = oldcodep->length + 1; + if (++free_entp > maxcodep) + { + if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */ + nbits = BITS_MAX; + nbitsmask = MAXCODE(nbits); + maxcodep = dec_codetab + nbitsmask - 1; + if (free_entp >= &dec_codetab[CSIZE]) + { + /* At that point, the next valid states are either EOI or a */ + /* CODE_CLEAR. If a regular code is read, at the next */ + /* attempt at registering a new entry, we will error out */ + /* due to setting free_entp before any valid code */ + free_entp = dec_codetab - 1; + } + } + oldcodep = codep; + + /* + * Code maps to a string, copy string + * value to output (written in reverse). + */ + /* tiny bit faster on x86_64 to store in unsigned short than int */ + unsigned short len = codep->length; + + if (len < 3) /* equivalent to len == 2 given all other conditions */ + { + if (occ <= 2) + { + if (occ == 2) + { + memcpy(op, &(codep->firstchar), 2); + op += 2; + occ -= 2; + goto after_loop; + } + goto too_short_buffer; + } + + memcpy(op, &(codep->firstchar), 2); + op += 2; + occ -= 2; + goto begin; /* we can save the comparison occ > 0 */ + } + + if (len == 3) + { + if (occ <= 3) + { + if (occ == 3) + { + op[0] = codep->firstchar; + op[1] = codep->next->value; + op[2] = codep->value; + op += 3; + occ -= 3; + goto after_loop; + } + goto too_short_buffer; + } + + op[0] = codep->firstchar; + op[1] = codep->next->value; + op[2] = codep->value; + op += 3; + occ -= 3; + goto begin; /* we can save the comparison occ > 0 */ + } + + if (len > occ) + { + goto too_short_buffer; + } + + if (codep->repeated) + { + memset(op, codep->value, len); + op += len; + occ -= len; + if (occ == 0) + goto after_loop; + goto begin; + } + + uint8_t *tp = op + len; + + assert(len >= 4); + + *--tp = codep->value; + codep = codep->next; + *--tp = codep->value; + codep = codep->next; + *--tp = codep->value; + codep = codep->next; + *--tp = codep->value; + if (tp > op) + { + do + { + codep = codep->next; + *--tp = codep->value; + } while (tp > op); + } + + assert(occ >= len); + op += len; + occ -= len; + if (occ == 0) + goto after_loop; + goto begin; +} + +code_clear: +{ + free_entp = dec_codetab + CODE_FIRST; + nbits = BITS_MIN; + nbitsmask = MAXCODE(BITS_MIN); + maxcodep = dec_codetab + nbitsmask - 1; + do + { + GetNextCodeLZW(); + } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */ + if (code == CODE_EOI) + goto after_loop; + if (code > CODE_EOI) + { + goto error_code; + } + *op++ = (uint8_t)code; + occ--; + oldcodep = dec_codetab + code; + if (occ == 0) + goto after_loop; + goto begin; +} +} + +too_short_buffer: +{ + /* + * String is too long for decode buffer, + * locate portion that will fit, copy to + * the decode buffer, and setup restart + * logic for the next decoding call. + */ + sp->dec_codep = codep; + do + { + codep = codep->next; + } while (codep->length > occ); + + sp->dec_restart = occ; + uint8_t *tp = op + occ; + do + { + *--tp = codep->value; + codep = codep->next; + } while (--occ); +} + +after_loop: + tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp); + tif->tif_rawcp = (uint8_t *)bp; + sp->old_tif_rawcc = tif->tif_rawcc; + sp->dec_bitsleft = dec_bitsleft; + sp->lzw_nbits = (unsigned short)nbits; + sp->lzw_nextdata = nextdata; + sp->lzw_nextbits = nextbits; + sp->dec_nbitsmask = nbitsmask; + sp->dec_oldcodep = oldcodep; + sp->dec_free_entp = free_entp; + sp->dec_maxcodep = maxcodep; + + if (occ > 0) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, + "Not enough data at scanline %" PRIu32 " (short %" PRIu64 + " bytes)", + tif->tif_row, (uint64_t)occ); + return (0); + } + return (1); + +no_eoi: + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, + "LZWDecode: Strip %" PRIu32 " not terminated with EOI code", + tif->tif_curstrip); + return 0; +error_code: + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, tif->tif_name, "Using code not yet in table"); + return 0; +} + +#ifdef LZW_COMPAT + +/* + * This check shouldn't be necessary because each + * strip is suppose to be terminated with CODE_EOI. + */ +#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) \ + { \ + if (dec_bitsleft < (uint64_t)nbits) \ + { \ + TIFFWarningExtR(_tif, module, \ + "LZWDecode: Strip %" PRIu32 \ + " not terminated with EOI code", \ + _tif->tif_curstrip); \ + _code = CODE_EOI; \ + } \ + else \ + { \ + _get(_sp, _bp, _code); \ + dec_bitsleft -= nbits; \ + } \ + } + +/* + * Decode a "hunk of data" for old images. + */ +#define GetNextCodeCompat(sp, bp, code) \ + { \ + nextdata |= (unsigned long)*(bp)++ << nextbits; \ + nextbits += 8; \ + if (nextbits < nbits) \ + { \ + nextdata |= (unsigned long)*(bp)++ << nextbits; \ + nextbits += 8; \ + } \ + code = (hcode_t)(nextdata & nbitsmask); \ + nextdata >>= nbits; \ + nextbits -= nbits; \ + } + +static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s) +{ + static const char module[] = "LZWDecodeCompat"; + LZWCodecState *sp = LZWDecoderState(tif); + uint8_t *op = (uint8_t *)op0; + tmsize_t occ = occ0; + uint8_t *tp; + uint8_t *bp; + int code, nbits; + int len; + long nextbits, nbitsmask; + WordType nextdata; + code_t *codep, *free_entp, *maxcodep, *oldcodep; + + (void)s; + assert(sp != NULL); + + /* + * Restart interrupted output operation. + */ + if (sp->dec_restart) + { + tmsize_t residue; + + codep = sp->dec_codep; + residue = codep->length - sp->dec_restart; + if (residue > occ) + { + /* + * Residue from previous decode is sufficient + * to satisfy decode request. Skip to the + * start of the decoded string, place decoded + * values in the output buffer, and return. + */ + sp->dec_restart += occ; + do + { + codep = codep->next; + } while (--residue > occ); + tp = op + occ; + do + { + *--tp = codep->value; + codep = codep->next; + } while (--occ); + return (1); + } + /* + * Residue satisfies only part of the decode request. + */ + op += residue; + occ -= residue; + tp = op; + do + { + *--tp = codep->value; + codep = codep->next; + } while (--residue); + sp->dec_restart = 0; + } + + bp = (uint8_t *)tif->tif_rawcp; + + sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3); + uint64_t dec_bitsleft = sp->dec_bitsleft; + + nbits = sp->lzw_nbits; + nextdata = sp->lzw_nextdata; + nextbits = sp->lzw_nextbits; + nbitsmask = sp->dec_nbitsmask; + oldcodep = sp->dec_oldcodep; + free_entp = sp->dec_free_entp; + maxcodep = sp->dec_maxcodep; + + while (occ > 0) + { + NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft); + if (code == CODE_EOI) + break; + if (code == CODE_CLEAR) + { + do + { + free_entp = sp->dec_codetab + CODE_FIRST; + _TIFFmemset(free_entp, 0, + (CSIZE - CODE_FIRST) * sizeof(code_t)); + nbits = BITS_MIN; + nbitsmask = MAXCODE(BITS_MIN); + maxcodep = sp->dec_codetab + nbitsmask; + NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft); + } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */ + if (code == CODE_EOI) + break; + if (code > CODE_CLEAR) + { + TIFFErrorExtR( + tif, tif->tif_name, + "LZWDecode: Corrupted LZW table at scanline %" PRIu32, + tif->tif_row); + return (0); + } + *op++ = (uint8_t)code; + occ--; + oldcodep = sp->dec_codetab + code; + continue; + } + codep = sp->dec_codetab + code; + + /* + * Add the new entry to the code table. + */ + if (free_entp < &sp->dec_codetab[0] || + free_entp >= &sp->dec_codetab[CSIZE]) + { + TIFFErrorExtR(tif, module, + "Corrupted LZW table at scanline %" PRIu32, + tif->tif_row); + return (0); + } + + free_entp->next = oldcodep; + if (free_entp->next < &sp->dec_codetab[0] || + free_entp->next >= &sp->dec_codetab[CSIZE]) + { + TIFFErrorExtR(tif, module, + "Corrupted LZW table at scanline %" PRIu32, + tif->tif_row); + return (0); + } + free_entp->firstchar = free_entp->next->firstchar; + free_entp->length = free_entp->next->length + 1; + free_entp->value = + (codep < free_entp) ? codep->firstchar : free_entp->firstchar; + if (++free_entp > maxcodep) + { + if (++nbits > BITS_MAX) /* should not happen */ + nbits = BITS_MAX; + nbitsmask = MAXCODE(nbits); + maxcodep = sp->dec_codetab + nbitsmask; + } + oldcodep = codep; + if (code >= 256) + { + /* + * Code maps to a string, copy string + * value to output (written in reverse). + */ + if (codep->length == 0) + { + TIFFErrorExtR( + tif, module, + "Wrong length of decoded " + "string: data probably corrupted at scanline %" PRIu32, + tif->tif_row); + return (0); + } + if (codep->length > occ) + { + /* + * String is too long for decode buffer, + * locate portion that will fit, copy to + * the decode buffer, and setup restart + * logic for the next decoding call. + */ + sp->dec_codep = codep; + do + { + codep = codep->next; + } while (codep->length > occ); + sp->dec_restart = occ; + tp = op + occ; + do + { + *--tp = codep->value; + codep = codep->next; + } while (--occ); + break; + } + len = codep->length; + tp = op + len; + do + { + *--tp = codep->value; + codep = codep->next; + } while (codep && tp > op); + assert(occ >= len); + op += len; + occ -= len; + } + else + { + *op++ = (uint8_t)code; + occ--; + } + } + + tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp); + tif->tif_rawcp = (uint8_t *)bp; + + sp->old_tif_rawcc = tif->tif_rawcc; + sp->dec_bitsleft = dec_bitsleft; + + sp->lzw_nbits = (unsigned short)nbits; + sp->lzw_nextdata = nextdata; + sp->lzw_nextbits = nextbits; + sp->dec_nbitsmask = nbitsmask; + sp->dec_oldcodep = oldcodep; + sp->dec_free_entp = free_entp; + sp->dec_maxcodep = maxcodep; + + if (occ > 0) + { + TIFFErrorExtR(tif, module, + "Not enough data at scanline %" PRIu32 " (short %" PRIu64 + " bytes)", + tif->tif_row, (uint64_t)occ); + return (0); + } + return (1); +} +#endif /* LZW_COMPAT */ + +#ifndef LZW_READ_ONLY + +static void cl_hash(LZWCodecState *); + +/* + * LZW Encoding. + */ + +static int LZWSetupEncode(TIFF *tif) +{ + static const char module[] = "LZWSetupEncode"; + LZWCodecState *sp = LZWEncoderState(tif); + + assert(sp != NULL); + sp->enc_hashtab = (hash_t *)_TIFFmallocExt(tif, HSIZE * sizeof(hash_t)); + if (sp->enc_hashtab == NULL) + { + TIFFErrorExtR(tif, module, "No space for LZW hash table"); + return (0); + } + return (1); +} + +/* + * Reset encoding state at the start of a strip. + */ +static int LZWPreEncode(TIFF *tif, uint16_t s) +{ + LZWCodecState *sp = LZWEncoderState(tif); + + (void)s; + assert(sp != NULL); + + if (sp->enc_hashtab == NULL) + { + tif->tif_setupencode(tif); + } + + sp->lzw_nbits = BITS_MIN; + sp->lzw_maxcode = MAXCODE(BITS_MIN); + sp->lzw_free_ent = CODE_FIRST; + sp->lzw_nextbits = 0; + sp->lzw_nextdata = 0; + sp->enc_checkpoint = CHECK_GAP; + sp->enc_ratio = 0; + sp->enc_incount = 0; + sp->enc_outcount = 0; + /* + * The 4 here insures there is space for 2 max-sized + * codes in LZWEncode and LZWPostDecode. + */ + sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize - 1 - 4; + cl_hash(sp); /* clear hash table */ + sp->enc_oldcode = (hcode_t)-1; /* generates CODE_CLEAR in LZWEncode */ + return (1); +} + +#define CALCRATIO(sp, rat) \ + { \ + if (incount > 0x007fffff) \ + { /* NB: shift will overflow */ \ + rat = outcount >> 8; \ + rat = (rat == 0 ? 0x7fffffff : incount / rat); \ + } \ + else \ + rat = (incount << 8) / outcount; \ + } + +/* Explicit 0xff masking to make icc -check=conversions happy */ +#define PutNextCode(op, c) \ + { \ + nextdata = (nextdata << nbits) | c; \ + nextbits += nbits; \ + *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \ + nextbits -= 8; \ + if (nextbits >= 8) \ + { \ + *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \ + nextbits -= 8; \ + } \ + outcount += nbits; \ + } + +/* + * Encode a chunk of pixels. + * + * Uses an open addressing double hashing (no chaining) on the + * prefix code/next character combination. We do a variant of + * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's + * relatively-prime secondary probe. Here, the modular division + * first probe is gives way to a faster exclusive-or manipulation. + * Also do block compression with an adaptive reset, whereby the + * code table is cleared when the compression ratio decreases, + * but after the table fills. The variable-length output codes + * are re-sized at this point, and a CODE_CLEAR is generated + * for the decoder. + */ +static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + register LZWCodecState *sp = LZWEncoderState(tif); + register long fcode; + register hash_t *hp; + register int h, c; + hcode_t ent; + long disp; + tmsize_t incount, outcount, checkpoint; + WordType nextdata; + long nextbits; + int free_ent, maxcode, nbits; + uint8_t *op; + uint8_t *limit; + + (void)s; + if (sp == NULL) + return (0); + + assert(sp->enc_hashtab != NULL); + + /* + * Load local state. + */ + incount = sp->enc_incount; + outcount = sp->enc_outcount; + checkpoint = sp->enc_checkpoint; + nextdata = sp->lzw_nextdata; + nextbits = sp->lzw_nextbits; + free_ent = sp->lzw_free_ent; + maxcode = sp->lzw_maxcode; + nbits = sp->lzw_nbits; + op = tif->tif_rawcp; + limit = sp->enc_rawlimit; + ent = (hcode_t)sp->enc_oldcode; + + if (ent == (hcode_t)-1 && cc > 0) + { + /* + * NB: This is safe because it can only happen + * at the start of a strip where we know there + * is space in the data buffer. + */ + PutNextCode(op, CODE_CLEAR); + ent = *bp++; + cc--; + incount++; + } + while (cc > 0) + { + c = *bp++; + cc--; + incount++; + fcode = ((long)c << BITS_MAX) + ent; + h = (c << HSHIFT) ^ ent; /* xor hashing */ +#ifdef _WINDOWS + /* + * Check hash index for an overflow. + */ + if (h >= HSIZE) + h -= HSIZE; +#endif + hp = &sp->enc_hashtab[h]; + if (hp->hash == fcode) + { + ent = hp->code; + continue; + } + if (hp->hash >= 0) + { + /* + * Primary hash failed, check secondary hash. + */ + disp = HSIZE - h; + if (h == 0) + disp = 1; + do + { + /* + * Avoid pointer arithmetic because of + * wraparound problems with segments. + */ + if ((h -= disp) < 0) + h += HSIZE; + hp = &sp->enc_hashtab[h]; + if (hp->hash == fcode) + { + ent = hp->code; + goto hit; + } + } while (hp->hash >= 0); + } + /* + * New entry, emit code and add to table. + */ + /* + * Verify there is space in the buffer for the code + * and any potential Clear code that might be emitted + * below. The value of limit is setup so that there + * are at least 4 bytes free--room for 2 codes. + */ + if (op > limit) + { + tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); + if (!TIFFFlushData1(tif)) + return 0; + op = tif->tif_rawdata; + } + PutNextCode(op, ent); + ent = (hcode_t)c; + hp->code = (hcode_t)(free_ent++); + hp->hash = fcode; + if (free_ent == CODE_MAX - 1) + { + /* table is full, emit clear code and reset */ + cl_hash(sp); + sp->enc_ratio = 0; + incount = 0; + outcount = 0; + free_ent = CODE_FIRST; + PutNextCode(op, CODE_CLEAR); + nbits = BITS_MIN; + maxcode = MAXCODE(BITS_MIN); + } + else + { + /* + * If the next entry is going to be too big for + * the code size, then increase it, if possible. + */ + if (free_ent > maxcode) + { + nbits++; + assert(nbits <= BITS_MAX); + maxcode = (int)MAXCODE(nbits); + } + else if (incount >= checkpoint) + { + tmsize_t rat; + /* + * Check compression ratio and, if things seem + * to be slipping, clear the hash table and + * reset state. The compression ratio is a + * 24+8-bit fractional number. + */ + checkpoint = incount + CHECK_GAP; + CALCRATIO(sp, rat); + if (rat <= sp->enc_ratio) + { + cl_hash(sp); + sp->enc_ratio = 0; + incount = 0; + outcount = 0; + free_ent = CODE_FIRST; + PutNextCode(op, CODE_CLEAR); + nbits = BITS_MIN; + maxcode = MAXCODE(BITS_MIN); + } + else + sp->enc_ratio = rat; + } + } + hit:; + } + + /* + * Restore global state. + */ + sp->enc_incount = incount; + sp->enc_outcount = outcount; + sp->enc_checkpoint = checkpoint; + sp->enc_oldcode = ent; + sp->lzw_nextdata = nextdata; + sp->lzw_nextbits = nextbits; + sp->lzw_free_ent = (unsigned short)free_ent; + sp->lzw_maxcode = (unsigned short)maxcode; + sp->lzw_nbits = (unsigned short)nbits; + tif->tif_rawcp = op; + return (1); +} + +/* + * Finish off an encoded strip by flushing the last + * string and tacking on an End Of Information code. + */ +static int LZWPostEncode(TIFF *tif) +{ + register LZWCodecState *sp = LZWEncoderState(tif); + uint8_t *op = tif->tif_rawcp; + long nextbits = sp->lzw_nextbits; + WordType nextdata = sp->lzw_nextdata; + tmsize_t outcount = sp->enc_outcount; + int nbits = sp->lzw_nbits; + + if (op > sp->enc_rawlimit) + { + tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); + if (!TIFFFlushData1(tif)) + return 0; + op = tif->tif_rawdata; + } + if (sp->enc_oldcode != (hcode_t)-1) + { + int free_ent = sp->lzw_free_ent; + + PutNextCode(op, sp->enc_oldcode); + sp->enc_oldcode = (hcode_t)-1; + free_ent++; + + if (free_ent == CODE_MAX - 1) + { + /* table is full, emit clear code and reset */ + outcount = 0; + PutNextCode(op, CODE_CLEAR); + nbits = BITS_MIN; + } + else + { + /* + * If the next entry is going to be too big for + * the code size, then increase it, if possible. + */ + if (free_ent > sp->lzw_maxcode) + { + nbits++; + assert(nbits <= BITS_MAX); + } + } + } + PutNextCode(op, CODE_EOI); + /* Explicit 0xff masking to make icc -check=conversions happy */ + if (nextbits > 0) + *op++ = (unsigned char)((nextdata << (8 - nextbits)) & 0xff); + tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); + (void)outcount; + return (1); +} + +/* + * Reset encoding hash table. + */ +static void cl_hash(LZWCodecState *sp) +{ + register hash_t *hp = &sp->enc_hashtab[HSIZE - 1]; + register long i = HSIZE - 8; + + do + { + i -= 8; + hp[-7].hash = -1; + hp[-6].hash = -1; + hp[-5].hash = -1; + hp[-4].hash = -1; + hp[-3].hash = -1; + hp[-2].hash = -1; + hp[-1].hash = -1; + hp[0].hash = -1; + hp -= 8; + } while (i >= 0); + for (i += 8; i > 0; i--, hp--) + hp->hash = -1; +} + +#endif + +static void LZWCleanup(TIFF *tif) +{ + (void)TIFFPredictorCleanup(tif); + + assert(tif->tif_data != NULL); + + if (LZWDecoderState(tif)->dec_codetab) + _TIFFfreeExt(tif, LZWDecoderState(tif)->dec_codetab); + + if (LZWEncoderState(tif)->enc_hashtab) + _TIFFfreeExt(tif, LZWEncoderState(tif)->enc_hashtab); + + _TIFFfreeExt(tif, tif->tif_data); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +int TIFFInitLZW(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitLZW"; + (void)scheme; + assert(scheme == COMPRESSION_LZW); + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState)); + if (tif->tif_data == NULL) + goto bad; + LZWDecoderState(tif)->dec_codetab = NULL; + LZWDecoderState(tif)->dec_decode = NULL; + LZWEncoderState(tif)->enc_hashtab = NULL; + LZWState(tif)->rw_mode = tif->tif_mode; + + /* + * Install codec methods. + */ + tif->tif_fixuptags = LZWFixupTags; + tif->tif_setupdecode = LZWSetupDecode; + tif->tif_predecode = LZWPreDecode; + tif->tif_decoderow = LZWDecode; + tif->tif_decodestrip = LZWDecode; + tif->tif_decodetile = LZWDecode; +#ifndef LZW_READ_ONLY + tif->tif_setupencode = LZWSetupEncode; + tif->tif_preencode = LZWPreEncode; + tif->tif_postencode = LZWPostEncode; + tif->tif_encoderow = LZWEncode; + tif->tif_encodestrip = LZWEncode; + tif->tif_encodetile = LZWEncode; +#endif + tif->tif_cleanup = LZWCleanup; + /* + * Setup predictor setup. + */ + (void)TIFFPredictorInit(tif); + return (1); +bad: + TIFFErrorExtR(tif, module, "No space for LZW state block"); + return (0); +} + +/* + * Copyright (c) 1985, 1986 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * James A. Woods, derived from original work by Spencer Thomas + * and Joseph Orost. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ +#endif /* LZW_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_next.c b/cpp/3rd_party/libtiff/tif_next.c new file mode 100644 index 0000000000..f000574ee7 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_next.c @@ -0,0 +1,194 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef NEXT_SUPPORT +/* + * TIFF Library. + * + * NeXT 2-bit Grey Scale Compression Algorithm Support + */ + +#define SETPIXEL(op, v) \ + { \ + switch (npixels++ & 3) \ + { \ + case 0: \ + op[0] = (unsigned char)((v) << 6); \ + break; \ + case 1: \ + op[0] |= (v) << 4; \ + break; \ + case 2: \ + op[0] |= (v) << 2; \ + break; \ + case 3: \ + *op++ |= (v); \ + op_offset++; \ + break; \ + } \ + } + +#define LITERALROW 0x00 +#define LITERALSPAN 0x40 +#define WHITE ((1 << 2) - 1) + +static int NeXTDecode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) +{ + static const char module[] = "NeXTDecode"; + unsigned char *bp, *op; + tmsize_t cc; + uint8_t *row; + tmsize_t scanline, n; + + (void)s; + /* + * Each scanline is assumed to start off as all + * white (we assume a PhotometricInterpretation + * of ``min-is-black''). + */ + for (op = (unsigned char *)buf, cc = occ; cc-- > 0;) + *op++ = 0xff; + + bp = (unsigned char *)tif->tif_rawcp; + cc = tif->tif_rawcc; + scanline = tif->tif_scanlinesize; + if (occ % scanline) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return (0); + } + for (row = buf; cc > 0 && occ > 0; occ -= scanline, row += scanline) + { + n = *bp++; + cc--; + switch (n) + { + case LITERALROW: + /* + * The entire scanline is given as literal values. + */ + if (cc < scanline) + goto bad; + _TIFFmemcpy(row, bp, scanline); + bp += scanline; + cc -= scanline; + break; + case LITERALSPAN: + { + tmsize_t off; + /* + * The scanline has a literal span that begins at some + * offset. + */ + if (cc < 4) + goto bad; + off = (bp[0] * 256) + bp[1]; + n = (bp[2] * 256) + bp[3]; + if (cc < 4 + n || off + n > scanline) + goto bad; + _TIFFmemcpy(row + off, bp + 4, n); + bp += 4 + n; + cc -= 4 + n; + break; + } + default: + { + uint32_t npixels = 0, grey; + tmsize_t op_offset = 0; + uint32_t imagewidth = tif->tif_dir.td_imagewidth; + if (isTiled(tif)) + imagewidth = tif->tif_dir.td_tilewidth; + + /* + * The scanline is composed of a sequence of constant + * color ``runs''. We shift into ``run mode'' and + * interpret bytes as codes of the form + * until we've filled the scanline. + */ + op = row; + for (;;) + { + grey = (uint32_t)((n >> 6) & 0x3); + n &= 0x3f; + /* + * Ensure the run does not exceed the scanline + * bounds, potentially resulting in a security + * issue. + */ + while (n-- > 0 && npixels < imagewidth && + op_offset < scanline) + SETPIXEL(op, grey); + if (npixels >= imagewidth) + break; + if (op_offset >= scanline) + { + TIFFErrorExtR(tif, module, + "Invalid data for scanline %" PRIu32, + tif->tif_row); + return (0); + } + if (cc == 0) + goto bad; + n = *bp++; + cc--; + } + break; + } + } + } + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + return (1); +bad: + TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32, + tif->tif_row); + return (0); +} + +static int NeXTPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "NeXTPreDecode"; + TIFFDirectory *td = &tif->tif_dir; + (void)s; + + if (td->td_bitspersample != 2) + { + TIFFErrorExtR(tif, module, "Unsupported BitsPerSample = %" PRIu16, + td->td_bitspersample); + return (0); + } + return (1); +} + +int TIFFInitNeXT(TIFF *tif, int scheme) +{ + (void)scheme; + tif->tif_predecode = NeXTPreDecode; + tif->tif_decoderow = NeXTDecode; + tif->tif_decodestrip = NeXTDecode; + tif->tif_decodetile = NeXTDecode; + return (1); +} +#endif /* NEXT_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_ojpeg.c b/cpp/3rd_party/libtiff/tif_ojpeg.c new file mode 100644 index 0000000000..5ef7a6af6f --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_ojpeg.c @@ -0,0 +1,2810 @@ +/* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0 + specification is now totally obsolete and deprecated for new applications and + images. This file was was created solely in order to read unconverted images + still present on some users' computer systems. It will never be extended + to write such files. Writing new-style JPEG compressed TIFFs is implemented + in tif_jpeg.c. + + The code is carefully crafted to robustly read all gathered JPEG-in-TIFF + testfiles, and anticipate as much as possible all other... But still, it may + fail on some. If you encounter problems, please report them on the TIFF + mailing list and/or to Joris Van Damme . + + Please read the file called "TIFF Technical Note #2" if you need to be + convinced this compression scheme is bad and breaks TIFF. That document + is linked to from the LibTiff site + and from AWare Systems' TIFF section + . It is also absorbed + in Adobe's specification supplements, marked "draft" up to this day, but + supported by the TIFF community. + + This file interfaces with Release 6B of the JPEG Library written by the + Independent JPEG Group. Previous versions of this file required a hack inside + the LibJpeg library. This version no longer requires that. Remember to + remove the hack if you update from the old version. + + Copyright (c) Joris Van Damme + Copyright (c) AWare Systems + + The licence agreement for this file is the same as the rest of the LibTiff + library. + + IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR + ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + OF THIS SOFTWARE. + + Joris Van Damme and/or AWare Systems may be available for custom + development. If you like what you see, and need anything similar or related, + contact . +*/ + +/* What is what, and what is not? + + This decoder starts with an input stream, that is essentially the + JpegInterchangeFormat stream, if any, followed by the strile data, if any. + This stream is read in OJPEGReadByte and related functions. + + It analyzes the start of this stream, until it encounters non-marker data, + i.e. compressed image data. Some of the header markers it sees have no actual + content, like the SOI marker, and APP/COM markers that really shouldn't even + be there. Some other markers do have content, and the valuable bits and + pieces of information in these markers are saved, checking all to verify that + the stream is more or less within expected bounds. This happens inside the + OJPEGReadHeaderInfoSecStreamXxx functions. + + Some OJPEG imagery contains no valid JPEG header markers. This situation is + picked up on if we've seen no SOF marker when we're at the start of the + compressed image data. In this case, the tables are read from JpegXxxTables + tags, and the other bits and pieces of information is initialized to its most + basic value. This is implemented in the OJPEGReadHeaderInfoSecTablesXxx + functions. + + When this is complete, a good and valid JPEG header can be assembled, and + this is passed through to LibJpeg. When that's done, the remainder of the + input stream, i.e. the compressed image data, can be passed through + unchanged. This is done in OJPEGWriteStream functions. + + LibTiff rightly expects to know the subsampling values before decompression. + Just like in new-style JPEG-in-TIFF, though, or even more so, actually, the + YCbCrsubsampling tag is notoriously unreliable. To correct these tag values + with the ones inside the JPEG stream, the first part of the input stream is + pre-scanned in OJPEGSubsamplingCorrect, making no note of any other data, + reporting no warnings or errors, up to the point where either these values + are read, or it's clear they aren't there. This means that some of the data + is read twice, but we feel speed in correcting these values is important + enough to warrant this sacrifice. Although there is currently no define or + other configuration mechanism to disable this behavior, the actual header + scanning is build to robustly respond with error report if it should + encounter an uncorrected mismatch of subsampling values. See + OJPEGReadHeaderInfoSecStreamSof. + + The restart interval and restart markers are the most tricky part... The + restart interval can be specified in a tag. It can also be set inside the + input JPEG stream. It can be used inside the input JPEG stream. If reading + from strile data, we've consistently discovered the need to insert restart + markers in between the different striles, as is also probably the most likely + interpretation of the original TIFF 6.0 specification. With all this setting + of interval, and actual use of markers that is not predictable at the time of + valid JPEG header assembly, the restart thing may turn out the Achilles heel + of this implementation. Fortunately, most OJPEG writer vendors succeed in + reading back what they write, which may be the reason why we've been able to + discover ways that seem to work. + + Some special provision is made for planarconfig separate OJPEG files. These + seem to consistently contain header info, a SOS marker, a plane, SOS marker, + plane, SOS, and plane. This may or may not be a valid JPEG configuration, we + don't know and don't care. We want LibTiff to be able to access the planes + individually, without huge buffering inside LibJpeg, anyway. So we compose + headers to feed to LibJpeg, in this case, that allow us to pass a single + plane such that LibJpeg sees a valid single-channel JPEG stream. Locating + subsequent SOS markers, and thus subsequent planes, is done inside + OJPEGReadSecondarySos. + + The benefit of the scheme is... that it works, basically. We know of no other + that does. It works without checking software tag, or otherwise going about + things in an OJPEG flavor specific manner. Instead, it is a single scheme, + that covers the cases with and without JpegInterchangeFormat, with and + without striles, with part of the header in JpegInterchangeFormat and + remainder in first strile, etc. It is forgiving and robust, may likely work + with OJPEG flavors we've not seen yet, and makes most out of the data. + + Another nice side-effect is that a complete JPEG single valid stream is build + if planarconfig is not separate (vast majority). We may one day use that to + build converters to JPEG, and/or to new-style JPEG compression inside TIFF. + + A disadvantage is the lack of random access to the individual striles. This + is the reason for much of the complicated restart-and-position stuff inside + OJPEGPreDecode. Applications would do well accessing all striles in order, as + this will result in a single sequential scan of the input stream, and no + restarting of LibJpeg decoding session. +*/ + +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRALEAN + +#include "tiffiop.h" +#ifdef OJPEG_SUPPORT + +/* Configuration defines here are: + * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some + * environments, like eg LibTiffDelphi, this is not possible. For this reason, + * the actual calls to libjpeg, with longjump stuff, are encapsulated in + * dedicated functions. When JPEG_ENCAP_EXTERNAL is defined, these encapsulating + * functions are declared external to this unit, and can be defined elsewhere to + * use stuff other then longjump. The default mode, without JPEG_ENCAP_EXTERNAL, + * implements the call encapsulators here, internally, with normal longjump. + * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent + * is conveniently available, but still it may be worthwhile to use _setjmp or + * sigsetjmp in place of plain setjmp. These macros will make it easier. It is + * useless to fiddle with these if you define JPEG_ENCAP_EXTERNAL. OJPEG_BUFFER: + * Define the size of the desired buffer here. Should be small enough so as to + * guarantee instant processing, optimal streaming and optimal use of processor + * cache, but also big enough so as to not result in significant call overhead. + * It should be at least a few bytes to accommodate some structures (this is + * verified in asserts), but it would not be sensible to make it this small + * anyway, and it should be at most 64K since it is indexed with uint16_t. We + * recommend 2K. EGYPTIANWALK: You could also define EGYPTIANWALK here, but it + * is not used anywhere and has absolutely no effect. That is why most people + * insist the EGYPTIANWALK is a bit silly. + */ + +/* define LIBJPEG_ENCAP_EXTERNAL */ +#define SETJMP(jbuf) setjmp(jbuf) +#define LONGJMP(jbuf, code) longjmp(jbuf, code) +#define JMP_BUF jmp_buf +#define OJPEG_BUFFER 2048 +/* define EGYPTIANWALK */ + +#define JPEG_MARKER_SOF0 0xC0 +#define JPEG_MARKER_SOF1 0xC1 +#define JPEG_MARKER_SOF3 0xC3 +#define JPEG_MARKER_DHT 0xC4 +#define JPEG_MARKER_RST0 0XD0 +#define JPEG_MARKER_SOI 0xD8 +#define JPEG_MARKER_EOI 0xD9 +#define JPEG_MARKER_SOS 0xDA +#define JPEG_MARKER_DQT 0xDB +#define JPEG_MARKER_DRI 0xDD +#define JPEG_MARKER_APP0 0xE0 +#define JPEG_MARKER_COM 0xFE + +#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC + 0) +#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC + 1) +#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC + 2) +#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC + 3) +#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC + 4) +#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC + 5) +#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC + 6) + +static const TIFFField ojpegFields[] = { + {TIFFTAG_JPEGIFOFFSET, 1, 1, TIFF_LONG8, 0, TIFF_SETGET_UINT64, + FIELD_OJPEG_JPEGINTERCHANGEFORMAT, TRUE, FALSE, "JpegInterchangeFormat", + NULL}, + {TIFFTAG_JPEGIFBYTECOUNT, 1, 1, TIFF_LONG8, 0, TIFF_SETGET_UINT64, + FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH, TRUE, FALSE, + "JpegInterchangeFormatLength", NULL}, + {TIFFTAG_JPEGQTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0, + TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGQTABLES, FALSE, TRUE, + "JpegQTables", NULL}, + {TIFFTAG_JPEGDCTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0, + TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGDCTABLES, FALSE, TRUE, + "JpegDcTables", NULL}, + {TIFFTAG_JPEGACTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0, + TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGACTABLES, FALSE, TRUE, + "JpegAcTables", NULL}, + {TIFFTAG_JPEGPROC, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, + FIELD_OJPEG_JPEGPROC, FALSE, FALSE, "JpegProc", NULL}, + {TIFFTAG_JPEGRESTARTINTERVAL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, + FIELD_OJPEG_JPEGRESTARTINTERVAL, FALSE, FALSE, "JpegRestartInterval", + NULL}, +}; + +#ifndef LIBJPEG_ENCAP_EXTERNAL +#include +#endif + +#include "jerror.h" +#include "jpeglib.h" + +#ifndef TIFF_jpeg_source_mgr_defined +#define TIFF_jpeg_source_mgr_defined +typedef struct jpeg_source_mgr jpeg_source_mgr; +#endif + +#ifndef TIFF_jpeg_error_mgr_defined +#define TIFF_jpeg_error_mgr_defined +typedef struct jpeg_error_mgr jpeg_error_mgr; +#endif + +typedef struct jpeg_common_struct jpeg_common_struct; +typedef struct jpeg_decompress_struct jpeg_decompress_struct; + +typedef enum +{ + osibsNotSetYet, + osibsJpegInterchangeFormat, + osibsStrile, + osibsEof +} OJPEGStateInBufferSource; + +typedef enum +{ + ososSoi, + ososQTable0, + ososQTable1, + ososQTable2, + ososQTable3, + ososDcTable0, + ososDcTable1, + ososDcTable2, + ososDcTable3, + ososAcTable0, + ososAcTable1, + ososAcTable2, + ososAcTable3, + ososDri, + ososSof, + ososSos, + ososCompressed, + ososRst, + ososEoi +} OJPEGStateOutState; + +typedef struct +{ + TIFF *tif; + int decoder_ok; + int error_in_raw_data_decoding; +#ifndef LIBJPEG_ENCAP_EXTERNAL + JMP_BUF exit_jmpbuf; +#endif + TIFFVGetMethod vgetparent; + TIFFVSetMethod vsetparent; + TIFFPrintMethod printdir; + uint64_t file_size; + uint32_t image_width; + uint32_t image_length; + uint32_t strile_width; + uint32_t strile_length; + uint32_t strile_length_total; + uint8_t samples_per_pixel; + uint8_t plane_sample_offset; + uint8_t samples_per_pixel_per_plane; + uint64_t jpeg_interchange_format; + uint64_t jpeg_interchange_format_length; + uint8_t jpeg_proc; + uint8_t subsamplingcorrect; + uint8_t subsamplingcorrect_done; + uint8_t subsampling_tag; + uint8_t subsampling_hor; + uint8_t subsampling_ver; + uint8_t subsampling_force_desubsampling_inside_decompression; + uint8_t qtable_offset_count; + uint8_t dctable_offset_count; + uint8_t actable_offset_count; + uint64_t qtable_offset[3]; + uint64_t dctable_offset[3]; + uint64_t actable_offset[3]; + uint8_t *qtable[4]; + uint8_t *dctable[4]; + uint8_t *actable[4]; + uint16_t restart_interval; + uint8_t restart_index; + uint8_t sof_log; + uint8_t sof_marker_id; + uint32_t sof_x; + uint32_t sof_y; + uint8_t sof_c[3]; + uint8_t sof_hv[3]; + uint8_t sof_tq[3]; + uint8_t sos_cs[3]; + uint8_t sos_tda[3]; + struct + { + uint8_t log; + OJPEGStateInBufferSource in_buffer_source; + uint32_t in_buffer_next_strile; + uint64_t in_buffer_file_pos; + uint64_t in_buffer_file_togo; + } sos_end[3]; + uint8_t readheader_done; + uint8_t writeheader_done; + uint16_t write_cursample; + uint32_t write_curstrile; + uint8_t libjpeg_session_active; + uint8_t libjpeg_jpeg_query_style; + jpeg_error_mgr libjpeg_jpeg_error_mgr; + jpeg_decompress_struct libjpeg_jpeg_decompress_struct; + jpeg_source_mgr libjpeg_jpeg_source_mgr; + uint8_t subsampling_convert_log; + uint32_t subsampling_convert_ylinelen; + uint32_t subsampling_convert_ylines; + uint32_t subsampling_convert_clinelen; + uint32_t subsampling_convert_clines; + uint32_t subsampling_convert_ybuflen; + uint32_t subsampling_convert_cbuflen; + uint32_t subsampling_convert_ycbcrbuflen; + uint8_t *subsampling_convert_ycbcrbuf; + uint8_t *subsampling_convert_ybuf; + uint8_t *subsampling_convert_cbbuf; + uint8_t *subsampling_convert_crbuf; + uint32_t subsampling_convert_ycbcrimagelen; + uint8_t **subsampling_convert_ycbcrimage; + uint32_t subsampling_convert_clinelenout; + uint32_t subsampling_convert_state; + uint32_t bytes_per_line; /* if the codec outputs subsampled data, a 'line' + in bytes_per_line */ + uint32_t lines_per_strile; /* and lines_per_strile means subsampling_ver + desubsampled rows */ + OJPEGStateInBufferSource in_buffer_source; + uint32_t in_buffer_next_strile; + uint32_t in_buffer_strile_count; + uint64_t in_buffer_file_pos; + uint8_t in_buffer_file_pos_log; + uint64_t in_buffer_file_togo; + uint16_t in_buffer_togo; + uint8_t *in_buffer_cur; + uint8_t in_buffer[OJPEG_BUFFER]; + OJPEGStateOutState out_state; + uint8_t out_buffer[OJPEG_BUFFER]; + uint8_t *skip_buffer; +} OJPEGState; + +static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap); +static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap); +static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags); + +static int OJPEGFixupTags(TIFF *tif); +static int OJPEGSetupDecode(TIFF *tif); +static int OJPEGPreDecode(TIFF *tif, uint16_t s); +static int OJPEGPreDecodeSkipRaw(TIFF *tif); +static int OJPEGPreDecodeSkipScanlines(TIFF *tif); +static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); +static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc); +static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc); +static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc); +static int OJPEGSetupEncode(TIFF *tif); +static int OJPEGPreEncode(TIFF *tif, uint16_t s); +static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); +static int OJPEGPostEncode(TIFF *tif); +static void OJPEGCleanup(TIFF *tif); + +static void OJPEGSubsamplingCorrect(TIFF *tif); +static int OJPEGReadHeaderInfo(TIFF *tif); +static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s); +static int OJPEGWriteHeaderInfo(TIFF *tif); +static void OJPEGLibjpegSessionAbort(TIFF *tif); + +static int OJPEGReadHeaderInfoSec(TIFF *tif); +static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif); +static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif); +static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif); +static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id); +static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif); +static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif); +static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif); +static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif); + +static int OJPEGReadBufferFill(OJPEGState *sp); +static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte); +static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte); +static void OJPEGReadByteAdvance(OJPEGState *sp); +static int OJPEGReadWord(OJPEGState *sp, uint16_t *word); +static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem); +static void OJPEGReadSkip(OJPEGState *sp, uint16_t len); + +static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len); +static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len); +static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem, + uint32_t *len); +static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem, + uint32_t *len); +static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem, + uint32_t *len); +static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len); +static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len); +static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len); +static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len); +static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len); +static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len); + +#ifdef LIBJPEG_ENCAP_EXTERNAL +extern int jpeg_create_decompress_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo); +extern int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo, + uint8_t require_image); +extern int jpeg_start_decompress_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo); +extern int jpeg_read_scanlines_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo, + void *scanlines, uint32_t max_lines); +extern int jpeg_read_raw_data_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo, void *data, + uint32_t max_lines); +extern void jpeg_encap_unwind(TIFF *tif); +#else +static int jpeg_create_decompress_encap(OJPEGState *sp, + jpeg_decompress_struct *j); +static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo, + uint8_t require_image); +static int jpeg_start_decompress_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo); +static int jpeg_read_scanlines_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo, + void *scanlines, uint32_t max_lines); +static int jpeg_read_raw_data_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo, void *data, + uint32_t max_lines); +static void jpeg_encap_unwind(TIFF *tif); +#endif + +static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo); +static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo); +static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo); +static boolean +OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo); +static void +OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo, + long num_bytes); +static boolean +OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo, + int desired); +static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo); + +int TIFFInitOJPEG(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitOJPEG"; + OJPEGState *sp; + + (void)scheme; + assert(scheme == COMPRESSION_OJPEG); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) + { + TIFFErrorExtR(tif, module, + "Merging Old JPEG codec-specific tags failed"); + return 0; + } + + /* state block */ + sp = _TIFFmallocExt(tif, sizeof(OJPEGState)); + if (sp == NULL) + { + TIFFErrorExtR(tif, module, "No space for OJPEG state block"); + return (0); + } + _TIFFmemset(sp, 0, sizeof(OJPEGState)); + sp->tif = tif; + sp->jpeg_proc = 1; + sp->subsampling_hor = 2; + sp->subsampling_ver = 2; + TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, 2, 2); + /* tif codec methods */ + tif->tif_fixuptags = OJPEGFixupTags; + tif->tif_setupdecode = OJPEGSetupDecode; + tif->tif_predecode = OJPEGPreDecode; + tif->tif_postdecode = OJPEGPostDecode; + tif->tif_decoderow = OJPEGDecode; + tif->tif_decodestrip = OJPEGDecode; + tif->tif_decodetile = OJPEGDecode; + tif->tif_setupencode = OJPEGSetupEncode; + tif->tif_preencode = OJPEGPreEncode; + tif->tif_postencode = OJPEGPostEncode; + tif->tif_encoderow = OJPEGEncode; + tif->tif_encodestrip = OJPEGEncode; + tif->tif_encodetile = OJPEGEncode; + tif->tif_cleanup = OJPEGCleanup; + tif->tif_data = (uint8_t *)sp; + /* tif tag methods */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = OJPEGVGetField; + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = OJPEGVSetField; + sp->printdir = tif->tif_tagmethods.printdir; + tif->tif_tagmethods.printdir = OJPEGPrintDir; + /* Some OJPEG files don't have strip or tile offsets or bytecounts tags. + Some others do, but have totally meaningless or corrupt values + in these tags. In these cases, the JpegInterchangeFormat stream is + reliable. In any case, this decoder reads the compressed data itself, + from the most reliable locations, and we need to notify encapsulating + LibTiff not to read raw strips or tiles for us. */ + tif->tif_flags |= TIFF_NOREADRAW; + return (1); +} + +static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + switch (tag) + { + case TIFFTAG_JPEGIFOFFSET: + *va_arg(ap, uint64_t *) = (uint64_t)sp->jpeg_interchange_format; + break; + case TIFFTAG_JPEGIFBYTECOUNT: + *va_arg(ap, uint64_t *) = + (uint64_t)sp->jpeg_interchange_format_length; + break; + case TIFFTAG_YCBCRSUBSAMPLING: + if (sp->subsamplingcorrect_done == 0) + OJPEGSubsamplingCorrect(tif); + *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_hor; + *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_ver; + break; + case TIFFTAG_JPEGQTABLES: + *va_arg(ap, uint32_t *) = (uint32_t)sp->qtable_offset_count; + *va_arg(ap, const void **) = (const void *)sp->qtable_offset; + break; + case TIFFTAG_JPEGDCTABLES: + *va_arg(ap, uint32_t *) = (uint32_t)sp->dctable_offset_count; + *va_arg(ap, const void **) = (const void *)sp->dctable_offset; + break; + case TIFFTAG_JPEGACTABLES: + *va_arg(ap, uint32_t *) = (uint32_t)sp->actable_offset_count; + *va_arg(ap, const void **) = (const void *)sp->actable_offset; + break; + case TIFFTAG_JPEGPROC: + *va_arg(ap, uint16_t *) = (uint16_t)sp->jpeg_proc; + break; + case TIFFTAG_JPEGRESTARTINTERVAL: + *va_arg(ap, uint16_t *) = sp->restart_interval; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return (1); +} + +static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "OJPEGVSetField"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint32_t ma; + uint64_t *mb; + uint32_t n; + const TIFFField *fip; + + switch (tag) + { + case TIFFTAG_JPEGIFOFFSET: + sp->jpeg_interchange_format = (uint64_t)va_arg(ap, uint64_t); + break; + case TIFFTAG_JPEGIFBYTECOUNT: + sp->jpeg_interchange_format_length = (uint64_t)va_arg(ap, uint64_t); + break; + case TIFFTAG_YCBCRSUBSAMPLING: + sp->subsampling_tag = 1; + sp->subsampling_hor = (uint8_t)va_arg(ap, uint16_vap); + sp->subsampling_ver = (uint8_t)va_arg(ap, uint16_vap); + tif->tif_dir.td_ycbcrsubsampling[0] = sp->subsampling_hor; + tif->tif_dir.td_ycbcrsubsampling[1] = sp->subsampling_ver; + break; + case TIFFTAG_JPEGQTABLES: + ma = (uint32_t)va_arg(ap, uint32_t); + if (ma != 0) + { + if (ma > 3) + { + TIFFErrorExtR(tif, module, + "JpegQTables tag has incorrect count"); + return (0); + } + sp->qtable_offset_count = (uint8_t)ma; + mb = (uint64_t *)va_arg(ap, uint64_t *); + for (n = 0; n < ma; n++) + sp->qtable_offset[n] = mb[n]; + } + break; + case TIFFTAG_JPEGDCTABLES: + ma = (uint32_t)va_arg(ap, uint32_t); + if (ma != 0) + { + if (ma > 3) + { + TIFFErrorExtR(tif, module, + "JpegDcTables tag has incorrect count"); + return (0); + } + sp->dctable_offset_count = (uint8_t)ma; + mb = (uint64_t *)va_arg(ap, uint64_t *); + for (n = 0; n < ma; n++) + sp->dctable_offset[n] = mb[n]; + } + break; + case TIFFTAG_JPEGACTABLES: + ma = (uint32_t)va_arg(ap, uint32_t); + if (ma != 0) + { + if (ma > 3) + { + TIFFErrorExtR(tif, module, + "JpegAcTables tag has incorrect count"); + return (0); + } + sp->actable_offset_count = (uint8_t)ma; + mb = (uint64_t *)va_arg(ap, uint64_t *); + for (n = 0; n < ma; n++) + sp->actable_offset[n] = mb[n]; + } + break; + case TIFFTAG_JPEGPROC: + sp->jpeg_proc = (uint8_t)va_arg(ap, uint16_vap); + break; + case TIFFTAG_JPEGRESTARTINTERVAL: + sp->restart_interval = (uint16_t)va_arg(ap, uint16_vap); + break; + default: + return (*sp->vsetparent)(tif, tag, ap); + } + fip = TIFFFieldWithTag(tif, tag); + if (fip == NULL) /* shouldn't happen */ + return (0); + TIFFSetFieldBit(tif, fip->field_bit); + tif->tif_flags |= TIFF_DIRTYDIRECT; + return (1); +} + +static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + (void)flags; + assert(sp != NULL); + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMAT)) + fprintf(fd, " JpegInterchangeFormat: %" PRIu64 "\n", + (uint64_t)sp->jpeg_interchange_format); + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH)) + fprintf(fd, " JpegInterchangeFormatLength: %" PRIu64 "\n", + (uint64_t)sp->jpeg_interchange_format_length); + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGQTABLES)) + { + fprintf(fd, " JpegQTables:"); + for (m = 0; m < sp->qtable_offset_count; m++) + fprintf(fd, " %" PRIu64, (uint64_t)sp->qtable_offset[m]); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGDCTABLES)) + { + fprintf(fd, " JpegDcTables:"); + for (m = 0; m < sp->dctable_offset_count; m++) + fprintf(fd, " %" PRIu64, (uint64_t)sp->dctable_offset[m]); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGACTABLES)) + { + fprintf(fd, " JpegAcTables:"); + for (m = 0; m < sp->actable_offset_count; m++) + fprintf(fd, " %" PRIu64, (uint64_t)sp->actable_offset[m]); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGPROC)) + fprintf(fd, " JpegProc: %" PRIu8 "\n", sp->jpeg_proc); + if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGRESTARTINTERVAL)) + fprintf(fd, " JpegRestartInterval: %" PRIu16 "\n", + sp->restart_interval); + if (sp->printdir) + (*sp->printdir)(tif, fd, flags); +} + +static int OJPEGFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +static int OJPEGSetupDecode(TIFF *tif) +{ + static const char module[] = "OJPEGSetupDecode"; + TIFFWarningExtR(tif, module, + "Deprecated and troublesome old-style JPEG compression " + "mode, please convert to new-style JPEG compression and " + "notify vendor of writing software"); + return (1); +} + +static int OJPEGPreDecode(TIFF *tif, uint16_t s) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint32_t m; + if (sp->subsamplingcorrect_done == 0) + OJPEGSubsamplingCorrect(tif); + if (sp->readheader_done == 0) + { + if (OJPEGReadHeaderInfo(tif) == 0) + return (0); + } + if (sp->sos_end[s].log == 0) + { + if (OJPEGReadSecondarySos(tif, s) == 0) + return (0); + } + if (isTiled(tif)) + m = tif->tif_curtile; + else + m = tif->tif_curstrip; + if ((sp->writeheader_done != 0) && + ((sp->write_cursample != s) || (sp->write_curstrile > m))) + { + if (sp->libjpeg_session_active != 0) + OJPEGLibjpegSessionAbort(tif); + sp->writeheader_done = 0; + } + if (sp->writeheader_done == 0) + { + sp->plane_sample_offset = (uint8_t)s; + sp->write_cursample = s; + sp->write_curstrile = s * tif->tif_dir.td_stripsperimage; + if ((sp->in_buffer_file_pos_log == 0) || + (sp->in_buffer_file_pos - sp->in_buffer_togo != + sp->sos_end[s].in_buffer_file_pos)) + { + sp->in_buffer_source = sp->sos_end[s].in_buffer_source; + sp->in_buffer_next_strile = sp->sos_end[s].in_buffer_next_strile; + sp->in_buffer_file_pos = sp->sos_end[s].in_buffer_file_pos; + sp->in_buffer_file_pos_log = 0; + sp->in_buffer_file_togo = sp->sos_end[s].in_buffer_file_togo; + sp->in_buffer_togo = 0; + sp->in_buffer_cur = 0; + } + if (OJPEGWriteHeaderInfo(tif) == 0) + return (0); + } + + sp->subsampling_convert_state = 0; + + while (sp->write_curstrile < m) + { + if (sp->libjpeg_jpeg_query_style == 0) + { + if (OJPEGPreDecodeSkipRaw(tif) == 0) + return (0); + } + else + { + if (OJPEGPreDecodeSkipScanlines(tif) == 0) + return (0); + } + sp->write_curstrile++; + } + sp->decoder_ok = 1; + return (1); +} + +static int OJPEGPreDecodeSkipRaw(TIFF *tif) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint32_t m; + m = sp->lines_per_strile; + if (sp->subsampling_convert_state != 0) + { + if (sp->subsampling_convert_clines - sp->subsampling_convert_state >= m) + { + sp->subsampling_convert_state += m; + if (sp->subsampling_convert_state == sp->subsampling_convert_clines) + sp->subsampling_convert_state = 0; + return (1); + } + m -= sp->subsampling_convert_clines - sp->subsampling_convert_state; + sp->subsampling_convert_state = 0; + sp->error_in_raw_data_decoding = 0; + } + while (m >= sp->subsampling_convert_clines) + { + if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), + sp->subsampling_convert_ycbcrimage, + sp->subsampling_ver * 8) == 0) + return (0); + m -= sp->subsampling_convert_clines; + } + if (m > 0) + { + if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), + sp->subsampling_convert_ycbcrimage, + sp->subsampling_ver * 8) == 0) + return (0); + sp->subsampling_convert_state = m; + } + return (1); +} + +static int OJPEGPreDecodeSkipScanlines(TIFF *tif) +{ + static const char module[] = "OJPEGPreDecodeSkipScanlines"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint32_t m; + if (sp->skip_buffer == NULL) + { + sp->skip_buffer = _TIFFmallocExt(tif, sp->bytes_per_line); + if (sp->skip_buffer == NULL) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + } + for (m = 0; m < sp->lines_per_strile; m++) + { + if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), + &sp->skip_buffer, 1) == 0) + return (0); + } + return (1); +} + +static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + static const char module[] = "OJPEGDecode"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + (void)s; + if (!sp->decoder_ok) + { + memset(buf, 0, (size_t)cc); + TIFFErrorExtR(tif, module, + "Cannot decode: decoder not correctly initialized"); + return 0; + } + if (sp->libjpeg_session_active == 0) + { + memset(buf, 0, (size_t)cc); + /* This should normally not happen, except that it does when */ + /* using TIFFReadScanline() which calls OJPEGPostDecode() for */ + /* each scanline, which assumes that a whole strile was read */ + /* and may thus incorrectly consider it has read the whole image, + * causing */ + /* OJPEGLibjpegSessionAbort() to be called prematurely. */ + /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */ + TIFFErrorExtR(tif, module, + "Cannot decode: libjpeg_session_active == 0"); + return 0; + } + if (sp->error_in_raw_data_decoding) + { + memset(buf, 0, (size_t)cc); + return 0; + } + if (sp->libjpeg_jpeg_query_style == 0) + { + if (OJPEGDecodeRaw(tif, buf, cc) == 0) + { + memset(buf, 0, (size_t)cc); + return (0); + } + } + else + { + if (OJPEGDecodeScanlines(tif, buf, cc) == 0) + { + memset(buf, 0, (size_t)cc); + return (0); + } + } + return (1); +} + +static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + static const char module[] = "OJPEGDecodeRaw"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t *m; + tmsize_t n; + uint8_t *oy; + uint8_t *ocb; + uint8_t *ocr; + uint8_t *p; + uint32_t q; + uint8_t *r; + uint8_t sx, sy; + if (cc % sp->bytes_per_line != 0) + { + TIFFErrorExtR(tif, module, "Fractional scanline not read"); + return (0); + } + assert(cc > 0); + m = buf; + n = cc; + do + { + if (sp->subsampling_convert_state == 0) + { + if (jpeg_read_raw_data_encap(sp, + &(sp->libjpeg_jpeg_decompress_struct), + sp->subsampling_convert_ycbcrimage, + sp->subsampling_ver * 8) == 0) + { + sp->error_in_raw_data_decoding = 1; + return (0); + } + } + oy = sp->subsampling_convert_ybuf + + sp->subsampling_convert_state * sp->subsampling_ver * + sp->subsampling_convert_ylinelen; + ocb = sp->subsampling_convert_cbbuf + + sp->subsampling_convert_state * sp->subsampling_convert_clinelen; + ocr = sp->subsampling_convert_crbuf + + sp->subsampling_convert_state * sp->subsampling_convert_clinelen; + p = m; + for (q = 0; q < sp->subsampling_convert_clinelenout; q++) + { + r = oy; + for (sy = 0; sy < sp->subsampling_ver; sy++) + { + for (sx = 0; sx < sp->subsampling_hor; sx++) + *p++ = *r++; + r += sp->subsampling_convert_ylinelen - sp->subsampling_hor; + } + oy += sp->subsampling_hor; + *p++ = *ocb++; + *p++ = *ocr++; + } + sp->subsampling_convert_state++; + if (sp->subsampling_convert_state == sp->subsampling_convert_clines) + sp->subsampling_convert_state = 0; + m += sp->bytes_per_line; + n -= sp->bytes_per_line; + } while (n > 0); + return (1); +} + +static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + static const char module[] = "OJPEGDecodeScanlines"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t *m; + tmsize_t n; + if (cc % sp->bytes_per_line != 0) + { + TIFFErrorExtR(tif, module, "Fractional scanline not read"); + return (0); + } + assert(cc > 0); + m = buf; + n = cc; + do + { + if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), + &m, 1) == 0) + return (0); + m += sp->bytes_per_line; + n -= sp->bytes_per_line; + } while (n > 0); + return (1); +} + +static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + (void)buf; + (void)cc; + /* This function somehow incorrectly assumes that a whole strile was read, + */ + /* which is not true when TIFFReadScanline() is called, */ + /* and may thus incorrectly consider it has read the whole image, causing */ + /* OJPEGLibjpegSessionAbort() to be called prematurely. */ + /* So this logic should be fixed to take into account cc, or disable */ + /* the scan line reading interface. */ + /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */ + sp->write_curstrile++; + if (sp->write_curstrile % tif->tif_dir.td_stripsperimage == 0) + { + assert(sp->libjpeg_session_active != 0); + OJPEGLibjpegSessionAbort(tif); + sp->writeheader_done = 0; + } +} + +static int OJPEGSetupEncode(TIFF *tif) +{ + static const char module[] = "OJPEGSetupEncode"; + TIFFErrorExtR( + tif, module, + "OJPEG encoding not supported; use new-style JPEG compression instead"); + return (0); +} + +static int OJPEGPreEncode(TIFF *tif, uint16_t s) +{ + static const char module[] = "OJPEGPreEncode"; + (void)s; + TIFFErrorExtR( + tif, module, + "OJPEG encoding not supported; use new-style JPEG compression instead"); + return (0); +} + +static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + static const char module[] = "OJPEGEncode"; + (void)buf; + (void)cc; + (void)s; + TIFFErrorExtR( + tif, module, + "OJPEG encoding not supported; use new-style JPEG compression instead"); + return (0); +} + +static int OJPEGPostEncode(TIFF *tif) +{ + static const char module[] = "OJPEGPostEncode"; + TIFFErrorExtR( + tif, module, + "OJPEG encoding not supported; use new-style JPEG compression instead"); + return (0); +} + +static void OJPEGCleanup(TIFF *tif) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + if (sp != 0) + { + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + tif->tif_tagmethods.printdir = sp->printdir; + if (sp->qtable[0] != 0) + _TIFFfreeExt(tif, sp->qtable[0]); + if (sp->qtable[1] != 0) + _TIFFfreeExt(tif, sp->qtable[1]); + if (sp->qtable[2] != 0) + _TIFFfreeExt(tif, sp->qtable[2]); + if (sp->qtable[3] != 0) + _TIFFfreeExt(tif, sp->qtable[3]); + if (sp->dctable[0] != 0) + _TIFFfreeExt(tif, sp->dctable[0]); + if (sp->dctable[1] != 0) + _TIFFfreeExt(tif, sp->dctable[1]); + if (sp->dctable[2] != 0) + _TIFFfreeExt(tif, sp->dctable[2]); + if (sp->dctable[3] != 0) + _TIFFfreeExt(tif, sp->dctable[3]); + if (sp->actable[0] != 0) + _TIFFfreeExt(tif, sp->actable[0]); + if (sp->actable[1] != 0) + _TIFFfreeExt(tif, sp->actable[1]); + if (sp->actable[2] != 0) + _TIFFfreeExt(tif, sp->actable[2]); + if (sp->actable[3] != 0) + _TIFFfreeExt(tif, sp->actable[3]); + if (sp->libjpeg_session_active != 0) + OJPEGLibjpegSessionAbort(tif); + if (sp->subsampling_convert_ycbcrbuf != 0) + _TIFFfreeExt(tif, sp->subsampling_convert_ycbcrbuf); + if (sp->subsampling_convert_ycbcrimage != 0) + _TIFFfreeExt(tif, sp->subsampling_convert_ycbcrimage); + if (sp->skip_buffer != 0) + _TIFFfreeExt(tif, sp->skip_buffer); + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + _TIFFSetDefaultCompressionState(tif); + } +} + +static void OJPEGSubsamplingCorrect(TIFF *tif) +{ + static const char module[] = "OJPEGSubsamplingCorrect"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t mh; + uint8_t mv; + + assert(sp->subsamplingcorrect_done == 0); + if ((tif->tif_dir.td_samplesperpixel != 3) || + ((tif->tif_dir.td_photometric != PHOTOMETRIC_YCBCR) && + (tif->tif_dir.td_photometric != PHOTOMETRIC_ITULAB))) + { + if (sp->subsampling_tag != 0) + TIFFWarningExtR(tif, module, + "Subsampling tag not appropriate for this " + "Photometric and/or SamplesPerPixel"); + sp->subsampling_hor = 1; + sp->subsampling_ver = 1; + sp->subsampling_force_desubsampling_inside_decompression = 0; + } + else + { + sp->subsamplingcorrect_done = 1; + mh = sp->subsampling_hor; + mv = sp->subsampling_ver; + sp->subsamplingcorrect = 1; + OJPEGReadHeaderInfoSec(tif); + if (sp->subsampling_force_desubsampling_inside_decompression != 0) + { + sp->subsampling_hor = 1; + sp->subsampling_ver = 1; + } + sp->subsamplingcorrect = 0; + if (((sp->subsampling_hor != mh) || (sp->subsampling_ver != mv)) && + (sp->subsampling_force_desubsampling_inside_decompression == 0)) + { + if (sp->subsampling_tag == 0) + TIFFWarningExtR( + tif, module, + "Subsampling tag is not set, yet subsampling inside JPEG " + "data [%" PRIu8 ",%" PRIu8 + "] does not match default values [2,2]; assuming " + "subsampling inside JPEG data is correct", + sp->subsampling_hor, sp->subsampling_ver); + else + TIFFWarningExtR( + tif, module, + "Subsampling inside JPEG data [%" PRIu8 ",%" PRIu8 + "] does not match subsampling tag values [%" PRIu8 + ",%" PRIu8 + "]; assuming subsampling inside JPEG data is correct", + sp->subsampling_hor, sp->subsampling_ver, mh, mv); + } + if (sp->subsampling_force_desubsampling_inside_decompression != 0) + { + if (sp->subsampling_tag == 0) + TIFFWarningExtR( + tif, module, + "Subsampling tag is not set, yet subsampling inside JPEG " + "data does not match default values [2,2] (nor any other " + "values allowed in TIFF); assuming subsampling inside JPEG " + "data is correct and desubsampling inside JPEG " + "decompression"); + else + TIFFWarningExtR( + tif, module, + "Subsampling inside JPEG data does not match subsampling " + "tag values [%" PRIu8 ",%" PRIu8 + "] (nor any other values allowed in TIFF); assuming " + "subsampling inside JPEG data is correct and desubsampling " + "inside JPEG decompression", + mh, mv); + } + if (sp->subsampling_force_desubsampling_inside_decompression == 0) + { + if (sp->subsampling_hor < sp->subsampling_ver) + TIFFWarningExtR(tif, module, + "Subsampling values [%" PRIu8 ",%" PRIu8 + "] are not allowed in TIFF", + sp->subsampling_hor, sp->subsampling_ver); + } + } + sp->subsamplingcorrect_done = 1; +} + +static int OJPEGReadHeaderInfo(TIFF *tif) +{ + static const char module[] = "OJPEGReadHeaderInfo"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + assert(sp->readheader_done == 0); + sp->image_width = tif->tif_dir.td_imagewidth; + sp->image_length = tif->tif_dir.td_imagelength; + if (isTiled(tif)) + { + sp->strile_width = tif->tif_dir.td_tilewidth; + sp->strile_length = tif->tif_dir.td_tilelength; + sp->strile_length_total = + ((sp->image_length + sp->strile_length - 1) / sp->strile_length) * + sp->strile_length; + } + else + { + sp->strile_width = sp->image_width; + sp->strile_length = tif->tif_dir.td_rowsperstrip; + if (sp->strile_length == (uint32_t)-1) + sp->strile_length = sp->image_length; + sp->strile_length_total = sp->image_length; + } + if (tif->tif_dir.td_samplesperpixel == 1) + { + sp->samples_per_pixel = 1; + sp->plane_sample_offset = 0; + sp->samples_per_pixel_per_plane = sp->samples_per_pixel; + sp->subsampling_hor = 1; + sp->subsampling_ver = 1; + } + else + { + if (tif->tif_dir.td_samplesperpixel != 3) + { + TIFFErrorExtR(tif, module, + "SamplesPerPixel %" PRIu8 + " not supported for this compression scheme", + sp->samples_per_pixel); + return (0); + } + sp->samples_per_pixel = 3; + sp->plane_sample_offset = 0; + if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) + sp->samples_per_pixel_per_plane = 3; + else + sp->samples_per_pixel_per_plane = 1; + } + if (sp->strile_length < sp->image_length) + { + if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) && + (sp->subsampling_hor != 4)) || + ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) && + (sp->subsampling_ver != 4))) + { + TIFFErrorExtR(tif, module, "Invalid subsampling values"); + return (0); + } + if (sp->strile_length % (sp->subsampling_ver * 8) != 0) + { + TIFFErrorExtR(tif, module, + "Incompatible vertical subsampling and image " + "strip/tile length"); + return (0); + } + sp->restart_interval = + (uint16_t)(((sp->strile_width + sp->subsampling_hor * 8 - 1) / + (sp->subsampling_hor * 8)) * + (sp->strile_length / (sp->subsampling_ver * 8))); + } + if (OJPEGReadHeaderInfoSec(tif) == 0) + return (0); + sp->sos_end[0].log = 1; + sp->sos_end[0].in_buffer_source = sp->in_buffer_source; + sp->sos_end[0].in_buffer_next_strile = sp->in_buffer_next_strile; + sp->sos_end[0].in_buffer_file_pos = + sp->in_buffer_file_pos - sp->in_buffer_togo; + sp->sos_end[0].in_buffer_file_togo = + sp->in_buffer_file_togo + sp->in_buffer_togo; + sp->readheader_done = 1; + return (1); +} + +static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + assert(s > 0); + assert(s < 3); + assert(sp->sos_end[0].log != 0); + assert(sp->sos_end[s].log == 0); + sp->plane_sample_offset = (uint8_t)(s - 1); + while (sp->sos_end[sp->plane_sample_offset].log == 0) + sp->plane_sample_offset--; + sp->in_buffer_source = + sp->sos_end[sp->plane_sample_offset].in_buffer_source; + sp->in_buffer_next_strile = + sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile; + sp->in_buffer_file_pos = + sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos; + sp->in_buffer_file_pos_log = 0; + sp->in_buffer_file_togo = + sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo; + sp->in_buffer_togo = 0; + sp->in_buffer_cur = 0; + while (sp->plane_sample_offset < s) + { + do + { + if (OJPEGReadByte(sp, &m) == 0) + return (0); + if (m == 255) + { + do + { + if (OJPEGReadByte(sp, &m) == 0) + return (0); + if (m != 255) + break; + } while (1); + if (m == JPEG_MARKER_SOS) + break; + } + } while (1); + sp->plane_sample_offset++; + if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0) + return (0); + sp->sos_end[sp->plane_sample_offset].log = 1; + sp->sos_end[sp->plane_sample_offset].in_buffer_source = + sp->in_buffer_source; + sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile = + sp->in_buffer_next_strile; + sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos = + sp->in_buffer_file_pos - sp->in_buffer_togo; + sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo = + sp->in_buffer_file_togo + sp->in_buffer_togo; + } + return (1); +} + +static int OJPEGWriteHeaderInfo(TIFF *tif) +{ + static const char module[] = "OJPEGWriteHeaderInfo"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t **m; + uint32_t n; + /* if a previous attempt failed, don't try again */ + if (sp->libjpeg_session_active != 0) + return 0; + sp->out_state = ososSoi; + sp->restart_index = 0; + jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr)); + sp->libjpeg_jpeg_error_mgr.output_message = + OJPEGLibjpegJpegErrorMgrOutputMessage; + sp->libjpeg_jpeg_error_mgr.error_exit = OJPEGLibjpegJpegErrorMgrErrorExit; + sp->libjpeg_jpeg_decompress_struct.err = &(sp->libjpeg_jpeg_error_mgr); + sp->libjpeg_jpeg_decompress_struct.client_data = (void *)tif; + if (jpeg_create_decompress_encap( + sp, &(sp->libjpeg_jpeg_decompress_struct)) == 0) + return (0); + sp->libjpeg_session_active = 1; + sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = 0; + sp->libjpeg_jpeg_source_mgr.init_source = + OJPEGLibjpegJpegSourceMgrInitSource; + sp->libjpeg_jpeg_source_mgr.fill_input_buffer = + OJPEGLibjpegJpegSourceMgrFillInputBuffer; + sp->libjpeg_jpeg_source_mgr.skip_input_data = + OJPEGLibjpegJpegSourceMgrSkipInputData; + sp->libjpeg_jpeg_source_mgr.resync_to_restart = + OJPEGLibjpegJpegSourceMgrResyncToRestart; + sp->libjpeg_jpeg_source_mgr.term_source = + OJPEGLibjpegJpegSourceMgrTermSource; + sp->libjpeg_jpeg_decompress_struct.src = &(sp->libjpeg_jpeg_source_mgr); + if (jpeg_read_header_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), 1) == + 0) + return (0); + if ((sp->subsampling_force_desubsampling_inside_decompression == 0) && + (sp->samples_per_pixel_per_plane > 1)) + { + sp->libjpeg_jpeg_decompress_struct.raw_data_out = 1; +#if JPEG_LIB_VERSION >= 70 + sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling = FALSE; +#endif + sp->libjpeg_jpeg_query_style = 0; + if (sp->subsampling_convert_log == 0) + { + assert(sp->subsampling_convert_ycbcrbuf == 0); + assert(sp->subsampling_convert_ycbcrimage == 0); + /* Check for division by zero. */ + if (sp->subsampling_hor == 0 || sp->subsampling_ver == 0) + return (0); + sp->subsampling_convert_ylinelen = + ((sp->strile_width + sp->subsampling_hor * 8 - 1) / + (sp->subsampling_hor * 8) * sp->subsampling_hor * 8); + sp->subsampling_convert_ylines = sp->subsampling_ver * 8; + sp->subsampling_convert_clinelen = + sp->subsampling_convert_ylinelen / sp->subsampling_hor; + sp->subsampling_convert_clines = 8; + sp->subsampling_convert_ybuflen = sp->subsampling_convert_ylinelen * + sp->subsampling_convert_ylines; + sp->subsampling_convert_cbuflen = sp->subsampling_convert_clinelen * + sp->subsampling_convert_clines; + sp->subsampling_convert_ycbcrbuflen = + sp->subsampling_convert_ybuflen + + 2 * sp->subsampling_convert_cbuflen; + /* The calloc is not normally necessary, except in some edge/broken + * cases */ + /* for example for a tiled image of height 1 with a tile height of 1 + * and subsampling_hor=subsampling_ver=2 */ + /* In that case, libjpeg will only fill the 8 first lines of the 16 + * lines */ + /* See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16844 + */ + /* Even if this case is allowed (?), its handling is broken because + * OJPEGPreDecode() should also likely */ + /* reset subsampling_convert_state to 0 when changing tile. */ + sp->subsampling_convert_ycbcrbuf = + _TIFFcallocExt(tif, 1, sp->subsampling_convert_ycbcrbuflen); + if (sp->subsampling_convert_ycbcrbuf == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + sp->subsampling_convert_ybuf = sp->subsampling_convert_ycbcrbuf; + sp->subsampling_convert_cbbuf = + sp->subsampling_convert_ybuf + sp->subsampling_convert_ybuflen; + sp->subsampling_convert_crbuf = + sp->subsampling_convert_cbbuf + sp->subsampling_convert_cbuflen; + sp->subsampling_convert_ycbcrimagelen = + 3 + sp->subsampling_convert_ylines + + 2 * sp->subsampling_convert_clines; + sp->subsampling_convert_ycbcrimage = _TIFFmallocExt( + tif, sp->subsampling_convert_ycbcrimagelen * sizeof(uint8_t *)); + if (sp->subsampling_convert_ycbcrimage == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + m = sp->subsampling_convert_ycbcrimage; + *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3); + *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 + + sp->subsampling_convert_ylines); + *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 + + sp->subsampling_convert_ylines + + sp->subsampling_convert_clines); + for (n = 0; n < sp->subsampling_convert_ylines; n++) + *m++ = sp->subsampling_convert_ybuf + + n * sp->subsampling_convert_ylinelen; + for (n = 0; n < sp->subsampling_convert_clines; n++) + *m++ = sp->subsampling_convert_cbbuf + + n * sp->subsampling_convert_clinelen; + for (n = 0; n < sp->subsampling_convert_clines; n++) + *m++ = sp->subsampling_convert_crbuf + + n * sp->subsampling_convert_clinelen; + sp->subsampling_convert_clinelenout = + sp->strile_width / sp->subsampling_hor + + ((sp->strile_width % sp->subsampling_hor) != 0 ? 1 : 0); + sp->subsampling_convert_state = 0; + sp->error_in_raw_data_decoding = 0; + sp->bytes_per_line = + sp->subsampling_convert_clinelenout * + (sp->subsampling_ver * sp->subsampling_hor + 2); + sp->lines_per_strile = + sp->strile_length / sp->subsampling_ver + + ((sp->strile_length % sp->subsampling_ver) != 0 ? 1 : 0); + sp->subsampling_convert_log = 1; + } + } + else + { + sp->libjpeg_jpeg_decompress_struct.jpeg_color_space = JCS_UNKNOWN; + sp->libjpeg_jpeg_decompress_struct.out_color_space = JCS_UNKNOWN; + sp->libjpeg_jpeg_query_style = 1; + sp->bytes_per_line = sp->samples_per_pixel_per_plane * sp->strile_width; + sp->lines_per_strile = sp->strile_length; + } + if (jpeg_start_decompress_encap(sp, + &(sp->libjpeg_jpeg_decompress_struct)) == 0) + return (0); + if (sp->libjpeg_jpeg_decompress_struct.image_width != sp->strile_width) + { + TIFFErrorExtR(tif, module, + "jpeg_start_decompress() returned image_width = %u, " + "expected %" PRIu32, + sp->libjpeg_jpeg_decompress_struct.image_width, + sp->strile_width); + return 0; + } + if (sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor != + sp->subsampling_hor || + sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor != + sp->subsampling_ver) + { + TIFFErrorExtR(tif, module, + "jpeg_start_decompress() returned max_h_samp_factor = %d " + "and max_v_samp_factor = %d, expected %" PRIu8 + " and %" PRIu8, + sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor, + sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor, + sp->subsampling_hor, sp->subsampling_ver); + return 0; + } + + sp->writeheader_done = 1; + return (1); +} + +static void OJPEGLibjpegSessionAbort(TIFF *tif) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + assert(sp->libjpeg_session_active != 0); + jpeg_destroy((jpeg_common_struct *)(&(sp->libjpeg_jpeg_decompress_struct))); + sp->libjpeg_session_active = 0; +} + +static int OJPEGReadHeaderInfoSec(TIFF *tif) +{ + static const char module[] = "OJPEGReadHeaderInfoSec"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + uint16_t n; + uint8_t o; + if (sp->file_size == 0) + sp->file_size = TIFFGetFileSize(tif); + if (sp->jpeg_interchange_format != 0) + { + if (sp->jpeg_interchange_format >= sp->file_size) + { + sp->jpeg_interchange_format = 0; + sp->jpeg_interchange_format_length = 0; + } + else + { + if ((sp->jpeg_interchange_format_length == 0) || + (sp->jpeg_interchange_format > + UINT64_MAX - sp->jpeg_interchange_format_length) || + (sp->jpeg_interchange_format + + sp->jpeg_interchange_format_length > + sp->file_size)) + sp->jpeg_interchange_format_length = + sp->file_size - sp->jpeg_interchange_format; + } + } + sp->in_buffer_source = osibsNotSetYet; + sp->in_buffer_next_strile = 0; + sp->in_buffer_strile_count = tif->tif_dir.td_nstrips; + sp->in_buffer_file_togo = 0; + sp->in_buffer_togo = 0; + do + { + if (OJPEGReadBytePeek(sp, &m) == 0) + return (0); + if (m != 255) + break; + OJPEGReadByteAdvance(sp); + do + { + if (OJPEGReadByte(sp, &m) == 0) + return (0); + } while (m == 255); + switch (m) + { + case JPEG_MARKER_SOI: + /* this type of marker has no data, and should be skipped */ + break; + case JPEG_MARKER_COM: + case JPEG_MARKER_APP0: + case JPEG_MARKER_APP0 + 1: + case JPEG_MARKER_APP0 + 2: + case JPEG_MARKER_APP0 + 3: + case JPEG_MARKER_APP0 + 4: + case JPEG_MARKER_APP0 + 5: + case JPEG_MARKER_APP0 + 6: + case JPEG_MARKER_APP0 + 7: + case JPEG_MARKER_APP0 + 8: + case JPEG_MARKER_APP0 + 9: + case JPEG_MARKER_APP0 + 10: + case JPEG_MARKER_APP0 + 11: + case JPEG_MARKER_APP0 + 12: + case JPEG_MARKER_APP0 + 13: + case JPEG_MARKER_APP0 + 14: + case JPEG_MARKER_APP0 + 15: + /* this type of marker has data, but it has no use to us (and no + * place here) and should be skipped */ + if (OJPEGReadWord(sp, &n) == 0) + return (0); + if (n < 2) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, "Corrupt JPEG data"); + return (0); + } + if (n > 2) + OJPEGReadSkip(sp, n - 2); + break; + case JPEG_MARKER_DRI: + if (OJPEGReadHeaderInfoSecStreamDri(tif) == 0) + return (0); + break; + case JPEG_MARKER_DQT: + if (OJPEGReadHeaderInfoSecStreamDqt(tif) == 0) + return (0); + break; + case JPEG_MARKER_DHT: + if (OJPEGReadHeaderInfoSecStreamDht(tif) == 0) + return (0); + break; + case JPEG_MARKER_SOF0: + case JPEG_MARKER_SOF1: + case JPEG_MARKER_SOF3: + if (OJPEGReadHeaderInfoSecStreamSof(tif, m) == 0) + return (0); + if (sp->subsamplingcorrect != 0) + return (1); + break; + case JPEG_MARKER_SOS: + if (sp->subsamplingcorrect != 0) + return (1); + assert(sp->plane_sample_offset == 0); + if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0) + return (0); + break; + default: + TIFFErrorExtR(tif, module, + "Unknown marker type %" PRIu8 " in JPEG data", m); + return (0); + } + } while (m != JPEG_MARKER_SOS); + if (sp->subsamplingcorrect) + return (1); + if (sp->sof_log == 0) + { + if (OJPEGReadHeaderInfoSecTablesQTable(tif) == 0) + return (0); + sp->sof_marker_id = JPEG_MARKER_SOF0; + for (o = 0; o < sp->samples_per_pixel; o++) + sp->sof_c[o] = o; + sp->sof_hv[0] = ((sp->subsampling_hor << 4) | sp->subsampling_ver); + for (o = 1; o < sp->samples_per_pixel; o++) + sp->sof_hv[o] = 17; + sp->sof_x = sp->strile_width; + sp->sof_y = sp->strile_length_total; + sp->sof_log = 1; + if (OJPEGReadHeaderInfoSecTablesDcTable(tif) == 0) + return (0); + if (OJPEGReadHeaderInfoSecTablesAcTable(tif) == 0) + return (0); + for (o = 1; o < sp->samples_per_pixel; o++) + sp->sos_cs[o] = o; + } + return (1); +} + +static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif) +{ + /* This could easily cause trouble in some cases... but no such cases have + occurred so far */ + static const char module[] = "OJPEGReadHeaderInfoSecStreamDri"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint16_t m; + if (OJPEGReadWord(sp, &m) == 0) + return (0); + if (m != 4) + { + TIFFErrorExtR(tif, module, "Corrupt DRI marker in JPEG data"); + return (0); + } + if (OJPEGReadWord(sp, &m) == 0) + return (0); + sp->restart_interval = m; + return (1); +} + +static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif) +{ + /* this is a table marker, and it is to be saved as a whole for exact + * pushing on the jpeg stream later on */ + static const char module[] = "OJPEGReadHeaderInfoSecStreamDqt"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint16_t m; + uint32_t na; + uint8_t *nb; + uint8_t o; + if (OJPEGReadWord(sp, &m) == 0) + return (0); + if (m <= 2) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data"); + return (0); + } + if (sp->subsamplingcorrect != 0) + OJPEGReadSkip(sp, m - 2); + else + { + m -= 2; + do + { + if (m < 65) + { + TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data"); + return (0); + } + na = sizeof(uint32_t) + 69; + nb = _TIFFmallocExt(tif, na); + if (nb == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + *(uint32_t *)nb = na; + nb[sizeof(uint32_t)] = 255; + nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT; + nb[sizeof(uint32_t) + 2] = 0; + nb[sizeof(uint32_t) + 3] = 67; + if (OJPEGReadBlock(sp, 65, &nb[sizeof(uint32_t) + 4]) == 0) + { + _TIFFfreeExt(tif, nb); + return (0); + } + o = nb[sizeof(uint32_t) + 4] & 15; + if (3 < o) + { + TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data"); + _TIFFfreeExt(tif, nb); + return (0); + } + if (sp->qtable[o] != 0) + _TIFFfreeExt(tif, sp->qtable[o]); + sp->qtable[o] = nb; + m -= 65; + } while (m > 0); + } + return (1); +} + +static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif) +{ + /* this is a table marker, and it is to be saved as a whole for exact + * pushing on the jpeg stream later on */ + /* TODO: the following assumes there is only one table in this marker... but + * i'm not quite sure that assumption is guaranteed correct */ + static const char module[] = "OJPEGReadHeaderInfoSecStreamDht"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint16_t m; + uint32_t na; + uint8_t *nb; + uint8_t o; + if (OJPEGReadWord(sp, &m) == 0) + return (0); + if (m <= 2) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); + return (0); + } + if (sp->subsamplingcorrect != 0) + { + OJPEGReadSkip(sp, m - 2); + } + else + { + na = sizeof(uint32_t) + 2 + m; + nb = _TIFFmallocExt(tif, na); + if (nb == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + *(uint32_t *)nb = na; + nb[sizeof(uint32_t)] = 255; + nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT; + nb[sizeof(uint32_t) + 2] = (m >> 8); + nb[sizeof(uint32_t) + 3] = (m & 255); + if (OJPEGReadBlock(sp, m - 2, &nb[sizeof(uint32_t) + 4]) == 0) + { + _TIFFfreeExt(tif, nb); + return (0); + } + o = nb[sizeof(uint32_t) + 4]; + if ((o & 240) == 0) + { + if (3 < o) + { + TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); + _TIFFfreeExt(tif, nb); + return (0); + } + if (sp->dctable[o] != 0) + _TIFFfreeExt(tif, sp->dctable[o]); + sp->dctable[o] = nb; + } + else + { + if ((o & 240) != 16) + { + TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); + _TIFFfreeExt(tif, nb); + return (0); + } + o &= 15; + if (3 < o) + { + TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); + _TIFFfreeExt(tif, nb); + return (0); + } + if (sp->actable[o] != 0) + _TIFFfreeExt(tif, sp->actable[o]); + sp->actable[o] = nb; + } + } + return (1); +} + +static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id) +{ + /* this marker needs to be checked, and part of its data needs to be saved + * for regeneration later on */ + static const char module[] = "OJPEGReadHeaderInfoSecStreamSof"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint16_t m; + uint16_t n; + uint8_t o; + uint16_t p; + uint16_t q; + if (sp->sof_log != 0) + { + TIFFErrorExtR(tif, module, "Corrupt JPEG data"); + return (0); + } + if (sp->subsamplingcorrect == 0) + sp->sof_marker_id = marker_id; + /* Lf: data length */ + if (OJPEGReadWord(sp, &m) == 0) + return (0); + if (m < 11) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data"); + return (0); + } + m -= 8; + if (m % 3 != 0) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data"); + return (0); + } + n = m / 3; + if (sp->subsamplingcorrect == 0) + { + if (n != sp->samples_per_pixel) + { + TIFFErrorExtR( + tif, module, + "JPEG compressed data indicates unexpected number of samples"); + return (0); + } + } + /* P: Sample precision */ + if (OJPEGReadByte(sp, &o) == 0) + return (0); + if (o != 8) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, + "JPEG compressed data indicates unexpected number of " + "bits per sample"); + return (0); + } + /* Y: Number of lines, X: Number of samples per line */ + if (sp->subsamplingcorrect) + OJPEGReadSkip(sp, 4); + else + { + /* Y: Number of lines */ + if (OJPEGReadWord(sp, &p) == 0) + return (0); + if (((uint32_t)p < sp->image_length) && + ((uint32_t)p < sp->strile_length_total)) + { + TIFFErrorExtR(tif, module, + "JPEG compressed data indicates unexpected height"); + return (0); + } + sp->sof_y = p; + /* X: Number of samples per line */ + if (OJPEGReadWord(sp, &p) == 0) + return (0); + if (((uint32_t)p < sp->image_width) && ((uint32_t)p < sp->strile_width)) + { + TIFFErrorExtR(tif, module, + "JPEG compressed data indicates unexpected width"); + return (0); + } + if ((uint32_t)p > sp->strile_width) + { + TIFFErrorExtR(tif, module, + "JPEG compressed data image width exceeds expected " + "image width"); + return (0); + } + sp->sof_x = p; + } + /* Nf: Number of image components in frame */ + if (OJPEGReadByte(sp, &o) == 0) + return (0); + if (o != n) + { + if (sp->subsamplingcorrect == 0) + TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data"); + return (0); + } + /* per component stuff */ + /* TODO: double-check that flow implies that n cannot be as big as to make + * us overflow sof_c, sof_hv and sof_tq arrays */ + for (q = 0; q < n; q++) + { + /* C: Component identifier */ + if (OJPEGReadByte(sp, &o) == 0) + return (0); + if (sp->subsamplingcorrect == 0) + sp->sof_c[q] = o; + /* H: Horizontal sampling factor, and V: Vertical sampling factor */ + if (OJPEGReadByte(sp, &o) == 0) + return (0); + if (sp->subsamplingcorrect != 0) + { + if (q == 0) + { + sp->subsampling_hor = (o >> 4); + sp->subsampling_ver = (o & 15); + if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) && + (sp->subsampling_hor != 4)) || + ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) && + (sp->subsampling_ver != 4))) + sp->subsampling_force_desubsampling_inside_decompression = + 1; + } + else + { + if (o != 17) + sp->subsampling_force_desubsampling_inside_decompression = + 1; + } + } + else + { + sp->sof_hv[q] = o; + if (sp->subsampling_force_desubsampling_inside_decompression == 0) + { + if (q == 0) + { + if (o != ((sp->subsampling_hor << 4) | sp->subsampling_ver)) + { + TIFFErrorExtR(tif, module, + "JPEG compressed data indicates " + "unexpected subsampling values"); + return (0); + } + } + else + { + if (o != 17) + { + TIFFErrorExtR(tif, module, + "JPEG compressed data indicates " + "unexpected subsampling values"); + return (0); + } + } + } + } + /* Tq: Quantization table destination selector */ + if (OJPEGReadByte(sp, &o) == 0) + return (0); + if (sp->subsamplingcorrect == 0) + sp->sof_tq[q] = o; + } + if (sp->subsamplingcorrect == 0) + sp->sof_log = 1; + return (1); +} + +static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif) +{ + /* this marker needs to be checked, and part of its data needs to be saved + * for regeneration later on */ + static const char module[] = "OJPEGReadHeaderInfoSecStreamSos"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint16_t m; + uint8_t n; + uint8_t o; + assert(sp->subsamplingcorrect == 0); + if (sp->sof_log == 0) + { + TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data"); + return (0); + } + /* Ls */ + if (OJPEGReadWord(sp, &m) == 0) + return (0); + if (m != 6 + sp->samples_per_pixel_per_plane * 2) + { + TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data"); + return (0); + } + /* Ns */ + if (OJPEGReadByte(sp, &n) == 0) + return (0); + if (n != sp->samples_per_pixel_per_plane) + { + TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data"); + return (0); + } + /* Cs, Td, and Ta */ + for (o = 0; o < sp->samples_per_pixel_per_plane; o++) + { + /* Cs */ + if (OJPEGReadByte(sp, &n) == 0) + return (0); + sp->sos_cs[sp->plane_sample_offset + o] = n; + /* Td and Ta */ + if (OJPEGReadByte(sp, &n) == 0) + return (0); + sp->sos_tda[sp->plane_sample_offset + o] = n; + } + /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as + * per LibJpeg source */ + OJPEGReadSkip(sp, 3); + return (1); +} + +static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif) +{ + static const char module[] = "OJPEGReadHeaderInfoSecTablesQTable"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + uint8_t n; + uint32_t oa; + uint8_t *ob; + uint32_t p; + if (sp->qtable_offset[0] == 0) + { + TIFFErrorExtR(tif, module, "Missing JPEG tables"); + return (0); + } + sp->in_buffer_file_pos_log = 0; + for (m = 0; m < sp->samples_per_pixel; m++) + { + if ((sp->qtable_offset[m] != 0) && + ((m == 0) || (sp->qtable_offset[m] != sp->qtable_offset[m - 1]))) + { + for (n = 0; n < m - 1; n++) + { + if (sp->qtable_offset[m] == sp->qtable_offset[n]) + { + TIFFErrorExtR(tif, module, "Corrupt JpegQTables tag value"); + return (0); + } + } + oa = sizeof(uint32_t) + 69; + ob = _TIFFmallocExt(tif, oa); + if (ob == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + *(uint32_t *)ob = oa; + ob[sizeof(uint32_t)] = 255; + ob[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT; + ob[sizeof(uint32_t) + 2] = 0; + ob[sizeof(uint32_t) + 3] = 67; + ob[sizeof(uint32_t) + 4] = m; + TIFFSeekFile(tif, sp->qtable_offset[m], SEEK_SET); + p = (uint32_t)TIFFReadFile(tif, &ob[sizeof(uint32_t) + 5], 64); + if (p != 64) + { + _TIFFfreeExt(tif, ob); + return (0); + } + if (sp->qtable[m] != 0) + _TIFFfreeExt(tif, sp->qtable[m]); + sp->qtable[m] = ob; + sp->sof_tq[m] = m; + } + else + sp->sof_tq[m] = sp->sof_tq[m - 1]; + } + return (1); +} + +static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif) +{ + static const char module[] = "OJPEGReadHeaderInfoSecTablesDcTable"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + uint8_t n; + uint8_t o[16]; + uint32_t p; + uint32_t q; + uint32_t ra; + uint8_t *rb; + if (sp->dctable_offset[0] == 0) + { + TIFFErrorExtR(tif, module, "Missing JPEG tables"); + return (0); + } + sp->in_buffer_file_pos_log = 0; + for (m = 0; m < sp->samples_per_pixel; m++) + { + if ((sp->dctable_offset[m] != 0) && + ((m == 0) || (sp->dctable_offset[m] != sp->dctable_offset[m - 1]))) + { + for (n = 0; n < m - 1; n++) + { + if (sp->dctable_offset[m] == sp->dctable_offset[n]) + { + TIFFErrorExtR(tif, module, + "Corrupt JpegDcTables tag value"); + return (0); + } + } + TIFFSeekFile(tif, sp->dctable_offset[m], SEEK_SET); + p = (uint32_t)TIFFReadFile(tif, o, 16); + if (p != 16) + return (0); + q = 0; + for (n = 0; n < 16; n++) + q += o[n]; + ra = sizeof(uint32_t) + 21 + q; + rb = _TIFFmallocExt(tif, ra); + if (rb == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + *(uint32_t *)rb = ra; + rb[sizeof(uint32_t)] = 255; + rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT; + rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8); + rb[sizeof(uint32_t) + 3] = ((19 + q) & 255); + rb[sizeof(uint32_t) + 4] = m; + for (n = 0; n < 16; n++) + rb[sizeof(uint32_t) + 5 + n] = o[n]; + p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q); + if (p != q) + { + _TIFFfreeExt(tif, rb); + return (0); + } + if (sp->dctable[m] != 0) + _TIFFfreeExt(tif, sp->dctable[m]); + sp->dctable[m] = rb; + sp->sos_tda[m] = (m << 4); + } + else + sp->sos_tda[m] = sp->sos_tda[m - 1]; + } + return (1); +} + +static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif) +{ + static const char module[] = "OJPEGReadHeaderInfoSecTablesAcTable"; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + uint8_t n; + uint8_t o[16]; + uint32_t p; + uint32_t q; + uint32_t ra; + uint8_t *rb; + if (sp->actable_offset[0] == 0) + { + TIFFErrorExtR(tif, module, "Missing JPEG tables"); + return (0); + } + sp->in_buffer_file_pos_log = 0; + for (m = 0; m < sp->samples_per_pixel; m++) + { + if ((sp->actable_offset[m] != 0) && + ((m == 0) || (sp->actable_offset[m] != sp->actable_offset[m - 1]))) + { + for (n = 0; n < m - 1; n++) + { + if (sp->actable_offset[m] == sp->actable_offset[n]) + { + TIFFErrorExtR(tif, module, + "Corrupt JpegAcTables tag value"); + return (0); + } + } + TIFFSeekFile(tif, sp->actable_offset[m], SEEK_SET); + p = (uint32_t)TIFFReadFile(tif, o, 16); + if (p != 16) + return (0); + q = 0; + for (n = 0; n < 16; n++) + q += o[n]; + ra = sizeof(uint32_t) + 21 + q; + rb = _TIFFmallocExt(tif, ra); + if (rb == 0) + { + TIFFErrorExtR(tif, module, "Out of memory"); + return (0); + } + *(uint32_t *)rb = ra; + rb[sizeof(uint32_t)] = 255; + rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT; + rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8); + rb[sizeof(uint32_t) + 3] = ((19 + q) & 255); + rb[sizeof(uint32_t) + 4] = (16 | m); + for (n = 0; n < 16; n++) + rb[sizeof(uint32_t) + 5 + n] = o[n]; + p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q); + if (p != q) + { + _TIFFfreeExt(tif, rb); + return (0); + } + if (sp->actable[m] != 0) + _TIFFfreeExt(tif, sp->actable[m]); + sp->actable[m] = rb; + sp->sos_tda[m] = (sp->sos_tda[m] | m); + } + else + sp->sos_tda[m] = (sp->sos_tda[m] | (sp->sos_tda[m - 1] & 15)); + } + return (1); +} + +static int OJPEGReadBufferFill(OJPEGState *sp) +{ + uint16_t m; + tmsize_t n; + /* TODO: double-check: when subsamplingcorrect is set, no call to + * TIFFErrorExt or TIFFWarningExt should be made in any other case, seek or + * read errors should be passed through */ + do + { + if (sp->in_buffer_file_togo != 0) + { + if (sp->in_buffer_file_pos_log == 0) + { + TIFFSeekFile(sp->tif, sp->in_buffer_file_pos, SEEK_SET); + sp->in_buffer_file_pos_log = 1; + } + m = OJPEG_BUFFER; + if ((uint64_t)m > sp->in_buffer_file_togo) + m = (uint16_t)sp->in_buffer_file_togo; + n = TIFFReadFile(sp->tif, sp->in_buffer, (tmsize_t)m); + if (n == 0) + return (0); + assert(n > 0); + assert(n <= OJPEG_BUFFER); + assert(n < 65536); + assert((uint64_t)n <= sp->in_buffer_file_togo); + m = (uint16_t)n; + sp->in_buffer_togo = m; + sp->in_buffer_cur = sp->in_buffer; + sp->in_buffer_file_togo -= m; + sp->in_buffer_file_pos += m; + break; + } + sp->in_buffer_file_pos_log = 0; + switch (sp->in_buffer_source) + { + case osibsNotSetYet: + if (sp->jpeg_interchange_format != 0) + { + sp->in_buffer_file_pos = sp->jpeg_interchange_format; + sp->in_buffer_file_togo = + sp->jpeg_interchange_format_length; + } + sp->in_buffer_source = osibsJpegInterchangeFormat; + break; + case osibsJpegInterchangeFormat: + sp->in_buffer_source = osibsStrile; + break; + case osibsStrile: + if (sp->in_buffer_next_strile == sp->in_buffer_strile_count) + sp->in_buffer_source = osibsEof; + else + { + int err = 0; + sp->in_buffer_file_pos = TIFFGetStrileOffsetWithErr( + sp->tif, sp->in_buffer_next_strile, &err); + if (err) + return 0; + if (sp->in_buffer_file_pos != 0) + { + uint64_t bytecount = TIFFGetStrileByteCountWithErr( + sp->tif, sp->in_buffer_next_strile, &err); + if (err) + return 0; + if (sp->in_buffer_file_pos >= sp->file_size) + sp->in_buffer_file_pos = 0; + else if (bytecount == 0) + sp->in_buffer_file_togo = + sp->file_size - sp->in_buffer_file_pos; + else + { + sp->in_buffer_file_togo = bytecount; + if (sp->in_buffer_file_togo == 0) + sp->in_buffer_file_pos = 0; + else if (sp->in_buffer_file_pos > + UINT64_MAX - sp->in_buffer_file_togo || + sp->in_buffer_file_pos + + sp->in_buffer_file_togo > + sp->file_size) + sp->in_buffer_file_togo = + sp->file_size - sp->in_buffer_file_pos; + } + } + sp->in_buffer_next_strile++; + } + break; + default: + return (0); + } + } while (1); + return (1); +} + +static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte) +{ + if (sp->in_buffer_togo == 0) + { + if (OJPEGReadBufferFill(sp) == 0) + return (0); + assert(sp->in_buffer_togo > 0); + } + *byte = *(sp->in_buffer_cur); + sp->in_buffer_cur++; + sp->in_buffer_togo--; + return (1); +} + +static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte) +{ + if (sp->in_buffer_togo == 0) + { + if (OJPEGReadBufferFill(sp) == 0) + return (0); + assert(sp->in_buffer_togo > 0); + } + *byte = *(sp->in_buffer_cur); + return (1); +} + +static void OJPEGReadByteAdvance(OJPEGState *sp) +{ + assert(sp->in_buffer_togo > 0); + sp->in_buffer_cur++; + sp->in_buffer_togo--; +} + +static int OJPEGReadWord(OJPEGState *sp, uint16_t *word) +{ + uint8_t m; + if (OJPEGReadByte(sp, &m) == 0) + return (0); + *word = (m << 8); + if (OJPEGReadByte(sp, &m) == 0) + return (0); + *word |= m; + return (1); +} + +static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem) +{ + uint16_t mlen; + uint8_t *mmem; + uint16_t n; + assert(len > 0); + mlen = len; + mmem = mem; + do + { + if (sp->in_buffer_togo == 0) + { + if (OJPEGReadBufferFill(sp) == 0) + return (0); + assert(sp->in_buffer_togo > 0); + } + n = mlen; + if (n > sp->in_buffer_togo) + n = sp->in_buffer_togo; + _TIFFmemcpy(mmem, sp->in_buffer_cur, n); + sp->in_buffer_cur += n; + sp->in_buffer_togo -= n; + mlen -= n; + mmem += n; + } while (mlen > 0); + return (1); +} + +static void OJPEGReadSkip(OJPEGState *sp, uint16_t len) +{ + uint16_t m; + uint16_t n; + m = len; + n = m; + if (n > sp->in_buffer_togo) + n = sp->in_buffer_togo; + sp->in_buffer_cur += n; + sp->in_buffer_togo -= n; + m -= n; + if (m > 0) + { + assert(sp->in_buffer_togo == 0); + n = m; + if ((uint64_t)n > sp->in_buffer_file_togo) + n = (uint16_t)sp->in_buffer_file_togo; + sp->in_buffer_file_pos += n; + sp->in_buffer_file_togo -= n; + sp->in_buffer_file_pos_log = 0; + /* we don't skip past jpeginterchangeformat/strile block... + * if that is asked from us, we're dealing with totally bazurk + * data anyway, and we've not seen this happening on any + * testfile, so we might as well likely cause some other + * meaningless error to be passed at some later time + */ + } +} + +static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + *len = 0; + do + { + assert(sp->out_state <= ososEoi); + switch (sp->out_state) + { + case ososSoi: + OJPEGWriteStreamSoi(tif, mem, len); + break; + case ososQTable0: + OJPEGWriteStreamQTable(tif, 0, mem, len); + break; + case ososQTable1: + OJPEGWriteStreamQTable(tif, 1, mem, len); + break; + case ososQTable2: + OJPEGWriteStreamQTable(tif, 2, mem, len); + break; + case ososQTable3: + OJPEGWriteStreamQTable(tif, 3, mem, len); + break; + case ososDcTable0: + OJPEGWriteStreamDcTable(tif, 0, mem, len); + break; + case ososDcTable1: + OJPEGWriteStreamDcTable(tif, 1, mem, len); + break; + case ososDcTable2: + OJPEGWriteStreamDcTable(tif, 2, mem, len); + break; + case ososDcTable3: + OJPEGWriteStreamDcTable(tif, 3, mem, len); + break; + case ososAcTable0: + OJPEGWriteStreamAcTable(tif, 0, mem, len); + break; + case ososAcTable1: + OJPEGWriteStreamAcTable(tif, 1, mem, len); + break; + case ososAcTable2: + OJPEGWriteStreamAcTable(tif, 2, mem, len); + break; + case ososAcTable3: + OJPEGWriteStreamAcTable(tif, 3, mem, len); + break; + case ososDri: + OJPEGWriteStreamDri(tif, mem, len); + break; + case ososSof: + OJPEGWriteStreamSof(tif, mem, len); + break; + case ososSos: + OJPEGWriteStreamSos(tif, mem, len); + break; + case ososCompressed: + if (OJPEGWriteStreamCompressed(tif, mem, len) == 0) + return (0); + break; + case ososRst: + OJPEGWriteStreamRst(tif, mem, len); + break; + case ososEoi: + OJPEGWriteStreamEoi(tif, mem, len); + break; + } + } while (*len == 0); + return (1); +} + +static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + assert(OJPEG_BUFFER >= 2); + sp->out_buffer[0] = 255; + sp->out_buffer[1] = JPEG_MARKER_SOI; + *len = 2; + *mem = (void *)sp->out_buffer; + sp->out_state++; +} + +static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem, + uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + if (sp->qtable[table_index] != 0) + { + *mem = (void *)(sp->qtable[table_index] + sizeof(uint32_t)); + *len = *((uint32_t *)sp->qtable[table_index]) - sizeof(uint32_t); + } + sp->out_state++; +} + +static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem, + uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + if (sp->dctable[table_index] != 0) + { + *mem = (void *)(sp->dctable[table_index] + sizeof(uint32_t)); + *len = *((uint32_t *)sp->dctable[table_index]) - sizeof(uint32_t); + } + sp->out_state++; +} + +static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem, + uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + if (sp->actable[table_index] != 0) + { + *mem = (void *)(sp->actable[table_index] + sizeof(uint32_t)); + *len = *((uint32_t *)sp->actable[table_index]) - sizeof(uint32_t); + } + sp->out_state++; +} + +static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + assert(OJPEG_BUFFER >= 6); + if (sp->restart_interval != 0) + { + sp->out_buffer[0] = 255; + sp->out_buffer[1] = JPEG_MARKER_DRI; + sp->out_buffer[2] = 0; + sp->out_buffer[3] = 4; + sp->out_buffer[4] = (sp->restart_interval >> 8); + sp->out_buffer[5] = (sp->restart_interval & 255); + *len = 6; + *mem = (void *)sp->out_buffer; + } + sp->out_state++; +} + +static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + assert(OJPEG_BUFFER >= 2 + 8 + sp->samples_per_pixel_per_plane * 3); + assert(255 >= 8 + sp->samples_per_pixel_per_plane * 3); + sp->out_buffer[0] = 255; + sp->out_buffer[1] = sp->sof_marker_id; + /* Lf */ + sp->out_buffer[2] = 0; + sp->out_buffer[3] = 8 + sp->samples_per_pixel_per_plane * 3; + /* P */ + sp->out_buffer[4] = 8; + /* Y */ + sp->out_buffer[5] = (uint8_t)(sp->sof_y >> 8); + sp->out_buffer[6] = (sp->sof_y & 255); + /* X */ + sp->out_buffer[7] = (uint8_t)(sp->sof_x >> 8); + sp->out_buffer[8] = (sp->sof_x & 255); + /* Nf */ + sp->out_buffer[9] = sp->samples_per_pixel_per_plane; + for (m = 0; m < sp->samples_per_pixel_per_plane; m++) + { + /* C */ + sp->out_buffer[10 + m * 3] = sp->sof_c[sp->plane_sample_offset + m]; + /* H and V */ + sp->out_buffer[10 + m * 3 + 1] = + sp->sof_hv[sp->plane_sample_offset + m]; + /* Tq */ + sp->out_buffer[10 + m * 3 + 2] = + sp->sof_tq[sp->plane_sample_offset + m]; + } + *len = 10 + sp->samples_per_pixel_per_plane * 3; + *mem = (void *)sp->out_buffer; + sp->out_state++; +} + +static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + uint8_t m; + assert(OJPEG_BUFFER >= 2 + 6 + sp->samples_per_pixel_per_plane * 2); + assert(255 >= 6 + sp->samples_per_pixel_per_plane * 2); + sp->out_buffer[0] = 255; + sp->out_buffer[1] = JPEG_MARKER_SOS; + /* Ls */ + sp->out_buffer[2] = 0; + sp->out_buffer[3] = 6 + sp->samples_per_pixel_per_plane * 2; + /* Ns */ + sp->out_buffer[4] = sp->samples_per_pixel_per_plane; + for (m = 0; m < sp->samples_per_pixel_per_plane; m++) + { + /* Cs */ + sp->out_buffer[5 + m * 2] = sp->sos_cs[sp->plane_sample_offset + m]; + /* Td and Ta */ + sp->out_buffer[5 + m * 2 + 1] = + sp->sos_tda[sp->plane_sample_offset + m]; + } + /* Ss */ + sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2] = 0; + /* Se */ + sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 1] = 63; + /* Ah and Al */ + sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 2] = 0; + *len = 8 + sp->samples_per_pixel_per_plane * 2; + *mem = (void *)sp->out_buffer; + sp->out_state++; +} + +static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + if (sp->in_buffer_togo == 0) + { + if (OJPEGReadBufferFill(sp) == 0) + return (0); + assert(sp->in_buffer_togo > 0); + } + *len = sp->in_buffer_togo; + *mem = (void *)sp->in_buffer_cur; + sp->in_buffer_togo = 0; + if (sp->in_buffer_file_togo == 0) + { + switch (sp->in_buffer_source) + { + case osibsStrile: + if (sp->in_buffer_next_strile < sp->in_buffer_strile_count) + sp->out_state = ososRst; + else + sp->out_state = ososEoi; + break; + case osibsEof: + sp->out_state = ososEoi; + break; + default: + break; + } + } + return (1); +} + +static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + assert(OJPEG_BUFFER >= 2); + sp->out_buffer[0] = 255; + sp->out_buffer[1] = JPEG_MARKER_RST0 + sp->restart_index; + sp->restart_index++; + if (sp->restart_index == 8) + sp->restart_index = 0; + *len = 2; + *mem = (void *)sp->out_buffer; + sp->out_state = ososCompressed; +} + +static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + assert(OJPEG_BUFFER >= 2); + sp->out_buffer[0] = 255; + sp->out_buffer[1] = JPEG_MARKER_EOI; + *len = 2; + *mem = (void *)sp->out_buffer; +} + +#ifndef LIBJPEG_ENCAP_EXTERNAL +static int jpeg_create_decompress_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo) +{ + if (SETJMP(sp->exit_jmpbuf)) + return 0; + else + { + jpeg_create_decompress(cinfo); + return 1; + } +} +#endif + +#ifndef LIBJPEG_ENCAP_EXTERNAL +static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo, + uint8_t require_image) +{ + if (SETJMP(sp->exit_jmpbuf)) + return 0; + else + { + jpeg_read_header(cinfo, require_image); + return 1; + } +} +#endif + +#ifndef LIBJPEG_ENCAP_EXTERNAL +static int jpeg_start_decompress_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo) +{ + if (SETJMP(sp->exit_jmpbuf)) + return 0; + else + { + jpeg_start_decompress(cinfo); + return 1; + } +} +#endif + +#ifndef LIBJPEG_ENCAP_EXTERNAL +static int jpeg_read_scanlines_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo, + void *scanlines, uint32_t max_lines) +{ + if (SETJMP(sp->exit_jmpbuf)) + return 0; + else + { + jpeg_read_scanlines(cinfo, scanlines, max_lines); + return 1; + } +} +#endif + +#ifndef LIBJPEG_ENCAP_EXTERNAL +static int jpeg_read_raw_data_encap(OJPEGState *sp, + jpeg_decompress_struct *cinfo, void *data, + uint32_t max_lines) +{ + if (SETJMP(sp->exit_jmpbuf)) + return 0; + else + { + jpeg_read_raw_data(cinfo, data, max_lines); + return 1; + } +} +#endif + +#ifndef LIBJPEG_ENCAP_EXTERNAL +static void jpeg_encap_unwind(TIFF *tif) +{ + OJPEGState *sp = (OJPEGState *)tif->tif_data; + LONGJMP(sp->exit_jmpbuf, 1); +} +#endif + +static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo) +{ + char buffer[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, buffer); + TIFFWarningExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer); +} + +static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo) +{ + char buffer[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, buffer); + TIFFErrorExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer); + jpeg_encap_unwind((TIFF *)(cinfo->client_data)); +} + +static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo) +{ + (void)cinfo; +} + +static boolean +OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo) +{ + TIFF *tif = (TIFF *)cinfo->client_data; + OJPEGState *sp = (OJPEGState *)tif->tif_data; + void *mem = 0; + uint32_t len = 0U; + if (OJPEGWriteStream(tif, &mem, &len) == 0) + { + TIFFErrorExtR(tif, "LibJpeg", "Premature end of JPEG data"); + jpeg_encap_unwind(tif); + } + sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = len; + sp->libjpeg_jpeg_source_mgr.next_input_byte = mem; + return (1); +} + +static void +OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo, + long num_bytes) +{ + TIFF *tif = (TIFF *)cinfo->client_data; + (void)num_bytes; + TIFFErrorExtR(tif, "LibJpeg", "Unexpected error"); + jpeg_encap_unwind(tif); +} + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4702) /* unreachable code */ +#endif +static boolean +OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo, + int desired) +{ + TIFF *tif = (TIFF *)cinfo->client_data; + (void)desired; + TIFFErrorExtR(tif, "LibJpeg", "Unexpected error"); + jpeg_encap_unwind(tif); + return (0); +} +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo) +{ + (void)cinfo; +} + +#endif diff --git a/cpp/3rd_party/libtiff/tif_open.c b/cpp/3rd_party/libtiff/tif_open.c new file mode 100644 index 0000000000..565d13cf69 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_open.c @@ -0,0 +1,945 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + */ + +#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS +#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS +#endif + +#include "tiffiop.h" +#include +#include + +/* + * Dummy functions to fill the omitted client procedures. + */ +int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize) +{ + (void)fd; + (void)pbase; + (void)psize; + return (0); +} + +void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size) +{ + (void)fd; + (void)base; + (void)size; +} + +int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode, + const char *module) +{ + int m = -1; + + switch (mode[0]) + { + case 'r': + m = O_RDONLY; + if (mode[1] == '+') + m = O_RDWR; + break; + case 'w': + case 'a': + m = O_RDWR | O_CREAT; + if (mode[0] == 'w') + m |= O_TRUNC; + break; + default: + _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode); + break; + } + return (m); +} + +TIFFOpenOptions *TIFFOpenOptionsAlloc() +{ + TIFFOpenOptions *opts = + (TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions)); + return opts; +} + +void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); } + +/** Define a limit in bytes for a single memory allocation done by libtiff. + * If max_single_mem_alloc is set to 0, which is the default, no other limit + * that the underlying _TIFFmalloc() or + * TIFFOpenOptionsSetMaxCumulatedMemAlloc() will be applied. + */ +void TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts, + tmsize_t max_single_mem_alloc) +{ + opts->max_single_mem_alloc = max_single_mem_alloc; +} + +/** Define a limit in bytes for the cumulated memory allocations done by libtiff + * on a given TIFF handle. + * If max_cumulated_mem_alloc is set to 0, which is the default, no other limit + * that the underlying _TIFFmalloc() or + * TIFFOpenOptionsSetMaxSingleMemAlloc() will be applied. + */ +void TIFFOpenOptionsSetMaxCumulatedMemAlloc(TIFFOpenOptions *opts, + tmsize_t max_cumulated_mem_alloc) +{ + opts->max_cumulated_mem_alloc = max_cumulated_mem_alloc; +} + +/** Whether a warning should be emitted when encountering a unknown tag. + * Default is FALSE since libtiff 4.7.1 + */ +void TIFFOpenOptionsSetWarnAboutUnknownTags(TIFFOpenOptions *opts, + int warn_about_unknown_tags) +{ + opts->warn_about_unknown_tags = warn_about_unknown_tags; +} + +void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts, + TIFFErrorHandlerExtR handler, + void *errorhandler_user_data) +{ + opts->errorhandler = handler; + opts->errorhandler_user_data = errorhandler_user_data; +} + +void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts, + TIFFErrorHandlerExtR handler, + void *warnhandler_user_data) +{ + opts->warnhandler = handler; + opts->warnhandler_user_data = warnhandler_user_data; +} + +static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif, + const char *pszFunction, + tmsize_t s) +{ + TIFFErrorExtR(tif, pszFunction, + "Memory allocation of %" PRIu64 + " bytes is beyond the %" PRIu64 + " byte limit defined in open options", + (uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc); +} + +static void _TIFFEmitErrorAboveMaxCumulatedMemAlloc(TIFF *tif, + const char *pszFunction, + tmsize_t s) +{ + TIFFErrorExtR(tif, pszFunction, + "Cumulated memory allocation of %" PRIu64 " + %" PRIu64 + " bytes is beyond the %" PRIu64 + " cumulated byte limit defined in open options", + (uint64_t)tif->tif_cur_cumulated_mem_alloc, (uint64_t)s, + (uint64_t)tif->tif_max_cumulated_mem_alloc); +} + +/* When allocating memory, we write at the beginning of the buffer it size. + * This allows us to keep track of the total memory allocated when we + * malloc/calloc/realloc and free. In theory we need just SIZEOF_SIZE_T bytes + * for that, but on x86_64, allocations of more than 16 bytes are aligned on + * 16 bytes. Hence using 2 * SIZEOF_SIZE_T. + * It is critical that _TIFFmallocExt/_TIFFcallocExt/_TIFFreallocExt are + * paired with _TIFFfreeExt. + * CMakeLists.txt defines TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS, which in + * turn disables the definition of the non Ext version in tiffio.h + */ +#define LEADING_AREA_TO_STORE_ALLOC_SIZE (2 * SIZEOF_SIZE_T) + +/** malloc() version that takes into account memory-specific open options */ +void *_TIFFmallocExt(TIFF *tif, tmsize_t s) +{ + if (tif != NULL && tif->tif_max_single_mem_alloc > 0 && + s > tif->tif_max_single_mem_alloc) + { + _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s); + return NULL; + } + if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) + { + if (s > tif->tif_max_cumulated_mem_alloc - + tif->tif_cur_cumulated_mem_alloc || + s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE) + { + _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFmallocExt", s); + return NULL; + } + void *ptr = _TIFFmalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s); + if (!ptr) + return NULL; + tif->tif_cur_cumulated_mem_alloc += s; + memcpy(ptr, &s, sizeof(s)); + return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE; + } + return _TIFFmalloc(s); +} + +/** calloc() version that takes into account memory-specific open options */ +void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz) +{ + if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz) + return NULL; + if (tif != NULL && tif->tif_max_single_mem_alloc > 0) + { + if (nmemb * siz > tif->tif_max_single_mem_alloc) + { + _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt", + nmemb * siz); + return NULL; + } + } + if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) + { + const tmsize_t s = nmemb * siz; + if (s > tif->tif_max_cumulated_mem_alloc - + tif->tif_cur_cumulated_mem_alloc || + s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE) + { + _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFcallocExt", s); + return NULL; + } + void *ptr = _TIFFcalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s, 1); + if (!ptr) + return NULL; + tif->tif_cur_cumulated_mem_alloc += s; + memcpy(ptr, &s, sizeof(s)); + return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE; + } + return _TIFFcalloc(nmemb, siz); +} + +/** realloc() version that takes into account memory-specific open options */ +void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s) +{ + if (tif != NULL && tif->tif_max_single_mem_alloc > 0 && + s > tif->tif_max_single_mem_alloc) + { + _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s); + return NULL; + } + if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) + { + void *oldPtr = p; + tmsize_t oldSize = 0; + if (p) + { + oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE; + memcpy(&oldSize, oldPtr, sizeof(oldSize)); + assert(oldSize <= tif->tif_cur_cumulated_mem_alloc); + } + if (s > oldSize && + (s > tif->tif_max_cumulated_mem_alloc - + (tif->tif_cur_cumulated_mem_alloc - oldSize) || + s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)) + { + _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFreallocExt", + s - oldSize); + return NULL; + } + void *newPtr = + _TIFFrealloc(oldPtr, LEADING_AREA_TO_STORE_ALLOC_SIZE + s); + if (newPtr == NULL) + return NULL; + tif->tif_cur_cumulated_mem_alloc -= oldSize; + tif->tif_cur_cumulated_mem_alloc += s; + memcpy(newPtr, &s, sizeof(s)); + return (char *)newPtr + LEADING_AREA_TO_STORE_ALLOC_SIZE; + } + return _TIFFrealloc(p, s); +} + +/** free() version that takes into account memory-specific open options */ +void _TIFFfreeExt(TIFF *tif, void *p) +{ + if (p != NULL && tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) + { + void *oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE; + tmsize_t oldSize; + memcpy(&oldSize, oldPtr, sizeof(oldSize)); + assert(oldSize <= tif->tif_cur_cumulated_mem_alloc); + tif->tif_cur_cumulated_mem_alloc -= oldSize; + p = oldPtr; + } + _TIFFfree(p); +} + +TIFF *TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata, + TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, + TIFFSeekProc seekproc, TIFFCloseProc closeproc, + TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, + TIFFUnmapFileProc unmapproc) +{ + return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc, + seekproc, closeproc, sizeproc, mapproc, unmapproc, + NULL); +} + +TIFF *TIFFClientOpenExt(const char *name, const char *mode, + thandle_t clientdata, TIFFReadWriteProc readproc, + TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, + TIFFCloseProc closeproc, TIFFSizeProc sizeproc, + TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc, + TIFFOpenOptions *opts) +{ + static const char module[] = "TIFFClientOpenExt"; + TIFF *tif; + int m; + const char *cp; + + /* The following are configuration checks. They should be redundant, but + * should not compile to any actual code in an optimised release build + * anyway. If any of them fail, (makefile-based or other) configuration is + * not correct */ + assert(sizeof(uint8_t) == 1); + assert(sizeof(int8_t) == 1); + assert(sizeof(uint16_t) == 2); + assert(sizeof(int16_t) == 2); + assert(sizeof(uint32_t) == 4); + assert(sizeof(int32_t) == 4); + assert(sizeof(uint64_t) == 8); + assert(sizeof(int64_t) == 8); + { + union + { + uint8_t a8[2]; + uint16_t a16; + } n; + n.a8[0] = 1; + n.a8[1] = 0; + (void)n; +#ifdef WORDS_BIGENDIAN + assert(n.a16 == 256); +#else + assert(n.a16 == 1); +#endif + } + + m = _TIFFgetMode(opts, clientdata, mode, module); + if (m == -1) + goto bad2; + tmsize_t size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1); + if (opts && opts->max_single_mem_alloc > 0 && + size_to_alloc > opts->max_single_mem_alloc) + { + _TIFFErrorEarly(opts, clientdata, module, + "%s: Memory allocation of %" PRIu64 + " bytes is beyond the %" PRIu64 + " byte limit defined in open options", + name, (uint64_t)size_to_alloc, + (uint64_t)opts->max_single_mem_alloc); + goto bad2; + } + if (opts && opts->max_cumulated_mem_alloc > 0 && + size_to_alloc > opts->max_cumulated_mem_alloc) + { + _TIFFErrorEarly(opts, clientdata, module, + "%s: Memory allocation of %" PRIu64 + " bytes is beyond the %" PRIu64 + " cumulated byte limit defined in open options", + name, (uint64_t)size_to_alloc, + (uint64_t)opts->max_cumulated_mem_alloc); + goto bad2; + } + tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc); + if (tif == NULL) + { + _TIFFErrorEarly(opts, clientdata, module, + "%s: Out of memory (TIFF structure)", name); + goto bad2; + } + _TIFFmemset(tif, 0, sizeof(*tif)); + tif->tif_name = (char *)tif + sizeof(TIFF); + strcpy(tif->tif_name, name); + tif->tif_mode = m & ~(O_CREAT | O_TRUNC); + tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; /* non-existent directory */ + tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER; + tif->tif_curoff = 0; + tif->tif_curstrip = (uint32_t)-1; /* invalid strip */ + tif->tif_row = (uint32_t)-1; /* read/write pre-increment */ + tif->tif_clientdata = clientdata; + tif->tif_readproc = readproc; + tif->tif_writeproc = writeproc; + tif->tif_seekproc = seekproc; + tif->tif_closeproc = closeproc; + tif->tif_sizeproc = sizeproc; + tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc; + tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc; + if (opts) + { + tif->tif_errorhandler = opts->errorhandler; + tif->tif_errorhandler_user_data = opts->errorhandler_user_data; + tif->tif_warnhandler = opts->warnhandler; + tif->tif_warnhandler_user_data = opts->warnhandler_user_data; + tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc; + tif->tif_max_cumulated_mem_alloc = opts->max_cumulated_mem_alloc; + tif->tif_warn_about_unknown_tags = opts->warn_about_unknown_tags; + } + + if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) + { + TIFFErrorExtR(tif, module, + "One of the client procedures is NULL pointer."); + _TIFFfreeExt(NULL, tif); + goto bad2; + } + + _TIFFSetDefaultCompressionState(tif); /* setup default state */ + /* + * Default is to return data MSB2LSB and enable the + * use of memory-mapped files and strip chopping when + * a file is opened read-only. + */ + tif->tif_flags = FILLORDER_MSB2LSB; + if (m == O_RDONLY) + tif->tif_flags |= TIFF_MAPPED; + +#ifdef STRIPCHOP_DEFAULT + if (m == O_RDONLY || m == O_RDWR) + tif->tif_flags |= STRIPCHOP_DEFAULT; +#endif + + /* + * Process library-specific flags in the open mode string. + * The following flags may be used to control intrinsic library + * behavior that may or may not be desirable (usually for + * compatibility with some application that claims to support + * TIFF but only supports some brain dead idea of what the + * vendor thinks TIFF is): + * + * 'l' use little-endian byte order for creating a file + * 'b' use big-endian byte order for creating a file + * 'L' read/write information using LSB2MSB bit order + * 'B' read/write information using MSB2LSB bit order + * 'H' read/write information using host bit order + * 'M' enable use of memory-mapped files when supported + * 'm' disable use of memory-mapped files + * 'C' enable strip chopping support when reading + * 'c' disable strip chopping support + * 'h' read TIFF header only, do not load the first IFD + * '4' ClassicTIFF for creating a file (default) + * '8' BigTIFF for creating a file + * 'D' enable use of deferred strip/tile offset/bytecount array loading. + * 'O' on-demand loading of values instead of whole array loading (implies + * D) + * + * The use of the 'l' and 'b' flags is strongly discouraged. + * These flags are provided solely because numerous vendors, + * typically on the PC, do not correctly support TIFF; they + * only support the Intel little-endian byte order. This + * support is not configured by default because it supports + * the violation of the TIFF spec that says that readers *MUST* + * support both byte orders. It is strongly recommended that + * you not use this feature except to deal with busted apps + * that write invalid TIFF. And even in those cases you should + * bang on the vendors to fix their software. + * + * The 'L', 'B', and 'H' flags are intended for applications + * that can optimize operations on data by using a particular + * bit order. By default the library returns data in MSB2LSB + * bit order for compatibility with older versions of this + * library. Returning data in the bit order of the native CPU + * makes the most sense but also requires applications to check + * the value of the FillOrder tag; something they probably do + * not do right now. + * + * The 'M' and 'm' flags are provided because some virtual memory + * systems exhibit poor behavior when large images are mapped. + * These options permit clients to control the use of memory-mapped + * files on a per-file basis. + * + * The 'C' and 'c' flags are provided because the library support + * for chopping up large strips into multiple smaller strips is not + * application-transparent and as such can cause problems. The 'c' + * option permits applications that only want to look at the tags, + * for example, to get the unadulterated TIFF tag information. + */ + for (cp = mode; *cp; cp++) + switch (*cp) + { + case 'b': +#ifndef WORDS_BIGENDIAN + if (m & O_CREAT) + tif->tif_flags |= TIFF_SWAB; +#endif + break; + case 'l': +#ifdef WORDS_BIGENDIAN + if ((m & O_CREAT)) + tif->tif_flags |= TIFF_SWAB; +#endif + break; + case 'B': + tif->tif_flags = + (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB; + break; + case 'L': + tif->tif_flags = + (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB; + break; + case 'H': + TIFFWarningExtR(tif, name, + "H(ost) mode is deprecated. Since " + "libtiff 4.5.1, it is an alias of 'B' / " + "FILLORDER_MSB2LSB."); + tif->tif_flags = + (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB; + break; + case 'M': + if (m == O_RDONLY) + tif->tif_flags |= TIFF_MAPPED; + break; + case 'm': + if (m == O_RDONLY) + tif->tif_flags &= ~TIFF_MAPPED; + break; + case 'C': + if (m == O_RDONLY) + tif->tif_flags |= TIFF_STRIPCHOP; + break; + case 'c': + if (m == O_RDONLY) + tif->tif_flags &= ~TIFF_STRIPCHOP; + break; + case 'h': + tif->tif_flags |= TIFF_HEADERONLY; + break; + case '8': + if (m & O_CREAT) + tif->tif_flags |= TIFF_BIGTIFF; + break; + case 'D': + tif->tif_flags |= TIFF_DEFERSTRILELOAD; + break; + case 'O': + if (m == O_RDONLY) + tif->tif_flags |= + (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD); + break; + } + +#ifdef DEFER_STRILE_LOAD + /* Compatibility with old DEFER_STRILE_LOAD compilation flag */ + /* Probably unneeded, since to the best of my knowledge (E. Rouault) */ + /* GDAL was the only user of this, and will now use the new 'D' flag */ + tif->tif_flags |= TIFF_DEFERSTRILELOAD; +#endif + + /* + * Read in TIFF header. + */ + if ((m & O_TRUNC) || + !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic))) + { + if (tif->tif_mode == O_RDONLY) + { + TIFFErrorExtR(tif, name, "Cannot read TIFF header"); + goto bad; + } + /* + * Setup header and write. + */ +#ifdef WORDS_BIGENDIAN + tif->tif_header.common.tiff_magic = + (tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN; +#else + tif->tif_header.common.tiff_magic = + (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN; +#endif + TIFFHeaderUnion tif_header_swapped; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + { + tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC; + tif->tif_header.classic.tiff_diroff = 0; + tif->tif_header_size = sizeof(TIFFHeaderClassic); + /* Swapped copy for writing */ + _TIFFmemcpy(&tif_header_swapped, &tif->tif_header, + sizeof(TIFFHeaderUnion)); + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&tif_header_swapped.common.tiff_version); + } + else + { + tif->tif_header.common.tiff_version = TIFF_VERSION_BIG; + tif->tif_header.big.tiff_offsetsize = 8; + tif->tif_header.big.tiff_unused = 0; + tif->tif_header.big.tiff_diroff = 0; + tif->tif_header_size = sizeof(TIFFHeaderBig); + /* Swapped copy for writing */ + _TIFFmemcpy(&tif_header_swapped, &tif->tif_header, + sizeof(TIFFHeaderUnion)); + if (tif->tif_flags & TIFF_SWAB) + { + TIFFSwabShort(&tif_header_swapped.common.tiff_version); + TIFFSwabShort(&tif_header_swapped.big.tiff_offsetsize); + } + } + /* + * The doc for "fopen" for some STD_C_LIBs says that if you + * open a file for modify ("+"), then you must fseek (or + * fflush?) between any freads and fwrites. This is not + * necessary on most systems, but has been shown to be needed + * on Solaris. + */ + TIFFSeekFile(tif, 0, SEEK_SET); + if (!WriteOK(tif, &tif_header_swapped, + (tmsize_t)(tif->tif_header_size))) + { + TIFFErrorExtR(tif, name, "Error writing TIFF header"); + goto bad; + } + /* + * Setup default directory. + */ + if (!TIFFDefaultDirectory(tif)) + goto bad; + tif->tif_diroff = 0; + tif->tif_lastdiroff = 0; + tif->tif_setdirectory_force_absolute = FALSE; + /* tif_curdircount = 0 means 'empty file opened for writing, but no IFD + * written yet' */ + tif->tif_curdircount = 0; + return (tif); + } + + /* + * Setup the byte order handling according to the opened file for reading. + */ + if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN && + tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN +#if MDI_SUPPORT + && +#if HOST_BIGENDIAN + tif->tif_header.common.tiff_magic != MDI_BIGENDIAN +#else + tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN +#endif + ) + { + TIFFErrorExtR(tif, name, + "Not a TIFF or MDI file, bad magic number %" PRIu16 + " (0x%" PRIx16 ")", +#else + ) + { + TIFFErrorExtR(tif, name, + "Not a TIFF file, bad magic number %" PRIu16 + " (0x%" PRIx16 ")", +#endif + tif->tif_header.common.tiff_magic, + tif->tif_header.common.tiff_magic); + goto bad; + } + if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) + { +#ifndef WORDS_BIGENDIAN + tif->tif_flags |= TIFF_SWAB; +#endif + } + else + { +#ifdef WORDS_BIGENDIAN + tif->tif_flags |= TIFF_SWAB; +#endif + } + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabShort(&tif->tif_header.common.tiff_version); + if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) && + (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG)) + { + TIFFErrorExtR(tif, name, + "Not a TIFF file, bad version number %" PRIu16 + " (0x%" PRIx16 ")", + tif->tif_header.common.tiff_version, + tif->tif_header.common.tiff_version); + goto bad; + } + if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC) + { + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabLong(&tif->tif_header.classic.tiff_diroff); + tif->tif_header_size = sizeof(TIFFHeaderClassic); + } + else + { + if (!ReadOK(tif, + ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)), + (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic)))) + { + TIFFErrorExtR(tif, name, "Cannot read TIFF header"); + goto bad; + } + if (tif->tif_flags & TIFF_SWAB) + { + TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize); + TIFFSwabLong8(&tif->tif_header.big.tiff_diroff); + } + if (tif->tif_header.big.tiff_offsetsize != 8) + { + TIFFErrorExtR(tif, name, + "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16 + " (0x%" PRIx16 ")", + tif->tif_header.big.tiff_offsetsize, + tif->tif_header.big.tiff_offsetsize); + goto bad; + } + if (tif->tif_header.big.tiff_unused != 0) + { + TIFFErrorExtR(tif, name, + "Not a TIFF file, bad BigTIFF unused %" PRIu16 + " (0x%" PRIx16 ")", + tif->tif_header.big.tiff_unused, + tif->tif_header.big.tiff_unused); + goto bad; + } + tif->tif_header_size = sizeof(TIFFHeaderBig); + tif->tif_flags |= TIFF_BIGTIFF; + } + tif->tif_flags |= TIFF_MYBUFFER; + tif->tif_rawcp = tif->tif_rawdata = 0; + tif->tif_rawdatasize = 0; + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = 0; + + switch (mode[0]) + { + case 'r': + if (!(tif->tif_flags & TIFF_BIGTIFF)) + tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff; + else + tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff; + /* + * Try to use a memory-mapped file if the client + * has not explicitly suppressed usage with the + * 'm' flag in the open mode (see above). + */ + if (tif->tif_flags & TIFF_MAPPED) + { + toff_t n; + if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n)) + { + tif->tif_size = (tmsize_t)n; + assert((toff_t)tif->tif_size == n); + } + else + tif->tif_flags &= ~TIFF_MAPPED; + } + /* + * Sometimes we do not want to read the first directory (for + * example, it may be broken) and want to proceed to other + * directories. I this case we use the TIFF_HEADERONLY flag to open + * file and return immediately after reading TIFF header. + * However, the pointer to TIFFSetField() and TIFFGetField() + * (i.e. tif->tif_tagmethods.vsetfield and + * tif->tif_tagmethods.vgetfield) need to be initialized, which is + * done in TIFFDefaultDirectory(). + */ + if (tif->tif_flags & TIFF_HEADERONLY) + { + if (!TIFFDefaultDirectory(tif)) + goto bad; + return (tif); + } + + /* + * Setup initial directory. + */ + if (TIFFReadDirectory(tif)) + { + return (tif); + } + break; + case 'a': + /* + * New directories are automatically append + * to the end of the directory chain when they + * are written out (see TIFFWriteDirectory). + */ + if (!TIFFDefaultDirectory(tif)) + goto bad; + return (tif); + } +bad: + tif->tif_mode = O_RDONLY; /* XXX avoid flush */ + TIFFCleanup(tif); +bad2: + return ((TIFF *)0); +} + +/* + * Query functions to access private data. + */ + +/* + * Return open file's name. + */ +const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); } + +/* + * Set the file name. + */ +const char *TIFFSetFileName(TIFF *tif, const char *name) +{ + const char *old_name = tif->tif_name; + tif->tif_name = (char *)name; + return (old_name); +} + +/* + * Return open file's I/O descriptor. + */ +int TIFFFileno(TIFF *tif) { return (tif->tif_fd); } + +/* + * Set open file's I/O descriptor, and return previous value. + */ +int TIFFSetFileno(TIFF *tif, int fd) +{ + int old_fd = tif->tif_fd; + tif->tif_fd = fd; + return old_fd; +} + +/* + * Return open file's clientdata. + */ +thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); } + +/* + * Set open file's clientdata, and return previous value. + */ +thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue) +{ + thandle_t m = tif->tif_clientdata; + tif->tif_clientdata = newvalue; + return m; +} + +/* + * Return read/write mode. + */ +int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); } + +/* + * Return read/write mode. + */ +int TIFFSetMode(TIFF *tif, int mode) +{ + int old_mode = tif->tif_mode; + tif->tif_mode = mode; + return (old_mode); +} + +/* + * Return nonzero if file is organized in + * tiles; zero if organized as strips. + */ +int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); } + +/* + * Return current row being read/written. + */ +uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_row); } + +/* + * Return index of the current directory. + */ +tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); } + +/* + * Return current strip. + */ +uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_curstrip); } + +/* + * Return current tile. + */ +uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_curtile); } + +/* + * Return nonzero if the file has byte-swapped data. + */ +int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); } + +/* + * Return nonzero if the data is returned up-sampled. + */ +int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); } + +/* + * Return nonzero if the data is returned in MSB-to-LSB bit order. + */ +int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); } + +/* + * Return nonzero if given file was written in big-endian order. + */ +int TIFFIsBigEndian(TIFF *tif) +{ + return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN); +} + +/* + * Return nonzero if given file is BigTIFF style. + */ +int TIFFIsBigTIFF(TIFF *tif) { return ((tif->tif_flags & TIFF_BIGTIFF) != 0); } + +/* + * Return pointer to file read method. + */ +TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); } + +/* + * Return pointer to file write method. + */ +TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); } + +/* + * Return pointer to file seek method. + */ +TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); } + +/* + * Return pointer to file close method. + */ +TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); } + +/* + * Return pointer to file size requesting method. + */ +TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); } + +/* + * Return pointer to memory mapping method. + */ +TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); } + +/* + * Return pointer to memory unmapping method. + */ +TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif) +{ + return (tif->tif_unmapproc); +} diff --git a/cpp/3rd_party/libtiff/tif_packbits.c b/cpp/3rd_party/libtiff/tif_packbits.c new file mode 100644 index 0000000000..d7db9b64ea --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_packbits.c @@ -0,0 +1,330 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef PACKBITS_SUPPORT +/* + * TIFF Library. + * + * PackBits Compression Algorithm Support + */ +#include + +#ifndef PACKBITS_READ_ONLY + +static int PackBitsPreEncode(TIFF *tif, uint16_t s) +{ + (void)s; + + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(tmsize_t)); + if (tif->tif_data == NULL) + return (0); + /* + * Calculate the scanline/tile-width size in bytes. + */ + if (isTiled(tif)) + *(tmsize_t *)tif->tif_data = TIFFTileRowSize(tif); + else + *(tmsize_t *)tif->tif_data = TIFFScanlineSize(tif); + return (1); +} + +static int PackBitsPostEncode(TIFF *tif) +{ + if (tif->tif_data) + _TIFFfreeExt(tif, tif->tif_data); + return (1); +} + +/* + * Encode a run of pixels. + */ +static int PackBitsEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) +{ + unsigned char *bp = (unsigned char *)buf; + uint8_t *op; + uint8_t *ep; + uint8_t *lastliteral; + long n, slop; + int b; + enum + { + BASE, + LITERAL, + RUN, + LITERAL_RUN + } state; + + (void)s; + op = tif->tif_rawcp; + ep = tif->tif_rawdata + tif->tif_rawdatasize; + state = BASE; + lastliteral = NULL; + while (cc > 0) + { + /* + * Find the longest string of identical bytes. + */ + b = *bp++; + cc--; + n = 1; + for (; cc > 0 && b == *bp; cc--, bp++) + n++; + again: + if (op + 2 >= ep) + { /* insure space for new data */ + /* + * Be careful about writing the last + * literal. Must write up to that point + * and then copy the remainder to the + * front of the buffer. + */ + if (state == LITERAL || state == LITERAL_RUN) + { + slop = (long)(op - lastliteral); + tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp); + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + while (slop-- > 0) + *op++ = *lastliteral++; + lastliteral = tif->tif_rawcp; + } + else + { + tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp); + if (!TIFFFlushData1(tif)) + return (0); + op = tif->tif_rawcp; + } + } + switch (state) + { + case BASE: /* initial state, set run/literal */ + if (n > 1) + { + state = RUN; + if (n > 128) + { + *op++ = (uint8_t)-127; + *op++ = (uint8_t)b; + n -= 128; + goto again; + } + *op++ = (uint8_t)(-(n - 1)); + *op++ = (uint8_t)b; + } + else + { + lastliteral = op; + *op++ = 0; + *op++ = (uint8_t)b; + state = LITERAL; + } + break; + case LITERAL: /* last object was literal string */ + if (n > 1) + { + state = LITERAL_RUN; + if (n > 128) + { + *op++ = (uint8_t)-127; + *op++ = (uint8_t)b; + n -= 128; + goto again; + } + *op++ = (uint8_t)(-(n - 1)); /* encode run */ + *op++ = (uint8_t)b; + } + else + { /* extend literal */ + if (++(*lastliteral) == 127) + state = BASE; + *op++ = (uint8_t)b; + } + break; + case RUN: /* last object was run */ + if (n > 1) + { + if (n > 128) + { + *op++ = (uint8_t)-127; + *op++ = (uint8_t)b; + n -= 128; + goto again; + } + *op++ = (uint8_t)(-(n - 1)); + *op++ = (uint8_t)b; + } + else + { + lastliteral = op; + *op++ = 0; + *op++ = (uint8_t)b; + state = LITERAL; + } + break; + case LITERAL_RUN: /* literal followed by a run */ + /* + * Check to see if previous run should + * be converted to a literal, in which + * case we convert literal-run-literal + * to a single literal. + */ + if (n == 1 && op[-2] == (uint8_t)-1 && *lastliteral < 126) + { + state = (((*lastliteral) += 2) == 127 ? BASE : LITERAL); + op[-2] = op[-1]; /* replicate */ + } + else + state = RUN; + goto again; + } + } + tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp); + tif->tif_rawcp = op; + return (1); +} + +/* + * Encode a rectangular chunk of pixels. We break it up + * into row-sized pieces to insure that encoded runs do + * not span rows. Otherwise, there can be problems with + * the decoder if data is read, for example, by scanlines + * when it was encoded by strips. + */ +static int PackBitsEncodeChunk(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + tmsize_t rowsize = *(tmsize_t *)tif->tif_data; + + while (cc > 0) + { + tmsize_t chunk = rowsize; + + if (cc < chunk) + chunk = cc; + + if (PackBitsEncode(tif, bp, chunk, s) < 0) + return (-1); + bp += chunk; + cc -= chunk; + } + return (1); +} + +#endif + +static int PackBitsDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "PackBitsDecode"; + int8_t *bp; + tmsize_t cc; + long n; + int b; + + (void)s; + bp = (int8_t *)tif->tif_rawcp; + cc = tif->tif_rawcc; + while (cc > 0 && occ > 0) + { + n = (long)*bp++; + cc--; + if (n < 0) + { /* replicate next byte -n+1 times */ + if (n == -128) /* nop */ + continue; + n = -n + 1; + if (occ < (tmsize_t)n) + { + TIFFWarningExtR(tif, module, + "Discarding %" TIFF_SSIZE_FORMAT + " bytes to avoid buffer overrun", + (tmsize_t)n - occ); + n = (long)occ; + } + if (cc == 0) + { + TIFFWarningExtR( + tif, module, + "Terminating PackBitsDecode due to lack of data."); + break; + } + occ -= n; + b = *bp++; + cc--; + while (n-- > 0) + *op++ = (uint8_t)b; + } + else + { /* copy next n+1 bytes literally */ + if (occ < (tmsize_t)(n + 1)) + { + TIFFWarningExtR(tif, module, + "Discarding %" TIFF_SSIZE_FORMAT + " bytes to avoid buffer overrun", + (tmsize_t)n - occ + 1); + n = (long)occ - 1; + } + if (cc < (tmsize_t)(n + 1)) + { + TIFFWarningExtR( + tif, module, + "Terminating PackBitsDecode due to lack of data."); + break; + } + _TIFFmemcpy(op, bp, ++n); + op += n; + occ -= n; + bp += n; + cc -= n; + } + } + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + if (occ > 0) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32, + tif->tif_row); + return (0); + } + return (1); +} + +int TIFFInitPackBits(TIFF *tif, int scheme) +{ + (void)scheme; + tif->tif_decoderow = PackBitsDecode; + tif->tif_decodestrip = PackBitsDecode; + tif->tif_decodetile = PackBitsDecode; +#ifndef PACKBITS_READ_ONLY + tif->tif_preencode = PackBitsPreEncode; + tif->tif_postencode = PackBitsPostEncode; + tif->tif_encoderow = PackBitsEncode; + tif->tif_encodestrip = PackBitsEncodeChunk; + tif->tif_encodetile = PackBitsEncodeChunk; +#endif + return (1); +} +#endif /* PACKBITS_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_pixarlog.c b/cpp/3rd_party/libtiff/tif_pixarlog.c new file mode 100644 index 0000000000..309054804d --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_pixarlog.c @@ -0,0 +1,1677 @@ +/* + * Copyright (c) 1996-1997 Sam Leffler + * Copyright (c) 1996 Pixar + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Pixar, Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef PIXARLOG_SUPPORT + +/* + * TIFF Library. + * PixarLog Compression Support + * + * Contributed by Dan McCoy. + * + * PixarLog film support uses the TIFF library to store companded + * 11 bit values into a tiff file, which are compressed using the + * zip compressor. + * + * The codec can take as input and produce as output 32-bit IEEE float values + * as well as 16-bit or 8-bit unsigned integer values. + * + * On writing any of the above are converted into the internal + * 11-bit log format. In the case of 8 and 16 bit values, the + * input is assumed to be unsigned linear color values that represent + * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to + * be the normal linear color range, in addition over 1 values are + * accepted up to a value of about 25.0 to encode "hot" highlights and such. + * The encoding is lossless for 8-bit values, slightly lossy for the + * other bit depths. The actual color precision should be better + * than the human eye can perceive with extra room to allow for + * error introduced by further image computation. As with any quantized + * color format, it is possible to perform image calculations which + * expose the quantization error. This format should certainly be less + * susceptible to such errors than standard 8-bit encodings, but more + * susceptible than straight 16-bit or 32-bit encodings. + * + * On reading the internal format is converted to the desired output format. + * The program can request which format it desires by setting the internal + * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values: + * PIXARLOGDATAFMT_FLOAT = provide IEEE float values. + * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values + * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values + * + * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer + * values with the difference that if there are exactly three or four channels + * (rgb or rgba) it swaps the channel order (bgr or abgr). + * + * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly + * packed in 16-bit values. However no tools are supplied for interpreting + * these values. + * + * "hot" (over 1.0) areas written in floating point get clamped to + * 1.0 in the integer data types. + * + * When the file is closed after writing, the bit depth and sample format + * are set always to appear as if 8-bit data has been written into it. + * That way a naive program unaware of the particulars of the encoding + * gets the format it is most likely able to handle. + * + * The codec does it's own horizontal differencing step on the coded + * values so the libraries predictor stuff should be turned off. + * The codec also handle byte swapping the encoded values as necessary + * since the library does not have the information necessary + * to know the bit depth of the raw unencoded buffer. + * + * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc. + * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT + * as noted in http://trac.osgeo.org/gdal/ticket/3894. FrankW - Jan'11 + */ + +#include "tif_predict.h" +#include "zlib.h" + +#include +#include +#include + +/* Tables for converting to/from 11 bit coded values */ + +#define TSIZE 2048 /* decode table size (11-bit tokens) */ +#define TSIZEP1 2049 /* Plus one for slop */ +#define ONE 1250 /* token value of 1.0 exactly */ +#define RATIO 1.004 /* nominal ratio for log part */ + +#define CODE_MASK 0x7ff /* 11 bits. */ + +static float Fltsize; +static float LogK1, LogK2; + +#define REPEAT(n, op) \ + { \ + int i; \ + i = n; \ + do \ + { \ + i--; \ + op; \ + } while (i > 0); \ + } + +static void horizontalAccumulateF(uint16_t *wp, int n, int stride, float *op, + float *ToLinearF) +{ + register unsigned int cr, cg, cb, ca, mask; + register float t0, t1, t2, t3; + + if (n >= stride) + { + mask = CODE_MASK; + if (stride == 3) + { + t0 = ToLinearF[cr = (wp[0] & mask)]; + t1 = ToLinearF[cg = (wp[1] & mask)]; + t2 = ToLinearF[cb = (wp[2] & mask)]; + op[0] = t0; + op[1] = t1; + op[2] = t2; + n -= 3; + while (n > 0) + { + wp += 3; + op += 3; + n -= 3; + t0 = ToLinearF[(cr += wp[0]) & mask]; + t1 = ToLinearF[(cg += wp[1]) & mask]; + t2 = ToLinearF[(cb += wp[2]) & mask]; + op[0] = t0; + op[1] = t1; + op[2] = t2; + } + } + else if (stride == 4) + { + t0 = ToLinearF[cr = (wp[0] & mask)]; + t1 = ToLinearF[cg = (wp[1] & mask)]; + t2 = ToLinearF[cb = (wp[2] & mask)]; + t3 = ToLinearF[ca = (wp[3] & mask)]; + op[0] = t0; + op[1] = t1; + op[2] = t2; + op[3] = t3; + n -= 4; + while (n > 0) + { + wp += 4; + op += 4; + n -= 4; + t0 = ToLinearF[(cr += wp[0]) & mask]; + t1 = ToLinearF[(cg += wp[1]) & mask]; + t2 = ToLinearF[(cb += wp[2]) & mask]; + t3 = ToLinearF[(ca += wp[3]) & mask]; + op[0] = t0; + op[1] = t1; + op[2] = t2; + op[3] = t3; + } + } + else + { + REPEAT(stride, *op = ToLinearF[*wp & mask]; wp++; op++) + n -= stride; + while (n > 0) + { + REPEAT(stride, wp[stride] += *wp; *op = ToLinearF[*wp & mask]; + wp++; op++) + n -= stride; + } + } + } +} + +static void horizontalAccumulate12(uint16_t *wp, int n, int stride, int16_t *op, + float *ToLinearF) +{ + register unsigned int cr, cg, cb, ca, mask; + register float t0, t1, t2, t3; + +#define SCALE12 2048.0F +#define CLAMP12(t) (((t) < 3071) ? (uint16_t)(t) : 3071) + + if (n >= stride) + { + mask = CODE_MASK; + if (stride == 3) + { + t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12; + t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12; + t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12; + op[0] = CLAMP12(t0); + op[1] = CLAMP12(t1); + op[2] = CLAMP12(t2); + n -= 3; + while (n > 0) + { + wp += 3; + op += 3; + n -= 3; + t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12; + t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12; + t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12; + op[0] = CLAMP12(t0); + op[1] = CLAMP12(t1); + op[2] = CLAMP12(t2); + } + } + else if (stride == 4) + { + t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12; + t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12; + t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12; + t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12; + op[0] = CLAMP12(t0); + op[1] = CLAMP12(t1); + op[2] = CLAMP12(t2); + op[3] = CLAMP12(t3); + n -= 4; + while (n > 0) + { + wp += 4; + op += 4; + n -= 4; + t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12; + t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12; + t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12; + t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12; + op[0] = CLAMP12(t0); + op[1] = CLAMP12(t1); + op[2] = CLAMP12(t2); + op[3] = CLAMP12(t3); + } + } + else + { + REPEAT(stride, t0 = ToLinearF[*wp & mask] * SCALE12; + *op = CLAMP12(t0); wp++; op++) + n -= stride; + while (n > 0) + { + REPEAT(stride, wp[stride] += *wp; + t0 = ToLinearF[wp[stride] & mask] * SCALE12; + *op = CLAMP12(t0); wp++; op++) + n -= stride; + } + } + } +} + +static void horizontalAccumulate16(uint16_t *wp, int n, int stride, + uint16_t *op, uint16_t *ToLinear16) +{ + register unsigned int cr, cg, cb, ca, mask; + + if (n >= stride) + { + mask = CODE_MASK; + if (stride == 3) + { + op[0] = ToLinear16[cr = (wp[0] & mask)]; + op[1] = ToLinear16[cg = (wp[1] & mask)]; + op[2] = ToLinear16[cb = (wp[2] & mask)]; + n -= 3; + while (n > 0) + { + wp += 3; + op += 3; + n -= 3; + op[0] = ToLinear16[(cr += wp[0]) & mask]; + op[1] = ToLinear16[(cg += wp[1]) & mask]; + op[2] = ToLinear16[(cb += wp[2]) & mask]; + } + } + else if (stride == 4) + { + op[0] = ToLinear16[cr = (wp[0] & mask)]; + op[1] = ToLinear16[cg = (wp[1] & mask)]; + op[2] = ToLinear16[cb = (wp[2] & mask)]; + op[3] = ToLinear16[ca = (wp[3] & mask)]; + n -= 4; + while (n > 0) + { + wp += 4; + op += 4; + n -= 4; + op[0] = ToLinear16[(cr += wp[0]) & mask]; + op[1] = ToLinear16[(cg += wp[1]) & mask]; + op[2] = ToLinear16[(cb += wp[2]) & mask]; + op[3] = ToLinear16[(ca += wp[3]) & mask]; + } + } + else + { + REPEAT(stride, *op = ToLinear16[*wp & mask]; wp++; op++) + n -= stride; + while (n > 0) + { + REPEAT(stride, wp[stride] += *wp; *op = ToLinear16[*wp & mask]; + wp++; op++) + n -= stride; + } + } + } +} + +/* + * Returns the log encoded 11-bit values with the horizontal + * differencing undone. + */ +static void horizontalAccumulate11(uint16_t *wp, int n, int stride, + uint16_t *op) +{ + register unsigned int cr, cg, cb, ca, mask; + + if (n >= stride) + { + mask = CODE_MASK; + if (stride == 3) + { + op[0] = wp[0]; + op[1] = wp[1]; + op[2] = wp[2]; + cr = wp[0]; + cg = wp[1]; + cb = wp[2]; + n -= 3; + while (n > 0) + { + wp += 3; + op += 3; + n -= 3; + op[0] = (uint16_t)((cr += wp[0]) & mask); + op[1] = (uint16_t)((cg += wp[1]) & mask); + op[2] = (uint16_t)((cb += wp[2]) & mask); + } + } + else if (stride == 4) + { + op[0] = wp[0]; + op[1] = wp[1]; + op[2] = wp[2]; + op[3] = wp[3]; + cr = wp[0]; + cg = wp[1]; + cb = wp[2]; + ca = wp[3]; + n -= 4; + while (n > 0) + { + wp += 4; + op += 4; + n -= 4; + op[0] = (uint16_t)((cr += wp[0]) & mask); + op[1] = (uint16_t)((cg += wp[1]) & mask); + op[2] = (uint16_t)((cb += wp[2]) & mask); + op[3] = (uint16_t)((ca += wp[3]) & mask); + } + } + else + { + REPEAT(stride, *op = *wp & mask; wp++; op++) + n -= stride; + while (n > 0) + { + REPEAT(stride, wp[stride] += *wp; *op = *wp & mask; wp++; op++) + n -= stride; + } + } + } +} + +static void horizontalAccumulate8(uint16_t *wp, int n, int stride, + unsigned char *op, unsigned char *ToLinear8) +{ + register unsigned int cr, cg, cb, ca, mask; + + if (n >= stride) + { + mask = CODE_MASK; + if (stride == 3) + { + op[0] = ToLinear8[cr = (wp[0] & mask)]; + op[1] = ToLinear8[cg = (wp[1] & mask)]; + op[2] = ToLinear8[cb = (wp[2] & mask)]; + n -= 3; + while (n > 0) + { + n -= 3; + wp += 3; + op += 3; + op[0] = ToLinear8[(cr += wp[0]) & mask]; + op[1] = ToLinear8[(cg += wp[1]) & mask]; + op[2] = ToLinear8[(cb += wp[2]) & mask]; + } + } + else if (stride == 4) + { + op[0] = ToLinear8[cr = (wp[0] & mask)]; + op[1] = ToLinear8[cg = (wp[1] & mask)]; + op[2] = ToLinear8[cb = (wp[2] & mask)]; + op[3] = ToLinear8[ca = (wp[3] & mask)]; + n -= 4; + while (n > 0) + { + n -= 4; + wp += 4; + op += 4; + op[0] = ToLinear8[(cr += wp[0]) & mask]; + op[1] = ToLinear8[(cg += wp[1]) & mask]; + op[2] = ToLinear8[(cb += wp[2]) & mask]; + op[3] = ToLinear8[(ca += wp[3]) & mask]; + } + } + else + { + REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++) + n -= stride; + while (n > 0) + { + REPEAT(stride, wp[stride] += *wp; *op = ToLinear8[*wp & mask]; + wp++; op++) + n -= stride; + } + } + } +} + +static void horizontalAccumulate8abgr(uint16_t *wp, int n, int stride, + unsigned char *op, + unsigned char *ToLinear8) +{ + register unsigned int cr, cg, cb, ca, mask; + register unsigned char t0, t1, t2, t3; + + if (n >= stride) + { + mask = CODE_MASK; + if (stride == 3) + { + op[0] = 0; + t1 = ToLinear8[cb = (wp[2] & mask)]; + t2 = ToLinear8[cg = (wp[1] & mask)]; + t3 = ToLinear8[cr = (wp[0] & mask)]; + op[1] = t1; + op[2] = t2; + op[3] = t3; + n -= 3; + while (n > 0) + { + n -= 3; + wp += 3; + op += 4; + op[0] = 0; + t1 = ToLinear8[(cb += wp[2]) & mask]; + t2 = ToLinear8[(cg += wp[1]) & mask]; + t3 = ToLinear8[(cr += wp[0]) & mask]; + op[1] = t1; + op[2] = t2; + op[3] = t3; + } + } + else if (stride == 4) + { + t0 = ToLinear8[ca = (wp[3] & mask)]; + t1 = ToLinear8[cb = (wp[2] & mask)]; + t2 = ToLinear8[cg = (wp[1] & mask)]; + t3 = ToLinear8[cr = (wp[0] & mask)]; + op[0] = t0; + op[1] = t1; + op[2] = t2; + op[3] = t3; + n -= 4; + while (n > 0) + { + n -= 4; + wp += 4; + op += 4; + t0 = ToLinear8[(ca += wp[3]) & mask]; + t1 = ToLinear8[(cb += wp[2]) & mask]; + t2 = ToLinear8[(cg += wp[1]) & mask]; + t3 = ToLinear8[(cr += wp[0]) & mask]; + op[0] = t0; + op[1] = t1; + op[2] = t2; + op[3] = t3; + } + } + else + { + REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++) + n -= stride; + while (n > 0) + { + REPEAT(stride, wp[stride] += *wp; *op = ToLinear8[*wp & mask]; + wp++; op++) + n -= stride; + } + } + } +} + +/* + * State block for each open TIFF + * file using PixarLog compression/decompression. + */ +typedef struct +{ + TIFFPredictorState predict; + z_stream stream; + tmsize_t tbuf_size; /* only set/used on reading for now */ + uint16_t *tbuf; + uint16_t stride; + int state; + int user_datafmt; + int quality; +#define PLSTATE_INIT 1 + + TIFFVSetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ + + float *ToLinearF; + uint16_t *ToLinear16; + unsigned char *ToLinear8; + uint16_t *FromLT2; + uint16_t *From14; /* Really for 16-bit data, but we shift down 2 */ + uint16_t *From8; + +} PixarLogState; + +static int PixarLogMakeTables(TIFF *tif, PixarLogState *sp) +{ + + /* + * We make several tables here to convert between various external + * representations (float, 16-bit, and 8-bit) and the internal + * 11-bit companded representation. The 11-bit representation has two + * distinct regions. A linear bottom end up through .018316 in steps + * of about .000073, and a region of constant ratio up to about 25. + * These floating point numbers are stored in the main table ToLinearF. + * All other tables are derived from this one. The tables (and the + * ratios) are continuous at the internal seam. + */ + + int nlin, lt2size; + int i, j; + double b, c, linstep, v; + float *ToLinearF; + uint16_t *ToLinear16; + unsigned char *ToLinear8; + uint16_t *FromLT2; + uint16_t *From14; /* Really for 16-bit data, but we shift down 2 */ + uint16_t *From8; + + c = log(RATIO); + nlin = (int)(1. / c); /* nlin must be an integer */ + c = 1. / nlin; + b = exp(-c * ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */ + linstep = b * c * exp(1.); + + LogK1 = (float)(1. / c); /* if (v >= 2) token = k1*log(v*k2) */ + LogK2 = (float)(1. / b); + lt2size = (int)(2. / linstep) + 1; + FromLT2 = (uint16_t *)_TIFFmallocExt(tif, lt2size * sizeof(uint16_t)); + From14 = (uint16_t *)_TIFFmallocExt(tif, 16384 * sizeof(uint16_t)); + From8 = (uint16_t *)_TIFFmallocExt(tif, 256 * sizeof(uint16_t)); + ToLinearF = (float *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(float)); + ToLinear16 = (uint16_t *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(uint16_t)); + ToLinear8 = + (unsigned char *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(unsigned char)); + if (FromLT2 == NULL || From14 == NULL || From8 == NULL || + ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) + { + if (FromLT2) + _TIFFfreeExt(tif, FromLT2); + if (From14) + _TIFFfreeExt(tif, From14); + if (From8) + _TIFFfreeExt(tif, From8); + if (ToLinearF) + _TIFFfreeExt(tif, ToLinearF); + if (ToLinear16) + _TIFFfreeExt(tif, ToLinear16); + if (ToLinear8) + _TIFFfreeExt(tif, ToLinear8); + sp->FromLT2 = NULL; + sp->From14 = NULL; + sp->From8 = NULL; + sp->ToLinearF = NULL; + sp->ToLinear16 = NULL; + sp->ToLinear8 = NULL; + return 0; + } + + j = 0; + + for (i = 0; i < nlin; i++) + { + v = i * linstep; + ToLinearF[j++] = (float)v; + } + + for (i = nlin; i < TSIZE; i++) + ToLinearF[j++] = (float)(b * exp(c * i)); + + ToLinearF[2048] = ToLinearF[2047]; + + for (i = 0; i < TSIZEP1; i++) + { + v = ToLinearF[i] * 65535.0 + 0.5; + ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16_t)v; + v = ToLinearF[i] * 255.0 + 0.5; + ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v; + } + + j = 0; + for (i = 0; i < lt2size; i++) + { + if ((i * linstep) * (i * linstep) > ToLinearF[j] * ToLinearF[j + 1]) + j++; + FromLT2[i] = (uint16_t)j; + } + + /* + * Since we lose info anyway on 16-bit data, we set up a 14-bit + * table and shift 16-bit values down two bits on input. + * saves a little table space. + */ + j = 0; + for (i = 0; i < 16384; i++) + { + while ((i / 16383.) * (i / 16383.) > ToLinearF[j] * ToLinearF[j + 1]) + j++; + From14[i] = (uint16_t)j; + } + + j = 0; + for (i = 0; i < 256; i++) + { + while ((i / 255.) * (i / 255.) > ToLinearF[j] * ToLinearF[j + 1]) + j++; + From8[i] = (uint16_t)j; + } + + Fltsize = (float)(lt2size / 2); + + sp->ToLinearF = ToLinearF; + sp->ToLinear16 = ToLinear16; + sp->ToLinear8 = ToLinear8; + sp->FromLT2 = FromLT2; + sp->From14 = From14; + sp->From8 = From8; + + return 1; +} + +#define PixarLogDecoderState(tif) ((PixarLogState *)(tif)->tif_data) +#define PixarLogEncoderState(tif) ((PixarLogState *)(tif)->tif_data) + +static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); +static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); + +#define PIXARLOGDATAFMT_UNKNOWN -1 + +static int PixarLogGuessDataFmt(TIFFDirectory *td) +{ + int guess = PIXARLOGDATAFMT_UNKNOWN; + int format = td->td_sampleformat; + + /* If the user didn't tell us his datafmt, + * take our best guess from the bitspersample. + */ + switch (td->td_bitspersample) + { + case 32: + if (format == SAMPLEFORMAT_IEEEFP) + guess = PIXARLOGDATAFMT_FLOAT; + break; + case 16: + if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT) + guess = PIXARLOGDATAFMT_16BIT; + break; + case 12: + if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT) + guess = PIXARLOGDATAFMT_12BITPICIO; + break; + case 11: + if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT) + guess = PIXARLOGDATAFMT_11BITLOG; + break; + case 8: + if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT) + guess = PIXARLOGDATAFMT_8BIT; + break; + } + + return guess; +} + +static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2) +{ + return _TIFFMultiplySSize(NULL, m1, m2, NULL); +} + +static tmsize_t add_ms(tmsize_t m1, tmsize_t m2) +{ + assert(m1 >= 0 && m2 >= 0); + /* if either input is zero, assume overflow already occurred */ + if (m1 == 0 || m2 == 0) + return 0; + else if (m1 > TIFF_TMSIZE_T_MAX - m2) + return 0; + + return m1 + m2; +} + +static int PixarLogFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +static int PixarLogSetupDecode(TIFF *tif) +{ + static const char module[] = "PixarLogSetupDecode"; + TIFFDirectory *td = &tif->tif_dir; + PixarLogState *sp = PixarLogDecoderState(tif); + tmsize_t tbuf_size; + uint32_t strip_height; + + assert(sp != NULL); + + /* This function can possibly be called several times by */ + /* PredictorSetupDecode() if this function succeeds but */ + /* PredictorSetup() fails */ + if ((sp->state & PLSTATE_INIT) != 0) + return 1; + + strip_height = td->td_rowsperstrip; + if (strip_height > td->td_imagelength) + strip_height = td->td_imagelength; + + /* Make sure no byte swapping happens on the data + * after decompression. */ + tif->tif_postdecode = _TIFFNoPostDecode; + + /* for some reason, we can't do this in TIFFInitPixarLog */ + + sp->stride = + (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel + : 1); + tbuf_size = multiply_ms( + multiply_ms(multiply_ms(sp->stride, td->td_imagewidth), strip_height), + sizeof(uint16_t)); + /* add one more stride in case input ends mid-stride */ + tbuf_size = add_ms(tbuf_size, sizeof(uint16_t) * sp->stride); + if (tbuf_size == 0) + return (0); /* TODO: this is an error return without error report + through TIFFErrorExt */ + sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size); + if (sp->tbuf == NULL) + return (0); + sp->tbuf_size = tbuf_size; + if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) + sp->user_datafmt = PixarLogGuessDataFmt(td); + if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) + { + _TIFFfreeExt(tif, sp->tbuf); + sp->tbuf = NULL; + sp->tbuf_size = 0; + TIFFErrorExtR(tif, module, + "PixarLog compression can't handle bits depth/data " + "format combination (depth: %" PRIu16 ")", + td->td_bitspersample); + return (0); + } + + if (inflateInit(&sp->stream) != Z_OK) + { + _TIFFfreeExt(tif, sp->tbuf); + sp->tbuf = NULL; + sp->tbuf_size = 0; + TIFFErrorExtR(tif, module, "%s", + sp->stream.msg ? sp->stream.msg : "(null)"); + return (0); + } + else + { + sp->state |= PLSTATE_INIT; + return (1); + } +} + +/* + * Setup state for decoding a strip. + */ +static int PixarLogPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "PixarLogPreDecode"; + PixarLogState *sp = PixarLogDecoderState(tif); + + (void)s; + assert(sp != NULL); + sp->stream.next_in = tif->tif_rawdata; + assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + sp->stream.avail_in = (uInt)tif->tif_rawcc; + if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc) + { + TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size"); + return (0); + } + return (inflateReset(&sp->stream) == Z_OK); +} + +static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "PixarLogDecode"; + TIFFDirectory *td = &tif->tif_dir; + PixarLogState *sp = PixarLogDecoderState(tif); + tmsize_t i; + tmsize_t nsamples; + int llen; + uint16_t *up; + + switch (sp->user_datafmt) + { + case PIXARLOGDATAFMT_FLOAT: + nsamples = occ / sizeof(float); /* XXX float == 32 bits */ + break; + case PIXARLOGDATAFMT_16BIT: + case PIXARLOGDATAFMT_12BITPICIO: + case PIXARLOGDATAFMT_11BITLOG: + nsamples = occ / sizeof(uint16_t); /* XXX uint16_t == 16 bits */ + break; + case PIXARLOGDATAFMT_8BIT: + case PIXARLOGDATAFMT_8BITABGR: + nsamples = occ; + break; + default: + TIFFErrorExtR(tif, module, + "%" PRIu16 " bit input not supported in PixarLog", + td->td_bitspersample); + memset(op, 0, (size_t)occ); + return 0; + } + + llen = sp->stride * td->td_imagewidth; + + (void)s; + assert(sp != NULL); + + sp->stream.next_in = tif->tif_rawcp; + sp->stream.avail_in = (uInt)tif->tif_rawcc; + + sp->stream.next_out = (unsigned char *)sp->tbuf; + assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + sp->stream.avail_out = (uInt)(nsamples * sizeof(uint16_t)); + if (sp->stream.avail_out != nsamples * sizeof(uint16_t)) + { + TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size"); + memset(op, 0, (size_t)occ); + return (0); + } + /* Check that we will not fill more than what was allocated */ + if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size) + { + TIFFErrorExtR(tif, module, "sp->stream.avail_out > sp->tbuf_size"); + memset(op, 0, (size_t)occ); + return (0); + } + do + { + int state = inflate(&sp->stream, Z_PARTIAL_FLUSH); + if (state == Z_STREAM_END) + { + break; /* XXX */ + } + if (state == Z_DATA_ERROR) + { + TIFFErrorExtR( + tif, module, "Decoding error at scanline %" PRIu32 ", %s", + tif->tif_row, sp->stream.msg ? sp->stream.msg : "(null)"); + memset(op, 0, (size_t)occ); + return (0); + } + if (state != Z_OK) + { + TIFFErrorExtR(tif, module, "ZLib error: %s", + sp->stream.msg ? sp->stream.msg : "(null)"); + memset(op, 0, (size_t)occ); + return (0); + } + } while (sp->stream.avail_out > 0); + + /* hopefully, we got all the bytes we needed */ + if (sp->stream.avail_out != 0) + { + TIFFErrorExtR(tif, module, + "Not enough data at scanline %" PRIu32 + " (short %u bytes)", + tif->tif_row, sp->stream.avail_out); + memset(op, 0, (size_t)occ); + return (0); + } + + tif->tif_rawcp = sp->stream.next_in; + tif->tif_rawcc = sp->stream.avail_in; + + up = sp->tbuf; + /* Swap bytes in the data if from a different endian machine. */ + if (tif->tif_flags & TIFF_SWAB) + TIFFSwabArrayOfShort(up, nsamples); + + /* + * if llen is not an exact multiple of nsamples, the decode operation + * may overflow the output buffer, so truncate it enough to prevent + * that but still salvage as much data as possible. + */ + if (nsamples % llen) + { + TIFFWarningExtR(tif, module, + "stride %d is not a multiple of sample count, " + "%" TIFF_SSIZE_FORMAT ", data truncated.", + llen, nsamples); + nsamples -= nsamples % llen; + } + + for (i = 0; i < nsamples; i += llen, up += llen) + { + switch (sp->user_datafmt) + { + case PIXARLOGDATAFMT_FLOAT: + horizontalAccumulateF(up, llen, sp->stride, (float *)op, + sp->ToLinearF); + op += llen * sizeof(float); + break; + case PIXARLOGDATAFMT_16BIT: + horizontalAccumulate16(up, llen, sp->stride, (uint16_t *)op, + sp->ToLinear16); + op += llen * sizeof(uint16_t); + break; + case PIXARLOGDATAFMT_12BITPICIO: + horizontalAccumulate12(up, llen, sp->stride, (int16_t *)op, + sp->ToLinearF); + op += llen * sizeof(int16_t); + break; + case PIXARLOGDATAFMT_11BITLOG: + horizontalAccumulate11(up, llen, sp->stride, (uint16_t *)op); + op += llen * sizeof(uint16_t); + break; + case PIXARLOGDATAFMT_8BIT: + horizontalAccumulate8(up, llen, sp->stride, (unsigned char *)op, + sp->ToLinear8); + op += llen * sizeof(unsigned char); + break; + case PIXARLOGDATAFMT_8BITABGR: + horizontalAccumulate8abgr(up, llen, sp->stride, + (unsigned char *)op, sp->ToLinear8); + op += llen * sizeof(unsigned char); + break; + default: + TIFFErrorExtR(tif, module, "Unsupported bits/sample: %" PRIu16, + td->td_bitspersample); + memset(op, 0, (size_t)occ); + return (0); + } + } + + return (1); +} + +static int PixarLogSetupEncode(TIFF *tif) +{ + static const char module[] = "PixarLogSetupEncode"; + TIFFDirectory *td = &tif->tif_dir; + PixarLogState *sp = PixarLogEncoderState(tif); + tmsize_t tbuf_size; + + assert(sp != NULL); + + /* for some reason, we can't do this in TIFFInitPixarLog */ + + sp->stride = + (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel + : 1); + tbuf_size = + multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth), + td->td_rowsperstrip), + sizeof(uint16_t)); + if (tbuf_size == 0) + return (0); /* TODO: this is an error return without error report + through TIFFErrorExt */ + sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size); + if (sp->tbuf == NULL) + return (0); + if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) + sp->user_datafmt = PixarLogGuessDataFmt(td); + if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) + { + TIFFErrorExtR(tif, module, + "PixarLog compression can't handle %" PRIu16 + " bit linear encodings", + td->td_bitspersample); + return (0); + } + + if (deflateInit(&sp->stream, sp->quality) != Z_OK) + { + TIFFErrorExtR(tif, module, "%s", + sp->stream.msg ? sp->stream.msg : "(null)"); + return (0); + } + else + { + sp->state |= PLSTATE_INIT; + return (1); + } +} + +/* + * Reset encoding state at the start of a strip. + */ +static int PixarLogPreEncode(TIFF *tif, uint16_t s) +{ + static const char module[] = "PixarLogPreEncode"; + PixarLogState *sp = PixarLogEncoderState(tif); + + (void)s; + assert(sp != NULL); + sp->stream.next_out = tif->tif_rawdata; + assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + sp->stream.avail_out = (uInt)tif->tif_rawdatasize; + if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) + { + TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size"); + return (0); + } + return (deflateReset(&sp->stream) == Z_OK); +} + +static void horizontalDifferenceF(float *ip, int n, int stride, uint16_t *wp, + uint16_t *FromLT2) +{ + int32_t r1, g1, b1, a1, r2, g2, b2, a2, mask; + float fltsize = Fltsize; + +#define CLAMP(v) \ + ((v < (float)0.) ? 0 \ + : (v < (float)2.) ? FromLT2[(int)(v * fltsize)] \ + : (v > (float)24.2) ? 2047 \ + : LogK1 * log(v * LogK2) + 0.5) + + mask = CODE_MASK; + if (n >= stride) + { + if (stride == 3) + { + r2 = wp[0] = (uint16_t)CLAMP(ip[0]); + g2 = wp[1] = (uint16_t)CLAMP(ip[1]); + b2 = wp[2] = (uint16_t)CLAMP(ip[2]); + n -= 3; + while (n > 0) + { + n -= 3; + wp += 3; + ip += 3; + r1 = (int32_t)CLAMP(ip[0]); + wp[0] = (uint16_t)((r1 - r2) & mask); + r2 = r1; + g1 = (int32_t)CLAMP(ip[1]); + wp[1] = (uint16_t)((g1 - g2) & mask); + g2 = g1; + b1 = (int32_t)CLAMP(ip[2]); + wp[2] = (uint16_t)((b1 - b2) & mask); + b2 = b1; + } + } + else if (stride == 4) + { + r2 = wp[0] = (uint16_t)CLAMP(ip[0]); + g2 = wp[1] = (uint16_t)CLAMP(ip[1]); + b2 = wp[2] = (uint16_t)CLAMP(ip[2]); + a2 = wp[3] = (uint16_t)CLAMP(ip[3]); + n -= 4; + while (n > 0) + { + n -= 4; + wp += 4; + ip += 4; + r1 = (int32_t)CLAMP(ip[0]); + wp[0] = (uint16_t)((r1 - r2) & mask); + r2 = r1; + g1 = (int32_t)CLAMP(ip[1]); + wp[1] = (uint16_t)((g1 - g2) & mask); + g2 = g1; + b1 = (int32_t)CLAMP(ip[2]); + wp[2] = (uint16_t)((b1 - b2) & mask); + b2 = b1; + a1 = (int32_t)CLAMP(ip[3]); + wp[3] = (uint16_t)((a1 - a2) & mask); + a2 = a1; + } + } + else + { + REPEAT(stride, wp[0] = (uint16_t)CLAMP(ip[0]); wp++; ip++) + n -= stride; + while (n > 0) + { + REPEAT(stride, + wp[0] = (uint16_t)(((int32_t)CLAMP(ip[0]) - + (int32_t)CLAMP(ip[-stride])) & + mask); + wp++; ip++) + n -= stride; + } + } + } +} + +static void horizontalDifference16(unsigned short *ip, int n, int stride, + unsigned short *wp, uint16_t *From14) +{ + register int r1, g1, b1, a1, r2, g2, b2, a2, mask; + +/* assumption is unsigned pixel values */ +#undef CLAMP +#define CLAMP(v) From14[(v) >> 2] + + mask = CODE_MASK; + if (n >= stride) + { + if (stride == 3) + { + r2 = wp[0] = CLAMP(ip[0]); + g2 = wp[1] = CLAMP(ip[1]); + b2 = wp[2] = CLAMP(ip[2]); + n -= 3; + while (n > 0) + { + n -= 3; + wp += 3; + ip += 3; + r1 = CLAMP(ip[0]); + wp[0] = (uint16_t)((r1 - r2) & mask); + r2 = r1; + g1 = CLAMP(ip[1]); + wp[1] = (uint16_t)((g1 - g2) & mask); + g2 = g1; + b1 = CLAMP(ip[2]); + wp[2] = (uint16_t)((b1 - b2) & mask); + b2 = b1; + } + } + else if (stride == 4) + { + r2 = wp[0] = CLAMP(ip[0]); + g2 = wp[1] = CLAMP(ip[1]); + b2 = wp[2] = CLAMP(ip[2]); + a2 = wp[3] = CLAMP(ip[3]); + n -= 4; + while (n > 0) + { + n -= 4; + wp += 4; + ip += 4; + r1 = CLAMP(ip[0]); + wp[0] = (uint16_t)((r1 - r2) & mask); + r2 = r1; + g1 = CLAMP(ip[1]); + wp[1] = (uint16_t)((g1 - g2) & mask); + g2 = g1; + b1 = CLAMP(ip[2]); + wp[2] = (uint16_t)((b1 - b2) & mask); + b2 = b1; + a1 = CLAMP(ip[3]); + wp[3] = (uint16_t)((a1 - a2) & mask); + a2 = a1; + } + } + else + { + REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++) + n -= stride; + while (n > 0) + { + REPEAT(stride, + wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) & + mask); + wp++; ip++) + n -= stride; + } + } + } +} + +static void horizontalDifference8(unsigned char *ip, int n, int stride, + unsigned short *wp, uint16_t *From8) +{ + register int r1, g1, b1, a1, r2, g2, b2, a2, mask; + +#undef CLAMP +#define CLAMP(v) (From8[(v)]) + + mask = CODE_MASK; + if (n >= stride) + { + if (stride == 3) + { + r2 = wp[0] = CLAMP(ip[0]); + g2 = wp[1] = CLAMP(ip[1]); + b2 = wp[2] = CLAMP(ip[2]); + n -= 3; + while (n > 0) + { + n -= 3; + r1 = CLAMP(ip[3]); + wp[3] = (uint16_t)((r1 - r2) & mask); + r2 = r1; + g1 = CLAMP(ip[4]); + wp[4] = (uint16_t)((g1 - g2) & mask); + g2 = g1; + b1 = CLAMP(ip[5]); + wp[5] = (uint16_t)((b1 - b2) & mask); + b2 = b1; + wp += 3; + ip += 3; + } + } + else if (stride == 4) + { + r2 = wp[0] = CLAMP(ip[0]); + g2 = wp[1] = CLAMP(ip[1]); + b2 = wp[2] = CLAMP(ip[2]); + a2 = wp[3] = CLAMP(ip[3]); + n -= 4; + while (n > 0) + { + n -= 4; + r1 = CLAMP(ip[4]); + wp[4] = (uint16_t)((r1 - r2) & mask); + r2 = r1; + g1 = CLAMP(ip[5]); + wp[5] = (uint16_t)((g1 - g2) & mask); + g2 = g1; + b1 = CLAMP(ip[6]); + wp[6] = (uint16_t)((b1 - b2) & mask); + b2 = b1; + a1 = CLAMP(ip[7]); + wp[7] = (uint16_t)((a1 - a2) & mask); + a2 = a1; + wp += 4; + ip += 4; + } + } + else + { + REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++) + n -= stride; + while (n > 0) + { + REPEAT(stride, + wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) & + mask); + wp++; ip++) + n -= stride; + } + } + } +} + +/* + * Encode a chunk of pixels. + */ +static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "PixarLogEncode"; + TIFFDirectory *td = &tif->tif_dir; + PixarLogState *sp = PixarLogEncoderState(tif); + tmsize_t i; + tmsize_t n; + int llen; + unsigned short *up; + + (void)s; + + switch (sp->user_datafmt) + { + case PIXARLOGDATAFMT_FLOAT: + n = cc / sizeof(float); /* XXX float == 32 bits */ + break; + case PIXARLOGDATAFMT_16BIT: + case PIXARLOGDATAFMT_12BITPICIO: + case PIXARLOGDATAFMT_11BITLOG: + n = cc / sizeof(uint16_t); /* XXX uint16_t == 16 bits */ + break; + case PIXARLOGDATAFMT_8BIT: + case PIXARLOGDATAFMT_8BITABGR: + n = cc; + break; + default: + TIFFErrorExtR(tif, module, + "%" PRIu16 " bit input not supported in PixarLog", + td->td_bitspersample); + return 0; + } + + llen = sp->stride * td->td_imagewidth; + /* Check against the number of elements (of size uint16_t) of sp->tbuf */ + if (n > ((tmsize_t)td->td_rowsperstrip * llen)) + { + TIFFErrorExtR(tif, module, "Too many input bytes provided"); + return 0; + } + + for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) + { + switch (sp->user_datafmt) + { + case PIXARLOGDATAFMT_FLOAT: + horizontalDifferenceF((float *)bp, llen, sp->stride, up, + sp->FromLT2); + bp += llen * sizeof(float); + break; + case PIXARLOGDATAFMT_16BIT: + horizontalDifference16((uint16_t *)bp, llen, sp->stride, up, + sp->From14); + bp += llen * sizeof(uint16_t); + break; + case PIXARLOGDATAFMT_8BIT: + horizontalDifference8((unsigned char *)bp, llen, sp->stride, up, + sp->From8); + bp += llen * sizeof(unsigned char); + break; + default: + TIFFErrorExtR(tif, module, + "%" PRIu16 " bit input not supported in PixarLog", + td->td_bitspersample); + return 0; + } + } + + sp->stream.next_in = (unsigned char *)sp->tbuf; + assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + sp->stream.avail_in = (uInt)(n * sizeof(uint16_t)); + if ((sp->stream.avail_in / sizeof(uint16_t)) != (uInt)n) + { + TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size"); + return (0); + } + + do + { + if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) + { + TIFFErrorExtR(tif, module, "Encoder error: %s", + sp->stream.msg ? sp->stream.msg : "(null)"); + return (0); + } + if (sp->stream.avail_out == 0) + { + tif->tif_rawcc = tif->tif_rawdatasize; + if (!TIFFFlushData1(tif)) + return 0; + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = + (uInt)tif + ->tif_rawdatasize; /* this is a safe typecast, as check is + made already in PixarLogPreEncode */ + } + } while (sp->stream.avail_in > 0); + return (1); +} + +/* + * Finish off an encoded strip by flushing the last + * string and tacking on an End Of Information code. + */ + +static int PixarLogPostEncode(TIFF *tif) +{ + static const char module[] = "PixarLogPostEncode"; + PixarLogState *sp = PixarLogEncoderState(tif); + int state; + + sp->stream.avail_in = 0; + + do + { + state = deflate(&sp->stream, Z_FINISH); + switch (state) + { + case Z_STREAM_END: + case Z_OK: + if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) + { + tif->tif_rawcc = + tif->tif_rawdatasize - sp->stream.avail_out; + if (!TIFFFlushData1(tif)) + return 0; + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = + (uInt)tif->tif_rawdatasize; /* this is a safe typecast, + as check is made already + in PixarLogPreEncode */ + } + break; + default: + TIFFErrorExtR(tif, module, "ZLib error: %s", + sp->stream.msg ? sp->stream.msg : "(null)"); + return (0); + } + } while (state != Z_STREAM_END); + return (1); +} + +static void PixarLogClose(TIFF *tif) +{ + PixarLogState *sp = (PixarLogState *)tif->tif_data; + TIFFDirectory *td = &tif->tif_dir; + + assert(sp != 0); + /* In a really sneaky (and really incorrect, and untruthful, and + * troublesome, and error-prone) maneuver that completely goes against + * the spirit of TIFF, and breaks TIFF, on close, we covertly + * modify both bitspersample and sampleformat in the directory to + * indicate 8-bit linear. This way, the decode "just works" even for + * readers that don't know about PixarLog, or how to set + * the PIXARLOGDATFMT pseudo-tag. + */ + + if (sp->state & PLSTATE_INIT) + { + /* We test the state to avoid an issue such as in + * http://bugzilla.maptools.org/show_bug.cgi?id=2604 + * What appends in that case is that the bitspersample is 1 and + * a TransferFunction is set. The size of the TransferFunction + * depends on 1<td_bitspersample = 8; + td->td_sampleformat = SAMPLEFORMAT_UINT; + } +} + +static void PixarLogCleanup(TIFF *tif) +{ + PixarLogState *sp = (PixarLogState *)tif->tif_data; + + assert(sp != 0); + + (void)TIFFPredictorCleanup(tif); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + if (sp->FromLT2) + _TIFFfreeExt(tif, sp->FromLT2); + if (sp->From14) + _TIFFfreeExt(tif, sp->From14); + if (sp->From8) + _TIFFfreeExt(tif, sp->From8); + if (sp->ToLinearF) + _TIFFfreeExt(tif, sp->ToLinearF); + if (sp->ToLinear16) + _TIFFfreeExt(tif, sp->ToLinear16); + if (sp->ToLinear8) + _TIFFfreeExt(tif, sp->ToLinear8); + if (sp->state & PLSTATE_INIT) + { + if (tif->tif_mode == O_RDONLY) + inflateEnd(&sp->stream); + else + deflateEnd(&sp->stream); + } + if (sp->tbuf) + _TIFFfreeExt(tif, sp->tbuf); + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static int PixarLogVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "PixarLogVSetField"; + PixarLogState *sp = (PixarLogState *)tif->tif_data; + int result; + + switch (tag) + { + case TIFFTAG_PIXARLOGQUALITY: + sp->quality = (int)va_arg(ap, int); + if (tif->tif_mode != O_RDONLY && (sp->state & PLSTATE_INIT)) + { + if (deflateParams(&sp->stream, sp->quality, + Z_DEFAULT_STRATEGY) != Z_OK) + { + TIFFErrorExtR(tif, module, "ZLib error: %s", + sp->stream.msg ? sp->stream.msg : "(null)"); + return (0); + } + } + return (1); + case TIFFTAG_PIXARLOGDATAFMT: + sp->user_datafmt = (int)va_arg(ap, int); + /* Tweak the TIFF header so that the rest of libtiff knows what + * size of data will be passed between app and library, and + * assume that the app knows what it is doing and is not + * confused by these header manipulations... + */ + switch (sp->user_datafmt) + { + case PIXARLOGDATAFMT_8BIT: + case PIXARLOGDATAFMT_8BITABGR: + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); + TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); + break; + case PIXARLOGDATAFMT_11BITLOG: + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); + TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); + break; + case PIXARLOGDATAFMT_12BITPICIO: + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); + TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT); + break; + case PIXARLOGDATAFMT_16BIT: + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); + TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); + break; + case PIXARLOGDATAFMT_FLOAT: + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32); + TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, + SAMPLEFORMAT_IEEEFP); + break; + } + /* + * Must recalculate sizes should bits/sample change. + */ + tif->tif_tilesize = + isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1); + tif->tif_scanlinesize = TIFFScanlineSize(tif); + result = 1; /* NB: pseudo tag */ + break; + default: + result = (*sp->vsetparent)(tif, tag, ap); + } + return (result); +} + +static int PixarLogVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + PixarLogState *sp = (PixarLogState *)tif->tif_data; + + switch (tag) + { + case TIFFTAG_PIXARLOGQUALITY: + *va_arg(ap, int *) = sp->quality; + break; + case TIFFTAG_PIXARLOGDATAFMT: + *va_arg(ap, int *) = sp->user_datafmt; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return (1); +} + +static const TIFFField pixarlogFields[] = { + {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + FALSE, FALSE, "", NULL}, + {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + FALSE, FALSE, "", NULL}}; + +int TIFFInitPixarLog(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitPixarLog"; + + PixarLogState *sp; + + (void)scheme; + assert(scheme == COMPRESSION_PIXARLOG); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, pixarlogFields, TIFFArrayCount(pixarlogFields))) + { + TIFFErrorExtR(tif, module, + "Merging PixarLog codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(PixarLogState)); + if (tif->tif_data == NULL) + goto bad; + sp = (PixarLogState *)tif->tif_data; + _TIFFmemset(sp, 0, sizeof(*sp)); + sp->stream.data_type = Z_BINARY; + sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN; + + /* + * Install codec methods. + */ + tif->tif_fixuptags = PixarLogFixupTags; + tif->tif_setupdecode = PixarLogSetupDecode; + tif->tif_predecode = PixarLogPreDecode; + tif->tif_decoderow = PixarLogDecode; + tif->tif_decodestrip = PixarLogDecode; + tif->tif_decodetile = PixarLogDecode; + tif->tif_setupencode = PixarLogSetupEncode; + tif->tif_preencode = PixarLogPreEncode; + tif->tif_postencode = PixarLogPostEncode; + tif->tif_encoderow = PixarLogEncode; + tif->tif_encodestrip = PixarLogEncode; + tif->tif_encodetile = PixarLogEncode; + tif->tif_close = PixarLogClose; + tif->tif_cleanup = PixarLogCleanup; + + /* Override SetField so we can handle our private pseudo-tag */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */ + + /* Default values for codec-specific fields */ + sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */ + sp->state = 0; + + /* we don't wish to use the predictor, + * the default is none, which predictor value 1 + */ + (void)TIFFPredictorInit(tif); + + /* + * build the companding tables + */ + PixarLogMakeTables(tif, sp); + + return (1); +bad: + TIFFErrorExtR(tif, module, "No space for PixarLog state block"); + return (0); +} +#endif /* PIXARLOG_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_predict.c b/cpp/3rd_party/libtiff/tif_predict.c new file mode 100644 index 0000000000..7a6fc4af8b --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_predict.c @@ -0,0 +1,1161 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Predictor Tag Support (used by multiple codecs). + */ +#include "tif_predict.h" +#include "tiffiop.h" + +#if defined(__x86_64__) || defined(_M_X64) +#include +#endif + +#define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data) + +static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc); +static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0, + uint16_t s); +static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0, + uint16_t s); +static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); +static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0, + uint16_t s); + +static int PredictorSetup(TIFF *tif) +{ + static const char module[] = "PredictorSetup"; + + TIFFPredictorState *sp = PredictorState(tif); + TIFFDirectory *td = &tif->tif_dir; + + switch (sp->predictor) /* no differencing */ + { + case PREDICTOR_NONE: + return 1; + case PREDICTOR_HORIZONTAL: + if (td->td_bitspersample != 8 && td->td_bitspersample != 16 && + td->td_bitspersample != 32 && td->td_bitspersample != 64) + { + TIFFErrorExtR(tif, module, + "Horizontal differencing \"Predictor\" not " + "supported with %" PRIu16 "-bit samples", + td->td_bitspersample); + return 0; + } + break; + case PREDICTOR_FLOATINGPOINT: + if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) + { + TIFFErrorExtR( + tif, module, + "Floating point \"Predictor\" not supported with %" PRIu16 + " data format", + td->td_sampleformat); + return 0; + } + if (td->td_bitspersample != 16 && td->td_bitspersample != 24 && + td->td_bitspersample != 32 && td->td_bitspersample != 64) + { /* Should 64 be allowed? */ + TIFFErrorExtR( + tif, module, + "Floating point \"Predictor\" not supported with %" PRIu16 + "-bit samples", + td->td_bitspersample); + return 0; + } + break; + default: + TIFFErrorExtR(tif, module, "\"Predictor\" value %d not supported", + sp->predictor); + return 0; + } + sp->stride = + (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel + : 1); + /* + * Calculate the scanline/tile-width size in bytes. + */ + if (isTiled(tif)) + sp->rowsize = TIFFTileRowSize(tif); + else + sp->rowsize = TIFFScanlineSize(tif); + if (sp->rowsize == 0) + return 0; + + return 1; +} + +static int PredictorSetupDecode(TIFF *tif) +{ + TIFFPredictorState *sp = PredictorState(tif); + TIFFDirectory *td = &tif->tif_dir; + + /* Note: when PredictorSetup() fails, the effets of setupdecode() */ + /* will not be "canceled" so setupdecode() might be robust to */ + /* be called several times. */ + if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif)) + return 0; + + if (sp->predictor == 2) + { + switch (td->td_bitspersample) + { + case 8: + sp->decodepfunc = horAcc8; + break; + case 16: + sp->decodepfunc = horAcc16; + break; + case 32: + sp->decodepfunc = horAcc32; + break; + case 64: + sp->decodepfunc = horAcc64; + break; + } + /* + * Override default decoding method with one that does the + * predictor stuff. + */ + if (tif->tif_decoderow != PredictorDecodeRow) + { + sp->decoderow = tif->tif_decoderow; + tif->tif_decoderow = PredictorDecodeRow; + sp->decodestrip = tif->tif_decodestrip; + tif->tif_decodestrip = PredictorDecodeTile; + sp->decodetile = tif->tif_decodetile; + tif->tif_decodetile = PredictorDecodeTile; + } + + /* + * If the data is horizontally differenced 16-bit data that + * requires byte-swapping, then it must be byte swapped before + * the accumulation step. We do this with a special-purpose + * routine and override the normal post decoding logic that + * the library setup when the directory was read. + */ + if (tif->tif_flags & TIFF_SWAB) + { + if (sp->decodepfunc == horAcc16) + { + sp->decodepfunc = swabHorAcc16; + tif->tif_postdecode = _TIFFNoPostDecode; + } + else if (sp->decodepfunc == horAcc32) + { + sp->decodepfunc = swabHorAcc32; + tif->tif_postdecode = _TIFFNoPostDecode; + } + else if (sp->decodepfunc == horAcc64) + { + sp->decodepfunc = swabHorAcc64; + tif->tif_postdecode = _TIFFNoPostDecode; + } + } + } + + else if (sp->predictor == 3) + { + sp->decodepfunc = fpAcc; + /* + * Override default decoding method with one that does the + * predictor stuff. + */ + if (tif->tif_decoderow != PredictorDecodeRow) + { + sp->decoderow = tif->tif_decoderow; + tif->tif_decoderow = PredictorDecodeRow; + sp->decodestrip = tif->tif_decodestrip; + tif->tif_decodestrip = PredictorDecodeTile; + sp->decodetile = tif->tif_decodetile; + tif->tif_decodetile = PredictorDecodeTile; + } + /* + * The data should not be swapped outside of the floating + * point predictor, the accumulation routine should return + * bytes in the native order. + */ + if (tif->tif_flags & TIFF_SWAB) + { + tif->tif_postdecode = _TIFFNoPostDecode; + } + } + + return 1; +} + +static int PredictorSetupEncode(TIFF *tif) +{ + TIFFPredictorState *sp = PredictorState(tif); + TIFFDirectory *td = &tif->tif_dir; + + if (!(*sp->setupencode)(tif) || !PredictorSetup(tif)) + return 0; + + if (sp->predictor == 2) + { + switch (td->td_bitspersample) + { + case 8: + sp->encodepfunc = horDiff8; + break; + case 16: + sp->encodepfunc = horDiff16; + break; + case 32: + sp->encodepfunc = horDiff32; + break; + case 64: + sp->encodepfunc = horDiff64; + break; + } + /* + * Override default encoding method with one that does the + * predictor stuff. + */ + if (tif->tif_encoderow != PredictorEncodeRow) + { + sp->encoderow = tif->tif_encoderow; + tif->tif_encoderow = PredictorEncodeRow; + sp->encodestrip = tif->tif_encodestrip; + tif->tif_encodestrip = PredictorEncodeTile; + sp->encodetile = tif->tif_encodetile; + tif->tif_encodetile = PredictorEncodeTile; + } + + /* + * If the data is horizontally differenced 16-bit data that + * requires byte-swapping, then it must be byte swapped after + * the differentiation step. We do this with a special-purpose + * routine and override the normal post decoding logic that + * the library setup when the directory was read. + */ + if (tif->tif_flags & TIFF_SWAB) + { + if (sp->encodepfunc == horDiff16) + { + sp->encodepfunc = swabHorDiff16; + tif->tif_postdecode = _TIFFNoPostDecode; + } + else if (sp->encodepfunc == horDiff32) + { + sp->encodepfunc = swabHorDiff32; + tif->tif_postdecode = _TIFFNoPostDecode; + } + else if (sp->encodepfunc == horDiff64) + { + sp->encodepfunc = swabHorDiff64; + tif->tif_postdecode = _TIFFNoPostDecode; + } + } + } + + else if (sp->predictor == 3) + { + sp->encodepfunc = fpDiff; + /* + * Override default encoding method with one that does the + * predictor stuff. + */ + if (tif->tif_encoderow != PredictorEncodeRow) + { + sp->encoderow = tif->tif_encoderow; + tif->tif_encoderow = PredictorEncodeRow; + sp->encodestrip = tif->tif_encodestrip; + tif->tif_encodestrip = PredictorEncodeTile; + sp->encodetile = tif->tif_encodetile; + tif->tif_encodetile = PredictorEncodeTile; + } + /* + * The data should not be swapped outside of the floating + * point predictor, the differentiation routine should return + * bytes in the native order. + */ + if (tif->tif_flags & TIFF_SWAB) + { + tif->tif_postdecode = _TIFFNoPostDecode; + } + } + + return 1; +} + +#define REPEAT4(n, op) \ + switch (n) \ + { \ + default: \ + { \ + tmsize_t i; \ + for (i = n - 4; i > 0; i--) \ + { \ + op; \ + } \ + } /*-fallthrough*/ \ + case 4: \ + op; /*-fallthrough*/ \ + case 3: \ + op; /*-fallthrough*/ \ + case 2: \ + op; /*-fallthrough*/ \ + case 1: \ + op; /*-fallthrough*/ \ + case 0:; \ + } + +/* Remarks related to C standard compliance in all below functions : */ +/* - to avoid any undefined behavior, we only operate on unsigned types */ +/* since the behavior of "overflows" is defined (wrap over) */ +/* - when storing into the byte stream, we explicitly mask with 0xff so */ +/* as to make icc -check=conversions happy (not necessary by the standard) */ + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + tmsize_t stride = PredictorState(tif)->stride; + + uint8_t *cp = cp0; + if ((cc % stride) != 0) + { + TIFFErrorExtR(tif, "horAcc8", "%s", "(cc%stride)!=0"); + return 0; + } + + if (cc > stride) + { + /* + * Pipeline the most common cases. + */ + if (stride == 1) + { + uint32_t acc = cp[0]; + tmsize_t i = stride; + for (; i < cc - 3; i += 4) + { + cp[i + 0] = (uint8_t)((acc += cp[i + 0]) & 0xff); + cp[i + 1] = (uint8_t)((acc += cp[i + 1]) & 0xff); + cp[i + 2] = (uint8_t)((acc += cp[i + 2]) & 0xff); + cp[i + 3] = (uint8_t)((acc += cp[i + 3]) & 0xff); + } + for (; i < cc; i++) + { + cp[i + 0] = (uint8_t)((acc += cp[i + 0]) & 0xff); + } + } + else if (stride == 3) + { + uint32_t cr = cp[0]; + uint32_t cg = cp[1]; + uint32_t cb = cp[2]; + tmsize_t i = stride; + for (; i < cc; i += stride) + { + cp[i + 0] = (uint8_t)((cr += cp[i + 0]) & 0xff); + cp[i + 1] = (uint8_t)((cg += cp[i + 1]) & 0xff); + cp[i + 2] = (uint8_t)((cb += cp[i + 2]) & 0xff); + } + } + else if (stride == 4) + { + uint32_t cr = cp[0]; + uint32_t cg = cp[1]; + uint32_t cb = cp[2]; + uint32_t ca = cp[3]; + tmsize_t i = stride; + for (; i < cc; i += stride) + { + cp[i + 0] = (uint8_t)((cr += cp[i + 0]) & 0xff); + cp[i + 1] = (uint8_t)((cg += cp[i + 1]) & 0xff); + cp[i + 2] = (uint8_t)((cb += cp[i + 2]) & 0xff); + cp[i + 3] = (uint8_t)((ca += cp[i + 3]) & 0xff); + } + } + else + { + cc -= stride; + do + { + REPEAT4(stride, + cp[stride] = (uint8_t)((cp[stride] + *cp) & 0xff); + cp++) + cc -= stride; + } while (cc > 0); + } + } + return 1; +} + +static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + uint16_t *wp = (uint16_t *)cp0; + tmsize_t wc = cc / 2; + + TIFFSwabArrayOfShort(wp, wc); + return horAcc16(tif, cp0, cc); +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + tmsize_t stride = PredictorState(tif)->stride; + uint16_t *wp = (uint16_t *)cp0; + tmsize_t wc = cc / 2; + + if ((cc % (2 * stride)) != 0) + { + TIFFErrorExtR(tif, "horAcc16", "%s", "cc%(2*stride))!=0"); + return 0; + } + + if (wc > stride) + { + wc -= stride; + do + { + REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] + + (unsigned int)wp[0]) & + 0xffff); + wp++) + wc -= stride; + } while (wc > 0); + } + return 1; +} + +static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + uint32_t *wp = (uint32_t *)cp0; + tmsize_t wc = cc / 4; + + TIFFSwabArrayOfLong(wp, wc); + return horAcc32(tif, cp0, cc); +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + tmsize_t stride = PredictorState(tif)->stride; + uint32_t *wp = (uint32_t *)cp0; + tmsize_t wc = cc / 4; + + if ((cc % (4 * stride)) != 0) + { + TIFFErrorExtR(tif, "horAcc32", "%s", "cc%(4*stride))!=0"); + return 0; + } + + if (wc > stride) + { + wc -= stride; + do + { + REPEAT4(stride, wp[stride] += wp[0]; wp++) + wc -= stride; + } while (wc > 0); + } + return 1; +} + +static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + uint64_t *wp = (uint64_t *)cp0; + tmsize_t wc = cc / 8; + + TIFFSwabArrayOfLong8(wp, wc); + return horAcc64(tif, cp0, cc); +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + tmsize_t stride = PredictorState(tif)->stride; + uint64_t *wp = (uint64_t *)cp0; + tmsize_t wc = cc / 8; + + if ((cc % (8 * stride)) != 0) + { + TIFFErrorExtR(tif, "horAcc64", "%s", "cc%(8*stride))!=0"); + return 0; + } + + if (wc > stride) + { + wc -= stride; + do + { + REPEAT4(stride, wp[stride] += wp[0]; wp++) + wc -= stride; + } while (wc > 0); + } + return 1; +} + +/* + * Floating point predictor accumulation routine. + */ +static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + tmsize_t stride = PredictorState(tif)->stride; + uint32_t bps = tif->tif_dir.td_bitspersample / 8; + tmsize_t wc = cc / bps; + tmsize_t count = cc; + uint8_t *cp = cp0; + uint8_t *tmp; + + if (cc % (bps * stride) != 0) + { + TIFFErrorExtR(tif, "fpAcc", "%s", "cc%(bps*stride))!=0"); + return 0; + } + + tmp = (uint8_t *)_TIFFmallocExt(tif, cc); + if (!tmp) + return 0; + + if (stride == 1) + { + /* Optimization of general case */ +#define OP \ + do \ + { \ + cp[1] = (uint8_t)((cp[1] + cp[0]) & 0xff); \ + ++cp; \ + } while (0) + for (; count > 8; count -= 8) + { + OP; + OP; + OP; + OP; + OP; + OP; + OP; + OP; + } + for (; count > 1; count -= 1) + { + OP; + } +#undef OP + } + else + { + while (count > stride) + { + REPEAT4(stride, cp[stride] = (uint8_t)((cp[stride] + cp[0]) & 0xff); + cp++) + count -= stride; + } + } + + _TIFFmemcpy(tmp, cp0, cc); + cp = (uint8_t *)cp0; + count = 0; + +#if defined(__x86_64__) || defined(_M_X64) + if (bps == 4) + { + /* Optimization of general case */ + for (; count + 15 < wc; count += 16) + { + /* Interlace 4*16 byte values */ + + __m128i xmm0 = + _mm_loadu_si128((const __m128i *)(tmp + count + 3 * wc)); + __m128i xmm1 = + _mm_loadu_si128((const __m128i *)(tmp + count + 2 * wc)); + __m128i xmm2 = + _mm_loadu_si128((const __m128i *)(tmp + count + 1 * wc)); + __m128i xmm3 = + _mm_loadu_si128((const __m128i *)(tmp + count + 0 * wc)); + /* (xmm0_0, xmm1_0, xmm0_1, xmm1_1, xmm0_2, xmm1_2, ...) */ + __m128i tmp0 = _mm_unpacklo_epi8(xmm0, xmm1); + /* (xmm0_8, xmm1_8, xmm0_9, xmm1_9, xmm0_10, xmm1_10, ...) */ + __m128i tmp1 = _mm_unpackhi_epi8(xmm0, xmm1); + /* (xmm2_0, xmm3_0, xmm2_1, xmm3_1, xmm2_2, xmm3_2, ...) */ + __m128i tmp2 = _mm_unpacklo_epi8(xmm2, xmm3); + /* (xmm2_8, xmm3_8, xmm2_9, xmm3_9, xmm2_10, xmm3_10, ...) */ + __m128i tmp3 = _mm_unpackhi_epi8(xmm2, xmm3); + /* (xmm0_0, xmm1_0, xmm2_0, xmm3_0, xmm0_1, xmm1_1, xmm2_1, xmm3_1, + * ...) */ + __m128i tmp2_0 = _mm_unpacklo_epi16(tmp0, tmp2); + __m128i tmp2_1 = _mm_unpackhi_epi16(tmp0, tmp2); + __m128i tmp2_2 = _mm_unpacklo_epi16(tmp1, tmp3); + __m128i tmp2_3 = _mm_unpackhi_epi16(tmp1, tmp3); + _mm_storeu_si128((__m128i *)(cp + 4 * count + 0 * 16), tmp2_0); + _mm_storeu_si128((__m128i *)(cp + 4 * count + 1 * 16), tmp2_1); + _mm_storeu_si128((__m128i *)(cp + 4 * count + 2 * 16), tmp2_2); + _mm_storeu_si128((__m128i *)(cp + 4 * count + 3 * 16), tmp2_3); + } + } +#endif + + for (; count < wc; count++) + { + uint32_t byte; + for (byte = 0; byte < bps; byte++) + { +#if WORDS_BIGENDIAN + cp[bps * count + byte] = tmp[byte * wc + count]; +#else + cp[bps * count + byte] = tmp[(bps - byte - 1) * wc + count]; +#endif + } + } + _TIFFfreeExt(tif, tmp); + return 1; +} + +/* + * Decode a scanline and apply the predictor routine. + */ +static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0, + uint16_t s) +{ + TIFFPredictorState *sp = PredictorState(tif); + + assert(sp != NULL); + assert(sp->decoderow != NULL); + assert(sp->decodepfunc != NULL); + + if ((*sp->decoderow)(tif, op0, occ0, s)) + { + return (*sp->decodepfunc)(tif, op0, occ0); + } + else + return 0; +} + +/* + * Decode a tile/strip and apply the predictor routine. + * Note that horizontal differencing must be done on a + * row-by-row basis. The width of a "row" has already + * been calculated at pre-decode time according to the + * strip/tile dimensions. + */ +static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0, + uint16_t s) +{ + TIFFPredictorState *sp = PredictorState(tif); + + assert(sp != NULL); + assert(sp->decodetile != NULL); + + if ((*sp->decodetile)(tif, op0, occ0, s)) + { + tmsize_t rowsize = sp->rowsize; + assert(rowsize > 0); + if ((occ0 % rowsize) != 0) + { + TIFFErrorExtR(tif, "PredictorDecodeTile", "%s", + "occ0%rowsize != 0"); + return 0; + } + assert(sp->decodepfunc != NULL); + while (occ0 > 0) + { + if (!(*sp->decodepfunc)(tif, op0, rowsize)) + return 0; + occ0 -= rowsize; + op0 += rowsize; + } + return 1; + } + else + return 0; +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + TIFFPredictorState *sp = PredictorState(tif); + tmsize_t stride = sp->stride; + unsigned char *cp = (unsigned char *)cp0; + + if ((cc % stride) != 0) + { + TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%stride)!=0"); + return 0; + } + + if (cc > stride) + { + cc -= stride; + /* + * Pipeline the most common cases. + */ + if (stride == 3) + { + unsigned int r1, g1, b1; + unsigned int r2 = cp[0]; + unsigned int g2 = cp[1]; + unsigned int b2 = cp[2]; + do + { + r1 = cp[3]; + cp[3] = (unsigned char)((r1 - r2) & 0xff); + r2 = r1; + g1 = cp[4]; + cp[4] = (unsigned char)((g1 - g2) & 0xff); + g2 = g1; + b1 = cp[5]; + cp[5] = (unsigned char)((b1 - b2) & 0xff); + b2 = b1; + cp += 3; + } while ((cc -= 3) > 0); + } + else if (stride == 4) + { + unsigned int r1, g1, b1, a1; + unsigned int r2 = cp[0]; + unsigned int g2 = cp[1]; + unsigned int b2 = cp[2]; + unsigned int a2 = cp[3]; + do + { + r1 = cp[4]; + cp[4] = (unsigned char)((r1 - r2) & 0xff); + r2 = r1; + g1 = cp[5]; + cp[5] = (unsigned char)((g1 - g2) & 0xff); + g2 = g1; + b1 = cp[6]; + cp[6] = (unsigned char)((b1 - b2) & 0xff); + b2 = b1; + a1 = cp[7]; + cp[7] = (unsigned char)((a1 - a2) & 0xff); + a2 = a1; + cp += 4; + } while ((cc -= 4) > 0); + } + else + { + cp += cc - 1; + do + { + REPEAT4(stride, + cp[stride] = + (unsigned char)((cp[stride] - cp[0]) & 0xff); + cp--) + } while ((cc -= stride) > 0); + } + } + return 1; +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + TIFFPredictorState *sp = PredictorState(tif); + tmsize_t stride = sp->stride; + uint16_t *wp = (uint16_t *)cp0; + tmsize_t wc = cc / 2; + + if ((cc % (2 * stride)) != 0) + { + TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%(2*stride))!=0"); + return 0; + } + + if (wc > stride) + { + wc -= stride; + wp += wc - 1; + do + { + REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] - + (unsigned int)wp[0]) & + 0xffff); + wp--) + wc -= stride; + } while (wc > 0); + } + return 1; +} + +static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + uint16_t *wp = (uint16_t *)cp0; + tmsize_t wc = cc / 2; + + if (!horDiff16(tif, cp0, cc)) + return 0; + + TIFFSwabArrayOfShort(wp, wc); + return 1; +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + TIFFPredictorState *sp = PredictorState(tif); + tmsize_t stride = sp->stride; + uint32_t *wp = (uint32_t *)cp0; + tmsize_t wc = cc / 4; + + if ((cc % (4 * stride)) != 0) + { + TIFFErrorExtR(tif, "horDiff32", "%s", "(cc%(4*stride))!=0"); + return 0; + } + + if (wc > stride) + { + wc -= stride; + wp += wc - 1; + do + { + REPEAT4(stride, wp[stride] -= wp[0]; wp--) + wc -= stride; + } while (wc > 0); + } + return 1; +} + +static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + uint32_t *wp = (uint32_t *)cp0; + tmsize_t wc = cc / 4; + + if (!horDiff32(tif, cp0, cc)) + return 0; + + TIFFSwabArrayOfLong(wp, wc); + return 1; +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + TIFFPredictorState *sp = PredictorState(tif); + tmsize_t stride = sp->stride; + uint64_t *wp = (uint64_t *)cp0; + tmsize_t wc = cc / 8; + + if ((cc % (8 * stride)) != 0) + { + TIFFErrorExtR(tif, "horDiff64", "%s", "(cc%(8*stride))!=0"); + return 0; + } + + if (wc > stride) + { + wc -= stride; + wp += wc - 1; + do + { + REPEAT4(stride, wp[stride] -= wp[0]; wp--) + wc -= stride; + } while (wc > 0); + } + return 1; +} + +static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + uint64_t *wp = (uint64_t *)cp0; + tmsize_t wc = cc / 8; + + if (!horDiff64(tif, cp0, cc)) + return 0; + + TIFFSwabArrayOfLong8(wp, wc); + return 1; +} + +/* + * Floating point predictor differencing routine. + */ +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc) +{ + tmsize_t stride = PredictorState(tif)->stride; + uint32_t bps = tif->tif_dir.td_bitspersample / 8; + tmsize_t wc = cc / bps; + tmsize_t count; + uint8_t *cp = (uint8_t *)cp0; + uint8_t *tmp; + + if ((cc % (bps * stride)) != 0) + { + TIFFErrorExtR(tif, "fpDiff", "%s", "(cc%(bps*stride))!=0"); + return 0; + } + + tmp = (uint8_t *)_TIFFmallocExt(tif, cc); + if (!tmp) + return 0; + + _TIFFmemcpy(tmp, cp0, cc); + for (count = 0; count < wc; count++) + { + uint32_t byte; + for (byte = 0; byte < bps; byte++) + { +#if WORDS_BIGENDIAN + cp[byte * wc + count] = tmp[bps * count + byte]; +#else + cp[(bps - byte - 1) * wc + count] = tmp[bps * count + byte]; +#endif + } + } + _TIFFfreeExt(tif, tmp); + + cp = (uint8_t *)cp0; + cp += cc - stride - 1; + for (count = cc; count > stride; count -= stride) + REPEAT4(stride, + cp[stride] = (unsigned char)((cp[stride] - cp[0]) & 0xff); + cp--) + return 1; +} + +static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "PredictorEncodeRow"; + TIFFPredictorState *sp = PredictorState(tif); + uint8_t *working_copy; + int result_code; + + assert(sp != NULL); + assert(sp->encodepfunc != NULL); + assert(sp->encoderow != NULL); + + /* + * Do predictor manipulation in a working buffer to avoid altering + * the callers buffer, like for PredictorEncodeTile(). + * https://gitlab.com/libtiff/libtiff/-/issues/5 + */ + working_copy = (uint8_t *)_TIFFmallocExt(tif, cc); + if (working_copy == NULL) + { + TIFFErrorExtR(tif, module, + "Out of memory allocating %" PRId64 " byte temp buffer.", + (int64_t)cc); + return 0; + } + memcpy(working_copy, bp, cc); + + if (!(*sp->encodepfunc)(tif, working_copy, cc)) + { + _TIFFfreeExt(tif, working_copy); + return 0; + } + result_code = (*sp->encoderow)(tif, working_copy, cc, s); + _TIFFfreeExt(tif, working_copy); + return result_code; +} + +static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0, + uint16_t s) +{ + static const char module[] = "PredictorEncodeTile"; + TIFFPredictorState *sp = PredictorState(tif); + uint8_t *working_copy; + tmsize_t cc = cc0, rowsize; + unsigned char *bp; + int result_code; + + assert(sp != NULL); + assert(sp->encodepfunc != NULL); + assert(sp->encodetile != NULL); + + /* + * Do predictor manipulation in a working buffer to avoid altering + * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965 + */ + working_copy = (uint8_t *)_TIFFmallocExt(tif, cc0); + if (working_copy == NULL) + { + TIFFErrorExtR(tif, module, + "Out of memory allocating %" PRId64 " byte temp buffer.", + (int64_t)cc0); + return 0; + } + memcpy(working_copy, bp0, cc0); + bp = working_copy; + + rowsize = sp->rowsize; + assert(rowsize > 0); + if ((cc0 % rowsize) != 0) + { + TIFFErrorExtR(tif, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0"); + _TIFFfreeExt(tif, working_copy); + return 0; + } + while (cc > 0) + { + (*sp->encodepfunc)(tif, bp, rowsize); + cc -= rowsize; + bp += rowsize; + } + result_code = (*sp->encodetile)(tif, working_copy, cc0, s); + + _TIFFfreeExt(tif, working_copy); + + return result_code; +} + +#define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */ + +static const TIFFField predictFields[] = { + {TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, + FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL}, +}; + +static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + TIFFPredictorState *sp = PredictorState(tif); + + assert(sp != NULL); + assert(sp->vsetparent != NULL); + + switch (tag) + { + case TIFFTAG_PREDICTOR: + sp->predictor = (uint16_t)va_arg(ap, uint16_vap); + TIFFSetFieldBit(tif, FIELD_PREDICTOR); + break; + default: + return (*sp->vsetparent)(tif, tag, ap); + } + tif->tif_flags |= TIFF_DIRTYDIRECT; + return 1; +} + +static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + TIFFPredictorState *sp = PredictorState(tif); + + assert(sp != NULL); + assert(sp->vgetparent != NULL); + + switch (tag) + { + case TIFFTAG_PREDICTOR: + *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return 1; +} + +static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags) +{ + TIFFPredictorState *sp = PredictorState(tif); + + (void)flags; + if (TIFFFieldSet(tif, FIELD_PREDICTOR)) + { + fprintf(fd, " Predictor: "); + switch (sp->predictor) + { + case 1: + fprintf(fd, "none "); + break; + case 2: + fprintf(fd, "horizontal differencing "); + break; + case 3: + fprintf(fd, "floating point predictor "); + break; + } + fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor); + } + if (sp->printdir) + (*sp->printdir)(tif, fd, flags); +} + +int TIFFPredictorInit(TIFF *tif) +{ + TIFFPredictorState *sp = PredictorState(tif); + + assert(sp != 0); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, predictFields, TIFFArrayCount(predictFields))) + { + TIFFErrorExtR(tif, "TIFFPredictorInit", + "Merging Predictor codec-specific tags failed"); + return 0; + } + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = + PredictorVGetField; /* hook for predictor tag */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = + PredictorVSetField; /* hook for predictor tag */ + sp->printdir = tif->tif_tagmethods.printdir; + tif->tif_tagmethods.printdir = + PredictorPrintDir; /* hook for predictor tag */ + + sp->setupdecode = tif->tif_setupdecode; + tif->tif_setupdecode = PredictorSetupDecode; + sp->setupencode = tif->tif_setupencode; + tif->tif_setupencode = PredictorSetupEncode; + + sp->predictor = 1; /* default value */ + sp->encodepfunc = NULL; /* no predictor routine */ + sp->decodepfunc = NULL; /* no predictor routine */ + return 1; +} + +int TIFFPredictorCleanup(TIFF *tif) +{ + TIFFPredictorState *sp = PredictorState(tif); + + assert(sp != 0); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + tif->tif_tagmethods.printdir = sp->printdir; + tif->tif_setupdecode = sp->setupdecode; + tif->tif_setupencode = sp->setupencode; + + return 1; +} diff --git a/cpp/3rd_party/libtiff/tif_predict.h b/cpp/3rd_party/libtiff/tif_predict.h new file mode 100644 index 0000000000..de77328352 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_predict.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 1995-1997 Sam Leffler + * Copyright (c) 1995-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _TIFFPREDICT_ +#define _TIFFPREDICT_ + +#include "tiffio.h" +#include "tiffiop.h" + +/* + * ``Library-private'' Support for the Predictor Tag + */ + +typedef int (*TIFFEncodeDecodeMethod)(TIFF *tif, uint8_t *buf, tmsize_t size); + +/* + * Codecs that want to support the Predictor tag must place + * this structure first in their private state block so that + * the predictor code can cast tif_data to find its state. + */ +typedef struct +{ + int predictor; /* predictor tag value */ + tmsize_t stride; /* sample stride over data */ + tmsize_t rowsize; /* tile/strip row size */ + + TIFFCodeMethod encoderow; /* parent codec encode/decode row */ + TIFFCodeMethod encodestrip; /* parent codec encode/decode strip */ + TIFFCodeMethod encodetile; /* parent codec encode/decode tile */ + TIFFEncodeDecodeMethod encodepfunc; /* horizontal differencer */ + + TIFFCodeMethod decoderow; /* parent codec encode/decode row */ + TIFFCodeMethod decodestrip; /* parent codec encode/decode strip */ + TIFFCodeMethod decodetile; /* parent codec encode/decode tile */ + TIFFEncodeDecodeMethod decodepfunc; /* horizontal accumulator */ + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ + TIFFPrintMethod printdir; /* super-class method */ + TIFFBoolMethod setupdecode; /* super-class method */ + TIFFBoolMethod setupencode; /* super-class method */ +} TIFFPredictorState; + +#if defined(__cplusplus) +extern "C" +{ +#endif + extern int TIFFPredictorInit(TIFF *); + extern int TIFFPredictorCleanup(TIFF *); +#if defined(__cplusplus) +} +#endif +#endif /* _TIFFPREDICT_ */ diff --git a/cpp/3rd_party/libtiff/tif_print.c b/cpp/3rd_party/libtiff/tif_print.c new file mode 100644 index 0000000000..addc03ab07 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_print.c @@ -0,0 +1,756 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Directory Printing Support + */ +#include "tiffiop.h" +#include + +#include + +static void _TIFFprintAsciiBounded(FILE *fd, const char *cp, size_t max_chars); + +static const char *const photoNames[] = { + "min-is-white", /* PHOTOMETRIC_MINISWHITE */ + "min-is-black", /* PHOTOMETRIC_MINISBLACK */ + "RGB color", /* PHOTOMETRIC_RGB */ + "palette color (RGB from colormap)", /* PHOTOMETRIC_PALETTE */ + "transparency mask", /* PHOTOMETRIC_MASK */ + "separated", /* PHOTOMETRIC_SEPARATED */ + "YCbCr", /* PHOTOMETRIC_YCBCR */ + "7 (0x7)", + "CIE L*a*b*", /* PHOTOMETRIC_CIELAB */ + "ICC L*a*b*", /* PHOTOMETRIC_ICCLAB */ + "ITU L*a*b*" /* PHOTOMETRIC_ITULAB */ +}; +#define NPHOTONAMES (sizeof(photoNames) / sizeof(photoNames[0])) + +static const char *const orientNames[] = { + "0 (0x0)", + "row 0 top, col 0 lhs", /* ORIENTATION_TOPLEFT */ + "row 0 top, col 0 rhs", /* ORIENTATION_TOPRIGHT */ + "row 0 bottom, col 0 rhs", /* ORIENTATION_BOTRIGHT */ + "row 0 bottom, col 0 lhs", /* ORIENTATION_BOTLEFT */ + "row 0 lhs, col 0 top", /* ORIENTATION_LEFTTOP */ + "row 0 rhs, col 0 top", /* ORIENTATION_RIGHTTOP */ + "row 0 rhs, col 0 bottom", /* ORIENTATION_RIGHTBOT */ + "row 0 lhs, col 0 bottom", /* ORIENTATION_LEFTBOT */ +}; +#define NORIENTNAMES (sizeof(orientNames) / sizeof(orientNames[0])) + +static const struct tagname +{ + uint16_t tag; + const char *name; +} tagnames[] = { + {TIFFTAG_GDAL_METADATA, "GDAL Metadata"}, + {TIFFTAG_GDAL_NODATA, "GDAL NoDataValue"}, +}; +#define NTAGS (sizeof(tagnames) / sizeof(tagnames[0])) + +static void _TIFFPrintField(FILE *fd, const TIFFField *fip, + uint32_t value_count, void *raw_data) +{ + uint32_t j; + + /* Print a user-friendly name for tags of relatively common use, but */ + /* which aren't registered by libtiff itself. */ + const char *field_name = fip->field_name; + if (TIFFFieldIsAnonymous(fip)) + { + for (size_t i = 0; i < NTAGS; ++i) + { + if (fip->field_tag == tagnames[i].tag) + { + field_name = tagnames[i].name; + break; + } + } + } + fprintf(fd, " %s: ", field_name); + + for (j = 0; j < value_count; j++) + { + if (fip->field_type == TIFF_BYTE) + fprintf(fd, "%" PRIu8, ((uint8_t *)raw_data)[j]); + else if (fip->field_type == TIFF_UNDEFINED) + fprintf(fd, "0x%" PRIx8, ((uint8_t *)raw_data)[j]); + else if (fip->field_type == TIFF_SBYTE) + fprintf(fd, "%" PRId8, ((int8_t *)raw_data)[j]); + else if (fip->field_type == TIFF_SHORT) + fprintf(fd, "%" PRIu16, ((uint16_t *)raw_data)[j]); + else if (fip->field_type == TIFF_SSHORT) + fprintf(fd, "%" PRId16, ((int16_t *)raw_data)[j]); + else if (fip->field_type == TIFF_LONG) + fprintf(fd, "%" PRIu32, ((uint32_t *)raw_data)[j]); + else if (fip->field_type == TIFF_SLONG) + fprintf(fd, "%" PRId32, ((int32_t *)raw_data)[j]); + else if (fip->field_type == TIFF_IFD) + fprintf(fd, "0x%" PRIx32, ((uint32_t *)raw_data)[j]); + else if (fip->field_type == TIFF_RATIONAL || + fip->field_type == TIFF_SRATIONAL) + { + int tv_size = TIFFFieldSetGetSize(fip); + if (tv_size == 8) + fprintf(fd, "%lf", ((double *)raw_data)[j]); + else + fprintf(fd, "%f", ((float *)raw_data)[j]); + } + else if (fip->field_type == TIFF_FLOAT) + fprintf(fd, "%f", ((float *)raw_data)[j]); + else if (fip->field_type == TIFF_LONG8) + fprintf(fd, "%" PRIu64, ((uint64_t *)raw_data)[j]); + else if (fip->field_type == TIFF_SLONG8) + fprintf(fd, "%" PRId64, ((int64_t *)raw_data)[j]); + else if (fip->field_type == TIFF_IFD8) + fprintf(fd, "0x%" PRIx64, ((uint64_t *)raw_data)[j]); + else if (fip->field_type == TIFF_DOUBLE) + fprintf(fd, "%lf", ((double *)raw_data)[j]); + else if (fip->field_type == TIFF_ASCII) + { + fprintf(fd, "%s", (char *)raw_data); + break; + } + else + { + fprintf(fd, ""); + break; + } + + if (j < value_count - 1) + fprintf(fd, ","); + } + + fprintf(fd, "\n"); +} + +static int _TIFFPrettyPrintField(TIFF *tif, const TIFFField *fip, FILE *fd, + uint32_t tag, uint32_t value_count, + void *raw_data) +{ + (void)tif; + + /* do not try to pretty print auto-defined fields */ + if (TIFFFieldIsAnonymous(fip)) + { + return 0; + } + + switch (tag) + { + case TIFFTAG_INKSET: + if (value_count == 2 && fip->field_type == TIFF_SHORT) + { + fprintf(fd, " Ink Set: "); + switch (*((uint16_t *)raw_data)) + { + case INKSET_CMYK: + fprintf(fd, "CMYK\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", + *((uint16_t *)raw_data), + *((uint16_t *)raw_data)); + break; + } + return 1; + } + return 0; + + case TIFFTAG_DOTRANGE: + if (value_count == 2 && fip->field_type == TIFF_SHORT) + { + fprintf(fd, " Dot Range: %" PRIu16 "-%" PRIu16 "\n", + ((uint16_t *)raw_data)[0], ((uint16_t *)raw_data)[1]); + return 1; + } + return 0; + + case TIFFTAG_WHITEPOINT: + if (value_count == 2 && fip->field_type == TIFF_RATIONAL) + { + fprintf(fd, " White Point: %g-%g\n", ((float *)raw_data)[0], + ((float *)raw_data)[1]); + return 1; + } + return 0; + + case TIFFTAG_XMLPACKET: + { + uint32_t i; + + fprintf(fd, " XMLPacket (XMP Metadata):\n"); + for (i = 0; i < value_count; i++) + fputc(((char *)raw_data)[i], fd); + fprintf(fd, "\n"); + return 1; + } + case TIFFTAG_RICHTIFFIPTC: + fprintf(fd, " RichTIFFIPTC Data: , %" PRIu32 " bytes\n", + value_count); + return 1; + + case TIFFTAG_PHOTOSHOP: + fprintf(fd, " Photoshop Data: , %" PRIu32 " bytes\n", + value_count); + return 1; + + case TIFFTAG_ICCPROFILE: + fprintf(fd, " ICC Profile: , %" PRIu32 " bytes\n", + value_count); + return 1; + + case TIFFTAG_STONITS: + if (value_count == 1 && fip->field_type == TIFF_DOUBLE) + { + fprintf(fd, " Sample to Nits conversion factor: %.4e\n", + *((double *)raw_data)); + return 1; + } + return 0; + } + + return 0; +} + +/* + * Print the contents of the current directory + * to the specified stdio file stream. + */ +void TIFFPrintDirectory(TIFF *tif, FILE *fd, long flags) +{ + TIFFDirectory *td = &tif->tif_dir; + char *sep; + long l, n; + + fprintf(fd, "TIFF Directory at offset 0x%" PRIx64 " (%" PRIu64 ")\n", + tif->tif_diroff, tif->tif_diroff); + if (TIFFFieldSet(tif, FIELD_SUBFILETYPE)) + { + fprintf(fd, " Subfile Type:"); + sep = " "; + if (td->td_subfiletype & FILETYPE_REDUCEDIMAGE) + { + fprintf(fd, "%sreduced-resolution image", sep); + sep = "/"; + } + if (td->td_subfiletype & FILETYPE_PAGE) + { + fprintf(fd, "%smulti-page document", sep); + sep = "/"; + } + if (td->td_subfiletype & FILETYPE_MASK) + fprintf(fd, "%stransparency mask", sep); + fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n", td->td_subfiletype, + td->td_subfiletype); + } + if (TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) + { + fprintf(fd, " Image Width: %" PRIu32 " Image Length: %" PRIu32, + td->td_imagewidth, td->td_imagelength); + if (TIFFFieldSet(tif, FIELD_IMAGEDEPTH)) + fprintf(fd, " Image Depth: %" PRIu32, td->td_imagedepth); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) + { + fprintf(fd, " Tile Width: %" PRIu32 " Tile Length: %" PRIu32, + td->td_tilewidth, td->td_tilelength); + if (TIFFFieldSet(tif, FIELD_TILEDEPTH)) + fprintf(fd, " Tile Depth: %" PRIu32, td->td_tiledepth); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_RESOLUTION)) + { + fprintf(fd, " Resolution: %g, %g", td->td_xresolution, + td->td_yresolution); + if (TIFFFieldSet(tif, FIELD_RESOLUTIONUNIT)) + { + switch (td->td_resolutionunit) + { + case RESUNIT_NONE: + fprintf(fd, " (unitless)"); + break; + case RESUNIT_INCH: + fprintf(fd, " pixels/inch"); + break; + case RESUNIT_CENTIMETER: + fprintf(fd, " pixels/cm"); + break; + default: + fprintf(fd, " (unit %" PRIu16 " = 0x%" PRIx16 ")", + td->td_resolutionunit, td->td_resolutionunit); + break; + } + } + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_POSITION)) + fprintf(fd, " Position: %g, %g\n", td->td_xposition, td->td_yposition); + if (TIFFFieldSet(tif, FIELD_BITSPERSAMPLE)) + fprintf(fd, " Bits/Sample: %" PRIu16 "\n", td->td_bitspersample); + if (TIFFFieldSet(tif, FIELD_SAMPLEFORMAT)) + { + fprintf(fd, " Sample Format: "); + switch (td->td_sampleformat) + { + case SAMPLEFORMAT_VOID: + fprintf(fd, "void\n"); + break; + case SAMPLEFORMAT_INT: + fprintf(fd, "signed integer\n"); + break; + case SAMPLEFORMAT_UINT: + fprintf(fd, "unsigned integer\n"); + break; + case SAMPLEFORMAT_IEEEFP: + fprintf(fd, "IEEE floating point\n"); + break; + case SAMPLEFORMAT_COMPLEXINT: + fprintf(fd, "complex signed integer\n"); + break; + case SAMPLEFORMAT_COMPLEXIEEEFP: + fprintf(fd, "complex IEEE floating point\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", + td->td_sampleformat, td->td_sampleformat); + break; + } + } + if (TIFFFieldSet(tif, FIELD_COMPRESSION)) + { + const TIFFCodec *c = TIFFFindCODEC(td->td_compression); + fprintf(fd, " Compression Scheme: "); + if (c) + fprintf(fd, "%s\n", c->name); + else + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", td->td_compression, + td->td_compression); + } + if (TIFFFieldSet(tif, FIELD_PHOTOMETRIC)) + { + fprintf(fd, " Photometric Interpretation: "); + if (td->td_photometric < NPHOTONAMES) + fprintf(fd, "%s\n", photoNames[td->td_photometric]); + else + { + switch (td->td_photometric) + { + case PHOTOMETRIC_LOGL: + fprintf(fd, "CIE Log2(L)\n"); + break; + case PHOTOMETRIC_LOGLUV: + fprintf(fd, "CIE Log2(L) (u',v')\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", + td->td_photometric, td->td_photometric); + break; + } + } + } + if (TIFFFieldSet(tif, FIELD_EXTRASAMPLES) && td->td_extrasamples) + { + uint16_t i; + fprintf(fd, " Extra Samples: %" PRIu16 "<", td->td_extrasamples); + sep = ""; + for (i = 0; i < td->td_extrasamples; i++) + { + switch (td->td_sampleinfo[i]) + { + case EXTRASAMPLE_UNSPECIFIED: + fprintf(fd, "%sunspecified", sep); + break; + case EXTRASAMPLE_ASSOCALPHA: + fprintf(fd, "%sassoc-alpha", sep); + break; + case EXTRASAMPLE_UNASSALPHA: + fprintf(fd, "%sunassoc-alpha", sep); + break; + default: + fprintf(fd, "%s%" PRIu16 " (0x%" PRIx16 ")", sep, + td->td_sampleinfo[i], td->td_sampleinfo[i]); + break; + } + sep = ", "; + } + fprintf(fd, ">\n"); + } + if (TIFFFieldSet(tif, FIELD_INKNAMES)) + { + char *cp; + uint16_t i; + fprintf(fd, " Ink Names: "); + i = td->td_samplesperpixel; + sep = ""; + for (cp = td->td_inknames; + i > 0 && cp < td->td_inknames + td->td_inknameslen; + cp = strchr(cp, '\0') + 1, i--) + { + size_t max_chars = td->td_inknameslen - (cp - td->td_inknames); + fputs(sep, fd); + _TIFFprintAsciiBounded(fd, cp, max_chars); + sep = ", "; + } + fputs("\n", fd); + } + if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS)) + { + fprintf(fd, " NumberOfInks: %d\n", td->td_numberofinks); + } + if (TIFFFieldSet(tif, FIELD_THRESHHOLDING)) + { + fprintf(fd, " Thresholding: "); + switch (td->td_threshholding) + { + case THRESHHOLD_BILEVEL: + fprintf(fd, "bilevel art scan\n"); + break; + case THRESHHOLD_HALFTONE: + fprintf(fd, "halftone or dithered scan\n"); + break; + case THRESHHOLD_ERRORDIFFUSE: + fprintf(fd, "error diffused\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", + td->td_threshholding, td->td_threshholding); + break; + } + } + if (TIFFFieldSet(tif, FIELD_FILLORDER)) + { + fprintf(fd, " FillOrder: "); + switch (td->td_fillorder) + { + case FILLORDER_MSB2LSB: + fprintf(fd, "msb-to-lsb\n"); + break; + case FILLORDER_LSB2MSB: + fprintf(fd, "lsb-to-msb\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", td->td_fillorder, + td->td_fillorder); + break; + } + } + if (TIFFFieldSet(tif, FIELD_YCBCRSUBSAMPLING)) + { + fprintf(fd, " YCbCr Subsampling: %" PRIu16 ", %" PRIu16 "\n", + td->td_ycbcrsubsampling[0], td->td_ycbcrsubsampling[1]); + } + if (TIFFFieldSet(tif, FIELD_YCBCRPOSITIONING)) + { + fprintf(fd, " YCbCr Positioning: "); + switch (td->td_ycbcrpositioning) + { + case YCBCRPOSITION_CENTERED: + fprintf(fd, "centered\n"); + break; + case YCBCRPOSITION_COSITED: + fprintf(fd, "cosited\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", + td->td_ycbcrpositioning, td->td_ycbcrpositioning); + break; + } + } + if (TIFFFieldSet(tif, FIELD_HALFTONEHINTS)) + fprintf(fd, " Halftone Hints: light %" PRIu16 " dark %" PRIu16 "\n", + td->td_halftonehints[0], td->td_halftonehints[1]); + if (TIFFFieldSet(tif, FIELD_ORIENTATION)) + { + fprintf(fd, " Orientation: "); + if (td->td_orientation < NORIENTNAMES) + fprintf(fd, "%s\n", orientNames[td->td_orientation]); + else + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", td->td_orientation, + td->td_orientation); + } + if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) + fprintf(fd, " Samples/Pixel: %" PRIx16 "\n", td->td_samplesperpixel); + if (TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) + { + fprintf(fd, " Rows/Strip: "); + if (td->td_rowsperstrip == (uint32_t)-1) + fprintf(fd, "(infinite)\n"); + else + fprintf(fd, "%" PRIu32 "\n", td->td_rowsperstrip); + } + if (TIFFFieldSet(tif, FIELD_MINSAMPLEVALUE)) + fprintf(fd, " Min Sample Value: %" PRIu16 "\n", td->td_minsamplevalue); + if (TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) + fprintf(fd, " Max Sample Value: %" PRIu16 "\n", td->td_maxsamplevalue); + if (TIFFFieldSet(tif, FIELD_SMINSAMPLEVALUE)) + { + int i; + int count = + (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1; + fprintf(fd, " SMin Sample Value:"); + for (i = 0; i < count; ++i) + fprintf(fd, " %g", td->td_sminsamplevalue[i]); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_SMAXSAMPLEVALUE)) + { + int i; + int count = + (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1; + fprintf(fd, " SMax Sample Value:"); + for (i = 0; i < count; ++i) + fprintf(fd, " %g", td->td_smaxsamplevalue[i]); + fprintf(fd, "\n"); + } + if (TIFFFieldSet(tif, FIELD_PLANARCONFIG)) + { + fprintf(fd, " Planar Configuration: "); + switch (td->td_planarconfig) + { + case PLANARCONFIG_CONTIG: + fprintf(fd, "single image plane\n"); + break; + case PLANARCONFIG_SEPARATE: + fprintf(fd, "separate image planes\n"); + break; + default: + fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", + td->td_planarconfig, td->td_planarconfig); + break; + } + } + if (TIFFFieldSet(tif, FIELD_PAGENUMBER)) + fprintf(fd, " Page Number: %" PRIu16 "-%" PRIu16 "\n", + td->td_pagenumber[0], td->td_pagenumber[1]); + if (TIFFFieldSet(tif, FIELD_COLORMAP)) + { + fprintf(fd, " Color Map: "); + if (flags & TIFFPRINT_COLORMAP) + { + fprintf(fd, "\n"); + n = 1L << td->td_bitspersample; + for (l = 0; l < n; l++) + fprintf(fd, " %5ld: %5" PRIu16 " %5" PRIu16 " %5" PRIu16 "\n", + l, td->td_colormap[0][l], td->td_colormap[1][l], + td->td_colormap[2][l]); + } + else + fprintf(fd, "(present)\n"); + } + if (TIFFFieldSet(tif, FIELD_REFBLACKWHITE)) + { + int i; + fprintf(fd, " Reference Black/White:\n"); + for (i = 0; i < 3; i++) + fprintf(fd, " %2d: %5g %5g\n", i, + td->td_refblackwhite[2 * i + 0], + td->td_refblackwhite[2 * i + 1]); + } + if (TIFFFieldSet(tif, FIELD_TRANSFERFUNCTION)) + { + fprintf(fd, " Transfer Function: "); + if (flags & TIFFPRINT_CURVES) + { + fprintf(fd, "\n"); + n = 1L << td->td_bitspersample; + for (l = 0; l < n; l++) + { + uint16_t i; + fprintf(fd, " %2ld: %5" PRIu16, l, + td->td_transferfunction[0][l]); + for (i = 1; + i < td->td_samplesperpixel - td->td_extrasamples && i < 3; + i++) + fprintf(fd, " %5" PRIu16, td->td_transferfunction[i][l]); + fputc('\n', fd); + } + } + else + fprintf(fd, "(present)\n"); + } + if (TIFFFieldSet(tif, FIELD_SUBIFD) && (td->td_subifd)) + { + uint16_t i; + fprintf(fd, " SubIFD Offsets:"); + for (i = 0; i < td->td_nsubifd; i++) + fprintf(fd, " %5" PRIu64, td->td_subifd[i]); + fputc('\n', fd); + } + + /* + ** Custom tag support. + */ + { + int i; + short count; + + count = (short)TIFFGetTagListCount(tif); + for (i = 0; i < count; i++) + { + uint32_t tag = TIFFGetTagListEntry(tif, i); + const TIFFField *fip; + uint32_t value_count; + int mem_alloc = 0; + void *raw_data = NULL; + uint16_t dotrange[2]; /* must be kept in that scope and not moved in + the below TIFFTAG_DOTRANGE specific case */ + + fip = TIFFFieldWithTag(tif, tag); + if (fip == NULL) + continue; + + if (fip->field_passcount) + { + if (fip->field_readcount == TIFF_VARIABLE2) + { + if (TIFFGetField(tif, tag, &value_count, &raw_data) != 1) + continue; + } + else if (fip->field_readcount == TIFF_VARIABLE) + { + uint16_t small_value_count; + if (TIFFGetField(tif, tag, &small_value_count, &raw_data) != + 1) + continue; + value_count = small_value_count; + } + else + { + assert(fip->field_readcount == TIFF_VARIABLE || + fip->field_readcount == TIFF_VARIABLE2); + continue; + } + } + else + { + if (fip->field_readcount == TIFF_VARIABLE || + fip->field_readcount == TIFF_VARIABLE2) + value_count = 1; + else if (fip->field_readcount == TIFF_SPP) + value_count = td->td_samplesperpixel; + else + value_count = fip->field_readcount; + if (fip->field_tag == TIFFTAG_DOTRANGE && + strcmp(fip->field_name, "DotRange") == 0) + { + /* TODO: This is an evil exception and should not have been + handled this way ... likely best if we move it into + the directory structure with an explicit field in + libtiff 4.1 and assign it a FIELD_ value */ + raw_data = dotrange; + TIFFGetField(tif, tag, dotrange + 0, dotrange + 1); + } + else if (fip->field_type == TIFF_ASCII || + fip->field_readcount == TIFF_VARIABLE || + fip->field_readcount == TIFF_VARIABLE2 || + fip->field_readcount == TIFF_SPP || value_count > 1) + { + if (TIFFGetField(tif, tag, &raw_data) != 1) + continue; + } + else + { + /*--: Rational2Double: For Rationals evaluate + * "set_get_field_type" to determine internal storage size. + */ + int tv_size = TIFFFieldSetGetSize(fip); + raw_data = _TIFFmallocExt(tif, tv_size * value_count); + mem_alloc = 1; + if (TIFFGetField(tif, tag, raw_data) != 1) + { + _TIFFfreeExt(tif, raw_data); + continue; + } + } + } + + /* + * Catch the tags which needs to be specially handled + * and pretty print them. If tag not handled in + * _TIFFPrettyPrintField() fall down and print it as + * any other tag. + */ + if (raw_data != NULL && + !_TIFFPrettyPrintField(tif, fip, fd, tag, value_count, + raw_data)) + _TIFFPrintField(fd, fip, value_count, raw_data); + + if (mem_alloc) + _TIFFfreeExt(tif, raw_data); + } + } + + if (tif->tif_tagmethods.printdir) + (*tif->tif_tagmethods.printdir)(tif, fd, flags); + + if ((flags & TIFFPRINT_STRIPS) && TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) + { + uint32_t s; + + fprintf(fd, " %" PRIu32 " %s:\n", td->td_nstrips, + isTiled(tif) ? "Tiles" : "Strips"); + for (s = 0; s < td->td_nstrips; s++) + fprintf(fd, " %3" PRIu32 ": [%8" PRIu64 ", %8" PRIu64 "]\n", s, + TIFFGetStrileOffset(tif, s), + TIFFGetStrileByteCount(tif, s)); + } +} + +void _TIFFprintAscii(FILE *fd, const char *cp) +{ + _TIFFprintAsciiBounded(fd, cp, strlen(cp)); +} + +static void _TIFFprintAsciiBounded(FILE *fd, const char *cp, size_t max_chars) +{ + for (; max_chars > 0 && *cp != '\0'; cp++, max_chars--) + { + const char *tp; + + if (isprint((int)*cp)) + { + fputc(*cp, fd); + continue; + } + for (tp = "\tt\bb\rr\nn\vv"; *tp; tp++) + if (*tp++ == *cp) + break; + if (*tp) + fprintf(fd, "\\%c", *tp); + else + fprintf(fd, "\\%03o", *cp & 0xff); + } +} + +void _TIFFprintAsciiTag(FILE *fd, const char *name, const char *value) +{ + fprintf(fd, " %s: \"", name); + _TIFFprintAscii(fd, value); + fprintf(fd, "\"\n"); +} diff --git a/cpp/3rd_party/libtiff/tif_read.c b/cpp/3rd_party/libtiff/tif_read.c new file mode 100644 index 0000000000..a2bb304623 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_read.c @@ -0,0 +1,1681 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * Scanline-oriented Read Support + */ +#include "tiffiop.h" +#include + +int TIFFFillStrip(TIFF *tif, uint32_t strip); +int TIFFFillTile(TIFF *tif, uint32_t tile); +static int TIFFStartStrip(TIFF *tif, uint32_t strip); +static int TIFFStartTile(TIFF *tif, uint32_t tile); +static int TIFFCheckRead(TIFF *, int); +static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf, + tmsize_t size, const char *module); +static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf, + tmsize_t size, const char *module); + +#define NOSTRIP ((uint32_t)(-1)) /* undefined state */ +#define NOTILE ((uint32_t)(-1)) /* undefined state */ + +#define INITIAL_THRESHOLD (1024 * 1024) +#define THRESHOLD_MULTIPLIER 10 +#define MAX_THRESHOLD \ + (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \ + INITIAL_THRESHOLD) + +#define TIFF_INT64_MAX ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF) + +/* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset' + * Returns 1 in case of success, 0 otherwise. */ +static int TIFFReadAndRealloc(TIFF *tif, tmsize_t size, tmsize_t rawdata_offset, + int is_strip, uint32_t strip_or_tile, + const char *module) +{ +#if SIZEOF_SIZE_T == 8 + tmsize_t threshold = INITIAL_THRESHOLD; +#endif + tmsize_t already_read = 0; + +#if SIZEOF_SIZE_T != 8 + /* On 32 bit processes, if the request is large enough, check against */ + /* file size */ + if (size > 1000 * 1000 * 1000) + { + uint64_t filesize = TIFFGetFileSize(tif); + if ((uint64_t)size >= filesize) + { + TIFFErrorExtR(tif, module, + "Chunk size requested is larger than file size."); + return 0; + } + } +#endif + + /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */ + /* so as to avoid allocating too much memory in case the file is too */ + /* short. We could ask for the file size, but this might be */ + /* expensive with some I/O layers (think of reading a gzipped file) */ + /* Restrict to 64 bit processes, so as to avoid reallocs() */ + /* on 32 bit processes where virtual memory is scarce. */ + while (already_read < size) + { + tmsize_t bytes_read; + tmsize_t to_read = size - already_read; +#if SIZEOF_SIZE_T == 8 + if (to_read >= threshold && threshold < MAX_THRESHOLD && + already_read + to_read + rawdata_offset > tif->tif_rawdatasize) + { + to_read = threshold; + threshold *= THRESHOLD_MULTIPLIER; + } +#endif + if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize) + { + uint8_t *new_rawdata; + assert((tif->tif_flags & TIFF_MYBUFFER) != 0); + tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64( + (uint64_t)already_read + to_read + rawdata_offset, 1024); + if (tif->tif_rawdatasize == 0) + { + TIFFErrorExtR(tif, module, "Invalid buffer size"); + return 0; + } + new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata, + tif->tif_rawdatasize); + if (new_rawdata == 0) + { + TIFFErrorExtR(tif, module, + "No space for data buffer at scanline %" PRIu32, + tif->tif_row); + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_rawdata = 0; + tif->tif_rawdatasize = 0; + return 0; + } + tif->tif_rawdata = new_rawdata; + } + if (tif->tif_rawdata == NULL) + { + /* should not happen in practice but helps CoverityScan */ + return 0; + } + + bytes_read = TIFFReadFile( + tif, tif->tif_rawdata + rawdata_offset + already_read, to_read); + already_read += bytes_read; + if (bytes_read != to_read) + { + memset(tif->tif_rawdata + rawdata_offset + already_read, 0, + tif->tif_rawdatasize - rawdata_offset - already_read); + if (is_strip) + { + TIFFErrorExtR(tif, module, + "Read error at scanline %" PRIu32 + "; got %" TIFF_SSIZE_FORMAT " bytes, " + "expected %" TIFF_SSIZE_FORMAT, + tif->tif_row, already_read, size); + } + else + { + TIFFErrorExtR(tif, module, + "Read error at row %" PRIu32 ", col %" PRIu32 + ", tile %" PRIu32 "; " + "got %" TIFF_SSIZE_FORMAT + " bytes, expected %" TIFF_SSIZE_FORMAT "", + tif->tif_row, tif->tif_col, strip_or_tile, + already_read, size); + } + return 0; + } + } + return 1; +} + +static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead, + int restart) +{ + static const char module[] = "TIFFFillStripPartial"; + register TIFFDirectory *td = &tif->tif_dir; + tmsize_t unused_data; + uint64_t read_offset; + tmsize_t to_read; + tmsize_t read_ahead_mod; + /* tmsize_t bytecountm; */ + + /* + * Expand raw data buffer, if needed, to hold data + * strip coming from file (perhaps should set upper + * bound on the size of a buffer we'll use?). + */ + + /* bytecountm=(tmsize_t) TIFFGetStrileByteCount(tif, strip); */ + + /* Not completely sure where the * 2 comes from, but probably for */ + /* an exponentional growth strategy of tif_rawdatasize */ + if (read_ahead < TIFF_TMSIZE_T_MAX / 2) + read_ahead_mod = read_ahead * 2; + else + read_ahead_mod = read_ahead; + if (read_ahead_mod > tif->tif_rawdatasize) + { + assert(restart); + + tif->tif_curstrip = NOSTRIP; + if ((tif->tif_flags & TIFF_MYBUFFER) == 0) + { + TIFFErrorExtR(tif, module, + "Data buffer too small to hold part of strip %d", + strip); + return (0); + } + } + + if (restart) + { + tif->tif_rawdataloaded = 0; + tif->tif_rawdataoff = 0; + } + + /* + ** If we are reading more data, move any unused data to the + ** start of the buffer. + */ + if (tif->tif_rawdataloaded > 0) + unused_data = + tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata); + else + unused_data = 0; + + if (unused_data > 0) + { + assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0); + memmove(tif->tif_rawdata, tif->tif_rawcp, unused_data); + } + + /* + ** Seek to the point in the file where more data should be read. + */ + read_offset = TIFFGetStrileOffset(tif, strip) + tif->tif_rawdataoff + + tif->tif_rawdataloaded; + + if (!SeekOK(tif, read_offset)) + { + TIFFErrorExtR(tif, module, + "Seek error at scanline %" PRIu32 ", strip %d", + tif->tif_row, strip); + return 0; + } + + /* + ** How much do we want to read? + */ + if (read_ahead_mod > tif->tif_rawdatasize) + to_read = read_ahead_mod - unused_data; + else + to_read = tif->tif_rawdatasize - unused_data; + if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, strip) - + tif->tif_rawdataoff - tif->tif_rawdataloaded) + { + to_read = (tmsize_t)TIFFGetStrileByteCount(tif, strip) - + tif->tif_rawdataoff - tif->tif_rawdataloaded; + } + + assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0); + if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, /* is_strip */ + 0, /* strip_or_tile */ + module)) + { + return 0; + } + + tif->tif_rawdataoff = + tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data; + tif->tif_rawdataloaded = unused_data + to_read; + + tif->tif_rawcc = tif->tif_rawdataloaded; + tif->tif_rawcp = tif->tif_rawdata; + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + { + assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0); + TIFFReverseBits(tif->tif_rawdata + unused_data, to_read); + } + + /* + ** When starting a strip from the beginning we need to + ** restart the decoder. + */ + if (restart) + { + +#ifdef JPEG_SUPPORT + /* A bit messy since breaks the codec abstraction. Ultimately */ + /* there should be a function pointer for that, but it seems */ + /* only JPEG is affected. */ + /* For JPEG, if there are multiple scans (can generally be known */ + /* with the read_ahead used), we need to read the whole strip */ + if (tif->tif_dir.td_compression == COMPRESSION_JPEG && + (uint64_t)tif->tif_rawcc < TIFFGetStrileByteCount(tif, strip)) + { + if (TIFFJPEGIsFullStripRequired(tif)) + { + return TIFFFillStrip(tif, strip); + } + } +#endif + + return TIFFStartStrip(tif, strip); + } + else + { + return 1; + } +} + +/* + * Seek to a random row+sample in a file. + * + * Only used by TIFFReadScanline, and is only used on + * strip organized files. We do some tricky stuff to try + * and avoid reading the whole compressed raw data for big + * strips. + */ +static int TIFFSeek(TIFF *tif, uint32_t row, uint16_t sample) +{ + register TIFFDirectory *td = &tif->tif_dir; + uint32_t strip; + int whole_strip; + tmsize_t read_ahead = 0; + + /* + ** Establish what strip we are working from. + */ + if (row >= td->td_imagelength) + { /* out of range */ + TIFFErrorExtR(tif, tif->tif_name, + "%" PRIu32 ": Row out of range, max %" PRIu32 "", row, + td->td_imagelength); + return (0); + } + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + { + if (sample >= td->td_samplesperpixel) + { + TIFFErrorExtR(tif, tif->tif_name, + "%" PRIu16 ": Sample out of range, max %" PRIu16 "", + sample, td->td_samplesperpixel); + return (0); + } + strip = (uint32_t)sample * td->td_stripsperimage + + row / td->td_rowsperstrip; + } + else + strip = row / td->td_rowsperstrip; + + /* + * Do we want to treat this strip as one whole chunk or + * read it a few lines at a time? + */ +#if defined(CHUNKY_STRIP_READ_SUPPORT) + whole_strip = TIFFGetStrileByteCount(tif, strip) < 10 || isMapped(tif); + if (td->td_compression == COMPRESSION_LERC || + td->td_compression == COMPRESSION_JBIG) + { + /* Ideally plugins should have a way to declare they don't support + * chunk strip */ + whole_strip = 1; + } + + if (!whole_strip) + { + /* 16 is for YCbCr mode where we may need to read 16 */ + /* lines at a time to get a decompressed line, and 5000 */ + /* is some constant value, for example for JPEG tables */ + + /* coverity[dead_error_line:SUPPRESS] */ + if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 && + tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000) + { + read_ahead = tif->tif_scanlinesize * 16 + 5000; + } + else + { + read_ahead = tif->tif_scanlinesize; + } + } +#else + whole_strip = 1; +#endif + + /* + * If we haven't loaded this strip, do so now, possibly + * only reading the first part. + */ + if (strip != tif->tif_curstrip) + { /* different strip, refill */ + + if (whole_strip) + { + if (!TIFFFillStrip(tif, strip)) + return (0); + } +#if defined(CHUNKY_STRIP_READ_SUPPORT) + else + { + if (!TIFFFillStripPartial(tif, strip, read_ahead, 1)) + return 0; + } +#endif + } + +#if defined(CHUNKY_STRIP_READ_SUPPORT) + /* + ** If we already have some data loaded, do we need to read some more? + */ + else if (!whole_strip) + { + /* coverity[dead_error_line:SUPPRESS] */ + if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < + read_ahead && + (uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded < + TIFFGetStrileByteCount(tif, strip)) + { + if (!TIFFFillStripPartial(tif, strip, read_ahead, 0)) + return 0; + } + } +#endif + + if (row < tif->tif_row) + { + /* + * Moving backwards within the same strip: backup + * to the start and then decode forward (below). + * + * NB: If you're planning on lots of random access within a + * strip, it's better to just read and decode the entire + * strip, and then access the decoded data in a random fashion. + */ + + if (tif->tif_rawdataoff != 0) + { + if (!TIFFFillStripPartial(tif, strip, read_ahead, 1)) + return 0; + } + else + { + if (!TIFFStartStrip(tif, strip)) + return (0); + } + } + + if (row != tif->tif_row) + { + /* + * Seek forward to the desired row. + */ + + /* TODO: Will this really work with partial buffers? */ + + if (!(*tif->tif_seek)(tif, row - tif->tif_row)) + return (0); + tif->tif_row = row; + } + + return (1); +} + +int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample) +{ + int e; + + if (!TIFFCheckRead(tif, 0)) + return (-1); + if ((e = TIFFSeek(tif, row, sample)) != 0) + { + /* + * Decompress desired row into user buffer. + */ + e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize, + sample); + + /* we are now poised at the beginning of the next row */ + tif->tif_row = row + 1; + + if (e) + (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize); + } + else + { + /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */ + if (buf) + memset(buf, 0, (size_t)tif->tif_scanlinesize); + } + return (e > 0 ? 1 : -1); +} + +/* + * Calculate the strip size according to the number of + * rows in the strip (check for truncated last strip on any + * of the separations). + */ +static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip, + uint16_t *pplane) +{ + static const char module[] = "TIFFReadEncodedStrip"; + TIFFDirectory *td = &tif->tif_dir; + uint32_t rowsperstrip; + uint32_t stripsperplane; + uint32_t stripinplane; + uint32_t rows; + tmsize_t stripsize; + if (!TIFFCheckRead(tif, 0)) + return ((tmsize_t)(-1)); + if (strip >= td->td_nstrips) + { + TIFFErrorExtR(tif, module, + "%" PRIu32 ": Strip out of range, max %" PRIu32, strip, + td->td_nstrips); + return ((tmsize_t)(-1)); + } + + rowsperstrip = td->td_rowsperstrip; + if (rowsperstrip > td->td_imagelength) + rowsperstrip = td->td_imagelength; + if (rowsperstrip == 0) + { + TIFFErrorExtR(tif, module, "rowsperstrip is zero"); + return ((tmsize_t)(-1)); + } + stripsperplane = + TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip); + stripinplane = (strip % stripsperplane); + if (pplane) + *pplane = (uint16_t)(strip / stripsperplane); + rows = td->td_imagelength - stripinplane * rowsperstrip; + if (rows > rowsperstrip) + rows = rowsperstrip; + stripsize = TIFFVStripSize(tif, rows); + if (stripsize == 0) + return ((tmsize_t)(-1)); + return stripsize; +} + +/* + * Read a strip of data and decompress the specified + * amount into the user-supplied buffer. + */ +tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf, + tmsize_t size) +{ + static const char module[] = "TIFFReadEncodedStrip"; + TIFFDirectory *td = &tif->tif_dir; + tmsize_t stripsize; + uint16_t plane; + + stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane); + if (stripsize == ((tmsize_t)(-1))) + return ((tmsize_t)(-1)); + + /* shortcut to avoid an extra memcpy() */ + if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) && + size >= stripsize && !isMapped(tif) && + ((tif->tif_flags & TIFF_NOREADRAW) == 0)) + { + if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize) + return ((tmsize_t)(-1)); + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits(buf, stripsize); + + (*tif->tif_postdecode)(tif, buf, stripsize); + return (stripsize); + } + + if ((size != (tmsize_t)(-1)) && (size < stripsize)) + stripsize = size; + if (!TIFFFillStrip(tif, strip)) + { + /* The output buf may be NULL, in particular if TIFFTAG_FAXFILLFUNC + is being used. Thus, memset must be conditional on buf not NULL. */ + if (buf) + memset(buf, 0, (size_t)stripsize); + return ((tmsize_t)(-1)); + } + if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0) + return ((tmsize_t)(-1)); + (*tif->tif_postdecode)(tif, buf, stripsize); + return (stripsize); +} + +/* Variant of TIFFReadEncodedStrip() that does + * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after + * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case + * of truncated file. + * * calls regular TIFFReadEncodedStrip() if *buf != NULL + */ +tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip, + void **buf, + tmsize_t bufsizetoalloc, + tmsize_t size_to_read) +{ + tmsize_t this_stripsize; + uint16_t plane; + + if (*buf != NULL) + { + return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read); + } + + this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane); + if (this_stripsize == ((tmsize_t)(-1))) + return ((tmsize_t)(-1)); + + if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize)) + this_stripsize = size_to_read; + if (!TIFFFillStrip(tif, strip)) + return ((tmsize_t)(-1)); + + *buf = _TIFFmallocExt(tif, bufsizetoalloc); + if (*buf == NULL) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer"); + return ((tmsize_t)(-1)); + } + _TIFFmemset(*buf, 0, bufsizetoalloc); + + if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0) + return ((tmsize_t)(-1)); + (*tif->tif_postdecode)(tif, *buf, this_stripsize); + return (this_stripsize); +} + +static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf, + tmsize_t size, const char *module) +{ + assert((tif->tif_flags & TIFF_NOREADRAW) == 0); + if (!isMapped(tif)) + { + tmsize_t cc; + + if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip))) + { + TIFFErrorExtR(tif, module, + "Seek error at scanline %" PRIu32 ", strip %" PRIu32, + tif->tif_row, strip); + return ((tmsize_t)(-1)); + } + cc = TIFFReadFile(tif, buf, size); + if (cc != size) + { + TIFFErrorExtR(tif, module, + "Read error at scanline %" PRIu32 + "; got %" TIFF_SSIZE_FORMAT + " bytes, expected %" TIFF_SSIZE_FORMAT, + tif->tif_row, cc, size); + return ((tmsize_t)(-1)); + } + } + else + { + tmsize_t ma = 0; + tmsize_t n; + if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) || + ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size)) + { + n = 0; + } + else if (ma > TIFF_TMSIZE_T_MAX - size) + { + n = 0; + } + else + { + tmsize_t mb = ma + size; + if (mb > tif->tif_size) + n = tif->tif_size - ma; + else + n = size; + } + if (n != size) + { + TIFFErrorExtR(tif, module, + "Read error at scanline %" PRIu32 ", strip %" PRIu32 + "; got %" TIFF_SSIZE_FORMAT + " bytes, expected %" TIFF_SSIZE_FORMAT, + tif->tif_row, strip, n, size); + return ((tmsize_t)(-1)); + } + _TIFFmemcpy(buf, tif->tif_base + ma, size); + } + return (size); +} + +static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile, + int is_strip, tmsize_t size, + const char *module) +{ + assert(!isMapped(tif)); + assert((tif->tif_flags & TIFF_NOREADRAW) == 0); + + if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile))) + { + if (is_strip) + { + TIFFErrorExtR(tif, module, + "Seek error at scanline %" PRIu32 ", strip %" PRIu32, + tif->tif_row, strip_or_tile); + } + else + { + TIFFErrorExtR(tif, module, + "Seek error at row %" PRIu32 ", col %" PRIu32 + ", tile %" PRIu32, + tif->tif_row, tif->tif_col, strip_or_tile); + } + return ((tmsize_t)(-1)); + } + + if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module)) + { + return ((tmsize_t)(-1)); + } + + return (size); +} + +/* + * Read a strip of data from the file. + */ +tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size) +{ + static const char module[] = "TIFFReadRawStrip"; + TIFFDirectory *td = &tif->tif_dir; + uint64_t bytecount64; + tmsize_t bytecountm; + + if (!TIFFCheckRead(tif, 0)) + return ((tmsize_t)(-1)); + if (strip >= td->td_nstrips) + { + TIFFErrorExtR(tif, module, + "%" PRIu32 ": Strip out of range, max %" PRIu32, strip, + td->td_nstrips); + return ((tmsize_t)(-1)); + } + if (tif->tif_flags & TIFF_NOREADRAW) + { + TIFFErrorExtR(tif, module, + "Compression scheme does not support access to raw " + "uncompressed data"); + return ((tmsize_t)(-1)); + } + bytecount64 = TIFFGetStrileByteCount(tif, strip); + if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64) + bytecountm = size; + else + bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module); + if (bytecountm == 0) + { + return ((tmsize_t)(-1)); + } + return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module)); +} + +TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; } + +/* + * Read the specified strip and setup for decoding. The data buffer is + * expanded, as necessary, to hold the strip's data. + */ +int TIFFFillStrip(TIFF *tif, uint32_t strip) +{ + static const char module[] = "TIFFFillStrip"; + TIFFDirectory *td = &tif->tif_dir; + + if ((tif->tif_flags & TIFF_NOREADRAW) == 0) + { + uint64_t bytecount = TIFFGetStrileByteCount(tif, strip); + if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX) + { + TIFFErrorExtR(tif, module, + "Invalid strip byte count %" PRIu64 + ", strip %" PRIu32, + bytecount, strip); + return (0); + } + + /* To avoid excessive memory allocations: */ + /* Byte count should normally not be larger than a number of */ + /* times the uncompressed size plus some margin */ + if (bytecount > 1024 * 1024) + { + /* 10 and 4096 are just values that could be adjusted. */ + /* Hopefully they are safe enough for all codecs */ + tmsize_t stripsize = TIFFStripSize(tif); + if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize) + { + uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096; + TIFFErrorExtR(tif, module, + "Too large strip byte count %" PRIu64 + ", strip %" PRIu32 ". Limiting to %" PRIu64, + bytecount, strip, newbytecount); + bytecount = newbytecount; + } + } + + if (isMapped(tif)) + { + /* + * We must check for overflow, potentially causing + * an OOB read. Instead of simple + * + * TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size + * + * comparison (which can overflow) we do the following + * two comparisons: + */ + if (bytecount > (uint64_t)tif->tif_size || + TIFFGetStrileOffset(tif, strip) > + (uint64_t)tif->tif_size - bytecount) + { + /* + * This error message might seem strange, but + * it's what would happen if a read were done + * instead. + */ + TIFFErrorExtR( + tif, module, + + "Read error on strip %" PRIu32 "; " + "got %" PRIu64 " bytes, expected %" PRIu64, + strip, + NoSanitizeSubUInt64(tif->tif_size, + TIFFGetStrileOffset(tif, strip)), + bytecount); + tif->tif_curstrip = NOSTRIP; + return (0); + } + } + + if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) || + (tif->tif_flags & TIFF_NOBITREV))) + { + /* + * The image is mapped into memory and we either don't + * need to flip bits or the compression routine is + * going to handle this operation itself. In this + * case, avoid copying the raw data and instead just + * reference the data from the memory mapped file + * image. This assumes that the decompression + * routines do not modify the contents of the raw data + * buffer (if they try to, the application will get a + * fault since the file is mapped read-only). + */ + if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) + { + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_rawdata = NULL; + tif->tif_rawdatasize = 0; + } + tif->tif_flags &= ~TIFF_MYBUFFER; + tif->tif_rawdatasize = (tmsize_t)bytecount; + tif->tif_rawdata = + tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip); + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = (tmsize_t)bytecount; + + /* + * When we have tif_rawdata reference directly into the memory + * mapped file we need to be pretty careful about how we use the + * rawdata. It is not a general purpose working buffer as it + * normally otherwise is. So we keep track of this fact to avoid + * using it improperly. + */ + tif->tif_flags |= TIFF_BUFFERMMAP; + } + else + { + /* + * Expand raw data buffer, if needed, to hold data + * strip coming from file (perhaps should set upper + * bound on the size of a buffer we'll use?). + */ + tmsize_t bytecountm; + bytecountm = (tmsize_t)bytecount; + if ((uint64_t)bytecountm != bytecount) + { + TIFFErrorExtR(tif, module, "Integer overflow"); + return (0); + } + if (bytecountm > tif->tif_rawdatasize) + { + tif->tif_curstrip = NOSTRIP; + if ((tif->tif_flags & TIFF_MYBUFFER) == 0) + { + TIFFErrorExtR( + tif, module, + "Data buffer too small to hold strip %" PRIu32, strip); + return (0); + } + } + if (tif->tif_flags & TIFF_BUFFERMMAP) + { + tif->tif_curstrip = NOSTRIP; + tif->tif_rawdata = NULL; + tif->tif_rawdatasize = 0; + tif->tif_flags &= ~TIFF_BUFFERMMAP; + } + + if (isMapped(tif)) + { + if (bytecountm > tif->tif_rawdatasize && + !TIFFReadBufferSetup(tif, 0, bytecountm)) + { + return (0); + } + if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm, + module) != bytecountm) + { + return (0); + } + } + else + { + if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm, + module) != bytecountm) + { + return (0); + } + } + + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = bytecountm; + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits(tif->tif_rawdata, bytecountm); + } + } + return (TIFFStartStrip(tif, strip)); +} + +/* + * Tile-oriented Read Support + * Contributed by Nancy Cam (Silicon Graphics). + */ + +/* + * Read and decompress a tile of data. The + * tile is selected by the (x,y,z,s) coordinates. + */ +tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, + uint16_t s) +{ + if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s)) + return ((tmsize_t)(-1)); + return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf, + (tmsize_t)(-1))); +} + +/* + * Read a tile of data and decompress the specified + * amount into the user-supplied buffer. + */ +tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size) +{ + static const char module[] = "TIFFReadEncodedTile"; + TIFFDirectory *td = &tif->tif_dir; + tmsize_t tilesize = tif->tif_tilesize; + + if (!TIFFCheckRead(tif, 1)) + return ((tmsize_t)(-1)); + if (tile >= td->td_nstrips) + { + TIFFErrorExtR(tif, module, + "%" PRIu32 ": Tile out of range, max %" PRIu32, tile, + td->td_nstrips); + return ((tmsize_t)(-1)); + } + + /* shortcut to avoid an extra memcpy() */ + if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) && + size >= tilesize && !isMapped(tif) && + ((tif->tif_flags & TIFF_NOREADRAW) == 0)) + { + if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize) + return ((tmsize_t)(-1)); + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits(buf, tilesize); + + (*tif->tif_postdecode)(tif, buf, tilesize); + return (tilesize); + } + + if (size == (tmsize_t)(-1)) + size = tilesize; + else if (size > tilesize) + size = tilesize; + if (!TIFFFillTile(tif, tile)) + { + /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */ + if (buf) + memset(buf, 0, (size_t)size); + return ((tmsize_t)(-1)); + } + else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size, + (uint16_t)(tile / td->td_stripsperimage))) + { + (*tif->tif_postdecode)(tif, (uint8_t *)buf, size); + return (size); + } + else + return ((tmsize_t)(-1)); +} + +/* Variant of TIFFReadTile() that does + * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after + * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case + * of truncated file. + * * calls regular TIFFReadEncodedTile() if *buf != NULL + */ +tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf, + tmsize_t bufsizetoalloc, uint32_t x, + uint32_t y, uint32_t z, uint16_t s) +{ + if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s)) + return ((tmsize_t)(-1)); + return (_TIFFReadEncodedTileAndAllocBuffer( + tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc, + (tmsize_t)(-1))); +} + +/* Variant of TIFFReadEncodedTile() that does + * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after + * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case + * of truncated file. + * * calls regular TIFFReadEncodedTile() if *buf != NULL + */ +tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile, + void **buf, tmsize_t bufsizetoalloc, + tmsize_t size_to_read) +{ + static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer"; + TIFFDirectory *td = &tif->tif_dir; + tmsize_t tilesize = tif->tif_tilesize; + + if (*buf != NULL) + { + return TIFFReadEncodedTile(tif, tile, *buf, size_to_read); + } + + if (!TIFFCheckRead(tif, 1)) + return ((tmsize_t)(-1)); + if (tile >= td->td_nstrips) + { + TIFFErrorExtR(tif, module, + "%" PRIu32 ": Tile out of range, max %" PRIu32, tile, + td->td_nstrips); + return ((tmsize_t)(-1)); + } + + if (!TIFFFillTile(tif, tile)) + return ((tmsize_t)(-1)); + + /* Sanity checks to avoid excessive memory allocation */ + /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */ + if (td->td_compression == COMPRESSION_NONE) + { + if (tif->tif_rawdatasize != tilesize) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Invalid tile byte count for tile %u. " + "Expected %" PRIu64 ", got %" PRIu64, + tile, (uint64_t)tilesize, + (uint64_t)tif->tif_rawdatasize); + return ((tmsize_t)(-1)); + } + } + else + { + /* Max compression ratio experimentally determined. Might be fragile... + * Only apply this heuristics to situations where the memory allocation + * would be big, to avoid breaking nominal use cases. + */ + const int maxCompressionRatio = + td->td_compression == COMPRESSION_ZSTD ? 33000 + : td->td_compression == COMPRESSION_JXL + ? + /* Evaluated on a 8000x8000 tile */ + 25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG + ? td->td_samplesperpixel + : 1) + : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000; + if (bufsizetoalloc > 100 * 1000 * 1000 && + tif->tif_rawdatasize < tilesize / maxCompressionRatio) + { + TIFFErrorExtR(tif, TIFFFileName(tif), + "Likely invalid tile byte count for tile %u. " + "Uncompressed tile size is %" PRIu64 ", " + "compressed one is %" PRIu64, + tile, (uint64_t)tilesize, + (uint64_t)tif->tif_rawdatasize); + return ((tmsize_t)(-1)); + } + } + + *buf = _TIFFmallocExt(tif, bufsizetoalloc); + if (*buf == NULL) + { + TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer"); + return ((tmsize_t)(-1)); + } + _TIFFmemset(*buf, 0, bufsizetoalloc); + + if (size_to_read == (tmsize_t)(-1)) + size_to_read = tilesize; + else if (size_to_read > tilesize) + size_to_read = tilesize; + if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read, + (uint16_t)(tile / td->td_stripsperimage))) + { + (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read); + return (size_to_read); + } + else + return ((tmsize_t)(-1)); +} + +static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf, + tmsize_t size, const char *module) +{ + assert((tif->tif_flags & TIFF_NOREADRAW) == 0); + if (!isMapped(tif)) + { + tmsize_t cc; + + if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile))) + { + TIFFErrorExtR(tif, module, + "Seek error at row %" PRIu32 ", col %" PRIu32 + ", tile %" PRIu32, + tif->tif_row, tif->tif_col, tile); + return ((tmsize_t)(-1)); + } + cc = TIFFReadFile(tif, buf, size); + if (cc != size) + { + TIFFErrorExtR(tif, module, + "Read error at row %" PRIu32 ", col %" PRIu32 + "; got %" TIFF_SSIZE_FORMAT + " bytes, expected %" TIFF_SSIZE_FORMAT, + tif->tif_row, tif->tif_col, cc, size); + return ((tmsize_t)(-1)); + } + } + else + { + tmsize_t ma, mb; + tmsize_t n; + ma = (tmsize_t)TIFFGetStrileOffset(tif, tile); + mb = ma + size; + if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) || + (ma > tif->tif_size)) + n = 0; + else if ((mb < ma) || (mb < size) || (mb > tif->tif_size)) + n = tif->tif_size - ma; + else + n = size; + if (n != size) + { + TIFFErrorExtR(tif, module, + "Read error at row %" PRIu32 ", col %" PRIu32 + ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT + " bytes, expected %" TIFF_SSIZE_FORMAT, + tif->tif_row, tif->tif_col, tile, n, size); + return ((tmsize_t)(-1)); + } + _TIFFmemcpy(buf, tif->tif_base + ma, size); + } + return (size); +} + +/* + * Read a tile of data from the file. + */ +tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size) +{ + static const char module[] = "TIFFReadRawTile"; + TIFFDirectory *td = &tif->tif_dir; + uint64_t bytecount64; + tmsize_t bytecountm; + + if (!TIFFCheckRead(tif, 1)) + return ((tmsize_t)(-1)); + if (tile >= td->td_nstrips) + { + TIFFErrorExtR(tif, module, + "%" PRIu32 ": Tile out of range, max %" PRIu32, tile, + td->td_nstrips); + return ((tmsize_t)(-1)); + } + if (tif->tif_flags & TIFF_NOREADRAW) + { + TIFFErrorExtR(tif, module, + "Compression scheme does not support access to raw " + "uncompressed data"); + return ((tmsize_t)(-1)); + } + bytecount64 = TIFFGetStrileByteCount(tif, tile); + if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64) + bytecountm = size; + else + bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module); + if (bytecountm == 0) + { + return ((tmsize_t)(-1)); + } + return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module)); +} + +/* + * Read the specified tile and setup for decoding. The data buffer is + * expanded, as necessary, to hold the tile's data. + */ +int TIFFFillTile(TIFF *tif, uint32_t tile) +{ + static const char module[] = "TIFFFillTile"; + TIFFDirectory *td = &tif->tif_dir; + + if ((tif->tif_flags & TIFF_NOREADRAW) == 0) + { + uint64_t bytecount = TIFFGetStrileByteCount(tif, tile); + if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX) + { + TIFFErrorExtR(tif, module, + "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32, + bytecount, tile); + return (0); + } + + /* To avoid excessive memory allocations: */ + /* Byte count should normally not be larger than a number of */ + /* times the uncompressed size plus some margin */ + if (bytecount > 1024 * 1024) + { + /* 10 and 4096 are just values that could be adjusted. */ + /* Hopefully they are safe enough for all codecs */ + tmsize_t stripsize = TIFFTileSize(tif); + if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize) + { + uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096; + TIFFErrorExtR(tif, module, + "Too large tile byte count %" PRIu64 + ", tile %" PRIu32 ". Limiting to %" PRIu64, + bytecount, tile, newbytecount); + bytecount = newbytecount; + } + } + + if (isMapped(tif)) + { + /* + * We must check for overflow, potentially causing + * an OOB read. Instead of simple + * + * TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size + * + * comparison (which can overflow) we do the following + * two comparisons: + */ + if (bytecount > (uint64_t)tif->tif_size || + TIFFGetStrileOffset(tif, tile) > + (uint64_t)tif->tif_size - bytecount) + { + tif->tif_curtile = NOTILE; + return (0); + } + } + + if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) || + (tif->tif_flags & TIFF_NOBITREV))) + { + /* + * The image is mapped into memory and we either don't + * need to flip bits or the compression routine is + * going to handle this operation itself. In this + * case, avoid copying the raw data and instead just + * reference the data from the memory mapped file + * image. This assumes that the decompression + * routines do not modify the contents of the raw data + * buffer (if they try to, the application will get a + * fault since the file is mapped read-only). + */ + if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) + { + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_rawdata = NULL; + tif->tif_rawdatasize = 0; + } + tif->tif_flags &= ~TIFF_MYBUFFER; + + tif->tif_rawdatasize = (tmsize_t)bytecount; + tif->tif_rawdata = + tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile); + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = (tmsize_t)bytecount; + tif->tif_flags |= TIFF_BUFFERMMAP; + } + else + { + /* + * Expand raw data buffer, if needed, to hold data + * tile coming from file (perhaps should set upper + * bound on the size of a buffer we'll use?). + */ + tmsize_t bytecountm; + bytecountm = (tmsize_t)bytecount; + if ((uint64_t)bytecountm != bytecount) + { + TIFFErrorExtR(tif, module, "Integer overflow"); + return (0); + } + if (bytecountm > tif->tif_rawdatasize) + { + tif->tif_curtile = NOTILE; + if ((tif->tif_flags & TIFF_MYBUFFER) == 0) + { + TIFFErrorExtR(tif, module, + "Data buffer too small to hold tile %" PRIu32, + tile); + return (0); + } + } + if (tif->tif_flags & TIFF_BUFFERMMAP) + { + tif->tif_curtile = NOTILE; + tif->tif_rawdata = NULL; + tif->tif_rawdatasize = 0; + tif->tif_flags &= ~TIFF_BUFFERMMAP; + } + + if (isMapped(tif)) + { + if (bytecountm > tif->tif_rawdatasize && + !TIFFReadBufferSetup(tif, 0, bytecountm)) + { + return (0); + } + if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm, + module) != bytecountm) + { + return (0); + } + } + else + { + if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) != + bytecountm) + { + return (0); + } + } + + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = bytecountm; + + if (tif->tif_rawdata != NULL && + !isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded); + } + } + return (TIFFStartTile(tif, tile)); +} + +/* + * Setup the raw data buffer in preparation for + * reading a strip of raw data. If the buffer + * is specified as zero, then a buffer of appropriate + * size is allocated by the library. Otherwise, + * the client must guarantee that the buffer is + * large enough to hold any individual strip of + * raw data. + */ +int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size) +{ + static const char module[] = "TIFFReadBufferSetup"; + + assert((tif->tif_flags & TIFF_NOREADRAW) == 0); + tif->tif_flags &= ~TIFF_BUFFERMMAP; + + if (tif->tif_rawdata) + { + if (tif->tif_flags & TIFF_MYBUFFER) + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_rawdata = NULL; + tif->tif_rawdatasize = 0; + } + if (bp) + { + tif->tif_rawdatasize = size; + tif->tif_rawdata = (uint8_t *)bp; + tif->tif_flags &= ~TIFF_MYBUFFER; + } + else + { + tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024); + if (tif->tif_rawdatasize == 0) + { + TIFFErrorExtR(tif, module, "Invalid buffer size"); + return (0); + } + /* Initialize to zero to avoid uninitialized buffers in case of */ + /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */ + tif->tif_rawdata = + (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize); + tif->tif_flags |= TIFF_MYBUFFER; + } + if (tif->tif_rawdata == NULL) + { + TIFFErrorExtR(tif, module, + "No space for data buffer at scanline %" PRIu32, + tif->tif_row); + tif->tif_rawdatasize = 0; + return (0); + } + return (1); +} + +/* + * Set state to appear as if a + * strip has just been read in. + */ +static int TIFFStartStrip(TIFF *tif, uint32_t strip) +{ + TIFFDirectory *td = &tif->tif_dir; + + if ((tif->tif_flags & TIFF_CODERSETUP) == 0) + { + if (!(*tif->tif_setupdecode)(tif)) + return (0); + tif->tif_flags |= TIFF_CODERSETUP; + } + tif->tif_curstrip = strip; + tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; + tif->tif_flags &= ~TIFF_BUF4WRITE; + + if (tif->tif_flags & TIFF_NOREADRAW) + { + tif->tif_rawcp = NULL; + tif->tif_rawcc = 0; + } + else + { + tif->tif_rawcp = tif->tif_rawdata; + if (tif->tif_rawdataloaded > 0) + tif->tif_rawcc = tif->tif_rawdataloaded; + else + tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip); + } + if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) == + 0) + { + /* Needed for example for scanline access, if tif_predecode */ + /* fails, and we try to read the same strip again. Without invalidating + */ + /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */ + /* codec state. */ + tif->tif_curstrip = NOSTRIP; + return 0; + } + return 1; +} + +/* + * Set state to appear as if a + * tile has just been read in. + */ +static int TIFFStartTile(TIFF *tif, uint32_t tile) +{ + static const char module[] = "TIFFStartTile"; + TIFFDirectory *td = &tif->tif_dir; + uint32_t howmany32; + + if ((tif->tif_flags & TIFF_CODERSETUP) == 0) + { + if (!(*tif->tif_setupdecode)(tif)) + return (0); + tif->tif_flags |= TIFF_CODERSETUP; + } + tif->tif_curtile = tile; + if (td->td_tilewidth == 0) + { + TIFFErrorExtR(tif, module, "Zero tilewidth"); + return 0; + } + howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth); + if (howmany32 == 0) + { + TIFFErrorExtR(tif, module, "Zero tiles"); + return 0; + } + tif->tif_row = (tile % howmany32) * td->td_tilelength; + howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength); + if (howmany32 == 0) + { + TIFFErrorExtR(tif, module, "Zero tiles"); + return 0; + } + tif->tif_col = (tile % howmany32) * td->td_tilewidth; + tif->tif_flags &= ~TIFF_BUF4WRITE; + if (tif->tif_flags & TIFF_NOREADRAW) + { + tif->tif_rawcp = NULL; + tif->tif_rawcc = 0; + } + else + { + tif->tif_rawcp = tif->tif_rawdata; + if (tif->tif_rawdataloaded > 0) + tif->tif_rawcc = tif->tif_rawdataloaded; + else + tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile); + } + return ( + (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage))); +} + +static int TIFFCheckRead(TIFF *tif, int tiles) +{ + if (tif->tif_mode == O_WRONLY) + { + TIFFErrorExtR(tif, tif->tif_name, "File not open for reading"); + return (0); + } + if (tiles ^ isTiled(tif)) + { + TIFFErrorExtR(tif, tif->tif_name, + tiles ? "Can not read tiles from a striped image" + : "Can not read scanlines from a tiled image"); + return (0); + } + return (1); +} + +/* Use the provided input buffer (inbuf, insize) and decompress it into + * (outbuf, outsize). + * This function replaces the use of + * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the + * buffer for the input data, for example when he wants to avoid libtiff to read + * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts] + * array. inbuf content must be writable (if bit reversal is needed) Returns 1 + * in case of success, 0 otherwise. + */ +int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf, + tmsize_t insize, void *outbuf, tmsize_t outsize) +{ + static const char module[] = "TIFFReadFromUserBuffer"; + TIFFDirectory *td = &tif->tif_dir; + int ret = 1; + uint32_t old_tif_flags = tif->tif_flags; + tmsize_t old_rawdatasize = tif->tif_rawdatasize; + void *old_rawdata = tif->tif_rawdata; + + if (tif->tif_mode == O_WRONLY) + { + TIFFErrorExtR(tif, tif->tif_name, "File not open for reading"); + return 0; + } + if (tif->tif_flags & TIFF_NOREADRAW) + { + TIFFErrorExtR(tif, module, + "Compression scheme does not support access to raw " + "uncompressed data"); + return 0; + } + + tif->tif_flags &= ~TIFF_MYBUFFER; + tif->tif_flags |= TIFF_BUFFERMMAP; + tif->tif_rawdatasize = insize; + tif->tif_rawdata = inbuf; + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = insize; + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + { + TIFFReverseBits(inbuf, insize); + } + + if (TIFFIsTiled(tif)) + { + if (!TIFFStartTile(tif, strile)) + { + ret = 0; + /* See related TIFFReadEncodedStrip comment. */ + if (outbuf) + memset(outbuf, 0, (size_t)outsize); + } + else if (!(*tif->tif_decodetile)( + tif, (uint8_t *)outbuf, outsize, + (uint16_t)(strile / td->td_stripsperimage))) + { + ret = 0; + } + } + else + { + uint32_t rowsperstrip = td->td_rowsperstrip; + uint32_t stripsperplane; + if (rowsperstrip > td->td_imagelength) + rowsperstrip = td->td_imagelength; + if (rowsperstrip == 0) + { + TIFFErrorExtR(tif, module, "rowsperstrip is zero"); + ret = 0; + } + else + { + stripsperplane = + TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip); + if (!TIFFStartStrip(tif, strile)) + { + ret = 0; + /* See related TIFFReadEncodedStrip comment. */ + if (outbuf) + memset(outbuf, 0, (size_t)outsize); + } + else if (!(*tif->tif_decodestrip)( + tif, (uint8_t *)outbuf, outsize, + (uint16_t)(strile / stripsperplane))) + { + ret = 0; + } + } + } + if (ret) + { + (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize); + } + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + { + TIFFReverseBits(inbuf, insize); + } + + tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) | + (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP)); + tif->tif_rawdatasize = old_rawdatasize; + tif->tif_rawdata = old_rawdata; + tif->tif_rawdataoff = 0; + tif->tif_rawdataloaded = 0; + + return ret; +} + +void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + (void)tif; + (void)buf; + (void)cc; +} + +void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + (void)tif; + assert((cc & 1) == 0); + TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2); +} + +void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + (void)tif; + assert((cc % 3) == 0); + TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3); +} + +void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + (void)tif; + assert((cc & 3) == 0); + TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4); +} + +void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) +{ + (void)tif; + assert((cc & 7) == 0); + TIFFSwabArrayOfDouble((double *)buf, cc / 8); +} diff --git a/cpp/3rd_party/libtiff/tif_stream.cxx b/cpp/3rd_party/libtiff/tif_stream.cxx new file mode 100644 index 0000000000..7c7deb6bba --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_stream.cxx @@ -0,0 +1,434 @@ +/* + * Copyright (c) 1988-1996 Sam Leffler + * Copyright (c) 1991-1996 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library UNIX-specific Routines. + */ +#include "tiffiop.h" +#include + +#ifndef __VMS +using namespace std; +#endif + +/* + ISO C++ uses a 'std::streamsize' type to define counts. This makes + it similar to, (but perhaps not the same as) size_t. + + The std::ios::pos_type is used to represent stream positions as used + by tellg(), tellp(), seekg(), and seekp(). This makes it similar to + (but perhaps not the same as) 'off_t'. The std::ios::streampos type + is used for character streams, but is documented to not be an + integral type anymore, so it should *not* be assigned to an integral + type. + + The std::ios::off_type is used to specify relative offsets needed by + the variants of seekg() and seekp() which accept a relative offset + argument. + + Useful prototype knowledge: + + Obtain read position + ios::pos_type basic_istream::tellg() + + Set read position + basic_istream& basic_istream::seekg(ios::pos_type) + basic_istream& basic_istream::seekg(ios::off_type, ios_base::seekdir) + + Read data + basic_istream& istream::read(char *str, streamsize count) + + Number of characters read in last unformatted read + streamsize istream::gcount(); + + Obtain write position + ios::pos_type basic_ostream::tellp() + + Set write position + basic_ostream& basic_ostream::seekp(ios::pos_type) + basic_ostream& basic_ostream::seekp(ios::off_type, ios_base::seekdir) + + Write data + basic_ostream& ostream::write(const char *str, streamsize count) +*/ + +struct tiffis_data; +struct tiffos_data; + +extern "C" { + + static tmsize_t _tiffosReadProc(thandle_t, void*, tmsize_t); + static tmsize_t _tiffisReadProc(thandle_t fd, void* buf, tmsize_t size); + static tmsize_t _tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size); + static tmsize_t _tiffisWriteProc(thandle_t, void*, tmsize_t); + static uint64 _tiffosSeekProc(thandle_t fd, uint64 off, int whence); + static uint64 _tiffisSeekProc(thandle_t fd, uint64 off, int whence); + static uint64 _tiffosSizeProc(thandle_t fd); + static uint64 _tiffisSizeProc(thandle_t fd); + static int _tiffosCloseProc(thandle_t fd); + static int _tiffisCloseProc(thandle_t fd); + static int _tiffDummyMapProc(thandle_t , const void** base, toff_t* size ); + static void _tiffDummyUnmapProc(thandle_t , const void* base, toff_t size ); + static TIFF* _tiffStreamOpen(const char* name, const char* mode, void *fd); + +struct tiffis_data +{ + istream *stream; + ios::pos_type start_pos; +}; + +struct tiffos_data +{ + ostream *stream; + ios::pos_type start_pos; +}; + +static tmsize_t +_tiffosReadProc(thandle_t, void*, tmsize_t) +{ + return 0; +} + +static tmsize_t +_tiffisReadProc(thandle_t fd, void* buf, tmsize_t size) +{ + tiffis_data *data = reinterpret_cast(fd); + + // Verify that type does not overflow. + streamsize request_size = size; + if (static_cast(request_size) != size) + return static_cast(-1); + + data->stream->read((char *) buf, request_size); + + return static_cast(data->stream->gcount()); +} + +static tmsize_t +_tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size) +{ + tiffos_data *data = reinterpret_cast(fd); + ostream *os = data->stream; + ios::pos_type pos = os->tellp(); + + // Verify that type does not overflow. + streamsize request_size = size; + if (static_cast(request_size) != size) + return static_cast(-1); + + os->write(reinterpret_cast(buf), request_size); + + return static_cast(os->tellp() - pos); +} + +static tmsize_t +_tiffisWriteProc(thandle_t, void*, tmsize_t) +{ + return 0; +} + +static uint64 +_tiffosSeekProc(thandle_t fd, uint64 off, int whence) +{ + tiffos_data *data = reinterpret_cast(fd); + ostream *os = data->stream; + + // if the stream has already failed, don't do anything + if( os->fail() ) + return static_cast(-1); + + switch(whence) { + case SEEK_SET: + { + // Compute 64-bit offset + uint64 new_offset = static_cast(data->start_pos) + off; + + // Verify that value does not overflow + ios::off_type offset = static_cast(new_offset); + if (static_cast(offset) != new_offset) + return static_cast(-1); + + os->seekp(offset, ios::beg); + break; + } + case SEEK_CUR: + { + // Verify that value does not overflow + ios::off_type offset = static_cast(off); + if (static_cast(offset) != off) + return static_cast(-1); + + os->seekp(offset, ios::cur); + break; + } + case SEEK_END: + { + // Verify that value does not overflow + ios::off_type offset = static_cast(off); + if (static_cast(offset) != off) + return static_cast(-1); + + os->seekp(offset, ios::end); + break; + } + } + + // Attempt to workaround problems with seeking past the end of the + // stream. ofstream doesn't have a problem with this but + // ostrstream/ostringstream does. In that situation, add intermediate + // '\0' characters. + if( os->fail() ) { +#ifdef __VMS + int old_state; +#else + ios::iostate old_state; +#endif + ios::pos_type origin; + + old_state = os->rdstate(); + // reset the fail bit or else tellp() won't work below + os->clear(os->rdstate() & ~ios::failbit); + switch( whence ) { + case SEEK_SET: + default: + origin = data->start_pos; + break; + case SEEK_CUR: + origin = os->tellp(); + break; + case SEEK_END: + os->seekp(0, ios::end); + origin = os->tellp(); + break; + } + // restore original stream state + os->clear(old_state); + + // only do something if desired seek position is valid + if( (static_cast(origin) + off) > static_cast(data->start_pos) ) { + uint64 num_fill; + + // clear the fail bit + os->clear(os->rdstate() & ~ios::failbit); + + // extend the stream to the expected size + os->seekp(0, ios::end); + num_fill = (static_cast(origin)) + off - os->tellp(); + for( uint64 i = 0; i < num_fill; i++ ) + os->put('\0'); + + // retry the seek + os->seekp(static_cast(static_cast(origin) + off), ios::beg); + } + } + + return static_cast(os->tellp()); +} + +static uint64 +_tiffisSeekProc(thandle_t fd, uint64 off, int whence) +{ + tiffis_data *data = reinterpret_cast(fd); + + switch(whence) { + case SEEK_SET: + { + // Compute 64-bit offset + uint64 new_offset = static_cast(data->start_pos) + off; + + // Verify that value does not overflow + ios::off_type offset = static_cast(new_offset); + if (static_cast(offset) != new_offset) + return static_cast(-1); + + data->stream->seekg(offset, ios::beg); + break; + } + case SEEK_CUR: + { + // Verify that value does not overflow + ios::off_type offset = static_cast(off); + if (static_cast(offset) != off) + return static_cast(-1); + + data->stream->seekg(offset, ios::cur); + break; + } + case SEEK_END: + { + // Verify that value does not overflow + ios::off_type offset = static_cast(off); + if (static_cast(offset) != off) + return static_cast(-1); + + data->stream->seekg(offset, ios::end); + break; + } + } + + return (uint64) (data->stream->tellg() - data->start_pos); +} + +static uint64 +_tiffosSizeProc(thandle_t fd) +{ + tiffos_data *data = reinterpret_cast(fd); + ostream *os = data->stream; + ios::pos_type pos = os->tellp(); + ios::pos_type len; + + os->seekp(0, ios::end); + len = os->tellp(); + os->seekp(pos); + + return (uint64) len; +} + +static uint64 +_tiffisSizeProc(thandle_t fd) +{ + tiffis_data *data = reinterpret_cast(fd); + ios::pos_type pos = data->stream->tellg(); + ios::pos_type len; + + data->stream->seekg(0, ios::end); + len = data->stream->tellg(); + data->stream->seekg(pos); + + return (uint64) len; +} + +static int +_tiffosCloseProc(thandle_t fd) +{ + // Our stream was not allocated by us, so it shouldn't be closed by us. + delete reinterpret_cast(fd); + return 0; +} + +static int +_tiffisCloseProc(thandle_t fd) +{ + // Our stream was not allocated by us, so it shouldn't be closed by us. + delete reinterpret_cast(fd); + return 0; +} + +static int +_tiffDummyMapProc(thandle_t , const void** base, toff_t* size ) +{ + (void) base; + (void) size; + return (0); +} + +static void +_tiffDummyUnmapProc(thandle_t , const void* base, toff_t size ) +{ + (void) base; + (void) size; +} + +/* + * Open a TIFF file descriptor for read/writing. + */ +static TIFF* +_tiffStreamOpen(const char* name, const char* mode, void *fd) +{ + TIFF* tif; + + if( strchr(mode, 'w') ) { + tiffos_data *data = new tiffos_data; + data->stream = reinterpret_cast(fd); + data->start_pos = data->stream->tellp(); + + // Open for writing. + tif = TIFFClientOpen(name, mode, + reinterpret_cast(data), + _tiffosReadProc, + _tiffosWriteProc, + _tiffosSeekProc, + _tiffosCloseProc, + _tiffosSizeProc, + _tiffDummyMapProc, + _tiffDummyUnmapProc); + if (!tif) { + delete data; + } + } else { + tiffis_data *data = new tiffis_data; + data->stream = reinterpret_cast(fd); + data->start_pos = data->stream->tellg(); + // Open for reading. + tif = TIFFClientOpen(name, mode, + reinterpret_cast(data), + _tiffisReadProc, + _tiffisWriteProc, + _tiffisSeekProc, + _tiffisCloseProc, + _tiffisSizeProc, + _tiffDummyMapProc, + _tiffDummyUnmapProc); + if (!tif) { + delete data; + } + } + + return (tif); +} + +} /* extern "C" */ + +TIFF* +TIFFStreamOpen(const char* name, ostream *os) +{ + // If os is either a ostrstream or ostringstream, and has no data + // written to it yet, then tellp() will return -1 which will break us. + // We workaround this by writing out a dummy character and + // then seek back to the beginning. + if( !os->fail() && static_cast(os->tellp()) < 0 ) { + *os << '\0'; + os->seekp(0); + } + + // NB: We don't support mapped files with streams so add 'm' + return _tiffStreamOpen(name, "wm", os); +} + +TIFF* +TIFFStreamOpen(const char* name, istream *is) +{ + // NB: We don't support mapped files with streams so add 'm' + return _tiffStreamOpen(name, "rm", is); +} + +/* vim: set ts=8 sts=8 sw=8 noet: */ +/* + * Local Variables: + * mode: c + * c-basic-offset: 8 + * fill-column: 78 + * End: + */ + diff --git a/cpp/3rd_party/libtiff/tif_strip.c b/cpp/3rd_party/libtiff/tif_strip.c new file mode 100644 index 0000000000..c9ba393669 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_strip.c @@ -0,0 +1,381 @@ +/* + * Copyright (c) 1991-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Strip-organized Image Support Routines. + */ +#include "tiffiop.h" + +/* + * Compute which strip a (row,sample) value is in. + */ +uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample) +{ + static const char module[] = "TIFFComputeStrip"; + TIFFDirectory *td = &tif->tif_dir; + uint32_t strip; + + if (td->td_rowsperstrip == 0) + { + TIFFErrorExtR(tif, module, + "Cannot compute strip: RowsPerStrip is zero"); + return 0; + } + strip = row / td->td_rowsperstrip; + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + { + if (sample >= td->td_samplesperpixel) + { + TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu", + (unsigned long)sample, + (unsigned long)td->td_samplesperpixel); + return (0); + } + strip += (uint32_t)sample * td->td_stripsperimage; + } + return (strip); +} + +/* + * Compute how many strips are in an image. + */ +uint32_t TIFFNumberOfStrips(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + uint32_t nstrips; + + if (td->td_rowsperstrip == 0) + { + TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero"); + return 0; + } + nstrips = (td->td_rowsperstrip == (uint32_t)-1 + ? 1 + : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip)); + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + nstrips = + _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel, + "TIFFNumberOfStrips"); + return (nstrips); +} + +/* + * Compute the # bytes in a variable height, row-aligned strip. + */ +uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows) +{ + static const char module[] = "TIFFVStripSize64"; + TIFFDirectory *td = &tif->tif_dir; + if (nrows == (uint32_t)(-1)) + nrows = td->td_imagelength; + if ((td->td_planarconfig == PLANARCONFIG_CONTIG) && + (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif))) + { + /* + * Packed YCbCr data contain one Cb+Cr for every + * HorizontalSampling*VerticalSampling Y values. + * Must also roundup width and height when calculating + * since images that are not a multiple of the + * horizontal/vertical subsampling area include + * YCbCr data for the extended image. + */ + uint16_t ycbcrsubsampling[2]; + uint16_t samplingblock_samples; + uint32_t samplingblocks_hor; + uint32_t samplingblocks_ver; + uint64_t samplingrow_samples; + uint64_t samplingrow_size; + if (td->td_samplesperpixel != 3) + { + TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value"); + return 0; + } + TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, + ycbcrsubsampling + 0, ycbcrsubsampling + 1); + if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && + ycbcrsubsampling[0] != 4) || + (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && + ycbcrsubsampling[1] != 4) || + (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0)) + { + TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)", + ycbcrsubsampling[0], ycbcrsubsampling[1]); + return 0; + } + samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2; + samplingblocks_hor = + TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]); + samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]); + samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor, + samplingblock_samples, module); + samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64( + tif, samplingrow_samples, td->td_bitspersample, module)); + return ( + _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module)); + } + else + return (_TIFFMultiply64(tif, nrows, TIFFScanlineSize64(tif), module)); +} +tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows) +{ + static const char module[] = "TIFFVStripSize"; + uint64_t m; + m = TIFFVStripSize64(tif, nrows); + return _TIFFCastUInt64ToSSize(tif, m, module); +} + +/* + * Compute the # bytes in a raw strip. + */ +uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip) +{ + static const char module[] = "TIFFRawStripSize64"; + uint64_t bytecount = TIFFGetStrileByteCount(tif, strip); + + if (bytecount == 0) + { + TIFFErrorExtR(tif, module, + "%" PRIu64 ": Invalid strip byte count, strip %lu", + (uint64_t)bytecount, (unsigned long)strip); + bytecount = (uint64_t)-1; + } + + return bytecount; +} +tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip) +{ + static const char module[] = "TIFFRawStripSize"; + uint64_t m; + tmsize_t n; + m = TIFFRawStripSize64(tif, strip); + if (m == (uint64_t)(-1)) + n = (tmsize_t)(-1); + else + { + n = (tmsize_t)m; + if ((uint64_t)n != m) + { + TIFFErrorExtR(tif, module, "Integer overflow"); + n = 0; + } + } + return (n); +} + +/* + * Compute the # bytes in a (row-aligned) strip. + * + * Note that if RowsPerStrip is larger than the + * recorded ImageLength, then the strip size is + * truncated to reflect the actual space required + * to hold the strip. + */ +uint64_t TIFFStripSize64(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + uint32_t rps = td->td_rowsperstrip; + if (rps > td->td_imagelength) + rps = td->td_imagelength; + return (TIFFVStripSize64(tif, rps)); +} +tmsize_t TIFFStripSize(TIFF *tif) +{ + static const char module[] = "TIFFStripSize"; + uint64_t m; + m = TIFFStripSize64(tif); + return _TIFFCastUInt64ToSSize(tif, m, module); +} + +/* + * Compute a default strip size based on the image + * characteristics and a requested value. If the + * request is <1 then we choose a strip size according + * to certain heuristics. + */ +uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request) +{ + return (*tif->tif_defstripsize)(tif, request); +} + +uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s) +{ + if ((int32_t)s < 1) + { + /* + * If RowsPerStrip is unspecified, try to break the + * image up into strips that are approximately + * STRIP_SIZE_DEFAULT bytes long. + */ + uint64_t scanlinesize; + uint64_t rows; + scanlinesize = TIFFScanlineSize64(tif); + if (scanlinesize == 0) + scanlinesize = 1; + rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize; + if (rows == 0) + rows = 1; + else if (rows > 0xFFFFFFFF) + rows = 0xFFFFFFFF; + s = (uint32_t)rows; + } + return (s); +} + +/* + * Return the number of bytes to read/write in a call to + * one of the scanline-oriented i/o routines. Note that + * this number may be 1/samples-per-pixel if data is + * stored as separate planes. + * The ScanlineSize in case of YCbCrSubsampling is defined as the + * strip size divided by the strip height, i.e. the size of a pack of vertical + * subsampling lines divided by vertical subsampling. It should thus make + * sense when multiplied by a multiple of vertical subsampling. + */ +uint64_t TIFFScanlineSize64(TIFF *tif) +{ + static const char module[] = "TIFFScanlineSize64"; + TIFFDirectory *td = &tif->tif_dir; + uint64_t scanline_size; + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + if ((td->td_photometric == PHOTOMETRIC_YCBCR) && + (td->td_samplesperpixel == 3) && (!isUpSampled(tif))) + { + uint16_t ycbcrsubsampling[2]; + uint16_t samplingblock_samples; + uint32_t samplingblocks_hor; + uint64_t samplingrow_samples; + uint64_t samplingrow_size; + if (td->td_samplesperpixel != 3) + { + TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value"); + return 0; + } + TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, + ycbcrsubsampling + 0, ycbcrsubsampling + 1); + if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) && + (ycbcrsubsampling[0] != 4)) || + ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) && + (ycbcrsubsampling[1] != 4)) || + ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0))) + { + TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling"); + return 0; + } + samplingblock_samples = + ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2; + samplingblocks_hor = + TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]); + samplingrow_samples = _TIFFMultiply64( + tif, samplingblocks_hor, samplingblock_samples, module); + samplingrow_size = + TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples, + td->td_bitspersample, module), + 8); + scanline_size = (samplingrow_size / ycbcrsubsampling[1]); + } + else + { + uint64_t scanline_samples; + uint32_t scanline_width = td->td_imagewidth; + +#if 0 + // Tries to fix https://gitlab.com/libtiff/libtiff/-/merge_requests/564 + // but causes regression when decoding legit files with tiffcp -c none + // Cf https://gitlab.com/libtiff/libtiff/-/merge_requests/644 + if (td->td_photometric == PHOTOMETRIC_YCBCR) + { + uint16_t subsampling_hor; + uint16_t ignored; + TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, + &subsampling_hor, &ignored); + if (subsampling_hor > 1) // roundup width for YCbCr + scanline_width = + TIFFroundup_32(scanline_width, subsampling_hor); + } +#endif + + scanline_samples = _TIFFMultiply64(tif, scanline_width, + td->td_samplesperpixel, module); + scanline_size = + TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples, + td->td_bitspersample, module), + 8); + } + } + else + { + scanline_size = + TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth, + td->td_bitspersample, module), + 8); + } + if (scanline_size == 0) + { + TIFFErrorExtR(tif, module, "Computed scanline size is zero"); + return 0; + } + return (scanline_size); +} +tmsize_t TIFFScanlineSize(TIFF *tif) +{ + static const char module[] = "TIFFScanlineSize"; + uint64_t m; + m = TIFFScanlineSize64(tif); + return _TIFFCastUInt64ToSSize(tif, m, module); +} + +/* + * Return the number of bytes required to store a complete + * decoded and packed raster scanline (as opposed to the + * I/O size returned by TIFFScanlineSize which may be less + * if data is store as separate planes). + */ +uint64_t TIFFRasterScanlineSize64(TIFF *tif) +{ + static const char module[] = "TIFFRasterScanlineSize64"; + TIFFDirectory *td = &tif->tif_dir; + uint64_t scanline; + + scanline = + _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module); + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + scanline = + _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module); + return (TIFFhowmany8_64(scanline)); + } + else + return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline), + td->td_samplesperpixel, module)); +} +tmsize_t TIFFRasterScanlineSize(TIFF *tif) +{ + static const char module[] = "TIFFRasterScanlineSize"; + uint64_t m; + m = TIFFRasterScanlineSize64(tif); + return _TIFFCastUInt64ToSSize(tif, m, module); +} diff --git a/cpp/3rd_party/libtiff/tif_swab.c b/cpp/3rd_party/libtiff/tif_swab.c new file mode 100644 index 0000000000..827b025ce7 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_swab.c @@ -0,0 +1,329 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library Bit & Byte Swapping Support. + * + * XXX We assume short = 16-bits and long = 32-bits XXX + */ +#include "tiffiop.h" + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabShort) +void TIFFSwabShort(uint16_t *wp) +{ + register unsigned char *cp = (unsigned char *)wp; + unsigned char t; + assert(sizeof(uint16_t) == 2); + t = cp[1]; + cp[1] = cp[0]; + cp[0] = t; +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabLong) +void TIFFSwabLong(uint32_t *lp) +{ + register unsigned char *cp = (unsigned char *)lp; + unsigned char t; + assert(sizeof(uint32_t) == 4); + t = cp[3]; + cp[3] = cp[0]; + cp[0] = t; + t = cp[2]; + cp[2] = cp[1]; + cp[1] = t; +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabLong8) +void TIFFSwabLong8(uint64_t *lp) +{ + register unsigned char *cp = (unsigned char *)lp; + unsigned char t; + assert(sizeof(uint64_t) == 8); + t = cp[7]; + cp[7] = cp[0]; + cp[0] = t; + t = cp[6]; + cp[6] = cp[1]; + cp[1] = t; + t = cp[5]; + cp[5] = cp[2]; + cp[2] = t; + t = cp[4]; + cp[4] = cp[3]; + cp[3] = t; +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfShort) +void TIFFSwabArrayOfShort(register uint16_t *wp, tmsize_t n) +{ + register unsigned char *cp; + register unsigned char t; + assert(sizeof(uint16_t) == 2); + /* XXX unroll loop some */ + while (n-- > 0) + { + cp = (unsigned char *)wp; + t = cp[1]; + cp[1] = cp[0]; + cp[0] = t; + wp++; + } +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfTriples) +void TIFFSwabArrayOfTriples(register uint8_t *tp, tmsize_t n) +{ + unsigned char *cp; + unsigned char t; + + /* XXX unroll loop some */ + while (n-- > 0) + { + cp = (unsigned char *)tp; + t = cp[2]; + cp[2] = cp[0]; + cp[0] = t; + tp += 3; + } +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfLong) +void TIFFSwabArrayOfLong(register uint32_t *lp, tmsize_t n) +{ + register unsigned char *cp; + register unsigned char t; + assert(sizeof(uint32_t) == 4); + /* XXX unroll loop some */ + while (n-- > 0) + { + cp = (unsigned char *)lp; + t = cp[3]; + cp[3] = cp[0]; + cp[0] = t; + t = cp[2]; + cp[2] = cp[1]; + cp[1] = t; + lp++; + } +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfLong8) +void TIFFSwabArrayOfLong8(register uint64_t *lp, tmsize_t n) +{ + register unsigned char *cp; + register unsigned char t; + assert(sizeof(uint64_t) == 8); + /* XXX unroll loop some */ + while (n-- > 0) + { + cp = (unsigned char *)lp; + t = cp[7]; + cp[7] = cp[0]; + cp[0] = t; + t = cp[6]; + cp[6] = cp[1]; + cp[1] = t; + t = cp[5]; + cp[5] = cp[2]; + cp[2] = t; + t = cp[4]; + cp[4] = cp[3]; + cp[3] = t; + lp++; + } +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabFloat) +void TIFFSwabFloat(float *fp) +{ + register unsigned char *cp = (unsigned char *)fp; + unsigned char t; + assert(sizeof(float) == 4); + t = cp[3]; + cp[3] = cp[0]; + cp[0] = t; + t = cp[2]; + cp[2] = cp[1]; + cp[1] = t; +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfFloat) +void TIFFSwabArrayOfFloat(register float *fp, tmsize_t n) +{ + register unsigned char *cp; + register unsigned char t; + assert(sizeof(float) == 4); + /* XXX unroll loop some */ + while (n-- > 0) + { + cp = (unsigned char *)fp; + t = cp[3]; + cp[3] = cp[0]; + cp[0] = t; + t = cp[2]; + cp[2] = cp[1]; + cp[1] = t; + fp++; + } +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabDouble) +void TIFFSwabDouble(double *dp) +{ + register unsigned char *cp = (unsigned char *)dp; + unsigned char t; + assert(sizeof(double) == 8); + t = cp[7]; + cp[7] = cp[0]; + cp[0] = t; + t = cp[6]; + cp[6] = cp[1]; + cp[1] = t; + t = cp[5]; + cp[5] = cp[2]; + cp[2] = t; + t = cp[4]; + cp[4] = cp[3]; + cp[3] = t; +} +#endif + +#if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfDouble) +void TIFFSwabArrayOfDouble(double *dp, tmsize_t n) +{ + register unsigned char *cp; + register unsigned char t; + assert(sizeof(double) == 8); + /* XXX unroll loop some */ + while (n-- > 0) + { + cp = (unsigned char *)dp; + t = cp[7]; + cp[7] = cp[0]; + cp[0] = t; + t = cp[6]; + cp[6] = cp[1]; + cp[1] = t; + t = cp[5]; + cp[5] = cp[2]; + cp[2] = t; + t = cp[4]; + cp[4] = cp[3]; + cp[3] = t; + dp++; + } +} +#endif + +/* + * Bit reversal tables. TIFFBitRevTable[] gives + * the bit reversed value of . Used in various + * places in the library when the FillOrder requires + * bit reversal of byte values (e.g. CCITT Fax 3 + * encoding/decoding). TIFFNoBitRevTable is provided + * for algorithms that want an equivalent table that + * do not reverse bit values. + */ +static const unsigned char TIFFBitRevTable[256] = { + 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, + 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, + 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, + 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, + 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, + 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, + 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, + 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, + 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, + 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, + 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, + 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, + 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, + 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, + 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, + 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, + 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, + 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, + 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, + 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, + 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, + 0x3f, 0xbf, 0x7f, 0xff}; +static const unsigned char TIFFNoBitRevTable[256] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, + 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, + 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, + 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, + 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, + 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, + 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, + 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, + 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, + 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, + 0xfc, 0xfd, 0xfe, 0xff, +}; + +const unsigned char *TIFFGetBitRevTable(int reversed) +{ + return (reversed ? TIFFBitRevTable : TIFFNoBitRevTable); +} + +void TIFFReverseBits(uint8_t *cp, tmsize_t n) +{ + for (; n > 8; n -= 8) + { + cp[0] = TIFFBitRevTable[cp[0]]; + cp[1] = TIFFBitRevTable[cp[1]]; + cp[2] = TIFFBitRevTable[cp[2]]; + cp[3] = TIFFBitRevTable[cp[3]]; + cp[4] = TIFFBitRevTable[cp[4]]; + cp[5] = TIFFBitRevTable[cp[5]]; + cp[6] = TIFFBitRevTable[cp[6]]; + cp[7] = TIFFBitRevTable[cp[7]]; + cp += 8; + } + while (n-- > 0) + { + *cp = TIFFBitRevTable[*cp]; + cp++; + } +} diff --git a/cpp/3rd_party/libtiff/tif_thunder.c b/cpp/3rd_party/libtiff/tif_thunder.c new file mode 100644 index 0000000000..bac0607dbe --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_thunder.c @@ -0,0 +1,202 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#include +#ifdef THUNDER_SUPPORT +/* + * TIFF Library. + * + * ThunderScan 4-bit Compression Algorithm Support + */ + +/* + * ThunderScan uses an encoding scheme designed for + * 4-bit pixel values. Data is encoded in bytes, with + * each byte split into a 2-bit code word and a 6-bit + * data value. The encoding gives raw data, runs of + * pixels, or pixel values encoded as a delta from the + * previous pixel value. For the latter, either 2-bit + * or 3-bit delta values are used, with the deltas packed + * into a single byte. + */ +#define THUNDER_DATA 0x3f /* mask for 6-bit data */ +#define THUNDER_CODE 0xc0 /* mask for 2-bit code word */ +/* code values */ +#define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */ +#define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */ +#define DELTA2_SKIP 2 /* skip code for 2-bit deltas */ +#define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */ +#define DELTA3_SKIP 4 /* skip code for 3-bit deltas */ +#define THUNDER_RAW 0xc0 /* raw data encoded */ + +static const int twobitdeltas[4] = {0, 1, 0, -1}; +static const int threebitdeltas[8] = {0, 1, 2, 3, 0, -3, -2, -1}; + +#define SETPIXEL(op, v) \ + { \ + lastpixel = (v)&0xf; \ + if (npixels < maxpixels) \ + { \ + if (npixels++ & 1) \ + *op++ |= lastpixel; \ + else \ + op[0] = (uint8_t)(lastpixel << 4); \ + } \ + } + +static int ThunderSetupDecode(TIFF *tif) +{ + static const char module[] = "ThunderSetupDecode"; + + if (tif->tif_dir.td_bitspersample != 4) + { + TIFFErrorExtR(tif, module, + "Wrong bitspersample value (%d), Thunder decoder only " + "supports 4bits per sample.", + (int)tif->tif_dir.td_bitspersample); + return 0; + } + + return (1); +} + +static int ThunderDecode(TIFF *tif, uint8_t *op0, tmsize_t maxpixels) +{ + static const char module[] = "ThunderDecode"; + register unsigned char *bp; + register tmsize_t cc; + unsigned int lastpixel; + tmsize_t npixels; + uint8_t *op = op0; + + bp = (unsigned char *)tif->tif_rawcp; + cc = tif->tif_rawcc; + lastpixel = 0; + npixels = 0; + while (cc > 0 && npixels < maxpixels) + { + int n, delta; + + n = *bp++; + cc--; + switch (n & THUNDER_CODE) + { + case THUNDER_RUN: /* pixel run */ + /* + * Replicate the last pixel n times, + * where n is the lower-order 6 bits. + */ + if (n == 0) + break; + if (npixels & 1) + { + op[0] |= lastpixel; + lastpixel = *op++; + npixels++; + n--; + } + else + lastpixel |= lastpixel << 4; + npixels += n; + if (npixels > maxpixels) + break; + for (; n > 0; n -= 2) + *op++ = (uint8_t)lastpixel; + if (n == -1) + *--op &= 0xf0; + lastpixel &= 0xf; + break; + case THUNDER_2BITDELTAS: /* 2-bit deltas */ + if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP) + SETPIXEL(op, + (unsigned)((int)lastpixel + twobitdeltas[delta])); + if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP) + SETPIXEL(op, + (unsigned)((int)lastpixel + twobitdeltas[delta])); + if ((delta = (n & 3)) != DELTA2_SKIP) + SETPIXEL(op, + (unsigned)((int)lastpixel + twobitdeltas[delta])); + break; + case THUNDER_3BITDELTAS: /* 3-bit deltas */ + if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP) + SETPIXEL( + op, (unsigned)((int)lastpixel + threebitdeltas[delta])); + if ((delta = (n & 7)) != DELTA3_SKIP) + SETPIXEL( + op, (unsigned)((int)lastpixel + threebitdeltas[delta])); + break; + case THUNDER_RAW: /* raw data */ + SETPIXEL(op, n); + break; + } + } + tif->tif_rawcp = (uint8_t *)bp; + tif->tif_rawcc = cc; + if (npixels != maxpixels) + { + uint8_t *op_end = op0 + (maxpixels + 1) / 2; + memset(op, 0, (size_t)(op_end - op)); + TIFFErrorExtR(tif, module, + "%s data at scanline %lu (%" PRIu64 " != %" PRIu64 ")", + npixels < maxpixels ? "Not enough" : "Too much", + (unsigned long)tif->tif_row, (uint64_t)npixels, + (uint64_t)maxpixels); + return (0); + } + + return (1); +} + +static int ThunderDecodeRow(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) +{ + static const char module[] = "ThunderDecodeRow"; + uint8_t *row = buf; + + (void)s; + if (occ % tif->tif_scanlinesize) + { + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return (0); + } + while (occ > 0) + { + if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth)) + return (0); + occ -= tif->tif_scanlinesize; + row += tif->tif_scanlinesize; + } + return (1); +} + +int TIFFInitThunderScan(TIFF *tif, int scheme) +{ + (void)scheme; + + tif->tif_setupdecode = ThunderSetupDecode; + tif->tif_decoderow = ThunderDecodeRow; + tif->tif_decodestrip = ThunderDecodeRow; + return (1); +} +#endif /* THUNDER_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_tile.c b/cpp/3rd_party/libtiff/tif_tile.c new file mode 100644 index 0000000000..f07032f731 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_tile.c @@ -0,0 +1,284 @@ +/* + * Copyright (c) 1991-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Tiled Image Support Routines. + */ +#include "tiffiop.h" + +/* + * Compute which tile an (x,y,z,s) value is in. + */ +uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, + uint16_t s) +{ + TIFFDirectory *td = &tif->tif_dir; + uint32_t dx = td->td_tilewidth; + uint32_t dy = td->td_tilelength; + uint32_t dz = td->td_tiledepth; + uint32_t tile = 1; + + if (td->td_imagedepth == 1) + z = 0; + if (dx == (uint32_t)-1) + dx = td->td_imagewidth; + if (dy == (uint32_t)-1) + dy = td->td_imagelength; + if (dz == (uint32_t)-1) + dz = td->td_imagedepth; + if (dx != 0 && dy != 0 && dz != 0) + { + uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx); + uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy); + uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz); + + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + tile = (xpt * ypt * zpt) * s + (xpt * ypt) * (z / dz) + + xpt * (y / dy) + x / dx; + else + tile = (xpt * ypt) * (z / dz) + xpt * (y / dy) + x / dx; + } + return (tile); +} + +/* + * Check an (x,y,z,s) coordinate + * against the image bounds. + */ +int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s) +{ + TIFFDirectory *td = &tif->tif_dir; + + if (x >= td->td_imagewidth) + { + TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu", + (unsigned long)x, (unsigned long)(td->td_imagewidth - 1)); + return (0); + } + if (y >= td->td_imagelength) + { + TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu", + (unsigned long)y, + (unsigned long)(td->td_imagelength - 1)); + return (0); + } + if (z >= td->td_imagedepth) + { + TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu", + (unsigned long)z, (unsigned long)(td->td_imagedepth - 1)); + return (0); + } + if (td->td_planarconfig == PLANARCONFIG_SEPARATE && + s >= td->td_samplesperpixel) + { + TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu", + (unsigned long)s, + (unsigned long)(td->td_samplesperpixel - 1)); + return (0); + } + return (1); +} + +/* + * Compute how many tiles are in an image. + */ +uint32_t TIFFNumberOfTiles(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + uint32_t dx = td->td_tilewidth; + uint32_t dy = td->td_tilelength; + uint32_t dz = td->td_tiledepth; + uint32_t ntiles; + + if (dx == (uint32_t)-1) + dx = td->td_imagewidth; + if (dy == (uint32_t)-1) + dy = td->td_imagelength; + if (dz == (uint32_t)-1) + dz = td->td_imagedepth; + ntiles = + (dx == 0 || dy == 0 || dz == 0) + ? 0 + : _TIFFMultiply32( + tif, + _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx), + TIFFhowmany_32(td->td_imagelength, dy), + "TIFFNumberOfTiles"), + TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles"); + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel, + "TIFFNumberOfTiles"); + return (ntiles); +} + +/* + * Compute the # bytes in each row of a tile. + */ +uint64_t TIFFTileRowSize64(TIFF *tif) +{ + static const char module[] = "TIFFTileRowSize64"; + TIFFDirectory *td = &tif->tif_dir; + uint64_t rowsize; + uint64_t tilerowsize; + + if (td->td_tilelength == 0) + { + TIFFErrorExtR(tif, module, "Tile length is zero"); + return 0; + } + if (td->td_tilewidth == 0) + { + TIFFErrorExtR(tif, module, "Tile width is zero"); + return (0); + } + rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth, + "TIFFTileRowSize"); + if (td->td_planarconfig == PLANARCONFIG_CONTIG) + { + if (td->td_samplesperpixel == 0) + { + TIFFErrorExtR(tif, module, "Samples per pixel is zero"); + return 0; + } + rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel, + "TIFFTileRowSize"); + } + tilerowsize = TIFFhowmany8_64(rowsize); + if (tilerowsize == 0) + { + TIFFErrorExtR(tif, module, "Computed tile row size is zero"); + return 0; + } + return (tilerowsize); +} +tmsize_t TIFFTileRowSize(TIFF *tif) +{ + static const char module[] = "TIFFTileRowSize"; + uint64_t m; + m = TIFFTileRowSize64(tif); + return _TIFFCastUInt64ToSSize(tif, m, module); +} + +/* + * Compute the # bytes in a variable length, row-aligned tile. + */ +uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows) +{ + static const char module[] = "TIFFVTileSize64"; + TIFFDirectory *td = &tif->tif_dir; + if (td->td_tilelength == 0 || td->td_tilewidth == 0 || + td->td_tiledepth == 0) + return (0); + if ((td->td_planarconfig == PLANARCONFIG_CONTIG) && + (td->td_photometric == PHOTOMETRIC_YCBCR) && + (td->td_samplesperpixel == 3) && (!isUpSampled(tif))) + { + /* + * Packed YCbCr data contain one Cb+Cr for every + * HorizontalSampling*VerticalSampling Y values. + * Must also roundup width and height when calculating + * since images that are not a multiple of the + * horizontal/vertical subsampling area include + * YCbCr data for the extended image. + */ + uint16_t ycbcrsubsampling[2]; + uint16_t samplingblock_samples; + uint32_t samplingblocks_hor; + uint32_t samplingblocks_ver; + uint64_t samplingrow_samples; + uint64_t samplingrow_size; + TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, + ycbcrsubsampling + 0, ycbcrsubsampling + 1); + if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && + ycbcrsubsampling[0] != 4) || + (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && + ycbcrsubsampling[1] != 4)) + { + TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)", + ycbcrsubsampling[0], ycbcrsubsampling[1]); + return 0; + } + samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2; + samplingblocks_hor = + TIFFhowmany_32(td->td_tilewidth, ycbcrsubsampling[0]); + samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]); + samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor, + samplingblock_samples, module); + samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64( + tif, samplingrow_samples, td->td_bitspersample, module)); + return ( + _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module)); + } + else + return (_TIFFMultiply64(tif, nrows, TIFFTileRowSize64(tif), module)); +} +tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows) +{ + static const char module[] = "TIFFVTileSize"; + uint64_t m; + m = TIFFVTileSize64(tif, nrows); + return _TIFFCastUInt64ToSSize(tif, m, module); +} + +/* + * Compute the # bytes in a row-aligned tile. + */ +uint64_t TIFFTileSize64(TIFF *tif) +{ + return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength)); +} +tmsize_t TIFFTileSize(TIFF *tif) +{ + static const char module[] = "TIFFTileSize"; + uint64_t m; + m = TIFFTileSize64(tif); + return _TIFFCastUInt64ToSSize(tif, m, module); +} + +/* + * Compute a default tile size based on the image + * characteristics and a requested value. If a + * request is <1 then we choose a size according + * to certain heuristics. + */ +void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th) +{ + (*tif->tif_deftilesize)(tif, tw, th); +} + +void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th) +{ + (void)tif; + if (*(int32_t *)tw < 1) + *tw = 256; + if (*(int32_t *)th < 1) + *th = 256; + /* roundup to a multiple of 16 per the spec */ + if (*tw & 0xf) + *tw = TIFFroundup_32(*tw, 16); + if (*th & 0xf) + *th = TIFFroundup_32(*th, 16); +} diff --git a/cpp/3rd_party/libtiff/tif_unix.c b/cpp/3rd_party/libtiff/tif_unix.c new file mode 100644 index 0000000000..9e6fc75b08 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_unix.c @@ -0,0 +1,382 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library UNIX-specific Routines. These are should also work with the + * Windows Common RunTime Library. + */ + +#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS +#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS +#endif + +#include "tif_config.h" + +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +#include + +#include +#include +#include + +#ifdef HAVE_UNISTD_H +#include +#endif + +#ifdef HAVE_FCNTL_H +#include +#endif + +#ifdef HAVE_IO_H +#include +#endif + +#include "tiffiop.h" + +#define TIFF_IO_MAX 2147483647U + +typedef union fd_as_handle_union +{ + int fd; + thandle_t h; +} fd_as_handle_union_t; + +static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size) +{ + fd_as_handle_union_t fdh; + const size_t bytes_total = (size_t)size; + size_t bytes_read; + tmsize_t count = -1; + if ((tmsize_t)bytes_total != size) + { + errno = EINVAL; + return (tmsize_t)-1; + } + fdh.h = fd; + for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count) + { + char *buf_offset = (char *)buf + bytes_read; + size_t io_size = bytes_total - bytes_read; + if (io_size > TIFF_IO_MAX) + io_size = TIFF_IO_MAX; + /* Below is an obvious false positive of Coverity Scan */ + /* coverity[overflow_sink] */ + count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size); + if (count <= 0) + break; + } + if (count < 0) + return (tmsize_t)-1; + /* Silence Coverity Scan warning about unsigned to signed underflow. */ + /* coverity[return_overflow:SUPPRESS] */ + return (tmsize_t)bytes_read; +} + +static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size) +{ + fd_as_handle_union_t fdh; + const size_t bytes_total = (size_t)size; + size_t bytes_written; + tmsize_t count = -1; + if ((tmsize_t)bytes_total != size) + { + errno = EINVAL; + return (tmsize_t)-1; + } + fdh.h = fd; + for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count) + { + const char *buf_offset = (char *)buf + bytes_written; + size_t io_size = bytes_total - bytes_written; + if (io_size > TIFF_IO_MAX) + io_size = TIFF_IO_MAX; + /* Below is an obvious false positive of Coverity Scan */ + /* coverity[overflow_sink] */ + count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size); + if (count <= 0) + break; + } + if (count < 0) + return (tmsize_t)-1; + /* Silence Coverity Scan warning about unsigned to signed underflow. */ + /* coverity[return_overflow:SUPPRESS] */ + return (tmsize_t)bytes_written; + /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */ +} + +static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence) +{ + fd_as_handle_union_t fdh; + _TIFF_off_t off_io = (_TIFF_off_t)off; + if ((uint64_t)off_io != off) + { + errno = EINVAL; + return (uint64_t)-1; /* this is really gross */ + } + fdh.h = fd; + return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence)); +} + +static int _tiffCloseProc(thandle_t fd) +{ + fd_as_handle_union_t fdh; + fdh.h = fd; + return (close(fdh.fd)); +} + +static uint64_t _tiffSizeProc(thandle_t fd) +{ + _TIFF_stat_s sb; + fd_as_handle_union_t fdh; + fdh.h = fd; + if (_TIFF_fstat_f(fdh.fd, &sb) < 0) + return (0); + else + return ((uint64_t)sb.st_size); +} + +#ifdef HAVE_MMAP +#include + +static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize) +{ + uint64_t size64 = _tiffSizeProc(fd); + tmsize_t sizem = (tmsize_t)size64; + if (size64 && (uint64_t)sizem == size64) + { + fd_as_handle_union_t fdh; + fdh.h = fd; + *pbase = + (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0); + if (*pbase != (void *)-1) + { + *psize = (tmsize_t)sizem; + return (1); + } + } + return (0); +} + +static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size) +{ + (void)fd; + (void)munmap(base, (off_t)size); +} +#else /* !HAVE_MMAP */ +static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize) +{ + (void)fd; + (void)pbase; + (void)psize; + return (0); +} + +static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size) +{ + (void)fd; + (void)base; + (void)size; +} +#endif /* !HAVE_MMAP */ + +/* + * Open a TIFF file descriptor for read/writing. + */ +TIFF *TIFFFdOpen(int fd, const char *name, const char *mode) +{ + return TIFFFdOpenExt(fd, name, mode, NULL); +} + +TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode, + TIFFOpenOptions *opts) +{ + TIFF *tif; + + fd_as_handle_union_t fdh; + fdh.fd = fd; + tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc, + _tiffSeekProc, _tiffCloseProc, _tiffSizeProc, + _tiffMapProc, _tiffUnmapProc, opts); + if (tif) + tif->tif_fd = fd; + return (tif); +} + +/* + * Open a TIFF file for read/writing. + */ +TIFF *TIFFOpen(const char *name, const char *mode) +{ + return TIFFOpenExt(name, mode, NULL); +} + +TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts) +{ + static const char module[] = "TIFFOpen"; + int m, fd; + TIFF *tif; + + m = _TIFFgetMode(opts, NULL, mode, module); + if (m == -1) + return ((TIFF *)0); + +/* for cygwin and mingw */ +#ifdef O_BINARY + m |= O_BINARY; +#endif + + fd = open(name, m, 0666); + if (fd < 0) + { + if (errno > 0 && strerror(errno) != NULL) + { + _TIFFErrorEarly(opts, NULL, module, "%s: %s", name, + strerror(errno)); + } + else + { + _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name); + } + return ((TIFF *)0); + } + + tif = TIFFFdOpenExt((int)fd, name, mode, opts); + if (!tif) + close(fd); + return tif; +} + +#ifdef _WIN32 +#include +/* + * Open a TIFF file with a Unicode filename, for read/writing. + */ +TIFF *TIFFOpenW(const wchar_t *name, const char *mode) +{ + return TIFFOpenWExt(name, mode, NULL); +} +TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts) +{ + static const char module[] = "TIFFOpenW"; + int m, fd; + int mbsize; + char *mbname; + TIFF *tif; + + m = _TIFFgetMode(opts, NULL, mode, module); + if (m == -1) + return ((TIFF *)0); + +/* for cygwin and mingw */ +#ifdef O_BINARY + m |= O_BINARY; +#endif + + fd = _wopen(name, m, 0666); + if (fd < 0) + { + _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name); + return ((TIFF *)0); + } + + mbname = NULL; + mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL); + if (mbsize > 0) + { + mbname = _TIFFmalloc(mbsize); + if (!mbname) + { + _TIFFErrorEarly( + opts, NULL, module, + "Can't allocate space for filename conversion buffer"); + return ((TIFF *)0); + } + + WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL); + } + + tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "", mode, + opts); + + _TIFFfree(mbname); + + if (!tif) + close(fd); + return tif; +} +#endif + +void *_TIFFmalloc(tmsize_t s) +{ + if (s == 0) + return ((void *)NULL); + + return (malloc((size_t)s)); +} + +void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz) +{ + if (nmemb == 0 || siz == 0) + return ((void *)NULL); + + return calloc((size_t)nmemb, (size_t)siz); +} + +void _TIFFfree(void *p) { free(p); } + +void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); } + +void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); } + +void _TIFFmemcpy(void *d, const void *s, tmsize_t c) +{ + memcpy(d, s, (size_t)c); +} + +int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c) +{ + return (memcmp(p1, p2, (size_t)c)); +} + +static void unixWarningHandler(const char *module, const char *fmt, va_list ap) +{ + if (module != NULL) + fprintf(stderr, "%s: ", module); + fprintf(stderr, "Warning, "); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); +} +TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler; + +static void unixErrorHandler(const char *module, const char *fmt, va_list ap) +{ + if (module != NULL) + fprintf(stderr, "%s: ", module); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); +} +TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler; diff --git a/cpp/3rd_party/libtiff/tif_version.c b/cpp/3rd_party/libtiff/tif_version.c new file mode 100644 index 0000000000..0b6c9bc00a --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_version.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 1992-1997 Sam Leffler + * Copyright (c) 1992-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +#include "tiffiop.h" + +static const char TIFFVersion[] = TIFFLIB_VERSION_STR; + +const char *TIFFGetVersion(void) { return (TIFFVersion); } diff --git a/cpp/3rd_party/libtiff/tif_warning.c b/cpp/3rd_party/libtiff/tif_warning.c new file mode 100644 index 0000000000..5468de55f2 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_warning.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + */ +#include "tiffiop.h" + +TIFFErrorHandlerExt _TIFFwarningHandlerExt = NULL; + +TIFFErrorHandler TIFFSetWarningHandler(TIFFErrorHandler handler) +{ + TIFFErrorHandler prev = _TIFFwarningHandler; + _TIFFwarningHandler = handler; + return (prev); +} + +TIFFErrorHandlerExt TIFFSetWarningHandlerExt(TIFFErrorHandlerExt handler) +{ + TIFFErrorHandlerExt prev = _TIFFwarningHandlerExt; + _TIFFwarningHandlerExt = handler; + return (prev); +} + +void TIFFWarning(const char *module, const char *fmt, ...) +{ + va_list ap; + if (_TIFFwarningHandler) + { + va_start(ap, fmt); + (*_TIFFwarningHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFwarningHandlerExt) + { + va_start(ap, fmt); + (*_TIFFwarningHandlerExt)(0, module, fmt, ap); + va_end(ap); + } +} + +void TIFFWarningExt(thandle_t fd, const char *module, const char *fmt, ...) +{ + va_list ap; + if (_TIFFwarningHandler) + { + va_start(ap, fmt); + (*_TIFFwarningHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFwarningHandlerExt) + { + va_start(ap, fmt); + (*_TIFFwarningHandlerExt)(fd, module, fmt, ap); + va_end(ap); + } +} + +void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt, ...) +{ + va_list ap; + if (tif && tif->tif_warnhandler) + { + va_start(ap, fmt); + int stop = (*tif->tif_warnhandler)(tif, tif->tif_warnhandler_user_data, + module, fmt, ap); + va_end(ap); + if (stop) + return; + } + if (_TIFFwarningHandler) + { + va_start(ap, fmt); + (*_TIFFwarningHandler)(module, fmt, ap); + va_end(ap); + } + if (_TIFFwarningHandlerExt) + { + va_start(ap, fmt); + (*_TIFFwarningHandlerExt)(tif ? tif->tif_clientdata : 0, module, fmt, + ap); + va_end(ap); + } +} diff --git a/cpp/3rd_party/libtiff/tif_webp.c b/cpp/3rd_party/libtiff/tif_webp.c new file mode 100644 index 0000000000..c06ce163b7 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_webp.c @@ -0,0 +1,917 @@ +/* + * Copyright (c) 2018, Mapbox + * Author: + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef WEBP_SUPPORT +/* + * TIFF Library. + * + * WEBP Compression Support + * + */ + +#include "webp/decode.h" +#include "webp/encode.h" + +#include +#include + +#define LSTATE_INIT_DECODE 0x01 +#define LSTATE_INIT_ENCODE 0x02 +/* + * State block for each open TIFF + * file using WEBP compression/decompression. + */ +typedef struct +{ + uint16_t nSamples; /* number of samples per pixel */ + + int read_error; /* whether a read error has occurred, and which should cause + further reads in the same strip/tile to be aborted */ + int lossless; /* lossy/lossless compression */ + int lossless_exact; /* lossless exact mode. If TRUE, R,G,B values in areas + with alpha = 0 will be preserved */ + int quality_level; /* compression level */ + WebPPicture sPicture; /* WebP Picture */ + WebPConfig sEncoderConfig; /* WebP encoder config */ + uint8_t *pBuffer; /* buffer to hold raw data on encoding */ + unsigned int buffer_offset; /* current offset into the buffer */ + unsigned int buffer_size; + + WebPIDecoder *psDecoder; /* WebPIDecoder */ + WebPDecBuffer sDecBuffer; /* Decoder buffer */ + int last_y; /* Last row decoded */ + + int state; /* state flags */ + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ +} WebPState; + +#define LState(tif) ((WebPState *)(tif)->tif_data) +#define DecoderState(tif) LState(tif) +#define EncoderState(tif) LState(tif) + +static int TWebPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); +static int TWebPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); + +static int TWebPDatasetWriter(const uint8_t *data, size_t data_size, + const WebPPicture *const picture) +{ + static const char module[] = "TWebPDatasetWriter"; + TIFF *tif = (TIFF *)(picture->custom_ptr); + + if ((tif->tif_rawcc + (tmsize_t)data_size) > tif->tif_rawdatasize) + { + TIFFErrorExtR( + tif, module, "Buffer too small by %" TIFF_SIZE_FORMAT " bytes.", + (size_t)(tif->tif_rawcc + data_size - tif->tif_rawdatasize)); + return 0; + } + else + { + _TIFFmemcpy(tif->tif_rawcp, data, data_size); + tif->tif_rawcc += data_size; + tif->tif_rawcp += data_size; + return 1; + } +} + +/* + * Encode a chunk of pixels. + */ +static int TWebPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "TWebPEncode"; + WebPState *sp = EncoderState(tif); + (void)s; + + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_ENCODE); + + if ((uint64_t)sp->buffer_offset + (uint64_t)cc > sp->buffer_size) + { + TIFFErrorExtR(tif, module, "Too many bytes to be written"); + return 0; + } + + memcpy(sp->pBuffer + sp->buffer_offset, bp, cc); + sp->buffer_offset += (unsigned)cc; + + return 1; +} + +static int TWebPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "WebPDecode"; + VP8StatusCode status = VP8_STATUS_OK; + WebPState *sp = DecoderState(tif); + uint32_t segment_width, segment_height; + bool decode_whole_strile = false; + + (void)s; + + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_DECODE); + + if (sp->read_error) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, + "ZIPDecode: Scanline %" PRIu32 " cannot be read due to " + "previous error", + tif->tif_row); + return 0; + } + + if (sp->psDecoder == NULL) + { + TIFFDirectory *td = &tif->tif_dir; + uint32_t buffer_size; + + if (isTiled(tif)) + { + segment_width = td->td_tilewidth; + segment_height = td->td_tilelength; + } + else + { + segment_width = td->td_imagewidth; + segment_height = td->td_imagelength - tif->tif_row; + if (segment_height > td->td_rowsperstrip) + segment_height = td->td_rowsperstrip; + } + + int webp_width, webp_height; + if (!WebPGetInfo(tif->tif_rawcp, + (uint64_t)tif->tif_rawcc > UINT32_MAX + ? UINT32_MAX + : (uint32_t)tif->tif_rawcc, + &webp_width, &webp_height)) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, "WebPGetInfo() failed"); + return 0; + } + if ((uint32_t)webp_width != segment_width || + (uint32_t)webp_height != segment_height) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR( + tif, module, "WebP blob dimension is %dx%d. Expected %ux%u", + webp_width, webp_height, segment_width, segment_height); + return 0; + } + +#if WEBP_DECODER_ABI_VERSION >= 0x0002 + WebPDecoderConfig config; + if (!WebPInitDecoderConfig(&config)) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, "WebPInitDecoderConfig() failed"); + return 0; + } + + const bool bWebPGetFeaturesOK = + WebPGetFeatures(tif->tif_rawcp, + (uint64_t)tif->tif_rawcc > UINT32_MAX + ? UINT32_MAX + : (uint32_t)tif->tif_rawcc, + &config.input) == VP8_STATUS_OK; + + WebPFreeDecBuffer(&config.output); + + if (!bWebPGetFeaturesOK) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, "WebPInitDecoderConfig() failed"); + return 0; + } + + const int webp_bands = config.input.has_alpha ? 4 : 3; + if (webp_bands != sp->nSamples && + /* We accept the situation where the WebP blob has only 3 bands, + * whereas the raster is 4 bands. This can happen when the alpha + * channel is fully opaque, and WebP decoding works fine in that + * situation. + */ + !(webp_bands == 3 && sp->nSamples == 4)) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, + "WebP blob band count is %d. Expected %d", webp_bands, + sp->nSamples); + return 0; + } +#endif + + buffer_size = segment_width * segment_height * sp->nSamples; + if (occ == (tmsize_t)buffer_size) + { + /* If decoding the whole strip/tile, we can directly use the */ + /* output buffer */ + decode_whole_strile = true; + } + else if (sp->pBuffer == NULL || buffer_size > sp->buffer_size) + { + if (sp->pBuffer != NULL) + { + _TIFFfreeExt(tif, sp->pBuffer); + sp->pBuffer = NULL; + } + + sp->pBuffer = _TIFFmallocExt(tif, buffer_size); + if (!sp->pBuffer) + { + TIFFErrorExtR(tif, module, "Cannot allocate buffer"); + memset(op, 0, (size_t)occ); + sp->read_error = 1; + return 0; + } + sp->buffer_size = buffer_size; + } + + sp->last_y = 0; + + WebPInitDecBuffer(&sp->sDecBuffer); + + sp->sDecBuffer.is_external_memory = 1; + sp->sDecBuffer.width = segment_width; + sp->sDecBuffer.height = segment_height; + sp->sDecBuffer.u.RGBA.rgba = decode_whole_strile ? op : sp->pBuffer; + sp->sDecBuffer.u.RGBA.stride = segment_width * sp->nSamples; + sp->sDecBuffer.u.RGBA.size = buffer_size; + + if (sp->nSamples > 3) + { + sp->sDecBuffer.colorspace = MODE_RGBA; + } + else + { + sp->sDecBuffer.colorspace = MODE_RGB; + } + + sp->psDecoder = WebPINewDecoder(&sp->sDecBuffer); + + if (sp->psDecoder == NULL) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, "Unable to allocate WebP decoder."); + return 0; + } + } + + if (occ % sp->sDecBuffer.u.RGBA.stride) + { + // read_error not set here as this is a usage issue that can be + // recovered in a following call. + memset(op, 0, (size_t)occ); + /* Do not set read_error as could potentially be recovered */ + TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); + return 0; + } + + status = WebPIAppend(sp->psDecoder, tif->tif_rawcp, tif->tif_rawcc); + + if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) + { + if (status == VP8_STATUS_INVALID_PARAM) + { + TIFFErrorExtR(tif, module, "Invalid parameter used."); + } + else if (status == VP8_STATUS_OUT_OF_MEMORY) + { + TIFFErrorExtR(tif, module, "Out of memory."); + } + else + { + TIFFErrorExtR(tif, module, "Unrecognized error."); + } + memset(op, 0, (size_t)occ); + sp->read_error = 1; + return 0; + } + else + { + int current_y, stride; + uint8_t *buf; + + /* Returns the RGB/A image decoded so far */ + buf = WebPIDecGetRGB(sp->psDecoder, ¤t_y, NULL, NULL, &stride); + + if ((buf != NULL) && + (occ <= (tmsize_t)stride * (current_y - sp->last_y))) + { + const int numberOfExpectedLines = + (int)(occ / sp->sDecBuffer.u.RGBA.stride); + if (decode_whole_strile) + { + if (current_y != numberOfExpectedLines) + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, + "Unable to decode WebP data: less lines than " + "expected."); + return 0; + } + } + else + { + memcpy(op, buf + (sp->last_y * stride), occ); + } + + tif->tif_rawcp += tif->tif_rawcc; + tif->tif_rawcc = 0; + sp->last_y += numberOfExpectedLines; + + if (decode_whole_strile) + { + /* We can now free the decoder as we're completely done */ + if (sp->psDecoder != NULL) + { + WebPIDelete(sp->psDecoder); + WebPFreeDecBuffer(&sp->sDecBuffer); + sp->psDecoder = NULL; + } + } + return 1; + } + else + { + memset(op, 0, (size_t)occ); + sp->read_error = 1; + TIFFErrorExtR(tif, module, "Unable to decode WebP data."); + return 0; + } + } +} + +static int TWebPFixupTags(TIFF *tif) +{ + (void)tif; + if (tif->tif_dir.td_planarconfig != PLANARCONFIG_CONTIG) + { + static const char module[] = "TWebPFixupTags"; + TIFFErrorExtR(tif, module, + "TIFF WEBP requires data to be stored contiguously in " + "RGB e.g. RGBRGBRGB " +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + "or RGBARGBARGBA" +#endif + ); + return 0; + } + return 1; +} + +static int TWebPSetupDecode(TIFF *tif) +{ + static const char module[] = "WebPSetupDecode"; + uint16_t nBitsPerSample = tif->tif_dir.td_bitspersample; + uint16_t sampleFormat = tif->tif_dir.td_sampleformat; + + WebPState *sp = DecoderState(tif); + assert(sp != NULL); + + sp->nSamples = tif->tif_dir.td_samplesperpixel; + + /* check band count */ + if (sp->nSamples != 3 +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + && sp->nSamples != 4 +#endif + ) + { + TIFFErrorExtR(tif, module, + "WEBP driver doesn't support %d bands. Must be 3 (RGB) " +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + "or 4 (RGBA) " +#endif + "bands.", + sp->nSamples); + return 0; + } + + /* check bits per sample and data type */ + if ((nBitsPerSample != 8) && (sampleFormat != 1)) + { + TIFFErrorExtR(tif, module, "WEBP driver requires 8 bit unsigned data"); + return 0; + } + + /* if we were last encoding, terminate this mode */ + if (sp->state & LSTATE_INIT_ENCODE) + { + WebPPictureFree(&sp->sPicture); + if (sp->pBuffer != NULL) + { + _TIFFfreeExt(tif, sp->pBuffer); + sp->pBuffer = NULL; + } + sp->buffer_offset = 0; + sp->state = 0; + } + + sp->state |= LSTATE_INIT_DECODE; + + return 1; +} + +/* + * Setup state for decoding a strip. + */ +static int TWebPPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "TWebPPreDecode"; + uint32_t segment_width, segment_height; + WebPState *sp = DecoderState(tif); + TIFFDirectory *td = &tif->tif_dir; + (void)s; + assert(sp != NULL); + + if (isTiled(tif)) + { + segment_width = td->td_tilewidth; + segment_height = td->td_tilelength; + } + else + { + segment_width = td->td_imagewidth; + segment_height = td->td_imagelength - tif->tif_row; + if (segment_height > td->td_rowsperstrip) + segment_height = td->td_rowsperstrip; + } + + if (segment_width > 16383 || segment_height > 16383) + { + TIFFErrorExtR(tif, module, + "WEBP maximum image dimensions are 16383 x 16383."); + return 0; + } + + if ((sp->state & LSTATE_INIT_DECODE) == 0) + tif->tif_setupdecode(tif); + + if (sp->psDecoder != NULL) + { + WebPIDelete(sp->psDecoder); + WebPFreeDecBuffer(&sp->sDecBuffer); + sp->psDecoder = NULL; + } + + sp->read_error = 0; + + return 1; +} + +static int TWebPSetupEncode(TIFF *tif) +{ + static const char module[] = "WebPSetupEncode"; + uint16_t nBitsPerSample = tif->tif_dir.td_bitspersample; + uint16_t sampleFormat = tif->tif_dir.td_sampleformat; + + WebPState *sp = EncoderState(tif); + assert(sp != NULL); + + sp->nSamples = tif->tif_dir.td_samplesperpixel; + + /* check band count */ + if (sp->nSamples != 3 +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + && sp->nSamples != 4 +#endif + ) + { + TIFFErrorExtR(tif, module, + "WEBP driver doesn't support %d bands. Must be 3 (RGB) " +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + "or 4 (RGBA) " +#endif + "bands.", + sp->nSamples); + return 0; + } + + /* check bits per sample and data type */ + if ((nBitsPerSample != 8) || (sampleFormat != SAMPLEFORMAT_UINT)) + { + TIFFErrorExtR(tif, module, "WEBP driver requires 8 bit unsigned data"); + return 0; + } + + if (sp->state & LSTATE_INIT_DECODE) + { + WebPIDelete(sp->psDecoder); + WebPFreeDecBuffer(&sp->sDecBuffer); + sp->psDecoder = NULL; + sp->last_y = 0; + sp->state = 0; + } + + sp->state |= LSTATE_INIT_ENCODE; + + if (!WebPPictureInit(&sp->sPicture)) + { + TIFFErrorExtR(tif, module, "Error initializing WebP picture."); + return 0; + } + + if (!WebPConfigInitInternal(&sp->sEncoderConfig, WEBP_PRESET_DEFAULT, + (float)sp->quality_level, + WEBP_ENCODER_ABI_VERSION)) + { + TIFFErrorExtR(tif, module, + "Error creating WebP encoder configuration."); + return 0; + } + +// WebPConfigInitInternal above sets lossless to false +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + sp->sEncoderConfig.lossless = sp->lossless; + if (sp->lossless) + { + sp->sPicture.use_argb = 1; +#if WEBP_ENCODER_ABI_VERSION >= 0x0209 + sp->sEncoderConfig.exact = sp->lossless_exact; +#endif + } +#endif + + if (!WebPValidateConfig(&sp->sEncoderConfig)) + { + TIFFErrorExtR(tif, module, "Error with WebP encoder configuration."); + return 0; + } + + return 1; +} + +/* + * Reset encoding state at the start of a strip. + */ +static int TWebPPreEncode(TIFF *tif, uint16_t s) +{ + static const char module[] = "TWebPPreEncode"; + uint32_t segment_width, segment_height; + WebPState *sp = EncoderState(tif); + TIFFDirectory *td = &tif->tif_dir; + + (void)s; + + assert(sp != NULL); + if (sp->state != LSTATE_INIT_ENCODE) + tif->tif_setupencode(tif); + + /* + * Set encoding parameters for this strip/tile. + */ + if (isTiled(tif)) + { + segment_width = td->td_tilewidth; + segment_height = td->td_tilelength; + } + else + { + segment_width = td->td_imagewidth; + segment_height = td->td_imagelength - tif->tif_row; + if (segment_height > td->td_rowsperstrip) + segment_height = td->td_rowsperstrip; + } + + if (segment_width > 16383 || segment_height > 16383) + { + TIFFErrorExtR(tif, module, + "WEBP maximum image dimensions are 16383 x 16383."); + return 0; + } + + /* set up buffer for raw data */ + /* given above check and that nSamples <= 4, buffer_size is <= 1 GB */ + sp->buffer_size = segment_width * segment_height * sp->nSamples; + + if (sp->pBuffer != NULL) + { + _TIFFfreeExt(tif, sp->pBuffer); + sp->pBuffer = NULL; + } + + sp->pBuffer = _TIFFmallocExt(tif, sp->buffer_size); + if (!sp->pBuffer) + { + TIFFErrorExtR(tif, module, "Cannot allocate buffer"); + return 0; + } + sp->buffer_offset = 0; + + sp->sPicture.width = segment_width; + sp->sPicture.height = segment_height; + sp->sPicture.writer = TWebPDatasetWriter; + sp->sPicture.custom_ptr = tif; + + return 1; +} + +/* + * Finish off an encoded strip by flushing it. + */ +static int TWebPPostEncode(TIFF *tif) +{ + static const char module[] = "WebPPostEncode"; + int64_t stride; + WebPState *sp = EncoderState(tif); + assert(sp != NULL); + + assert(sp->state == LSTATE_INIT_ENCODE); + + stride = (int64_t)sp->sPicture.width * sp->nSamples; + +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + if (sp->nSamples == 4) + { + if (!WebPPictureImportRGBA(&sp->sPicture, sp->pBuffer, (int)stride)) + { + TIFFErrorExtR(tif, module, "WebPPictureImportRGBA() failed"); + return 0; + } + } + else +#endif + if (!WebPPictureImportRGB(&sp->sPicture, sp->pBuffer, (int)stride)) + { + TIFFErrorExtR(tif, module, "WebPPictureImportRGB() failed"); + return 0; + } + + if (!WebPEncode(&sp->sEncoderConfig, &sp->sPicture)) + { + +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + const char *pszErrorMsg = NULL; + switch (sp->sPicture.error_code) + { + case VP8_ENC_ERROR_OUT_OF_MEMORY: + pszErrorMsg = "Out of memory"; + break; + case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY: + pszErrorMsg = "Out of memory while flushing bits"; + break; + case VP8_ENC_ERROR_NULL_PARAMETER: + pszErrorMsg = "A pointer parameter is NULL"; + break; + case VP8_ENC_ERROR_INVALID_CONFIGURATION: + pszErrorMsg = "Configuration is invalid"; + break; + case VP8_ENC_ERROR_BAD_DIMENSION: + pszErrorMsg = "Picture has invalid width/height"; + break; + case VP8_ENC_ERROR_PARTITION0_OVERFLOW: + pszErrorMsg = "Partition is bigger than 512k. Try using less " + "SEGMENTS, or increase PARTITION_LIMIT value"; + break; + case VP8_ENC_ERROR_PARTITION_OVERFLOW: + pszErrorMsg = "Partition is bigger than 16M"; + break; + case VP8_ENC_ERROR_BAD_WRITE: + pszErrorMsg = "Error while fludshing bytes"; + break; + case VP8_ENC_ERROR_FILE_TOO_BIG: + pszErrorMsg = "File is bigger than 4G"; + break; + case VP8_ENC_ERROR_USER_ABORT: + pszErrorMsg = "User interrupted"; + break; + default: + TIFFErrorExtR(tif, module, + "WebPEncode returned an unknown error code: %d", + sp->sPicture.error_code); + pszErrorMsg = "Unknown WebP error type."; + break; + } + TIFFErrorExtR(tif, module, "WebPEncode() failed : %s", pszErrorMsg); +#else + TIFFErrorExtR(tif, module, "Error in WebPEncode()"); +#endif + return 0; + } + + sp->sPicture.custom_ptr = NULL; + + if (!TIFFFlushData1(tif)) + { + TIFFErrorExtR(tif, module, "Error flushing TIFF WebP encoder."); + return 0; + } + + return 1; +} + +static void TWebPCleanup(TIFF *tif) +{ + WebPState *sp = LState(tif); + + assert(sp != 0); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + if (sp->state & LSTATE_INIT_ENCODE) + { + WebPPictureFree(&sp->sPicture); + } + + if (sp->psDecoder != NULL) + { + WebPIDelete(sp->psDecoder); + WebPFreeDecBuffer(&sp->sDecBuffer); + sp->psDecoder = NULL; + sp->last_y = 0; + } + + if (sp->pBuffer != NULL) + { + _TIFFfreeExt(tif, sp->pBuffer); + sp->pBuffer = NULL; + } + + _TIFFfreeExt(tif, tif->tif_data); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static int TWebPVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "WebPVSetField"; + WebPState *sp = LState(tif); + + switch (tag) + { + case TIFFTAG_WEBP_LEVEL: + sp->quality_level = (int)va_arg(ap, int); + if (sp->quality_level <= 0 || sp->quality_level > 100.0f) + { + TIFFWarningExtR(tif, module, + "WEBP_LEVEL should be between 1 and 100"); + } + return 1; + case TIFFTAG_WEBP_LOSSLESS: +#if WEBP_ENCODER_ABI_VERSION >= 0x0100 + sp->lossless = va_arg(ap, int); + if (sp->lossless) + { + sp->quality_level = 100; + } + return 1; +#else + TIFFErrorExtR( + tif, module, + "Need to upgrade WEBP driver, this version doesn't support " + "lossless compression."); + return 0; +#endif + case TIFFTAG_WEBP_LOSSLESS_EXACT: +#if WEBP_ENCODER_ABI_VERSION >= 0x0209 + sp->lossless_exact = va_arg(ap, int); + return 1; +#else + TIFFErrorExtR( + tif, module, + "Need to upgrade WEBP driver, this version doesn't support " + "lossless compression."); + return 0; +#endif + default: + return (*sp->vsetparent)(tif, tag, ap); + } + /*NOTREACHED*/ +} + +static int TWebPVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + WebPState *sp = LState(tif); + + switch (tag) + { + case TIFFTAG_WEBP_LEVEL: + *va_arg(ap, int *) = sp->quality_level; + break; + case TIFFTAG_WEBP_LOSSLESS: + *va_arg(ap, int *) = sp->lossless; + break; + case TIFFTAG_WEBP_LOSSLESS_EXACT: + *va_arg(ap, int *) = sp->lossless_exact; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return 1; +} + +static const TIFFField TWebPFields[] = { + {TIFFTAG_WEBP_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE, + FALSE, "WEBP quality", NULL}, + {TIFFTAG_WEBP_LOSSLESS, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + TRUE, FALSE, "WEBP lossless/lossy", NULL}, + {TIFFTAG_WEBP_LOSSLESS_EXACT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, + FIELD_PSEUDO, TRUE, FALSE, "WEBP exact lossless", NULL}, +}; + +int TIFFInitWebP(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitWebP"; + WebPState *sp; + + (void)scheme; + assert(scheme == COMPRESSION_WEBP); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, TWebPFields, TIFFArrayCount(TWebPFields))) + { + TIFFErrorExtR(tif, module, "Merging WebP codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(WebPState)); + if (tif->tif_data == NULL) + goto bad; + sp = LState(tif); + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = TWebPVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = TWebPVSetField; /* hook for codec tags */ + + /* Default values for codec-specific fields */ + sp->quality_level = 75; /* default comp. level */ + sp->lossless = 0; /* default to false */ + sp->lossless_exact = 1; /* exact lossless mode (if lossless enabled) */ + sp->state = 0; + sp->nSamples = 0; + sp->psDecoder = NULL; + sp->last_y = 0; + + sp->buffer_offset = 0; + sp->pBuffer = NULL; + + /* + * Install codec methods. + * Notes: + * encoderow is not supported + */ + tif->tif_fixuptags = TWebPFixupTags; + tif->tif_setupdecode = TWebPSetupDecode; + tif->tif_predecode = TWebPPreDecode; + tif->tif_decoderow = TWebPDecode; + tif->tif_decodestrip = TWebPDecode; + tif->tif_decodetile = TWebPDecode; + tif->tif_setupencode = TWebPSetupEncode; + tif->tif_preencode = TWebPPreEncode; + tif->tif_postencode = TWebPPostEncode; + tif->tif_encoderow = TWebPEncode; + tif->tif_encodestrip = TWebPEncode; + tif->tif_encodetile = TWebPEncode; + tif->tif_cleanup = TWebPCleanup; + + return 1; +bad: + TIFFErrorExtR(tif, module, "No space for WebP state block"); + return 0; +} + +#endif /* WEBP_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_win32.c b/cpp/3rd_party/libtiff/tif_win32.c new file mode 100644 index 0000000000..525cebec76 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_win32.c @@ -0,0 +1,431 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by + * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA + */ + +#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS +#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS +#endif + +#include "tiffiop.h" +#include + +#include + +/* + CreateFileA/CreateFileW return type 'HANDLE' while TIFFFdOpen() takes 'int', + which is formally incompatible and can even seemingly be of different size: + HANDLE is 64 bit under Win64, while int is still 32 bits there. + + However, only the lower 32 bits of a HANDLE are significant under Win64 as, + for interoperability reasons, they must have the same values in 32- and + 64-bit programs running on the same system, see + + https://docs.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication + + Because of this, it is safe to define the following trivial functions for + casting between ints and HANDLEs, which are only really needed to avoid + compiler warnings (and, perhaps, to make the code slightly more clear). + Note that using the intermediate cast to "intptr_t" is crucial for warning + avoidance, as this integer type has the same size as HANDLE in all builds. +*/ + +static inline thandle_t thandle_from_int(int ifd) +{ + return (thandle_t)(intptr_t)ifd; +} + +static inline int thandle_to_int(thandle_t fd) { return (int)(intptr_t)fd; } + +static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size) +{ + /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes + * 32bit sizes, so we loop through the data in suitable 32bit sized + * chunks */ + uint8_t *ma; + uint64_t mb; + DWORD n; + DWORD o; + tmsize_t p; + ma = (uint8_t *)buf; + mb = size; + p = 0; + while (mb > 0) + { + n = 0x80000000UL; + if ((uint64_t)n > mb) + n = (DWORD)mb; + if (!ReadFile(fd, (LPVOID)ma, n, &o, NULL)) + return (0); + ma += o; + mb -= o; + p += o; + if (o != n) + break; + } + return (p); +} + +static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size) +{ + /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes + * 32bit sizes, so we loop through the data in suitable 32bit sized + * chunks */ + uint8_t *ma; + uint64_t mb; + DWORD n; + DWORD o; + tmsize_t p; + ma = (uint8_t *)buf; + mb = size; + p = 0; + while (mb > 0) + { + n = 0x80000000UL; + if ((uint64_t)n > mb) + n = (DWORD)mb; + if (!WriteFile(fd, (LPVOID)ma, n, &o, NULL)) + return (0); + ma += o; + mb -= o; + p += o; + if (o != n) + break; + } + return (p); +} + +static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence) +{ + LARGE_INTEGER offli; + DWORD dwMoveMethod; + offli.QuadPart = off; + switch (whence) + { + case SEEK_SET: + dwMoveMethod = FILE_BEGIN; + break; + case SEEK_CUR: + dwMoveMethod = FILE_CURRENT; + break; + case SEEK_END: + dwMoveMethod = FILE_END; + break; + default: + dwMoveMethod = FILE_BEGIN; + break; + } + offli.LowPart = + SetFilePointer(fd, offli.LowPart, &offli.HighPart, dwMoveMethod); + if ((offli.LowPart == INVALID_SET_FILE_POINTER) && + (GetLastError() != NO_ERROR)) + offli.QuadPart = 0; + return (offli.QuadPart); +} + +static int _tiffCloseProc(thandle_t fd) { return (CloseHandle(fd) ? 0 : -1); } + +static uint64_t _tiffSizeProc(thandle_t fd) +{ + LARGE_INTEGER m; + if (GetFileSizeEx(fd, &m)) + return (m.QuadPart); + else + return (0); +} + +/* + * From "Hermann Josef Hill" : + * + * Windows uses both a handle and a pointer for file mapping, + * but according to the SDK documentation and Richter's book + * "Advanced Windows Programming" it is safe to free the handle + * after obtaining the file mapping pointer + * + * This removes a nasty OS dependency and cures a problem + * with Visual C++ 5.0 + */ +static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize) +{ + uint64_t size; + tmsize_t sizem; + HANDLE hMapFile; + + size = _tiffSizeProc(fd); + sizem = (tmsize_t)size; + if (!size || (uint64_t)sizem != size) + return (0); + + /* By passing in 0 for the maximum file size, it specifies that we + create a file mapping object for the full file size. */ + hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL); + if (hMapFile == NULL) + return (0); + *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0); + CloseHandle(hMapFile); + if (*pbase == NULL) + return (0); + *psize = size; + return (1); +} + +static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size) +{ + (void)fd; + (void)size; + UnmapViewOfFile(base); +} + +/* + * Open a TIFF file descriptor for read/writing. + * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode + * string, which forces the file to be opened unmapped. + */ +TIFF *TIFFFdOpen(int ifd, const char *name, const char *mode) +{ + return TIFFFdOpenExt(ifd, name, mode, NULL); +} + +TIFF *TIFFFdOpenExt(int ifd, const char *name, const char *mode, + TIFFOpenOptions *opts) +{ + TIFF *tif; + int fSuppressMap; + int m; + + fSuppressMap = 0; + for (m = 0; mode[m] != 0; m++) + { + if (mode[m] == 'u') + { + fSuppressMap = 1; + break; + } + } + + tif = TIFFClientOpenExt( + name, mode, thandle_from_int(ifd), _tiffReadProc, _tiffWriteProc, + _tiffSeekProc, _tiffCloseProc, _tiffSizeProc, + fSuppressMap ? _tiffDummyMapProc : _tiffMapProc, + fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc, opts); + if (tif) + tif->tif_fd = ifd; + return (tif); +} + +#ifndef _WIN32_WCE + +/* + * Open a TIFF file for read/writing. + */ +TIFF *TIFFOpen(const char *name, const char *mode) +{ + return TIFFOpenExt(name, mode, NULL); +} + +TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts) +{ + static const char module[] = "TIFFOpen"; + thandle_t fd; + int m; + DWORD dwMode; + TIFF *tif; + + m = _TIFFgetMode(opts, NULL, mode, module); + + switch (m) + { + case O_RDONLY: + dwMode = OPEN_EXISTING; + break; + case O_RDWR: + dwMode = OPEN_EXISTING; + break; + case O_RDWR | O_CREAT: + dwMode = OPEN_ALWAYS; + break; + case O_RDWR | O_TRUNC: + dwMode = CREATE_ALWAYS; + break; + case O_RDWR | O_CREAT | O_TRUNC: + dwMode = CREATE_ALWAYS; + break; + default: + return ((TIFF *)0); + } + + fd = (thandle_t)CreateFileA( + name, (m == O_RDONLY) ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE), + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode, + (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, + NULL); + if (fd == INVALID_HANDLE_VALUE) + { + _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name); + return ((TIFF *)0); + } + + tif = TIFFFdOpenExt(thandle_to_int(fd), name, mode, opts); + if (!tif) + CloseHandle(fd); + return tif; +} + +/* + * Open a TIFF file with a Unicode filename, for read/writing. + */ +TIFF *TIFFOpenW(const wchar_t *name, const char *mode) +{ + return TIFFOpenWExt(name, mode, NULL); +} + +TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts) +{ + static const char module[] = "TIFFOpenW"; + thandle_t fd; + int m; + DWORD dwMode; + int mbsize; + char *mbname; + TIFF *tif; + + m = _TIFFgetMode(opts, NULL, mode, module); + + switch (m) + { + case O_RDONLY: + dwMode = OPEN_EXISTING; + break; + case O_RDWR: + dwMode = OPEN_EXISTING; + break; + case O_RDWR | O_CREAT: + dwMode = OPEN_ALWAYS; + break; + case O_RDWR | O_TRUNC: + dwMode = CREATE_ALWAYS; + break; + case O_RDWR | O_CREAT | O_TRUNC: + dwMode = CREATE_ALWAYS; + break; + default: + return ((TIFF *)0); + } + + fd = (thandle_t)CreateFileW( + name, (m == O_RDONLY) ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE), + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode, + (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, + NULL); + if (fd == INVALID_HANDLE_VALUE) + { + _TIFFErrorEarly(opts, NULL, module, "%S: Cannot open", name); + return ((TIFF *)0); + } + + mbname = NULL; + mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL); + if (mbsize > 0) + { + mbname = (char *)_TIFFmalloc(mbsize); + if (!mbname) + { + _TIFFErrorEarly( + opts, NULL, module, + "Can't allocate space for filename conversion buffer"); + return ((TIFF *)0); + } + + WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL); + } + + tif = TIFFFdOpenExt(thandle_to_int(fd), + (mbname != NULL) ? mbname : "", mode, opts); + if (!tif) + CloseHandle(fd); + + _TIFFfree(mbname); + + return tif; +} + +#endif /* ndef _WIN32_WCE */ + +void *_TIFFmalloc(tmsize_t s) +{ + if (s == 0) + return ((void *)NULL); + + return (malloc((size_t)s)); +} + +void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz) +{ + if (nmemb == 0 || siz == 0) + return ((void *)NULL); + + return calloc((size_t)nmemb, (size_t)siz); +} + +void _TIFFfree(void *p) { free(p); } + +void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); } + +void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); } + +void _TIFFmemcpy(void *d, const void *s, tmsize_t c) +{ + memcpy(d, s, (size_t)c); +} + +int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c) +{ + return (memcmp(p1, p2, (size_t)c)); +} + +#ifndef _WIN32_WCE + +static void Win32WarningHandler(const char *module, const char *fmt, va_list ap) +{ + if (module != NULL) + fprintf(stderr, "%s: ", module); + fprintf(stderr, "Warning, "); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); +} +TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler; + +static void Win32ErrorHandler(const char *module, const char *fmt, va_list ap) +{ + if (module != NULL) + fprintf(stderr, "%s: ", module); + vfprintf(stderr, fmt, ap); + fprintf(stderr, ".\n"); +} +TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler; + +#endif /* ndef _WIN32_WCE */ diff --git a/cpp/3rd_party/libtiff/tif_write.c b/cpp/3rd_party/libtiff/tif_write.c new file mode 100644 index 0000000000..3263853cc0 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_write.c @@ -0,0 +1,965 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * TIFF Library. + * + * Scanline-oriented Write Support + */ +#include "tiffiop.h" +#include + +#define STRIPINCR 20 /* expansion factor on strip array */ + +#define WRITECHECKSTRIPS(tif, module) \ + (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 0, module)) +#define WRITECHECKTILES(tif, module) \ + (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 1, module)) +#define BUFFERCHECK(tif) \ + ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \ + TIFFWriteBufferSetup((tif), NULL, (tmsize_t)-1)) + +static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module); +static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, + tmsize_t cc); + +int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample) +{ + static const char module[] = "TIFFWriteScanline"; + register TIFFDirectory *td; + int status, imagegrew = 0; + uint32_t strip; + + if (!WRITECHECKSTRIPS(tif, module)) + return (-1); + /* + * Handle delayed allocation of data buffer. This + * permits it to be sized more intelligently (using + * directory information). + */ + if (!BUFFERCHECK(tif)) + return (-1); + tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/ + + td = &tif->tif_dir; + /* + * Extend image length if needed + * (but only for PlanarConfig=1). + */ + if (row >= td->td_imagelength) + { /* extend image */ + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + { + TIFFErrorExtR( + tif, module, + "Can not change \"ImageLength\" when using separate planes"); + return (-1); + } + td->td_imagelength = row + 1; + imagegrew = 1; + } + /* + * Calculate strip and check for crossings. + */ + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + { + if (sample >= td->td_samplesperpixel) + { + TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu", + (unsigned long)sample, + (unsigned long)td->td_samplesperpixel); + return (-1); + } + strip = sample * td->td_stripsperimage + row / td->td_rowsperstrip; + } + else + strip = row / td->td_rowsperstrip; + /* + * Check strip array to make sure there's space. We don't support + * dynamically growing files that have data organized in separate + * bitplanes because it's too painful. In that case we require that + * the imagelength be set properly before the first write (so that the + * strips array will be fully allocated above). + */ + if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module)) + return (-1); + if (strip != tif->tif_curstrip) + { + /* + * Changing strips -- flush any data present. + */ + if (!TIFFFlushData(tif)) + return (-1); + tif->tif_curstrip = strip; + /* + * Watch out for a growing image. The value of strips/image + * will initially be 1 (since it can't be deduced until the + * imagelength is known). + */ + if (strip >= td->td_stripsperimage && imagegrew) + td->td_stripsperimage = + TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); + if (td->td_stripsperimage == 0) + { + TIFFErrorExtR(tif, module, "Zero strips per image"); + return (-1); + } + tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; + if ((tif->tif_flags & TIFF_CODERSETUP) == 0) + { + if (!(*tif->tif_setupencode)(tif)) + return (-1); + tif->tif_flags |= TIFF_CODERSETUP; + } + + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + + /* this informs TIFFAppendToStrip() we have changed strip */ + tif->tif_curoff = 0; + + if (!(*tif->tif_preencode)(tif, sample)) + return (-1); + tif->tif_flags |= TIFF_POSTENCODE; + } + /* + * Ensure the write is either sequential or at the + * beginning of a strip (or that we can randomly + * access the data -- i.e. no encoding). + */ + if (row != tif->tif_row) + { + if (row < tif->tif_row) + { + /* + * Moving backwards within the same strip: + * backup to the start and then decode + * forward (below). + */ + tif->tif_row = + (strip % td->td_stripsperimage) * td->td_rowsperstrip; + tif->tif_rawcp = tif->tif_rawdata; + } + /* + * Seek forward to the desired row. + */ + if (!(*tif->tif_seek)(tif, row - tif->tif_row)) + return (-1); + tif->tif_row = row; + } + + /* swab if needed - note that source buffer will be altered */ + tif->tif_postdecode(tif, (uint8_t *)buf, tif->tif_scanlinesize); + + status = (*tif->tif_encoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize, + sample); + + /* we are now poised at the beginning of the next row */ + tif->tif_row = row + 1; + return (status); +} + +/* Make sure that at the first attempt of rewriting a tile/strip, we will have + */ +/* more bytes available in the output buffer than the previous byte count, */ +/* so that TIFFAppendToStrip() will detect the overflow when it is called the + * first */ +/* time if the new compressed tile is bigger than the older one. (GDAL #4771) */ +static int _TIFFReserveLargeEnoughWriteBuffer(TIFF *tif, uint32_t strip_or_tile) +{ + TIFFDirectory *td = &tif->tif_dir; + if (td->td_stripbytecount_p[strip_or_tile] > 0) + { + /* The +1 is to ensure at least one extra bytes */ + /* The +4 is because the LZW encoder flushes 4 bytes before the limit */ + uint64_t safe_buffer_size = + (uint64_t)(td->td_stripbytecount_p[strip_or_tile] + 1 + 4); + if (tif->tif_rawdatasize <= (tmsize_t)safe_buffer_size) + { + if (!(TIFFWriteBufferSetup( + tif, NULL, + (tmsize_t)TIFFroundup_64(safe_buffer_size, 1024)))) + return 0; + } + } + return 1; +} + +/* + * Encode the supplied data and write it to the + * specified strip. + * + * NB: Image length must be setup before writing. + */ +tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data, + tmsize_t cc) +{ + static const char module[] = "TIFFWriteEncodedStrip"; + TIFFDirectory *td = &tif->tif_dir; + uint16_t sample; + + if (!WRITECHECKSTRIPS(tif, module)) + return ((tmsize_t)-1); + /* + * Check strip array to make sure there's space. + * We don't support dynamically growing files that + * have data organized in separate bitplanes because + * it's too painful. In that case we require that + * the imagelength be set properly before the first + * write (so that the strips array will be fully + * allocated above). + */ + if (strip >= td->td_nstrips) + { + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + { + TIFFErrorExtR( + tif, module, + "Can not grow image by strips when using separate planes"); + return ((tmsize_t)-1); + } + if (!TIFFGrowStrips(tif, 1, module)) + return ((tmsize_t)-1); + td->td_stripsperimage = + TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); + } + /* + * Handle delayed allocation of data buffer. This + * permits it to be sized according to the directory + * info. + */ + if (!BUFFERCHECK(tif)) + return ((tmsize_t)-1); + + tif->tif_flags |= TIFF_BUF4WRITE; + + tif->tif_curstrip = strip; + + /* this informs TIFFAppendToStrip() we have changed or reset strip */ + tif->tif_curoff = 0; + + if (!_TIFFReserveLargeEnoughWriteBuffer(tif, strip)) + { + return ((tmsize_t)(-1)); + } + + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + + if (td->td_stripsperimage == 0) + { + TIFFErrorExtR(tif, module, "Zero strips per image"); + return ((tmsize_t)-1); + } + + tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; + if ((tif->tif_flags & TIFF_CODERSETUP) == 0) + { + if (!(*tif->tif_setupencode)(tif)) + return ((tmsize_t)-1); + tif->tif_flags |= TIFF_CODERSETUP; + } + + tif->tif_flags &= ~TIFF_POSTENCODE; + + /* shortcut to avoid an extra memcpy() */ + if (td->td_compression == COMPRESSION_NONE) + { + /* swab if needed - note that source buffer will be altered */ + tif->tif_postdecode(tif, (uint8_t *)data, cc); + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits((uint8_t *)data, cc); + + if (cc > 0 && !TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc)) + return ((tmsize_t)-1); + return (cc); + } + + sample = (uint16_t)(strip / td->td_stripsperimage); + if (!(*tif->tif_preencode)(tif, sample)) + return ((tmsize_t)-1); + + /* swab if needed - note that source buffer will be altered */ + tif->tif_postdecode(tif, (uint8_t *)data, cc); + + if (!(*tif->tif_encodestrip)(tif, (uint8_t *)data, cc, sample)) + return ((tmsize_t)-1); + if (!(*tif->tif_postencode)(tif)) + return ((tmsize_t)-1); + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc); + if (tif->tif_rawcc > 0 && + !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc)) + return ((tmsize_t)-1); + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + return (cc); +} + +/* + * Write the supplied data to the specified strip. + * + * NB: Image length must be setup before writing. + */ +tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc) +{ + static const char module[] = "TIFFWriteRawStrip"; + TIFFDirectory *td = &tif->tif_dir; + + if (!WRITECHECKSTRIPS(tif, module)) + return ((tmsize_t)-1); + /* + * Check strip array to make sure there's space. + * We don't support dynamically growing files that + * have data organized in separate bitplanes because + * it's too painful. In that case we require that + * the imagelength be set properly before the first + * write (so that the strips array will be fully + * allocated above). + */ + if (strip >= td->td_nstrips) + { + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + { + TIFFErrorExtR( + tif, module, + "Can not grow image by strips when using separate planes"); + return ((tmsize_t)-1); + } + /* + * Watch out for a growing image. The value of + * strips/image will initially be 1 (since it + * can't be deduced until the imagelength is known). + */ + if (strip >= td->td_stripsperimage) + td->td_stripsperimage = + TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); + if (!TIFFGrowStrips(tif, 1, module)) + return ((tmsize_t)-1); + } + + if (tif->tif_curstrip != strip) + { + tif->tif_curstrip = strip; + + /* this informs TIFFAppendToStrip() we have changed or reset strip */ + tif->tif_curoff = 0; + } + + if (td->td_stripsperimage == 0) + { + TIFFErrorExtR(tif, module, "Zero strips per image"); + return ((tmsize_t)-1); + } + tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; + return (TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc) ? cc + : (tmsize_t)-1); +} + +/* + * Write and compress a tile of data. The + * tile is selected by the (x,y,z,s) coordinates. + */ +tmsize_t TIFFWriteTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, + uint16_t s) +{ + if (!TIFFCheckTile(tif, x, y, z, s)) + return ((tmsize_t)(-1)); + /* + * NB: A tile size of -1 is used instead of tif_tilesize knowing + * that TIFFWriteEncodedTile will clamp this to the tile size. + * This is done because the tile size may not be defined until + * after the output buffer is setup in TIFFWriteBufferSetup. + */ + return (TIFFWriteEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf, + (tmsize_t)(-1))); +} + +/* + * Encode the supplied data and write it to the + * specified tile. There must be space for the + * data. The function clamps individual writes + * to a tile to the tile size, but does not (and + * can not) check that multiple writes to the same + * tile do not write more than tile size data. + * + * NB: Image length must be setup before writing; this + * interface does not support automatically growing + * the image on each write (as TIFFWriteScanline does). + */ +tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc) +{ + static const char module[] = "TIFFWriteEncodedTile"; + TIFFDirectory *td; + uint16_t sample; + uint32_t howmany32; + + if (!WRITECHECKTILES(tif, module)) + return ((tmsize_t)(-1)); + td = &tif->tif_dir; + if (tile >= td->td_nstrips) + { + TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu", + (unsigned long)tile, (unsigned long)td->td_nstrips); + return ((tmsize_t)(-1)); + } + /* + * Handle delayed allocation of data buffer. This + * permits it to be sized more intelligently (using + * directory information). + */ + if (!BUFFERCHECK(tif)) + return ((tmsize_t)(-1)); + + tif->tif_flags |= TIFF_BUF4WRITE; + + tif->tif_curtile = tile; + + /* this informs TIFFAppendToStrip() we have changed or reset tile */ + tif->tif_curoff = 0; + + if (!_TIFFReserveLargeEnoughWriteBuffer(tif, tile)) + { + return ((tmsize_t)(-1)); + } + + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + + /* + * Compute tiles per row & per column to compute + * current row and column + */ + howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength); + if (howmany32 == 0) + { + TIFFErrorExtR(tif, module, "Zero tiles"); + return ((tmsize_t)(-1)); + } + tif->tif_row = (tile % howmany32) * td->td_tilelength; + howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth); + if (howmany32 == 0) + { + TIFFErrorExtR(tif, module, "Zero tiles"); + return ((tmsize_t)(-1)); + } + tif->tif_col = (tile % howmany32) * td->td_tilewidth; + + if ((tif->tif_flags & TIFF_CODERSETUP) == 0) + { + if (!(*tif->tif_setupencode)(tif)) + return ((tmsize_t)(-1)); + tif->tif_flags |= TIFF_CODERSETUP; + } + tif->tif_flags &= ~TIFF_POSTENCODE; + + /* + * Clamp write amount to the tile size. This is mostly + * done so that callers can pass in some large number + * (e.g. -1) and have the tile size used instead. + */ + if (cc < 1 || cc > tif->tif_tilesize) + cc = tif->tif_tilesize; + + /* shortcut to avoid an extra memcpy() */ + if (td->td_compression == COMPRESSION_NONE) + { + /* swab if needed - note that source buffer will be altered */ + tif->tif_postdecode(tif, (uint8_t *)data, cc); + + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits((uint8_t *)data, cc); + + if (cc > 0 && !TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc)) + return ((tmsize_t)-1); + return (cc); + } + + sample = (uint16_t)(tile / td->td_stripsperimage); + if (!(*tif->tif_preencode)(tif, sample)) + return ((tmsize_t)(-1)); + /* swab if needed - note that source buffer will be altered */ + tif->tif_postdecode(tif, (uint8_t *)data, cc); + + if (!(*tif->tif_encodetile)(tif, (uint8_t *)data, cc, sample)) + return ((tmsize_t)-1); + if (!(*tif->tif_postencode)(tif)) + return ((tmsize_t)(-1)); + if (!isFillOrder(tif, td->td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc); + if (tif->tif_rawcc > 0 && + !TIFFAppendToStrip(tif, tile, tif->tif_rawdata, tif->tif_rawcc)) + return ((tmsize_t)(-1)); + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + return (cc); +} + +/* + * Write the supplied data to the specified strip. + * There must be space for the data; we don't check + * if strips overlap! + * + * NB: Image length must be setup before writing; this + * interface does not support automatically growing + * the image on each write (as TIFFWriteScanline does). + */ +tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc) +{ + static const char module[] = "TIFFWriteRawTile"; + + if (!WRITECHECKTILES(tif, module)) + return ((tmsize_t)(-1)); + if (tile >= tif->tif_dir.td_nstrips) + { + TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu", + (unsigned long)tile, + (unsigned long)tif->tif_dir.td_nstrips); + return ((tmsize_t)(-1)); + } + return (TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc) ? cc + : (tmsize_t)(-1)); +} + +#define isUnspecified(tif, f) \ + (TIFFFieldSet(tif, f) && (tif)->tif_dir.td_imagelength == 0) + +int TIFFSetupStrips(TIFF *tif) +{ + TIFFDirectory *td = &tif->tif_dir; + + if (isTiled(tif)) + td->td_stripsperimage = isUnspecified(tif, FIELD_TILEDIMENSIONS) + ? td->td_samplesperpixel + : TIFFNumberOfTiles(tif); + else + td->td_stripsperimage = isUnspecified(tif, FIELD_ROWSPERSTRIP) + ? td->td_samplesperpixel + : TIFFNumberOfStrips(tif); + td->td_nstrips = td->td_stripsperimage; + /* TIFFWriteDirectoryTagData has a limitation to 0x80000000U bytes */ + if (td->td_nstrips >= + 0x80000000U / ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U)) + { + TIFFErrorExtR(tif, "TIFFSetupStrips", + "Too large Strip/Tile Offsets/ByteCounts arrays"); + return 0; + } + if (td->td_planarconfig == PLANARCONFIG_SEPARATE) + td->td_stripsperimage /= td->td_samplesperpixel; + + if (td->td_stripoffset_p != NULL) + _TIFFfreeExt(tif, td->td_stripoffset_p); + td->td_stripoffset_p = (uint64_t *)_TIFFCheckMalloc( + tif, td->td_nstrips, sizeof(uint64_t), "for \"StripOffsets\" array"); + if (td->td_stripbytecount_p != NULL) + _TIFFfreeExt(tif, td->td_stripbytecount_p); + td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc( + tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array"); + if (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL) + return (0); + /* + * Place data at the end-of-file + * (by setting offsets to zero). + */ + _TIFFmemset(td->td_stripoffset_p, 0, td->td_nstrips * sizeof(uint64_t)); + _TIFFmemset(td->td_stripbytecount_p, 0, td->td_nstrips * sizeof(uint64_t)); + TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS); + TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); + return (1); +} +#undef isUnspecified + +/* + * Verify file is writable and that the directory + * information is setup properly. In doing the latter + * we also "freeze" the state of the directory so + * that important information is not changed. + */ +int TIFFWriteCheck(TIFF *tif, int tiles, const char *module) +{ + if (tif->tif_mode == O_RDONLY) + { + TIFFErrorExtR(tif, module, "File not open for writing"); + return (0); + } + if (tiles ^ isTiled(tif)) + { + TIFFErrorExtR(tif, module, + tiles ? "Can not write tiles to a striped image" + : "Can not write scanlines to a tiled image"); + return (0); + } + + _TIFFFillStriles(tif); + + /* + * On the first write verify all the required information + * has been setup and initialize any data structures that + * had to wait until directory information was set. + * Note that a lot of our work is assumed to remain valid + * because we disallow any of the important parameters + * from changing after we start writing (i.e. once + * TIFF_BEENWRITING is set, TIFFSetField will only allow + * the image's length to be changed). + */ + if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) + { + TIFFErrorExtR(tif, module, + "Must set \"ImageWidth\" before writing data"); + return (0); + } + if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) + { + tif->tif_dir.td_nstrips = 0; + TIFFErrorExtR(tif, module, "No space for %s arrays", + isTiled(tif) ? "tile" : "strip"); + return (0); + } + if (isTiled(tif)) + { + tif->tif_tilesize = TIFFTileSize(tif); + if (tif->tif_tilesize == 0) + return (0); + } + else + tif->tif_tilesize = (tmsize_t)(-1); + tif->tif_scanlinesize = TIFFScanlineSize(tif); + if (tif->tif_scanlinesize == 0) + return (0); + tif->tif_flags |= TIFF_BEENWRITING; + + if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && + tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && + tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && + tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 && + !(tif->tif_flags & TIFF_DIRTYDIRECT)) + { + TIFFForceStrileArrayWriting(tif); + } + + return (1); +} + +/* + * Setup the raw data buffer used for encoding. + */ +int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size) +{ + static const char module[] = "TIFFWriteBufferSetup"; + + if (tif->tif_rawdata) + { + if (tif->tif_flags & TIFF_MYBUFFER) + { + _TIFFfreeExt(tif, tif->tif_rawdata); + tif->tif_flags &= ~TIFF_MYBUFFER; + } + tif->tif_rawdata = NULL; + } + if (size == (tmsize_t)(-1)) + { + size = (isTiled(tif) ? tif->tif_tilesize : TIFFStripSize(tif)); + + /* Adds 10% margin for cases where compression would expand a bit */ + if (size < TIFF_TMSIZE_T_MAX - size / 10) + size += size / 10; + /* + * Make raw data buffer at least 8K + */ + if (size < 8 * 1024) + size = 8 * 1024; + bp = NULL; /* NB: force malloc */ + } + if (bp == NULL) + { + bp = _TIFFmallocExt(tif, size); + if (bp == NULL) + { + TIFFErrorExtR(tif, module, "No space for output buffer"); + return (0); + } + tif->tif_flags |= TIFF_MYBUFFER; + } + else + tif->tif_flags &= ~TIFF_MYBUFFER; + tif->tif_rawdata = (uint8_t *)bp; + tif->tif_rawdatasize = size; + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + tif->tif_flags |= TIFF_BUFFERSETUP; + return (1); +} + +/* + * Grow the strip data structures by delta strips. + */ +static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module) +{ + TIFFDirectory *td = &tif->tif_dir; + uint64_t *new_stripoffset; + uint64_t *new_stripbytecount; + + assert(td->td_planarconfig == PLANARCONFIG_CONTIG); + new_stripoffset = (uint64_t *)_TIFFreallocExt( + tif, td->td_stripoffset_p, (td->td_nstrips + delta) * sizeof(uint64_t)); + new_stripbytecount = (uint64_t *)_TIFFreallocExt( + tif, td->td_stripbytecount_p, + (td->td_nstrips + delta) * sizeof(uint64_t)); + if (new_stripoffset == NULL || new_stripbytecount == NULL) + { + if (new_stripoffset) + _TIFFfreeExt(tif, new_stripoffset); + if (new_stripbytecount) + _TIFFfreeExt(tif, new_stripbytecount); + td->td_nstrips = 0; + TIFFErrorExtR(tif, module, "No space to expand strip arrays"); + return (0); + } + td->td_stripoffset_p = new_stripoffset; + td->td_stripbytecount_p = new_stripbytecount; + _TIFFmemset(td->td_stripoffset_p + td->td_nstrips, 0, + delta * sizeof(uint64_t)); + _TIFFmemset(td->td_stripbytecount_p + td->td_nstrips, 0, + delta * sizeof(uint64_t)); + td->td_nstrips += delta; + tif->tif_flags |= TIFF_DIRTYDIRECT; + + return (1); +} + +/* + * Append the data to the specified strip. + */ +static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, + tmsize_t cc) +{ + static const char module[] = "TIFFAppendToStrip"; + TIFFDirectory *td = &tif->tif_dir; + uint64_t m; + int64_t old_byte_count = -1; + + if (tif->tif_curoff == 0) + tif->tif_lastvalidoff = 0; + + if (td->td_stripoffset_p[strip] == 0 || tif->tif_curoff == 0) + { + assert(td->td_nstrips > 0); + + if (td->td_stripbytecount_p[strip] != 0 && + td->td_stripoffset_p[strip] != 0 && + td->td_stripbytecount_p[strip] >= (uint64_t)cc) + { + /* + * There is already tile data on disk, and the new tile + * data we have will fit in the same space. The only + * aspect of this that is risky is that there could be + * more data to append to this strip before we are done + * depending on how we are getting called. + */ + if (!SeekOK(tif, td->td_stripoffset_p[strip])) + { + TIFFErrorExtR(tif, module, "Seek error at scanline %lu", + (unsigned long)tif->tif_row); + return (0); + } + + tif->tif_lastvalidoff = + td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip]; + } + else + { + /* + * Seek to end of file, and set that as our location to + * write this strip. + */ + td->td_stripoffset_p[strip] = TIFFSeekFile(tif, 0, SEEK_END); + tif->tif_flags |= TIFF_DIRTYSTRIP; + } + + tif->tif_curoff = td->td_stripoffset_p[strip]; + + /* + * We are starting a fresh strip/tile, so set the size to zero. + */ + old_byte_count = td->td_stripbytecount_p[strip]; + td->td_stripbytecount_p[strip] = 0; + } + + m = tif->tif_curoff + cc; + if (!(tif->tif_flags & TIFF_BIGTIFF)) + m = (uint32_t)m; + if ((m < tif->tif_curoff) || (m < (uint64_t)cc)) + { + TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); + return (0); + } + + if (tif->tif_lastvalidoff != 0 && m > tif->tif_lastvalidoff && + td->td_stripbytecount_p[strip] > 0) + { + /* Ouch: we have detected that we are rewriting in place a strip/tile */ + /* with several calls to TIFFAppendToStrip(). The first call was with */ + /* a size smaller than the previous size of the strip/tile, so we */ + /* opted to rewrite in place, but a following call causes us to go */ + /* outsize of the strip/tile area, so we have to finally go for a */ + /* append-at-end-of-file strategy, and start by moving what we already + */ + /* wrote. */ + tmsize_t tempSize; + void *temp; + uint64_t offsetRead; + uint64_t offsetWrite; + uint64_t toCopy = td->td_stripbytecount_p[strip]; + + if (toCopy < 1024 * 1024) + tempSize = (tmsize_t)toCopy; + else + tempSize = 1024 * 1024; + + offsetRead = td->td_stripoffset_p[strip]; + offsetWrite = TIFFSeekFile(tif, 0, SEEK_END); + + m = offsetWrite + toCopy + cc; + if (!(tif->tif_flags & TIFF_BIGTIFF) && m != (uint32_t)m) + { + TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); + return (0); + } + + temp = _TIFFmallocExt(tif, tempSize); + if (temp == NULL) + { + TIFFErrorExtR(tif, module, "No space for output buffer"); + return (0); + } + + tif->tif_flags |= TIFF_DIRTYSTRIP; + + td->td_stripoffset_p[strip] = offsetWrite; + td->td_stripbytecount_p[strip] = 0; + + /* Move data written by previous calls to us at end of file */ + while (toCopy > 0) + { + if (!SeekOK(tif, offsetRead)) + { + TIFFErrorExtR(tif, module, "Seek error"); + _TIFFfreeExt(tif, temp); + return (0); + } + if (!ReadOK(tif, temp, tempSize)) + { + TIFFErrorExtR(tif, module, "Cannot read"); + _TIFFfreeExt(tif, temp); + return (0); + } + if (!SeekOK(tif, offsetWrite)) + { + TIFFErrorExtR(tif, module, "Seek error"); + _TIFFfreeExt(tif, temp); + return (0); + } + if (!WriteOK(tif, temp, tempSize)) + { + TIFFErrorExtR(tif, module, "Cannot write"); + _TIFFfreeExt(tif, temp); + return (0); + } + offsetRead += tempSize; + offsetWrite += tempSize; + td->td_stripbytecount_p[strip] += tempSize; + toCopy -= tempSize; + } + _TIFFfreeExt(tif, temp); + + /* Append the data of this call */ + offsetWrite += cc; + m = offsetWrite; + } + + if (!WriteOK(tif, data, cc)) + { + TIFFErrorExtR(tif, module, "Write error at scanline %lu", + (unsigned long)tif->tif_row); + return (0); + } + tif->tif_curoff = m; + td->td_stripbytecount_p[strip] += cc; + + if ((int64_t)td->td_stripbytecount_p[strip] != old_byte_count) + tif->tif_flags |= TIFF_DIRTYSTRIP; + + return (1); +} + +/* + * Internal version of TIFFFlushData that can be + * called by ``encodestrip routines'' w/o concern + * for infinite recursion. + */ +int TIFFFlushData1(TIFF *tif) +{ + if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE) + { + if (!isFillOrder(tif, tif->tif_dir.td_fillorder) && + (tif->tif_flags & TIFF_NOBITREV) == 0) + TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc); + if (!TIFFAppendToStrip( + tif, isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip, + tif->tif_rawdata, tif->tif_rawcc)) + { + /* We update those variables even in case of error since there's */ + /* code that doesn't really check the return code of this */ + /* function */ + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + return (0); + } + tif->tif_rawcc = 0; + tif->tif_rawcp = tif->tif_rawdata; + } + return (1); +} + +/* + * Set the current write offset. This should only be + * used to set the offset to a known previous location + * (very carefully), or to 0 so that the next write gets + * appended to the end of the file. + */ +void TIFFSetWriteOffset(TIFF *tif, toff_t off) +{ + tif->tif_curoff = off; + tif->tif_lastvalidoff = 0; +} diff --git a/cpp/3rd_party/libtiff/tif_zip.c b/cpp/3rd_party/libtiff/tif_zip.c new file mode 100644 index 0000000000..8abf88ae76 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_zip.c @@ -0,0 +1,776 @@ +/* + * Copyright (c) 1995-1997 Sam Leffler + * Copyright (c) 1995-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef ZIP_SUPPORT +/* + * TIFF Library. + * + * ZIP (aka Deflate) Compression Support + * + * This file is an interface to the zlib library written by + * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later + * of the library. + * + * Optionally, libdeflate (https://github.com/ebiggers/libdeflate) may be used + * to do the compression and decompression, but only for whole strips and tiles. + * For scanline access, zlib will be sued as a fallback. + */ +#include "tif_predict.h" +#include "zlib.h" + +#if LIBDEFLATE_SUPPORT +#include "libdeflate.h" +#endif +#define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12 + +#include + +/* + * Sigh, ZLIB_VERSION is defined as a string so there's no + * way to do a proper check here. Instead we guess based + * on the presence of #defines that were added between the + * 0.95 and 1.0 distributions. + */ +#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED) +#error "Antiquated ZLIB software; you must use version 1.0 or later" +#endif + +#define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg) + +/* + * State block for each open TIFF + * file using ZIP compression/decompression. + */ +typedef struct +{ + TIFFPredictorState predict; + z_stream stream; + int read_error; /* whether a read error has occurred, and which should cause + further reads in the same strip/tile to be aborted */ + int zipquality; /* compression level */ + int state; /* state flags */ + int subcodec; /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */ +#if LIBDEFLATE_SUPPORT + int libdeflate_state; /* -1 = until first time ZIPEncode() / ZIPDecode() is + called, 0 = use zlib, 1 = use libdeflate */ + struct libdeflate_decompressor *libdeflate_dec; + struct libdeflate_compressor *libdeflate_enc; +#endif +#define ZSTATE_INIT_DECODE 0x01 +#define ZSTATE_INIT_ENCODE 0x02 + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ +} ZIPState; + +#define GetZIPState(tif) ((ZIPState *)(tif)->tif_data) +#define ZIPDecoderState(tif) GetZIPState(tif) +#define ZIPEncoderState(tif) GetZIPState(tif) + +static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); +static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); + +static int ZIPFixupTags(TIFF *tif) +{ + (void)tif; + return (1); +} + +static int ZIPSetupDecode(TIFF *tif) +{ + static const char module[] = "ZIPSetupDecode"; + ZIPState *sp = ZIPDecoderState(tif); + + assert(sp != NULL); + + /* if we were last encoding, terminate this mode */ + if (sp->state & ZSTATE_INIT_ENCODE) + { + deflateEnd(&sp->stream); + sp->state = 0; + } + + /* This function can possibly be called several times by */ + /* PredictorSetupDecode() if this function succeeds but */ + /* PredictorSetup() fails */ + if ((sp->state & ZSTATE_INIT_DECODE) == 0 && + inflateInit(&sp->stream) != Z_OK) + { + TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp)); + return (0); + } + else + { + sp->state |= ZSTATE_INIT_DECODE; + return (1); + } +} + +static inline uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b) +{ + return a < b ? a : b; +} + +static inline uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v) +{ + return (uInt)TIFF_MIN_UINT64(v, INT32_MAX); +} + +/* + * Setup state for decoding a strip. + */ +static int ZIPPreDecode(TIFF *tif, uint16_t s) +{ + ZIPState *sp = ZIPDecoderState(tif); + + (void)s; + assert(sp != NULL); + + if ((sp->state & ZSTATE_INIT_DECODE) == 0) + tif->tif_setupdecode(tif); + +#if LIBDEFLATE_SUPPORT + sp->libdeflate_state = -1; +#endif + sp->stream.next_in = tif->tif_rawdata; + assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + sp->stream.avail_in = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc); + if (inflateReset(&sp->stream) == Z_OK) + { + sp->read_error = 0; + return 1; + } + return 0; +} + +static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "ZIPDecode"; + ZIPState *sp = ZIPDecoderState(tif); + + (void)s; + assert(sp != NULL); + assert(sp->state == ZSTATE_INIT_DECODE); + + if (sp->read_error) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, + "ZIPDecode: Scanline %" PRIu32 " cannot be read due to " + "previous error", + tif->tif_row); + return 0; + } + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_state == 1) + return 0; + + /* If we have libdeflate support and we are asked to read a whole */ + /* strip/tile, then go for using it */ + do + { + TIFFDirectory *td = &tif->tif_dir; + + if (sp->libdeflate_state == 0) + break; + if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB) + break; + + /* Check if we are in the situation where we can use libdeflate */ + if (isTiled(tif)) + { + if (TIFFTileSize64(tif) != (uint64_t)occ) + break; + } + else + { + uint32_t strip_height = td->td_imagelength - tif->tif_row; + if (strip_height > td->td_rowsperstrip) + strip_height = td->td_rowsperstrip; + if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ) + break; + } + + /* Check for overflow */ + if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc) + break; + if ((size_t)occ != (uint64_t)occ) + break; + + /* Go for decompression using libdeflate */ + { + enum libdeflate_result res; + if (sp->libdeflate_dec == NULL) + { + sp->libdeflate_dec = libdeflate_alloc_decompressor(); + if (sp->libdeflate_dec == NULL) + { + break; + } + } + + sp->libdeflate_state = 1; + + res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp, + (size_t)tif->tif_rawcc, op, + (size_t)occ, NULL); + + tif->tif_rawcp += tif->tif_rawcc; + tif->tif_rawcc = 0; + + /* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */ + /* There are odd files in the wild where the last strip, when */ + /* it is smaller in height than td_rowsperstrip, actually contains + */ + /* data for td_rowsperstrip lines. Just ignore that silently. */ + if (res != LIBDEFLATE_SUCCESS && + res != LIBDEFLATE_INSUFFICIENT_SPACE) + { + memset(op, 0, (size_t)occ); + TIFFErrorExtR(tif, module, "Decoding error at scanline %lu", + (unsigned long)tif->tif_row); + sp->read_error = 1; + return 0; + } + + return 1; + } + } while (0); + sp->libdeflate_state = 0; +#endif /* LIBDEFLATE_SUPPORT */ + + sp->stream.next_in = tif->tif_rawcp; + + sp->stream.next_out = op; + assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + do + { + int state; + uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc); + uInt avail_out_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(occ); + sp->stream.avail_in = avail_in_before; + sp->stream.avail_out = avail_out_before; + state = inflate(&sp->stream, Z_PARTIAL_FLUSH); + tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in); + occ -= (avail_out_before - sp->stream.avail_out); + if (state == Z_STREAM_END) + break; + if (state == Z_DATA_ERROR) + { + memset(sp->stream.next_out, 0, (size_t)occ); + TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s", + (unsigned long)tif->tif_row, SAFE_MSG(sp)); + sp->read_error = 1; + return (0); + } + if (state != Z_OK) + { + memset(sp->stream.next_out, 0, (size_t)occ); + TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp)); + sp->read_error = 1; + return (0); + } + } while (occ > 0); + if (occ != 0) + { + TIFFErrorExtR(tif, module, + "Not enough data at scanline %lu (short %" PRIu64 + " bytes)", + (unsigned long)tif->tif_row, (uint64_t)occ); + memset(sp->stream.next_out, 0, (size_t)occ); + sp->read_error = 1; + return (0); + } + + tif->tif_rawcp = sp->stream.next_in; + + return (1); +} + +static int ZIPSetupEncode(TIFF *tif) +{ + static const char module[] = "ZIPSetupEncode"; + ZIPState *sp = ZIPEncoderState(tif); + int cappedQuality; + + assert(sp != NULL); + if (sp->state & ZSTATE_INIT_DECODE) + { + inflateEnd(&sp->stream); + sp->state = 0; + } + + cappedQuality = sp->zipquality; + if (cappedQuality > Z_BEST_COMPRESSION) + cappedQuality = Z_BEST_COMPRESSION; + + if (deflateInit(&sp->stream, cappedQuality) != Z_OK) + { + TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp)); + return (0); + } + else + { + sp->state |= ZSTATE_INIT_ENCODE; + return (1); + } +} + +/* + * Reset encoding state at the start of a strip. + */ +static int ZIPPreEncode(TIFF *tif, uint16_t s) +{ + ZIPState *sp = ZIPEncoderState(tif); + + (void)s; + assert(sp != NULL); + if (sp->state != ZSTATE_INIT_ENCODE) + tif->tif_setupencode(tif); + +#if LIBDEFLATE_SUPPORT + sp->libdeflate_state = -1; +#endif + sp->stream.next_out = tif->tif_rawdata; + assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU + ? (uInt)tif->tif_rawdatasize + : 0xFFFFFFFFU; + return (deflateReset(&sp->stream) == Z_OK); +} + +/* + * Encode a chunk of pixels. + */ +static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "ZIPEncode"; + ZIPState *sp = ZIPEncoderState(tif); + + assert(sp != NULL); + assert(sp->state == ZSTATE_INIT_ENCODE); + + (void)s; + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_state == 1) + return 0; + + /* If we have libdeflate support and we are asked to write a whole */ + /* strip/tile, then go for using it */ + do + { + TIFFDirectory *td = &tif->tif_dir; + + if (sp->libdeflate_state == 0) + break; + if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB) + break; + + /* Libdeflate does not support the 0-compression level */ + if (sp->zipquality == Z_NO_COMPRESSION) + break; + + /* Check if we are in the situation where we can use libdeflate */ + if (isTiled(tif)) + { + if (TIFFTileSize64(tif) != (uint64_t)cc) + break; + } + else + { + uint32_t strip_height = td->td_imagelength - tif->tif_row; + if (strip_height > td->td_rowsperstrip) + strip_height = td->td_rowsperstrip; + if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc) + break; + } + + /* Check for overflow */ + if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize) + break; + if ((size_t)cc != (uint64_t)cc) + break; + + /* Go for compression using libdeflate */ + { + size_t nCompressedBytes; + if (sp->libdeflate_enc == NULL) + { + /* To get results as good as zlib, we asked for an extra */ + /* level of compression */ + sp->libdeflate_enc = libdeflate_alloc_compressor( + sp->zipquality == Z_DEFAULT_COMPRESSION ? 7 + : sp->zipquality >= 6 && sp->zipquality <= 9 + ? sp->zipquality + 1 + : sp->zipquality); + if (sp->libdeflate_enc == NULL) + { + TIFFErrorExtR(tif, module, "Cannot allocate compressor"); + break; + } + } + + /* Make sure the output buffer is large enough for the worse case. + */ + /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */ + /* we've taken a 10% margin over the uncompressed size, which should + */ + /* be large enough even for the the worse case scenario. */ + if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) > + (size_t)tif->tif_rawdatasize) + { + break; + } + + sp->libdeflate_state = 1; + nCompressedBytes = libdeflate_zlib_compress( + sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata, + (size_t)tif->tif_rawdatasize); + + if (nCompressedBytes == 0) + { + TIFFErrorExtR(tif, module, "Encoder error at scanline %lu", + (unsigned long)tif->tif_row); + return 0; + } + + tif->tif_rawcc = nCompressedBytes; + + if (!TIFFFlushData1(tif)) + return 0; + + return 1; + } + } while (0); + sp->libdeflate_state = 0; +#endif /* LIBDEFLATE_SUPPORT */ + + sp->stream.next_in = bp; + assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised, + we need to simplify this code to reflect a ZLib that is likely updated + to deal with 8byte memory sizes, though this code will respond + appropriately even before we simplify it */ + do + { + uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(cc); + sp->stream.avail_in = avail_in_before; + if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) + { + TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp)); + return (0); + } + if (sp->stream.avail_out == 0) + { + tif->tif_rawcc = tif->tif_rawdatasize; + if (!TIFFFlushData1(tif)) + return 0; + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = + TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawdatasize); + } + cc -= (avail_in_before - sp->stream.avail_in); + } while (cc > 0); + return (1); +} + +/* + * Finish off an encoded strip by flushing the last + * string and tacking on an End Of Information code. + */ +static int ZIPPostEncode(TIFF *tif) +{ + static const char module[] = "ZIPPostEncode"; + ZIPState *sp = ZIPEncoderState(tif); + int state; + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_state == 1) + return 1; +#endif + + sp->stream.avail_in = 0; + do + { + state = deflate(&sp->stream, Z_FINISH); + switch (state) + { + case Z_STREAM_END: + case Z_OK: + if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) + { + tif->tif_rawcc = + tif->tif_rawdatasize - sp->stream.avail_out; + if (!TIFFFlushData1(tif)) + return 0; + sp->stream.next_out = tif->tif_rawdata; + sp->stream.avail_out = + (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU + ? (uInt)tif->tif_rawdatasize + : 0xFFFFFFFFU; + } + break; + default: + TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp)); + return (0); + } + } while (state != Z_STREAM_END); + return (1); +} + +static void ZIPCleanup(TIFF *tif) +{ + ZIPState *sp = GetZIPState(tif); + + assert(sp != 0); + + (void)TIFFPredictorCleanup(tif); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + if (sp->state & ZSTATE_INIT_ENCODE) + { + deflateEnd(&sp->stream); + sp->state = 0; + } + else if (sp->state & ZSTATE_INIT_DECODE) + { + inflateEnd(&sp->stream); + sp->state = 0; + } + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_dec) + libdeflate_free_decompressor(sp->libdeflate_dec); + if (sp->libdeflate_enc) + libdeflate_free_compressor(sp->libdeflate_enc); +#endif + + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "ZIPVSetField"; + ZIPState *sp = GetZIPState(tif); + + switch (tag) + { + case TIFFTAG_ZIPQUALITY: + sp->zipquality = (int)va_arg(ap, int); + if (sp->zipquality < Z_DEFAULT_COMPRESSION || + sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL) + { + TIFFErrorExtR( + tif, module, + "Invalid ZipQuality value. Should be in [-1,%d] range", + LIBDEFLATE_MAX_COMPRESSION_LEVEL); + return 0; + } + + if (sp->state & ZSTATE_INIT_ENCODE) + { + int cappedQuality = sp->zipquality; + if (cappedQuality > Z_BEST_COMPRESSION) + cappedQuality = Z_BEST_COMPRESSION; + if (deflateParams(&sp->stream, cappedQuality, + Z_DEFAULT_STRATEGY) != Z_OK) + { + TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp)); + return (0); + } + } + +#if LIBDEFLATE_SUPPORT + if (sp->libdeflate_enc) + { + libdeflate_free_compressor(sp->libdeflate_enc); + sp->libdeflate_enc = NULL; + } +#endif + + return (1); + + case TIFFTAG_DEFLATE_SUBCODEC: + sp->subcodec = (int)va_arg(ap, int); + if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB && + sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE) + { + TIFFErrorExtR(tif, module, "Invalid DeflateCodec value."); + return 0; + } +#if !LIBDEFLATE_SUPPORT + if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE) + { + TIFFErrorExtR(tif, module, + "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE " + "unsupported in this build"); + return 0; + } +#endif + return 1; + + default: + return (*sp->vsetparent)(tif, tag, ap); + } + /*NOTREACHED*/ +} + +static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + ZIPState *sp = GetZIPState(tif); + + switch (tag) + { + case TIFFTAG_ZIPQUALITY: + *va_arg(ap, int *) = sp->zipquality; + break; + + case TIFFTAG_DEFLATE_SUBCODEC: + *va_arg(ap, int *) = sp->subcodec; + break; + + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return (1); +} + +static const TIFFField zipFields[] = { + {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE, + FALSE, "", NULL}, + {TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, + TRUE, FALSE, "", NULL}, +}; + +static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size) +{ + static const char module[] = "TIFF_zalloc"; + TIFF *tif = opaque; + + if (items > ~(size_t)0 / size) + { + TIFFErrorExtR(tif, module, "Overflow"); + return NULL; + } + + return _TIFFmallocExt(tif, items * size); +} + +static void TIFF_zfree(void *opaque, void *ptr) +{ + _TIFFfreeExt((TIFF *)opaque, ptr); +} + +int TIFFInitZIP(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitZIP"; + ZIPState *sp; + + assert((scheme == COMPRESSION_DEFLATE) || + (scheme == COMPRESSION_ADOBE_DEFLATE)); +#ifdef NDEBUG + (void)scheme; +#endif + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) + { + TIFFErrorExtR(tif, module, + "Merging Deflate codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1); + if (tif->tif_data == NULL) + goto bad; + sp = GetZIPState(tif); + sp->stream.zalloc = TIFF_zalloc; + sp->stream.zfree = TIFF_zfree; + sp->stream.opaque = tif; + sp->stream.data_type = Z_BINARY; + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */ + + /* Default values for codec-specific fields */ + sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */ + sp->state = 0; +#if LIBDEFLATE_SUPPORT + sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE; +#else + sp->subcodec = DEFLATE_SUBCODEC_ZLIB; +#endif + + /* + * Install codec methods. + */ + tif->tif_fixuptags = ZIPFixupTags; + tif->tif_setupdecode = ZIPSetupDecode; + tif->tif_predecode = ZIPPreDecode; + tif->tif_decoderow = ZIPDecode; + tif->tif_decodestrip = ZIPDecode; + tif->tif_decodetile = ZIPDecode; + tif->tif_setupencode = ZIPSetupEncode; + tif->tif_preencode = ZIPPreEncode; + tif->tif_postencode = ZIPPostEncode; + tif->tif_encoderow = ZIPEncode; + tif->tif_encodestrip = ZIPEncode; + tif->tif_encodetile = ZIPEncode; + tif->tif_cleanup = ZIPCleanup; + /* + * Setup predictor setup. + */ + (void)TIFFPredictorInit(tif); + return (1); +bad: + TIFFErrorExtR(tif, module, "No space for ZIP state block"); + return (0); +} +#endif /* ZIP_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tif_zstd.c b/cpp/3rd_party/libtiff/tif_zstd.c new file mode 100644 index 0000000000..c828dbf743 --- /dev/null +++ b/cpp/3rd_party/libtiff/tif_zstd.c @@ -0,0 +1,437 @@ +/* + * Copyright (c) 2017, Planet Labs + * Author: + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include "tiffiop.h" +#ifdef ZSTD_SUPPORT +/* + * TIFF Library. + * + * ZSTD Compression Support + * + */ + +#include "tif_predict.h" +#include "zstd.h" + +#include + +/* + * State block for each open TIFF file using ZSTD compression/decompression. + */ +typedef struct +{ + TIFFPredictorState predict; + ZSTD_DStream *dstream; + ZSTD_CStream *cstream; + int compression_level; /* compression level */ + ZSTD_outBuffer out_buffer; + int state; /* state flags */ +#define LSTATE_INIT_DECODE 0x01 +#define LSTATE_INIT_ENCODE 0x02 + + TIFFVGetMethod vgetparent; /* super-class method */ + TIFFVSetMethod vsetparent; /* super-class method */ +} ZSTDState; + +#define GetZSTDState(tif) ((ZSTDState *)(tif)->tif_data) +#define ZSTDDecoderState(tif) GetZSTDState(tif) +#define ZSTDEncoderState(tif) GetZSTDState(tif) + +static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); +static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); + +static int ZSTDFixupTags(TIFF *tif) +{ + (void)tif; + return 1; +} + +static int ZSTDSetupDecode(TIFF *tif) +{ + ZSTDState *sp = ZSTDDecoderState(tif); + + assert(sp != NULL); + + /* if we were last encoding, terminate this mode */ + if (sp->state & LSTATE_INIT_ENCODE) + { + ZSTD_freeCStream(sp->cstream); + sp->cstream = NULL; + sp->state = 0; + } + + sp->state |= LSTATE_INIT_DECODE; + return 1; +} + +/* + * Setup state for decoding a strip. + */ +static int ZSTDPreDecode(TIFF *tif, uint16_t s) +{ + static const char module[] = "ZSTDPreDecode"; + ZSTDState *sp = ZSTDDecoderState(tif); + size_t zstd_ret; + + (void)s; + assert(sp != NULL); + + if ((sp->state & LSTATE_INIT_DECODE) == 0) + tif->tif_setupdecode(tif); + + if (sp->dstream == NULL) + { + sp->dstream = ZSTD_createDStream(); + if (sp->dstream == NULL) + { + TIFFErrorExtR(tif, module, "Cannot allocate decompression stream"); + return 0; + } + } + + zstd_ret = ZSTD_initDStream(sp->dstream); + if (ZSTD_isError(zstd_ret)) + { + TIFFErrorExtR(tif, module, "Error in ZSTD_initDStream(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + + return 1; +} + +static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) +{ + static const char module[] = "ZSTDDecode"; + ZSTDState *sp = ZSTDDecoderState(tif); + ZSTD_inBuffer in_buffer; + ZSTD_outBuffer out_buffer; + size_t zstd_ret; + + (void)s; + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_DECODE); + + in_buffer.src = tif->tif_rawcp; + in_buffer.size = (size_t)tif->tif_rawcc; + in_buffer.pos = 0; + + out_buffer.dst = op; + out_buffer.size = (size_t)occ; + out_buffer.pos = 0; + + do + { + zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer, &in_buffer); + if (ZSTD_isError(zstd_ret)) + { + memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos); + TIFFErrorExtR(tif, module, "Error in ZSTD_decompressStream(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + } while (zstd_ret != 0 && in_buffer.pos < in_buffer.size && + out_buffer.pos < out_buffer.size); + + if (out_buffer.pos < (size_t)occ) + { + memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos); + TIFFErrorExtR(tif, module, + "Not enough data at scanline %lu (short %lu bytes)", + (unsigned long)tif->tif_row, + (unsigned long)((size_t)occ - out_buffer.pos)); + return 0; + } + + tif->tif_rawcp += in_buffer.pos; + tif->tif_rawcc -= in_buffer.pos; + + return 1; +} + +static int ZSTDSetupEncode(TIFF *tif) +{ + ZSTDState *sp = ZSTDEncoderState(tif); + + assert(sp != NULL); + if (sp->state & LSTATE_INIT_DECODE) + { + ZSTD_freeDStream(sp->dstream); + sp->dstream = NULL; + sp->state = 0; + } + + sp->state |= LSTATE_INIT_ENCODE; + return 1; +} + +/* + * Reset encoding state at the start of a strip. + */ +static int ZSTDPreEncode(TIFF *tif, uint16_t s) +{ + static const char module[] = "ZSTDPreEncode"; + ZSTDState *sp = ZSTDEncoderState(tif); + size_t zstd_ret; + + (void)s; + assert(sp != NULL); + if (sp->state != LSTATE_INIT_ENCODE) + tif->tif_setupencode(tif); + + if (sp->cstream == NULL) + { + sp->cstream = ZSTD_createCStream(); + if (sp->cstream == NULL) + { + TIFFErrorExtR(tif, module, "Cannot allocate compression stream"); + return 0; + } + } + + zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level); + if (ZSTD_isError(zstd_ret)) + { + TIFFErrorExtR(tif, module, "Error in ZSTD_initCStream(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + + sp->out_buffer.dst = tif->tif_rawdata; + sp->out_buffer.size = (size_t)tif->tif_rawdatasize; + sp->out_buffer.pos = 0; + + return 1; +} + +/* + * Encode a chunk of pixels. + */ +static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) +{ + static const char module[] = "ZSTDEncode"; + ZSTDState *sp = ZSTDEncoderState(tif); + ZSTD_inBuffer in_buffer; + size_t zstd_ret; + + assert(sp != NULL); + assert(sp->state == LSTATE_INIT_ENCODE); + + (void)s; + + in_buffer.src = bp; + in_buffer.size = (size_t)cc; + in_buffer.pos = 0; + + do + { + zstd_ret = + ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer); + if (ZSTD_isError(zstd_ret)) + { + TIFFErrorExtR(tif, module, "Error in ZSTD_compressStream(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + if (sp->out_buffer.pos == sp->out_buffer.size) + { + tif->tif_rawcc = tif->tif_rawdatasize; + if (!TIFFFlushData1(tif)) + return 0; + sp->out_buffer.dst = tif->tif_rawcp; + sp->out_buffer.pos = 0; + } + } while (in_buffer.pos < in_buffer.size); + + return 1; +} + +/* + * Finish off an encoded strip by flushing it. + */ +static int ZSTDPostEncode(TIFF *tif) +{ + static const char module[] = "ZSTDPostEncode"; + ZSTDState *sp = ZSTDEncoderState(tif); + size_t zstd_ret; + + do + { + zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer); + if (ZSTD_isError(zstd_ret)) + { + TIFFErrorExtR(tif, module, "Error in ZSTD_endStream(): %s", + ZSTD_getErrorName(zstd_ret)); + return 0; + } + if (sp->out_buffer.pos > 0) + { + tif->tif_rawcc = sp->out_buffer.pos; + if (!TIFFFlushData1(tif)) + return 0; + sp->out_buffer.dst = tif->tif_rawcp; + sp->out_buffer.pos = 0; + } + } while (zstd_ret != 0); + return 1; +} + +static void ZSTDCleanup(TIFF *tif) +{ + ZSTDState *sp = GetZSTDState(tif); + + assert(sp != 0); + + (void)TIFFPredictorCleanup(tif); + + tif->tif_tagmethods.vgetfield = sp->vgetparent; + tif->tif_tagmethods.vsetfield = sp->vsetparent; + + if (sp->dstream) + { + ZSTD_freeDStream(sp->dstream); + sp->dstream = NULL; + } + if (sp->cstream) + { + ZSTD_freeCStream(sp->cstream); + sp->cstream = NULL; + } + _TIFFfreeExt(tif, sp); + tif->tif_data = NULL; + + _TIFFSetDefaultCompressionState(tif); +} + +static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap) +{ + static const char module[] = "ZSTDVSetField"; + ZSTDState *sp = GetZSTDState(tif); + + switch (tag) + { + case TIFFTAG_ZSTD_LEVEL: + sp->compression_level = (int)va_arg(ap, int); + if (sp->compression_level <= 0 || + sp->compression_level > ZSTD_maxCLevel()) + { + TIFFWarningExtR(tif, module, + "ZSTD_LEVEL should be between 1 and %d", + ZSTD_maxCLevel()); + } + return 1; + default: + return (*sp->vsetparent)(tif, tag, ap); + } + /*NOTREACHED*/ +} + +static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap) +{ + ZSTDState *sp = GetZSTDState(tif); + + switch (tag) + { + case TIFFTAG_ZSTD_LEVEL: + *va_arg(ap, int *) = sp->compression_level; + break; + default: + return (*sp->vgetparent)(tif, tag, ap); + } + return 1; +} + +static const TIFFField ZSTDFields[] = { + {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE, + FALSE, "ZSTD compression_level", NULL}, +}; + +int TIFFInitZSTD(TIFF *tif, int scheme) +{ + static const char module[] = "TIFFInitZSTD"; + ZSTDState *sp; + + (void)scheme; + assert(scheme == COMPRESSION_ZSTD); + + /* + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields))) + { + TIFFErrorExtR(tif, module, "Merging ZSTD codec-specific tags failed"); + return 0; + } + + /* + * Allocate state block so tag methods have storage to record values. + */ + tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(ZSTDState)); + if (tif->tif_data == NULL) + goto bad; + sp = GetZSTDState(tif); + + /* + * Override parent get/set field methods. + */ + sp->vgetparent = tif->tif_tagmethods.vgetfield; + tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */ + sp->vsetparent = tif->tif_tagmethods.vsetfield; + tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */ + + /* Default values for codec-specific fields */ + sp->compression_level = 9; /* default comp. level */ + sp->state = 0; + sp->dstream = 0; + sp->cstream = 0; + sp->out_buffer.dst = NULL; + sp->out_buffer.size = 0; + sp->out_buffer.pos = 0; + + /* + * Install codec methods. + */ + tif->tif_fixuptags = ZSTDFixupTags; + tif->tif_setupdecode = ZSTDSetupDecode; + tif->tif_predecode = ZSTDPreDecode; + tif->tif_decoderow = ZSTDDecode; + tif->tif_decodestrip = ZSTDDecode; + tif->tif_decodetile = ZSTDDecode; + tif->tif_setupencode = ZSTDSetupEncode; + tif->tif_preencode = ZSTDPreEncode; + tif->tif_postencode = ZSTDPostEncode; + tif->tif_encoderow = ZSTDEncode; + tif->tif_encodestrip = ZSTDEncode; + tif->tif_encodetile = ZSTDEncode; + tif->tif_cleanup = ZSTDCleanup; + /* + * Setup predictor setup. + */ + (void)TIFFPredictorInit(tif); + return 1; +bad: + TIFFErrorExtR(tif, module, "No space for ZSTD state block"); + return 0; +} +#endif /* ZSTD_SUPPORT */ diff --git a/cpp/3rd_party/libtiff/tiff.h b/cpp/3rd_party/libtiff/tiff.h new file mode 100644 index 0000000000..980e8e8f52 --- /dev/null +++ b/cpp/3rd_party/libtiff/tiff.h @@ -0,0 +1,900 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _TIFF_ +#define _TIFF_ + +#include "tiffconf.h" + +/* + * Tag Image File Format (TIFF) + * + * Based on Rev 6.0 from: + * Developer's Desk + * Aldus Corporation + * 411 First Ave. South + * Suite 200 + * Seattle, WA 98104 + * 206-622-5500 + * + * (http://partners.adobe.com/asn/developer/PDFS/TN/TIFF6.pdf) + * + * For BigTIFF design notes see the following links + * http://www.remotesensing.org/libtiff/bigtiffdesign.html + * http://www.awaresystems.be/imaging/tiff/bigtiff.html + */ + +#define TIFF_VERSION_CLASSIC 42 +#define TIFF_VERSION_BIG 43 + +#define TIFF_BIGENDIAN 0x4d4d +#define TIFF_LITTLEENDIAN 0x4949 +#define MDI_LITTLEENDIAN 0x5045 +#define MDI_BIGENDIAN 0x4550 + +/* + * Intrinsic data types required by the file format: + * + * 8-bit quantities int8_t/uint_8_t + * 16-bit quantities int16_t/uint_16_t + * 32-bit quantities int32_t/uint_32_t + * 64-bit quantities int64_t/uint_64_t + * strings unsigned char* + */ +#ifdef __GNUC__ +#define TIFF_GCC_DEPRECATED __attribute__((deprecated)) +#else +#define TIFF_GCC_DEPRECATED +#endif +#ifdef _MSC_VER +#define TIFF_MSC_DEPRECATED \ + __declspec(deprecated("libtiff type deprecated; please use corresponding " \ + "C99 stdint.h type")) +#else +#define TIFF_MSC_DEPRECATED +#endif + +#ifndef TIFF_DISABLE_DEPRECATED +typedef TIFF_MSC_DEPRECATED int8_t int8 TIFF_GCC_DEPRECATED; +typedef TIFF_MSC_DEPRECATED uint8_t uint8 TIFF_GCC_DEPRECATED; + +typedef TIFF_MSC_DEPRECATED int16_t int16 TIFF_GCC_DEPRECATED; +typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED; + +typedef TIFF_MSC_DEPRECATED int32_t int32 TIFF_GCC_DEPRECATED; +typedef TIFF_MSC_DEPRECATED uint32_t uint32 TIFF_GCC_DEPRECATED; + +typedef TIFF_MSC_DEPRECATED int64_t int64 TIFF_GCC_DEPRECATED; +typedef TIFF_MSC_DEPRECATED uint64_t uint64 TIFF_GCC_DEPRECATED; +#endif /* TIFF_DISABLE_DEPRECATED */ + +/* + * Some types as promoted in a variable argument list + * We use uint16_vap rather then directly using int, because this way + * we document the type we actually want to pass through, conceptually, + * rather then confusing the issue by merely stating the type it gets + * promoted to + */ + +typedef int uint16_vap; + +/* + * TIFF header. + */ +typedef struct +{ + uint16_t tiff_magic; /* magic number (defines byte order) */ + uint16_t tiff_version; /* TIFF version number */ +} TIFFHeaderCommon; +typedef struct +{ + uint16_t tiff_magic; /* magic number (defines byte order) */ + uint16_t tiff_version; /* TIFF version number */ + uint32_t tiff_diroff; /* byte offset to first directory */ +} TIFFHeaderClassic; +typedef struct +{ + uint16_t tiff_magic; /* magic number (defines byte order) */ + uint16_t tiff_version; /* TIFF version number */ + uint16_t tiff_offsetsize; /* size of offsets, should be 8 */ + uint16_t tiff_unused; /* unused word, should be 0 */ + uint64_t tiff_diroff; /* byte offset to first directory */ +} TIFFHeaderBig; + +/* + * NB: In the comments below, + * - items marked with a + are obsoleted by revision 5.0, + * - items marked with a ! are introduced in revision 6.0. + * - items marked with a % are introduced post revision 6.0. + * - items marked with a $ are obsoleted by revision 6.0. + * - items marked with a & are introduced by Adobe DNG specification. + */ + +/* + * Tag data type information. + * + * Note: RATIONALs are the ratio of two 32-bit integer values. + *--: + * Note2: TIFF_IFD8 data type is used in tiffFields[]-tag definition in order to + distinguish the write-handling of those tags between ClassicTIFF and BigTiff: + For ClassicTIFF libtiff writes a 32-bit value and the TIFF_IFD + type-id into the file For BigTIFF libtiff writes a 64-bit value and the + TIFF_IFD8 type-id into the file + */ +typedef enum +{ + TIFF_NOTYPE = 0, /* placeholder */ + TIFF_BYTE = 1, /* 8-bit unsigned integer */ + TIFF_ASCII = 2, /* 8-bit bytes w/ last byte null */ + TIFF_SHORT = 3, /* 16-bit unsigned integer */ + TIFF_LONG = 4, /* 32-bit unsigned integer */ + TIFF_RATIONAL = 5, /* 64-bit unsigned fraction */ + TIFF_SBYTE = 6, /* !8-bit signed integer */ + TIFF_UNDEFINED = 7, /* !8-bit untyped data */ + TIFF_SSHORT = 8, /* !16-bit signed integer */ + TIFF_SLONG = 9, /* !32-bit signed integer */ + TIFF_SRATIONAL = 10, /* !64-bit signed fraction */ + TIFF_FLOAT = 11, /* !32-bit IEEE floating point */ + TIFF_DOUBLE = 12, /* !64-bit IEEE floating point */ + TIFF_IFD = 13, /* %32-bit unsigned integer (offset) */ + TIFF_LONG8 = 16, /* BigTIFF 64-bit unsigned integer */ + TIFF_SLONG8 = 17, /* BigTIFF 64-bit signed integer */ + TIFF_IFD8 = 18 /* BigTIFF 64-bit unsigned integer (offset) */ +} TIFFDataType; + +/* + * TIFF Tag Definitions. + */ +/* clang-format off */ /* for better readability of tag comments */ +#define TIFFTAG_SUBFILETYPE 254 /* subfile data descriptor */ +#define FILETYPE_REDUCEDIMAGE 0x1 /* reduced resolution version */ +#define FILETYPE_PAGE 0x2 /* one page of many */ +#define FILETYPE_MASK 0x4 /* transparency mask */ +#define TIFFTAG_OSUBFILETYPE 255 /* +kind of data in subfile */ +#define OFILETYPE_IMAGE 1 /* full resolution image data */ +#define OFILETYPE_REDUCEDIMAGE 2 /* reduced size image data */ +#define OFILETYPE_PAGE 3 /* one page of many */ +#define TIFFTAG_IMAGEWIDTH 256 /* image width in pixels */ +#define TIFFTAG_IMAGELENGTH 257 /* image height in pixels */ +#define TIFFTAG_BITSPERSAMPLE 258 /* bits per channel (sample) */ +#define TIFFTAG_COMPRESSION 259 /* data compression technique */ +#define COMPRESSION_NONE 1 /* dump mode */ +#define COMPRESSION_CCITTRLE 2 /* CCITT modified Huffman RLE */ +#define COMPRESSION_CCITTFAX3 3 /* CCITT Group 3 fax encoding */ +#define COMPRESSION_CCITT_T4 3 /* CCITT T.4 (TIFF 6 name) */ +#define COMPRESSION_CCITTFAX4 4 /* CCITT Group 4 fax encoding */ +#define COMPRESSION_CCITT_T6 4 /* CCITT T.6 (TIFF 6 name) */ +#define COMPRESSION_LZW 5 /* Lempel-Ziv & Welch */ +#define COMPRESSION_OJPEG 6 /* !6.0 JPEG */ +#define COMPRESSION_JPEG 7 /* %JPEG DCT compression */ +#define COMPRESSION_T85 9 /* !TIFF/FX T.85 JBIG compression */ +#define COMPRESSION_T43 10 /* !TIFF/FX T.43 colour by layered JBIG compression */ +#define COMPRESSION_NEXT 32766 /* NeXT 2-bit RLE */ +#define COMPRESSION_CCITTRLEW 32771 /* #1 w/ word alignment */ +#define COMPRESSION_PACKBITS 32773 /* Macintosh RLE */ +#define COMPRESSION_THUNDERSCAN 32809 /* ThunderScan RLE */ +/* codes 32895-32898 are reserved for ANSI IT8 TIFF/IT */ +#define COMPRESSION_DCS 32947 /* Kodak DCS encoding */ +#define COMPRESSION_JBIG 34661 /* ISO JBIG */ +#define COMPRESSION_SGILOG 34676 /* SGI Log Luminance RLE */ +#define COMPRESSION_SGILOG24 34677 /* SGI Log 24-bit packed */ +#define COMPRESSION_JP2000 34712 /* Leadtools JPEG2000 */ +#define COMPRESSION_LERC 34887 /* ESRI Lerc codec: https://github.com/Esri/lerc */ +/* compression codes 34887-34889 are reserved for ESRI */ +#define COMPRESSION_LZMA 34925 /* LZMA2 */ +#define COMPRESSION_ZSTD 50000 /* ZSTD: WARNING not registered in Adobe-maintained registry */ +#define COMPRESSION_WEBP 50001 /* WEBP: WARNING not registered in Adobe-maintained registry */ +#define COMPRESSION_JXL 50002 /* JPEGXL: WARNING not registered in Adobe-maintained registry */ +#define COMPRESSION_JXL_DNG_1_7 52546 /* JPEGXL from DNG 1.7 specification */ +#define TIFFTAG_PHOTOMETRIC 262 /* photometric interpretation */ +#define PHOTOMETRIC_MINISWHITE 0 /* min value is white */ +#define PHOTOMETRIC_MINISBLACK 1 /* min value is black */ +#define PHOTOMETRIC_RGB 2 /* RGB color model */ +#define PHOTOMETRIC_PALETTE 3 /* color map indexed */ +#define PHOTOMETRIC_MASK 4 /* $holdout mask */ +#define PHOTOMETRIC_SEPARATED 5 /* !color separations */ +#define PHOTOMETRIC_YCBCR 6 /* !CCIR 601 */ +#define PHOTOMETRIC_CIELAB 8 /* !1976 CIE L*a*b* */ +#define PHOTOMETRIC_ICCLAB 9 /* ICC L*a*b* [Adobe TIFF Technote 4] */ +#define PHOTOMETRIC_ITULAB 10 /* ITU L*a*b* */ +#define PHOTOMETRIC_CFA 32803 /* color filter array */ +#define PHOTOMETRIC_LOGL 32844 /* CIE Log2(L) */ +#define PHOTOMETRIC_LOGLUV 32845 /* CIE Log2(L) (u',v') */ +#define TIFFTAG_THRESHHOLDING 263 /* +thresholding used on data */ +#define THRESHHOLD_BILEVEL 1 /* b&w art scan */ +#define THRESHHOLD_HALFTONE 2 /* or dithered scan */ +#define THRESHHOLD_ERRORDIFFUSE 3 /* usually floyd-steinberg */ +#define TIFFTAG_CELLWIDTH 264 /* +dithering matrix width */ +#define TIFFTAG_CELLLENGTH 265 /* +dithering matrix height */ +#define TIFFTAG_FILLORDER 266 /* data order within a byte */ +#define FILLORDER_MSB2LSB 1 /* most significant -> least */ +#define FILLORDER_LSB2MSB 2 /* least significant -> most */ +#define TIFFTAG_DOCUMENTNAME 269 /* name of doc. image is from */ +#define TIFFTAG_IMAGEDESCRIPTION 270 /* info about image */ +#define TIFFTAG_MAKE 271 /* scanner manufacturer name */ +#define TIFFTAG_MODEL 272 /* scanner model name/number */ +#define TIFFTAG_STRIPOFFSETS 273 /* offsets to data strips */ +#define TIFFTAG_ORIENTATION 274 /* +image orientation */ +#define ORIENTATION_TOPLEFT 1 /* row 0 top, col 0 lhs */ +#define ORIENTATION_TOPRIGHT 2 /* row 0 top, col 0 rhs */ +#define ORIENTATION_BOTRIGHT 3 /* row 0 bottom, col 0 rhs */ +#define ORIENTATION_BOTLEFT 4 /* row 0 bottom, col 0 lhs */ +#define ORIENTATION_LEFTTOP 5 /* row 0 lhs, col 0 top */ +#define ORIENTATION_RIGHTTOP 6 /* row 0 rhs, col 0 top */ +#define ORIENTATION_RIGHTBOT 7 /* row 0 rhs, col 0 bottom */ +#define ORIENTATION_LEFTBOT 8 /* row 0 lhs, col 0 bottom */ +#define TIFFTAG_SAMPLESPERPIXEL 277 /* samples per pixel */ +#define TIFFTAG_ROWSPERSTRIP 278 /* rows per strip of data */ +#define TIFFTAG_STRIPBYTECOUNTS 279 /* bytes counts for strips */ +#define TIFFTAG_MINSAMPLEVALUE 280 /* +minimum sample value */ +#define TIFFTAG_MAXSAMPLEVALUE 281 /* +maximum sample value */ +#define TIFFTAG_XRESOLUTION 282 /* pixels/resolution in x */ +#define TIFFTAG_YRESOLUTION 283 /* pixels/resolution in y */ +#define TIFFTAG_PLANARCONFIG 284 /* storage organization */ +#define PLANARCONFIG_CONTIG 1 /* single image plane */ +#define PLANARCONFIG_SEPARATE 2 /* separate planes of data */ +#define TIFFTAG_PAGENAME 285 /* page name image is from */ +#define TIFFTAG_XPOSITION 286 /* x page offset of image lhs */ +#define TIFFTAG_YPOSITION 287 /* y page offset of image lhs */ +#define TIFFTAG_FREEOFFSETS 288 /* +byte offset to free block */ +#define TIFFTAG_FREEBYTECOUNTS 289 /* +sizes of free blocks */ +#define TIFFTAG_GRAYRESPONSEUNIT 290 /* $gray scale curve accuracy */ +#define GRAYRESPONSEUNIT_10S 1 /* tenths of a unit */ +#define GRAYRESPONSEUNIT_100S 2 /* hundredths of a unit */ +#define GRAYRESPONSEUNIT_1000S 3 /* thousandths of a unit */ +#define GRAYRESPONSEUNIT_10000S 4 /* ten-thousandths of a unit */ +#define GRAYRESPONSEUNIT_100000S 5 /* hundred-thousandths */ +#define TIFFTAG_GRAYRESPONSECURVE 291 /* $gray scale response curve */ +#define TIFFTAG_GROUP3OPTIONS 292 /* 32 flag bits */ +#define TIFFTAG_T4OPTIONS 292 /* TIFF 6.0 proper name alias */ +#define GROUP3OPT_2DENCODING 0x1 /* 2-dimensional coding */ +#define GROUP3OPT_UNCOMPRESSED 0x2 /* data not compressed */ +#define GROUP3OPT_FILLBITS 0x4 /* fill to byte boundary */ +#define TIFFTAG_GROUP4OPTIONS 293 /* 32 flag bits */ +#define TIFFTAG_T6OPTIONS 293 /* TIFF 6.0 proper name */ +#define GROUP4OPT_UNCOMPRESSED 0x2 /* data not compressed */ +#define TIFFTAG_RESOLUTIONUNIT 296 /* units of resolutions */ +#define RESUNIT_NONE 1 /* no meaningful units */ +#define RESUNIT_INCH 2 /* english */ +#define RESUNIT_CENTIMETER 3 /* metric */ +#define TIFFTAG_PAGENUMBER 297 /* page numbers of multi-page */ +#define TIFFTAG_COLORRESPONSEUNIT 300 /* $color curve accuracy */ +#define COLORRESPONSEUNIT_10S 1 /* tenths of a unit */ +#define COLORRESPONSEUNIT_100S 2 /* hundredths of a unit */ +#define COLORRESPONSEUNIT_1000S 3 /* thousandths of a unit */ +#define COLORRESPONSEUNIT_10000S 4 /* ten-thousandths of a unit */ +#define COLORRESPONSEUNIT_100000S 5 /* hundred-thousandths */ +#define TIFFTAG_TRANSFERFUNCTION 301 /* !colorimetry info */ +#define TIFFTAG_SOFTWARE 305 /* name & release */ +#define TIFFTAG_DATETIME 306 /* creation date and time */ +#define TIFFTAG_ARTIST 315 /* creator of image */ +#define TIFFTAG_HOSTCOMPUTER 316 /* machine where created */ +#define TIFFTAG_PREDICTOR 317 /* prediction scheme w/ LZW */ +#define PREDICTOR_NONE 1 /* no prediction scheme used */ +#define PREDICTOR_HORIZONTAL 2 /* horizontal differencing */ +#define PREDICTOR_FLOATINGPOINT 3 /* floating point predictor */ +#define TIFFTAG_WHITEPOINT 318 /* image white point */ +#define TIFFTAG_PRIMARYCHROMATICITIES 319 /* !primary chromaticities */ +#define TIFFTAG_COLORMAP 320 /* RGB map for palette image */ +#define TIFFTAG_HALFTONEHINTS 321 /* !highlight+shadow info */ +#define TIFFTAG_TILEWIDTH 322 /* !tile width in pixels */ +#define TIFFTAG_TILELENGTH 323 /* !tile height in pixels */ +#define TIFFTAG_TILEOFFSETS 324 /* !offsets to data tiles */ +#define TIFFTAG_TILEBYTECOUNTS 325 /* !byte counts for tiles */ +#define TIFFTAG_BADFAXLINES 326 /* lines w/ wrong pixel count */ +#define TIFFTAG_CLEANFAXDATA 327 /* regenerated line info */ +#define CLEANFAXDATA_CLEAN 0 /* no errors detected */ +#define CLEANFAXDATA_REGENERATED 1 /* receiver regenerated lines */ +#define CLEANFAXDATA_UNCLEAN 2 /* uncorrected errors exist */ +#define TIFFTAG_CONSECUTIVEBADFAXLINES 328 /* max consecutive bad lines */ +#define TIFFTAG_SUBIFD 330 /* subimage descriptors */ +#define TIFFTAG_INKSET 332 /* !inks in separated image */ +#define INKSET_CMYK 1 /* !cyan-magenta-yellow-black color */ +#define INKSET_MULTIINK 2 /* !multi-ink or hi-fi color */ +#define TIFFTAG_INKNAMES 333 /* !ascii names of inks */ +#define TIFFTAG_NUMBEROFINKS 334 /* !number of inks */ +#define TIFFTAG_DOTRANGE 336 /* !0% and 100% dot codes */ +#define TIFFTAG_TARGETPRINTER 337 /* !separation target */ +#define TIFFTAG_EXTRASAMPLES 338 /* !info about extra samples */ +#define EXTRASAMPLE_UNSPECIFIED 0 /* !unspecified data */ +#define EXTRASAMPLE_ASSOCALPHA 1 /* !associated alpha data */ +#define EXTRASAMPLE_UNASSALPHA 2 /* !unassociated alpha data */ +#define TIFFTAG_SAMPLEFORMAT 339 /* !data sample format */ +#define SAMPLEFORMAT_UINT 1 /* !unsigned integer data */ +#define SAMPLEFORMAT_INT 2 /* !signed integer data */ +#define SAMPLEFORMAT_IEEEFP 3 /* !IEEE floating point data */ +#define SAMPLEFORMAT_VOID 4 /* !untyped data */ +#define SAMPLEFORMAT_COMPLEXINT 5 /* !complex signed int */ +#define SAMPLEFORMAT_COMPLEXIEEEFP 6 /* !complex ieee floating */ +#define TIFFTAG_SMINSAMPLEVALUE 340 /* !variable MinSampleValue */ +#define TIFFTAG_SMAXSAMPLEVALUE 341 /* !variable MaxSampleValue */ +#define TIFFTAG_CLIPPATH 343 /* %ClipPath [Adobe TIFF technote 2] */ +#define TIFFTAG_XCLIPPATHUNITS 344 /* %XClipPathUnits [Adobe TIFF technote 2] */ +#define TIFFTAG_YCLIPPATHUNITS 345 /* %YClipPathUnits [Adobe TIFF technote 2] */ +#define TIFFTAG_INDEXED 346 /* %Indexed [Adobe TIFF Technote 3] */ +#define TIFFTAG_JPEGTABLES 347 /* %JPEG table stream */ +#define TIFFTAG_OPIPROXY 351 /* %OPI Proxy [Adobe TIFF technote] */ +/* Tags 400-435 are from the TIFF/FX spec */ +#define TIFFTAG_GLOBALPARAMETERSIFD 400 /* ! */ +#define TIFFTAG_PROFILETYPE 401 /* ! */ +#define PROFILETYPE_UNSPECIFIED 0 /* ! */ +#define PROFILETYPE_G3_FAX 1 /* ! */ +#define TIFFTAG_FAXPROFILE 402 /* ! */ +#define FAXPROFILE_S 1 /* !TIFF/FX FAX profile S */ +#define FAXPROFILE_F 2 /* !TIFF/FX FAX profile F */ +#define FAXPROFILE_J 3 /* !TIFF/FX FAX profile J */ +#define FAXPROFILE_C 4 /* !TIFF/FX FAX profile C */ +#define FAXPROFILE_L 5 /* !TIFF/FX FAX profile L */ +#define FAXPROFILE_M 6 /* !TIFF/FX FAX profile LM */ +#define TIFFTAG_CODINGMETHODS 403 /* !TIFF/FX coding methods */ +#define CODINGMETHODS_T4_1D (1 << 1) /* !T.4 1D */ +#define CODINGMETHODS_T4_2D (1 << 2) /* !T.4 2D */ +#define CODINGMETHODS_T6 (1 << 3) /* !T.6 */ +#define CODINGMETHODS_T85 (1 << 4) /* !T.85 JBIG */ +#define CODINGMETHODS_T42 (1 << 5) /* !T.42 JPEG */ +#define CODINGMETHODS_T43 (1 << 6) /* !T.43 colour by layered JBIG */ +#define TIFFTAG_VERSIONYEAR 404 /* !TIFF/FX version year */ +#define TIFFTAG_MODENUMBER 405 /* !TIFF/FX mode number */ +#define TIFFTAG_DECODE 433 /* !TIFF/FX decode */ +#define TIFFTAG_IMAGEBASECOLOR 434 /* !TIFF/FX image base colour */ +#define TIFFTAG_T82OPTIONS 435 /* !TIFF/FX T.82 options */ +/* + * Tags 512-521 are obsoleted by Technical Note #2 which specifies a + * revised JPEG-in-TIFF scheme. + */ +#define TIFFTAG_JPEGPROC 512 /* !JPEG processing algorithm */ +#define JPEGPROC_BASELINE 1 /* !baseline sequential */ +#define JPEGPROC_LOSSLESS 14 /* !Huffman coded lossless */ +#define TIFFTAG_JPEGIFOFFSET 513 /* !pointer to SOI marker */ +#define TIFFTAG_JPEGIFBYTECOUNT 514 /* !JFIF stream length */ +#define TIFFTAG_JPEGRESTARTINTERVAL 515 /* !restart interval length */ +#define TIFFTAG_JPEGLOSSLESSPREDICTORS 517 /* !lossless proc predictor */ +#define TIFFTAG_JPEGPOINTTRANSFORM 518 /* !lossless point transform */ +#define TIFFTAG_JPEGQTABLES 519 /* !Q matrix offsets */ +#define TIFFTAG_JPEGDCTABLES 520 /* !DCT table offsets */ +#define TIFFTAG_JPEGACTABLES 521 /* !AC coefficient offsets */ +#define TIFFTAG_YCBCRCOEFFICIENTS 529 /* !RGB -> YCbCr transform */ +#define TIFFTAG_YCBCRSUBSAMPLING 530 /* !YCbCr subsampling factors */ +#define TIFFTAG_YCBCRPOSITIONING 531 /* !subsample positioning */ +#define YCBCRPOSITION_CENTERED 1 /* !as in PostScript Level 2 */ +#define YCBCRPOSITION_COSITED 2 /* !as in CCIR 601-1 */ +#define TIFFTAG_REFERENCEBLACKWHITE 532 /* !colorimetry info */ +#define TIFFTAG_STRIPROWCOUNTS 559 /* !TIFF/FX strip row counts */ +#define TIFFTAG_XMLPACKET 700 /* %XML packet [Adobe XMP Specification, January 2004 */ +#define TIFFTAG_OPIIMAGEID 32781 /* %OPI ImageID [Adobe TIFF technote] */ +/* For eiStream Annotation Specification, Version 1.00.06 see + * http://web.archive.org/web/20050309141348/http://www.kofile.com/support%20pro/faqs/annospec.htm */ +#define TIFFTAG_TIFFANNOTATIONDATA 32932 +/* tags 32952-32956 are private tags registered to Island Graphics */ +#define TIFFTAG_REFPTS 32953 /* image reference points */ +#define TIFFTAG_REGIONTACKPOINT 32954 /* region-xform tack point */ +#define TIFFTAG_REGIONWARPCORNERS 32955 /* warp quadrilateral */ +#define TIFFTAG_REGIONAFFINE 32956 /* affine transformation mat */ +/* tags 32995-32999 are private tags registered to SGI */ +#define TIFFTAG_MATTEING 32995 /* $use ExtraSamples */ +#define TIFFTAG_DATATYPE 32996 /* $use SampleFormat */ +#define TIFFTAG_IMAGEDEPTH 32997 /* z depth of image */ +#define TIFFTAG_TILEDEPTH 32998 /* z depth/data tile */ +/* tags 33300-33309 are private tags registered to Pixar */ +/* + * TIFFTAG_PIXAR_IMAGEFULLWIDTH and TIFFTAG_PIXAR_IMAGEFULLLENGTH + * are set when an image has been cropped out of a larger image. + * They reflect the size of the original uncropped image. + * The TIFFTAG_XPOSITION and TIFFTAG_YPOSITION can be used + * to determine the position of the smaller image in the larger one. + */ +#define TIFFTAG_PIXAR_IMAGEFULLWIDTH 33300 /* full image size in x */ +#define TIFFTAG_PIXAR_IMAGEFULLLENGTH 33301 /* full image size in y */ +/* Tags 33302-33306 are used to identify special image modes and data + * used by Pixar's texture formats. + */ +#define TIFFTAG_PIXAR_TEXTUREFORMAT 33302 /* texture map format */ +#define TIFFTAG_PIXAR_WRAPMODES 33303 /* s & t wrap modes */ +#define TIFFTAG_PIXAR_FOVCOT 33304 /* cotan(fov) for env. maps */ +#define TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN 33305 +#define TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA 33306 +/* tag 33405 is a private tag registered to Eastman Kodak */ +#define TIFFTAG_WRITERSERIALNUMBER 33405 /* device serial number */ +#define TIFFTAG_CFAREPEATPATTERNDIM 33421 /* (alias for TIFFTAG_EP_CFAREPEATPATTERNDIM)*/ +#define TIFFTAG_CFAPATTERN 33422 /* (alias for TIFFTAG_EP_CFAPATTERN) */ +#define TIFFTAG_BATTERYLEVEL 33423 /* (alias for TIFFTAG_EP_BATTERYLEVEL) */ +/* tag 33432 is listed in the 6.0 spec w/ unknown ownership */ +#define TIFFTAG_COPYRIGHT 33432 /* copyright string */ +/* Tags 33445-33452 are used for Molecular Dynamics GEL fileformat, + * see http://research.stowers-institute.org/mcm/efg/ScientificSoftware/Utility/TiffTags/GEL-FileFormat.pdf + * (2023: the above web site is unavailable but tags are explained briefly at + * https://www.awaresystems.be/imaging/tiff/tifftags/docs/gel.html + */ +#define TIFFTAG_MD_FILETAG 33445 /* Specifies the pixel data format encoding in the GEL file format. */ +#define TIFFTAG_MD_SCALEPIXEL 33446 /* scale factor */ +#define TIFFTAG_MD_COLORTABLE 33447 /* conversion from 16bit to 8bit */ +#define TIFFTAG_MD_LABNAME 33448 /* name of the lab that scanned this file. */ +#define TIFFTAG_MD_SAMPLEINFO 33449 /* information about the scanned GEL sample */ +#define TIFFTAG_MD_PREPDATE 33450 /* information about the date the sample was prepared YY/MM/DD */ +#define TIFFTAG_MD_PREPTIME 33451 /* information about the time the sample was prepared HH:MM*/ +#define TIFFTAG_MD_FILEUNITS 33452 /* Units for data in this file, as used in the GEL file format. */ +/* IPTC TAG from RichTIFF specifications */ +#define TIFFTAG_RICHTIFFIPTC 33723 +#define TIFFTAG_INGR_PACKET_DATA_TAG 33918 /* Intergraph Application specific storage. */ +#define TIFFTAG_INGR_FLAG_REGISTERS 33919 /* Intergraph Application specific flags. */ +#define TIFFTAG_IRASB_TRANSORMATION_MATRIX 33920 /* Originally part of Intergraph's GeoTIFF tags, but likely understood by IrasB only. */ +#define TIFFTAG_MODELTIEPOINTTAG 33922 /* GeoTIFF */ +/* 34016-34029 are reserved for ANSI IT8 TIFF/IT */ +#define TIFFTAG_STONITS 37439 /* Sample value to Nits */ +/* tag 34929 is a private tag registered to FedEx */ +#define TIFFTAG_FEDEX_EDR 34929 /* unknown use */ +#define TIFFTAG_IMAGESOURCEDATA 37724 /* http://justsolve.archiveteam.org/wiki/PSD, http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/ */ +#define TIFFTAG_INTEROPERABILITYIFD 40965 /* Pointer to EXIF Interoperability private directory */ +#define TIFFTAG_GDAL_METADATA 42112 /* Used by the GDAL library */ +#define TIFFTAG_GDAL_NODATA 42113 /* Used by the GDAL library */ +#define TIFFTAG_OCE_SCANJOB_DESCRIPTION 50215 /* Used in the Oce scanning process */ +#define TIFFTAG_OCE_APPLICATION_SELECTOR 50216 /* Used in the Oce scanning process. */ +#define TIFFTAG_OCE_IDENTIFICATION_NUMBER 50217 +#define TIFFTAG_OCE_IMAGELOGIC_CHARACTERISTICS 50218 +/* tags 50674 to 50677 are reserved for ESRI */ +#define TIFFTAG_LERC_PARAMETERS 50674 /* Stores LERC version and additional compression method */ + +/* Adobe Digital Negative (DNG) format tags */ +#define TIFFTAG_DNGVERSION 50706 /* &DNG version number */ +#define TIFFTAG_DNGBACKWARDVERSION 50707 /* &DNG compatibility version */ +#define TIFFTAG_UNIQUECAMERAMODEL 50708 /* &name for the camera model */ +#define TIFFTAG_LOCALIZEDCAMERAMODEL 50709 /* &localized camera model name (UTF-8) */ +#define TIFFTAG_CFAPLANECOLOR 50710 /* &CFAPattern->LinearRaw space mapping */ +#define TIFFTAG_CFALAYOUT 50711 /* &spatial layout of the CFA */ +#define TIFFTAG_LINEARIZATIONTABLE 50712 /* &lookup table description */ +#define TIFFTAG_BLACKLEVELREPEATDIM 50713 /* &repeat pattern size for the BlackLevel tag */ +#define TIFFTAG_BLACKLEVEL 50714 /* &zero light encoding level */ +#define TIFFTAG_BLACKLEVELDELTAH 50715 /* &zero light encoding level differences (columns) */ +#define TIFFTAG_BLACKLEVELDELTAV 50716 /* &zero light encoding level differences (rows) */ +#define TIFFTAG_WHITELEVEL 50717 /* &fully saturated encoding level */ +#define TIFFTAG_DEFAULTSCALE 50718 /* &default scale factors */ +#define TIFFTAG_DEFAULTCROPORIGIN 50719 /* &origin of the final image area */ +#define TIFFTAG_DEFAULTCROPSIZE 50720 /* &size of the final image area */ +#define TIFFTAG_COLORMATRIX1 50721 /* &XYZ->reference color space transformation matrix 1 */ +#define TIFFTAG_COLORMATRIX2 50722 /* &XYZ->reference color space transformation matrix 2 */ +#define TIFFTAG_CAMERACALIBRATION1 50723 /* &calibration matrix 1 */ +#define TIFFTAG_CAMERACALIBRATION2 50724 /* &calibration matrix 2 */ +#define TIFFTAG_REDUCTIONMATRIX1 50725 /* &dimensionality reduction matrix 1 */ +#define TIFFTAG_REDUCTIONMATRIX2 50726 /* &dimensionality reduction matrix 2 */ +#define TIFFTAG_ANALOGBALANCE 50727 /* &gain applied the stored raw values*/ +#define TIFFTAG_ASSHOTNEUTRAL 50728 /* &selected white balance in linear reference space */ +#define TIFFTAG_ASSHOTWHITEXY 50729 /* &selected white balance in x-y chromaticity coordinates */ +#define TIFFTAG_BASELINEEXPOSURE 50730 /* &how much to move the zero point */ +#define TIFFTAG_BASELINENOISE 50731 /* &relative noise level */ +#define TIFFTAG_BASELINESHARPNESS 50732 /* &relative amount of sharpening */ +/* TIFFTAG_BAYERGREENSPLIT: &how closely the values of the green pixels in the blue/green rows + * track the values of the green pixels in the red/green rows */ +#define TIFFTAG_BAYERGREENSPLIT 50733 +#define TIFFTAG_LINEARRESPONSELIMIT 50734 /* &non-linear encoding range */ +#define TIFFTAG_CAMERASERIALNUMBER 50735 /* &camera's serial number */ +#define TIFFTAG_LENSINFO 50736 /* info about the lens */ +#define TIFFTAG_CHROMABLURRADIUS 50737 /* &chroma blur radius */ +#define TIFFTAG_ANTIALIASSTRENGTH 50738 /* &relative strength of the camera's anti-alias filter */ +#define TIFFTAG_SHADOWSCALE 50739 /* &used by Adobe Camera Raw */ +#define TIFFTAG_DNGPRIVATEDATA 50740 /* &manufacturer's private data */ +#define TIFFTAG_MAKERNOTESAFETY 50741 /* &whether the EXIF MakerNote tag is safe to preserve along with the rest of the EXIF data */ +#define TIFFTAG_CALIBRATIONILLUMINANT1 50778 /* &illuminant 1 */ +#define TIFFTAG_CALIBRATIONILLUMINANT2 50779 /* &illuminant 2 */ +#define TIFFTAG_BESTQUALITYSCALE 50780 /* &best quality multiplier */ +#define TIFFTAG_RAWDATAUNIQUEID 50781 /* &unique identifier for the raw image data */ +#define TIFFTAG_ORIGINALRAWFILENAME 50827 /* &file name of the original raw file (UTF-8) */ +#define TIFFTAG_ORIGINALRAWFILEDATA 50828 /* &contents of the original raw file */ +#define TIFFTAG_ACTIVEAREA 50829 /* &active (non-masked) pixels of the sensor */ +#define TIFFTAG_MASKEDAREAS 50830 /* &list of coordinates of fully masked pixels */ +#define TIFFTAG_ASSHOTICCPROFILE 50831 /* &these two tags used to */ +#define TIFFTAG_ASSHOTPREPROFILEMATRIX 50832 /* map cameras's color space into ICC profile space */ +#define TIFFTAG_CURRENTICCPROFILE 50833 /* & */ +#define TIFFTAG_CURRENTPREPROFILEMATRIX 50834 /* & */ + +/* DNG 1.2.0.0 */ +#define TIFFTAG_COLORIMETRICREFERENCE 50879 /* &colorimetric reference */ +#define TIFFTAG_CAMERACALIBRATIONSIGNATURE 50931 /* &camera calibration signature (UTF-8) */ +#define TIFFTAG_PROFILECALIBRATIONSIGNATURE 50932 /* &profile calibration signature (UTF-8) */ +/* TIFFTAG_EXTRACAMERAPROFILES 50933 &extra camera profiles : is already defined for GeoTIFF DGIWG */ +#define TIFFTAG_ASSHOTPROFILENAME 50934 /* &as shot profile name (UTF-8) */ +#define TIFFTAG_NOISEREDUCTIONAPPLIED 50935 /* &amount of applied noise reduction */ +#define TIFFTAG_PROFILENAME 50936 /* &camera profile name (UTF-8) */ +#define TIFFTAG_PROFILEHUESATMAPDIMS 50937 /* &dimensions of HSV mapping */ +#define TIFFTAG_PROFILEHUESATMAPDATA1 50938 /* &first HSV mapping table */ +#define TIFFTAG_PROFILEHUESATMAPDATA2 50939 /* &second HSV mapping table */ +#define TIFFTAG_PROFILETONECURVE 50940 /* &default tone curve */ +#define TIFFTAG_PROFILEEMBEDPOLICY 50941 /* &profile embedding policy */ +#define TIFFTAG_PROFILECOPYRIGHT 50942 /* &profile copyright information (UTF-8) */ +#define TIFFTAG_FORWARDMATRIX1 50964 /* &matrix for mapping white balanced camera colors to XYZ D50 */ +#define TIFFTAG_FORWARDMATRIX2 50965 /* &matrix for mapping white balanced camera colors to XYZ D50 */ +#define TIFFTAG_PREVIEWAPPLICATIONNAME 50966 /* &name of application that created preview (UTF-8) */ +#define TIFFTAG_PREVIEWAPPLICATIONVERSION 50967 /* &version of application that created preview (UTF-8) */ +#define TIFFTAG_PREVIEWSETTINGSNAME 50968 /* &name of conversion settings (UTF-8) */ +#define TIFFTAG_PREVIEWSETTINGSDIGEST 50969 /* &unique id of conversion settings */ +#define TIFFTAG_PREVIEWCOLORSPACE 50970 /* &preview color space */ +#define TIFFTAG_PREVIEWDATETIME 50971 /* &date/time preview was rendered */ +#define TIFFTAG_RAWIMAGEDIGEST 50972 /* &md5 of raw image data */ +#define TIFFTAG_ORIGINALRAWFILEDIGEST 50973 /* &md5 of the data stored in the OriginalRawFileData tag */ +#define TIFFTAG_SUBTILEBLOCKSIZE 50974 /* &subtile block size */ +#define TIFFTAG_ROWINTERLEAVEFACTOR 50975 /* &number of interleaved fields */ +#define TIFFTAG_PROFILELOOKTABLEDIMS 50981 /* &num of input samples in each dim of default "look" table */ +#define TIFFTAG_PROFILELOOKTABLEDATA 50982 /* &default "look" table for use as starting point */ + +/* DNG 1.3.0.0 */ +#define TIFFTAG_OPCODELIST1 51008 /* &opcodes that should be applied to raw image after reading */ +#define TIFFTAG_OPCODELIST2 51009 /* &opcodes that should be applied after mapping to linear reference */ +#define TIFFTAG_OPCODELIST3 51022 /* &opcodes that should be applied after demosaicing */ +#define TIFFTAG_NOISEPROFILE 51041 /* &noise profile */ + +/* DNG 1.4.0.0 */ +#define TIFFTAG_DEFAULTUSERCROP 51125 /* &default user crop rectangle in relative coords */ +#define TIFFTAG_DEFAULTBLACKRENDER 51110 /* &black rendering hint */ +#define TIFFTAG_BASELINEEXPOSUREOFFSET 51109 /* &baseline exposure offset */ +#define TIFFTAG_PROFILELOOKTABLEENCODING 51108 /* &3D LookTable indexing conversion */ +#define TIFFTAG_PROFILEHUESATMAPENCODING 51107 /* &3D HueSatMap indexing conversion */ +#define TIFFTAG_ORIGINALDEFAULTFINALSIZE 51089 /* &default final size of larger original file for this proxy */ +#define TIFFTAG_ORIGINALBESTQUALITYFINALSIZE 51090 /* &best quality final size of larger original file for this proxy */ +#define TIFFTAG_ORIGINALDEFAULTCROPSIZE 51091 /* &the default crop size of larger original file for this proxy */ +#define TIFFTAG_NEWRAWIMAGEDIGEST 51111 /* &modified MD5 digest of the raw image data */ +#define TIFFTAG_RAWTOPREVIEWGAIN 51112 /* &The gain between the main raw FD and the preview IFD containing this tag */ + +/* DNG 1.5.0.0 */ +#define TIFFTAG_DEPTHFORMAT 51177 /* &encoding of the depth data in the file */ +#define TIFFTAG_DEPTHNEAR 51178 /* &distance from the camera represented by value 0 in the depth map */ +#define TIFFTAG_DEPTHFAR 51179 /* &distance from the camera represented by the maximum value in the depth map */ +#define TIFFTAG_DEPTHUNITS 51180 /* &measurement units for DepthNear and DepthFar */ +#define TIFFTAG_DEPTHMEASURETYPE 51181 /* &measurement geometry for the depth map */ +#define TIFFTAG_ENHANCEPARAMS 51182 /* &a string that documents how the enhanced image data was processed. */ + +/* DNG 1.6.0.0 */ +#define TIFFTAG_PROFILEGAINTABLEMAP 52525 /* &spatially varying gain tables that can be applied as starting point */ +#define TIFFTAG_SEMANTICNAME 52526 /* &a string that identifies the semantic mask */ +#define TIFFTAG_SEMANTICINSTANCEID 52528 /* &a string that identifies a specific instance in a semantic mask */ +#define TIFFTAG_MASKSUBAREA 52536 /* &the crop rectangle of this IFD's mask, relative to the main image */ +#define TIFFTAG_RGBTABLES 52543 /* &color transforms to apply to masked image regions */ +#define TIFFTAG_CALIBRATIONILLUMINANT3 52529 /* &the illuminant used for the third set of color calibration tags */ +#define TIFFTAG_COLORMATRIX3 52531 /* &matrix to convert XYZ values to reference camera native color space under CalibrationIlluminant3 */ +#define TIFFTAG_CAMERACALIBRATION3 52530 /* &matrix to transform reference camera native space values to individual camera native space values under CalibrationIlluminant3 */ +#define TIFFTAG_REDUCTIONMATRIX3 52538 /* &dimensionality reduction matrix for use in color conversion to XYZ under CalibrationIlluminant3 */ +#define TIFFTAG_PROFILEHUESATMAPDATA3 52537 /* &the data for the third HSV table */ +#define TIFFTAG_FORWARDMATRIX3 52532 /* &matrix to map white balanced camera colors to XYZ D50 */ +#define TIFFTAG_ILLUMINANTDATA1 52533 /* &data for the first calibration illuminant */ +#define TIFFTAG_ILLUMINANTDATA2 52534 /* &data for the second calibration illuminant */ +#define TIFFTAG_ILLUMINANTDATA3 53535 /* &data for the third calibration illuminant */ + +/* TIFF/EP */ +#define TIFFTAG_EP_CFAREPEATPATTERNDIM 33421 /* dimensions of CFA pattern */ +#define TIFFTAG_EP_CFAPATTERN 33422 /* color filter array pattern */ +#define TIFFTAG_EP_BATTERYLEVEL 33423 /* battery level (rational or ASCII) */ +#define TIFFTAG_EP_INTERLACE 34857 /* Number of multi-field images */ +/* TIFFTAG_EP_IPTC_NAA and TIFFTAG_RICHTIFFIPTC share the same tag number (33723) + * LibTIFF type is UNDEFINED or BYTE, but often times incorrectly specified as LONG, + * because TIFF/EP (ISO/DIS 12234-2) specifies type LONG or ASCII. */ +#define TIFFTAG_EP_IPTC_NAA 33723 /* Alias IPTC/NAA Newspaper Association RichTIFF */ +#define TIFFTAG_EP_TIMEZONEOFFSET 34858 /* Time zone offset relative to UTC */ +#define TIFFTAG_EP_SELFTIMERMODE 34859 /* Number of seconds capture was delayed from button press */ +#define TIFFTAG_EP_FLASHENERGY 37387 /* Flash energy, or range if there is uncertainty */ +#define TIFFTAG_EP_SPATIALFREQUENCYRESPONSE 37388 /* Spatial frequency response */ +#define TIFFTAG_EP_NOISE 37389 /* Camera noise measurement values */ +#define TIFFTAG_EP_FOCALPLANEXRESOLUTION 37390 /* Focal plane X resolution */ +#define TIFFTAG_EP_FOCALPLANEYRESOLUTION 37391 /* Focal plane Y resolution */ +#define TIFFTAG_EP_FOCALPLANERESOLUTIONUNIT 37392 /* Focal plane resolution unit */ +#define TIFFTAG_EP_IMAGENUMBER 37393 /* Number of image when several of burst shot stored in same TIFF/EP */ +#define TIFFTAG_EP_SECURITYCLASSIFICATION 37394 /* Security classification */ +#define TIFFTAG_EP_IMAGEHISTORY 37395 /* Record of what has been done to the image */ +#define TIFFTAG_EP_EXPOSUREINDEX 37397 /* Exposure index */ +#define TIFFTAG_EP_STANDARDID 37398 /* TIFF/EP standard version, n.n.n.n */ +#define TIFFTAG_EP_SENSINGMETHOD 37399 /* Type of image sensor */ +/* + * TIFF/EP tags equivalent to EXIF tags + * Note that TIFF-EP and EXIF use nearly the same metadata tag set, but TIFF-EP stores the tags in IFD 0, + * while EXIF store the tags in a separate IFD. Either location is allowed by DNG, but the EXIF location is preferred. + */ +#define TIFFTAG_EP_EXPOSURETIME 33434 /* Exposure time */ +#define TIFFTAG_EP_FNUMBER 33437 /* F number */ +#define TIFFTAG_EP_EXPOSUREPROGRAM 34850 /* Exposure program */ +#define TIFFTAG_EP_SPECTRALSENSITIVITY 34852 /* Spectral sensitivity */ +#define TIFFTAG_EP_ISOSPEEDRATINGS 34855 /* ISO speed rating */ +#define TIFFTAG_EP_OECF 34856 /* Optoelectric conversion factor */ +#define TIFFTAG_EP_DATETIMEORIGINAL 36867 /* Date and time of original data generation */ +#define TIFFTAG_EP_COMPRESSEDBITSPERPIXEL 37122 /* Image compression mode */ +#define TIFFTAG_EP_SHUTTERSPEEDVALUE 37377 /* Shutter speed */ +#define TIFFTAG_EP_APERTUREVALUE 37378 /* Aperture */ +#define TIFFTAG_EP_BRIGHTNESSVALUE 37379 /* Brightness */ +#define TIFFTAG_EP_EXPOSUREBIASVALUE 37380 /* Exposure bias */ +#define TIFFTAG_EP_MAXAPERTUREVALUE 37381 /* Maximum lens aperture */ +#define TIFFTAG_EP_SUBJECTDISTANCE 37382 /* Subject distance */ +#define TIFFTAG_EP_METERINGMODE 37383 /* Metering mode */ +#define TIFFTAG_EP_LIGHTSOURCE 37384 /* Light source */ +#define TIFFTAG_EP_FLASH 37385 /* Flash */ +#define TIFFTAG_EP_FOCALLENGTH 37386 /* Lens focal length */ +#define TIFFTAG_EP_SUBJECTLOCATION 37396 /* Subject location (area) */ + +#define TIFFTAG_RPCCOEFFICIENT 50844 /* Define by GDAL for geospatial georeferencing through RPC: http://geotiff.maptools.org/rpc_prop.html */ +#define TIFFTAG_ALIAS_LAYER_METADATA 50784 /* Alias Sketchbook Pro layer usage description. */ + +/* GeoTIFF DGIWG */ +#define TIFFTAG_TIFF_RSID 50908 /* https://www.awaresystems.be/imaging/tiff/tifftags/tiff_rsid.html */ +#define TIFFTAG_GEO_METADATA 50909 /* https://www.awaresystems.be/imaging/tiff/tifftags/geo_metadata.html */ +#define TIFFTAG_EXTRACAMERAPROFILES 50933 /* http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/dng_spec_1.4.0.0.pdf */ + +/* tag 65535 is an undefined tag used by Eastman Kodak */ +#define TIFFTAG_DCSHUESHIFTVALUES 65535 /* hue shift correction data */ + +/* + * The following are ``pseudo tags'' that can be used to control + * codec-specific functionality. These tags are not written to file. + * Note that these values start at 0xffff+1 so that they'll never + * collide with Aldus-assigned tags. + * + * If you want your private pseudo tags ``registered'' (i.e. added to + * this file), please post a bug report via the tracking system at + * http://www.remotesensing.org/libtiff/bugs.html with the appropriate + * C definitions to add. + */ +#define TIFFTAG_FAXMODE 65536 /* Group 3/4 format control */ +#define FAXMODE_CLASSIC 0x0000 /* default, include RTC */ +#define FAXMODE_NORTC 0x0001 /* no RTC at end of data */ +#define FAXMODE_NOEOL 0x0002 /* no EOL code at end of row */ +#define FAXMODE_BYTEALIGN 0x0004 /* byte align row */ +#define FAXMODE_WORDALIGN 0x0008 /* word align row */ +#define FAXMODE_CLASSF FAXMODE_NORTC /* TIFF Class F */ +#define TIFFTAG_JPEGQUALITY 65537 /* Compression quality level */ +/* Note: quality level is on the IJG 0-100 scale. Default value is 75 */ +#define TIFFTAG_JPEGCOLORMODE 65538 /* Auto RGB<=>YCbCr convert? */ +#define JPEGCOLORMODE_RAW 0x0000 /* no conversion (default) */ +#define JPEGCOLORMODE_RGB 0x0001 /* do auto conversion */ +#define TIFFTAG_JPEGTABLESMODE 65539 /* What to put in JPEGTables */ +#define JPEGTABLESMODE_QUANT 0x0001 /* include quantization tbls */ +#define JPEGTABLESMODE_HUFF 0x0002 /* include Huffman tbls */ +/* Note: default is JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF */ +#define TIFFTAG_FAXFILLFUNC 65540 /* G3/G4 fill function */ +#define TIFFTAG_PIXARLOGDATAFMT 65549 /* PixarLogCodec I/O data sz */ +#define PIXARLOGDATAFMT_8BIT 0 /* regular u_char samples */ +#define PIXARLOGDATAFMT_8BITABGR 1 /* ABGR-order u_chars */ +#define PIXARLOGDATAFMT_11BITLOG 2 /* 11-bit log-encoded (raw) */ +#define PIXARLOGDATAFMT_12BITPICIO 3 /* as per PICIO (1.0==2048) */ +#define PIXARLOGDATAFMT_16BIT 4 /* signed short samples */ +#define PIXARLOGDATAFMT_FLOAT 5 /* IEEE float samples */ +/* 65550-65556 are allocated to Oceana Matrix */ +#define TIFFTAG_DCSIMAGERTYPE 65550 /* imager model & filter */ +#define DCSIMAGERMODEL_M3 0 /* M3 chip (1280 x 1024) */ +#define DCSIMAGERMODEL_M5 1 /* M5 chip (1536 x 1024) */ +#define DCSIMAGERMODEL_M6 2 /* M6 chip (3072 x 2048) */ +#define DCSIMAGERFILTER_IR 0 /* infrared filter */ +#define DCSIMAGERFILTER_MONO 1 /* monochrome filter */ +#define DCSIMAGERFILTER_CFA 2 /* color filter array */ +#define DCSIMAGERFILTER_OTHER 3 /* other filter */ +#define TIFFTAG_DCSINTERPMODE 65551 /* interpolation mode */ +#define DCSINTERPMODE_NORMAL 0x0 /* whole image, default */ +#define DCSINTERPMODE_PREVIEW 0x1 /* preview of image (384x256) */ +#define TIFFTAG_DCSBALANCEARRAY 65552 /* color balance values */ +#define TIFFTAG_DCSCORRECTMATRIX 65553 /* color correction values */ +#define TIFFTAG_DCSGAMMA 65554 /* gamma value */ +#define TIFFTAG_DCSTOESHOULDERPTS 65555 /* toe & shoulder points */ +#define TIFFTAG_DCSCALIBRATIONFD 65556 /* calibration file desc */ +/* Note: quality level is on the ZLIB 1-9 scale. Default value is -1 */ +#define TIFFTAG_ZIPQUALITY 65557 /* compression quality level */ +#define TIFFTAG_PIXARLOGQUALITY 65558 /* PixarLog uses same scale */ +/* 65559 is allocated to Oceana Matrix */ +#define TIFFTAG_DCSCLIPRECTANGLE 65559 /* area of image to acquire */ +#define TIFFTAG_SGILOGDATAFMT 65560 /* SGILog user data format */ +#define SGILOGDATAFMT_FLOAT 0 /* IEEE float samples */ +#define SGILOGDATAFMT_16BIT 1 /* 16-bit samples */ +#define SGILOGDATAFMT_RAW 2 /* uninterpreted data */ +#define SGILOGDATAFMT_8BIT 3 /* 8-bit RGB monitor values */ +#define TIFFTAG_SGILOGENCODE 65561 /* SGILog data encoding control*/ +#define SGILOGENCODE_NODITHER 0 /* do not dither encoded values*/ +#define SGILOGENCODE_RANDITHER 1 /* randomly dither encd values */ +#define TIFFTAG_LZMAPRESET 65562 /* LZMA2 preset (compression level) */ +#define TIFFTAG_PERSAMPLE 65563 /* interface for per sample tags */ +#define PERSAMPLE_MERGED 0 /* present as a single value */ +#define PERSAMPLE_MULTI 1 /* present as multiple values */ +#define TIFFTAG_ZSTD_LEVEL 65564 /* ZSTD compression level */ +#define TIFFTAG_LERC_VERSION 65565 /* LERC version */ +#define LERC_VERSION_2_4 4 +#define TIFFTAG_LERC_ADD_COMPRESSION 65566 /* LERC additional compression */ +#define LERC_ADD_COMPRESSION_NONE 0 +#define LERC_ADD_COMPRESSION_DEFLATE 1 +#define LERC_ADD_COMPRESSION_ZSTD 2 +#define TIFFTAG_LERC_MAXZERROR 65567 /* LERC maximum error */ +#define TIFFTAG_WEBP_LEVEL 65568 /* WebP compression level */ +#define TIFFTAG_WEBP_LOSSLESS 65569 /* WebP lossless/lossy */ +#define TIFFTAG_WEBP_LOSSLESS_EXACT 65571 /* WebP lossless exact mode. Set-only mode. Default is 1. Can be set to 0 to increase compression rate, but R,G,B in areas where alpha = 0 will not be preserved */ +#define TIFFTAG_DEFLATE_SUBCODEC 65570 /* ZIP codec: to get/set the sub-codec to use. Will default to libdeflate when available */ +#define DEFLATE_SUBCODEC_ZLIB 0 +#define DEFLATE_SUBCODEC_LIBDEFLATE 1 + +/* + * EXIF tags + */ +#define EXIFTAG_EXPOSURETIME 33434 /* Exposure time */ +#define EXIFTAG_FNUMBER 33437 /* F number */ +#define EXIFTAG_EXPOSUREPROGRAM 34850 /* Exposure program */ +#define EXIFTAG_SPECTRALSENSITIVITY 34852 /* Spectral sensitivity */ +/* After EXIF 2.2.1 ISOSpeedRatings is named PhotographicSensitivity. + In addition, while "Count=Any", only 1 count should be used. */ +#define EXIFTAG_ISOSPEEDRATINGS 34855 /* ISO speed rating */ +#define EXIFTAG_PHOTOGRAPHICSENSITIVITY 34855 /* Photographic Sensitivity (new name for tag 34855) */ +#define EXIFTAG_OECF 34856 /* Optoelectric conversion factor */ +#define EXIFTAG_EXIFVERSION 36864 /* Exif version */ +#define EXIFTAG_DATETIMEORIGINAL 36867 /* Date and time of original data generation */ +#define EXIFTAG_DATETIMEDIGITIZED 36868 /* Date and time of digital data generation */ +#define EXIFTAG_COMPONENTSCONFIGURATION 37121 /* Meaning of each component */ +#define EXIFTAG_COMPRESSEDBITSPERPIXEL 37122 /* Image compression mode */ +#define EXIFTAG_SHUTTERSPEEDVALUE 37377 /* Shutter speed */ +#define EXIFTAG_APERTUREVALUE 37378 /* Aperture */ +#define EXIFTAG_BRIGHTNESSVALUE 37379 /* Brightness */ +#define EXIFTAG_EXPOSUREBIASVALUE 37380 /* Exposure bias */ +#define EXIFTAG_MAXAPERTUREVALUE 37381 /* Maximum lens aperture */ +#define EXIFTAG_SUBJECTDISTANCE 37382 /* Subject distance */ +#define EXIFTAG_METERINGMODE 37383 /* Metering mode */ +#define EXIFTAG_LIGHTSOURCE 37384 /* Light source */ +#define EXIFTAG_FLASH 37385 /* Flash */ +#define EXIFTAG_FOCALLENGTH 37386 /* Lens focal length */ +#define EXIFTAG_SUBJECTAREA 37396 /* Subject area */ +#define EXIFTAG_MAKERNOTE 37500 /* Manufacturer notes */ +#define EXIFTAG_USERCOMMENT 37510 /* User comments */ +#define EXIFTAG_SUBSECTIME 37520 /* DateTime subseconds */ +#define EXIFTAG_SUBSECTIMEORIGINAL 37521 /* DateTimeOriginal subseconds */ +#define EXIFTAG_SUBSECTIMEDIGITIZED 37522 /* DateTimeDigitized subseconds */ +#define EXIFTAG_FLASHPIXVERSION 40960 /* Supported Flashpix version */ +#define EXIFTAG_COLORSPACE 40961 /* Color space information */ +#define EXIFTAG_PIXELXDIMENSION 40962 /* Valid image width */ +#define EXIFTAG_PIXELYDIMENSION 40963 /* Valid image height */ +#define EXIFTAG_RELATEDSOUNDFILE 40964 /* Related audio file */ +#define EXIFTAG_FLASHENERGY 41483 /* Flash energy */ +#define EXIFTAG_SPATIALFREQUENCYRESPONSE 41484 /* Spatial frequency response */ +#define EXIFTAG_FOCALPLANEXRESOLUTION 41486 /* Focal plane X resolution */ +#define EXIFTAG_FOCALPLANEYRESOLUTION 41487 /* Focal plane Y resolution */ +#define EXIFTAG_FOCALPLANERESOLUTIONUNIT 41488 /* Focal plane resolution unit */ +#define EXIFTAG_SUBJECTLOCATION 41492 /* Subject location */ +#define EXIFTAG_EXPOSUREINDEX 41493 /* Exposure index */ +#define EXIFTAG_SENSINGMETHOD 41495 /* Sensing method */ +#define EXIFTAG_FILESOURCE 41728 /* File source */ +#define EXIFTAG_SCENETYPE 41729 /* Scene type */ +#define EXIFTAG_CFAPATTERN 41730 /* CFA pattern */ +#define EXIFTAG_CUSTOMRENDERED 41985 /* Custom image processing */ +#define EXIFTAG_EXPOSUREMODE 41986 /* Exposure mode */ +#define EXIFTAG_WHITEBALANCE 41987 /* White balance */ +#define EXIFTAG_DIGITALZOOMRATIO 41988 /* Digital zoom ratio */ +#define EXIFTAG_FOCALLENGTHIN35MMFILM 41989 /* Focal length in 35 mm film */ +#define EXIFTAG_SCENECAPTURETYPE 41990 /* Scene capture type */ +#define EXIFTAG_GAINCONTROL 41991 /* Gain control */ +#define EXIFTAG_CONTRAST 41992 /* Contrast */ +#define EXIFTAG_SATURATION 41993 /* Saturation */ +#define EXIFTAG_SHARPNESS 41994 /* Sharpness */ +#define EXIFTAG_DEVICESETTINGDESCRIPTION 41995 /* Device settings description */ +#define EXIFTAG_SUBJECTDISTANCERANGE 41996 /* Subject distance range */ +#define EXIFTAG_IMAGEUNIQUEID 42016 /* Unique image ID */ + +/*--: New for EXIF-Version 2.32, May 2019 ... */ +#define EXIFTAG_SENSITIVITYTYPE 34864 /* The SensitivityType tag indicates which one of the parameters of ISO12232 is the PhotographicSensitivity tag. */ +#define EXIFTAG_STANDARDOUTPUTSENSITIVITY 34865 /* This tag indicates the standard output sensitivity value of a camera or input device defined in ISO 12232. */ +#define EXIFTAG_RECOMMENDEDEXPOSUREINDEX 34866 /* recommended exposure index */ +#define EXIFTAG_ISOSPEED 34867 /* ISO speed value */ +#define EXIFTAG_ISOSPEEDLATITUDEYYY 34868 /* ISO speed latitude yyy */ +#define EXIFTAG_ISOSPEEDLATITUDEZZZ 34869 /* ISO speed latitude zzz */ +#define EXIFTAG_OFFSETTIME 36880 /* offset from UTC of the time of DateTime tag. */ +#define EXIFTAG_OFFSETTIMEORIGINAL 36881 /* offset from UTC of the time of DateTimeOriginal tag. */ +#define EXIFTAG_OFFSETTIMEDIGITIZED 36882 /* offset from UTC of the time of DateTimeDigitized tag. */ +#define EXIFTAG_TEMPERATURE 37888 /* Temperature as the ambient situation at the shot in dergee Celsius */ +#define EXIFTAG_HUMIDITY 37889 /* Humidity as the ambient situation at the shot in percent */ +#define EXIFTAG_PRESSURE 37890 /* Pressure as the ambient situation at the shot hecto-Pascal (hPa) */ +#define EXIFTAG_WATERDEPTH 37891 /* WaterDepth as the ambient situation at the shot in meter (m) */ +#define EXIFTAG_ACCELERATION 37892 /* Acceleration (a scalar regardless of direction) as the ambientsituation at the shot in units of mGal (10-5 m/s^2) */ +/* EXIFTAG_CAMERAELEVATIONANGLE: Elevation/depression. angle of the orientation of the camera(imaging optical axis) + * as the ambient situation at the shot in degree from -180deg to +180deg. */ +#define EXIFTAG_CAMERAELEVATIONANGLE 37893 +#define EXIFTAG_CAMERAOWNERNAME 42032 /* owner of a camera */ +#define EXIFTAG_BODYSERIALNUMBER 42033 /* serial number of the body of the camera */ +/* EXIFTAG_LENSSPECIFICATION: minimum focal length (in mm), maximum focal length (in mm),minimum F number in the minimum focal length, + * and minimum F number in the maximum focal length, */ +#define EXIFTAG_LENSSPECIFICATION 42034 +#define EXIFTAG_LENSMAKE 42035 /* the lens manufacturer */ +#define EXIFTAG_LENSMODEL 42036 /* the lens model name and model number */ +#define EXIFTAG_LENSSERIALNUMBER 42037 /* the serial number of the interchangeable lens */ +#define EXIFTAG_GAMMA 42240 /* value of coefficient gamma */ +#define EXIFTAG_COMPOSITEIMAGE 42080 /* composite image */ +#define EXIFTAG_SOURCEIMAGENUMBEROFCOMPOSITEIMAGE 42081 /* source image number of composite image */ +#define EXIFTAG_SOURCEEXPOSURETIMESOFCOMPOSITEIMAGE 42082 /* source exposure times of composite image */ + +/* + * EXIF-GPS tags (Version 2.31, July 2016) + */ +#define GPSTAG_VERSIONID 0 /* Indicates the version of GPSInfoIFD. */ +#define GPSTAG_LATITUDEREF 1 /* Indicates whether the latitude is north or south latitude. */ +#define GPSTAG_LATITUDE 2 /* Indicates the latitude. */ +#define GPSTAG_LONGITUDEREF 3 /* Indicates whether the longitude is east or west longitude. */ +#define GPSTAG_LONGITUDE 4 /* Indicates the longitude. */ +#define GPSTAG_ALTITUDEREF 5 /* Indicates the altitude used as the reference altitude. */ +#define GPSTAG_ALTITUDE 6 /* Indicates the altitude based on the reference in GPSAltitudeRef. */ +#define GPSTAG_TIMESTAMP 7 /*Indicates the time as UTC (Coordinated Universal Time). */ +#define GPSTAG_SATELLITES 8 /*Indicates the GPS satellites used for measurements. */ +#define GPSTAG_STATUS 9 /* Indicates the status of the GPS receiver when the image is recorded. */ +#define GPSTAG_MEASUREMODE 10 /* Indicates the GPS measurement mode. */ +#define GPSTAG_DOP 11 /* Indicates the GPS DOP (data degree of precision). */ +#define GPSTAG_SPEEDREF 12 /* Indicates the unit used to express the GPS receiver speed of movement. */ +#define GPSTAG_SPEED 13 /* Indicates the speed of GPS receiver movement. */ +#define GPSTAG_TRACKREF 14 /* Indicates the reference for giving the direction of GPS receiver movement. */ +#define GPSTAG_TRACK 15 /* Indicates the direction of GPS receiver movement. */ +#define GPSTAG_IMGDIRECTIONREF 16 /* Indicates the reference for giving the direction of the image when it is captured. */ +#define GPSTAG_IMGDIRECTION 17 /* Indicates the direction of the image when it was captured. */ +#define GPSTAG_MAPDATUM 18 /* Indicates the geodetic survey data used by the GPS receiver. (e.g. WGS-84) */ +#define GPSTAG_DESTLATITUDEREF 19 /* Indicates whether the latitude of the destination point is north or south latitude. */ +#define GPSTAG_DESTLATITUDE 20 /* Indicates the latitude of the destination point. */ +#define GPSTAG_DESTLONGITUDEREF 21 /* Indicates whether the longitude of the destination point is east or west longitude. */ +#define GPSTAG_DESTLONGITUDE 22 /* Indicates the longitude of the destination point. */ +#define GPSTAG_DESTBEARINGREF 23 /* Indicates the reference used for giving the bearing to the destination point. */ +#define GPSTAG_DESTBEARING 24 /* Indicates the bearing to the destination point. */ +#define GPSTAG_DESTDISTANCEREF 25 /* Indicates the unit used to express the distance to the destination point. */ +#define GPSTAG_DESTDISTANCE 26 /* Indicates the distance to the destination point. */ +#define GPSTAG_PROCESSINGMETHOD 27 /* A character string recording the name of the method used for location finding. */ +#define GPSTAG_AREAINFORMATION 28 /* A character string recording the name of the GPS area. */ +#define GPSTAG_DATESTAMP 29 /* A character string recording date and time information relative to UTC (Coordinated Universal Time). */ +#define GPSTAG_DIFFERENTIAL 30 /* Indicates whether differential correction is applied to the GPS receiver. */ +#define GPSTAG_GPSHPOSITIONINGERROR 31 /* Indicates horizontal positioning errors in meters. */ + +#endif /* _TIFF_ */ diff --git a/cpp/3rd_party/libtiff/tiffconf.h.cmake.in b/cpp/3rd_party/libtiff/tiffconf.h.cmake.in new file mode 100644 index 0000000000..56dfe79334 --- /dev/null +++ b/cpp/3rd_party/libtiff/tiffconf.h.cmake.in @@ -0,0 +1,127 @@ +/* + Configuration defines for installed libtiff. + This file maintained for backward compatibility. Do not use definitions + from this file in your programs. +*/ + +#ifndef _TIFFCONF_ +#define _TIFFCONF_ + +#include +#include + +/* Signed 16-bit type */ +#define TIFF_INT16_T @TIFF_INT16_T@ + +/* Signed 32-bit type */ +#define TIFF_INT32_T @TIFF_INT32_T@ + +/* Signed 64-bit type */ +#define TIFF_INT64_T @TIFF_INT64_T@ + +/* Signed 8-bit type */ +#define TIFF_INT8_T @TIFF_INT8_T@ + +/* Unsigned 16-bit type */ +#define TIFF_UINT16_T @TIFF_UINT16_T@ + +/* Unsigned 32-bit type */ +#define TIFF_UINT32_T @TIFF_UINT32_T@ + +/* Unsigned 64-bit type */ +#define TIFF_UINT64_T @TIFF_UINT64_T@ + +/* Unsigned 8-bit type */ +#define TIFF_UINT8_T @TIFF_UINT8_T@ + +/* Unsigned size type */ +#define TIFF_SIZE_T @TIFF_SIZE_T@ + +/* Signed size type */ +#define TIFF_SSIZE_T @TIFF_SSIZE_T@ + +/* Pointer difference type */ +#define TIFF_PTRDIFF_T @TIFF_PTRDIFF_T@ + +/* Compatibility stuff. */ + +/* Define as 0 or 1 according to the floating point format suported by the + machine */ +#cmakedefine HAVE_IEEEFP 1 + +/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */ +#define HOST_FILLORDER @HOST_FILLORDER@ + +/* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian + (Intel) */ +#define HOST_BIGENDIAN @HOST_BIG_ENDIAN@ + +/* Support CCITT Group 3 & 4 algorithms */ +#cmakedefine CCITT_SUPPORT 1 + +/* Support JPEG compression (requires IJG JPEG library) */ +#cmakedefine JPEG_SUPPORT 1 + +/* Support JBIG compression (requires JBIG-KIT library) */ +#cmakedefine JBIG_SUPPORT + +/* Support LogLuv high dynamic range encoding */ +#cmakedefine LOGLUV_SUPPORT 1 + +/* Support LZW algorithm */ +#cmakedefine LZW_SUPPORT 1 + +/* Support NeXT 2-bit RLE algorithm */ +#cmakedefine NEXT_SUPPORT 1 + +/* Support Old JPEG compresson (read contrib/ojpeg/README first! Compilation + fails with unpatched IJG JPEG library) */ +#cmakedefine OJPEG_SUPPORT 1 + +/* Support Macintosh PackBits algorithm */ +#cmakedefine PACKBITS_SUPPORT 1 + +/* Support Pixar log-format algorithm (requires Zlib) */ +#cmakedefine PIXARLOG_SUPPORT 1 + +/* Support ThunderScan 4-bit RLE algorithm */ +#cmakedefine THUNDER_SUPPORT 1 + +/* Support Deflate compression */ +#cmakedefine ZIP_SUPPORT 1 + +/* Support libdeflate enhanced compression */ +#cmakedefine LIBDEFLATE_SUPPORT 1 + +/* Support strip chopping (whether or not to convert single-strip uncompressed + images to mutiple strips of ~8Kb to reduce memory usage) */ +#cmakedefine STRIPCHOP_DEFAULT 1 + +/* Enable SubIFD tag (330) support */ +#cmakedefine SUBIFD_SUPPORT 1 + +/* Treat extra sample as alpha (default enabled). The RGBA interface will + treat a fourth sample with no EXTRASAMPLE_ value as being ASSOCALPHA. Many + packages produce RGBA files but don't mark the alpha properly. */ +#cmakedefine DEFAULT_EXTRASAMPLE_AS_ALPHA 1 + +/* Pick up YCbCr subsampling info from the JPEG data stream to support files + lacking the tag (default enabled). */ +#cmakedefine CHECK_JPEG_YCBCR_SUBSAMPLING 1 + +/* Support MS MDI magic number files as TIFF */ +#cmakedefine MDI_SUPPORT 1 + +/* + * Feature support definitions. + * XXX: These macros are obsoleted. Don't use them in your apps! + * Macros stays here for backward compatibility and should be always defined. + */ +#define COLORIMETRY_SUPPORT +#define YCBCR_SUPPORT +#define CMYK_SUPPORT +#define ICC_SUPPORT +#define PHOTOSHOP_SUPPORT +#define IPTC_SUPPORT + +#endif /* _TIFFCONF_ */ diff --git a/cpp/3rd_party/libtiff/tiffio.h b/cpp/3rd_party/libtiff/tiffio.h new file mode 100644 index 0000000000..f9c206e32a --- /dev/null +++ b/cpp/3rd_party/libtiff/tiffio.h @@ -0,0 +1,671 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _TIFFIO_ +#define _TIFFIO_ + +/* + * TIFF I/O Library Definitions. + */ +#include "tiff.h" +#include "tiffvers.h" + +/* + * TIFF is defined as an incomplete type to hide the + * library's internal data structures from clients. + */ +typedef struct tiff TIFF; + +/* + * The following typedefs define the intrinsic size of + * data types used in the *exported* interfaces. These + * definitions depend on the proper definition of types + * in tiff.h. Note also that the varargs interface used + * to pass tag types and values uses the types defined in + * tiff.h directly. + * + * NB: ttag_t -> deprecated and replaced by uint32_t + * is unsigned int and not unsigned short because + * ANSI C requires that the type before the ellipsis be a + * promoted type (i.e. one of int, unsigned int, pointer, + * or double) and because we defined pseudo-tags that are + * outside the range of legal Aldus-assigned tags. + * NB: tsize_t -> deprecated and replaced by tmsize_t + * is signed and not unsigned because some functions + * return -1. + * NB: toff_t is not off_t for many reasons; TIFFs max out at + * 32-bit file offsets, and BigTIFF maxes out at 64-bit + * offsets being the most important, and to ensure use of + * a consistently unsigned type across architectures. + * Prior to libtiff 4.0, this was an unsigned 32 bit type. + */ +/* + * this is the machine addressing size type, only it's signed, so make it + * int32_t on 32bit machines, int64_t on 64bit machines + */ +typedef TIFF_SSIZE_T tmsize_t; +#define TIFF_TMSIZE_T_MAX (tmsize_t)(SIZE_MAX >> 1) + +typedef uint64_t toff_t; /* file offset */ +typedef uint32_t tdir_t; /* directory index */ + +/* the following are deprecated and should be replaced by their defining + counterparts */ +typedef uint32_t ttag_t; /* directory tag */ +typedef uint16_t tsample_t; /* sample number */ +typedef uint32_t tstrile_t; /* strip or tile number */ +typedef tstrile_t tstrip_t; /* strip number */ +typedef tstrile_t ttile_t; /* tile number */ +typedef tmsize_t tsize_t; /* i/o size in bytes */ +typedef void *tdata_t; /* image data ref */ + +/* + * On windows you should define USE_WIN32_FILEIO if you are using tif_win32.c + * or AVOID_WIN32_FILEIO if you are using something else (like tif_unix.c). + * + * By default tif_unix.c is assumed. + */ + +#if defined(_WIN32) +#if !defined(__CYGWIN) && !defined(AVOID_WIN32_FILEIO) && \ + !defined(USE_WIN32_FILEIO) +#define AVOID_WIN32_FILEIO +#endif +#endif + +#if defined(USE_WIN32_FILEIO) +#define VC_EXTRALEAN +#include +#ifdef _WIN32 +DECLARE_HANDLE(thandle_t); /* Win32 file handle */ +#else +typedef HFILE thandle_t; /* client data handle */ +#endif /* _WIN32 */ +#else +typedef void *thandle_t; /* client data handle */ +#endif /* USE_WIN32_FILEIO */ + +/* + * Flags to pass to TIFFPrintDirectory to control + * printing of data structures that are potentially + * very large. Bit-or these flags to enable printing + * multiple items. + */ +#define TIFFPRINT_NONE 0x0 /* no extra info */ +#define TIFFPRINT_STRIPS 0x1 /* strips/tiles info */ +#define TIFFPRINT_CURVES 0x2 /* color/gray response curves */ +#define TIFFPRINT_COLORMAP 0x4 /* colormap */ +#define TIFFPRINT_JPEGQTABLES 0x100 /* JPEG Q matrices */ +#define TIFFPRINT_JPEGACTABLES 0x200 /* JPEG AC tables */ +#define TIFFPRINT_JPEGDCTABLES 0x200 /* JPEG DC tables */ + +/* + * Colour conversion stuff + */ + +/* reference white */ +#define D65_X0 (95.0470F) +#define D65_Y0 (100.0F) +#define D65_Z0 (108.8827F) + +#define D50_X0 (96.4250F) +#define D50_Y0 (100.0F) +#define D50_Z0 (82.4680F) + +/* Structure for holding information about a display device. */ + +typedef unsigned char TIFFRGBValue; /* 8-bit samples */ + +typedef struct +{ + float d_mat[3][3]; /* XYZ -> luminance matrix */ + float d_YCR; /* Light o/p for reference white */ + float d_YCG; + float d_YCB; + uint32_t d_Vrwr; /* Pixel values for ref. white */ + uint32_t d_Vrwg; + uint32_t d_Vrwb; + float d_Y0R; /* Residual light for black pixel */ + float d_Y0G; + float d_Y0B; + float d_gammaR; /* Gamma values for the three guns */ + float d_gammaG; + float d_gammaB; +} TIFFDisplay; + +/* YCbCr->RGB support for TIFFYCbCrToRGBInit() and TIFFYCbCrToRGB() + * Attention: + * Functions TIFFYCbCrToRGBInit() and TIFFYCbCrToRGB() require a user provided + * large memory buffer, where several tables can be setup. + * The pointers to these tables are stored in the structure TIFFYCbCrToRGB, + * which is located at the beginning of the buffer. Thus, this memory has to be + * allocated as follows: + * TIFFYCbCrToRGB *ycbcr = (TIFFYCbCrToRGB *)_TIFFmalloc( + * TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) + + * 4 * 256 * sizeof(TIFFRGBValue) + 2 * 256 * sizeof(int) + + * 3 * 256 * sizeof(int32_t)); + */ +typedef struct +{ /* YCbCr->RGB support */ + TIFFRGBValue *clamptab; /* range clamping table */ + int *Cr_r_tab; + int *Cb_b_tab; + int32_t *Cr_g_tab; + int32_t *Cb_g_tab; + int32_t *Y_tab; +} TIFFYCbCrToRGB; + +typedef struct +{ /* CIE Lab 1976->RGB support */ + int range; /* Size of conversion table */ +#define CIELABTORGB_TABLE_RANGE 1500 + float rstep, gstep, bstep; + float X0, Y0, Z0; /* Reference white point */ + TIFFDisplay display; + float Yr2r[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yr to r */ + float Yg2g[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yg to g */ + float Yb2b[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yb to b */ +} TIFFCIELabToRGB; + +/* + * RGBA-style image support. + */ +typedef struct _TIFFRGBAImage TIFFRGBAImage; +/* + * The image reading and conversion routines invoke + * ``put routines'' to copy/image/whatever tiles of + * raw image data. A default set of routines are + * provided to convert/copy raw image data to 8-bit + * packed ABGR format rasters. Applications can supply + * alternate routines that unpack the data into a + * different format or, for example, unpack the data + * and draw the unpacked raster on the display. + */ +typedef void (*tileContigRoutine)(TIFFRGBAImage *, uint32_t *, uint32_t, + uint32_t, uint32_t, uint32_t, int32_t, + int32_t, unsigned char *); +typedef void (*tileSeparateRoutine)(TIFFRGBAImage *, uint32_t *, uint32_t, + uint32_t, uint32_t, uint32_t, int32_t, + int32_t, unsigned char *, unsigned char *, + unsigned char *, unsigned char *); +/* + * RGBA-reader state. + */ +struct _TIFFRGBAImage +{ + TIFF *tif; /* image handle */ + int stoponerr; /* stop on read error */ + int isContig; /* data is packed/separate */ + int alpha; /* type of alpha data present */ + uint32_t width; /* image width */ + uint32_t height; /* image height */ + uint16_t bitspersample; /* image bits/sample */ + uint16_t samplesperpixel; /* image samples/pixel */ + uint16_t orientation; /* image orientation */ + uint16_t req_orientation; /* requested orientation */ + uint16_t photometric; /* image photometric interp */ + uint16_t *redcmap; /* colormap palette */ + uint16_t *greencmap; + uint16_t *bluecmap; + /* get image data routine */ + int (*get)(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); + /* put decoded strip/tile */ + union + { + void (*any)(TIFFRGBAImage *); + tileContigRoutine contig; + tileSeparateRoutine separate; + } put; + TIFFRGBValue *Map; /* sample mapping array */ + uint32_t **BWmap; /* black&white map */ + uint32_t **PALmap; /* palette image map */ + TIFFYCbCrToRGB *ycbcr; /* YCbCr conversion state */ + TIFFCIELabToRGB *cielab; /* CIE L*a*b conversion state */ + + uint8_t *UaToAa; /* Unassociated alpha to associated alpha conversion LUT */ + uint8_t *Bitdepth16To8; /* LUT for conversion from 16bit to 8bit values */ + + int row_offset; + int col_offset; +}; + +/* + * Macros for extracting components from the + * packed ABGR form returned by TIFFReadRGBAImage. + */ +#define TIFFGetR(abgr) ((abgr)&0xff) +#define TIFFGetG(abgr) (((abgr) >> 8) & 0xff) +#define TIFFGetB(abgr) (((abgr) >> 16) & 0xff) +#define TIFFGetA(abgr) (((abgr) >> 24) & 0xff) + +/* + * A CODEC is a software package that implements decoding, + * encoding, or decoding+encoding of a compression algorithm. + * The library provides a collection of builtin codecs. + * More codecs may be registered through calls to the library + * and/or the builtin implementations may be overridden. + */ +typedef int (*TIFFInitMethod)(TIFF *, int); +typedef struct +{ + char *name; + uint16_t scheme; + TIFFInitMethod init; +} TIFFCodec; + +typedef struct +{ + uint32_t uNum; + uint32_t uDenom; +} TIFFRational_t; + +#include +#include + +/* share internal LogLuv conversion routines? */ +#ifndef LOGLUV_PUBLIC +#define LOGLUV_PUBLIC 1 +#endif + +#if defined(__GNUC__) || defined(__clang__) || defined(__attribute__) +#define TIFF_ATTRIBUTE(x) __attribute__(x) +#else +#define TIFF_ATTRIBUTE(x) /*nothing*/ +#endif + +#if defined(c_plusplus) || defined(__cplusplus) +extern "C" +{ +#endif + typedef void (*TIFFErrorHandler)(const char *, const char *, va_list); + typedef void (*TIFFErrorHandlerExt)(thandle_t, const char *, const char *, + va_list); + typedef int (*TIFFErrorHandlerExtR)(TIFF *, void *user_data, const char *, + const char *, va_list); + typedef tmsize_t (*TIFFReadWriteProc)(thandle_t, void *, tmsize_t); + typedef toff_t (*TIFFSeekProc)(thandle_t, toff_t, int); + typedef int (*TIFFCloseProc)(thandle_t); + typedef toff_t (*TIFFSizeProc)(thandle_t); + typedef int (*TIFFMapFileProc)(thandle_t, void **base, toff_t *size); + typedef void (*TIFFUnmapFileProc)(thandle_t, void *base, toff_t size); + typedef void (*TIFFExtendProc)(TIFF *); + + extern const char *TIFFGetVersion(void); + + extern const TIFFCodec *TIFFFindCODEC(uint16_t); + extern TIFFCodec *TIFFRegisterCODEC(uint16_t, const char *, TIFFInitMethod); + extern void TIFFUnRegisterCODEC(TIFFCodec *); + extern int TIFFIsCODECConfigured(uint16_t); + extern TIFFCodec *TIFFGetConfiguredCODECs(void); + + /* + * Auxiliary functions. + */ +#ifndef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS + extern void *_TIFFmalloc(tmsize_t s); + extern void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz); + extern void *_TIFFrealloc(void *p, tmsize_t s); + extern void _TIFFfree(void *p); +#endif + extern void _TIFFmemset(void *p, int v, tmsize_t c); + extern void _TIFFmemcpy(void *d, const void *s, tmsize_t c); + extern int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c); + + /* + ** Stuff, related to tag handling and creating custom tags. + */ + extern int TIFFGetTagListCount(TIFF *); + extern uint32_t TIFFGetTagListEntry(TIFF *, int tag_index); + +#define TIFF_ANY TIFF_NOTYPE /* for field descriptor searching */ +#define TIFF_VARIABLE -1 /* marker for variable length tags */ +#define TIFF_SPP -2 /* marker for SamplesPerPixel tags */ +#define TIFF_VARIABLE2 -3 /* marker for uint32_t var-length tags */ + +#define FIELD_CUSTOM 65 + + typedef struct _TIFFField TIFFField; + typedef struct _TIFFFieldArray TIFFFieldArray; + + extern const TIFFField *TIFFFindField(TIFF *, uint32_t, TIFFDataType); + extern const TIFFField *TIFFFieldWithTag(TIFF *, uint32_t); + extern const TIFFField *TIFFFieldWithName(TIFF *, const char *); + + extern uint32_t TIFFFieldTag(const TIFFField *); + extern const char *TIFFFieldName(const TIFFField *); + extern TIFFDataType TIFFFieldDataType(const TIFFField *); + extern int TIFFFieldPassCount(const TIFFField *); + extern int TIFFFieldReadCount(const TIFFField *); + extern int TIFFFieldWriteCount(const TIFFField *); + extern int + TIFFFieldSetGetSize(const TIFFField *); /* returns internal storage size of + TIFFSetGetFieldType in bytes. */ + extern int TIFFFieldSetGetCountSize( + const TIFFField *); /* returns size of count parameter 0=none, + 2=uint16_t, 4=uint32_t */ + extern int TIFFFieldIsAnonymous(const TIFFField *); + + typedef int (*TIFFVSetMethod)(TIFF *, uint32_t, va_list); + typedef int (*TIFFVGetMethod)(TIFF *, uint32_t, va_list); + typedef void (*TIFFPrintMethod)(TIFF *, FILE *, long); + + typedef struct + { + TIFFVSetMethod vsetfield; /* tag set routine */ + TIFFVGetMethod vgetfield; /* tag get routine */ + TIFFPrintMethod printdir; /* directory print routine */ + } TIFFTagMethods; + + extern TIFFTagMethods *TIFFAccessTagMethods(TIFF *); + extern void *TIFFGetClientInfo(TIFF *, const char *); + extern void TIFFSetClientInfo(TIFF *, void *, const char *); + + extern void TIFFCleanup(TIFF *tif); + extern void TIFFClose(TIFF *tif); + extern int TIFFFlush(TIFF *tif); + extern int TIFFFlushData(TIFF *tif); + extern int TIFFGetField(TIFF *tif, uint32_t tag, ...); + extern int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap); + extern int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag, ...); + extern int TIFFVGetFieldDefaulted(TIFF *tif, uint32_t tag, va_list ap); + extern int TIFFReadDirectory(TIFF *tif); + extern int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff, + const TIFFFieldArray *infoarray); + extern int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff); + extern int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff); + extern uint64_t TIFFScanlineSize64(TIFF *tif); + extern tmsize_t TIFFScanlineSize(TIFF *tif); + extern uint64_t TIFFRasterScanlineSize64(TIFF *tif); + extern tmsize_t TIFFRasterScanlineSize(TIFF *tif); + extern uint64_t TIFFStripSize64(TIFF *tif); + extern tmsize_t TIFFStripSize(TIFF *tif); + extern uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip); + extern tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip); + extern uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows); + extern tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows); + extern uint64_t TIFFTileRowSize64(TIFF *tif); + extern tmsize_t TIFFTileRowSize(TIFF *tif); + extern uint64_t TIFFTileSize64(TIFF *tif); + extern tmsize_t TIFFTileSize(TIFF *tif); + extern uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows); + extern tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows); + extern uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request); + extern void TIFFDefaultTileSize(TIFF *, uint32_t *, uint32_t *); + extern int TIFFFileno(TIFF *); + extern int TIFFSetFileno(TIFF *, int); + extern thandle_t TIFFClientdata(TIFF *); + extern thandle_t TIFFSetClientdata(TIFF *, thandle_t); + extern int TIFFGetMode(TIFF *); + extern int TIFFSetMode(TIFF *, int); + extern int TIFFIsTiled(TIFF *); + extern int TIFFIsByteSwapped(TIFF *); + extern int TIFFIsUpSampled(TIFF *); + extern int TIFFIsMSB2LSB(TIFF *); + extern int TIFFIsBigEndian(TIFF *); + extern int TIFFIsBigTIFF(TIFF *); + extern TIFFReadWriteProc TIFFGetReadProc(TIFF *); + extern TIFFReadWriteProc TIFFGetWriteProc(TIFF *); + extern TIFFSeekProc TIFFGetSeekProc(TIFF *); + extern TIFFCloseProc TIFFGetCloseProc(TIFF *); + extern TIFFSizeProc TIFFGetSizeProc(TIFF *); + extern TIFFMapFileProc TIFFGetMapFileProc(TIFF *); + extern TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *); + extern uint32_t TIFFCurrentRow(TIFF *); + extern tdir_t TIFFCurrentDirectory(TIFF *); + extern tdir_t TIFFNumberOfDirectories(TIFF *); + extern uint64_t TIFFCurrentDirOffset(TIFF *); + extern uint32_t TIFFCurrentStrip(TIFF *); + extern uint32_t TIFFCurrentTile(TIFF *tif); + extern int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size); + extern int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size); + extern int TIFFSetupStrips(TIFF *); + extern int TIFFWriteCheck(TIFF *, int, const char *); + extern void TIFFFreeDirectory(TIFF *); + extern int TIFFCreateDirectory(TIFF *); + extern int TIFFCreateCustomDirectory(TIFF *, const TIFFFieldArray *); + extern int TIFFCreateEXIFDirectory(TIFF *); + extern int TIFFCreateGPSDirectory(TIFF *); + extern int TIFFLastDirectory(TIFF *); + extern int TIFFSetDirectory(TIFF *, tdir_t); + extern int TIFFSetSubDirectory(TIFF *, uint64_t); + extern int TIFFUnlinkDirectory(TIFF *, tdir_t); + extern int TIFFSetField(TIFF *, uint32_t, ...); + extern int TIFFVSetField(TIFF *, uint32_t, va_list); + extern int TIFFUnsetField(TIFF *, uint32_t); + extern int TIFFWriteDirectory(TIFF *); + extern int TIFFWriteCustomDirectory(TIFF *, uint64_t *); + extern int TIFFCheckpointDirectory(TIFF *); + extern int TIFFRewriteDirectory(TIFF *); + extern int TIFFDeferStrileArrayWriting(TIFF *); + extern int TIFFForceStrileArrayWriting(TIFF *); + +#if defined(c_plusplus) || defined(__cplusplus) + extern void TIFFPrintDirectory(TIFF *, FILE *, long = 0); + extern int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, + uint16_t sample = 0); + extern int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, + uint16_t sample = 0); + extern int TIFFReadRGBAImage(TIFF *, uint32_t, uint32_t, uint32_t *, + int = 0); + extern int TIFFReadRGBAImageOriented(TIFF *, uint32_t, uint32_t, uint32_t *, + int = ORIENTATION_BOTLEFT, int = 0); +#else +extern void TIFFPrintDirectory(TIFF *, FILE *, long); +extern int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, + uint16_t sample); +extern int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, + uint16_t sample); +extern int TIFFReadRGBAImage(TIFF *, uint32_t, uint32_t, uint32_t *, int); +extern int TIFFReadRGBAImageOriented(TIFF *, uint32_t, uint32_t, uint32_t *, + int, int); +#endif + + extern int TIFFReadRGBAStrip(TIFF *, uint32_t, uint32_t *); + extern int TIFFReadRGBATile(TIFF *, uint32_t, uint32_t, uint32_t *); + extern int TIFFReadRGBAStripExt(TIFF *, uint32_t, uint32_t *, + int stop_on_error); + extern int TIFFReadRGBATileExt(TIFF *, uint32_t, uint32_t, uint32_t *, + int stop_on_error); + extern int TIFFRGBAImageOK(TIFF *, char[1024]); + extern int TIFFRGBAImageBegin(TIFFRGBAImage *, TIFF *, int, char[1024]); + extern int TIFFRGBAImageGet(TIFFRGBAImage *, uint32_t *, uint32_t, + uint32_t); + extern void TIFFRGBAImageEnd(TIFFRGBAImage *); + + extern const char *TIFFFileName(TIFF *); + extern const char *TIFFSetFileName(TIFF *, const char *); + extern void TIFFError(const char *, const char *, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 2, 3))); + extern void TIFFErrorExt(thandle_t, const char *, const char *, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 3, 4))); + extern void TIFFWarning(const char *, const char *, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 2, 3))); + extern void TIFFWarningExt(thandle_t, const char *, const char *, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 3, 4))); + extern TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler); + extern TIFFErrorHandlerExt TIFFSetErrorHandlerExt(TIFFErrorHandlerExt); + extern TIFFErrorHandler TIFFSetWarningHandler(TIFFErrorHandler); + extern TIFFErrorHandlerExt TIFFSetWarningHandlerExt(TIFFErrorHandlerExt); + + extern void TIFFWarningExtR(TIFF *, const char *, const char *, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 3, 4))); + extern void TIFFErrorExtR(TIFF *, const char *, const char *, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 3, 4))); + + typedef struct TIFFOpenOptions TIFFOpenOptions; + extern TIFFOpenOptions *TIFFOpenOptionsAlloc(void); + extern void TIFFOpenOptionsFree(TIFFOpenOptions *); + extern void + TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts, + tmsize_t max_single_mem_alloc); + extern void + TIFFOpenOptionsSetMaxCumulatedMemAlloc(TIFFOpenOptions *opts, + tmsize_t max_cumulated_mem_alloc); + extern void + TIFFOpenOptionsSetWarnAboutUnknownTags(TIFFOpenOptions *opts, + int warn_about_unknown_tags); + extern void + TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts, + TIFFErrorHandlerExtR handler, + void *errorhandler_user_data); + extern void + TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts, + TIFFErrorHandlerExtR handler, + void *warnhandler_user_data); + + extern TIFF *TIFFOpen(const char *, const char *); + extern TIFF *TIFFOpenExt(const char *, const char *, TIFFOpenOptions *opts); +#ifdef _WIN32 + extern TIFF *TIFFOpenW(const wchar_t *, const char *); + extern TIFF *TIFFOpenWExt(const wchar_t *, const char *, + TIFFOpenOptions *opts); +#endif /* _WIN32 */ + extern TIFF *TIFFFdOpen(int, const char *, const char *); + extern TIFF *TIFFFdOpenExt(int, const char *, const char *, + TIFFOpenOptions *opts); + extern TIFF *TIFFClientOpen(const char *, const char *, thandle_t, + TIFFReadWriteProc, TIFFReadWriteProc, + TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, + TIFFMapFileProc, TIFFUnmapFileProc); + extern TIFF *TIFFClientOpenExt(const char *, const char *, thandle_t, + TIFFReadWriteProc, TIFFReadWriteProc, + TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, + TIFFMapFileProc, TIFFUnmapFileProc, + TIFFOpenOptions *opts); + extern TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc); + extern uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, + uint32_t z, uint16_t s); + extern int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, + uint16_t s); + extern uint32_t TIFFNumberOfTiles(TIFF *); + extern tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, + uint32_t z, uint16_t s); + extern tmsize_t TIFFWriteTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, + uint32_t z, uint16_t s); + extern uint32_t TIFFComputeStrip(TIFF *, uint32_t, uint16_t); + extern uint32_t TIFFNumberOfStrips(TIFF *); + extern tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf, + tmsize_t size); + extern tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, + tmsize_t size); + extern tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, + tmsize_t size); + extern tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, + tmsize_t size); + extern int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf, + tmsize_t insize, void *outbuf, + tmsize_t outsize); + extern tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data, + tmsize_t cc); + extern tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, + tmsize_t cc); + extern tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, + tmsize_t cc); + extern tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, + tmsize_t cc); + extern int TIFFDataWidth( + TIFFDataType); /* table of tag datatype widths within TIFF file. */ + extern void TIFFSetWriteOffset(TIFF *tif, toff_t off); + extern void TIFFSwabShort(uint16_t *); + extern void TIFFSwabLong(uint32_t *); + extern void TIFFSwabLong8(uint64_t *); + extern void TIFFSwabFloat(float *); + extern void TIFFSwabDouble(double *); + extern void TIFFSwabArrayOfShort(uint16_t *wp, tmsize_t n); + extern void TIFFSwabArrayOfTriples(uint8_t *tp, tmsize_t n); + extern void TIFFSwabArrayOfLong(uint32_t *lp, tmsize_t n); + extern void TIFFSwabArrayOfLong8(uint64_t *lp, tmsize_t n); + extern void TIFFSwabArrayOfFloat(float *fp, tmsize_t n); + extern void TIFFSwabArrayOfDouble(double *dp, tmsize_t n); + extern void TIFFReverseBits(uint8_t *cp, tmsize_t n); + extern const unsigned char *TIFFGetBitRevTable(int); + + extern uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile); + extern uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile); + extern uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, + int *pbErr); + extern uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, + int *pbErr); + +#if LOGLUV_PUBLIC +#define U_NEU 0.210526316 +#define V_NEU 0.473684211 +#define UVSCALE 410. + extern double LogL16toY(int); + extern double LogL10toY(int); + extern void XYZtoRGB24(float *, uint8_t *); + extern int uv_decode(double *, double *, int); + extern void LogLuv24toXYZ(uint32_t, float *); + extern void LogLuv32toXYZ(uint32_t, float *); +#if defined(c_plusplus) || defined(__cplusplus) + extern int LogL16fromY(double, int = SGILOGENCODE_NODITHER); + extern int LogL10fromY(double, int = SGILOGENCODE_NODITHER); + extern int uv_encode(double, double, int = SGILOGENCODE_NODITHER); + extern uint32_t LogLuv24fromXYZ(float *, int = SGILOGENCODE_NODITHER); + extern uint32_t LogLuv32fromXYZ(float *, int = SGILOGENCODE_NODITHER); +#else + extern int LogL16fromY(double, int); + extern int LogL10fromY(double, int); + extern int uv_encode(double, double, int); + extern uint32_t LogLuv24fromXYZ(float *, int); + extern uint32_t LogLuv32fromXYZ(float *, int); +#endif +#endif /* LOGLUV_PUBLIC */ + + extern int TIFFCIELabToRGBInit(TIFFCIELabToRGB *, const TIFFDisplay *, + float *); + extern void TIFFCIELabToXYZ(TIFFCIELabToRGB *, uint32_t, int32_t, int32_t, + float *, float *, float *); + extern void TIFFXYZToRGB(TIFFCIELabToRGB *, float, float, float, uint32_t *, + uint32_t *, uint32_t *); + + extern int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *, float *, float *); + extern void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *, uint32_t, int32_t, int32_t, + uint32_t *, uint32_t *, uint32_t *); + + /**************************************************************************** + * O B S O L E T E D I N T E R F A C E S + * + * Don't use this stuff in your applications, it may be removed in the + *future libtiff versions. + ****************************************************************************/ + typedef struct + { + uint32_t field_tag; /* field's tag */ + short field_readcount; /* read count/TIFF_VARIABLE/TIFF_SPP */ + short field_writecount; /* write count/TIFF_VARIABLE */ + TIFFDataType field_type; /* type of associated data */ + unsigned short field_bit; /* bit in fieldsset bit vector */ + unsigned char field_oktochange; /* if true, can change while writing */ + unsigned char field_passcount; /* if true, pass dir count on set */ + char *field_name; /* ASCII name */ + } TIFFFieldInfo; + + extern int TIFFMergeFieldInfo(TIFF *, const TIFFFieldInfo[], uint32_t); + +#if defined(c_plusplus) || defined(__cplusplus) +} +#endif + +#endif /* _TIFFIO_ */ diff --git a/cpp/3rd_party/libtiff/tiffio.hxx b/cpp/3rd_party/libtiff/tiffio.hxx new file mode 100644 index 0000000000..df2cbbceb7 --- /dev/null +++ b/cpp/3rd_party/libtiff/tiffio.hxx @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _TIFFIO_HXX_ +#define _TIFFIO_HXX_ + +/* + * TIFF I/O library definitions which provide C++ streams API. + */ + +#include +#include "tiff.h" +#include "tiffio.h" + +extern TIFF* TIFFStreamOpen(const char*, std::ostream *); +extern TIFF* TIFFStreamOpen(const char*, std::istream *); + +#endif /* _TIFFIO_HXX_ */ + +/* vim: set ts=8 sts=8 sw=8 noet: */ +/* + * Local Variables: + * mode: c++ + * c-basic-offset: 8 + * fill-column: 78 + * End: + */ diff --git a/cpp/3rd_party/libtiff/tiffiop.h b/cpp/3rd_party/libtiff/tiffiop.h new file mode 100644 index 0000000000..80c04c279b --- /dev/null +++ b/cpp/3rd_party/libtiff/tiffiop.h @@ -0,0 +1,549 @@ +/* + * Copyright (c) 1988-1997 Sam Leffler + * Copyright (c) 1991-1997 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef _TIFFIOP_ +#define _TIFFIOP_ +/* + * ``Library-private'' definitions. + */ + +#include "tif_config.h" + +#ifdef HAVE_FCNTL_H +#include +#endif + +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +#include + +#ifdef HAVE_ASSERT_H +#include +#else +#define assert(x) +#endif + +#include "tif_hash_set.h" +#include "tiffio.h" + +#include "tif_dir.h" + +#include + +#ifndef STRIP_SIZE_DEFAULT +#define STRIP_SIZE_DEFAULT 8192 +#endif + +#ifndef TIFF_MAX_DIR_COUNT +#define TIFF_MAX_DIR_COUNT 1048576 +#endif + +#define TIFF_NON_EXISTENT_DIR_NUMBER UINT_MAX + +#define streq(a, b) (strcmp(a, b) == 0) +#define strneq(a, b, n) (strncmp(a, b, n) == 0) + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif + +typedef struct client_info +{ + struct client_info *next; + void *data; + char *name; +} TIFFClientInfoLink; + +/* + * Typedefs for ``method pointers'' used internally. + * these are deprecated and provided only for backwards compatibility. + */ +typedef unsigned char tidataval_t; /* internal image data value type */ +typedef tidataval_t *tidata_t; /* reference to internal image data */ + +typedef void (*TIFFVoidMethod)(TIFF *); +typedef int (*TIFFBoolMethod)(TIFF *); +typedef int (*TIFFPreMethod)(TIFF *, uint16_t); +typedef int (*TIFFCodeMethod)(TIFF *tif, uint8_t *buf, tmsize_t size, + uint16_t sample); +typedef int (*TIFFSeekMethod)(TIFF *, uint32_t); +typedef void (*TIFFPostMethod)(TIFF *tif, uint8_t *buf, tmsize_t size); +typedef uint32_t (*TIFFStripMethod)(TIFF *, uint32_t); +typedef void (*TIFFTileMethod)(TIFF *, uint32_t *, uint32_t *); + +struct TIFFOffsetAndDirNumber +{ + uint64_t offset; + tdir_t dirNumber; +}; +typedef struct TIFFOffsetAndDirNumber TIFFOffsetAndDirNumber; + +typedef union +{ + TIFFHeaderCommon common; + TIFFHeaderClassic classic; + TIFFHeaderBig big; +} TIFFHeaderUnion; + +struct tiff +{ + char *tif_name; /* name of open file */ + int tif_fd; /* open file descriptor */ + int tif_mode; /* open mode (O_*) */ + uint32_t tif_flags; +#define TIFF_FILLORDER 0x00003U /* natural bit fill order for machine */ +#define TIFF_DIRTYHEADER 0x00004U /* header must be written on close */ +#define TIFF_DIRTYDIRECT 0x00008U /* current directory must be written */ +#define TIFF_BUFFERSETUP 0x00010U /* data buffers setup */ +#define TIFF_CODERSETUP 0x00020U /* encoder/decoder setup done */ +#define TIFF_BEENWRITING 0x00040U /* written 1+ scanlines to file */ +#define TIFF_SWAB 0x00080U /* byte swap file information */ +#define TIFF_NOBITREV 0x00100U /* inhibit bit reversal logic */ +#define TIFF_MYBUFFER 0x00200U /* my raw data buffer; free on close */ +#define TIFF_ISTILED 0x00400U /* file is tile, not strip- based */ +#define TIFF_MAPPED 0x00800U /* file is mapped into memory */ +#define TIFF_POSTENCODE 0x01000U /* need call to postencode routine */ +#define TIFF_INSUBIFD 0x02000U /* currently writing a subifd */ +#define TIFF_UPSAMPLED 0x04000U /* library is doing data up-sampling */ +#define TIFF_STRIPCHOP 0x08000U /* enable strip chopping support */ +#define TIFF_HEADERONLY \ + 0x10000U /* read header only, do not process the first directory */ +#define TIFF_NOREADRAW \ + 0x20000U /* skip reading of raw uncompressed image data */ +#define TIFF_INCUSTOMIFD 0x40000U /* currently writing a custom IFD */ +#define TIFF_BIGTIFF 0x80000U /* read/write bigtiff */ +#define TIFF_BUF4WRITE 0x100000U /* rawcc bytes are for writing */ +#define TIFF_DIRTYSTRIP 0x200000U /* stripoffsets/stripbytecount dirty*/ +#define TIFF_PERSAMPLE 0x400000U /* get/set per sample tags as arrays */ +#define TIFF_BUFFERMMAP \ + 0x800000U /* read buffer (tif_rawdata) points into mmap() memory */ +#define TIFF_DEFERSTRILELOAD \ + 0x1000000U /* defer strip/tile offset/bytecount array loading. */ +#define TIFF_LAZYSTRILELOAD \ + 0x2000000U /* lazy/ondemand loading of strip/tile offset/bytecount values. \ + Only used if TIFF_DEFERSTRILELOAD is set and in read-only \ + mode */ +#define TIFF_CHOPPEDUPARRAYS \ + 0x4000000U /* set when allocChoppedUpStripArrays() has modified strip \ + array */ + uint64_t tif_diroff; /* file offset of current directory */ + uint64_t tif_nextdiroff; /* file offset of following directory */ + uint64_t tif_lastdiroff; /* file offset of last directory written so far */ + TIFFHashSet *tif_map_dir_offset_to_number; + TIFFHashSet *tif_map_dir_number_to_offset; + int tif_setdirectory_force_absolute; /* switch between relative and absolute + stepping in TIFFSetDirectory() */ + TIFFDirectory tif_dir; /* internal rep of current directory */ + TIFFDirectory + tif_customdir; /* custom IFDs are separated from the main ones */ + TIFFHeaderUnion tif_header; /* file's header block Classic/BigTIFF union */ + uint16_t tif_header_size; /* file's header block and its length */ + uint32_t tif_row; /* current scanline */ + + /* There are IFDs in the file and an "active" IFD in memory, + * from which fields are "set" and "get". + * tif_curdir is set to: + * a) TIFF_NON_EXISTENT_DIR_NUMBER if there is no IFD in the file + * or the state is unknown, + * or the last read (i.e. TIFFFetchDirectory()) failed, + * or a custom directory was written. + * b) IFD index of last IFD written in the file. In this case the + * active IFD is a new (empty) one and tif_diroff is zero. + * If writing fails, tif_curdir is not changed. + * c) IFD index of IFD read from file into memory (=active IFD), + * even if IFD is corrupt and TIFFReadDirectory() returns 0. + * Then tif_diroff contains the offset of the IFD in the file. + * d) IFD index 0, whenever a custom directory or an unchained SubIFD + * was read. */ + tdir_t tif_curdir; /* current directory (index) */ + /* tif_curdircount: number of directories (main-IFDs) in file: + * - TIFF_NON_EXISTENT_DIR_NUMBER means 'dont know number of IFDs'. + * - 0 means 'empty file opened for writing, but no IFD written yet' */ + tdir_t tif_curdircount; + uint32_t tif_curstrip; /* current strip for read/write */ + uint64_t tif_curoff; /* current offset for read/write */ + uint64_t tif_lastvalidoff; /* last valid offset allowed for rewrite in + place. Used only by TIFFAppendToStrip() */ + uint64_t tif_dataoff; /* current offset for writing dir (IFD) */ + /* SubIFD support */ + uint16_t tif_nsubifd; /* remaining subifds to write */ + uint64_t tif_subifdoff; /* offset for patching SubIFD link */ + /* tiling support */ + uint32_t tif_col; /* current column (offset by row too) */ + uint32_t tif_curtile; /* current tile for read/write */ + tmsize_t tif_tilesize; /* # of bytes in a tile */ + /* compression scheme hooks */ + int tif_decodestatus; + TIFFBoolMethod tif_fixuptags; /* called in TIFFReadDirectory */ + TIFFBoolMethod tif_setupdecode; /* called once before predecode */ + TIFFPreMethod tif_predecode; /* pre- row/strip/tile decoding */ + TIFFBoolMethod tif_setupencode; /* called once before preencode */ + int tif_encodestatus; + TIFFPreMethod tif_preencode; /* pre- row/strip/tile encoding */ + TIFFBoolMethod tif_postencode; /* post- row/strip/tile encoding */ + TIFFCodeMethod tif_decoderow; /* scanline decoding routine */ + TIFFCodeMethod tif_encoderow; /* scanline encoding routine */ + TIFFCodeMethod tif_decodestrip; /* strip decoding routine */ + TIFFCodeMethod tif_encodestrip; /* strip encoding routine */ + TIFFCodeMethod tif_decodetile; /* tile decoding routine */ + TIFFCodeMethod tif_encodetile; /* tile encoding routine */ + TIFFVoidMethod tif_close; /* cleanup-on-close routine */ + TIFFSeekMethod tif_seek; /* position within a strip routine */ + TIFFVoidMethod tif_cleanup; /* cleanup state routine */ + TIFFStripMethod tif_defstripsize; /* calculate/constrain strip size */ + TIFFTileMethod tif_deftilesize; /* calculate/constrain tile size */ + uint8_t *tif_data; /* compression scheme private data */ + /* input/output buffering */ + tmsize_t tif_scanlinesize; /* # of bytes in a scanline */ + tmsize_t tif_scanlineskew; /* scanline skew for reading strips */ + uint8_t *tif_rawdata; /* raw data buffer */ + tmsize_t tif_rawdatasize; /* # of bytes in raw data buffer */ + tmsize_t tif_rawdataoff; /* rawdata offset within strip */ + tmsize_t tif_rawdataloaded; /* amount of data in rawdata */ + uint8_t *tif_rawcp; /* current spot in raw buffer */ + tmsize_t tif_rawcc; /* bytes unread from raw buffer */ + /* memory-mapped file support */ + uint8_t *tif_base; /* base of mapped file */ + tmsize_t tif_size; /* size of mapped file region (bytes, thus tmsize_t) */ + TIFFMapFileProc tif_mapproc; /* map file method */ + TIFFUnmapFileProc tif_unmapproc; /* unmap file method */ + /* input/output callback methods */ + thandle_t tif_clientdata; /* callback parameter */ + TIFFReadWriteProc tif_readproc; /* read method */ + TIFFReadWriteProc tif_writeproc; /* write method */ + TIFFSeekProc tif_seekproc; /* lseek method */ + TIFFCloseProc tif_closeproc; /* close method */ + TIFFSizeProc tif_sizeproc; /* filesize method */ + /* post-decoding support */ + TIFFPostMethod tif_postdecode; /* post decoding routine */ + /* tag support */ + TIFFField **tif_fields; /* sorted table of registered tags */ + size_t tif_nfields; /* # entries in registered tag table */ + const TIFFField *tif_foundfield; /* cached pointer to already found tag */ + TIFFTagMethods tif_tagmethods; /* tag get/set/print routines */ + TIFFClientInfoLink *tif_clientinfo; /* extra client information. */ + /* Backward compatibility stuff. We need these two fields for + * setting up an old tag extension scheme. */ + TIFFFieldArray *tif_fieldscompat; + size_t tif_nfieldscompat; + /* Error handler support */ + TIFFErrorHandlerExtR tif_errorhandler; + void *tif_errorhandler_user_data; + TIFFErrorHandlerExtR tif_warnhandler; + void *tif_warnhandler_user_data; + tmsize_t tif_max_single_mem_alloc; /* in bytes. 0 for unlimited */ + tmsize_t tif_max_cumulated_mem_alloc; /* in bytes. 0 for unlimited */ + tmsize_t tif_cur_cumulated_mem_alloc; /* in bytes */ + int tif_warn_about_unknown_tags; +}; + +struct TIFFOpenOptions +{ + TIFFErrorHandlerExtR errorhandler; /* may be NULL */ + void *errorhandler_user_data; /* may be NULL */ + TIFFErrorHandlerExtR warnhandler; /* may be NULL */ + void *warnhandler_user_data; /* may be NULL */ + tmsize_t max_single_mem_alloc; /* in bytes. 0 for unlimited */ + tmsize_t max_cumulated_mem_alloc; /* in bytes. 0 for unlimited */ + int warn_about_unknown_tags; +}; + +#define isPseudoTag(t) (t > 0xffff) /* is tag value normal or pseudo */ + +#define isTiled(tif) (((tif)->tif_flags & TIFF_ISTILED) != 0) +#define isMapped(tif) (((tif)->tif_flags & TIFF_MAPPED) != 0) +#define isFillOrder(tif, o) (((tif)->tif_flags & (o)) != 0) +#define isUpSampled(tif) (((tif)->tif_flags & TIFF_UPSAMPLED) != 0) +#define TIFFReadFile(tif, buf, size) \ + ((*(tif)->tif_readproc)((tif)->tif_clientdata, (buf), (size))) +#define TIFFWriteFile(tif, buf, size) \ + ((*(tif)->tif_writeproc)((tif)->tif_clientdata, (buf), (size))) +#define TIFFSeekFile(tif, off, whence) \ + ((*(tif)->tif_seekproc)((tif)->tif_clientdata, (off), (whence))) +#define TIFFCloseFile(tif) ((*(tif)->tif_closeproc)((tif)->tif_clientdata)) +#define TIFFGetFileSize(tif) ((*(tif)->tif_sizeproc)((tif)->tif_clientdata)) +#define TIFFMapFileContents(tif, paddr, psize) \ + ((*(tif)->tif_mapproc)((tif)->tif_clientdata, (paddr), (psize))) +#define TIFFUnmapFileContents(tif, addr, size) \ + ((*(tif)->tif_unmapproc)((tif)->tif_clientdata, (addr), (size))) + +/* + * Default Read/Seek/Write definitions. + */ +#ifndef ReadOK +#define ReadOK(tif, buf, size) (TIFFReadFile((tif), (buf), (size)) == (size)) +#endif +#ifndef SeekOK +#define SeekOK(tif, off) _TIFFSeekOK(tif, off) +#endif +#ifndef WriteOK +#define WriteOK(tif, buf, size) (TIFFWriteFile((tif), (buf), (size)) == (size)) +#endif + +/* NB: the uint32_t casts are to silence certain ANSI-C compilers */ +#define TIFFhowmany_32(x, y) \ + (((uint32_t)x < (0xffffffff - (uint32_t)(y - 1))) \ + ? ((((uint32_t)(x)) + (((uint32_t)(y)) - 1)) / ((uint32_t)(y))) \ + : 0U) +/* Variant of TIFFhowmany_32() that doesn't return 0 if x close to MAXUINT. */ +/* Caution: TIFFhowmany_32_maxuint_compat(x,y)*y might overflow */ +#define TIFFhowmany_32_maxuint_compat(x, y) \ + (((uint32_t)(x) / (uint32_t)(y)) + \ + ((((uint32_t)(x) % (uint32_t)(y)) != 0) ? 1 : 0)) +#define TIFFhowmany8_32(x) \ + (((x)&0x07) ? ((uint32_t)(x) >> 3) + 1 : (uint32_t)(x) >> 3) +#define TIFFroundup_32(x, y) (TIFFhowmany_32(x, y) * (y)) +#define TIFFhowmany_64(x, y) \ + ((((uint64_t)(x)) + (((uint64_t)(y)) - 1)) / ((uint64_t)(y))) +#define TIFFhowmany8_64(x) \ + (((x)&0x07) ? ((uint64_t)(x) >> 3) + 1 : (uint64_t)(x) >> 3) +#define TIFFroundup_64(x, y) (TIFFhowmany_64(x, y) * (y)) + +/* Safe multiply which returns zero if there is an *unsigned* integer overflow. + * This macro is not safe for *signed* integer types */ +#define TIFFSafeMultiply(t, v, m) \ + ((((t)(m) != (t)0) && (((t)(((v) * (m)) / (m))) == (t)(v))) \ + ? (t)((v) * (m)) \ + : (t)0) + +#define TIFFmax(A, B) ((A) > (B) ? (A) : (B)) +#define TIFFmin(A, B) ((A) < (B) ? (A) : (B)) + +#define TIFFArrayCount(a) (sizeof(a) / sizeof((a)[0])) + +/* + Support for large files. + + Windows read/write APIs support only 'unsigned int' rather than 'size_t'. + Windows off_t is only 32-bit, even in 64-bit builds. +*/ +#if defined(HAVE_FSEEKO) +/* + Use fseeko() and ftello() if they are available since they use + 'off_t' rather than 'long'. It is wrong to use fseeko() and + ftello() only on systems with special LFS support since some systems + (e.g. FreeBSD) support a 64-bit off_t by default. + + For MinGW, __MSVCRT_VERSION__ must be at least 0x800 to expose these + interfaces. The MinGW compiler must support the requested version. MinGW + does not distribute the CRT (it is supplied by Microsoft) so the correct CRT + must be available on the target computer in order for the program to run. +*/ +#if defined(HAVE_FSEEKO) +#define fseek(stream, offset, whence) fseeko(stream, offset, whence) +#define ftell(stream, offset, whence) ftello(stream, offset, whence) +#endif +#endif +#if defined(_WIN32) && \ + !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800) +typedef unsigned int TIFFIOSize_t; +#define _TIFF_lseek_f(fildes, offset, whence) \ + _lseeki64(fildes, /* __int64 */ offset, whence) +/* #define _TIFF_tell_f(fildes) /\* __int64 *\/ _telli64(fildes) */ +#define _TIFF_fseek_f(stream, offset, whence) \ + _fseeki64(stream, /* __int64 */ offset, whence) +#define _TIFF_fstat_f(fildes, stat_buff) \ + _fstati64(fildes, /* struct _stati64 */ stat_buff) +/* #define _TIFF_ftell_f(stream) /\* __int64 *\/ _ftelli64(stream) */ +/* #define _TIFF_stat_f(path,stat_buff) _stati64(path,/\* struct _stati64 *\/ + * stat_buff) */ +#define _TIFF_stat_s struct _stati64 +#define _TIFF_off_t __int64 +#else +typedef size_t TIFFIOSize_t; +#define _TIFF_lseek_f(fildes, offset, whence) lseek(fildes, offset, whence) +/* #define _TIFF_tell_f(fildes) (_TIFF_lseek_f(fildes,0,SEEK_CUR)) */ +#define _TIFF_fseek_f(stream, offset, whence) fseek(stream, offset, whence) +#define _TIFF_fstat_f(fildes, stat_buff) fstat(fildes, stat_buff) +/* #define _TIFF_ftell_f(stream) ftell(stream) */ +/* #define _TIFF_stat_f(path,stat_buff) stat(path,stat_buff) */ +#define _TIFF_stat_s struct stat +#define _TIFF_off_t off_t +#endif + +#if defined(__has_attribute) && defined(__clang__) +#if __has_attribute(no_sanitize) +#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW \ + __attribute__((no_sanitize("unsigned-integer-overflow"))) +#else +#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +#endif +#else +#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW +#endif + +#if defined(__cplusplus) +extern "C" +{ +#endif + extern int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize); + extern void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size); + extern int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, + const char *mode, const char *module); + extern int _TIFFNoRowEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, + uint16_t s); + extern int _TIFFNoStripEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, + uint16_t s); + extern int _TIFFNoTileEncode(TIFF *, uint8_t *pp, tmsize_t cc, uint16_t s); + extern int _TIFFNoRowDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, + uint16_t s); + extern int _TIFFNoStripDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, + uint16_t s); + extern int _TIFFNoTileDecode(TIFF *, uint8_t *pp, tmsize_t cc, uint16_t s); + extern void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc); + extern int _TIFFNoPreCode(TIFF *tif, uint16_t s); + extern int _TIFFNoSeek(TIFF *tif, uint32_t off); + extern void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc); + extern void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc); + extern void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc); + extern void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc); + extern int TIFFFlushData1(TIFF *tif); + extern int TIFFDefaultDirectory(TIFF *tif); + extern void _TIFFSetDefaultCompressionState(TIFF *tif); + extern int _TIFFRewriteField(TIFF *, uint16_t, TIFFDataType, tmsize_t, + void *); + extern int TIFFSetCompressionScheme(TIFF *tif, int scheme); + extern int TIFFSetDefaultCompressionState(TIFF *tif); + extern uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s); + extern void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th); + + extern void _TIFFsetByteArray(void **, const void *, uint32_t); + extern void _TIFFsetByteArrayExt(TIFF *, void **, const void *, uint32_t); + extern void _TIFFsetShortArray(uint16_t **, const uint16_t *, uint32_t); + extern void _TIFFsetShortArrayExt(TIFF *, uint16_t **, const uint16_t *, + uint32_t); + extern void _TIFFsetLongArray(uint32_t **, const uint32_t *, uint32_t); + extern void _TIFFsetLongArrayExt(TIFF *, uint32_t **, const uint32_t *, + uint32_t); + extern void _TIFFsetFloatArray(float **, const float *, uint32_t); + extern void _TIFFsetFloatArrayExt(TIFF *, float **, const float *, + uint32_t); + extern void _TIFFsetDoubleArray(double **, const double *, uint32_t); + extern void _TIFFsetDoubleArrayExt(TIFF *, double **, const double *, + uint32_t); + + extern void _TIFFprintAscii(FILE *, const char *); + extern void _TIFFprintAsciiTag(FILE *, const char *, const char *); + + extern TIFFErrorHandler _TIFFwarningHandler; + extern TIFFErrorHandler _TIFFerrorHandler; + extern TIFFErrorHandlerExt _TIFFwarningHandlerExt; + extern TIFFErrorHandlerExt _TIFFerrorHandlerExt; + void _TIFFErrorEarly(TIFFOpenOptions *opts, thandle_t clientdata, + const char *module, const char *fmt, ...) + TIFF_ATTRIBUTE((__format__(__printf__, 4, 5))); + + extern uint32_t _TIFFMultiply32(TIFF *, uint32_t, uint32_t, const char *); + extern uint64_t _TIFFMultiply64(TIFF *, uint64_t, uint64_t, const char *); + extern tmsize_t _TIFFMultiplySSize(TIFF *, tmsize_t, tmsize_t, + const char *); + extern tmsize_t _TIFFCastUInt64ToSSize(TIFF *, uint64_t, const char *); + extern void *_TIFFCheckMalloc(TIFF *, tmsize_t, tmsize_t, const char *); + extern void *_TIFFCheckRealloc(TIFF *, void *, tmsize_t, tmsize_t, + const char *); + + extern float _TIFFClampDoubleToFloat(double); + extern uint32_t _TIFFClampDoubleToUInt32(double); + + extern void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif); + + extern tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, + uint32_t strip, + void **buf, + tmsize_t bufsizetoalloc, + tmsize_t size_to_read); + extern tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile, + void **buf, + tmsize_t bufsizetoalloc, + tmsize_t size_to_read); + extern tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf, + tmsize_t bufsizetoalloc, + uint32_t x, uint32_t y, + uint32_t z, uint16_t s); + extern int _TIFFSeekOK(TIFF *tif, toff_t off); + + extern int TIFFInitDumpMode(TIFF *, int); +#ifdef PACKBITS_SUPPORT + extern int TIFFInitPackBits(TIFF *, int); +#endif +#ifdef CCITT_SUPPORT + extern int TIFFInitCCITTRLE(TIFF *, int), TIFFInitCCITTRLEW(TIFF *, int); + extern int TIFFInitCCITTFax3(TIFF *, int), TIFFInitCCITTFax4(TIFF *, int); +#endif +#ifdef THUNDER_SUPPORT + extern int TIFFInitThunderScan(TIFF *, int); +#endif +#ifdef NEXT_SUPPORT + extern int TIFFInitNeXT(TIFF *, int); +#endif +#ifdef LZW_SUPPORT + extern int TIFFInitLZW(TIFF *, int); +#endif +#ifdef OJPEG_SUPPORT + extern int TIFFInitOJPEG(TIFF *, int); +#endif +#ifdef JPEG_SUPPORT + extern int TIFFInitJPEG(TIFF *, int); + extern int TIFFJPEGIsFullStripRequired(TIFF *); +#endif +#ifdef JBIG_SUPPORT + extern int TIFFInitJBIG(TIFF *, int); +#endif +#ifdef ZIP_SUPPORT + extern int TIFFInitZIP(TIFF *, int); +#endif +#ifdef PIXARLOG_SUPPORT + extern int TIFFInitPixarLog(TIFF *, int); +#endif +#ifdef LOGLUV_SUPPORT + extern int TIFFInitSGILog(TIFF *, int); +#endif +#ifdef LERC_SUPPORT + extern int TIFFInitLERC(TIFF *tif, int); +#endif +#ifdef LZMA_SUPPORT + extern int TIFFInitLZMA(TIFF *, int); +#endif +#ifdef ZSTD_SUPPORT + extern int TIFFInitZSTD(TIFF *, int); +#endif +#ifdef WEBP_SUPPORT + extern int TIFFInitWebP(TIFF *, int); +#endif + extern const TIFFCodec _TIFFBuiltinCODECS[]; + extern void TIFFCIELab16ToXYZ(TIFFCIELabToRGB *, uint32_t l, int32_t a, + int32_t b, float *, float *, float *); + + extern void *_TIFFmallocExt(TIFF *tif, tmsize_t s); + extern void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz); + extern void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s); + extern void _TIFFfreeExt(TIFF *tif, void *p); + +#if defined(__cplusplus) +} +#endif +#endif /* _TIFFIOP_ */ diff --git a/cpp/3rd_party/libtiff/tiffvers.h b/cpp/3rd_party/libtiff/tiffvers.h new file mode 100644 index 0000000000..0cce798b83 --- /dev/null +++ b/cpp/3rd_party/libtiff/tiffvers.h @@ -0,0 +1,9 @@ +#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.2.0\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc." +/* + * This define can be used in code that requires + * compilation-related definitions specific to a + * version or versions of the library. Runtime + * version checking should be done based on the + * string returned by TIFFGetVersion. + */ +#define TIFFLIB_VERSION 20201219 diff --git a/cpp/3rd_party/libtiff/uvcode.h b/cpp/3rd_party/libtiff/uvcode.h new file mode 100644 index 0000000000..fc87729244 --- /dev/null +++ b/cpp/3rd_party/libtiff/uvcode.h @@ -0,0 +1,93 @@ +/* Version 1.0 generated April 7, 1997 by Greg Ward Larson, SGI */ +#define UV_SQSIZ (float)0.003500 +#define UV_NDIVS 16289 +#define UV_VSTART (float)0.016940 +#define UV_NVS 163 +static const struct +{ + float ustart; + short nus, ncum; +} uv_row[UV_NVS] = { + {(float)0.247663, 4, 0}, {(float)0.243779, 6, 4}, + {(float)0.241684, 7, 10}, {(float)0.237874, 9, 17}, + {(float)0.235906, 10, 26}, {(float)0.232153, 12, 36}, + {(float)0.228352, 14, 48}, {(float)0.226259, 15, 62}, + {(float)0.222371, 17, 77}, {(float)0.220410, 18, 94}, + {(float)0.214710, 21, 112}, {(float)0.212714, 22, 133}, + {(float)0.210721, 23, 155}, {(float)0.204976, 26, 178}, + {(float)0.202986, 27, 204}, {(float)0.199245, 29, 231}, + {(float)0.195525, 31, 260}, {(float)0.193560, 32, 291}, + {(float)0.189878, 34, 323}, {(float)0.186216, 36, 357}, + {(float)0.186216, 36, 393}, {(float)0.182592, 38, 429}, + {(float)0.179003, 40, 467}, {(float)0.175466, 42, 507}, + {(float)0.172001, 44, 549}, {(float)0.172001, 44, 593}, + {(float)0.168612, 46, 637}, {(float)0.168612, 46, 683}, + {(float)0.163575, 49, 729}, {(float)0.158642, 52, 778}, + {(float)0.158642, 52, 830}, {(float)0.158642, 52, 882}, + {(float)0.153815, 55, 934}, {(float)0.153815, 55, 989}, + {(float)0.149097, 58, 1044}, {(float)0.149097, 58, 1102}, + {(float)0.142746, 62, 1160}, {(float)0.142746, 62, 1222}, + {(float)0.142746, 62, 1284}, {(float)0.138270, 65, 1346}, + {(float)0.138270, 65, 1411}, {(float)0.138270, 65, 1476}, + {(float)0.132166, 69, 1541}, {(float)0.132166, 69, 1610}, + {(float)0.126204, 73, 1679}, {(float)0.126204, 73, 1752}, + {(float)0.126204, 73, 1825}, {(float)0.120381, 77, 1898}, + {(float)0.120381, 77, 1975}, {(float)0.120381, 77, 2052}, + {(float)0.120381, 77, 2129}, {(float)0.112962, 82, 2206}, + {(float)0.112962, 82, 2288}, {(float)0.112962, 82, 2370}, + {(float)0.107450, 86, 2452}, {(float)0.107450, 86, 2538}, + {(float)0.107450, 86, 2624}, {(float)0.107450, 86, 2710}, + {(float)0.100343, 91, 2796}, {(float)0.100343, 91, 2887}, + {(float)0.100343, 91, 2978}, {(float)0.095126, 95, 3069}, + {(float)0.095126, 95, 3164}, {(float)0.095126, 95, 3259}, + {(float)0.095126, 95, 3354}, {(float)0.088276, 100, 3449}, + {(float)0.088276, 100, 3549}, {(float)0.088276, 100, 3649}, + {(float)0.088276, 100, 3749}, {(float)0.081523, 105, 3849}, + {(float)0.081523, 105, 3954}, {(float)0.081523, 105, 4059}, + {(float)0.081523, 105, 4164}, {(float)0.074861, 110, 4269}, + {(float)0.074861, 110, 4379}, {(float)0.074861, 110, 4489}, + {(float)0.074861, 110, 4599}, {(float)0.068290, 115, 4709}, + {(float)0.068290, 115, 4824}, {(float)0.068290, 115, 4939}, + {(float)0.068290, 115, 5054}, {(float)0.063573, 119, 5169}, + {(float)0.063573, 119, 5288}, {(float)0.063573, 119, 5407}, + {(float)0.063573, 119, 5526}, {(float)0.057219, 124, 5645}, + {(float)0.057219, 124, 5769}, {(float)0.057219, 124, 5893}, + {(float)0.057219, 124, 6017}, {(float)0.050985, 129, 6141}, + {(float)0.050985, 129, 6270}, {(float)0.050985, 129, 6399}, + {(float)0.050985, 129, 6528}, {(float)0.050985, 129, 6657}, + {(float)0.044859, 134, 6786}, {(float)0.044859, 134, 6920}, + {(float)0.044859, 134, 7054}, {(float)0.044859, 134, 7188}, + {(float)0.040571, 138, 7322}, {(float)0.040571, 138, 7460}, + {(float)0.040571, 138, 7598}, {(float)0.040571, 138, 7736}, + {(float)0.036339, 142, 7874}, {(float)0.036339, 142, 8016}, + {(float)0.036339, 142, 8158}, {(float)0.036339, 142, 8300}, + {(float)0.032139, 146, 8442}, {(float)0.032139, 146, 8588}, + {(float)0.032139, 146, 8734}, {(float)0.032139, 146, 8880}, + {(float)0.027947, 150, 9026}, {(float)0.027947, 150, 9176}, + {(float)0.027947, 150, 9326}, {(float)0.023739, 154, 9476}, + {(float)0.023739, 154, 9630}, {(float)0.023739, 154, 9784}, + {(float)0.023739, 154, 9938}, {(float)0.019504, 158, 10092}, + {(float)0.019504, 158, 10250}, {(float)0.019504, 158, 10408}, + {(float)0.016976, 161, 10566}, {(float)0.016976, 161, 10727}, + {(float)0.016976, 161, 10888}, {(float)0.016976, 161, 11049}, + {(float)0.012639, 165, 11210}, {(float)0.012639, 165, 11375}, + {(float)0.012639, 165, 11540}, {(float)0.009991, 168, 11705}, + {(float)0.009991, 168, 11873}, {(float)0.009991, 168, 12041}, + {(float)0.009016, 170, 12209}, {(float)0.009016, 170, 12379}, + {(float)0.009016, 170, 12549}, {(float)0.006217, 173, 12719}, + {(float)0.006217, 173, 12892}, {(float)0.005097, 175, 13065}, + {(float)0.005097, 175, 13240}, {(float)0.005097, 175, 13415}, + {(float)0.003909, 177, 13590}, {(float)0.003909, 177, 13767}, + {(float)0.002340, 177, 13944}, {(float)0.002389, 170, 14121}, + {(float)0.001068, 164, 14291}, {(float)0.001653, 157, 14455}, + {(float)0.000717, 150, 14612}, {(float)0.001614, 143, 14762}, + {(float)0.000270, 136, 14905}, {(float)0.000484, 129, 15041}, + {(float)0.001103, 123, 15170}, {(float)0.001242, 115, 15293}, + {(float)0.001188, 109, 15408}, {(float)0.001011, 103, 15517}, + {(float)0.000709, 97, 15620}, {(float)0.000301, 89, 15717}, + {(float)0.002416, 82, 15806}, {(float)0.003251, 76, 15888}, + {(float)0.003246, 69, 15964}, {(float)0.004141, 62, 16033}, + {(float)0.005963, 55, 16095}, {(float)0.008839, 47, 16150}, + {(float)0.010490, 40, 16197}, {(float)0.016994, 31, 16237}, + {(float)0.023659, 21, 16268}, +}; diff --git a/cpp/3rd_party/npy/npy.hpp b/cpp/3rd_party/npy/npy.hpp new file mode 100644 index 0000000000..94db74c923 --- /dev/null +++ b/cpp/3rd_party/npy/npy.hpp @@ -0,0 +1,557 @@ +/* + Copyright 2017 Leon Merten Lohse + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#ifndef NPY_HPP_ +#define NPY_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace npy { + +/* Compile-time test for byte order. + If your compiler does not define these per default, you may want to define + one of these constants manually. + Defaults to little endian order. */ +#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \ + defined(__BIG_ENDIAN__) || \ + defined(__ARMEB__) || \ + defined(__THUMBEB__) || \ + defined(__AARCH64EB__) || \ + defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) +const bool big_endian = true; +#else +const bool big_endian = false; +#endif + + +const char magic_string[] = "\x93NUMPY"; +const size_t magic_string_length = 6; + +const char little_endian_char = '<'; +const char big_endian_char = '>'; +const char no_endian_char = '|'; + +constexpr std::array +endian_chars = {little_endian_char, big_endian_char, no_endian_char}; +constexpr std::array +numtype_chars = {'f', 'i', 'u', 'c'}; + +constexpr char host_endian_char = (big_endian ? + big_endian_char : + little_endian_char); + +/* npy array length */ +typedef unsigned long int ndarray_len_t; + +typedef std::pair version_t; + +struct dtype_t { + const char byteorder; + const char kind; + const unsigned int itemsize; + +// TODO(llohse): implement as constexpr + inline std::string str() const { + const size_t max_buflen = 16; + char buf[max_buflen]; + std::snprintf(buf, max_buflen, "%c%c%u", byteorder, kind, itemsize); + return std::string(buf); + } + + inline std::tuple tie() const { + return std::tie(byteorder, kind, itemsize); + } +}; + + +struct header_t { + const dtype_t dtype; + const bool fortran_order; + const std::vector shape; +}; + +inline void write_magic(std::ostream &ostream, version_t version) { + ostream.write(magic_string, magic_string_length); + ostream.put(version.first); + ostream.put(version.second); +} + +inline version_t read_magic(std::istream &istream) { + char buf[magic_string_length + 2]; + istream.read(buf, magic_string_length + 2); + + if (!istream) { + throw std::runtime_error("io error: failed reading file"); + } + + if (0 != std::memcmp(buf, magic_string, magic_string_length)) + throw std::runtime_error("this file does not have a valid npy format."); + + version_t version; + version.first = buf[magic_string_length]; + version.second = buf[magic_string_length + 1]; + + return version; +} + +const std::unordered_map dtype_map = { + {std::type_index(typeid(float)), {host_endian_char, 'f', sizeof(float)}}, + {std::type_index(typeid(double)), {host_endian_char, 'f', sizeof(double)}}, + {std::type_index(typeid(long double)), {host_endian_char, 'f', sizeof(long double)}}, + {std::type_index(typeid(char)), {no_endian_char, 'i', sizeof(char)}}, + {std::type_index(typeid(signed char)), {no_endian_char, 'i', sizeof(signed char)}}, + {std::type_index(typeid(short)), {host_endian_char, 'i', sizeof(short)}}, + {std::type_index(typeid(int)), {host_endian_char, 'i', sizeof(int)}}, + {std::type_index(typeid(long)), {host_endian_char, 'i', sizeof(long)}}, + {std::type_index(typeid(long long)), {host_endian_char, 'i', sizeof(long long)}}, + {std::type_index(typeid(unsigned char)), {no_endian_char, 'u', sizeof(unsigned char)}}, + {std::type_index(typeid(unsigned short)), {host_endian_char, 'u', sizeof(unsigned short)}}, + {std::type_index(typeid(unsigned int)), {host_endian_char, 'u', sizeof(unsigned int)}}, + {std::type_index(typeid(unsigned long)), {host_endian_char, 'u', sizeof(unsigned long)}}, + {std::type_index(typeid(unsigned long long)), {host_endian_char, 'u', sizeof(unsigned long long)}}, + {std::type_index(typeid(std::complex)), {host_endian_char, 'c', sizeof(std::complex)}}, + {std::type_index(typeid(std::complex)), {host_endian_char, 'c', sizeof(std::complex)}}, + {std::type_index(typeid(std::complex)), {host_endian_char, 'c', sizeof(std::complex)}} +}; + + +// helpers +inline bool is_digits(const std::string &str) { + return std::all_of(str.begin(), str.end(), ::isdigit); +} + +template +inline bool in_array(T val, const std::array &arr) { + return std::find(std::begin(arr), std::end(arr), val) != std::end(arr); +} + +inline dtype_t parse_descr(std::string typestring) { + if (typestring.length() < 3) { + throw std::runtime_error("invalid typestring (length)"); + } + + char byteorder_c = typestring.at(0); + char kind_c = typestring.at(1); + std::string itemsize_s = typestring.substr(2); + + if (!in_array(byteorder_c, endian_chars)) { + throw std::runtime_error("invalid typestring (byteorder)"); + } + + if (!in_array(kind_c, numtype_chars)) { + throw std::runtime_error("invalid typestring (kind)"); + } + + if (!is_digits(itemsize_s)) { + throw std::runtime_error("invalid typestring (itemsize)"); + } + unsigned int itemsize = std::stoul(itemsize_s); + + return {byteorder_c, kind_c, itemsize}; +} + +namespace pyparse { + +/** + Removes leading and trailing whitespaces + */ +inline std::string trim(const std::string &str) { + const std::string whitespace = " \t"; + auto begin = str.find_first_not_of(whitespace); + + if (begin == std::string::npos) + return ""; + + auto end = str.find_last_not_of(whitespace); + + return str.substr(begin, end - begin + 1); +} + + +inline std::string get_value_from_map(const std::string &mapstr) { + size_t sep_pos = mapstr.find_first_of(":"); + if (sep_pos == std::string::npos) + return ""; + + std::string tmp = mapstr.substr(sep_pos + 1); + return trim(tmp); +} + +/** + Parses the string representation of a Python dict + + The keys need to be known and may not appear anywhere else in the data. + */ +inline std::unordered_map parse_dict(std::string in, const std::vector &keys) { + std::unordered_map map; + + if (keys.size() == 0) + return map; + + in = trim(in); + + // unwrap dictionary + if ((in.front() == '{') && (in.back() == '}')) + in = in.substr(1, in.length() - 2); + else + throw std::runtime_error("Not a Python dictionary."); + + std::vector > positions; + + for (auto const &value : keys) { + size_t pos = in.find("'" + value + "'"); + + if (pos == std::string::npos) + throw std::runtime_error("Missing '" + value + "' key."); + + std::pair position_pair{pos, value}; + positions.push_back(position_pair); + } + + // sort by position in dict + std::sort(positions.begin(), positions.end()); + + for (size_t i = 0; i < positions.size(); ++i) { + std::string raw_value; + size_t begin{positions[i].first}; + size_t end{std::string::npos}; + + std::string key = positions[i].second; + + if (i + 1 < positions.size()) + end = positions[i + 1].first; + + raw_value = in.substr(begin, end - begin); + + raw_value = trim(raw_value); + + if (raw_value.back() == ',') + raw_value.pop_back(); + + map[key] = get_value_from_map(raw_value); + } + + return map; +} + +/** + Parses the string representation of a Python boolean + */ +inline bool parse_bool(const std::string &in) { + if (in == "True") + return true; + if (in == "False") + return false; + + throw std::runtime_error("Invalid python boolan."); +} + +/** + Parses the string representation of a Python str + */ +inline std::string parse_str(const std::string &in) { + if ((in.front() == '\'') && (in.back() == '\'')) + return in.substr(1, in.length() - 2); + + throw std::runtime_error("Invalid python string."); +} + +/** + Parses the string represenatation of a Python tuple into a vector of its items + */ +inline std::vector parse_tuple(std::string in) { + std::vector v; + const char seperator = ','; + + in = trim(in); + + if ((in.front() == '(') && (in.back() == ')')) + in = in.substr(1, in.length() - 2); + else + throw std::runtime_error("Invalid Python tuple."); + + std::istringstream iss(in); + + for (std::string token; std::getline(iss, token, seperator);) { + v.push_back(token); + } + + return v; +} + +template +inline std::string write_tuple(const std::vector &v) { + if (v.size() == 0) + return "()"; + + std::ostringstream ss; + + if (v.size() == 1) { + ss << "(" << v.front() << ",)"; + } else { + const std::string delimiter = ", "; + // v.size() > 1 + ss << "("; + std::copy(v.begin(), v.end() - 1, std::ostream_iterator(ss, delimiter.c_str())); + ss << v.back(); + ss << ")"; + } + + return ss.str(); +} + +inline std::string write_boolean(bool b) { + if (b) + return "True"; + else + return "False"; +} + +} // namespace pyparse + + +inline header_t parse_header(std::string header) { + /* + The first 6 bytes are a magic string: exactly "x93NUMPY". + The next 1 byte is an unsigned byte: the major version number of the file format, e.g. x01. + The next 1 byte is an unsigned byte: the minor version number of the file format, e.g. x00. Note: the version of the file format is not tied to the version of the numpy package. + The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN. + The next HEADER_LEN bytes form the header data describing the array's format. It is an ASCII string which contains a Python literal expression of a dictionary. It is terminated by a newline ('n') and padded with spaces ('x20') to make the total length of the magic string + 4 + HEADER_LEN be evenly divisible by 16 for alignment purposes. + The dictionary contains three keys: + + "descr" : dtype.descr + An object that can be passed as an argument to the numpy.dtype() constructor to create the array's dtype. + "fortran_order" : bool + Whether the array data is Fortran-contiguous or not. Since Fortran-contiguous arrays are a common form of non-C-contiguity, we allow them to be written directly to disk for efficiency. + "shape" : tuple of int + The shape of the array. + For repeatability and readability, this dictionary is formatted using pprint.pformat() so the keys are in alphabetic order. + */ + + // remove trailing newline + if (header.back() != '\n') + throw std::runtime_error("invalid header"); + header.pop_back(); + + // parse the dictionary + std::vector keys{"descr", "fortran_order", "shape"}; + auto dict_map = npy::pyparse::parse_dict(header, keys); + + if (dict_map.size() == 0) + throw std::runtime_error("invalid dictionary in header"); + + std::string descr_s = dict_map["descr"]; + std::string fortran_s = dict_map["fortran_order"]; + std::string shape_s = dict_map["shape"]; + + std::string descr = npy::pyparse::parse_str(descr_s); + dtype_t dtype = parse_descr(descr); + + // convert literal Python bool to C++ bool + bool fortran_order = npy::pyparse::parse_bool(fortran_s); + + // parse the shape tuple + auto shape_v = npy::pyparse::parse_tuple(shape_s); + + std::vector shape; + for (auto item : shape_v) { + ndarray_len_t dim = static_cast(std::stoul(item)); + shape.push_back(dim); + } + + return {dtype, fortran_order, shape}; +} + + +inline std::string +write_header_dict(const std::string &descr, bool fortran_order, const std::vector &shape) { + std::string s_fortran_order = npy::pyparse::write_boolean(fortran_order); + std::string shape_s = npy::pyparse::write_tuple(shape); + + return "{'descr': '" + descr + "', 'fortran_order': " + s_fortran_order + ", 'shape': " + shape_s + ", }"; +} + +inline void write_header(std::ostream &out, const header_t &header) { + std::string header_dict = write_header_dict(header.dtype.str(), header.fortran_order, header.shape); + + size_t length = magic_string_length + 2 + 2 + header_dict.length() + 1; + + version_t version{1, 0}; + if (length >= 255 * 255) { + length = magic_string_length + 2 + 4 + header_dict.length() + 1; + version = {2, 0}; + } + size_t padding_len = 16 - length % 16; + std::string padding(padding_len, ' '); + + // write magic + write_magic(out, version); + + // write header length + if (version == version_t{1, 0}) { + uint8_t header_len_le16[2]; + uint16_t header_len = static_cast(header_dict.length() + padding.length() + 1); + + header_len_le16[0] = (header_len >> 0) & 0xff; + header_len_le16[1] = (header_len >> 8) & 0xff; + out.write(reinterpret_cast(header_len_le16), 2); + } else { + uint8_t header_len_le32[4]; + uint32_t header_len = static_cast(header_dict.length() + padding.length() + 1); + + header_len_le32[0] = (header_len >> 0) & 0xff; + header_len_le32[1] = (header_len >> 8) & 0xff; + header_len_le32[2] = (header_len >> 16) & 0xff; + header_len_le32[3] = (header_len >> 24) & 0xff; + out.write(reinterpret_cast(header_len_le32), 4); + } + + out << header_dict << padding << '\n'; +} + +inline std::string read_header(std::istream &istream) { + // check magic bytes an version number + version_t version = read_magic(istream); + + uint32_t header_length; + if (version == version_t{1, 0}) { + uint8_t header_len_le16[2]; + istream.read(reinterpret_cast(header_len_le16), 2); + header_length = (header_len_le16[0] << 0) | (header_len_le16[1] << 8); + + if ((magic_string_length + 2 + 2 + header_length) % 16 != 0) { + // TODO(llohse): display warning + } + } else if (version == version_t{2, 0}) { + uint8_t header_len_le32[4]; + istream.read(reinterpret_cast(header_len_le32), 4); + + header_length = (header_len_le32[0] << 0) | (header_len_le32[1] << 8) + | (header_len_le32[2] << 16) | (header_len_le32[3] << 24); + + if ((magic_string_length + 2 + 4 + header_length) % 16 != 0) { + // TODO(llohse): display warning + } + } else { + throw std::runtime_error("unsupported file format version"); + } + + auto buf_v = std::vector(header_length); + istream.read(buf_v.data(), header_length); + std::string header(buf_v.data(), header_length); + + return header; +} + +inline ndarray_len_t comp_size(const std::vector &shape) { + ndarray_len_t size = 1; + for (ndarray_len_t i : shape) + size *= i; + + return size; +} + +template +inline void +SaveArrayAsNumpy(std::ostream& stream, bool fortran_order, unsigned int n_dims, const unsigned long shape[], + const Scalar* data) { +// static_assert(has_typestring::value, "scalar type not understood"); + const dtype_t dtype = dtype_map.at(std::type_index(typeid(Scalar))); + + if (!stream) { + throw std::runtime_error("io error: failed to open a file."); + } + + std::vector shape_v(shape, shape + n_dims); + header_t header{dtype, fortran_order, shape_v}; + write_header(stream, header); + + auto size = static_cast(comp_size(shape_v)); + + stream.write(reinterpret_cast(data), sizeof(Scalar) * size); +} + +template +inline void +SaveArrayAsNumpy(std::ostream& stream, bool fortran_order, unsigned int n_dims, const unsigned long shape[], + const std::vector &data) { + SaveArrayAsNumpy(stream, fortran_order, n_dims, shape, data.data()); +} + +template +inline void LoadArrayFromNumpy(std::istream& stream, std::vector &shape, bool &fortran_order, + std::vector &data) { + if (!stream) { + throw std::runtime_error("io error: failed to open a file."); + } + + std::string header_s = read_header(stream); + + // parse header + header_t header = parse_header(header_s); + + // check if the typestring matches the given one +// static_assert(has_typestring::value, "scalar type not understood"); + const dtype_t dtype = dtype_map.at(std::type_index(typeid(Scalar))); + + if (header.dtype.tie() != dtype.tie()) { + throw std::runtime_error("formatting error: typestrings not matching"); + } + + shape = header.shape; + fortran_order = header.fortran_order; + + // compute the data size based on the shape + auto size = static_cast(comp_size(shape)); + data.resize(size); + + // read the data + stream.read(reinterpret_cast(data.data()), sizeof(Scalar) * size); +} + +template +inline void +LoadArrayFromNumpy(std::istream& stream, std::vector &shape, std::vector &data) { + bool fortran_order; + LoadArrayFromNumpy(stream, shape, fortran_order, data); +} + +} // namespace npy + +#endif // NPY_HPP_ diff --git a/cpp/3rd_party/openjpeg/AUTHORS.md b/cpp/3rd_party/openjpeg/AUTHORS.md new file mode 100644 index 0000000000..35aa84b55e --- /dev/null +++ b/cpp/3rd_party/openjpeg/AUTHORS.md @@ -0,0 +1,21 @@ +# Authors of OpenJPEG +See also [THANKS](https://github.com/uclouvain/openjpeg/blob/master/THANKS.md) + +David Janssens designed and implemented the first version of OpenJPEG. + +Kaori Hagihara designed and implemented the first version of OpenJPIP. + +Jerome Fimes implemented the alpha version of OpenJPEG 2.0. + +Giuseppe Baruffa added the JPWL functionalities. + +Mickaël Savinaud implemented the final OpenJPEG 2.0 version based on a big merge between 1.5 version and alpha version of 2.0. + +Mathieu Malaterre participated to the OpenJPEG 2.0 version and improved the libraries and utilities. + +Yannick Verschueren, +Herve Drolon, +Francois-Olivier Devaux, +Antonin Descampe + improved the libraries and utilities. + diff --git a/cpp/3rd_party/openjpeg/CHANGELOG.md b/cpp/3rd_party/openjpeg/CHANGELOG.md new file mode 100644 index 0000000000..4187b06730 --- /dev/null +++ b/cpp/3rd_party/openjpeg/CHANGELOG.md @@ -0,0 +1,770 @@ +# Changelog + +## [v2.4.0](https://github.com/uclouvain/openjpeg/releases/v2.4.0) (2020-12-28) + +[Full Changelog](https://github.com/uclouvain/openjpeg/compare/v2.3.1...v2.4.0) + +**Closed issues:** + +- OPENJPEG\_INSTALL\_DOC\_DIR does not control a destination directory where HTML docs would be installed. [\#1309](https://github.com/uclouvain/openjpeg/issues/1309) +- Heap-buffer-overflow in lib/openjp2/pi.c:312 [\#1302](https://github.com/uclouvain/openjpeg/issues/1302) +- Heap-buffer-overflow in lib/openjp2/t2.c:973 [\#1299](https://github.com/uclouvain/openjpeg/issues/1299) +- Heap-buffer-overflow in lib/openjp2/pi.c:623 [\#1293](https://github.com/uclouvain/openjpeg/issues/1293) +- Global-buffer-overflow in lib/openjp2/dwt.c:1980 [\#1286](https://github.com/uclouvain/openjpeg/issues/1286) +- Heap-buffer-overflow in lib/openjp2/tcd.c:2417 [\#1284](https://github.com/uclouvain/openjpeg/issues/1284) +- Heap-buffer-overflow in lib/openjp2/mqc.c:499 [\#1283](https://github.com/uclouvain/openjpeg/issues/1283) +- Openjpeg could not encode 32bit RGB float image [\#1281](https://github.com/uclouvain/openjpeg/issues/1281) +- Openjpeg could not encode 32bit RGB float image [\#1280](https://github.com/uclouvain/openjpeg/issues/1280) +- ISO/IEC 15444-1:2019 \(E\) compared with 'cio.h' [\#1277](https://github.com/uclouvain/openjpeg/issues/1277) +- Test-suite failure due to hash mismatch [\#1264](https://github.com/uclouvain/openjpeg/issues/1264) +- Heap use-after-free [\#1261](https://github.com/uclouvain/openjpeg/issues/1261) +- Memory leak when failing to allocate object... [\#1259](https://github.com/uclouvain/openjpeg/issues/1259) +- Memory leak of Tier 1 handle when OpenJPEG fails to set it as TLS... [\#1257](https://github.com/uclouvain/openjpeg/issues/1257) +- Any plan to build release for CVE-2020-8112/CVE-2020-6851 [\#1247](https://github.com/uclouvain/openjpeg/issues/1247) +- failing to convert 16-bit file: opj\_t2\_encode\_packet\(\): only 5251 bytes remaining in output buffer. 5621 needed. [\#1243](https://github.com/uclouvain/openjpeg/issues/1243) +- CMake+VS2017 Compile OK, thirdparty Compile OK, but thirdparty not install [\#1239](https://github.com/uclouvain/openjpeg/issues/1239) +- New release to solve CVE-2019-6988 ? [\#1238](https://github.com/uclouvain/openjpeg/issues/1238) +- Many tests fail to pass after the update of libtiff to version 4.1.0 [\#1233](https://github.com/uclouvain/openjpeg/issues/1233) +- Another heap buffer overflow in libopenjp2 [\#1231](https://github.com/uclouvain/openjpeg/issues/1231) +- Heap buffer overflow in libopenjp2 [\#1228](https://github.com/uclouvain/openjpeg/issues/1228) +- Endianness of binary volume \(JP3D\) [\#1224](https://github.com/uclouvain/openjpeg/issues/1224) +- New release to resolve CVE-2019-12973 [\#1222](https://github.com/uclouvain/openjpeg/issues/1222) +- how to set the block size,like 128,256 ? [\#1216](https://github.com/uclouvain/openjpeg/issues/1216) +- compress YUV files to motion jpeg2000 standard [\#1213](https://github.com/uclouvain/openjpeg/issues/1213) +- Repair/update Java wrapper, and include in release [\#1208](https://github.com/uclouvain/openjpeg/issues/1208) +- abc [\#1206](https://github.com/uclouvain/openjpeg/issues/1206) +- Slow decoding [\#1202](https://github.com/uclouvain/openjpeg/issues/1202) +- Installation question [\#1201](https://github.com/uclouvain/openjpeg/issues/1201) +- Typo in test\_decode\_area - \*ptilew is assigned instead of \*ptileh [\#1195](https://github.com/uclouvain/openjpeg/issues/1195) +- Creating a J2K file with one POC is broken [\#1191](https://github.com/uclouvain/openjpeg/issues/1191) +- Make fails on Arch Linux [\#1174](https://github.com/uclouvain/openjpeg/issues/1174) +- Heap buffer overflow in opj\_t1\_clbl\_decode\_processor\(\) triggered with Ghostscript [\#1158](https://github.com/uclouvain/openjpeg/issues/1158) +- opj\_stream\_get\_number\_byte\_left: Assertion `p\_stream-\>m\_byte\_offset \>= 0' failed. [\#1151](https://github.com/uclouvain/openjpeg/issues/1151) +- The fuzzer ignores too many inputs [\#1079](https://github.com/uclouvain/openjpeg/issues/1079) +- out of bounds read [\#1068](https://github.com/uclouvain/openjpeg/issues/1068) + +**Merged pull requests:** + +- Change defined WIN32 [\#1310](https://github.com/uclouvain/openjpeg/pull/1310) ([Jamaika1](https://github.com/Jamaika1)) +- docs: fix simple typo, producted -\> produced [\#1308](https://github.com/uclouvain/openjpeg/pull/1308) ([timgates42](https://github.com/timgates42)) +- Set ${OPENJPEG\_INSTALL\_DOC\_DIR} to DESTINATION of HTMLs [\#1307](https://github.com/uclouvain/openjpeg/pull/1307) ([lemniscati](https://github.com/lemniscati)) +- Use INC\_DIR for OPENJPEG\_INCLUDE\_DIRS \(fixes uclouvain\#1174\) [\#1306](https://github.com/uclouvain/openjpeg/pull/1306) ([matthew-sharp](https://github.com/matthew-sharp)) +- pi.c: avoid out of bounds access with POC \(fixes \#1302\) [\#1304](https://github.com/uclouvain/openjpeg/pull/1304) ([rouault](https://github.com/rouault)) +- Encoder: grow again buffer size [\#1303](https://github.com/uclouvain/openjpeg/pull/1303) ([zodf0055980](https://github.com/zodf0055980)) +- opj\_j2k\_write\_sod\(\): avoid potential heap buffer overflow \(fixes \#1299\) \(probably master only\) [\#1301](https://github.com/uclouvain/openjpeg/pull/1301) ([rouault](https://github.com/rouault)) +- pi.c: avoid out of bounds access with POC \(refs https://github.com/uclouvain/openjpeg/issues/1293\#issuecomment-737122836\) [\#1300](https://github.com/uclouvain/openjpeg/pull/1300) ([rouault](https://github.com/rouault)) +- opj\_t2\_encode\_packet\(\): avoid out of bound access of \#1297, but likely not the proper fix [\#1298](https://github.com/uclouvain/openjpeg/pull/1298) ([rouault](https://github.com/rouault)) +- opj\_t2\_encode\_packet\(\): avoid out of bound access of \#1294, but likely not the proper fix [\#1296](https://github.com/uclouvain/openjpeg/pull/1296) ([rouault](https://github.com/rouault)) +- opj\_j2k\_setup\_encoder\(\): validate POC compno0 and compno1 \(fixes \#1293\) [\#1295](https://github.com/uclouvain/openjpeg/pull/1295) ([rouault](https://github.com/rouault)) +- Encoder: avoid global buffer overflow on irreversible conversion when… [\#1292](https://github.com/uclouvain/openjpeg/pull/1292) ([rouault](https://github.com/rouault)) +- Decoding: deal with some SPOT6 images that have tiles with a single tile-part with TPsot == 0 and TNsot == 0, and with missing EOC [\#1291](https://github.com/uclouvain/openjpeg/pull/1291) ([rouault](https://github.com/rouault)) +- Free p\_tcd\_marker\_info to avoid memory leak [\#1288](https://github.com/uclouvain/openjpeg/pull/1288) ([zodf0055980](https://github.com/zodf0055980)) +- Encoder: grow again buffer size [\#1287](https://github.com/uclouvain/openjpeg/pull/1287) ([zodf0055980](https://github.com/zodf0055980)) +- Encoder: avoid uint32 overflow when allocating memory for codestream buffer \(fixes \#1243\) [\#1276](https://github.com/uclouvain/openjpeg/pull/1276) ([rouault](https://github.com/rouault)) +- Java compatibility from 1.5 to 1.6 [\#1263](https://github.com/uclouvain/openjpeg/pull/1263) ([jiapei100](https://github.com/jiapei100)) +- opj\_decompress: fix double-free on input directory with mix of valid and invalid images [\#1262](https://github.com/uclouvain/openjpeg/pull/1262) ([rouault](https://github.com/rouault)) +- openjp2: Plug image leak when failing to allocate codestream index. [\#1260](https://github.com/uclouvain/openjpeg/pull/1260) ([sebras](https://github.com/sebras)) +- openjp2: Plug memory leak when setting data as TLS fails. [\#1258](https://github.com/uclouvain/openjpeg/pull/1258) ([sebras](https://github.com/sebras)) +- openjp2: Error out if failing to create Tier 1 handle. [\#1256](https://github.com/uclouvain/openjpeg/pull/1256) ([sebras](https://github.com/sebras)) +- Testing for invalid values of width, height, numcomps [\#1254](https://github.com/uclouvain/openjpeg/pull/1254) ([szukw000](https://github.com/szukw000)) +- Single-threaded performance improvements in forward DWT for 5-3 and 9-7 \(and other improvements\) [\#1253](https://github.com/uclouvain/openjpeg/pull/1253) ([rouault](https://github.com/rouault)) +- Add support for multithreading in encoder [\#1248](https://github.com/uclouvain/openjpeg/pull/1248) ([rouault](https://github.com/rouault)) +- Add support for generation of PLT markers in encoder [\#1246](https://github.com/uclouvain/openjpeg/pull/1246) ([rouault](https://github.com/rouault)) +- Fix warnings about signed/unsigned casts in pi.c [\#1244](https://github.com/uclouvain/openjpeg/pull/1244) ([rouault](https://github.com/rouault)) +- opj\_decompress: add sanity checks to avoid segfault in case of decoding error [\#1240](https://github.com/uclouvain/openjpeg/pull/1240) ([rouault](https://github.com/rouault)) +- ignore wrong icc [\#1236](https://github.com/uclouvain/openjpeg/pull/1236) ([szukw000](https://github.com/szukw000)) +- Implement writing of IMF profiles [\#1235](https://github.com/uclouvain/openjpeg/pull/1235) ([rouault](https://github.com/rouault)) +- tests: add alternate checksums for libtiff 4.1 [\#1234](https://github.com/uclouvain/openjpeg/pull/1234) ([rouault](https://github.com/rouault)) +- opj\_tcd\_init\_tile\(\): avoid integer overflow [\#1232](https://github.com/uclouvain/openjpeg/pull/1232) ([rouault](https://github.com/rouault)) +- tests/fuzzers: link fuzz binaries using $LIB\_FUZZING\_ENGINE. [\#1230](https://github.com/uclouvain/openjpeg/pull/1230) ([Dor1s](https://github.com/Dor1s)) +- opj\_j2k\_update\_image\_dimensions\(\): reject images whose coordinates are beyond INT\_MAX \(fixes \#1228\) [\#1229](https://github.com/uclouvain/openjpeg/pull/1229) ([rouault](https://github.com/rouault)) +- Fix resource leaks [\#1226](https://github.com/uclouvain/openjpeg/pull/1226) ([dodys](https://github.com/dodys)) +- abi-check.sh: fix false postive ABI error, and display output error log [\#1218](https://github.com/uclouvain/openjpeg/pull/1218) ([rouault](https://github.com/rouault)) +- pi.c: avoid integer overflow, resulting in later invalid access to memory in opj\_t2\_decode\_packets\(\) [\#1217](https://github.com/uclouvain/openjpeg/pull/1217) ([rouault](https://github.com/rouault)) +- Add check to validate SGcod/SPcoc/SPcod parameter values. [\#1211](https://github.com/uclouvain/openjpeg/pull/1211) ([sebras](https://github.com/sebras)) +- Fix buffer overflow reading an image file less than four characters [\#1196](https://github.com/uclouvain/openjpeg/pull/1196) ([robert-ancell](https://github.com/robert-ancell)) +- compression: emit POC marker when only one single POC is requested \(f… [\#1192](https://github.com/uclouvain/openjpeg/pull/1192) ([rouault](https://github.com/rouault)) +- Fix several potential vulnerabilities [\#1185](https://github.com/uclouvain/openjpeg/pull/1185) ([Young-X](https://github.com/Young-X)) +- openjp2/j2k: Report error if all wanted components are not decoded. [\#1164](https://github.com/uclouvain/openjpeg/pull/1164) ([sebras](https://github.com/sebras)) + +## [v2.3.1](https://github.com/uclouvain/openjpeg/releases/v2.3.1) (2019-04-02) +[Full Changelog](https://github.com/uclouvain/openjpeg/compare/v2.3.0...v2.3.1) + +**Closed issues:** + +- v2.2.0 regression for decoding images where TNsot == 0 [\#1120](https://github.com/uclouvain/openjpeg/issues/1120) +- Int overflow in jp3d [\#1162](https://github.com/uclouvain/openjpeg/issues/1162) +- Heap buffer overflow in opj\_j2k\_update\_image\_data\(\) triggered with Ghostscript [\#1157](https://github.com/uclouvain/openjpeg/issues/1157) +- LINUX install doesn't work when building shared libraries is disabled [\#1155](https://github.com/uclouvain/openjpeg/issues/1155) +- OPENJPEG null ptr dereference in openjpeg-2.3.0/src/bin/jp2/convert.c:2243 [\#1152](https://github.com/uclouvain/openjpeg/issues/1152) +- How to drop certain subbands/layers in DWT [\#1147](https://github.com/uclouvain/openjpeg/issues/1147) +- where is the MQ-Coder ouput stream in t2.c? [\#1146](https://github.com/uclouvain/openjpeg/issues/1146) +- OpenJPEG 2.3 \(and 2.2?\) multi component image fails to decode with KDU v7.10 [\#1132](https://github.com/uclouvain/openjpeg/issues/1132) +- Missing checks for header\_info.height and header\_info.width in function pnmtoimage in src/bin/jpwl/convert.c, which can lead to heap buffer overflow [\#1126](https://github.com/uclouvain/openjpeg/issues/1126) +- Assertion Failure in jp2.c [\#1125](https://github.com/uclouvain/openjpeg/issues/1125) +- Division-by-zero vulnerabilities in the function pi\_next\_pcrl, pi\_next\_cprl and pi\_next\_rpcl in src/lib/openjp3d/pi.c [\#1123](https://github.com/uclouvain/openjpeg/issues/1123) +- Precinct switch \(-c\) doesn't right-shift last record to remaining resolution levels [\#1117](https://github.com/uclouvain/openjpeg/issues/1117) +- Sample: encode J2K a data using streams??? [\#1114](https://github.com/uclouvain/openjpeg/issues/1114) +- HIGH THROUGHPUT JPEG 2000 \(HTJ2K\) [\#1112](https://github.com/uclouvain/openjpeg/issues/1112) +- How to build openjpeg for arm linux? [\#1108](https://github.com/uclouvain/openjpeg/issues/1108) +- crash [\#1106](https://github.com/uclouvain/openjpeg/issues/1106) +- JP2000 returning OPJ\_CLRSPC\_UNKNOWN color space [\#1103](https://github.com/uclouvain/openjpeg/issues/1103) +- Compilation successful but install unsuccessful: Calling executables throws libraries missing error [\#1102](https://github.com/uclouvain/openjpeg/issues/1102) +- fprintf format string requires 1 parameter but only 0 are given [\#1093](https://github.com/uclouvain/openjpeg/issues/1093) +- fprintf format string requires 1 parameter but only 0 are given [\#1092](https://github.com/uclouvain/openjpeg/issues/1092) +- sprintf buffer overflow [\#1088](https://github.com/uclouvain/openjpeg/issues/1088) +- sprintf buffer overflow [\#1085](https://github.com/uclouvain/openjpeg/issues/1085) +- Infinite loop when reading jp2 [\#1081](https://github.com/uclouvain/openjpeg/issues/1081) +- missing format string parameter [\#1074](https://github.com/uclouvain/openjpeg/issues/1074) +- Excessive Iteration in opj\_t1\_encode\_cblks \(src/lib/openjp2/t1.c\) [\#1059](https://github.com/uclouvain/openjpeg/issues/1059) +- Out-of-bound left shift in opj\_j2k\_setup\_encoder \(src/lib/openjp2/j2k.c\) [\#1057](https://github.com/uclouvain/openjpeg/issues/1057) +- Encode image on Unsplash [\#1054](https://github.com/uclouvain/openjpeg/issues/1054) +- Integer overflow in opj\_t1\_encode\_cblks \(src/lib/openjp2/t1.c\) [\#1053](https://github.com/uclouvain/openjpeg/issues/1053) +- Signed Integer Overflow - 68065512 [\#1048](https://github.com/uclouvain/openjpeg/issues/1048) +- Similar vulnerable functions related to CVE-2017-14041 [\#1044](https://github.com/uclouvain/openjpeg/issues/1044) +- \[ERROR\] COD marker already read. No more than one COD marker per tile. [\#1043](https://github.com/uclouvain/openjpeg/issues/1043) +- failing to install latest version of openjpeg from source [\#1041](https://github.com/uclouvain/openjpeg/issues/1041) +- Trouble compressing large raw image [\#1032](https://github.com/uclouvain/openjpeg/issues/1032) +- Download and installed code from 2.3 archive. Installing 2.2? [\#1030](https://github.com/uclouvain/openjpeg/issues/1030) +- missing fclose [\#1029](https://github.com/uclouvain/openjpeg/issues/1029) +- NULL Pointer Access in function imagetopnm of convert.c\(jp2\):1289 [\#860](https://github.com/uclouvain/openjpeg/issues/860) +- NULL Pointer Access in function imagetopnm of convert.c:2226\(jp2\) [\#859](https://github.com/uclouvain/openjpeg/issues/859) +- Heap Buffer Overflow in function imagetotga of convert.c\(jp2\):942 [\#858](https://github.com/uclouvain/openjpeg/issues/858) + +**Merged pull requests:** + +- abi-check.sh: fix broken download URL [\#1188](https://github.com/uclouvain/openjpeg/pull/1188) ([rouault](https://github.com/rouault)) +- opj\_t1\_encode\_cblks: fix UBSAN signed integer overflow [\#1187](https://github.com/uclouvain/openjpeg/pull/1187) ([rouault](https://github.com/rouault)) +- convertbmp: detect invalid file dimensions early \(CVE-2018-6616\) [\#1172](https://github.com/uclouvain/openjpeg/pull/1172) ([hlef](https://github.com/hlef)) +- color\_apply\_icc\_profile: avoid potential heap buffer overflow [\#1170](https://github.com/uclouvain/openjpeg/pull/1170) ([rouault](https://github.com/rouault)) +- Fix multiple potential vulnerabilities and bugs [\#1168](https://github.com/uclouvain/openjpeg/pull/1168) ([Young-X](https://github.com/Young-X)) +- Fix several memory and resource leaks [\#1163](https://github.com/uclouvain/openjpeg/pull/1163) ([nforro](https://github.com/nforro)) +- Fix some potential overflow issues [\#1161](https://github.com/uclouvain/openjpeg/pull/1161) ([stweil](https://github.com/stweil)) +- jp3d/jpwl convert: fix write stack buffer overflow [\#1160](https://github.com/uclouvain/openjpeg/pull/1160) ([hlef](https://github.com/hlef)) +- Int overflow fixed [\#1159](https://github.com/uclouvain/openjpeg/pull/1159) ([ichlubna](https://github.com/ichlubna)) +- Update knownfailures- files given current configurations [\#1149](https://github.com/uclouvain/openjpeg/pull/1149) ([rouault](https://github.com/rouault)) +- CVE-2018-5785: fix issues with zero bitmasks [\#1148](https://github.com/uclouvain/openjpeg/pull/1148) ([hlef](https://github.com/hlef)) +- openjp2/jp2: Fix two format strings [\#1143](https://github.com/uclouvain/openjpeg/pull/1143) ([stweil](https://github.com/stweil)) +- Changes in pnmtoimage if image data are missing [\#1141](https://github.com/uclouvain/openjpeg/pull/1141) ([szukw000](https://github.com/szukw000)) +- Relative path to header files is hardcoded in OpenJPEGConfig.cmake.in file [\#1140](https://github.com/uclouvain/openjpeg/pull/1140) ([bukatlib](https://github.com/bukatlib)) +- Cast on uint ceildiv [\#1136](https://github.com/uclouvain/openjpeg/pull/1136) ([reverson](https://github.com/reverson)) +- Add -DBUILD\_PKGCONFIG\_FILES to install instructions [\#1133](https://github.com/uclouvain/openjpeg/pull/1133) ([robe2](https://github.com/robe2)) +- Fix some typos in code comments and documentation [\#1128](https://github.com/uclouvain/openjpeg/pull/1128) ([stweil](https://github.com/stweil)) +- Fix regression in reading files with TNsot == 0 \(refs \#1120\) [\#1121](https://github.com/uclouvain/openjpeg/pull/1121) ([rouault](https://github.com/rouault)) +- Use local type declaration for POSIX standard type only for MS compiler [\#1119](https://github.com/uclouvain/openjpeg/pull/1119) ([stweil](https://github.com/stweil)) +- Fix Mac builds [\#1104](https://github.com/uclouvain/openjpeg/pull/1104) ([rouault](https://github.com/rouault)) +- jp3d: Replace sprintf\(\) by snprintf\(\) in volumetobin\(\) [\#1101](https://github.com/uclouvain/openjpeg/pull/1101) ([kbabioch](https://github.com/kbabioch)) +- opj\_mj2\_extract: Rename output\_location to output\_prefix [\#1096](https://github.com/uclouvain/openjpeg/pull/1096) ([kbabioch](https://github.com/kbabioch)) +- mj2: Add missing variable to format string in fprintf\(\) invocation in meta\_out.c [\#1094](https://github.com/uclouvain/openjpeg/pull/1094) ([kbabioch](https://github.com/kbabioch)) +- Convert files to UTF-8 encoding [\#1090](https://github.com/uclouvain/openjpeg/pull/1090) ([stweil](https://github.com/stweil)) +- fix unchecked integer multiplication overflow [\#1080](https://github.com/uclouvain/openjpeg/pull/1080) ([setharnold](https://github.com/setharnold)) +- Fixed typos [\#1062](https://github.com/uclouvain/openjpeg/pull/1062) ([radarhere](https://github.com/radarhere)) +- Note that seek uses SEEK\_SET behavior. [\#1055](https://github.com/uclouvain/openjpeg/pull/1055) ([ideasman42](https://github.com/ideasman42)) +- Some Doxygen tags are removed [\#1050](https://github.com/uclouvain/openjpeg/pull/1050) ([szukw000](https://github.com/szukw000)) +- Fix resource leak \(CID 179466\) [\#1047](https://github.com/uclouvain/openjpeg/pull/1047) ([stweil](https://github.com/stweil)) +- Changed cmake version test to allow for cmake 2.8.11.x [\#1042](https://github.com/uclouvain/openjpeg/pull/1042) ([radarhere](https://github.com/radarhere)) +- Add missing fclose\(\) statement in error condition. [\#1037](https://github.com/uclouvain/openjpeg/pull/1037) ([gfiumara](https://github.com/gfiumara)) + +## [v2.3.0](https://github.com/uclouvain/openjpeg/releases/v2.3.0) (2017-10-04) +[Full Changelog](https://github.com/uclouvain/openjpeg/compare/v2.2.0...v2.3.0) + +**Implemented enhancements:** + +- Sub-tile decoding: only decode precincts and codeblocks that intersect the window specified in opj_set_decode_area() [\#990](https://github.com/uclouvain/openjpeg/pull/990) ([rouault](https://github.com/rouault)) +- Sub-tile decoding: only apply IDWT on areas that participate to the window of interest [\#1001](https://github.com/uclouvain/openjpeg/pull/1001) ([rouault](https://github.com/rouault)) +- Sub-tile decoding: memory use reduction and perf improvements [\#1010](https://github.com/uclouvain/openjpeg/pull/1010) ([rouault](https://github.com/rouault)) +- Add capability to decode only a subset of all components of an image. [\#1022](https://github.com/uclouvain/openjpeg/pull/1022) ([rouault](https://github.com/rouault)) + +**Fixed bugs:** + +- Setting x offset of decode region to -1 causes opj\_decompress to go into infinite loop [\#736](https://github.com/uclouvain/openjpeg/issues/736) +- Problem decoding multiple tiles with get\_decoded\_tile when cmap/pclr/cdef boxes are present in jp2 file [\#484](https://github.com/uclouvain/openjpeg/issues/484) +- set reduce\_factor\_may\_fail [\#474](https://github.com/uclouvain/openjpeg/issues/474) +- opj\_compress.exe, command line parser, infinite loop [\#469](https://github.com/uclouvain/openjpeg/issues/469) +- Various memory access issues found via fuzzing [\#448](https://github.com/uclouvain/openjpeg/issues/448) +- Multiple warnings when building OpenJPEG \(trunk\) [\#442](https://github.com/uclouvain/openjpeg/issues/442) +- Bulk fuzz-testing report [\#427](https://github.com/uclouvain/openjpeg/issues/427) +- remove all printf from openjpeg / use proper function pointer for logging [\#371](https://github.com/uclouvain/openjpeg/issues/371) +- minor changes, clean-up [\#349](https://github.com/uclouvain/openjpeg/issues/349) +- image-\>numcomps \> 4 [\#333](https://github.com/uclouvain/openjpeg/issues/333) +- Improve support for region of interest [\#39](https://github.com/uclouvain/openjpeg/issues/39) +- Public function to tell kernel type used \(5x3 vs 9x7\) [\#3](https://github.com/uclouvain/openjpeg/issues/3) +- elf binary in source package ? [\#1026](https://github.com/uclouvain/openjpeg/issues/1026) +- opj\_cio\_open [\#1025](https://github.com/uclouvain/openjpeg/issues/1025) +- Building with Visual Studio 2015 [\#1023](https://github.com/uclouvain/openjpeg/issues/1023) +- tcd.cpp\>:1617:33: error: assigning to 'OPJ\_INT32 \*' \(aka 'int \*'\) from incompatible type 'void \*' [\#1021](https://github.com/uclouvain/openjpeg/issues/1021) +- j2k.cpp \> comparison of address of 'p\_j2k-\>m\_cp.tcps\[0\].m\_data' not equal to a null pointer is always true [\#1020](https://github.com/uclouvain/openjpeg/issues/1020) +- Openjpeg 2.2.0 always build shared library even though -DBUILD\_SHARED\_LIBS:bool=off [\#1019](https://github.com/uclouvain/openjpeg/issues/1019) +- missing fclose [\#1018](https://github.com/uclouvain/openjpeg/issues/1018) +- Use opj\_image\_data\_free instead of opj\_free for image-\>comps\[\].data [\#1014](https://github.com/uclouvain/openjpeg/issues/1014) +- malloc poison on some compilers - cross compiling [\#1013](https://github.com/uclouvain/openjpeg/issues/1013) +- Add OPJ\_VERSION\_MAJOR, OPJ\_VERSION\_MINOR, OPJ\_VERSION\_MICRO macros in openjpeg.h [\#1011](https://github.com/uclouvain/openjpeg/issues/1011) +- Encode: do not perform rate control for single-tile lossless [\#1009](https://github.com/uclouvain/openjpeg/issues/1009) +- opj\_set\_decoded\_resolution\_factor\(\): bad interaction with opj\_set\_decode\_area\(\) and/or opj\_decode\(\) [\#1006](https://github.com/uclouvain/openjpeg/issues/1006) +- memory allocation failure with .pgx file [\#999](https://github.com/uclouvain/openjpeg/issues/999) +- Unable to fuzz with raw image as input [\#998](https://github.com/uclouvain/openjpeg/issues/998) +- stack-based buffer overflow write in pgxtoimage \(/convert.c\) [\#997](https://github.com/uclouvain/openjpeg/issues/997) +- freeze with a crafted bmp [\#996](https://github.com/uclouvain/openjpeg/issues/996) +- invalid memory write in tgatoimage \(convert.c\) [\#995](https://github.com/uclouvain/openjpeg/issues/995) +- static build on Windows fails [\#994](https://github.com/uclouvain/openjpeg/issues/994) +- another heap-based buffer overflow in opj\_t2\_encode\_packet \(t2.c\) [\#993](https://github.com/uclouvain/openjpeg/issues/993) +- heap-based buffer overflow in opj\_t2\_encode\_packet \(t2.c\) [\#992](https://github.com/uclouvain/openjpeg/issues/992) +- heap-based buffer overflow in opj\_write\_bytes\_LE \(cio.c\) \(unfixed \#985\) [\#991](https://github.com/uclouvain/openjpeg/issues/991) +- heap overflow in opj\_compress [\#988](https://github.com/uclouvain/openjpeg/issues/988) +- heap overflow in opj\_decompress [\#987](https://github.com/uclouvain/openjpeg/issues/987) +- heap-based buffer overflow in opj\_bio\_byteout \(bio.c\) [\#986](https://github.com/uclouvain/openjpeg/issues/986) +- heap-based buffer overflow in opj\_write\_bytes\_LE \(cio.c\) [\#985](https://github.com/uclouvain/openjpeg/issues/985) +- memory allocation failure in opj\_aligned\_alloc\_n \(opj\_malloc.c\) [\#983](https://github.com/uclouvain/openjpeg/issues/983) +- heap-base buffer overflow in opj\_mqc\_flush \(mqc.c\) [\#982](https://github.com/uclouvain/openjpeg/issues/982) +- Decode fails for JP2s with ICC profile [\#981](https://github.com/uclouvain/openjpeg/issues/981) +- Unit tests failing on Ubuntu 17.04 [\#916](https://github.com/uclouvain/openjpeg/issues/916) +- Encoder crashes on small images [\#901](https://github.com/uclouvain/openjpeg/issues/901) +- openjpeg-1.5.3 fails to compile [\#830](https://github.com/uclouvain/openjpeg/issues/830) +- opj\_compress crops image \(win\) or creates a jp2 which cannot be decompressed \(lin\) [\#716](https://github.com/uclouvain/openjpeg/issues/716) +- -d flag is silently ignored when decoding a single tile [\#693](https://github.com/uclouvain/openjpeg/issues/693) +- transition away from dev-utils [\#628](https://github.com/uclouvain/openjpeg/issues/628) +- update instructions to build with Visual Studio and 64-Bit Visual C++ Toolset. [\#1028](https://github.com/uclouvain/openjpeg/pull/1028) ([quangnh89](https://github.com/quangnh89)) +- Add missing newline at end of file [\#1024](https://github.com/uclouvain/openjpeg/pull/1024) ([stweil](https://github.com/stweil)) +- merge master into coverity\_scan to update coverity results [\#1008](https://github.com/uclouvain/openjpeg/pull/1008) ([detonin](https://github.com/detonin)) +- Use more const qualifiers [\#984](https://github.com/uclouvain/openjpeg/pull/984) ([stweil](https://github.com/stweil)) +- Changes in converttif.c for PPC64 [\#980](https://github.com/uclouvain/openjpeg/pull/980) ([szukw000](https://github.com/szukw000)) + +## [v2.2.0](https://github.com/uclouvain/openjpeg/releases/v2.2.0) (2017-08-10) +[Full Changelog](https://github.com/uclouvain/openjpeg/compare/v2.1.2...v2.2.0) + +**Implemented enhancements:** + +- Memory consumption reduction at decoding side [\#968](https://github.com/uclouvain/openjpeg/pull/968) ([rouault](https://github.com/rouault)) +- T1 & DWT multithreading decoding optimizations [\#786](https://github.com/uclouvain/openjpeg/pull/786) ([rouault](https://github.com/rouault)) +- Tier1 decoder speed optimizations [\#783](https://github.com/uclouvain/openjpeg/pull/783) ([rouault](https://github.com/rouault)) +- Inverse DWT 5x3: lift implementation / SSE accelerated version [\#953](https://github.com/uclouvain/openjpeg/issues/953) +- install static libraries [\#969](https://github.com/uclouvain/openjpeg/pull/969) ([jeroen](https://github.com/jeroen)) +- IDWT 5x3 single-pass lifting and SSE2/AVX2 implementation [\#957](https://github.com/uclouvain/openjpeg/pull/957) ([rouault](https://github.com/rouault)) +- build both shared and static library [\#954](https://github.com/uclouvain/openjpeg/pull/954) ([jeroen](https://github.com/jeroen)) +- T1 flag optimizations \(\#172\) [\#945](https://github.com/uclouvain/openjpeg/pull/945) ([rouault](https://github.com/rouault)) +- CMake: add stronger warnings for openjp2 lib/bin by default, and error out on declaration-after-statement [\#936](https://github.com/uclouvain/openjpeg/pull/936) ([rouault](https://github.com/rouault)) +- Quiet mode for opj\_decompress via -quiet long parameter. [\#928](https://github.com/uclouvain/openjpeg/pull/928) ([RussellMcOrmond](https://github.com/RussellMcOrmond)) +- Implement predictive termination check [\#800](https://github.com/uclouvain/openjpeg/pull/800) ([rouault](https://github.com/rouault)) + +**Fixed bugs:** + +- Several issues spotted by Google OSS Fuzz - [see here](https://github.com/search?l=&q=OSS+Fuzz+author-date%3A2017-07-04..2017-08-01+repo%3Auclouvain%2Fopenjpeg&ref=advsearch&type=Commits&utf8=%E2%9C%93) +- Missing fclose [\#976](https://github.com/uclouvain/openjpeg/issues/976) +- Heap buffer overflow read in openjpeg imagetopnm [\#970](https://github.com/uclouvain/openjpeg/issues/970) +- opj\_decompress opj\_j2k\_update\_image\_data\(\) Segment falut [\#948](https://github.com/uclouvain/openjpeg/issues/948) +- Generic Crash in 1.5.0 [\#941](https://github.com/uclouvain/openjpeg/issues/941) +- Segmentation Faults [\#940](https://github.com/uclouvain/openjpeg/issues/940) +- Assertions thrown [\#939](https://github.com/uclouvain/openjpeg/issues/939) +- Floating Point Errors [\#938](https://github.com/uclouvain/openjpeg/issues/938) +- Division by zero crash [\#937](https://github.com/uclouvain/openjpeg/issues/937) +- malformed jp2 can cause heap-buffer-overflow [\#909](https://github.com/uclouvain/openjpeg/issues/909) +- NULL dereference can cause by malformed file [\#908](https://github.com/uclouvain/openjpeg/issues/908) +- Out of bound read in opj\_j2k\_add\_mct [\#907](https://github.com/uclouvain/openjpeg/issues/907) +- Check bpno\_plus\_one in opj\_t1\_decode\_cblk [\#903](https://github.com/uclouvain/openjpeg/issues/903) +- Undefined-shift in opj\_j2k\_read\_siz [\#902](https://github.com/uclouvain/openjpeg/issues/902) +- opj\_compress v2.1.2 can create images opj\_decompress cannot read [\#891](https://github.com/uclouvain/openjpeg/issues/891) +- Improper usage of opj\_int\_ceildiv can cause overflows [\#889](https://github.com/uclouvain/openjpeg/issues/889) +- Undefined shift in opj\_get\_all\_encoding\_parameters [\#885](https://github.com/uclouvain/openjpeg/issues/885) +- Denial of service \(crash\) due to use-after-free when decoding an illegal JPEG2000 image file v2.1.2 \(2017-04 [\#880](https://github.com/uclouvain/openjpeg/issues/880) +- Denial of service \(crash\) when decoding an illegal JPEG2000 image file v2.1.2 \(2017-03\) [\#879](https://github.com/uclouvain/openjpeg/issues/879) +- bug png 2 j2k [\#868](https://github.com/uclouvain/openjpeg/issues/868) +- Inconsistent compression using cinema settings on folder of non-compliant image [\#864](https://github.com/uclouvain/openjpeg/issues/864) +- Openjpeg-2.1.2 Heap Buffer Overflow Vulnerability due to Insufficient check [\#862](https://github.com/uclouvain/openjpeg/issues/862) +- Heap Buffer Overflow in function pnmtoimage of convert.c [\#861](https://github.com/uclouvain/openjpeg/issues/861) +- CVE-2016-9112 FPE\(Floating Point Exception\) in lib/openjp2/pi.c:523 [\#855](https://github.com/uclouvain/openjpeg/issues/855) +- CVE-2016-5139, CVE-2016-5152, CVE-2016-5158, CVE-2016-5159 [\#854](https://github.com/uclouvain/openjpeg/issues/854) +- Undefined Reference error [\#853](https://github.com/uclouvain/openjpeg/issues/853) +- opj\_compress with lossy compression results in strange pixel values [\#851](https://github.com/uclouvain/openjpeg/issues/851) +- CVE-2016-1626 and CVE-2016-1628 [\#850](https://github.com/uclouvain/openjpeg/issues/850) +- Out-of-Bounds Write in opj\_mqc\_byteout of mqc.c [\#835](https://github.com/uclouvain/openjpeg/issues/835) +- WARNING in tgt\_create tree-\>numnodes == 0, no tree created. [\#794](https://github.com/uclouvain/openjpeg/issues/794) +- Potential overflow when precision is larger than 32 [\#781](https://github.com/uclouvain/openjpeg/issues/781) +- division-by-zero in function opj\_pi\_next\_rpcl of pi.c \(line 366\) [\#780](https://github.com/uclouvain/openjpeg/issues/780) +- division-by-zero in function opj\_pi\_next\_rpcl of pi.c \(line 363\) [\#779](https://github.com/uclouvain/openjpeg/issues/779) +- division-by-zero in function opj\_pi\_next\_pcrl of pi.c \(line 447\) [\#778](https://github.com/uclouvain/openjpeg/issues/778) +- division-by-zero in function opj\_pi\_next\_pcrl of pi.c \(line 444\) [\#777](https://github.com/uclouvain/openjpeg/issues/777) +- Encoding the following file with 32x32 tiling produces jp2 image with artifact [\#737](https://github.com/uclouvain/openjpeg/issues/737) +- division-by-zero \(SIGFPE\) error in opj\_pi\_next\_cprl function \(line 526 of pi.c\) [\#732](https://github.com/uclouvain/openjpeg/issues/732) +- division-by-zero \(SIGFPE\) error in opj\_pi\_next\_cprl function \(line 523 of pi.c\) [\#731](https://github.com/uclouvain/openjpeg/issues/731) +- OpenJpeg 2.1 and 1.4 fails to decompress this file correctly [\#721](https://github.com/uclouvain/openjpeg/issues/721) +- MQ Encode :uninitialized memory access when first pass does not output any bytes [\#709](https://github.com/uclouvain/openjpeg/issues/709) +- Out-of-bounds read in opj\_j2k\_update\_image\_data and opj\_tgt\_reset function [\#704](https://github.com/uclouvain/openjpeg/issues/704) +- Remove opj\_aligned\_malloc / opj\_aligned\_realloc / opj\_aligned\_free? [\#689](https://github.com/uclouvain/openjpeg/issues/689) +- There is an issue with rendering some type of jpeg file. Please ref the link. [\#672](https://github.com/uclouvain/openjpeg/issues/672) +- Null Dereference in tcd\_malloc\_decode\_tile [\#657](https://github.com/uclouvain/openjpeg/issues/657) +- ETS-C1P0-p0\_12.j2k-compare2ref & NR-C1P0-p0\_12.j2k-compare2base failing under windows [\#655](https://github.com/uclouvain/openjpeg/issues/655) +- Memory leak [\#631](https://github.com/uclouvain/openjpeg/issues/631) +- Test 481 reports error in valgrind memcheck [\#612](https://github.com/uclouvain/openjpeg/issues/612) +- reserved identifier violation [\#587](https://github.com/uclouvain/openjpeg/issues/587) +- Buffer overflow when compressing some 16 bits images of the test suite [\#539](https://github.com/uclouvain/openjpeg/issues/539) +- Heap-buffer-overflow in opj\_dwt\_decode\_1 [\#480](https://github.com/uclouvain/openjpeg/issues/480) +- Automated fuzz testing [\#468](https://github.com/uclouvain/openjpeg/issues/468) +- Expected to find EPH marker [\#425](https://github.com/uclouvain/openjpeg/issues/425) +- read: segment too long \(6182\) with max \(35872\) for codeblock 0 \(p=19, b=2, r=5, c=1\) [\#284](https://github.com/uclouvain/openjpeg/issues/284) +- building 64bit version has lots of warnings [\#244](https://github.com/uclouvain/openjpeg/issues/244) +- Wrong encoding of small tiles with high level number [\#239](https://github.com/uclouvain/openjpeg/issues/239) +- Errors raised in pi.c by VS11 analyzer [\#190](https://github.com/uclouvain/openjpeg/issues/190) +- Undocumented optimization found in v2 branch of openjpeg [\#183](https://github.com/uclouvain/openjpeg/issues/183) +- T1 optimisations jpeg2000 [\#172](https://github.com/uclouvain/openjpeg/issues/172) +- Remove OPJ\_NOSANITIZE in opj\_bio\_read\(\) and opj\_bio\_write\(\) \(\#761\) [\#955](https://github.com/uclouvain/openjpeg/pull/955) ([rouault](https://github.com/rouault)) +- Fix bypass pterm termall and lossless decomposition issue \(\#891, \#892\) [\#949](https://github.com/uclouvain/openjpeg/pull/949) ([rouault](https://github.com/rouault)) +- Escape quotes to ensure README renders on GitHub correctly [\#914](https://github.com/uclouvain/openjpeg/pull/914) ([alexwlchan](https://github.com/alexwlchan)) +- Remove spurious .R macros from manpages [\#899](https://github.com/uclouvain/openjpeg/pull/899) ([jwilk](https://github.com/jwilk)) +- Remove warnings related to empty tag-trees. [\#893](https://github.com/uclouvain/openjpeg/pull/893) ([rouault](https://github.com/rouault)) + +**Maintenance-related tasks:** + +- Submit OpenJPEG to oss-fuzz [\#965](https://github.com/uclouvain/openjpeg/issues/965) +- Updates for Doxygen to suppress warnings [\#849](https://github.com/uclouvain/openjpeg/issues/849) +- Remove useless knownfailures \(since LAZY encoding is fixed\) [\#964](https://github.com/uclouvain/openjpeg/pull/964) ([rouault](https://github.com/rouault)) +- Enable AVX2 at runtime on Travis-CI and AppVeyor [\#963](https://github.com/uclouvain/openjpeg/pull/963) ([rouault](https://github.com/rouault)) +- Tests: test opj\_compress in VSC mode \(related to \#172\) [\#935](https://github.com/uclouvain/openjpeg/pull/935) ([rouault](https://github.com/rouault)) +- Reformat: apply reformattin on .h files \(\#128\) [\#926](https://github.com/uclouvain/openjpeg/pull/926) ([rouault](https://github.com/rouault)) +- Add mechanisms to reformat and check code style, and reformat whole codebase \(\#128\) [\#919](https://github.com/uclouvain/openjpeg/pull/919) ([rouault](https://github.com/rouault)) +- Add profiling of CPU and memory usage \(\#912\) [\#918](https://github.com/uclouvain/openjpeg/pull/918) ([rouault](https://github.com/rouault)) +- Add performance benchmarking scripts [\#917](https://github.com/uclouvain/openjpeg/pull/917) ([rouault](https://github.com/rouault)) +- Fix retrieval of jpylyzer in AppVeyor [\#915](https://github.com/uclouvain/openjpeg/pull/915) ([rouault](https://github.com/rouault)) + +## [v2.1.2](https://github.com/uclouvain/openjpeg/releases/v2.1.2) (2016-09-28) +[Full Changelog](https://github.com/uclouvain/openjpeg/compare/v2.1.1...v2.1.2) + +**Closed issues:** + +- null ptr dereference in convert.c:1331 [\#843](https://github.com/uclouvain/openjpeg/issues/843) +- Out-of-Bounds Read in function bmp24toimage of convertbmp.c [\#833](https://github.com/uclouvain/openjpeg/issues/833) +- Disable automatic compilation of t1\_generate\_luts in CMakeLists.txt [\#831](https://github.com/uclouvain/openjpeg/issues/831) +- CVE-2016-7163 Integer overflow in opj\_pi\_create\_decode [\#826](https://github.com/uclouvain/openjpeg/issues/826) +- Security Advisory for OpenJPEG [\#810](https://github.com/uclouvain/openjpeg/issues/810) +- Add dashboard with static lib [\#804](https://github.com/uclouvain/openjpeg/issues/804) +- hidden visibility for the static library / building with -DOPJ\_STATIC against shared lib [\#802](https://github.com/uclouvain/openjpeg/issues/802) +- Optimization when building library from source [\#799](https://github.com/uclouvain/openjpeg/issues/799) +- unsigned int16 on Solaris 11.2/sparc [\#796](https://github.com/uclouvain/openjpeg/issues/796) +- appveyor [\#793](https://github.com/uclouvain/openjpeg/issues/793) +- FFMpeg will not link to 2.1.1 release built as shared library [\#766](https://github.com/uclouvain/openjpeg/issues/766) +- API change since v2: opj\_event\_mgr\_t not available [\#754](https://github.com/uclouvain/openjpeg/issues/754) +- openjpeg.h needs dependencies [\#673](https://github.com/uclouvain/openjpeg/issues/673) +- "master" does not build on ubuntu [\#658](https://github.com/uclouvain/openjpeg/issues/658) +- Package 'openjp2', required by 'libopenjpip', not found [\#594](https://github.com/uclouvain/openjpeg/issues/594) + +**Merged pull requests:** + +- Fix PNM file reading [\#847](https://github.com/uclouvain/openjpeg/pull/847) ([mayeut](https://github.com/mayeut)) +- Fix some issues reported by Coverity Scan [\#846](https://github.com/uclouvain/openjpeg/pull/846) ([stweil](https://github.com/stweil)) +- Fix potential out-of-bounds read \(coverity\) [\#844](https://github.com/uclouvain/openjpeg/pull/844) ([stweil](https://github.com/stweil)) +- Remove TODO for overflow check [\#842](https://github.com/uclouvain/openjpeg/pull/842) ([mayeut](https://github.com/mayeut)) +- Add overflow checks for opj\_aligned\_malloc [\#841](https://github.com/uclouvain/openjpeg/pull/841) ([mayeut](https://github.com/mayeut)) +- Flags in T1 shall be unsigned [\#840](https://github.com/uclouvain/openjpeg/pull/840) ([mayeut](https://github.com/mayeut)) +- Fix some warnings [\#838](https://github.com/uclouvain/openjpeg/pull/838) ([mayeut](https://github.com/mayeut)) +- Fix issue 833. [\#834](https://github.com/uclouvain/openjpeg/pull/834) ([trylab](https://github.com/trylab)) +- Add overflow checks for opj\_aligned\_malloc [\#832](https://github.com/uclouvain/openjpeg/pull/832) ([mayeut](https://github.com/mayeut)) +- Add test for issue 820 [\#829](https://github.com/uclouvain/openjpeg/pull/829) ([mayeut](https://github.com/mayeut)) +- Add test for issue 826 [\#827](https://github.com/uclouvain/openjpeg/pull/827) ([mayeut](https://github.com/mayeut)) +- Fix coverity 113065 \(CWE-484\) [\#824](https://github.com/uclouvain/openjpeg/pull/824) ([mayeut](https://github.com/mayeut)) +- Add sanity check for tile coordinates [\#823](https://github.com/uclouvain/openjpeg/pull/823) ([mayeut](https://github.com/mayeut)) +- Add test for PR 818 [\#822](https://github.com/uclouvain/openjpeg/pull/822) ([mayeut](https://github.com/mayeut)) +- Update to libpng 1.6.25 [\#821](https://github.com/uclouvain/openjpeg/pull/821) ([mayeut](https://github.com/mayeut)) +- CVE-2016-8332: fix incrementing of "l\_tcp-\>m\_nb\_mcc\_records" in opj\_j2k\_read\_mcc [\#820](https://github.com/uclouvain/openjpeg/pull/820) ([mayeut](https://github.com/mayeut)) +- Add overflow check in opj\_tcd\_init\_tile [\#819](https://github.com/uclouvain/openjpeg/pull/819) ([mayeut](https://github.com/mayeut)) +- Fix leak & invalid behavior of opj\_jp2\_read\_ihdr [\#818](https://github.com/uclouvain/openjpeg/pull/818) ([mayeut](https://github.com/mayeut)) +- Add overflow check in opj\_j2k\_update\_image\_data [\#817](https://github.com/uclouvain/openjpeg/pull/817) ([mayeut](https://github.com/mayeut)) +- Change 'restrict' define to 'OPJ\_RESTRICT' [\#816](https://github.com/uclouvain/openjpeg/pull/816) ([mayeut](https://github.com/mayeut)) +- Switch to clang 3.8 [\#814](https://github.com/uclouvain/openjpeg/pull/814) ([mayeut](https://github.com/mayeut)) +- Fix an integer overflow issue [\#809](https://github.com/uclouvain/openjpeg/pull/809) ([trylab](https://github.com/trylab)) +- Update to lcms 2.8 [\#808](https://github.com/uclouvain/openjpeg/pull/808) ([mayeut](https://github.com/mayeut)) +- Update to libpng 1.6.24 [\#807](https://github.com/uclouvain/openjpeg/pull/807) ([mayeut](https://github.com/mayeut)) +- Reenable clang-3.9 build on travis [\#806](https://github.com/uclouvain/openjpeg/pull/806) ([mayeut](https://github.com/mayeut)) +- Bit fields type [\#805](https://github.com/uclouvain/openjpeg/pull/805) ([smuehlst](https://github.com/smuehlst)) +- Add compilation test for standalone inclusion of openjpeg.h [\#798](https://github.com/uclouvain/openjpeg/pull/798) ([mayeut](https://github.com/mayeut)) +- jpwl: Remove non-portable data type u\_int16\_t \(fix issue \#796\) [\#797](https://github.com/uclouvain/openjpeg/pull/797) ([stweil](https://github.com/stweil)) +- Fix dependency for pkg-config \(issue \#594\) [\#795](https://github.com/uclouvain/openjpeg/pull/795) ([stweil](https://github.com/stweil)) +- Add .gitignore [\#787](https://github.com/uclouvain/openjpeg/pull/787) ([stweil](https://github.com/stweil)) + +## [v2.1.1](https://github.com/uclouvain/openjpeg/releases/tag/v2.1.1) (2016-07-05) +[Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.2.1...v2.1.1) + +**Implemented enhancements:** + +- opj\_malloc replacement [\#625](https://github.com/uclouvain/openjpeg/issues/625) +- backport "-p" and "-force-rgb" options in 1.5 [\#606](https://github.com/uclouvain/openjpeg/issues/606) +- Use travis-ci matrix build [\#581](https://github.com/uclouvain/openjpeg/issues/581) +- Add Coverity Scan analysis [\#580](https://github.com/uclouvain/openjpeg/issues/580) +- Unnecessary rate distortion calculations [\#479](https://github.com/uclouvain/openjpeg/issues/479) +- Add images from various security issues to test suite [\#415](https://github.com/uclouvain/openjpeg/issues/415) +- Coding speed for 9/7 on 32bits platforms \(x86/ARM\) can be improved with a quick fix [\#220](https://github.com/uclouvain/openjpeg/issues/220) + +**Fixed bugs:** + +- Out-of-Bounds Access in function opj\_tgt\_reset of tgt.c [\#775](https://github.com/uclouvain/openjpeg/issues/775) +- Heap Buffer Overflow in function color\_cmyk\_to\_rgb of color.c [\#774](https://github.com/uclouvain/openjpeg/issues/774) +- division-by-zero \(SIGFPE\) error in opj\_tcd\_init\_tile function \(line 730 of tcd.c\) [\#733](https://github.com/uclouvain/openjpeg/issues/733) +- Out-Of-Bounds Read in sycc422\_to\_rgb function [\#726](https://github.com/uclouvain/openjpeg/issues/726) +- Heap Corruption in opj\_free function [\#725](https://github.com/uclouvain/openjpeg/issues/725) +- Out-Of-Bounds Read in opj\_tcd\_free\_tile function [\#724](https://github.com/uclouvain/openjpeg/issues/724) +- Cannot handle box of undefined size [\#653](https://github.com/uclouvain/openjpeg/issues/653) +- Compilation fails without platform-supplied aligned malloc [\#642](https://github.com/uclouvain/openjpeg/issues/642) +- HP compiler warns about redeclaration of static function [\#640](https://github.com/uclouvain/openjpeg/issues/640) +- Implementation-defined behavior of malloc causes different behavior on Linux and AIX [\#635](https://github.com/uclouvain/openjpeg/issues/635) +- Build on AIX fails because "opj\_includes.h" is included after system headers [\#633](https://github.com/uclouvain/openjpeg/issues/633) +- Compiling with SSE2 on Linux 32-bit causes crashes in OpenJPEG [\#624](https://github.com/uclouvain/openjpeg/issues/624) +- Build on AIX fails because of "restrict" pointers [\#620](https://github.com/uclouvain/openjpeg/issues/620) +- bug in new tif conversion code [\#609](https://github.com/uclouvain/openjpeg/issues/609) +- bin/jp2/convert.c line 1085 Resource leak [\#607](https://github.com/uclouvain/openjpeg/issues/607) +- bin/jp2/convert.c memory leak [\#601](https://github.com/uclouvain/openjpeg/issues/601) +- Resource leak in opj\_j2k\_create\_cstr\_index in case of failure [\#599](https://github.com/uclouvain/openjpeg/issues/599) +- Resource leak in opj\_j2k\_encode in case of failure [\#598](https://github.com/uclouvain/openjpeg/issues/598) +- Resource leak in opj\_j2k\_decode\_one\_tile in case of failure [\#597](https://github.com/uclouvain/openjpeg/issues/597) +- Resource Leak [\#573](https://github.com/uclouvain/openjpeg/issues/573) +- opj\_compress fails to compress lossless on gcc/x86 \(-m32\) [\#571](https://github.com/uclouvain/openjpeg/issues/571) +- Use-after-free in opj\_j2k\_write\_mco [\#563](https://github.com/uclouvain/openjpeg/issues/563) +- openjpeg-master-2015-07-30 failed to compile on LINUX [\#556](https://github.com/uclouvain/openjpeg/issues/556) +- PNG images are always read as RGB\(A\) images [\#536](https://github.com/uclouvain/openjpeg/issues/536) +- g4\_colr.j2c not handled properly [\#532](https://github.com/uclouvain/openjpeg/issues/532) +- Bigendian: opj\_compress + opj\_decompress fails [\#518](https://github.com/uclouvain/openjpeg/issues/518) +- Suspicious code in j2k.c [\#517](https://github.com/uclouvain/openjpeg/issues/517) +- Decode times almost double\(!!\) on Visual Studio 2013, 2015 [\#505](https://github.com/uclouvain/openjpeg/issues/505) +- opj\_data/input/nonregression/issue226.j2k [\#500](https://github.com/uclouvain/openjpeg/issues/500) +- opj\_setup\_encoder always returns true [\#497](https://github.com/uclouvain/openjpeg/issues/497) +- Double free in j2k\_read\_ppm\_v3 parsing \(\(presumably invalid\) image. [\#496](https://github.com/uclouvain/openjpeg/issues/496) +- Invalid write in opj\_j2k\_update\_image\_data [\#495](https://github.com/uclouvain/openjpeg/issues/495) +- Undefined printf format specifier %ud used in code [\#494](https://github.com/uclouvain/openjpeg/issues/494) +- Potential double free on malloc failure in opj\_j2k\_copy\_default\_tcp\_and\_create\_tcp\(\) [\#492](https://github.com/uclouvain/openjpeg/issues/492) +- Do not link with -ffast-math [\#488](https://github.com/uclouvain/openjpeg/issues/488) +- Heap-buffer-overflow in opj\_dwt\_decode [\#486](https://github.com/uclouvain/openjpeg/issues/486) +- opj\_dump fails on Windows 7, 64 bits [\#482](https://github.com/uclouvain/openjpeg/issues/482) +- SIGSEGV in opj\_j2k\_update\_image\_data via pdfium\_test [\#481](https://github.com/uclouvain/openjpeg/issues/481) +- Heap-buffer-overflow in opj\_j2k\_tcp\_destroy [\#477](https://github.com/uclouvain/openjpeg/issues/477) +- Invalid image causes write past end of heap buffer [\#476](https://github.com/uclouvain/openjpeg/issues/476) +- Assertion `l\_res-\>x0 \>= 0' fails when parsing invalid images [\#475](https://github.com/uclouvain/openjpeg/issues/475) +- Bug on opj\_write\_bytes\_BE function [\#472](https://github.com/uclouvain/openjpeg/issues/472) +- Refactor j2k\_read\_ppm\_v3 function [\#470](https://github.com/uclouvain/openjpeg/issues/470) +- compression: strange precinct dimensions [\#466](https://github.com/uclouvain/openjpeg/issues/466) +- \(:- Console message in opj\_decompress -:\) [\#465](https://github.com/uclouvain/openjpeg/issues/465) +- opj\_decompress fails to decompress any files [\#463](https://github.com/uclouvain/openjpeg/issues/463) +- bio-\>ct is unnecessarily set to zero in opj\_bio\_flush method [\#461](https://github.com/uclouvain/openjpeg/issues/461) +- Maximal unsigned short is 65535, not 65536 [\#460](https://github.com/uclouvain/openjpeg/issues/460) +- OpenJpeg fails to encode components with different precision properly [\#459](https://github.com/uclouvain/openjpeg/issues/459) +- component precision upscaling isn't correct in opj\_decompress [\#458](https://github.com/uclouvain/openjpeg/issues/458) +- Multiple precision components won't get encoded to jp2 if 1 component is unsigned 1 bit [\#457](https://github.com/uclouvain/openjpeg/issues/457) +- Incorrect code in ../bin/jp2/convert.c, function rawtoimage\_common\(...\) [\#456](https://github.com/uclouvain/openjpeg/issues/456) +- \[OpenJPEG-trunk\] opj\_stream\_get\_number\_byte\_left throws assert [\#455](https://github.com/uclouvain/openjpeg/issues/455) +- NR-DEC-kodak\_2layers\_lrcp.j2c-31-decode-md5 fails randomly when running tests in parallel [\#454](https://github.com/uclouvain/openjpeg/issues/454) +- compare\_raw\_files doesn't report an error on invalid arguments / missing input files [\#453](https://github.com/uclouvain/openjpeg/issues/453) +- Forward discrete wavelet transform: implement periodic symmetric extension at boundaries [\#452](https://github.com/uclouvain/openjpeg/issues/452) +- Bug in tiff reading method in convert.c [\#449](https://github.com/uclouvain/openjpeg/issues/449) +- Image in pdf don't display [\#447](https://github.com/uclouvain/openjpeg/issues/447) +- Multiple issues causing opj\_decompress to segfault [\#446](https://github.com/uclouvain/openjpeg/issues/446) +- opj\_compress: 40% of encode time is spent freeing data [\#445](https://github.com/uclouvain/openjpeg/issues/445) +- Multiple warnings when configuring OpenJPEG on MacOS with CMake 3.x \(trunk\) [\#443](https://github.com/uclouvain/openjpeg/issues/443) +- valgrind memleak found [\#437](https://github.com/uclouvain/openjpeg/issues/437) +- global-buffer-overflow src/lib/openjp2/t1.c:1146 opj\_t1\_getwmsedec [\#436](https://github.com/uclouvain/openjpeg/issues/436) +- Warning introduced on trunk r2923 & r2924 [\#435](https://github.com/uclouvain/openjpeg/issues/435) +- heap-buffer-overflow in opj\_t1\_decode\_cblks [\#432](https://github.com/uclouvain/openjpeg/issues/432) +- Heap-buffer-overflow in opj\_tcd\_init\_decode\_tile [\#431](https://github.com/uclouvain/openjpeg/issues/431) +- Heap-buffer-overflow in opj\_j2k\_tcp\_destroy [\#430](https://github.com/uclouvain/openjpeg/issues/430) +- Heap-buffer-overflow in opj\_jp2\_apply\_pclr [\#429](https://github.com/uclouvain/openjpeg/issues/429) +- issue412 revisited [\#428](https://github.com/uclouvain/openjpeg/issues/428) +- Image distorted \(sides look cankered\) [\#423](https://github.com/uclouvain/openjpeg/issues/423) +- openjpeg-2.x-trunk-r2918 is broken in color.c [\#422](https://github.com/uclouvain/openjpeg/issues/422) +- Heap-buffer-overflow in opj\_tcd\_init\_decode\_tile [\#420](https://github.com/uclouvain/openjpeg/issues/420) +- Heap-use-after-free in opj\_t1\_decode\_cblks [\#418](https://github.com/uclouvain/openjpeg/issues/418) +- UNKNOWN in opj\_read\_bytes\_LE [\#417](https://github.com/uclouvain/openjpeg/issues/417) +- Transparency problem [\#416](https://github.com/uclouvain/openjpeg/issues/416) +- Image with per channel alpha \(cdef\) does not decode properly [\#414](https://github.com/uclouvain/openjpeg/issues/414) +- OpenJPEG crashes with attached image [\#413](https://github.com/uclouvain/openjpeg/issues/413) +- Palette image with cdef fails to decompress [\#412](https://github.com/uclouvain/openjpeg/issues/412) +- Invalid member values from opj\_read\_header or opj\_decode ? [\#411](https://github.com/uclouvain/openjpeg/issues/411) +- MD5 Checksum hangs under valgrind on MacOS X [\#410](https://github.com/uclouvain/openjpeg/issues/410) +- Heap-buffer-overflow in opj\_tcd\_get\_decoded\_tile\_size [\#408](https://github.com/uclouvain/openjpeg/issues/408) +- C++ style comments in trunk/src/lib/openjp2/j2k.c [\#407](https://github.com/uclouvain/openjpeg/issues/407) +- Backport bugfixes from trunk to 2.1 branch [\#405](https://github.com/uclouvain/openjpeg/issues/405) +- Heap-buffer-overflow in parse\_cmdline\_encoder [\#403](https://github.com/uclouvain/openjpeg/issues/403) +- Heap-buffer-overflow in opj\_v4dwt\_interleave\_h [\#400](https://github.com/uclouvain/openjpeg/issues/400) +- Heap-buffer-overflow in opj\_dwt\_decode [\#399](https://github.com/uclouvain/openjpeg/issues/399) +- Heap-use-after-free in opj\_t1\_decode\_cblks [\#398](https://github.com/uclouvain/openjpeg/issues/398) +- Heap-buffer-overflow in opj\_jp2\_apply\_cdef [\#397](https://github.com/uclouvain/openjpeg/issues/397) +- Heap-buffer-overflow in opj\_t2\_read\_packet\_header [\#396](https://github.com/uclouvain/openjpeg/issues/396) +- Heap-buffer-overflow in opj\_t2\_read\_packet\_header [\#395](https://github.com/uclouvain/openjpeg/issues/395) +- Heap-buffer-overflow in opj\_dwt\_decode\_1 [\#394](https://github.com/uclouvain/openjpeg/issues/394) +- Heap-double-free in j2k\_read\_ppm\_v3 [\#393](https://github.com/uclouvain/openjpeg/issues/393) +- Security hole in j2k.c [\#392](https://github.com/uclouvain/openjpeg/issues/392) +- Security: double-free in opj\_tcd\_code\_block\_dec\_deallocate [\#391](https://github.com/uclouvain/openjpeg/issues/391) +- check for negative-size params in code [\#390](https://github.com/uclouvain/openjpeg/issues/390) +- Heap-buffer-overflow in opj\_t2\_read\_packet\_header [\#389](https://github.com/uclouvain/openjpeg/issues/389) +- Heap overflow in OpenJpeg 1.5.2 [\#388](https://github.com/uclouvain/openjpeg/issues/388) +- openjpip.so.6 file too short [\#387](https://github.com/uclouvain/openjpeg/issues/387) +- Corrupted JP3D file [\#386](https://github.com/uclouvain/openjpeg/issues/386) +- variable assigned to itself [\#383](https://github.com/uclouvain/openjpeg/issues/383) +- Null pointer dereferencing [\#382](https://github.com/uclouvain/openjpeg/issues/382) +- bad use of case statement [\#381](https://github.com/uclouvain/openjpeg/issues/381) +- Release 2.1 as a Ubuntu package [\#380](https://github.com/uclouvain/openjpeg/issues/380) +- Bug in libopenjpwl.pc [\#374](https://github.com/uclouvain/openjpeg/issues/374) +- inconsistent tile numbering in decode output message [\#370](https://github.com/uclouvain/openjpeg/issues/370) +- error in code block calculations [\#369](https://github.com/uclouvain/openjpeg/issues/369) +- r2872 fails to compile due to "attempt to use poisoned malloc" error in j2k.c [\#368](https://github.com/uclouvain/openjpeg/issues/368) +- OSX build gives libopenjp2.6.dylib with not-absolute install name id [\#367](https://github.com/uclouvain/openjpeg/issues/367) +- opj\_decompress gives error but successfully decompress in OPJ 2.1 [\#366](https://github.com/uclouvain/openjpeg/issues/366) +- pngtoimage\(\) and imagetopng\(\) have wrong byte order for 16-Bit image [\#365](https://github.com/uclouvain/openjpeg/issues/365) +- PDF crash in chrome - part2 \(due to attachment limit\) [\#364](https://github.com/uclouvain/openjpeg/issues/364) +- PDF crash in chrome - part1 [\#363](https://github.com/uclouvain/openjpeg/issues/363) +- PDF crash in chrome - part0 [\#362](https://github.com/uclouvain/openjpeg/issues/362) +- Compilation fails on Windows with mingw32 gcc4.8 [\#361](https://github.com/uclouvain/openjpeg/issues/361) +- security issue [\#360](https://github.com/uclouvain/openjpeg/issues/360) +- improve memory management [\#359](https://github.com/uclouvain/openjpeg/issues/359) +- how to compress a yuv420 raw data using opj\_compress [\#357](https://github.com/uclouvain/openjpeg/issues/357) +- Some memory allocation are not checked [\#355](https://github.com/uclouvain/openjpeg/issues/355) +- Static library symbols shall be marked as hidden [\#354](https://github.com/uclouvain/openjpeg/issues/354) +- opj\_compress rejects valid bmp files [\#353](https://github.com/uclouvain/openjpeg/issues/353) +- opj\_compress crashes when number of resolutions is set to zero [\#352](https://github.com/uclouvain/openjpeg/issues/352) +- Compilation error under Visual Studio 2003 [\#351](https://github.com/uclouvain/openjpeg/issues/351) +- opj\_compress description example error \[Low priority\] [\#350](https://github.com/uclouvain/openjpeg/issues/350) +- opj\_write\_bytes\_BE is wrong in trunk [\#345](https://github.com/uclouvain/openjpeg/issues/345) +- PART1ONLY option in release.sh doesn't work properly [\#332](https://github.com/uclouvain/openjpeg/issues/332) +- openjpeg crash error [\#330](https://github.com/uclouvain/openjpeg/issues/330) +- openjpeg decompress error [\#329](https://github.com/uclouvain/openjpeg/issues/329) +- openjpeg decompress issue [\#326](https://github.com/uclouvain/openjpeg/issues/326) +- limited tif support [\#322](https://github.com/uclouvain/openjpeg/issues/322) +- asoc value of 65536 is allowed [\#321](https://github.com/uclouvain/openjpeg/issues/321) +- opj\_skip\_from\_file error [\#314](https://github.com/uclouvain/openjpeg/issues/314) +- Heavy quota usage in openjpeg [\#309](https://github.com/uclouvain/openjpeg/issues/309) +- Verify -help actually match letter [\#307](https://github.com/uclouvain/openjpeg/issues/307) +- g3\_colr.j2c not handled [\#288](https://github.com/uclouvain/openjpeg/issues/288) +- reopen/fix issue 165 [\#280](https://github.com/uclouvain/openjpeg/issues/280) +- kakadu conformance tests [\#279](https://github.com/uclouvain/openjpeg/issues/279) +- missing break after case statement in opj\_dwt\_decode\_real [\#274](https://github.com/uclouvain/openjpeg/issues/274) +- Run Coverity on trunk [\#270](https://github.com/uclouvain/openjpeg/issues/270) +- NR-ENC-random-issue-0005.tif-12-encode [\#259](https://github.com/uclouvain/openjpeg/issues/259) +- Use new add\_test signature to handle cross compilation [\#258](https://github.com/uclouvain/openjpeg/issues/258) +- Loss decoding quality in 2.0.0 [\#254](https://github.com/uclouvain/openjpeg/issues/254) +- Decompress that worked in 1.5.1 fails in 2.0 [\#252](https://github.com/uclouvain/openjpeg/issues/252) +- Expected endianness with raw input is not documented leading to SEGFAULT [\#251](https://github.com/uclouvain/openjpeg/issues/251) +- OpenJPEG writes to stderr [\#246](https://github.com/uclouvain/openjpeg/issues/246) +- Inconsistent logging of tile index [\#245](https://github.com/uclouvain/openjpeg/issues/245) +- patch for openjpeg-trunk-r2347 and BIG\_ENDIAN [\#242](https://github.com/uclouvain/openjpeg/issues/242) +- CMAP: MTYP == 0 \(direct use\) not handled properly [\#235](https://github.com/uclouvain/openjpeg/issues/235) +- Black Pixel [\#233](https://github.com/uclouvain/openjpeg/issues/233) +- opj\_compress runtime error after fresh Linux install due to apparent failure to execute ldconfig [\#219](https://github.com/uclouvain/openjpeg/issues/219) +- openjp2 debug works, release build does not [\#217](https://github.com/uclouvain/openjpeg/issues/217) +- openjpeg-branch15-r2299 and openjpeg-trunk-r2299 fail to decode a JP2 file [\#212](https://github.com/uclouvain/openjpeg/issues/212) +- openjpeg-trunk issue with Win7 [\#201](https://github.com/uclouvain/openjpeg/issues/201) +- undefined reference to `opj\_version' [\#200](https://github.com/uclouvain/openjpeg/issues/200) +- In tgt.c we used fprintf not the openjpeg message reporter [\#184](https://github.com/uclouvain/openjpeg/issues/184) +- Windows binaries not working under WinXP [\#176](https://github.com/uclouvain/openjpeg/issues/176) +- add ability to use intel ipp \(performance primitive\) within OpenJPEG [\#164](https://github.com/uclouvain/openjpeg/issues/164) +- Migration guide v2 [\#160](https://github.com/uclouvain/openjpeg/issues/160) +- Cannot decompress JPEG2000Aware3.18.7.3Win32\_kdutranscode6.3.1.j2k [\#158](https://github.com/uclouvain/openjpeg/issues/158) +- Cannot decompress JPEG2000Aware3.18.7.3Win32.j2k [\#157](https://github.com/uclouvain/openjpeg/issues/157) +- openjpeg@googlegroups.com has disappeared [\#153](https://github.com/uclouvain/openjpeg/issues/153) +- OpenJPEG 1.5.0 crashes on a ridiculously big file... [\#151](https://github.com/uclouvain/openjpeg/issues/151) +- opj\_image vs free [\#146](https://github.com/uclouvain/openjpeg/issues/146) +- Windows .dll file invalid [\#140](https://github.com/uclouvain/openjpeg/issues/140) +- Problem with second layer of a 2 layer coded LRCP \(with precincts\) [\#135](https://github.com/uclouvain/openjpeg/issues/135) +- version 1.4 crashes when opening PDF file with JPEG2000 images [\#133](https://github.com/uclouvain/openjpeg/issues/133) +- Setup a win64 dashboard [\#132](https://github.com/uclouvain/openjpeg/issues/132) +- J2KP4files/codestreams\_profile0/p0\_13.j2k question jpeg2000 [\#131](https://github.com/uclouvain/openjpeg/issues/131) +- Out of memory: Kill process 11204 \(opj\_server\) score 917 or sacrifice child [\#123](https://github.com/uclouvain/openjpeg/issues/123) +- FILE\* in opj API is unsafe [\#120](https://github.com/uclouvain/openjpeg/issues/120) +- third-party lib order [\#119](https://github.com/uclouvain/openjpeg/issues/119) +- openjpeg-1.5.0-Darwin-powerpc.dmg is huge ! [\#113](https://github.com/uclouvain/openjpeg/issues/113) +- misleading info in JP2 box lead to wrong number of components [\#110](https://github.com/uclouvain/openjpeg/issues/110) +- Image\_to\_j2k says that j2k files is generated but no file is on the HDD [\#109](https://github.com/uclouvain/openjpeg/issues/109) +- Error in openjpegV1.4 on compiling image\_to\_j2k: crash on reading bmp file [\#108](https://github.com/uclouvain/openjpeg/issues/108) +- Update to abi-compliance-checker 1.96 [\#106](https://github.com/uclouvain/openjpeg/issues/106) +- Decode error on the attached JPEG...works in KDU and with JASPER...please help! [\#101](https://github.com/uclouvain/openjpeg/issues/101) +- Mac binaries v1.4 is broken [\#95](https://github.com/uclouvain/openjpeg/issues/95) +- jp2\_read\_boxhdr\(\) has size bug in version 1 [\#92](https://github.com/uclouvain/openjpeg/issues/92) +- Support for Java JAI Imageio [\#90](https://github.com/uclouvain/openjpeg/issues/90) +- encoding test failing [\#86](https://github.com/uclouvain/openjpeg/issues/86) +- source archive on demand [\#85](https://github.com/uclouvain/openjpeg/issues/85) +- CMakeLists.txt and Makefile.am for JPIP are buggy [\#84](https://github.com/uclouvain/openjpeg/issues/84) +- pclr-cmap-cdef [\#82](https://github.com/uclouvain/openjpeg/issues/82) +- Error when compiling openjpeg\_v1\_4\_sources\_r697 [\#79](https://github.com/uclouvain/openjpeg/issues/79) +- J2K codec issue on Windows Mobile [\#77](https://github.com/uclouvain/openjpeg/issues/77) +- image\_to\_j2k.exe crashes on large .bmp file [\#75](https://github.com/uclouvain/openjpeg/issues/75) +- fatal error C1900 building the project on windows [\#65](https://github.com/uclouvain/openjpeg/issues/65) +- same option but different size [\#54](https://github.com/uclouvain/openjpeg/issues/54) +- Missing openjpegConfigure.h [\#38](https://github.com/uclouvain/openjpeg/issues/38) +- Not an issue in openjpeg, but ... [\#37](https://github.com/uclouvain/openjpeg/issues/37) +- OpenJPEG-1.3.0 pclr, cmap and cdef [\#27](https://github.com/uclouvain/openjpeg/issues/27) +- realloc maybe too big \(t2.c\) [\#26](https://github.com/uclouvain/openjpeg/issues/26) +- libopenjpeg/opj\_malloc.h breaks on FreeBSD/Darwin systems [\#20](https://github.com/uclouvain/openjpeg/issues/20) +- image\_to\_j2k not outputting to win32 console properly [\#18](https://github.com/uclouvain/openjpeg/issues/18) +- \[OpenJPEG\] OpenJPEG\_v13: tiled image part 2 [\#17](https://github.com/uclouvain/openjpeg/issues/17) +- JP2 Color Space modification by Matteo Italia [\#13](https://github.com/uclouvain/openjpeg/issues/13) +- Patch submission \( exotic video formats, and a few things \) [\#12](https://github.com/uclouvain/openjpeg/issues/12) +- 16 bits lossy compression [\#10](https://github.com/uclouvain/openjpeg/issues/10) +- pnm file formats not accepting bitdepth greater than 8 bpp [\#8](https://github.com/uclouvain/openjpeg/issues/8) +- Heap corruption in j2k encoder [\#5](https://github.com/uclouvain/openjpeg/issues/5) +- JPWL crash in marker reallocation\(+patch\), segfault while decoding image with main header protection [\#4](https://github.com/uclouvain/openjpeg/issues/4) +- a couple of small errors in libopenjpeg detected by coverity [\#1](https://github.com/uclouvain/openjpeg/issues/1) + +**Closed issues:** + +- Shared library build broken on ubuntu [\#728](https://github.com/uclouvain/openjpeg/issues/728) +- opj\_includes.h shouldn't define `\_\_attribute\_\_` [\#727](https://github.com/uclouvain/openjpeg/issues/727) +- Possible website problems due to Jekyll upgrade [\#713](https://github.com/uclouvain/openjpeg/issues/713) +- Stable Release? [\#712](https://github.com/uclouvain/openjpeg/issues/712) +- Meta Issue : try to fix some of these critical bugs before thinking about optimizing the library [\#710](https://github.com/uclouvain/openjpeg/issues/710) +- Tiled encoding broken for images with non power of 2 dimensions [\#702](https://github.com/uclouvain/openjpeg/issues/702) +- install\_name \(still\) not set on OS X [\#700](https://github.com/uclouvain/openjpeg/issues/700) +- Add section in wiki describing where one can get test images [\#699](https://github.com/uclouvain/openjpeg/issues/699) +- Make EvenManager into singleton [\#698](https://github.com/uclouvain/openjpeg/issues/698) +- Remove old branches from repo [\#696](https://github.com/uclouvain/openjpeg/issues/696) +- MQ Coder encode: Conditional jump or move depends on uninitialised value\(s\) [\#695](https://github.com/uclouvain/openjpeg/issues/695) +- Can we add these files to our test suite ? [\#688](https://github.com/uclouvain/openjpeg/issues/688) +- -t and -d command line flags for decode are not documented on OpenJPEG website [\#685](https://github.com/uclouvain/openjpeg/issues/685) +- Decoding at the precinct level [\#676](https://github.com/uclouvain/openjpeg/issues/676) +- Support unscaled 10 bit data for 2K cinema @ 48 FPS, as per DCI standard [\#671](https://github.com/uclouvain/openjpeg/issues/671) +- Use parallel jobs in ctest [\#664](https://github.com/uclouvain/openjpeg/issues/664) +- \[Security\]Multiple Memory error [\#663](https://github.com/uclouvain/openjpeg/issues/663) +- lossy encoding a 16 bit TIF file : severe artifacts in decompressed image [\#660](https://github.com/uclouvain/openjpeg/issues/660) +- opj\_compress and opj\_decompress : get\_next\_file method uses hard-coded unix path separator [\#630](https://github.com/uclouvain/openjpeg/issues/630) +- Uninitialized variable [\#629](https://github.com/uclouvain/openjpeg/issues/629) +- Use of enum variable for bit flags prevents compilation as C++ source [\#619](https://github.com/uclouvain/openjpeg/issues/619) +- Serious problem with quantization during lossy encoding [\#615](https://github.com/uclouvain/openjpeg/issues/615) +- Decompression does not work with sequential data source [\#613](https://github.com/uclouvain/openjpeg/issues/613) +- potential overflow in opj\_tcd\_tile\_t [\#605](https://github.com/uclouvain/openjpeg/issues/605) +- Logical condition [\#596](https://github.com/uclouvain/openjpeg/issues/596) +- file9.jp2 does not dump correctly on 1.5 [\#595](https://github.com/uclouvain/openjpeg/issues/595) +- opj\_compress man page is missing documentation of -jpip option [\#593](https://github.com/uclouvain/openjpeg/issues/593) +- opj\_compress fails to compress lossless on gcc/x86 \(-m32\) in 1.5 branch [\#591](https://github.com/uclouvain/openjpeg/issues/591) +- Example: opj\_compress -i image.j2k -o image.pgm [\#577](https://github.com/uclouvain/openjpeg/issues/577) +- Mismatching delete [\#575](https://github.com/uclouvain/openjpeg/issues/575) +- Compilation fails on Win7 [\#546](https://github.com/uclouvain/openjpeg/issues/546) +- NR-JP2-file5.jp2-compare2base fails with third party libcms [\#540](https://github.com/uclouvain/openjpeg/issues/540) +- CTest spits out an error at the end of the test run [\#516](https://github.com/uclouvain/openjpeg/issues/516) +- opj\_uint\_adds\(\) is questionable [\#515](https://github.com/uclouvain/openjpeg/issues/515) +- Might consider renaming this method: [\#491](https://github.com/uclouvain/openjpeg/issues/491) +- opj\_compress run twice gives different fiile sizes for same file [\#490](https://github.com/uclouvain/openjpeg/issues/490) +- Android Support [\#483](https://github.com/uclouvain/openjpeg/issues/483) +- Add SSE2/SSE41 implementations for mct.c [\#451](https://github.com/uclouvain/openjpeg/issues/451) +- Reduce encoder code block memory usage for non 64x64 code block sizes [\#444](https://github.com/uclouvain/openjpeg/issues/444) +- valgrind "Uninitialized Memory Read" & "Uninitialized Memory Conditional" found [\#438](https://github.com/uclouvain/openjpeg/issues/438) +- No way to debug opj\_tcd\_init\_encode\_tile or opj\_tcd\_init\_decode\_tile [\#433](https://github.com/uclouvain/openjpeg/issues/433) +- Add option to call dsymutil on built binaries [\#409](https://github.com/uclouvain/openjpeg/issues/409) +- Allow opj\_compress and opj\_decompress to read/write images over stdin/stdout [\#379](https://github.com/uclouvain/openjpeg/issues/379) +- reduce memory significantly for single tile RGB encoding [\#375](https://github.com/uclouvain/openjpeg/issues/375) +- Switch code repo to github and start using pull request workflow [\#373](https://github.com/uclouvain/openjpeg/issues/373) +- This is a BigTIFF file. This format not supported [\#125](https://github.com/uclouvain/openjpeg/issues/125) +- Add a test suite to check the convert functions [\#99](https://github.com/uclouvain/openjpeg/issues/99) +- Add build config to the dashboard to verify the autotools build [\#88](https://github.com/uclouvain/openjpeg/issues/88) + +**Merged pull requests:** + +- Correct abi-check.sh for PR [\#791](https://github.com/uclouvain/openjpeg/pull/791) ([mayeut](https://github.com/mayeut)) +- Update tcd.c [\#790](https://github.com/uclouvain/openjpeg/pull/790) ([maddin200](https://github.com/maddin200)) +- Update lcms2 [\#773](https://github.com/uclouvain/openjpeg/pull/773) ([mayeut](https://github.com/mayeut)) +- Use lowercase for cmake commands consistently [\#769](https://github.com/uclouvain/openjpeg/pull/769) ([julienmalik](https://github.com/julienmalik)) +- Ignore clang's summary warning [\#768](https://github.com/uclouvain/openjpeg/pull/768) ([julienmalik](https://github.com/julienmalik)) +- Fix UBSan gcc warning for first arg to memset non null [\#767](https://github.com/uclouvain/openjpeg/pull/767) ([julienmalik](https://github.com/julienmalik)) +- Update to libtiff-4.0.6 [\#764](https://github.com/uclouvain/openjpeg/pull/764) ([mayeut](https://github.com/mayeut)) +- Fix warnings [\#763](https://github.com/uclouvain/openjpeg/pull/763) ([mayeut](https://github.com/mayeut)) +- Check SSIZ is valid in opj\_j2k\_read\_siz [\#762](https://github.com/uclouvain/openjpeg/pull/762) ([mayeut](https://github.com/mayeut)) +- Fix unsigned int overflow reported by UBSan [\#761](https://github.com/uclouvain/openjpeg/pull/761) ([mayeut](https://github.com/mayeut)) +- Fix unsigned int overflow reported by UBSan [\#759](https://github.com/uclouvain/openjpeg/pull/759) ([mayeut](https://github.com/mayeut)) +- Fix negative shift left reported by UBSan [\#758](https://github.com/uclouvain/openjpeg/pull/758) ([mayeut](https://github.com/mayeut)) +- Fix negative shift left reported by UBSan [\#757](https://github.com/uclouvain/openjpeg/pull/757) ([mayeut](https://github.com/mayeut)) +- Add clang 3.9 build to Travis matrix [\#753](https://github.com/uclouvain/openjpeg/pull/753) ([julienmalik](https://github.com/julienmalik)) +- Fix implicit floating bool conversion [\#752](https://github.com/uclouvain/openjpeg/pull/752) ([julienmalik](https://github.com/julienmalik)) +- Do not define \_\_attribute\_\_ in opj\_includes.h [\#751](https://github.com/uclouvain/openjpeg/pull/751) ([mayeut](https://github.com/mayeut)) +- Allow to read/write 3/5/7/9/11/13/15 bpp TIF files [\#750](https://github.com/uclouvain/openjpeg/pull/750) ([mayeut](https://github.com/mayeut)) +- Fix heap-buffer-overflow in color\_esycc\_to\_rgb [\#748](https://github.com/uclouvain/openjpeg/pull/748) ([mayeut](https://github.com/mayeut)) +- update libpng to from 1.6.17 to 1.6.21 [\#747](https://github.com/uclouvain/openjpeg/pull/747) ([julienmalik](https://github.com/julienmalik)) +- Update cmake & jpylyzer for travis builds [\#746](https://github.com/uclouvain/openjpeg/pull/746) ([julienmalik](https://github.com/julienmalik)) +- Fix Out-Of-Bounds Read in sycc42x\_to\_rgb function [\#745](https://github.com/uclouvain/openjpeg/pull/745) ([mayeut](https://github.com/mayeut)) +- cppcheck fix for openjp2 [\#740](https://github.com/uclouvain/openjpeg/pull/740) ([julienmalik](https://github.com/julienmalik)) +- Fix uninitialized variable reported by cppcheck [\#735](https://github.com/uclouvain/openjpeg/pull/735) ([julienmalik](https://github.com/julienmalik)) +- Remove dead code in opj\_dump [\#734](https://github.com/uclouvain/openjpeg/pull/734) ([julienmalik](https://github.com/julienmalik)) +- issue \#695 MQ Encode: ensure that bp pointer never points to uninitialized memory [\#708](https://github.com/uclouvain/openjpeg/pull/708) ([boxerab](https://github.com/boxerab)) +- Fix issue 135 [\#706](https://github.com/uclouvain/openjpeg/pull/706) ([mayeut](https://github.com/mayeut)) +- Fix implementation of opj\_calloc [\#705](https://github.com/uclouvain/openjpeg/pull/705) ([stweil](https://github.com/stweil)) +- \[git/2.1 regression\] Fix opj\_write\_tile\(\) failure when numresolutions=1 [\#690](https://github.com/uclouvain/openjpeg/pull/690) ([rouault](https://github.com/rouault)) +- Fix fatal crash on 64 bit Linux [\#687](https://github.com/uclouvain/openjpeg/pull/687) ([stweil](https://github.com/stweil)) +- \[libtiff\] Add missing include statement for ssize\_t [\#686](https://github.com/uclouvain/openjpeg/pull/686) ([mayeut](https://github.com/mayeut)) +- Fix duplicate article in comments [\#684](https://github.com/uclouvain/openjpeg/pull/684) ([stweil](https://github.com/stweil)) +- Fix grammar in comment [\#679](https://github.com/uclouvain/openjpeg/pull/679) ([stweil](https://github.com/stweil)) +- Remove whitespace and CR at line endings [\#678](https://github.com/uclouvain/openjpeg/pull/678) ([stweil](https://github.com/stweil)) +- Fix typos [\#665](https://github.com/uclouvain/openjpeg/pull/665) ([jwilk](https://github.com/jwilk)) +- Add missing source for the JPIP library and executables \(issue \#658\) [\#659](https://github.com/uclouvain/openjpeg/pull/659) ([stweil](https://github.com/stweil)) +- Fix undefined size jp2 box handling [\#654](https://github.com/uclouvain/openjpeg/pull/654) ([mayeut](https://github.com/mayeut)) +- opj\_decompress: Update error message [\#651](https://github.com/uclouvain/openjpeg/pull/651) ([stweil](https://github.com/stweil)) +- Fix support of posix\_memalloc for Linux [\#648](https://github.com/uclouvain/openjpeg/pull/648) ([stweil](https://github.com/stweil)) +- Fix typo in comments [\#647](https://github.com/uclouvain/openjpeg/pull/647) ([stweil](https://github.com/stweil)) +- Avoid pointer arithmetic with \(void \*\) pointers [\#644](https://github.com/uclouvain/openjpeg/pull/644) ([smuehlst](https://github.com/smuehlst)) +- Fix HP compiler warning about redeclaration of function \(\#640\) [\#641](https://github.com/uclouvain/openjpeg/pull/641) ([smuehlst](https://github.com/smuehlst)) +- Fix format strings and unneeded assignment [\#638](https://github.com/uclouvain/openjpeg/pull/638) ([stweil](https://github.com/stweil)) +- Fix repository for JPEG2000 test data [\#637](https://github.com/uclouvain/openjpeg/pull/637) ([stweil](https://github.com/stweil)) +- Update allocation functions [\#636](https://github.com/uclouvain/openjpeg/pull/636) ([mayeut](https://github.com/mayeut)) +- Fix OpenJPEG GitHub issue \#633. [\#634](https://github.com/uclouvain/openjpeg/pull/634) ([smuehlst](https://github.com/smuehlst)) +- travis-ci: Include add ons in matrix [\#632](https://github.com/uclouvain/openjpeg/pull/632) ([mayeut](https://github.com/mayeut)) +- Add Appveyor [\#627](https://github.com/uclouvain/openjpeg/pull/627) ([mayeut](https://github.com/mayeut)) +- Use Travis-ci to run ABI check [\#626](https://github.com/uclouvain/openjpeg/pull/626) ([mayeut](https://github.com/mayeut)) +- Fix warnings for C++ [\#623](https://github.com/uclouvain/openjpeg/pull/623) ([stweil](https://github.com/stweil)) +- Fixed problem that C++ compilation failed because of enum variable. [\#622](https://github.com/uclouvain/openjpeg/pull/622) ([smuehlst](https://github.com/smuehlst)) +- Added missing casts for return values of opj\_malloc\(\)/opj\_calloc\(\). [\#618](https://github.com/uclouvain/openjpeg/pull/618) ([smuehlst](https://github.com/smuehlst)) +- Add check for seek support before trying TPsot==TNsot workaround [\#617](https://github.com/uclouvain/openjpeg/pull/617) ([mayeut](https://github.com/mayeut)) +- Fix some typos found by codespell [\#610](https://github.com/uclouvain/openjpeg/pull/610) ([stweil](https://github.com/stweil)) +- Correct leak in color\_cielab\_to\_rgb [\#590](https://github.com/uclouvain/openjpeg/pull/590) ([mayeut](https://github.com/mayeut)) +- Add Travis-ci build matrix [\#584](https://github.com/uclouvain/openjpeg/pull/584) ([mayeut](https://github.com/mayeut)) +- Correct lossless issue on linux x86 [\#579](https://github.com/uclouvain/openjpeg/pull/579) ([mayeut](https://github.com/mayeut)) +- Travis-ci update [\#578](https://github.com/uclouvain/openjpeg/pull/578) ([mayeut](https://github.com/mayeut)) +- Correct CMake version requirements [\#572](https://github.com/uclouvain/openjpeg/pull/572) ([mayeut](https://github.com/mayeut)) +- Add tests for CMYK/esYCC/CIELab [\#567](https://github.com/uclouvain/openjpeg/pull/567) ([mayeut](https://github.com/mayeut)) +- Add support for CIELab, EYCC and CMYK [\#559](https://github.com/uclouvain/openjpeg/pull/559) ([szukw000](https://github.com/szukw000)) +- Remove printf/fprintf to stdout/stderr throughout openjp2 lib [\#558](https://github.com/uclouvain/openjpeg/pull/558) ([mayeut](https://github.com/mayeut)) +- better -ffast-math handling [\#555](https://github.com/uclouvain/openjpeg/pull/555) ([rdieter](https://github.com/rdieter)) +- Add jpylyzer tests for JP2 compression [\#552](https://github.com/uclouvain/openjpeg/pull/552) ([mayeut](https://github.com/mayeut)) +- Add COC/QCC in main header when needed [\#551](https://github.com/uclouvain/openjpeg/pull/551) ([mayeut](https://github.com/mayeut)) +- Use \_\_emul under msvc x86 for fast 64 = 32 \* 32 [\#550](https://github.com/uclouvain/openjpeg/pull/550) ([mayeut](https://github.com/mayeut)) +- Update convert for PNG output [\#549](https://github.com/uclouvain/openjpeg/pull/549) ([mayeut](https://github.com/mayeut)) +- Remove some warnings when building [\#548](https://github.com/uclouvain/openjpeg/pull/548) ([mayeut](https://github.com/mayeut)) +- Switch to libpng-1.6.17 [\#547](https://github.com/uclouvain/openjpeg/pull/547) ([mayeut](https://github.com/mayeut)) +- Add some missing static keywords [\#545](https://github.com/uclouvain/openjpeg/pull/545) ([mayeut](https://github.com/mayeut)) +- Switch to libcms2 mm2/Little-CMS@0e8234e090d6aab33f90e2eb0296f30aa0705e57 [\#544](https://github.com/uclouvain/openjpeg/pull/544) ([mayeut](https://github.com/mayeut)) +- Prevent overflow when coding 16 bits images [\#543](https://github.com/uclouvain/openjpeg/pull/543) ([mayeut](https://github.com/mayeut)) +- Switch to libcms2-2.6 [\#542](https://github.com/uclouvain/openjpeg/pull/542) ([mayeut](https://github.com/mayeut)) +- Update PNG support [\#538](https://github.com/uclouvain/openjpeg/pull/538) ([mayeut](https://github.com/mayeut)) +- Various Minor fixes [\#537](https://github.com/uclouvain/openjpeg/pull/537) ([mayeut](https://github.com/mayeut)) +- Update TIFF conversion to support more bit depth. [\#535](https://github.com/uclouvain/openjpeg/pull/535) ([mayeut](https://github.com/mayeut)) +- Add checks for odd looking cmap & for cmap outside jp2h box [\#534](https://github.com/uclouvain/openjpeg/pull/534) ([mayeut](https://github.com/mayeut)) +- Refactor opj\_j2k\_read\_ppm & opj\_j2k\_read\_ppt [\#533](https://github.com/uclouvain/openjpeg/pull/533) ([mayeut](https://github.com/mayeut)) +- Add option to force component splitting in imagetopnm [\#531](https://github.com/uclouvain/openjpeg/pull/531) ([mayeut](https://github.com/mayeut)) +- fix Suspicious code in j2k.c \#517 [\#529](https://github.com/uclouvain/openjpeg/pull/529) ([renevanderark](https://github.com/renevanderark)) +- Update zlib to version 1.2.8 [\#528](https://github.com/uclouvain/openjpeg/pull/528) ([mayeut](https://github.com/mayeut)) +- Fix opj\_write\_bytes\_BE \(\#518\) [\#521](https://github.com/uclouvain/openjpeg/pull/521) ([manisandro](https://github.com/manisandro)) +- Correctly decode files with incorrect tile-part header fields \(TPsot==TNsot\) [\#514](https://github.com/uclouvain/openjpeg/pull/514) ([mayeut](https://github.com/mayeut)) +- Fixed typos [\#510](https://github.com/uclouvain/openjpeg/pull/510) ([radarhere](https://github.com/radarhere)) +- Formatted the readme file [\#507](https://github.com/uclouvain/openjpeg/pull/507) ([htmfilho](https://github.com/htmfilho)) + +## [version.2.1](https://github.com/uclouvain/openjpeg/releases/tag/version.2.1) (2014-04-29) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.2.0.1...version.2.1) + +## [version.2.0.1](https://github.com/uclouvain/openjpeg/releases/tag/version.2.0.1) (2014-04-22) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.5.2...version.2.0.1) + +## [version.1.5.2](https://github.com/uclouvain/openjpeg/releases/tag/version.1.5.2) (2014-03-28) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.2.0...version.1.5.2) + +## [version.2.0](https://github.com/uclouvain/openjpeg/releases/tag/version.2.0) (2014-03-28) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.5.1...version.2.0) + +## [version.1.5.1](https://github.com/uclouvain/openjpeg/releases/tag/version.1.5.1) (2012-09-13) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.5...version.1.5.1) + +## [version.1.5](https://github.com/uclouvain/openjpeg/releases/tag/version.1.5) (2012-02-07) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.3...version.1.5) + +## [version.1.3](https://github.com/uclouvain/openjpeg/releases/tag/version.1.3) (2011-07-03) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.4...version.1.3) + +## [version.1.4](https://github.com/uclouvain/openjpeg/releases/tag/version.1.4) (2011-07-03) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.2...version.1.4) + +## [version.1.2](https://github.com/uclouvain/openjpeg/releases/tag/version.1.2) (2007-06-04) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.1...version.1.2) + +## [version.1.1](https://github.com/uclouvain/openjpeg/releases/tag/version.1.1) (2007-01-31) +List of fixed issues and enhancements unavailable, see [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) or [Full Changelog](https://github.com/uclouvain/openjpeg/compare/version.1.0...version.1.1) + + + +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* diff --git a/cpp/3rd_party/openjpeg/CMakeLists.txt b/cpp/3rd_party/openjpeg/CMakeLists.txt new file mode 100644 index 0000000000..c6143b280a --- /dev/null +++ b/cpp/3rd_party/openjpeg/CMakeLists.txt @@ -0,0 +1,109 @@ +cmake_policy(SET CMP0003 NEW) +if(POLICY CMP0042) + cmake_policy(SET CMP0042 NEW) +endif() + +set(OPENJPEG_LIBRARY_NAME openjp2) + +project(openjpeg C) + +#----------------------------------------------------------------------------- +# OPENJPEG version number, useful for packaging and doxygen doc: +set(OPENJPEG_VERSION_MAJOR 2) +set(OPENJPEG_VERSION_MINOR 4) +set(OPENJPEG_VERSION_BUILD 0) +set(OPENJPEG_VERSION + "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}") +set(PACKAGE_VERSION + "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}") + +# Because autotools does not support X.Y notation for SOVERSION, we have to use +# two numbering, one for the openjpeg version and one for openjpeg soversion +# version | soversion +# 1.0 | 0 +# 1.1 | 1 +# 1.2 | 2 +# 1.3 | 3 +# 1.4 | 4 +# 1.5 | 5 +# 1.5.1 | 5 +# 2.0 | 6 +# 2.0.1 | 6 +# 2.1 | 7 +# 2.1.1 | 7 +# 2.1.2 | 7 +# 2.2.0 | 7 +# 2.3.0 | 7 +# 2.3.1 | 7 +# 2.4.0 | 7 +# above is the recommendation by the OPJ team. If you really need to override this default, +# you can specify your own OPENJPEG_SOVERSION at cmake configuration time: +# cmake -DOPENJPEG_SOVERSION:STRING=42 /path/to/openjpeg +if(NOT OPENJPEG_SOVERSION) + set(OPENJPEG_SOVERSION 7) +endif() + +set(OPENJPEG_LIBRARY_PROPERTIES + VERSION "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}" + SOVERSION "${OPENJPEG_SOVERSION}" +) + +set(OPENJPEG_BUILD "openjp2-${OPENJPEG_VERSION}") +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(OPENJPEG_BUILD "${OPENJPEG_BUILD}-debug") +endif() + +message(STATUS "OpenJPEG: VERSION = ${OPENJPEG_VERSION}, BUILD = ${OPENJPEG_BUILD}") + +#----------------------------------------------------------------------------- +# opj_config.h generation (1/2) + +# Check if some include files are provided by the system +# These files are mandatory, so if they are not provided OpenJPEG library can't be built +include(CheckIncludeFile) +macro(ensure_file_include INCLUDE_FILENAME VARIABLE_NAME MANDATORY_STATUS) + check_include_file(${INCLUDE_FILENAME} ${VARIABLE_NAME}) + if(NOT ${VARIABLE_NAME}) + if(${MANDATORY_STATUS}) + message(STATUS "The file '${INCLUDE_FILENAME}' is mandatory for OpenJPEG build, but not found on your system") + return() + else() + message(STATUS "The file '${INCLUDE_FILENAME}' is optional for OpenJPEG build and not found on your system." + " Internal implementation will be used.") + endif() + endif() +endmacro() + +# Allocating Aligned Memory Blocks +include(CheckIncludeFiles) +include(CheckSymbolExists) +#----------------------------------------------------------------------------- +# opj_config.h generation (2/2) +configure_file( + ${CMAKE_CURRENT_LIST_DIR}/openjp2/opj_config.h.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/openjp2/opj_config.h + @ONLY +) + +configure_file( + ${CMAKE_CURRENT_LIST_DIR}/openjp2/opj_config_private.h.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/openjp2/opj_config_private.h + @ONLY +) + +add_subdirectory(openjp2) + +# Setting all necessary variables +set(OPENJPEG_LIBRARIES ${OPENJPEG_LIBRARY_NAME} PARENT_SCOPE) +set(OPENJPEG_VERSION ${OPENJPEG_VERSION} PARENT_SCOPE) +set(OPENJPEG_MAJOR_VERSION ${OPENJPEG_VERSION_MAJOR} PARENT_SCOPE) +set(OPENJPEG_MINOR_VERSION ${OPENJPEG_VERSION_MINOR} PARENT_SCOPE) +set(OPENJPEG_BUILD_VERSION ${OPENJPEG_VERSION_BUILD} PARENT_SCOPE) +get_target_property(_openjpeg_include_dirs ${OPENJPEG_LIBRARY_NAME} INCLUDE_DIRECTORIES) +set(OPENJPEG_INCLUDE_DIRS ${_openjpeg_include_dirs} PARENT_SCOPE) + +# OpenJPEG can't be built only if configuration script doesn't encounter any problem +if(NOT DEFINED CAN_BUILD_OPENJPEG) + # all prerequisites are fulfilled + set(CAN_BUILD_OPENJPEG TRUE PARENT_SCOPE) +endif() diff --git a/cpp/3rd_party/openjpeg/LICENSE b/cpp/3rd_party/openjpeg/LICENSE new file mode 100644 index 0000000000..e8fa41040d --- /dev/null +++ b/cpp/3rd_party/openjpeg/LICENSE @@ -0,0 +1,39 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2003-2009, Francois-Olivier Devaux + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France + * Copyright (c) 2012, CS Systemes d'Information, France + * + * 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. + */ diff --git a/cpp/3rd_party/openjpeg/README.md b/cpp/3rd_party/openjpeg/README.md new file mode 100644 index 0000000000..f703d0eb6b --- /dev/null +++ b/cpp/3rd_party/openjpeg/README.md @@ -0,0 +1,83 @@ + +# OPENJPEG Library and Applications + +## What is OpenJPEG ? + +OpenJPEG is an open-source JPEG 2000 codec written in C language. It has been developed in order to promote the use of [JPEG 2000](http://www.jpeg.org/jpeg2000), a still-image compression standard from the Joint Photographic Experts Group ([JPEG](http://www.jpeg.org)). Since April 2015, it is officially recognized by ISO/IEC and ITU-T as a [JPEG 2000 Reference Software](http://www.itu.int/rec/T-REC-T.804-201504-I!Amd2). + +## Who can use the code ? +[![badge-license]][link-license] + +Anyone. As the OpenJPEG code is released under the [BSD 2-clause "Simplified" License][link-license], anyone can use or modify the code, even for commercial applications. The only restriction is to retain the copyright in the sources or in the binaries documentation. Of course, if you modified the code in a way that might be of interest for other users, you are encouraged to share it (through a [github pull request](https://github.com/uclouvain/openjpeg/pulls) or by filling an [issue](https://github.com/uclouvain/openjpeg/issues)) but this is not a requirement. + +## How to install and use OpenJPEG ? +API Documentation needs a major refactoring. Meanwhile, you can check [installation](https://github.com/uclouvain/openjpeg/wiki/Installation) instructions and [codec documentation](https://github.com/uclouvain/openjpeg/wiki/DocJ2KCodec). + +## Current Status +[![badge-build]][link-build] + +[![badge-msvc-build]][link-msvc-build] + +[![badge-coverity]][link-coverity] + +## Who are the developers ? + +The library is developed and maintained by the Image and Signal Processing Group ([ISPGroup](http://sites.uclouvain.be/ispgroup/)), in the Université catholique de Louvain ([UCL](http://www.uclouvain.be/en-index.html), with the support of the [CNES](https://cnes.fr/), the [CS](http://www.c-s.fr/) company and the [intoPIX](http://www.intopix.com) company. The JPWL module has been developed by the Digital Signal Processing Lab ([DSPLab](http://dsplab.diei.unipg.it/)) of the University of Perugia, Italy ([UNIPG](http://www.unipg.it/)). + +## Details on folders hierarchy + +* src + * lib + * openjp2: contains the sources of the openjp2 library (Part 1 & 2) + * openjpwl: contains the additional sources if you want to build a JPWL-flavoured library. + * openjpip: complete client-server architecture for remote browsing of jpeg 2000 images. + * openjp3d: JP3D implementation + * openmj2: MJ2 implementation + * bin: contains all applications that use the openjpeg library + * common: common files to all applications + * jp2: a basic codec + * mj2: motion jpeg 2000 executables + * jpip: OpenJPIP applications (server and dec server) + * java: a Java client viewer for JPIP + * jp3d: JP3D applications + * tcltk: a test tool for JP3D + * wx + * OPJViewer: gui for displaying j2k files (based on wxWidget) +* wrapping + * java: java jni to use openjpeg in a java program +* thirdparty: thirdparty libraries used by some applications. These libraries will be built only if there are not found on the system. Note that libopenjpeg itself does not have any dependency. +* doc: doxygen documentation setup file and man pages +* tests: configuration files and utilities for the openjpeg test suite. All test images are located in [openjpeg-data](https://github.com/uclouvain/openjpeg-data) repository. +* cmake: cmake related files +* scripts: scripts for developers + +See [LICENSE][link-license] for license and copyright information. + +See [INSTALL](https://github.com/uclouvain/openjpeg/blob/master/INSTALL.md) for installation procedures. + +See [NEWS](https://github.com/uclouvain/openjpeg/blob/master/NEWS.md) for user visible changes in successive releases. + +## API/ABI + +An API/ABI timeline is automatically updated [here][link-api-timeline]. + +OpenJPEG strives to provide a stable API/ABI for your applications. As such it +only exposes a limited subset of its functions. It uses a mechanism of +exporting/hiding functions. If you are unsure which functions you can use in +your applications, you should compile OpenJPEG using something similar to gcc: +`-fvisibility=hidden` compilation flag. +See also: http://gcc.gnu.org/wiki/Visibility + +On windows, MSVC directly supports export/hiding function and as such the only +API available is the one supported by OpenJPEG. + +[comment-license]: https://img.shields.io/github/license/uclouvain/openjpeg.svg "https://img.shields.io/badge/license-BSD--2--Clause-blue.svg" +[badge-license]: https://img.shields.io/badge/license-BSD--2--Clause-blue.svg "BSD 2-clause \"Simplified\" License" +[link-license]: https://github.com/uclouvain/openjpeg/blob/master/LICENSE "BSD 2-clause \"Simplified\" License" +[badge-build]: https://travis-ci.org/uclouvain/openjpeg.svg?branch=master "Build Status" +[link-build]: https://travis-ci.org/uclouvain/openjpeg "Build Status" +[badge-msvc-build]: https://ci.appveyor.com/api/projects/status/github/uclouvain/openjpeg?branch=master&svg=true "Windows Build Status" +[link-msvc-build]: https://ci.appveyor.com/project/detonin/openjpeg/branch/master "Windows Build Status" +[badge-coverity]: https://scan.coverity.com/projects/6383/badge.svg "Coverity Scan Build Status" +[link-coverity]: https://scan.coverity.com/projects/uclouvain-openjpeg "Coverity Scan Build Status" +[link-api-timeline]: http://www.openjpeg.org/abi-check/timeline/openjpeg "OpenJPEG API/ABI timeline" diff --git a/cpp/3rd_party/openjpeg/THANKS.md b/cpp/3rd_party/openjpeg/THANKS.md new file mode 100644 index 0000000000..460eab10ae --- /dev/null +++ b/cpp/3rd_party/openjpeg/THANKS.md @@ -0,0 +1,39 @@ +# OpenJPEG THANKS file + +Many people have contributed to OpenJPEG by reporting problems, suggesting various improvements, +or submitting actual code. Here is a list of these people. Help me keep +it complete and exempt of errors. + +* Giuseppe Baruffa +* Ben Boeckel +* Aaron Boxer +* David Burken +* Matthieu Darbois +* Rex Dieter +* Herve Drolon +* Antonin Descampe +* Francois-Olivier Devaux +* Parvatha Elangovan +* Jerôme Fimes +* Bob Friesenhahn +* Kaori Hagihara +* Luc Hermitte +* Luis Ibanez +* David Janssens +* Hans Johnson +* Callum Lerwick +* Ke Liu (Tencent's Xuanwu LAB) +* Sebastien Lugan +* Benoit Macq +* Mathieu Malaterre +* Julien Malik +* Arnaud Maye +* Vincent Nicolas +* Aleksander Nikolic (Cisco Talos) +* Glenn Pearson +* Even Rouault +* Dzonatas Sol +* Winfried Szukalski +* Vincent Torri +* Yannick Verschueren +* Peter Wimmer diff --git a/cpp/3rd_party/openjpeg/openjp2/CMakeLists.txt b/cpp/3rd_party/openjpeg/openjp2/CMakeLists.txt new file mode 100644 index 0000000000..8d0182d707 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/CMakeLists.txt @@ -0,0 +1,79 @@ +# Defines the source code for the library +set(OPENJPEG_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/thread.c + ${CMAKE_CURRENT_SOURCE_DIR}/bio.c + ${CMAKE_CURRENT_SOURCE_DIR}/cio.c + ${CMAKE_CURRENT_SOURCE_DIR}/dwt.c + ${CMAKE_CURRENT_SOURCE_DIR}/event.c + ${CMAKE_CURRENT_SOURCE_DIR}/image.c + ${CMAKE_CURRENT_SOURCE_DIR}/invert.c + ${CMAKE_CURRENT_SOURCE_DIR}/j2k.c + ${CMAKE_CURRENT_SOURCE_DIR}/jp2.c + ${CMAKE_CURRENT_SOURCE_DIR}/mct.c + ${CMAKE_CURRENT_SOURCE_DIR}/mqc.c + ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg.c + ${CMAKE_CURRENT_SOURCE_DIR}/opj_clock.c + ${CMAKE_CURRENT_SOURCE_DIR}/pi.c + ${CMAKE_CURRENT_SOURCE_DIR}/t1.c + ${CMAKE_CURRENT_SOURCE_DIR}/t2.c + ${CMAKE_CURRENT_SOURCE_DIR}/tcd.c + ${CMAKE_CURRENT_SOURCE_DIR}/tgt.c + ${CMAKE_CURRENT_SOURCE_DIR}/function_list.c + ${CMAKE_CURRENT_SOURCE_DIR}/opj_malloc.c + ${CMAKE_CURRENT_SOURCE_DIR}/sparse_array.c +) + +option(OPJ_DISABLE_TPSOT_FIX "Disable TPsot==TNsot fix. See https://github.com/uclouvain/openjpeg/issues/254." OFF) +if(OPJ_DISABLE_TPSOT_FIX) + add_definitions(-DOPJ_DISABLE_TPSOT_FIX) +endif() + +add_library(${OPENJPEG_LIBRARY_NAME} STATIC ${OPENJPEG_SRCS}) + +target_compile_definitions(${OPENJPEG_LIBRARY_NAME} PUBLIC OPJ_STATIC) + +target_compile_options(${TIFF_LIBRARY} PRIVATE "-w") + +include_directories("${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_CURRENT_BINARY_DIR}") + +if(UNIX) + target_link_libraries(${OPENJPEG_LIBRARY_NAME} PRIVATE m) +endif() + +set_target_properties(${OPENJPEG_LIBRARY_NAME} + PROPERTIES + ${OPENJPEG_LIBRARY_PROPERTIES} +) + +################################################################################# +# threading configuration +################################################################################# + +option(OPJ_USE_THREAD "Build with thread/mutex support " ON) +if(NOT OPJ_USE_THREAD) + add_definitions(-DMUTEX_stub) +endif() + +find_package(Threads QUIET) + +if(OPJ_USE_THREAD AND WIN32 AND NOT Threads_FOUND ) + add_definitions(-DMUTEX_win32) + set(Threads_FOUND YES) +endif() + +if(OPJ_USE_THREAD AND Threads_FOUND AND CMAKE_USE_WIN32_THREADS_INIT) + add_definitions(-DMUTEX_win32) +endif() + +if(OPJ_USE_THREAD AND Threads_FOUND AND CMAKE_USE_PTHREADS_INIT ) + add_definitions(-DMUTEX_pthread) +endif() + +if(OPJ_USE_THREAD AND NOT Threads_FOUND) + message(STATUS "No thread library found and thread/mutex support is required by OPJ_USE_THREAD option") + set(CAN_BUILD_OPENJPEG FALSE PARENT_SCOPE) +endif() + +if(OPJ_USE_THREAD AND Threads_FOUND AND CMAKE_USE_PTHREADS_INIT) + target_link_libraries(${OPENJPEG_LIBRARY_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT}) +endif() diff --git a/cpp/3rd_party/openjpeg/openjp2/bio.c b/cpp/3rd_party/openjpeg/openjp2/bio.c new file mode 100644 index 0000000000..09dcd7f524 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/bio.c @@ -0,0 +1,217 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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" + +/** @defgroup BIO BIO - Individual bit input-output stream */ +/*@{*/ + +/** @name Local static functions */ +/*@{*/ + +/** +Write a bit +@param bio BIO handle +@param b Bit to write (0 or 1) +*/ +static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b); +/** +Read a bit +@param bio BIO handle +@return Returns the read bit +*/ +static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio); +/** +Write a byte +@param bio BIO handle +@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise +*/ +static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio); +/** +Read a byte +@param bio BIO handle +@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise +*/ +static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio); + +/*@}*/ + +/*@}*/ + +/* +========================================================== + local functions +========================================================== +*/ + +static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio) +{ + bio->buf = (bio->buf << 8) & 0xffff; + bio->ct = bio->buf == 0xff00 ? 7 : 8; + if ((OPJ_SIZE_T)bio->bp >= (OPJ_SIZE_T)bio->end) { + return OPJ_FALSE; + } + *bio->bp++ = (OPJ_BYTE)(bio->buf >> 8); + return OPJ_TRUE; +} + +static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio) +{ + bio->buf = (bio->buf << 8) & 0xffff; + bio->ct = bio->buf == 0xff00 ? 7 : 8; + if ((OPJ_SIZE_T)bio->bp >= (OPJ_SIZE_T)bio->end) { + return OPJ_FALSE; + } + bio->buf |= *bio->bp++; + return OPJ_TRUE; +} + +static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b) +{ + if (bio->ct == 0) { + opj_bio_byteout( + bio); /* MSD: why not check the return value of this function ? */ + } + bio->ct--; + bio->buf |= b << bio->ct; +} + +static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio) +{ + if (bio->ct == 0) { + opj_bio_bytein( + bio); /* MSD: why not check the return value of this function ? */ + } + bio->ct--; + return (bio->buf >> bio->ct) & 1; +} + +/* +========================================================== + Bit Input/Output interface +========================================================== +*/ + +opj_bio_t* opj_bio_create(void) +{ + opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t)); + return bio; +} + +void opj_bio_destroy(opj_bio_t *bio) +{ + if (bio) { + opj_free(bio); + } +} + +ptrdiff_t opj_bio_numbytes(opj_bio_t *bio) +{ + return (bio->bp - bio->start); +} + +void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) +{ + bio->start = bp; + bio->end = bp + len; + bio->bp = bp; + bio->buf = 0; + bio->ct = 8; +} + +void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) +{ + bio->start = bp; + bio->end = bp + len; + bio->bp = bp; + bio->buf = 0; + bio->ct = 0; +} + +void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n) +{ + OPJ_INT32 i; + + assert((n > 0U) && (n <= 32U)); + for (i = (OPJ_INT32)n - 1; i >= 0; i--) { + opj_bio_putbit(bio, (v >> i) & 1); + } +} + +OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n) +{ + OPJ_INT32 i; + OPJ_UINT32 v; + + assert((n > 0U) /* && (n <= 32U)*/); +#ifdef OPJ_UBSAN_BUILD + /* This assert fails for some corrupted images which are gracefully rejected */ + /* Add this assert only for ubsan build. */ + /* This is the condition for overflow not to occur below which is needed because of OPJ_NOSANITIZE */ + assert(n <= 32U); +#endif + v = 0U; + for (i = (OPJ_INT32)n - 1; i >= 0; i--) { + v |= opj_bio_getbit(bio) << + i; /* can't overflow, opj_bio_getbit returns 0 or 1 */ + } + return v; +} + +OPJ_BOOL opj_bio_flush(opj_bio_t *bio) +{ + if (! opj_bio_byteout(bio)) { + return OPJ_FALSE; + } + if (bio->ct == 7) { + if (! opj_bio_byteout(bio)) { + return OPJ_FALSE; + } + } + return OPJ_TRUE; +} + +OPJ_BOOL opj_bio_inalign(opj_bio_t *bio) +{ + if ((bio->buf & 0xff) == 0xff) { + if (! opj_bio_bytein(bio)) { + return OPJ_FALSE; + } + } + bio->ct = 0; + return OPJ_TRUE; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/bio.h b/cpp/3rd_party/openjpeg/openjp2/bio.h new file mode 100644 index 0000000000..448fdda219 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/bio.h @@ -0,0 +1,134 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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 OPJ_BIO_H +#define OPJ_BIO_H + +#include /* ptrdiff_t */ + +/** +@file bio.h +@brief Implementation of an individual bit input-output (BIO) + +The functions in BIO.C have for goal to realize an individual bit input - output. +*/ + +/** @defgroup BIO BIO - Individual bit input-output stream */ +/*@{*/ + +/** +Individual bit input-output stream (BIO) +*/ +typedef struct opj_bio { + /** pointer to the start of the buffer */ + OPJ_BYTE *start; + /** pointer to the end of the buffer */ + OPJ_BYTE *end; + /** pointer to the present position in the buffer */ + OPJ_BYTE *bp; + /** temporary place where each byte is read or written */ + OPJ_UINT32 buf; + /** coder : number of bits free to write. decoder : number of bits read */ + OPJ_UINT32 ct; +} opj_bio_t; + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ +/** +Create a new BIO handle +@return Returns a new BIO handle if successful, returns NULL otherwise +*/ +opj_bio_t* opj_bio_create(void); +/** +Destroy a previously created BIO handle +@param bio BIO handle to destroy +*/ +void opj_bio_destroy(opj_bio_t *bio); +/** +Number of bytes written. +@param bio BIO handle +@return Returns the number of bytes written +*/ +ptrdiff_t opj_bio_numbytes(opj_bio_t *bio); +/** +Init encoder +@param bio BIO handle +@param bp Output buffer +@param len Output buffer length +*/ +void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len); +/** +Init decoder +@param bio BIO handle +@param bp Input buffer +@param len Input buffer length +*/ +void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len); +/** +Write bits +@param bio BIO handle +@param v Value of bits +@param n Number of bits to write +*/ +void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n); +/** +Read bits +@param bio BIO handle +@param n Number of bits to read +@return Returns the corresponding read number +*/ +OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n); +/** +Flush bits +@param bio BIO handle +@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise +*/ +OPJ_BOOL opj_bio_flush(opj_bio_t *bio); +/** +Passes the ending bits (coming from flushing) +@param bio BIO handle +@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise +*/ +OPJ_BOOL opj_bio_inalign(opj_bio_t *bio); +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_BIO_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/cio.c b/cpp/3rd_party/openjpeg/openjp2/cio.c new file mode 100644 index 0000000000..4fde9fe239 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/cio.c @@ -0,0 +1,683 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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" + +/* ----------------------------------------------------------------------- */ + + +/* ----------------------------------------------------------------------- */ + +void opj_write_bytes_BE(OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, + OPJ_UINT32 p_nb_bytes) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof( + OPJ_UINT32) - p_nb_bytes; + + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + + memcpy(p_buffer, l_data_ptr, p_nb_bytes); +} + +void opj_write_bytes_LE(OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, + OPJ_UINT32 p_nb_bytes) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1; + OPJ_UINT32 i; + + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + + for (i = 0; i < p_nb_bytes; ++i) { + *(p_buffer++) = *(l_data_ptr--); + } +} + +void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, + OPJ_UINT32 p_nb_bytes) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value); + + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + + *p_value = 0; + memcpy(l_data_ptr + sizeof(OPJ_UINT32) - p_nb_bytes, p_buffer, p_nb_bytes); +} + +void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, + OPJ_UINT32 p_nb_bytes) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes - 1; + OPJ_UINT32 i; + + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + + *p_value = 0; + for (i = 0; i < p_nb_bytes; ++i) { + *(l_data_ptr--) = *(p_buffer++); + } +} + +void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value); + memcpy(p_buffer, l_data_ptr, sizeof(OPJ_FLOAT64)); +} + +void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof( + OPJ_FLOAT64) - 1; + OPJ_UINT32 i; + for (i = 0; i < sizeof(OPJ_FLOAT64); ++i) { + *(p_buffer++) = *(l_data_ptr--); + } +} + +void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value); + memcpy(l_data_ptr, p_buffer, sizeof(OPJ_FLOAT64)); +} + +void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT64) - 1; + OPJ_UINT32 i; + for (i = 0; i < sizeof(OPJ_FLOAT64); ++i) { + *(l_data_ptr--) = *(p_buffer++); + } +} + +void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value); + memcpy(p_buffer, l_data_ptr, sizeof(OPJ_FLOAT32)); +} + +void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof( + OPJ_FLOAT32) - 1; + OPJ_UINT32 i; + for (i = 0; i < sizeof(OPJ_FLOAT32); ++i) { + *(p_buffer++) = *(l_data_ptr--); + } +} + +void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value); + memcpy(l_data_ptr, p_buffer, sizeof(OPJ_FLOAT32)); +} + +void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT32) - 1; + OPJ_UINT32 i; + for (i = 0; i < sizeof(OPJ_FLOAT32); ++i) { + *(l_data_ptr--) = *(p_buffer++); + } +} + +opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size, + OPJ_BOOL l_is_input) +{ + opj_stream_private_t * l_stream = 00; + l_stream = (opj_stream_private_t*) opj_calloc(1, sizeof(opj_stream_private_t)); + if (! l_stream) { + return 00; + } + + l_stream->m_buffer_size = p_buffer_size; + l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_buffer_size); + if (! l_stream->m_stored_data) { + opj_free(l_stream); + return 00; + } + + l_stream->m_current_data = l_stream->m_stored_data; + + if (l_is_input) { + l_stream->m_status |= OPJ_STREAM_STATUS_INPUT; + l_stream->m_opj_skip = opj_stream_read_skip; + l_stream->m_opj_seek = opj_stream_read_seek; + } else { + l_stream->m_status |= OPJ_STREAM_STATUS_OUTPUT; + l_stream->m_opj_skip = opj_stream_write_skip; + l_stream->m_opj_seek = opj_stream_write_seek; + } + + l_stream->m_read_fn = opj_stream_default_read; + l_stream->m_write_fn = opj_stream_default_write; + l_stream->m_skip_fn = opj_stream_default_skip; + l_stream->m_seek_fn = opj_stream_default_seek; + + return (opj_stream_t *) l_stream; +} + +opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL l_is_input) +{ + return opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, l_is_input); +} + +void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + + if (l_stream) { + if (l_stream->m_free_user_data_fn) { + l_stream->m_free_user_data_fn(l_stream->m_user_data); + } + opj_free(l_stream->m_stored_data); + l_stream->m_stored_data = 00; + opj_free(l_stream); + } +} + +void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, + opj_stream_read_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + + if ((!l_stream) || (!(l_stream->m_status & OPJ_STREAM_STATUS_INPUT))) { + return; + } + + l_stream->m_read_fn = p_function; +} + +void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, + opj_stream_seek_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + + if (!l_stream) { + return; + } + l_stream->m_seek_fn = p_function; +} + +void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, + opj_stream_write_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + + if ((!l_stream) || (!(l_stream->m_status & OPJ_STREAM_STATUS_OUTPUT))) { + return; + } + + l_stream->m_write_fn = p_function; +} + +void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, + opj_stream_skip_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + + if (! l_stream) { + return; + } + + l_stream->m_skip_fn = p_function; +} + +void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, + void * p_data, opj_stream_free_user_data_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if (!l_stream) { + return; + } + l_stream->m_user_data = p_data; + l_stream->m_free_user_data_fn = p_function; +} + +void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, + OPJ_UINT64 data_length) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if (!l_stream) { + return; + } + l_stream->m_user_data_length = data_length; +} + +OPJ_SIZE_T opj_stream_read_data(opj_stream_private_t * p_stream, + OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr) +{ + OPJ_SIZE_T l_read_nb_bytes = 0; + if (p_stream->m_bytes_in_buffer >= p_size) { + memcpy(p_buffer, p_stream->m_current_data, p_size); + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer -= p_size; + l_read_nb_bytes += p_size; + p_stream->m_byte_offset += (OPJ_OFF_T)p_size; + return l_read_nb_bytes; + } + + /* we are now in the case when the remaining data if not sufficient */ + if (p_stream->m_status & OPJ_STREAM_STATUS_END) { + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + memcpy(p_buffer, p_stream->m_current_data, p_stream->m_bytes_in_buffer); + p_stream->m_current_data += p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T) - 1; + } + + /* the flag is not set, we copy data and then do an actual read on the stream */ + if (p_stream->m_bytes_in_buffer) { + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + memcpy(p_buffer, p_stream->m_current_data, p_stream->m_bytes_in_buffer); + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += p_stream->m_bytes_in_buffer; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } else { + /* case where we are already at the end of the buffer + so reset the m_current_data to point to the start of the + stored buffer to get ready to read from disk*/ + p_stream->m_current_data = p_stream->m_stored_data; + } + + for (;;) { + /* we should read less than a chunk -> read a chunk */ + if (p_size < p_stream->m_buffer_size) { + /* we should do an actual read on the media */ + p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_stream->m_stored_data, + p_stream->m_buffer_size, p_stream->m_user_data); + + if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T) - 1) { + /* end of stream */ + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + + p_stream->m_bytes_in_buffer = 0; + p_stream->m_status |= OPJ_STREAM_STATUS_END; + /* end of stream */ + return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T) - 1; + } else if (p_stream->m_bytes_in_buffer < p_size) { + /* not enough data */ + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + memcpy(p_buffer, p_stream->m_current_data, p_stream->m_bytes_in_buffer); + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += p_stream->m_bytes_in_buffer; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } else { + l_read_nb_bytes += p_size; + memcpy(p_buffer, p_stream->m_current_data, p_size); + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer -= p_size; + p_stream->m_byte_offset += (OPJ_OFF_T)p_size; + return l_read_nb_bytes; + } + } else { + /* direct read on the dest buffer */ + p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_buffer, p_size, + p_stream->m_user_data); + + if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T) - 1) { + /* end of stream */ + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + + p_stream->m_bytes_in_buffer = 0; + p_stream->m_status |= OPJ_STREAM_STATUS_END; + /* end of stream */ + return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T) - 1; + } else if (p_stream->m_bytes_in_buffer < p_size) { + /* not enough data */ + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += p_stream->m_bytes_in_buffer; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } else { + /* we have read the exact size */ + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_current_data = p_stream->m_stored_data; + p_stream->m_bytes_in_buffer = 0; + return l_read_nb_bytes; + } + } + } +} + +OPJ_SIZE_T opj_stream_write_data(opj_stream_private_t * p_stream, + const OPJ_BYTE * p_buffer, + OPJ_SIZE_T p_size, + opj_event_mgr_t * p_event_mgr) +{ + OPJ_SIZE_T l_remaining_bytes = 0; + OPJ_SIZE_T l_write_nb_bytes = 0; + + if (p_stream->m_status & OPJ_STREAM_STATUS_ERROR) { + return (OPJ_SIZE_T) - 1; + } + + for (;;) { + l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer; + + /* we have more memory than required */ + if (l_remaining_bytes >= p_size) { + memcpy(p_stream->m_current_data, p_buffer, p_size); + + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer += p_size; + l_write_nb_bytes += p_size; + p_stream->m_byte_offset += (OPJ_OFF_T)p_size; + + return l_write_nb_bytes; + } + + /* we copy data and then do an actual read on the stream */ + if (l_remaining_bytes) { + l_write_nb_bytes += l_remaining_bytes; + + memcpy(p_stream->m_current_data, p_buffer, l_remaining_bytes); + + p_stream->m_current_data = p_stream->m_stored_data; + + p_buffer += l_remaining_bytes; + p_size -= l_remaining_bytes; + p_stream->m_bytes_in_buffer += l_remaining_bytes; + p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes; + } + + if (! opj_stream_flush(p_stream, p_event_mgr)) { + return (OPJ_SIZE_T) - 1; + } + } + +} + +OPJ_BOOL opj_stream_flush(opj_stream_private_t * p_stream, + opj_event_mgr_t * p_event_mgr) +{ + /* the number of bytes written on the media. */ + OPJ_SIZE_T l_current_write_nb_bytes = 0; + + p_stream->m_current_data = p_stream->m_stored_data; + + while (p_stream->m_bytes_in_buffer) { + /* we should do an actual write on the media */ + l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data, + p_stream->m_bytes_in_buffer, + p_stream->m_user_data); + + if (l_current_write_nb_bytes == (OPJ_SIZE_T) - 1) { + p_stream->m_status |= OPJ_STREAM_STATUS_ERROR; + opj_event_msg(p_event_mgr, EVT_INFO, "Error on writing stream!\n"); + + return OPJ_FALSE; + } + + p_stream->m_current_data += l_current_write_nb_bytes; + p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes; + } + + p_stream->m_current_data = p_stream->m_stored_data; + + return OPJ_TRUE; +} + +OPJ_OFF_T opj_stream_read_skip(opj_stream_private_t * p_stream, + OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr) +{ + OPJ_OFF_T l_skip_nb_bytes = 0; + OPJ_OFF_T l_current_skip_nb_bytes = 0; + + assert(p_size >= 0); + + if (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size) { + p_stream->m_current_data += p_size; + /* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer + which is of type OPJ_SIZE_T */ + p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size; + l_skip_nb_bytes += p_size; + p_stream->m_byte_offset += l_skip_nb_bytes; + return l_skip_nb_bytes; + } + + /* we are now in the case when the remaining data if not sufficient */ + if (p_stream->m_status & OPJ_STREAM_STATUS_END) { + l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_current_data += p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + p_stream->m_byte_offset += l_skip_nb_bytes; + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) - 1; + } + + /* the flag is not set, we copy data and then do an actual skip on the stream */ + if (p_stream->m_bytes_in_buffer) { + l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_current_data = p_stream->m_stored_data; + p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } + + while (p_size > 0) { + /* Check if we are going beyond the end of file. Most skip_fn do not */ + /* check that, but we must be careful not to advance m_byte_offset */ + /* beyond m_user_data_length, otherwise */ + /* opj_stream_get_number_byte_left() will assert. */ + if ((OPJ_UINT64)(p_stream->m_byte_offset + l_skip_nb_bytes + p_size) > + p_stream->m_user_data_length) { + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + + p_stream->m_byte_offset += l_skip_nb_bytes; + l_skip_nb_bytes = (OPJ_OFF_T)(p_stream->m_user_data_length - + (OPJ_UINT64)p_stream->m_byte_offset); + + opj_stream_read_seek(p_stream, (OPJ_OFF_T)p_stream->m_user_data_length, + p_event_mgr); + p_stream->m_status |= OPJ_STREAM_STATUS_END; + + /* end if stream */ + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) - 1; + } + + /* we should do an actual skip on the media */ + l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data); + if (l_current_skip_nb_bytes == (OPJ_OFF_T) - 1) { + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + + p_stream->m_status |= OPJ_STREAM_STATUS_END; + p_stream->m_byte_offset += l_skip_nb_bytes; + /* end if stream */ + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) - 1; + } + p_size -= l_current_skip_nb_bytes; + l_skip_nb_bytes += l_current_skip_nb_bytes; + } + + p_stream->m_byte_offset += l_skip_nb_bytes; + + return l_skip_nb_bytes; +} + +OPJ_OFF_T opj_stream_write_skip(opj_stream_private_t * p_stream, + OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr) +{ + OPJ_BOOL l_is_written = 0; + OPJ_OFF_T l_current_skip_nb_bytes = 0; + OPJ_OFF_T l_skip_nb_bytes = 0; + + if (p_stream->m_status & OPJ_STREAM_STATUS_ERROR) { + return (OPJ_OFF_T) - 1; + } + + /* we should flush data */ + l_is_written = opj_stream_flush(p_stream, p_event_mgr); + if (! l_is_written) { + p_stream->m_status |= OPJ_STREAM_STATUS_ERROR; + p_stream->m_bytes_in_buffer = 0; + return (OPJ_OFF_T) - 1; + } + /* then skip */ + + while (p_size > 0) { + /* we should do an actual skip on the media */ + l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data); + + if (l_current_skip_nb_bytes == (OPJ_OFF_T) - 1) { + opj_event_msg(p_event_mgr, EVT_INFO, "Stream error!\n"); + + p_stream->m_status |= OPJ_STREAM_STATUS_ERROR; + p_stream->m_byte_offset += l_skip_nb_bytes; + /* end if stream */ + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) - 1; + } + p_size -= l_current_skip_nb_bytes; + l_skip_nb_bytes += l_current_skip_nb_bytes; + } + + p_stream->m_byte_offset += l_skip_nb_bytes; + + return l_skip_nb_bytes; +} + +OPJ_OFF_T opj_stream_tell(const opj_stream_private_t * p_stream) +{ + return p_stream->m_byte_offset; +} + +OPJ_OFF_T opj_stream_get_number_byte_left(const opj_stream_private_t * p_stream) +{ + assert(p_stream->m_byte_offset >= 0); + assert(p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset); + return p_stream->m_user_data_length ? + (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset : + 0; +} + +OPJ_OFF_T opj_stream_skip(opj_stream_private_t * p_stream, OPJ_OFF_T p_size, + opj_event_mgr_t * p_event_mgr) +{ + assert(p_size >= 0); + return p_stream->m_opj_skip(p_stream, p_size, p_event_mgr); +} + +OPJ_BOOL opj_stream_read_seek(opj_stream_private_t * p_stream, OPJ_OFF_T p_size, + opj_event_mgr_t * p_event_mgr) +{ + OPJ_ARG_NOT_USED(p_event_mgr); + p_stream->m_current_data = p_stream->m_stored_data; + p_stream->m_bytes_in_buffer = 0; + + if (!(p_stream->m_seek_fn(p_size, p_stream->m_user_data))) { + p_stream->m_status |= OPJ_STREAM_STATUS_END; + return OPJ_FALSE; + } else { + /* reset stream status */ + p_stream->m_status &= (~OPJ_STREAM_STATUS_END); + p_stream->m_byte_offset = p_size; + + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_stream_write_seek(opj_stream_private_t * p_stream, + OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr) +{ + if (! opj_stream_flush(p_stream, p_event_mgr)) { + p_stream->m_status |= OPJ_STREAM_STATUS_ERROR; + return OPJ_FALSE; + } + + p_stream->m_current_data = p_stream->m_stored_data; + p_stream->m_bytes_in_buffer = 0; + + if (! p_stream->m_seek_fn(p_size, p_stream->m_user_data)) { + p_stream->m_status |= OPJ_STREAM_STATUS_ERROR; + return OPJ_FALSE; + } else { + p_stream->m_byte_offset = p_size; + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_stream_seek(opj_stream_private_t * p_stream, OPJ_OFF_T p_size, + struct opj_event_mgr * p_event_mgr) +{ + assert(p_size >= 0); + return p_stream->m_opj_seek(p_stream, p_size, p_event_mgr); +} + +OPJ_BOOL opj_stream_has_seek(const opj_stream_private_t * p_stream) +{ + return p_stream->m_seek_fn != opj_stream_default_seek; +} + +OPJ_SIZE_T opj_stream_default_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes, + void * p_user_data) +{ + OPJ_ARG_NOT_USED(p_buffer); + OPJ_ARG_NOT_USED(p_nb_bytes); + OPJ_ARG_NOT_USED(p_user_data); + return (OPJ_SIZE_T) - 1; +} + +OPJ_SIZE_T opj_stream_default_write(void * p_buffer, OPJ_SIZE_T p_nb_bytes, + void * p_user_data) +{ + OPJ_ARG_NOT_USED(p_buffer); + OPJ_ARG_NOT_USED(p_nb_bytes); + OPJ_ARG_NOT_USED(p_user_data); + return (OPJ_SIZE_T) - 1; +} + +OPJ_OFF_T opj_stream_default_skip(OPJ_OFF_T p_nb_bytes, void * p_user_data) +{ + OPJ_ARG_NOT_USED(p_nb_bytes); + OPJ_ARG_NOT_USED(p_user_data); + return (OPJ_OFF_T) - 1; +} + +OPJ_BOOL opj_stream_default_seek(OPJ_OFF_T p_nb_bytes, void * p_user_data) +{ + OPJ_ARG_NOT_USED(p_nb_bytes); + OPJ_ARG_NOT_USED(p_user_data); + return OPJ_FALSE; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/cio.h b/cpp/3rd_party/openjpeg/openjp2/cio.h new file mode 100644 index 0000000000..6996a9a0a1 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/cio.h @@ -0,0 +1,412 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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 OPJ_CIO_H +#define OPJ_CIO_H +/** +@file cio.h +@brief Implementation of a byte input-output process (CIO) + +The functions in CIO.C have for goal to realize a byte input / output process. +*/ + +/** @defgroup CIO CIO - byte input-output stream */ +/*@{*/ + +#include "opj_config_private.h" + +/* ----------------------------------------------------------------------- */ + +#if defined(OPJ_BIG_ENDIAN) +#define opj_write_bytes opj_write_bytes_BE +#define opj_read_bytes opj_read_bytes_BE +#define opj_write_double opj_write_double_BE +#define opj_read_double opj_read_double_BE +#define opj_write_float opj_write_float_BE +#define opj_read_float opj_read_float_BE +#else +#define opj_write_bytes opj_write_bytes_LE +#define opj_read_bytes opj_read_bytes_LE +#define opj_write_double opj_write_double_LE +#define opj_read_double opj_read_double_LE +#define opj_write_float opj_write_float_LE +#define opj_read_float opj_read_float_LE +#endif + + +#define OPJ_STREAM_STATUS_OUTPUT 0x1U +#define OPJ_STREAM_STATUS_INPUT 0x2U +#define OPJ_STREAM_STATUS_END 0x4U +#define OPJ_STREAM_STATUS_ERROR 0x8U + +/** +Byte input-output stream. +*/ +typedef struct opj_stream_private { + /** + * User data, be it files, ... The actual data depends on the type of the stream. + */ + void * m_user_data; + + /** + * Pointer to function to free m_user_data (NULL at initialization) + * when destroying the stream. If pointer is NULL the function is not + * called and the m_user_data is not freed (even if non-NULL). + */ + opj_stream_free_user_data_fn m_free_user_data_fn; + + /** + * User data length + */ + OPJ_UINT64 m_user_data_length; + + /** + * Pointer to actual read function (NULL at the initialization of the cio. + */ + opj_stream_read_fn m_read_fn; + + /** + * Pointer to actual write function (NULL at the initialization of the cio. + */ + opj_stream_write_fn m_write_fn; + + /** + * Pointer to actual skip function (NULL at the initialization of the cio. + * There is no seek function to prevent from back and forth slow procedures. + */ + opj_stream_skip_fn m_skip_fn; + + /** + * Pointer to actual seek function (if available). + */ + opj_stream_seek_fn m_seek_fn; + + /** + * Actual data stored into the stream if readed from. Data is read by chunk of fixed size. + * you should never access this data directly. + */ + OPJ_BYTE * m_stored_data; + + /** + * Pointer to the current read data. + */ + OPJ_BYTE * m_current_data; + + /** + * FIXME DOC. + */ + OPJ_OFF_T(* m_opj_skip)(struct opj_stream_private *, OPJ_OFF_T, + struct opj_event_mgr *); + + /** + * FIXME DOC. + */ + OPJ_BOOL(* m_opj_seek)(struct opj_stream_private *, OPJ_OFF_T, + struct opj_event_mgr *); + + /** + * number of bytes containing in the buffer. + */ + OPJ_SIZE_T m_bytes_in_buffer; + + /** + * The number of bytes read/written from the beginning of the stream + */ + OPJ_OFF_T m_byte_offset; + + /** + * The size of the buffer. + */ + OPJ_SIZE_T m_buffer_size; + + /** + * Flags to tell the status of the stream. + * Used with OPJ_STREAM_STATUS_* defines. + */ + OPJ_UINT32 m_status; + +} +opj_stream_private_t; + +/** @name Exported functions (see also openjpeg.h) */ +/*@{*/ +/* ----------------------------------------------------------------------- */ +/** + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + * @param p_nb_bytes the number of bytes to write +*/ +void opj_write_bytes_BE(OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, + OPJ_UINT32 p_nb_bytes); + +/** + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + * @param p_nb_bytes the nb bytes to read. + * @return the number of bytes read or -1 if an error occurred. + */ +void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, + OPJ_UINT32 p_nb_bytes); + +/** + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + * @param p_nb_bytes the number of bytes to write + * @return the number of bytes written or -1 if an error occurred +*/ +void opj_write_bytes_LE(OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, + OPJ_UINT32 p_nb_bytes); + +/** + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + * @param p_nb_bytes the nb bytes to read. + * @return the number of bytes read or -1 if an error occurred. + */ +void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, + OPJ_UINT32 p_nb_bytes); + + +/** + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value); + +/*** + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value); + +/** + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value); + +/*** + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value); + +/** + * Reads some bytes from the stream. + * @param p_stream the stream to read data from. + * @param p_buffer pointer to the data buffer that will receive the data. + * @param p_size number of bytes to read. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes read, or -1 if an error occurred or if the stream is at the end. + */ +OPJ_SIZE_T opj_stream_read_data(opj_stream_private_t * p_stream, + OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Writes some bytes to the stream. + * @param p_stream the stream to write data to. + * @param p_buffer pointer to the data buffer holds the data to be writtent. + * @param p_size number of bytes to write. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes writtent, or -1 if an error occurred. + */ +OPJ_SIZE_T opj_stream_write_data(opj_stream_private_t * p_stream, + const OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, + struct opj_event_mgr * p_event_mgr); + +/** + * Writes the content of the stream buffer to the stream. + * @param p_stream the stream to write data to. + * @param p_event_mgr the user event manager to be notified of special events. + * @return true if the data could be flushed, false else. + */ +OPJ_BOOL opj_stream_flush(opj_stream_private_t * p_stream, + struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occurred. + */ +OPJ_OFF_T opj_stream_skip(opj_stream_private_t * p_stream, OPJ_OFF_T p_size, + struct opj_event_mgr * p_event_mgr); + +/** + * Tells the byte offset on the stream (similar to ftell). + * + * @param p_stream the stream to get the information from. + * + * @return the current position o fthe stream. + */ +OPJ_OFF_T opj_stream_tell(const opj_stream_private_t * p_stream); + + +/** + * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft). + * + * @param p_stream the stream to get the information from. + * + * @return Number of bytes left before the end of the stream. + */ +OPJ_OFF_T opj_stream_get_number_byte_left(const opj_stream_private_t * + p_stream); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occurred. + */ +OPJ_OFF_T opj_stream_write_skip(opj_stream_private_t * p_stream, + OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occurred. + */ +OPJ_OFF_T opj_stream_read_skip(opj_stream_private_t * p_stream, + OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return OPJ_TRUE if success, or OPJ_FALSE if an error occurred. + */ +OPJ_BOOL opj_stream_read_seek(opj_stream_private_t * p_stream, OPJ_OFF_T p_size, + struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occurred. + */ +OPJ_BOOL opj_stream_write_seek(opj_stream_private_t * p_stream, + OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Seeks a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return true if the stream is seekable. + */ +OPJ_BOOL opj_stream_seek(opj_stream_private_t * p_stream, OPJ_OFF_T p_size, + struct opj_event_mgr * p_event_mgr); + +/** + * Tells if the given stream is seekable. + */ +OPJ_BOOL opj_stream_has_seek(const opj_stream_private_t * p_stream); + +/** + * FIXME DOC. + */ +OPJ_SIZE_T opj_stream_default_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes, + void * p_user_data); + +/** + * FIXME DOC. + */ +OPJ_SIZE_T opj_stream_default_write(void * p_buffer, OPJ_SIZE_T p_nb_bytes, + void * p_user_data); + +/** + * FIXME DOC. + */ +OPJ_OFF_T opj_stream_default_skip(OPJ_OFF_T p_nb_bytes, void * p_user_data); + +/** + * FIXME DOC. + */ +OPJ_BOOL opj_stream_default_seek(OPJ_OFF_T p_nb_bytes, void * p_user_data); + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + + +#endif /* OPJ_CIO_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/dwt.c b/cpp/3rd_party/openjpeg/openjp2/dwt.c new file mode 100644 index 0000000000..4164ba090e --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/dwt.c @@ -0,0 +1,3756 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2007, Jonathan Ballard + * Copyright (c) 2007, Callum Lerwick + * Copyright (c) 2017, IntoPIX SA + * 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 + +#define OPJ_SKIP_POISON +#include "opj_includes.h" + +#ifdef __SSE__ +#include +#endif +#ifdef __SSE2__ +#include +#endif +#ifdef __SSSE3__ +#include +#endif +#ifdef __AVX2__ +#include +#endif + +#if defined(__GNUC__) +#pragma GCC poison malloc calloc realloc free +#endif + +/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ +/*@{*/ + +#define OPJ_WS(i) v->mem[(i)*2] +#define OPJ_WD(i) v->mem[(1+(i)*2)] + +#ifdef __AVX2__ +/** Number of int32 values in a AVX2 register */ +#define VREG_INT_COUNT 8 +#else +/** Number of int32 values in a SSE2 register */ +#define VREG_INT_COUNT 4 +#endif + +/** Number of columns that we can process in parallel in the vertical pass */ +#define PARALLEL_COLS_53 (2*VREG_INT_COUNT) + +/** @name Local data structures */ +/*@{*/ + +typedef struct dwt_local { + OPJ_INT32* mem; + OPJ_INT32 dn; /* number of elements in high pass band */ + OPJ_INT32 sn; /* number of elements in low pass band */ + OPJ_INT32 cas; /* 0 = start on even coord, 1 = start on odd coord */ +} opj_dwt_t; + +#define NB_ELTS_V8 8 + +typedef union { + OPJ_FLOAT32 f[NB_ELTS_V8]; +} opj_v8_t; + +typedef struct v8dwt_local { + opj_v8_t* wavelet ; + OPJ_INT32 dn ; /* number of elements in high pass band */ + OPJ_INT32 sn ; /* number of elements in low pass band */ + OPJ_INT32 cas ; /* 0 = start on even coord, 1 = start on odd coord */ + OPJ_UINT32 win_l_x0; /* start coord in low pass band */ + OPJ_UINT32 win_l_x1; /* end coord in low pass band */ + OPJ_UINT32 win_h_x0; /* start coord in high pass band */ + OPJ_UINT32 win_h_x1; /* end coord in high pass band */ +} opj_v8dwt_t ; + +/* From table F.4 from the standard */ +static const OPJ_FLOAT32 opj_dwt_alpha = -1.586134342f; +static const OPJ_FLOAT32 opj_dwt_beta = -0.052980118f; +static const OPJ_FLOAT32 opj_dwt_gamma = 0.882911075f; +static const OPJ_FLOAT32 opj_dwt_delta = 0.443506852f; + +static const OPJ_FLOAT32 opj_K = 1.230174105f; +static const OPJ_FLOAT32 opj_invK = (OPJ_FLOAT32)(1.0 / 1.230174105); + +/*@}*/ + +/** @name Local static functions */ +/*@{*/ + +/** +Forward lazy transform (horizontal) +*/ +static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a, + OPJ_INT32 * OPJ_RESTRICT b, + OPJ_INT32 dn, + OPJ_INT32 sn, OPJ_INT32 cas); + +/** +Forward 9-7 wavelet transform in 1-D +*/ +static void opj_dwt_encode_1_real(void *a, OPJ_INT32 dn, OPJ_INT32 sn, + OPJ_INT32 cas); +/** +Explicit calculation of the Quantization Stepsizes +*/ +static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, + opj_stepsize_t *bandno_stepsize); +/** +Inverse wavelet transform in 2-D. +*/ +static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp, + opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i); + +static OPJ_BOOL opj_dwt_decode_partial_tile( + opj_tcd_tilecomp_t* tilec, + OPJ_UINT32 numres); + +/* Forward transform, for the vertical pass, processing cols columns */ +/* where cols <= NB_ELTS_V8 */ +/* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */ +typedef void (*opj_encode_and_deinterleave_v_fnptr_type)( + void *array, + void *tmp, + OPJ_UINT32 height, + OPJ_BOOL even, + OPJ_UINT32 stride_width, + OPJ_UINT32 cols); + +/* Where void* is a OPJ_INT32* for 5x3 and OPJ_FLOAT32* for 9x7 */ +typedef void (*opj_encode_and_deinterleave_h_one_row_fnptr_type)( + void *row, + void *tmp, + OPJ_UINT32 width, + OPJ_BOOL even); + +static OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp, + opj_tcd_tilecomp_t * tilec, + opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v, + opj_encode_and_deinterleave_h_one_row_fnptr_type + p_encode_and_deinterleave_h_one_row); + +static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r, + OPJ_UINT32 i); + +/* */ +/* Inverse 9-7 wavelet transform in 1-D. */ +/* */ + +/*@}*/ + +/*@}*/ + +#define OPJ_S(i) a[(i)*2] +#define OPJ_D(i) a[(1+(i)*2)] +#define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i))) +#define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i))) +/* new */ +#define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i))) +#define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i))) + +/* */ +/* This table contains the norms of the 5-3 wavelets for different bands. */ +/* */ +/* FIXME! the array should really be extended up to 33 resolution levels */ +/* See https://github.com/uclouvain/openjpeg/issues/493 */ +static const OPJ_FLOAT64 opj_dwt_norms[4][10] = { + {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, + {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, + {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, + {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93} +}; + +/* */ +/* This table contains the norms of the 9-7 wavelets for different bands. */ +/* */ +/* FIXME! the array should really be extended up to 33 resolution levels */ +/* See https://github.com/uclouvain/openjpeg/issues/493 */ +static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = { + {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, + {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, + {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, + {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2} +}; + +/* +========================================================== + local functions +========================================================== +*/ + +/* */ +/* Forward lazy transform (horizontal). */ +/* */ +static void opj_dwt_deinterleave_h(const OPJ_INT32 * OPJ_RESTRICT a, + OPJ_INT32 * OPJ_RESTRICT b, + OPJ_INT32 dn, + OPJ_INT32 sn, OPJ_INT32 cas) +{ + OPJ_INT32 i; + OPJ_INT32 * OPJ_RESTRICT l_dest = b; + const OPJ_INT32 * OPJ_RESTRICT l_src = a + cas; + + for (i = 0; i < sn; ++i) { + *l_dest++ = *l_src; + l_src += 2; + } + + l_dest = b + sn; + l_src = a + 1 - cas; + + for (i = 0; i < dn; ++i) { + *l_dest++ = *l_src; + l_src += 2; + } +} + +#ifdef STANDARD_SLOW_VERSION +/* */ +/* Inverse lazy transform (horizontal). */ +/* */ +static void opj_dwt_interleave_h(const opj_dwt_t* h, OPJ_INT32 *a) +{ + const OPJ_INT32 *ai = a; + OPJ_INT32 *bi = h->mem + h->cas; + OPJ_INT32 i = h->sn; + while (i--) { + *bi = *(ai++); + bi += 2; + } + ai = a + h->sn; + bi = h->mem + 1 - h->cas; + i = h->dn ; + while (i--) { + *bi = *(ai++); + bi += 2; + } +} + +/* */ +/* Inverse lazy transform (vertical). */ +/* */ +static void opj_dwt_interleave_v(const opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) +{ + const OPJ_INT32 *ai = a; + OPJ_INT32 *bi = v->mem + v->cas; + OPJ_INT32 i = v->sn; + while (i--) { + *bi = *ai; + bi += 2; + ai += x; + } + ai = a + (v->sn * (OPJ_SIZE_T)x); + bi = v->mem + 1 - v->cas; + i = v->dn ; + while (i--) { + *bi = *ai; + bi += 2; + ai += x; + } +} + +#endif /* STANDARD_SLOW_VERSION */ + +#ifdef STANDARD_SLOW_VERSION +/* */ +/* Inverse 5-3 wavelet transform in 1-D. */ +/* */ +static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, + OPJ_INT32 cas) +{ + OPJ_INT32 i; + + if (!cas) { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + for (i = 0; i < sn; i++) { + OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2; + } + for (i = 0; i < dn; i++) { + OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1; + } + } + } else { + if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */ + OPJ_S(0) /= 2; + } else { + for (i = 0; i < sn; i++) { + OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2; + } + for (i = 0; i < dn; i++) { + OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1; + } + } + } +} + +static void opj_dwt_decode_1(const opj_dwt_t *v) +{ + opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas); +} + +#endif /* STANDARD_SLOW_VERSION */ + +#if !defined(STANDARD_SLOW_VERSION) +static void opj_idwt53_h_cas0(OPJ_INT32* tmp, + const OPJ_INT32 sn, + const OPJ_INT32 len, + OPJ_INT32* tiledp) +{ + OPJ_INT32 i, j; + const OPJ_INT32* in_even = &tiledp[0]; + const OPJ_INT32* in_odd = &tiledp[sn]; + +#ifdef TWO_PASS_VERSION + /* For documentation purpose: performs lifting in two iterations, */ + /* but without explicit interleaving */ + + assert(len > 1); + + /* Even */ + tmp[0] = in_even[0] - ((in_odd[0] + 1) >> 1); + for (i = 2, j = 0; i <= len - 2; i += 2, j++) { + tmp[i] = in_even[j + 1] - ((in_odd[j] + in_odd[j + 1] + 2) >> 2); + } + if (len & 1) { /* if len is odd */ + tmp[len - 1] = in_even[(len - 1) / 2] - ((in_odd[(len - 2) / 2] + 1) >> 1); + } + + /* Odd */ + for (i = 1, j = 0; i < len - 1; i += 2, j++) { + tmp[i] = in_odd[j] + ((tmp[i - 1] + tmp[i + 1]) >> 1); + } + if (!(len & 1)) { /* if len is even */ + tmp[len - 1] = in_odd[(len - 1) / 2] + tmp[len - 2]; + } +#else + OPJ_INT32 d1c, d1n, s1n, s0c, s0n; + + assert(len > 1); + + /* Improved version of the TWO_PASS_VERSION: */ + /* Performs lifting in one single iteration. Saves memory */ + /* accesses and explicit interleaving. */ + s1n = in_even[0]; + d1n = in_odd[0]; + s0n = s1n - ((d1n + 1) >> 1); + + for (i = 0, j = 1; i < (len - 3); i += 2, j++) { + d1c = d1n; + s0c = s0n; + + s1n = in_even[j]; + d1n = in_odd[j]; + + s0n = s1n - ((d1c + d1n + 2) >> 2); + + tmp[i ] = s0c; + tmp[i + 1] = d1c + ((s0c + s0n) >> 1); + } + + tmp[i] = s0n; + + if (len & 1) { + tmp[len - 1] = in_even[(len - 1) / 2] - ((d1n + 1) >> 1); + tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1); + } else { + tmp[len - 1] = d1n + s0n; + } +#endif + memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32)); +} + +static void opj_idwt53_h_cas1(OPJ_INT32* tmp, + const OPJ_INT32 sn, + const OPJ_INT32 len, + OPJ_INT32* tiledp) +{ + OPJ_INT32 i, j; + const OPJ_INT32* in_even = &tiledp[sn]; + const OPJ_INT32* in_odd = &tiledp[0]; + +#ifdef TWO_PASS_VERSION + /* For documentation purpose: performs lifting in two iterations, */ + /* but without explicit interleaving */ + + assert(len > 2); + + /* Odd */ + for (i = 1, j = 0; i < len - 1; i += 2, j++) { + tmp[i] = in_odd[j] - ((in_even[j] + in_even[j + 1] + 2) >> 2); + } + if (!(len & 1)) { + tmp[len - 1] = in_odd[len / 2 - 1] - ((in_even[len / 2 - 1] + 1) >> 1); + } + + /* Even */ + tmp[0] = in_even[0] + tmp[1]; + for (i = 2, j = 1; i < len - 1; i += 2, j++) { + tmp[i] = in_even[j] + ((tmp[i + 1] + tmp[i - 1]) >> 1); + } + if (len & 1) { + tmp[len - 1] = in_even[len / 2] + tmp[len - 2]; + } +#else + OPJ_INT32 s1, s2, dc, dn; + + assert(len > 2); + + /* Improved version of the TWO_PASS_VERSION: */ + /* Performs lifting in one single iteration. Saves memory */ + /* accesses and explicit interleaving. */ + + s1 = in_even[1]; + dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2); + tmp[0] = in_even[0] + dc; + + for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) { + + s2 = in_even[j + 1]; + + dn = in_odd[j] - ((s1 + s2 + 2) >> 2); + tmp[i ] = dc; + tmp[i + 1] = s1 + ((dn + dc) >> 1); + + dc = dn; + s1 = s2; + } + + tmp[i] = dc; + + if (!(len & 1)) { + dn = in_odd[len / 2 - 1] - ((s1 + 1) >> 1); + tmp[len - 2] = s1 + ((dn + dc) >> 1); + tmp[len - 1] = dn; + } else { + tmp[len - 1] = s1 + dc; + } +#endif + memcpy(tiledp, tmp, (OPJ_UINT32)len * sizeof(OPJ_INT32)); +} + + +#endif /* !defined(STANDARD_SLOW_VERSION) */ + +/* */ +/* Inverse 5-3 wavelet transform in 1-D for one row. */ +/* */ +/* Performs interleave, inverse wavelet transform and copy back to buffer */ +static void opj_idwt53_h(const opj_dwt_t *dwt, + OPJ_INT32* tiledp) +{ +#ifdef STANDARD_SLOW_VERSION + /* For documentation purpose */ + opj_dwt_interleave_h(dwt, tiledp); + opj_dwt_decode_1(dwt); + memcpy(tiledp, dwt->mem, (OPJ_UINT32)(dwt->sn + dwt->dn) * sizeof(OPJ_INT32)); +#else + const OPJ_INT32 sn = dwt->sn; + const OPJ_INT32 len = sn + dwt->dn; + if (dwt->cas == 0) { /* Left-most sample is on even coordinate */ + if (len > 1) { + opj_idwt53_h_cas0(dwt->mem, sn, len, tiledp); + } else { + /* Unmodified value */ + } + } else { /* Left-most sample is on odd coordinate */ + if (len == 1) { + tiledp[0] /= 2; + } else if (len == 2) { + OPJ_INT32* out = dwt->mem; + const OPJ_INT32* in_even = &tiledp[sn]; + const OPJ_INT32* in_odd = &tiledp[0]; + out[1] = in_odd[0] - ((in_even[0] + 1) >> 1); + out[0] = in_even[0] + out[1]; + memcpy(tiledp, dwt->mem, (OPJ_UINT32)len * sizeof(OPJ_INT32)); + } else if (len > 2) { + opj_idwt53_h_cas1(dwt->mem, sn, len, tiledp); + } + } +#endif +} + +#if (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) + +/* Conveniency macros to improve the readabilty of the formulas */ +#if __AVX2__ +#define VREG __m256i +#define LOAD_CST(x) _mm256_set1_epi32(x) +#define LOAD(x) _mm256_load_si256((const VREG*)(x)) +#define LOADU(x) _mm256_loadu_si256((const VREG*)(x)) +#define STORE(x,y) _mm256_store_si256((VREG*)(x),(y)) +#define STOREU(x,y) _mm256_storeu_si256((VREG*)(x),(y)) +#define ADD(x,y) _mm256_add_epi32((x),(y)) +#define SUB(x,y) _mm256_sub_epi32((x),(y)) +#define SAR(x,y) _mm256_srai_epi32((x),(y)) +#else +#define VREG __m128i +#define LOAD_CST(x) _mm_set1_epi32(x) +#define LOAD(x) _mm_load_si128((const VREG*)(x)) +#define LOADU(x) _mm_loadu_si128((const VREG*)(x)) +#define STORE(x,y) _mm_store_si128((VREG*)(x),(y)) +#define STOREU(x,y) _mm_storeu_si128((VREG*)(x),(y)) +#define ADD(x,y) _mm_add_epi32((x),(y)) +#define SUB(x,y) _mm_sub_epi32((x),(y)) +#define SAR(x,y) _mm_srai_epi32((x),(y)) +#endif +#define ADD3(x,y,z) ADD(ADD(x,y),z) + +static +void opj_idwt53_v_final_memcpy(OPJ_INT32* tiledp_col, + const OPJ_INT32* tmp, + OPJ_INT32 len, + OPJ_SIZE_T stride) +{ + OPJ_INT32 i; + for (i = 0; i < len; ++i) { + /* A memcpy(&tiledp_col[i * stride + 0], + &tmp[PARALLEL_COLS_53 * i + 0], + PARALLEL_COLS_53 * sizeof(OPJ_INT32)) + would do but would be a tiny bit slower. + We can take here advantage of our knowledge of alignment */ + STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + 0], + LOAD(&tmp[PARALLEL_COLS_53 * i + 0])); + STOREU(&tiledp_col[(OPJ_SIZE_T)i * stride + VREG_INT_COUNT], + LOAD(&tmp[PARALLEL_COLS_53 * i + VREG_INT_COUNT])); + } +} + +/** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or + * 16 in AVX2, when top-most pixel is on even coordinate */ +static void opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2( + OPJ_INT32* tmp, + const OPJ_INT32 sn, + const OPJ_INT32 len, + OPJ_INT32* tiledp_col, + const OPJ_SIZE_T stride) +{ + const OPJ_INT32* in_even = &tiledp_col[0]; + const OPJ_INT32* in_odd = &tiledp_col[(OPJ_SIZE_T)sn * stride]; + + OPJ_INT32 i; + OPJ_SIZE_T j; + VREG d1c_0, d1n_0, s1n_0, s0c_0, s0n_0; + VREG d1c_1, d1n_1, s1n_1, s0c_1, s0n_1; + const VREG two = LOAD_CST(2); + + assert(len > 1); +#if __AVX2__ + assert(PARALLEL_COLS_53 == 16); + assert(VREG_INT_COUNT == 8); +#else + assert(PARALLEL_COLS_53 == 8); + assert(VREG_INT_COUNT == 4); +#endif + + /* Note: loads of input even/odd values must be done in a unaligned */ + /* fashion. But stores in tmp can be done with aligned store, since */ + /* the temporary buffer is properly aligned */ + assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0); + + s1n_0 = LOADU(in_even + 0); + s1n_1 = LOADU(in_even + VREG_INT_COUNT); + d1n_0 = LOADU(in_odd); + d1n_1 = LOADU(in_odd + VREG_INT_COUNT); + + /* s0n = s1n - ((d1n + 1) >> 1); <==> */ + /* s0n = s1n - ((d1n + d1n + 2) >> 2); */ + s0n_0 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2)); + s0n_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2)); + + for (i = 0, j = 1; i < (len - 3); i += 2, j++) { + d1c_0 = d1n_0; + s0c_0 = s0n_0; + d1c_1 = d1n_1; + s0c_1 = s0n_1; + + s1n_0 = LOADU(in_even + j * stride); + s1n_1 = LOADU(in_even + j * stride + VREG_INT_COUNT); + d1n_0 = LOADU(in_odd + j * stride); + d1n_1 = LOADU(in_odd + j * stride + VREG_INT_COUNT); + + /*s0n = s1n - ((d1c + d1n + 2) >> 2);*/ + s0n_0 = SUB(s1n_0, SAR(ADD3(d1c_0, d1n_0, two), 2)); + s0n_1 = SUB(s1n_1, SAR(ADD3(d1c_1, d1n_1, two), 2)); + + STORE(tmp + PARALLEL_COLS_53 * (i + 0), s0c_0); + STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0c_1); + + /* d1c + ((s0c + s0n) >> 1) */ + STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0, + ADD(d1c_0, SAR(ADD(s0c_0, s0n_0), 1))); + STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT, + ADD(d1c_1, SAR(ADD(s0c_1, s0n_1), 1))); + } + + STORE(tmp + PARALLEL_COLS_53 * (i + 0) + 0, s0n_0); + STORE(tmp + PARALLEL_COLS_53 * (i + 0) + VREG_INT_COUNT, s0n_1); + + if (len & 1) { + VREG tmp_len_minus_1; + s1n_0 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride); + /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */ + tmp_len_minus_1 = SUB(s1n_0, SAR(ADD3(d1n_0, d1n_0, two), 2)); + STORE(tmp + PARALLEL_COLS_53 * (len - 1), tmp_len_minus_1); + /* d1n + ((s0n + tmp_len_minus_1) >> 1) */ + STORE(tmp + PARALLEL_COLS_53 * (len - 2), + ADD(d1n_0, SAR(ADD(s0n_0, tmp_len_minus_1), 1))); + + s1n_1 = LOADU(in_even + (OPJ_SIZE_T)((len - 1) / 2) * stride + VREG_INT_COUNT); + /* tmp_len_minus_1 = s1n - ((d1n + 1) >> 1); */ + tmp_len_minus_1 = SUB(s1n_1, SAR(ADD3(d1n_1, d1n_1, two), 2)); + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, + tmp_len_minus_1); + /* d1n + ((s0n + tmp_len_minus_1) >> 1) */ + STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT, + ADD(d1n_1, SAR(ADD(s0n_1, tmp_len_minus_1), 1))); + + + } else { + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, + ADD(d1n_0, s0n_0)); + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, + ADD(d1n_1, s0n_1)); + } + + opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride); +} + + +/** Vertical inverse 5x3 wavelet transform for 8 columns in SSE2, or + * 16 in AVX2, when top-most pixel is on odd coordinate */ +static void opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2( + OPJ_INT32* tmp, + const OPJ_INT32 sn, + const OPJ_INT32 len, + OPJ_INT32* tiledp_col, + const OPJ_SIZE_T stride) +{ + OPJ_INT32 i; + OPJ_SIZE_T j; + + VREG s1_0, s2_0, dc_0, dn_0; + VREG s1_1, s2_1, dc_1, dn_1; + const VREG two = LOAD_CST(2); + + const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride]; + const OPJ_INT32* in_odd = &tiledp_col[0]; + + assert(len > 2); +#if __AVX2__ + assert(PARALLEL_COLS_53 == 16); + assert(VREG_INT_COUNT == 8); +#else + assert(PARALLEL_COLS_53 == 8); + assert(VREG_INT_COUNT == 4); +#endif + + /* Note: loads of input even/odd values must be done in a unaligned */ + /* fashion. But stores in tmp can be done with aligned store, since */ + /* the temporary buffer is properly aligned */ + assert((OPJ_SIZE_T)tmp % (sizeof(OPJ_INT32) * VREG_INT_COUNT) == 0); + + s1_0 = LOADU(in_even + stride); + /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */ + dc_0 = SUB(LOADU(in_odd + 0), + SAR(ADD3(LOADU(in_even + 0), s1_0, two), 2)); + STORE(tmp + PARALLEL_COLS_53 * 0, ADD(LOADU(in_even + 0), dc_0)); + + s1_1 = LOADU(in_even + stride + VREG_INT_COUNT); + /* in_odd[0] - ((in_even[0] + s1 + 2) >> 2); */ + dc_1 = SUB(LOADU(in_odd + VREG_INT_COUNT), + SAR(ADD3(LOADU(in_even + VREG_INT_COUNT), s1_1, two), 2)); + STORE(tmp + PARALLEL_COLS_53 * 0 + VREG_INT_COUNT, + ADD(LOADU(in_even + VREG_INT_COUNT), dc_1)); + + for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) { + + s2_0 = LOADU(in_even + (j + 1) * stride); + s2_1 = LOADU(in_even + (j + 1) * stride + VREG_INT_COUNT); + + /* dn = in_odd[j * stride] - ((s1 + s2 + 2) >> 2); */ + dn_0 = SUB(LOADU(in_odd + j * stride), + SAR(ADD3(s1_0, s2_0, two), 2)); + dn_1 = SUB(LOADU(in_odd + j * stride + VREG_INT_COUNT), + SAR(ADD3(s1_1, s2_1, two), 2)); + + STORE(tmp + PARALLEL_COLS_53 * i, dc_0); + STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1); + + /* tmp[i + 1] = s1 + ((dn + dc) >> 1); */ + STORE(tmp + PARALLEL_COLS_53 * (i + 1) + 0, + ADD(s1_0, SAR(ADD(dn_0, dc_0), 1))); + STORE(tmp + PARALLEL_COLS_53 * (i + 1) + VREG_INT_COUNT, + ADD(s1_1, SAR(ADD(dn_1, dc_1), 1))); + + dc_0 = dn_0; + s1_0 = s2_0; + dc_1 = dn_1; + s1_1 = s2_1; + } + STORE(tmp + PARALLEL_COLS_53 * i, dc_0); + STORE(tmp + PARALLEL_COLS_53 * i + VREG_INT_COUNT, dc_1); + + if (!(len & 1)) { + /*dn = in_odd[(len / 2 - 1) * stride] - ((s1 + 1) >> 1); */ + dn_0 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride), + SAR(ADD3(s1_0, s1_0, two), 2)); + dn_1 = SUB(LOADU(in_odd + (OPJ_SIZE_T)(len / 2 - 1) * stride + VREG_INT_COUNT), + SAR(ADD3(s1_1, s1_1, two), 2)); + + /* tmp[len - 2] = s1 + ((dn + dc) >> 1); */ + STORE(tmp + PARALLEL_COLS_53 * (len - 2) + 0, + ADD(s1_0, SAR(ADD(dn_0, dc_0), 1))); + STORE(tmp + PARALLEL_COLS_53 * (len - 2) + VREG_INT_COUNT, + ADD(s1_1, SAR(ADD(dn_1, dc_1), 1))); + + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, dn_0); + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, dn_1); + } else { + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + 0, ADD(s1_0, dc_0)); + STORE(tmp + PARALLEL_COLS_53 * (len - 1) + VREG_INT_COUNT, + ADD(s1_1, dc_1)); + } + + opj_idwt53_v_final_memcpy(tiledp_col, tmp, len, stride); +} + +#undef VREG +#undef LOAD_CST +#undef LOADU +#undef LOAD +#undef STORE +#undef STOREU +#undef ADD +#undef ADD3 +#undef SUB +#undef SAR + +#endif /* (defined(__SSE2__) || defined(__AVX2__)) && !defined(STANDARD_SLOW_VERSION) */ + +#if !defined(STANDARD_SLOW_VERSION) +/** Vertical inverse 5x3 wavelet transform for one column, when top-most + * pixel is on even coordinate */ +static void opj_idwt3_v_cas0(OPJ_INT32* tmp, + const OPJ_INT32 sn, + const OPJ_INT32 len, + OPJ_INT32* tiledp_col, + const OPJ_SIZE_T stride) +{ + OPJ_INT32 i, j; + OPJ_INT32 d1c, d1n, s1n, s0c, s0n; + + assert(len > 1); + + /* Performs lifting in one single iteration. Saves memory */ + /* accesses and explicit interleaving. */ + + s1n = tiledp_col[0]; + d1n = tiledp_col[(OPJ_SIZE_T)sn * stride]; + s0n = s1n - ((d1n + 1) >> 1); + + for (i = 0, j = 0; i < (len - 3); i += 2, j++) { + d1c = d1n; + s0c = s0n; + + s1n = tiledp_col[(OPJ_SIZE_T)(j + 1) * stride]; + d1n = tiledp_col[(OPJ_SIZE_T)(sn + j + 1) * stride]; + + s0n = s1n - ((d1c + d1n + 2) >> 2); + + tmp[i ] = s0c; + tmp[i + 1] = d1c + ((s0c + s0n) >> 1); + } + + tmp[i] = s0n; + + if (len & 1) { + tmp[len - 1] = + tiledp_col[(OPJ_SIZE_T)((len - 1) / 2) * stride] - + ((d1n + 1) >> 1); + tmp[len - 2] = d1n + ((s0n + tmp[len - 1]) >> 1); + } else { + tmp[len - 1] = d1n + s0n; + } + + for (i = 0; i < len; ++i) { + tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i]; + } +} + + +/** Vertical inverse 5x3 wavelet transform for one column, when top-most + * pixel is on odd coordinate */ +static void opj_idwt3_v_cas1(OPJ_INT32* tmp, + const OPJ_INT32 sn, + const OPJ_INT32 len, + OPJ_INT32* tiledp_col, + const OPJ_SIZE_T stride) +{ + OPJ_INT32 i, j; + OPJ_INT32 s1, s2, dc, dn; + const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride]; + const OPJ_INT32* in_odd = &tiledp_col[0]; + + assert(len > 2); + + /* Performs lifting in one single iteration. Saves memory */ + /* accesses and explicit interleaving. */ + + s1 = in_even[stride]; + dc = in_odd[0] - ((in_even[0] + s1 + 2) >> 2); + tmp[0] = in_even[0] + dc; + for (i = 1, j = 1; i < (len - 2 - !(len & 1)); i += 2, j++) { + + s2 = in_even[(OPJ_SIZE_T)(j + 1) * stride]; + + dn = in_odd[(OPJ_SIZE_T)j * stride] - ((s1 + s2 + 2) >> 2); + tmp[i ] = dc; + tmp[i + 1] = s1 + ((dn + dc) >> 1); + + dc = dn; + s1 = s2; + } + tmp[i] = dc; + if (!(len & 1)) { + dn = in_odd[(OPJ_SIZE_T)(len / 2 - 1) * stride] - ((s1 + 1) >> 1); + tmp[len - 2] = s1 + ((dn + dc) >> 1); + tmp[len - 1] = dn; + } else { + tmp[len - 1] = s1 + dc; + } + + for (i = 0; i < len; ++i) { + tiledp_col[(OPJ_SIZE_T)i * stride] = tmp[i]; + } +} +#endif /* !defined(STANDARD_SLOW_VERSION) */ + +/* */ +/* Inverse vertical 5-3 wavelet transform in 1-D for several columns. */ +/* */ +/* Performs interleave, inverse wavelet transform and copy back to buffer */ +static void opj_idwt53_v(const opj_dwt_t *dwt, + OPJ_INT32* tiledp_col, + OPJ_SIZE_T stride, + OPJ_INT32 nb_cols) +{ +#ifdef STANDARD_SLOW_VERSION + /* For documentation purpose */ + OPJ_INT32 k, c; + for (c = 0; c < nb_cols; c ++) { + opj_dwt_interleave_v(dwt, tiledp_col + c, stride); + opj_dwt_decode_1(dwt); + for (k = 0; k < dwt->sn + dwt->dn; ++k) { + tiledp_col[c + k * stride] = dwt->mem[k]; + } + } +#else + const OPJ_INT32 sn = dwt->sn; + const OPJ_INT32 len = sn + dwt->dn; + if (dwt->cas == 0) { + /* If len == 1, unmodified value */ + +#if (defined(__SSE2__) || defined(__AVX2__)) + if (len > 1 && nb_cols == PARALLEL_COLS_53) { + /* Same as below general case, except that thanks to SSE2/AVX2 */ + /* we can efficiently process 8/16 columns in parallel */ + opj_idwt53_v_cas0_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride); + return; + } +#endif + if (len > 1) { + OPJ_INT32 c; + for (c = 0; c < nb_cols; c++, tiledp_col++) { + opj_idwt3_v_cas0(dwt->mem, sn, len, tiledp_col, stride); + } + return; + } + } else { + if (len == 1) { + OPJ_INT32 c; + for (c = 0; c < nb_cols; c++, tiledp_col++) { + tiledp_col[0] /= 2; + } + return; + } + + if (len == 2) { + OPJ_INT32 c; + OPJ_INT32* out = dwt->mem; + for (c = 0; c < nb_cols; c++, tiledp_col++) { + OPJ_INT32 i; + const OPJ_INT32* in_even = &tiledp_col[(OPJ_SIZE_T)sn * stride]; + const OPJ_INT32* in_odd = &tiledp_col[0]; + + out[1] = in_odd[0] - ((in_even[0] + 1) >> 1); + out[0] = in_even[0] + out[1]; + + for (i = 0; i < len; ++i) { + tiledp_col[(OPJ_SIZE_T)i * stride] = out[i]; + } + } + + return; + } + +#if (defined(__SSE2__) || defined(__AVX2__)) + if (len > 2 && nb_cols == PARALLEL_COLS_53) { + /* Same as below general case, except that thanks to SSE2/AVX2 */ + /* we can efficiently process 8/16 columns in parallel */ + opj_idwt53_v_cas1_mcols_SSE2_OR_AVX2(dwt->mem, sn, len, tiledp_col, stride); + return; + } +#endif + if (len > 2) { + OPJ_INT32 c; + for (c = 0; c < nb_cols; c++, tiledp_col++) { + opj_idwt3_v_cas1(dwt->mem, sn, len, tiledp_col, stride); + } + return; + } + } +#endif +} + +#if 0 +static void opj_dwt_encode_step1(OPJ_FLOAT32* fw, + OPJ_UINT32 end, + const OPJ_FLOAT32 c) +{ + OPJ_UINT32 i = 0; + for (; i < end; ++i) { + fw[0] *= c; + fw += 2; + } +} +#else +static void opj_dwt_encode_step1_combined(OPJ_FLOAT32* fw, + OPJ_UINT32 iters_c1, + OPJ_UINT32 iters_c2, + const OPJ_FLOAT32 c1, + const OPJ_FLOAT32 c2) +{ + OPJ_UINT32 i = 0; + const OPJ_UINT32 iters_common = opj_uint_min(iters_c1, iters_c2); + assert((((OPJ_SIZE_T)fw) & 0xf) == 0); + assert(opj_int_abs((OPJ_INT32)iters_c1 - (OPJ_INT32)iters_c2) <= 1); + for (; i + 3 < iters_common; i += 4) { +#ifdef __SSE__ + const __m128 vcst = _mm_set_ps(c2, c1, c2, c1); + *(__m128*)fw = _mm_mul_ps(*(__m128*)fw, vcst); + *(__m128*)(fw + 4) = _mm_mul_ps(*(__m128*)(fw + 4), vcst); +#else + fw[0] *= c1; + fw[1] *= c2; + fw[2] *= c1; + fw[3] *= c2; + fw[4] *= c1; + fw[5] *= c2; + fw[6] *= c1; + fw[7] *= c2; +#endif + fw += 8; + } + for (; i < iters_common; i++) { + fw[0] *= c1; + fw[1] *= c2; + fw += 2; + } + if (i < iters_c1) { + fw[0] *= c1; + } else if (i < iters_c2) { + fw[1] *= c2; + } +} + +#endif + +static void opj_dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw, + OPJ_UINT32 end, + OPJ_UINT32 m, + OPJ_FLOAT32 c) +{ + OPJ_UINT32 i; + OPJ_UINT32 imax = opj_uint_min(end, m); + if (imax > 0) { + fw[-1] += (fl[0] + fw[0]) * c; + fw += 2; + i = 1; + for (; i + 3 < imax; i += 4) { + fw[-1] += (fw[-2] + fw[0]) * c; + fw[1] += (fw[0] + fw[2]) * c; + fw[3] += (fw[2] + fw[4]) * c; + fw[5] += (fw[4] + fw[6]) * c; + fw += 8; + } + for (; i < imax; ++i) { + fw[-1] += (fw[-2] + fw[0]) * c; + fw += 2; + } + } + if (m < end) { + assert(m + 1 == end); + fw[-1] += (2 * fw[-2]) * c; + } +} + +static void opj_dwt_encode_1_real(void *aIn, OPJ_INT32 dn, OPJ_INT32 sn, + OPJ_INT32 cas) +{ + OPJ_FLOAT32* w = (OPJ_FLOAT32*)aIn; + OPJ_INT32 a, b; + assert(dn + sn > 1); + if (cas == 0) { + a = 0; + b = 1; + } else { + a = 1; + b = 0; + } + opj_dwt_encode_step2(w + a, w + b + 1, + (OPJ_UINT32)dn, + (OPJ_UINT32)opj_int_min(dn, sn - b), + opj_dwt_alpha); + opj_dwt_encode_step2(w + b, w + a + 1, + (OPJ_UINT32)sn, + (OPJ_UINT32)opj_int_min(sn, dn - a), + opj_dwt_beta); + opj_dwt_encode_step2(w + a, w + b + 1, + (OPJ_UINT32)dn, + (OPJ_UINT32)opj_int_min(dn, sn - b), + opj_dwt_gamma); + opj_dwt_encode_step2(w + b, w + a + 1, + (OPJ_UINT32)sn, + (OPJ_UINT32)opj_int_min(sn, dn - a), + opj_dwt_delta); +#if 0 + opj_dwt_encode_step1(w + b, (OPJ_UINT32)dn, + opj_K); + opj_dwt_encode_step1(w + a, (OPJ_UINT32)sn, + opj_invK); +#else + if (a == 0) { + opj_dwt_encode_step1_combined(w, + (OPJ_UINT32)sn, + (OPJ_UINT32)dn, + opj_invK, + opj_K); + } else { + opj_dwt_encode_step1_combined(w, + (OPJ_UINT32)dn, + (OPJ_UINT32)sn, + opj_K, + opj_invK); + } +#endif +} + +static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, + opj_stepsize_t *bandno_stepsize) +{ + OPJ_INT32 p, n; + p = opj_int_floorlog2(stepsize) - 13; + n = 11 - opj_int_floorlog2(stepsize); + bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; + bandno_stepsize->expn = numbps - p; +} + +/* +========================================================== + DWT interface +========================================================== +*/ + +/** Process one line for the horizontal pass of the 5x3 forward transform */ +static +void opj_dwt_encode_and_deinterleave_h_one_row(void* rowIn, + void* tmpIn, + OPJ_UINT32 width, + OPJ_BOOL even) +{ + OPJ_INT32* OPJ_RESTRICT row = (OPJ_INT32*)rowIn; + OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32*)tmpIn; + const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1); + const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn); + + if (even) { + if (width > 1) { + OPJ_INT32 i; + for (i = 0; i < sn - 1; i++) { + tmp[sn + i] = row[2 * i + 1] - ((row[(i) * 2] + row[(i + 1) * 2]) >> 1); + } + if ((width % 2) == 0) { + tmp[sn + i] = row[2 * i + 1] - row[(i) * 2]; + } + row[0] += (tmp[sn] + tmp[sn] + 2) >> 2; + for (i = 1; i < dn; i++) { + row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + i] + 2) >> 2); + } + if ((width % 2) == 1) { + row[i] = row[2 * i] + ((tmp[sn + (i - 1)] + tmp[sn + (i - 1)] + 2) >> 2); + } + memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32)); + } + } else { + if (width == 1) { + row[0] *= 2; + } else { + OPJ_INT32 i; + tmp[sn + 0] = row[0] - row[1]; + for (i = 1; i < sn; i++) { + tmp[sn + i] = row[2 * i] - ((row[2 * i + 1] + row[2 * (i - 1) + 1]) >> 1); + } + if ((width % 2) == 1) { + tmp[sn + i] = row[2 * i] - row[2 * (i - 1) + 1]; + } + + for (i = 0; i < dn - 1; i++) { + row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i + 1] + 2) >> 2); + } + if ((width % 2) == 0) { + row[i] = row[2 * i + 1] + ((tmp[sn + i] + tmp[sn + i] + 2) >> 2); + } + memcpy(row + sn, tmp + sn, (OPJ_SIZE_T)dn * sizeof(OPJ_INT32)); + } + } +} + +/** Process one line for the horizontal pass of the 9x7 forward transform */ +static +void opj_dwt_encode_and_deinterleave_h_one_row_real(void* rowIn, + void* tmpIn, + OPJ_UINT32 width, + OPJ_BOOL even) +{ + OPJ_FLOAT32* OPJ_RESTRICT row = (OPJ_FLOAT32*)rowIn; + OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32*)tmpIn; + const OPJ_INT32 sn = (OPJ_INT32)((width + (even ? 1 : 0)) >> 1); + const OPJ_INT32 dn = (OPJ_INT32)(width - (OPJ_UINT32)sn); + if (width == 1) { + return; + } + memcpy(tmp, row, width * sizeof(OPJ_FLOAT32)); + opj_dwt_encode_1_real(tmp, dn, sn, even ? 0 : 1); + opj_dwt_deinterleave_h((OPJ_INT32 * OPJ_RESTRICT)tmp, + (OPJ_INT32 * OPJ_RESTRICT)row, + dn, sn, even ? 0 : 1); +} + +typedef struct { + opj_dwt_t h; + OPJ_UINT32 rw; /* Width of the resolution to process */ + OPJ_UINT32 w; /* Width of tiledp */ + OPJ_INT32 * OPJ_RESTRICT tiledp; + OPJ_UINT32 min_j; + OPJ_UINT32 max_j; + opj_encode_and_deinterleave_h_one_row_fnptr_type p_function; +} opj_dwt_encode_h_job_t; + +static void opj_dwt_encode_h_func(void* user_data, opj_tls_t* tls) +{ + OPJ_UINT32 j; + opj_dwt_encode_h_job_t* job; + (void)tls; + + job = (opj_dwt_encode_h_job_t*)user_data; + for (j = job->min_j; j < job->max_j; j++) { + OPJ_INT32* OPJ_RESTRICT aj = job->tiledp + j * job->w; + (*job->p_function)(aj, job->h.mem, job->rw, + job->h.cas == 0 ? OPJ_TRUE : OPJ_FALSE); + } + + opj_aligned_free(job->h.mem); + opj_free(job); +} + +typedef struct { + opj_dwt_t v; + OPJ_UINT32 rh; + OPJ_UINT32 w; + OPJ_INT32 * OPJ_RESTRICT tiledp; + OPJ_UINT32 min_j; + OPJ_UINT32 max_j; + opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v; +} opj_dwt_encode_v_job_t; + +static void opj_dwt_encode_v_func(void* user_data, opj_tls_t* tls) +{ + OPJ_UINT32 j; + opj_dwt_encode_v_job_t* job; + (void)tls; + + job = (opj_dwt_encode_v_job_t*)user_data; + for (j = job->min_j; j + NB_ELTS_V8 - 1 < job->max_j; j += NB_ELTS_V8) { + (*job->p_encode_and_deinterleave_v)(job->tiledp + j, + job->v.mem, + job->rh, + job->v.cas == 0, + job->w, + NB_ELTS_V8); + } + if (j < job->max_j) { + (*job->p_encode_and_deinterleave_v)(job->tiledp + j, + job->v.mem, + job->rh, + job->v.cas == 0, + job->w, + job->max_j - j); + } + + opj_aligned_free(job->v.mem); + opj_free(job); +} + +/** Fetch up to cols <= NB_ELTS_V8 for each line, and put them in tmpOut */ +/* that has a NB_ELTS_V8 interleave factor. */ +static void opj_dwt_fetch_cols_vertical_pass(const void *arrayIn, + void *tmpOut, + OPJ_UINT32 height, + OPJ_UINT32 stride_width, + OPJ_UINT32 cols) +{ + const OPJ_INT32* OPJ_RESTRICT array = (const OPJ_INT32 * OPJ_RESTRICT)arrayIn; + OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpOut; + if (cols == NB_ELTS_V8) { + OPJ_UINT32 k; + for (k = 0; k < height; ++k) { + memcpy(tmp + NB_ELTS_V8 * k, + array + k * stride_width, + NB_ELTS_V8 * sizeof(OPJ_INT32)); + } + } else { + OPJ_UINT32 k; + for (k = 0; k < height; ++k) { + OPJ_UINT32 c; + for (c = 0; c < cols; c++) { + tmp[NB_ELTS_V8 * k + c] = array[c + k * stride_width]; + } + for (; c < NB_ELTS_V8; c++) { + tmp[NB_ELTS_V8 * k + c] = 0; + } + } + } +} + +/* Deinterleave result of forward transform, where cols <= NB_ELTS_V8 */ +/* and src contains NB_ELTS_V8 consecutive values for up to NB_ELTS_V8 */ +/* columns. */ +static INLINE void opj_dwt_deinterleave_v_cols( + const OPJ_INT32 * OPJ_RESTRICT src, + OPJ_INT32 * OPJ_RESTRICT dst, + OPJ_INT32 dn, + OPJ_INT32 sn, + OPJ_UINT32 stride_width, + OPJ_INT32 cas, + OPJ_UINT32 cols) +{ + OPJ_INT32 k; + OPJ_INT32 i = sn; + OPJ_INT32 * OPJ_RESTRICT l_dest = dst; + const OPJ_INT32 * OPJ_RESTRICT l_src = src + cas * NB_ELTS_V8; + OPJ_UINT32 c; + + for (k = 0; k < 2; k++) { + while (i--) { + if (cols == NB_ELTS_V8) { + memcpy(l_dest, l_src, NB_ELTS_V8 * sizeof(OPJ_INT32)); + } else { + c = 0; + switch (cols) { + case 7: + l_dest[c] = l_src[c]; + c++; /* fallthru */ + case 6: + l_dest[c] = l_src[c]; + c++; /* fallthru */ + case 5: + l_dest[c] = l_src[c]; + c++; /* fallthru */ + case 4: + l_dest[c] = l_src[c]; + c++; /* fallthru */ + case 3: + l_dest[c] = l_src[c]; + c++; /* fallthru */ + case 2: + l_dest[c] = l_src[c]; + c++; /* fallthru */ + default: + l_dest[c] = l_src[c]; + break; + } + } + l_dest += stride_width; + l_src += 2 * NB_ELTS_V8; + } + + l_dest = dst + (OPJ_SIZE_T)sn * (OPJ_SIZE_T)stride_width; + l_src = src + (1 - cas) * NB_ELTS_V8; + i = dn; + } +} + + +/* Forward 5-3 transform, for the vertical pass, processing cols columns */ +/* where cols <= NB_ELTS_V8 */ +static void opj_dwt_encode_and_deinterleave_v( + void *arrayIn, + void *tmpIn, + OPJ_UINT32 height, + OPJ_BOOL even, + OPJ_UINT32 stride_width, + OPJ_UINT32 cols) +{ + OPJ_INT32* OPJ_RESTRICT array = (OPJ_INT32 * OPJ_RESTRICT)arrayIn; + OPJ_INT32* OPJ_RESTRICT tmp = (OPJ_INT32 * OPJ_RESTRICT)tmpIn; + const OPJ_UINT32 sn = (height + (even ? 1 : 0)) >> 1; + const OPJ_UINT32 dn = height - sn; + + opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols); + +#define OPJ_Sc(i) tmp[(i)*2* NB_ELTS_V8 + c] +#define OPJ_Dc(i) tmp[((1+(i)*2))* NB_ELTS_V8 + c] + +#ifdef __SSE2__ + if (height == 1) { + if (!even) { + OPJ_UINT32 c; + for (c = 0; c < NB_ELTS_V8; c++) { + tmp[c] *= 2; + } + } + } else if (even) { + OPJ_UINT32 c; + OPJ_UINT32 i; + i = 0; + if (i + 1 < sn) { + __m128i xmm_Si_0 = *(const __m128i*)(tmp + 4 * 0); + __m128i xmm_Si_1 = *(const __m128i*)(tmp + 4 * 1); + for (; i + 1 < sn; i++) { + __m128i xmm_Sip1_0 = *(const __m128i*)(tmp + + (i + 1) * 2 * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Sip1_1 = *(const __m128i*)(tmp + + (i + 1) * 2 * NB_ELTS_V8 + 4 * 1); + __m128i xmm_Di_0 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Di_1 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 1); + xmm_Di_0 = _mm_sub_epi32(xmm_Di_0, + _mm_srai_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), 1)); + xmm_Di_1 = _mm_sub_epi32(xmm_Di_1, + _mm_srai_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), 1)); + *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0; + *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1; + xmm_Si_0 = xmm_Sip1_0; + xmm_Si_1 = xmm_Sip1_1; + } + } + if (((height) % 2) == 0) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Dc(i) -= OPJ_Sc(i); + } + } + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2; + } + i = 1; + if (i < dn) { + __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 + + (i - 1) * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 + + (i - 1) * 2) * NB_ELTS_V8 + 4 * 1); + const __m128i xmm_two = _mm_set1_epi32(2); + for (; i < dn; i++) { + __m128i xmm_Di_0 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Di_1 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 1); + __m128i xmm_Si_0 = *(const __m128i*)(tmp + + (i * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Si_1 = *(const __m128i*)(tmp + + (i * 2) * NB_ELTS_V8 + 4 * 1); + xmm_Si_0 = _mm_add_epi32(xmm_Si_0, + _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_0, xmm_Di_0), xmm_two), 2)); + xmm_Si_1 = _mm_add_epi32(xmm_Si_1, + _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Dim1_1, xmm_Di_1), xmm_two), 2)); + *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0; + *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1; + xmm_Dim1_0 = xmm_Di_0; + xmm_Dim1_1 = xmm_Di_1; + } + } + if (((height) % 2) == 1) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2; + } + } + } else { + OPJ_UINT32 c; + OPJ_UINT32 i; + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(0) -= OPJ_Dc(0); + } + i = 1; + if (i < sn) { + __m128i xmm_Dim1_0 = *(const __m128i*)(tmp + (1 + + (i - 1) * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Dim1_1 = *(const __m128i*)(tmp + (1 + + (i - 1) * 2) * NB_ELTS_V8 + 4 * 1); + for (; i < sn; i++) { + __m128i xmm_Di_0 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Di_1 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 1); + __m128i xmm_Si_0 = *(const __m128i*)(tmp + + (i * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Si_1 = *(const __m128i*)(tmp + + (i * 2) * NB_ELTS_V8 + 4 * 1); + xmm_Si_0 = _mm_sub_epi32(xmm_Si_0, + _mm_srai_epi32(_mm_add_epi32(xmm_Di_0, xmm_Dim1_0), 1)); + xmm_Si_1 = _mm_sub_epi32(xmm_Si_1, + _mm_srai_epi32(_mm_add_epi32(xmm_Di_1, xmm_Dim1_1), 1)); + *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Si_0; + *(__m128i*)(tmp + (i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Si_1; + xmm_Dim1_0 = xmm_Di_0; + xmm_Dim1_1 = xmm_Di_1; + } + } + if (((height) % 2) == 1) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(i) -= OPJ_Dc(i - 1); + } + } + i = 0; + if (i + 1 < dn) { + __m128i xmm_Si_0 = *((const __m128i*)(tmp + 4 * 0)); + __m128i xmm_Si_1 = *((const __m128i*)(tmp + 4 * 1)); + const __m128i xmm_two = _mm_set1_epi32(2); + for (; i + 1 < dn; i++) { + __m128i xmm_Sip1_0 = *(const __m128i*)(tmp + + (i + 1) * 2 * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Sip1_1 = *(const __m128i*)(tmp + + (i + 1) * 2 * NB_ELTS_V8 + 4 * 1); + __m128i xmm_Di_0 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 0); + __m128i xmm_Di_1 = *(const __m128i*)(tmp + + (1 + i * 2) * NB_ELTS_V8 + 4 * 1); + xmm_Di_0 = _mm_add_epi32(xmm_Di_0, + _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_0, xmm_Sip1_0), xmm_two), 2)); + xmm_Di_1 = _mm_add_epi32(xmm_Di_1, + _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(xmm_Si_1, xmm_Sip1_1), xmm_two), 2)); + *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 0) = xmm_Di_0; + *(__m128i*)(tmp + (1 + i * 2) * NB_ELTS_V8 + 4 * 1) = xmm_Di_1; + xmm_Si_0 = xmm_Sip1_0; + xmm_Si_1 = xmm_Sip1_1; + } + } + if (((height) % 2) == 0) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2; + } + } + } +#else + if (even) { + OPJ_UINT32 c; + if (height > 1) { + OPJ_UINT32 i; + for (i = 0; i + 1 < sn; i++) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Dc(i) -= (OPJ_Sc(i) + OPJ_Sc(i + 1)) >> 1; + } + } + if (((height) % 2) == 0) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Dc(i) -= OPJ_Sc(i); + } + } + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(0) += (OPJ_Dc(0) + OPJ_Dc(0) + 2) >> 2; + } + for (i = 1; i < dn; i++) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i) + 2) >> 2; + } + } + if (((height) % 2) == 1) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(i) += (OPJ_Dc(i - 1) + OPJ_Dc(i - 1) + 2) >> 2; + } + } + } + } else { + OPJ_UINT32 c; + if (height == 1) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(0) *= 2; + } + } else { + OPJ_UINT32 i; + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(0) -= OPJ_Dc(0); + } + for (i = 1; i < sn; i++) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(i) -= (OPJ_Dc(i) + OPJ_Dc(i - 1)) >> 1; + } + } + if (((height) % 2) == 1) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Sc(i) -= OPJ_Dc(i - 1); + } + } + for (i = 0; i + 1 < dn; i++) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i + 1) + 2) >> 2; + } + } + if (((height) % 2) == 0) { + for (c = 0; c < NB_ELTS_V8; c++) { + OPJ_Dc(i) += (OPJ_Sc(i) + OPJ_Sc(i) + 2) >> 2; + } + } + } + } +#endif + + if (cols == NB_ELTS_V8) { + opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn, + stride_width, even ? 0 : 1, NB_ELTS_V8); + } else { + opj_dwt_deinterleave_v_cols(tmp, array, (OPJ_INT32)dn, (OPJ_INT32)sn, + stride_width, even ? 0 : 1, cols); + } +} + +static void opj_v8dwt_encode_step1(OPJ_FLOAT32* fw, + OPJ_UINT32 end, + const OPJ_FLOAT32 cst) +{ + OPJ_UINT32 i; +#ifdef __SSE__ + __m128* vw = (__m128*) fw; + const __m128 vcst = _mm_set1_ps(cst); + for (i = 0; i < end; ++i) { + vw[0] = _mm_mul_ps(vw[0], vcst); + vw[1] = _mm_mul_ps(vw[1], vcst); + vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128)); + } +#else + OPJ_UINT32 c; + for (i = 0; i < end; ++i) { + for (c = 0; c < NB_ELTS_V8; c++) { + fw[i * 2 * NB_ELTS_V8 + c] *= cst; + } + } +#endif +} + +static void opj_v8dwt_encode_step2(OPJ_FLOAT32* fl, OPJ_FLOAT32* fw, + OPJ_UINT32 end, + OPJ_UINT32 m, + OPJ_FLOAT32 cst) +{ + OPJ_UINT32 i; + OPJ_UINT32 imax = opj_uint_min(end, m); +#ifdef __SSE__ + __m128* vw = (__m128*) fw; + __m128 vcst = _mm_set1_ps(cst); + if (imax > 0) { + __m128* vl = (__m128*) fl; + vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), vcst)); + vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), vcst)); + vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128)); + i = 1; + + for (; i < imax; ++i) { + vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), vcst)); + vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), vcst)); + vw += 2 * (NB_ELTS_V8 * sizeof(OPJ_FLOAT32) / sizeof(__m128)); + } + } + if (m < end) { + assert(m + 1 == end); + vcst = _mm_add_ps(vcst, vcst); + vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(vw[-4], vcst)); + vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(vw[-3], vcst)); + } +#else + OPJ_INT32 c; + if (imax > 0) { + for (c = 0; c < NB_ELTS_V8; c++) { + fw[-1 * NB_ELTS_V8 + c] += (fl[0 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) * + cst; + } + fw += 2 * NB_ELTS_V8; + i = 1; + for (; i < imax; ++i) { + for (c = 0; c < NB_ELTS_V8; c++) { + fw[-1 * NB_ELTS_V8 + c] += (fw[-2 * NB_ELTS_V8 + c] + fw[0 * NB_ELTS_V8 + c]) * + cst; + } + fw += 2 * NB_ELTS_V8; + } + } + if (m < end) { + assert(m + 1 == end); + for (c = 0; c < NB_ELTS_V8; c++) { + fw[-1 * NB_ELTS_V8 + c] += (2 * fw[-2 * NB_ELTS_V8 + c]) * cst; + } + } +#endif +} + +/* Forward 9-7 transform, for the vertical pass, processing cols columns */ +/* where cols <= NB_ELTS_V8 */ +static void opj_dwt_encode_and_deinterleave_v_real( + void *arrayIn, + void *tmpIn, + OPJ_UINT32 height, + OPJ_BOOL even, + OPJ_UINT32 stride_width, + OPJ_UINT32 cols) +{ + OPJ_FLOAT32* OPJ_RESTRICT array = (OPJ_FLOAT32 * OPJ_RESTRICT)arrayIn; + OPJ_FLOAT32* OPJ_RESTRICT tmp = (OPJ_FLOAT32 * OPJ_RESTRICT)tmpIn; + const OPJ_INT32 sn = (OPJ_INT32)((height + (even ? 1 : 0)) >> 1); + const OPJ_INT32 dn = (OPJ_INT32)(height - (OPJ_UINT32)sn); + OPJ_INT32 a, b; + + if (height == 1) { + return; + } + + opj_dwt_fetch_cols_vertical_pass(arrayIn, tmpIn, height, stride_width, cols); + + if (even) { + a = 0; + b = 1; + } else { + a = 1; + b = 0; + } + opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8, + tmp + (b + 1) * NB_ELTS_V8, + (OPJ_UINT32)dn, + (OPJ_UINT32)opj_int_min(dn, sn - b), + opj_dwt_alpha); + opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8, + tmp + (a + 1) * NB_ELTS_V8, + (OPJ_UINT32)sn, + (OPJ_UINT32)opj_int_min(sn, dn - a), + opj_dwt_beta); + opj_v8dwt_encode_step2(tmp + a * NB_ELTS_V8, + tmp + (b + 1) * NB_ELTS_V8, + (OPJ_UINT32)dn, + (OPJ_UINT32)opj_int_min(dn, sn - b), + opj_dwt_gamma); + opj_v8dwt_encode_step2(tmp + b * NB_ELTS_V8, + tmp + (a + 1) * NB_ELTS_V8, + (OPJ_UINT32)sn, + (OPJ_UINT32)opj_int_min(sn, dn - a), + opj_dwt_delta); + opj_v8dwt_encode_step1(tmp + b * NB_ELTS_V8, (OPJ_UINT32)dn, + opj_K); + opj_v8dwt_encode_step1(tmp + a * NB_ELTS_V8, (OPJ_UINT32)sn, + opj_invK); + + + if (cols == NB_ELTS_V8) { + opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp, + (OPJ_INT32*)array, + (OPJ_INT32)dn, (OPJ_INT32)sn, + stride_width, even ? 0 : 1, NB_ELTS_V8); + } else { + opj_dwt_deinterleave_v_cols((OPJ_INT32*)tmp, + (OPJ_INT32*)array, + (OPJ_INT32)dn, (OPJ_INT32)sn, + stride_width, even ? 0 : 1, cols); + } +} + + +/* */ +/* Forward 5-3 wavelet transform in 2-D. */ +/* */ +static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_thread_pool_t* tp, + opj_tcd_tilecomp_t * tilec, + opj_encode_and_deinterleave_v_fnptr_type p_encode_and_deinterleave_v, + opj_encode_and_deinterleave_h_one_row_fnptr_type + p_encode_and_deinterleave_h_one_row) +{ + OPJ_INT32 i; + OPJ_INT32 *bj = 00; + OPJ_UINT32 w; + OPJ_INT32 l; + + OPJ_SIZE_T l_data_size; + + opj_tcd_resolution_t * l_cur_res = 0; + opj_tcd_resolution_t * l_last_res = 0; + const int num_threads = opj_thread_pool_get_thread_count(tp); + OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data; + + w = (OPJ_UINT32)(tilec->x1 - tilec->x0); + l = (OPJ_INT32)tilec->numresolutions - 1; + + l_cur_res = tilec->resolutions + l; + l_last_res = l_cur_res - 1; + + l_data_size = opj_dwt_max_resolution(tilec->resolutions, tilec->numresolutions); + /* overflow check */ + if (l_data_size > (SIZE_MAX / (NB_ELTS_V8 * sizeof(OPJ_INT32)))) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + l_data_size *= NB_ELTS_V8 * sizeof(OPJ_INT32); + bj = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size); + /* l_data_size is equal to 0 when numresolutions == 1 but bj is not used */ + /* in that case, so do not error out */ + if (l_data_size != 0 && ! bj) { + return OPJ_FALSE; + } + i = l; + + while (i--) { + OPJ_UINT32 j; + OPJ_UINT32 rw; /* width of the resolution level computed */ + OPJ_UINT32 rh; /* height of the resolution level computed */ + OPJ_UINT32 + rw1; /* width of the resolution level once lower than computed one */ + OPJ_UINT32 + rh1; /* height of the resolution level once lower than computed one */ + OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ + OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ + OPJ_INT32 dn, sn; + + rw = (OPJ_UINT32)(l_cur_res->x1 - l_cur_res->x0); + rh = (OPJ_UINT32)(l_cur_res->y1 - l_cur_res->y0); + rw1 = (OPJ_UINT32)(l_last_res->x1 - l_last_res->x0); + rh1 = (OPJ_UINT32)(l_last_res->y1 - l_last_res->y0); + + cas_row = l_cur_res->x0 & 1; + cas_col = l_cur_res->y0 & 1; + + sn = (OPJ_INT32)rh1; + dn = (OPJ_INT32)(rh - rh1); + + /* Perform vertical pass */ + if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) { + for (j = 0; j + NB_ELTS_V8 - 1 < rw; j += NB_ELTS_V8) { + p_encode_and_deinterleave_v(tiledp + j, + bj, + rh, + cas_col == 0, + w, + NB_ELTS_V8); + } + if (j < rw) { + p_encode_and_deinterleave_v(tiledp + j, + bj, + rh, + cas_col == 0, + w, + rw - j); + } + } else { + OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads; + OPJ_UINT32 step_j; + + if (rw < num_jobs) { + num_jobs = rw; + } + step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8; + + for (j = 0; j < num_jobs; j++) { + opj_dwt_encode_v_job_t* job; + + job = (opj_dwt_encode_v_job_t*) opj_malloc(sizeof(opj_dwt_encode_v_job_t)); + if (!job) { + opj_thread_pool_wait_completion(tp, 0); + opj_aligned_free(bj); + return OPJ_FALSE; + } + job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size); + if (!job->v.mem) { + opj_thread_pool_wait_completion(tp, 0); + opj_free(job); + opj_aligned_free(bj); + return OPJ_FALSE; + } + job->v.dn = dn; + job->v.sn = sn; + job->v.cas = cas_col; + job->rh = rh; + job->w = w; + job->tiledp = tiledp; + job->min_j = j * step_j; + job->max_j = (j + 1 == num_jobs) ? rw : (j + 1) * step_j; + job->p_encode_and_deinterleave_v = p_encode_and_deinterleave_v; + opj_thread_pool_submit_job(tp, opj_dwt_encode_v_func, job); + } + opj_thread_pool_wait_completion(tp, 0); + } + + sn = (OPJ_INT32)rw1; + dn = (OPJ_INT32)(rw - rw1); + + /* Perform horizontal pass */ + if (num_threads <= 1 || rh <= 1) { + for (j = 0; j < rh; j++) { + OPJ_INT32* OPJ_RESTRICT aj = tiledp + j * w; + (*p_encode_and_deinterleave_h_one_row)(aj, bj, rw, + cas_row == 0 ? OPJ_TRUE : OPJ_FALSE); + } + } else { + OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads; + OPJ_UINT32 step_j; + + if (rh < num_jobs) { + num_jobs = rh; + } + step_j = (rh / num_jobs); + + for (j = 0; j < num_jobs; j++) { + opj_dwt_encode_h_job_t* job; + + job = (opj_dwt_encode_h_job_t*) opj_malloc(sizeof(opj_dwt_encode_h_job_t)); + if (!job) { + opj_thread_pool_wait_completion(tp, 0); + opj_aligned_free(bj); + return OPJ_FALSE; + } + job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(l_data_size); + if (!job->h.mem) { + opj_thread_pool_wait_completion(tp, 0); + opj_free(job); + opj_aligned_free(bj); + return OPJ_FALSE; + } + job->h.dn = dn; + job->h.sn = sn; + job->h.cas = cas_row; + job->rw = rw; + job->w = w; + job->tiledp = tiledp; + job->min_j = j * step_j; + job->max_j = (j + 1U) * step_j; /* this can overflow */ + if (j == (num_jobs - 1U)) { /* this will take care of the overflow */ + job->max_j = rh; + } + job->p_function = p_encode_and_deinterleave_h_one_row; + opj_thread_pool_submit_job(tp, opj_dwt_encode_h_func, job); + } + opj_thread_pool_wait_completion(tp, 0); + } + + l_cur_res = l_last_res; + + --l_last_res; + } + + opj_aligned_free(bj); + return OPJ_TRUE; +} + +/* Forward 5-3 wavelet transform in 2-D. */ +/* */ +OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t * tilec) +{ + return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec, + opj_dwt_encode_and_deinterleave_v, + opj_dwt_encode_and_deinterleave_h_one_row); +} + +/* */ +/* Inverse 5-3 wavelet transform in 2-D. */ +/* */ +OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, opj_tcd_tilecomp_t* tilec, + OPJ_UINT32 numres) +{ + if (p_tcd->whole_tile_decoding) { + return opj_dwt_decode_tile(p_tcd->thread_pool, tilec, numres); + } else { + return opj_dwt_decode_partial_tile(tilec, numres); + } +} + +/* */ +/* Get norm of 5-3 wavelet. */ +/* */ +OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) +{ + /* FIXME ! This is just a band-aid to avoid a buffer overflow */ + /* but the array should really be extended up to 33 resolution levels */ + /* See https://github.com/uclouvain/openjpeg/issues/493 */ + if (orient == 0 && level >= 10) { + level = 9; + } else if (orient > 0 && level >= 9) { + level = 8; + } + return opj_dwt_norms[orient][level]; +} + +/* */ +/* Forward 9-7 wavelet transform in 2-D. */ +/* */ +OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t * tilec) +{ + return opj_dwt_encode_procedure(p_tcd->thread_pool, tilec, + opj_dwt_encode_and_deinterleave_v_real, + opj_dwt_encode_and_deinterleave_h_one_row_real); +} + +/* */ +/* Get norm of 9-7 wavelet. */ +/* */ +OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) +{ + /* FIXME ! This is just a band-aid to avoid a buffer overflow */ + /* but the array should really be extended up to 33 resolution levels */ + /* See https://github.com/uclouvain/openjpeg/issues/493 */ + if (orient == 0 && level >= 10) { + level = 9; + } else if (orient > 0 && level >= 9) { + level = 8; + } + return opj_dwt_norms_real[orient][level]; +} + +void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) +{ + OPJ_UINT32 numbands, bandno; + numbands = 3 * tccp->numresolutions - 2; + for (bandno = 0; bandno < numbands; bandno++) { + OPJ_FLOAT64 stepsize; + OPJ_UINT32 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 { + OPJ_FLOAT64 norm = opj_dwt_getnorm_real(level, orient); + stepsize = (1 << (gain)) / norm; + } + opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), + (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]); + } +} + +/* */ +/* Determine maximum computed resolution level for inverse wavelet transform */ +/* */ +static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* OPJ_RESTRICT r, + OPJ_UINT32 i) +{ + OPJ_UINT32 mr = 0; + OPJ_UINT32 w; + while (--i) { + ++r; + if (mr < (w = (OPJ_UINT32)(r->x1 - r->x0))) { + mr = w ; + } + if (mr < (w = (OPJ_UINT32)(r->y1 - r->y0))) { + mr = w ; + } + } + return mr ; +} + +typedef struct { + opj_dwt_t h; + OPJ_UINT32 rw; + OPJ_UINT32 w; + OPJ_INT32 * OPJ_RESTRICT tiledp; + OPJ_UINT32 min_j; + OPJ_UINT32 max_j; +} opj_dwt_decode_h_job_t; + +static void opj_dwt_decode_h_func(void* user_data, opj_tls_t* tls) +{ + OPJ_UINT32 j; + opj_dwt_decode_h_job_t* job; + (void)tls; + + job = (opj_dwt_decode_h_job_t*)user_data; + for (j = job->min_j; j < job->max_j; j++) { + opj_idwt53_h(&job->h, &job->tiledp[j * job->w]); + } + + opj_aligned_free(job->h.mem); + opj_free(job); +} + +typedef struct { + opj_dwt_t v; + OPJ_UINT32 rh; + OPJ_UINT32 w; + OPJ_INT32 * OPJ_RESTRICT tiledp; + OPJ_UINT32 min_j; + OPJ_UINT32 max_j; +} opj_dwt_decode_v_job_t; + +static void opj_dwt_decode_v_func(void* user_data, opj_tls_t* tls) +{ + OPJ_UINT32 j; + opj_dwt_decode_v_job_t* job; + (void)tls; + + job = (opj_dwt_decode_v_job_t*)user_data; + for (j = job->min_j; j + PARALLEL_COLS_53 <= job->max_j; + j += PARALLEL_COLS_53) { + opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w, + PARALLEL_COLS_53); + } + if (j < job->max_j) + opj_idwt53_v(&job->v, &job->tiledp[j], (OPJ_SIZE_T)job->w, + (OPJ_INT32)(job->max_j - j)); + + opj_aligned_free(job->v.mem); + opj_free(job); +} + + +/* */ +/* Inverse wavelet transform in 2-D. */ +/* */ +static OPJ_BOOL opj_dwt_decode_tile(opj_thread_pool_t* tp, + opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) +{ + opj_dwt_t h; + opj_dwt_t v; + + opj_tcd_resolution_t* tr = tilec->resolutions; + + OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - + tr->x0); /* width of the resolution level computed */ + OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - + tr->y0); /* height of the resolution level computed */ + + OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions - + 1].x1 - + tilec->resolutions[tilec->minimum_num_resolutions - 1].x0); + OPJ_SIZE_T h_mem_size; + int num_threads; + + if (numres == 1U) { + return OPJ_TRUE; + } + num_threads = opj_thread_pool_get_thread_count(tp); + h_mem_size = opj_dwt_max_resolution(tr, numres); + /* overflow check */ + if (h_mem_size > (SIZE_MAX / PARALLEL_COLS_53 / sizeof(OPJ_INT32))) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + /* We need PARALLEL_COLS_53 times the height of the array, */ + /* since for the vertical pass */ + /* we process PARALLEL_COLS_53 columns at a time */ + h_mem_size *= PARALLEL_COLS_53 * sizeof(OPJ_INT32); + h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size); + if (! h.mem) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + + v.mem = h.mem; + + while (--numres) { + OPJ_INT32 * OPJ_RESTRICT tiledp = tilec->data; + OPJ_UINT32 j; + + ++tr; + h.sn = (OPJ_INT32)rw; + v.sn = (OPJ_INT32)rh; + + rw = (OPJ_UINT32)(tr->x1 - tr->x0); + rh = (OPJ_UINT32)(tr->y1 - tr->y0); + + h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn); + h.cas = tr->x0 % 2; + + if (num_threads <= 1 || rh <= 1) { + for (j = 0; j < rh; ++j) { + opj_idwt53_h(&h, &tiledp[(OPJ_SIZE_T)j * w]); + } + } else { + OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads; + OPJ_UINT32 step_j; + + if (rh < num_jobs) { + num_jobs = rh; + } + step_j = (rh / num_jobs); + + for (j = 0; j < num_jobs; j++) { + opj_dwt_decode_h_job_t* job; + + job = (opj_dwt_decode_h_job_t*) opj_malloc(sizeof(opj_dwt_decode_h_job_t)); + if (!job) { + /* It would be nice to fallback to single thread case, but */ + /* unfortunately some jobs may be launched and have modified */ + /* tiledp, so it is not practical to recover from that error */ + /* FIXME event manager error callback */ + opj_thread_pool_wait_completion(tp, 0); + opj_aligned_free(h.mem); + return OPJ_FALSE; + } + job->h = h; + job->rw = rw; + job->w = w; + job->tiledp = tiledp; + job->min_j = j * step_j; + job->max_j = (j + 1U) * step_j; /* this can overflow */ + if (j == (num_jobs - 1U)) { /* this will take care of the overflow */ + job->max_j = rh; + } + job->h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size); + if (!job->h.mem) { + /* FIXME event manager error callback */ + opj_thread_pool_wait_completion(tp, 0); + opj_free(job); + opj_aligned_free(h.mem); + return OPJ_FALSE; + } + opj_thread_pool_submit_job(tp, opj_dwt_decode_h_func, job); + } + opj_thread_pool_wait_completion(tp, 0); + } + + v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn); + v.cas = tr->y0 % 2; + + if (num_threads <= 1 || rw <= 1) { + for (j = 0; j + PARALLEL_COLS_53 <= rw; + j += PARALLEL_COLS_53) { + opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, PARALLEL_COLS_53); + } + if (j < rw) { + opj_idwt53_v(&v, &tiledp[j], (OPJ_SIZE_T)w, (OPJ_INT32)(rw - j)); + } + } else { + OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads; + OPJ_UINT32 step_j; + + if (rw < num_jobs) { + num_jobs = rw; + } + step_j = (rw / num_jobs); + + for (j = 0; j < num_jobs; j++) { + opj_dwt_decode_v_job_t* job; + + job = (opj_dwt_decode_v_job_t*) opj_malloc(sizeof(opj_dwt_decode_v_job_t)); + if (!job) { + /* It would be nice to fallback to single thread case, but */ + /* unfortunately some jobs may be launched and have modified */ + /* tiledp, so it is not practical to recover from that error */ + /* FIXME event manager error callback */ + opj_thread_pool_wait_completion(tp, 0); + opj_aligned_free(v.mem); + return OPJ_FALSE; + } + job->v = v; + job->rh = rh; + job->w = w; + job->tiledp = tiledp; + job->min_j = j * step_j; + job->max_j = (j + 1U) * step_j; /* this can overflow */ + if (j == (num_jobs - 1U)) { /* this will take care of the overflow */ + job->max_j = rw; + } + job->v.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size); + if (!job->v.mem) { + /* FIXME event manager error callback */ + opj_thread_pool_wait_completion(tp, 0); + opj_free(job); + opj_aligned_free(v.mem); + return OPJ_FALSE; + } + opj_thread_pool_submit_job(tp, opj_dwt_decode_v_func, job); + } + opj_thread_pool_wait_completion(tp, 0); + } + } + opj_aligned_free(h.mem); + return OPJ_TRUE; +} + +static void opj_dwt_interleave_partial_h(OPJ_INT32 *dest, + OPJ_INT32 cas, + opj_sparse_array_int32_t* sa, + OPJ_UINT32 sa_line, + OPJ_UINT32 sn, + OPJ_UINT32 win_l_x0, + OPJ_UINT32 win_l_x1, + OPJ_UINT32 win_h_x0, + OPJ_UINT32 win_h_x1) +{ + OPJ_BOOL ret; + ret = opj_sparse_array_int32_read(sa, + win_l_x0, sa_line, + win_l_x1, sa_line + 1, + dest + cas + 2 * win_l_x0, + 2, 0, OPJ_TRUE); + assert(ret); + ret = opj_sparse_array_int32_read(sa, + sn + win_h_x0, sa_line, + sn + win_h_x1, sa_line + 1, + dest + 1 - cas + 2 * win_h_x0, + 2, 0, OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); +} + + +static void opj_dwt_interleave_partial_v(OPJ_INT32 *dest, + OPJ_INT32 cas, + opj_sparse_array_int32_t* sa, + OPJ_UINT32 sa_col, + OPJ_UINT32 nb_cols, + OPJ_UINT32 sn, + OPJ_UINT32 win_l_y0, + OPJ_UINT32 win_l_y1, + OPJ_UINT32 win_h_y0, + OPJ_UINT32 win_h_y1) +{ + OPJ_BOOL ret; + ret = opj_sparse_array_int32_read(sa, + sa_col, win_l_y0, + sa_col + nb_cols, win_l_y1, + dest + cas * 4 + 2 * 4 * win_l_y0, + 1, 2 * 4, OPJ_TRUE); + assert(ret); + ret = opj_sparse_array_int32_read(sa, + sa_col, sn + win_h_y0, + sa_col + nb_cols, sn + win_h_y1, + dest + (1 - cas) * 4 + 2 * 4 * win_h_y0, + 1, 2 * 4, OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); +} + +static void opj_dwt_decode_partial_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, + OPJ_INT32 cas, + OPJ_INT32 win_l_x0, + OPJ_INT32 win_l_x1, + OPJ_INT32 win_h_x0, + OPJ_INT32 win_h_x1) +{ + OPJ_INT32 i; + + if (!cas) { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + + /* Naive version is : + for (i = win_l_x0; i < i_max; i++) { + OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2; + } + for (i = win_h_x0; i < win_h_x1; i++) { + OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1; + } + but the compiler doesn't manage to unroll it to avoid bound + checking in OPJ_S_ and OPJ_D_ macros + */ + + i = win_l_x0; + if (i < win_l_x1) { + OPJ_INT32 i_max; + + /* Left-most case */ + OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2; + i ++; + + i_max = win_l_x1; + if (i_max > dn) { + i_max = dn; + } + for (; i < i_max; i++) { + /* No bound checking */ + OPJ_S(i) -= (OPJ_D(i - 1) + OPJ_D(i) + 2) >> 2; + } + for (; i < win_l_x1; i++) { + /* Right-most case */ + OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2; + } + } + + i = win_h_x0; + if (i < win_h_x1) { + OPJ_INT32 i_max = win_h_x1; + if (i_max >= sn) { + i_max = sn - 1; + } + for (; i < i_max; i++) { + /* No bound checking */ + OPJ_D(i) += (OPJ_S(i) + OPJ_S(i + 1)) >> 1; + } + for (; i < win_h_x1; i++) { + /* Right-most case */ + OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1; + } + } + } + } else { + if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */ + OPJ_S(0) /= 2; + } else { + for (i = win_l_x0; i < win_l_x1; i++) { + OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2; + } + for (i = win_h_x0; i < win_h_x1; i++) { + OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1; + } + } + } +} + +#define OPJ_S_off(i,off) a[(OPJ_UINT32)(i)*2*4+off] +#define OPJ_D_off(i,off) a[(1+(OPJ_UINT32)(i)*2)*4+off] +#define OPJ_S__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=sn?OPJ_S_off(sn-1,off):OPJ_S_off(i,off))) +#define OPJ_D__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=dn?OPJ_D_off(dn-1,off):OPJ_D_off(i,off))) +#define OPJ_SS__off(i,off) ((i)<0?OPJ_S_off(0,off):((i)>=dn?OPJ_S_off(dn-1,off):OPJ_S_off(i,off))) +#define OPJ_DD__off(i,off) ((i)<0?OPJ_D_off(0,off):((i)>=sn?OPJ_D_off(sn-1,off):OPJ_D_off(i,off))) + +static void opj_dwt_decode_partial_1_parallel(OPJ_INT32 *a, + OPJ_UINT32 nb_cols, + OPJ_INT32 dn, OPJ_INT32 sn, + OPJ_INT32 cas, + OPJ_INT32 win_l_x0, + OPJ_INT32 win_l_x1, + OPJ_INT32 win_h_x0, + OPJ_INT32 win_h_x1) +{ + OPJ_INT32 i; + OPJ_UINT32 off; + + (void)nb_cols; + + if (!cas) { + if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ + + /* Naive version is : + for (i = win_l_x0; i < i_max; i++) { + OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2; + } + for (i = win_h_x0; i < win_h_x1; i++) { + OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1; + } + but the compiler doesn't manage to unroll it to avoid bound + checking in OPJ_S_ and OPJ_D_ macros + */ + + i = win_l_x0; + if (i < win_l_x1) { + OPJ_INT32 i_max; + + /* Left-most case */ + for (off = 0; off < 4; off++) { + OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2; + } + i ++; + + i_max = win_l_x1; + if (i_max > dn) { + i_max = dn; + } + +#ifdef __SSE2__ + if (i + 1 < i_max) { + const __m128i two = _mm_set1_epi32(2); + __m128i Dm1 = _mm_load_si128((__m128i * const)(a + 4 + (i - 1) * 8)); + for (; i + 1 < i_max; i += 2) { + /* No bound checking */ + __m128i S = _mm_load_si128((__m128i * const)(a + i * 8)); + __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8)); + __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8)); + __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8)); + S = _mm_sub_epi32(S, + _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(Dm1, D), two), 2)); + S1 = _mm_sub_epi32(S1, + _mm_srai_epi32(_mm_add_epi32(_mm_add_epi32(D, D1), two), 2)); + _mm_store_si128((__m128i*)(a + i * 8), S); + _mm_store_si128((__m128i*)(a + (i + 1) * 8), S1); + Dm1 = D1; + } + } +#endif + + for (; i < i_max; i++) { + /* No bound checking */ + for (off = 0; off < 4; off++) { + OPJ_S_off(i, off) -= (OPJ_D_off(i - 1, off) + OPJ_D_off(i, off) + 2) >> 2; + } + } + for (; i < win_l_x1; i++) { + /* Right-most case */ + for (off = 0; off < 4; off++) { + OPJ_S_off(i, off) -= (OPJ_D__off(i - 1, off) + OPJ_D__off(i, off) + 2) >> 2; + } + } + } + + i = win_h_x0; + if (i < win_h_x1) { + OPJ_INT32 i_max = win_h_x1; + if (i_max >= sn) { + i_max = sn - 1; + } + +#ifdef __SSE2__ + if (i + 1 < i_max) { + __m128i S = _mm_load_si128((__m128i * const)(a + i * 8)); + for (; i + 1 < i_max; i += 2) { + /* No bound checking */ + __m128i D = _mm_load_si128((__m128i * const)(a + 4 + i * 8)); + __m128i S1 = _mm_load_si128((__m128i * const)(a + (i + 1) * 8)); + __m128i D1 = _mm_load_si128((__m128i * const)(a + 4 + (i + 1) * 8)); + __m128i S2 = _mm_load_si128((__m128i * const)(a + (i + 2) * 8)); + D = _mm_add_epi32(D, _mm_srai_epi32(_mm_add_epi32(S, S1), 1)); + D1 = _mm_add_epi32(D1, _mm_srai_epi32(_mm_add_epi32(S1, S2), 1)); + _mm_store_si128((__m128i*)(a + 4 + i * 8), D); + _mm_store_si128((__m128i*)(a + 4 + (i + 1) * 8), D1); + S = S2; + } + } +#endif + + for (; i < i_max; i++) { + /* No bound checking */ + for (off = 0; off < 4; off++) { + OPJ_D_off(i, off) += (OPJ_S_off(i, off) + OPJ_S_off(i + 1, off)) >> 1; + } + } + for (; i < win_h_x1; i++) { + /* Right-most case */ + for (off = 0; off < 4; off++) { + OPJ_D_off(i, off) += (OPJ_S__off(i, off) + OPJ_S__off(i + 1, off)) >> 1; + } + } + } + } + } else { + if (!sn && dn == 1) { /* NEW : CASE ONE ELEMENT */ + for (off = 0; off < 4; off++) { + OPJ_S_off(0, off) /= 2; + } + } else { + for (i = win_l_x0; i < win_l_x1; i++) { + for (off = 0; off < 4; off++) { + OPJ_D_off(i, off) -= (OPJ_SS__off(i, off) + OPJ_SS__off(i + 1, off) + 2) >> 2; + } + } + for (i = win_h_x0; i < win_h_x1; i++) { + for (off = 0; off < 4; off++) { + OPJ_S_off(i, off) += (OPJ_DD__off(i, off) + OPJ_DD__off(i - 1, off)) >> 1; + } + } + } + } +} + +static void opj_dwt_get_band_coordinates(opj_tcd_tilecomp_t* tilec, + OPJ_UINT32 resno, + OPJ_UINT32 bandno, + OPJ_UINT32 tcx0, + OPJ_UINT32 tcy0, + OPJ_UINT32 tcx1, + OPJ_UINT32 tcy1, + OPJ_UINT32* tbx0, + OPJ_UINT32* tby0, + OPJ_UINT32* tbx1, + OPJ_UINT32* tby1) +{ + /* Compute number of decomposition for this band. See table F-1 */ + OPJ_UINT32 nb = (resno == 0) ? + tilec->numresolutions - 1 : + tilec->numresolutions - resno; + /* Map above tile-based coordinates to sub-band-based coordinates per */ + /* equation B-15 of the standard */ + OPJ_UINT32 x0b = bandno & 1; + OPJ_UINT32 y0b = bandno >> 1; + if (tbx0) { + *tbx0 = (nb == 0) ? tcx0 : + (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 : + opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb); + } + if (tby0) { + *tby0 = (nb == 0) ? tcy0 : + (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 : + opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb); + } + if (tbx1) { + *tbx1 = (nb == 0) ? tcx1 : + (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 : + opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb); + } + if (tby1) { + *tby1 = (nb == 0) ? tcy1 : + (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 : + opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb); + } +} + +static void opj_dwt_segment_grow(OPJ_UINT32 filter_width, + OPJ_UINT32 max_size, + OPJ_UINT32* start, + OPJ_UINT32* end) +{ + *start = opj_uint_subs(*start, filter_width); + *end = opj_uint_adds(*end, filter_width); + *end = opj_uint_min(*end, max_size); +} + + +static opj_sparse_array_int32_t* opj_dwt_init_sparse_array( + opj_tcd_tilecomp_t* tilec, + OPJ_UINT32 numres) +{ + opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]); + OPJ_UINT32 w = (OPJ_UINT32)(tr_max->x1 - tr_max->x0); + OPJ_UINT32 h = (OPJ_UINT32)(tr_max->y1 - tr_max->y0); + OPJ_UINT32 resno, bandno, precno, cblkno; + opj_sparse_array_int32_t* sa = opj_sparse_array_int32_create( + w, h, opj_uint_min(w, 64), opj_uint_min(h, 64)); + if (sa == NULL) { + return NULL; + } + + for (resno = 0; resno < numres; ++resno) { + opj_tcd_resolution_t* res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; ++bandno) { + opj_tcd_band_t* band = &res->bands[bandno]; + + for (precno = 0; precno < res->pw * res->ph; ++precno) { + opj_tcd_precinct_t* precinct = &band->precincts[precno]; + for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) { + opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno]; + if (cblk->decoded_data != NULL) { + OPJ_UINT32 x = (OPJ_UINT32)(cblk->x0 - band->x0); + OPJ_UINT32 y = (OPJ_UINT32)(cblk->y0 - band->y0); + OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0); + OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0); + + if (band->bandno & 1) { + opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1]; + x += (OPJ_UINT32)(pres->x1 - pres->x0); + } + if (band->bandno & 2) { + opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1]; + y += (OPJ_UINT32)(pres->y1 - pres->y0); + } + + if (!opj_sparse_array_int32_write(sa, x, y, + x + cblk_w, y + cblk_h, + cblk->decoded_data, + 1, cblk_w, OPJ_TRUE)) { + opj_sparse_array_int32_free(sa); + return NULL; + } + } + } + } + } + } + + return sa; +} + + +static OPJ_BOOL opj_dwt_decode_partial_tile( + opj_tcd_tilecomp_t* tilec, + OPJ_UINT32 numres) +{ + opj_sparse_array_int32_t* sa; + opj_dwt_t h; + opj_dwt_t v; + OPJ_UINT32 resno; + /* This value matches the maximum left/right extension given in tables */ + /* F.2 and F.3 of the standard. */ + const OPJ_UINT32 filter_width = 2U; + + opj_tcd_resolution_t* tr = tilec->resolutions; + opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]); + + OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - + tr->x0); /* width of the resolution level computed */ + OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - + tr->y0); /* height of the resolution level computed */ + + OPJ_SIZE_T h_mem_size; + + /* Compute the intersection of the area of interest, expressed in tile coordinates */ + /* with the tile coordinates */ + OPJ_UINT32 win_tcx0 = tilec->win_x0; + OPJ_UINT32 win_tcy0 = tilec->win_y0; + OPJ_UINT32 win_tcx1 = tilec->win_x1; + OPJ_UINT32 win_tcy1 = tilec->win_y1; + + if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) { + return OPJ_TRUE; + } + + sa = opj_dwt_init_sparse_array(tilec, numres); + if (sa == NULL) { + return OPJ_FALSE; + } + + if (numres == 1U) { + OPJ_BOOL ret = opj_sparse_array_int32_read(sa, + tr_max->win_x0 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y0 - (OPJ_UINT32)tr_max->y0, + tr_max->win_x1 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y1 - (OPJ_UINT32)tr_max->y0, + tilec->data_win, + 1, tr_max->win_x1 - tr_max->win_x0, + OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); + opj_sparse_array_int32_free(sa); + return OPJ_TRUE; + } + h_mem_size = opj_dwt_max_resolution(tr, numres); + /* overflow check */ + /* in vertical pass, we process 4 columns at a time */ + if (h_mem_size > (SIZE_MAX / (4 * sizeof(OPJ_INT32)))) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + return OPJ_FALSE; + } + + h_mem_size *= 4 * sizeof(OPJ_INT32); + h.mem = (OPJ_INT32*)opj_aligned_32_malloc(h_mem_size); + if (! h.mem) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + return OPJ_FALSE; + } + + v.mem = h.mem; + + for (resno = 1; resno < numres; resno ++) { + OPJ_UINT32 i, j; + /* Window of interest subband-based coordinates */ + OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1; + OPJ_UINT32 win_hl_x0, win_hl_x1; + OPJ_UINT32 win_lh_y0, win_lh_y1; + /* Window of interest tile-resolution-based coordinates */ + OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1; + /* Tile-resolution subband-based coordinates */ + OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0; + + ++tr; + + h.sn = (OPJ_INT32)rw; + v.sn = (OPJ_INT32)rh; + + rw = (OPJ_UINT32)(tr->x1 - tr->x0); + rh = (OPJ_UINT32)(tr->y1 - tr->y0); + + h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn); + h.cas = tr->x0 % 2; + + v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn); + v.cas = tr->y0 % 2; + + /* Get the subband coordinates for the window of interest */ + /* LL band */ + opj_dwt_get_band_coordinates(tilec, resno, 0, + win_tcx0, win_tcy0, win_tcx1, win_tcy1, + &win_ll_x0, &win_ll_y0, + &win_ll_x1, &win_ll_y1); + + /* HL band */ + opj_dwt_get_band_coordinates(tilec, resno, 1, + win_tcx0, win_tcy0, win_tcx1, win_tcy1, + &win_hl_x0, NULL, &win_hl_x1, NULL); + + /* LH band */ + opj_dwt_get_band_coordinates(tilec, resno, 2, + win_tcx0, win_tcy0, win_tcx1, win_tcy1, + NULL, &win_lh_y0, NULL, &win_lh_y1); + + /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */ + tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0; + tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0; + tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0; + tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0; + + /* Subtract the origin of the bands for this tile, to the subwindow */ + /* of interest band coordinates, so as to get them relative to the */ + /* tile */ + win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0); + win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0); + win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0); + win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0); + win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0); + win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0); + win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0); + win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0); + + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1); + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1); + + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1); + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1); + + /* Compute the tile-resolution-based coordinates for the window of interest */ + if (h.cas == 0) { + win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1); + win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw); + } else { + win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1); + win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw); + } + + if (v.cas == 0) { + win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1); + win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh); + } else { + win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1); + win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh); + } + + for (j = 0; j < rh; ++j) { + if ((j >= win_ll_y0 && j < win_ll_y1) || + (j >= win_lh_y0 + (OPJ_UINT32)v.sn && j < win_lh_y1 + (OPJ_UINT32)v.sn)) { + + /* Avoids dwt.c:1584:44 (in opj_dwt_decode_partial_1): runtime error: */ + /* signed integer overflow: -1094795586 + -1094795586 cannot be represented in type 'int' */ + /* on opj_decompress -i ../../openjpeg/MAPA.jp2 -o out.tif -d 0,0,256,256 */ + /* This is less extreme than memsetting the whole buffer to 0 */ + /* although we could potentially do better with better handling of edge conditions */ + if (win_tr_x1 >= 1 && win_tr_x1 < rw) { + h.mem[win_tr_x1 - 1] = 0; + } + if (win_tr_x1 < rw) { + h.mem[win_tr_x1] = 0; + } + + opj_dwt_interleave_partial_h(h.mem, + h.cas, + sa, + j, + (OPJ_UINT32)h.sn, + win_ll_x0, + win_ll_x1, + win_hl_x0, + win_hl_x1); + opj_dwt_decode_partial_1(h.mem, h.dn, h.sn, h.cas, + (OPJ_INT32)win_ll_x0, + (OPJ_INT32)win_ll_x1, + (OPJ_INT32)win_hl_x0, + (OPJ_INT32)win_hl_x1); + if (!opj_sparse_array_int32_write(sa, + win_tr_x0, j, + win_tr_x1, j + 1, + h.mem + win_tr_x0, + 1, 0, OPJ_TRUE)) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + opj_aligned_free(h.mem); + return OPJ_FALSE; + } + } + } + + for (i = win_tr_x0; i < win_tr_x1;) { + OPJ_UINT32 nb_cols = opj_uint_min(4U, win_tr_x1 - i); + opj_dwt_interleave_partial_v(v.mem, + v.cas, + sa, + i, + nb_cols, + (OPJ_UINT32)v.sn, + win_ll_y0, + win_ll_y1, + win_lh_y0, + win_lh_y1); + opj_dwt_decode_partial_1_parallel(v.mem, nb_cols, v.dn, v.sn, v.cas, + (OPJ_INT32)win_ll_y0, + (OPJ_INT32)win_ll_y1, + (OPJ_INT32)win_lh_y0, + (OPJ_INT32)win_lh_y1); + if (!opj_sparse_array_int32_write(sa, + i, win_tr_y0, + i + nb_cols, win_tr_y1, + v.mem + 4 * win_tr_y0, + 1, 4, OPJ_TRUE)) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + opj_aligned_free(h.mem); + return OPJ_FALSE; + } + + i += nb_cols; + } + } + opj_aligned_free(h.mem); + + { + OPJ_BOOL ret = opj_sparse_array_int32_read(sa, + tr_max->win_x0 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y0 - (OPJ_UINT32)tr_max->y0, + tr_max->win_x1 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y1 - (OPJ_UINT32)tr_max->y0, + tilec->data_win, + 1, tr_max->win_x1 - tr_max->win_x0, + OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); + } + opj_sparse_array_int32_free(sa); + return OPJ_TRUE; +} + +static void opj_v8dwt_interleave_h(opj_v8dwt_t* OPJ_RESTRICT dwt, + OPJ_FLOAT32* OPJ_RESTRICT a, + OPJ_UINT32 width, + OPJ_UINT32 remaining_height) +{ + OPJ_FLOAT32* OPJ_RESTRICT bi = (OPJ_FLOAT32*)(dwt->wavelet + dwt->cas); + OPJ_UINT32 i, k; + OPJ_UINT32 x0 = dwt->win_l_x0; + OPJ_UINT32 x1 = dwt->win_l_x1; + + for (k = 0; k < 2; ++k) { + if (remaining_height >= NB_ELTS_V8 && ((OPJ_SIZE_T) a & 0x0f) == 0 && + ((OPJ_SIZE_T) bi & 0x0f) == 0) { + /* Fast code path */ + for (i = x0; i < x1; ++i) { + OPJ_UINT32 j = i; + OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8; + dst[0] = a[j]; + j += width; + dst[1] = a[j]; + j += width; + dst[2] = a[j]; + j += width; + dst[3] = a[j]; + j += width; + dst[4] = a[j]; + j += width; + dst[5] = a[j]; + j += width; + dst[6] = a[j]; + j += width; + dst[7] = a[j]; + } + } else { + /* Slow code path */ + for (i = x0; i < x1; ++i) { + OPJ_UINT32 j = i; + OPJ_FLOAT32* OPJ_RESTRICT dst = bi + i * 2 * NB_ELTS_V8; + dst[0] = a[j]; + j += width; + if (remaining_height == 1) { + continue; + } + dst[1] = a[j]; + j += width; + if (remaining_height == 2) { + continue; + } + dst[2] = a[j]; + j += width; + if (remaining_height == 3) { + continue; + } + dst[3] = a[j]; + j += width; + if (remaining_height == 4) { + continue; + } + dst[4] = a[j]; + j += width; + if (remaining_height == 5) { + continue; + } + dst[5] = a[j]; + j += width; + if (remaining_height == 6) { + continue; + } + dst[6] = a[j]; + j += width; + if (remaining_height == 7) { + continue; + } + dst[7] = a[j]; + } + } + + bi = (OPJ_FLOAT32*)(dwt->wavelet + 1 - dwt->cas); + a += dwt->sn; + x0 = dwt->win_h_x0; + x1 = dwt->win_h_x1; + } +} + +static void opj_v8dwt_interleave_partial_h(opj_v8dwt_t* dwt, + opj_sparse_array_int32_t* sa, + OPJ_UINT32 sa_line, + OPJ_UINT32 remaining_height) +{ + OPJ_UINT32 i; + for (i = 0; i < remaining_height; i++) { + OPJ_BOOL ret; + ret = opj_sparse_array_int32_read(sa, + dwt->win_l_x0, sa_line + i, + dwt->win_l_x1, sa_line + i + 1, + /* Nasty cast from float* to int32* */ + (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0) + i, + 2 * NB_ELTS_V8, 0, OPJ_TRUE); + assert(ret); + ret = opj_sparse_array_int32_read(sa, + (OPJ_UINT32)dwt->sn + dwt->win_h_x0, sa_line + i, + (OPJ_UINT32)dwt->sn + dwt->win_h_x1, sa_line + i + 1, + /* Nasty cast from float* to int32* */ + (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0) + i, + 2 * NB_ELTS_V8, 0, OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); + } +} + +static INLINE void opj_v8dwt_interleave_v(opj_v8dwt_t* OPJ_RESTRICT dwt, + OPJ_FLOAT32* OPJ_RESTRICT a, + OPJ_UINT32 width, + OPJ_UINT32 nb_elts_read) +{ + opj_v8_t* OPJ_RESTRICT bi = dwt->wavelet + dwt->cas; + OPJ_UINT32 i; + + for (i = dwt->win_l_x0; i < dwt->win_l_x1; ++i) { + memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width], + (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32)); + } + + a += (OPJ_UINT32)dwt->sn * (OPJ_SIZE_T)width; + bi = dwt->wavelet + 1 - dwt->cas; + + for (i = dwt->win_h_x0; i < dwt->win_h_x1; ++i) { + memcpy(&bi[i * 2], &a[i * (OPJ_SIZE_T)width], + (OPJ_SIZE_T)nb_elts_read * sizeof(OPJ_FLOAT32)); + } +} + +static void opj_v8dwt_interleave_partial_v(opj_v8dwt_t* OPJ_RESTRICT dwt, + opj_sparse_array_int32_t* sa, + OPJ_UINT32 sa_col, + OPJ_UINT32 nb_elts_read) +{ + OPJ_BOOL ret; + ret = opj_sparse_array_int32_read(sa, + sa_col, dwt->win_l_x0, + sa_col + nb_elts_read, dwt->win_l_x1, + (OPJ_INT32*)(dwt->wavelet + dwt->cas + 2 * dwt->win_l_x0), + 1, 2 * NB_ELTS_V8, OPJ_TRUE); + assert(ret); + ret = opj_sparse_array_int32_read(sa, + sa_col, (OPJ_UINT32)dwt->sn + dwt->win_h_x0, + sa_col + nb_elts_read, (OPJ_UINT32)dwt->sn + dwt->win_h_x1, + (OPJ_INT32*)(dwt->wavelet + 1 - dwt->cas + 2 * dwt->win_h_x0), + 1, 2 * NB_ELTS_V8, OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); +} + +#ifdef __SSE__ + +static void opj_v8dwt_decode_step1_sse(opj_v8_t* w, + OPJ_UINT32 start, + OPJ_UINT32 end, + const __m128 c) +{ + __m128* OPJ_RESTRICT vw = (__m128*) w; + OPJ_UINT32 i = start; + /* To be adapted if NB_ELTS_V8 changes */ + vw += 4 * start; + /* Note: attempt at loop unrolling x2 doesn't help */ + for (; i < end; ++i, vw += 4) { + vw[0] = _mm_mul_ps(vw[0], c); + vw[1] = _mm_mul_ps(vw[1], c); + } +} + +static void opj_v8dwt_decode_step2_sse(opj_v8_t* l, opj_v8_t* w, + OPJ_UINT32 start, + OPJ_UINT32 end, + OPJ_UINT32 m, + __m128 c) +{ + __m128* OPJ_RESTRICT vl = (__m128*) l; + __m128* OPJ_RESTRICT vw = (__m128*) w; + /* To be adapted if NB_ELTS_V8 changes */ + OPJ_UINT32 i; + OPJ_UINT32 imax = opj_uint_min(end, m); + if (start == 0) { + if (imax >= 1) { + vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vl[0], vw[0]), c)); + vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vl[1], vw[1]), c)); + vw += 4; + start = 1; + } + } else { + vw += start * 4; + } + + i = start; + /* Note: attempt at loop unrolling x2 doesn't help */ + for (; i < imax; ++i) { + vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(_mm_add_ps(vw[-4], vw[0]), c)); + vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(_mm_add_ps(vw[-3], vw[1]), c)); + vw += 4; + } + if (m < end) { + assert(m + 1 == end); + c = _mm_add_ps(c, c); + vw[-2] = _mm_add_ps(vw[-2], _mm_mul_ps(c, vw[-4])); + vw[-1] = _mm_add_ps(vw[-1], _mm_mul_ps(c, vw[-3])); + } +} + +#else + +static void opj_v8dwt_decode_step1(opj_v8_t* w, + OPJ_UINT32 start, + OPJ_UINT32 end, + const OPJ_FLOAT32 c) +{ + OPJ_FLOAT32* OPJ_RESTRICT fw = (OPJ_FLOAT32*) w; + OPJ_UINT32 i; + /* To be adapted if NB_ELTS_V8 changes */ + for (i = start; i < end; ++i) { + fw[i * 2 * 8 ] = fw[i * 2 * 8 ] * c; + fw[i * 2 * 8 + 1] = fw[i * 2 * 8 + 1] * c; + fw[i * 2 * 8 + 2] = fw[i * 2 * 8 + 2] * c; + fw[i * 2 * 8 + 3] = fw[i * 2 * 8 + 3] * c; + fw[i * 2 * 8 + 4] = fw[i * 2 * 8 + 4] * c; + fw[i * 2 * 8 + 5] = fw[i * 2 * 8 + 5] * c; + fw[i * 2 * 8 + 6] = fw[i * 2 * 8 + 6] * c; + fw[i * 2 * 8 + 7] = fw[i * 2 * 8 + 7] * c; + } +} + +static void opj_v8dwt_decode_step2(opj_v8_t* l, opj_v8_t* w, + OPJ_UINT32 start, + OPJ_UINT32 end, + OPJ_UINT32 m, + OPJ_FLOAT32 c) +{ + OPJ_FLOAT32* fl = (OPJ_FLOAT32*) l; + OPJ_FLOAT32* fw = (OPJ_FLOAT32*) w; + OPJ_UINT32 i; + OPJ_UINT32 imax = opj_uint_min(end, m); + if (start > 0) { + fw += 2 * NB_ELTS_V8 * start; + fl = fw - 2 * NB_ELTS_V8; + } + /* To be adapted if NB_ELTS_V8 changes */ + for (i = start; i < imax; ++i) { + fw[-8] = fw[-8] + ((fl[0] + fw[0]) * c); + fw[-7] = fw[-7] + ((fl[1] + fw[1]) * c); + fw[-6] = fw[-6] + ((fl[2] + fw[2]) * c); + fw[-5] = fw[-5] + ((fl[3] + fw[3]) * c); + fw[-4] = fw[-4] + ((fl[4] + fw[4]) * c); + fw[-3] = fw[-3] + ((fl[5] + fw[5]) * c); + fw[-2] = fw[-2] + ((fl[6] + fw[6]) * c); + fw[-1] = fw[-1] + ((fl[7] + fw[7]) * c); + fl = fw; + fw += 2 * NB_ELTS_V8; + } + if (m < end) { + assert(m + 1 == end); + c += c; + fw[-8] = fw[-8] + fl[0] * c; + fw[-7] = fw[-7] + fl[1] * c; + fw[-6] = fw[-6] + fl[2] * c; + fw[-5] = fw[-5] + fl[3] * c; + fw[-4] = fw[-4] + fl[4] * c; + fw[-3] = fw[-3] + fl[5] * c; + fw[-2] = fw[-2] + fl[6] * c; + fw[-1] = fw[-1] + fl[7] * c; + } +} + +#endif + +/* */ +/* Inverse 9-7 wavelet transform in 1-D. */ +/* */ +static void opj_v8dwt_decode(opj_v8dwt_t* OPJ_RESTRICT dwt) +{ + OPJ_INT32 a, b; + /* BUG_WEIRD_TWO_INVK (look for this identifier in tcd.c) */ + /* Historic value for 2 / opj_invK */ + /* Normally, we should use invK, but if we do so, we have failures in the */ + /* conformance test, due to MSE and peak errors significantly higher than */ + /* accepted value */ + /* Due to using two_invK instead of invK, we have to compensate in tcd.c */ + /* the computation of the stepsize for the non LL subbands */ + const float two_invK = 1.625732422f; + if (dwt->cas == 0) { + if (!((dwt->dn > 0) || (dwt->sn > 1))) { + return; + } + a = 0; + b = 1; + } else { + if (!((dwt->sn > 0) || (dwt->dn > 1))) { + return; + } + a = 1; + b = 0; + } +#ifdef __SSE__ + opj_v8dwt_decode_step1_sse(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1, + _mm_set1_ps(opj_K)); + opj_v8dwt_decode_step1_sse(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1, + _mm_set1_ps(two_invK)); + opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1, + dwt->win_l_x0, dwt->win_l_x1, + (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a), + _mm_set1_ps(-opj_dwt_delta)); + opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1, + dwt->win_h_x0, dwt->win_h_x1, + (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b), + _mm_set1_ps(-opj_dwt_gamma)); + opj_v8dwt_decode_step2_sse(dwt->wavelet + b, dwt->wavelet + a + 1, + dwt->win_l_x0, dwt->win_l_x1, + (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a), + _mm_set1_ps(-opj_dwt_beta)); + opj_v8dwt_decode_step2_sse(dwt->wavelet + a, dwt->wavelet + b + 1, + dwt->win_h_x0, dwt->win_h_x1, + (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b), + _mm_set1_ps(-opj_dwt_alpha)); +#else + opj_v8dwt_decode_step1(dwt->wavelet + a, dwt->win_l_x0, dwt->win_l_x1, + opj_K); + opj_v8dwt_decode_step1(dwt->wavelet + b, dwt->win_h_x0, dwt->win_h_x1, + two_invK); + opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1, + dwt->win_l_x0, dwt->win_l_x1, + (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a), + -opj_dwt_delta); + opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1, + dwt->win_h_x0, dwt->win_h_x1, + (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b), + -opj_dwt_gamma); + opj_v8dwt_decode_step2(dwt->wavelet + b, dwt->wavelet + a + 1, + dwt->win_l_x0, dwt->win_l_x1, + (OPJ_UINT32)opj_int_min(dwt->sn, dwt->dn - a), + -opj_dwt_beta); + opj_v8dwt_decode_step2(dwt->wavelet + a, dwt->wavelet + b + 1, + dwt->win_h_x0, dwt->win_h_x1, + (OPJ_UINT32)opj_int_min(dwt->dn, dwt->sn - b), + -opj_dwt_alpha); +#endif +} + +typedef struct { + opj_v8dwt_t h; + OPJ_UINT32 rw; + OPJ_UINT32 w; + OPJ_FLOAT32 * OPJ_RESTRICT aj; + OPJ_UINT32 nb_rows; +} opj_dwt97_decode_h_job_t; + +static void opj_dwt97_decode_h_func(void* user_data, opj_tls_t* tls) +{ + OPJ_UINT32 j; + opj_dwt97_decode_h_job_t* job; + OPJ_FLOAT32 * OPJ_RESTRICT aj; + OPJ_UINT32 w; + (void)tls; + + job = (opj_dwt97_decode_h_job_t*)user_data; + w = job->w; + + assert((job->nb_rows % NB_ELTS_V8) == 0); + + aj = job->aj; + for (j = 0; j + NB_ELTS_V8 <= job->nb_rows; j += NB_ELTS_V8) { + OPJ_UINT32 k; + opj_v8dwt_interleave_h(&job->h, aj, job->w, NB_ELTS_V8); + opj_v8dwt_decode(&job->h); + + /* To be adapted if NB_ELTS_V8 changes */ + for (k = 0; k < job->rw; k++) { + aj[k ] = job->h.wavelet[k].f[0]; + aj[k + (OPJ_SIZE_T)w ] = job->h.wavelet[k].f[1]; + aj[k + (OPJ_SIZE_T)w * 2] = job->h.wavelet[k].f[2]; + aj[k + (OPJ_SIZE_T)w * 3] = job->h.wavelet[k].f[3]; + } + for (k = 0; k < job->rw; k++) { + aj[k + (OPJ_SIZE_T)w * 4] = job->h.wavelet[k].f[4]; + aj[k + (OPJ_SIZE_T)w * 5] = job->h.wavelet[k].f[5]; + aj[k + (OPJ_SIZE_T)w * 6] = job->h.wavelet[k].f[6]; + aj[k + (OPJ_SIZE_T)w * 7] = job->h.wavelet[k].f[7]; + } + + aj += w * NB_ELTS_V8; + } + + opj_aligned_free(job->h.wavelet); + opj_free(job); +} + + +typedef struct { + opj_v8dwt_t v; + OPJ_UINT32 rh; + OPJ_UINT32 w; + OPJ_FLOAT32 * OPJ_RESTRICT aj; + OPJ_UINT32 nb_columns; +} opj_dwt97_decode_v_job_t; + +static void opj_dwt97_decode_v_func(void* user_data, opj_tls_t* tls) +{ + OPJ_UINT32 j; + opj_dwt97_decode_v_job_t* job; + OPJ_FLOAT32 * OPJ_RESTRICT aj; + (void)tls; + + job = (opj_dwt97_decode_v_job_t*)user_data; + + assert((job->nb_columns % NB_ELTS_V8) == 0); + + aj = job->aj; + for (j = 0; j + NB_ELTS_V8 <= job->nb_columns; j += NB_ELTS_V8) { + OPJ_UINT32 k; + + opj_v8dwt_interleave_v(&job->v, aj, job->w, NB_ELTS_V8); + opj_v8dwt_decode(&job->v); + + for (k = 0; k < job->rh; ++k) { + memcpy(&aj[k * (OPJ_SIZE_T)job->w], &job->v.wavelet[k], + NB_ELTS_V8 * sizeof(OPJ_FLOAT32)); + } + aj += NB_ELTS_V8; + } + + opj_aligned_free(job->v.wavelet); + opj_free(job); +} + + +/* */ +/* Inverse 9-7 wavelet transform in 2-D. */ +/* */ +static +OPJ_BOOL opj_dwt_decode_tile_97(opj_thread_pool_t* tp, + opj_tcd_tilecomp_t* OPJ_RESTRICT tilec, + OPJ_UINT32 numres) +{ + opj_v8dwt_t h; + opj_v8dwt_t v; + + opj_tcd_resolution_t* res = tilec->resolutions; + + OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 - + res->x0); /* width of the resolution level computed */ + OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 - + res->y0); /* height of the resolution level computed */ + + OPJ_UINT32 w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions - + 1].x1 - + tilec->resolutions[tilec->minimum_num_resolutions - 1].x0); + + OPJ_SIZE_T l_data_size; + const int num_threads = opj_thread_pool_get_thread_count(tp); + + if (numres == 1) { + return OPJ_TRUE; + } + + l_data_size = opj_dwt_max_resolution(res, numres); + /* overflow check */ + if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t)); + if (!h.wavelet) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + v.wavelet = h.wavelet; + + while (--numres) { + OPJ_FLOAT32 * OPJ_RESTRICT aj = (OPJ_FLOAT32*) tilec->data; + OPJ_UINT32 j; + + h.sn = (OPJ_INT32)rw; + v.sn = (OPJ_INT32)rh; + + ++res; + + rw = (OPJ_UINT32)(res->x1 - + res->x0); /* width of the resolution level computed */ + rh = (OPJ_UINT32)(res->y1 - + res->y0); /* height of the resolution level computed */ + + h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn); + h.cas = res->x0 % 2; + + h.win_l_x0 = 0; + h.win_l_x1 = (OPJ_UINT32)h.sn; + h.win_h_x0 = 0; + h.win_h_x1 = (OPJ_UINT32)h.dn; + + if (num_threads <= 1 || rh < 2 * NB_ELTS_V8) { + for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) { + OPJ_UINT32 k; + opj_v8dwt_interleave_h(&h, aj, w, NB_ELTS_V8); + opj_v8dwt_decode(&h); + + /* To be adapted if NB_ELTS_V8 changes */ + for (k = 0; k < rw; k++) { + aj[k ] = h.wavelet[k].f[0]; + aj[k + (OPJ_SIZE_T)w ] = h.wavelet[k].f[1]; + aj[k + (OPJ_SIZE_T)w * 2] = h.wavelet[k].f[2]; + aj[k + (OPJ_SIZE_T)w * 3] = h.wavelet[k].f[3]; + } + for (k = 0; k < rw; k++) { + aj[k + (OPJ_SIZE_T)w * 4] = h.wavelet[k].f[4]; + aj[k + (OPJ_SIZE_T)w * 5] = h.wavelet[k].f[5]; + aj[k + (OPJ_SIZE_T)w * 6] = h.wavelet[k].f[6]; + aj[k + (OPJ_SIZE_T)w * 7] = h.wavelet[k].f[7]; + } + + aj += w * NB_ELTS_V8; + } + } else { + OPJ_UINT32 num_jobs = (OPJ_UINT32)num_threads; + OPJ_UINT32 step_j; + + if ((rh / NB_ELTS_V8) < num_jobs) { + num_jobs = rh / NB_ELTS_V8; + } + step_j = ((rh / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8; + for (j = 0; j < num_jobs; j++) { + opj_dwt97_decode_h_job_t* job; + + job = (opj_dwt97_decode_h_job_t*) opj_malloc(sizeof(opj_dwt97_decode_h_job_t)); + if (!job) { + opj_thread_pool_wait_completion(tp, 0); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + job->h.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t)); + if (!job->h.wavelet) { + opj_thread_pool_wait_completion(tp, 0); + opj_free(job); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + job->h.dn = h.dn; + job->h.sn = h.sn; + job->h.cas = h.cas; + job->h.win_l_x0 = h.win_l_x0; + job->h.win_l_x1 = h.win_l_x1; + job->h.win_h_x0 = h.win_h_x0; + job->h.win_h_x1 = h.win_h_x1; + job->rw = rw; + job->w = w; + job->aj = aj; + job->nb_rows = (j + 1 == num_jobs) ? (rh & (OPJ_UINT32)~ + (NB_ELTS_V8 - 1)) - j * step_j : step_j; + aj += w * job->nb_rows; + opj_thread_pool_submit_job(tp, opj_dwt97_decode_h_func, job); + } + opj_thread_pool_wait_completion(tp, 0); + j = rh & (OPJ_UINT32)~(NB_ELTS_V8 - 1); + } + + if (j < rh) { + OPJ_UINT32 k; + opj_v8dwt_interleave_h(&h, aj, w, rh - j); + opj_v8dwt_decode(&h); + for (k = 0; k < rw; k++) { + OPJ_UINT32 l; + for (l = 0; l < rh - j; l++) { + aj[k + (OPJ_SIZE_T)w * l ] = h.wavelet[k].f[l]; + } + } + } + + v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn); + v.cas = res->y0 % 2; + v.win_l_x0 = 0; + v.win_l_x1 = (OPJ_UINT32)v.sn; + v.win_h_x0 = 0; + v.win_h_x1 = (OPJ_UINT32)v.dn; + + aj = (OPJ_FLOAT32*) tilec->data; + if (num_threads <= 1 || rw < 2 * NB_ELTS_V8) { + for (j = rw; j > (NB_ELTS_V8 - 1); j -= NB_ELTS_V8) { + OPJ_UINT32 k; + + opj_v8dwt_interleave_v(&v, aj, w, NB_ELTS_V8); + opj_v8dwt_decode(&v); + + for (k = 0; k < rh; ++k) { + memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], NB_ELTS_V8 * sizeof(OPJ_FLOAT32)); + } + aj += NB_ELTS_V8; + } + } else { + /* "bench_dwt -I" shows that scaling is poor, likely due to RAM + transfer being the limiting factor. So limit the number of + threads. + */ + OPJ_UINT32 num_jobs = opj_uint_max((OPJ_UINT32)num_threads / 2, 2U); + OPJ_UINT32 step_j; + + if ((rw / NB_ELTS_V8) < num_jobs) { + num_jobs = rw / NB_ELTS_V8; + } + step_j = ((rw / num_jobs) / NB_ELTS_V8) * NB_ELTS_V8; + for (j = 0; j < num_jobs; j++) { + opj_dwt97_decode_v_job_t* job; + + job = (opj_dwt97_decode_v_job_t*) opj_malloc(sizeof(opj_dwt97_decode_v_job_t)); + if (!job) { + opj_thread_pool_wait_completion(tp, 0); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + job->v.wavelet = (opj_v8_t*)opj_aligned_malloc(l_data_size * sizeof(opj_v8_t)); + if (!job->v.wavelet) { + opj_thread_pool_wait_completion(tp, 0); + opj_free(job); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + job->v.dn = v.dn; + job->v.sn = v.sn; + job->v.cas = v.cas; + job->v.win_l_x0 = v.win_l_x0; + job->v.win_l_x1 = v.win_l_x1; + job->v.win_h_x0 = v.win_h_x0; + job->v.win_h_x1 = v.win_h_x1; + job->rh = rh; + job->w = w; + job->aj = aj; + job->nb_columns = (j + 1 == num_jobs) ? (rw & (OPJ_UINT32)~ + (NB_ELTS_V8 - 1)) - j * step_j : step_j; + aj += job->nb_columns; + opj_thread_pool_submit_job(tp, opj_dwt97_decode_v_func, job); + } + opj_thread_pool_wait_completion(tp, 0); + } + + if (rw & (NB_ELTS_V8 - 1)) { + OPJ_UINT32 k; + + j = rw & (NB_ELTS_V8 - 1); + + opj_v8dwt_interleave_v(&v, aj, w, j); + opj_v8dwt_decode(&v); + + for (k = 0; k < rh; ++k) { + memcpy(&aj[k * (OPJ_SIZE_T)w], &v.wavelet[k], + (OPJ_SIZE_T)j * sizeof(OPJ_FLOAT32)); + } + } + } + + opj_aligned_free(h.wavelet); + return OPJ_TRUE; +} + +static +OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec, + OPJ_UINT32 numres) +{ + opj_sparse_array_int32_t* sa; + opj_v8dwt_t h; + opj_v8dwt_t v; + OPJ_UINT32 resno; + /* This value matches the maximum left/right extension given in tables */ + /* F.2 and F.3 of the standard. Note: in opj_tcd_is_subband_area_of_interest() */ + /* we currently use 3. */ + const OPJ_UINT32 filter_width = 4U; + + opj_tcd_resolution_t* tr = tilec->resolutions; + opj_tcd_resolution_t* tr_max = &(tilec->resolutions[numres - 1]); + + OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - + tr->x0); /* width of the resolution level computed */ + OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - + tr->y0); /* height of the resolution level computed */ + + OPJ_SIZE_T l_data_size; + + /* Compute the intersection of the area of interest, expressed in tile coordinates */ + /* with the tile coordinates */ + OPJ_UINT32 win_tcx0 = tilec->win_x0; + OPJ_UINT32 win_tcy0 = tilec->win_y0; + OPJ_UINT32 win_tcx1 = tilec->win_x1; + OPJ_UINT32 win_tcy1 = tilec->win_y1; + + if (tr_max->x0 == tr_max->x1 || tr_max->y0 == tr_max->y1) { + return OPJ_TRUE; + } + + sa = opj_dwt_init_sparse_array(tilec, numres); + if (sa == NULL) { + return OPJ_FALSE; + } + + if (numres == 1U) { + OPJ_BOOL ret = opj_sparse_array_int32_read(sa, + tr_max->win_x0 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y0 - (OPJ_UINT32)tr_max->y0, + tr_max->win_x1 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y1 - (OPJ_UINT32)tr_max->y0, + tilec->data_win, + 1, tr_max->win_x1 - tr_max->win_x0, + OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); + opj_sparse_array_int32_free(sa); + return OPJ_TRUE; + } + + l_data_size = opj_dwt_max_resolution(tr, numres); + /* overflow check */ + if (l_data_size > (SIZE_MAX / sizeof(opj_v8_t))) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + return OPJ_FALSE; + } + h.wavelet = (opj_v8_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v8_t)); + if (!h.wavelet) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + return OPJ_FALSE; + } + v.wavelet = h.wavelet; + + for (resno = 1; resno < numres; resno ++) { + OPJ_UINT32 j; + /* Window of interest subband-based coordinates */ + OPJ_UINT32 win_ll_x0, win_ll_y0, win_ll_x1, win_ll_y1; + OPJ_UINT32 win_hl_x0, win_hl_x1; + OPJ_UINT32 win_lh_y0, win_lh_y1; + /* Window of interest tile-resolution-based coordinates */ + OPJ_UINT32 win_tr_x0, win_tr_x1, win_tr_y0, win_tr_y1; + /* Tile-resolution subband-based coordinates */ + OPJ_UINT32 tr_ll_x0, tr_ll_y0, tr_hl_x0, tr_lh_y0; + + ++tr; + + h.sn = (OPJ_INT32)rw; + v.sn = (OPJ_INT32)rh; + + rw = (OPJ_UINT32)(tr->x1 - tr->x0); + rh = (OPJ_UINT32)(tr->y1 - tr->y0); + + h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn); + h.cas = tr->x0 % 2; + + v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn); + v.cas = tr->y0 % 2; + + /* Get the subband coordinates for the window of interest */ + /* LL band */ + opj_dwt_get_band_coordinates(tilec, resno, 0, + win_tcx0, win_tcy0, win_tcx1, win_tcy1, + &win_ll_x0, &win_ll_y0, + &win_ll_x1, &win_ll_y1); + + /* HL band */ + opj_dwt_get_band_coordinates(tilec, resno, 1, + win_tcx0, win_tcy0, win_tcx1, win_tcy1, + &win_hl_x0, NULL, &win_hl_x1, NULL); + + /* LH band */ + opj_dwt_get_band_coordinates(tilec, resno, 2, + win_tcx0, win_tcy0, win_tcx1, win_tcy1, + NULL, &win_lh_y0, NULL, &win_lh_y1); + + /* Beware: band index for non-LL0 resolution are 0=HL, 1=LH and 2=HH */ + tr_ll_x0 = (OPJ_UINT32)tr->bands[1].x0; + tr_ll_y0 = (OPJ_UINT32)tr->bands[0].y0; + tr_hl_x0 = (OPJ_UINT32)tr->bands[0].x0; + tr_lh_y0 = (OPJ_UINT32)tr->bands[1].y0; + + /* Subtract the origin of the bands for this tile, to the subwindow */ + /* of interest band coordinates, so as to get them relative to the */ + /* tile */ + win_ll_x0 = opj_uint_subs(win_ll_x0, tr_ll_x0); + win_ll_y0 = opj_uint_subs(win_ll_y0, tr_ll_y0); + win_ll_x1 = opj_uint_subs(win_ll_x1, tr_ll_x0); + win_ll_y1 = opj_uint_subs(win_ll_y1, tr_ll_y0); + win_hl_x0 = opj_uint_subs(win_hl_x0, tr_hl_x0); + win_hl_x1 = opj_uint_subs(win_hl_x1, tr_hl_x0); + win_lh_y0 = opj_uint_subs(win_lh_y0, tr_lh_y0); + win_lh_y1 = opj_uint_subs(win_lh_y1, tr_lh_y0); + + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.sn, &win_ll_x0, &win_ll_x1); + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)h.dn, &win_hl_x0, &win_hl_x1); + + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.sn, &win_ll_y0, &win_ll_y1); + opj_dwt_segment_grow(filter_width, (OPJ_UINT32)v.dn, &win_lh_y0, &win_lh_y1); + + /* Compute the tile-resolution-based coordinates for the window of interest */ + if (h.cas == 0) { + win_tr_x0 = opj_uint_min(2 * win_ll_x0, 2 * win_hl_x0 + 1); + win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_ll_x1, 2 * win_hl_x1 + 1), rw); + } else { + win_tr_x0 = opj_uint_min(2 * win_hl_x0, 2 * win_ll_x0 + 1); + win_tr_x1 = opj_uint_min(opj_uint_max(2 * win_hl_x1, 2 * win_ll_x1 + 1), rw); + } + + if (v.cas == 0) { + win_tr_y0 = opj_uint_min(2 * win_ll_y0, 2 * win_lh_y0 + 1); + win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_ll_y1, 2 * win_lh_y1 + 1), rh); + } else { + win_tr_y0 = opj_uint_min(2 * win_lh_y0, 2 * win_ll_y0 + 1); + win_tr_y1 = opj_uint_min(opj_uint_max(2 * win_lh_y1, 2 * win_ll_y1 + 1), rh); + } + + h.win_l_x0 = win_ll_x0; + h.win_l_x1 = win_ll_x1; + h.win_h_x0 = win_hl_x0; + h.win_h_x1 = win_hl_x1; + for (j = 0; j + (NB_ELTS_V8 - 1) < rh; j += NB_ELTS_V8) { + if ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) || + (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn && + j < win_lh_y1 + (OPJ_UINT32)v.sn)) { + opj_v8dwt_interleave_partial_h(&h, sa, j, opj_uint_min(NB_ELTS_V8, rh - j)); + opj_v8dwt_decode(&h); + if (!opj_sparse_array_int32_write(sa, + win_tr_x0, j, + win_tr_x1, j + NB_ELTS_V8, + (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0], + NB_ELTS_V8, 1, OPJ_TRUE)) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + } + } + + if (j < rh && + ((j + (NB_ELTS_V8 - 1) >= win_ll_y0 && j < win_ll_y1) || + (j + (NB_ELTS_V8 - 1) >= win_lh_y0 + (OPJ_UINT32)v.sn && + j < win_lh_y1 + (OPJ_UINT32)v.sn))) { + opj_v8dwt_interleave_partial_h(&h, sa, j, rh - j); + opj_v8dwt_decode(&h); + if (!opj_sparse_array_int32_write(sa, + win_tr_x0, j, + win_tr_x1, rh, + (OPJ_INT32*)&h.wavelet[win_tr_x0].f[0], + NB_ELTS_V8, 1, OPJ_TRUE)) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + } + + v.win_l_x0 = win_ll_y0; + v.win_l_x1 = win_ll_y1; + v.win_h_x0 = win_lh_y0; + v.win_h_x1 = win_lh_y1; + for (j = win_tr_x0; j < win_tr_x1; j += NB_ELTS_V8) { + OPJ_UINT32 nb_elts = opj_uint_min(NB_ELTS_V8, win_tr_x1 - j); + + opj_v8dwt_interleave_partial_v(&v, sa, j, nb_elts); + opj_v8dwt_decode(&v); + + if (!opj_sparse_array_int32_write(sa, + j, win_tr_y0, + j + nb_elts, win_tr_y1, + (OPJ_INT32*)&h.wavelet[win_tr_y0].f[0], + 1, NB_ELTS_V8, OPJ_TRUE)) { + /* FIXME event manager error callback */ + opj_sparse_array_int32_free(sa); + opj_aligned_free(h.wavelet); + return OPJ_FALSE; + } + } + } + + { + OPJ_BOOL ret = opj_sparse_array_int32_read(sa, + tr_max->win_x0 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y0 - (OPJ_UINT32)tr_max->y0, + tr_max->win_x1 - (OPJ_UINT32)tr_max->x0, + tr_max->win_y1 - (OPJ_UINT32)tr_max->y0, + tilec->data_win, + 1, tr_max->win_x1 - tr_max->win_x0, + OPJ_TRUE); + assert(ret); + OPJ_UNUSED(ret); + } + opj_sparse_array_int32_free(sa); + + opj_aligned_free(h.wavelet); + return OPJ_TRUE; +} + + +OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t* OPJ_RESTRICT tilec, + OPJ_UINT32 numres) +{ + if (p_tcd->whole_tile_decoding) { + return opj_dwt_decode_tile_97(p_tcd->thread_pool, tilec, numres); + } else { + return opj_dwt_decode_partial_97(tilec, numres); + } +} diff --git a/cpp/3rd_party/openjpeg/openjp2/dwt.h b/cpp/3rd_party/openjpeg/openjp2/dwt.h new file mode 100644 index 0000000000..215061e6b9 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/dwt.h @@ -0,0 +1,120 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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 OPJ_DWT_H +#define OPJ_DWT_H +/** +@file dwt.h +@brief Implementation of a discrete wavelet transform (DWT) + +The functions in DWT.C have for goal to realize forward and inverse discret wavelet +transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in +DWT.C are used by some function in TCD.C. +*/ + +/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ +/*@{*/ + + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ +/** +Forward 5-3 wavelet transform in 2-D. +Apply a reversible DWT transform to a component of an image. +@param p_tcd TCD handle +@param tilec Tile component information (current tile) +*/ +OPJ_BOOL opj_dwt_encode(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t * tilec); + +/** +Inverse 5-3 wavelet transform in 2-D. +Apply a reversible inverse DWT transform to a component of an image. +@param p_tcd TCD handle +@param tilec Tile component information (current tile) +@param numres Number of resolution levels to decode +*/ +OPJ_BOOL opj_dwt_decode(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t* tilec, + OPJ_UINT32 numres); + +/** +Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT. +@param level Level of the wavelet function +@param orient Band of the wavelet function +@return Returns the norm of the wavelet function +*/ +OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient); +/** +Forward 9-7 wavelet transform in 2-D. +Apply an irreversible DWT transform to a component of an image. +@param p_tcd TCD handle +@param tilec Tile component information (current tile) +*/ +OPJ_BOOL opj_dwt_encode_real(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t * tilec); +/** +Inverse 9-7 wavelet transform in 2-D. +Apply an irreversible inverse DWT transform to a component of an image. +@param p_tcd TCD handle +@param tilec Tile component information (current tile) +@param numres Number of resolution levels to decode +*/ +OPJ_BOOL opj_dwt_decode_real(opj_tcd_t *p_tcd, + opj_tcd_tilecomp_t* OPJ_RESTRICT tilec, + OPJ_UINT32 numres); + +/** +Get the norm of a wavelet function of a subband at a specified level for the irreversible 9-7 DWT +@param level Level of the wavelet function +@param orient Band of the wavelet function +@return Returns the norm of the 9-7 wavelet +*/ +OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient); +/** +Explicit calculation of the Quantization Stepsizes +@param tccp Tile-component coding parameters +@param prec Precint analyzed +*/ +void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec); +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_DWT_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/event.c b/cpp/3rd_party/openjpeg/openjp2/event.c new file mode 100644 index 0000000000..aad9d76c98 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/event.c @@ -0,0 +1,151 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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" + +/* ========================================================== + Utility functions + ==========================================================*/ + +#ifdef OPJ_CODE_NOT_USED +#ifndef _WIN32 +static char* +i2a(unsigned i, char *a, unsigned r) +{ + if (i / r > 0) { + a = i2a(i / r, a, r); + } + *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % r]; + return a + 1; +} + +/** + Transforms integer i into an ascii string and stores the result in a; + string is encoded in the base indicated by r. + @param i Number to be converted + @param a String result + @param r Base of value; must be in the range 2 - 36 + @return Returns a +*/ +static char * +_itoa(int i, char *a, int r) +{ + r = ((r < 2) || (r > 36)) ? 10 : r; + if (i < 0) { + *a = '-'; + *i2a(-i, a + 1, r) = 0; + } else { + *i2a(i, a, r) = 0; + } + return a; +} + +#endif /* !_WIN32 */ +#endif + +/* ----------------------------------------------------------------------- */ +/** + * Default callback function. + * Do nothing. + */ +static void opj_default_callback(const char *msg, void *client_data) +{ + OPJ_ARG_NOT_USED(msg); + OPJ_ARG_NOT_USED(client_data); +} + +/* ----------------------------------------------------------------------- */ + + +/* ----------------------------------------------------------------------- */ +OPJ_BOOL opj_event_msg(opj_event_mgr_t* p_event_mgr, OPJ_INT32 event_type, + const char *fmt, ...) +{ +#define OPJ_MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */ + opj_msg_callback msg_handler = 00; + void * l_data = 00; + + if (p_event_mgr != 00) { + switch (event_type) { + case EVT_ERROR: + msg_handler = p_event_mgr->error_handler; + l_data = p_event_mgr->m_error_data; + break; + case EVT_WARNING: + msg_handler = p_event_mgr->warning_handler; + l_data = p_event_mgr->m_warning_data; + break; + case EVT_INFO: + msg_handler = p_event_mgr->info_handler; + l_data = p_event_mgr->m_info_data; + break; + default: + break; + } + if (msg_handler == 00) { + return OPJ_FALSE; + } + } else { + return OPJ_FALSE; + } + + if ((fmt != 00) && (p_event_mgr != 00)) { + va_list arg; + char message[OPJ_MSG_SIZE]; + memset(message, 0, OPJ_MSG_SIZE); + /* initialize the optional parameter list */ + va_start(arg, fmt); + /* parse the format string and put the result in 'message' */ + vsnprintf(message, OPJ_MSG_SIZE, fmt, arg); + /* force zero termination for Windows _vsnprintf() of old MSVC */ + message[OPJ_MSG_SIZE - 1] = '\0'; + /* deinitialize the optional parameter list */ + va_end(arg); + + /* output the message to the user program */ + msg_handler(message, l_data); + } + + return OPJ_TRUE; +} + +void opj_set_default_event_handler(opj_event_mgr_t * p_manager) +{ + p_manager->m_error_data = 00; + p_manager->m_warning_data = 00; + p_manager->m_info_data = 00; + p_manager->error_handler = opj_default_callback; + p_manager->info_handler = opj_default_callback; + p_manager->warning_handler = opj_default_callback; +} + diff --git a/cpp/3rd_party/openjpeg/openjp2/event.h b/cpp/3rd_party/openjpeg/openjp2/event.h new file mode 100644 index 0000000000..d880388d07 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/event.h @@ -0,0 +1,108 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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 OPJ_EVENT_H +#define OPJ_EVENT_H +/** +@file event.h +@brief Implementation of a event callback system + +The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user. +*/ +/** +Message handler object +used for +
    +
  • Error messages +
  • Warning messages +
  • Debugging messages +
+*/ +typedef struct opj_event_mgr { + /** Data to call the event manager upon */ + void * m_error_data; + /** Data to call the event manager upon */ + void * m_warning_data; + /** Data to call the event manager upon */ + void * m_info_data; + /** Error message callback if available, NULL otherwise */ + opj_msg_callback error_handler; + /** Warning message callback if available, NULL otherwise */ + opj_msg_callback warning_handler; + /** Debug message callback if available, NULL otherwise */ + opj_msg_callback info_handler; +} opj_event_mgr_t; + + +#define EVT_ERROR 1 /**< Error event type */ +#define EVT_WARNING 2 /**< Warning event type */ +#define EVT_INFO 4 /**< Debug event type */ + +/** @defgroup EVENT EVENT - Implementation of a event callback system */ +/*@{*/ + +/** @name Exported functions (see also openjpeg.h) */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + + +/* ----------------------------------------------------------------------- */ + +/** + * Write formatted data to a string and send the string to a user callback. + * + * @param event_mgr Event handler + * @param event_type Event type or callback to use to send the message + * @param fmt Format-control string (plus optional arguments) + * + * @return Returns true if successful, returns false otherwise + */ +OPJ_BOOL opj_event_msg(opj_event_mgr_t* event_mgr, OPJ_INT32 event_type, + const char *fmt, ...); +/* ----------------------------------------------------------------------- */ + +/** + * Set the event manager with the default callback function for the 3 levels. + */ +void opj_set_default_event_handler(opj_event_mgr_t * p_manager); + +/* +#ifdef __GNUC__ +#pragma GCC poison printf fprintf +#endif +*/ + +/*@}*/ + +/*@}*/ + +#endif /* OPJ_EVENT_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/function_list.c b/cpp/3rd_party/openjpeg/openjp2/function_list.c new file mode 100644 index 0000000000..e1c1af3897 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/function_list.c @@ -0,0 +1,117 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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" + +/** + * Default size of the validation list, if not sufficient, data will be reallocated with a double size. + */ +#define OPJ_VALIDATION_SIZE 10 + +opj_procedure_list_t * opj_procedure_list_create() +{ + /* memory allocation */ + opj_procedure_list_t * l_validation = (opj_procedure_list_t *) opj_calloc(1, + sizeof(opj_procedure_list_t)); + if (! l_validation) { + return 00; + } + /* initialization */ + l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE; + l_validation->m_procedures = (opj_procedure*)opj_calloc(OPJ_VALIDATION_SIZE, + sizeof(opj_procedure)); + if (! l_validation->m_procedures) { + opj_free(l_validation); + return 00; + } + return l_validation; +} + +void opj_procedure_list_destroy(opj_procedure_list_t * p_list) +{ + if (! p_list) { + return; + } + /* initialization */ + if (p_list->m_procedures) { + opj_free(p_list->m_procedures); + } + opj_free(p_list); +} + +OPJ_BOOL opj_procedure_list_add_procedure(opj_procedure_list_t * + p_validation_list, opj_procedure p_procedure, opj_event_mgr_t* p_manager) +{ + + assert(p_manager != NULL); + + if (p_validation_list->m_nb_max_procedures == + p_validation_list->m_nb_procedures) { + opj_procedure * new_procedures; + + p_validation_list->m_nb_max_procedures += OPJ_VALIDATION_SIZE; + new_procedures = (opj_procedure*)opj_realloc( + p_validation_list->m_procedures, + p_validation_list->m_nb_max_procedures * sizeof(opj_procedure)); + if (! new_procedures) { + opj_free(p_validation_list->m_procedures); + p_validation_list->m_nb_max_procedures = 0; + p_validation_list->m_nb_procedures = 0; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to add a new validation procedure\n"); + return OPJ_FALSE; + } else { + p_validation_list->m_procedures = new_procedures; + } + } + p_validation_list->m_procedures[p_validation_list->m_nb_procedures] = + p_procedure; + ++p_validation_list->m_nb_procedures; + + return OPJ_TRUE; +} + +OPJ_UINT32 opj_procedure_list_get_nb_procedures(opj_procedure_list_t * + p_validation_list) +{ + return p_validation_list->m_nb_procedures; +} + +opj_procedure* opj_procedure_list_get_first_procedure(opj_procedure_list_t * + p_validation_list) +{ + return p_validation_list->m_procedures; +} + +void opj_procedure_list_clear(opj_procedure_list_t * p_validation_list) +{ + p_validation_list->m_nb_procedures = 0; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/function_list.h b/cpp/3rd_party/openjpeg/openjp2/function_list.h new file mode 100644 index 0000000000..81a3954a16 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/function_list.h @@ -0,0 +1,134 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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 OPJ_FUNCTION_LIST_H +#define OPJ_FUNCTION_LIST_H + +/** + * @file function_list.h + * @brief Implementation of a list of procedures. + + * The functions in validation.c aims to have access to a list of procedures. +*/ + +/** @defgroup VAL VAL - validation procedure*/ +/*@{*/ + +/************************************************************************************************** + ***************************************** FORWARD DECLARATION ************************************ + **************************************************************************************************/ + +/** + * declare a function pointer + */ +typedef void (*opj_procedure)(void); + +/** + * A list of procedures. +*/ +typedef struct opj_procedure_list { + /** + * The number of validation procedures. + */ + OPJ_UINT32 m_nb_procedures; + /** + * The number of the array of validation procedures. + */ + OPJ_UINT32 m_nb_max_procedures; + /** + * The array of procedures. + */ + opj_procedure * m_procedures; + +} opj_procedure_list_t; + +/* ----------------------------------------------------------------------- */ + +/** + * Creates a validation list. + * + * @return the newly created validation list. + */ +opj_procedure_list_t * opj_procedure_list_create(void); + +/** + * Destroys a validation list. + * + * @param p_list the list to destroy. + */ +void opj_procedure_list_destroy(opj_procedure_list_t * p_list); + +/** + * Adds a new validation procedure. + * + * @param p_validation_list the list of procedure to modify. + * @param p_procedure the procedure to add. + * @param p_manager the user event manager. + * + * @return OPJ_TRUE if the procedure could be added. + */ +OPJ_BOOL opj_procedure_list_add_procedure(opj_procedure_list_t * + p_validation_list, opj_procedure p_procedure, opj_event_mgr_t* p_manager); + +/** + * Gets the number of validation procedures. + * + * @param p_validation_list the list of procedure to modify. + * + * @return the number of validation procedures. + */ +OPJ_UINT32 opj_procedure_list_get_nb_procedures(opj_procedure_list_t * + p_validation_list); + +/** + * Gets the pointer on the first validation procedure. This function is similar to the C++ + * iterator class to iterate through all the procedures inside the validation list. + * the caller does not take ownership of the pointer. + * + * @param p_validation_list the list of procedure to get the first procedure from. + * + * @return a pointer to the first procedure. + */ +opj_procedure* opj_procedure_list_get_first_procedure(opj_procedure_list_t * + p_validation_list); + + +/** + * Clears the list of validation procedures. + * + * @param p_validation_list the list of procedure to clear. + * + */ +void opj_procedure_list_clear(opj_procedure_list_t * p_validation_list); +/*@}*/ + +#endif /* OPJ_FUNCTION_LIST_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/image.c b/cpp/3rd_party/openjpeg/openjp2/image.c new file mode 100644 index 0000000000..fe37390534 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/image.c @@ -0,0 +1,264 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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" + +opj_image_t* opj_image_create0(void) +{ + opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t)); + return image; +} + +opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, + opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) +{ + OPJ_UINT32 compno; + opj_image_t *image = NULL; + + image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t)); + if (image) { + image->color_space = clrspc; + image->numcomps = numcmpts; + /* allocate memory for the per-component information */ + image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, + sizeof(opj_image_comp_t)); + if (!image->comps) { + /* TODO replace with event manager, breaks API */ + /* fprintf(stderr,"Unable to allocate memory for image.\n"); */ + opj_image_destroy(image); + return NULL; + } + /* create the individual image components */ + for (compno = 0; compno < numcmpts; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + comp->dx = cmptparms[compno].dx; + comp->dy = cmptparms[compno].dy; + comp->w = cmptparms[compno].w; + comp->h = cmptparms[compno].h; + comp->x0 = cmptparms[compno].x0; + comp->y0 = cmptparms[compno].y0; + comp->prec = cmptparms[compno].prec; + comp->bpp = cmptparms[compno].bpp; + comp->sgnd = cmptparms[compno].sgnd; + if (comp->h != 0 && + (OPJ_SIZE_T)comp->w > SIZE_MAX / comp->h / sizeof(OPJ_INT32)) { + /* TODO event manager */ + opj_image_destroy(image); + return NULL; + } + comp->data = (OPJ_INT32*) opj_image_data_alloc( + (size_t)comp->w * comp->h * sizeof(OPJ_INT32)); + if (!comp->data) { + /* TODO replace with event manager, breaks API */ + /* fprintf(stderr,"Unable to allocate memory for image.\n"); */ + opj_image_destroy(image); + return NULL; + } + memset(comp->data, 0, (size_t)comp->w * comp->h * sizeof(OPJ_INT32)); + } + } + + return image; +} + +void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) +{ + if (image) { + if (image->comps) { + OPJ_UINT32 compno; + + /* image components */ + for (compno = 0; compno < image->numcomps; compno++) { + opj_image_comp_t *image_comp = &(image->comps[compno]); + if (image_comp->data) { + opj_image_data_free(image_comp->data); + } + } + opj_free(image->comps); + } + + if (image->icc_profile_buf) { + opj_free(image->icc_profile_buf); + } + + opj_free(image); + } +} + +/** + * Updates the components characteristics of the image from the coding parameters. + * + * @param p_image_header the image header to update. + * @param p_cp the coding parameters from which to update the image. + */ +void opj_image_comp_header_update(opj_image_t * p_image_header, + const struct opj_cp * p_cp) +{ + OPJ_UINT32 i, l_width, l_height; + OPJ_UINT32 l_x0, l_y0, l_x1, l_y1; + OPJ_UINT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1; + opj_image_comp_t* l_img_comp = NULL; + + l_x0 = opj_uint_max(p_cp->tx0, p_image_header->x0); + l_y0 = opj_uint_max(p_cp->ty0, p_image_header->y0); + l_x1 = p_cp->tx0 + (p_cp->tw - 1U) * + p_cp->tdx; /* validity of p_cp members used here checked in opj_j2k_read_siz. Can't overflow. */ + l_y1 = p_cp->ty0 + (p_cp->th - 1U) * p_cp->tdy; /* can't overflow */ + l_x1 = opj_uint_min(opj_uint_adds(l_x1, p_cp->tdx), + p_image_header->x1); /* use add saturated to prevent overflow */ + l_y1 = opj_uint_min(opj_uint_adds(l_y1, p_cp->tdy), + p_image_header->y1); /* use add saturated to prevent overflow */ + + l_img_comp = p_image_header->comps; + for (i = 0; i < p_image_header->numcomps; ++i) { + l_comp_x0 = opj_uint_ceildiv(l_x0, l_img_comp->dx); + l_comp_y0 = opj_uint_ceildiv(l_y0, l_img_comp->dy); + l_comp_x1 = opj_uint_ceildiv(l_x1, l_img_comp->dx); + l_comp_y1 = opj_uint_ceildiv(l_y1, l_img_comp->dy); + l_width = opj_uint_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor); + l_height = opj_uint_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor); + l_img_comp->w = l_width; + l_img_comp->h = l_height; + l_img_comp->x0 = l_comp_x0; + l_img_comp->y0 = l_comp_y0; + ++l_img_comp; + } +} + + +/** + * Copy only header of image and its component header (no data are copied) + * if dest image have data, they will be freed + * + * @param p_image_src the src image + * @param p_image_dest the dest image + * + */ +void opj_copy_image_header(const opj_image_t* p_image_src, + opj_image_t* p_image_dest) +{ + OPJ_UINT32 compno; + + /* preconditions */ + assert(p_image_src != 00); + assert(p_image_dest != 00); + + p_image_dest->x0 = p_image_src->x0; + p_image_dest->y0 = p_image_src->y0; + p_image_dest->x1 = p_image_src->x1; + p_image_dest->y1 = p_image_src->y1; + + if (p_image_dest->comps) { + for (compno = 0; compno < p_image_dest->numcomps; compno++) { + opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]); + if (image_comp->data) { + opj_image_data_free(image_comp->data); + } + } + opj_free(p_image_dest->comps); + p_image_dest->comps = NULL; + } + + p_image_dest->numcomps = p_image_src->numcomps; + + p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * + sizeof(opj_image_comp_t)); + if (!p_image_dest->comps) { + p_image_dest->comps = NULL; + p_image_dest->numcomps = 0; + return; + } + + for (compno = 0; compno < p_image_dest->numcomps; compno++) { + memcpy(&(p_image_dest->comps[compno]), + &(p_image_src->comps[compno]), + sizeof(opj_image_comp_t)); + p_image_dest->comps[compno].data = NULL; + } + + p_image_dest->color_space = p_image_src->color_space; + p_image_dest->icc_profile_len = p_image_src->icc_profile_len; + + if (p_image_dest->icc_profile_len) { + p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc( + p_image_dest->icc_profile_len); + if (!p_image_dest->icc_profile_buf) { + p_image_dest->icc_profile_buf = NULL; + p_image_dest->icc_profile_len = 0; + return; + } + memcpy(p_image_dest->icc_profile_buf, + p_image_src->icc_profile_buf, + p_image_src->icc_profile_len); + } else { + p_image_dest->icc_profile_buf = NULL; + } + + return; +} + +opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, + opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) +{ + OPJ_UINT32 compno; + opj_image_t *image = 00; + + image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t)); + if (image) { + + image->color_space = clrspc; + image->numcomps = numcmpts; + + /* allocate memory for the per-component information */ + image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, + sizeof(opj_image_comp_t)); + if (!image->comps) { + opj_image_destroy(image); + return 00; + } + + /* create the individual image components */ + for (compno = 0; compno < numcmpts; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + comp->dx = cmptparms[compno].dx; + comp->dy = cmptparms[compno].dy; + comp->w = cmptparms[compno].w; + comp->h = cmptparms[compno].h; + comp->x0 = cmptparms[compno].x0; + comp->y0 = cmptparms[compno].y0; + comp->prec = cmptparms[compno].prec; + comp->sgnd = cmptparms[compno].sgnd; + comp->data = 0; + } + } + + return image; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/image.h b/cpp/3rd_party/openjpeg/openjp2/image.h new file mode 100644 index 0000000000..bad83c6135 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/image.h @@ -0,0 +1,70 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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 OPJ_IMAGE_H +#define OPJ_IMAGE_H +/** +@file image.h +@brief Implementation of operations on images (IMAGE) + +The functions in IMAGE.C have for goal to realize operations on images. +*/ + +struct opj_image; +struct opj_cp; + +/** @defgroup IMAGE IMAGE - Implementation of operations on images */ +/*@{*/ + +/** + * Create an empty image + * + * @return returns an empty image if successful, returns NULL otherwise + */ +opj_image_t* opj_image_create0(void); + + + +/** + * Updates the components characteristics of the image from the coding parameters. + * + * @param p_image_header the image header to update. + * @param p_cp the coding parameters from which to update the image. + */ +void opj_image_comp_header_update(opj_image_t * p_image, + const struct opj_cp* p_cp); + +void opj_copy_image_header(const opj_image_t* p_image_src, + opj_image_t* p_image_dest); + +/*@}*/ + +#endif /* OPJ_IMAGE_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/invert.c b/cpp/3rd_party/openjpeg/openjp2/invert.c new file mode 100644 index 0000000000..89f607155f --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/invert.c @@ -0,0 +1,295 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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" + +/** + * LUP decomposition + */ +static OPJ_BOOL opj_lupDecompose(OPJ_FLOAT32 * matrix, + OPJ_UINT32 * permutations, + OPJ_FLOAT32 * p_swap_area, + OPJ_UINT32 nb_compo); +/** + * LUP solving + */ +static void opj_lupSolve(OPJ_FLOAT32 * pResult, + OPJ_FLOAT32* pMatrix, + OPJ_FLOAT32* pVector, + OPJ_UINT32* pPermutations, + OPJ_UINT32 nb_compo, + OPJ_FLOAT32 * p_intermediate_data); + +/** + *LUP inversion (call with the result of lupDecompose) + */ +static void opj_lupInvert(OPJ_FLOAT32 * pSrcMatrix, + OPJ_FLOAT32 * pDestMatrix, + OPJ_UINT32 nb_compo, + OPJ_UINT32 * pPermutations, + OPJ_FLOAT32 * p_src_temp, + OPJ_FLOAT32 * p_dest_temp, + OPJ_FLOAT32 * p_swap_area); + +/* +========================================================== + Matric inversion interface +========================================================== +*/ +/** + * Matrix inversion. + */ +OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix, + OPJ_FLOAT32 * pDestMatrix, + OPJ_UINT32 nb_compo) +{ + OPJ_BYTE * l_data = 00; + OPJ_UINT32 l_permutation_size = nb_compo * (OPJ_UINT32)sizeof(OPJ_UINT32); + OPJ_UINT32 l_swap_size = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32); + OPJ_UINT32 l_total_size = l_permutation_size + 3 * l_swap_size; + OPJ_UINT32 * lPermutations = 00; + OPJ_FLOAT32 * l_double_data = 00; + + l_data = (OPJ_BYTE *) opj_malloc(l_total_size); + if (l_data == 0) { + return OPJ_FALSE; + } + lPermutations = (OPJ_UINT32 *) l_data; + l_double_data = (OPJ_FLOAT32 *)(l_data + l_permutation_size); + memset(lPermutations, 0, l_permutation_size); + + if (! opj_lupDecompose(pSrcMatrix, lPermutations, l_double_data, nb_compo)) { + opj_free(l_data); + return OPJ_FALSE; + } + + opj_lupInvert(pSrcMatrix, pDestMatrix, nb_compo, lPermutations, l_double_data, + l_double_data + nb_compo, l_double_data + 2 * nb_compo); + opj_free(l_data); + + return OPJ_TRUE; +} + + +/* +========================================================== + Local functions +========================================================== +*/ +static OPJ_BOOL opj_lupDecompose(OPJ_FLOAT32 * matrix, + OPJ_UINT32 * permutations, + OPJ_FLOAT32 * p_swap_area, + OPJ_UINT32 nb_compo) +{ + OPJ_UINT32 * tmpPermutations = permutations; + OPJ_UINT32 * dstPermutations; + OPJ_UINT32 k2 = 0, t; + OPJ_FLOAT32 temp; + OPJ_UINT32 i, j, k; + OPJ_FLOAT32 p; + OPJ_UINT32 lLastColum = nb_compo - 1; + OPJ_UINT32 lSwapSize = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32); + OPJ_FLOAT32 * lTmpMatrix = matrix; + OPJ_FLOAT32 * lColumnMatrix, * lDestMatrix; + OPJ_UINT32 offset = 1; + OPJ_UINT32 lStride = nb_compo - 1; + + /*initialize permutations */ + for (i = 0; i < nb_compo; ++i) { + *tmpPermutations++ = i; + } + /* now make a pivot with column switch */ + tmpPermutations = permutations; + for (k = 0; k < lLastColum; ++k) { + p = 0.0; + + /* take the middle element */ + lColumnMatrix = lTmpMatrix + k; + + /* make permutation with the biggest value in the column */ + for (i = k; i < nb_compo; ++i) { + temp = ((*lColumnMatrix > 0) ? *lColumnMatrix : -(*lColumnMatrix)); + if (temp > p) { + p = temp; + k2 = i; + } + /* next line */ + lColumnMatrix += nb_compo; + } + + /* a whole rest of 0 -> non singular */ + if (p == 0.0) { + return OPJ_FALSE; + } + + /* should we permute ? */ + if (k2 != k) { + /*exchange of line */ + /* k2 > k */ + dstPermutations = tmpPermutations + k2 - k; + /* swap indices */ + t = *tmpPermutations; + *tmpPermutations = *dstPermutations; + *dstPermutations = t; + + /* and swap entire line. */ + lColumnMatrix = lTmpMatrix + (k2 - k) * nb_compo; + memcpy(p_swap_area, lColumnMatrix, lSwapSize); + memcpy(lColumnMatrix, lTmpMatrix, lSwapSize); + memcpy(lTmpMatrix, p_swap_area, lSwapSize); + } + + /* now update data in the rest of the line and line after */ + lDestMatrix = lTmpMatrix + k; + lColumnMatrix = lDestMatrix + nb_compo; + /* take the middle element */ + temp = *(lDestMatrix++); + + /* now compute up data (i.e. coeff up of the diagonal). */ + for (i = offset; i < nb_compo; ++i) { + /*lColumnMatrix; */ + /* divide the lower column elements by the diagonal value */ + + /* matrix[i][k] /= matrix[k][k]; */ + /* p = matrix[i][k] */ + p = *lColumnMatrix / temp; + *(lColumnMatrix++) = p; + + for (j = /* k + 1 */ offset; j < nb_compo; ++j) { + /* matrix[i][j] -= matrix[i][k] * matrix[k][j]; */ + *(lColumnMatrix++) -= p * (*(lDestMatrix++)); + } + /* come back to the k+1th element */ + lDestMatrix -= lStride; + /* go to kth element of the next line */ + lColumnMatrix += k; + } + + /* offset is now k+2 */ + ++offset; + /* 1 element less for stride */ + --lStride; + /* next line */ + lTmpMatrix += nb_compo; + /* next permutation element */ + ++tmpPermutations; + } + return OPJ_TRUE; +} + +static void opj_lupSolve(OPJ_FLOAT32 * pResult, + OPJ_FLOAT32 * pMatrix, + OPJ_FLOAT32 * pVector, + OPJ_UINT32* pPermutations, + OPJ_UINT32 nb_compo, OPJ_FLOAT32 * p_intermediate_data) +{ + OPJ_INT32 k; + OPJ_UINT32 i, j; + OPJ_FLOAT32 sum; + OPJ_FLOAT32 u; + OPJ_UINT32 lStride = nb_compo + 1; + OPJ_FLOAT32 * lCurrentPtr; + OPJ_FLOAT32 * lIntermediatePtr; + OPJ_FLOAT32 * lDestPtr; + OPJ_FLOAT32 * lTmpMatrix; + OPJ_FLOAT32 * lLineMatrix = pMatrix; + OPJ_FLOAT32 * lBeginPtr = pResult + nb_compo - 1; + OPJ_FLOAT32 * lGeneratedData; + OPJ_UINT32 * lCurrentPermutationPtr = pPermutations; + + + lIntermediatePtr = p_intermediate_data; + lGeneratedData = p_intermediate_data + nb_compo - 1; + + for (i = 0; i < nb_compo; ++i) { + sum = 0.0; + lCurrentPtr = p_intermediate_data; + lTmpMatrix = lLineMatrix; + for (j = 1; j <= i; ++j) { + /* sum += matrix[i][j-1] * y[j-1]; */ + sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++)); + } + /*y[i] = pVector[pPermutations[i]] - sum; */ + *(lIntermediatePtr++) = pVector[*(lCurrentPermutationPtr++)] - sum; + lLineMatrix += nb_compo; + } + + /* we take the last point of the matrix */ + lLineMatrix = pMatrix + nb_compo * nb_compo - 1; + + /* and we take after the last point of the destination vector */ + lDestPtr = pResult + nb_compo; + + + assert(nb_compo != 0); + for (k = (OPJ_INT32)nb_compo - 1; k != -1 ; --k) { + sum = 0.0; + lTmpMatrix = lLineMatrix; + u = *(lTmpMatrix++); + lCurrentPtr = lDestPtr--; + for (j = (OPJ_UINT32)(k + 1); j < nb_compo; ++j) { + /* sum += matrix[k][j] * x[j] */ + sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++)); + } + /*x[k] = (y[k] - sum) / u; */ + *(lBeginPtr--) = (*(lGeneratedData--) - sum) / u; + lLineMatrix -= lStride; + } +} + + +static void opj_lupInvert(OPJ_FLOAT32 * pSrcMatrix, + OPJ_FLOAT32 * pDestMatrix, + OPJ_UINT32 nb_compo, + OPJ_UINT32 * pPermutations, + OPJ_FLOAT32 * p_src_temp, + OPJ_FLOAT32 * p_dest_temp, + OPJ_FLOAT32 * p_swap_area) +{ + OPJ_UINT32 j, i; + OPJ_FLOAT32 * lCurrentPtr; + OPJ_FLOAT32 * lLineMatrix = pDestMatrix; + OPJ_UINT32 lSwapSize = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32); + + for (j = 0; j < nb_compo; ++j) { + lCurrentPtr = lLineMatrix++; + memset(p_src_temp, 0, lSwapSize); + p_src_temp[j] = 1.0; + opj_lupSolve(p_dest_temp, pSrcMatrix, p_src_temp, pPermutations, nb_compo, + p_swap_area); + + for (i = 0; i < nb_compo; ++i) { + *(lCurrentPtr) = p_dest_temp[i]; + lCurrentPtr += nb_compo; + } + } +} + diff --git a/cpp/3rd_party/openjpeg/openjp2/invert.h b/cpp/3rd_party/openjpeg/openjp2/invert.h new file mode 100644 index 0000000000..7040213559 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/invert.h @@ -0,0 +1,64 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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 OPJ_INVERT_H +#define OPJ_INVERT_H +/** +@file invert.h +@brief Implementation of the matrix inversion + +The function in INVERT.H compute a matrix inversion with a LUP method +*/ + +/** @defgroup INVERT INVERT - Implementation of a matrix inversion */ +/*@{*/ +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** + * Calculates a n x n double matrix inversion with a LUP method. Data is aligned, rows after rows (or columns after columns). + * The function does not take ownership of any memory block, data must be fred by the user. + * + * @param pSrcMatrix the matrix to invert. + * @param pDestMatrix data to store the inverted matrix. + * @param nb_compo size of the matrix + * @return OPJ_TRUE if the inversion is successful, OPJ_FALSE if the matrix is singular. + */ +OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix, + OPJ_FLOAT32 * pDestMatrix, + OPJ_UINT32 nb_compo); +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_INVERT_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/j2k.c b/cpp/3rd_party/openjpeg/openjp2/j2k.c new file mode 100644 index 0000000000..8e343ab2e3 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/j2k.c @@ -0,0 +1,13014 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2010-2011, Kaori Hagihara + * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France + * Copyright (c) 2012, CS Systemes d'Information, France + * Copyright (c) 2017, IntoPIX SA + * 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" + +/** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ +/*@{*/ + +/** @name Local static functions */ +/*@{*/ + +/** + * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures. + */ +static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager); + +/** + * The read header procedure. + */ +static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * The default encoding validation procedure without any extension. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * The default decoding validation procedure without any extension. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager); + +/** + * The mct encoding validation procedure. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Builds the tcd decoder to use to decode tile. + */ +static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); +/** + * Builds the tcd encoder to use to encode tile. + */ +static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Creates a tile-coder encoder. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Excutes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param p_j2k the jpeg2000 codec to execute the procedures on. + * @param p_stream the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Updates the rates of the tcp. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Copies the decoding tile parameters onto all the tile parameters. + * Creates also the tile decoder. + */ +static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Destroys the memory associated with the decoding of headers. + */ +static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads the lookup table containing all the marker, status and action, and returns the handler associated + * with the marker value. + * @param p_id Marker value to look up + * + * @return the handler associated with the id. +*/ +static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler( + OPJ_UINT32 p_id); + +/** + * Destroys a tile coding parameter structure. + * + * @param p_tcp the tile coding parameter to destroy. + */ +static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp); + +/** + * Destroys the data inside a tile coding parameter structure. + * + * @param p_tcp the tile coding parameter which contain data to destroy. + */ +static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp); + +/** + * Destroys a coding parameter structure. + * + * @param p_cp the coding parameter to destroy. + */ +static void opj_j2k_cp_destroy(opj_cp_t *p_cp); + +/** + * Compare 2 a SPCod/ SPCoc elements, i.e. the coding style of a given component of a tile. + * + * @param p_j2k J2K codec. + * @param p_tile_no Tile number + * @param p_first_comp_no The 1st component number to compare. + * @param p_second_comp_no The 1st component number to compare. + * + * @return OPJ_TRUE if SPCdod are equals. + */ +static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); + +/** + * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. + * + * @param p_j2k J2K codec. + * @param p_tile_no FIXME DOC + * @param p_comp_no the component number to output. + * @param p_data FIXME DOC + * @param p_header_size FIXME DOC + * @param p_manager the user event manager. + * + * @return FIXME DOC +*/ +static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. + * + * @param p_j2k the J2K codec. + * @param p_tile_no the tile index. + * @param p_comp_no the component being outputted. + * + * @return the number of bytes taken by the SPCod element. + */ +static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no); + +/** + * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. + * @param p_j2k the jpeg2000 codec. + * @param compno FIXME DOC + * @param p_header_data the data contained in the COM box. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_tile_no the tile index. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. + * + * @return the number of bytes taken by the SPCod element. + */ +static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no); + +/** + * Compares 2 SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_j2k J2K codec. + * @param p_tile_no the tile to output. + * @param p_first_comp_no the first component number to compare. + * @param p_second_comp_no the second component number to compare. + * + * @return OPJ_TRUE if equals. + */ +static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); + + +/** + * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + * +*/ +static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Updates the Tile Length Marker. + */ +static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size); + +/** + * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_j2k J2K codec. + * @param compno the component number to output. + * @param p_header_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_manager the user event manager. + * +*/ +static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Copies the tile component parameters of all the component from the first tile component. + * + * @param p_j2k the J2k codec. + */ +static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k); + +/** + * Copies the tile quantization parameters of all the component from the first tile component. + * + * @param p_j2k the J2k codec. + */ +static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k); + +/** + * Reads the tiles. + */ +static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, + opj_image_t* p_output_image); + +static void opj_get_tile_dimensions(opj_image_t * l_image, + opj_tcd_tilecomp_t * l_tilec, + opj_image_comp_t * l_img_comp, + OPJ_UINT32* l_size_comp, + OPJ_UINT32* l_width, + OPJ_UINT32* l_height, + OPJ_UINT32* l_offset_x, + OPJ_UINT32* l_offset_y, + OPJ_UINT32* l_image_width, + OPJ_UINT32* l_stride, + OPJ_UINT32* l_tile_offset); + +static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data); + +static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Sets up the procedures to do on writing header. + * Developers wanting to extend the library can add their own writing procedures. + */ +static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager); + +static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager); + +/** + * Gets the offset of the header. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); + +/* + * ----------------------------------------------------------------------- + * ----------------------------------------------------------------------- + * ----------------------------------------------------------------------- + */ + +/** + * Writes the SOC marker (Start Of Codestream) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a SOC marker (Start of Codestream) + * @param p_j2k the jpeg2000 file codec. + * @param p_stream XXX needs data + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes the SIZ marker (image and tile size) + * + * @param p_j2k J2K codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a SIZ marker (image and tile size) + * @param p_j2k the jpeg2000 file codec. + * @param p_header_data the data contained in the SIZ box. + * @param p_header_size the size of the data contained in the SIZ marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the COM marker (comment) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a COM marker (comments) + * @param p_j2k the jpeg2000 file codec. + * @param p_header_data the data contained in the COM box. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +/** + * Writes the COD marker (Coding style default) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a COD marker (Coding style defaults) + * @param p_header_data the data contained in the COD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COD marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Compares 2 COC markers (Coding style component) + * + * @param p_j2k J2K codec. + * @param p_first_comp_no the index of the first component to compare. + * @param p_second_comp_no the index of the second component to compare. + * + * @return OPJ_TRUE if equals + */ +static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); + +/** + * Writes the COC marker (Coding style component) + * + * @param p_j2k J2K codec. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes the COC marker (Coding style component) + * + * @param p_j2k J2K codec. + * @param p_comp_no the index of the component to output. + * @param p_data FIXME DOC + * @param p_data_written FIXME DOC + * @param p_manager the user event manager. +*/ +static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager); + +/** + * Gets the maximum size taken by a coc. + * + * @param p_j2k the jpeg2000 codec to use. + */ +static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k); + +/** + * Reads a COC marker (Coding Style Component) + * @param p_header_data the data contained in the COC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the QCD marker (quantization default) + * + * @param p_j2k J2K codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a QCD marker (Quantization defaults) + * @param p_header_data the data contained in the QCD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCD marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Compare QCC markers (quantization component) + * + * @param p_j2k J2K codec. + * @param p_first_comp_no the index of the first component to compare. + * @param p_second_comp_no the index of the second component to compare. + * + * @return OPJ_TRUE if equals. + */ +static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no); + +/** + * Writes the QCC marker (quantization component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes the QCC marker (quantization component) + * + * @param p_j2k J2K codec. + * @param p_comp_no the index of the component to output. + * @param p_data FIXME DOC + * @param p_data_written the stream to write data to. + * @param p_manager the user event manager. +*/ +static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager); + +/** + * Gets the maximum size taken by a qcc. + */ +static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k); + +/** + * Reads a QCC marker (Quantization component) + * @param p_header_data the data contained in the QCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +/** + * Writes the POC marker (Progression Order Change) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); +/** + * Writes the POC marker (Progression Order Change) + * + * @param p_j2k J2K codec. + * @param p_data FIXME DOC + * @param p_data_written the stream to write data to. + * @param p_manager the user event manager. + */ +static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager); +/** + * Gets the maximum size taken by the writing of a POC. + */ +static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k); + +/** + * Reads a POC marker (Progression Order Change) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. + */ +static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k); + +/** + * Gets the maximum size taken by the headers of the SOT. + * + * @param p_j2k the jpeg2000 codec to use. + */ +static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); + +/** + * Reads a CRG marker (Component registration) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +/** + * Reads a TLM marker (Tile Length Marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the updated tlm. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a PLM marker (Packet length, main header marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +/** + * Reads a PLT marker (Packet length, tile-part header) + * + * @param p_header_data the data contained in the PLT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PLT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Reads a PPM marker (Packed headers, main header) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. + */ + +static OPJ_BOOL opj_j2k_read_ppm( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Merges all PPM markers read (Packed headers, main header) + * + * @param p_cp main coding parameters. + * @param p_manager the user event manager. + */ +static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager); + +/** + * Reads a PPT marker (Packed packet headers, tile-part header) + * + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Merges all PPT markers read (Packed headers, tile-part header) + * + * @param p_tcp the tile. + * @param p_manager the user event manager. + */ +static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, + opj_event_mgr_t * p_manager); + + +/** + * Writes the TLM marker (Tile Length Marker) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes the SOT marker (Start of tile-part) + * + * @param p_j2k J2K codec. + * @param p_data Output buffer + * @param total_data_size Output buffer size + * @param p_data_written Number of bytes written into stream + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 total_data_size, + OPJ_UINT32 * p_data_written, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads values from a SOT marker (Start of tile-part) + * + * the j2k decoder state is not affected. No side effects, no checks except for p_header_size. + * + * @param p_header_data the data contained in the SOT marker. + * @param p_header_size the size of the data contained in the SOT marker. + * @param p_tile_no Isot. + * @param p_tot_len Psot. + * @param p_current_part TPsot. + * @param p_num_parts TNsot. + * @param p_manager the user event manager. + */ +static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + OPJ_UINT32* p_tile_no, + OPJ_UINT32* p_tot_len, + OPJ_UINT32* p_current_part, + OPJ_UINT32* p_num_parts, + opj_event_mgr_t * p_manager); +/** + * Reads a SOT marker (Start of tile-part) + * + * @param p_header_data the data contained in the SOT marker. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +/** + * Writes the SOD marker (Start of data) + * + * This also writes optional PLT markers (before SOD) + * + * @param p_j2k J2K codec. + * @param p_tile_coder FIXME DOC + * @param p_data FIXME DOC + * @param p_data_written FIXME DOC + * @param total_data_size FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, + opj_tcd_t * p_tile_coder, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 total_data_size, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a SOD marker (Start Of Data) + * + * @param p_j2k the jpeg2000 codec. + * @param p_stream FIXME DOC + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size) +{ + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, + p_j2k->m_current_tile_number, 1); /* PSOT */ + ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current; + + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current, + p_tile_part_size, 4); /* PSOT */ + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; +} + +/** + * Writes the RGN marker (Region Of Interest) + * + * @param p_tile_no the tile to output + * @param p_comp_no the component to output + * @param nb_comps the number of components + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_UINT32 nb_comps, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a RGN marker (Region Of Interest) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the EOC marker (End of Codestream) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +#if 0 +/** + * Reads a EOC marker (End Of Codestream) + * + * @param p_j2k the jpeg2000 codec. + * @param p_stream FIXME DOC + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); +#endif + +/** + * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Inits the Info + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** +Add main header marker information +@param cstr_index Codestream information structure +@param type marker type +@param pos byte offset of marker segment +@param len length of marker segment + */ +static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, + OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; +/** +Add tile header marker information +@param tileno tile index number +@param cstr_index Codestream information structure +@param type marker type +@param pos byte offset of marker segment +@param len length of marker segment + */ +static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, + opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, + OPJ_UINT32 len); + +/** + * Reads an unknown marker + * + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream object to read from. + * @param output_marker FIXME DOC + * @param p_manager the user event manager. + * + * @return true if the marker could be deduced. +*/ +static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + OPJ_UINT32 *output_marker, + opj_event_mgr_t * p_manager); + +/** + * Writes the MCT marker (Multiple Component Transform) + * + * @param p_j2k J2K codec. + * @param p_mct_record FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, + opj_mct_data_t * p_mct_record, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a MCT marker (Multiple Component Transform) + * + * @param p_header_data the data contained in the MCT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the MCC marker (Multiple Component Collection) + * + * @param p_j2k J2K codec. + * @param p_mcc_record FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, + opj_simple_mcc_decorrelation_data_t * p_mcc_record, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a MCC marker (Multiple Component Collection) + * + * @param p_header_data the data contained in the MCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the MCO marker (Multiple component transformation ordering) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a MCO marker (Multiple Component Transform Ordering) + * + * @param p_header_data the data contained in the MCO box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCO marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, + OPJ_UINT32 p_index); + +static void opj_j2k_read_int16_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_read_int32_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_read_float32_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_read_float64_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); + +static void opj_j2k_read_int16_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_read_int32_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_read_float32_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_read_float64_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); + +static void opj_j2k_write_float_to_int16(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_write_float_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_write_float_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void opj_j2k_write_float_to_float64(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); + +/** + * Ends the encoding, i.e. frees memory. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes the CBD marker (Component bit depth definition) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a CBD marker (Component bit depth definition) + * @param p_header_data the data contained in the CBD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the CBD marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + + +/** + * Writes COC marker for each component. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes QCC marker for each component. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes regions of interests. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes EPC ???? + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Checks the progression order changes values. Tells of the poc given as input are valid. + * A nice message is outputted at errors. + * + * @param p_pocs the progression order changes. + * @param tileno the tile number of interest + * @param p_nb_pocs the number of progression order changes. + * @param p_nb_resolutions the number of resolutions. + * @param numcomps the number of components + * @param numlayers the number of layers. + * @param p_manager the user event manager. + * + * @return true if the pocs are valid. + */ +static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, + OPJ_UINT32 tileno, + OPJ_UINT32 p_nb_pocs, + OPJ_UINT32 p_nb_resolutions, + OPJ_UINT32 numcomps, + OPJ_UINT32 numlayers, + opj_event_mgr_t * p_manager); + +/** + * Gets the number of tile parts used for the given change of progression (if any) and the given tile. + * + * @param cp the coding parameters. + * @param pino the offset of the given poc (i.e. its position in the coding parameter). + * @param tileno the given tile. + * + * @return the number of tile parts. + */ +static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, + OPJ_UINT32 tileno); + +/** + * Calculates the total number of tile parts needed by the encoder to + * encode such an image. If not enough memory is available, then the function return false. + * + * @param p_nb_tiles pointer that will hold the number of tile parts. + * @param cp the coding parameters for the image. + * @param image the image to encode. + * @param p_j2k the p_j2k encoder. + * @param p_manager the user event manager. + * + * @return true if the function was successful, false else. + */ +static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, + opj_cp_t *cp, + OPJ_UINT32 * p_nb_tiles, + opj_image_t *image, + opj_event_mgr_t * p_manager); + +static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream); + +static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream); + +static opj_codestream_index_t* opj_j2k_create_cstr_index(void); + +static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp); + +static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp); + +static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres); + +static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, + opj_image_t *image, opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, + opj_event_mgr_t *p_manager); + +static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, + opj_image_t *image, opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t *p_manager); + +/** + * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254. + * + * @param p_stream the stream to read data from. + * @param tile_no tile number we're looking for. + * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction. + * @param p_manager the user event manager. + * + * @return true if the function was successful, false else. + */ +static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t + *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, + opj_event_mgr_t * p_manager); + +/*@}*/ + +/*@}*/ + +/* ----------------------------------------------------------------------- */ +typedef struct j2k_prog_order { + OPJ_PROG_ORDER enum_prog; + char str_prog[5]; +} j2k_prog_order_t; + +static const j2k_prog_order_t j2k_prog_order_list[] = { + {OPJ_CPRL, "CPRL"}, + {OPJ_LRCP, "LRCP"}, + {OPJ_PCRL, "PCRL"}, + {OPJ_RLCP, "RLCP"}, + {OPJ_RPCL, "RPCL"}, + {(OPJ_PROG_ORDER) - 1, ""} +}; + +/** + * FIXME DOC + */ +static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = { + 2, + 4, + 4, + 8 +}; + +typedef void (* opj_j2k_mct_function)(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem); + +static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = { + opj_j2k_read_int16_to_float, + opj_j2k_read_int32_to_float, + opj_j2k_read_float32_to_float, + opj_j2k_read_float64_to_float +}; + +static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = { + opj_j2k_read_int16_to_int32, + opj_j2k_read_int32_to_int32, + opj_j2k_read_float32_to_int32, + opj_j2k_read_float64_to_int32 +}; + +static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = { + opj_j2k_write_float_to_int16, + opj_j2k_write_float_to_int32, + opj_j2k_write_float_to_float, + opj_j2k_write_float_to_float64 +}; + +typedef struct opj_dec_memory_marker_handler { + /** marker value */ + OPJ_UINT32 id; + /** value of the state when the marker can appear */ + OPJ_UINT32 states; + /** action linked to the marker */ + OPJ_BOOL(*handler)(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +} +opj_dec_memory_marker_handler_t; + +static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = +{ + {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot}, + {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod}, + {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc}, + {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn}, + {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd}, + {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc}, + {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc}, + {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz}, + {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm}, + {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm}, + {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt}, + {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm}, + {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt}, + {J2K_MS_SOP, 0, 0}, + {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg}, + {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com}, + {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct}, + {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd}, + {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc}, + {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco}, +#ifdef USE_JPWL +#ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */ + {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, + {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, + {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, + {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, +#endif +#endif /* USE_JPWL */ +#ifdef USE_JPSEC + {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, + {J2K_MS_INSEC, 0, j2k_read_insec} +#endif /* USE_JPSEC */ + {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/ +}; + +static void opj_j2k_read_int16_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_bytes(l_src_data, &l_temp, 2); + + l_src_data += sizeof(OPJ_INT16); + + *(l_dest_data++) = (OPJ_FLOAT32) l_temp; + } +} + +static void opj_j2k_read_int32_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_bytes(l_src_data, &l_temp, 4); + + l_src_data += sizeof(OPJ_INT32); + + *(l_dest_data++) = (OPJ_FLOAT32) l_temp; + } +} + +static void opj_j2k_read_float32_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_FLOAT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_float(l_src_data, &l_temp); + + l_src_data += sizeof(OPJ_FLOAT32); + + *(l_dest_data++) = l_temp; + } +} + +static void opj_j2k_read_float64_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_FLOAT64 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_double(l_src_data, &l_temp); + + l_src_data += sizeof(OPJ_FLOAT64); + + *(l_dest_data++) = (OPJ_FLOAT32) l_temp; + } +} + +static void opj_j2k_read_int16_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_bytes(l_src_data, &l_temp, 2); + + l_src_data += sizeof(OPJ_INT16); + + *(l_dest_data++) = (OPJ_INT32) l_temp; + } +} + +static void opj_j2k_read_int32_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_bytes(l_src_data, &l_temp, 4); + + l_src_data += sizeof(OPJ_INT32); + + *(l_dest_data++) = (OPJ_INT32) l_temp; + } +} + +static void opj_j2k_read_float32_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_FLOAT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_float(l_src_data, &l_temp); + + l_src_data += sizeof(OPJ_FLOAT32); + + *(l_dest_data++) = (OPJ_INT32) l_temp; + } +} + +static void opj_j2k_read_float64_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_FLOAT64 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + opj_read_double(l_src_data, &l_temp); + + l_src_data += sizeof(OPJ_FLOAT64); + + *(l_dest_data++) = (OPJ_INT32) l_temp; + } +} + +static void opj_j2k_write_float_to_int16(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; + OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + l_temp = (OPJ_UINT32) * (l_src_data++); + + opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT16)); + + l_dest_data += sizeof(OPJ_INT16); + } +} + +static void opj_j2k_write_float_to_int32(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; + OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + l_temp = (OPJ_UINT32) * (l_src_data++); + + opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT32)); + + l_dest_data += sizeof(OPJ_INT32); + } +} + +static void opj_j2k_write_float_to_float(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; + OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; + OPJ_UINT32 i; + OPJ_FLOAT32 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + l_temp = (OPJ_FLOAT32) * (l_src_data++); + + opj_write_float(l_dest_data, l_temp); + + l_dest_data += sizeof(OPJ_FLOAT32); + } +} + +static void opj_j2k_write_float_to_float64(const void * p_src_data, + void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data; + OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data; + OPJ_UINT32 i; + OPJ_FLOAT64 l_temp; + + for (i = 0; i < p_nb_elem; ++i) { + l_temp = (OPJ_FLOAT64) * (l_src_data++); + + opj_write_double(l_dest_data, l_temp); + + l_dest_data += sizeof(OPJ_FLOAT64); + } +} + +const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order) +{ + const j2k_prog_order_t *po; + for (po = j2k_prog_order_list; po->enum_prog != -1; po++) { + if (po->enum_prog == prg_order) { + return po->str_prog; + } + } + return po->str_prog; +} + +static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs, + OPJ_UINT32 tileno, + OPJ_UINT32 p_nb_pocs, + OPJ_UINT32 p_nb_resolutions, + OPJ_UINT32 p_num_comps, + OPJ_UINT32 p_num_layers, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32* packet_array; + OPJ_UINT32 index, resno, compno, layno; + OPJ_UINT32 i; + OPJ_UINT32 step_c = 1; + OPJ_UINT32 step_r = p_num_comps * step_c; + OPJ_UINT32 step_l = p_nb_resolutions * step_r; + OPJ_BOOL loss = OPJ_FALSE; + + assert(p_nb_pocs > 0); + + packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, + sizeof(OPJ_UINT32)); + if (packet_array == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory for checking the poc values.\n"); + return OPJ_FALSE; + } + + /* iterate through all the pocs that match our tile of interest. */ + for (i = 0; i < p_nb_pocs; ++i) { + const opj_poc_t *poc = &p_pocs[i]; + if (tileno + 1 == poc->tile) { + index = step_r * poc->resno0; + + /* take each resolution for each poc */ + for (resno = poc->resno0 ; + resno < opj_uint_min(poc->resno1, p_nb_resolutions); ++resno) { + OPJ_UINT32 res_index = index + poc->compno0 * step_c; + + /* take each comp of each resolution for each poc */ + for (compno = poc->compno0 ; + compno < opj_uint_min(poc->compno1, p_num_comps); ++compno) { + /* The layer index always starts at zero for every progression. */ + const OPJ_UINT32 layno0 = 0; + OPJ_UINT32 comp_index = res_index + layno0 * step_l; + + /* and finally take each layer of each res of ... */ + for (layno = layno0; layno < opj_uint_min(poc->layno1, p_num_layers); + ++layno) { + packet_array[comp_index] = 1; + comp_index += step_l; + } + + res_index += step_c; + } + + index += step_r; + } + } + } + + index = 0; + for (layno = 0; layno < p_num_layers ; ++layno) { + for (resno = 0; resno < p_nb_resolutions; ++resno) { + for (compno = 0; compno < p_num_comps; ++compno) { + loss |= (packet_array[index] != 1); +#ifdef DEBUG_VERBOSE + if (packet_array[index] != 1) { + fprintf(stderr, + "Missing packet in POC: layno=%d resno=%d compno=%d\n", + layno, resno, compno); + } +#endif + index += step_c; + } + } + } + + if (loss) { + opj_event_msg(p_manager, EVT_ERROR, "Missing packets possible loss of data\n"); + } + + opj_free(packet_array); + + return !loss; +} + +/* ----------------------------------------------------------------------- */ + +static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, + OPJ_UINT32 tileno) +{ + const OPJ_CHAR *prog = 00; + OPJ_INT32 i; + OPJ_UINT32 tpnum = 1; + opj_tcp_t *tcp = 00; + opj_poc_t * l_current_poc = 00; + + /* preconditions */ + assert(tileno < (cp->tw * cp->th)); + assert(pino < (cp->tcps[tileno].numpocs + 1)); + + /* get the given tile coding parameter */ + tcp = &cp->tcps[tileno]; + assert(tcp != 00); + + l_current_poc = &(tcp->pocs[pino]); + assert(l_current_poc != 0); + + /* get the progression order as a character string */ + prog = opj_j2k_convert_progression_order(tcp->prg); + assert(strlen(prog) > 0); + + if (cp->m_specific_param.m_enc.m_tp_on == 1) { + for (i = 0; i < 4; ++i) { + switch (prog[i]) { + /* component wise */ + case 'C': + tpnum *= l_current_poc->compE; + break; + /* resolution wise */ + case 'R': + tpnum *= l_current_poc->resE; + break; + /* precinct wise */ + case 'P': + tpnum *= l_current_poc->prcE; + break; + /* layer wise */ + case 'L': + tpnum *= l_current_poc->layE; + break; + } + /* whould we split here ? */ + if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) { + cp->m_specific_param.m_enc.m_tp_pos = i; + break; + } + } + } else { + tpnum = 1; + } + + return tpnum; +} + +static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k, + opj_cp_t *cp, + OPJ_UINT32 * p_nb_tiles, + opj_image_t *image, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 pino, tileno; + OPJ_UINT32 l_nb_tiles; + opj_tcp_t *tcp; + + /* preconditions */ + assert(p_nb_tiles != 00); + assert(cp != 00); + assert(image != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_j2k); + OPJ_UNUSED(p_manager); + + l_nb_tiles = cp->tw * cp->th; + * p_nb_tiles = 0; + tcp = cp->tcps; + + /* INDEX >> */ + /* TODO mergeV2: check this part which use cstr_info */ + /*if (p_j2k->cstr_info) { + opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; + + for (tileno = 0; tileno < l_nb_tiles; ++tileno) { + OPJ_UINT32 cur_totnum_tp = 0; + + opj_pi_update_encoding_parameters(image,cp,tileno); + + for (pino = 0; pino <= tcp->numpocs; ++pino) + { + OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); + + *p_nb_tiles = *p_nb_tiles + tp_num; + + cur_totnum_tp += tp_num; + } + + tcp->m_nb_tile_parts = cur_totnum_tp; + + l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); + if (l_info_tile_ptr->tp == 00) { + return OPJ_FALSE; + } + + memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); + + l_info_tile_ptr->num_tps = cur_totnum_tp; + + ++l_info_tile_ptr; + ++tcp; + } + } + else */{ + for (tileno = 0; tileno < l_nb_tiles; ++tileno) { + OPJ_UINT32 cur_totnum_tp = 0; + + opj_pi_update_encoding_parameters(image, cp, tileno); + + for (pino = 0; pino <= tcp->numpocs; ++pino) { + OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp, pino, tileno); + + *p_nb_tiles = *p_nb_tiles + tp_num; + + cur_totnum_tp += tp_num; + } + tcp->m_nb_tile_parts = cur_totnum_tp; + + ++tcp; + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + /* 2 bytes will be written */ + OPJ_BYTE * l_start_stream = 00; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* write SOC identifier */ + opj_write_bytes(l_start_stream, J2K_MS_SOC, 2); + + if (opj_stream_write_data(p_stream, l_start_stream, 2, p_manager) != 2) { + return OPJ_FALSE; + } + + /* UniPG>> */ +#ifdef USE_JPWL + /* update markers struct */ + /* + OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); + */ + assert(0 && "TODO"); +#endif /* USE_JPWL */ + /* <m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; + + /* FIXME move it in a index structure included in p_j2k*/ + p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; + + opj_event_msg(p_manager, EVT_INFO, + "Start to read j2k main header (%" PRId64 ").\n", + p_j2k->cstr_index->main_head_start); + + /* Add the marker to the codestream index*/ + if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, + p_j2k->cstr_index->main_head_start, 2)) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + return OPJ_FALSE; + } + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_size_len; + OPJ_BYTE * l_current_ptr; + opj_image_t * l_image = 00; + opj_cp_t *cp = 00; + opj_image_comp_t * l_img_comp = 00; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + cp = &(p_j2k->m_cp); + l_size_len = 40 + 3 * l_image->numcomps; + l_img_comp = l_image->comps; + + if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; + } + + l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* write SOC identifier */ + opj_write_bytes(l_current_ptr, J2K_MS_SIZ, 2); /* SIZ */ + l_current_ptr += 2; + + opj_write_bytes(l_current_ptr, l_size_len - 2, 2); /* L_SIZ */ + l_current_ptr += 2; + + opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ + l_current_ptr += 2; + + opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ + l_current_ptr += 4; + + opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ + l_current_ptr += 2; + + for (i = 0; i < l_image->numcomps; ++i) { + /* TODO here with MCT ? */ + opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), + 1); /* Ssiz_i */ + ++l_current_ptr; + + opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ + ++l_current_ptr; + + opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ + ++l_current_ptr; + + ++l_img_comp; + } + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len, + p_manager) != l_size_len) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a SIZ marker (image and tile size) + * @param p_j2k the jpeg2000 file codec. + * @param p_header_data the data contained in the SIZ box. + * @param p_header_size the size of the data contained in the SIZ marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_comp_remain; + OPJ_UINT32 l_remaining_size; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_tmp, l_tx1, l_ty1; + OPJ_UINT32 l_prec0, l_sgnd0; + opj_image_t *l_image = 00; + opj_cp_t *l_cp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcp_t * l_current_tile_param = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + l_image = p_j2k->m_private_image; + l_cp = &(p_j2k->m_cp); + + /* minimum size == 39 - 3 (= minimum component parameter) */ + if (p_header_size < 36) { + opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return OPJ_FALSE; + } + + l_remaining_size = p_header_size - 36; + l_nb_comp = l_remaining_size / 3; + l_nb_comp_remain = l_remaining_size % 3; + if (l_nb_comp_remain != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_tmp, + 2); /* Rsiz (capabilities) */ + p_header_data += 2; + l_cp->rsiz = (OPJ_UINT16) l_tmp; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, + 4); /* XTsiz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, + 4); /* YTsiz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, + 4); /* XT0siz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, + 4); /* YT0siz */ + p_header_data += 4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, + 2); /* Csiz */ + p_header_data += 2; + if (l_tmp < 16385) { + l_image->numcomps = (OPJ_UINT16) l_tmp; + } else { + opj_event_msg(p_manager, EVT_ERROR, + "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); + return OPJ_FALSE; + } + + if (l_image->numcomps != l_nb_comp) { + opj_event_msg(p_manager, EVT_ERROR, + "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", + l_image->numcomps, l_nb_comp); + return OPJ_FALSE; + } + + /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ + /* testcase issue427-null-image-size.jp2 */ + if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64 + ")\n", (OPJ_INT64)l_image->x1 - l_image->x0, + (OPJ_INT64)l_image->y1 - l_image->y0); + return OPJ_FALSE; + } + /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ + if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, + l_cp->tdy); + return OPJ_FALSE; + } + + /* testcase issue427-illegal-tile-offset.jp2 */ + l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ + l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ + if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || + (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error with SIZ marker: illegal tile offset\n"); + return OPJ_FALSE; + } + if (!p_j2k->dump_state) { + OPJ_UINT32 siz_w, siz_h; + + siz_w = l_image->x1 - l_image->x0; + siz_h = l_image->y1 - l_image->y0; + + if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0 + && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w, + p_j2k->ihdr_h, siz_w, siz_h); + return OPJ_FALSE; + } + } +#ifdef USE_JPWL + if (l_cp->correct) { + /* if JPWL is on, we check whether TX errors have damaged + too much the SIZ parameters */ + if (!(l_image->x1 * l_image->y1)) { + opj_event_msg(p_manager, EVT_ERROR, + "JPWL: bad image size (%d x %d)\n", + l_image->x1, l_image->y1); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + + /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ? + if (l_image->numcomps != ((len - 38) / 3)) { + opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", + l_image->numcomps, ((len - 38) / 3)); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + */ /* we try to correct */ + /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"); + if (l_image->numcomps < ((len - 38) / 3)) { + len = 38 + 3 * l_image->numcomps; + opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", + len); + } else { + l_image->numcomps = ((len - 38) / 3); + opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", + l_image->numcomps); + } + } + */ + + /* update components number in the jpwl_exp_comps filed */ + l_cp->exp_comps = l_image->numcomps; + } +#endif /* USE_JPWL */ + + /* Allocate the resulting image components */ + l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, + sizeof(opj_image_comp_t)); + if (l_image->comps == 00) { + l_image->numcomps = 0; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + + l_img_comp = l_image->comps; + + l_prec0 = 0; + l_sgnd0 = 0; + /* Read the component information */ + for (i = 0; i < l_image->numcomps; ++i) { + OPJ_UINT32 tmp; + opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */ + ++p_header_data; + l_img_comp->prec = (tmp & 0x7f) + 1; + l_img_comp->sgnd = tmp >> 7; + + if (p_j2k->dump_state == 0) { + if (i == 0) { + l_prec0 = l_img_comp->prec; + l_sgnd0 = l_img_comp->sgnd; + } else if (!l_cp->allow_different_bit_depth_sign + && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) { + opj_event_msg(p_manager, EVT_WARNING, + "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n" + " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0, + i, l_img_comp->prec, l_img_comp->sgnd); + } + /* TODO: we should perhaps also check against JP2 BPCC values */ + } + opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */ + ++p_header_data; + l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ + opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */ + ++p_header_data; + l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ + if (l_img_comp->dx < 1 || l_img_comp->dx > 255 || + l_img_comp->dy < 1 || l_img_comp->dy > 255) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n", + i, l_img_comp->dx, l_img_comp->dy); + return OPJ_FALSE; + } + /* Avoids later undefined shift in computation of */ + /* p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 + << (l_image->comps[i].prec - 1); */ + if (l_img_comp->prec > 31) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n", + i, l_img_comp->prec); + return OPJ_FALSE; + } +#ifdef USE_JPWL + if (l_cp->correct) { + /* if JPWL is on, we check whether TX errors have damaged + too much the SIZ parameters, again */ + if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { + opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", + i, i, l_image->comps[i].dx, l_image->comps[i].dy); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); + if (!l_image->comps[i].dx) { + l_image->comps[i].dx = 1; + opj_event_msg(p_manager, EVT_WARNING, + "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", + i, l_image->comps[i].dx); + } + if (!l_image->comps[i].dy) { + l_image->comps[i].dy = 1; + opj_event_msg(p_manager, EVT_WARNING, + "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", + i, l_image->comps[i].dy); + } + } + } +#endif /* USE_JPWL */ + l_img_comp->resno_decoded = + 0; /* number of resolution decoded */ + l_img_comp->factor = + l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ + ++l_img_comp; + } + + if (l_cp->tdx == 0 || l_cp->tdy == 0) { + return OPJ_FALSE; + } + + /* Compute the number of tiles */ + l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), + (OPJ_INT32)l_cp->tdx); + l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), + (OPJ_INT32)l_cp->tdy); + + /* Check that the number of tiles is valid */ + if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", + l_cp->tw, l_cp->th); + return OPJ_FALSE; + } + l_nb_tiles = l_cp->tw * l_cp->th; + + /* Define the tiles which will be decoded */ + if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = + (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = + (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(( + OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), + (OPJ_INT32)l_cp->tdx); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(( + OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), + (OPJ_INT32)l_cp->tdy); + } else { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + } + +#ifdef USE_JPWL + if (l_cp->correct) { + /* if JPWL is on, we check whether TX errors have damaged + too much the SIZ parameters */ + if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || + (l_cp->th > l_cp->max_tiles)) { + opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: bad number of tiles (%d x %d)\n", + l_cp->tw, l_cp->th); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); + if (l_cp->tw < 1) { + l_cp->tw = 1; + opj_event_msg(p_manager, EVT_WARNING, + "- setting %d tiles in x => HYPOTHESIS!!!\n", + l_cp->tw); + } + if (l_cp->tw > l_cp->max_tiles) { + l_cp->tw = 1; + opj_event_msg(p_manager, EVT_WARNING, + "- too large x, increase expectance of %d\n" + "- setting %d tiles in x => HYPOTHESIS!!!\n", + l_cp->max_tiles, l_cp->tw); + } + if (l_cp->th < 1) { + l_cp->th = 1; + opj_event_msg(p_manager, EVT_WARNING, + "- setting %d tiles in y => HYPOTHESIS!!!\n", + l_cp->th); + } + if (l_cp->th > l_cp->max_tiles) { + l_cp->th = 1; + opj_event_msg(p_manager, EVT_WARNING, + "- too large y, increase expectance of %d to continue\n", + "- setting %d tiles in y => HYPOTHESIS!!!\n", + l_cp->max_tiles, l_cp->th); + } + } + } +#endif /* USE_JPWL */ + + /* memory allocations */ + l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); + if (l_cp->tcps == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + +#ifdef USE_JPWL + if (l_cp->correct) { + if (!l_cp->tcps) { + opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: could not alloc tcps field of cp\n"); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + } +#endif /* USE_JPWL */ + + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = + (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); + if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = + (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS, + sizeof(opj_mct_data_t)); + + if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = + OPJ_J2K_MCT_DEFAULT_NB_RECORDS; + + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = + (opj_simple_mcc_decorrelation_data_t*) + opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, + sizeof(opj_simple_mcc_decorrelation_data_t)); + + if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = + OPJ_J2K_MCC_DEFAULT_NB_RECORDS; + + /* set up default dc level shift */ + for (i = 0; i < l_image->numcomps; ++i) { + if (! l_image->comps[i].sgnd) { + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 + << (l_image->comps[i].prec - 1); + } + } + + l_current_tile_param = l_cp->tcps; + for (i = 0; i < l_nb_tiles; ++i) { + l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, + sizeof(opj_tccp_t)); + if (l_current_tile_param->tccps == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + + ++l_current_tile_param; + } + + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; + opj_image_comp_header_update(l_image, l_cp); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_comment_size; + OPJ_UINT32 l_total_com_size; + const OPJ_CHAR *l_comment; + OPJ_BYTE * l_current_ptr = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_comment = p_j2k->m_cp.comment; + l_comment_size = (OPJ_UINT32)strlen(l_comment); + l_total_com_size = l_comment_size + 6; + + if (l_total_com_size > + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to write the COM marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; + } + + l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_ptr, J2K_MS_COM, 2); /* COM */ + l_current_ptr += 2; + + opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2); /* L_COM */ + l_current_ptr += 2; + + opj_write_bytes(l_current_ptr, 1, + 2); /* General use (IS 8859-15:1999 (Latin) values) */ + l_current_ptr += 2; + + memcpy(l_current_ptr, l_comment, l_comment_size); + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size, + p_manager) != l_total_com_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a COM marker (comments) + * @param p_j2k the jpeg2000 file codec. + * @param p_header_data the data contained in the COM box. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + OPJ_UNUSED(p_j2k); + OPJ_UNUSED(p_header_data); + OPJ_UNUSED(p_header_size); + OPJ_UNUSED(p_manager); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_code_size, l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k, + p_j2k->m_current_tile_number, 0); + l_remaining_size = l_code_size; + + if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data, J2K_MS_COD, 2); /* COD */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_code_size - 2, 2); /* L_COD */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_tcp->csty, 1); /* Scod */ + ++l_current_data; + + opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */ + ++l_current_data; + + opj_write_bytes(l_current_data, l_tcp->numlayers, 2); /* SGcod (B) */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_tcp->mct, 1); /* SGcod (C) */ + ++l_current_data; + + l_remaining_size -= 9; + + if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, + l_current_data, &l_remaining_size, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); + return OPJ_FALSE; + } + + if (l_remaining_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); + return OPJ_FALSE; + } + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size, + p_manager) != l_code_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a COD marker (Coding style defaults) + * @param p_header_data the data contained in the COD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COD marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + /* loop */ + OPJ_UINT32 i; + OPJ_UINT32 l_tmp; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_image_t *l_image = 00; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_cp = &(p_j2k->m_cp); + + /* If we are in the first tile-part header of the current tile */ + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + +#if 0 + /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */ + /* but this is no longer necessary to handle issue476.jp2 */ + /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */ + /* Only one COD per tile */ + if (l_tcp->cod) { + opj_event_msg(p_manager, EVT_ERROR, + "COD marker already read. No more than one COD marker per tile.\n"); + return OPJ_FALSE; + } +#endif + l_tcp->cod = 1; + + /* Make sure room is sufficient */ + if (p_header_size < 5) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */ + ++p_header_data; + /* Make sure we know how to decode this */ + if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | + J2K_CP_CSTY_EPH)) != 0U) { + opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n"); + return OPJ_FALSE; + } + opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */ + ++p_header_data; + l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; + /* Make sure progression order is valid */ + if (l_tcp->prg > OPJ_CPRL) { + opj_event_msg(p_manager, EVT_ERROR, + "Unknown progression order in COD marker\n"); + l_tcp->prg = OPJ_PROG_UNKNOWN; + } + opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */ + p_header_data += 2; + + if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid number of layers in COD marker : %d not in range [1-65535]\n", + l_tcp->numlayers); + return OPJ_FALSE; + } + + /* If user didn't set a number layer to decode take the max specify in the codestream. */ + if (l_cp->m_specific_param.m_dec.m_layer) { + l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; + } else { + l_tcp->num_layers_to_decode = l_tcp->numlayers; + } + + opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */ + ++p_header_data; + + if (l_tcp->mct > 1) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid multiple component transformation\n"); + return OPJ_FALSE; + } + + p_header_size -= 5; + for (i = 0; i < l_image->numcomps; ++i) { + l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; + } + + if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return OPJ_FALSE; + } + + if (p_header_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return OPJ_FALSE; + } + + /* Apply the coding style to other components of the current tile or the m_default_tcp*/ + opj_j2k_copy_tile_component_parameters(p_j2k); + + /* Index */ +#ifdef WIP_REMOVE_MSD + if (p_j2k->cstr_info) { + /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ + p_j2k->cstr_info->prog = l_tcp->prg; + p_j2k->cstr_info->numlayers = l_tcp->numlayers; + p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc( + l_image->numcomps * sizeof(OPJ_UINT32)); + if (!p_j2k->cstr_info->numdecompos) { + return OPJ_FALSE; + } + for (i = 0; i < l_image->numcomps; ++i) { + p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; + } + } +#endif + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 l_coc_size, l_remaining_size; + OPJ_UINT32 l_comp_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; + + l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, + p_j2k->m_current_tile_number, p_comp_no); + + if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data; + /*p_j2k->m_specific_param.m_encoder.m_header_tile_data + = (OPJ_BYTE*)opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_coc_size);*/ + + new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; + } + + opj_j2k_write_coc_in_memory(p_j2k, p_comp_no, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, + p_manager); + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size, + p_manager) != l_coc_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) +{ + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + + if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) { + return OPJ_FALSE; + } + + + return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, + p_first_comp_no, p_second_comp_no); +} + +static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_coc_size, l_remaining_size; + OPJ_BYTE * l_current_data = 00; + opj_image_t *l_image = 00; + OPJ_UINT32 l_comp_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_image = p_j2k->m_private_image; + l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; + + l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k, + p_j2k->m_current_tile_number, p_comp_no); + l_remaining_size = l_coc_size; + + l_current_data = p_data; + + opj_write_bytes(l_current_data, J2K_MS_COC, + 2); /* COC */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_coc_size - 2, + 2); /* L_COC */ + l_current_data += 2; + + opj_write_bytes(l_current_data, p_comp_no, l_comp_room); /* Ccoc */ + l_current_data += l_comp_room; + + opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, + 1); /* Scoc */ + ++l_current_data; + + l_remaining_size -= (5 + l_comp_room); + opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0, + l_current_data, &l_remaining_size, p_manager); + * p_data_written = l_coc_size; +} + +static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 i, j; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max = 0; + + /* preconditions */ + + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; + l_nb_comp = p_j2k->m_private_image->numcomps; + + for (i = 0; i < l_nb_tiles; ++i) { + for (j = 0; j < l_nb_comp; ++j) { + l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j)); + } + } + + return 6 + l_max; +} + +/** + * Reads a COC marker (Coding Style Component) + * @param p_header_data the data contained in the COC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + opj_image_t *l_image = NULL; + OPJ_UINT32 l_comp_room; + OPJ_UINT32 l_comp_no; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) + ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_image = p_j2k->m_private_image; + + l_comp_room = l_image->numcomps <= 256 ? 1 : 2; + + /* make sure room is sufficient*/ + if (p_header_size < l_comp_room + 1) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return OPJ_FALSE; + } + p_header_size -= l_comp_room + 1; + + opj_read_bytes(p_header_data, &l_comp_no, + l_comp_room); /* Ccoc */ + p_header_data += l_comp_room; + if (l_comp_no >= l_image->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, + "Error reading COC marker (bad number of components)\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty, + 1); /* Scoc */ + ++p_header_data ; + + if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return OPJ_FALSE; + } + + if (p_header_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return OPJ_FALSE; + } + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_qcd_size, l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, + 0); + l_remaining_size = l_qcd_size; + + if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data, J2K_MS_QCD, 2); /* QCD */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_qcd_size - 2, 2); /* L_QCD */ + l_current_data += 2; + + l_remaining_size -= 4; + + if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0, + l_current_data, &l_remaining_size, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); + return OPJ_FALSE; + } + + if (l_remaining_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); + return OPJ_FALSE; + } + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size, + p_manager) != l_qcd_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a QCD marker (Quantization defaults) + * @param p_header_data the data contained in the QCD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCD marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + return OPJ_FALSE; + } + + if (p_header_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + return OPJ_FALSE; + } + + /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ + opj_j2k_copy_tile_quantization_parameters(p_j2k); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_qcc_size, l_remaining_size; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, + p_comp_no); + l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1; + l_remaining_size = l_qcc_size; + + if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; + } + + opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size, + p_manager); + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size, + p_manager) != l_qcc_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) +{ + return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, + p_first_comp_no, p_second_comp_no); +} + +static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_qcc_size, l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number, + p_comp_no); + l_remaining_size = l_qcc_size; + + l_current_data = p_data; + + opj_write_bytes(l_current_data, J2K_MS_QCC, 2); /* QCC */ + l_current_data += 2; + + if (p_j2k->m_private_image->numcomps <= 256) { + --l_qcc_size; + + opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ + l_current_data += 2; + + opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ + ++l_current_data; + + /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ + l_remaining_size -= 6; + } else { + opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */ + l_current_data += 2; + + opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ + l_current_data += 2; + + l_remaining_size -= 6; + } + + opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no, + l_current_data, &l_remaining_size, p_manager); + + *p_data_written = l_qcc_size; +} + +static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k) +{ + return opj_j2k_get_max_coc_size(p_j2k); +} + +/** + * Reads a QCC marker (Quantization component) + * @param p_header_data the data contained in the QCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_num_comp, l_comp_no; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_num_comp = p_j2k->m_private_image->numcomps; + + if (l_num_comp <= 256) { + if (p_header_size < 1) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } + opj_read_bytes(p_header_data, &l_comp_no, 1); + ++p_header_data; + --p_header_size; + } else { + if (p_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } + opj_read_bytes(p_header_data, &l_comp_no, 2); + p_header_data += 2; + p_header_size -= 2; + } + +#ifdef USE_JPWL + if (p_j2k->m_cp.correct) { + + static OPJ_UINT32 backup_compno = 0; + + /* compno is negative or larger than the number of components!!! */ + if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { + opj_event_msg(p_manager, EVT_ERROR, + "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", + l_comp_no, l_num_comp); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_comp_no = backup_compno % l_num_comp; + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" + "- setting component number to %d\n", + l_comp_no); + } + + /* keep your private count of tiles */ + backup_compno++; + }; +#endif /* USE_JPWL */ + + if (l_comp_no >= p_j2k->m_private_image->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid component number: %d, regarding the number of components %d\n", + l_comp_no, p_j2k->m_private_image->numcomps); + return OPJ_FALSE; + } + + if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } + + if (p_header_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + OPJ_UINT32 l_written_size = 0; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_poc_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + l_nb_comp = p_j2k->m_private_image->numcomps; + l_nb_poc = 1 + l_tcp->numpocs; + + if (l_nb_comp <= 256) { + l_poc_room = 1; + } else { + l_poc_room = 2; + } + l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; + + if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; + } + + opj_j2k_write_poc_in_memory(p_j2k, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size, + p_manager); + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size, + p_manager) != l_poc_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + opj_image_t *l_image = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + opj_poc_t *l_current_poc = 00; + OPJ_UINT32 l_poc_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_manager); + + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + l_tccp = &l_tcp->tccps[0]; + l_image = p_j2k->m_private_image; + l_nb_comp = l_image->numcomps; + l_nb_poc = 1 + l_tcp->numpocs; + + if (l_nb_comp <= 256) { + l_poc_room = 1; + } else { + l_poc_room = 2; + } + + l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; + + l_current_data = p_data; + + opj_write_bytes(l_current_data, J2K_MS_POC, + 2); /* POC */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_poc_size - 2, + 2); /* Lpoc */ + l_current_data += 2; + + l_current_poc = l_tcp->pocs; + for (i = 0; i < l_nb_poc; ++i) { + opj_write_bytes(l_current_data, l_current_poc->resno0, + 1); /* RSpoc_i */ + ++l_current_data; + + opj_write_bytes(l_current_data, l_current_poc->compno0, + l_poc_room); /* CSpoc_i */ + l_current_data += l_poc_room; + + opj_write_bytes(l_current_data, l_current_poc->layno1, + 2); /* LYEpoc_i */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_current_poc->resno1, + 1); /* REpoc_i */ + ++l_current_data; + + opj_write_bytes(l_current_data, l_current_poc->compno1, + l_poc_room); /* CEpoc_i */ + l_current_data += l_poc_room; + + opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg, + 1); /* Ppoc_i */ + ++l_current_data; + + /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ + l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) + l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers); + l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) + l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions); + l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32) + l_current_poc->compno1, (OPJ_INT32)l_nb_comp); + + ++l_current_poc; + } + + *p_data_written = l_poc_size; +} + +static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k) +{ + opj_tcp_t * l_tcp = 00; + OPJ_UINT32 l_nb_tiles = 0; + OPJ_UINT32 l_max_poc = 0; + OPJ_UINT32 i; + + l_tcp = p_j2k->m_cp.tcps; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + + for (i = 0; i < l_nb_tiles; ++i) { + l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs); + ++l_tcp; + } + + ++l_max_poc; + + return 4 + 9 * l_max_poc; +} + +static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max = 0; + opj_tcp_t * l_tcp = 00; + + l_tcp = p_j2k->m_cp.tcps; + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; + + for (i = 0; i < l_nb_tiles; ++i) { + l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts); + + ++l_tcp; + } + + return 12 * l_max; +} + +static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 l_nb_bytes = 0; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_coc_bytes, l_qcc_bytes; + + l_nb_comps = p_j2k->m_private_image->numcomps - 1; + l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); + + if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) { + l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); + l_nb_bytes += l_nb_comps * l_coc_bytes; + + l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); + l_nb_bytes += l_nb_comps * l_qcc_bytes; + } + + l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); + + if (p_j2k->m_specific_param.m_encoder.m_PLT) { + /* Reserve space for PLT markers */ + + OPJ_UINT32 i; + const opj_cp_t * l_cp = &(p_j2k->m_cp); + OPJ_UINT32 l_max_packet_count = 0; + for (i = 0; i < l_cp->th * l_cp->tw; ++i) { + l_max_packet_count = opj_uint_max(l_max_packet_count, + opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i)); + } + /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */ + /* estimate of 4 bytes for a packet size), one can write */ + /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */ + p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT = + 6 * opj_uint_ceildiv(l_max_packet_count, 16382); + /* Maximum 5 bytes per packet to encode a full UINT32 */ + p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += + l_nb_bytes += 5 * l_max_packet_count; + p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1; + l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT; + } + + /*** DEVELOPER CORNER, Add room for your headers ***/ + + return l_nb_bytes; +} + +/** + * Reads a POC marker (Progression Order Change) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i, l_nb_comp, l_tmp; + opj_image_t * l_image = 00; + OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; + OPJ_UINT32 l_chunk_size, l_comp_room; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_poc_t *l_current_poc = 00; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_nb_comp = l_image->numcomps; + if (l_nb_comp <= 256) { + l_comp_room = 1; + } else { + l_comp_room = 2; + } + l_chunk_size = 5 + 2 * l_comp_room; + l_current_poc_nb = p_header_size / l_chunk_size; + l_current_poc_remaining = p_header_size % l_chunk_size; + + if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; + l_current_poc_nb += l_old_poc_nb; + + if (l_current_poc_nb >= J2K_MAX_POCS) { + opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); + return OPJ_FALSE; + } + + /* now poc is in use.*/ + l_tcp->POC = 1; + + l_current_poc = &l_tcp->pocs[l_old_poc_nb]; + for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { + opj_read_bytes(p_header_data, &(l_current_poc->resno0), + 1); /* RSpoc_i */ + ++p_header_data; + opj_read_bytes(p_header_data, &(l_current_poc->compno0), + l_comp_room); /* CSpoc_i */ + p_header_data += l_comp_room; + opj_read_bytes(p_header_data, &(l_current_poc->layno1), + 2); /* LYEpoc_i */ + /* make sure layer end is in acceptable bounds */ + l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers); + p_header_data += 2; + opj_read_bytes(p_header_data, &(l_current_poc->resno1), + 1); /* REpoc_i */ + ++p_header_data; + opj_read_bytes(p_header_data, &(l_current_poc->compno1), + l_comp_room); /* CEpoc_i */ + p_header_data += l_comp_room; + opj_read_bytes(p_header_data, &l_tmp, + 1); /* Ppoc_i */ + ++p_header_data; + l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; + /* make sure comp is in acceptable bounds */ + l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp); + ++l_current_poc; + } + + l_tcp->numpocs = l_current_poc_nb - 1; + return OPJ_TRUE; +} + +/** + * Reads a CRG marker (Component registration) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_nb_comp; + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_header_data); + + l_nb_comp = p_j2k->m_private_image->numcomps; + + if (p_header_size != l_nb_comp * 4) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n"); + return OPJ_FALSE; + } + /* Do not care of this at the moment since only local variables are set here */ + /* + for + (i = 0; i < l_nb_comp; ++i) + { + opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i + p_header_data+=2; + opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i + p_header_data+=2; + } + */ + return OPJ_TRUE; +} + +/** + * Reads a TLM marker (Tile Length Marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, + l_Ptlm_size; + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_j2k); + + if (p_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + return OPJ_FALSE; + } + p_header_size -= 2; + + opj_read_bytes(p_header_data, &l_Ztlm, + 1); /* Ztlm */ + ++p_header_data; + opj_read_bytes(p_header_data, &l_Stlm, + 1); /* Stlm */ + ++p_header_data; + + l_ST = ((l_Stlm >> 4) & 0x3); + l_SP = (l_Stlm >> 6) & 0x1; + + l_Ptlm_size = (l_SP + 1) * 2; + l_quotient = l_Ptlm_size + l_ST; + + l_tot_num_tp_remaining = p_header_size % l_quotient; + + if (l_tot_num_tp_remaining != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + return OPJ_FALSE; + } + /* FIXME Do not care of this at the moment since only local variables are set here */ + /* + for + (i = 0; i < l_tot_num_tp; ++i) + { + opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i + p_header_data += l_ST; + opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i + p_header_data += l_Ptlm_size; + }*/ + return OPJ_TRUE; +} + +/** + * Reads a PLM marker (Packet length, main header marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_j2k); + OPJ_UNUSED(p_header_data); + + if (p_header_size < 1) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return OPJ_FALSE; + } + /* Do not care of this at the moment since only local variables are set here */ + /* + opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm + ++p_header_data; + --p_header_size; + + while + (p_header_size > 0) + { + opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm + ++p_header_data; + p_header_size -= (1+l_Nplm); + if + (p_header_size < 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + for + (i = 0; i < l_Nplm; ++i) + { + opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij + ++p_header_data; + // take only the last seven bytes + l_packet_len |= (l_tmp & 0x7f); + if + (l_tmp & 0x80) + { + l_packet_len <<= 7; + } + else + { + // store packet length and proceed to next packet + l_packet_len = 0; + } + } + if + (l_packet_len != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + } + */ + return OPJ_TRUE; +} + +/** + * Reads a PLT marker (Packet length, tile-part header) + * + * @param p_header_data the data contained in the PLT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PLT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_j2k); + + if (p_header_size < 1) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */ + ++p_header_data; + --p_header_size; + + for (i = 0; i < p_header_size; ++i) { + opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */ + ++p_header_data; + /* take only the last seven bytes */ + l_packet_len |= (l_tmp & 0x7f); + if (l_tmp & 0x80) { + l_packet_len <<= 7; + } else { + /* store packet length and proceed to next packet */ + l_packet_len = 0; + } + } + + if (l_packet_len != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a PPM marker (Packed packet headers, main header) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. + */ + +static OPJ_BOOL opj_j2k_read_ppm( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager) +{ + opj_cp_t *l_cp = 00; + OPJ_UINT32 l_Z_ppm; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */ + if (p_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + l_cp->ppm = 1; + + opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */ + ++p_header_data; + --p_header_size; + + /* check allocation needed */ + if (l_cp->ppm_markers == NULL) { /* first PPM marker */ + OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ + assert(l_cp->ppm_markers_count == 0U); + + l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); + if (l_cp->ppm_markers == NULL) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + return OPJ_FALSE; + } + l_cp->ppm_markers_count = l_newCount; + } else if (l_cp->ppm_markers_count <= l_Z_ppm) { + OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ + opj_ppx *new_ppm_markers; + new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, + l_newCount * sizeof(opj_ppx)); + if (new_ppm_markers == NULL) { + /* clean up to be done on l_cp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + return OPJ_FALSE; + } + l_cp->ppm_markers = new_ppm_markers; + memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0, + (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx)); + l_cp->ppm_markers_count = l_newCount; + } + + if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) { + /* clean up to be done on l_cp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm); + return OPJ_FALSE; + } + + l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); + if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) { + /* clean up to be done on l_cp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + return OPJ_FALSE; + } + l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size; + memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size); + + return OPJ_TRUE; +} + +/** + * Merges all PPM markers read (Packed headers, main header) + * + * @param p_cp main coding parameters. + * @param p_manager the user event manager. + */ +static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining; + + /* preconditions */ + assert(p_cp != 00); + assert(p_manager != 00); + assert(p_cp->ppm_buffer == NULL); + + if (p_cp->ppm == 0U) { + return OPJ_TRUE; + } + + l_ppm_data_size = 0U; + l_N_ppm_remaining = 0U; + for (i = 0U; i < p_cp->ppm_markers_count; ++i) { + if (p_cp->ppm_markers[i].m_data != + NULL) { /* standard doesn't seem to require contiguous Zppm */ + OPJ_UINT32 l_N_ppm; + OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; + const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; + + if (l_N_ppm_remaining >= l_data_size) { + l_N_ppm_remaining -= l_data_size; + l_data_size = 0U; + } else { + l_data += l_N_ppm_remaining; + l_data_size -= l_N_ppm_remaining; + l_N_ppm_remaining = 0U; + } + + if (l_data_size > 0U) { + do { + /* read Nppm */ + if (l_data_size < 4U) { + /* clean up to be done on l_cp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); + return OPJ_FALSE; + } + opj_read_bytes(l_data, &l_N_ppm, 4); + l_data += 4; + l_data_size -= 4; + l_ppm_data_size += + l_N_ppm; /* can't overflow, max 256 markers of max 65536 bytes, that is when PPM markers are not corrupted which is checked elsewhere */ + + if (l_data_size >= l_N_ppm) { + l_data_size -= l_N_ppm; + l_data += l_N_ppm; + } else { + l_N_ppm_remaining = l_N_ppm - l_data_size; + l_data_size = 0U; + } + } while (l_data_size > 0U); + } + } + } + + if (l_N_ppm_remaining != 0U) { + /* clean up to be done on l_cp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n"); + return OPJ_FALSE; + } + + p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size); + if (p_cp->ppm_buffer == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + return OPJ_FALSE; + } + p_cp->ppm_len = l_ppm_data_size; + l_ppm_data_size = 0U; + l_N_ppm_remaining = 0U; + for (i = 0U; i < p_cp->ppm_markers_count; ++i) { + if (p_cp->ppm_markers[i].m_data != + NULL) { /* standard doesn't seem to require contiguous Zppm */ + OPJ_UINT32 l_N_ppm; + OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size; + const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data; + + if (l_N_ppm_remaining >= l_data_size) { + memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); + l_ppm_data_size += l_data_size; + l_N_ppm_remaining -= l_data_size; + l_data_size = 0U; + } else { + memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining); + l_ppm_data_size += l_N_ppm_remaining; + l_data += l_N_ppm_remaining; + l_data_size -= l_N_ppm_remaining; + l_N_ppm_remaining = 0U; + } + + if (l_data_size > 0U) { + do { + /* read Nppm */ + if (l_data_size < 4U) { + /* clean up to be done on l_cp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); + return OPJ_FALSE; + } + opj_read_bytes(l_data, &l_N_ppm, 4); + l_data += 4; + l_data_size -= 4; + + if (l_data_size >= l_N_ppm) { + memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm); + l_ppm_data_size += l_N_ppm; + l_data_size -= l_N_ppm; + l_data += l_N_ppm; + } else { + memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size); + l_ppm_data_size += l_data_size; + l_N_ppm_remaining = l_N_ppm - l_data_size; + l_data_size = 0U; + } + } while (l_data_size > 0U); + } + opj_free(p_cp->ppm_markers[i].m_data); + p_cp->ppm_markers[i].m_data = NULL; + p_cp->ppm_markers[i].m_data_size = 0U; + } + } + + p_cp->ppm_data = p_cp->ppm_buffer; + p_cp->ppm_data_size = p_cp->ppm_len; + + p_cp->ppm_markers_count = 0U; + opj_free(p_cp->ppm_markers); + p_cp->ppm_markers = NULL; + + return OPJ_TRUE; +} + +/** + * Reads a PPT marker (Packed packet headers, tile-part header) + * + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_Z_ppt; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */ + if (p_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + if (l_cp->ppm) { + opj_event_msg(p_manager, EVT_ERROR, + "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); + return OPJ_FALSE; + } + + l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); + l_tcp->ppt = 1; + + opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */ + ++p_header_data; + --p_header_size; + + /* check allocation needed */ + if (l_tcp->ppt_markers == NULL) { /* first PPT marker */ + OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ + assert(l_tcp->ppt_markers_count == 0U); + + l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); + if (l_tcp->ppt_markers == NULL) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + l_tcp->ppt_markers_count = l_newCount; + } else if (l_tcp->ppt_markers_count <= l_Z_ppt) { + OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ + opj_ppx *new_ppt_markers; + new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, + l_newCount * sizeof(opj_ppx)); + if (new_ppt_markers == NULL) { + /* clean up to be done on l_tcp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + l_tcp->ppt_markers = new_ppt_markers; + memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0, + (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx)); + l_tcp->ppt_markers_count = l_newCount; + } + + if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) { + /* clean up to be done on l_tcp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt); + return OPJ_FALSE; + } + + l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size); + if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) { + /* clean up to be done on l_tcp destruction */ + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size; + memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size); + return OPJ_TRUE; +} + +/** + * Merges all PPT markers read (Packed packet headers, tile-part header) + * + * @param p_tcp the tile. + * @param p_manager the user event manager. + */ +static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, l_ppt_data_size; + /* preconditions */ + assert(p_tcp != 00); + assert(p_manager != 00); + + if (p_tcp->ppt_buffer != NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_j2k_merge_ppt() has already been called\n"); + return OPJ_FALSE; + } + + if (p_tcp->ppt == 0U) { + return OPJ_TRUE; + } + + l_ppt_data_size = 0U; + for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { + l_ppt_data_size += + p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ + } + + p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size); + if (p_tcp->ppt_buffer == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + p_tcp->ppt_len = l_ppt_data_size; + l_ppt_data_size = 0U; + for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { + if (p_tcp->ppt_markers[i].m_data != + NULL) { /* standard doesn't seem to require contiguous Zppt */ + memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, + p_tcp->ppt_markers[i].m_data_size); + l_ppt_data_size += + p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ + + opj_free(p_tcp->ppt_markers[i].m_data); + p_tcp->ppt_markers[i].m_data = NULL; + p_tcp->ppt_markers[i].m_data_size = 0U; + } + } + + p_tcp->ppt_markers_count = 0U; + opj_free(p_tcp->ppt_markers); + p_tcp->ppt_markers = NULL; + + p_tcp->ppt_data = p_tcp->ppt_buffer; + p_tcp->ppt_data_size = p_tcp->ppt_len; + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tlm_size; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tlm_size = 6 + (5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + + if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* change the way data is written to avoid seeking if possible */ + /* TODO */ + p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); + + opj_write_bytes(l_current_data, J2K_MS_TLM, + 2); /* TLM */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_tlm_size - 2, + 2); /* Lpoc */ + l_current_data += 2; + + opj_write_bytes(l_current_data, 0, + 1); /* Ztlm=0*/ + ++l_current_data; + + opj_write_bytes(l_current_data, 0x50, + 1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ + ++l_current_data; + + /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size, + p_manager) != l_tlm_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 total_data_size, + OPJ_UINT32 * p_data_written, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_stream); + + if (total_data_size < 12) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough bytes in output buffer to write SOT marker\n"); + return OPJ_FALSE; + } + + opj_write_bytes(p_data, J2K_MS_SOT, + 2); /* SOT */ + p_data += 2; + + opj_write_bytes(p_data, 10, + 2); /* Lsot */ + p_data += 2; + + opj_write_bytes(p_data, p_j2k->m_current_tile_number, + 2); /* Isot */ + p_data += 2; + + /* Psot */ + p_data += 4; + + opj_write_bytes(p_data, + p_j2k->m_specific_param.m_encoder.m_current_tile_part_number, + 1); /* TPsot */ + ++p_data; + + opj_write_bytes(p_data, + p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts, + 1); /* TNsot */ + ++p_data; + + /* UniPG>> */ +#ifdef USE_JPWL + /* update markers struct */ + /* + OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); + */ + assert(0 && "TODO"); +#endif /* USE_JPWL */ + + * p_data_written = 12; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + OPJ_UINT32* p_tile_no, + OPJ_UINT32* p_tot_len, + OPJ_UINT32* p_current_part, + OPJ_UINT32* p_num_parts, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_header_data != 00); + assert(p_manager != 00); + + /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ + if (p_header_size != 8) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */ + p_header_data += 2; + opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */ + p_header_data += 4; + opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */ + ++p_header_data; + opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */ + ++p_header_data; + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_tot_len, l_num_parts = 0; + OPJ_UINT32 l_current_part; + OPJ_UINT32 l_tile_x, l_tile_y; + + /* preconditions */ + + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_j2k_get_sot_values(p_header_data, p_header_size, + &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); + return OPJ_FALSE; + } +#ifdef DEBUG_VERBOSE + fprintf(stderr, "SOT %d %d %d %d\n", + p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts); +#endif + + l_cp = &(p_j2k->m_cp); + + /* testcase 2.pdf.SIGFPE.706.1112 */ + if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", + p_j2k->m_current_tile_number); + return OPJ_FALSE; + } + + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; + l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; + + if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 || + p_j2k->m_current_tile_number == (OPJ_UINT32) + p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) { + /* Do only this check if we decode all tile part headers, or if */ + /* we decode one precise tile. Otherwise the m_current_tile_part_number */ + /* might not be valid */ + /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */ + /* of https://github.com/uclouvain/openjpeg/issues/939 */ + /* We must avoid reading twice the same tile part number for a given tile */ + /* so as to avoid various issues, like opj_j2k_merge_ppt being called */ + /* several times. */ + /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */ + /* should appear in increasing order. */ + if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid tile part index for tile number %d. " + "Got %d, expected %d\n", + p_j2k->m_current_tile_number, + l_current_part, + l_tcp->m_current_tile_part_number + 1); + return OPJ_FALSE; + } + } + + l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part; + +#ifdef USE_JPWL + if (l_cp->correct) { + + OPJ_UINT32 tileno = p_j2k->m_current_tile_number; + static OPJ_UINT32 backup_tileno = 0; + + /* tileno is negative or larger than the number of tiles!!! */ + if (tileno > (l_cp->tw * l_cp->th)) { + opj_event_msg(p_manager, EVT_ERROR, + "JPWL: bad tile number (%d out of a maximum of %d)\n", + tileno, (l_cp->tw * l_cp->th)); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + tileno = backup_tileno; + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" + "- setting tile number to %d\n", + tileno); + } + + /* keep your private count of tiles */ + backup_tileno++; + }; +#endif /* USE_JPWL */ + + /* look for the tile in the list of already processed tile (in parts). */ + /* Optimization possible here with a more complex data structure and with the removing of tiles */ + /* since the time taken by this function can only grow at the time */ + + /* PSot should be equal to zero or >=14 or <= 2^32-1 */ + if ((l_tot_len != 0) && (l_tot_len < 14)) { + if (l_tot_len == + 12) { /* MSD: Special case for the PHR data which are read by kakadu*/ + opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", + l_tot_len); + } else { + opj_event_msg(p_manager, EVT_ERROR, + "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len); + return OPJ_FALSE; + } + } + +#ifdef USE_JPWL + if (l_cp->correct) { + + /* totlen is negative or larger than the bytes left!!! */ + if (/*(l_tot_len < 0) ||*/ (l_tot_len > + p_header_size)) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ + opj_event_msg(p_manager, EVT_ERROR, + "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", + l_tot_len, + p_header_size); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_tot_len = 0; + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" + "- setting Psot to %d => assuming it is the last tile\n", + l_tot_len); + } + }; +#endif /* USE_JPWL */ + + /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ + if (!l_tot_len) { + opj_event_msg(p_manager, EVT_INFO, + "Psot value of the current tile-part is equal to zero, " + "we assuming it is the last tile-part of the codestream.\n"); + p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; + } + + if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) { + /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */ + opj_event_msg(p_manager, EVT_ERROR, + "In SOT marker, TPSot (%d) is not valid regards to the previous " + "number of tile-part (%d), giving up\n", l_current_part, + l_tcp->m_nb_tile_parts); + p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; + return OPJ_FALSE; + } + + if (l_num_parts != + 0) { /* Number of tile-part header is provided by this tile-part header */ + l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction; + /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of + * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ + if (l_tcp->m_nb_tile_parts) { + if (l_current_part >= l_tcp->m_nb_tile_parts) { + opj_event_msg(p_manager, EVT_ERROR, + "In SOT marker, TPSot (%d) is not valid regards to the current " + "number of tile-part (%d), giving up\n", l_current_part, + l_tcp->m_nb_tile_parts); + p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; + return OPJ_FALSE; + } + } + if (l_current_part >= l_num_parts) { + /* testcase 451.pdf.SIGSEGV.ce9.3723 */ + opj_event_msg(p_manager, EVT_ERROR, + "In SOT marker, TPSot (%d) is not valid regards to the current " + "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts); + p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; + return OPJ_FALSE; + } + l_tcp->m_nb_tile_parts = l_num_parts; + } + + /* If know the number of tile part header we will check if we didn't read the last*/ + if (l_tcp->m_nb_tile_parts) { + if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { + p_j2k->m_specific_param.m_decoder.m_can_decode = + 1; /* Process the last tile-part header*/ + } + } + + if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) { + /* Keep the size of data to skip after this marker */ + p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - + 12; /* SOT_marker_size = 12 */ + } else { + /* FIXME: need to be computed from the number of bytes remaining in the codestream */ + p_j2k->m_specific_param.m_decoder.m_sot_length = 0; + } + + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; + + /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ + if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { + p_j2k->m_specific_param.m_decoder.m_skip_data = + (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) + || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) + || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) + || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); + } else { + assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0); + p_j2k->m_specific_param.m_decoder.m_skip_data = + (p_j2k->m_current_tile_number != (OPJ_UINT32) + p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); + } + + /* Index */ + if (p_j2k->cstr_index) { + assert(p_j2k->cstr_index->tile_index != 00); + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = + p_j2k->m_current_tile_number; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = + l_current_part; + + if (l_num_parts != 0) { + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = + l_num_parts; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = + l_num_parts; + + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = + (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to read SOT marker. Tile index allocation failed\n"); + return OPJ_FALSE; + } + } else { + opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, + l_num_parts * sizeof(opj_tp_index_t)); + if (! new_tp_index) { + opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to read SOT marker. Tile index allocation failed\n"); + return OPJ_FALSE; + } + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = + new_tp_index; + } + } else { + /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { + + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = + (opj_tp_index_t*)opj_calloc( + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, + sizeof(opj_tp_index_t)); + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to read SOT marker. Tile index allocation failed\n"); + return OPJ_FALSE; + } + } + + if (l_current_part >= + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) { + opj_tp_index_t *new_tp_index; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = + l_current_part + 1; + new_tp_index = (opj_tp_index_t *) opj_realloc( + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * + sizeof(opj_tp_index_t)); + if (! new_tp_index) { + opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to read SOT marker. Tile index allocation failed\n"); + return OPJ_FALSE; + } + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = + new_tp_index; + } + } + + } + + } + + /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */ + /* if (p_j2k->cstr_info) { + if (l_tcp->first) { + if (tileno == 0) { + p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; + } + + p_j2k->cstr_info->tile[tileno].tileno = tileno; + p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; + p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; + p_j2k->cstr_info->tile[tileno].num_tps = numparts; + + if (numparts) { + p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); + } + else { + p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) + } + } + else { + p_j2k->cstr_info->tile[tileno].end_pos += totlen; + } + + p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; + p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = + p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; + }*/ + return OPJ_TRUE; +} + +/** + * Write one or more PLT markers in the provided buffer + */ +static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k, + opj_tcd_marker_info_t* marker_info, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager) +{ + OPJ_BYTE Zplt = 0; + OPJ_UINT16 Lplt; + OPJ_BYTE* p_data_start = p_data; + OPJ_BYTE* p_data_Lplt = p_data + 2; + OPJ_UINT32 i; + + OPJ_UNUSED(p_j2k); + + opj_write_bytes(p_data, J2K_MS_PLT, 2); + p_data += 2; + + /* Reserve space for Lplt */ + p_data += 2; + + opj_write_bytes(p_data, Zplt, 1); + p_data += 1; + + Lplt = 3; + + for (i = 0; i < marker_info->packet_count; i++) { + OPJ_BYTE var_bytes[5]; + OPJ_UINT8 var_bytes_size = 0; + OPJ_UINT32 packet_size = marker_info->p_packet_size[i]; + + /* Packet size written in variable-length way, starting with LSB */ + var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f); + var_bytes_size ++; + packet_size >>= 7; + while (packet_size > 0) { + var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80); + var_bytes_size ++; + packet_size >>= 7; + } + + /* Check if that can fit in the current PLT marker. If not, finish */ + /* current one, and start a new one */ + if (Lplt + var_bytes_size > 65535) { + if (Zplt == 255) { + opj_event_msg(p_manager, EVT_ERROR, + "More than 255 PLT markers would be needed for current tile-part !\n"); + return OPJ_FALSE; + } + + /* Patch Lplt */ + opj_write_bytes(p_data_Lplt, Lplt, 2); + + /* Start new segment */ + opj_write_bytes(p_data, J2K_MS_PLT, 2); + p_data += 2; + + /* Reserve space for Lplt */ + p_data_Lplt = p_data; + p_data += 2; + + Zplt ++; + opj_write_bytes(p_data, Zplt, 1); + p_data += 1; + + Lplt = 3; + } + + Lplt = (OPJ_UINT16)(Lplt + var_bytes_size); + + /* Serialize variable-length packet size, starting with MSB */ + for (; var_bytes_size > 0; --var_bytes_size) { + opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1); + p_data += 1; + } + } + + *p_data_written = (OPJ_UINT32)(p_data - p_data_start); + + /* Patch Lplt */ + opj_write_bytes(p_data_Lplt, Lplt, 2); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k, + opj_tcd_t * p_tile_coder, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 total_data_size, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + opj_codestream_info_t *l_cstr_info = 00; + OPJ_UINT32 l_remaining_data; + opj_tcd_marker_info_t* marker_info = NULL; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_stream); + + if (total_data_size < 4) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough bytes in output buffer to write SOD marker\n"); + return OPJ_FALSE; + } + + opj_write_bytes(p_data, J2K_MS_SOD, + 2); /* SOD */ + + /* make room for the EOF marker */ + l_remaining_data = total_data_size - 4; + + /* update tile coder */ + p_tile_coder->tp_num = + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; + p_tile_coder->cur_tp_num = + p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + + /* INDEX >> */ + /* TODO mergeV2: check this part which use cstr_info */ + /*l_cstr_info = p_j2k->cstr_info; + if (l_cstr_info) { + if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { + //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; + l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; + } + else {*/ + /* + TODO + if + (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) + { + cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); + }*/ + /*}*/ + /* UniPG>> */ +#ifdef USE_JPWL + /* update markers struct */ + /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); + */ + assert(0 && "TODO"); +#endif /* USE_JPWL */ + /* <m_specific_param.m_encoder.m_current_tile_part_number == 0) { + p_tile_coder->tcd_image->tiles->packno = 0; +#ifdef deadcode + if (l_cstr_info) { + l_cstr_info->packno = 0; + } +#endif + } + + *p_data_written = 0; + + if (p_j2k->m_specific_param.m_encoder.m_PLT) { + marker_info = opj_tcd_marker_info_create( + p_j2k->m_specific_param.m_encoder.m_PLT); + if (marker_info == NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "Cannot encode tile: opj_tcd_marker_info_create() failed\n"); + return OPJ_FALSE; + } + } + + if (l_remaining_data < + p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough bytes in output buffer to write SOD marker\n"); + opj_tcd_marker_info_destroy(marker_info); + return OPJ_FALSE; + } + l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT; + + if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, + p_data + 2, + p_data_written, l_remaining_data, l_cstr_info, + marker_info, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n"); + opj_tcd_marker_info_destroy(marker_info); + return OPJ_FALSE; + } + + /* For SOD */ + *p_data_written += 2; + + if (p_j2k->m_specific_param.m_encoder.m_PLT) { + OPJ_UINT32 l_data_written_PLT = 0; + OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc( + p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT); + if (!p_PLT_buffer) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n"); + opj_tcd_marker_info_destroy(marker_info); + return OPJ_FALSE; + } + if (!opj_j2k_write_plt_in_memory(p_j2k, + marker_info, + p_PLT_buffer, + &l_data_written_PLT, + p_manager)) { + opj_tcd_marker_info_destroy(marker_info); + opj_free(p_PLT_buffer); + return OPJ_FALSE; + } + + assert(l_data_written_PLT <= + p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT); + + /* Move PLT marker(s) before SOD */ + memmove(p_data + l_data_written_PLT, p_data, *p_data_written); + memcpy(p_data, p_PLT_buffer, l_data_written_PLT); + opj_free(p_PLT_buffer); + *p_data_written += l_data_written_PLT; + } + + opj_tcd_marker_info_destroy(marker_info); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_SIZE_T l_current_read_size; + opj_codestream_index_t * l_cstr_index = 00; + OPJ_BYTE ** l_current_data = 00; + opj_tcp_t * l_tcp = 00; + OPJ_UINT32 * l_tile_len = 00; + OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + + if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { + /* opj_stream_get_number_byte_left returns OPJ_OFF_T + // but we are in the last tile part, + // so its result will fit on OPJ_UINT32 unless we find + // a file with a single tile part of more than 4 GB...*/ + p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)( + opj_stream_get_number_byte_left(p_stream) - 2); + } else { + /* Check to avoid pass the limit of OPJ_UINT32 */ + if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) { + p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; + } else { + /* MSD: case commented to support empty SOT marker (PHR data) */ + } + } + + l_current_data = &(l_tcp->m_data); + l_tile_len = &l_tcp->m_data_size; + + /* Patch to support new PHR data */ + if (p_j2k->m_specific_param.m_decoder.m_sot_length) { + /* If we are here, we'll try to read the data after allocation */ + /* Check enough bytes left in stream before allocation */ + if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > + opj_stream_get_number_byte_left(p_stream)) { + opj_event_msg(p_manager, EVT_ERROR, + "Tile part length size inconsistent with stream length\n"); + return OPJ_FALSE; + } + if (p_j2k->m_specific_param.m_decoder.m_sot_length > + UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) { + opj_event_msg(p_manager, EVT_ERROR, + "p_j2k->m_specific_param.m_decoder.m_sot_length > " + "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA"); + return OPJ_FALSE; + } + /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */ + /* do so that opj_mqc_init_dec_common() can safely add a synthetic */ + /* 0xFFFF marker. */ + if (! *l_current_data) { + /* LH: oddly enough, in this path, l_tile_len!=0. + * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...). + */ + *l_current_data = (OPJ_BYTE*) opj_malloc( + p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA); + } else { + OPJ_BYTE *l_new_current_data; + if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - + p_j2k->m_specific_param.m_decoder.m_sot_length) { + opj_event_msg(p_manager, EVT_ERROR, + "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - " + "p_j2k->m_specific_param.m_decoder.m_sot_length"); + return OPJ_FALSE; + } + + l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, + *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length + + OPJ_COMMON_CBLK_DATA_EXTRA); + if (! l_new_current_data) { + opj_free(*l_current_data); + /*nothing more is done as l_current_data will be set to null, and just + afterward we enter in the error path + and the actual tile_len is updated (committed) at the end of the + function. */ + } + *l_current_data = l_new_current_data; + } + + if (*l_current_data == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n"); + return OPJ_FALSE; + } + } else { + l_sot_length_pb_detected = OPJ_TRUE; + } + + /* Index */ + l_cstr_index = p_j2k->cstr_index; + if (l_cstr_index) { + OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; + + OPJ_UINT32 l_current_tile_part = + l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; + l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header + = + l_current_pos; + l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos + = + l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; + + if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, + l_cstr_index, + J2K_MS_SOD, + l_current_pos, + p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); + return OPJ_FALSE; + } + + /*l_cstr_index->packno = 0;*/ + } + + /* Patch to support new PHR data */ + if (!l_sot_length_pb_detected) { + l_current_read_size = opj_stream_read_data( + p_stream, + *l_current_data + *l_tile_len, + p_j2k->m_specific_param.m_decoder.m_sot_length, + p_manager); + } else { + l_current_read_size = 0; + } + + if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; + } else { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + } + + *l_tile_len += (OPJ_UINT32)l_current_read_size; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_UINT32 nb_comps, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_rgn_size; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_UINT32 l_comp_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + if (nb_comps <= 256) { + l_comp_room = 1; + } else { + l_comp_room = 2; + } + + l_rgn_size = 6 + l_comp_room; + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data, J2K_MS_RGN, + 2); /* RGN */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_rgn_size - 2, + 2); /* Lrgn */ + l_current_data += 2; + + opj_write_bytes(l_current_data, p_comp_no, + l_comp_room); /* Crgn */ + l_current_data += l_comp_room; + + opj_write_bytes(l_current_data, 0, + 1); /* Srgn */ + ++l_current_data; + + opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift, + 1); /* SPrgn */ + ++l_current_data; + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size, + p_manager) != l_rgn_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data, + J2K_MS_EOC, 2); /* EOC */ + + /* UniPG>> */ +#ifdef USE_JPWL + /* update markers struct */ + /* + OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); + */ +#endif /* USE_JPWL */ + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) { + return OPJ_FALSE; + } + + if (! opj_stream_flush(p_stream, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a RGN marker (Region Of Interest) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_nb_comp; + opj_image_t * l_image = 00; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; + + /* preconditions*/ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_nb_comp = l_image->numcomps; + + if (l_nb_comp <= 256) { + l_comp_room = 1; + } else { + l_comp_room = 2; + } + + if (p_header_size != 2 + l_comp_room) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */ + p_header_data += l_comp_room; + opj_read_bytes(p_header_data, &l_roi_sty, + 1); /* Srgn */ + ++p_header_data; + +#ifdef USE_JPWL + if (l_cp->correct) { + /* totlen is negative or larger than the bytes left!!! */ + if (l_comp_room >= l_nb_comp) { + opj_event_msg(p_manager, EVT_ERROR, + "JPWL: bad component number in RGN (%d when there are only %d)\n", + l_comp_room, l_nb_comp); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + }; +#endif /* USE_JPWL */ + + /* testcase 3635.pdf.asan.77.2930 */ + if (l_comp_no >= l_nb_comp) { + opj_event_msg(p_manager, EVT_ERROR, + "bad component number in RGN (%d when there are only %d)\n", + l_comp_no, l_nb_comp); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, + (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */ + ++p_header_data; + + return OPJ_TRUE; + +} + +static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp) +{ + return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14); +} + +static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp) +{ + (void)p_tcp; + return 0; +} + +static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + opj_cp_t * l_cp = 00; + opj_image_t * l_image = 00; + opj_tcp_t * l_tcp = 00; + opj_image_comp_t * l_img_comp = 00; + + OPJ_UINT32 i, j, k; + OPJ_INT32 l_x0, l_y0, l_x1, l_y1; + OPJ_FLOAT32 * l_rates = 0; + OPJ_FLOAT32 l_sot_remove; + OPJ_UINT32 l_bits_empty, l_size_pixel; + OPJ_UINT64 l_tile_size = 0; + OPJ_UINT32 l_last_res; + OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_manager); + + l_cp = &(p_j2k->m_cp); + l_image = p_j2k->m_private_image; + l_tcp = l_cp->tcps; + + l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; + l_size_pixel = l_image->numcomps * l_image->comps->prec; + l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)( + l_cp->th * l_cp->tw); + + if (l_cp->m_specific_param.m_enc.m_tp_on) { + l_tp_stride_func = opj_j2k_get_tp_stride; + } else { + l_tp_stride_func = opj_j2k_get_default_stride; + } + + for (i = 0; i < l_cp->th; ++i) { + for (j = 0; j < l_cp->tw; ++j) { + OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / + (OPJ_FLOAT32)l_tcp->numlayers; + + /* 4 borders of the tile rescale on the image if necessary */ + l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), + (OPJ_INT32)l_image->x0); + l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), + (OPJ_INT32)l_image->y0); + l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), + (OPJ_INT32)l_image->x1); + l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), + (OPJ_INT32)l_image->y1); + + l_rates = l_tcp->rates; + + /* Modification of the RATE >> */ + for (k = 0; k < l_tcp->numlayers; ++k) { + if (*l_rates > 0.0f) { + *l_rates = (OPJ_FLOAT32)(((OPJ_FLOAT64)l_size_pixel * (OPJ_UINT32)( + l_x1 - l_x0) * + (OPJ_UINT32)(l_y1 - l_y0)) + / ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)) + - + l_offset; + } + + ++l_rates; + } + + ++l_tcp; + + } + } + + l_tcp = l_cp->tcps; + + for (i = 0; i < l_cp->th; ++i) { + for (j = 0; j < l_cp->tw; ++j) { + l_rates = l_tcp->rates; + + if (*l_rates > 0.0f) { + *l_rates -= l_sot_remove; + + if (*l_rates < 30.0f) { + *l_rates = 30.0f; + } + } + + ++l_rates; + + l_last_res = l_tcp->numlayers - 1; + + for (k = 1; k < l_last_res; ++k) { + + if (*l_rates > 0.0f) { + *l_rates -= l_sot_remove; + + if (*l_rates < * (l_rates - 1) + 10.0f) { + *l_rates = (*(l_rates - 1)) + 20.0f; + } + } + + ++l_rates; + } + + if (*l_rates > 0.0f) { + *l_rates -= (l_sot_remove + 2.f); + + if (*l_rates < * (l_rates - 1) + 10.0f) { + *l_rates = (*(l_rates - 1)) + 20.0f; + } + } + + ++l_tcp; + } + } + + l_img_comp = l_image->comps; + l_tile_size = 0; + + for (i = 0; i < l_image->numcomps; ++i) { + l_tile_size += (OPJ_UINT64)opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx) + * + opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy) + * + l_img_comp->prec; + + ++l_img_comp; + } + + /* TODO: where does this magic value come from ? */ + /* This used to be 1.3 / 8, but with random data and very small code */ + /* block sizes, this is not enough. For example with */ + /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */ + /* TODO revise this to take into account the overhead linked to the */ + /* number of packets and number of code blocks in packets */ + l_tile_size = (OPJ_UINT64)((double)l_tile_size * 1.4 / 8); + + /* Arbitrary amount to make the following work: */ + /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */ + l_tile_size += 500; + + l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); + + if (l_tile_size > UINT_MAX) { + l_tile_size = UINT_MAX; + } + + p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = (OPJ_UINT32)l_tile_size; + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = + (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); + if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate m_encoded_tile_data. %u MB required\n", + (OPJ_UINT32)(l_tile_size / 1024 / 1024)); + return OPJ_FALSE; + } + + if (OPJ_IS_CINEMA(l_cp->rsiz) || OPJ_IS_IMF(l_cp->rsiz)) { + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = + (OPJ_BYTE *) opj_malloc(5 * + p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { + return OPJ_FALSE; + } + + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; + } + + return OPJ_TRUE; +} + +#if 0 +static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i; + opj_tcd_t * l_tcd = 00; + OPJ_UINT32 l_nb_tiles; + opj_tcp_t * l_tcp = 00; + OPJ_BOOL l_success; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + + l_tcd = opj_tcd_create(OPJ_TRUE); + if (l_tcd == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + for (i = 0; i < l_nb_tiles; ++i) { + if (l_tcp->m_data) { + if (! opj_tcd_init_decode_tile(l_tcd, i)) { + opj_tcd_destroy(l_tcd); + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, + p_j2k->cstr_index); + /* cleanup */ + + if (! l_success) { + p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; + break; + } + } + + opj_j2k_tcp_destroy(l_tcp); + ++l_tcp; + } + + opj_tcd_destroy(l_tcd); + return OPJ_TRUE; +} +#endif + +static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_manager); + + p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 i; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_mct_record; + opj_tcp_t * l_tcp; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) { + return OPJ_FALSE; + } + + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + l_mct_record = l_tcp->m_mct_records; + + for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { + + if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) { + return OPJ_FALSE; + } + + ++l_mct_record; + } + + l_mcc_record = l_tcp->m_mcc_records; + + for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { + + if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) { + return OPJ_FALSE; + } + + ++l_mcc_record; + } + + if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_all_coc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 compno; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { + /* cod is first component of first tile */ + if (! opj_j2k_compare_coc(p_j2k, 0, compno)) { + if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) { + return OPJ_FALSE; + } + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_all_qcc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 compno; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) { + /* qcd is first component of first tile */ + if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) { + if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) { + return OPJ_FALSE; + } + } + } + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 compno; + const opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tccp = p_j2k->m_cp.tcps->tccps; + + for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { + if (l_tccp->roishift) { + + if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps, + p_stream, p_manager)) { + return OPJ_FALSE; + } + } + + ++l_tccp; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + opj_codestream_index_t * l_cstr_index = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_manager); + + l_cstr_index = p_j2k->cstr_index; + if (l_cstr_index) { + l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream); + /* UniPG>> */ + /* The following adjustment is done to adjust the codestream size */ + /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ + /* the first bunch of bytes is not in the codestream */ + l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start; + /* <epc_on) { + + /* encode according to JPWL */ + jpwl_encode(p_j2k, p_stream, image); + + } +#endif + assert(0 && "TODO"); +#endif /* USE_JPWL */ + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + OPJ_UINT32 *output_marker, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_unknown_marker; + const opj_dec_memory_marker_handler_t * l_marker_handler; + OPJ_UINT32 l_size_unk = 2; + + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); + + for (;;) { + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* read 2 bytes as the new marker ID*/ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, + &l_unknown_marker, 2); + + if (!(l_unknown_marker < 0xff00)) { + + /* Get the marker handler from the marker ID*/ + l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); + + if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { + opj_event_msg(p_manager, EVT_ERROR, + "Marker is not compliant with its position\n"); + return OPJ_FALSE; + } else { + if (l_marker_handler->id != J2K_MS_UNK) { + /* Add the marker to the codestream index*/ + if (l_marker_handler->id != J2K_MS_SOT) { + OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, + (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, + l_size_unk); + if (res == OPJ_FALSE) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + return OPJ_FALSE; + } + } + break; /* next marker is known and well located */ + } else { + l_size_unk += 2; + } + } + } + } + + *output_marker = l_marker_handler->id ; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k, + opj_mct_data_t * p_mct_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_mct_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tmp; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_mct_size = 10 + p_mct_record->m_data_size; + + if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data, J2K_MS_MCT, + 2); /* MCT */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_mct_size - 2, + 2); /* Lmct */ + l_current_data += 2; + + opj_write_bytes(l_current_data, 0, + 2); /* Zmct */ + l_current_data += 2; + + /* only one marker atm */ + l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | + (p_mct_record->m_element_type << 10); + + opj_write_bytes(l_current_data, l_tmp, 2); + l_current_data += 2; + + opj_write_bytes(l_current_data, 0, + 2); /* Ymct */ + l_current_data += 2; + + memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size); + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size, + p_manager) != l_mct_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a MCT marker (Multiple Component Transform) + * + * @param p_header_data the data contained in the MCT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCT marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_indix; + opj_mct_data_t * l_mct_data; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if (p_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return OPJ_FALSE; + } + + /* first marker */ + opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */ + p_header_data += 2; + if (l_tmp != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge mct data within multiple MCT records\n"); + return OPJ_TRUE; + } + + if (p_header_size <= 6) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return OPJ_FALSE; + } + + /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ + opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */ + p_header_data += 2; + + l_indix = l_tmp & 0xff; + l_mct_data = l_tcp->m_mct_records; + + for (i = 0; i < l_tcp->m_nb_mct_records; ++i) { + if (l_mct_data->m_index == l_indix) { + break; + } + ++l_mct_data; + } + + /* NOT FOUND */ + if (i == l_tcp->m_nb_mct_records) { + if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { + opj_mct_data_t *new_mct_records; + l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; + + new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, + l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if (! new_mct_records) { + opj_free(l_tcp->m_mct_records); + l_tcp->m_mct_records = NULL; + l_tcp->m_nb_max_mct_records = 0; + l_tcp->m_nb_mct_records = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n"); + return OPJ_FALSE; + } + + /* Update m_mcc_records[].m_offset_array and m_decorrelation_array + * to point to the new addresses */ + if (new_mct_records != l_tcp->m_mct_records) { + for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { + opj_simple_mcc_decorrelation_data_t* l_mcc_record = + &(l_tcp->m_mcc_records[i]); + if (l_mcc_record->m_decorrelation_array) { + l_mcc_record->m_decorrelation_array = + new_mct_records + + (l_mcc_record->m_decorrelation_array - + l_tcp->m_mct_records); + } + if (l_mcc_record->m_offset_array) { + l_mcc_record->m_offset_array = + new_mct_records + + (l_mcc_record->m_offset_array - + l_tcp->m_mct_records); + } + } + } + + l_tcp->m_mct_records = new_mct_records; + l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; + memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * + sizeof(opj_mct_data_t)); + } + + l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; + ++l_tcp->m_nb_mct_records; + } + + if (l_mct_data->m_data) { + opj_free(l_mct_data->m_data); + l_mct_data->m_data = 00; + l_mct_data->m_data_size = 0; + } + + l_mct_data->m_index = l_indix; + l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); + l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); + + opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */ + p_header_data += 2; + if (l_tmp != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge multiple MCT markers\n"); + return OPJ_TRUE; + } + + p_header_size -= 6; + + l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); + if (! l_mct_data->m_data) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return OPJ_FALSE; + } + memcpy(l_mct_data->m_data, p_header_data, p_header_size); + + l_mct_data->m_data_size = p_header_size; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k, + struct opj_simple_mcc_decorrelation_data * p_mcc_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_mcc_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_bytes_for_comp; + OPJ_UINT32 l_mask; + OPJ_UINT32 l_tmcc; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + if (p_mcc_record->m_nb_comps > 255) { + l_nb_bytes_for_comp = 2; + l_mask = 0x8000; + } else { + l_nb_bytes_for_comp = 1; + l_mask = 0; + } + + l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; + if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data, J2K_MS_MCC, + 2); /* MCC */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_mcc_size - 2, + 2); /* Lmcc */ + l_current_data += 2; + + /* first marker */ + opj_write_bytes(l_current_data, 0, + 2); /* Zmcc */ + l_current_data += 2; + + opj_write_bytes(l_current_data, p_mcc_record->m_index, + 1); /* Imcc -> no need for other values, take the first */ + ++l_current_data; + + /* only one marker atm */ + opj_write_bytes(l_current_data, 0, + 2); /* Ymcc */ + l_current_data += 2; + + opj_write_bytes(l_current_data, 1, + 2); /* Qmcc -> number of collections -> 1 */ + l_current_data += 2; + + opj_write_bytes(l_current_data, 0x1, + 1); /* Xmcci type of component transformation -> array based decorrelation */ + ++l_current_data; + + opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, + 2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ + l_current_data += 2; + + for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { + opj_write_bytes(l_current_data, i, + l_nb_bytes_for_comp); /* Cmccij Component offset*/ + l_current_data += l_nb_bytes_for_comp; + } + + opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask, + 2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ + l_current_data += 2; + + for (i = 0; i < p_mcc_record->m_nb_comps; ++i) { + opj_write_bytes(l_current_data, i, + l_nb_bytes_for_comp); /* Wmccij Component offset*/ + l_current_data += l_nb_bytes_for_comp; + } + + l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16; + + if (p_mcc_record->m_decorrelation_array) { + l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; + } + + if (p_mcc_record->m_offset_array) { + l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8); + } + + opj_write_bytes(l_current_data, l_tmcc, + 3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ + l_current_data += 3; + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size, + p_manager) != l_mcc_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, j; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_indix; + opj_tcp_t * l_tcp; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_mct_data; + OPJ_UINT32 l_nb_collections; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_nb_bytes_by_comp; + OPJ_BOOL l_new_mcc = OPJ_FALSE; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if (p_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + /* first marker */ + opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */ + p_header_data += 2; + if (l_tmp != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge multiple data spanning\n"); + return OPJ_TRUE; + } + + if (p_header_size < 7) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_indix, + 1); /* Imcc -> no need for other values, take the first */ + ++p_header_data; + + l_mcc_record = l_tcp->m_mcc_records; + + for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { + if (l_mcc_record->m_index == l_indix) { + break; + } + ++l_mcc_record; + } + + /** NOT FOUND */ + if (i == l_tcp->m_nb_mcc_records) { + if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { + opj_simple_mcc_decorrelation_data_t *new_mcc_records; + l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; + + new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( + l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof( + opj_simple_mcc_decorrelation_data_t)); + if (! new_mcc_records) { + opj_free(l_tcp->m_mcc_records); + l_tcp->m_mcc_records = NULL; + l_tcp->m_nb_max_mcc_records = 0; + l_tcp->m_nb_mcc_records = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n"); + return OPJ_FALSE; + } + l_tcp->m_mcc_records = new_mcc_records; + l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; + memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records) + * sizeof(opj_simple_mcc_decorrelation_data_t)); + } + l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; + l_new_mcc = OPJ_TRUE; + } + l_mcc_record->m_index = l_indix; + + /* only one marker atm */ + opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */ + p_header_data += 2; + if (l_tmp != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge multiple data spanning\n"); + return OPJ_TRUE; + } + + opj_read_bytes(p_header_data, &l_nb_collections, + 2); /* Qmcc -> number of collections -> 1 */ + p_header_data += 2; + + if (l_nb_collections > 1) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge multiple collections\n"); + return OPJ_TRUE; + } + + p_header_size -= 7; + + for (i = 0; i < l_nb_collections; ++i) { + if (p_header_size < 3) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_tmp, + 1); /* Xmcci type of component transformation -> array based decorrelation */ + ++p_header_data; + + if (l_tmp != 1) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge collections other than array decorrelation\n"); + return OPJ_TRUE; + } + + opj_read_bytes(p_header_data, &l_nb_comps, 2); + + p_header_data += 2; + p_header_size -= 3; + + l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); + l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; + + if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); + + for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { + opj_read_bytes(p_header_data, &l_tmp, + l_nb_bytes_by_comp); /* Cmccij Component offset*/ + p_header_data += l_nb_bytes_by_comp; + + if (l_tmp != j) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge collections with indix shuffle\n"); + return OPJ_TRUE; + } + } + + opj_read_bytes(p_header_data, &l_nb_comps, 2); + p_header_data += 2; + + l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15); + l_nb_comps &= 0x7fff; + + if (l_nb_comps != l_mcc_record->m_nb_comps) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge collections without same number of indixes\n"); + return OPJ_TRUE; + } + + if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); + + for (j = 0; j < l_mcc_record->m_nb_comps; ++j) { + opj_read_bytes(p_header_data, &l_tmp, + l_nb_bytes_by_comp); /* Wmccij Component offset*/ + p_header_data += l_nb_bytes_by_comp; + + if (l_tmp != j) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge collections with indix shuffle\n"); + return OPJ_TRUE; + } + } + + opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/ + p_header_data += 3; + + l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1); + l_mcc_record->m_decorrelation_array = 00; + l_mcc_record->m_offset_array = 00; + + l_indix = l_tmp & 0xff; + if (l_indix != 0) { + l_mct_data = l_tcp->m_mct_records; + for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { + if (l_mct_data->m_index == l_indix) { + l_mcc_record->m_decorrelation_array = l_mct_data; + break; + } + ++l_mct_data; + } + + if (l_mcc_record->m_decorrelation_array == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + } + + l_indix = (l_tmp >> 8) & 0xff; + if (l_indix != 0) { + l_mct_data = l_tcp->m_mct_records; + for (j = 0; j < l_tcp->m_nb_mct_records; ++j) { + if (l_mct_data->m_index == l_indix) { + l_mcc_record->m_offset_array = l_mct_data; + break; + } + ++l_mct_data; + } + + if (l_mcc_record->m_offset_array == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + } + } + + if (p_header_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + if (l_new_mcc) { + ++l_tcp->m_nb_mcc_records; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_mco_size; + opj_tcp_t * l_tcp = 00; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + OPJ_UINT32 i; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + + l_mco_size = 5 + l_tcp->m_nb_mcc_records; + if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; + } + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + + opj_write_bytes(l_current_data, J2K_MS_MCO, 2); /* MCO */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_mco_size - 2, 2); /* Lmco */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records, + 1); /* Nmco : only one transform stage*/ + ++l_current_data; + + l_mcc_record = l_tcp->m_mcc_records; + for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) { + opj_write_bytes(l_current_data, l_mcc_record->m_index, + 1); /* Imco -> use the mcc indicated by 1*/ + ++l_current_data; + ++l_mcc_record; + } + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size, + p_manager) != l_mco_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a MCO marker (Multiple Component Transform Ordering) + * + * @param p_header_data the data contained in the MCO box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCO marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_tmp, i; + OPJ_UINT32 l_nb_stages; + opj_tcp_t * l_tcp; + opj_tccp_t * l_tccp; + opj_image_t * l_image; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if (p_header_size < 1) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_nb_stages, + 1); /* Nmco : only one transform stage*/ + ++p_header_data; + + if (l_nb_stages > 1) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot take in charge multiple transformation stages.\n"); + return OPJ_TRUE; + } + + if (p_header_size != l_nb_stages + 1) { + opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); + return OPJ_FALSE; + } + + l_tccp = l_tcp->tccps; + + for (i = 0; i < l_image->numcomps; ++i) { + l_tccp->m_dc_level_shift = 0; + ++l_tccp; + } + + if (l_tcp->m_mct_decoding_matrix) { + opj_free(l_tcp->m_mct_decoding_matrix); + l_tcp->m_mct_decoding_matrix = 00; + } + + for (i = 0; i < l_nb_stages; ++i) { + opj_read_bytes(p_header_data, &l_tmp, 1); + ++p_header_data; + + if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) { + return OPJ_FALSE; + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, + OPJ_UINT32 p_index) +{ + OPJ_UINT32 i; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_deco_array, * l_offset_array; + OPJ_UINT32 l_data_size, l_mct_size, l_offset_size; + OPJ_UINT32 l_nb_elem; + OPJ_UINT32 * l_offset_data, * l_current_offset_data; + opj_tccp_t * l_tccp; + + /* preconditions */ + assert(p_tcp != 00); + + l_mcc_record = p_tcp->m_mcc_records; + + for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) { + if (l_mcc_record->m_index == p_index) { + break; + } + } + + if (i == p_tcp->m_nb_mcc_records) { + /** element discarded **/ + return OPJ_TRUE; + } + + if (l_mcc_record->m_nb_comps != p_image->numcomps) { + /** do not support number of comps != image */ + return OPJ_TRUE; + } + + l_deco_array = l_mcc_record->m_decorrelation_array; + + if (l_deco_array) { + l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps + * p_image->numcomps; + if (l_deco_array->m_data_size != l_data_size) { + return OPJ_FALSE; + } + + l_nb_elem = p_image->numcomps * p_image->numcomps; + l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); + p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); + + if (! p_tcp->m_mct_decoding_matrix) { + return OPJ_FALSE; + } + + j2k_mct_read_functions_to_float[l_deco_array->m_element_type]( + l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem); + } + + l_offset_array = l_mcc_record->m_offset_array; + + if (l_offset_array) { + l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * + p_image->numcomps; + if (l_offset_array->m_data_size != l_data_size) { + return OPJ_FALSE; + } + + l_nb_elem = p_image->numcomps; + l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); + l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); + + if (! l_offset_data) { + return OPJ_FALSE; + } + + j2k_mct_read_functions_to_int32[l_offset_array->m_element_type]( + l_offset_array->m_data, l_offset_data, l_nb_elem); + + l_tccp = p_tcp->tccps; + l_current_offset_data = l_offset_data; + + for (i = 0; i < p_image->numcomps; ++i) { + l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++); + ++l_tccp; + } + + opj_free(l_offset_data); + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_cbd_size; + OPJ_BYTE * l_current_data = 00; + opj_image_t *l_image = 00; + opj_image_comp_t * l_comp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_image = p_j2k->m_private_image; + l_cbd_size = 6 + p_j2k->m_private_image->numcomps; + + if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data, J2K_MS_CBD, 2); /* CBD */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_cbd_size - 2, 2); /* L_CBD */ + l_current_data += 2; + + opj_write_bytes(l_current_data, l_image->numcomps, 2); /* Ncbd */ + l_current_data += 2; + + l_comp = l_image->comps; + + for (i = 0; i < l_image->numcomps; ++i) { + opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), + 1); /* Component bit depth */ + ++l_current_data; + + ++l_comp; + } + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size, + p_manager) != l_cbd_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads a CBD marker (Component bit depth definition) + * @param p_header_data the data contained in the CBD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the CBD marker. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_nb_comp, l_num_comp; + OPJ_UINT32 l_comp_def; + OPJ_UINT32 i; + opj_image_comp_t * l_comp = 00; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_num_comp = p_j2k->m_private_image->numcomps; + + if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { + opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &l_nb_comp, + 2); /* Ncbd */ + p_header_data += 2; + + if (l_nb_comp != l_num_comp) { + opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + return OPJ_FALSE; + } + + l_comp = p_j2k->m_private_image->comps; + for (i = 0; i < l_num_comp; ++i) { + opj_read_bytes(p_header_data, &l_comp_def, + 1); /* Component bit depth */ + ++p_header_data; + l_comp->sgnd = (l_comp_def >> 7) & 1; + l_comp->prec = (l_comp_def & 0x7f) + 1; + + if (l_comp->prec > 31) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n", + i, l_comp->prec); + return OPJ_FALSE; + } + ++l_comp; + } + + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ +/* J2K / JPT decoder interface */ +/* ----------------------------------------------------------------------- */ + +void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) +{ + if (j2k && parameters) { + j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; + j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; + + j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG); +#ifdef USE_JPWL + j2k->m_cp.correct = parameters->jpwl_correct; + j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; + j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; +#endif /* USE_JPWL */ + } +} + +OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads) +{ + /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */ + /* afterwards */ + if (opj_has_thread_support() && j2k->m_tcd == NULL) { + opj_thread_pool_destroy(j2k->m_tp); + j2k->m_tp = NULL; + if (num_threads <= (OPJ_UINT32)INT_MAX) { + j2k->m_tp = opj_thread_pool_create((int)num_threads); + } + if (j2k->m_tp == NULL) { + j2k->m_tp = opj_thread_pool_create(0); + return OPJ_FALSE; + } + return OPJ_TRUE; + } + return OPJ_FALSE; +} + +static int opj_j2k_get_default_thread_count() +{ + const char* num_threads_str = getenv("OPJ_NUM_THREADS"); + int num_cpus; + int num_threads; + + if (num_threads_str == NULL || !opj_has_thread_support()) { + return 0; + } + num_cpus = opj_get_num_cpus(); + if (strcmp(num_threads_str, "ALL_CPUS") == 0) { + return num_cpus; + } + if (num_cpus == 0) { + num_cpus = 32; + } + num_threads = atoi(num_threads_str); + if (num_threads < 0) { + num_threads = 0; + } else if (num_threads > 2 * num_cpus) { + num_threads = 2 * num_cpus; + } + return num_threads; +} + +/* ----------------------------------------------------------------------- */ +/* J2K encoder interface */ +/* ----------------------------------------------------------------------- */ + +opj_j2k_t* opj_j2k_create_compress(void) +{ + opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); + if (!l_j2k) { + return NULL; + } + + + l_j2k->m_is_decoder = 0; + l_j2k->m_cp.m_is_decoder = 0; + + l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc( + OPJ_J2K_DEFAULT_HEADER_SIZE); + if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = + OPJ_J2K_DEFAULT_HEADER_SIZE; + + /* validation list creation*/ + l_j2k->m_validation_list = opj_procedure_list_create(); + if (! l_j2k->m_validation_list) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + /* execution list creation*/ + l_j2k->m_procedure_list = opj_procedure_list_create(); + if (! l_j2k->m_procedure_list) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); + if (!l_j2k->m_tp) { + l_j2k->m_tp = opj_thread_pool_create(0); + } + if (!l_j2k->m_tp) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + return l_j2k; +} + +static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres) +{ + POC[0].tile = 1; + POC[0].resno0 = 0; + POC[0].compno0 = 0; + POC[0].layno1 = 1; + POC[0].resno1 = (OPJ_UINT32)(numres - 1); + POC[0].compno1 = 3; + POC[0].prg1 = OPJ_CPRL; + POC[1].tile = 1; + POC[1].resno0 = (OPJ_UINT32)(numres - 1); + POC[1].compno0 = 0; + POC[1].layno1 = 1; + POC[1].resno1 = (OPJ_UINT32)numres; + POC[1].compno1 = 3; + POC[1].prg1 = OPJ_CPRL; + return 2; +} + +static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, + opj_image_t *image, opj_event_mgr_t *p_manager) +{ + /* Configure cinema parameters */ + int i; + + /* No tiling */ + parameters->tile_size_on = OPJ_FALSE; + parameters->cp_tdx = 1; + parameters->cp_tdy = 1; + + /* One tile part for each component */ + parameters->tp_flag = 'C'; + parameters->tp_on = 1; + + /* Tile and Image shall be at (0,0) */ + parameters->cp_tx0 = 0; + parameters->cp_ty0 = 0; + parameters->image_offset_x0 = 0; + parameters->image_offset_y0 = 0; + + /* Codeblock size= 32*32 */ + parameters->cblockw_init = 32; + parameters->cblockh_init = 32; + + /* Codeblock style: no mode switch enabled */ + parameters->mode = 0; + + /* No ROI */ + parameters->roi_compno = -1; + + /* No subsampling */ + parameters->subsampling_dx = 1; + parameters->subsampling_dy = 1; + + /* 9-7 transform */ + parameters->irreversible = 1; + + /* Number of layers */ + if (parameters->tcp_numlayers > 1) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" + "1 single quality layer" + "-> Number of layers forced to 1 (rather than %d)\n" + "-> Rate of the last layer (%3.1f) will be used", + parameters->tcp_numlayers, + parameters->tcp_rates[parameters->tcp_numlayers - 1]); + parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1]; + parameters->tcp_numlayers = 1; + } + + /* Resolution levels */ + switch (parameters->rsiz) { + case OPJ_PROFILE_CINEMA_2K: + if (parameters->numresolution > 6) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 (2k dc profile) requires:\n" + "Number of decomposition levels <= 5\n" + "-> Number of decomposition levels forced to 5 (rather than %d)\n", + parameters->numresolution + 1); + parameters->numresolution = 6; + } + break; + case OPJ_PROFILE_CINEMA_4K: + if (parameters->numresolution < 2) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-4 (4k dc profile) requires:\n" + "Number of decomposition levels >= 1 && <= 6\n" + "-> Number of decomposition levels forced to 1 (rather than %d)\n", + parameters->numresolution + 1); + parameters->numresolution = 1; + } else if (parameters->numresolution > 7) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-4 (4k dc profile) requires:\n" + "Number of decomposition levels >= 1 && <= 6\n" + "-> Number of decomposition levels forced to 6 (rather than %d)\n", + parameters->numresolution + 1); + parameters->numresolution = 7; + } + break; + default : + break; + } + + /* Precincts */ + parameters->csty |= J2K_CP_CSTY_PRT; + if (parameters->numresolution == 1) { + parameters->res_spec = 1; + parameters->prcw_init[0] = 128; + parameters->prch_init[0] = 128; + } else { + parameters->res_spec = parameters->numresolution - 1; + for (i = 0; i < parameters->res_spec; i++) { + parameters->prcw_init[i] = 256; + parameters->prch_init[i] = 256; + } + } + + /* The progression order shall be CPRL */ + parameters->prog_order = OPJ_CPRL; + + /* Progression order changes for 4K, disallowed for 2K */ + if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) { + parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC, + parameters->numresolution); + } else { + parameters->numpocs = 0; + } + + /* Limited bit-rate */ + parameters->cp_disto_alloc = 1; + if (parameters->max_cs_size <= 0) { + /* No rate has been introduced, 24 fps is assumed */ + parameters->max_cs_size = OPJ_CINEMA_24_CS; + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" + "Maximum 1302083 compressed bytes @ 24fps\n" + "As no rate has been given, this limit will be used.\n"); + } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" + "Maximum 1302083 compressed bytes @ 24fps\n" + "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n"); + parameters->max_cs_size = OPJ_CINEMA_24_CS; + } + + if (parameters->max_comp_size <= 0) { + /* No rate has been introduced, 24 fps is assumed */ + parameters->max_comp_size = OPJ_CINEMA_24_COMP; + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" + "Maximum 1041666 compressed bytes @ 24fps\n" + "As no rate has been given, this limit will be used.\n"); + } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n" + "Maximum 1041666 compressed bytes @ 24fps\n" + "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n"); + parameters->max_comp_size = OPJ_CINEMA_24_COMP; + } + + parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * + image->comps[0].h * image->comps[0].prec) / + (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * + image->comps[0].dy); + +} + +static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, + opj_event_mgr_t *p_manager) +{ + OPJ_UINT32 i; + + /* Number of components */ + if (image->numcomps != 3) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 (2k dc profile) requires:\n" + "3 components" + "-> Number of components of input image (%d) is not compliant\n" + "-> Non-profile-3 codestream will be generated\n", + image->numcomps); + return OPJ_FALSE; + } + + /* Bitdepth */ + for (i = 0; i < image->numcomps; i++) { + if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)) { + char signed_str[] = "signed"; + char unsigned_str[] = "unsigned"; + char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 (2k dc profile) requires:\n" + "Precision of each component shall be 12 bits unsigned" + "-> At least component %d of input image (%d bits, %s) is not compliant\n" + "-> Non-profile-3 codestream will be generated\n", + i, image->comps[i].bpp, tmp_str); + return OPJ_FALSE; + } + } + + /* Image size */ + switch (rsiz) { + case OPJ_PROFILE_CINEMA_2K: + if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-3 (2k dc profile) requires:\n" + "width <= 2048 and height <= 1080\n" + "-> Input image size %d x %d is not compliant\n" + "-> Non-profile-3 codestream will be generated\n", + image->comps[0].w, image->comps[0].h); + return OPJ_FALSE; + } + break; + case OPJ_PROFILE_CINEMA_4K: + if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Profile-4 (4k dc profile) requires:\n" + "width <= 4096 and height <= 2160\n" + "-> Image size %d x %d is not compliant\n" + "-> Non-profile-4 codestream will be generated\n", + image->comps[0].w, image->comps[0].h); + return OPJ_FALSE; + } + break; + default : + break; + } + + return OPJ_TRUE; +} + +static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters, + opj_image_t *image) +{ + /* Decomposition levels */ + const OPJ_UINT16 rsiz = parameters->rsiz; + const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); + const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) + parameters->cp_tdx : image->x1; + switch (profile) { + case OPJ_PROFILE_IMF_2K: + return 5; + case OPJ_PROFILE_IMF_4K: + return 6; + case OPJ_PROFILE_IMF_8K: + return 7; + case OPJ_PROFILE_IMF_2K_R: { + if (XTsiz >= 2048) { + return 5; + } else if (XTsiz >= 1024) { + return 4; + } + break; + } + case OPJ_PROFILE_IMF_4K_R: { + if (XTsiz >= 4096) { + return 6; + } else if (XTsiz >= 2048) { + return 5; + } else if (XTsiz >= 1024) { + return 4; + } + break; + } + case OPJ_PROFILE_IMF_8K_R: { + if (XTsiz >= 8192) { + return 7; + } else if (XTsiz >= 4096) { + return 6; + } else if (XTsiz >= 2048) { + return 5; + } else if (XTsiz >= 1024) { + return 4; + } + break; + } + default: + break; + } + return -1; +} + +static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters, + opj_image_t *image, opj_event_mgr_t *p_manager) +{ + const OPJ_UINT16 rsiz = parameters->rsiz; + const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); + + OPJ_UNUSED(p_manager); + + /* Override defaults set by opj_set_default_encoder_parameters */ + if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW && + parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) { + parameters->cblockw_init = 32; + parameters->cblockh_init = 32; + } + + /* One tile part for each component */ + parameters->tp_flag = 'C'; + parameters->tp_on = 1; + + if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) { + parameters->prog_order = OPJ_CPRL; + } + + if (profile == OPJ_PROFILE_IMF_2K || + profile == OPJ_PROFILE_IMF_4K || + profile == OPJ_PROFILE_IMF_8K) { + /* 9-7 transform */ + parameters->irreversible = 1; + } + + /* Adjust the number of resolutions if set to its defaults */ + if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION && + image->x0 == 0 && + image->y0 == 0) { + const int max_NL = opj_j2k_get_imf_max_NL(parameters, image); + if (max_NL >= 0 && parameters->numresolution > max_NL) { + parameters->numresolution = max_NL + 1; + } + + /* Note: below is generic logic */ + if (!parameters->tile_size_on) { + while (parameters->numresolution > 0) { + if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { + parameters->numresolution --; + continue; + } + if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) { + parameters->numresolution --; + continue; + } + break; + } + } + } + + /* Set defaults precincts */ + if (parameters->csty == 0) { + parameters->csty |= J2K_CP_CSTY_PRT; + if (parameters->numresolution == 1) { + parameters->res_spec = 1; + parameters->prcw_init[0] = 128; + parameters->prch_init[0] = 128; + } else { + int i; + parameters->res_spec = parameters->numresolution - 1; + for (i = 0; i < parameters->res_spec; i++) { + parameters->prcw_init[i] = 256; + parameters->prch_init[i] = 256; + } + } + } +} + +/* Table A.53 from JPEG2000 standard */ +static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = { + 15, /* unspecified */ + 1, + 1, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9 +}; + +static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t *p_manager) +{ + OPJ_UINT32 i; + const OPJ_UINT16 rsiz = parameters->rsiz; + const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz); + const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz); + const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz); + const int NL = parameters->numresolution - 1; + const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32) + parameters->cp_tdx : image->x1; + OPJ_BOOL ret = OPJ_TRUE; + + /* Validate mainlevel */ + if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile require mainlevel <= 11.\n" + "-> %d is thus not compliant\n" + "-> Non-IMF codestream will be generated\n", + mainlevel); + ret = OPJ_FALSE; + } + + /* Validate sublevel */ + assert(sizeof(tabMaxSubLevelFromMainLevel) == + (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0])); + if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile require sublevel <= %d for mainlevel = %d.\n" + "-> %d is thus not compliant\n" + "-> Non-IMF codestream will be generated\n", + tabMaxSubLevelFromMainLevel[mainlevel], + mainlevel, + sublevel); + ret = OPJ_FALSE; + } + + /* Number of components */ + if (image->numcomps > 3) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require at most 3 components.\n" + "-> Number of components of input image (%d) is not compliant\n" + "-> Non-IMF codestream will be generated\n", + image->numcomps); + ret = OPJ_FALSE; + } + + if (image->x0 != 0 || image->y0 != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require image origin to be at 0,0.\n" + "-> %d,%d is not compliant\n" + "-> Non-IMF codestream will be generated\n", + image->x0, image->y0 != 0); + ret = OPJ_FALSE; + } + + if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require tile origin to be at 0,0.\n" + "-> %d,%d is not compliant\n" + "-> Non-IMF codestream will be generated\n", + parameters->cp_tx0, parameters->cp_ty0); + ret = OPJ_FALSE; + } + + if (parameters->tile_size_on) { + if (profile == OPJ_PROFILE_IMF_2K || + profile == OPJ_PROFILE_IMF_4K || + profile == OPJ_PROFILE_IMF_8K) { + if ((OPJ_UINT32)parameters->cp_tdx < image->x1 || + (OPJ_UINT32)parameters->cp_tdy < image->y1) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n" + "-> %d,%d is lesser than %d,%d\n" + "-> Non-IMF codestream will be generated\n", + parameters->cp_tdx, + parameters->cp_tdy, + image->x1, + image->y1); + ret = OPJ_FALSE; + } + } else { + if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 && + (OPJ_UINT32)parameters->cp_tdy >= image->y1) { + /* ok */ + } else if (parameters->cp_tdx == 1024 && + parameters->cp_tdy == 1024) { + /* ok */ + } else if (parameters->cp_tdx == 2048 && + parameters->cp_tdy == 2048 && + (profile == OPJ_PROFILE_IMF_4K || + profile == OPJ_PROFILE_IMF_8K)) { + /* ok */ + } else if (parameters->cp_tdx == 4096 && + parameters->cp_tdy == 4096 && + profile == OPJ_PROFILE_IMF_8K) { + /* ok */ + } else { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K_R/4K_R/8K_R single/multiple tile profiles " + "require tile to be greater or equal to image size,\n" + "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R " + "or (4096,4096) for 8K_R.\n" + "-> %d,%d is non conformant\n" + "-> Non-IMF codestream will be generated\n", + parameters->cp_tdx, + parameters->cp_tdy); + ret = OPJ_FALSE; + } + } + } + + /* Bitdepth */ + for (i = 0; i < image->numcomps; i++) { + if (!(image->comps[i].bpp >= 8 && image->comps[i].bpp <= 16) || + (image->comps[i].sgnd)) { + char signed_str[] = "signed"; + char unsigned_str[] = "unsigned"; + char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str; + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require precision of each component to b in [8-16] bits unsigned" + "-> At least component %d of input image (%d bits, %s) is not compliant\n" + "-> Non-IMF codestream will be generated\n", + i, image->comps[i].bpp, tmp_str); + ret = OPJ_FALSE; + } + } + + /* Sub-sampling */ + for (i = 0; i < image->numcomps; i++) { + if (i == 0 && image->comps[i].dx != 1) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n" + "-> Non-IMF codestream will be generated\n", + image->comps[i].dx); + ret = OPJ_FALSE; + } + if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n" + "-> Non-IMF codestream will be generated\n", + image->comps[i].dx); + ret = OPJ_FALSE; + } + if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require XRSiz%d to be the same as XRSiz2. " + "Here it is set to %d instead of %d.\n" + "-> Non-IMF codestream will be generated\n", + i + 1, image->comps[i].dx, image->comps[i - 1].dx); + ret = OPJ_FALSE; + } + if (image->comps[i].dy != 1) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require YRsiz == 1. " + "Here it is set to %d for component i.\n" + "-> Non-IMF codestream will be generated\n", + image->comps[i].dy, i); + ret = OPJ_FALSE; + } + } + + /* Image size */ + switch (profile) { + case OPJ_PROFILE_IMF_2K: + case OPJ_PROFILE_IMF_2K_R: + if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K/2K_R profile require:\n" + "width <= 2048 and height <= 1556\n" + "-> Input image size %d x %d is not compliant\n" + "-> Non-IMF codestream will be generated\n", + image->comps[0].w, image->comps[0].h); + ret = OPJ_FALSE; + } + break; + case OPJ_PROFILE_IMF_4K: + case OPJ_PROFILE_IMF_4K_R: + if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K/4K_R profile require:\n" + "width <= 4096 and height <= 3112\n" + "-> Input image size %d x %d is not compliant\n" + "-> Non-IMF codestream will be generated\n", + image->comps[0].w, image->comps[0].h); + ret = OPJ_FALSE; + } + break; + case OPJ_PROFILE_IMF_8K: + case OPJ_PROFILE_IMF_8K_R: + if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 8K/8K_R profile require:\n" + "width <= 8192 and height <= 6224\n" + "-> Input image size %d x %d is not compliant\n" + "-> Non-IMF codestream will be generated\n", + image->comps[0].w, image->comps[0].h); + ret = OPJ_FALSE; + } + break; + default : + assert(0); + return OPJ_FALSE; + } + + if (parameters->roi_compno != -1) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile forbid RGN / region of interest marker.\n" + "-> Compression parameters specify a ROI\n" + "-> Non-IMF codestream will be generated\n"); + ret = OPJ_FALSE; + } + + if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile require code block size to be 32x32.\n" + "-> Compression parameters set it to %dx%d.\n" + "-> Non-IMF codestream will be generated\n", + parameters->cblockw_init, + parameters->cblockh_init); + ret = OPJ_FALSE; + } + + if (parameters->prog_order != OPJ_CPRL) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile require progression order to be CPRL.\n" + "-> Compression parameters set it to %d.\n" + "-> Non-IMF codestream will be generated\n", + parameters->prog_order); + ret = OPJ_FALSE; + } + + if (parameters->numpocs != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile forbid POC markers.\n" + "-> Compression parameters set %d POC.\n" + "-> Non-IMF codestream will be generated\n", + parameters->numpocs); + ret = OPJ_FALSE; + } + + /* Codeblock style: no mode switch enabled */ + if (parameters->mode != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profile forbid mode switch in code block style.\n" + "-> Compression parameters set code block style to %d.\n" + "-> Non-IMF codestream will be generated\n", + parameters->mode); + ret = OPJ_FALSE; + } + + if (profile == OPJ_PROFILE_IMF_2K || + profile == OPJ_PROFILE_IMF_4K || + profile == OPJ_PROFILE_IMF_8K) { + /* Expect 9-7 transform */ + if (parameters->irreversible != 1) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n" + "-> Compression parameters set it to reversible.\n" + "-> Non-IMF codestream will be generated\n"); + ret = OPJ_FALSE; + } + } else { + /* Expect 5-3 transform */ + if (parameters->irreversible != 0) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n" + "-> Compression parameters set it to irreversible.\n" + "-> Non-IMF codestream will be generated\n"); + ret = OPJ_FALSE; + } + } + + /* Number of layers */ + if (parameters->tcp_numlayers != 1) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K/4K/8K profiles require 1 single quality layer.\n" + "-> Number of layers is %d.\n" + "-> Non-IMF codestream will be generated\n", + parameters->tcp_numlayers); + ret = OPJ_FALSE; + } + + /* Decomposition levels */ + switch (profile) { + case OPJ_PROFILE_IMF_2K: + if (!(NL >= 1 && NL <= 5)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K profile requires 1 <= NL <= 5:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + break; + case OPJ_PROFILE_IMF_4K: + if (!(NL >= 1 && NL <= 6)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K profile requires 1 <= NL <= 6:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + break; + case OPJ_PROFILE_IMF_8K: + if (!(NL >= 1 && NL <= 7)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 8K profile requires 1 <= NL <= 7:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + break; + case OPJ_PROFILE_IMF_2K_R: { + if (XTsiz >= 2048) { + if (!(NL >= 1 && NL <= 5)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else if (XTsiz >= 1024) { + if (!(NL >= 1 && NL <= 4)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } + break; + } + case OPJ_PROFILE_IMF_4K_R: { + if (XTsiz >= 4096) { + if (!(NL >= 1 && NL <= 6)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else if (XTsiz >= 2048) { + if (!(NL >= 1 && NL <= 5)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else if (XTsiz >= 1024) { + if (!(NL >= 1 && NL <= 4)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } + break; + } + case OPJ_PROFILE_IMF_8K_R: { + if (XTsiz >= 8192) { + if (!(NL >= 1 && NL <= 7)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else if (XTsiz >= 4096) { + if (!(NL >= 1 && NL <= 6)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else if (XTsiz >= 2048) { + if (!(NL >= 1 && NL <= 5)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else if (XTsiz >= 1024) { + if (!(NL >= 1 && NL <= 4)) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n" + "-> Number of decomposition levels is %d.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } + break; + } + default: + break; + } + + if (parameters->numresolution == 1) { + if (parameters->res_spec != 1 || + parameters->prcw_init[0] != 128 || + parameters->prch_init[0] != 128) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" + "-> Supplied values are different from that.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } else { + int i; + for (i = 0; i < parameters->res_spec; i++) { + if (parameters->prcw_init[i] != 256 || + parameters->prch_init[i] != 256) { + opj_event_msg(p_manager, EVT_WARNING, + "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n" + "-> Supplied values are different from that.\n" + "-> Non-IMF codestream will be generated\n", + NL); + ret = OPJ_FALSE; + } + } + } + + return ret; +} + + +OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, + opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, j, tileno, numpocs_tile; + opj_cp_t *cp = 00; + OPJ_UINT32 cblkw, cblkh; + + if (!p_j2k || !parameters || ! image) { + return OPJ_FALSE; + } + + if ((parameters->numresolution <= 0) || + (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid number of resolutions : %d not in range [1,%d]\n", + parameters->numresolution, OPJ_J2K_MAXRLVLS); + return OPJ_FALSE; + } + + if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", + parameters->cblockw_init); + return OPJ_FALSE; + } + if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n", + parameters->cblockh_init); + return OPJ_FALSE; + } + if (parameters->cblockw_init * parameters->cblockh_init > 4096) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n"); + return OPJ_FALSE; + } + cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); + cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); + if (parameters->cblockw_init != (1 << cblkw)) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", + parameters->cblockw_init); + return OPJ_FALSE; + } + if (parameters->cblockh_init != (1 << cblkh)) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n", + parameters->cblockh_init); + return OPJ_FALSE; + } + + /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ + cp = &(p_j2k->m_cp); + + /* set default values for cp */ + cp->tw = 1; + cp->th = 1; + + /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */ + if (parameters->rsiz == + OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */ + OPJ_BOOL deprecated_used = OPJ_FALSE; + switch (parameters->cp_cinema) { + case OPJ_CINEMA2K_24: + parameters->rsiz = OPJ_PROFILE_CINEMA_2K; + parameters->max_cs_size = OPJ_CINEMA_24_CS; + parameters->max_comp_size = OPJ_CINEMA_24_COMP; + deprecated_used = OPJ_TRUE; + break; + case OPJ_CINEMA2K_48: + parameters->rsiz = OPJ_PROFILE_CINEMA_2K; + parameters->max_cs_size = OPJ_CINEMA_48_CS; + parameters->max_comp_size = OPJ_CINEMA_48_COMP; + deprecated_used = OPJ_TRUE; + break; + case OPJ_CINEMA4K_24: + parameters->rsiz = OPJ_PROFILE_CINEMA_4K; + parameters->max_cs_size = OPJ_CINEMA_24_CS; + parameters->max_comp_size = OPJ_CINEMA_24_COMP; + deprecated_used = OPJ_TRUE; + break; + case OPJ_OFF: + default: + break; + } + switch (parameters->cp_rsiz) { + case OPJ_CINEMA2K: + parameters->rsiz = OPJ_PROFILE_CINEMA_2K; + deprecated_used = OPJ_TRUE; + break; + case OPJ_CINEMA4K: + parameters->rsiz = OPJ_PROFILE_CINEMA_4K; + deprecated_used = OPJ_TRUE; + break; + case OPJ_MCT: + parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT; + deprecated_used = OPJ_TRUE; + case OPJ_STD_RSIZ: + default: + break; + } + if (deprecated_used) { + opj_event_msg(p_manager, EVT_WARNING, + "Deprecated fields cp_cinema or cp_rsiz are used\n" + "Please consider using only the rsiz field\n" + "See openjpeg.h documentation for more details\n"); + } + } + + /* If no explicit layers are provided, use lossless settings */ + if (parameters->tcp_numlayers == 0) { + parameters->tcp_numlayers = 1; + parameters->cp_disto_alloc = 1; + parameters->tcp_rates[0] = 0; + } + + if (parameters->cp_disto_alloc) { + /* Emit warnings if tcp_rates are not decreasing */ + for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { + OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i]; + OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1]; + if (rate_i_corr <= 1.0) { + rate_i_corr = 1.0; + } + if (rate_i_m_1_corr <= 1.0) { + rate_i_m_1_corr = 1.0; + } + if (rate_i_corr >= rate_i_m_1_corr) { + if (rate_i_corr != parameters->tcp_rates[i] && + rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { + opj_event_msg(p_manager, EVT_WARNING, + "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " + "than tcp_rates[%d]=%f (corrected as %f)\n", + i, parameters->tcp_rates[i], rate_i_corr, + i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); + } else if (rate_i_corr != parameters->tcp_rates[i]) { + opj_event_msg(p_manager, EVT_WARNING, + "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser " + "than tcp_rates[%d]=%f\n", + i, parameters->tcp_rates[i], rate_i_corr, + i - 1, parameters->tcp_rates[i - 1]); + } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) { + opj_event_msg(p_manager, EVT_WARNING, + "tcp_rates[%d]=%f should be strictly lesser " + "than tcp_rates[%d]=%f (corrected as %f)\n", + i, parameters->tcp_rates[i], + i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr); + } else { + opj_event_msg(p_manager, EVT_WARNING, + "tcp_rates[%d]=%f should be strictly lesser " + "than tcp_rates[%d]=%f\n", + i, parameters->tcp_rates[i], + i - 1, parameters->tcp_rates[i - 1]); + } + } + } + } else if (parameters->cp_fixed_quality) { + /* Emit warnings if tcp_distoratio are not increasing */ + for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { + if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] && + !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 && + parameters->tcp_distoratio[i] == 0)) { + opj_event_msg(p_manager, EVT_WARNING, + "tcp_distoratio[%d]=%f should be strictly greater " + "than tcp_distoratio[%d]=%f\n", + i, parameters->tcp_distoratio[i], i - 1, + parameters->tcp_distoratio[i - 1]); + } + } + } + + /* see if max_codestream_size does limit input rate */ + if (parameters->max_cs_size <= 0) { + if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) { + OPJ_FLOAT32 temp_size; + temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * + image->comps[0].h * image->comps[0].prec) / + ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 * + image->comps[0].dx * image->comps[0].dy)); + if (temp_size > INT_MAX) { + parameters->max_cs_size = INT_MAX; + } else { + parameters->max_cs_size = (int) floor(temp_size); + } + } else { + parameters->max_cs_size = 0; + } + } else { + OPJ_FLOAT32 temp_rate; + OPJ_BOOL cap = OPJ_FALSE; + + if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 && + parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) { + parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w * + image->comps[0].h * image->comps[0].prec) / + (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * + image->comps[0].dy); + } + + temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w * + image->comps[0].h * image->comps[0].prec) / + (((double)parameters->max_cs_size) * 8 * image->comps[0].dx * + image->comps[0].dy)); + for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) { + if (parameters->tcp_rates[i] < temp_rate) { + parameters->tcp_rates[i] = temp_rate; + cap = OPJ_TRUE; + } + } + if (cap) { + opj_event_msg(p_manager, EVT_WARNING, + "The desired maximum codestream size has limited\n" + "at least one of the desired quality layers\n"); + } + } + + /* Manage profiles and applications and set RSIZ */ + /* set cinema parameters if required */ + if (OPJ_IS_CINEMA(parameters->rsiz)) { + if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) + || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n"); + parameters->rsiz = OPJ_PROFILE_NONE; + } else { + opj_j2k_set_cinema_parameters(parameters, image, p_manager); + if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) { + parameters->rsiz = OPJ_PROFILE_NONE; + } + } + } else if (OPJ_IS_STORAGE(parameters->rsiz)) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Long Term Storage profile not yet supported\n"); + parameters->rsiz = OPJ_PROFILE_NONE; + } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Broadcast profiles not yet supported\n"); + parameters->rsiz = OPJ_PROFILE_NONE; + } else if (OPJ_IS_IMF(parameters->rsiz)) { + opj_j2k_set_imf_parameters(parameters, image, p_manager); + if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) { + parameters->rsiz = OPJ_PROFILE_NONE; + } + } else if (OPJ_IS_PART2(parameters->rsiz)) { + if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG 2000 Part-2 profile defined\n" + "but no Part-2 extension enabled.\n" + "Profile set to NONE.\n"); + parameters->rsiz = OPJ_PROFILE_NONE; + } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { + opj_event_msg(p_manager, EVT_WARNING, + "Unsupported Part-2 extension enabled\n" + "Profile set to NONE.\n"); + parameters->rsiz = OPJ_PROFILE_NONE; + } + } + + /* + copy user encoding parameters + */ + cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32) + parameters->max_comp_size; + cp->rsiz = parameters->rsiz; + cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32) + parameters->cp_disto_alloc & 1u; + cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32) + parameters->cp_fixed_alloc & 1u; + cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32) + parameters->cp_fixed_quality & 1u; + + /* mod fixed_quality */ + if (parameters->cp_fixed_alloc && parameters->cp_matrice) { + size_t array_size = (size_t)parameters->tcp_numlayers * + (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); + cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); + if (!cp->m_specific_param.m_enc.m_matrice) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate copy of user encoding parameters matrix \n"); + return OPJ_FALSE; + } + memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, + array_size); + } + + /* tiles */ + cp->tdx = (OPJ_UINT32)parameters->cp_tdx; + cp->tdy = (OPJ_UINT32)parameters->cp_tdy; + + /* tile offset */ + cp->tx0 = (OPJ_UINT32)parameters->cp_tx0; + cp->ty0 = (OPJ_UINT32)parameters->cp_ty0; + + /* comment string */ + if (parameters->cp_comment) { + cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); + if (!cp->comment) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate copy of comment string\n"); + return OPJ_FALSE; + } + strcpy(cp->comment, parameters->cp_comment); + } else { + /* Create default comment for codestream */ + const char comment[] = "Created by OpenJPEG version "; + const size_t clen = strlen(comment); + const char *version = opj_version(); + + /* UniPG>> */ +#ifdef USE_JPWL + cp->comment = (char*)opj_malloc(clen + strlen(version) + 11); + if (!cp->comment) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate comment string\n"); + return OPJ_FALSE; + } + sprintf(cp->comment, "%s%s with JPWL", comment, version); +#else + cp->comment = (char*)opj_malloc(clen + strlen(version) + 1); + if (!cp->comment) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate comment string\n"); + return OPJ_FALSE; + } + sprintf(cp->comment, "%s%s", comment, version); +#endif + /* <tile_size_on) { + if (cp->tdx == 0) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n"); + return OPJ_FALSE; + } + if (cp->tdy == 0) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n"); + return OPJ_FALSE; + } + cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), + (OPJ_INT32)cp->tdx); + cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), + (OPJ_INT32)cp->tdy); + } else { + cp->tdx = image->x1 - cp->tx0; + cp->tdy = image->y1 - cp->ty0; + } + + if (parameters->tp_on) { + cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag; + cp->m_specific_param.m_enc.m_tp_on = 1; + } + +#ifdef USE_JPWL + /* + calculate JPWL encoding parameters + */ + + if (parameters->jpwl_epc_on) { + OPJ_INT32 i; + + /* set JPWL on */ + cp->epc_on = OPJ_TRUE; + cp->info_on = OPJ_FALSE; /* no informative technique */ + + /* set EPB on */ + if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { + cp->epb_on = OPJ_TRUE; + + cp->hprot_MH = parameters->jpwl_hprot_MH; + for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { + cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; + cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; + } + /* if tile specs are not specified, copy MH specs */ + if (cp->hprot_TPH[0] == -1) { + cp->hprot_TPH_tileno[0] = 0; + cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; + } + for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { + cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; + cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; + cp->pprot[i] = parameters->jpwl_pprot[i]; + } + } + + /* set ESD writing */ + if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { + cp->esd_on = OPJ_TRUE; + + cp->sens_size = parameters->jpwl_sens_size; + cp->sens_addr = parameters->jpwl_sens_addr; + cp->sens_range = parameters->jpwl_sens_range; + + cp->sens_MH = parameters->jpwl_sens_MH; + for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { + cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; + cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; + } + } + + /* always set RED writing to false: we are at the encoder */ + cp->red_on = OPJ_FALSE; + + } else { + cp->epc_on = OPJ_FALSE; + } +#endif /* USE_JPWL */ + + /* initialize the mutiple tiles */ + /* ---------------------------- */ + cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); + if (!cp->tcps) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate tile coding parameters\n"); + return OPJ_FALSE; + } + + for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + opj_tcp_t *tcp = &cp->tcps[tileno]; + tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers; + + for (j = 0; j < tcp->numlayers; j++) { + if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) { + if (cp->m_specific_param.m_enc.m_fixed_quality) { + tcp->distoratio[j] = parameters->tcp_distoratio[j]; + } + tcp->rates[j] = parameters->tcp_rates[j]; + } else { + if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */ + tcp->distoratio[j] = parameters->tcp_distoratio[j]; + } else { + tcp->rates[j] = parameters->tcp_rates[j]; + } + } + if (!cp->m_specific_param.m_enc.m_fixed_quality && + tcp->rates[j] <= 1.0) { + tcp->rates[j] = 0.0; /* force lossless */ + } + } + + tcp->csty = (OPJ_UINT32)parameters->csty; + tcp->prg = parameters->prog_order; + tcp->mct = (OPJ_UINT32)parameters->tcp_mct; + + numpocs_tile = 0; + tcp->POC = 0; + + if (parameters->numpocs) { + /* initialisation of POC */ + for (i = 0; i < parameters->numpocs; i++) { + if (tileno + 1 == parameters->POC[i].tile) { + opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; + + if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid compno0 for POC %d\n", i); + return OPJ_FALSE; + } + + tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; + tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; + tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; + tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; + tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1, + image->numcomps); + tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; + tcp_poc->tile = parameters->POC[numpocs_tile].tile; + + numpocs_tile++; + } + } + + if (numpocs_tile) { + + /* TODO MSD use the return value*/ + opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs, + (OPJ_UINT32)parameters->numresolution, image->numcomps, + (OPJ_UINT32)parameters->tcp_numlayers, p_manager); + + tcp->POC = 1; + tcp->numpocs = numpocs_tile - 1 ; + } + } else { + tcp->numpocs = 0; + } + + tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); + if (!tcp->tccps) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate tile component coding parameters\n"); + return OPJ_FALSE; + } + if (parameters->mct_data) { + + OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof( + OPJ_FLOAT32); + OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); + OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data + + lMctSize); + + if (!lTmpBuf) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate temp buffer\n"); + return OPJ_FALSE; + } + + tcp->mct = 2; + tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); + if (! tcp->m_mct_coding_matrix) { + opj_free(lTmpBuf); + lTmpBuf = NULL; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate encoder MCT coding matrix \n"); + return OPJ_FALSE; + } + memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize); + memcpy(lTmpBuf, parameters->mct_data, lMctSize); + + tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); + if (! tcp->m_mct_decoding_matrix) { + opj_free(lTmpBuf); + lTmpBuf = NULL; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate encoder MCT decoding matrix \n"); + return OPJ_FALSE; + } + if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix), + image->numcomps) == OPJ_FALSE) { + opj_free(lTmpBuf); + lTmpBuf = NULL; + opj_event_msg(p_manager, EVT_ERROR, + "Failed to inverse encoder MCT decoding matrix \n"); + return OPJ_FALSE; + } + + tcp->mct_norms = (OPJ_FLOAT64*) + opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); + if (! tcp->mct_norms) { + opj_free(lTmpBuf); + lTmpBuf = NULL; + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to allocate encoder MCT norms \n"); + return OPJ_FALSE; + } + opj_calculate_norms(tcp->mct_norms, image->numcomps, + tcp->m_mct_decoding_matrix); + opj_free(lTmpBuf); + + for (i = 0; i < image->numcomps; i++) { + opj_tccp_t *tccp = &tcp->tccps[i]; + tccp->m_dc_level_shift = l_dc_shift[i]; + } + + if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) { + /* free will be handled by opj_j2k_destroy */ + opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n"); + return OPJ_FALSE; + } + } else { + if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */ + if ((image->comps[0].dx != image->comps[1].dx) || + (image->comps[0].dx != image->comps[2].dx) || + (image->comps[0].dy != image->comps[1].dy) || + (image->comps[0].dy != image->comps[2].dy)) { + opj_event_msg(p_manager, EVT_WARNING, + "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); + tcp->mct = 0; + } + } + for (i = 0; i < image->numcomps; i++) { + opj_tccp_t *tccp = &tcp->tccps[i]; + opj_image_comp_t * l_comp = &(image->comps[i]); + + if (! l_comp->sgnd) { + tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); + } + } + } + + for (i = 0; i < image->numcomps; i++) { + opj_tccp_t *tccp = &tcp->tccps[i]; + + tccp->csty = parameters->csty & + 0x01; /* 0 => one precinct || 1 => custom precinct */ + tccp->numresolutions = (OPJ_UINT32)parameters->numresolution; + tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init); + tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init); + tccp->cblksty = (OPJ_UINT32)parameters->mode; + tccp->qmfbid = parameters->irreversible ? 0 : 1; + tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : + J2K_CCP_QNTSTY_NOQNT; + tccp->numgbits = 2; + + if ((OPJ_INT32)i == parameters->roi_compno) { + tccp->roishift = parameters->roi_shift; + } else { + tccp->roishift = 0; + } + + if (parameters->csty & J2K_CCP_CSTY_PRT) { + OPJ_INT32 p = 0, it_res; + assert(tccp->numresolutions > 0); + for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) { + if (p < parameters->res_spec) { + + if (parameters->prcw_init[p] < 1) { + tccp->prcw[it_res] = 1; + } else { + tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]); + } + + if (parameters->prch_init[p] < 1) { + tccp->prch[it_res] = 1; + } else { + tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]); + } + + } else { + OPJ_INT32 res_spec = parameters->res_spec; + OPJ_INT32 size_prcw = 0; + OPJ_INT32 size_prch = 0; + + assert(res_spec > 0); /* issue 189 */ + size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); + size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); + + + if (size_prcw < 1) { + tccp->prcw[it_res] = 1; + } else { + tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw); + } + + if (size_prch < 1) { + tccp->prch[it_res] = 1; + } else { + tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch); + } + } + p++; + /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ + } /*end for*/ + } else { + for (j = 0; j < tccp->numresolutions; j++) { + tccp->prcw[j] = 15; + tccp->prch[j] = 15; + } + } + + opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); + } + } + + if (parameters->mct_data) { + opj_free(parameters->mct_data); + parameters->mct_data = 00; + } + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, + OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) +{ + assert(cstr_index != 00); + + /* expand the list? */ + if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { + opj_marker_info_t *new_marker; + cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) + cstr_index->maxmarknum); + new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, + cstr_index->maxmarknum * sizeof(opj_marker_info_t)); + if (! new_marker) { + opj_free(cstr_index->marker); + cstr_index->marker = NULL; + cstr_index->maxmarknum = 0; + cstr_index->marknum = 0; + /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ + return OPJ_FALSE; + } + cstr_index->marker = new_marker; + } + + /* add the marker */ + cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; + cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; + cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; + cstr_index->marknum++; + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, + opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, + OPJ_UINT32 len) +{ + assert(cstr_index != 00); + assert(cstr_index->tile_index != 00); + + /* expand the list? */ + if ((cstr_index->tile_index[tileno].marknum + 1) > + cstr_index->tile_index[tileno].maxmarknum) { + opj_marker_info_t *new_marker; + cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); + new_marker = (opj_marker_info_t *) opj_realloc( + cstr_index->tile_index[tileno].marker, + cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t)); + if (! new_marker) { + opj_free(cstr_index->tile_index[tileno].marker); + cstr_index->tile_index[tileno].marker = NULL; + cstr_index->tile_index[tileno].maxmarknum = 0; + cstr_index->tile_index[tileno].marknum = 0; + /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ + return OPJ_FALSE; + } + cstr_index->tile_index[tileno].marker = new_marker; + } + + /* add the marker */ + cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type + = (OPJ_UINT16)type; + cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos + = (OPJ_INT32)pos; + cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len + = (OPJ_INT32)len; + cstr_index->tile_index[tileno].marknum++; + + if (type == J2K_MS_SOT) { + OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; + + if (cstr_index->tile_index[tileno].tp_index) { + cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; + } + + } + return OPJ_TRUE; +} + +/* + * ----------------------------------------------------------------------- + * ----------------------------------------------------------------------- + * ----------------------------------------------------------------------- + */ + +OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + (void)p_j2k; + (void)p_stream; + (void)p_manager; + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream, + opj_j2k_t* p_j2k, + opj_image_t** p_image, + opj_event_mgr_t* p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + /* create an empty image header */ + p_j2k->m_private_image = opj_image_create0(); + if (! p_j2k->m_private_image) { + return OPJ_FALSE; + } + + /* customization of the validation */ + if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + /* validation of the parameters codec */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + /* customization of the encoding */ + if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + /* read header */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + *p_image = opj_image_create0(); + if (!(*p_image)) { + return OPJ_FALSE; + } + + /* Copy codestream image information to the output image */ + opj_copy_image_header(p_j2k->m_private_image, *p_image); + + /*Allocate and initialize some elements of codestrem index*/ + if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { + opj_image_destroy(*p_image); + *p_image = NULL; + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_read_header_procedure, p_manager)) { + return OPJ_FALSE; + } + + /* DEVELOPER CORNER, add your custom procedures */ + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, + (opj_procedure)opj_j2k_build_decoder, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, + (opj_procedure)opj_j2k_decoding_validation, p_manager)) { + return OPJ_FALSE; + } + + /* DEVELOPER CORNER, add your custom validation procedure */ + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_BOOL l_is_valid = OPJ_TRUE; + OPJ_UINT32 i, j; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_stream); + OPJ_UNUSED(p_manager); + + if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { + OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; + + for (i = 0; i < l_nb_tiles; ++i) { + if (l_tcp->mct == 2) { + opj_tccp_t * l_tccp = l_tcp->tccps; + l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); + + for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) { + l_is_valid &= !(l_tccp->qmfbid & 1); + ++l_tccp; + } + } + ++l_tcp; + } + } + + return l_is_valid; +} + +OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_indix = 1; + opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00; + opj_simple_mcc_decorrelation_data_t * l_mcc_data; + OPJ_UINT32 l_mct_size, l_nb_elem; + OPJ_FLOAT32 * l_data, * l_current_data; + opj_tccp_t * l_tccp; + + /* preconditions */ + assert(p_tcp != 00); + + if (p_tcp->mct != 2) { + return OPJ_TRUE; + } + + if (p_tcp->m_mct_decoding_matrix) { + if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { + opj_mct_data_t *new_mct_records; + p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; + + new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, + p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if (! new_mct_records) { + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = NULL; + p_tcp->m_nb_max_mct_records = 0; + p_tcp->m_nb_mct_records = 0; + /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + return OPJ_FALSE; + } + p_tcp->m_mct_records = new_mct_records; + l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + memset(l_mct_deco_data, 0, + (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( + opj_mct_data_t)); + } + l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + if (l_mct_deco_data->m_data) { + opj_free(l_mct_deco_data->m_data); + l_mct_deco_data->m_data = 00; + } + + l_mct_deco_data->m_index = l_indix++; + l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; + l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; + l_nb_elem = p_image->numcomps * p_image->numcomps; + l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; + l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); + + if (! l_mct_deco_data->m_data) { + return OPJ_FALSE; + } + + j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type]( + p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem); + + l_mct_deco_data->m_data_size = l_mct_size; + ++p_tcp->m_nb_mct_records; + } + + if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { + opj_mct_data_t *new_mct_records; + p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; + new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, + p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if (! new_mct_records) { + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = NULL; + p_tcp->m_nb_max_mct_records = 0; + p_tcp->m_nb_mct_records = 0; + /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + return OPJ_FALSE; + } + p_tcp->m_mct_records = new_mct_records; + l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + memset(l_mct_offset_data, 0, + (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof( + opj_mct_data_t)); + + if (l_mct_deco_data) { + l_mct_deco_data = l_mct_offset_data - 1; + } + } + + l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + if (l_mct_offset_data->m_data) { + opj_free(l_mct_offset_data->m_data); + l_mct_offset_data->m_data = 00; + } + + l_mct_offset_data->m_index = l_indix++; + l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; + l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; + l_nb_elem = p_image->numcomps; + l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; + l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size); + + if (! l_mct_offset_data->m_data) { + return OPJ_FALSE; + } + + l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); + if (! l_data) { + opj_free(l_mct_offset_data->m_data); + l_mct_offset_data->m_data = 00; + return OPJ_FALSE; + } + + l_tccp = p_tcp->tccps; + l_current_data = l_data; + + for (i = 0; i < l_nb_elem; ++i) { + *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift); + ++l_tccp; + } + + j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data, + l_mct_offset_data->m_data, l_nb_elem); + + opj_free(l_data); + + l_mct_offset_data->m_data_size = l_mct_size; + + ++p_tcp->m_nb_mct_records; + + if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { + opj_simple_mcc_decorrelation_data_t *new_mcc_records; + p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; + new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( + p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof( + opj_simple_mcc_decorrelation_data_t)); + if (! new_mcc_records) { + opj_free(p_tcp->m_mcc_records); + p_tcp->m_mcc_records = NULL; + p_tcp->m_nb_max_mcc_records = 0; + p_tcp->m_nb_mcc_records = 0; + /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + return OPJ_FALSE; + } + p_tcp->m_mcc_records = new_mcc_records; + l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; + memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * + sizeof(opj_simple_mcc_decorrelation_data_t)); + + } + + l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; + l_mcc_data->m_decorrelation_array = l_mct_deco_data; + l_mcc_data->m_is_irreversible = 1; + l_mcc_data->m_nb_comps = p_image->numcomps; + l_mcc_data->m_index = l_indix++; + l_mcc_data->m_offset_array = l_mct_offset_data; + ++p_tcp->m_nb_mcc_records; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + /* add here initialization of cp + copy paste of setup_decoder */ + (void)p_j2k; + (void)p_stream; + (void)p_manager; + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + /* add here initialization of cp + copy paste of setup_encoder */ + (void)p_j2k; + (void)p_stream; + (void)p_manager; + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_BOOL l_is_valid = OPJ_TRUE; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_stream); + + /* STATE checking */ + /* make sure the state is at 0 */ + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); + + /* POINTER validation */ + /* make sure a p_j2k codec is present */ + l_is_valid &= (p_j2k->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (p_j2k->m_validation_list != 00); + + /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */ + /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ + /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ + if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || + (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { + opj_event_msg(p_manager, EVT_ERROR, + "Number of resolutions is too high in comparison to the size of tiles\n"); + return OPJ_FALSE; + } + + if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 << + (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { + opj_event_msg(p_manager, EVT_ERROR, + "Number of resolutions is too high in comparison to the size of tiles\n"); + return OPJ_FALSE; + } + + if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 << + (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { + opj_event_msg(p_manager, EVT_ERROR, + "Number of resolutions is too high in comparison to the size of tiles\n"); + return OPJ_FALSE; + } + + /* PARAMETER VALIDATION */ + return l_is_valid; +} + +static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_BOOL l_is_valid = OPJ_TRUE; + + /* preconditions*/ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_stream); + OPJ_UNUSED(p_manager); + + /* STATE checking */ + /* make sure the state is at 0 */ +#ifdef TODO_MSD + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); +#endif + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); + + /* POINTER validation */ + /* make sure a p_j2k codec is present */ + /* make sure a procedure list is present */ + l_is_valid &= (p_j2k->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (p_j2k->m_validation_list != 00); + + /* PARAMETER VALIDATION */ + return l_is_valid; +} + +static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 l_current_marker; + OPJ_UINT32 l_marker_size; + const opj_dec_memory_marker_handler_t * l_marker_handler = 00; + OPJ_BOOL l_has_siz = 0; + OPJ_BOOL l_has_cod = 0; + OPJ_BOOL l_has_qcd = 0; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* We enter in the main header */ + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; + + /* Try to read the SOC marker, the codestream must begin with SOC marker */ + if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n"); + return OPJ_FALSE; + } + + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, + &l_current_marker, 2); + + /* Try to read until the SOT is detected */ + while (l_current_marker != J2K_MS_SOT) { + + /* Check if the current marker ID is valid */ + if (l_current_marker < 0xff00) { + opj_event_msg(p_manager, EVT_ERROR, + "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); + return OPJ_FALSE; + } + + /* Get the marker handler from the marker ID */ + l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); + + /* Manage case where marker is unknown */ + if (l_marker_handler->id == J2K_MS_UNK) { + if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Unknow marker have been detected and generated error.\n"); + return OPJ_FALSE; + } + + if (l_current_marker == J2K_MS_SOT) { + break; /* SOT marker is detected main header is completely read */ + } else { /* Get the marker handler from the marker ID */ + l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); + } + } + + if (l_marker_handler->id == J2K_MS_SIZ) { + /* Mark required SIZ marker as found */ + l_has_siz = 1; + } + if (l_marker_handler->id == J2K_MS_COD) { + /* Mark required COD marker as found */ + l_has_cod = 1; + } + if (l_marker_handler->id == J2K_MS_QCD) { + /* Mark required QCD marker as found */ + l_has_qcd = 1; + } + + /* Check if the marker is known and if it is the right place to find it */ + if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { + opj_event_msg(p_manager, EVT_ERROR, + "Marker is not compliant with its position\n"); + return OPJ_FALSE; + } + + /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* read 2 bytes as the marker size */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, + 2); + if (l_marker_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n"); + return OPJ_FALSE; + } + l_marker_size -= 2; /* Subtract the size of the marker ID already read */ + + /* Check if the marker size is compatible with the header data size */ + if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { + OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); + if (! new_header_data) { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = NULL; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; + p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; + } + + /* Try to read the rest of the marker segment from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, + p_manager) != l_marker_size) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read the marker segment with the correct marker handler */ + if (!(*(l_marker_handler->handler))(p_j2k, + p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Marker handler function failed to read the marker segment\n"); + return OPJ_FALSE; + } + + /* Add the marker to the codestream index*/ + if (OPJ_FALSE == opj_j2k_add_mhmarker( + p_j2k->cstr_index, + l_marker_handler->id, + (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, + l_marker_size + 4)) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + return OPJ_FALSE; + } + + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* read 2 bytes as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, + &l_current_marker, 2); + } + + if (l_has_siz == 0) { + opj_event_msg(p_manager, EVT_ERROR, + "required SIZ marker not found in main header\n"); + return OPJ_FALSE; + } + if (l_has_cod == 0) { + opj_event_msg(p_manager, EVT_ERROR, + "required COD marker not found in main header\n"); + return OPJ_FALSE; + } + if (l_has_qcd == 0) { + opj_event_msg(p_manager, EVT_ERROR, + "required QCD marker not found in main header\n"); + return OPJ_FALSE; + } + + if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n"); + return OPJ_FALSE; + } + + opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); + + /* Position of the last element if the main header */ + p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; + + /* Next step: read a tile-part header */ + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *, + opj_event_mgr_t *) = 00; + OPJ_BOOL l_result = OPJ_TRUE; + OPJ_UINT32 l_nb_proc, i; + + /* preconditions*/ + assert(p_procedure_list != 00); + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); + l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *, + opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + + for (i = 0; i < l_nb_proc; ++i) { + l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager)); + ++l_procedure; + } + + /* and clear the procedure list at the end.*/ + opj_procedure_list_clear(p_procedure_list); + return l_result; +} + +/* FIXME DOC*/ +static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + opj_tcp_t * l_tcp = 00; + opj_tcp_t * l_default_tcp = 00; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 i, j; + opj_tccp_t *l_current_tccp = 00; + OPJ_UINT32 l_tccp_size; + OPJ_UINT32 l_mct_size; + opj_image_t * l_image; + OPJ_UINT32 l_mcc_records_size, l_mct_records_size; + opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; + opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; + OPJ_UINT32 l_offset; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_stream); + + l_image = p_j2k->m_private_image; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t); + l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof( + OPJ_FLOAT32); + + /* For each tile */ + for (i = 0; i < l_nb_tiles; ++i) { + /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/ + l_current_tccp = l_tcp->tccps; + /*Copy default coding parameters into the current tile coding parameters*/ + memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t)); + /* Initialize some values of the current tile coding parameters*/ + l_tcp->cod = 0; + l_tcp->ppt = 0; + l_tcp->ppt_data = 00; + l_tcp->m_current_tile_part_number = -1; + /* Remove memory not owned by this tile in case of early error return. */ + l_tcp->m_mct_decoding_matrix = 00; + l_tcp->m_nb_max_mct_records = 0; + l_tcp->m_mct_records = 00; + l_tcp->m_nb_max_mcc_records = 0; + l_tcp->m_mcc_records = 00; + /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ + l_tcp->tccps = l_current_tccp; + + /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ + if (l_default_tcp->m_mct_decoding_matrix) { + l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); + if (! l_tcp->m_mct_decoding_matrix) { + return OPJ_FALSE; + } + memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix, + l_mct_size); + } + + /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ + l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof( + opj_mct_data_t); + l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); + if (! l_tcp->m_mct_records) { + return OPJ_FALSE; + } + memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size); + + /* Copy the mct record data from dflt_tile_cp to the current tile*/ + l_src_mct_rec = l_default_tcp->m_mct_records; + l_dest_mct_rec = l_tcp->m_mct_records; + + for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) { + + if (l_src_mct_rec->m_data) { + + l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); + if (! l_dest_mct_rec->m_data) { + return OPJ_FALSE; + } + memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data, + l_src_mct_rec->m_data_size); + } + + ++l_src_mct_rec; + ++l_dest_mct_rec; + /* Update with each pass to free exactly what has been allocated on early return. */ + l_tcp->m_nb_max_mct_records += 1; + } + + /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ + l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof( + opj_simple_mcc_decorrelation_data_t); + l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc( + l_mcc_records_size); + if (! l_tcp->m_mcc_records) { + return OPJ_FALSE; + } + memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size); + l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records; + + /* Copy the mcc record data from dflt_tile_cp to the current tile*/ + l_src_mcc_rec = l_default_tcp->m_mcc_records; + l_dest_mcc_rec = l_tcp->m_mcc_records; + + for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) { + + if (l_src_mcc_rec->m_decorrelation_array) { + l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - + l_default_tcp->m_mct_records); + l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; + } + + if (l_src_mcc_rec->m_offset_array) { + l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - + l_default_tcp->m_mct_records); + l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; + } + + ++l_src_mcc_rec; + ++l_dest_mcc_rec; + } + + /* Copy all the dflt_tile_compo_cp to the current tile cp */ + memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size); + + /* Move to next tile cp*/ + ++l_tcp; + } + + /* Create the current tile decoder*/ + p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE); + if (! p_j2k->m_tcd) { + return OPJ_FALSE; + } + + if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) { + opj_tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler( + OPJ_UINT32 p_id) +{ + const opj_dec_memory_marker_handler_t *e; + for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { + if (e->id == p_id) { + break; /* we find a handler corresponding to the marker ID*/ + } + } + return e; +} + +void opj_j2k_destroy(opj_j2k_t *p_j2k) +{ + if (p_j2k == 00) { + return; + } + + if (p_j2k->m_is_decoder) { + + if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { + opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); + opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); + p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; + } + + if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = 00; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; + } + + opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); + p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00; + p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; + + } else { + + if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; + } + + if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { + opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; + } + + if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + } + } + + opj_tcd_destroy(p_j2k->m_tcd); + + opj_j2k_cp_destroy(&(p_j2k->m_cp)); + memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t)); + + opj_procedure_list_destroy(p_j2k->m_procedure_list); + p_j2k->m_procedure_list = 00; + + opj_procedure_list_destroy(p_j2k->m_validation_list); + p_j2k->m_procedure_list = 00; + + j2k_destroy_cstr_index(p_j2k->cstr_index); + p_j2k->cstr_index = NULL; + + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + + opj_image_destroy(p_j2k->m_output_image); + p_j2k->m_output_image = NULL; + + opj_thread_pool_destroy(p_j2k->m_tp); + p_j2k->m_tp = NULL; + + opj_free(p_j2k); +} + +void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind) +{ + if (p_cstr_ind) { + + if (p_cstr_ind->marker) { + opj_free(p_cstr_ind->marker); + p_cstr_ind->marker = NULL; + } + + if (p_cstr_ind->tile_index) { + OPJ_UINT32 it_tile = 0; + + for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { + + if (p_cstr_ind->tile_index[it_tile].packet_index) { + opj_free(p_cstr_ind->tile_index[it_tile].packet_index); + p_cstr_ind->tile_index[it_tile].packet_index = NULL; + } + + if (p_cstr_ind->tile_index[it_tile].tp_index) { + opj_free(p_cstr_ind->tile_index[it_tile].tp_index); + p_cstr_ind->tile_index[it_tile].tp_index = NULL; + } + + if (p_cstr_ind->tile_index[it_tile].marker) { + opj_free(p_cstr_ind->tile_index[it_tile].marker); + p_cstr_ind->tile_index[it_tile].marker = NULL; + + } + } + + opj_free(p_cstr_ind->tile_index); + p_cstr_ind->tile_index = NULL; + } + + opj_free(p_cstr_ind); + } +} + +static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp) +{ + if (p_tcp == 00) { + return; + } + + if (p_tcp->ppt_markers != 00) { + OPJ_UINT32 i; + for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { + if (p_tcp->ppt_markers[i].m_data != NULL) { + opj_free(p_tcp->ppt_markers[i].m_data); + } + } + p_tcp->ppt_markers_count = 0U; + opj_free(p_tcp->ppt_markers); + p_tcp->ppt_markers = NULL; + } + + if (p_tcp->ppt_buffer != 00) { + opj_free(p_tcp->ppt_buffer); + p_tcp->ppt_buffer = 00; + } + + if (p_tcp->tccps != 00) { + opj_free(p_tcp->tccps); + p_tcp->tccps = 00; + } + + if (p_tcp->m_mct_coding_matrix != 00) { + opj_free(p_tcp->m_mct_coding_matrix); + p_tcp->m_mct_coding_matrix = 00; + } + + if (p_tcp->m_mct_decoding_matrix != 00) { + opj_free(p_tcp->m_mct_decoding_matrix); + p_tcp->m_mct_decoding_matrix = 00; + } + + if (p_tcp->m_mcc_records) { + opj_free(p_tcp->m_mcc_records); + p_tcp->m_mcc_records = 00; + p_tcp->m_nb_max_mcc_records = 0; + p_tcp->m_nb_mcc_records = 0; + } + + if (p_tcp->m_mct_records) { + opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; + OPJ_UINT32 i; + + for (i = 0; i < p_tcp->m_nb_mct_records; ++i) { + if (l_mct_data->m_data) { + opj_free(l_mct_data->m_data); + l_mct_data->m_data = 00; + } + + ++l_mct_data; + } + + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = 00; + } + + if (p_tcp->mct_norms != 00) { + opj_free(p_tcp->mct_norms); + p_tcp->mct_norms = 00; + } + + opj_j2k_tcp_data_destroy(p_tcp); + +} + +static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp) +{ + if (p_tcp->m_data) { + opj_free(p_tcp->m_data); + p_tcp->m_data = NULL; + p_tcp->m_data_size = 0; + } +} + +static void opj_j2k_cp_destroy(opj_cp_t *p_cp) +{ + OPJ_UINT32 l_nb_tiles; + opj_tcp_t * l_current_tile = 00; + + if (p_cp == 00) { + return; + } + if (p_cp->tcps != 00) { + OPJ_UINT32 i; + l_current_tile = p_cp->tcps; + l_nb_tiles = p_cp->th * p_cp->tw; + + for (i = 0U; i < l_nb_tiles; ++i) { + opj_j2k_tcp_destroy(l_current_tile); + ++l_current_tile; + } + opj_free(p_cp->tcps); + p_cp->tcps = 00; + } + if (p_cp->ppm_markers != 00) { + OPJ_UINT32 i; + for (i = 0U; i < p_cp->ppm_markers_count; ++i) { + if (p_cp->ppm_markers[i].m_data != NULL) { + opj_free(p_cp->ppm_markers[i].m_data); + } + } + p_cp->ppm_markers_count = 0U; + opj_free(p_cp->ppm_markers); + p_cp->ppm_markers = NULL; + } + opj_free(p_cp->ppm_buffer); + p_cp->ppm_buffer = 00; + p_cp->ppm_data = + NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ + opj_free(p_cp->comment); + p_cp->comment = 00; + if (! p_cp->m_is_decoder) { + opj_free(p_cp->m_specific_param.m_enc.m_matrice); + p_cp->m_specific_param.m_enc.m_matrice = 00; + } +} + +static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t + *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed, + opj_event_mgr_t * p_manager) +{ + OPJ_BYTE l_header_data[10]; + OPJ_OFF_T l_stream_pos_backup; + OPJ_UINT32 l_current_marker; + OPJ_UINT32 l_marker_size; + OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts; + + /* initialize to no correction needed */ + *p_correction_needed = OPJ_FALSE; + + if (!opj_stream_has_seek(p_stream)) { + /* We can't do much in this case, seek is needed */ + return OPJ_TRUE; + } + + l_stream_pos_backup = opj_stream_tell(p_stream); + if (l_stream_pos_backup == -1) { + /* let's do nothing */ + return OPJ_TRUE; + } + + for (;;) { + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { + /* assume all is OK */ + if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { + return OPJ_FALSE; + } + return OPJ_TRUE; + } + + /* Read 2 bytes from buffer as the new marker ID */ + opj_read_bytes(l_header_data, &l_current_marker, 2); + + if (l_current_marker != J2K_MS_SOT) { + /* assume all is OK */ + if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { + return OPJ_FALSE; + } + return OPJ_TRUE; + } + + /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes from the buffer as the marker size */ + opj_read_bytes(l_header_data, &l_marker_size, 2); + + /* Check marker size for SOT Marker */ + if (l_marker_size != 10) { + opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); + return OPJ_FALSE; + } + l_marker_size -= 2; + + if (opj_stream_read_data(p_stream, l_header_data, l_marker_size, + p_manager) != l_marker_size) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, + &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { + return OPJ_FALSE; + } + + if (l_tile_no == tile_no) { + /* we found what we were looking for */ + break; + } + + if (l_tot_len < 14U) { + /* last SOT until EOC or invalid Psot value */ + /* assume all is OK */ + if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { + return OPJ_FALSE; + } + return OPJ_TRUE; + } + l_tot_len -= 12U; + /* look for next SOT marker */ + if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len), + p_manager) != (OPJ_OFF_T)(l_tot_len)) { + /* assume all is OK */ + if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { + return OPJ_FALSE; + } + return OPJ_TRUE; + } + } + + /* check for correction */ + if (l_current_part == l_num_parts) { + *p_correction_needed = OPJ_TRUE; + } + + if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) { + return OPJ_FALSE; + } + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 l_current_marker = J2K_MS_SOT; + OPJ_UINT32 l_marker_size; + const opj_dec_memory_marker_handler_t * l_marker_handler = 00; + opj_tcp_t * l_tcp = NULL; + const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* Reach the End Of Codestream ?*/ + if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { + l_current_marker = J2K_MS_EOC; + } + /* We need to encounter a SOT marker (a new tile-part header) */ + else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { + return OPJ_FALSE; + } + + /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ + while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) && + (l_current_marker != J2K_MS_EOC)) { + + /* Try to read until the Start Of Data is detected */ + while (l_current_marker != J2K_MS_SOD) { + + if (opj_stream_get_number_byte_left(p_stream) == 0) { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; + break; + } + + /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes from the buffer as the marker size */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size, + 2); + + /* Check marker size (does not include marker ID but includes marker size) */ + if (l_marker_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); + return OPJ_FALSE; + } + + /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ + if (l_current_marker == 0x8080 && + opj_stream_get_number_byte_left(p_stream) == 0) { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; + break; + } + + /* Why this condition? FIXME */ + if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) { + p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); + } + l_marker_size -= 2; /* Subtract the size of the marker ID already read */ + + /* Get the marker handler from the marker ID */ + l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); + + /* Check if the marker is known and if it is the right place to find it */ + if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { + opj_event_msg(p_manager, EVT_ERROR, + "Marker is not compliant with its position\n"); + return OPJ_FALSE; + } + /* FIXME manage case of unknown marker as in the main header ? */ + + /* Check if the marker size is compatible with the header data size */ + if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { + OPJ_BYTE *new_header_data = NULL; + /* If we are here, this means we consider this marker as known & we will read it */ + /* Check enough bytes left in stream before allocation */ + if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { + opj_event_msg(p_manager, EVT_ERROR, + "Marker size inconsistent with stream length\n"); + return OPJ_FALSE; + } + new_header_data = (OPJ_BYTE *) opj_realloc( + p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); + if (! new_header_data) { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = NULL; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; + p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; + } + + /* Try to read the rest of the marker segment from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, + p_manager) != l_marker_size) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + if (!l_marker_handler->handler) { + /* See issue #175 */ + opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n"); + return OPJ_FALSE; + } + /* Read the marker segment with the correct marker handler */ + if (!(*(l_marker_handler->handler))(p_j2k, + p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Fail to read the current marker segment (%#x)\n", l_current_marker); + return OPJ_FALSE; + } + + /* Add the marker to the codestream index*/ + if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, + p_j2k->cstr_index, + l_marker_handler->id, + (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, + l_marker_size + 4)) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); + return OPJ_FALSE; + } + + /* Keep the position of the last SOT marker read */ + if (l_marker_handler->id == J2K_MS_SOT) { + OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 + ; + if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) { + p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; + } + } + + if (p_j2k->m_specific_param.m_decoder.m_skip_data) { + /* Skip the rest of the tile part header*/ + if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length, + p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ + } else { + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + /* Read 2 bytes from the buffer as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, + &l_current_marker, 2); + } + } + if (opj_stream_get_number_byte_left(p_stream) == 0 + && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { + break; + } + + /* If we didn't skip data before, we need to read the SOD marker*/ + if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { + /* Try to read the SOD marker and skip data ? FIXME */ + if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { + return OPJ_FALSE; + } + if (p_j2k->m_specific_param.m_decoder.m_can_decode && + !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { + /* Issue 254 */ + OPJ_BOOL l_correction_needed; + + p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; + if (!opj_j2k_need_nb_tile_parts_correction(p_stream, + p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_j2k_apply_nb_tile_parts_correction error\n"); + return OPJ_FALSE; + } + if (l_correction_needed) { + OPJ_UINT32 l_tile_no; + + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1; + /* correct tiles */ + for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { + if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) { + p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1; + } + } + opj_event_msg(p_manager, EVT_WARNING, + "Non conformant codestream TPsot==TNsot.\n"); + } + } + } else { + /* Indicate we will try to read a new tile-part header*/ + p_j2k->m_specific_param.m_decoder.m_skip_data = 0; + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + } + + if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream, + p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) { + + /* Deal with likely non conformant SPOT6 files, where the last */ + /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */ + /* but no other tile-parts were found. */ + if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) { + OPJ_UINT32 l_tile_no; + for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) { + if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 && + p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) { + break; + } + } + if (l_tile_no < l_nb_tiles) { + opj_event_msg(p_manager, EVT_INFO, + "Tile %u has TPsot == 0 and TNsot == 0, " + "but no other tile-parts were found. " + "EOC is also missing.\n", + l_tile_no); + p_j2k->m_current_tile_number = l_tile_no; + l_current_marker = J2K_MS_EOC; + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; + break; + } + } + + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes from buffer as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, + &l_current_marker, 2); + } + } + + /* Current marker is the EOC marker ?*/ + if (l_current_marker == J2K_MS_EOC) { + if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; + } + } + + /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */ + if (! p_j2k->m_specific_param.m_decoder.m_can_decode) { + l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; + + while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) { + ++p_j2k->m_current_tile_number; + ++l_tcp; + } + + if (p_j2k->m_current_tile_number == l_nb_tiles) { + *p_go_on = OPJ_FALSE; + return OPJ_TRUE; + } + } + + if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n"); + return OPJ_FALSE; + } + /*FIXME ???*/ + if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", + p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); + + *p_tile_index = p_j2k->m_current_tile_number; + *p_go_on = OPJ_TRUE; + if (p_data_size) { + /* For internal use in j2k.c, we don't need this */ + /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */ + *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE); + if (*p_data_size == UINT_MAX) { + return OPJ_FALSE; + } + } + *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; + *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; + *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; + *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; + *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; + + p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; + + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 l_current_marker; + OPJ_BYTE l_data [2]; + opj_tcp_t * l_tcp; + opj_image_t* l_image_for_bounds; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA) + || (p_tile_index != p_j2k->m_current_tile_number)) { + return OPJ_FALSE; + } + + l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); + if (! l_tcp->m_data) { + opj_j2k_tcp_destroy(l_tcp); + return OPJ_FALSE; + } + + /* When using the opj_read_tile_header / opj_decode_tile_data API */ + /* such as in test_tile_decoder, m_output_image is NULL, so fall back */ + /* to the full image dimension. This is a bit surprising that */ + /* opj_set_decode_area() is only used to determinte intersecting tiles, */ + /* but full tile decoding is done */ + l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image : + p_j2k->m_private_image; + if (! opj_tcd_decode_tile(p_j2k->m_tcd, + l_image_for_bounds->x0, + l_image_for_bounds->y0, + l_image_for_bounds->x1, + l_image_for_bounds->y1, + p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode, + p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, + l_tcp->m_data, + l_tcp->m_data_size, + p_tile_index, + p_j2k->cstr_index, p_manager)) { + opj_j2k_tcp_destroy(l_tcp); + p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; + opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); + return OPJ_FALSE; + } + + /* p_data can be set to NULL when the call will take care of using */ + /* itself the TCD data. This is typically the case for whole single */ + /* tile decoding optimization. */ + if (p_data != NULL) { + if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { + return OPJ_FALSE; + } + + /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) + * we destroy just the data which will be re-read in read_tile_header*/ + /*opj_j2k_tcp_destroy(l_tcp); + p_j2k->m_tcd->tcp = 0;*/ + opj_j2k_tcp_data_destroy(l_tcp); + } + + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA); + + if (opj_stream_get_number_byte_left(p_stream) == 0 + && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { + return OPJ_TRUE; + } + + if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) { + if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + opj_read_bytes(l_data, &l_current_marker, 2); + + if (l_current_marker == J2K_MS_EOC) { + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; + } else if (l_current_marker != J2K_MS_SOT) { + if (opj_stream_get_number_byte_left(p_stream) == 0) { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; + opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); + return OPJ_TRUE; + } + opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); + return OPJ_FALSE; + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, + opj_image_t* p_output_image) +{ + OPJ_UINT32 i, j; + OPJ_UINT32 l_width_src, l_height_src; + OPJ_UINT32 l_width_dest, l_height_dest; + OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; + OPJ_SIZE_T l_start_offset_src; + OPJ_UINT32 l_start_x_dest, l_start_y_dest; + OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; + OPJ_SIZE_T l_start_offset_dest; + + opj_image_comp_t * l_img_comp_src = 00; + opj_image_comp_t * l_img_comp_dest = 00; + + opj_tcd_tilecomp_t * l_tilec = 00; + opj_image_t * l_image_src = 00; + OPJ_INT32 * l_dest_ptr; + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_image_src = p_tcd->image; + l_img_comp_src = l_image_src->comps; + + l_img_comp_dest = p_output_image->comps; + + for (i = 0; i < l_image_src->numcomps; + i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) { + OPJ_INT32 res_x0, res_x1, res_y0, res_y1; + OPJ_UINT32 src_data_stride; + const OPJ_INT32* p_src_data; + + /* Copy info from decoded comp image to output image */ + l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; + + if (p_tcd->whole_tile_decoding) { + opj_tcd_resolution_t* l_res = l_tilec->resolutions + + l_img_comp_src->resno_decoded; + res_x0 = l_res->x0; + res_y0 = l_res->y0; + res_x1 = l_res->x1; + res_y1 = l_res->y1; + src_data_stride = (OPJ_UINT32)( + l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 - + l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0); + p_src_data = l_tilec->data; + } else { + opj_tcd_resolution_t* l_res = l_tilec->resolutions + + l_img_comp_src->resno_decoded; + res_x0 = (OPJ_INT32)l_res->win_x0; + res_y0 = (OPJ_INT32)l_res->win_y0; + res_x1 = (OPJ_INT32)l_res->win_x1; + res_y1 = (OPJ_INT32)l_res->win_y1; + src_data_stride = l_res->win_x1 - l_res->win_x0; + p_src_data = l_tilec->data_win; + } + + if (p_src_data == NULL) { + /* Happens for partial component decoding */ + continue; + } + + l_width_src = (OPJ_UINT32)(res_x1 - res_x0); + l_height_src = (OPJ_UINT32)(res_y1 - res_y0); + + + /* Current tile component size*/ + /*if (i == 0) { + fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", + res_x0, res_x1, res_y0, res_y1); + }*/ + + + /* Border of the current output component*/ + l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); + l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); + l_x1_dest = l_x0_dest + + l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */ + l_y1_dest = l_y0_dest + l_img_comp_dest->h; + + /*if (i == 0) { + fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", + l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); + }*/ + + /*-----*/ + /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) + * of the input buffer (decoded tile component) which will be move + * in the output buffer. Compute the area of the output buffer (l_start_x_dest, + * l_start_y_dest, l_width_dest, l_height_dest) which will be modified + * by this input area. + * */ + assert(res_x0 >= 0); + assert(res_x1 >= 0); + if (l_x0_dest < (OPJ_UINT32)res_x0) { + l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest; + l_offset_x0_src = 0; + + if (l_x1_dest >= (OPJ_UINT32)res_x1) { + l_width_dest = l_width_src; + l_offset_x1_src = 0; + } else { + l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ; + l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest); + } + } else { + l_start_x_dest = 0U; + l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0; + + if (l_x1_dest >= (OPJ_UINT32)res_x1) { + l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src; + l_offset_x1_src = 0; + } else { + l_width_dest = l_img_comp_dest->w ; + l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest; + } + } + + if (l_y0_dest < (OPJ_UINT32)res_y0) { + l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest; + l_offset_y0_src = 0; + + if (l_y1_dest >= (OPJ_UINT32)res_y1) { + l_height_dest = l_height_src; + l_offset_y1_src = 0; + } else { + l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ; + l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest); + } + } else { + l_start_y_dest = 0U; + l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0; + + if (l_y1_dest >= (OPJ_UINT32)res_y1) { + l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src; + l_offset_y1_src = 0; + } else { + l_height_dest = l_img_comp_dest->h ; + l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest; + } + } + + if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) || + (l_offset_y1_src < 0)) { + return OPJ_FALSE; + } + /* testcase 2977.pdf.asan.67.2198 */ + if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) { + return OPJ_FALSE; + } + /*-----*/ + + /* Compute the input buffer offset */ + l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src + * (OPJ_SIZE_T)src_data_stride; + + /* Compute the output buffer offset */ + l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest + * (OPJ_SIZE_T)l_img_comp_dest->w; + + /* Allocate output component buffer if necessary */ + if (l_img_comp_dest->data == NULL && + l_start_offset_src == 0 && l_start_offset_dest == 0 && + src_data_stride == l_img_comp_dest->w && + l_width_dest == l_img_comp_dest->w && + l_height_dest == l_img_comp_dest->h) { + /* If the final image matches the tile buffer, then borrow it */ + /* directly to save a copy */ + if (p_tcd->whole_tile_decoding) { + l_img_comp_dest->data = l_tilec->data; + l_tilec->data = NULL; + } else { + l_img_comp_dest->data = l_tilec->data_win; + l_tilec->data_win = NULL; + } + continue; + } else if (l_img_comp_dest->data == NULL) { + OPJ_SIZE_T l_width = l_img_comp_dest->w; + OPJ_SIZE_T l_height = l_img_comp_dest->h; + + if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) || + l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) { + /* would overflow */ + return OPJ_FALSE; + } + l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height * + sizeof(OPJ_INT32)); + if (! l_img_comp_dest->data) { + return OPJ_FALSE; + } + + if (l_img_comp_dest->w != l_width_dest || + l_img_comp_dest->h != l_height_dest) { + memset(l_img_comp_dest->data, 0, + (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32)); + } + } + + /* Move the output buffer to the first place where we will write*/ + l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; + + { + const OPJ_INT32 * l_src_ptr = p_src_data; + l_src_ptr += l_start_offset_src; + + for (j = 0; j < l_height_dest; ++j) { + memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32)); + l_dest_ptr += l_img_comp_dest->w; + l_src_ptr += src_data_stride; + } + } + + + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 it_comp; + OPJ_INT32 l_comp_x1, l_comp_y1; + opj_image_comp_t* l_img_comp = NULL; + + l_img_comp = p_image->comps; + for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { + OPJ_INT32 l_h, l_w; + if (p_image->x0 > (OPJ_UINT32)INT_MAX || + p_image->y0 > (OPJ_UINT32)INT_MAX || + p_image->x1 > (OPJ_UINT32)INT_MAX || + p_image->y1 > (OPJ_UINT32)INT_MAX) { + opj_event_msg(p_manager, EVT_ERROR, + "Image coordinates above INT_MAX are not supported\n"); + return OPJ_FALSE; + } + + l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, + (OPJ_INT32)l_img_comp->dx); + l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, + (OPJ_INT32)l_img_comp->dy); + l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); + l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); + + l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) + - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); + if (l_w < 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", + it_comp, l_w); + return OPJ_FALSE; + } + l_img_comp->w = (OPJ_UINT32)l_w; + + l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) + - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); + if (l_h < 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", + it_comp, l_h); + return OPJ_FALSE; + } + l_img_comp->h = (OPJ_UINT32)l_h; + + l_img_comp++; + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k, + OPJ_UINT32 numcomps, + const OPJ_UINT32* comps_indices, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i; + OPJ_BOOL* already_mapped; + + if (p_j2k->m_private_image == NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_read_header() should be called before " + "opj_set_decoded_components().\n"); + return OPJ_FALSE; + } + + already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL), + p_j2k->m_private_image->numcomps); + if (already_mapped == NULL) { + return OPJ_FALSE; + } + + for (i = 0; i < numcomps; i++) { + if (comps_indices[i] >= p_j2k->m_private_image->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid component index: %u\n", + comps_indices[i]); + opj_free(already_mapped); + return OPJ_FALSE; + } + if (already_mapped[comps_indices[i]]) { + opj_event_msg(p_manager, EVT_ERROR, + "Component index %u used several times\n", + comps_indices[i]); + opj_free(already_mapped); + return OPJ_FALSE; + } + already_mapped[comps_indices[i]] = OPJ_TRUE; + } + opj_free(already_mapped); + + opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode); + if (numcomps) { + p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = + (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32)); + if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) { + p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0; + return OPJ_FALSE; + } + memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode, + comps_indices, + numcomps * sizeof(OPJ_UINT32)); + } else { + p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL; + } + p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps; + + return OPJ_TRUE; +} + + +OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, + opj_event_mgr_t * p_manager) +{ + opj_cp_t * l_cp = &(p_j2k->m_cp); + opj_image_t * l_image = p_j2k->m_private_image; + OPJ_BOOL ret; + OPJ_UINT32 it_comp; + + if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && + p_j2k->m_cp.tcps[0].m_data != NULL) { + /* In the case of a single-tiled image whose codestream we have already */ + /* ingested, go on */ + } + /* Check if we are read the main header */ + else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { + opj_event_msg(p_manager, EVT_ERROR, + "Need to decode the main header before begin to decode the remaining codestream.\n"); + return OPJ_FALSE; + } + + /* Update the comps[].factor member of the output image with the one */ + /* of m_reduce */ + for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { + p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; + } + + if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) { + opj_event_msg(p_manager, EVT_INFO, + "No decoded area parameters, set the decoded area to the whole image\n"); + + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + + p_image->x0 = l_image->x0; + p_image->y0 = l_image->y0; + p_image->x1 = l_image->x1; + p_image->y1 = l_image->y1; + + return opj_j2k_update_image_dimensions(p_image, p_manager); + } + + /* ----- */ + /* Check if the positions provided by the user are correct */ + + /* Left */ + if (p_start_x < 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Left position of the decoded area (region_x0=%d) should be >= 0.\n", + p_start_x); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_start_x > l_image->x1) { + opj_event_msg(p_manager, EVT_ERROR, + "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", + p_start_x, l_image->x1); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_start_x < l_image->x0) { + opj_event_msg(p_manager, EVT_WARNING, + "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", + p_start_x, l_image->x0); + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_image->x0 = l_image->x0; + } else { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - + l_cp->tx0) / l_cp->tdx; + p_image->x0 = (OPJ_UINT32)p_start_x; + } + + /* Up */ + if (p_start_y < 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Up position of the decoded area (region_y0=%d) should be >= 0.\n", + p_start_y); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_start_y > l_image->y1) { + opj_event_msg(p_manager, EVT_ERROR, + "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", + p_start_y, l_image->y1); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_start_y < l_image->y0) { + opj_event_msg(p_manager, EVT_WARNING, + "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", + p_start_y, l_image->y0); + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_image->y0 = l_image->y0; + } else { + p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - + l_cp->ty0) / l_cp->tdy; + p_image->y0 = (OPJ_UINT32)p_start_y; + } + + /* Right */ + if (p_end_x <= 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Right position of the decoded area (region_x1=%d) should be > 0.\n", + p_end_x); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_end_x < l_image->x0) { + opj_event_msg(p_manager, EVT_ERROR, + "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", + p_end_x, l_image->x0); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_end_x > l_image->x1) { + opj_event_msg(p_manager, EVT_WARNING, + "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", + p_end_x, l_image->x1); + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_image->x1 = l_image->x1; + } else { + p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv( + p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx); + p_image->x1 = (OPJ_UINT32)p_end_x; + } + + /* Bottom */ + if (p_end_y <= 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Bottom position of the decoded area (region_y1=%d) should be > 0.\n", + p_end_y); + return OPJ_FALSE; + } else if ((OPJ_UINT32)p_end_y < l_image->y0) { + opj_event_msg(p_manager, EVT_ERROR, + "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", + p_end_y, l_image->y0); + return OPJ_FALSE; + } + if ((OPJ_UINT32)p_end_y > l_image->y1) { + opj_event_msg(p_manager, EVT_WARNING, + "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", + p_end_y, l_image->y1); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + p_image->y1 = l_image->y1; + } else { + p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv( + p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy); + p_image->y1 = (OPJ_UINT32)p_end_y; + } + /* ----- */ + + p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; + + ret = opj_j2k_update_image_dimensions(p_image, p_manager); + + if (ret) { + opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n", + p_image->x0, p_image->y0, p_image->x1, p_image->y1); + } + + return ret; +} + +opj_j2k_t* opj_j2k_create_decompress(void) +{ + opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); + if (!l_j2k) { + return 00; + } + + l_j2k->m_is_decoder = 1; + l_j2k->m_cp.m_is_decoder = 1; + /* in the absence of JP2 boxes, consider different bit depth / sign */ + /* per component is allowed */ + l_j2k->m_cp.allow_different_bit_depth_sign = 1; + +#ifdef OPJ_DISABLE_TPSOT_FIX + l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; +#endif + + l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1, + sizeof(opj_tcp_t)); + if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { + opj_j2k_destroy(l_j2k); + return 00; + } + + l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1, + OPJ_J2K_DEFAULT_HEADER_SIZE); + if (! l_j2k->m_specific_param.m_decoder.m_header_data) { + opj_j2k_destroy(l_j2k); + return 00; + } + + l_j2k->m_specific_param.m_decoder.m_header_data_size = + OPJ_J2K_DEFAULT_HEADER_SIZE; + + l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; + + l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; + + /* codestream index creation */ + l_j2k->cstr_index = opj_j2k_create_cstr_index(); + if (!l_j2k->cstr_index) { + opj_j2k_destroy(l_j2k); + return 00; + } + + /* validation list creation */ + l_j2k->m_validation_list = opj_procedure_list_create(); + if (! l_j2k->m_validation_list) { + opj_j2k_destroy(l_j2k); + return 00; + } + + /* execution list creation */ + l_j2k->m_procedure_list = opj_procedure_list_create(); + if (! l_j2k->m_procedure_list) { + opj_j2k_destroy(l_j2k); + return 00; + } + + l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count()); + if (!l_j2k->m_tp) { + l_j2k->m_tp = opj_thread_pool_create(0); + } + if (!l_j2k->m_tp) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + return l_j2k; +} + +static opj_codestream_index_t* opj_j2k_create_cstr_index(void) +{ + opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) + opj_calloc(1, sizeof(opj_codestream_index_t)); + if (!cstr_index) { + return NULL; + } + + cstr_index->maxmarknum = 100; + cstr_index->marknum = 0; + cstr_index->marker = (opj_marker_info_t*) + opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); + if (!cstr_index-> marker) { + opj_free(cstr_index); + return NULL; + } + + cstr_index->tile_index = NULL; + + return cstr_index; +} + +static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + /* preconditions again */ + assert(p_tile_no < (l_cp->tw * l_cp->th)); + assert(p_comp_no < p_j2k->m_private_image->numcomps); + + if (l_tccp->csty & J2K_CCP_CSTY_PRT) { + return 5 + l_tccp->numresolutions; + } else { + return 5; + } +} + +static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) +{ + OPJ_UINT32 i; + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + opj_tccp_t *l_tccp0 = NULL; + opj_tccp_t *l_tccp1 = NULL; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp0 = &l_tcp->tccps[p_first_comp_no]; + l_tccp1 = &l_tcp->tccps[p_second_comp_no]; + + if (l_tccp0->numresolutions != l_tccp1->numresolutions) { + return OPJ_FALSE; + } + if (l_tccp0->cblkw != l_tccp1->cblkw) { + return OPJ_FALSE; + } + if (l_tccp0->cblkh != l_tccp1->cblkh) { + return OPJ_FALSE; + } + if (l_tccp0->cblksty != l_tccp1->cblksty) { + return OPJ_FALSE; + } + if (l_tccp0->qmfbid != l_tccp1->qmfbid) { + return OPJ_FALSE; + } + if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) { + return OPJ_FALSE; + } + + for (i = 0U; i < l_tccp0->numresolutions; ++i) { + if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) { + return OPJ_FALSE; + } + if (l_tccp0->prch[i] != l_tccp1->prch[i]) { + return OPJ_FALSE; + } + } + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 i; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_header_size != 00); + assert(p_manager != 00); + assert(p_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + /* preconditions again */ + assert(p_tile_no < (l_cp->tw * l_cp->th)); + assert(p_comp_no < (p_j2k->m_private_image->numcomps)); + + if (*p_header_size < 5) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); + return OPJ_FALSE; + } + + opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ + ++p_data; + + opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */ + ++p_data; + + opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */ + ++p_data; + + opj_write_bytes(p_data, l_tccp->cblksty, + 1); /* SPcoc (G) */ + ++p_data; + + opj_write_bytes(p_data, l_tccp->qmfbid, + 1); /* SPcoc (H) */ + ++p_data; + + *p_header_size = *p_header_size - 5; + + if (l_tccp->csty & J2K_CCP_CSTY_PRT) { + + if (*p_header_size < l_tccp->numresolutions) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); + return OPJ_FALSE; + } + + for (i = 0; i < l_tccp->numresolutions; ++i) { + opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4), + 1); /* SPcoc (I_i) */ + ++p_data; + } + + *p_header_size = *p_header_size - l_tccp->numresolutions; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k, + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, l_tmp; + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + opj_tccp_t *l_tccp = NULL; + OPJ_BYTE * l_current_ptr = NULL; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + /* precondition again */ + assert(compno < p_j2k->m_private_image->numcomps); + + l_tccp = &l_tcp->tccps[compno]; + l_current_ptr = p_header_data; + + /* make sure room is sufficient */ + if (*p_header_size < 5) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + return OPJ_FALSE; + } + + /* SPcod (D) / SPcoc (A) */ + opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1); + ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ + if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n", + l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); + return OPJ_FALSE; + } + ++l_current_ptr; + + /* If user wants to remove more resolutions than the codestream contains, return error */ + if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { + opj_event_msg(p_manager, EVT_ERROR, + "Error decoding component %d.\nThe number of resolutions " + "to remove (%d) is greater or equal than the number " + "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n", + compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions); + p_j2k->m_specific_param.m_decoder.m_state |= + 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ + return OPJ_FALSE; + } + + /* SPcod (E) / SPcoc (B) */ + opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1); + ++l_current_ptr; + l_tccp->cblkw += 2; + + /* SPcod (F) / SPcoc (C) */ + opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1); + ++l_current_ptr; + l_tccp->cblkh += 2; + + if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || + ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); + return OPJ_FALSE; + } + + /* SPcod (G) / SPcoc (D) */ + opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1); + ++l_current_ptr; + if (l_tccp->cblksty & 0xC0U) { /* 2 msb are reserved, assume we can't read */ + opj_event_msg(p_manager, EVT_ERROR, + "Error reading SPCod SPCoc element, Invalid code-block style found\n"); + return OPJ_FALSE; + } + + /* SPcod (H) / SPcoc (E) */ + opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1); + ++l_current_ptr; + + if (l_tccp->qmfbid > 1) { + opj_event_msg(p_manager, EVT_ERROR, + "Error reading SPCod SPCoc element, Invalid transformation found\n"); + return OPJ_FALSE; + } + + *p_header_size = *p_header_size - 5; + + /* use custom precinct size ? */ + if (l_tccp->csty & J2K_CCP_CSTY_PRT) { + if (*p_header_size < l_tccp->numresolutions) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + return OPJ_FALSE; + } + + /* SPcod (I_i) / SPcoc (F_i) */ + for (i = 0; i < l_tccp->numresolutions; ++i) { + opj_read_bytes(l_current_ptr, &l_tmp, 1); + ++l_current_ptr; + /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ + if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n"); + return OPJ_FALSE; + } + l_tccp->prcw[i] = l_tmp & 0xf; + l_tccp->prch[i] = l_tmp >> 4; + } + + *p_header_size = *p_header_size - l_tccp->numresolutions; + } else { + /* set default size for the precinct width and height */ + for (i = 0; i < l_tccp->numresolutions; ++i) { + l_tccp->prcw[i] = 15; + l_tccp->prch[i] = 15; + } + } + +#ifdef WIP_REMOVE_MSD + /* INDEX >> */ + if (p_j2k->cstr_info && compno == 0) { + OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); + + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = + l_tccp->cblkh; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = + l_tccp->cblkw; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions + = l_tccp->numresolutions; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = + l_tccp->cblksty; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = + l_tccp->qmfbid; + + memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw, + l_data_size); + memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch, + l_data_size); + } + /* << INDEX */ +#endif + + return OPJ_TRUE; +} + +static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k) +{ + /* loop */ + OPJ_UINT32 i; + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; + OPJ_UINT32 l_prc_size; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) + ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + l_ref_tccp = &l_tcp->tccps[0]; + l_copied_tccp = l_ref_tccp + 1; + l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32); + + for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { + l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; + l_copied_tccp->cblkw = l_ref_tccp->cblkw; + l_copied_tccp->cblkh = l_ref_tccp->cblkh; + l_copied_tccp->cblksty = l_ref_tccp->cblksty; + l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; + memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size); + memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size); + ++l_copied_tccp; + } +} + +static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no) +{ + OPJ_UINT32 l_num_bands; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + /* preconditions again */ + assert(p_tile_no < l_cp->tw * l_cp->th); + assert(p_comp_no < p_j2k->m_private_image->numcomps); + + l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : + (l_tccp->numresolutions * 3 - 2); + + if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + return 1 + l_num_bands; + } else { + return 1 + 2 * l_num_bands; + } +} + +static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no) +{ + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + opj_tccp_t *l_tccp0 = NULL; + opj_tccp_t *l_tccp1 = NULL; + OPJ_UINT32 l_band_no, l_num_bands; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp0 = &l_tcp->tccps[p_first_comp_no]; + l_tccp1 = &l_tcp->tccps[p_second_comp_no]; + + if (l_tccp0->qntsty != l_tccp1->qntsty) { + return OPJ_FALSE; + } + if (l_tccp0->numgbits != l_tccp1->numgbits) { + return OPJ_FALSE; + } + if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) { + l_num_bands = 1U; + } else { + l_num_bands = l_tccp0->numresolutions * 3U - 2U; + if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) { + return OPJ_FALSE; + } + } + + for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { + if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) { + return OPJ_FALSE; + } + } + if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) { + for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { + if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) { + return OPJ_FALSE; + } + } + } + return OPJ_TRUE; +} + + +static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_header_size; + OPJ_UINT32 l_band_no, l_num_bands; + OPJ_UINT32 l_expn, l_mant; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_header_size != 00); + assert(p_manager != 00); + assert(p_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + /* preconditions again */ + assert(p_tile_no < l_cp->tw * l_cp->th); + assert(p_comp_no < p_j2k->m_private_image->numcomps); + + l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : + (l_tccp->numresolutions * 3 - 2); + + if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + l_header_size = 1 + l_num_bands; + + if (*p_header_size < l_header_size) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); + return OPJ_FALSE; + } + + opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), + 1); /* Sqcx */ + ++p_data; + + for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { + l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; + opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ + ++p_data; + } + } else { + l_header_size = 1 + 2 * l_num_bands; + + if (*p_header_size < l_header_size) { + opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); + return OPJ_FALSE; + } + + opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5), + 1); /* Sqcx */ + ++p_data; + + for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { + l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn; + l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant; + + opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ + p_data += 2; + } + } + + *p_header_size = *p_header_size - l_header_size; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE* p_header_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager + ) +{ + /* loop*/ + OPJ_UINT32 l_band_no; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_BYTE * l_current_ptr = 00; + OPJ_UINT32 l_tmp, l_num_band; + + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + l_cp = &(p_j2k->m_cp); + /* come from tile part header or main header ?*/ + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) + ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + /* precondition again*/ + assert(p_comp_no < p_j2k->m_private_image->numcomps); + + l_tccp = &l_tcp->tccps[p_comp_no]; + l_current_ptr = p_header_data; + + if (*p_header_size < 1) { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); + return OPJ_FALSE; + } + *p_header_size -= 1; + + opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */ + ++l_current_ptr; + + l_tccp->qntsty = l_tmp & 0x1f; + l_tccp->numgbits = l_tmp >> 5; + if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { + l_num_band = 1; + } else { + l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? + (*p_header_size) : + (*p_header_size) / 2; + + if (l_num_band > OPJ_J2K_MAXBANDS) { + opj_event_msg(p_manager, EVT_WARNING, + "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " + "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " + "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, + OPJ_J2K_MAXBANDS); + /*return OPJ_FALSE;*/ + } + } + +#ifdef USE_JPWL + if (l_cp->correct) { + + /* if JPWL is on, we check whether there are too many subbands */ + if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { + opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: bad number of subbands in Sqcx (%d)\n", + l_num_band); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_num_band = 1; + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" + "- setting number of bands to %d => HYPOTHESIS!!!\n", + l_num_band); + }; + + }; +#endif /* USE_JPWL */ + + if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { + opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */ + ++l_current_ptr; + if (l_band_no < OPJ_J2K_MAXBANDS) { + l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3); + l_tccp->stepsizes[l_band_no].mant = 0; + } + } + *p_header_size = *p_header_size - l_num_band; + } else { + for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { + opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */ + l_current_ptr += 2; + if (l_band_no < OPJ_J2K_MAXBANDS) { + l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11); + l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; + } + } + *p_header_size = *p_header_size - 2 * l_num_band; + } + + /* Add Antonin : if scalar_derived -> compute other stepsizes */ + if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { + for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) { + l_tccp->stepsizes[l_band_no].expn = + ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) + ? + (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0; + l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; + } + } + + return OPJ_TRUE; +} + +static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 i; + opj_cp_t *l_cp = NULL; + opj_tcp_t *l_tcp = NULL; + opj_tccp_t *l_ref_tccp = NULL; + opj_tccp_t *l_copied_tccp = NULL; + OPJ_UINT32 l_size; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + l_ref_tccp = &l_tcp->tccps[0]; + l_copied_tccp = l_ref_tccp + 1; + l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t); + + for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) { + l_copied_tccp->qntsty = l_ref_tccp->qntsty; + l_copied_tccp->numgbits = l_ref_tccp->numgbits; + memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size); + ++l_copied_tccp; + } +} + +static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile, + OPJ_INT32 numcomps, FILE* out_stream) +{ + if (l_default_tile) { + OPJ_INT32 compno; + + fprintf(out_stream, "\t default tile {\n"); + fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); + fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); + fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); + fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); + + for (compno = 0; compno < numcomps; compno++) { + opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); + OPJ_UINT32 resno; + OPJ_INT32 bandno, numbands; + + /* coding style*/ + fprintf(out_stream, "\t\t comp %d {\n", compno); + fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); + fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); + fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); + fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); + fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); + fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); + + fprintf(out_stream, "\t\t\t preccintsize (w,h)="); + for (resno = 0; resno < l_tccp->numresolutions; resno++) { + fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); + } + fprintf(out_stream, "\n"); + + /* quantization style*/ + fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); + fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); + fprintf(out_stream, "\t\t\t stepsizes (m,e)="); + numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : + (OPJ_INT32)l_tccp->numresolutions * 3 - 2; + for (bandno = 0; bandno < numbands; bandno++) { + fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, + l_tccp->stepsizes[bandno].expn); + } + fprintf(out_stream, "\n"); + + /* RGN value*/ + fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); + + fprintf(out_stream, "\t\t }\n"); + } /*end of component of default tile*/ + fprintf(out_stream, "\t }\n"); /*end of default tile*/ + } +} + +void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) +{ + /* Check if the flag is compatible with j2k file*/ + if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) { + fprintf(out_stream, "Wrong flag\n"); + return; + } + + /* Dump the image_header */ + if (flag & OPJ_IMG_INFO) { + if (p_j2k->m_private_image) { + j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); + } + } + + /* Dump the codestream info from main header */ + if (flag & OPJ_J2K_MH_INFO) { + if (p_j2k->m_private_image) { + opj_j2k_dump_MH_info(p_j2k, out_stream); + } + } + /* Dump all tile/codestream info */ + if (flag & OPJ_J2K_TCH_INFO) { + OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + OPJ_UINT32 i; + opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; + if (p_j2k->m_private_image) { + for (i = 0; i < l_nb_tiles; ++i) { + opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps, + out_stream); + ++l_tcp; + } + } + } + + /* Dump the codestream info of the current tile */ + if (flag & OPJ_J2K_TH_INFO) { + + } + + /* Dump the codestream index from main header */ + if (flag & OPJ_J2K_MH_IND) { + opj_j2k_dump_MH_index(p_j2k, out_stream); + } + + /* Dump the codestream index of the current tile */ + if (flag & OPJ_J2K_TH_IND) { + + } + +} + +static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream) +{ + opj_codestream_index_t* cstr_index = p_j2k->cstr_index; + OPJ_UINT32 it_marker, it_tile, it_tile_part; + + fprintf(out_stream, "Codestream index from main header: {\n"); + + fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" + "\t Main header end position=%" PRIi64 "\n", + cstr_index->main_head_start, cstr_index->main_head_end); + + fprintf(out_stream, "\t Marker list: {\n"); + + if (cstr_index->marker) { + for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) { + fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", + cstr_index->marker[it_marker].type, + cstr_index->marker[it_marker].pos, + cstr_index->marker[it_marker].len); + } + } + + fprintf(out_stream, "\t }\n"); + + if (cstr_index->tile_index) { + + /* Simple test to avoid to write empty information*/ + OPJ_UINT32 l_acc_nb_of_tile_part = 0; + for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { + l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; + } + + if (l_acc_nb_of_tile_part) { + fprintf(out_stream, "\t Tile index: {\n"); + + for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) { + OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; + + fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, + nb_of_tile_part); + + if (cstr_index->tile_index[it_tile].tp_index) { + for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) { + fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" + PRIi64 ", end_pos=%" PRIi64 ".\n", + it_tile_part, + cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, + cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, + cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); + } + } + + if (cstr_index->tile_index[it_tile].marker) { + for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ; + it_marker++) { + fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", + cstr_index->tile_index[it_tile].marker[it_marker].type, + cstr_index->tile_index[it_tile].marker[it_marker].pos, + cstr_index->tile_index[it_tile].marker[it_marker].len); + } + } + } + fprintf(out_stream, "\t }\n"); + } + } + + fprintf(out_stream, "}\n"); + +} + + +static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream) +{ + + fprintf(out_stream, "Codestream info from main header: {\n"); + + fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0); + fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy); + fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th); + opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp, + (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream); + fprintf(out_stream, "}\n"); +} + +void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, + FILE* out_stream) +{ + char tab[2]; + + if (dev_dump_flag) { + fprintf(stdout, "[DEV] Dump an image_header struct {\n"); + tab[0] = '\0'; + } else { + fprintf(out_stream, "Image info {\n"); + tab[0] = '\t'; + tab[1] = '\0'; + } + + fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); + fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, + img_header->y1); + fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); + + if (img_header->comps) { + OPJ_UINT32 compno; + for (compno = 0; compno < img_header->numcomps; compno++) { + fprintf(out_stream, "%s\t component %d {\n", tab, compno); + j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, + out_stream); + fprintf(out_stream, "%s}\n", tab); + } + } + + fprintf(out_stream, "}\n"); +} + +void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, + OPJ_BOOL dev_dump_flag, FILE* out_stream) +{ + char tab[3]; + + if (dev_dump_flag) { + fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); + tab[0] = '\0'; + } else { + tab[0] = '\t'; + tab[1] = '\t'; + tab[2] = '\0'; + } + + fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); + fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); + fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); + + if (dev_dump_flag) { + fprintf(out_stream, "}\n"); + } +} + +opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) +{ + OPJ_UINT32 compno; + OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; + opj_tcp_t *l_default_tile; + opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1, + sizeof(opj_codestream_info_v2_t)); + if (!cstr_info) { + return NULL; + } + + cstr_info->nbcomps = p_j2k->m_private_image->numcomps; + + cstr_info->tx0 = p_j2k->m_cp.tx0; + cstr_info->ty0 = p_j2k->m_cp.ty0; + cstr_info->tdx = p_j2k->m_cp.tdx; + cstr_info->tdy = p_j2k->m_cp.tdy; + cstr_info->tw = p_j2k->m_cp.tw; + cstr_info->th = p_j2k->m_cp.th; + + cstr_info->tile_info = NULL; /* Not fill from the main header*/ + + l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; + + cstr_info->m_default_tile_info.csty = l_default_tile->csty; + cstr_info->m_default_tile_info.prg = l_default_tile->prg; + cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; + cstr_info->m_default_tile_info.mct = l_default_tile->mct; + + cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc( + cstr_info->nbcomps, sizeof(opj_tccp_info_t)); + if (!cstr_info->m_default_tile_info.tccp_info) { + opj_destroy_cstr_info(&cstr_info); + return NULL; + } + + for (compno = 0; compno < numcomps; compno++) { + opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); + opj_tccp_info_t *l_tccp_info = & + (cstr_info->m_default_tile_info.tccp_info[compno]); + OPJ_INT32 bandno, numbands; + + /* coding style*/ + l_tccp_info->csty = l_tccp->csty; + l_tccp_info->numresolutions = l_tccp->numresolutions; + l_tccp_info->cblkw = l_tccp->cblkw; + l_tccp_info->cblkh = l_tccp->cblkh; + l_tccp_info->cblksty = l_tccp->cblksty; + l_tccp_info->qmfbid = l_tccp->qmfbid; + if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) { + memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); + memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); + } + + /* quantization style*/ + l_tccp_info->qntsty = l_tccp->qntsty; + l_tccp_info->numgbits = l_tccp->numgbits; + + numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : + (OPJ_INT32)l_tccp->numresolutions * 3 - 2; + if (numbands < OPJ_J2K_MAXBANDS) { + for (bandno = 0; bandno < numbands; bandno++) { + l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32) + l_tccp->stepsizes[bandno].mant; + l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32) + l_tccp->stepsizes[bandno].expn; + } + } + + /* RGN value*/ + l_tccp_info->roishift = l_tccp->roishift; + } + + return cstr_info; +} + +opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) +{ + opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) + opj_calloc(1, sizeof(opj_codestream_index_t)); + if (!l_cstr_index) { + return NULL; + } + + l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; + l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; + l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; + + l_cstr_index->marknum = p_j2k->cstr_index->marknum; + l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum * + sizeof(opj_marker_info_t)); + if (!l_cstr_index->marker) { + opj_free(l_cstr_index); + return NULL; + } + + if (p_j2k->cstr_index->marker) { + memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, + l_cstr_index->marknum * sizeof(opj_marker_info_t)); + } else { + opj_free(l_cstr_index->marker); + l_cstr_index->marker = NULL; + } + + l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; + l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( + l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); + if (!l_cstr_index->tile_index) { + opj_free(l_cstr_index->marker); + opj_free(l_cstr_index); + return NULL; + } + + if (!p_j2k->cstr_index->tile_index) { + opj_free(l_cstr_index->tile_index); + l_cstr_index->tile_index = NULL; + } else { + OPJ_UINT32 it_tile = 0; + for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) { + + /* Tile Marker*/ + l_cstr_index->tile_index[it_tile].marknum = + p_j2k->cstr_index->tile_index[it_tile].marknum; + + l_cstr_index->tile_index[it_tile].marker = + (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum * + sizeof(opj_marker_info_t)); + + if (!l_cstr_index->tile_index[it_tile].marker) { + OPJ_UINT32 it_tile_free; + + for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { + opj_free(l_cstr_index->tile_index[it_tile_free].marker); + } + + opj_free(l_cstr_index->tile_index); + opj_free(l_cstr_index->marker); + opj_free(l_cstr_index); + return NULL; + } + + if (p_j2k->cstr_index->tile_index[it_tile].marker) + memcpy(l_cstr_index->tile_index[it_tile].marker, + p_j2k->cstr_index->tile_index[it_tile].marker, + l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t)); + else { + opj_free(l_cstr_index->tile_index[it_tile].marker); + l_cstr_index->tile_index[it_tile].marker = NULL; + } + + /* Tile part index*/ + l_cstr_index->tile_index[it_tile].nb_tps = + p_j2k->cstr_index->tile_index[it_tile].nb_tps; + + l_cstr_index->tile_index[it_tile].tp_index = + (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof( + opj_tp_index_t)); + + if (!l_cstr_index->tile_index[it_tile].tp_index) { + OPJ_UINT32 it_tile_free; + + for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) { + opj_free(l_cstr_index->tile_index[it_tile_free].marker); + opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); + } + + opj_free(l_cstr_index->tile_index); + opj_free(l_cstr_index->marker); + opj_free(l_cstr_index); + return NULL; + } + + if (p_j2k->cstr_index->tile_index[it_tile].tp_index) { + memcpy(l_cstr_index->tile_index[it_tile].tp_index, + p_j2k->cstr_index->tile_index[it_tile].tp_index, + l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t)); + } else { + opj_free(l_cstr_index->tile_index[it_tile].tp_index); + l_cstr_index->tile_index[it_tile].tp_index = NULL; + } + + /* Packet index (NOT USED)*/ + l_cstr_index->tile_index[it_tile].nb_packet = 0; + l_cstr_index->tile_index[it_tile].packet_index = NULL; + + } + } + + return l_cstr_index; +} + +static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 it_tile = 0; + + p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; + p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc( + p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); + if (!p_j2k->cstr_index->tile_index) { + return OPJ_FALSE; + } + + for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) { + p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; + p_j2k->cstr_index->tile_index[it_tile].marknum = 0; + p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) + opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, + sizeof(opj_marker_info_t)); + if (!p_j2k->cstr_index->tile_index[it_tile].marker) { + return OPJ_FALSE; + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 compno; + OPJ_BOOL decoded_all_used_components = OPJ_TRUE; + + if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) { + for (compno = 0; + compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { + OPJ_UINT32 dec_compno = + p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; + if (p_j2k->m_output_image->comps[dec_compno].data == NULL) { + opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", + dec_compno); + decoded_all_used_components = OPJ_FALSE; + } + } + } else { + for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) { + if (p_j2k->m_output_image->comps[compno].data == NULL) { + opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n", + compno); + decoded_all_used_components = OPJ_FALSE; + } + } + } + + if (decoded_all_used_components == OPJ_FALSE) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + + +static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_BOOL l_go_on = OPJ_TRUE; + OPJ_UINT32 l_current_tile_no; + OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 nr_tiles = 0; + + /* Particular case for whole single tile decoding */ + /* We can avoid allocating intermediate tile buffers */ + if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && + p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 && + p_j2k->m_output_image->x0 == 0 && + p_j2k->m_output_image->y0 == 0 && + p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx && + p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) { + OPJ_UINT32 i; + if (! opj_j2k_read_tile_header(p_j2k, + &l_current_tile_no, + NULL, + &l_tile_x0, &l_tile_y0, + &l_tile_x1, &l_tile_y1, + &l_nb_comps, + &l_go_on, + p_stream, + p_manager)) { + return OPJ_FALSE; + } + + if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, + p_stream, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n"); + return OPJ_FALSE; + } + + /* Transfer TCD data to output image data */ + for (i = 0; i < p_j2k->m_output_image->numcomps; i++) { + opj_image_data_free(p_j2k->m_output_image->comps[i].data); + p_j2k->m_output_image->comps[i].data = + p_j2k->m_tcd->tcd_image->tiles->comps[i].data; + p_j2k->m_output_image->comps[i].resno_decoded = + p_j2k->m_tcd->image->comps[i].resno_decoded; + p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL; + } + + return OPJ_TRUE; + } + + for (;;) { + if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && + p_j2k->m_cp.tcps[0].m_data != NULL) { + l_current_tile_no = 0; + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA; + } else { + if (! opj_j2k_read_tile_header(p_j2k, + &l_current_tile_no, + NULL, + &l_tile_x0, &l_tile_y0, + &l_tile_x1, &l_tile_y1, + &l_nb_comps, + &l_go_on, + p_stream, + p_manager)) { + return OPJ_FALSE; + } + + if (! l_go_on) { + break; + } + } + + if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, + p_stream, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", + l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); + return OPJ_FALSE; + } + + opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", + l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); + + if (! opj_j2k_update_image_data(p_j2k->m_tcd, + p_j2k->m_output_image)) { + return OPJ_FALSE; + } + + if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 && + !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 && + p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 && + p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 && + p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) { + /* Keep current tcp data */ + } else { + opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); + } + + opj_event_msg(p_manager, EVT_INFO, + "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); + + if (opj_stream_get_number_byte_left(p_stream) == 0 + && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { + break; + } + if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) { + break; + } + } + + if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures. + */ +static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_decode_tiles, p_manager)) { + return OPJ_FALSE; + } + /* DEVELOPER CORNER, add your custom procedures */ + + return OPJ_TRUE; +} + +/* + * Read and decode one tile. + */ +static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_BOOL l_go_on = OPJ_TRUE; + OPJ_UINT32 l_current_tile_no; + OPJ_UINT32 l_tile_no_to_dec; + OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 i; + + /*Allocate and initialize some elements of codestrem index if not already done*/ + if (!p_j2k->cstr_index->tile_index) { + if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) { + return OPJ_FALSE; + } + } + /* Move into the codestream to the first SOT used to decode the desired tile */ + l_tile_no_to_dec = (OPJ_UINT32) + p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; + if (p_j2k->cstr_index->tile_index) + if (p_j2k->cstr_index->tile_index->tp_index) { + if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { + /* the index for this tile has not been built, + * so move to the last SOT read */ + if (!(opj_stream_read_seek(p_stream, + p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) { + opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); + return OPJ_FALSE; + } + } else { + if (!(opj_stream_read_seek(p_stream, + p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos + 2, + p_manager))) { + opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); + return OPJ_FALSE; + } + } + /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ + if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + } + } + + /* Reset current tile part number for all tiles, and not only the one */ + /* of interest. */ + /* Not completely sure this is always correct but required for */ + /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */ + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; + for (i = 0; i < l_nb_tiles; ++i) { + p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1; + } + + for (;;) { + if (! opj_j2k_read_tile_header(p_j2k, + &l_current_tile_no, + NULL, + &l_tile_x0, &l_tile_y0, + &l_tile_x1, &l_tile_y1, + &l_nb_comps, + &l_go_on, + p_stream, + p_manager)) { + return OPJ_FALSE; + } + + if (! l_go_on) { + break; + } + + if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0, + p_stream, p_manager)) { + return OPJ_FALSE; + } + opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", + l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw); + + if (! opj_j2k_update_image_data(p_j2k->m_tcd, + p_j2k->m_output_image)) { + return OPJ_FALSE; + } + opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]); + + opj_event_msg(p_manager, EVT_INFO, + "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); + + if (l_current_tile_no == l_tile_no_to_dec) { + /* move into the codestream to the first SOT (FIXME or not move?)*/ + if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, + p_manager))) { + opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n"); + return OPJ_FALSE; + } + break; + } else { + opj_event_msg(p_manager, EVT_WARNING, + "Tile read, decoded and updated is not the desired one (%d vs %d).\n", + l_current_tile_no + 1, l_tile_no_to_dec + 1); + } + + } + + if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures. + */ +static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_decode_one_tile, p_manager)) { + return OPJ_FALSE; + } + /* DEVELOPER CORNER, add your custom procedures */ + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k, + opj_image_t * p_image) +{ + OPJ_UINT32 compno; + + /* Move data and copy one information from codec to output image*/ + if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) { + opj_image_comp_t* newcomps = + (opj_image_comp_t*) opj_malloc( + p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode * + sizeof(opj_image_comp_t)); + if (newcomps == NULL) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + for (compno = 0; compno < p_image->numcomps; compno++) { + opj_image_data_free(p_image->comps[compno].data); + p_image->comps[compno].data = NULL; + } + for (compno = 0; + compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) { + OPJ_UINT32 src_compno = + p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno]; + memcpy(&(newcomps[compno]), + &(p_j2k->m_output_image->comps[src_compno]), + sizeof(opj_image_comp_t)); + newcomps[compno].resno_decoded = + p_j2k->m_output_image->comps[src_compno].resno_decoded; + newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data; + p_j2k->m_output_image->comps[src_compno].data = NULL; + } + for (compno = 0; compno < p_image->numcomps; compno++) { + assert(p_j2k->m_output_image->comps[compno].data == NULL); + opj_image_data_free(p_j2k->m_output_image->comps[compno].data); + p_j2k->m_output_image->comps[compno].data = NULL; + } + p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; + opj_free(p_image->comps); + p_image->comps = newcomps; + } else { + for (compno = 0; compno < p_image->numcomps; compno++) { + p_image->comps[compno].resno_decoded = + p_j2k->m_output_image->comps[compno].resno_decoded; + opj_image_data_free(p_image->comps[compno].data); + p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; +#if 0 + char fn[256]; + sprintf(fn, "/tmp/%d.raw", compno); + FILE *debug = fopen(fn, "wb"); + fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32), + p_image->comps[compno].w * p_image->comps[compno].h, debug); + fclose(debug); +#endif + p_j2k->m_output_image->comps[compno].data = NULL; + } + } + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, + opj_stream_private_t * p_stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager) +{ + if (!p_image) { + return OPJ_FALSE; + } + + /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */ + /* and finally opj_decode_image() without manual setting of comps[].factor */ + /* We could potentially always execute it, if we don't allow people to do */ + /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */ + if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 && + p_j2k->m_private_image != NULL && + p_j2k->m_private_image->numcomps > 0 && + p_j2k->m_private_image->comps[0].factor == + p_j2k->m_cp.m_specific_param.m_dec.m_reduce && + p_image->numcomps > 0 && + p_image->comps[0].factor == 0 && + /* Don't mess with image dimension if the user has allocated it */ + p_image->comps[0].data == NULL) { + OPJ_UINT32 it_comp; + + /* Update the comps[].factor member of the output image with the one */ + /* of m_reduce */ + for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) { + p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce; + } + if (!opj_j2k_update_image_dimensions(p_image, p_manager)) { + return OPJ_FALSE; + } + } + + if (p_j2k->m_output_image == NULL) { + p_j2k->m_output_image = opj_image_create0(); + if (!(p_j2k->m_output_image)) { + return OPJ_FALSE; + } + } + opj_copy_image_header(p_image, p_j2k->m_output_image); + + /* customization of the decoding */ + if (!opj_j2k_setup_decoding(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + /* Decode the codestream */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + /* Move data and copy one information from codec to output image*/ + return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); +} + +OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager, + OPJ_UINT32 tile_index) +{ + OPJ_UINT32 compno; + OPJ_UINT32 l_tile_x, l_tile_y; + opj_image_comp_t* l_img_comp; + + if (!p_image) { + opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n"); + return OPJ_FALSE; + } + + if (p_image->numcomps < p_j2k->m_private_image->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, + "Image has less components than codestream.\n"); + return OPJ_FALSE; + } + + if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) { + opj_event_msg(p_manager, EVT_ERROR, + "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, + (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); + return OPJ_FALSE; + } + + /* Compute the dimension of the desired tile*/ + l_tile_x = tile_index % p_j2k->m_cp.tw; + l_tile_y = tile_index / p_j2k->m_cp.tw; + + p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; + if (p_image->x0 < p_j2k->m_private_image->x0) { + p_image->x0 = p_j2k->m_private_image->x0; + } + p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; + if (p_image->x1 > p_j2k->m_private_image->x1) { + p_image->x1 = p_j2k->m_private_image->x1; + } + + p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; + if (p_image->y0 < p_j2k->m_private_image->y0) { + p_image->y0 = p_j2k->m_private_image->y0; + } + p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; + if (p_image->y1 > p_j2k->m_private_image->y1) { + p_image->y1 = p_j2k->m_private_image->y1; + } + + l_img_comp = p_image->comps; + for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { + OPJ_INT32 l_comp_x1, l_comp_y1; + + l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; + + l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, + (OPJ_INT32)l_img_comp->dx); + l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, + (OPJ_INT32)l_img_comp->dy); + l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx); + l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy); + + l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, + (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, + (OPJ_INT32)l_img_comp->factor)); + l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, + (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, + (OPJ_INT32)l_img_comp->factor)); + + l_img_comp++; + } + + if (p_image->numcomps > p_j2k->m_private_image->numcomps) { + /* Can happen when calling repeatdly opj_get_decoded_tile() on an + * image with a color palette, where color palette expansion is done + * later in jp2.c */ + for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps; + ++compno) { + opj_image_data_free(p_image->comps[compno].data); + p_image->comps[compno].data = NULL; + } + p_image->numcomps = p_j2k->m_private_image->numcomps; + } + + /* Destroy the previous output image*/ + if (p_j2k->m_output_image) { + opj_image_destroy(p_j2k->m_output_image); + } + + /* Create the ouput image from the information previously computed*/ + p_j2k->m_output_image = opj_image_create0(); + if (!(p_j2k->m_output_image)) { + return OPJ_FALSE; + } + opj_copy_image_header(p_image, p_j2k->m_output_image); + + p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; + + /* customization of the decoding */ + if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + /* Decode the codestream */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + /* Move data and copy one information from codec to output image*/ + return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image); +} + +OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, + OPJ_UINT32 res_factor, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 it_comp; + + p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; + + if (p_j2k->m_private_image) { + if (p_j2k->m_private_image->comps) { + if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { + if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { + for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { + OPJ_UINT32 max_res = + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; + if (res_factor >= max_res) { + opj_event_msg(p_manager, EVT_ERROR, + "Resolution factor is greater than the maximum resolution in the component.\n"); + return OPJ_FALSE; + } + p_j2k->m_private_image->comps[it_comp].factor = res_factor; + } + return OPJ_TRUE; + } + } + } + } + + return OPJ_FALSE; +} + +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL opj_j2k_encoder_set_extra_options( + opj_j2k_t *p_j2k, + const char* const* p_options, + opj_event_mgr_t * p_manager) +{ + const char* const* p_option_iter; + + if (p_options == NULL) { + return OPJ_TRUE; + } + + for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) { + if (strncmp(*p_option_iter, "PLT=", 4) == 0) { + if (strcmp(*p_option_iter, "PLT=YES") == 0) { + p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE; + } else if (strcmp(*p_option_iter, "PLT=NO") == 0) { + p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE; + } else { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for option: %s.\n", *p_option_iter); + return OPJ_FALSE; + } + } else { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid option: %s.\n", *p_option_iter); + return OPJ_FALSE; + } + } + + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, j; + OPJ_UINT32 l_nb_tiles; + OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size; + OPJ_BYTE * l_current_data = 00; + OPJ_BOOL l_reuse_data = OPJ_FALSE; + opj_tcd_t* p_tcd = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + p_tcd = p_j2k->m_tcd; + + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + if (l_nb_tiles == 1) { + l_reuse_data = OPJ_TRUE; +#ifdef __SSE__ + for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { + opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; + if (((size_t)l_img_comp->data & 0xFU) != + 0U) { /* tile data shall be aligned on 16 bytes */ + l_reuse_data = OPJ_FALSE; + } + } +#endif + } + for (i = 0; i < l_nb_tiles; ++i) { + if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) { + if (l_current_data) { + opj_free(l_current_data); + } + return OPJ_FALSE; + } + + /* if we only have one tile, then simply set tile component data equal to image component data */ + /* otherwise, allocate the data */ + for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { + opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; + if (l_reuse_data) { + opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; + l_tilec->data = l_img_comp->data; + l_tilec->ownsData = OPJ_FALSE; + } else { + if (! opj_alloc_tile_component_data(l_tilec)) { + opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); + if (l_current_data) { + opj_free(l_current_data); + } + return OPJ_FALSE; + } + } + } + l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd); + if (!l_reuse_data) { + if (l_current_tile_size > l_max_tile_size) { + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, + l_current_tile_size); + if (! l_new_current_data) { + if (l_current_data) { + opj_free(l_current_data); + } + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); + return OPJ_FALSE; + } + l_current_data = l_new_current_data; + l_max_tile_size = l_current_tile_size; + } + if (l_current_data == NULL) { + /* Should not happen in practice, but will avoid Coverity to */ + /* complain about a null pointer dereference */ + assert(0); + return OPJ_FALSE; + } + + /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */ + /* 32 bit components @ 8 bit precision get converted to 8 bit */ + /* 32 bit components @ 16 bit precision get converted to 16 bit */ + opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data); + + /* now copy this data into the tile component */ + if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data, + l_current_tile_size)) { + opj_event_msg(p_manager, EVT_ERROR, + "Size mismatch between tile data and sent data."); + opj_free(l_current_data); + return OPJ_FALSE; + } + } + + if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { + if (l_current_data) { + opj_free(l_current_data); + } + return OPJ_FALSE; + } + } + + if (l_current_data) { + opj_free(l_current_data); + } + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + /* customization of the encoding */ + if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + p_j2k->m_private_image = opj_image_create0(); + if (! p_j2k->m_private_image) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header."); + return OPJ_FALSE; + } + opj_copy_image_header(p_image, p_j2k->m_private_image); + + /* TODO_MSD: Find a better way */ + if (p_image->comps) { + OPJ_UINT32 it_comp; + for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { + if (p_image->comps[it_comp].data) { + p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data; + p_image->comps[it_comp].data = NULL; + + } + } + } + + /* customization of the validation */ + if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + /* validation of the parameters codec */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) { + return OPJ_FALSE; + } + + /* customization of the encoding */ + if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) { + return OPJ_FALSE; + } + + /* write header */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + (void)p_stream; + if (p_tile_index != p_j2k->m_current_tile_number) { + opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match."); + return OPJ_FALSE; + } + + opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", + p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); + + p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; + p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; + + /* initialisation before tile encoding */ + if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, + p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static void opj_get_tile_dimensions(opj_image_t * l_image, + opj_tcd_tilecomp_t * l_tilec, + opj_image_comp_t * l_img_comp, + OPJ_UINT32* l_size_comp, + OPJ_UINT32* l_width, + OPJ_UINT32* l_height, + OPJ_UINT32* l_offset_x, + OPJ_UINT32* l_offset_y, + OPJ_UINT32* l_image_width, + OPJ_UINT32* l_stride, + OPJ_UINT32* l_tile_offset) +{ + OPJ_UINT32 l_remaining; + *l_size_comp = l_img_comp->prec >> 3; /* (/8) */ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + if (l_remaining) { + *l_size_comp += 1; + } + + if (*l_size_comp == 3) { + *l_size_comp = 4; + } + + *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); + *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); + *l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, + (OPJ_INT32)l_img_comp->dx); + *l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, + (OPJ_INT32)l_img_comp->dy); + *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - + (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx); + *l_stride = *l_image_width - *l_width; + *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + (( + OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width; +} + +static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data) +{ + OPJ_UINT32 i, j, k = 0; + + for (i = 0; i < p_tcd->image->numcomps; ++i) { + opj_image_t * l_image = p_tcd->image; + OPJ_INT32 * l_src_ptr; + opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i; + opj_image_comp_t * l_img_comp = l_image->comps + i; + OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y, + l_image_width, l_stride, l_tile_offset; + + opj_get_tile_dimensions(l_image, + l_tilec, + l_img_comp, + &l_size_comp, + &l_width, + &l_height, + &l_offset_x, + &l_offset_y, + &l_image_width, + &l_stride, + &l_tile_offset); + + l_src_ptr = l_img_comp->data + l_tile_offset; + + switch (l_size_comp) { + case 1: { + OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; + if (l_img_comp->sgnd) { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr); + ++l_dest_ptr; + ++l_src_ptr; + } + l_src_ptr += l_stride; + } + } else { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff); + ++l_dest_ptr; + ++l_src_ptr; + } + l_src_ptr += l_stride; + } + } + + p_data = (OPJ_BYTE*) l_dest_ptr; + } + break; + case 2: { + OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data; + if (l_img_comp->sgnd) { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++)); + } + l_src_ptr += l_stride; + } + } else { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); + } + l_src_ptr += l_stride; + } + } + + p_data = (OPJ_BYTE*) l_dest_ptr; + } + break; + case 4: { + OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data; + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr++) = *(l_src_ptr++); + } + l_src_ptr += l_stride; + } + + p_data = (OPJ_BYTE*) l_dest_ptr; + } + break; + } + } +} + +static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 l_nb_bytes_written; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tile_size = 0; + OPJ_UINT32 l_available_data; + + /* preconditions */ + assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + + l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; + l_available_data = l_tile_size; + l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; + + l_nb_bytes_written = 0; + if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written, + l_available_data, p_stream, p_manager)) { + return OPJ_FALSE; + } + l_current_data += l_nb_bytes_written; + l_available_data -= l_nb_bytes_written; + + l_nb_bytes_written = 0; + if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written, + l_available_data, p_stream, p_manager)) { + return OPJ_FALSE; + } + + l_available_data -= l_nb_bytes_written; + l_nb_bytes_written = l_tile_size - l_available_data; + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, + l_nb_bytes_written, p_manager) != l_nb_bytes_written) { + return OPJ_FALSE; + } + + ++p_j2k->m_current_tile_number; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + /* DEVELOPER CORNER, insert your custom procedures */ + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_eoc, p_manager)) { + return OPJ_FALSE; + } + + if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz) || OPJ_IS_IMF(p_j2k->m_cp.rsiz)) { + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) { + return OPJ_FALSE; + } + } + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_epc, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_end_encoding, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) { + return OPJ_FALSE; + } + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, + (opj_procedure)opj_j2k_build_encoder, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, + (opj_procedure)opj_j2k_encoding_validation, p_manager)) { + return OPJ_FALSE; + } + + /* DEVELOPER CORNER, add your custom validation procedure */ + if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list, + (opj_procedure)opj_j2k_mct_validation, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_init_info, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_soc, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_siz, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_cod, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_qcd, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_all_coc, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_all_qcc, p_manager)) { + return OPJ_FALSE; + } + + if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz) || OPJ_IS_IMF(p_j2k->m_cp.rsiz)) { + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_tlm, p_manager)) { + return OPJ_FALSE; + } + + if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) { + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_poc, p_manager)) { + return OPJ_FALSE; + } + } + } + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_regions, p_manager)) { + return OPJ_FALSE; + } + + if (p_j2k->m_cp.comment != 00) { + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_com, p_manager)) { + return OPJ_FALSE; + } + } + + /* DEVELOPER CORNER, insert your custom procedures */ + if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) == + (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) { + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) { + return OPJ_FALSE; + } + } + /* End of Developer Corner */ + + if (p_j2k->cstr_index) { + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_get_end_header, p_manager)) { + return OPJ_FALSE; + } + } + + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_create_tcd, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list, + (opj_procedure)opj_j2k_update_rates, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_nb_bytes_written = 0; + OPJ_UINT32 l_current_nb_bytes_written; + OPJ_BYTE * l_begin_data = 00; + + opj_tcd_t * l_tcd = 00; + opj_cp_t * l_cp = 00; + + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + + l_tcd->cur_pino = 0; + + /*Get number of tile parts*/ + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; + + /* INDEX >> */ + /* << INDEX */ + + l_current_nb_bytes_written = 0; + l_begin_data = p_data; + if (! opj_j2k_write_sot(p_j2k, p_data, total_data_size, + &l_current_nb_bytes_written, p_stream, + p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + + if (!OPJ_IS_CINEMA(l_cp->rsiz)) { +#if 0 + for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { + l_current_nb_bytes_written = 0; + opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, + p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + + l_current_nb_bytes_written = 0; + opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written, + p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + } +#endif + if (l_cp->tcps[p_j2k->m_current_tile_number].POC) { + l_current_nb_bytes_written = 0; + opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written, + p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + } + } + + l_current_nb_bytes_written = 0; + if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, + total_data_size, p_stream, p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + * p_data_written = l_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6, l_nb_bytes_written, + 4); /* PSOT */ + + if (OPJ_IS_CINEMA(l_cp->rsiz) || OPJ_IS_IMF(l_cp->rsiz)) { + opj_j2k_update_tlm(p_j2k, l_nb_bytes_written); + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 tilepartno = 0; + OPJ_UINT32 l_nb_bytes_written = 0; + OPJ_UINT32 l_current_nb_bytes_written; + OPJ_UINT32 l_part_tile_size; + OPJ_UINT32 tot_num_tp; + OPJ_UINT32 pino; + + OPJ_BYTE * l_begin_data; + opj_tcp_t *l_tcp = 00; + opj_tcd_t * l_tcd = 00; + opj_cp_t * l_cp = 00; + + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + + /*Get number of tile parts*/ + tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number); + + /* start writing remaining tile parts */ + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; + l_current_nb_bytes_written = 0; + l_part_tile_size = 0; + l_begin_data = p_data; + + if (! opj_j2k_write_sot(p_j2k, p_data, + total_data_size, + &l_current_nb_bytes_written, + p_stream, + p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; + + l_current_nb_bytes_written = 0; + if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, + total_data_size, p_stream, p_manager)) { + return OPJ_FALSE; + } + + p_data += l_current_nb_bytes_written; + l_nb_bytes_written += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6, l_part_tile_size, + 4); /* PSOT */ + + if (OPJ_IS_CINEMA(l_cp->rsiz) || OPJ_IS_IMF(l_cp->rsiz)) { + opj_j2k_update_tlm(p_j2k, l_part_tile_size); + } + + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + } + + for (pino = 1; pino <= l_tcp->numpocs; ++pino) { + l_tcd->cur_pino = pino; + + /*Get number of tile parts*/ + tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number); + for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; + l_current_nb_bytes_written = 0; + l_part_tile_size = 0; + l_begin_data = p_data; + + if (! opj_j2k_write_sot(p_j2k, p_data, + total_data_size, + &l_current_nb_bytes_written, p_stream, + p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; + + l_current_nb_bytes_written = 0; + + if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written, + total_data_size, p_stream, p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6, l_part_tile_size, + 4); /* PSOT */ + + if (OPJ_IS_CINEMA(l_cp->rsiz) || OPJ_IS_IMF(l_cp->rsiz)) { + opj_j2k_update_tlm(p_j2k, l_part_tile_size); + } + + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + } + } + + *p_data_written = l_nb_bytes_written; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_tlm_size; + OPJ_OFF_T l_tlm_position, l_current_position; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts; + l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; + l_current_position = opj_stream_tell(p_stream); + + if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) { + return OPJ_FALSE; + } + + if (opj_stream_write_data(p_stream, + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size, + p_manager) != l_tlm_size) { + return OPJ_FALSE; + } + + if (! opj_stream_seek(p_stream, l_current_position, p_manager)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_stream); + OPJ_UNUSED(p_manager); + + opj_tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + + if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { + opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; + } + + if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; + } + + p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; + + return OPJ_TRUE; +} + +/** + * Destroys the memory associated with the decoding of headers. + */ +static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_stream); + OPJ_UNUSED(p_manager); + + if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; + } + + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + opj_codestream_info_t * l_cstr_info = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + (void)l_cstr_info; + + OPJ_UNUSED(p_stream); + + /* TODO mergeV2: check this part which use cstr_info */ + /*l_cstr_info = p_j2k->cstr_info; + + if (l_cstr_info) { + OPJ_UINT32 compno; + l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t)); + + l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; + l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; + + l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; + + l_cstr_info->tw = p_j2k->m_cp.tw; + l_cstr_info->th = p_j2k->m_cp.th; + + l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ + /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ + /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ + /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ + + /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; + + l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; + + l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); + + for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { + l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; + } + + l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ + + /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ + + /*l_cstr_info->maxmarknum = 100; + l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); + l_cstr_info->marknum = 0; + }*/ + + return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp), + &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image, + p_manager); +} + +/** + * Creates a tile-coder encoder. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + OPJ_UNUSED(p_stream); + + p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); + + if (! p_j2k->m_tcd) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); + return OPJ_FALSE; + } + + if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp, + p_j2k->m_tp)) { + opj_tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); + return OPJ_FALSE; + } else { + OPJ_UINT32 j; + /* Allocate data */ + for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) { + opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; + + if (! opj_alloc_tile_component_data(l_tilec)) { + opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data."); + return OPJ_FALSE; + } + } + + /* now copy data into the tile component */ + if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) { + opj_event_msg(p_manager, EVT_ERROR, + "Size mismatch between tile data and sent data."); + return OPJ_FALSE; + } + if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); + return OPJ_FALSE; + } + } + + return OPJ_TRUE; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/j2k.h b/cpp/3rd_party/openjpeg/openjp2/j2k.h new file mode 100644 index 0000000000..9eb50b50da --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/j2k.h @@ -0,0 +1,900 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France + * Copyright (c) 2012, CS Systemes d'Information, France + * + * 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 OPJ_J2K_H +#define OPJ_J2K_H +/** +@file j2k.h +@brief The JPEG-2000 Codestream Reader/Writer (J2K) + +The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data. +*/ + +/** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ +/*@{*/ + +#define J2K_CP_CSTY_PRT 0x01 +#define J2K_CP_CSTY_SOP 0x02 +#define J2K_CP_CSTY_EPH 0x04 +#define J2K_CCP_CSTY_PRT 0x01 +#define J2K_CCP_CBLKSTY_LAZY 0x01 /**< Selective arithmetic coding bypass */ +#define J2K_CCP_CBLKSTY_RESET 0x02 /**< Reset context probabilities on coding pass boundaries */ +#define J2K_CCP_CBLKSTY_TERMALL 0x04 /**< Termination on each coding pass */ +#define J2K_CCP_CBLKSTY_VSC 0x08 /**< Vertically stripe causal context */ +#define J2K_CCP_CBLKSTY_PTERM 0x10 /**< Predictable termination */ +#define J2K_CCP_CBLKSTY_SEGSYM 0x20 /**< Segmentation symbols are used */ +#define J2K_CCP_QNTSTY_NOQNT 0 +#define J2K_CCP_QNTSTY_SIQNT 1 +#define J2K_CCP_QNTSTY_SEQNT 2 + +/* ----------------------------------------------------------------------- */ + +#define J2K_MS_SOC 0xff4f /**< SOC marker value */ +#define J2K_MS_SOT 0xff90 /**< SOT marker value */ +#define J2K_MS_SOD 0xff93 /**< SOD marker value */ +#define J2K_MS_EOC 0xffd9 /**< EOC marker value */ +#define J2K_MS_SIZ 0xff51 /**< SIZ marker value */ +#define J2K_MS_COD 0xff52 /**< COD marker value */ +#define J2K_MS_COC 0xff53 /**< COC marker value */ +#define J2K_MS_RGN 0xff5e /**< RGN marker value */ +#define J2K_MS_QCD 0xff5c /**< QCD marker value */ +#define J2K_MS_QCC 0xff5d /**< QCC marker value */ +#define J2K_MS_POC 0xff5f /**< POC marker value */ +#define J2K_MS_TLM 0xff55 /**< TLM marker value */ +#define J2K_MS_PLM 0xff57 /**< PLM marker value */ +#define J2K_MS_PLT 0xff58 /**< PLT marker value */ +#define J2K_MS_PPM 0xff60 /**< PPM marker value */ +#define J2K_MS_PPT 0xff61 /**< PPT marker value */ +#define J2K_MS_SOP 0xff91 /**< SOP marker value */ +#define J2K_MS_EPH 0xff92 /**< EPH marker value */ +#define J2K_MS_CRG 0xff63 /**< CRG marker value */ +#define J2K_MS_COM 0xff64 /**< COM marker value */ +#define J2K_MS_CBD 0xff78 /**< CBD marker value */ +#define J2K_MS_MCC 0xff75 /**< MCC marker value */ +#define J2K_MS_MCT 0xff74 /**< MCT marker value */ +#define J2K_MS_MCO 0xff77 /**< MCO marker value */ + +#define J2K_MS_UNK 0 /**< UNKNOWN marker value */ + +/* UniPG>> */ +#ifdef USE_JPWL +#define J2K_MS_EPC 0xff68 /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */ +#define J2K_MS_EPB 0xff66 /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */ +#define J2K_MS_ESD 0xff67 /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */ +#define J2K_MS_RED 0xff69 /**< RED marker value (Part 11: JPEG 2000 for Wireless) */ +#endif /* USE_JPWL */ +#ifdef USE_JPSEC +#define J2K_MS_SEC 0xff65 /**< SEC marker value (Part 8: Secure JPEG 2000) */ +#define J2K_MS_INSEC 0xff94 /**< INSEC marker value (Part 8: Secure JPEG 2000) */ +#endif /* USE_JPSEC */ +/* < Zppx not read yet */ + OPJ_UINT32 m_data_size; +} opj_ppx; + +/** +Tile coding parameters : +this structure is used to store coding/decoding parameters common to all +tiles (information like COD, COC in main header) +*/ +typedef struct opj_tcp { + /** coding style */ + OPJ_UINT32 csty; + /** progression order */ + OPJ_PROG_ORDER prg; + /** number of layers */ + OPJ_UINT32 numlayers; + OPJ_UINT32 num_layers_to_decode; + /** multi-component transform identifier */ + OPJ_UINT32 mct; + /** rates of layers */ + OPJ_FLOAT32 rates[100]; + /** number of progression order changes */ + OPJ_UINT32 numpocs; + /** progression order changes */ + opj_poc_t pocs[J2K_MAX_POCS]; + + /** number of ppt markers (reserved size) */ + OPJ_UINT32 ppt_markers_count; + /** ppt markers data (table indexed by Zppt) */ + opj_ppx* ppt_markers; + + /** packet header store there for future use in t2_decode_packet */ + OPJ_BYTE *ppt_data; + /** used to keep a track of the allocated memory */ + OPJ_BYTE *ppt_buffer; + /** Number of bytes stored inside ppt_data*/ + OPJ_UINT32 ppt_data_size; + /** size of ppt_data*/ + OPJ_UINT32 ppt_len; + /** add fixed_quality */ + OPJ_FLOAT32 distoratio[100]; + /** tile-component coding parameters */ + opj_tccp_t *tccps; + /** current tile part number or -1 if first time into this tile */ + OPJ_INT32 m_current_tile_part_number; + /** number of tile parts for the tile. */ + OPJ_UINT32 m_nb_tile_parts; + /** data for the tile */ + OPJ_BYTE * m_data; + /** size of data */ + OPJ_UINT32 m_data_size; + /** encoding norms */ + OPJ_FLOAT64 * mct_norms; + /** the mct decoding matrix */ + OPJ_FLOAT32 * m_mct_decoding_matrix; + /** the mct coding matrix */ + OPJ_FLOAT32 * m_mct_coding_matrix; + /** mct records */ + opj_mct_data_t * m_mct_records; + /** the number of mct records. */ + OPJ_UINT32 m_nb_mct_records; + /** the max number of mct records. */ + OPJ_UINT32 m_nb_max_mct_records; + /** mcc records */ + opj_simple_mcc_decorrelation_data_t * m_mcc_records; + /** the number of mct records. */ + OPJ_UINT32 m_nb_mcc_records; + /** the max number of mct records. */ + OPJ_UINT32 m_nb_max_mcc_records; + + + /***** FLAGS *******/ + /** If cod == 1 --> there was a COD marker for the present tile */ + OPJ_BITFIELD cod : 1; + /** If ppt == 1 --> there was a PPT marker for the present tile */ + OPJ_BITFIELD ppt : 1; + /** indicates if a POC marker has been used O:NO, 1:YES */ + OPJ_BITFIELD POC : 1; +} opj_tcp_t; + + + + +typedef struct opj_encoding_param { + /** Maximum rate for each component. If == 0, component size limitation is not considered */ + OPJ_UINT32 m_max_comp_size; + /** Position of tile part flag in progression order*/ + OPJ_INT32 m_tp_pos; + /** fixed layer */ + OPJ_INT32 *m_matrice; + /** Flag determining tile part generation*/ + OPJ_BYTE m_tp_flag; + /** allocation by rate/distortion */ + OPJ_BITFIELD m_disto_alloc : 1; + /** allocation by fixed layer */ + OPJ_BITFIELD m_fixed_alloc : 1; + /** add fixed_quality */ + OPJ_BITFIELD m_fixed_quality : 1; + /** Enabling Tile part generation*/ + OPJ_BITFIELD m_tp_on : 1; +} +opj_encoding_param_t; + +typedef struct opj_decoding_param { + /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */ + OPJ_UINT32 m_reduce; + /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */ + OPJ_UINT32 m_layer; +} +opj_decoding_param_t; + + +/** + * Coding parameters + */ +typedef struct opj_cp { + /** Size of the image in bits*/ + /*int img_size;*/ + /** Rsiz*/ + OPJ_UINT16 rsiz; + /** XTOsiz */ + OPJ_UINT32 tx0; /* MSD see norm */ + /** YTOsiz */ + OPJ_UINT32 ty0; /* MSD see norm */ + /** XTsiz */ + OPJ_UINT32 tdx; + /** YTsiz */ + OPJ_UINT32 tdy; + /** comment */ + OPJ_CHAR *comment; + /** number of tiles in width */ + OPJ_UINT32 tw; + /** number of tiles in height */ + OPJ_UINT32 th; + + /** number of ppm markers (reserved size) */ + OPJ_UINT32 ppm_markers_count; + /** ppm markers data (table indexed by Zppm) */ + opj_ppx* ppm_markers; + + /** packet header store there for future use in t2_decode_packet */ + OPJ_BYTE *ppm_data; + /** size of the ppm_data*/ + OPJ_UINT32 ppm_len; + /** size of the ppm_data*/ + OPJ_UINT32 ppm_data_read; + + OPJ_BYTE *ppm_data_current; + + /** packet header storage original buffer */ + OPJ_BYTE *ppm_buffer; + /** pointer remaining on the first byte of the first header if ppm is used */ + OPJ_BYTE *ppm_data_first; + /** Number of bytes actually stored inside the ppm_data */ + OPJ_UINT32 ppm_data_size; + /** use in case of multiple marker PPM (number of info already store) */ + OPJ_INT32 ppm_store; + /** use in case of multiple marker PPM (case on non-finished previous info) */ + OPJ_INT32 ppm_previous; + + /** tile coding parameters */ + opj_tcp_t *tcps; + + union { + opj_decoding_param_t m_dec; + opj_encoding_param_t m_enc; + } + m_specific_param; + + + /* UniPG>> */ +#ifdef USE_JPWL + /** enables writing of EPC in MH, thus activating JPWL */ + OPJ_BOOL epc_on; + /** enables writing of EPB, in case of activated JPWL */ + OPJ_BOOL epb_on; + /** enables writing of ESD, in case of activated JPWL */ + OPJ_BOOL esd_on; + /** enables writing of informative techniques of ESD, in case of activated JPWL */ + OPJ_BOOL info_on; + /** enables writing of RED, in case of activated JPWL */ + OPJ_BOOL red_on; + /** error protection method for MH (0,1,16,32,37-128) */ + int hprot_MH; + /** tile number of header protection specification (>=0) */ + int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS]; + /** error protection methods for TPHs (0,1,16,32,37-128) */ + int hprot_TPH[JPWL_MAX_NO_TILESPECS]; + /** tile number of packet protection specification (>=0) */ + int pprot_tileno[JPWL_MAX_NO_PACKSPECS]; + /** packet number of packet protection specification (>=0) */ + int pprot_packno[JPWL_MAX_NO_PACKSPECS]; + /** error protection methods for packets (0,1,16,32,37-128) */ + int pprot[JPWL_MAX_NO_PACKSPECS]; + /** enables writing of ESD, (0/2/4 bytes) */ + int sens_size; + /** sensitivity addressing size (0=auto/2/4 bytes) */ + int sens_addr; + /** sensitivity range (0-3) */ + int sens_range; + /** sensitivity method for MH (-1,0-7) */ + int sens_MH; + /** tile number of sensitivity specification (>=0) */ + int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS]; + /** sensitivity methods for TPHs (-1,0-7) */ + int sens_TPH[JPWL_MAX_NO_TILESPECS]; + /** enables JPWL correction at the decoder */ + OPJ_BOOL correct; + /** expected number of components at the decoder */ + int exp_comps; + /** maximum number of tiles at the decoder */ + OPJ_UINT32 max_tiles; +#endif /* USE_JPWL */ + + /******** FLAGS *********/ + /** if ppm == 1 --> there was a PPM marker*/ + OPJ_BITFIELD ppm : 1; + /** tells if the parameter is a coding or decoding one */ + OPJ_BITFIELD m_is_decoder : 1; + /** whether different bit depth or sign per component is allowed. Decoder only for ow */ + OPJ_BITFIELD allow_different_bit_depth_sign : 1; + /* <cp. +@param j2k J2K decompressor handle +@param parameters decompression parameters +*/ +void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters); + +OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads); + +/** + * Creates a J2K compression structure + * + * @return Returns a handle to a J2K compressor if successful, returns NULL otherwise +*/ +opj_j2k_t* opj_j2k_create_compress(void); + + +OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k, + opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t * p_manager); + +/** +Converts an enum type progression order to string type +*/ +const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order); + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +/** + * Ends the decompression procedures and possibiliy add data to be read after the + * codestream. + */ +OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a jpeg2000 codestream header structure. + * + * @param p_stream the stream to read data from. + * @param p_j2k the jpeg2000 codec. + * @param p_image FIXME DOC + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream, + opj_j2k_t* p_j2k, + opj_image_t** p_image, + opj_event_mgr_t* p_manager); + + +/** + * Destroys a jpeg2000 codec. + * + * @param p_j2k the jpeg20000 structure to destroy. + */ +void opj_j2k_destroy(opj_j2k_t *p_j2k); + +/** + * Destroys a codestream index structure. + * + * @param p_cstr_ind the codestream index parameter to destroy. + */ +void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind); + +/** + * Decode tile data. + * @param p_j2k the jpeg2000 codec. + * @param p_tile_index + * @param p_data FIXME DOC + * @param p_data_size FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a tile header. + * @param p_j2k the jpeg2000 codec. + * @param p_tile_index FIXME DOC + * @param p_data_size FIXME DOC + * @param p_tile_x0 FIXME DOC + * @param p_tile_y0 FIXME DOC + * @param p_tile_x1 FIXME DOC + * @param p_tile_y1 FIXME DOC + * @param p_nb_comps FIXME DOC + * @param p_go_on FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + + +/** Sets the indices of the components to decode. + * + * @param p_j2k the jpeg2000 codec. + * @param numcomps Number of components to decode. + * @param comps_indices Array of num_compts indices (numbering starting at 0) + * corresponding to the components to decode. + * @param p_manager Event manager + * + * @return OPJ_TRUE in case of success. + */ +OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k, + OPJ_UINT32 numcomps, + const OPJ_UINT32* comps_indices, + opj_event_mgr_t * p_manager); + +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_j2k the jpeg2000 codec. + * @param p_image FIXME DOC + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * @param p_manager the user event manager + * + * @return true if the area could be set. + */ +OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, + opj_event_mgr_t * p_manager); + +/** + * Creates a J2K decompression structure. + * + * @return a handle to a J2K decompressor if successful, NULL otherwise. + */ +opj_j2k_t* opj_j2k_create_decompress(void); + + +/** + * Dump some elements from the J2K decompression structure . + * + *@param p_j2k the jpeg2000 codec. + *@param flag flag to describe what elements are dump. + *@param out_stream output stream where dump the elements. + * +*/ +void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream); + + + +/** + * Dump an image header structure. + * + *@param image the image header to dump. + *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function + *@param out_stream output stream where dump the elements. + */ +void j2k_dump_image_header(opj_image_t* image, OPJ_BOOL dev_dump_flag, + FILE* out_stream); + +/** + * Dump a component image header structure. + * + *@param comp the component image header to dump. + *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function + *@param out_stream output stream where dump the elements. + */ +void j2k_dump_image_comp_header(opj_image_comp_t* comp, OPJ_BOOL dev_dump_flag, + FILE* out_stream); + +/** + * Get the codestream info from a JPEG2000 codec. + * + *@param p_j2k the component image header to dump. + * + *@return the codestream information extract from the jpg2000 codec + */ +opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k); + +/** + * Get the codestream index from a JPEG2000 codec. + * + *@param p_j2k the component image header to dump. + * + *@return the codestream index extract from the jpg2000 codec + */ +opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k); + +/** + * Decode an image from a JPEG-2000 codestream + * @param j2k J2K decompressor handle + * @param p_stream FIXME DOC + * @param p_image FIXME DOC + * @param p_manager FIXME DOC + * @return FIXME DOC +*/ +OPJ_BOOL opj_j2k_decode(opj_j2k_t *j2k, + opj_stream_private_t *p_stream, + opj_image_t *p_image, + opj_event_mgr_t *p_manager); + + +OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager, + OPJ_UINT32 tile_index); + +OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, + OPJ_UINT32 res_factor, + opj_event_mgr_t * p_manager); + +/** + * Specify extra options for the encoder. + * + * @param p_j2k the jpeg2000 codec. + * @param p_options options + * @param p_manager the user event manager + * + * @see opj_encoder_set_extra_options() for more details. + */ +OPJ_BOOL opj_j2k_encoder_set_extra_options( + opj_j2k_t *p_j2k, + const char* const* p_options, + opj_event_mgr_t * p_manager); + +/** + * Writes a tile. + * @param p_j2k the jpeg2000 codec. + * @param p_tile_index FIXME DOC + * @param p_data FIXME DOC + * @param p_data_size FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Encodes an image into a JPEG-2000 codestream + */ +OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Starts a compression scheme, i.e. validates the codec parameters, writes the header. + * + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream object. + * @param p_image FIXME DOC + * @param p_manager the user event manager. + * + * @return true if the codec is valid. + */ +OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager); + +/** + * Ends the compression procedures and possibiliy add data to be read after the + * codestream. + */ +OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image); + + +#endif /* OPJ_J2K_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/jp2.c b/cpp/3rd_party/openjpeg/openjp2/jp2.c new file mode 100644 index 0000000000..7c065ba742 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/jp2.c @@ -0,0 +1,3443 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2010-2011, Kaori Hagihara + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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" + +/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */ +/*@{*/ + +#define OPJ_BOX_SIZE 1024 + +#define OPJ_UNUSED(x) (void)x + +/** @name Local static functions */ +/*@{*/ + +/*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/ + +/** + * Reads a IHDR box - Image Header box + * + * @param p_image_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_image_header_size the size of the image header + * @param p_manager the user event manager. + * + * @return true if the image header is valid, false else. + */ +static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2, + OPJ_BYTE *p_image_header_data, + OPJ_UINT32 p_image_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the Image Header box - Image Header box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written); + +/** + * Writes the Bit per Component box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written); + +/** + * Reads a Bit per Component box. + * + * @param p_bpc_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_bpc_header_size the size of the bpc header + * @param p_manager the user event manager. + * + * @return true if the bpc header is valid, false else. + */ +static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2, + OPJ_BYTE * p_bpc_header_data, + OPJ_UINT32 p_bpc_header_size, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2, + OPJ_BYTE * p_cdef_header_data, + OPJ_UINT32 p_cdef_header_size, + opj_event_mgr_t * p_manager); + +static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, + opj_event_mgr_t *); + +/** + * Writes the Channel Definition box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. + */ +static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written); + +/** + * Writes the Colour Specification box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written); + +/** + * Writes a FTYP box - File type box + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager the user event manager. + * + * @return true if writing was successful. + */ +static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Reads a a FTYP box - File type box + * + * @param p_header_data the data contained in the FTYP box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the FTYP box. + * @param p_manager the user event manager. + * + * @return true if the FTYP box is valid. + */ +static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager); + +/** + * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param p_header_data the data contained in the file header box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the file header box. + * @param p_manager the user event manager. + * + * @return true if the JP2 Header box was successfully recognized. +*/ +static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2, + OPJ_BYTE *p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param jp2 the jpeg2000 file codec. + * @param stream the stream to write data to. + * @param p_manager user event manager. + * + * @return true if writing was successful. + */ +static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager); + +/** + * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writing was successful. +*/ +static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +#ifdef USE_JPIP +/** + * Write index Finder box + * @param cio the stream to write to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. +*/ +static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Write index Finder box + * @param cio the stream to write to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + */ +static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Write file Index (superbox) + * @param cio the stream to write to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + */ +static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); +#endif /* USE_JPIP */ + +/** + * Reads a jpeg2000 file signature box. + * + * @param p_header_data the data contained in the signature box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the signature box. + * @param p_manager the user event manager. + * + * @return true if the file signature box is valid. + */ +static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); + +/** + * Writes a jpeg2000 file signature box. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager the user event manager. + * + * @return true if writing was successful. + */ +static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** +Apply collected palette data +@param image Image. +@param color Collector for profile, cdef and pclr data. +@param p_manager the user event manager. +@return true in case of success +*/ +static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image, + opj_jp2_color_t *color, + opj_event_mgr_t * p_manager); + +static void opj_jp2_free_pclr(opj_jp2_color_t *color); + +/** + * Collect palette data + * + * @param jp2 JP2 handle + * @param p_pclr_header_data FIXME DOC + * @param p_pclr_header_size FIXME DOC + * @param p_manager + * + * @return Returns true if successful, returns false otherwise +*/ +static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2, + OPJ_BYTE * p_pclr_header_data, + OPJ_UINT32 p_pclr_header_size, + opj_event_mgr_t * p_manager); + +/** + * Collect component mapping data + * + * @param jp2 JP2 handle + * @param p_cmap_header_data FIXME DOC + * @param p_cmap_header_size FIXME DOC + * @param p_manager FIXME DOC + * + * @return Returns true if successful, returns false otherwise +*/ + +static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2, + OPJ_BYTE * p_cmap_header_data, + OPJ_UINT32 p_cmap_header_size, + opj_event_mgr_t * p_manager); + +/** + * Reads the Color Specification box. + * + * @param p_colr_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_colr_header_size the size of the color header + * @param p_manager the user event manager. + * + * @return true if the bpc header is valid, false else. +*/ +static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2, + OPJ_BYTE * p_colr_header_data, + OPJ_UINT32 p_colr_header_size, + opj_event_mgr_t * p_manager); + +/*@}*/ + +/*@}*/ + +/** + * Sets up the procedures to do on writing header after the codestream. + * Developpers wanting to extend the library can add their own writing procedures. + */ +static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager); + +/** + * Sets up the procedures to do on reading header after the codestream. + * Developpers wanting to extend the library can add their own writing procedures. + */ +static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager); + +/** + * Reads a jpeg2000 file header structure. + * + * @param jp2 the jpeg2000 file header structure. + * @param stream the stream to read data from. + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager); + +/** + * Executes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param jp2 the jpeg2000 file codec to execute the procedures on. + * @param stream the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager); + +/** + * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. + * + * @param cio the input stream to read data from. + * @param box the box structure to fill. + * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). + * @param p_manager user event manager. + * + * @return true if the box is recognized, false otherwise +*/ +static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box, + OPJ_UINT32 * p_number_bytes_read, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager); + +/** + * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures. + */ +static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager); + +static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Finds the image execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or NULL if it could not be found. + */ +static const opj_jp2_header_handler_t * opj_jp2_img_find_handler( + OPJ_UINT32 p_id); + +/** + * Finds the execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or NULL if it could not be found. + */ +static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id); + +static const opj_jp2_header_handler_t jp2_header [] = { + {JP2_JP, opj_jp2_read_jp}, + {JP2_FTYP, opj_jp2_read_ftyp}, + {JP2_JP2H, opj_jp2_read_jp2h} +}; + +static const opj_jp2_header_handler_t jp2_img_header [] = { + {JP2_IHDR, opj_jp2_read_ihdr}, + {JP2_COLR, opj_jp2_read_colr}, + {JP2_BPCC, opj_jp2_read_bpcc}, + {JP2_PCLR, opj_jp2_read_pclr}, + {JP2_CMAP, opj_jp2_read_cmap}, + {JP2_CDEF, opj_jp2_read_cdef} + +}; + +/** + * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string + * + * @param box the box structure to fill. + * @param p_data the character string to read data from. + * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). + * @param p_box_max_size the maximum number of bytes in the box. + * @param p_manager FIXME DOC + * + * @return true if the box is recognized, false otherwise +*/ +static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_number_bytes_read, + OPJ_UINT32 p_box_max_size, + opj_event_mgr_t * p_manager); + +/** + * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager); + +/** + * Sets up the procedures to do on reading header. + * Developpers wanting to extend the library can add their own writing procedures. + */ +static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager); + +/* ----------------------------------------------------------------------- */ +static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box, + OPJ_UINT32 * p_number_bytes_read, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + /* read header from file */ + OPJ_BYTE l_data_header [8]; + + /* preconditions */ + assert(cio != 00); + assert(box != 00); + assert(p_number_bytes_read != 00); + assert(p_manager != 00); + + *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, l_data_header, 8, + p_manager); + if (*p_number_bytes_read != 8) { + return OPJ_FALSE; + } + + /* process read data */ + opj_read_bytes(l_data_header, &(box->length), 4); + opj_read_bytes(l_data_header + 4, &(box->type), 4); + + if (box->length == 0) { /* last box */ + const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio); + if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) { + opj_event_msg(p_manager, EVT_ERROR, + "Cannot handle box sizes higher than 2^32\n"); + return OPJ_FALSE; + } + box->length = (OPJ_UINT32)bleft + 8U; + assert((OPJ_OFF_T)box->length == bleft + 8); + return OPJ_TRUE; + } + + /* do we have a "special very large box ?" */ + /* read then the XLBox */ + if (box->length == 1) { + OPJ_UINT32 l_xl_part_size; + + OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, + l_data_header, 8, p_manager); + if (l_nb_bytes_read != 8) { + if (l_nb_bytes_read > 0) { + *p_number_bytes_read += l_nb_bytes_read; + } + + return OPJ_FALSE; + } + + *p_number_bytes_read = 16; + opj_read_bytes(l_data_header, &l_xl_part_size, 4); + if (l_xl_part_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Cannot handle box sizes higher than 2^32\n"); + return OPJ_FALSE; + } + opj_read_bytes(l_data_header + 4, &(box->length), 4); + } + return OPJ_TRUE; +} + +#if 0 +static void jp2_write_url(opj_cio_t *cio, char *Idx_file) +{ + OPJ_UINT32 i; + opj_jp2_box_t box; + + box.init_pos = cio_tell(cio); + cio_skip(cio, 4); + cio_write(cio, JP2_URL, 4); /* DBTL */ + cio_write(cio, 0, 1); /* VERS */ + cio_write(cio, 0, 3); /* FLAG */ + + if (Idx_file) { + for (i = 0; i < strlen(Idx_file); i++) { + cio_write(cio, Idx_file[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); +} +#endif + +static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2, + OPJ_BYTE *p_image_header_data, + OPJ_UINT32 p_image_header_size, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_image_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + if (jp2->comps != NULL) { + opj_event_msg(p_manager, EVT_WARNING, + "Ignoring ihdr box. First ihdr box already read\n"); + return OPJ_TRUE; + } + + if (p_image_header_size != 14) { + opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_image_header_data, &(jp2->h), 4); /* HEIGHT */ + p_image_header_data += 4; + opj_read_bytes(p_image_header_data, &(jp2->w), 4); /* WIDTH */ + p_image_header_data += 4; + opj_read_bytes(p_image_header_data, &(jp2->numcomps), 2); /* NC */ + p_image_header_data += 2; + + if (jp2->h < 1 || jp2->w < 1 || jp2->numcomps < 1) { + opj_event_msg(p_manager, EVT_ERROR, + "Wrong values for: w(%d) h(%d) numcomps(%d) (ihdr)\n", + jp2->w, jp2->h, jp2->numcomps); + return OPJ_FALSE; + } + if ((jp2->numcomps - 1U) >= + 16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */ + opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n"); + return OPJ_FALSE; + } + + /* allocate memory for components */ + jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps, + sizeof(opj_jp2_comps_t)); + if (jp2->comps == 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to handle image header (ihdr)\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_image_header_data, &(jp2->bpc), 1); /* BPC */ + ++ p_image_header_data; + + opj_read_bytes(p_image_header_data, &(jp2->C), 1); /* C */ + ++ p_image_header_data; + + /* Should be equal to 7 cf. chapter about image header box of the norm */ + if (jp2->C != 7) { + opj_event_msg(p_manager, EVT_INFO, + "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", + jp2->C); + } + + opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1); /* UnkC */ + ++ p_image_header_data; + opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */ + ++ p_image_header_data; + + jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255); + jp2->j2k->ihdr_w = jp2->w; + jp2->j2k->ihdr_h = jp2->h; + jp2->has_ihdr = 1; + + return OPJ_TRUE; +} + +static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written + ) +{ + OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr; + + /* preconditions */ + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + + /* default image header is 22 bytes wide */ + l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22); + if (l_ihdr_data == 00) { + return 00; + } + + l_current_ihdr_ptr = l_ihdr_data; + + opj_write_bytes(l_current_ihdr_ptr, 22, 4); /* write box size */ + l_current_ihdr_ptr += 4; + + opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4); /* IHDR */ + l_current_ihdr_ptr += 4; + + opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4); /* HEIGHT */ + l_current_ihdr_ptr += 4; + + opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */ + l_current_ihdr_ptr += 4; + + opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */ + l_current_ihdr_ptr += 2; + + opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */ + ++l_current_ihdr_ptr; + + opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */ + ++l_current_ihdr_ptr; + + opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, + 1); /* UnkC, colorspace unknown */ + ++l_current_ihdr_ptr; + + opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, + 1); /* IPR, no intellectual property */ + ++l_current_ihdr_ptr; + + *p_nb_bytes_written = 22; + + return l_ihdr_data; +} + +static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written + ) +{ + OPJ_UINT32 i; + /* room for 8 bytes for box and 1 byte for each component */ + OPJ_UINT32 l_bpcc_size; + OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr; + + /* preconditions */ + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + l_bpcc_size = 8 + jp2->numcomps; + + l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size); + if (l_bpcc_data == 00) { + return 00; + } + + l_current_bpcc_ptr = l_bpcc_data; + + opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size, + 4); /* write box size */ + l_current_bpcc_ptr += 4; + + opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4); /* BPCC */ + l_current_bpcc_ptr += 4; + + for (i = 0; i < jp2->numcomps; ++i) { + opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, + 1); /* write each component information */ + ++l_current_bpcc_ptr; + } + + *p_nb_bytes_written = l_bpcc_size; + + return l_bpcc_data; +} + +static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2, + OPJ_BYTE * p_bpc_header_data, + OPJ_UINT32 p_bpc_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i; + + /* preconditions */ + assert(p_bpc_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + + if (jp2->bpc != 255) { + opj_event_msg(p_manager, EVT_WARNING, + "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n", + jp2->bpc); + } + + /* and length is relevant */ + if (p_bpc_header_size != jp2->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n"); + return OPJ_FALSE; + } + + /* read info for each component */ + for (i = 0; i < jp2->numcomps; ++i) { + opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc, + 1); /* read each BPCC component */ + ++p_bpc_header_data; + } + + return OPJ_TRUE; +} +static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written) +{ + /* room for 8 bytes for box, 2 for n */ + OPJ_UINT32 l_cdef_size = 10; + OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr; + OPJ_UINT32 l_value; + OPJ_UINT16 i; + + /* preconditions */ + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + assert(jp2->color.jp2_cdef != 00); + assert(jp2->color.jp2_cdef->info != 00); + assert(jp2->color.jp2_cdef->n > 0U); + + l_cdef_size += 6U * jp2->color.jp2_cdef->n; + + l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size); + if (l_cdef_data == 00) { + return 00; + } + + l_current_cdef_ptr = l_cdef_data; + + opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4); /* write box size */ + l_current_cdef_ptr += 4; + + opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4); /* BPCC */ + l_current_cdef_ptr += 4; + + l_value = jp2->color.jp2_cdef->n; + opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* N */ + l_current_cdef_ptr += 2; + + for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) { + l_value = jp2->color.jp2_cdef->info[i].cn; + opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Cni */ + l_current_cdef_ptr += 2; + l_value = jp2->color.jp2_cdef->info[i].typ; + opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Typi */ + l_current_cdef_ptr += 2; + l_value = jp2->color.jp2_cdef->info[i].asoc; + opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Asoci */ + l_current_cdef_ptr += 2; + } + *p_nb_bytes_written = l_cdef_size; + + return l_cdef_data; +} + +static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2, + OPJ_UINT32 * p_nb_bytes_written + ) +{ + /* room for 8 bytes for box 3 for common data and variable upon profile*/ + OPJ_UINT32 l_colr_size = 11; + OPJ_BYTE * l_colr_data, * l_current_colr_ptr; + + /* preconditions */ + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + assert(jp2->meth == 1 || jp2->meth == 2); + + switch (jp2->meth) { + case 1 : + l_colr_size += 4; /* EnumCS */ + break; + case 2 : + assert(jp2->color.icc_profile_len); /* ICC profile */ + l_colr_size += jp2->color.icc_profile_len; + break; + default : + return 00; + } + + l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size); + if (l_colr_data == 00) { + return 00; + } + + l_current_colr_ptr = l_colr_data; + + opj_write_bytes(l_current_colr_ptr, l_colr_size, + 4); /* write box size */ + l_current_colr_ptr += 4; + + opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4); /* BPCC */ + l_current_colr_ptr += 4; + + opj_write_bytes(l_current_colr_ptr, jp2->meth, 1); /* METH */ + ++l_current_colr_ptr; + + opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1); /* PRECEDENCE */ + ++l_current_colr_ptr; + + opj_write_bytes(l_current_colr_ptr, jp2->approx, 1); /* APPROX */ + ++l_current_colr_ptr; + + if (jp2->meth == + 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */ + opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4); + } /* EnumCS */ + else { + if (jp2->meth == 2) { /* ICC profile */ + OPJ_UINT32 i; + for (i = 0; i < jp2->color.icc_profile_len; ++i) { + opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1); + ++l_current_colr_ptr; + } + } + } + + *p_nb_bytes_written = l_colr_size; + + return l_colr_data; +} + +static void opj_jp2_free_pclr(opj_jp2_color_t *color) +{ + opj_free(color->jp2_pclr->channel_sign); + opj_free(color->jp2_pclr->channel_size); + opj_free(color->jp2_pclr->entries); + + if (color->jp2_pclr->cmap) { + opj_free(color->jp2_pclr->cmap); + } + + opj_free(color->jp2_pclr); + color->jp2_pclr = NULL; +} + +static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, + opj_event_mgr_t *p_manager) +{ + OPJ_UINT16 i; + + /* testcase 4149.pdf.SIGSEGV.cf7.3501 */ + if (color->jp2_cdef) { + opj_jp2_cdef_info_t *info = color->jp2_cdef->info; + OPJ_UINT16 n = color->jp2_cdef->n; + OPJ_UINT32 nr_channels = + image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */ + + /* cdef applies to cmap channels if any */ + if (color->jp2_pclr && color->jp2_pclr->cmap) { + nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels; + } + + for (i = 0; i < n; i++) { + if (info[i].cn >= nr_channels) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", + info[i].cn, nr_channels); + return OPJ_FALSE; + } + if (info[i].asoc == 65535U) { + continue; + } + + if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", + info[i].asoc - 1, nr_channels); + return OPJ_FALSE; + } + } + + /* issue 397 */ + /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */ + while (nr_channels > 0) { + for (i = 0; i < n; ++i) { + if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) { + break; + } + } + if (i == n) { + opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n"); + return OPJ_FALSE; + } + --nr_channels; + } + } + + /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and + 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */ + if (color->jp2_pclr && color->jp2_pclr->cmap) { + OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels; + opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap; + OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE; + + /* verify that all original components match an existing one */ + for (i = 0; i < nr_channels; i++) { + if (cmap[i].cmp >= image->numcomps) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", + cmap[i].cmp, image->numcomps); + is_sane = OPJ_FALSE; + } + } + + pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL)); + if (!pcol_usage) { + opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n"); + return OPJ_FALSE; + } + /* verify that no component is targeted more than once */ + for (i = 0; i < nr_channels; i++) { + OPJ_BYTE mtyp = cmap[i].mtyp; + OPJ_BYTE pcol = cmap[i].pcol; + /* See ISO 15444-1 Table I.14 – MTYPi field values */ + if (mtyp != 0 && mtyp != 1) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid value for cmap[%d].mtyp = %d.\n", i, + mtyp); + is_sane = OPJ_FALSE; + } else if (pcol >= nr_channels) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid component/palette index for direct mapping %d.\n", pcol); + is_sane = OPJ_FALSE; + } else if (pcol_usage[pcol] && mtyp == 1) { + opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol); + is_sane = OPJ_FALSE; + } else if (mtyp == 0 && pcol != 0) { + /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then + * the value of this field shall be 0. */ + opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, + pcol); + is_sane = OPJ_FALSE; + } else if (mtyp == 1 && pcol != i) { + /* OpenJPEG implementation limitation. See assert(i == pcol); */ + /* in opj_jp2_apply_pclr() */ + opj_event_msg(p_manager, EVT_ERROR, + "Implementation limitation: for palette mapping, " + "pcol[%d] should be equal to %d, but is equal " + "to %d.\n", i, i, pcol); + is_sane = OPJ_FALSE; + } else { + pcol_usage[pcol] = OPJ_TRUE; + } + } + /* verify that all components are targeted at least once */ + for (i = 0; i < nr_channels; i++) { + if (!pcol_usage[i] && cmap[i].mtyp != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", + i); + is_sane = OPJ_FALSE; + } + } + /* Issue 235/447 weird cmap */ + if (1 && is_sane && (image->numcomps == 1U)) { + for (i = 0; i < nr_channels; i++) { + if (!pcol_usage[i]) { + is_sane = 0U; + opj_event_msg(p_manager, EVT_WARNING, + "Component mapping seems wrong. Trying to correct.\n"); + break; + } + } + if (!is_sane) { + is_sane = OPJ_TRUE; + for (i = 0; i < nr_channels; i++) { + cmap[i].mtyp = 1U; + cmap[i].pcol = (OPJ_BYTE) i; + } + } + } + opj_free(pcol_usage); + if (!is_sane) { + return OPJ_FALSE; + } + } + + return OPJ_TRUE; +} + +/* file9.jp2 */ +static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image, + opj_jp2_color_t *color, + opj_event_mgr_t * p_manager) +{ + opj_image_comp_t *old_comps, *new_comps; + OPJ_BYTE *channel_size, *channel_sign; + OPJ_UINT32 *entries; + opj_jp2_cmap_comp_t *cmap; + OPJ_INT32 *src, *dst; + OPJ_UINT32 j, max; + OPJ_UINT16 i, nr_channels, cmp, pcol; + OPJ_INT32 k, top_k; + + channel_size = color->jp2_pclr->channel_size; + channel_sign = color->jp2_pclr->channel_sign; + entries = color->jp2_pclr->entries; + cmap = color->jp2_pclr->cmap; + nr_channels = color->jp2_pclr->nr_channels; + + for (i = 0; i < nr_channels; ++i) { + /* Palette mapping: */ + cmp = cmap[i].cmp; + if (image->comps[cmp].data == NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "image->comps[%d].data == NULL in opj_jp2_apply_pclr().\n", i); + return OPJ_FALSE; + } + } + + old_comps = image->comps; + new_comps = (opj_image_comp_t*) + opj_malloc(nr_channels * sizeof(opj_image_comp_t)); + if (!new_comps) { + opj_event_msg(p_manager, EVT_ERROR, + "Memory allocation failure in opj_jp2_apply_pclr().\n"); + return OPJ_FALSE; + } + for (i = 0; i < nr_channels; ++i) { + pcol = cmap[i].pcol; + cmp = cmap[i].cmp; + + /* Direct use */ + if (cmap[i].mtyp == 0) { + assert(pcol == 0); + new_comps[i] = old_comps[cmp]; + } else { + assert(i == pcol); + new_comps[pcol] = old_comps[cmp]; + } + + /* Palette mapping: */ + new_comps[i].data = (OPJ_INT32*) + opj_image_data_alloc(sizeof(OPJ_INT32) * old_comps[cmp].w * old_comps[cmp].h); + if (!new_comps[i].data) { + while (i > 0) { + -- i; + opj_image_data_free(new_comps[i].data); + } + opj_free(new_comps); + opj_event_msg(p_manager, EVT_ERROR, + "Memory allocation failure in opj_jp2_apply_pclr().\n"); + return OPJ_FALSE; + } + new_comps[i].prec = channel_size[i]; + new_comps[i].sgnd = channel_sign[i]; + } + + top_k = color->jp2_pclr->nr_entries - 1; + + for (i = 0; i < nr_channels; ++i) { + /* Palette mapping: */ + cmp = cmap[i].cmp; + pcol = cmap[i].pcol; + src = old_comps[cmp].data; + assert(src); /* verified above */ + max = new_comps[pcol].w * new_comps[pcol].h; + + /* Direct use: */ + if (cmap[i].mtyp == 0) { + dst = new_comps[i].data; + assert(dst); + for (j = 0; j < max; ++j) { + dst[j] = src[j]; + } + } else { + assert(i == pcol); + dst = new_comps[pcol].data; + assert(dst); + for (j = 0; j < max; ++j) { + /* The index */ + if ((k = src[j]) < 0) { + k = 0; + } else if (k > top_k) { + k = top_k; + } + + /* The colour */ + dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol]; + } + } + } + + max = image->numcomps; + for (i = 0; i < max; ++i) { + if (old_comps[i].data) { + opj_image_data_free(old_comps[i].data); + } + } + + opj_free(old_comps); + image->comps = new_comps; + image->numcomps = nr_channels; + + return OPJ_TRUE; +}/* apply_pclr() */ + +static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2, + OPJ_BYTE * p_pclr_header_data, + OPJ_UINT32 p_pclr_header_size, + opj_event_mgr_t * p_manager + ) +{ + opj_jp2_pclr_t *jp2_pclr; + OPJ_BYTE *channel_size, *channel_sign; + OPJ_UINT32 *entries; + OPJ_UINT16 nr_entries, nr_channels; + OPJ_UINT16 i, j; + OPJ_UINT32 l_value; + OPJ_BYTE *orig_header_data = p_pclr_header_data; + + /* preconditions */ + assert(p_pclr_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + (void)p_pclr_header_size; + + if (jp2->color.jp2_pclr) { + return OPJ_FALSE; + } + + if (p_pclr_header_size < 3) { + return OPJ_FALSE; + } + + opj_read_bytes(p_pclr_header_data, &l_value, 2); /* NE */ + p_pclr_header_data += 2; + nr_entries = (OPJ_UINT16) l_value; + if ((nr_entries == 0U) || (nr_entries > 1024U)) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n", + (int)nr_entries); + return OPJ_FALSE; + } + + opj_read_bytes(p_pclr_header_data, &l_value, 1); /* NPC */ + ++p_pclr_header_data; + nr_channels = (OPJ_UINT16) l_value; + if (nr_channels == 0U) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid PCLR box. Reports 0 palette columns\n"); + return OPJ_FALSE; + } + + if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) { + return OPJ_FALSE; + } + + entries = (OPJ_UINT32*) opj_malloc(sizeof(OPJ_UINT32) * nr_channels * + nr_entries); + if (!entries) { + return OPJ_FALSE; + } + channel_size = (OPJ_BYTE*) opj_malloc(nr_channels); + if (!channel_size) { + opj_free(entries); + return OPJ_FALSE; + } + channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels); + if (!channel_sign) { + opj_free(entries); + opj_free(channel_size); + return OPJ_FALSE; + } + + jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t)); + if (!jp2_pclr) { + opj_free(entries); + opj_free(channel_size); + opj_free(channel_sign); + return OPJ_FALSE; + } + + jp2_pclr->channel_sign = channel_sign; + jp2_pclr->channel_size = channel_size; + jp2_pclr->entries = entries; + jp2_pclr->nr_entries = nr_entries; + jp2_pclr->nr_channels = (OPJ_BYTE) l_value; + jp2_pclr->cmap = NULL; + + jp2->color.jp2_pclr = jp2_pclr; + + for (i = 0; i < nr_channels; ++i) { + opj_read_bytes(p_pclr_header_data, &l_value, 1); /* Bi */ + ++p_pclr_header_data; + + channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1); + channel_sign[i] = (l_value & 0x80) ? 1 : 0; + } + + for (j = 0; j < nr_entries; ++j) { + for (i = 0; i < nr_channels; ++i) { + OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3); + + if (bytes_to_read > sizeof(OPJ_UINT32)) { + bytes_to_read = sizeof(OPJ_UINT32); + } + if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data - + orig_header_data) + (ptrdiff_t)bytes_to_read) { + return OPJ_FALSE; + } + + opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read); /* Cji */ + p_pclr_header_data += bytes_to_read; + *entries = (OPJ_UINT32) l_value; + entries++; + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2, + OPJ_BYTE * p_cmap_header_data, + OPJ_UINT32 p_cmap_header_size, + opj_event_mgr_t * p_manager + ) +{ + opj_jp2_cmap_comp_t *cmap; + OPJ_BYTE i, nr_channels; + OPJ_UINT32 l_value; + + /* preconditions */ + assert(jp2 != 00); + assert(p_cmap_header_data != 00); + assert(p_manager != 00); + (void)p_cmap_header_size; + + /* Need nr_channels: */ + if (jp2->color.jp2_pclr == NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "Need to read a PCLR box before the CMAP box.\n"); + return OPJ_FALSE; + } + + /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box + * inside a JP2 Header box' : + */ + if (jp2->color.jp2_pclr->cmap) { + opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n"); + return OPJ_FALSE; + } + + nr_channels = jp2->color.jp2_pclr->nr_channels; + if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) { + opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n"); + return OPJ_FALSE; + } + + cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof( + opj_jp2_cmap_comp_t)); + if (!cmap) { + return OPJ_FALSE; + } + + + for (i = 0; i < nr_channels; ++i) { + opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */ + p_cmap_header_data += 2; + cmap[i].cmp = (OPJ_UINT16) l_value; + + opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */ + ++p_cmap_header_data; + cmap[i].mtyp = (OPJ_BYTE) l_value; + + opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */ + ++p_cmap_header_data; + cmap[i].pcol = (OPJ_BYTE) l_value; + } + + jp2->color.jp2_pclr->cmap = cmap; + + return OPJ_TRUE; +} + +static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, + opj_event_mgr_t *manager) +{ + opj_jp2_cdef_info_t *info; + OPJ_UINT16 i, n, cn, asoc, acn; + + info = color->jp2_cdef->info; + n = color->jp2_cdef->n; + + for (i = 0; i < n; ++i) { + /* WATCH: acn = asoc - 1 ! */ + asoc = info[i].asoc; + cn = info[i].cn; + + if (cn >= image->numcomps) { + opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n", + cn, image->numcomps); + continue; + } + if (asoc == 0 || asoc == 65535) { + image->comps[cn].alpha = info[i].typ; + continue; + } + + acn = (OPJ_UINT16)(asoc - 1); + if (acn >= image->numcomps) { + opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n", + acn, image->numcomps); + continue; + } + + /* Swap only if color channel */ + if ((cn != acn) && (info[i].typ == 0)) { + opj_image_comp_t saved; + OPJ_UINT16 j; + + memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t)); + memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t)); + memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t)); + + /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */ + for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) { + if (info[j].cn == cn) { + info[j].cn = acn; + } else if (info[j].cn == acn) { + info[j].cn = cn; + } + /* asoc is related to color index. Do not update. */ + } + } + + image->comps[cn].alpha = info[i].typ; + } + + if (color->jp2_cdef->info) { + opj_free(color->jp2_cdef->info); + } + + opj_free(color->jp2_cdef); + color->jp2_cdef = NULL; + +}/* jp2_apply_cdef() */ + +static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2, + OPJ_BYTE * p_cdef_header_data, + OPJ_UINT32 p_cdef_header_size, + opj_event_mgr_t * p_manager + ) +{ + opj_jp2_cdef_info_t *cdef_info; + OPJ_UINT16 i; + OPJ_UINT32 l_value; + + /* preconditions */ + assert(jp2 != 00); + assert(p_cdef_header_data != 00); + assert(p_manager != 00); + (void)p_cdef_header_size; + + /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box + * inside a JP2 Header box.'*/ + if (jp2->color.jp2_cdef) { + return OPJ_FALSE; + } + + if (p_cdef_header_size < 2) { + opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_cdef_header_data, &l_value, 2); /* N */ + p_cdef_header_data += 2; + + if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */ + opj_event_msg(p_manager, EVT_ERROR, + "Number of channel description is equal to zero in CDEF box.\n"); + return OPJ_FALSE; + } + + if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) { + opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n"); + return OPJ_FALSE; + } + + cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof( + opj_jp2_cdef_info_t)); + if (!cdef_info) { + return OPJ_FALSE; + } + + jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t)); + if (!jp2->color.jp2_cdef) { + opj_free(cdef_info); + return OPJ_FALSE; + } + jp2->color.jp2_cdef->info = cdef_info; + jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value; + + for (i = 0; i < jp2->color.jp2_cdef->n; ++i) { + opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */ + p_cdef_header_data += 2; + cdef_info[i].cn = (OPJ_UINT16) l_value; + + opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */ + p_cdef_header_data += 2; + cdef_info[i].typ = (OPJ_UINT16) l_value; + + opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */ + p_cdef_header_data += 2; + cdef_info[i].asoc = (OPJ_UINT16) l_value; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2, + OPJ_BYTE * p_colr_header_data, + OPJ_UINT32 p_colr_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_value; + + /* preconditions */ + assert(jp2 != 00); + assert(p_colr_header_data != 00); + assert(p_manager != 00); + + if (p_colr_header_size < 3) { + opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n"); + return OPJ_FALSE; + } + + /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour + * Specification boxes after the first.' + */ + if (jp2->color.jp2_has_colr) { + opj_event_msg(p_manager, EVT_INFO, + "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n"); + p_colr_header_data += p_colr_header_size; + return OPJ_TRUE; + } + + opj_read_bytes(p_colr_header_data, &jp2->meth, 1); /* METH */ + ++p_colr_header_data; + + opj_read_bytes(p_colr_header_data, &jp2->precedence, 1); /* PRECEDENCE */ + ++p_colr_header_data; + + opj_read_bytes(p_colr_header_data, &jp2->approx, 1); /* APPROX */ + ++p_colr_header_data; + + if (jp2->meth == 1) { + if (p_colr_header_size < 7) { + opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", + p_colr_header_size); + return OPJ_FALSE; + } + if ((p_colr_header_size > 7) && + (jp2->enumcs != 14)) { /* handled below for CIELab) */ + /* testcase Altona_Technical_v20_x4.pdf */ + opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", + p_colr_header_size); + } + + opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4); /* EnumCS */ + + p_colr_header_data += 4; + + if (jp2->enumcs == 14) { /* CIELab */ + OPJ_UINT32 *cielab; + OPJ_UINT32 rl, ol, ra, oa, rb, ob, il; + + cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32)); + if (cielab == NULL) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n"); + return OPJ_FALSE; + } + cielab[0] = 14; /* enumcs */ + + /* default values */ + rl = ra = rb = ol = oa = ob = 0; + il = 0x00443530; /* D50 */ + cielab[1] = 0x44454600;/* DEF */ + + if (p_colr_header_size == 35) { + opj_read_bytes(p_colr_header_data, &rl, 4); + p_colr_header_data += 4; + opj_read_bytes(p_colr_header_data, &ol, 4); + p_colr_header_data += 4; + opj_read_bytes(p_colr_header_data, &ra, 4); + p_colr_header_data += 4; + opj_read_bytes(p_colr_header_data, &oa, 4); + p_colr_header_data += 4; + opj_read_bytes(p_colr_header_data, &rb, 4); + p_colr_header_data += 4; + opj_read_bytes(p_colr_header_data, &ob, 4); + p_colr_header_data += 4; + opj_read_bytes(p_colr_header_data, &il, 4); + p_colr_header_data += 4; + + cielab[1] = 0; + } else if (p_colr_header_size != 7) { + opj_event_msg(p_manager, EVT_WARNING, + "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size); + } + cielab[2] = rl; + cielab[4] = ra; + cielab[6] = rb; + cielab[3] = ol; + cielab[5] = oa; + cielab[7] = ob; + cielab[8] = il; + + jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab; + jp2->color.icc_profile_len = 0; + } + jp2->color.jp2_has_colr = 1; + } else if (jp2->meth == 2) { + /* ICC profile */ + OPJ_INT32 it_icc_value = 0; + OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3; + + jp2->color.icc_profile_len = (OPJ_UINT32)icc_len; + jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len); + if (!jp2->color.icc_profile_buf) { + jp2->color.icc_profile_len = 0; + return OPJ_FALSE; + } + + for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) { + opj_read_bytes(p_colr_header_data, &l_value, 1); /* icc values */ + ++p_colr_header_data; + jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value; + } + + jp2->color.jp2_has_colr = 1; + } else if (jp2->meth > 2) { + /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values: + conforming JP2 reader shall ignore the entire Colour Specification box.*/ + opj_event_msg(p_manager, EVT_INFO, + "COLR BOX meth value is not a regular value (%d), " + "so we will ignore the entire Colour Specification box. \n", jp2->meth); + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager) +{ + if (!p_image) { + return OPJ_FALSE; + } + + /* J2K decoding */ + if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Failed to decode the codestream in the JP2 file\n"); + return OPJ_FALSE; + } + + if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) { + /* Bypass all JP2 component transforms */ + return OPJ_TRUE; + } + + if (!jp2->ignore_pclr_cmap_cdef) { + if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) { + return OPJ_FALSE; + } + + /* Set Image Color Space */ + if (jp2->enumcs == 16) { + p_image->color_space = OPJ_CLRSPC_SRGB; + } else if (jp2->enumcs == 17) { + p_image->color_space = OPJ_CLRSPC_GRAY; + } else if (jp2->enumcs == 18) { + p_image->color_space = OPJ_CLRSPC_SYCC; + } else if (jp2->enumcs == 24) { + p_image->color_space = OPJ_CLRSPC_EYCC; + } else if (jp2->enumcs == 12) { + p_image->color_space = OPJ_CLRSPC_CMYK; + } else { + p_image->color_space = OPJ_CLRSPC_UNKNOWN; + } + + if (jp2->color.jp2_pclr) { + /* Part 1, I.5.3.4: Either both or none : */ + if (!jp2->color.jp2_pclr->cmap) { + opj_jp2_free_pclr(&(jp2->color)); + } else { + if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) { + return OPJ_FALSE; + } + } + } + + /* Apply the color space if needed */ + if (jp2->color.jp2_cdef) { + opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager); + } + + if (jp2->color.icc_profile_buf) { + p_image->icc_profile_buf = jp2->color.icc_profile_buf; + p_image->icc_profile_len = jp2->color.icc_profile_len; + jp2->color.icc_profile_buf = NULL; + } + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager + ) +{ + opj_jp2_img_header_writer_handler_t l_writers [4]; + opj_jp2_img_header_writer_handler_t * l_current_writer; + + OPJ_INT32 i, l_nb_pass; + /* size of data for super box*/ + OPJ_UINT32 l_jp2h_size = 8; + OPJ_BOOL l_result = OPJ_TRUE; + + /* to store the data of the super box */ + OPJ_BYTE l_jp2h_data [8]; + + /* preconditions */ + assert(stream != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + memset(l_writers, 0, sizeof(l_writers)); + + if (jp2->bpc == 255) { + l_nb_pass = 3; + l_writers[0].handler = opj_jp2_write_ihdr; + l_writers[1].handler = opj_jp2_write_bpcc; + l_writers[2].handler = opj_jp2_write_colr; + } else { + l_nb_pass = 2; + l_writers[0].handler = opj_jp2_write_ihdr; + l_writers[1].handler = opj_jp2_write_colr; + } + + if (jp2->color.jp2_cdef != NULL) { + l_writers[l_nb_pass].handler = opj_jp2_write_cdef; + l_nb_pass++; + } + + /* write box header */ + /* write JP2H type */ + opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4); + + l_current_writer = l_writers; + for (i = 0; i < l_nb_pass; ++i) { + l_current_writer->m_data = l_current_writer->handler(jp2, + &(l_current_writer->m_size)); + if (l_current_writer->m_data == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to hold JP2 Header data\n"); + l_result = OPJ_FALSE; + break; + } + + l_jp2h_size += l_current_writer->m_size; + ++l_current_writer; + } + + if (! l_result) { + l_current_writer = l_writers; + for (i = 0; i < l_nb_pass; ++i) { + if (l_current_writer->m_data != 00) { + opj_free(l_current_writer->m_data); + } + ++l_current_writer; + } + + return OPJ_FALSE; + } + + /* write super box size */ + opj_write_bytes(l_jp2h_data, l_jp2h_size, 4); + + /* write super box data on stream */ + if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) { + opj_event_msg(p_manager, EVT_ERROR, + "Stream error while writing JP2 Header box\n"); + l_result = OPJ_FALSE; + } + + if (l_result) { + l_current_writer = l_writers; + for (i = 0; i < l_nb_pass; ++i) { + if (opj_stream_write_data(stream, l_current_writer->m_data, + l_current_writer->m_size, p_manager) != l_current_writer->m_size) { + opj_event_msg(p_manager, EVT_ERROR, + "Stream error while writing JP2 Header box\n"); + l_result = OPJ_FALSE; + break; + } + ++l_current_writer; + } + } + + l_current_writer = l_writers; + + /* cleanup */ + for (i = 0; i < l_nb_pass; ++i) { + if (l_current_writer->m_data != 00) { + opj_free(l_current_writer->m_data); + } + ++l_current_writer; + } + + return l_result; +} + +static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_ftyp_size; + OPJ_BYTE * l_ftyp_data, * l_current_data_ptr; + OPJ_BOOL l_result; + + /* preconditions */ + assert(cio != 00); + assert(jp2 != 00); + assert(p_manager != 00); + l_ftyp_size = 16 + 4 * jp2->numcl; + + l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size); + + if (l_ftyp_data == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n"); + return OPJ_FALSE; + } + + l_current_data_ptr = l_ftyp_data; + + opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */ + l_current_data_ptr += 4; + + opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */ + l_current_data_ptr += 4; + + opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */ + l_current_data_ptr += 4; + + opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */ + l_current_data_ptr += 4; + + for (i = 0; i < jp2->numcl; i++) { + opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */ + } + + l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size, + p_manager) == l_ftyp_size); + if (! l_result) { + opj_event_msg(p_manager, EVT_ERROR, + "Error while writing ftyp data to stream\n"); + } + + opj_free(l_ftyp_data); + + return l_result; +} + +static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_OFF_T j2k_codestream_exit; + OPJ_BYTE l_data_header [8]; + + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + assert(opj_stream_has_seek(cio)); + + j2k_codestream_exit = opj_stream_tell(cio); + opj_write_bytes(l_data_header, + (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset), + 4); /* size of codestream */ + opj_write_bytes(l_data_header + 4, JP2_JP2C, + 4); /* JP2C */ + + if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + /* 12 bytes will be read */ + OPJ_BYTE l_signature_data [12]; + + /* preconditions */ + assert(cio != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + OPJ_UNUSED(jp2); + + /* write box length */ + opj_write_bytes(l_signature_data, 12, 4); + /* writes box type */ + opj_write_bytes(l_signature_data + 4, JP2_JP, 4); + /* writes magic number*/ + opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4); + + if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ +/* JP2 decoder interface */ +/* ----------------------------------------------------------------------- */ + +void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) +{ + /* setup the J2K codec */ + opj_j2k_setup_decoder(jp2->j2k, parameters); + + /* further JP2 initializations go here */ + jp2->color.jp2_has_colr = 0; + jp2->ignore_pclr_cmap_cdef = parameters->flags & + OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; +} + +OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads) +{ + return opj_j2k_set_threads(jp2->j2k, num_threads); +} + +/* ----------------------------------------------------------------------- */ +/* JP2 encoder interface */ +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2, + opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i; + OPJ_UINT32 depth_0; + OPJ_UINT32 sign; + OPJ_UINT32 alpha_count; + OPJ_UINT32 color_channels = 0U; + OPJ_UINT32 alpha_channel = 0U; + + + if (!jp2 || !parameters || !image) { + return OPJ_FALSE; + } + + /* setup the J2K codec */ + /* ------------------- */ + + /* Check if number of components respects standard */ + if (image->numcomps < 1 || image->numcomps > 16384) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid number of components specified while setting up JP2 encoder\n"); + return OPJ_FALSE; + } + + if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, + p_manager) == OPJ_FALSE) { + return OPJ_FALSE; + } + + /* setup the JP2 codec */ + /* ------------------- */ + + /* Profile box */ + + jp2->brand = JP2_JP2; /* BR */ + jp2->minversion = 0; /* MinV */ + jp2->numcl = 1; + jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32)); + if (!jp2->cl) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory when setup the JP2 encoder\n"); + return OPJ_FALSE; + } + jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */ + + /* Image Header box */ + + jp2->numcomps = image->numcomps; /* NC */ + jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof( + opj_jp2_comps_t)); + if (!jp2->comps) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory when setup the JP2 encoder\n"); + /* Memory of jp2->cl will be freed by opj_jp2_destroy */ + return OPJ_FALSE; + } + + jp2->h = image->y1 - image->y0; /* HEIGHT */ + jp2->w = image->x1 - image->x0; /* WIDTH */ + /* BPC */ + depth_0 = image->comps[0].prec - 1; + sign = image->comps[0].sgnd; + jp2->bpc = depth_0 + (sign << 7); + for (i = 1; i < image->numcomps; i++) { + OPJ_UINT32 depth = image->comps[i].prec - 1; + sign = image->comps[i].sgnd; + if (depth_0 != depth) { + jp2->bpc = 255; + } + } + jp2->C = 7; /* C : Always 7 */ + jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */ + jp2->IPR = 0; /* IPR, no intellectual property */ + + /* BitsPerComponent box */ + for (i = 0; i < image->numcomps; i++) { + jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7); + } + + /* Colour Specification box */ + if (image->icc_profile_len) { + jp2->meth = 2; + jp2->enumcs = 0; + } else { + jp2->meth = 1; + if (image->color_space == 1) { + jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */ + } else if (image->color_space == 2) { + jp2->enumcs = 17; /* greyscale */ + } else if (image->color_space == 3) { + jp2->enumcs = 18; /* YUV */ + } + } + + /* Channel Definition box */ + /* FIXME not provided by parameters */ + /* We try to do what we can... */ + alpha_count = 0U; + for (i = 0; i < image->numcomps; i++) { + if (image->comps[i].alpha != 0) { + alpha_count++; + alpha_channel = i; + } + } + if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */ + switch (jp2->enumcs) { + case 16: + case 18: + color_channels = 3; + break; + case 17: + color_channels = 1; + break; + default: + alpha_count = 0U; + break; + } + if (alpha_count == 0U) { + opj_event_msg(p_manager, EVT_WARNING, + "Alpha channel specified but unknown enumcs. No cdef box will be created.\n"); + } else if (image->numcomps < (color_channels + 1)) { + opj_event_msg(p_manager, EVT_WARNING, + "Alpha channel specified but not enough image components for an automatic cdef box creation.\n"); + alpha_count = 0U; + } else if ((OPJ_UINT32)alpha_channel < color_channels) { + opj_event_msg(p_manager, EVT_WARNING, + "Alpha channel position conflicts with color channel. No cdef box will be created.\n"); + alpha_count = 0U; + } + } else if (alpha_count > 1) { + opj_event_msg(p_manager, EVT_WARNING, + "Multiple alpha channels specified. No cdef box will be created.\n"); + } + if (alpha_count == 1U) { /* if here, we know what we can do */ + jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t)); + if (!jp2->color.jp2_cdef) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to setup the JP2 encoder\n"); + return OPJ_FALSE; + } + /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */ + /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */ + jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc( + image->numcomps * sizeof(opj_jp2_cdef_info_t)); + if (!jp2->color.jp2_cdef->info) { + /* memory will be freed by opj_jp2_destroy */ + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to setup the JP2 encoder\n"); + return OPJ_FALSE; + } + jp2->color.jp2_cdef->n = (OPJ_UINT16) + image->numcomps; /* cast is valid : image->numcomps [1,16384] */ + for (i = 0U; i < color_channels; i++) { + jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16) + i; /* cast is valid : image->numcomps [1,16384] */ + jp2->color.jp2_cdef->info[i].typ = 0U; + jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i + + 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */ + } + for (; i < image->numcomps; i++) { + if (image->comps[i].alpha != 0) { /* we'll be here exactly once */ + jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16) + i; /* cast is valid : image->numcomps [1,16384] */ + jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */ + jp2->color.jp2_cdef->info[i].asoc = + 0U; /* Apply alpha channel to the whole image */ + } else { + /* Unknown channel */ + jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16) + i; /* cast is valid : image->numcomps [1,16384] */ + jp2->color.jp2_cdef->info[i].typ = 65535U; + jp2->color.jp2_cdef->info[i].asoc = 65535U; + } + } + } + + jp2->precedence = 0; /* PRECEDENCE */ + jp2->approx = 0; /* APPROX */ + + jp2->jpip_on = parameters->jpip_on; + + return OPJ_TRUE; +} + +OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager) +{ + return opj_j2k_encode(jp2->j2k, stream, p_manager); +} + +OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + /* customization of the end encoding */ + if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) { + return OPJ_FALSE; + } + + /* write header */ + if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) { + return OPJ_FALSE; + } + + return opj_j2k_end_decompress(jp2->j2k, cio, p_manager); +} + +OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + /* customization of the end encoding */ + if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) { + return OPJ_FALSE; + } + + if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) { + return OPJ_FALSE; + } + + /* write header */ + return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager); +} + +static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_manager != 00); + +#ifdef USE_JPIP + if (jp2->jpip_on) { + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jpip_write_iptr, p_manager)) { + return OPJ_FALSE; + } + } +#endif + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_write_jp2c, p_manager)) { + return OPJ_FALSE; + } + /* DEVELOPER CORNER, add your custom procedures */ +#ifdef USE_JPIP + if (jp2->jpip_on) { + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jpip_write_cidx, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jpip_write_fidx, p_manager)) { + return OPJ_FALSE; + } + } +#endif + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_read_header_procedure, p_manager)) { + return OPJ_FALSE; + } + /* DEVELOPER CORNER, add your custom procedures */ + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + OPJ_BOOL l_is_valid = OPJ_TRUE; + OPJ_UINT32 i; + + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + OPJ_UNUSED(p_manager); + + /* JPEG2000 codec validation */ + + /* STATE checking */ + /* make sure the state is at 0 */ + l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE); + + /* make sure not reading a jp2h ???? WEIRD */ + l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE); + + /* POINTER validation */ + /* make sure a j2k codec is present */ + l_is_valid &= (jp2->j2k != 00); + + /* make sure a procedure list is present */ + l_is_valid &= (jp2->m_procedure_list != 00); + + /* make sure a validation list is present */ + l_is_valid &= (jp2->m_validation_list != 00); + + /* PARAMETER VALIDATION */ + /* number of components */ + l_is_valid &= (jp2->numcl > 0); + /* width */ + l_is_valid &= (jp2->h > 0); + /* height */ + l_is_valid &= (jp2->w > 0); + /* precision */ + for (i = 0; i < jp2->numcomps; ++i) { + l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) < + 38U); /* 0 is valid, ignore sign for check */ + } + + /* METH */ + l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3)); + + /* stream validation */ + /* back and forth is needed */ + l_is_valid &= opj_stream_has_seek(cio); + + return l_is_valid; +} + +static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager + ) +{ + opj_jp2_box_t box; + OPJ_UINT32 l_nb_bytes_read; + const opj_jp2_header_handler_t * l_current_handler; + const opj_jp2_header_handler_t * l_current_handler_misplaced; + OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE; + OPJ_UINT32 l_current_data_size; + OPJ_BYTE * l_current_data = 00; + + /* preconditions */ + assert(stream != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size); + + if (l_current_data == 00) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to handle jpeg2000 file header\n"); + return OPJ_FALSE; + } + + while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) { + /* is it the codestream box ? */ + if (box.type == JP2_JP2C) { + if (jp2->jp2_state & JP2_STATE_HEADER) { + jp2->jp2_state |= JP2_STATE_CODESTREAM; + opj_free(l_current_data); + return OPJ_TRUE; + } else { + opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + } else if (box.length == 0) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + /* testcase 1851.pdf.SIGSEGV.ce9.948 */ + else if (box.length < l_nb_bytes_read) { + opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, + box.type); + opj_free(l_current_data); + return OPJ_FALSE; + } + + l_current_handler = opj_jp2_find_handler(box.type); + l_current_handler_misplaced = opj_jp2_img_find_handler(box.type); + l_current_data_size = box.length - l_nb_bytes_read; + + if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) { + if (l_current_handler == 00) { + opj_event_msg(p_manager, EVT_WARNING, + "Found a misplaced '%c%c%c%c' box outside jp2h box\n", + (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16), + (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0)); + if (jp2->jp2_state & JP2_STATE_HEADER) { + /* read anyway, we already have jp2h */ + l_current_handler = l_current_handler_misplaced; + } else { + opj_event_msg(p_manager, EVT_WARNING, + "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n", + (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16), + (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0)); + jp2->jp2_state |= JP2_STATE_UNKNOWN; + if (opj_stream_skip(stream, l_current_data_size, + p_manager) != l_current_data_size) { + opj_event_msg(p_manager, EVT_ERROR, + "Problem with skipping JPEG2000 box, stream error\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + continue; + } + } + if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) { + /* do not even try to malloc if we can't read */ + opj_event_msg(p_manager, EVT_ERROR, + "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n", + box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16), + (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size, + (OPJ_UINT32)opj_stream_get_number_byte_left(stream)); + opj_free(l_current_data); + return OPJ_FALSE; + } + if (l_current_data_size > l_last_data_size) { + OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data, + l_current_data_size); + if (!new_current_data) { + opj_free(l_current_data); + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to handle jpeg2000 box\n"); + return OPJ_FALSE; + } + l_current_data = new_current_data; + l_last_data_size = l_current_data_size; + } + + l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data, + l_current_data_size, p_manager); + if (l_nb_bytes_read != l_current_data_size) { + opj_event_msg(p_manager, EVT_ERROR, + "Problem with reading JPEG2000 box, stream error\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + + if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size, + p_manager)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + } else { + if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) { + opj_event_msg(p_manager, EVT_ERROR, + "Malformed JP2 file format: first box must be JPEG 2000 signature box\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) { + opj_event_msg(p_manager, EVT_ERROR, + "Malformed JP2 file format: second box must be file type box\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + jp2->jp2_state |= JP2_STATE_UNKNOWN; + if (opj_stream_skip(stream, l_current_data_size, + p_manager) != l_current_data_size) { + if (jp2->jp2_state & JP2_STATE_CODESTREAM) { + /* If we already read the codestream, do not error out */ + /* Needed for data/input/nonregression/issue254.jp2 */ + opj_event_msg(p_manager, EVT_WARNING, + "Problem with skipping JPEG2000 box, stream error\n"); + opj_free(l_current_data); + return OPJ_TRUE; + } else { + opj_event_msg(p_manager, EVT_ERROR, + "Problem with skipping JPEG2000 box, stream error\n"); + opj_free(l_current_data); + return OPJ_FALSE; + } + } + } + } + + opj_free(l_current_data); + + return OPJ_TRUE; +} + +/** + * Executes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param jp2 the jpeg2000 file codec to execute the procedures on. + * @param stream the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager + ) + +{ + OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *, + opj_event_mgr_t *) = 00; + OPJ_BOOL l_result = OPJ_TRUE; + OPJ_UINT32 l_nb_proc, i; + + /* preconditions */ + assert(p_procedure_list != 00); + assert(jp2 != 00); + assert(stream != 00); + assert(p_manager != 00); + + l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); + l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *, + opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + + for (i = 0; i < l_nb_proc; ++i) { + l_result = l_result && (*l_procedure)(jp2, stream, p_manager); + ++l_procedure; + } + + /* and clear the procedure list at the end. */ + opj_procedure_list_clear(p_procedure_list); + return l_result; +} + +OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(jp2 != 00); + assert(stream != 00); + assert(p_manager != 00); + + /* customization of the validation */ + if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) { + return OPJ_FALSE; + } + + /* validation of the parameters codec */ + if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) { + return OPJ_FALSE; + } + + /* customization of the encoding */ + if (! opj_jp2_setup_header_writing(jp2, p_manager)) { + return OPJ_FALSE; + } + + /* write header */ + if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) { + return OPJ_FALSE; + } + + return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager); +} + +static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id) +{ + OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof( + opj_jp2_header_handler_t); + + for (i = 0; i < l_handler_size; ++i) { + if (jp2_header[i].id == p_id) { + return &jp2_header[i]; + } + } + return NULL; +} + +/** + * Finds the image execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or 00 if it could not be found. + */ +static const opj_jp2_header_handler_t * opj_jp2_img_find_handler( + OPJ_UINT32 p_id) +{ + OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof( + opj_jp2_header_handler_t); + for (i = 0; i < l_handler_size; ++i) { + if (jp2_img_header[i].id == p_id) { + return &jp2_img_header[i]; + } + } + + return NULL; +} + +/** + * Reads a jpeg2000 file signature box. + * + * @param p_header_data the data contained in the signature box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the signature box. + * @param p_manager the user event manager. + * + * @return true if the file signature box is valid. + */ +static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) + +{ + OPJ_UINT32 l_magic_number; + + /* preconditions */ + assert(p_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + if (jp2->jp2_state != JP2_STATE_NONE) { + opj_event_msg(p_manager, EVT_ERROR, + "The signature box must be the first box in the file.\n"); + return OPJ_FALSE; + } + + /* assure length of data is correct (4 -> magic number) */ + if (p_header_size != 4) { + opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n"); + return OPJ_FALSE; + } + + /* rearrange data */ + opj_read_bytes(p_header_data, &l_magic_number, 4); + if (l_magic_number != 0x0d0a870a) { + opj_event_msg(p_manager, EVT_ERROR, + "Error with JP Signature : bad magic number\n"); + return OPJ_FALSE; + } + + jp2->jp2_state |= JP2_STATE_SIGNATURE; + + return OPJ_TRUE; +} + +/** + * Reads a a FTYP box - File type box + * + * @param p_header_data the data contained in the FTYP box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the FTYP box. + * @param p_manager the user event manager. + * + * @return true if the FTYP box is valid. + */ +static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i, l_remaining_bytes; + + /* preconditions */ + assert(p_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + if (jp2->jp2_state != JP2_STATE_SIGNATURE) { + opj_event_msg(p_manager, EVT_ERROR, + "The ftyp box must be the second box in the file.\n"); + return OPJ_FALSE; + } + + /* assure length of data is correct */ + if (p_header_size < 8) { + opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data, &jp2->brand, 4); /* BR */ + p_header_data += 4; + + opj_read_bytes(p_header_data, &jp2->minversion, 4); /* MinV */ + p_header_data += 4; + + l_remaining_bytes = p_header_size - 8; + + /* the number of remaining bytes should be a multiple of 4 */ + if ((l_remaining_bytes & 0x3) != 0) { + opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n"); + return OPJ_FALSE; + } + + /* div by 4 */ + jp2->numcl = l_remaining_bytes >> 2; + if (jp2->numcl) { + jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32)); + if (jp2->cl == 00) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n"); + return OPJ_FALSE; + } + } + + for (i = 0; i < jp2->numcl; ++i) { + opj_read_bytes(p_header_data, &jp2->cl[i], 4); /* CLi */ + p_header_data += 4; + } + + jp2->jp2_state |= JP2_STATE_FILE_TYPE; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(stream != 00); + assert(p_manager != 00); + + jp2->j2k_codestream_offset = opj_stream_tell(stream); + + if (opj_stream_skip(stream, 8, p_manager) != 8) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(stream != 00); + assert(p_manager != 00); + + jp2->jpip_iptr_offset = opj_stream_tell(stream); + + if (opj_stream_skip(stream, 24, p_manager) != 24) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** + * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param p_header_data the data contained in the file header box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the file header box. + * @param p_manager the user event manager. + * + * @return true if the JP2 Header box was successfully recognized. +*/ +static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2, + OPJ_BYTE *p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_box_size = 0, l_current_data_size = 0; + opj_jp2_box_t box; + const opj_jp2_header_handler_t * l_current_handler; + OPJ_BOOL l_has_ihdr = 0; + + /* preconditions */ + assert(p_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + /* make sure the box is well placed */ + if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) { + opj_event_msg(p_manager, EVT_ERROR, + "The box must be the first box in the file.\n"); + return OPJ_FALSE; + } + + jp2->jp2_img_state = JP2_IMG_STATE_NONE; + + /* iterate while remaining data */ + while (p_header_size > 0) { + + if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size, + p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, + "Stream error while reading JP2 Header box\n"); + return OPJ_FALSE; + } + + if (box.length > p_header_size) { + opj_event_msg(p_manager, EVT_ERROR, + "Stream error while reading JP2 Header box: box length is inconsistent.\n"); + return OPJ_FALSE; + } + + l_current_handler = opj_jp2_img_find_handler(box.type); + l_current_data_size = box.length - l_box_size; + p_header_data += l_box_size; + + if (l_current_handler != 00) { + if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size, + p_manager)) { + return OPJ_FALSE; + } + } else { + jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN; + } + + if (box.type == JP2_IHDR) { + l_has_ihdr = 1; + } + + p_header_data += l_current_data_size; + p_header_size -= box.length; + } + + if (l_has_ihdr == 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Stream error while reading JP2 Header box: no 'ihdr' box.\n"); + return OPJ_FALSE; + } + + jp2->jp2_state |= JP2_STATE_HEADER; + jp2->has_jp2h = 1; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_number_bytes_read, + OPJ_UINT32 p_box_max_size, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_value; + + /* preconditions */ + assert(p_data != 00); + assert(box != 00); + assert(p_number_bytes_read != 00); + assert(p_manager != 00); + + if (p_box_max_size < 8) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n"); + return OPJ_FALSE; + } + + /* process read data */ + opj_read_bytes(p_data, &l_value, 4); + p_data += 4; + box->length = (OPJ_UINT32)(l_value); + + opj_read_bytes(p_data, &l_value, 4); + p_data += 4; + box->type = (OPJ_UINT32)(l_value); + + *p_number_bytes_read = 8; + + /* do we have a "special very large box ?" */ + /* read then the XLBox */ + if (box->length == 1) { + OPJ_UINT32 l_xl_part_size; + + if (p_box_max_size < 16) { + opj_event_msg(p_manager, EVT_ERROR, + "Cannot handle XL box of less than 16 bytes\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_data, &l_xl_part_size, 4); + p_data += 4; + *p_number_bytes_read += 4; + + if (l_xl_part_size != 0) { + opj_event_msg(p_manager, EVT_ERROR, + "Cannot handle box sizes higher than 2^32\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_data, &l_value, 4); + *p_number_bytes_read += 4; + box->length = (OPJ_UINT32)(l_value); + + if (box->length == 0) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); + return OPJ_FALSE; + } + } else if (box->length == 0) { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); + return OPJ_FALSE; + } + if (box->length < *p_number_bytes_read) { + opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n"); + return OPJ_FALSE; + } + return OPJ_TRUE; +} + +OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream, + opj_jp2_t *jp2, + opj_image_t ** p_image, + opj_event_mgr_t * p_manager + ) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + /* customization of the validation */ + if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) { + return OPJ_FALSE; + } + + /* customization of the encoding */ + if (! opj_jp2_setup_header_reading(jp2, p_manager)) { + return OPJ_FALSE; + } + + /* validation of the parameters codec */ + if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) { + return OPJ_FALSE; + } + + /* read header */ + if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) { + return OPJ_FALSE; + } + if (jp2->has_jp2h == 0) { + opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n"); + return OPJ_FALSE; + } + if (jp2->has_ihdr == 0) { + opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n"); + return OPJ_FALSE; + } + + return opj_j2k_read_header(p_stream, + jp2->j2k, + p_image, + p_manager); +} + +static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(jp2->m_validation_list, + (opj_procedure)opj_jp2_default_validation, p_manager)) { + return OPJ_FALSE; + } + /* DEVELOPER CORNER, add your custom validation procedure */ + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_manager != 00); + + OPJ_UNUSED(jp2); + OPJ_UNUSED(p_manager); + + /* DEVELOPER CORNER, add your custom validation procedure */ + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_write_jp, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_write_ftyp, p_manager)) { + return OPJ_FALSE; + } + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_write_jp2h, p_manager)) { + return OPJ_FALSE; + } + if (jp2->jpip_on) { + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jpip_skip_iptr, p_manager)) { + return OPJ_FALSE; + } + } + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_skip_jp2c, p_manager)) { + return OPJ_FALSE; + } + + /* DEVELOPER CORNER, insert your custom procedures */ + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(jp2 != 00); + assert(p_manager != 00); + + if (! opj_procedure_list_add_procedure(jp2->m_procedure_list, + (opj_procedure)opj_jp2_read_header_procedure, p_manager)) { + return OPJ_FALSE; + } + + /* DEVELOPER CORNER, add your custom procedures */ + + return OPJ_TRUE; +} + +OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + return opj_j2k_read_tile_header(p_jp2->j2k, + p_tile_index, + p_data_size, + p_tile_x0, p_tile_y0, + p_tile_x1, p_tile_y1, + p_nb_comps, + p_go_on, + p_stream, + p_manager); +} + +OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) + +{ + return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size, + p_stream, p_manager); +} + +OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size, + p_stream, p_manager); +} + +void opj_jp2_destroy(opj_jp2_t *jp2) +{ + if (jp2) { + /* destroy the J2K codec */ + opj_j2k_destroy(jp2->j2k); + jp2->j2k = 00; + + if (jp2->comps) { + opj_free(jp2->comps); + jp2->comps = 00; + } + + if (jp2->cl) { + opj_free(jp2->cl); + jp2->cl = 00; + } + + if (jp2->color.icc_profile_buf) { + opj_free(jp2->color.icc_profile_buf); + jp2->color.icc_profile_buf = 00; + } + + if (jp2->color.jp2_cdef) { + if (jp2->color.jp2_cdef->info) { + opj_free(jp2->color.jp2_cdef->info); + jp2->color.jp2_cdef->info = NULL; + } + + opj_free(jp2->color.jp2_cdef); + jp2->color.jp2_cdef = 00; + } + + if (jp2->color.jp2_pclr) { + if (jp2->color.jp2_pclr->cmap) { + opj_free(jp2->color.jp2_pclr->cmap); + jp2->color.jp2_pclr->cmap = NULL; + } + if (jp2->color.jp2_pclr->channel_sign) { + opj_free(jp2->color.jp2_pclr->channel_sign); + jp2->color.jp2_pclr->channel_sign = NULL; + } + if (jp2->color.jp2_pclr->channel_size) { + opj_free(jp2->color.jp2_pclr->channel_size); + jp2->color.jp2_pclr->channel_size = NULL; + } + if (jp2->color.jp2_pclr->entries) { + opj_free(jp2->color.jp2_pclr->entries); + jp2->color.jp2_pclr->entries = NULL; + } + + opj_free(jp2->color.jp2_pclr); + jp2->color.jp2_pclr = 00; + } + + if (jp2->m_validation_list) { + opj_procedure_list_destroy(jp2->m_validation_list); + jp2->m_validation_list = 00; + } + + if (jp2->m_procedure_list) { + opj_procedure_list_destroy(jp2->m_procedure_list); + jp2->m_procedure_list = 00; + } + + opj_free(jp2); + } +} + +OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *p_jp2, + OPJ_UINT32 numcomps, + const OPJ_UINT32* comps_indices, + opj_event_mgr_t * p_manager) +{ + return opj_j2k_set_decoded_components(p_jp2->j2k, + numcomps, comps_indices, + p_manager); +} + +OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, + opj_event_mgr_t * p_manager + ) +{ + return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, + p_end_x, p_end_y, p_manager); +} + +OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager, + OPJ_UINT32 tile_index + ) +{ + if (!p_image) { + return OPJ_FALSE; + } + + opj_event_msg(p_manager, EVT_WARNING, + "JP2 box which are after the codestream will not be read by this function.\n"); + + if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) { + opj_event_msg(p_manager, EVT_ERROR, + "Failed to decode the codestream in the JP2 file\n"); + return OPJ_FALSE; + } + + if (p_jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) { + /* Bypass all JP2 component transforms */ + return OPJ_TRUE; + } + + if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) { + return OPJ_FALSE; + } + + /* Set Image Color Space */ + if (p_jp2->enumcs == 16) { + p_image->color_space = OPJ_CLRSPC_SRGB; + } else if (p_jp2->enumcs == 17) { + p_image->color_space = OPJ_CLRSPC_GRAY; + } else if (p_jp2->enumcs == 18) { + p_image->color_space = OPJ_CLRSPC_SYCC; + } else if (p_jp2->enumcs == 24) { + p_image->color_space = OPJ_CLRSPC_EYCC; + } else if (p_jp2->enumcs == 12) { + p_image->color_space = OPJ_CLRSPC_CMYK; + } else { + p_image->color_space = OPJ_CLRSPC_UNKNOWN; + } + + if (p_jp2->color.jp2_pclr) { + /* Part 1, I.5.3.4: Either both or none : */ + if (!p_jp2->color.jp2_pclr->cmap) { + opj_jp2_free_pclr(&(p_jp2->color)); + } else { + if (!opj_jp2_apply_pclr(p_image, &(p_jp2->color), p_manager)) { + return OPJ_FALSE; + } + } + } + + /* Apply the color space if needed */ + if (p_jp2->color.jp2_cdef) { + opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager); + } + + if (p_jp2->color.icc_profile_buf) { + p_image->icc_profile_buf = p_jp2->color.icc_profile_buf; + p_image->icc_profile_len = p_jp2->color.icc_profile_len; + p_jp2->color.icc_profile_buf = NULL; + } + + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ +/* JP2 encoder interface */ +/* ----------------------------------------------------------------------- */ + +opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder) +{ + opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t)); + if (jp2) { + + /* create the J2K codec */ + if (! p_is_decoder) { + jp2->j2k = opj_j2k_create_compress(); + } else { + jp2->j2k = opj_j2k_create_decompress(); + } + + if (jp2->j2k == 00) { + opj_jp2_destroy(jp2); + return 00; + } + + /* Color structure */ + jp2->color.icc_profile_buf = NULL; + jp2->color.icc_profile_len = 0; + jp2->color.jp2_cdef = NULL; + jp2->color.jp2_pclr = NULL; + jp2->color.jp2_has_colr = 0; + + /* validation list creation */ + jp2->m_validation_list = opj_procedure_list_create(); + if (! jp2->m_validation_list) { + opj_jp2_destroy(jp2); + return 00; + } + + /* execution list creation */ + jp2->m_procedure_list = opj_procedure_list_create(); + if (! jp2->m_procedure_list) { + opj_jp2_destroy(jp2); + return 00; + } + } + + return jp2; +} + +void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream) +{ + /* preconditions */ + assert(p_jp2 != 00); + + j2k_dump(p_jp2->j2k, + flag, + out_stream); +} + +opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2) +{ + return j2k_get_cstr_index(p_jp2->j2k); +} + +opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2) +{ + return j2k_get_cstr_info(p_jp2->j2k); +} + +OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2, + OPJ_UINT32 res_factor, + opj_event_mgr_t * p_manager) +{ + return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager); +} + +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL opj_jp2_encoder_set_extra_options( + opj_jp2_t *p_jp2, + const char* const* p_options, + opj_event_mgr_t * p_manager) +{ + return opj_j2k_encoder_set_extra_options(p_jp2->j2k, p_options, p_manager); +} + +/* ----------------------------------------------------------------------- */ + +/* JPIP specific */ + +#ifdef USE_JPIP +static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_OFF_T j2k_codestream_exit; + OPJ_BYTE l_data_header [24]; + + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + assert(opj_stream_has_seek(cio)); + + j2k_codestream_exit = opj_stream_tell(cio); + opj_write_bytes(l_data_header, 24, 4); /* size of iptr */ + opj_write_bytes(l_data_header + 4, JPIP_IPTR, + 4); /* IPTR */ +#if 0 + opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */ + opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */ +#else + opj_write_double(l_data_header + 4 + 4, 0); /* offset */ + opj_write_double(l_data_header + 8 + 8, 0); /* length */ +#endif + + if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_OFF_T j2k_codestream_exit; + OPJ_BYTE l_data_header [24]; + + OPJ_UNUSED(jp2); + + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + assert(opj_stream_has_seek(cio)); + + opj_write_bytes(l_data_header, 24, 4); /* size of iptr */ + opj_write_bytes(l_data_header + 4, JPIP_FIDX, + 4); /* IPTR */ + opj_write_double(l_data_header + 4 + 4, 0); /* offset */ + opj_write_double(l_data_header + 8 + 8, 0); /* length */ + + if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + j2k_codestream_exit = opj_stream_tell(cio); + if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_OFF_T j2k_codestream_exit; + OPJ_BYTE l_data_header [24]; + + OPJ_UNUSED(jp2); + + /* preconditions */ + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + assert(opj_stream_has_seek(cio)); + + j2k_codestream_exit = opj_stream_tell(cio); + opj_write_bytes(l_data_header, 24, 4); /* size of iptr */ + opj_write_bytes(l_data_header + 4, JPIP_CIDX, + 4); /* IPTR */ +#if 0 + opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */ + opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */ +#else + opj_write_double(l_data_header + 4 + 4, 0); /* offset */ + opj_write_double(l_data_header + 8 + 8, 0); /* length */ +#endif + + if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + j2k_codestream_exit = opj_stream_tell(cio); + if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +#if 0 +static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx, + int length_idx, opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_BYTE l_data_header [8]; + OPJ_OFF_T len, lenp; + + lenp = opj_stream_tell(cio); + opj_stream_skip(cio, 4, p_manager); /* L [at the end] */ + opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR */ + opj_stream_write_data(cio, l_data_header, 4, p_manager); + + opj_write_bytes(l_data_header, offset_jp2c, 8); /* OOFF */ + opj_stream_write_data(cio, l_data_header, 8, p_manager); + opj_write_bytes(l_data_header, length_jp2c, 4); /* OBH part 1 */ + opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2 */ + opj_stream_write_data(cio, l_data_header, 8, p_manager); + + opj_write_bytes(l_data_header, 1, 1); /* NI */ + opj_stream_write_data(cio, l_data_header, 1, p_manager); + + opj_write_bytes(l_data_header, offset_idx, 8); /* IOFF */ + opj_stream_write_data(cio, l_data_header, 8, p_manager); + opj_write_bytes(l_data_header, length_idx, 4); /* IBH part 1 */ + opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4); /* IBH part 2 */ + opj_stream_write_data(cio, l_data_header, 8, p_manager); + + len = opj_stream_tell(cio) - lenp; + opj_stream_skip(cio, lenp, p_manager); + opj_write_bytes(l_data_header, len, 4); /* L */ + opj_stream_write_data(cio, l_data_header, 4, p_manager); + opj_stream_seek(cio, lenp + len, p_manager); +} +#endif + + +#if 0 +static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx, + int length_idx, opj_stream_private_t *cio, + opj_event_mgr_t * p_manager) +{ + OPJ_BYTE l_data_header [4]; + OPJ_OFF_T len, lenp; + + lenp = opj_stream_tell(cio); + opj_stream_skip(cio, 4, p_manager); + opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */ + opj_stream_write_data(cio, l_data_header, 4, p_manager); + + write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager); + + len = opj_stream_tell(cio) - lenp; + opj_stream_skip(cio, lenp, p_manager); + opj_write_bytes(l_data_header, len, 4); /* L */ + opj_stream_write_data(cio, l_data_header, 4, p_manager); + opj_stream_seek(cio, lenp + len, p_manager); + + return len; +} +#endif +#endif /* USE_JPIP */ diff --git a/cpp/3rd_party/openjpeg/openjp2/jp2.h b/cpp/3rd_party/openjpeg/openjp2/jp2.h new file mode 100644 index 0000000000..9e7fa56674 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/jp2.h @@ -0,0 +1,512 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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 OPJ_JP2_H +#define OPJ_JP2_H +/** +@file jp2.h +@brief The JPEG-2000 file format Reader/Writer (JP2) + +*/ + +/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */ +/*@{*/ + +/*#define JPIP_JPIP 0x6a706970*/ + +#define JP2_JP 0x6a502020 /**< JPEG 2000 signature box */ +#define JP2_FTYP 0x66747970 /**< File type box */ +#define JP2_JP2H 0x6a703268 /**< JP2 header box (super-box) */ +#define JP2_IHDR 0x69686472 /**< Image header box */ +#define JP2_COLR 0x636f6c72 /**< Colour specification box */ +#define JP2_JP2C 0x6a703263 /**< Contiguous codestream box */ +#define JP2_URL 0x75726c20 /**< Data entry URL box */ +#define JP2_PCLR 0x70636c72 /**< Palette box */ +#define JP2_CMAP 0x636d6170 /**< Component Mapping box */ +#define JP2_CDEF 0x63646566 /**< Channel Definition box */ +#define JP2_DTBL 0x6474626c /**< Data Reference box */ +#define JP2_BPCC 0x62706363 /**< Bits per component box */ +#define JP2_JP2 0x6a703220 /**< File type fields */ + +/* For the future */ +/* #define JP2_RES 0x72657320 */ /**< Resolution box (super-box) */ +/* #define JP2_JP2I 0x6a703269 */ /**< Intellectual property box */ +/* #define JP2_XML 0x786d6c20 */ /**< XML box */ +/* #define JP2_UUID 0x75756994 */ /**< UUID box */ +/* #define JP2_UINF 0x75696e66 */ /**< UUID info box (super-box) */ +/* #define JP2_ULST 0x756c7374 */ /**< UUID list box */ + +/* ----------------------------------------------------------------------- */ + +typedef enum { + JP2_STATE_NONE = 0x0, + JP2_STATE_SIGNATURE = 0x1, + JP2_STATE_FILE_TYPE = 0x2, + JP2_STATE_HEADER = 0x4, + JP2_STATE_CODESTREAM = 0x8, + JP2_STATE_END_CODESTREAM = 0x10, + JP2_STATE_UNKNOWN = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */ +} +JP2_STATE; + +typedef enum { + JP2_IMG_STATE_NONE = 0x0, + JP2_IMG_STATE_UNKNOWN = 0x7fffffff +} +JP2_IMG_STATE; + +/** +Channel description: channel index, type, association +*/ +typedef struct opj_jp2_cdef_info { + OPJ_UINT16 cn, typ, asoc; +} opj_jp2_cdef_info_t; + +/** +Channel descriptions and number of descriptions +*/ +typedef struct opj_jp2_cdef { + opj_jp2_cdef_info_t *info; + OPJ_UINT16 n; +} opj_jp2_cdef_t; + +/** +Component mappings: channel index, mapping type, palette index +*/ +typedef struct opj_jp2_cmap_comp { + OPJ_UINT16 cmp; + OPJ_BYTE mtyp, pcol; +} opj_jp2_cmap_comp_t; + +/** +Palette data: table entries, palette columns +*/ +typedef struct opj_jp2_pclr { + OPJ_UINT32 *entries; + OPJ_BYTE *channel_sign; + OPJ_BYTE *channel_size; + opj_jp2_cmap_comp_t *cmap; + OPJ_UINT16 nr_entries; + OPJ_BYTE nr_channels; +} opj_jp2_pclr_t; + +/** +Collector for ICC profile, palette, component mapping, channel description +*/ +typedef struct opj_jp2_color { + OPJ_BYTE *icc_profile_buf; + OPJ_UINT32 icc_profile_len; + + opj_jp2_cdef_t *jp2_cdef; + opj_jp2_pclr_t *jp2_pclr; + OPJ_BYTE jp2_has_colr; +} opj_jp2_color_t; + +/** +JP2 component +*/ +typedef struct opj_jp2_comps { + OPJ_UINT32 depth; + OPJ_UINT32 sgnd; + OPJ_UINT32 bpcc; +} opj_jp2_comps_t; + +/** +JPEG-2000 file format reader/writer +*/ +typedef struct opj_jp2 { + /** handle to the J2K codec */ + opj_j2k_t *j2k; + /** list of validation procedures */ + struct opj_procedure_list * m_validation_list; + /** list of execution procedures */ + struct opj_procedure_list * m_procedure_list; + + /* width of image */ + OPJ_UINT32 w; + /* height of image */ + OPJ_UINT32 h; + /* number of components in the image */ + OPJ_UINT32 numcomps; + OPJ_UINT32 bpc; + OPJ_UINT32 C; + OPJ_UINT32 UnkC; + OPJ_UINT32 IPR; + OPJ_UINT32 meth; + OPJ_UINT32 approx; + OPJ_UINT32 enumcs; + OPJ_UINT32 precedence; + OPJ_UINT32 brand; + OPJ_UINT32 minversion; + OPJ_UINT32 numcl; + OPJ_UINT32 *cl; + opj_jp2_comps_t *comps; + /* FIXME: The following two variables are used to save offset + as we write out a JP2 file to disk. This mechanism is not flexible + as codec writers will need to extand those fields as new part + of the standard are implemented. + */ + OPJ_OFF_T j2k_codestream_offset; + OPJ_OFF_T jpip_iptr_offset; + OPJ_BOOL jpip_on; + OPJ_UINT32 jp2_state; + OPJ_UINT32 jp2_img_state; + + opj_jp2_color_t color; + + OPJ_BOOL ignore_pclr_cmap_cdef; + OPJ_BYTE has_jp2h; + OPJ_BYTE has_ihdr; +} +opj_jp2_t; + +/** +JP2 Box +*/ +typedef struct opj_jp2_box { + OPJ_UINT32 length; + OPJ_UINT32 type; + OPJ_INT32 init_pos; +} opj_jp2_box_t; + +typedef struct opj_jp2_header_handler { + /* marker value */ + OPJ_UINT32 id; + /* action linked to the marker */ + OPJ_BOOL(*handler)(opj_jp2_t *jp2, + OPJ_BYTE *p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); +} +opj_jp2_header_handler_t; + + +typedef struct opj_jp2_img_header_writer_handler { + /* action to perform */ + OPJ_BYTE* (*handler)(opj_jp2_t *jp2, OPJ_UINT32 * p_data_size); + /* result of the action : data */ + OPJ_BYTE* m_data; + /* size of data */ + OPJ_UINT32 m_size; +} +opj_jp2_img_header_writer_handler_t; + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Setup the decoder decoding parameters using user parameters. +Decoding parameters are returned in jp2->j2k->cp. +@param jp2 JP2 decompressor handle +@param parameters decompression parameters +*/ +void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters); + +/** Allocates worker threads for the compressor/decompressor. + * + * @param jp2 JP2 decompressor handle + * @param num_threads Number of threads. + * @return OPJ_TRUE in case of success. + */ +OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads); + +/** + * Decode an image from a JPEG-2000 file stream + * @param jp2 JP2 decompressor handle + * @param p_stream FIXME DOC + * @param p_image FIXME DOC + * @param p_manager FIXME DOC + * + * @return Returns a decoded image if successful, returns NULL otherwise +*/ +OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager); + +/** + * Setup the encoder parameters using the current image and using user parameters. + * Coding parameters are returned in jp2->j2k->cp. + * + * @param jp2 JP2 compressor handle + * @param parameters compression parameters + * @param image input filled image + * @param p_manager FIXME DOC + * @return OPJ_TRUE if successful, OPJ_FALSE otherwise +*/ +OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2, + opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t * p_manager); + +/** +Encode an image into a JPEG-2000 file stream +@param jp2 JP2 compressor handle +@param stream Output buffer stream +@param p_manager event manager +@return Returns true if successful, returns false otherwise +*/ +OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_event_mgr_t * p_manager); + + +/** + * Starts a compression scheme, i.e. validates the codec parameters, writes the header. + * + * @param jp2 the jpeg2000 file codec. + * @param stream the stream object. + * @param p_image FIXME DOC + * @param p_manager FIXME DOC + * + * @return true if the codec is valid. + */ +OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2, + opj_stream_private_t *stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager); + + +/** + * Ends the compression procedures and possibiliy add data to be read after the + * codestream. + */ +OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/* ----------------------------------------------------------------------- */ + +/** + * Ends the decompression procedures and possibiliy add data to be read after the + * codestream. + */ +OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager); + +/** + * Reads a jpeg2000 file header structure. + * + * @param p_stream the stream to read data from. + * @param jp2 the jpeg2000 file header structure. + * @param p_image FIXME DOC + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream, + opj_jp2_t *jp2, + opj_image_t ** p_image, + opj_event_mgr_t * p_manager); + +/** Sets the indices of the components to decode. + * + * @param jp2 JP2 decompressor handle + * @param numcomps Number of components to decode. + * @param comps_indices Array of num_compts indices (numbering starting at 0) + * corresponding to the components to decode. + * @param p_manager Event manager; + * + * @return OPJ_TRUE in case of success. + */ +OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *jp2, + OPJ_UINT32 numcomps, + const OPJ_UINT32* comps_indices, + opj_event_mgr_t * p_manager); + +/** + * Reads a tile header. + * @param p_jp2 the jpeg2000 codec. + * @param p_tile_index FIXME DOC + * @param p_data_size FIXME DOC + * @param p_tile_x0 FIXME DOC + * @param p_tile_y0 FIXME DOC + * @param p_tile_x1 FIXME DOC + * @param p_tile_y1 FIXME DOC + * @param p_nb_comps FIXME DOC + * @param p_go_on FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Writes a tile. + * + * @param p_jp2 the jpeg2000 codec. + * @param p_tile_index FIXME DOC + * @param p_data FIXME DOC + * @param p_data_size FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Decode tile data. + * @param p_jp2 the jpeg2000 codec. + * @param p_tile_index FIXME DOC + * @param p_data FIXME DOC + * @param p_data_size FIXME DOC + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + * + * @return FIXME DOC + */ +OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager); + +/** + * Creates a jpeg2000 file decompressor. + * + * @return an empty jpeg2000 file codec. + */ +opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder); + +/** +Destroy a JP2 decompressor handle +@param jp2 JP2 decompressor handle to destroy +*/ +void opj_jp2_destroy(opj_jp2_t *jp2); + + +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_jp2 the jpeg2000 codec. + * @param p_image FIXME DOC + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * @param p_manager the user event manager + * + * @return true if the area could be set. + */ +OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, + opj_event_mgr_t * p_manager); + +/** +* +*/ +OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager, + OPJ_UINT32 tile_index); + + +/** + * + */ +OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2, + OPJ_UINT32 res_factor, + opj_event_mgr_t * p_manager); + +/** + * Specify extra options for the encoder. + * + * @param p_jp2 the jpeg2000 codec. + * @param p_options options + * @param p_manager the user event manager + * + * @see opj_encoder_set_extra_options() for more details. + */ +OPJ_BOOL opj_jp2_encoder_set_extra_options( + opj_jp2_t *p_jp2, + const char* const* p_options, + opj_event_mgr_t * p_manager); + + +/* TODO MSD: clean these 3 functions */ +/** + * Dump some elements from the JP2 decompression structure . + * + *@param p_jp2 the jp2 codec. + *@param flag flag to describe what elements are dump. + *@param out_stream output stream where dump the elements. + * +*/ +void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream); + +/** + * Get the codestream info from a JPEG2000 codec. + * + *@param p_jp2 jp2 codec. + * + *@return the codestream information extract from the jpg2000 codec + */ +opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2); + +/** + * Get the codestream index from a JPEG2000 codec. + * + *@param p_jp2 jp2 codec. + * + *@return the codestream index extract from the jpg2000 codec + */ +opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2); + + +/*@}*/ + +/*@}*/ + +#endif /* OPJ_JP2_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/mct.c b/cpp/3rd_party/openjpeg/openjp2/mct.c new file mode 100644 index 0000000000..88c8f40920 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/mct.c @@ -0,0 +1,464 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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. + */ + +#ifdef __SSE__ +#include +#endif +#ifdef __SSE2__ +#include +#endif +#ifdef __SSE4_1__ +#include +#endif + +#include "opj_includes.h" + +/* */ +/* This table contains the norms of the basis function of the reversible MCT. */ +/* */ +static const OPJ_FLOAT64 opj_mct_norms[3] = { 1.732, .8292, .8292 }; + +/* */ +/* This table contains the norms of the basis function of the irreversible MCT. */ +/* */ +static const OPJ_FLOAT64 opj_mct_norms_real[3] = { 1.732, 1.805, 1.573 }; + +const OPJ_FLOAT64 * opj_mct_get_mct_norms() +{ + return opj_mct_norms; +} + +const OPJ_FLOAT64 * opj_mct_get_mct_norms_real() +{ + return opj_mct_norms_real; +} + +/* */ +/* Forward reversible MCT. */ +/* */ +#ifdef __SSE2__ +void opj_mct_encode( + OPJ_INT32* OPJ_RESTRICT c0, + OPJ_INT32* OPJ_RESTRICT c1, + OPJ_INT32* OPJ_RESTRICT c2, + OPJ_SIZE_T n) +{ + OPJ_SIZE_T i; + const OPJ_SIZE_T len = n; + /* buffer are aligned on 16 bytes */ + assert(((size_t)c0 & 0xf) == 0); + assert(((size_t)c1 & 0xf) == 0); + assert(((size_t)c2 & 0xf) == 0); + + for (i = 0; i < (len & ~3U); i += 4) { + __m128i y, u, v; + __m128i r = _mm_load_si128((const __m128i *) & (c0[i])); + __m128i g = _mm_load_si128((const __m128i *) & (c1[i])); + __m128i b = _mm_load_si128((const __m128i *) & (c2[i])); + y = _mm_add_epi32(g, g); + y = _mm_add_epi32(y, b); + y = _mm_add_epi32(y, r); + y = _mm_srai_epi32(y, 2); + u = _mm_sub_epi32(b, g); + v = _mm_sub_epi32(r, g); + _mm_store_si128((__m128i *) & (c0[i]), y); + _mm_store_si128((__m128i *) & (c1[i]), u); + _mm_store_si128((__m128i *) & (c2[i]), v); + } + + for (; i < len; ++i) { + OPJ_INT32 r = c0[i]; + OPJ_INT32 g = c1[i]; + OPJ_INT32 b = c2[i]; + OPJ_INT32 y = (r + (g * 2) + b) >> 2; + OPJ_INT32 u = b - g; + OPJ_INT32 v = r - g; + c0[i] = y; + c1[i] = u; + c2[i] = v; + } +} +#else +void opj_mct_encode( + OPJ_INT32* OPJ_RESTRICT c0, + OPJ_INT32* OPJ_RESTRICT c1, + OPJ_INT32* OPJ_RESTRICT c2, + OPJ_SIZE_T n) +{ + OPJ_SIZE_T i; + const OPJ_SIZE_T len = n; + + for (i = 0; i < len; ++i) { + OPJ_INT32 r = c0[i]; + OPJ_INT32 g = c1[i]; + OPJ_INT32 b = c2[i]; + OPJ_INT32 y = (r + (g * 2) + b) >> 2; + OPJ_INT32 u = b - g; + OPJ_INT32 v = r - g; + c0[i] = y; + c1[i] = u; + c2[i] = v; + } +} +#endif + +/* */ +/* Inverse reversible MCT. */ +/* */ +#ifdef __SSE2__ +void opj_mct_decode( + OPJ_INT32* OPJ_RESTRICT c0, + OPJ_INT32* OPJ_RESTRICT c1, + OPJ_INT32* OPJ_RESTRICT c2, + OPJ_SIZE_T n) +{ + OPJ_SIZE_T i; + const OPJ_SIZE_T len = n; + + for (i = 0; i < (len & ~3U); i += 4) { + __m128i r, g, b; + __m128i y = _mm_load_si128((const __m128i *) & (c0[i])); + __m128i u = _mm_load_si128((const __m128i *) & (c1[i])); + __m128i v = _mm_load_si128((const __m128i *) & (c2[i])); + g = y; + g = _mm_sub_epi32(g, _mm_srai_epi32(_mm_add_epi32(u, v), 2)); + r = _mm_add_epi32(v, g); + b = _mm_add_epi32(u, g); + _mm_store_si128((__m128i *) & (c0[i]), r); + _mm_store_si128((__m128i *) & (c1[i]), g); + _mm_store_si128((__m128i *) & (c2[i]), b); + } + for (; i < len; ++i) { + OPJ_INT32 y = c0[i]; + OPJ_INT32 u = c1[i]; + OPJ_INT32 v = c2[i]; + OPJ_INT32 g = y - ((u + v) >> 2); + OPJ_INT32 r = v + g; + OPJ_INT32 b = u + g; + c0[i] = r; + c1[i] = g; + c2[i] = b; + } +} +#else +void opj_mct_decode( + OPJ_INT32* OPJ_RESTRICT c0, + OPJ_INT32* OPJ_RESTRICT c1, + OPJ_INT32* OPJ_RESTRICT c2, + OPJ_SIZE_T n) +{ + OPJ_SIZE_T i; + for (i = 0; i < n; ++i) { + OPJ_INT32 y = c0[i]; + OPJ_INT32 u = c1[i]; + OPJ_INT32 v = c2[i]; + OPJ_INT32 g = y - ((u + v) >> 2); + OPJ_INT32 r = v + g; + OPJ_INT32 b = u + g; + c0[i] = r; + c1[i] = g; + c2[i] = b; + } +} +#endif + +/* */ +/* Get norm of basis function of reversible MCT. */ +/* */ +OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno) +{ + return opj_mct_norms[compno]; +} + +/* */ +/* Forward irreversible MCT. */ +/* */ +void opj_mct_encode_real( + OPJ_FLOAT32* OPJ_RESTRICT c0, + OPJ_FLOAT32* OPJ_RESTRICT c1, + OPJ_FLOAT32* OPJ_RESTRICT c2, + OPJ_SIZE_T n) +{ + OPJ_SIZE_T i; +#ifdef __SSE__ + const __m128 YR = _mm_set1_ps(0.299f); + const __m128 YG = _mm_set1_ps(0.587f); + const __m128 YB = _mm_set1_ps(0.114f); + const __m128 UR = _mm_set1_ps(-0.16875f); + const __m128 UG = _mm_set1_ps(-0.331260f); + const __m128 UB = _mm_set1_ps(0.5f); + const __m128 VR = _mm_set1_ps(0.5f); + const __m128 VG = _mm_set1_ps(-0.41869f); + const __m128 VB = _mm_set1_ps(-0.08131f); + for (i = 0; i < (n >> 3); i ++) { + __m128 r, g, b, y, u, v; + + r = _mm_load_ps(c0); + g = _mm_load_ps(c1); + b = _mm_load_ps(c2); + y = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, YR), _mm_mul_ps(g, YG)), + _mm_mul_ps(b, YB)); + u = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, UR), _mm_mul_ps(g, UG)), + _mm_mul_ps(b, UB)); + v = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, VR), _mm_mul_ps(g, VG)), + _mm_mul_ps(b, VB)); + _mm_store_ps(c0, y); + _mm_store_ps(c1, u); + _mm_store_ps(c2, v); + c0 += 4; + c1 += 4; + c2 += 4; + + r = _mm_load_ps(c0); + g = _mm_load_ps(c1); + b = _mm_load_ps(c2); + y = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, YR), _mm_mul_ps(g, YG)), + _mm_mul_ps(b, YB)); + u = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, UR), _mm_mul_ps(g, UG)), + _mm_mul_ps(b, UB)); + v = _mm_add_ps(_mm_add_ps(_mm_mul_ps(r, VR), _mm_mul_ps(g, VG)), + _mm_mul_ps(b, VB)); + _mm_store_ps(c0, y); + _mm_store_ps(c1, u); + _mm_store_ps(c2, v); + c0 += 4; + c1 += 4; + c2 += 4; + } + n &= 7; +#endif + for (i = 0; i < n; ++i) { + OPJ_FLOAT32 r = c0[i]; + OPJ_FLOAT32 g = c1[i]; + OPJ_FLOAT32 b = c2[i]; + OPJ_FLOAT32 y = 0.299f * r + 0.587f * g + 0.114f * b; + OPJ_FLOAT32 u = -0.16875f * r - 0.331260f * g + 0.5f * b; + OPJ_FLOAT32 v = 0.5f * r - 0.41869f * g - 0.08131f * b; + c0[i] = y; + c1[i] = u; + c2[i] = v; + } +} + +/* */ +/* Inverse irreversible MCT. */ +/* */ +void opj_mct_decode_real( + OPJ_FLOAT32* OPJ_RESTRICT c0, + OPJ_FLOAT32* OPJ_RESTRICT c1, + OPJ_FLOAT32* OPJ_RESTRICT c2, + OPJ_SIZE_T n) +{ + OPJ_SIZE_T i; +#ifdef __SSE__ + __m128 vrv, vgu, vgv, vbu; + vrv = _mm_set1_ps(1.402f); + vgu = _mm_set1_ps(0.34413f); + vgv = _mm_set1_ps(0.71414f); + vbu = _mm_set1_ps(1.772f); + for (i = 0; i < (n >> 3); ++i) { + __m128 vy, vu, vv; + __m128 vr, vg, vb; + + vy = _mm_load_ps(c0); + vu = _mm_load_ps(c1); + vv = _mm_load_ps(c2); + vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv)); + vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv)); + vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu)); + _mm_store_ps(c0, vr); + _mm_store_ps(c1, vg); + _mm_store_ps(c2, vb); + c0 += 4; + c1 += 4; + c2 += 4; + + vy = _mm_load_ps(c0); + vu = _mm_load_ps(c1); + vv = _mm_load_ps(c2); + vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv)); + vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv)); + vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu)); + _mm_store_ps(c0, vr); + _mm_store_ps(c1, vg); + _mm_store_ps(c2, vb); + c0 += 4; + c1 += 4; + c2 += 4; + } + n &= 7; +#endif + for (i = 0; i < n; ++i) { + OPJ_FLOAT32 y = c0[i]; + OPJ_FLOAT32 u = c1[i]; + OPJ_FLOAT32 v = c2[i]; + OPJ_FLOAT32 r = y + (v * 1.402f); + OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f)); + OPJ_FLOAT32 b = y + (u * 1.772f); + c0[i] = r; + c1[i] = g; + c2[i] = b; + } +} + +/* */ +/* Get norm of basis function of irreversible MCT. */ +/* */ +OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno) +{ + return opj_mct_norms_real[compno]; +} + + +OPJ_BOOL opj_mct_encode_custom( + OPJ_BYTE * pCodingdata, + OPJ_SIZE_T n, + OPJ_BYTE ** pData, + OPJ_UINT32 pNbComp, + OPJ_UINT32 isSigned) +{ + OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata; + OPJ_SIZE_T i; + OPJ_UINT32 j; + OPJ_UINT32 k; + OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp; + OPJ_INT32 * lCurrentData = 00; + OPJ_INT32 * lCurrentMatrix = 00; + OPJ_INT32 ** lData = (OPJ_INT32 **) pData; + OPJ_UINT32 lMultiplicator = 1 << 13; + OPJ_INT32 * lMctPtr; + + OPJ_ARG_NOT_USED(isSigned); + + lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof( + OPJ_INT32)); + if (! lCurrentData) { + return OPJ_FALSE; + } + + lCurrentMatrix = lCurrentData + pNbComp; + + for (i = 0; i < lNbMatCoeff; ++i) { + lCurrentMatrix[i] = (OPJ_INT32)(*(lMct++) * (OPJ_FLOAT32)lMultiplicator); + } + + for (i = 0; i < n; ++i) { + lMctPtr = lCurrentMatrix; + for (j = 0; j < pNbComp; ++j) { + lCurrentData[j] = (*(lData[j])); + } + + for (j = 0; j < pNbComp; ++j) { + *(lData[j]) = 0; + for (k = 0; k < pNbComp; ++k) { + *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]); + ++lMctPtr; + } + + ++lData[j]; + } + } + + opj_free(lCurrentData); + + return OPJ_TRUE; +} + +OPJ_BOOL opj_mct_decode_custom( + OPJ_BYTE * pDecodingData, + OPJ_SIZE_T n, + OPJ_BYTE ** pData, + OPJ_UINT32 pNbComp, + OPJ_UINT32 isSigned) +{ + OPJ_FLOAT32 * lMct; + OPJ_SIZE_T i; + OPJ_UINT32 j; + OPJ_UINT32 k; + + OPJ_FLOAT32 * lCurrentData = 00; + OPJ_FLOAT32 * lCurrentResult = 00; + OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData; + + OPJ_ARG_NOT_USED(isSigned); + + lCurrentData = (OPJ_FLOAT32 *) opj_malloc(2 * pNbComp * sizeof(OPJ_FLOAT32)); + if (! lCurrentData) { + return OPJ_FALSE; + } + lCurrentResult = lCurrentData + pNbComp; + + for (i = 0; i < n; ++i) { + lMct = (OPJ_FLOAT32 *) pDecodingData; + for (j = 0; j < pNbComp; ++j) { + lCurrentData[j] = (OPJ_FLOAT32)(*(lData[j])); + } + for (j = 0; j < pNbComp; ++j) { + lCurrentResult[j] = 0; + for (k = 0; k < pNbComp; ++k) { + lCurrentResult[j] += *(lMct++) * lCurrentData[k]; + } + *(lData[j]++) = (OPJ_FLOAT32)(lCurrentResult[j]); + } + } + opj_free(lCurrentData); + return OPJ_TRUE; +} + +void opj_calculate_norms(OPJ_FLOAT64 * pNorms, + OPJ_UINT32 pNbComps, + OPJ_FLOAT32 * pMatrix) +{ + OPJ_UINT32 i, j, lIndex; + OPJ_FLOAT32 lCurrentValue; + OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms; + OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix; + + for (i = 0; i < pNbComps; ++i) { + lNorms[i] = 0; + lIndex = i; + + for (j = 0; j < pNbComps; ++j) { + lCurrentValue = lMatrix[lIndex]; + lIndex += pNbComps; + lNorms[i] += lCurrentValue * lCurrentValue; + } + lNorms[i] = sqrt(lNorms[i]); + } +} diff --git a/cpp/3rd_party/openjpeg/openjp2/mct.h b/cpp/3rd_party/openjpeg/openjp2/mct.h new file mode 100644 index 0000000000..3e1f5e4946 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/mct.h @@ -0,0 +1,160 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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 OPJ_MCT_H +#define OPJ_MCT_H +/** +@file mct.h +@brief Implementation of a multi-component transforms (MCT) + +The functions in MCT.C have for goal to realize reversible and irreversible multicomponent +transform. The functions in MCT.C are used by some function in TCD.C. +*/ + +/** @defgroup MCT MCT - Implementation of a multi-component transform */ +/*@{*/ + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ +/** +Apply a reversible multi-component transform to an image +@param c0 Samples for red component +@param c1 Samples for green component +@param c2 Samples blue component +@param n Number of samples for each component +*/ +void opj_mct_encode(OPJ_INT32* OPJ_RESTRICT c0, OPJ_INT32* OPJ_RESTRICT c1, + OPJ_INT32* OPJ_RESTRICT c2, OPJ_SIZE_T n); +/** +Apply a reversible multi-component inverse transform to an image +@param c0 Samples for luminance component +@param c1 Samples for red chrominance component +@param c2 Samples for blue chrominance component +@param n Number of samples for each component +*/ +void opj_mct_decode(OPJ_INT32* OPJ_RESTRICT c0, OPJ_INT32* OPJ_RESTRICT c1, + OPJ_INT32* OPJ_RESTRICT c2, OPJ_SIZE_T n); +/** +Get norm of the basis function used for the reversible multi-component transform +@param compno Number of the component (0->Y, 1->U, 2->V) +@return +*/ +OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno); + +/** +Apply an irreversible multi-component transform to an image +@param c0 Samples for red component +@param c1 Samples for green component +@param c2 Samples blue component +@param n Number of samples for each component +*/ +void opj_mct_encode_real(OPJ_FLOAT32* OPJ_RESTRICT c0, + OPJ_FLOAT32* OPJ_RESTRICT c1, + OPJ_FLOAT32* OPJ_RESTRICT c2, OPJ_SIZE_T n); +/** +Apply an irreversible multi-component inverse transform to an image +@param c0 Samples for luminance component +@param c1 Samples for red chrominance component +@param c2 Samples for blue chrominance component +@param n Number of samples for each component +*/ +void opj_mct_decode_real(OPJ_FLOAT32* OPJ_RESTRICT c0, + OPJ_FLOAT32* OPJ_RESTRICT c1, OPJ_FLOAT32* OPJ_RESTRICT c2, OPJ_SIZE_T n); +/** +Get norm of the basis function used for the irreversible multi-component transform +@param compno Number of the component (0->Y, 1->U, 2->V) +@return +*/ +OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno); + +/** +FIXME DOC +@param p_coding_data MCT data +@param n size of components +@param p_data components +@param p_nb_comp nb of components (i.e. size of p_data) +@param is_signed tells if the data is signed +@return OPJ_FALSE if function encounter a problem, OPJ_TRUE otherwise +*/ +OPJ_BOOL opj_mct_encode_custom( + OPJ_BYTE * p_coding_data, + OPJ_SIZE_T n, + OPJ_BYTE ** p_data, + OPJ_UINT32 p_nb_comp, + OPJ_UINT32 is_signed); +/** +FIXME DOC +@param pDecodingData MCT data +@param n size of components +@param pData components +@param pNbComp nb of components (i.e. size of p_data) +@param isSigned tells if the data is signed +@return OPJ_FALSE if function encounter a problem, OPJ_TRUE otherwise +*/ +OPJ_BOOL opj_mct_decode_custom( + OPJ_BYTE * pDecodingData, + OPJ_SIZE_T n, + OPJ_BYTE ** pData, + OPJ_UINT32 pNbComp, + OPJ_UINT32 isSigned); +/** +FIXME DOC +@param pNorms MCT data +@param p_nb_comps size of components +@param pMatrix components +@return +*/ +void opj_calculate_norms(OPJ_FLOAT64 * pNorms, + OPJ_UINT32 p_nb_comps, + OPJ_FLOAT32 * pMatrix); +/** +FIXME DOC +*/ +const OPJ_FLOAT64 * opj_mct_get_mct_norms(void); +/** +FIXME DOC +*/ +const OPJ_FLOAT64 * opj_mct_get_mct_norms_real(void); +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_MCT_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/mqc.c b/cpp/3rd_party/openjpeg/openjp2/mqc.c new file mode 100644 index 0000000000..4cbfabd033 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/mqc.c @@ -0,0 +1,524 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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 + +/** @defgroup MQC MQC - Implementation of an MQ-Coder */ +/*@{*/ + +/** @name Local static functions */ +/*@{*/ + +/** +Fill mqc->c with 1's for flushing +@param mqc MQC handle +*/ +static void opj_mqc_setbits(opj_mqc_t *mqc); +/*@}*/ + +/*@}*/ + +/* */ +/* This array defines all the possible states for a context. */ +/* */ +static const opj_mqc_state_t mqc_states[47 * 2] = { + {0x5601, 0, &mqc_states[2], &mqc_states[3]}, + {0x5601, 1, &mqc_states[3], &mqc_states[2]}, + {0x3401, 0, &mqc_states[4], &mqc_states[12]}, + {0x3401, 1, &mqc_states[5], &mqc_states[13]}, + {0x1801, 0, &mqc_states[6], &mqc_states[18]}, + {0x1801, 1, &mqc_states[7], &mqc_states[19]}, + {0x0ac1, 0, &mqc_states[8], &mqc_states[24]}, + {0x0ac1, 1, &mqc_states[9], &mqc_states[25]}, + {0x0521, 0, &mqc_states[10], &mqc_states[58]}, + {0x0521, 1, &mqc_states[11], &mqc_states[59]}, + {0x0221, 0, &mqc_states[76], &mqc_states[66]}, + {0x0221, 1, &mqc_states[77], &mqc_states[67]}, + {0x5601, 0, &mqc_states[14], &mqc_states[13]}, + {0x5601, 1, &mqc_states[15], &mqc_states[12]}, + {0x5401, 0, &mqc_states[16], &mqc_states[28]}, + {0x5401, 1, &mqc_states[17], &mqc_states[29]}, + {0x4801, 0, &mqc_states[18], &mqc_states[28]}, + {0x4801, 1, &mqc_states[19], &mqc_states[29]}, + {0x3801, 0, &mqc_states[20], &mqc_states[28]}, + {0x3801, 1, &mqc_states[21], &mqc_states[29]}, + {0x3001, 0, &mqc_states[22], &mqc_states[34]}, + {0x3001, 1, &mqc_states[23], &mqc_states[35]}, + {0x2401, 0, &mqc_states[24], &mqc_states[36]}, + {0x2401, 1, &mqc_states[25], &mqc_states[37]}, + {0x1c01, 0, &mqc_states[26], &mqc_states[40]}, + {0x1c01, 1, &mqc_states[27], &mqc_states[41]}, + {0x1601, 0, &mqc_states[58], &mqc_states[42]}, + {0x1601, 1, &mqc_states[59], &mqc_states[43]}, + {0x5601, 0, &mqc_states[30], &mqc_states[29]}, + {0x5601, 1, &mqc_states[31], &mqc_states[28]}, + {0x5401, 0, &mqc_states[32], &mqc_states[28]}, + {0x5401, 1, &mqc_states[33], &mqc_states[29]}, + {0x5101, 0, &mqc_states[34], &mqc_states[30]}, + {0x5101, 1, &mqc_states[35], &mqc_states[31]}, + {0x4801, 0, &mqc_states[36], &mqc_states[32]}, + {0x4801, 1, &mqc_states[37], &mqc_states[33]}, + {0x3801, 0, &mqc_states[38], &mqc_states[34]}, + {0x3801, 1, &mqc_states[39], &mqc_states[35]}, + {0x3401, 0, &mqc_states[40], &mqc_states[36]}, + {0x3401, 1, &mqc_states[41], &mqc_states[37]}, + {0x3001, 0, &mqc_states[42], &mqc_states[38]}, + {0x3001, 1, &mqc_states[43], &mqc_states[39]}, + {0x2801, 0, &mqc_states[44], &mqc_states[38]}, + {0x2801, 1, &mqc_states[45], &mqc_states[39]}, + {0x2401, 0, &mqc_states[46], &mqc_states[40]}, + {0x2401, 1, &mqc_states[47], &mqc_states[41]}, + {0x2201, 0, &mqc_states[48], &mqc_states[42]}, + {0x2201, 1, &mqc_states[49], &mqc_states[43]}, + {0x1c01, 0, &mqc_states[50], &mqc_states[44]}, + {0x1c01, 1, &mqc_states[51], &mqc_states[45]}, + {0x1801, 0, &mqc_states[52], &mqc_states[46]}, + {0x1801, 1, &mqc_states[53], &mqc_states[47]}, + {0x1601, 0, &mqc_states[54], &mqc_states[48]}, + {0x1601, 1, &mqc_states[55], &mqc_states[49]}, + {0x1401, 0, &mqc_states[56], &mqc_states[50]}, + {0x1401, 1, &mqc_states[57], &mqc_states[51]}, + {0x1201, 0, &mqc_states[58], &mqc_states[52]}, + {0x1201, 1, &mqc_states[59], &mqc_states[53]}, + {0x1101, 0, &mqc_states[60], &mqc_states[54]}, + {0x1101, 1, &mqc_states[61], &mqc_states[55]}, + {0x0ac1, 0, &mqc_states[62], &mqc_states[56]}, + {0x0ac1, 1, &mqc_states[63], &mqc_states[57]}, + {0x09c1, 0, &mqc_states[64], &mqc_states[58]}, + {0x09c1, 1, &mqc_states[65], &mqc_states[59]}, + {0x08a1, 0, &mqc_states[66], &mqc_states[60]}, + {0x08a1, 1, &mqc_states[67], &mqc_states[61]}, + {0x0521, 0, &mqc_states[68], &mqc_states[62]}, + {0x0521, 1, &mqc_states[69], &mqc_states[63]}, + {0x0441, 0, &mqc_states[70], &mqc_states[64]}, + {0x0441, 1, &mqc_states[71], &mqc_states[65]}, + {0x02a1, 0, &mqc_states[72], &mqc_states[66]}, + {0x02a1, 1, &mqc_states[73], &mqc_states[67]}, + {0x0221, 0, &mqc_states[74], &mqc_states[68]}, + {0x0221, 1, &mqc_states[75], &mqc_states[69]}, + {0x0141, 0, &mqc_states[76], &mqc_states[70]}, + {0x0141, 1, &mqc_states[77], &mqc_states[71]}, + {0x0111, 0, &mqc_states[78], &mqc_states[72]}, + {0x0111, 1, &mqc_states[79], &mqc_states[73]}, + {0x0085, 0, &mqc_states[80], &mqc_states[74]}, + {0x0085, 1, &mqc_states[81], &mqc_states[75]}, + {0x0049, 0, &mqc_states[82], &mqc_states[76]}, + {0x0049, 1, &mqc_states[83], &mqc_states[77]}, + {0x0025, 0, &mqc_states[84], &mqc_states[78]}, + {0x0025, 1, &mqc_states[85], &mqc_states[79]}, + {0x0015, 0, &mqc_states[86], &mqc_states[80]}, + {0x0015, 1, &mqc_states[87], &mqc_states[81]}, + {0x0009, 0, &mqc_states[88], &mqc_states[82]}, + {0x0009, 1, &mqc_states[89], &mqc_states[83]}, + {0x0005, 0, &mqc_states[90], &mqc_states[84]}, + {0x0005, 1, &mqc_states[91], &mqc_states[85]}, + {0x0001, 0, &mqc_states[90], &mqc_states[86]}, + {0x0001, 1, &mqc_states[91], &mqc_states[87]}, + {0x5601, 0, &mqc_states[92], &mqc_states[92]}, + {0x5601, 1, &mqc_states[93], &mqc_states[93]}, +}; + +/* +========================================================== + local functions +========================================================== +*/ + +static void opj_mqc_setbits(opj_mqc_t *mqc) +{ + OPJ_UINT32 tempc = mqc->c + mqc->a; + mqc->c |= 0xffff; + if (mqc->c >= tempc) { + mqc->c -= 0x8000; + } +} + +/* +========================================================== + MQ-Coder interface +========================================================== +*/ + +OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc) +{ + const ptrdiff_t diff = mqc->bp - mqc->start; +#if 0 + assert(diff <= 0xffffffff && diff >= 0); /* UINT32_MAX */ +#endif + return (OPJ_UINT32)diff; +} + +void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) +{ + /* To avoid the curctx pointer to be dangling, but not strictly */ + /* required as the current context is always set before encoding */ + opj_mqc_setcurctx(mqc, 0); + + /* As specified in Figure C.10 - Initialization of the encoder */ + /* (C.2.8 Initialization of the encoder (INITENC)) */ + mqc->a = 0x8000; + mqc->c = 0; + /* Yes, we point before the start of the buffer, but this is safe */ + /* given opj_tcd_code_block_enc_allocate_data() */ + mqc->bp = bp - 1; + mqc->ct = 12; + /* At this point we should test *(mqc->bp) against 0xFF, but this is not */ + /* necessary, as this is only used at the beginning of the code block */ + /* and our initial fake byte is set at 0 */ + assert(*(mqc->bp) != 0xff); + + mqc->start = bp; + mqc->end_of_byte_stream_counter = 0; +} + + +void opj_mqc_flush(opj_mqc_t *mqc) +{ + /* C.2.9 Termination of coding (FLUSH) */ + /* Figure C.11 – FLUSH procedure */ + opj_mqc_setbits(mqc); + mqc->c <<= mqc->ct; + opj_mqc_byteout(mqc); + mqc->c <<= mqc->ct; + opj_mqc_byteout(mqc); + + /* It is forbidden that a coding pass ends with 0xff */ + if (*mqc->bp != 0xff) { + /* Advance pointer so that opj_mqc_numbytes() returns a valid value */ + mqc->bp++; + } +} + +void opj_mqc_bypass_init_enc(opj_mqc_t *mqc) +{ + /* This function is normally called after at least one opj_mqc_flush() */ + /* which will have advance mqc->bp by at least 2 bytes beyond its */ + /* initial position */ + assert(mqc->bp >= mqc->start); + mqc->c = 0; + /* in theory we should initialize to 8, but use this special value */ + /* as a hint that opj_mqc_bypass_enc() has never been called, so */ + /* as to avoid the 0xff 0x7f elimination trick in opj_mqc_bypass_flush_enc() */ + /* to trigger when we don't have output any bit during this bypass sequence */ + /* Any value > 8 will do */ + mqc->ct = BYPASS_CT_INIT; + /* Given that we are called after opj_mqc_flush(), the previous byte */ + /* cannot be 0xff. */ + assert(mqc->bp[-1] != 0xff); +} + +void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) +{ + if (mqc->ct == BYPASS_CT_INIT) { + mqc->ct = 8; + } + mqc->ct--; + mqc->c = mqc->c + (d << mqc->ct); + if (mqc->ct == 0) { + *mqc->bp = (OPJ_BYTE)mqc->c; + mqc->ct = 8; + /* If the previous byte was 0xff, make sure that the next msb is 0 */ + if (*mqc->bp == 0xff) { + mqc->ct = 7; + } + mqc->bp++; + mqc->c = 0; + } +} + +OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm) +{ + return (mqc->ct < 7 || + (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0; +} + +void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm) +{ + /* Is there any bit remaining to be flushed ? */ + /* If the last output byte is 0xff, we can discard it, unless */ + /* erterm is required (I'm not completely sure why in erterm */ + /* we must output 0xff 0x2a if the last byte was 0xff instead of */ + /* discarding it, but Kakadu requires it when decoding */ + /* in -fussy mode) */ + if (mqc->ct < 7 || (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) { + OPJ_BYTE bit_value = 0; + /* If so, fill the remaining lsbs with an alternating sequence of */ + /* 0,1,... */ + /* Note: it seems the standard only requires that for a ERTERM flush */ + /* and doesn't specify what to do for a regular BYPASS flush */ + while (mqc->ct > 0) { + mqc->ct--; + mqc->c += (OPJ_UINT32)(bit_value << mqc->ct); + bit_value = (OPJ_BYTE)(1U - bit_value); + } + *mqc->bp = (OPJ_BYTE)mqc->c; + /* Advance pointer so that opj_mqc_numbytes() returns a valid value */ + mqc->bp++; + } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) { + /* Discard last 0xff */ + assert(!erterm); + mqc->bp --; + } else if (mqc->ct == 8 && !erterm && + mqc->bp[-1] == 0x7f && mqc->bp[-2] == 0xff) { + /* Tiny optimization: discard terminating 0xff 0x7f since it is */ + /* interpreted as 0xff 0x7f [0xff 0xff] by the decoder, and given */ + /* the bit stuffing, in fact as 0xff 0xff [0xff ..] */ + /* Happens once on opj_compress -i ../MAPA.tif -o MAPA.j2k -M 1 */ + mqc->bp -= 2; + } + + assert(mqc->bp[-1] != 0xff); +} + +void opj_mqc_reset_enc(opj_mqc_t *mqc) +{ + opj_mqc_resetstates(mqc); + opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); + opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); + opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); +} + +#ifdef notdef +OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc) +{ + OPJ_UINT32 correction = 1; + + /* */ + OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct); + mqc->c <<= mqc->ct; + while (n > 0) { + opj_mqc_byteout(mqc); + n -= (OPJ_INT32)mqc->ct; + mqc->c <<= mqc->ct; + } + opj_mqc_byteout(mqc); + + return correction; +} +#endif + +void opj_mqc_restart_init_enc(opj_mqc_t *mqc) +{ + /* */ + + /* As specified in Figure C.10 - Initialization of the encoder */ + /* (C.2.8 Initialization of the encoder (INITENC)) */ + mqc->a = 0x8000; + mqc->c = 0; + mqc->ct = 12; + /* This function is normally called after at least one opj_mqc_flush() */ + /* which will have advance mqc->bp by at least 2 bytes beyond its */ + /* initial position */ + mqc->bp --; + assert(mqc->bp >= mqc->start - 1); + assert(*mqc->bp != 0xff); + if (*mqc->bp == 0xff) { + mqc->ct = 13; + } +} + +void opj_mqc_erterm_enc(opj_mqc_t *mqc) +{ + OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1); + + while (k > 0) { + mqc->c <<= mqc->ct; + mqc->ct = 0; + opj_mqc_byteout(mqc); + k -= (OPJ_INT32)mqc->ct; + } + + if (*mqc->bp != 0xff) { + opj_mqc_byteout(mqc); + } +} + +static INLINE void opj_mqc_renorme(opj_mqc_t *mqc) +{ + opj_mqc_renorme_macro(mqc, mqc->a, mqc->c, mqc->ct); +} + +/** +Encode the most probable symbol +@param mqc MQC handle +*/ +static INLINE void opj_mqc_codemps(opj_mqc_t *mqc) +{ + opj_mqc_codemps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct); +} + +/** +Encode the most least symbol +@param mqc MQC handle +*/ +static INLINE void opj_mqc_codelps(opj_mqc_t *mqc) +{ + opj_mqc_codelps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct); +} + +/** +Encode a symbol using the MQ-coder +@param mqc MQC handle +@param d The symbol to be encoded (0 or 1) +*/ +static INLINE void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) +{ + if ((*mqc->curctx)->mps == d) { + opj_mqc_codemps(mqc); + } else { + opj_mqc_codelps(mqc); + } +} + +void opj_mqc_segmark_enc(opj_mqc_t *mqc) +{ + OPJ_UINT32 i; + opj_mqc_setcurctx(mqc, 18); + + for (i = 1; i < 5; i++) { + opj_mqc_encode(mqc, i % 2); + } +} + +static void opj_mqc_init_dec_common(opj_mqc_t *mqc, + OPJ_BYTE *bp, + OPJ_UINT32 len, + OPJ_UINT32 extra_writable_bytes) +{ + (void)extra_writable_bytes; + + assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA); + mqc->start = bp; + mqc->end = bp + len; + /* Insert an artificial 0xFF 0xFF marker at end of the code block */ + /* data so that the bytein routines stop on it. This saves us comparing */ + /* the bp and end pointers */ + /* But before inserting it, backup th bytes we will overwrite */ + memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA); + mqc->end[0] = 0xFF; + mqc->end[1] = 0xFF; + mqc->bp = bp; +} +void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len, + OPJ_UINT32 extra_writable_bytes) +{ + /* Implements ISO 15444-1 C.3.5 Initialization of the decoder (INITDEC) */ + /* Note: alternate "J.1 - Initialization of the software-conventions */ + /* decoder" has been tried, but does */ + /* not bring any improvement. */ + /* See https://github.com/uclouvain/openjpeg/issues/921 */ + opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes); + opj_mqc_setcurctx(mqc, 0); + mqc->end_of_byte_stream_counter = 0; + if (len == 0) { + mqc->c = 0xff << 16; + } else { + mqc->c = (OPJ_UINT32)(*mqc->bp << 16); + } + + opj_mqc_bytein(mqc); + mqc->c <<= 7; + mqc->ct -= 7; + mqc->a = 0x8000; +} + + +void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len, + OPJ_UINT32 extra_writable_bytes) +{ + opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes); + mqc->c = 0; + mqc->ct = 0; +} + + +void opq_mqc_finish_dec(opj_mqc_t *mqc) +{ + /* Restore the bytes overwritten by opj_mqc_init_dec_common() */ + memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA); +} + +void opj_mqc_resetstates(opj_mqc_t *mqc) +{ + OPJ_UINT32 i; + for (i = 0; i < MQC_NUMCTXS; i++) { + mqc->ctxs[i] = mqc_states; + } +} + +void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, + OPJ_INT32 prob) +{ + mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)]; +} + +void opj_mqc_byteout(opj_mqc_t *mqc) +{ + /* bp is initialized to start - 1 in opj_mqc_init_enc() */ + /* but this is safe, see opj_tcd_code_block_enc_allocate_data() */ + assert(mqc->bp >= mqc->start - 1); + if (*mqc->bp == 0xff) { + mqc->bp++; + *mqc->bp = (OPJ_BYTE)(mqc->c >> 20); + mqc->c &= 0xfffff; + mqc->ct = 7; + } else { + if ((mqc->c & 0x8000000) == 0) { + mqc->bp++; + *mqc->bp = (OPJ_BYTE)(mqc->c >> 19); + mqc->c &= 0x7ffff; + mqc->ct = 8; + } else { + (*mqc->bp)++; + if (*mqc->bp == 0xff) { + mqc->c &= 0x7ffffff; + mqc->bp++; + *mqc->bp = (OPJ_BYTE)(mqc->c >> 20); + mqc->c &= 0xfffff; + mqc->ct = 7; + } else { + mqc->bp++; + *mqc->bp = (OPJ_BYTE)(mqc->c >> 19); + mqc->c &= 0x7ffff; + mqc->ct = 8; + } + } + } +} \ No newline at end of file diff --git a/cpp/3rd_party/openjpeg/openjp2/mqc.h b/cpp/3rd_party/openjpeg/openjp2/mqc.h new file mode 100644 index 0000000000..9850fed031 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/mqc.h @@ -0,0 +1,268 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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 OPJ_MQC_H +#define OPJ_MQC_H + +#include "opj_common.h" + +/** +@file mqc.h +@brief Implementation of an MQ-Coder (MQC) + +The functions in MQC.C have for goal to realize the MQ-coder operations. The functions +in MQC.C are used by some function in T1.C. +*/ + +/** @defgroup MQC MQC - Implementation of an MQ-Coder */ +/*@{*/ + +/** +This struct defines the state of a context. +*/ +typedef struct opj_mqc_state { + /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */ + OPJ_UINT32 qeval; + /** the Most Probable Symbol (0 or 1) */ + OPJ_UINT32 mps; + /** next state if the next encoded symbol is the MPS */ + const struct opj_mqc_state *nmps; + /** next state if the next encoded symbol is the LPS */ + const struct opj_mqc_state *nlps; +} opj_mqc_state_t; + +#define MQC_NUMCTXS 19 + +/** +MQ coder +*/ +typedef struct opj_mqc { + /** temporary buffer where bits are coded or decoded */ + OPJ_UINT32 c; + /** only used by MQ decoder */ + OPJ_UINT32 a; + /** number of bits already read or free to write */ + OPJ_UINT32 ct; + /* only used by decoder, to count the number of times a terminating 0xFF >0x8F marker is read */ + OPJ_UINT32 end_of_byte_stream_counter; + /** pointer to the current position in the buffer */ + OPJ_BYTE *bp; + /** pointer to the start of the buffer */ + OPJ_BYTE *start; + /** pointer to the end of the buffer */ + OPJ_BYTE *end; + /** Array of contexts */ + const opj_mqc_state_t *ctxs[MQC_NUMCTXS]; + /** Active context */ + const opj_mqc_state_t **curctx; + /* lut_ctxno_zc shifted by (1 << 9) * bandno */ + const OPJ_BYTE* lut_ctxno_zc_orient; + /** Original value of the 2 bytes at end[0] and end[1] */ + OPJ_BYTE backup[OPJ_COMMON_CBLK_DATA_EXTRA]; +} opj_mqc_t; + +#define BYPASS_CT_INIT 0xDEADBEEF + +#include "mqc_inl.h" + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Return the number of bytes written/read since initialisation +@param mqc MQC handle +@return Returns the number of bytes already encoded +*/ +OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc); +/** +Reset the states of all the context of the coder/decoder +(each context is set to a state where 0 and 1 are more or less equiprobable) +@param mqc MQC handle +*/ +void opj_mqc_resetstates(opj_mqc_t *mqc); +/** +Set the state of a particular context +@param mqc MQC handle +@param ctxno Number that identifies the context +@param msb The MSB of the new state of the context +@param prob Number that identifies the probability of the symbols for the new state of the context +*/ +void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, + OPJ_INT32 prob); +/** +Initialize the encoder +@param mqc MQC handle +@param bp Pointer to the start of the buffer where the bytes will be written +*/ +void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp); +/** +Set the current context used for coding/decoding +@param mqc MQC handle +@param ctxno Number that identifies the context +*/ +#define opj_mqc_setcurctx(mqc, ctxno) (mqc)->curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)] + +/** +Flush the encoder, so that all remaining data is written +@param mqc MQC handle +*/ +void opj_mqc_flush(opj_mqc_t *mqc); +/** +BYPASS mode switch, initialization operation. +JPEG 2000 p 505. +@param mqc MQC handle +*/ +void opj_mqc_bypass_init_enc(opj_mqc_t *mqc); + +/** Return number of extra bytes to add to opj_mqc_numbytes() for the² + size of a non-terminating BYPASS pass +@param mqc MQC handle +@param erterm 1 if ERTERM is enabled, 0 otherwise +*/ +OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm); + +/** +BYPASS mode switch, coding operation. +JPEG 2000 p 505. +@param mqc MQC handle +@param d The symbol to be encoded (0 or 1) +*/ +void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d); +/** +BYPASS mode switch, flush operation +@param mqc MQC handle +@param erterm 1 if ERTERM is enabled, 0 otherwise +*/ +void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm); +/** +RESET mode switch +@param mqc MQC handle +*/ +void opj_mqc_reset_enc(opj_mqc_t *mqc); + +#ifdef notdef +/** +RESTART mode switch (TERMALL) +@param mqc MQC handle +@return Returns 1 (always) +*/ +OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc); +#endif + +/** +RESTART mode switch (TERMALL) reinitialisation +@param mqc MQC handle +*/ +void opj_mqc_restart_init_enc(opj_mqc_t *mqc); +/** +ERTERM mode switch (PTERM) +@param mqc MQC handle +*/ +void opj_mqc_erterm_enc(opj_mqc_t *mqc); +/** +SEGMARK mode switch (SEGSYM) +@param mqc MQC handle +*/ +void opj_mqc_segmark_enc(opj_mqc_t *mqc); + +/** +Initialize the decoder for MQ decoding. + +opj_mqc_finish_dec() must be absolutely called after finishing the decoding +passes, so as to restore the bytes temporarily overwritten. + +@param mqc MQC handle +@param bp Pointer to the start of the buffer from which the bytes will be read + Note that OPJ_COMMON_CBLK_DATA_EXTRA bytes at the end of the buffer + will be temporarily overwritten with an artificial 0xFF 0xFF marker. + (they will be backuped in the mqc structure to be restored later) + So bp must be at least len + OPJ_COMMON_CBLK_DATA_EXTRA large, and + writable. +@param len Length of the input buffer +@param extra_writable_bytes Indicate how many bytes after len are writable. + This is to indicate your consent that bp must be + large enough. +*/ +void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len, + OPJ_UINT32 extra_writable_bytes); + +/** +Initialize the decoder for RAW decoding. + +opj_mqc_finish_dec() must be absolutely called after finishing the decoding +passes, so as to restore the bytes temporarily overwritten. + +@param mqc MQC handle +@param bp Pointer to the start of the buffer from which the bytes will be read + Note that OPJ_COMMON_CBLK_DATA_EXTRA bytes at the end of the buffer + will be temporarily overwritten with an artificial 0xFF 0xFF marker. + (they will be backuped in the mqc structure to be restored later) + So bp must be at least len + OPJ_COMMON_CBLK_DATA_EXTRA large, and + writable. +@param len Length of the input buffer +@param extra_writable_bytes Indicate how many bytes after len are writable. + This is to indicate your consent that bp must be + large enough. +*/ +void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len, + OPJ_UINT32 extra_writable_bytes); + + +/** +Terminate RAW/MQC decoding + +This restores the bytes temporarily overwritten by opj_mqc_init_dec()/ +opj_mqc_raw_init_dec() + +@param mqc MQC handle +*/ +void opq_mqc_finish_dec(opj_mqc_t *mqc); + +/** +Decode a symbol +@param mqc MQC handle +@return Returns the decoded symbol (0 or 1) +*/ +/*static INLINE OPJ_UINT32 opj_mqc_decode(opj_mqc_t * const mqc);*/ +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_MQC_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/mqc_inl.h b/cpp/3rd_party/openjpeg/openjp2/mqc_inl.h new file mode 100644 index 0000000000..0031b94be3 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/mqc_inl.h @@ -0,0 +1,282 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * 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 OPJ_MQC_INL_H +#define OPJ_MQC_INL_H + +/* For internal use of opj_mqc_decode_macro() */ +#define opj_mqc_mpsexchange_macro(d, curctx, a) \ +{ \ + if (a < (*curctx)->qeval) { \ + d = !((*curctx)->mps); \ + *curctx = (*curctx)->nlps; \ + } else { \ + d = (*curctx)->mps; \ + *curctx = (*curctx)->nmps; \ + } \ +} + +/* For internal use of opj_mqc_decode_macro() */ +#define opj_mqc_lpsexchange_macro(d, curctx, a) \ +{ \ + if (a < (*curctx)->qeval) { \ + a = (*curctx)->qeval; \ + d = (*curctx)->mps; \ + *curctx = (*curctx)->nmps; \ + } else { \ + a = (*curctx)->qeval; \ + d = !((*curctx)->mps); \ + *curctx = (*curctx)->nlps; \ + } \ +} + + +/** +Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN +@param mqc MQC handle +@return Returns the decoded symbol (0 or 1) +*/ +static INLINE OPJ_UINT32 opj_mqc_raw_decode(opj_mqc_t *mqc) +{ + OPJ_UINT32 d; + if (mqc->ct == 0) { + /* Given opj_mqc_raw_init_dec() we know that at some point we will */ + /* have a 0xFF 0xFF artificial marker */ + if (mqc->c == 0xff) { + if (*mqc->bp > 0x8f) { + mqc->c = 0xff; + mqc->ct = 8; + } else { + mqc->c = *mqc->bp; + mqc->bp ++; + mqc->ct = 7; + } + } else { + mqc->c = *mqc->bp; + mqc->bp ++; + mqc->ct = 8; + } + } + mqc->ct--; + d = ((OPJ_UINT32)mqc->c >> mqc->ct) & 0x01U; + + return d; +} + + +#define opj_mqc_bytein_macro(mqc, c, ct) \ +{ \ + OPJ_UINT32 l_c; \ + /* Given opj_mqc_init_dec() we know that at some point we will */ \ + /* have a 0xFF 0xFF artificial marker */ \ + l_c = *(mqc->bp + 1); \ + if (*mqc->bp == 0xff) { \ + if (l_c > 0x8f) { \ + c += 0xff00; \ + ct = 8; \ + mqc->end_of_byte_stream_counter ++; \ + } else { \ + mqc->bp++; \ + c += l_c << 9; \ + ct = 7; \ + } \ + } else { \ + mqc->bp++; \ + c += l_c << 8; \ + ct = 8; \ + } \ +} + +/* For internal use of opj_mqc_decode_macro() */ +#define opj_mqc_renormd_macro(mqc, a, c, ct) \ +{ \ + do { \ + if (ct == 0) { \ + opj_mqc_bytein_macro(mqc, c, ct); \ + } \ + a <<= 1; \ + c <<= 1; \ + ct--; \ + } while (a < 0x8000); \ +} + +#define opj_mqc_decode_macro(d, mqc, curctx, a, c, ct) \ +{ \ + /* Implements ISO 15444-1 C.3.2 Decoding a decision (DECODE) */ \ + /* Note: alternate "J.2 - Decoding an MPS or an LPS in the */ \ + /* software-conventions decoder" has been tried, but does not bring any */ \ + /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */ \ + a -= (*curctx)->qeval; \ + if ((c >> 16) < (*curctx)->qeval) { \ + opj_mqc_lpsexchange_macro(d, curctx, a); \ + opj_mqc_renormd_macro(mqc, a, c, ct); \ + } else { \ + c -= (*curctx)->qeval << 16; \ + if ((a & 0x8000) == 0) { \ + opj_mqc_mpsexchange_macro(d, curctx, a); \ + opj_mqc_renormd_macro(mqc, a, c, ct); \ + } else { \ + d = (*curctx)->mps; \ + } \ + } \ +} + +#define DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \ + register const opj_mqc_state_t **curctx = mqc->curctx; \ + register OPJ_UINT32 c = mqc->c; \ + register OPJ_UINT32 a = mqc->a; \ + register OPJ_UINT32 ct = mqc->ct + +#define UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \ + mqc->curctx = curctx; \ + mqc->c = c; \ + mqc->a = a; \ + mqc->ct = ct; + +/** +Input a byte +@param mqc MQC handle +*/ +static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc) +{ + opj_mqc_bytein_macro(mqc, mqc->c, mqc->ct); +} + +/** +Renormalize mqc->a and mqc->c while decoding +@param mqc MQC handle +*/ +#define opj_mqc_renormd(mqc) \ + opj_mqc_renormd_macro(mqc, mqc->a, mqc->c, mqc->ct) + +/** +Decode a symbol +@param d OPJ_UINT32 value where to store the decoded symbol +@param mqc MQC handle +@return Returns the decoded symbol (0 or 1) in d +*/ +#define opj_mqc_decode(d, mqc) \ + opj_mqc_decode_macro(d, mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct) + +/** +Output a byte, doing bit-stuffing if necessary. +After a 0xff byte, the next byte must be smaller than 0x90. +@param mqc MQC handle +*/ +void opj_mqc_byteout(opj_mqc_t *mqc); + +/** +Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000 +@param mqc MQC handle +@param a_ value of mqc->a +@param c_ value of mqc->c_ +@param ct_ value of mqc->ct_ +*/ +#define opj_mqc_renorme_macro(mqc, a_, c_, ct_) \ +{ \ + do { \ + a_ <<= 1; \ + c_ <<= 1; \ + ct_--; \ + if (ct_ == 0) { \ + mqc->c = c_; \ + opj_mqc_byteout(mqc); \ + c_ = mqc->c; \ + ct_ = mqc->ct; \ + } \ + } while( (a_ & 0x8000) == 0); \ +} + +#define opj_mqc_codemps_macro(mqc, curctx, a, c, ct) \ +{ \ + a -= (*curctx)->qeval; \ + if ((a & 0x8000) == 0) { \ + if (a < (*curctx)->qeval) { \ + a = (*curctx)->qeval; \ + } else { \ + c += (*curctx)->qeval; \ + } \ + *curctx = (*curctx)->nmps; \ + opj_mqc_renorme_macro(mqc, a, c, ct); \ + } else { \ + c += (*curctx)->qeval; \ + } \ +} + +#define opj_mqc_codelps_macro(mqc, curctx, a, c, ct) \ +{ \ + a -= (*curctx)->qeval; \ + if (a < (*curctx)->qeval) { \ + c += (*curctx)->qeval; \ + } else { \ + a = (*curctx)->qeval; \ + } \ + *curctx = (*curctx)->nlps; \ + opj_mqc_renorme_macro(mqc, a, c, ct); \ +} + +#define opj_mqc_encode_macro(mqc, curctx, a, c, ct, d) \ +{ \ + if ((*curctx)->mps == (d)) { \ + opj_mqc_codemps_macro(mqc, curctx, a, c, ct); \ + } else { \ + opj_mqc_codelps_macro(mqc, curctx, a, c, ct); \ + } \ +} + + +#define opj_mqc_bypass_enc_macro(mqc, c, ct, d) \ +{\ + if (ct == BYPASS_CT_INIT) {\ + ct = 8;\ + }\ + ct--;\ + c = c + ((d) << ct);\ + if (ct == 0) {\ + *mqc->bp = (OPJ_BYTE)c;\ + ct = 8;\ + /* If the previous byte was 0xff, make sure that the next msb is 0 */ \ + if (*mqc->bp == 0xff) {\ + ct = 7;\ + }\ + mqc->bp++;\ + c = 0;\ + }\ +} + +#endif /* OPJ_MQC_INL_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/openjpeg.c b/cpp/3rd_party/openjpeg/openjp2/openjpeg.c new file mode 100644 index 0000000000..9c9b6eb0c0 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/openjpeg.c @@ -0,0 +1,1102 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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. + */ + +#ifdef _WIN32 +#include +#endif /* _WIN32 */ + +#include "opj_includes.h" + + +/* ---------------------------------------------------------------------- */ +/* Functions to set the message handlers */ + +OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, + opj_msg_callback p_callback, + void * p_user_data) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + if (! l_codec) { + return OPJ_FALSE; + } + + l_codec->m_event_mgr.info_handler = p_callback; + l_codec->m_event_mgr.m_info_data = p_user_data; + + return OPJ_TRUE; +} + +OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, + opj_msg_callback p_callback, + void * p_user_data) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + if (! l_codec) { + return OPJ_FALSE; + } + + l_codec->m_event_mgr.warning_handler = p_callback; + l_codec->m_event_mgr.m_warning_data = p_user_data; + + return OPJ_TRUE; +} + +OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, + opj_msg_callback p_callback, + void * p_user_data) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + if (! l_codec) { + return OPJ_FALSE; + } + + l_codec->m_event_mgr.error_handler = p_callback; + l_codec->m_event_mgr.m_error_data = p_user_data; + + return OPJ_TRUE; +} + +/* ---------------------------------------------------------------------- */ + +static OPJ_SIZE_T opj_read_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes, + FILE * p_file) +{ + OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file); + return l_nb_read ? l_nb_read : (OPJ_SIZE_T) - 1; +} + +static OPJ_UINT64 opj_get_data_length_from_file(FILE * p_file) +{ + OPJ_OFF_T file_length = 0; + + OPJ_FSEEK(p_file, 0, SEEK_END); + file_length = (OPJ_OFF_T)OPJ_FTELL(p_file); + OPJ_FSEEK(p_file, 0, SEEK_SET); + + return (OPJ_UINT64)file_length; +} + +static OPJ_SIZE_T opj_write_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes, + FILE * p_file) +{ + return fwrite(p_buffer, 1, p_nb_bytes, p_file); +} + +static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data) +{ + if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_CUR)) { + return -1; + } + + return p_nb_bytes; +} + +static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data) +{ + if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_SET)) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/* ---------------------------------------------------------------------- */ +#ifdef _WIN32 +#ifndef OPJ_STATIC +BOOL APIENTRY +DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +{ + + OPJ_ARG_NOT_USED(lpReserved); + OPJ_ARG_NOT_USED(hModule); + + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH : + break; + case DLL_PROCESS_DETACH : + break; + case DLL_THREAD_ATTACH : + case DLL_THREAD_DETACH : + break; + } + + return TRUE; +} +#endif /* OPJ_STATIC */ +#endif /* _WIN32 */ + +/* ---------------------------------------------------------------------- */ + +const char* OPJ_CALLCONV opj_version(void) +{ + return OPJ_PACKAGE_VERSION; +} + +/* ---------------------------------------------------------------------- */ +/* DECOMPRESSION FUNCTIONS*/ + +opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) +{ + opj_codec_private_t *l_codec = 00; + + l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t)); + if (!l_codec) { + return 00; + } + + l_codec->is_decompressor = 1; + + switch (p_format) { + case OPJ_CODEC_J2K: + l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) j2k_dump; + + l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)( + void*)) j2k_get_cstr_info; + + l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)( + void*)) j2k_get_cstr_index; + + l_codec->m_codec_data.m_decompression.opj_decode = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + opj_image_t*, struct opj_event_mgr *)) opj_j2k_decode; + + l_codec->m_codec_data.m_decompression.opj_end_decompress = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_end_decompress; + + l_codec->m_codec_data.m_decompression.opj_read_header = + (OPJ_BOOL(*)(struct opj_stream_private *, + void *, + opj_image_t **, + struct opj_event_mgr *)) opj_j2k_read_header; + + l_codec->m_codec_data.m_decompression.opj_destroy = + (void (*)(void *))opj_j2k_destroy; + + l_codec->m_codec_data.m_decompression.opj_setup_decoder = + (void (*)(void *, opj_dparameters_t *)) opj_j2k_setup_decoder; + + l_codec->m_codec_data.m_decompression.opj_read_tile_header = + (OPJ_BOOL(*)(void *, + OPJ_UINT32*, + OPJ_UINT32*, + OPJ_INT32*, OPJ_INT32*, + OPJ_INT32*, OPJ_INT32*, + OPJ_UINT32*, + OPJ_BOOL*, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_read_tile_header; + + l_codec->m_codec_data.m_decompression.opj_decode_tile_data = + (OPJ_BOOL(*)(void *, + OPJ_UINT32, + OPJ_BYTE*, + OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_decode_tile; + + l_codec->m_codec_data.m_decompression.opj_set_decode_area = + (OPJ_BOOL(*)(void *, + opj_image_t*, + OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, + struct opj_event_mgr *)) opj_j2k_set_decode_area; + + l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = + (OPJ_BOOL(*)(void *p_codec, + opj_stream_private_t *p_cio, + opj_image_t *p_image, + struct opj_event_mgr * p_manager, + OPJ_UINT32 tile_index)) opj_j2k_get_tile; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = + (OPJ_BOOL(*)(void * p_codec, + OPJ_UINT32 res_factor, + struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_components = + (OPJ_BOOL(*)(void * p_codec, + OPJ_UINT32 numcomps, + const OPJ_UINT32 * comps_indices, + struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_components; + + l_codec->opj_set_threads = + (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads; + + l_codec->m_codec = opj_j2k_create_decompress(); + + if (! l_codec->m_codec) { + opj_free(l_codec); + return NULL; + } + + break; + + case OPJ_CODEC_JP2: + /* get a JP2 decoder handle */ + l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) jp2_dump; + + l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)( + void*)) jp2_get_cstr_info; + + l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)( + void*)) jp2_get_cstr_index; + + l_codec->m_codec_data.m_decompression.opj_decode = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + opj_image_t*, + struct opj_event_mgr *)) opj_jp2_decode; + + l_codec->m_codec_data.m_decompression.opj_end_decompress = + (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_end_decompress; + + l_codec->m_codec_data.m_decompression.opj_read_header = + (OPJ_BOOL(*)(struct opj_stream_private *, + void *, + opj_image_t **, + struct opj_event_mgr *)) opj_jp2_read_header; + + l_codec->m_codec_data.m_decompression.opj_read_tile_header = + (OPJ_BOOL(*)(void *, + OPJ_UINT32*, + OPJ_UINT32*, + OPJ_INT32*, + OPJ_INT32*, + OPJ_INT32 *, + OPJ_INT32 *, + OPJ_UINT32 *, + OPJ_BOOL *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_read_tile_header; + + l_codec->m_codec_data.m_decompression.opj_decode_tile_data = + (OPJ_BOOL(*)(void *, + OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_decode_tile; + + l_codec->m_codec_data.m_decompression.opj_destroy = (void (*)( + void *))opj_jp2_destroy; + + l_codec->m_codec_data.m_decompression.opj_setup_decoder = + (void (*)(void *, opj_dparameters_t *)) opj_jp2_setup_decoder; + + l_codec->m_codec_data.m_decompression.opj_set_decode_area = + (OPJ_BOOL(*)(void *, + opj_image_t*, + OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, + struct opj_event_mgr *)) opj_jp2_set_decode_area; + + l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = + (OPJ_BOOL(*)(void *p_codec, + opj_stream_private_t *p_cio, + opj_image_t *p_image, + struct opj_event_mgr * p_manager, + OPJ_UINT32 tile_index)) opj_jp2_get_tile; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = + (OPJ_BOOL(*)(void * p_codec, + OPJ_UINT32 res_factor, + opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor; + + l_codec->m_codec_data.m_decompression.opj_set_decoded_components = + (OPJ_BOOL(*)(void * p_codec, + OPJ_UINT32 numcomps, + const OPJ_UINT32 * comps_indices, + struct opj_event_mgr * p_manager)) opj_jp2_set_decoded_components; + + l_codec->opj_set_threads = + (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads; + + l_codec->m_codec = opj_jp2_create(OPJ_TRUE); + + if (! l_codec->m_codec) { + opj_free(l_codec); + return 00; + } + + break; + case OPJ_CODEC_UNKNOWN: + case OPJ_CODEC_JPT: + default: + opj_free(l_codec); + return 00; + } + + opj_set_default_event_handler(&(l_codec->m_event_mgr)); + return (opj_codec_t*) l_codec; +} + +void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t + *parameters) +{ + if (parameters) { + memset(parameters, 0, sizeof(opj_dparameters_t)); + /* default decoding parameters */ + parameters->cp_layer = 0; + parameters->cp_reduce = 0; + + parameters->decod_format = -1; + parameters->cod_format = -1; + parameters->flags = 0; + /* UniPG>> */ +#ifdef USE_JPWL + parameters->jpwl_correct = OPJ_FALSE; + parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS; + parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES; +#endif /* USE_JPWL */ + /* <= 0)) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32)num_threads); + } + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec, + opj_dparameters_t *parameters + ) +{ + if (p_codec && parameters) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + if (! l_codec->is_decompressor) { + opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, + "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n"); + return OPJ_FALSE; + } + + l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec, + parameters); + return OPJ_TRUE; + } + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream, + opj_codec_t *p_codec, + opj_image_t **p_image) +{ + if (p_codec && p_stream) { + opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + + if (! l_codec->is_decompressor) { + opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, + "Codec provided to the opj_read_header function is not a decompressor handler.\n"); + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_read_header(l_stream, + l_codec->m_codec, + p_image, + &(l_codec->m_event_mgr)); + } + + return OPJ_FALSE; +} + + +OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec, + OPJ_UINT32 numcomps, + const OPJ_UINT32* comps_indices, + OPJ_BOOL apply_color_transforms) +{ + if (p_codec) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + if (! l_codec->is_decompressor) { + opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, + "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n"); + return OPJ_FALSE; + } + + if (apply_color_transforms) { + opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, + "apply_color_transforms = OPJ_TRUE is not supported.\n"); + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_set_decoded_components( + l_codec->m_codec, + numcomps, + comps_indices, + &(l_codec->m_event_mgr)); + } + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_codec, + opj_stream_t *p_stream, + opj_image_t* p_image) +{ + if (p_codec && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec, + l_stream, + p_image, + &(l_codec->m_event_mgr)); + } + + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y + ) +{ + if (p_codec) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + if (! l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_set_decode_area( + l_codec->m_codec, + p_image, + p_start_x, p_start_y, + p_end_x, p_end_y, + &(l_codec->m_event_mgr)); + } + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec, + opj_stream_t * p_stream, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_should_go_on) +{ + if (p_codec && p_stream && p_data_size && p_tile_index) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_read_tile_header( + l_codec->m_codec, + p_tile_index, + p_data_size, + p_tile_x0, p_tile_y0, + p_tile_x1, p_tile_y1, + p_nb_comps, + p_should_go_on, + l_stream, + &(l_codec->m_event_mgr)); + } + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream + ) +{ + if (p_codec && p_data && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( + l_codec->m_codec, + p_tile_index, + p_data, + p_data_size, + l_stream, + &(l_codec->m_event_mgr)); + } + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec, + opj_stream_t *p_stream, + opj_image_t *p_image, + OPJ_UINT32 tile_index) +{ + if (p_codec && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( + l_codec->m_codec, + l_stream, + p_image, + &(l_codec->m_event_mgr), + tile_index); + } + + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, + OPJ_UINT32 res_factor) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + if (!l_codec) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor( + l_codec->m_codec, + res_factor, + &(l_codec->m_event_mgr)); +} + +/* ---------------------------------------------------------------------- */ +/* COMPRESSION FUNCTIONS*/ + +opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) +{ + opj_codec_private_t *l_codec = 00; + + l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t)); + if (!l_codec) { + return 00; + } + + l_codec->is_decompressor = 0; + + switch (p_format) { + case OPJ_CODEC_J2K: + l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_encode; + + l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_end_compress; + + l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_image *, + struct opj_event_mgr *)) opj_j2k_start_compress; + + l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *, + OPJ_UINT32, + OPJ_BYTE*, + OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_j2k_write_tile; + + l_codec->m_codec_data.m_compression.opj_destroy = (void (*)( + void *)) opj_j2k_destroy; + + l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *, + opj_cparameters_t *, + struct opj_image *, + struct opj_event_mgr *)) opj_j2k_setup_encoder; + + l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options = (OPJ_BOOL( + *)(void *, + const char* const*, + struct opj_event_mgr *)) opj_j2k_encoder_set_extra_options; + + l_codec->opj_set_threads = + (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads; + + l_codec->m_codec = opj_j2k_create_compress(); + if (! l_codec->m_codec) { + opj_free(l_codec); + return 00; + } + + break; + + case OPJ_CODEC_JP2: + /* get a JP2 decoder handle */ + l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_encode; + + l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_end_compress; + + l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *, + struct opj_stream_private *, + struct opj_image *, + struct opj_event_mgr *)) opj_jp2_start_compress; + + l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *, + OPJ_UINT32, + OPJ_BYTE*, + OPJ_UINT32, + struct opj_stream_private *, + struct opj_event_mgr *)) opj_jp2_write_tile; + + l_codec->m_codec_data.m_compression.opj_destroy = (void (*)( + void *)) opj_jp2_destroy; + + l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *, + opj_cparameters_t *, + struct opj_image *, + struct opj_event_mgr *)) opj_jp2_setup_encoder; + + l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options = (OPJ_BOOL( + *)(void *, + const char* const*, + struct opj_event_mgr *)) opj_jp2_encoder_set_extra_options; + + l_codec->opj_set_threads = + (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads; + + l_codec->m_codec = opj_jp2_create(OPJ_FALSE); + if (! l_codec->m_codec) { + opj_free(l_codec); + return 00; + } + + break; + + case OPJ_CODEC_UNKNOWN: + case OPJ_CODEC_JPT: + default: + opj_free(l_codec); + return 00; + } + + opj_set_default_event_handler(&(l_codec->m_event_mgr)); + return (opj_codec_t*) l_codec; +} + +void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t + *parameters) +{ + if (parameters) { + memset(parameters, 0, sizeof(opj_cparameters_t)); + /* default coding parameters */ + parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */ + parameters->rsiz = OPJ_PROFILE_NONE; + parameters->max_comp_size = 0; + parameters->numresolution = OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION; + parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */ + parameters->cblockw_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKW; + parameters->cblockh_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKH; + parameters->prog_order = OPJ_COMP_PARAM_DEFAULT_PROG_ORDER; + parameters->roi_compno = -1; /* no ROI */ + parameters->subsampling_dx = 1; + parameters->subsampling_dy = 1; + parameters->tp_on = 0; + parameters->decod_format = -1; + parameters->cod_format = -1; + parameters->tcp_rates[0] = 0; + parameters->tcp_numlayers = 0; + parameters->cp_disto_alloc = 0; + parameters->cp_fixed_alloc = 0; + parameters->cp_fixed_quality = 0; + parameters->jpip_on = OPJ_FALSE; + /* UniPG>> */ +#ifdef USE_JPWL + parameters->jpwl_epc_on = OPJ_FALSE; + parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */ + { + int i; + for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { + parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */ + parameters->jpwl_hprot_TPH[i] = 0; /* absent */ + } + }; + { + int i; + for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { + parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */ + parameters->jpwl_pprot_packno[i] = -1; /* unassigned */ + parameters->jpwl_pprot[i] = 0; /* absent */ + } + }; + parameters->jpwl_sens_size = 0; /* 0 means no ESD */ + parameters->jpwl_sens_addr = 0; /* 0 means auto */ + parameters->jpwl_sens_range = 0; /* 0 means packet */ + parameters->jpwl_sens_MH = -1; /* -1 means unassigned */ + { + int i; + for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { + parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */ + parameters->jpwl_sens_TPH[i] = -1; /* absent */ + } + }; +#endif /* USE_JPWL */ + /* <is_decompressor) { + return l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec, + parameters, + p_image, + &(l_codec->m_event_mgr)); + } + } + + return OPJ_FALSE; +} + +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL OPJ_CALLCONV opj_encoder_set_extra_options(opj_codec_t *p_codec, + const char* const* options) +{ + if (p_codec) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + if (! l_codec->is_decompressor) { + return l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options( + l_codec->m_codec, + options, + &(l_codec->m_event_mgr)); + } + } + + return OPJ_FALSE; +} + +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec, + opj_image_t * p_image, + opj_stream_t *p_stream) +{ + if (p_codec && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec, + l_stream, + p_image, + &(l_codec->m_event_mgr)); + } + } + + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream) +{ + if (p_info && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec, + l_stream, + &(l_codec->m_event_mgr)); + } + } + + return OPJ_FALSE; + +} + +OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec, + opj_stream_t *p_stream) +{ + if (p_codec && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec, + l_stream, + &(l_codec->m_event_mgr)); + } + } + return OPJ_FALSE; + +} + +OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec, + opj_stream_t *p_stream) +{ + if (p_codec && p_stream) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (! l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_decompression.opj_end_decompress( + l_codec->m_codec, + l_stream, + &(l_codec->m_event_mgr)); + } + + return OPJ_FALSE; +} + +OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters, + OPJ_FLOAT32 * pEncodingMatrix, + OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp) +{ + OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32); + OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32); + OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size; + + /* add MCT capability */ + if (OPJ_IS_PART2(parameters->rsiz)) { + parameters->rsiz |= OPJ_EXTENSION_MCT; + } else { + parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT)); + } + parameters->irreversible = 1; + + /* use array based MCT */ + parameters->tcp_mct = 2; + parameters->mct_data = opj_malloc(l_mct_total_size); + if (! parameters->mct_data) { + return OPJ_FALSE; + } + + memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size); + memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size, p_dc_shift, + l_dc_shift_size); + + return OPJ_TRUE; +} + +OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream) +{ + if (p_codec && p_stream && p_data) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream; + + if (l_codec->is_decompressor) { + return OPJ_FALSE; + } + + return l_codec->m_codec_data.m_compression.opj_write_tile(l_codec->m_codec, + p_tile_index, + p_data, + p_data_size, + l_stream, + &(l_codec->m_event_mgr)); + } + + return OPJ_FALSE; +} + +/* ---------------------------------------------------------------------- */ + +void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec) +{ + if (p_codec) { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + + if (l_codec->is_decompressor) { + l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec); + } else { + l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec); + } + + l_codec->m_codec = 00; + opj_free(l_codec); + } +} + +/* ---------------------------------------------------------------------- */ + +void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec, + OPJ_INT32 info_flag, + FILE* output_stream) +{ + if (p_codec) { + opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + + l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream); + return; + } + + /* TODO return error */ + /* fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n"); */ + return; +} + +opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec) +{ + if (p_codec) { + opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + + return l_codec->opj_get_codec_info(l_codec->m_codec); + } + + return NULL; +} + +void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) +{ + if (cstr_info) { + + if ((*cstr_info)->m_default_tile_info.tccp_info) { + opj_free((*cstr_info)->m_default_tile_info.tccp_info); + } + + if ((*cstr_info)->tile_info) { + /* FIXME not used for the moment*/ + } + + opj_free((*cstr_info)); + (*cstr_info) = NULL; + } +} + +opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec) +{ + if (p_codec) { + opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec; + + return l_codec->opj_get_codec_index(l_codec->m_codec); + } + + return NULL; +} + +void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index) +{ + if (*p_cstr_index) { + j2k_destroy_cstr_index(*p_cstr_index); + (*p_cstr_index) = NULL; + } +} + +opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream( + const char *fname, OPJ_BOOL p_is_read_stream) +{ + return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE, + p_is_read_stream); +} + +opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( + const char *fname, + OPJ_SIZE_T p_size, + OPJ_BOOL p_is_read_stream) +{ + opj_stream_t* l_stream = 00; + FILE *p_file; + const char *mode; + + if (! fname) { + return NULL; + } + + if (p_is_read_stream) { + mode = "rb"; + } else { + mode = "wb"; + } + + p_file = fopen(fname, mode); + + if (! p_file) { + return NULL; + } + + l_stream = opj_stream_create(p_size, p_is_read_stream); + if (! l_stream) { + fclose(p_file); + return NULL; + } + + opj_stream_set_user_data(l_stream, p_file, + (opj_stream_free_user_data_fn) fclose); + opj_stream_set_user_data_length(l_stream, + opj_get_data_length_from_file(p_file)); + opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file); + opj_stream_set_write_function(l_stream, + (opj_stream_write_fn) opj_write_from_file); + opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); + opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); + + return l_stream; +} + + +void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size) +{ + void* ret = opj_aligned_malloc(size); + /* printf("opj_image_data_alloc %p\n", ret); */ + return ret; +} + +void OPJ_CALLCONV opj_image_data_free(void* ptr) +{ + /* printf("opj_image_data_free %p\n", ptr); */ + opj_aligned_free(ptr); +} diff --git a/cpp/3rd_party/openjpeg/openjp2/openjpeg.h b/cpp/3rd_party/openjpeg/openjp2/openjpeg.h new file mode 100644 index 0000000000..269ac329ae --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/openjpeg.h @@ -0,0 +1,1751 @@ +/* +* The copyright in this software is being made available under the 2-clauses +* BSD License, included below. This software may be subject to other third +* party and contributor rights, including patent rights, and no such rights +* are granted under this license. +* +* Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium +* Copyright (c) 2002-2014, Professor Benoit Macq +* Copyright (c) 2001-2003, David Janssens +* Copyright (c) 2002-2003, Yannick Verschueren +* Copyright (c) 2003-2007, Francois-Olivier Devaux +* Copyright (c) 2003-2014, Antonin Descampe +* Copyright (c) 2005, Herve Drolon, FreeImage Team +* Copyright (c) 2006-2007, Parvatha Elangovan +* Copyright (c) 2008, Jerome Fimes, Communications & Systemes +* Copyright (c) 2010-2011, Kaori Hagihara +* Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France +* Copyright (c) 2012, CS Systemes d'Information, France +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef OPENJPEG_H +#define OPENJPEG_H + + +/* +========================================================== + Compiler directives +========================================================== +*/ + +/* +The inline keyword is supported by C99 but not by C90. +Most compilers implement their own version of this keyword ... +*/ +#ifndef INLINE +#if defined(_MSC_VER) +#define INLINE __forceinline +#elif defined(__GNUC__) +#define INLINE __inline__ +#elif defined(__MWERKS__) +#define INLINE inline +#else +/* add other compilers here ... */ +#define INLINE +#endif /* defined() */ +#endif /* INLINE */ + +/* deprecated attribute */ +#ifdef __GNUC__ +#define OPJ_DEPRECATED(func) func __attribute__ ((deprecated)) +#elif defined(_MSC_VER) +#define OPJ_DEPRECATED(func) __declspec(deprecated) func +#else +#pragma message("WARNING: You need to implement DEPRECATED for this compiler") +#define OPJ_DEPRECATED(func) func +#endif + +#if defined(OPJ_STATIC) || !defined(_WIN32) +/* http://gcc.gnu.org/wiki/Visibility */ +# if !defined(_WIN32) && __GNUC__ >= 4 +# if defined(OPJ_STATIC) /* static library uses "hidden" */ +# define OPJ_API __attribute__ ((visibility ("hidden"))) +# else +# define OPJ_API __attribute__ ((visibility ("default"))) +# endif +# define OPJ_LOCAL __attribute__ ((visibility ("hidden"))) +# else +# define OPJ_API +# define OPJ_LOCAL +# endif +# define OPJ_CALLCONV +#else +# define OPJ_CALLCONV __stdcall +/* +The following ifdef block is the standard way of creating macros which make exporting +from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS +symbol defined on the command line. this symbol should not be defined on any project +that uses this DLL. This way any other project whose source files include this file see +OPJ_API functions as being imported from a DLL, whereas this DLL sees symbols +defined with this macro as being exported. +*/ +# if defined(OPJ_EXPORTS) || defined(DLL_EXPORT) +# define OPJ_API __declspec(dllexport) +# else +# define OPJ_API __declspec(dllimport) +# endif /* OPJ_EXPORTS */ +#endif /* !OPJ_STATIC || !_WIN32 */ + +typedef int OPJ_BOOL; +#define OPJ_TRUE 1 +#define OPJ_FALSE 0 + +typedef char OPJ_CHAR; +typedef float OPJ_FLOAT32; +typedef double OPJ_FLOAT64; +typedef unsigned char OPJ_BYTE; + +#include "opj_stdint.h" + +typedef int8_t OPJ_INT8; +typedef uint8_t OPJ_UINT8; +typedef int16_t OPJ_INT16; +typedef uint16_t OPJ_UINT16; +typedef int32_t OPJ_INT32; +typedef uint32_t OPJ_UINT32; +typedef int64_t OPJ_INT64; +typedef uint64_t OPJ_UINT64; + +typedef int64_t OPJ_OFF_T; /* 64-bit file offset type */ + +#include +typedef size_t OPJ_SIZE_T; + +/* Avoid compile-time warning because parameter is not used */ +#define OPJ_ARG_NOT_USED(x) (void)(x) + +/* +========================================================== + Useful constant definitions +========================================================== +*/ + +#define OPJ_PATH_LEN 4096 /**< Maximum allowed size for filenames */ + +#define OPJ_J2K_MAXRLVLS 33 /**< Number of maximum resolution level authorized */ +#define OPJ_J2K_MAXBANDS (3*OPJ_J2K_MAXRLVLS-2) /**< Number of maximum sub-band linked to number of resolution level */ + +#define OPJ_J2K_DEFAULT_NB_SEGS 10 +#define OPJ_J2K_STREAM_CHUNK_SIZE 0x100000 /** 1 mega by default */ +#define OPJ_J2K_DEFAULT_HEADER_SIZE 1000 +#define OPJ_J2K_MCC_DEFAULT_NB_RECORDS 10 +#define OPJ_J2K_MCT_DEFAULT_NB_RECORDS 10 + +/* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */ +#define JPWL_MAX_NO_TILESPECS 16 /**< Maximum number of tile parts expected by JPWL: increase at your will */ +#define JPWL_MAX_NO_PACKSPECS 16 /**< Maximum number of packet parts expected by JPWL: increase at your will */ +#define JPWL_MAX_NO_MARKERS 512 /**< Maximum number of JPWL markers: increase at your will */ +#define JPWL_PRIVATEINDEX_NAME "jpwl_index_privatefilename" /**< index file name used when JPWL is on */ +#define JPWL_EXPECTED_COMPONENTS 3 /**< Expect this number of components, so you'll find better the first EPB */ +#define JPWL_MAXIMUM_TILES 8192 /**< Expect this maximum number of tiles, to avoid some crashes */ +#define JPWL_MAXIMUM_HAMMING 2 /**< Expect this maximum number of bit errors in marker id's */ +#define JPWL_MAXIMUM_EPB_ROOM 65450 /**< Expect this maximum number of bytes for composition of EPBs */ +/* <= OPJ_PROFILE_CINEMA_2K)&&((v) <= OPJ_PROFILE_CINEMA_S4K)) +#define OPJ_IS_STORAGE(v) ((v) == OPJ_PROFILE_CINEMA_LTS) +#define OPJ_IS_BROADCAST(v) (((v) >= OPJ_PROFILE_BC_SINGLE)&&((v) <= ((OPJ_PROFILE_BC_MULTI_R) | (0x000b)))) +#define OPJ_IS_IMF(v) (((v) >= OPJ_PROFILE_IMF_2K)&&((v) <= ((OPJ_PROFILE_IMF_8K_R) | (0x009b)))) +#define OPJ_IS_PART2(v) ((v) & OPJ_PROFILE_PART2) + +#define OPJ_GET_IMF_PROFILE(v) ((v) & 0xff00) /** Extract IMF profile without mainlevel/sublevel */ +#define OPJ_GET_IMF_MAINLEVEL(v) ((v) & 0xf) /** Extract IMF main level */ +#define OPJ_GET_IMF_SUBLEVEL(v) (((v) >> 4) & 0xf) /** Extract IMF sub level */ + +#define OPJ_IMF_MAINLEVEL_MAX 11 /** Maximum main level */ + +/** Max. Components Sampling Rate (MSamples/sec) per IMF main level */ +#define OPJ_IMF_MAINLEVEL_1_MSAMPLESEC 65 /** MSamples/sec for IMF main level 1 */ +#define OPJ_IMF_MAINLEVEL_2_MSAMPLESEC 130 /** MSamples/sec for IMF main level 2 */ +#define OPJ_IMF_MAINLEVEL_3_MSAMPLESEC 195 /** MSamples/sec for IMF main level 3 */ +#define OPJ_IMF_MAINLEVEL_4_MSAMPLESEC 260 /** MSamples/sec for IMF main level 4 */ +#define OPJ_IMF_MAINLEVEL_5_MSAMPLESEC 520 /** MSamples/sec for IMF main level 5 */ +#define OPJ_IMF_MAINLEVEL_6_MSAMPLESEC 1200 /** MSamples/sec for IMF main level 6 */ +#define OPJ_IMF_MAINLEVEL_7_MSAMPLESEC 2400 /** MSamples/sec for IMF main level 7 */ +#define OPJ_IMF_MAINLEVEL_8_MSAMPLESEC 4800 /** MSamples/sec for IMF main level 8 */ +#define OPJ_IMF_MAINLEVEL_9_MSAMPLESEC 9600 /** MSamples/sec for IMF main level 9 */ +#define OPJ_IMF_MAINLEVEL_10_MSAMPLESEC 19200 /** MSamples/sec for IMF main level 10 */ +#define OPJ_IMF_MAINLEVEL_11_MSAMPLESEC 38400 /** MSamples/sec for IMF main level 11 */ + +/** Max. compressed Bit Rate (Mbits/s) per IMF sub level */ +#define OPJ_IMF_SUBLEVEL_1_MBITSSEC 200 /** Mbits/s for IMF sub level 1 */ +#define OPJ_IMF_SUBLEVEL_2_MBITSSEC 400 /** Mbits/s for IMF sub level 2 */ +#define OPJ_IMF_SUBLEVEL_3_MBITSSEC 800 /** Mbits/s for IMF sub level 3 */ +#define OPJ_IMF_SUBLEVEL_4_MBITSSEC 1600 /** Mbits/s for IMF sub level 4 */ +#define OPJ_IMF_SUBLEVEL_5_MBITSSEC 3200 /** Mbits/s for IMF sub level 5 */ +#define OPJ_IMF_SUBLEVEL_6_MBITSSEC 6400 /** Mbits/s for IMF sub level 6 */ +#define OPJ_IMF_SUBLEVEL_7_MBITSSEC 12800 /** Mbits/s for IMF sub level 7 */ +#define OPJ_IMF_SUBLEVEL_8_MBITSSEC 25600 /** Mbits/s for IMF sub level 8 */ +#define OPJ_IMF_SUBLEVEL_9_MBITSSEC 51200 /** Mbits/s for IMF sub level 9 */ + +/** + * JPEG 2000 codestream and component size limits in cinema profiles + * */ +#define OPJ_CINEMA_24_CS 1302083 /** Maximum codestream length for 24fps */ +#define OPJ_CINEMA_48_CS 651041 /** Maximum codestream length for 48fps */ +#define OPJ_CINEMA_24_COMP 1041666 /** Maximum size per color component for 2K & 4K @ 24fps */ +#define OPJ_CINEMA_48_COMP 520833 /** Maximum size per color component for 2K @ 48fps */ + +/* +========================================================== + enum definitions +========================================================== +*/ + +/** + * DEPRECATED: use RSIZ, OPJ_PROFILE_* and OPJ_EXTENSION_* instead + * Rsiz Capabilities + * */ +typedef enum RSIZ_CAPABILITIES { + OPJ_STD_RSIZ = 0, /** Standard JPEG2000 profile*/ + OPJ_CINEMA2K = 3, /** Profile name for a 2K image*/ + OPJ_CINEMA4K = 4, /** Profile name for a 4K image*/ + OPJ_MCT = 0x8100 +} OPJ_RSIZ_CAPABILITIES; + +/** + * DEPRECATED: use RSIZ, OPJ_PROFILE_* and OPJ_EXTENSION_* instead + * Digital cinema operation mode + * */ +typedef enum CINEMA_MODE { + OPJ_OFF = 0, /** Not Digital Cinema*/ + OPJ_CINEMA2K_24 = 1, /** 2K Digital Cinema at 24 fps*/ + OPJ_CINEMA2K_48 = 2, /** 2K Digital Cinema at 48 fps*/ + OPJ_CINEMA4K_24 = 3 /** 4K Digital Cinema at 24 fps*/ +} OPJ_CINEMA_MODE; + +/** + * Progression order + * */ +typedef enum PROG_ORDER { + OPJ_PROG_UNKNOWN = -1, /**< place-holder */ + OPJ_LRCP = 0, /**< layer-resolution-component-precinct order */ + OPJ_RLCP = 1, /**< resolution-layer-component-precinct order */ + OPJ_RPCL = 2, /**< resolution-precinct-component-layer order */ + OPJ_PCRL = 3, /**< precinct-component-resolution-layer order */ + OPJ_CPRL = 4 /**< component-precinct-resolution-layer order */ +} OPJ_PROG_ORDER; + +/** + * Supported image color spaces +*/ +typedef enum COLOR_SPACE { + OPJ_CLRSPC_UNKNOWN = -1, /**< not supported by the library */ + OPJ_CLRSPC_UNSPECIFIED = 0, /**< not specified in the codestream */ + OPJ_CLRSPC_SRGB = 1, /**< sRGB */ + OPJ_CLRSPC_GRAY = 2, /**< grayscale */ + OPJ_CLRSPC_SYCC = 3, /**< YUV */ + OPJ_CLRSPC_EYCC = 4, /**< e-YCC */ + OPJ_CLRSPC_CMYK = 5 /**< CMYK */ +} OPJ_COLOR_SPACE; + +/** + * Supported codec +*/ +typedef enum CODEC_FORMAT { + OPJ_CODEC_UNKNOWN = -1, /**< place-holder */ + OPJ_CODEC_J2K = 0, /**< JPEG-2000 codestream : read/write */ + OPJ_CODEC_JPT = 1, /**< JPT-stream (JPEG 2000, JPIP) : read only */ + OPJ_CODEC_JP2 = 2, /**< JP2 file format : read/write */ + OPJ_CODEC_JPP = 3, /**< JPP-stream (JPEG 2000, JPIP) : to be coded */ + OPJ_CODEC_JPX = 4 /**< JPX file format (JPEG 2000 Part-2) : to be coded */ +} OPJ_CODEC_FORMAT; + + +/* +========================================================== + event manager typedef definitions +========================================================== +*/ + +/** + * Callback function prototype for events + * @param msg Event message + * @param client_data Client object where will be return the event message + * */ +typedef void (*opj_msg_callback)(const char *msg, void *client_data); + +/* +========================================================== + codec typedef definitions +========================================================== +*/ + +#ifndef OPJ_UINT32_SEMANTICALLY_BUT_INT32 +#define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_INT32 +#endif + +/** + * Progression order changes + * + */ +typedef struct opj_poc { + /** Resolution num start, Component num start, given by POC */ + OPJ_UINT32 resno0, compno0; + /** Layer num end,Resolution num end, Component num end, given by POC */ + OPJ_UINT32 layno1, resno1, compno1; + /** Layer num start,Precinct num start, Precinct num end */ + OPJ_UINT32 layno0, precno0, precno1; + /** Progression order enum*/ + OPJ_PROG_ORDER prg1, prg; + /** Progression order string*/ + OPJ_CHAR progorder[5]; + /** Tile number (starting at 1) */ + OPJ_UINT32 tile; + /** Start and end values for Tile width and height*/ + OPJ_UINT32_SEMANTICALLY_BUT_INT32 tx0, tx1, ty0, ty1; + /** Start value, initialised in pi_initialise_encode*/ + OPJ_UINT32 layS, resS, compS, prcS; + /** End value, initialised in pi_initialise_encode */ + OPJ_UINT32 layE, resE, compE, prcE; + /** Start and end values of Tile width and height, initialised in pi_initialise_encode*/ + OPJ_UINT32 txS, txE, tyS, tyE, dx, dy; + /** Temporary values for Tile parts, initialised in pi_create_encode */ + OPJ_UINT32 lay_t, res_t, comp_t, prc_t, tx0_t, ty0_t; +} opj_poc_t; + +/** + * Compression parameters + * */ +typedef struct opj_cparameters { + /** size of tile: tile_size_on = false (not in argument) or = true (in argument) */ + OPJ_BOOL tile_size_on; + /** XTOsiz */ + int cp_tx0; + /** YTOsiz */ + int cp_ty0; + /** XTsiz */ + int cp_tdx; + /** YTsiz */ + int cp_tdy; + /** allocation by rate/distortion */ + int cp_disto_alloc; + /** allocation by fixed layer */ + int cp_fixed_alloc; + /** add fixed_quality */ + int cp_fixed_quality; + /** fixed layer */ + int *cp_matrice; + /** comment for coding */ + char *cp_comment; + /** csty : coding style */ + int csty; + /** progression order (default OPJ_LRCP) */ + OPJ_PROG_ORDER prog_order; + /** progression order changes */ + opj_poc_t POC[32]; + /** number of progression order changes (POC), default to 0 */ + OPJ_UINT32 numpocs; + /** number of layers */ + int tcp_numlayers; + /** rates of layers - might be subsequently limited by the max_cs_size field. + * Should be decreasing. 1 can be + * used as last value to indicate the last layer is lossless. */ + float tcp_rates[100]; + /** different psnr for successive layers. Should be increasing. 0 can be + * used as last value to indicate the last layer is lossless. */ + float tcp_distoratio[100]; + /** number of resolutions */ + int numresolution; + /** initial code block width, default to 64 */ + int cblockw_init; + /** initial code block height, default to 64 */ + int cblockh_init; + /** mode switch (cblk_style) */ + int mode; + /** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */ + int irreversible; + /** region of interest: affected component in [0..3], -1 means no ROI */ + int roi_compno; + /** region of interest: upshift value */ + int roi_shift; + /* number of precinct size specifications */ + int res_spec; + /** initial precinct width */ + int prcw_init[OPJ_J2K_MAXRLVLS]; + /** initial precinct height */ + int prch_init[OPJ_J2K_MAXRLVLS]; + + /**@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]; + /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */ + int index_on; + /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */ + char index[OPJ_PATH_LEN]; + /** subimage encoding: origin image offset in x direction */ + int image_offset_x0; + /** subimage encoding: origin image offset in y direction */ + int image_offset_y0; + /** subsampling value for dx */ + int subsampling_dx; + /** subsampling value for dy */ + int subsampling_dy; + /** input file format 0: PGX, 1: PxM, 2: BMP 3:TIF*/ + int decod_format; + /** output file format 0: J2K, 1: JP2, 2: JPT */ + int cod_format; + /*@}*/ + + /* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */ + /**@name JPWL encoding parameters */ + /*@{*/ + /** enables writing of EPC in MH, thus activating JPWL */ + OPJ_BOOL jpwl_epc_on; + /** error protection method for MH (0,1,16,32,37-128) */ + int jpwl_hprot_MH; + /** tile number of header protection specification (>=0) */ + int jpwl_hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS]; + /** error protection methods for TPHs (0,1,16,32,37-128) */ + int jpwl_hprot_TPH[JPWL_MAX_NO_TILESPECS]; + /** tile number of packet protection specification (>=0) */ + int jpwl_pprot_tileno[JPWL_MAX_NO_PACKSPECS]; + /** packet number of packet protection specification (>=0) */ + int jpwl_pprot_packno[JPWL_MAX_NO_PACKSPECS]; + /** error protection methods for packets (0,1,16,32,37-128) */ + int jpwl_pprot[JPWL_MAX_NO_PACKSPECS]; + /** enables writing of ESD, (0=no/1/2 bytes) */ + int jpwl_sens_size; + /** sensitivity addressing size (0=auto/2/4 bytes) */ + int jpwl_sens_addr; + /** sensitivity range (0-3) */ + int jpwl_sens_range; + /** sensitivity method for MH (-1=no,0-7) */ + int jpwl_sens_MH; + /** tile number of sensitivity specification (>=0) */ + int jpwl_sens_TPH_tileno[JPWL_MAX_NO_TILESPECS]; + /** sensitivity methods for TPHs (-1=no,0-7) */ + int jpwl_sens_TPH[JPWL_MAX_NO_TILESPECS]; + /*@}*/ + /* <> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */ + /**@name JPWL decoding parameters */ + /*@{*/ + /** activates the JPWL correction capabilities */ + OPJ_BOOL jpwl_correct; + /** expected number of components */ + int jpwl_exp_comps; + /** maximum number of tiles */ + int jpwl_max_tiles; + /*@}*/ + /* <> */ +/** + * Marker structure + * */ +typedef struct opj_marker_info { + /** marker type */ + unsigned short int type; + /** position in codestream */ + OPJ_OFF_T pos; + /** length, marker val included */ + int len; +} opj_marker_info_t; +/* <> */ + /** number of markers */ + int marknum; + /** list of markers */ + opj_marker_info_t *marker; + /** actual size of markers array */ + int maxmarknum; + /* <> */ /* NOT USED FOR THE MOMENT IN THE V2 VERSION */ + /** number of markers */ + OPJ_UINT32 marknum; + /** list of markers */ + opj_marker_info_t *marker; + /** actual size of markers array */ + OPJ_UINT32 maxmarknum; + /* <> */ /* NOT USED FOR THE MOMENT IN THE V2 VERSION */ + /** number of markers */ + OPJ_UINT32 marknum; + /** list of markers */ + opj_marker_info_t *marker; + /** actual size of markers array */ + OPJ_UINT32 maxmarknum; + /* < */ + +/* +========================================================== + Metadata from the JP2file +========================================================== +*/ + +/** + * Info structure of the JP2 file + * EXPERIMENTAL FOR THE MOMENT + */ +typedef struct opj_jp2_metadata { + /** */ + OPJ_INT32 not_used; + +} opj_jp2_metadata_t; + +/** + * Index structure of the JP2 file + * EXPERIMENTAL FOR THE MOMENT + */ +typedef struct opj_jp2_index { + /** */ + OPJ_INT32 not_used; + +} opj_jp2_index_t; + + +#ifdef __cplusplus +extern "C" { +#endif + + +/* +========================================================== + openjpeg version +========================================================== +*/ + +/* Get the version of the openjpeg library*/ +OPJ_API const char * OPJ_CALLCONV opj_version(void); + +/* +========================================================== + image functions definitions +========================================================== +*/ + +/** + * Create an image + * + * @param numcmpts number of components + * @param cmptparms components parameters + * @param clrspc image color space + * @return returns a new image structure if successful, returns NULL otherwise + * */ +OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, + opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); + +/** + * Deallocate any resources associated with an image + * + * @param image image to be destroyed + */ +OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image); + +/** + * Creates an image without allocating memory for the image (used in the new version of the library). + * + * @param numcmpts the number of components + * @param cmptparms the components parameters + * @param clrspc the image color space + * + * @return a new image structure if successful, NULL otherwise. +*/ +OPJ_API opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, + opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); + +/** + * Allocator for opj_image_t->comps[].data + * To be paired with opj_image_data_free. + * + * @param size number of bytes to allocate + * + * @return a new pointer if successful, NULL otherwise. + * @since 2.2.0 +*/ +OPJ_API void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size); + +/** + * Destructor for opj_image_t->comps[].data + * To be paired with opj_image_data_alloc. + * + * @param ptr Pointer to free + * + * @since 2.2.0 +*/ +OPJ_API void OPJ_CALLCONV opj_image_data_free(void* ptr); + +/* +========================================================== + stream functions definitions +========================================================== +*/ + +/** + * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream. + * + * @param p_is_input if set to true then the stream will be an input stream, an output stream else. + * + * @return a stream object. +*/ +OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create( + OPJ_BOOL p_is_input); + +/** + * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream. + * + * @param p_buffer_size FIXME DOC + * @param p_is_input if set to true then the stream will be an input stream, an output stream else. + * + * @return a stream object. +*/ +OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size, + OPJ_BOOL p_is_input); + +/** + * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must + * close its own implementation of the stream. + * + * @param p_stream the stream to destroy. + */ +OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream); + +/** + * Sets the given function to be used as a read function. + * @param p_stream the stream to modify + * @param p_function the function to use a read function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, + opj_stream_read_fn p_function); + +/** + * Sets the given function to be used as a write function. + * @param p_stream the stream to modify + * @param p_function the function to use a write function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, + opj_stream_write_fn p_function); + +/** + * Sets the given function to be used as a skip function. + * @param p_stream the stream to modify + * @param p_function the function to use a skip function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, + opj_stream_skip_fn p_function); + +/** + * Sets the given function to be used as a seek function, the stream is then seekable, + * using SEEK_SET behavior. + * @param p_stream the stream to modify + * @param p_function the function to use a skip function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, + opj_stream_seek_fn p_function); + +/** + * Sets the given data to be used as a user data for the stream. + * @param p_stream the stream to modify + * @param p_data the data to set. + * @param p_function the function to free p_data when opj_stream_destroy() is called. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, + void * p_data, opj_stream_free_user_data_fn p_function); + +/** + * Sets the length of the user data for the stream. + * + * @param p_stream the stream to modify + * @param data_length length of the user_data. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_length( + opj_stream_t* p_stream, OPJ_UINT64 data_length); + +/** + * Create a stream from a file identified with its filename with default parameters (helper function) + * @param fname the filename of the file to stream + * @param p_is_read_stream whether the stream is a read stream (true) or not (false) +*/ +OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream( + const char *fname, OPJ_BOOL p_is_read_stream); + +/** Create a stream from a file identified with its filename with a specific buffer size + * @param fname the filename of the file to stream + * @param p_buffer_size size of the chunk used to stream + * @param p_is_read_stream whether the stream is a read stream (true) or not (false) +*/ +OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( + const char *fname, + OPJ_SIZE_T p_buffer_size, + OPJ_BOOL p_is_read_stream); + +/* +========================================================== + event manager functions definitions +========================================================== +*/ +/** + * Set the info handler use by openjpeg. + * @param p_codec the codec previously initialise + * @param p_callback the callback function which will be used + * @param p_user_data client object where will be returned the message +*/ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, + opj_msg_callback p_callback, + void * p_user_data); +/** + * Set the warning handler use by openjpeg. + * @param p_codec the codec previously initialise + * @param p_callback the callback function which will be used + * @param p_user_data client object where will be returned the message +*/ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, + opj_msg_callback p_callback, + void * p_user_data); +/** + * Set the error handler use by openjpeg. + * @param p_codec the codec previously initialise + * @param p_callback the callback function which will be used + * @param p_user_data client object where will be returned the message +*/ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, + opj_msg_callback p_callback, + void * p_user_data); + +/* +========================================================== + codec functions definitions +========================================================== +*/ + +/** + * Creates a J2K/JP2 decompression structure + * @param format Decoder to select + * + * @return Returns a handle to a decompressor if successful, returns NULL otherwise + * */ +OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_decompress( + OPJ_CODEC_FORMAT format); + +/** + * Destroy a decompressor handle + * + * @param p_codec decompressor handle to destroy + */ +OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec); + +/** + * Read after the codestream if necessary + * @param p_codec the JPEG2000 codec to read. + * @param p_stream the JPEG2000 stream. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec, + opj_stream_t *p_stream); + + +/** + * Set decoding parameters to default values + * @param parameters Decompression parameters + */ +OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters( + opj_dparameters_t *parameters); + +/** + * Setup the decoder with decompression parameters provided by the user and with the message handler + * provided by the user. + * + * @param p_codec decompressor handler + * @param parameters decompression parameters + * + * @return true if the decoder is correctly set + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec, + opj_dparameters_t *parameters); + +/** + * Allocates worker threads for the compressor/decompressor. + * + * By default, only the main thread is used. If this function is not used, + * but the OPJ_NUM_THREADS environment variable is set, its value will be + * used to initialize the number of threads. The value can be either an integer + * number, or "ALL_CPUS". If OPJ_NUM_THREADS is set and this function is called, + * this function will override the behaviour of the environment variable. + * + * This function must be called after opj_setup_decoder() and + * before opj_read_header() for the decoding side, or after opj_setup_encoder() + * and before opj_start_compress() for the encoding side. + * + * @param p_codec decompressor or compressor handler + * @param num_threads number of threads. + * + * @return OPJ_TRUE if the function is successful. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_codec_set_threads(opj_codec_t *p_codec, + int num_threads); + +/** + * Decodes an image header. + * + * @param p_stream the jpeg2000 stream. + * @param p_codec the jpeg2000 codec to read. + * @param p_image the image structure initialized with the characteristics of encoded image. + * + * @return true if the main header of the codestream and the JP2 header is correctly read. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream, + opj_codec_t *p_codec, + opj_image_t **p_image); + + +/** Restrict the number of components to decode. + * + * This function should be called after opj_read_header(). + * + * This function enables to restrict the set of decoded components to the + * specified indices. + * Note that the current implementation (apply_color_transforms == OPJ_FALSE) + * is such that neither the multi-component transform at codestream level, + * nor JP2 channel transformations will be applied. + * Consequently the indices are relative to the codestream. + * + * Note: opj_decode_tile_data() should not be used together with opj_set_decoded_components(). + * + * @param p_codec the jpeg2000 codec to read. + * @param numcomps Size of the comps_indices array. + * @param comps_indices Array of numcomps values representing the indices + * of the components to decode (relative to the + * codestream, starting at 0) + * @param apply_color_transforms Whether multi-component transform at codestream level + * or JP2 channel transformations should be applied. + * Currently this parameter should be set to OPJ_FALSE. + * Setting it to OPJ_TRUE will result in an error. + * + * @return OPJ_TRUE in case of success. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec, + OPJ_UINT32 numcomps, + const OPJ_UINT32* comps_indices, + OPJ_BOOL apply_color_transforms); + +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * The coordinates passed to this function should be expressed in the reference grid, + * that is to say at the highest resolution level, even if requesting the image at lower + * resolution levels. + * + * Generally opj_set_decode_area() should be followed by opj_decode(), and the + * codec cannot be re-used. + * In the particular case of an image made of a single tile, several sequences of + * calls to opoj_set_decode_area() and opj_decode() are allowed, and will bring + * performance improvements when reading an image by chunks. + * + * @param p_codec the jpeg2000 codec. + * @param p_image the decoded image previously set by opj_read_header + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * + * @return true if the area could be set. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y); + +/** + * Decode an image from a JPEG-2000 codestream + * + * @param p_decompressor decompressor handle + * @param p_stream Input buffer stream + * @param p_image the decoded image + * @return true if success, otherwise false + * */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_decompressor, + opj_stream_t *p_stream, + opj_image_t *p_image); + +/** + * Get the decoded tile from the codec + * + * @param p_codec the jpeg2000 codec. + * @param p_stream input streamm + * @param p_image output image + * @param tile_index index of the tile which will be decode + * + * @return true if success, otherwise false + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec, + opj_stream_t *p_stream, + opj_image_t *p_image, + OPJ_UINT32 tile_index); + +/** + * Set the resolution factor of the decoded image + * @param p_codec the jpeg2000 codec. + * @param res_factor resolution factor to set + * + * @return true if success, otherwise false + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor( + opj_codec_t *p_codec, OPJ_UINT32 res_factor); + +/** + * Writes a tile with the given data. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence. + * @param p_data pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set. + * @param p_data_size this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of + * tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes, depending on the precision of the given component. + * @param p_stream the stream to write data to. + * + * @return true if the data could be written. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream); + +/** + * Reads a tile header. This function is compulsory and allows one to know the size of the tile that will be decoded. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success. + * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case + * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same + * as depicted in opj_write_tile. + * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image). + * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image). + * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image). + * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image). + * @param p_nb_comps pointer to a value that will hold the number of components in the tile. + * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the + * codestream is over at the time of the call, the value will be set to false. The user should then stop + * the decoding. + * @param p_stream the stream to decode. + * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true. + * returning false may be the result of a shortage of memory or an internal error. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec, + opj_stream_t * p_stream, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_should_go_on); + +/** + * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * Note: opj_decode_tile_data() should not be used together with opj_set_decoded_components(). + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header. + * @param p_data pointer to a memory block that will hold the decoded data. + * @param p_data_size size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header. + * @param p_stream the stream to decode. + * + * @return true if the data could be decoded. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream); + +/* COMPRESSION FUNCTIONS*/ + +/** + * Creates a J2K/JP2 compression structure + * @param format Coder to select + * @return Returns a handle to a compressor if successful, returns NULL otherwise + */ +OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format); + +/** +Set encoding parameters to default values, that means : +
    +
  • Lossless +
  • 1 tile +
  • Size of precinct : 2^15 x 2^15 (means 1 precinct) +
  • Size of code-block : 64 x 64 +
  • Number of resolutions: 6 +
  • No SOP marker in the codestream +
  • No EPH marker in the codestream +
  • No sub-sampling in x or y direction +
  • No mode switch activated +
  • Progression order: LRCP +
  • No index file +
  • No ROI upshifted +
  • No offset of the origin of the image +
  • No offset of the origin of the tiles +
  • Reversible DWT 5-3 +
+@param parameters Compression parameters +*/ +OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters( + opj_cparameters_t *parameters); + +/** + * Setup the encoder parameters using the current image and using user parameters. + * @param p_codec Compressor handle + * @param parameters Compression parameters + * @param image Input filled image + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, + opj_cparameters_t *parameters, + opj_image_t *image); + + +/** + * Specify extra options for the encoder. + * + * This may be called after opj_setup_encoder() and before opj_start_compress() + * + * This is the way to add new options in a fully ABI compatible way, without + * extending the opj_cparameters_t structure. + * + * Currently supported options are: + *
    + *
  • PLT=YES/NO. Defaults to NO. If set to YES, PLT marker segments, + * indicating the length of each packet in the tile-part header, will be + * written. Since 2.3.2
  • + *
+ * + * @param p_codec Compressor handle + * @param p_options Compression options. This should be a NULL terminated + * array of strings. Each string is of the form KEY=VALUE. + * + * @return OPJ_TRUE in case of success. + * @since 2.3.2 + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_encoder_set_extra_options( + opj_codec_t *p_codec, + const char* const* p_options); + +/** + * Start to compress the current image. + * @param p_codec Compressor handle + * @param p_image Input filled image + * @param p_stream Input stgream + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec, + opj_image_t * p_image, + opj_stream_t *p_stream); + +/** + * End to compress the current image. + * @param p_codec Compressor handle + * @param p_stream Input stgream + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec, + opj_stream_t *p_stream); + +/** + * Encode an image into a JPEG-2000 codestream + * @param p_codec compressor handle + * @param p_stream Output buffer stream + * + * @return Returns true if successful, returns false otherwise + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_codec, + opj_stream_t *p_stream); +/* +========================================================== + codec output functions definitions +========================================================== +*/ +/* EXPERIMENTAL FUNCTIONS FOR NOW, USED ONLY IN J2K_DUMP*/ + +/** +Destroy Codestream information after compression or decompression +@param cstr_info Codestream information structure +*/ +OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t + **cstr_info); + + +/** + * Dump the codec information into the output stream + * + * @param p_codec the jpeg2000 codec. + * @param info_flag type of information dump. + * @param output_stream output stream where dump the information gotten from the codec. + * + */ +OPJ_API void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec, + OPJ_INT32 info_flag, + FILE* output_stream); + +/** + * Get the codestream information from the codec + * + * @param p_codec the jpeg2000 codec. + * + * @return a pointer to a codestream information structure. + * + */ +OPJ_API opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info( + opj_codec_t *p_codec); + +/** + * Get the codestream index from the codec + * + * @param p_codec the jpeg2000 codec. + * + * @return a pointer to a codestream index structure. + * + */ +OPJ_API opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index( + opj_codec_t *p_codec); + +OPJ_API void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t + **p_cstr_index); + + +/** + * Get the JP2 file information from the codec FIXME + * + * @param p_codec the jpeg2000 codec. + * + * @return a pointer to a JP2 metadata structure. + * + */ +OPJ_API opj_jp2_metadata_t* OPJ_CALLCONV opj_get_jp2_metadata( + opj_codec_t *p_codec); + +/** + * Get the JP2 file index from the codec FIXME + * + * @param p_codec the jpeg2000 codec. + * + * @return a pointer to a JP2 index structure. + * + */ +OPJ_API opj_jp2_index_t* OPJ_CALLCONV opj_get_jp2_index(opj_codec_t *p_codec); + + +/* +========================================================== + MCT functions +========================================================== +*/ + +/** + * Sets the MCT matrix to use. + * + * @param parameters the parameters to change. + * @param pEncodingMatrix the encoding matrix. + * @param p_dc_shift the dc shift coefficients to use. + * @param pNbComp the number of components of the image. + * + * @return true if the parameters could be set. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters, + OPJ_FLOAT32 * pEncodingMatrix, + OPJ_INT32 * p_dc_shift, + OPJ_UINT32 pNbComp); + +/* +========================================================== + Thread functions +========================================================== +*/ + +/** Returns if the library is built with thread support. + * OPJ_TRUE if mutex, condition, thread, thread pool are available. + */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void); + +/** Return the number of virtual CPUs */ +OPJ_API int OPJ_CALLCONV opj_get_num_cpus(void); + + +#ifdef __cplusplus +} +#endif + +#endif /* OPENJPEG_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_clock.c b/cpp/3rd_party/openjpeg/openjp2/opj_clock.c new file mode 100644 index 0000000000..24f79a9ae7 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_clock.c @@ -0,0 +1,67 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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" + +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif /* _WIN32 */ + +OPJ_FLOAT64 opj_clock(void) +{ +#ifdef _WIN32 + /* _WIN32: use QueryPerformance (very accurate) */ + LARGE_INTEGER freq, t ; + /* freq is the clock speed of the CPU */ + QueryPerformanceFrequency(&freq) ; + /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ + /* t is the high resolution performance counter (see MSDN) */ + QueryPerformanceCounter(& t) ; + return ((OPJ_FLOAT64) t.QuadPart / (OPJ_FLOAT64) freq.QuadPart) ; +#else + /* Unix or Linux: use resource usage */ + struct rusage t; + OPJ_FLOAT64 procTime; + /* (1) Get the rusage data structure at this moment (man getrusage) */ + getrusage(0, &t); + /* (2) What is the elapsed time ? - CPU time = User time + System time */ + /* (2a) Get the seconds */ + procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec); + /* (2b) More precisely! Get the microseconds part ! */ + return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * + 1e-6) ; +#endif +} + diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_clock.h b/cpp/3rd_party/openjpeg/openjp2/opj_clock.h new file mode 100644 index 0000000000..76366f53b5 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_clock.h @@ -0,0 +1,59 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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 OPJ_CLOCK_H +#define OPJ_CLOCK_H +/** +@file opj_clock.h +@brief Internal function for timing + +The functions in OPJ_CLOCK.C are internal utilities mainly used for timing. +*/ + +/** @defgroup MISC MISC - Miscellaneous internal functions */ +/*@{*/ + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Difference in successive opj_clock() calls tells you the elapsed time +@return Returns time in seconds +*/ +OPJ_FLOAT64 opj_clock(void); + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_CLOCK_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_codec.h b/cpp/3rd_party/openjpeg/openjp2/opj_codec.h new file mode 100644 index 0000000000..8a8af9119e --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_codec.h @@ -0,0 +1,176 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * 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 OPJ_CODEC_H +#define OPJ_CODEC_H +/** +@file opj_codec.h +*/ + + +/** + * Main codec handler used for compression or decompression. + */ +typedef struct opj_codec_private { + /** FIXME DOC */ + union { + /** + * Decompression handler. + */ + struct opj_decompression { + /** Main header reading function handler */ + OPJ_BOOL(*opj_read_header)(struct opj_stream_private * cio, + void * p_codec, + opj_image_t **p_image, + struct opj_event_mgr * p_manager); + + /** Decoding function */ + OPJ_BOOL(*opj_decode)(void * p_codec, + struct opj_stream_private * p_cio, + opj_image_t * p_image, + struct opj_event_mgr * p_manager); + + /** FIXME DOC */ + OPJ_BOOL(*opj_read_tile_header)(void * p_codec, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + OPJ_BOOL * p_should_go_on, + struct opj_stream_private * p_cio, + struct opj_event_mgr * p_manager); + + /** FIXME DOC */ + OPJ_BOOL(*opj_decode_tile_data)(void * p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private * p_cio, + struct opj_event_mgr * p_manager); + + /** Reading function used after codestream if necessary */ + OPJ_BOOL(* opj_end_decompress)(void *p_codec, + struct opj_stream_private * cio, + struct opj_event_mgr * p_manager); + + /** Codec destroy function handler */ + void (*opj_destroy)(void * p_codec); + + /** Setup decoder function handler */ + void (*opj_setup_decoder)(void * p_codec, opj_dparameters_t * p_param); + + /** Set decode area function handler */ + OPJ_BOOL(*opj_set_decode_area)(void * p_codec, + opj_image_t * p_image, + OPJ_INT32 p_start_x, + OPJ_INT32 p_end_x, + OPJ_INT32 p_start_y, + OPJ_INT32 p_end_y, + struct opj_event_mgr * p_manager); + + /** Get tile function */ + OPJ_BOOL(*opj_get_decoded_tile)(void *p_codec, + opj_stream_private_t * p_cio, + opj_image_t *p_image, + struct opj_event_mgr * p_manager, + OPJ_UINT32 tile_index); + + /** Set the decoded resolution factor */ + OPJ_BOOL(*opj_set_decoded_resolution_factor)(void * p_codec, + OPJ_UINT32 res_factor, + opj_event_mgr_t * p_manager); + + /** Set the decoded components */ + OPJ_BOOL(*opj_set_decoded_components)(void * p_codec, + OPJ_UINT32 num_comps, + const OPJ_UINT32* comps_indices, + opj_event_mgr_t * p_manager); + } m_decompression; + + /** + * Compression handler. FIXME DOC + */ + struct opj_compression { + OPJ_BOOL(* opj_start_compress)(void *p_codec, + struct opj_stream_private * cio, + struct opj_image * p_image, + struct opj_event_mgr * p_manager); + + OPJ_BOOL(* opj_encode)(void * p_codec, + struct opj_stream_private *p_cio, + struct opj_event_mgr * p_manager); + + OPJ_BOOL(* opj_write_tile)(void * p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private * p_cio, + struct opj_event_mgr * p_manager); + + OPJ_BOOL(* opj_end_compress)(void * p_codec, + struct opj_stream_private * p_cio, + struct opj_event_mgr * p_manager); + + void (* opj_destroy)(void * p_codec); + + OPJ_BOOL(* opj_setup_encoder)(void * p_codec, + opj_cparameters_t * p_param, + struct opj_image * p_image, + struct opj_event_mgr * p_manager); + + OPJ_BOOL(* opj_encoder_set_extra_options)(void * p_codec, + const char* const* p_options, + struct opj_event_mgr * p_manager); + + } m_compression; + } m_codec_data; + /** FIXME DOC*/ + void * m_codec; + /** Event handler */ + opj_event_mgr_t m_event_mgr; + /** Flag to indicate if the codec is used to decode or encode*/ + OPJ_BOOL is_decompressor; + void (*opj_dump_codec)(void * p_codec, OPJ_INT32 info_flag, + FILE* output_stream); + opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec); + opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec); + + /** Set number of threads */ + OPJ_BOOL(*opj_set_threads)(void * p_codec, OPJ_UINT32 num_threads); +} +opj_codec_private_t; + + +#endif /* OPJ_CODEC_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_common.h b/cpp/3rd_party/openjpeg/openjp2/opj_common.h new file mode 100644 index 0000000000..ee8adf4725 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_common.h @@ -0,0 +1,47 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2017, IntoPIX SA + * 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 OPJ_COMMMON_H +#define OPJ_COMMMON_H + +/* + ========================================================== + Common constants shared among several modules + ========================================================== +*/ +#define OPJ_COMMON_CBLK_DATA_EXTRA 2 /**< Margin for a fake FFFF marker */ + + +#define OPJ_COMP_PARAM_DEFAULT_CBLOCKW 64 +#define OPJ_COMP_PARAM_DEFAULT_CBLOCKH 64 +#define OPJ_COMP_PARAM_DEFAULT_PROG_ORDER OPJ_LRCP +#define OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION 6 + +#endif /* OPJ_COMMMON_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_config.h.cmake.in b/cpp/3rd_party/openjpeg/openjp2/opj_config.h.cmake.in new file mode 100644 index 0000000000..049635264f --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_config.h.cmake.in @@ -0,0 +1,7 @@ +/*--------------------------------------------------------------------------*/ +/* OpenJPEG Versioning */ + +/* Version number. */ +#define OPJ_VERSION_MAJOR @OPENJPEG_VERSION_MAJOR@ +#define OPJ_VERSION_MINOR @OPENJPEG_VERSION_MINOR@ +#define OPJ_VERSION_BUILD @OPENJPEG_VERSION_BUILD@ diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_config_private.h.cmake.in b/cpp/3rd_party/openjpeg/openjp2/opj_config_private.h.cmake.in new file mode 100644 index 0000000000..eb5d89222c --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_config_private.h.cmake.in @@ -0,0 +1,39 @@ +/* create opj_config_private.h for CMake */ +#cmakedefine OPJ_HAVE_INTTYPES_H @OPJ_HAVE_INTTYPES_H@ + +#define OPJ_PACKAGE_VERSION "@PACKAGE_VERSION@" + +/* Not used by openjp2*/ +/*#cmakedefine HAVE_STDLIB_H @HAVE_STDLIB_H@*/ +/*#cmakedefine HAVE_SYS_STAT_H @HAVE_SYS_STAT_H@*/ +/*#cmakedefine HAVE_UNISTD_H @HAVE_UNISTD_H@*/ + +#cmakedefine _LARGEFILE_SOURCE +#cmakedefine _LARGE_FILES +#cmakedefine _FILE_OFFSET_BITS @_FILE_OFFSET_BITS@ +#cmakedefine OPJ_HAVE_FSEEKO @OPJ_HAVE_FSEEKO@ + +/* check if function `aligned_alloc` exists */ +#cmakedefine OPJ_HAVE_ALIGNED_ALLOC +/* check if function `_aligned_malloc` exists */ +#cmakedefine OPJ_HAVE__ALIGNED_MALLOC +/* check if function `memalign` exists */ +#cmakedefine OPJ_HAVE_MEMALIGN + +#if !defined(_POSIX_C_SOURCE) +/* Get declarations of fseeko, ftello, posix_memalign. */ +#define _POSIX_C_SOURCE 200112L +#endif + +/* Byte order. */ +/* All compilers that support Mac OS X define either __BIG_ENDIAN__ or +__LITTLE_ENDIAN__ to match the endianness of the architecture being +compiled for. This is not necessarily the same as the architecture of the +machine doing the building. In order to support Universal Binaries on +Mac OS X, we prefer those defines to decide the endianness. +On other platforms we use the result of the TRY_RUN. */ +#if !defined(__APPLE__) +#cmakedefine OPJ_BIG_ENDIAN +#elif defined(__BIG_ENDIAN__) +# define OPJ_BIG_ENDIAN +#endif diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_includes.h b/cpp/3rd_party/openjpeg/openjp2/opj_includes.h new file mode 100644 index 0000000000..0a8628c96b --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_includes.h @@ -0,0 +1,265 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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 OPJ_INCLUDES_H +#define OPJ_INCLUDES_H + +/* + * This must be included before any system headers, + * since they can react to macro defined there + */ +#include "opj_config_private.h" + +/* + ========================================================== + Standard includes used by the library + ========================================================== +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + Use fseeko() and ftello() if they are available since they use + 'off_t' rather than 'long'. It is wrong to use fseeko() and + ftello() only on systems with special LFS support since some systems + (e.g. FreeBSD) support a 64-bit off_t by default. +*/ +#if defined(OPJ_HAVE_FSEEKO) && !defined(fseek) +# define fseek fseeko +# define ftell ftello +#endif + + +#if defined(WIN32) && !defined(Windows95) && !defined(__BORLANDC__) && \ + !(defined(_MSC_VER) && _MSC_VER < 1400) && \ + !(defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x800) +/* + Windows '95 and Borland C do not support _lseeki64 + Visual Studio does not support _fseeki64 and _ftelli64 until the 2005 release. + Without these interfaces, files over 2GB in size are not supported for Windows. +*/ +# define OPJ_FSEEK(stream,offset,whence) _fseeki64(stream,/* __int64 */ offset,whence) +# define OPJ_FSTAT(fildes,stat_buff) _fstati64(fildes,/* struct _stati64 */ stat_buff) +# define OPJ_FTELL(stream) /* __int64 */ _ftelli64(stream) +# define OPJ_STAT_STRUCT_T struct _stati64 +# define OPJ_STAT(path,stat_buff) _stati64(path,/* struct _stati64 */ stat_buff) +#else +# define OPJ_FSEEK(stream,offset,whence) fseek(stream,offset,whence) +# define OPJ_FSTAT(fildes,stat_buff) fstat(fildes,stat_buff) +# define OPJ_FTELL(stream) ftell(stream) +# define OPJ_STAT_STRUCT_T struct stat +# define OPJ_STAT(path,stat_buff) stat(path,stat_buff) +#endif + + +/* + ========================================================== + OpenJPEG interface + ========================================================== + */ +#include "openjpeg.h" + +/* + ========================================================== + OpenJPEG modules + ========================================================== +*/ + +/* Are restricted pointers available? (C99) */ +#if (__STDC_VERSION__ >= 199901L) +#define OPJ_RESTRICT restrict +#else +/* Not a C99 compiler */ +#if defined(__GNUC__) +#define OPJ_RESTRICT __restrict__ + +/* + vc14 (2015) outputs wrong results. + Need to check OPJ_RESTRICT usage (or a bug in vc14) + #elif defined(_MSC_VER) && (_MSC_VER >= 1400) + #define OPJ_RESTRICT __restrict +*/ +#else +#define OPJ_RESTRICT /* restrict */ +#endif +#endif + +#ifdef __has_attribute +#if __has_attribute(no_sanitize) +#define OPJ_NOSANITIZE(kind) __attribute__((no_sanitize(kind))) +#endif +#endif +#ifndef OPJ_NOSANITIZE +#define OPJ_NOSANITIZE(kind) +#endif + + +/* MSVC before 2013 and Borland C do not have lrintf */ +#if defined(_MSC_VER) +#include +static INLINE long opj_lrintf(float f) +{ +#ifdef _M_X64 + return _mm_cvt_ss2si(_mm_load_ss(&f)); + + /* commented out line breaks many tests */ + /* return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f)); */ +#elif defined(_M_IX86) + int i; + _asm{ + fld f + fistp i + }; + + return i; +#else + return (long)((f>0.0f) ? (f + 0.5f) : (f - 0.5f)); +#endif +} +#elif defined(__BORLANDC__) +static INLINE long opj_lrintf(float f) +{ +#ifdef _M_X64 + return (long)((f > 0.0f) ? (f + 0.5f) : (f - 0.5f)); +#else + int i; + + _asm { + fld f + fistp i + }; + + return i; +#endif +} +#else +static INLINE long opj_lrintf(float f) +{ + return lrintf(f); +} +#endif + +#if defined(_MSC_VER) && (_MSC_VER < 1400) +#define vsnprintf _vsnprintf +#endif + +/* MSVC x86 is really bad at doing int64 = int32 * int32 on its own. Use intrinsic. */ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86) +# include +# pragma intrinsic(__emul) +#endif + +/* Apparently Visual Studio doesn't define __SSE__ / __SSE2__ macros */ +#if defined(_M_X64) +/* Intel 64bit support SSE and SSE2 */ +# ifndef __SSE__ +# define __SSE__ 1 +# endif +# ifndef __SSE2__ +# define __SSE2__ 1 +# endif +#endif + +/* For x86, test the value of the _M_IX86_FP macro. */ +/* See https://msdn.microsoft.com/en-us/library/b0084kay.aspx */ +#if defined(_M_IX86_FP) +# if _M_IX86_FP >= 1 +# ifndef __SSE__ +# define __SSE__ 1 +# endif +# endif +# if _M_IX86_FP >= 2 +# ifndef __SSE2__ +# define __SSE2__ 1 +# endif +# endif +#endif + +/* Type to use for bit-fields in internal headers */ +typedef unsigned int OPJ_BITFIELD; + +#define OPJ_UNUSED(x) (void)x + +#include "opj_inttypes.h" +#include "opj_clock.h" +#include "opj_malloc.h" +#include "event.h" +#include "function_list.h" +#include "bio.h" +#include "cio.h" + +#include "thread.h" +#include "tls_keys.h" + +#include "image.h" +#include "invert.h" +#include "j2k.h" +#include "jp2.h" + +#include "mqc.h" +#include "bio.h" + +#include "pi.h" +#include "tgt.h" +#include "tcd.h" +#include "t1.h" +#include "dwt.h" +#include "t2.h" +#include "mct.h" +#include "opj_intmath.h" +#include "sparse_array.h" + +#ifdef USE_JPIP +#include "cidx_manager.h" +#include "indexbox_manager.h" +#endif + +/* JPWL>> */ +#ifdef USE_JPWL +#include "openjpwl/jpwl.h" +#endif /* USE_JPWL */ +/* < b else b +*/ +static INLINE OPJ_INT32 opj_int_max(OPJ_INT32 a, OPJ_INT32 b) +{ + return (a > b) ? a : b; +} + +/** +Get the maximum of two integers +@return Returns a if a > b else b +*/ +static INLINE OPJ_UINT32 opj_uint_max(OPJ_UINT32 a, OPJ_UINT32 b) +{ + return (a > b) ? a : b; +} + +/** + Get the saturated sum of two unsigned integers + @return Returns saturated sum of a+b + */ +static INLINE OPJ_UINT32 opj_uint_adds(OPJ_UINT32 a, OPJ_UINT32 b) +{ + OPJ_UINT64 sum = (OPJ_UINT64)a + (OPJ_UINT64)b; + return (OPJ_UINT32)(-(OPJ_INT32)(sum >> 32)) | (OPJ_UINT32)sum; +} + +/** + Get the saturated difference of two unsigned integers + @return Returns saturated sum of a-b + */ +static INLINE OPJ_UINT32 opj_uint_subs(OPJ_UINT32 a, OPJ_UINT32 b) +{ + return (a >= b) ? a - b : 0; +} + +/** +Clamp an integer inside an interval +@return +
    +
  • Returns a if (min < a < max) +
  • Returns max if (a > max) +
  • Returns min if (a < min) +
+*/ +static INLINE OPJ_INT32 opj_int_clamp(OPJ_INT32 a, OPJ_INT32 min, + OPJ_INT32 max) +{ + if (a < min) { + return min; + } + if (a > max) { + return max; + } + return a; +} + +/** +Clamp an integer inside an interval +@return +
    +
  • Returns a if (min < a < max) +
  • Returns max if (a > max) +
  • Returns min if (a < min) +
+*/ +static INLINE OPJ_INT64 opj_int64_clamp(OPJ_INT64 a, OPJ_INT64 min, + OPJ_INT64 max) +{ + if (a < min) { + return min; + } + if (a > max) { + return max; + } + return a; +} + +/** +@return Get absolute value of integer +*/ +static INLINE OPJ_INT32 opj_int_abs(OPJ_INT32 a) +{ + return a < 0 ? -a : a; +} +/** +Divide an integer and round upwards +@return Returns a divided by b +*/ +static INLINE OPJ_INT32 opj_int_ceildiv(OPJ_INT32 a, OPJ_INT32 b) +{ + assert(b); + return (OPJ_INT32)(((OPJ_INT64)a + b - 1) / b); +} + +/** +Divide an integer and round upwards +@return Returns a divided by b +*/ +static INLINE OPJ_UINT32 opj_uint_ceildiv(OPJ_UINT32 a, OPJ_UINT32 b) +{ + assert(b); + return (OPJ_UINT32)(((OPJ_UINT64)a + b - 1) / b); +} + +/** +Divide an integer by a power of 2 and round upwards +@return Returns a divided by 2^b +*/ +static INLINE OPJ_INT32 opj_int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b) +{ + return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b); +} + +/** + Divide a 64bits integer by a power of 2 and round upwards + @return Returns a divided by 2^b + */ +static INLINE OPJ_INT32 opj_int64_ceildivpow2(OPJ_INT64 a, OPJ_INT32 b) +{ + return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b); +} + +/** + Divide an integer by a power of 2 and round upwards + @return Returns a divided by 2^b + */ +static INLINE OPJ_UINT32 opj_uint_ceildivpow2(OPJ_UINT32 a, OPJ_UINT32 b) +{ + return (OPJ_UINT32)((a + ((OPJ_UINT64)1U << b) - 1U) >> b); +} + +/** +Divide an integer by a power of 2 and round downwards +@return Returns a divided by 2^b +*/ +static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b) +{ + return a >> b; +} + +/** +Divide an integer by a power of 2 and round downwards +@return Returns a divided by 2^b +*/ +static INLINE OPJ_UINT32 opj_uint_floordivpow2(OPJ_UINT32 a, OPJ_UINT32 b) +{ + return a >> b; +} + +/** +Get logarithm of an integer and round downwards +@return Returns log2(a) +*/ +static INLINE OPJ_INT32 opj_int_floorlog2(OPJ_INT32 a) +{ + OPJ_INT32 l; + for (l = 0; a > 1; l++) { + a >>= 1; + } + return l; +} +/** +Get logarithm of an integer and round downwards +@return Returns log2(a) +*/ +static INLINE OPJ_UINT32 opj_uint_floorlog2(OPJ_UINT32 a) +{ + OPJ_UINT32 l; + for (l = 0; a > 1; ++l) { + a >>= 1; + } + return l; +} + +/** +Multiply two fixed-precision rational numbers. +@param a +@param b +@return Returns a * b +*/ +static INLINE OPJ_INT32 opj_int_fix_mul(OPJ_INT32 a, OPJ_INT32 b) +{ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86) + OPJ_INT64 temp = __emul(a, b); +#else + OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ; +#endif + temp += 4096; + assert((temp >> 13) <= (OPJ_INT64)0x7FFFFFFF); + assert((temp >> 13) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1)); + return (OPJ_INT32)(temp >> 13); +} + +static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b) +{ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86) + OPJ_INT64 temp = __emul(a, b); +#else + OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ; +#endif + temp += 4096; + assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) <= (OPJ_INT64)0x7FFFFFFF); + assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) >= (-(OPJ_INT64)0x7FFFFFFF - + (OPJ_INT64)1)); + return (OPJ_INT32)(temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ; +} + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_INTMATH_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_inttypes.h b/cpp/3rd_party/openjpeg/openjp2/opj_inttypes.h new file mode 100644 index 0000000000..752d87eca5 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_inttypes.h @@ -0,0 +1,37 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2012, Mathieu Malaterre + * 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 OPJ_INTTYPES_H +#define OPJ_INTTYPES_H + +#include "opj_config_private.h" +#include + +#endif /* OPJ_INTTYPES_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_malloc.c b/cpp/3rd_party/openjpeg/openjp2/opj_malloc.c new file mode 100644 index 0000000000..c231db6340 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_malloc.c @@ -0,0 +1,153 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2015, Mathieu Malaterre + * Copyright (c) 2015, Matthieu Darbois + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#define OPJ_SKIP_POISON +#include "opj_includes.h" + +#include + +#ifndef SIZE_MAX +# define SIZE_MAX ((size_t) -1) +#endif + +static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) +{ + void* ptr; + + /* alignment shall be power of 2 */ + assert((alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + /* alignment shall be at least sizeof(void*) */ + assert(alignment >= sizeof(void*)); + + if (size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + +#if defined(_WIN32) || defined(_WIN64) + /* Use Windows aligned allocation */ + ptr = _aligned_malloc(size, alignment); +#else + /* aligned_alloc requires c11, restrict to posix_memalign for now. Quote: + * This function was introduced in POSIX 1003.1d. Although this function is + * superseded by aligned_alloc, it is more portable to older POSIX systems + * that do not support ISO C11. */ + if (posix_memalign(&ptr, alignment, size)) { + ptr = NULL; + } +#endif + return ptr; +} +static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, + size_t new_size) +{ + void *r_ptr; + + /* alignment shall be power of 2 */ + assert((alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + /* alignment shall be at least sizeof(void*) */ + assert(alignment >= sizeof(void*)); + + if (new_size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + + /* glibc doc states one can mix aligned malloc with realloc */ + r_ptr = realloc(ptr, new_size); /* fast path */ + /* we simply use `size_t` to cast, since we are only interest in binary AND + * operator */ + if (((size_t)r_ptr & (alignment - 1U)) != 0U) { + /* this is non-trivial to implement a portable aligned realloc, so use a + * simple approach where we do not need a function that return the size of an + * allocated array (eg. _msize on Windows, malloc_size on MacOS, + * malloc_usable_size on systems with glibc) */ + void *a_ptr = opj_aligned_alloc_n(alignment, new_size); + if (a_ptr != NULL) { + memcpy(a_ptr, r_ptr, new_size); + } + free(r_ptr); + r_ptr = a_ptr; + } + return r_ptr; +} +void * opj_malloc(size_t size) +{ + if (size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + return malloc(size); +} +void * opj_calloc(size_t num, size_t size) +{ + if (num == 0 || size == 0) { + /* prevent implementation defined behavior of realloc */ + return NULL; + } + return calloc(num, size); +} + +void *opj_aligned_malloc(size_t size) +{ + return opj_aligned_alloc_n(16U, size); +} +void * opj_aligned_realloc(void *ptr, size_t size) +{ + return opj_aligned_realloc_n(ptr, 16U, size); +} + +void *opj_aligned_32_malloc(size_t size) +{ + return opj_aligned_alloc_n(32U, size); +} +void * opj_aligned_32_realloc(void *ptr, size_t size) +{ + return opj_aligned_realloc_n(ptr, 32U, size); +} + +void opj_aligned_free(void* ptr) +{ +#if defined(_WIN32) || defined(_WIN64) + _aligned_free(ptr); +#else + free(ptr); +#endif +} + +void * opj_realloc(void *ptr, size_t new_size) +{ + if (new_size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + return realloc(ptr, new_size); +} +void opj_free(void *ptr) +{ + free(ptr); +} diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_malloc.h b/cpp/3rd_party/openjpeg/openjp2/opj_malloc.h new file mode 100644 index 0000000000..cbc4106c7c --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_malloc.h @@ -0,0 +1,106 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2007, Callum Lerwick + * 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 OPJ_MALLOC_H +#define OPJ_MALLOC_H + +#include +/** +@file opj_malloc.h +@brief Internal functions + +The functions in opj_malloc.h are internal utilities used for memory management. +*/ + +/** @defgroup MISC MISC - Miscellaneous internal functions */ +/*@{*/ + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Allocate an uninitialized memory block +@param size Bytes to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +void * opj_malloc(size_t size); + +/** +Allocate a memory block with elements initialized to 0 +@param numOfElements Blocks to allocate +@param sizeOfElements Bytes per block to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +void * opj_calloc(size_t numOfElements, size_t sizeOfElements); + +/** +Allocate memory aligned to a 16 byte boundary +@param size Bytes to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +void * opj_aligned_malloc(size_t size); +void * opj_aligned_realloc(void *ptr, size_t size); +void opj_aligned_free(void* ptr); + +/** +Allocate memory aligned to a 32 byte boundary +@param size Bytes to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +void * opj_aligned_32_malloc(size_t size); +void * opj_aligned_32_realloc(void *ptr, size_t size); + +/** +Reallocate memory blocks. +@param m Pointer to previously allocated memory block +@param s New size in bytes +@return Returns a void pointer to the reallocated (and possibly moved) memory block +*/ +void * opj_realloc(void * m, size_t s); + +/** +Deallocates or frees a memory block. +@param m Previously allocated memory block to be freed +*/ +void opj_free(void * m); + +#if defined(__GNUC__) && !defined(OPJ_SKIP_POISON) +#pragma GCC poison malloc calloc realloc free +#endif + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_MALLOC_H */ + diff --git a/cpp/3rd_party/openjpeg/openjp2/opj_stdint.h b/cpp/3rd_party/openjpeg/openjp2/opj_stdint.h new file mode 100644 index 0000000000..e05054fae3 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/opj_stdint.h @@ -0,0 +1,37 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2012, Mathieu Malaterre + * 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 OPJ_STDINT_H +#define OPJ_STDINT_H + +#include "opj_config.h" +#include + +#endif /* OPJ_STDINT_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/pi.c b/cpp/3rd_party/openjpeg/openjp2/pi.c new file mode 100644 index 0000000000..4f7dd50f16 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/pi.c @@ -0,0 +1,2154 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2006-2007, Parvatha Elangovan + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32 + +#include "opj_includes.h" + +/** @defgroup PI PI - Implementation of a packet iterator */ +/*@{*/ + +/** @name Local static functions */ +/*@{*/ + +/** +Get next packet in layer-resolution-component-precinct order. +@param pi packet iterator to modify +@return returns false if pi pointed to the last packet or else returns true +*/ +static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi); +/** +Get next packet in resolution-layer-component-precinct order. +@param pi packet iterator to modify +@return returns false if pi pointed to the last packet or else returns true +*/ +static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi); +/** +Get next packet in resolution-precinct-component-layer order. +@param pi packet iterator to modify +@return returns false if pi pointed to the last packet or else returns true +*/ +static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi); +/** +Get next packet in precinct-component-resolution-layer order. +@param pi packet iterator to modify +@return returns false if pi pointed to the last packet or else returns true +*/ +static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi); +/** +Get next packet in component-precinct-resolution-layer order. +@param pi packet iterator to modify +@return returns false if pi pointed to the last packet or else returns true +*/ +static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi); + +/** + * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used). + * + * @param p_cp the coding parameters to modify + * @param p_tileno the tile index being concerned. + * @param p_tx0 X0 parameter for the tile + * @param p_tx1 X1 parameter for the tile + * @param p_ty0 Y0 parameter for the tile + * @param p_ty1 Y1 parameter for the tile + * @param p_max_prec the maximum precision for all the bands of the tile + * @param p_max_res the maximum number of resolutions for all the poc inside the tile. + * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile. + * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile. + */ +static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, + OPJ_UINT32 p_tileno, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min); + +/** + * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used). + * + * @param p_cp the coding parameters to modify + * @param p_num_comps the number of components + * @param p_tileno the tile index being concerned. + * @param p_tx0 X0 parameter for the tile + * @param p_tx1 X1 parameter for the tile + * @param p_ty0 Y0 parameter for the tile + * @param p_ty1 Y1 parameter for the tile + * @param p_max_prec the maximum precision for all the bands of the tile + * @param p_max_res the maximum number of resolutions for all the poc inside the tile. + * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile. + * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile. + */ +static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, + OPJ_UINT32 p_num_comps, + OPJ_UINT32 p_tileno, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min); +/** + * Gets the encoding parameters needed to update the coding parameters and all the pocs. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param tileno the tile index of the tile being encoded. + * @param p_tx0 pointer that will hold the X0 parameter for the tile + * @param p_tx1 pointer that will hold the X1 parameter for the tile + * @param p_ty0 pointer that will hold the Y0 parameter for the tile + * @param p_ty1 pointer that will hold the Y1 parameter for the tile + * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile + * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile. + * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile. + * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile. + */ +static void opj_get_encoding_parameters(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res); + +/** + * Gets the encoding parameters needed to update the coding parameters and all the pocs. + * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well. + * the last parameter of the function should be an array of pointers of size nb components, each pointer leading + * to an area of size 4 * max_res. The data is stored inside this area with the following pattern : + * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ... + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param tileno the tile index of the tile being encoded. + * @param p_tx0 pointer that will hold the X0 parameter for the tile + * @param p_tx1 pointer that will hold the X1 parameter for the tile + * @param p_ty0 pointer that will hold the Y0 parameter for the tile + * @param p_ty1 pointer that will hold the Y1 parameter for the tile + * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile + * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile. + * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile. + * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile. + * @param p_resolutions pointer to an area corresponding to the one described above. + */ +static void opj_get_all_encoding_parameters(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res, + OPJ_UINT32 ** p_resolutions); +/** + * Allocates memory for a packet iterator. Data and data sizes are set by this operation. + * No other data is set. The include section of the packet iterator is not allocated. + * + * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant. + * @param p_cp the coding parameters. + * @param tileno the index of the tile from which creating the packet iterator. + * @param manager Event manager + */ +static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + opj_event_mgr_t* manager); +/** + * FIXME DOC + */ +static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi, + opj_tcp_t * p_tcp, + OPJ_UINT32 p_max_precision, + OPJ_UINT32 p_max_res); +/** + * FIXME DOC + */ +static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi, + opj_tcp_t * p_tcp, + OPJ_UINT32 p_max_precision, + OPJ_UINT32 p_max_res); + +/** + * FIXME DOC + */ +static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos, + opj_cp_t *cp, + OPJ_UINT32 tileno, + OPJ_UINT32 pino, + const OPJ_CHAR *prog); + +/*@}*/ + +/*@}*/ + +/* +========================================================== + local functions +========================================================== +*/ + +static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) +{ + opj_pi_comp_t *comp = NULL; + opj_pi_resolution_t *res = NULL; + OPJ_UINT32 index = 0; + + if (pi->poc.compno0 >= pi->numcomps || + pi->poc.compno1 >= pi->numcomps + 1) { + opj_event_msg(pi->manager, EVT_ERROR, + "opj_pi_next_lrcp(): invalid compno0/compno1\n"); + return OPJ_FALSE; + } + + if (!pi->first) { + comp = &pi->comps[pi->compno]; + res = &comp->resolutions[pi->resno]; + goto LABEL_SKIP; + } else { + pi->first = 0; + } + + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; + pi->resno++) { + for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { + comp = &pi->comps[pi->compno]; + if (pi->resno >= comp->numresolutions) { + continue; + } + res = &comp->resolutions[pi->resno]; + if (!pi->tp_on) { + pi->poc.precno1 = res->pw * res->ph; + } + for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) { + index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * + pi->step_c + pi->precno * pi->step_p; + /* Avoids index out of bounds access with */ + /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */ + /* https://github.com/uclouvain/openjpeg/issues/938 */ + /* Not sure if this is the most clever fix. Perhaps */ + /* include should be resized when a POC arises, or */ + /* the POC should be rejected */ + if (index >= pi->include_size) { + opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); + return OPJ_FALSE; + } + if (!pi->include[index]) { + pi->include[index] = 1; + return OPJ_TRUE; + } +LABEL_SKIP: + ; + } + } + } + } + + return OPJ_FALSE; +} + +static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) +{ + opj_pi_comp_t *comp = NULL; + opj_pi_resolution_t *res = NULL; + OPJ_UINT32 index = 0; + + if (pi->poc.compno0 >= pi->numcomps || + pi->poc.compno1 >= pi->numcomps + 1) { + opj_event_msg(pi->manager, EVT_ERROR, + "opj_pi_next_rlcp(): invalid compno0/compno1\n"); + return OPJ_FALSE; + } + + if (!pi->first) { + comp = &pi->comps[pi->compno]; + res = &comp->resolutions[pi->resno]; + goto LABEL_SKIP; + } else { + pi->first = 0; + } + + for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { + for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { + comp = &pi->comps[pi->compno]; + if (pi->resno >= comp->numresolutions) { + continue; + } + res = &comp->resolutions[pi->resno]; + if (!pi->tp_on) { + pi->poc.precno1 = res->pw * res->ph; + } + for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) { + index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * + pi->step_c + pi->precno * pi->step_p; + if (index >= pi->include_size) { + opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); + return OPJ_FALSE; + } + if (!pi->include[index]) { + pi->include[index] = 1; + return OPJ_TRUE; + } +LABEL_SKIP: + ; + } + } + } + } + + return OPJ_FALSE; +} + +static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) +{ + opj_pi_comp_t *comp = NULL; + opj_pi_resolution_t *res = NULL; + OPJ_UINT32 index = 0; + + if (pi->poc.compno0 >= pi->numcomps || + pi->poc.compno1 >= pi->numcomps + 1) { + opj_event_msg(pi->manager, EVT_ERROR, + "opj_pi_next_rpcl(): invalid compno0/compno1\n"); + return OPJ_FALSE; + } + + if (!pi->first) { + goto LABEL_SKIP; + } else { + OPJ_UINT32 compno, resno; + pi->first = 0; + pi->dx = 0; + pi->dy = 0; + for (compno = 0; compno < pi->numcomps; compno++) { + comp = &pi->comps[compno]; + for (resno = 0; resno < comp->numresolutions; resno++) { + OPJ_UINT32 dx, dy; + res = &comp->resolutions[resno]; + if (res->pdx + comp->numresolutions - 1 - resno < 32 && + comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) { + dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); + pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx); + } + if (res->pdy + comp->numresolutions - 1 - resno < 32 && + comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) { + dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy); + } + } + } + if (pi->dx == 0 || pi->dy == 0) { + return OPJ_FALSE; + } + } + if (!pi->tp_on) { + pi->poc.ty0 = pi->ty0; + pi->poc.tx0 = pi->tx0; + pi->poc.ty1 = pi->ty1; + pi->poc.tx1 = pi->tx1; + } + for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { + for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1; + pi->y += (pi->dy - (pi->y % pi->dy))) { + for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1; + pi->x += (pi->dx - (pi->x % pi->dx))) { + for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { + OPJ_UINT32 levelno; + OPJ_UINT32 trx0, try0; + OPJ_UINT32 trx1, try1; + OPJ_UINT32 rpx, rpy; + OPJ_UINT32 prci, prcj; + comp = &pi->comps[pi->compno]; + if (pi->resno >= comp->numresolutions) { + continue; + } + res = &comp->resolutions[pi->resno]; + levelno = comp->numresolutions - 1 - pi->resno; + /* Avoids division by zero */ + /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */ + /* of https://github.com/uclouvain/openjpeg/issues/938 */ + if (levelno >= 32 || + ((comp->dx << levelno) >> levelno) != comp->dx || + ((comp->dy << levelno) >> levelno) != comp->dy) { + continue; + } + if ((comp->dx << levelno) > INT_MAX || + (comp->dy << levelno) > INT_MAX) { + continue; + } + trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno)); + try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno)); + trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno)); + try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno)); + rpx = res->pdx + levelno; + rpy = res->pdy + levelno; + + /* To avoid divisions by zero / undefined behaviour on shift */ + /* in below tests */ + /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */ + /* of https://github.com/uclouvain/openjpeg/issues/938 */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) { + continue; + } + + /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */ + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && + ((try0 << levelno) % (1U << rpy))))) { + continue; + } + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && + ((trx0 << levelno) % (1U << rpx))))) { + continue; + } + + if ((res->pw == 0) || (res->ph == 0)) { + continue; + } + + if ((trx0 == trx1) || (try0 == try1)) { + continue; + } + + prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x, + (comp->dx << levelno)), res->pdx) + - opj_uint_floordivpow2(trx0, res->pdx); + prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y, + (comp->dy << levelno)), res->pdy) + - opj_uint_floordivpow2(try0, res->pdy); + pi->precno = prci + prcj * res->pw; + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { + index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * + pi->step_c + pi->precno * pi->step_p; + if (index >= pi->include_size) { + opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); + return OPJ_FALSE; + } + if (!pi->include[index]) { + pi->include[index] = 1; + return OPJ_TRUE; + } +LABEL_SKIP: + ; + } + } + } + } + } + + return OPJ_FALSE; +} + +static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) +{ + opj_pi_comp_t *comp = NULL; + opj_pi_resolution_t *res = NULL; + OPJ_UINT32 index = 0; + + if (pi->poc.compno0 >= pi->numcomps || + pi->poc.compno1 >= pi->numcomps + 1) { + opj_event_msg(pi->manager, EVT_ERROR, + "opj_pi_next_pcrl(): invalid compno0/compno1\n"); + return OPJ_FALSE; + } + + if (!pi->first) { + comp = &pi->comps[pi->compno]; + goto LABEL_SKIP; + } else { + OPJ_UINT32 compno, resno; + pi->first = 0; + pi->dx = 0; + pi->dy = 0; + for (compno = 0; compno < pi->numcomps; compno++) { + comp = &pi->comps[compno]; + for (resno = 0; resno < comp->numresolutions; resno++) { + OPJ_UINT32 dx, dy; + res = &comp->resolutions[resno]; + if (res->pdx + comp->numresolutions - 1 - resno < 32 && + comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) { + dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); + pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx); + } + if (res->pdy + comp->numresolutions - 1 - resno < 32 && + comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) { + dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy); + } + } + } + if (pi->dx == 0 || pi->dy == 0) { + return OPJ_FALSE; + } + } + if (!pi->tp_on) { + pi->poc.ty0 = pi->ty0; + pi->poc.tx0 = pi->tx0; + pi->poc.ty1 = pi->ty1; + pi->poc.tx1 = pi->tx1; + } + for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1; + pi->y += (pi->dy - (pi->y % pi->dy))) { + for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1; + pi->x += (pi->dx - (pi->x % pi->dx))) { + for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { + comp = &pi->comps[pi->compno]; + for (pi->resno = pi->poc.resno0; + pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { + OPJ_UINT32 levelno; + OPJ_UINT32 trx0, try0; + OPJ_UINT32 trx1, try1; + OPJ_UINT32 rpx, rpy; + OPJ_UINT32 prci, prcj; + res = &comp->resolutions[pi->resno]; + levelno = comp->numresolutions - 1 - pi->resno; + /* Avoids division by zero */ + /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */ + /* of https://github.com/uclouvain/openjpeg/issues/938 */ + if (levelno >= 32 || + ((comp->dx << levelno) >> levelno) != comp->dx || + ((comp->dy << levelno) >> levelno) != comp->dy) { + continue; + } + if ((comp->dx << levelno) > INT_MAX || + (comp->dy << levelno) > INT_MAX) { + continue; + } + trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno)); + try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno)); + trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno)); + try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno)); + rpx = res->pdx + levelno; + rpy = res->pdy + levelno; + + /* To avoid divisions by zero / undefined behaviour on shift */ + /* in below tests */ + /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */ + /* of https://github.com/uclouvain/openjpeg/issues/938 */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) { + continue; + } + + /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */ + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && + ((try0 << levelno) % (1U << rpy))))) { + continue; + } + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && + ((trx0 << levelno) % (1U << rpx))))) { + continue; + } + + if ((res->pw == 0) || (res->ph == 0)) { + continue; + } + + if ((trx0 == trx1) || (try0 == try1)) { + continue; + } + + prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x, + (comp->dx << levelno)), res->pdx) + - opj_uint_floordivpow2(trx0, res->pdx); + prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y, + (comp->dy << levelno)), res->pdy) + - opj_uint_floordivpow2(try0, res->pdy); + pi->precno = prci + prcj * res->pw; + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { + index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * + pi->step_c + pi->precno * pi->step_p; + if (index >= pi->include_size) { + opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); + return OPJ_FALSE; + } + if (!pi->include[index]) { + pi->include[index] = 1; + return OPJ_TRUE; + } +LABEL_SKIP: + ; + } + } + } + } + } + + return OPJ_FALSE; +} + +static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) +{ + opj_pi_comp_t *comp = NULL; + opj_pi_resolution_t *res = NULL; + OPJ_UINT32 index = 0; + + if (pi->poc.compno0 >= pi->numcomps || + pi->poc.compno1 >= pi->numcomps + 1) { + opj_event_msg(pi->manager, EVT_ERROR, + "opj_pi_next_cprl(): invalid compno0/compno1\n"); + return OPJ_FALSE; + } + + if (!pi->first) { + comp = &pi->comps[pi->compno]; + goto LABEL_SKIP; + } else { + pi->first = 0; + } + + for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { + OPJ_UINT32 resno; + comp = &pi->comps[pi->compno]; + pi->dx = 0; + pi->dy = 0; + for (resno = 0; resno < comp->numresolutions; resno++) { + OPJ_UINT32 dx, dy; + res = &comp->resolutions[resno]; + if (res->pdx + comp->numresolutions - 1 - resno < 32 && + comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) { + dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); + pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx); + } + if (res->pdy + comp->numresolutions - 1 - resno < 32 && + comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) { + dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy); + } + } + if (pi->dx == 0 || pi->dy == 0) { + return OPJ_FALSE; + } + if (!pi->tp_on) { + pi->poc.ty0 = pi->ty0; + pi->poc.tx0 = pi->tx0; + pi->poc.ty1 = pi->ty1; + pi->poc.tx1 = pi->tx1; + } + for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1; + pi->y += (pi->dy - (pi->y % pi->dy))) { + for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1; + pi->x += (pi->dx - (pi->x % pi->dx))) { + for (pi->resno = pi->poc.resno0; + pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { + OPJ_UINT32 levelno; + OPJ_UINT32 trx0, try0; + OPJ_UINT32 trx1, try1; + OPJ_UINT32 rpx, rpy; + OPJ_UINT32 prci, prcj; + res = &comp->resolutions[pi->resno]; + levelno = comp->numresolutions - 1 - pi->resno; + /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */ + /* of https://github.com/uclouvain/openjpeg/issues/938 */ + if (levelno >= 32 || + ((comp->dx << levelno) >> levelno) != comp->dx || + ((comp->dy << levelno) >> levelno) != comp->dy) { + continue; + } + if ((comp->dx << levelno) > INT_MAX || + (comp->dy << levelno) > INT_MAX) { + continue; + } + trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno)); + try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno)); + trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno)); + try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno)); + rpx = res->pdx + levelno; + rpy = res->pdy + levelno; + + /* To avoid divisions by zero / undefined behaviour on shift */ + /* in below tests */ + /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */ + /* of https://github.com/uclouvain/openjpeg/issues/938 */ + if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx || + rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) { + continue; + } + + /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */ + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && + ((try0 << levelno) % (1U << rpy))))) { + continue; + } + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && + ((trx0 << levelno) % (1U << rpx))))) { + continue; + } + + if ((res->pw == 0) || (res->ph == 0)) { + continue; + } + + if ((trx0 == trx1) || (try0 == try1)) { + continue; + } + + prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x, + (comp->dx << levelno)), res->pdx) + - opj_uint_floordivpow2(trx0, res->pdx); + prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y, + (comp->dy << levelno)), res->pdy) + - opj_uint_floordivpow2(try0, res->pdy); + pi->precno = (OPJ_UINT32)(prci + prcj * res->pw); + for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { + index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * + pi->step_c + pi->precno * pi->step_p; + if (index >= pi->include_size) { + opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); + return OPJ_FALSE; + } + if (!pi->include[index]) { + pi->include[index] = 1; + return OPJ_TRUE; + } +LABEL_SKIP: + ; + } + } + } + } + } + + return OPJ_FALSE; +} + +static void opj_get_encoding_parameters(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 p_tileno, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res) +{ + /* loop */ + OPJ_UINT32 compno, resno; + /* pointers */ + const opj_tcp_t *l_tcp = 00; + const opj_tccp_t * l_tccp = 00; + const opj_image_comp_t * l_img_comp = 00; + + /* position in x and y of tile */ + OPJ_UINT32 p, q; + + /* non-corrected (in regard to image offset) tile offset */ + OPJ_UINT32 l_tx0, l_ty0; + + /* preconditions */ + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tileno < p_cp->tw * p_cp->th); + + /* initializations */ + l_tcp = &p_cp->tcps [p_tileno]; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */ + p = p_tileno % p_cp->tw; + q = p_tileno / p_cp->tw; + + /* find extent of tile */ + l_tx0 = p_cp->tx0 + p * + p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */ + *p_tx0 = opj_uint_max(l_tx0, p_image->x0); + *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); + l_ty0 = p_cp->ty0 + q * + p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ + *p_ty0 = opj_uint_max(l_ty0, p_image->y0); + *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); + + /* max precision is 0 (can only grow) */ + *p_max_prec = 0; + *p_max_res = 0; + + /* take the largest value for dx_min and dy_min */ + *p_dx_min = 0x7fffffff; + *p_dy_min = 0x7fffffff; + + for (compno = 0; compno < p_image->numcomps; ++compno) { + /* arithmetic variables to calculate */ + OPJ_UINT32 l_level_no; + OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1; + OPJ_UINT32 l_px0, l_py0, l_px1, py1; + OPJ_UINT32 l_pdx, l_pdy; + OPJ_UINT32 l_pw, l_ph; + OPJ_UINT32 l_product; + OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; + + l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx); + l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy); + l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx); + l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy); + + if (l_tccp->numresolutions > *p_max_res) { + *p_max_res = l_tccp->numresolutions; + } + + /* use custom size for precincts */ + for (resno = 0; resno < l_tccp->numresolutions; ++resno) { + OPJ_UINT32 l_dx, l_dy; + + /* precinct width and height */ + l_pdx = l_tccp->prcw[resno]; + l_pdy = l_tccp->prch[resno]; + + l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno)); + l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno)); + + /* take the minimum size for dx for each comp and resolution */ + *p_dx_min = opj_uint_min(*p_dx_min, l_dx); + *p_dy_min = opj_uint_min(*p_dy_min, l_dy); + + /* various calculations of extents */ + l_level_no = l_tccp->numresolutions - 1 - resno; + + l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no); + l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no); + l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no); + l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no); + + l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx; + l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy; + l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx; + + py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy; + + l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx); + l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy); + + l_product = l_pw * l_ph; + + /* update precision */ + if (l_product > *p_max_prec) { + *p_max_prec = l_product; + } + } + ++l_img_comp; + ++l_tccp; + } +} + + +static void opj_get_all_encoding_parameters(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res, + OPJ_UINT32 ** p_resolutions) +{ + /* loop*/ + OPJ_UINT32 compno, resno; + + /* pointers*/ + const opj_tcp_t *tcp = 00; + const opj_tccp_t * l_tccp = 00; + const opj_image_comp_t * l_img_comp = 00; + + /* to store l_dx, l_dy, w and h for each resolution and component.*/ + OPJ_UINT32 * lResolutionPtr; + + /* position in x and y of tile*/ + OPJ_UINT32 p, q; + + /* non-corrected (in regard to image offset) tile offset */ + OPJ_UINT32 l_tx0, l_ty0; + + /* preconditions in debug*/ + assert(p_cp != 00); + assert(p_image != 00); + assert(tileno < p_cp->tw * p_cp->th); + + /* initializations*/ + tcp = &p_cp->tcps [tileno]; + l_tccp = tcp->tccps; + l_img_comp = p_image->comps; + + /* position in x and y of tile*/ + p = tileno % p_cp->tw; + q = tileno / p_cp->tw; + + /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */ + l_tx0 = p_cp->tx0 + p * + p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */ + *p_tx0 = opj_uint_max(l_tx0, p_image->x0); + *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); + l_ty0 = p_cp->ty0 + q * + p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ + *p_ty0 = opj_uint_max(l_ty0, p_image->y0); + *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); + + /* max precision and resolution is 0 (can only grow)*/ + *p_max_prec = 0; + *p_max_res = 0; + + /* take the largest value for dx_min and dy_min*/ + *p_dx_min = 0x7fffffff; + *p_dy_min = 0x7fffffff; + + for (compno = 0; compno < p_image->numcomps; ++compno) { + /* aritmetic variables to calculate*/ + OPJ_UINT32 l_level_no; + OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1; + OPJ_UINT32 l_px0, l_py0, l_px1, py1; + OPJ_UINT32 l_product; + OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; + OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph; + + lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL; + + l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx); + l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy); + l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx); + l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy); + + if (l_tccp->numresolutions > *p_max_res) { + *p_max_res = l_tccp->numresolutions; + } + + /* use custom size for precincts*/ + l_level_no = l_tccp->numresolutions; + for (resno = 0; resno < l_tccp->numresolutions; ++resno) { + OPJ_UINT32 l_dx, l_dy; + + --l_level_no; + + /* precinct width and height*/ + l_pdx = l_tccp->prcw[resno]; + l_pdy = l_tccp->prch[resno]; + if (lResolutionPtr) { + *lResolutionPtr++ = l_pdx; + *lResolutionPtr++ = l_pdy; + } + if (l_pdx + l_level_no < 32 && + l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) { + l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no)); + /* take the minimum size for l_dx for each comp and resolution*/ + *p_dx_min = opj_uint_min(*p_dx_min, l_dx); + } + if (l_pdy + l_level_no < 32 && + l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) { + l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no)); + *p_dy_min = opj_uint_min(*p_dy_min, l_dy); + } + + /* various calculations of extents*/ + l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no); + l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no); + l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no); + l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no); + l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx; + l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy; + l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx; + py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy; + l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx); + l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy); + if (lResolutionPtr) { + *lResolutionPtr++ = l_pw; + *lResolutionPtr++ = l_ph; + } + l_product = l_pw * l_ph; + + /* update precision*/ + if (l_product > *p_max_prec) { + *p_max_prec = l_product; + } + + } + ++l_tccp; + ++l_img_comp; + } +} + +static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image, + const opj_cp_t *cp, + OPJ_UINT32 tileno, + opj_event_mgr_t* manager) +{ + /* loop*/ + OPJ_UINT32 pino, compno; + /* number of poc in the p_pi*/ + OPJ_UINT32 l_poc_bound; + + /* pointers to tile coding parameters and components.*/ + opj_pi_iterator_t *l_pi = 00; + opj_tcp_t *tcp = 00; + const opj_tccp_t *tccp = 00; + + /* current packet iterator being allocated*/ + opj_pi_iterator_t *l_current_pi = 00; + + /* preconditions in debug*/ + assert(cp != 00); + assert(image != 00); + assert(tileno < cp->tw * cp->th); + + /* initializations*/ + tcp = &cp->tcps[tileno]; + l_poc_bound = tcp->numpocs + 1; + + /* memory allocations*/ + l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), + sizeof(opj_pi_iterator_t)); + if (!l_pi) { + return NULL; + } + + l_current_pi = l_pi; + for (pino = 0; pino < l_poc_bound ; ++pino) { + + l_current_pi->manager = manager; + + l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, + sizeof(opj_pi_comp_t)); + if (! l_current_pi->comps) { + opj_pi_destroy(l_pi, l_poc_bound); + return NULL; + } + + l_current_pi->numcomps = image->numcomps; + + for (compno = 0; compno < image->numcomps; ++compno) { + opj_pi_comp_t *comp = &l_current_pi->comps[compno]; + + tccp = &tcp->tccps[compno]; + + comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions, + sizeof(opj_pi_resolution_t)); + if (!comp->resolutions) { + opj_pi_destroy(l_pi, l_poc_bound); + return 00; + } + + comp->numresolutions = tccp->numresolutions; + } + ++l_current_pi; + } + return l_pi; +} + +static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, + OPJ_UINT32 p_tileno, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min) +{ + /* loop*/ + OPJ_UINT32 pino; + /* tile coding parameter*/ + opj_tcp_t *l_tcp = 00; + /* current poc being updated*/ + opj_poc_t * l_current_poc = 00; + + /* number of pocs*/ + OPJ_UINT32 l_poc_bound; + + OPJ_ARG_NOT_USED(p_max_res); + + /* preconditions in debug*/ + assert(p_cp != 00); + assert(p_tileno < p_cp->tw * p_cp->th); + + /* initializations*/ + l_tcp = &p_cp->tcps [p_tileno]; + /* number of iterations in the loop */ + l_poc_bound = l_tcp->numpocs + 1; + + /* start at first element, and to make sure the compiler will not make a calculation each time in the loop + store a pointer to the current element to modify rather than l_tcp->pocs[i]*/ + l_current_poc = l_tcp->pocs; + + l_current_poc->compS = l_current_poc->compno0; + l_current_poc->compE = l_current_poc->compno1; + l_current_poc->resS = l_current_poc->resno0; + l_current_poc->resE = l_current_poc->resno1; + l_current_poc->layE = l_current_poc->layno1; + + /* special treatment for the first element*/ + l_current_poc->layS = 0; + l_current_poc->prg = l_current_poc->prg1; + l_current_poc->prcS = 0; + + l_current_poc->prcE = p_max_prec; + l_current_poc->txS = (OPJ_UINT32)p_tx0; + l_current_poc->txE = (OPJ_UINT32)p_tx1; + l_current_poc->tyS = (OPJ_UINT32)p_ty0; + l_current_poc->tyE = (OPJ_UINT32)p_ty1; + l_current_poc->dx = p_dx_min; + l_current_poc->dy = p_dy_min; + + ++ l_current_poc; + for (pino = 1; pino < l_poc_bound ; ++pino) { + l_current_poc->compS = l_current_poc->compno0; + l_current_poc->compE = l_current_poc->compno1; + l_current_poc->resS = l_current_poc->resno0; + l_current_poc->resE = l_current_poc->resno1; + l_current_poc->layE = l_current_poc->layno1; + l_current_poc->prg = l_current_poc->prg1; + l_current_poc->prcS = 0; + /* special treatment here different from the first element*/ + l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ? + l_current_poc->layE : 0; + + l_current_poc->prcE = p_max_prec; + l_current_poc->txS = (OPJ_UINT32)p_tx0; + l_current_poc->txE = (OPJ_UINT32)p_tx1; + l_current_poc->tyS = (OPJ_UINT32)p_ty0; + l_current_poc->tyE = (OPJ_UINT32)p_ty1; + l_current_poc->dx = p_dx_min; + l_current_poc->dy = p_dy_min; + ++ l_current_poc; + } +} + +static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, + OPJ_UINT32 p_num_comps, + OPJ_UINT32 p_tileno, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min) +{ + /* loop*/ + OPJ_UINT32 pino; + /* tile coding parameter*/ + opj_tcp_t *l_tcp = 00; + /* current poc being updated*/ + opj_poc_t * l_current_poc = 00; + /* number of pocs*/ + OPJ_UINT32 l_poc_bound; + + /* preconditions in debug*/ + assert(p_cp != 00); + assert(p_tileno < p_cp->tw * p_cp->th); + + /* initializations*/ + l_tcp = &p_cp->tcps [p_tileno]; + + /* number of iterations in the loop */ + l_poc_bound = l_tcp->numpocs + 1; + + /* start at first element, and to make sure the compiler will not make a calculation each time in the loop + store a pointer to the current element to modify rather than l_tcp->pocs[i]*/ + l_current_poc = l_tcp->pocs; + + for (pino = 0; pino < l_poc_bound ; ++pino) { + l_current_poc->compS = 0; + l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/ + l_current_poc->resS = 0; + l_current_poc->resE = p_max_res; + l_current_poc->layS = 0; + l_current_poc->layE = l_tcp->numlayers; + l_current_poc->prg = l_tcp->prg; + l_current_poc->prcS = 0; + l_current_poc->prcE = p_max_prec; + l_current_poc->txS = p_tx0; + l_current_poc->txE = p_tx1; + l_current_poc->tyS = p_ty0; + l_current_poc->tyE = p_ty1; + l_current_poc->dx = p_dx_min; + l_current_poc->dy = p_dy_min; + ++ l_current_poc; + } +} + +static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi, + opj_tcp_t * p_tcp, + OPJ_UINT32 p_max_precision, + OPJ_UINT32 p_max_res) +{ + /* loop*/ + OPJ_UINT32 pino; + + /* encoding prameters to set*/ + OPJ_UINT32 l_bound; + + opj_pi_iterator_t * l_current_pi = 00; + opj_poc_t* l_current_poc = 0; + + OPJ_ARG_NOT_USED(p_max_res); + + /* preconditions in debug*/ + assert(p_pi != 00); + assert(p_tcp != 00); + + /* initializations*/ + l_bound = p_tcp->numpocs + 1; + l_current_pi = p_pi; + l_current_poc = p_tcp->pocs; + + for (pino = 0; pino < l_bound; ++pino) { + l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */ + l_current_pi->first = 1; + + l_current_pi->poc.resno0 = + l_current_poc->resno0; /* Resolution Level Index #0 (Start) */ + l_current_pi->poc.compno0 = + l_current_poc->compno0; /* Component Index #0 (Start) */ + l_current_pi->poc.layno0 = 0; + l_current_pi->poc.precno0 = 0; + l_current_pi->poc.resno1 = + l_current_poc->resno1; /* Resolution Level Index #0 (End) */ + l_current_pi->poc.compno1 = + l_current_poc->compno1; /* Component Index #0 (End) */ + l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1, + p_tcp->numlayers); /* Layer Index #0 (End) */ + l_current_pi->poc.precno1 = p_max_precision; + ++l_current_pi; + ++l_current_poc; + } +} + +static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi, + opj_tcp_t * p_tcp, + OPJ_UINT32 p_max_precision, + OPJ_UINT32 p_max_res) +{ + /* loop*/ + OPJ_UINT32 pino; + + /* encoding prameters to set*/ + OPJ_UINT32 l_bound; + + opj_pi_iterator_t * l_current_pi = 00; + /* preconditions in debug*/ + assert(p_tcp != 00); + assert(p_pi != 00); + + /* initializations*/ + l_bound = p_tcp->numpocs + 1; + l_current_pi = p_pi; + + for (pino = 0; pino < l_bound; ++pino) { + l_current_pi->poc.prg = p_tcp->prg; + l_current_pi->first = 1; + l_current_pi->poc.resno0 = 0; + l_current_pi->poc.compno0 = 0; + l_current_pi->poc.layno0 = 0; + l_current_pi->poc.precno0 = 0; + l_current_pi->poc.resno1 = p_max_res; + l_current_pi->poc.compno1 = l_current_pi->numcomps; + l_current_pi->poc.layno1 = p_tcp->numlayers; + l_current_pi->poc.precno1 = p_max_precision; + ++l_current_pi; + } +} + + + +static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos, + opj_cp_t *cp, + OPJ_UINT32 tileno, + OPJ_UINT32 pino, + const OPJ_CHAR *prog) +{ + OPJ_INT32 i; + opj_tcp_t *tcps = &cp->tcps[tileno]; + opj_poc_t *tcp = &tcps->pocs[pino]; + + if (pos >= 0) { + for (i = pos; pos >= 0; i--) { + switch (prog[i]) { + case 'R': + if (tcp->res_t == tcp->resE) { + if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) { + return OPJ_TRUE; + } else { + return OPJ_FALSE; + } + } else { + return OPJ_TRUE; + } + break; + case 'C': + if (tcp->comp_t == tcp->compE) { + if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) { + return OPJ_TRUE; + } else { + return OPJ_FALSE; + } + } else { + return OPJ_TRUE; + } + break; + case 'L': + if (tcp->lay_t == tcp->layE) { + if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) { + return OPJ_TRUE; + } else { + return OPJ_FALSE; + } + } else { + return OPJ_TRUE; + } + break; + case 'P': + switch (tcp->prg) { + case OPJ_LRCP: /* fall through */ + case OPJ_RLCP: + if (tcp->prc_t == tcp->prcE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + return OPJ_TRUE; + } else { + return OPJ_FALSE; + } + } else { + return OPJ_TRUE; + } + break; + default: + if (tcp->tx0_t == tcp->txE) { + /*TY*/ + if (tcp->ty0_t == tcp->tyE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + return OPJ_TRUE; + } else { + return OPJ_FALSE; + } + } else { + return OPJ_TRUE; + }/*TY*/ + } else { + return OPJ_TRUE; + } + break; + }/*end case P*/ + }/*end switch*/ + }/*end for*/ + }/*end if*/ + return OPJ_FALSE; +} + + +/* +========================================================== + Packet iterator interface +========================================================== +*/ +opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no, + opj_event_mgr_t* manager) +{ + OPJ_UINT32 numcomps = p_image->numcomps; + + /* loop */ + OPJ_UINT32 pino; + OPJ_UINT32 compno, resno; + + /* to store w, h, dx and dy fro all components and resolutions */ + OPJ_UINT32 * l_tmp_data; + OPJ_UINT32 ** l_tmp_ptr; + + /* encoding prameters to set */ + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_dx_min, l_dy_min; + OPJ_UINT32 l_bound; + OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ; + OPJ_UINT32 l_data_stride; + + /* pointers */ + opj_pi_iterator_t *l_pi = 00; + opj_tcp_t *l_tcp = 00; + const opj_tccp_t *l_tccp = 00; + opj_pi_comp_t *l_current_comp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_pi_iterator_t * l_current_pi = 00; + OPJ_UINT32 * l_encoding_value_ptr = 00; + + /* preconditions in debug */ + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); + + /* initializations */ + l_tcp = &p_cp->tcps[p_tile_no]; + l_bound = l_tcp->numpocs + 1; + + l_data_stride = 4 * OPJ_J2K_MAXRLVLS; + l_tmp_data = (OPJ_UINT32*)opj_malloc( + l_data_stride * numcomps * sizeof(OPJ_UINT32)); + if + (! l_tmp_data) { + return 00; + } + l_tmp_ptr = (OPJ_UINT32**)opj_malloc( + numcomps * sizeof(OPJ_UINT32 *)); + if + (! l_tmp_ptr) { + opj_free(l_tmp_data); + return 00; + } + + /* memory allocation for pi */ + l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager); + if (!l_pi) { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + return 00; + } + + l_encoding_value_ptr = l_tmp_data; + /* update pointer array */ + for + (compno = 0; compno < numcomps; ++compno) { + l_tmp_ptr[compno] = l_encoding_value_ptr; + l_encoding_value_ptr += l_data_stride; + } + /* get encoding parameters */ + opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, + &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr); + + /* step calculations */ + l_step_p = 1; + l_step_c = l_max_prec * l_step_p; + l_step_r = numcomps * l_step_c; + l_step_l = l_max_res * l_step_r; + + /* set values for first packet iterator */ + l_current_pi = l_pi; + + /* memory allocation for include */ + /* prevent an integer overflow issue */ + /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */ + l_current_pi->include = 00; + if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) { + l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l; + l_current_pi->include = (OPJ_INT16*) opj_calloc( + l_current_pi->include_size, sizeof(OPJ_INT16)); + } + + if (!l_current_pi->include) { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + opj_pi_destroy(l_pi, l_bound); + return 00; + } + + /* special treatment for the first packet iterator */ + l_current_comp = l_current_pi->comps; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + + /*l_current_pi->dx = l_img_comp->dx;*/ + /*l_current_pi->dy = l_img_comp->dy;*/ + + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by opj_pi_create */ + for + (compno = 0; compno < numcomps; ++compno) { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for + (resno = 0; resno < l_current_comp->numresolutions; resno++) { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + ++l_current_pi; + + for (pino = 1 ; pino < l_bound ; ++pino) { + l_current_comp = l_current_pi->comps; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + /*l_current_pi->dx = l_dx_min;*/ + /*l_current_pi->dy = l_dy_min;*/ + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by opj_pi_create */ + for + (compno = 0; compno < numcomps; ++compno) { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for + (resno = 0; resno < l_current_comp->numresolutions; resno++) { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + /* special treatment*/ + l_current_pi->include = (l_current_pi - 1)->include; + l_current_pi->include_size = (l_current_pi - 1)->include_size; + ++l_current_pi; + } + opj_free(l_tmp_data); + l_tmp_data = 00; + opj_free(l_tmp_ptr); + l_tmp_ptr = 00; + if + (l_tcp->POC) { + opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res); + } else { + opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res); + } + return l_pi; +} + + +OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no) +{ + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_dx_min, l_dy_min; + + /* preconditions in debug*/ + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); + + /* get encoding parameters*/ + opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, + &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL); + + return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps * + l_max_res; +} + + +opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no, + J2K_T2_MODE p_t2_mode, + opj_event_mgr_t* manager) +{ + OPJ_UINT32 numcomps = p_image->numcomps; + + /* loop*/ + OPJ_UINT32 pino; + OPJ_UINT32 compno, resno; + + /* to store w, h, dx and dy fro all components and resolutions*/ + OPJ_UINT32 * l_tmp_data; + OPJ_UINT32 ** l_tmp_ptr; + + /* encoding prameters to set*/ + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_dx_min, l_dy_min; + OPJ_UINT32 l_bound; + OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ; + OPJ_UINT32 l_data_stride; + + /* pointers*/ + opj_pi_iterator_t *l_pi = 00; + opj_tcp_t *l_tcp = 00; + const opj_tccp_t *l_tccp = 00; + opj_pi_comp_t *l_current_comp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_pi_iterator_t * l_current_pi = 00; + OPJ_UINT32 * l_encoding_value_ptr = 00; + + /* preconditions in debug*/ + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); + + /* initializations*/ + l_tcp = &p_cp->tcps[p_tile_no]; + l_bound = l_tcp->numpocs + 1; + + l_data_stride = 4 * OPJ_J2K_MAXRLVLS; + l_tmp_data = (OPJ_UINT32*)opj_malloc( + l_data_stride * numcomps * sizeof(OPJ_UINT32)); + if (! l_tmp_data) { + return 00; + } + + l_tmp_ptr = (OPJ_UINT32**)opj_malloc( + numcomps * sizeof(OPJ_UINT32 *)); + if (! l_tmp_ptr) { + opj_free(l_tmp_data); + return 00; + } + + /* memory allocation for pi*/ + l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager); + if (!l_pi) { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + return 00; + } + + l_encoding_value_ptr = l_tmp_data; + /* update pointer array*/ + for (compno = 0; compno < numcomps; ++compno) { + l_tmp_ptr[compno] = l_encoding_value_ptr; + l_encoding_value_ptr += l_data_stride; + } + + /* get encoding parameters*/ + opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, + &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr); + + /* step calculations*/ + l_step_p = 1; + l_step_c = l_max_prec * l_step_p; + l_step_r = numcomps * l_step_c; + l_step_l = l_max_res * l_step_r; + + /* set values for first packet iterator*/ + l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on; + l_current_pi = l_pi; + + /* memory allocation for include*/ + l_current_pi->include_size = l_tcp->numlayers * l_step_l; + l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size, + sizeof(OPJ_INT16)); + if (!l_current_pi->include) { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + opj_pi_destroy(l_pi, l_bound); + return 00; + } + + /* special treatment for the first packet iterator*/ + l_current_comp = l_current_pi->comps; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + l_current_pi->dx = l_dx_min; + l_current_pi->dy = l_dy_min; + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by opj_pi_create */ + for (compno = 0; compno < numcomps; ++compno) { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + + /* resolutions have already been initialized */ + for (resno = 0; resno < l_current_comp->numresolutions; resno++) { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + ++l_current_pi; + + for (pino = 1 ; pino < l_bound ; ++pino) { + l_current_comp = l_current_pi->comps; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + l_current_pi->dx = l_dx_min; + l_current_pi->dy = l_dy_min; + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by opj_pi_create */ + for (compno = 0; compno < numcomps; ++compno) { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for (resno = 0; resno < l_current_comp->numresolutions; resno++) { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + + /* special treatment*/ + l_current_pi->include = (l_current_pi - 1)->include; + l_current_pi->include_size = (l_current_pi - 1)->include_size; + ++l_current_pi; + } + + opj_free(l_tmp_data); + l_tmp_data = 00; + opj_free(l_tmp_ptr); + l_tmp_ptr = 00; + + if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) { + opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1, + l_max_prec, l_max_res, l_dx_min, l_dy_min); + } else { + opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1, + l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min); + } + + return l_pi; +} + +void opj_pi_create_encode(opj_pi_iterator_t *pi, + opj_cp_t *cp, + OPJ_UINT32 tileno, + OPJ_UINT32 pino, + OPJ_UINT32 tpnum, + OPJ_INT32 tppos, + J2K_T2_MODE t2_mode) +{ + const OPJ_CHAR *prog; + OPJ_INT32 i; + OPJ_UINT32 incr_top = 1, resetX = 0; + opj_tcp_t *tcps = &cp->tcps[tileno]; + opj_poc_t *tcp = &tcps->pocs[pino]; + + prog = opj_j2k_convert_progression_order(tcp->prg); + + pi[pino].first = 1; + pi[pino].poc.prg = tcp->prg; + + if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) && + !OPJ_IS_IMF(cp->rsiz) && + (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) { + pi[pino].poc.resno0 = tcp->resS; + pi[pino].poc.resno1 = tcp->resE; + pi[pino].poc.compno0 = tcp->compS; + pi[pino].poc.compno1 = tcp->compE; + pi[pino].poc.layno0 = tcp->layS; + pi[pino].poc.layno1 = tcp->layE; + pi[pino].poc.precno0 = tcp->prcS; + pi[pino].poc.precno1 = tcp->prcE; + pi[pino].poc.tx0 = tcp->txS; + pi[pino].poc.ty0 = tcp->tyS; + pi[pino].poc.tx1 = tcp->txE; + pi[pino].poc.ty1 = tcp->tyE; + } else { + for (i = tppos + 1; i < 4; i++) { + switch (prog[i]) { + case 'R': + pi[pino].poc.resno0 = tcp->resS; + pi[pino].poc.resno1 = tcp->resE; + break; + case 'C': + pi[pino].poc.compno0 = tcp->compS; + pi[pino].poc.compno1 = tcp->compE; + break; + case 'L': + pi[pino].poc.layno0 = tcp->layS; + pi[pino].poc.layno1 = tcp->layE; + break; + case 'P': + switch (tcp->prg) { + case OPJ_LRCP: + case OPJ_RLCP: + pi[pino].poc.precno0 = tcp->prcS; + pi[pino].poc.precno1 = tcp->prcE; + break; + default: + pi[pino].poc.tx0 = tcp->txS; + pi[pino].poc.ty0 = tcp->tyS; + pi[pino].poc.tx1 = tcp->txE; + pi[pino].poc.ty1 = tcp->tyE; + break; + } + break; + } + } + + if (tpnum == 0) { + for (i = tppos; i >= 0; i--) { + switch (prog[i]) { + case 'C': + tcp->comp_t = tcp->compS; + pi[pino].poc.compno0 = tcp->comp_t; + pi[pino].poc.compno1 = tcp->comp_t + 1; + tcp->comp_t += 1; + break; + case 'R': + tcp->res_t = tcp->resS; + pi[pino].poc.resno0 = tcp->res_t; + pi[pino].poc.resno1 = tcp->res_t + 1; + tcp->res_t += 1; + break; + case 'L': + tcp->lay_t = tcp->layS; + pi[pino].poc.layno0 = tcp->lay_t; + pi[pino].poc.layno1 = tcp->lay_t + 1; + tcp->lay_t += 1; + break; + case 'P': + switch (tcp->prg) { + case OPJ_LRCP: + case OPJ_RLCP: + tcp->prc_t = tcp->prcS; + pi[pino].poc.precno0 = tcp->prc_t; + pi[pino].poc.precno1 = tcp->prc_t + 1; + tcp->prc_t += 1; + break; + default: + tcp->tx0_t = tcp->txS; + tcp->ty0_t = tcp->tyS; + pi[pino].poc.tx0 = tcp->tx0_t; + pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); + tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; + tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1; + break; + } + break; + } + } + incr_top = 1; + } else { + for (i = tppos; i >= 0; i--) { + switch (prog[i]) { + case 'C': + pi[pino].poc.compno0 = tcp->comp_t - 1; + pi[pino].poc.compno1 = tcp->comp_t; + break; + case 'R': + pi[pino].poc.resno0 = tcp->res_t - 1; + pi[pino].poc.resno1 = tcp->res_t; + break; + case 'L': + pi[pino].poc.layno0 = tcp->lay_t - 1; + pi[pino].poc.layno1 = tcp->lay_t; + break; + case 'P': + switch (tcp->prg) { + case OPJ_LRCP: + case OPJ_RLCP: + pi[pino].poc.precno0 = tcp->prc_t - 1; + pi[pino].poc.precno1 = tcp->prc_t; + break; + default: + pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx); + pi[pino].poc.tx1 = tcp->tx0_t ; + pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); + pi[pino].poc.ty1 = tcp->ty0_t ; + break; + } + break; + } + if (incr_top == 1) { + switch (prog[i]) { + case 'R': + if (tcp->res_t == tcp->resE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + tcp->res_t = tcp->resS; + pi[pino].poc.resno0 = tcp->res_t; + pi[pino].poc.resno1 = tcp->res_t + 1; + tcp->res_t += 1; + incr_top = 1; + } else { + incr_top = 0; + } + } else { + pi[pino].poc.resno0 = tcp->res_t; + pi[pino].poc.resno1 = tcp->res_t + 1; + tcp->res_t += 1; + incr_top = 0; + } + break; + case 'C': + if (tcp->comp_t == tcp->compE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + tcp->comp_t = tcp->compS; + pi[pino].poc.compno0 = tcp->comp_t; + pi[pino].poc.compno1 = tcp->comp_t + 1; + tcp->comp_t += 1; + incr_top = 1; + } else { + incr_top = 0; + } + } else { + pi[pino].poc.compno0 = tcp->comp_t; + pi[pino].poc.compno1 = tcp->comp_t + 1; + tcp->comp_t += 1; + incr_top = 0; + } + break; + case 'L': + if (tcp->lay_t == tcp->layE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + tcp->lay_t = tcp->layS; + pi[pino].poc.layno0 = tcp->lay_t; + pi[pino].poc.layno1 = tcp->lay_t + 1; + tcp->lay_t += 1; + incr_top = 1; + } else { + incr_top = 0; + } + } else { + pi[pino].poc.layno0 = tcp->lay_t; + pi[pino].poc.layno1 = tcp->lay_t + 1; + tcp->lay_t += 1; + incr_top = 0; + } + break; + case 'P': + switch (tcp->prg) { + case OPJ_LRCP: + case OPJ_RLCP: + if (tcp->prc_t == tcp->prcE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + tcp->prc_t = tcp->prcS; + pi[pino].poc.precno0 = tcp->prc_t; + pi[pino].poc.precno1 = tcp->prc_t + 1; + tcp->prc_t += 1; + incr_top = 1; + } else { + incr_top = 0; + } + } else { + pi[pino].poc.precno0 = tcp->prc_t; + pi[pino].poc.precno1 = tcp->prc_t + 1; + tcp->prc_t += 1; + incr_top = 0; + } + break; + default: + if (tcp->tx0_t >= tcp->txE) { + if (tcp->ty0_t >= tcp->tyE) { + if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { + tcp->ty0_t = tcp->tyS; + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); + tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1; + incr_top = 1; + resetX = 1; + } else { + incr_top = 0; + resetX = 0; + } + } else { + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); + tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1; + incr_top = 0; + resetX = 1; + } + if (resetX == 1) { + tcp->tx0_t = tcp->txS; + pi[pino].poc.tx0 = tcp->tx0_t; + pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); + tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; + } + } else { + pi[pino].poc.tx0 = tcp->tx0_t; + pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); + tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; + incr_top = 0; + } + break; + } + break; + } + } + } + } + } +} + +void opj_pi_destroy(opj_pi_iterator_t *p_pi, + OPJ_UINT32 p_nb_elements) +{ + OPJ_UINT32 compno, pino; + opj_pi_iterator_t *l_current_pi = p_pi; + if (p_pi) { + if (p_pi->include) { + opj_free(p_pi->include); + p_pi->include = 00; + } + for (pino = 0; pino < p_nb_elements; ++pino) { + if (l_current_pi->comps) { + opj_pi_comp_t *l_current_component = l_current_pi->comps; + for (compno = 0; compno < l_current_pi->numcomps; compno++) { + if (l_current_component->resolutions) { + opj_free(l_current_component->resolutions); + l_current_component->resolutions = 00; + } + + ++l_current_component; + } + opj_free(l_current_pi->comps); + l_current_pi->comps = 0; + } + ++l_current_pi; + } + opj_free(p_pi); + } +} + + + +void opj_pi_update_encoding_parameters(const opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no) +{ + /* encoding parameters to set */ + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_dx_min, l_dy_min; + + /* pointers */ + opj_tcp_t *l_tcp = 00; + + /* preconditions */ + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); + + l_tcp = &(p_cp->tcps[p_tile_no]); + + /* get encoding parameters */ + opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0, + &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res); + + if (l_tcp->POC) { + opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1, + l_max_prec, l_max_res, l_dx_min, l_dy_min); + } else { + opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1, + l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min); + } +} + +OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi) +{ + switch (pi->poc.prg) { + case OPJ_LRCP: + return opj_pi_next_lrcp(pi); + case OPJ_RLCP: + return opj_pi_next_rlcp(pi); + case OPJ_RPCL: + return opj_pi_next_rpcl(pi); + case OPJ_PCRL: + return opj_pi_next_pcrl(pi); + case OPJ_CPRL: + return opj_pi_next_cprl(pi); + case OPJ_PROG_UNKNOWN: + return OPJ_FALSE; + } + + return OPJ_FALSE; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/pi.h b/cpp/3rd_party/openjpeg/openjp2/pi.h new file mode 100644 index 0000000000..0320523b76 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/pi.h @@ -0,0 +1,207 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * 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 OPJ_PI_H +#define OPJ_PI_H +/** +@file pi.h +@brief Implementation of a packet iterator (PI) + +The functions in PI.C have for goal to realize a packet iterator that permits to get the next +packet following the progression order and change of it. The functions in PI.C are used +by some function in T2.C. +*/ + +/** @defgroup PI PI - Implementation of a packet iterator */ +/*@{*/ + +/** +FIXME DOC +*/ +typedef struct opj_pi_resolution { + OPJ_UINT32 pdx, pdy; + OPJ_UINT32 pw, ph; +} opj_pi_resolution_t; + +/** +FIXME DOC +*/ +typedef struct opj_pi_comp { + OPJ_UINT32 dx, dy; + /** number of resolution levels */ + OPJ_UINT32 numresolutions; + opj_pi_resolution_t *resolutions; +} opj_pi_comp_t; + +/** +Packet iterator +*/ +typedef struct opj_pi_iterator { + /** Enabling Tile part generation*/ + OPJ_BYTE tp_on; + /** precise if the packet has been already used (useful for progression order change) */ + OPJ_INT16 *include; + /** Number of elements in include array */ + OPJ_UINT32 include_size; + /** layer step used to localize the packet in the include vector */ + OPJ_UINT32 step_l; + /** resolution step used to localize the packet in the include vector */ + OPJ_UINT32 step_r; + /** component step used to localize the packet in the include vector */ + OPJ_UINT32 step_c; + /** precinct step used to localize the packet in the include vector */ + OPJ_UINT32 step_p; + /** component that identify the packet */ + OPJ_UINT32 compno; + /** resolution that identify the packet */ + OPJ_UINT32 resno; + /** precinct that identify the packet */ + OPJ_UINT32 precno; + /** layer that identify the packet */ + OPJ_UINT32 layno; + /** 0 if the first packet */ + OPJ_BOOL first; + /** progression order change information */ + opj_poc_t poc; + /** number of components in the image */ + OPJ_UINT32 numcomps; + /** Components*/ + opj_pi_comp_t *comps; + /** FIXME DOC*/ + OPJ_UINT32 tx0, ty0, tx1, ty1; + /** FIXME DOC*/ + OPJ_UINT32 x, y; + /** FIXME DOC*/ + OPJ_UINT32 dx, dy; + /** event manager */ + opj_event_mgr_t* manager; +} opj_pi_iterator_t; + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ +/** + * Creates a packet iterator for encoding. + * + * @param image the image being encoded. + * @param cp the coding parameters. + * @param tileno index of the tile being encoded. + * @param t2_mode the type of pass for generating the packet iterator + * @param manager Event manager + * + * @return a list of packet iterator that points to the first packet of the tile (not true). +*/ +opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *image, + opj_cp_t *cp, + OPJ_UINT32 tileno, + J2K_T2_MODE t2_mode, + opj_event_mgr_t* manager); + +/** + * Updates the encoding parameters of the codec. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param p_tile_no index of the tile being encoded. +*/ +void opj_pi_update_encoding_parameters(const opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no); + +/** +Modify the packet iterator for enabling tile part generation +@param pi Handle to the packet iterator generated in pi_initialise_encode +@param cp Coding parameters +@param tileno Number that identifies the tile for which to list the packets +@param pino FIXME DOC +@param tpnum Tile part number of the current tile +@param tppos The position of the tile part flag in the progression order +@param t2_mode FIXME DOC +*/ +void opj_pi_create_encode(opj_pi_iterator_t *pi, + opj_cp_t *cp, + OPJ_UINT32 tileno, + OPJ_UINT32 pino, + OPJ_UINT32 tpnum, + OPJ_INT32 tppos, + J2K_T2_MODE t2_mode); + +/** +Create a packet iterator for Decoder +@param image Raw image for which the packets will be listed +@param cp Coding parameters +@param tileno Number that identifies the tile for which to list the packets +@param manager Event manager +@return Returns a packet iterator that points to the first packet of the tile +@see opj_pi_destroy +*/ +opj_pi_iterator_t *opj_pi_create_decode(opj_image_t * image, + opj_cp_t * cp, + OPJ_UINT32 tileno, + opj_event_mgr_t* manager); +/** + * Destroys a packet iterator array. + * + * @param p_pi the packet iterator array to destroy. + * @param p_nb_elements the number of elements in the array. + */ +void opj_pi_destroy(opj_pi_iterator_t *p_pi, + OPJ_UINT32 p_nb_elements); + +/** +Modify the packet iterator to point to the next packet +@param pi Packet iterator to modify +@return Returns false if pi pointed to the last packet or else returns true +*/ +OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi); + +/** + * Return the number of packets in the tile. + * @param image the image being encoded. + * @param cp Coding parameters + * @param tileno Number that identifies the tile. + */ +OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no); + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_PI_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/sparse_array.c b/cpp/3rd_party/openjpeg/openjp2/sparse_array.c new file mode 100644 index 0000000000..73192924ed --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/sparse_array.c @@ -0,0 +1,346 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2017, IntoPix SA + * 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" + + +struct opj_sparse_array_int32 { + OPJ_UINT32 width; + OPJ_UINT32 height; + OPJ_UINT32 block_width; + OPJ_UINT32 block_height; + OPJ_UINT32 block_count_hor; + OPJ_UINT32 block_count_ver; + OPJ_INT32** data_blocks; +}; + +opj_sparse_array_int32_t* opj_sparse_array_int32_create(OPJ_UINT32 width, + OPJ_UINT32 height, + OPJ_UINT32 block_width, + OPJ_UINT32 block_height) +{ + opj_sparse_array_int32_t* sa; + + if (width == 0 || height == 0 || block_width == 0 || block_height == 0) { + return NULL; + } + if (block_width > ((OPJ_UINT32)~0U) / block_height / sizeof(OPJ_INT32)) { + return NULL; + } + + sa = (opj_sparse_array_int32_t*) opj_calloc(1, + sizeof(opj_sparse_array_int32_t)); + sa->width = width; + sa->height = height; + sa->block_width = block_width; + sa->block_height = block_height; + sa->block_count_hor = opj_uint_ceildiv(width, block_width); + sa->block_count_ver = opj_uint_ceildiv(height, block_height); + if (sa->block_count_hor > ((OPJ_UINT32)~0U) / sa->block_count_ver) { + opj_free(sa); + return NULL; + } + sa->data_blocks = (OPJ_INT32**) opj_calloc(sizeof(OPJ_INT32*), + sa->block_count_hor * sa->block_count_ver); + if (sa->data_blocks == NULL) { + opj_free(sa); + return NULL; + } + + return sa; +} + +void opj_sparse_array_int32_free(opj_sparse_array_int32_t* sa) +{ + if (sa) { + OPJ_UINT32 i; + for (i = 0; i < sa->block_count_hor * sa->block_count_ver; i++) { + if (sa->data_blocks[i]) { + opj_free(sa->data_blocks[i]); + } + } + opj_free(sa->data_blocks); + opj_free(sa); + } +} + +OPJ_BOOL opj_sparse_array_is_region_valid(const opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1) +{ + return !(x0 >= sa->width || x1 <= x0 || x1 > sa->width || + y0 >= sa->height || y1 <= y0 || y1 > sa->height); +} + +static OPJ_BOOL opj_sparse_array_int32_read_or_write( + const opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1, + OPJ_INT32* buf, + OPJ_UINT32 buf_col_stride, + OPJ_UINT32 buf_line_stride, + OPJ_BOOL forgiving, + OPJ_BOOL is_read_op) +{ + OPJ_UINT32 y, block_y; + OPJ_UINT32 y_incr = 0; + const OPJ_UINT32 block_width = sa->block_width; + + if (!opj_sparse_array_is_region_valid(sa, x0, y0, x1, y1)) { + return forgiving; + } + + block_y = y0 / sa->block_height; + for (y = y0; y < y1; block_y ++, y += y_incr) { + OPJ_UINT32 x, block_x; + OPJ_UINT32 x_incr = 0; + OPJ_UINT32 block_y_offset; + y_incr = (y == y0) ? sa->block_height - (y0 % sa->block_height) : + sa->block_height; + block_y_offset = sa->block_height - y_incr; + y_incr = opj_uint_min(y_incr, y1 - y); + block_x = x0 / block_width; + for (x = x0; x < x1; block_x ++, x += x_incr) { + OPJ_UINT32 j; + OPJ_UINT32 block_x_offset; + OPJ_INT32* src_block; + x_incr = (x == x0) ? block_width - (x0 % block_width) : block_width; + block_x_offset = block_width - x_incr; + x_incr = opj_uint_min(x_incr, x1 - x); + src_block = sa->data_blocks[block_y * sa->block_count_hor + block_x]; + if (is_read_op) { + if (src_block == NULL) { + if (buf_col_stride == 1) { + OPJ_INT32* dest_ptr = buf + (y - y0) * (OPJ_SIZE_T)buf_line_stride + + (x - x0) * buf_col_stride; + for (j = 0; j < y_incr; j++) { + memset(dest_ptr, 0, sizeof(OPJ_INT32) * x_incr); + dest_ptr += buf_line_stride; + } + } else { + OPJ_INT32* dest_ptr = buf + (y - y0) * (OPJ_SIZE_T)buf_line_stride + + (x - x0) * buf_col_stride; + for (j = 0; j < y_incr; j++) { + OPJ_UINT32 k; + for (k = 0; k < x_incr; k++) { + dest_ptr[k * buf_col_stride] = 0; + } + dest_ptr += buf_line_stride; + } + } + } else { + const OPJ_INT32* OPJ_RESTRICT src_ptr = src_block + block_y_offset * + (OPJ_SIZE_T)block_width + block_x_offset; + if (buf_col_stride == 1) { + OPJ_INT32* OPJ_RESTRICT dest_ptr = buf + (y - y0) * (OPJ_SIZE_T)buf_line_stride + + + (x - x0) * buf_col_stride; + if (x_incr == 4) { + /* Same code as general branch, but the compiler */ + /* can have an efficient memcpy() */ + (void)(x_incr); /* trick to silent cppcheck duplicateBranch warning */ + for (j = 0; j < y_incr; j++) { + memcpy(dest_ptr, src_ptr, sizeof(OPJ_INT32) * x_incr); + dest_ptr += buf_line_stride; + src_ptr += block_width; + } + } else { + for (j = 0; j < y_incr; j++) { + memcpy(dest_ptr, src_ptr, sizeof(OPJ_INT32) * x_incr); + dest_ptr += buf_line_stride; + src_ptr += block_width; + } + } + } else { + OPJ_INT32* OPJ_RESTRICT dest_ptr = buf + (y - y0) * (OPJ_SIZE_T)buf_line_stride + + + (x - x0) * buf_col_stride; + if (x_incr == 1) { + for (j = 0; j < y_incr; j++) { + *dest_ptr = *src_ptr; + dest_ptr += buf_line_stride; + src_ptr += block_width; + } + } else if (y_incr == 1 && buf_col_stride == 2) { + OPJ_UINT32 k; + for (k = 0; k < (x_incr & ~3U); k += 4) { + dest_ptr[k * buf_col_stride] = src_ptr[k]; + dest_ptr[(k + 1) * buf_col_stride] = src_ptr[k + 1]; + dest_ptr[(k + 2) * buf_col_stride] = src_ptr[k + 2]; + dest_ptr[(k + 3) * buf_col_stride] = src_ptr[k + 3]; + } + for (; k < x_incr; k++) { + dest_ptr[k * buf_col_stride] = src_ptr[k]; + } + } else if (x_incr >= 8 && buf_col_stride == 8) { + for (j = 0; j < y_incr; j++) { + OPJ_UINT32 k; + for (k = 0; k < (x_incr & ~3U); k += 4) { + dest_ptr[k * buf_col_stride] = src_ptr[k]; + dest_ptr[(k + 1) * buf_col_stride] = src_ptr[k + 1]; + dest_ptr[(k + 2) * buf_col_stride] = src_ptr[k + 2]; + dest_ptr[(k + 3) * buf_col_stride] = src_ptr[k + 3]; + } + for (; k < x_incr; k++) { + dest_ptr[k * buf_col_stride] = src_ptr[k]; + } + dest_ptr += buf_line_stride; + src_ptr += block_width; + } + } else { + /* General case */ + for (j = 0; j < y_incr; j++) { + OPJ_UINT32 k; + for (k = 0; k < x_incr; k++) { + dest_ptr[k * buf_col_stride] = src_ptr[k]; + } + dest_ptr += buf_line_stride; + src_ptr += block_width; + } + } + } + } + } else { + if (src_block == NULL) { + src_block = (OPJ_INT32*) opj_calloc(1, + sa->block_width * sa->block_height * sizeof(OPJ_INT32)); + if (src_block == NULL) { + return OPJ_FALSE; + } + sa->data_blocks[block_y * sa->block_count_hor + block_x] = src_block; + } + + if (buf_col_stride == 1) { + OPJ_INT32* OPJ_RESTRICT dest_ptr = src_block + block_y_offset * + (OPJ_SIZE_T)block_width + block_x_offset; + const OPJ_INT32* OPJ_RESTRICT src_ptr = buf + (y - y0) * + (OPJ_SIZE_T)buf_line_stride + (x - x0) * buf_col_stride; + if (x_incr == 4) { + /* Same code as general branch, but the compiler */ + /* can have an efficient memcpy() */ + (void)(x_incr); /* trick to silent cppcheck duplicateBranch warning */ + for (j = 0; j < y_incr; j++) { + memcpy(dest_ptr, src_ptr, sizeof(OPJ_INT32) * x_incr); + dest_ptr += block_width; + src_ptr += buf_line_stride; + } + } else { + for (j = 0; j < y_incr; j++) { + memcpy(dest_ptr, src_ptr, sizeof(OPJ_INT32) * x_incr); + dest_ptr += block_width; + src_ptr += buf_line_stride; + } + } + } else { + OPJ_INT32* OPJ_RESTRICT dest_ptr = src_block + block_y_offset * + (OPJ_SIZE_T)block_width + block_x_offset; + const OPJ_INT32* OPJ_RESTRICT src_ptr = buf + (y - y0) * + (OPJ_SIZE_T)buf_line_stride + (x - x0) * buf_col_stride; + if (x_incr == 1) { + for (j = 0; j < y_incr; j++) { + *dest_ptr = *src_ptr; + src_ptr += buf_line_stride; + dest_ptr += block_width; + } + } else if (x_incr >= 8 && buf_col_stride == 8) { + for (j = 0; j < y_incr; j++) { + OPJ_UINT32 k; + for (k = 0; k < (x_incr & ~3U); k += 4) { + dest_ptr[k] = src_ptr[k * buf_col_stride]; + dest_ptr[k + 1] = src_ptr[(k + 1) * buf_col_stride]; + dest_ptr[k + 2] = src_ptr[(k + 2) * buf_col_stride]; + dest_ptr[k + 3] = src_ptr[(k + 3) * buf_col_stride]; + } + for (; k < x_incr; k++) { + dest_ptr[k] = src_ptr[k * buf_col_stride]; + } + src_ptr += buf_line_stride; + dest_ptr += block_width; + } + } else { + /* General case */ + for (j = 0; j < y_incr; j++) { + OPJ_UINT32 k; + for (k = 0; k < x_incr; k++) { + dest_ptr[k] = src_ptr[k * buf_col_stride]; + } + src_ptr += buf_line_stride; + dest_ptr += block_width; + } + } + } + } + } + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_sparse_array_int32_read(const opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1, + OPJ_INT32* dest, + OPJ_UINT32 dest_col_stride, + OPJ_UINT32 dest_line_stride, + OPJ_BOOL forgiving) +{ + return opj_sparse_array_int32_read_or_write( + (opj_sparse_array_int32_t*)sa, x0, y0, x1, y1, + dest, + dest_col_stride, + dest_line_stride, + forgiving, + OPJ_TRUE); +} + +OPJ_BOOL opj_sparse_array_int32_write(opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1, + const OPJ_INT32* src, + OPJ_UINT32 src_col_stride, + OPJ_UINT32 src_line_stride, + OPJ_BOOL forgiving) +{ + return opj_sparse_array_int32_read_or_write(sa, x0, y0, x1, y1, + (OPJ_INT32*)src, + src_col_stride, + src_line_stride, + forgiving, + OPJ_FALSE); +} diff --git a/cpp/3rd_party/openjpeg/openjp2/sparse_array.h b/cpp/3rd_party/openjpeg/openjp2/sparse_array.h new file mode 100644 index 0000000000..fd927eaa0b --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/sparse_array.h @@ -0,0 +1,141 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2017, IntoPix SA + * 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" + +#ifndef OPJ_SPARSE_ARRAY_H +#define OPJ_SPARSE_ARRAY_H +/** +@file sparse_array.h +@brief Sparse array management + +The functions in this file manage sparse arrays. Sparse arrays are arrays with +potential big dimensions, but with very few samples actually set. Such sparse +arrays require allocating a low amount of memory, by just allocating memory +for blocks of the array that are set. The minimum memory allocation unit is a +a block. There is a trade-off to pick up an appropriate dimension for blocks. +If it is too big, and pixels set are far from each other, too much memory will +be used. If blocks are too small, the book-keeping costs of blocks will raise. +*/ + +/** @defgroup SPARSE_ARRAY SPARSE ARRAYS - Sparse arrays */ +/*@{*/ + +/** Opaque type for sparse arrays that contain int32 values */ +typedef struct opj_sparse_array_int32 opj_sparse_array_int32_t; + +/** Creates a new sparse array. + * @param width total width of the array. + * @param height total height of the array + * @param block_width width of a block. + * @param block_height height of a block. + * @return a new sparse array instance, or NULL in case of failure. + */ +opj_sparse_array_int32_t* opj_sparse_array_int32_create(OPJ_UINT32 width, + OPJ_UINT32 height, + OPJ_UINT32 block_width, + OPJ_UINT32 block_height); + +/** Frees a sparse array. + * @param sa sparse array instance. + */ +void opj_sparse_array_int32_free(opj_sparse_array_int32_t* sa); + +/** Returns whether region bounds are valid (non empty and within array bounds) + * @param sa sparse array instance. + * @param x0 left x coordinate of the region. + * @param y0 top x coordinate of the region. + * @param x1 right x coordinate (not included) of the region. Must be greater than x0. + * @param y1 bottom y coordinate (not included) of the region. Must be greater than y0. + * @return OPJ_TRUE or OPJ_FALSE. + */ +OPJ_BOOL opj_sparse_array_is_region_valid(const opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1); + +/** Read the content of a rectangular region of the sparse array into a + * user buffer. + * + * Regions not written with opj_sparse_array_int32_write() are read as 0. + * + * @param sa sparse array instance. + * @param x0 left x coordinate of the region to read in the sparse array. + * @param y0 top x coordinate of the region to read in the sparse array. + * @param x1 right x coordinate (not included) of the region to read in the sparse array. Must be greater than x0. + * @param y1 bottom y coordinate (not included) of the region to read in the sparse array. Must be greater than y0. + * @param dest user buffer to fill. Must be at least sizeof(int32) * ( (y1 - y0 - 1) * dest_line_stride + (x1 - x0 - 1) * dest_col_stride + 1) bytes large. + * @param dest_col_stride spacing (in elements, not in bytes) in x dimension between consecutive elements of the user buffer. + * @param dest_line_stride spacing (in elements, not in bytes) in y dimension between consecutive elements of the user buffer. + * @param forgiving if set to TRUE and the region is invalid, OPJ_TRUE will still be returned. + * @return OPJ_TRUE in case of success. + */ +OPJ_BOOL opj_sparse_array_int32_read(const opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1, + OPJ_INT32* dest, + OPJ_UINT32 dest_col_stride, + OPJ_UINT32 dest_line_stride, + OPJ_BOOL forgiving); + + +/** Write the content of a rectangular region into the sparse array from a + * user buffer. + * + * Blocks intersecting the region are allocated, if not already done. + * + * @param sa sparse array instance. + * @param x0 left x coordinate of the region to write into the sparse array. + * @param y0 top x coordinate of the region to write into the sparse array. + * @param x1 right x coordinate (not included) of the region to write into the sparse array. Must be greater than x0. + * @param y1 bottom y coordinate (not included) of the region to write into the sparse array. Must be greater than y0. + * @param src user buffer to fill. Must be at least sizeof(int32) * ( (y1 - y0 - 1) * src_line_stride + (x1 - x0 - 1) * src_col_stride + 1) bytes large. + * @param src_col_stride spacing (in elements, not in bytes) in x dimension between consecutive elements of the user buffer. + * @param src_line_stride spacing (in elements, not in bytes) in y dimension between consecutive elements of the user buffer. + * @param forgiving if set to TRUE and the region is invalid, OPJ_TRUE will still be returned. + * @return OPJ_TRUE in case of success. + */ +OPJ_BOOL opj_sparse_array_int32_write(opj_sparse_array_int32_t* sa, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1, + const OPJ_INT32* src, + OPJ_UINT32 src_col_stride, + OPJ_UINT32 src_line_stride, + OPJ_BOOL forgiving); + +/*@}*/ + +#endif /* OPJ_SPARSE_ARRAY_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/t1.c b/cpp/3rd_party/openjpeg/openjp2/t1.c new file mode 100644 index 0000000000..1bea54b0d5 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/t1.c @@ -0,0 +1,2551 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2007, Callum Lerwick + * Copyright (c) 2012, Carl Hetherington + * Copyright (c) 2017, IntoPIX SA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define OPJ_SKIP_POISON +#include "opj_includes.h" + +#ifdef __SSE__ +#include +#endif +#ifdef __SSE2__ +#include +#endif + +#if defined(__GNUC__) +#pragma GCC poison malloc calloc realloc free +#endif + +#include "t1_luts.h" + +/** @defgroup T1 T1 - Implementation of the tier-1 coding */ +/*@{*/ + +#define T1_FLAGS(x, y) (t1->flags[x + 1 + ((y / 4) + 1) * (t1->w+2)]) + +#define opj_t1_setcurctx(curctx, ctxno) curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)] + +/* Macros to deal with signed integer with just MSB bit set for + * negative values (smr = signed magnitude representation) */ +#define opj_smr_abs(x) (((OPJ_UINT32)(x)) & 0x7FFFFFFFU) +#define opj_smr_sign(x) (((OPJ_UINT32)(x)) >> 31) +#define opj_to_smr(x) ((x) >= 0 ? (OPJ_UINT32)(x) : ((OPJ_UINT32)(-x) | 0x80000000U)) + + +/** @name Local static functions */ +/*@{*/ + +static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f); +static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f); +static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos); +static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos); +static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci, + OPJ_UINT32 s, OPJ_UINT32 stride, + OPJ_UINT32 vsc); + + +/** +Decode significant pass +*/ + +static INLINE void opj_t1_dec_sigpass_step_raw( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 oneplushalf, + OPJ_UINT32 vsc, + OPJ_UINT32 row); +static INLINE void opj_t1_dec_sigpass_step_mqc( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 oneplushalf, + OPJ_UINT32 row, + OPJ_UINT32 flags_stride, + OPJ_UINT32 vsc); + +/** +Encode significant pass +*/ +static void opj_t1_enc_sigpass(opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 cblksty); + +/** +Decode significant pass +*/ +static void opj_t1_dec_sigpass_raw( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 cblksty); + +/** +Encode refinement pass +*/ +static void opj_t1_enc_refpass(opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_BYTE type); + +/** +Decode refinement pass +*/ +static void opj_t1_dec_refpass_raw( + opj_t1_t *t1, + OPJ_INT32 bpno); + + +/** +Decode refinement pass +*/ + +static INLINE void opj_t1_dec_refpass_step_raw( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 poshalf, + OPJ_UINT32 row); +static INLINE void opj_t1_dec_refpass_step_mqc( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 poshalf, + OPJ_UINT32 row); + + +/** +Decode clean-up pass +*/ + +static void opj_t1_dec_clnpass_step( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 oneplushalf, + OPJ_UINT32 row, + OPJ_UINT32 vsc); + +/** +Encode clean-up pass +*/ +static void opj_t1_enc_clnpass( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_UINT32 cblksty); + +static OPJ_FLOAT64 opj_t1_getwmsedec( + OPJ_INT32 nmsedec, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 numcomps, + const OPJ_FLOAT64 * mct_norms, + OPJ_UINT32 mct_numcomps); + +/** Return "cumwmsedec" that should be used to increase tile->distotile */ +static double opj_t1_encode_cblk(opj_t1_t *t1, + opj_tcd_cblk_enc_t* cblk, + OPJ_UINT32 orient, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 cblksty, + OPJ_UINT32 numcomps, + const OPJ_FLOAT64 * mct_norms, + OPJ_UINT32 mct_numcomps); + +/** +Decode 1 code-block +@param t1 T1 handle +@param cblk Code-block coding parameters +@param orient +@param roishift Region of interest shifting value +@param cblksty Code-block style +@param p_manager the event manager +@param p_manager_mutex mutex for the event manager +@param check_pterm whether PTERM correct termination should be checked +*/ +static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1, + opj_tcd_cblk_dec_t* cblk, + OPJ_UINT32 orient, + OPJ_UINT32 roishift, + OPJ_UINT32 cblksty, + opj_event_mgr_t *p_manager, + opj_mutex_t* p_manager_mutex, + OPJ_BOOL check_pterm); + +static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1, + OPJ_UINT32 w, + OPJ_UINT32 h); + +/*@}*/ + +/*@}*/ + +/* ----------------------------------------------------------------------- */ + +static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f) +{ + return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)]; +} + +static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX, + OPJ_UINT32 pfX, + OPJ_UINT32 nfX, + OPJ_UINT32 ci) +{ + /* + 0 pfX T1_CHI_THIS T1_LUT_SGN_W + 1 tfX T1_SIGMA_1 T1_LUT_SIG_N + 2 nfX T1_CHI_THIS T1_LUT_SGN_E + 3 tfX T1_SIGMA_3 T1_LUT_SIG_W + 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N + 5 tfX T1_SIGMA_5 T1_LUT_SIG_E + 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S + 7 tfX T1_SIGMA_7 T1_LUT_SIG_S + */ + + OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 | + T1_SIGMA_7); + + lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0); + lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2); + if (ci == 0U) { + lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4); + } else { + lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4); + } + lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6); + return lu; +} + +static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu) +{ + return lut_ctxno_sc[lu]; +} + +static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) +{ + OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG; + OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp; + return tmp2; +} + +static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu) +{ + return lut_spb[lu]; +} + +static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) +{ + if (bitpos > 0) { + return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)]; + } + + return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; +} + +static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) +{ + if (bitpos > 0) { + return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)]; + } + + return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; +} + +#define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride, vsc) \ +{ \ + /* east */ \ + flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \ + \ + /* mark target as significant */ \ + flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \ + \ + /* west */ \ + flagsp[1] |= T1_SIGMA_3 << (3U * ci); \ + \ + /* north-west, north, north-east */ \ + if (ci == 0U && !(vsc)) { \ + opj_flag_t* north = flagsp - (stride); \ + *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \ + north[-1] |= T1_SIGMA_17; \ + north[1] |= T1_SIGMA_15; \ + } \ + \ + /* south-west, south, south-east */ \ + if (ci == 3U) { \ + opj_flag_t* south = flagsp + (stride); \ + *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \ + south[-1] |= T1_SIGMA_2; \ + south[1] |= T1_SIGMA_0; \ + } \ +} + + +static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci, + OPJ_UINT32 s, OPJ_UINT32 stride, + OPJ_UINT32 vsc) +{ + opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride, vsc); +} + +/** +Encode significant pass +*/ +#define opj_t1_enc_sigpass_step_macro(mqc, curctx, a, c, ct, flagspIn, datapIn, bpno, one, nmsedec, type, ciIn, vscIn) \ +{ \ + OPJ_UINT32 v; \ + const OPJ_UINT32 ci = (ciIn); \ + const OPJ_UINT32 vsc = (vscIn); \ + const OPJ_INT32* l_datap = (datapIn); \ + opj_flag_t* flagsp = (flagspIn); \ + OPJ_UINT32 const flags = *flagsp; \ + if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \ + (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \ + OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \ + v = (opj_smr_abs(*l_datap) & (OPJ_UINT32)one) ? 1 : 0; \ +/* #ifdef DEBUG_ENC_SIG */ \ +/* fprintf(stderr, " ctxt1=%d\n", ctxt1); */ \ +/* #endif */ \ + opj_t1_setcurctx(curctx, ctxt1); \ + if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \ + opj_mqc_bypass_enc_macro(mqc, c, ct, v); \ + } else { \ + opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \ + } \ + if (v) { \ + OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \ + *flagsp, \ + flagsp[-1], flagsp[1], \ + ci); \ + OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \ + v = opj_smr_sign(*l_datap); \ + *nmsedec += opj_t1_getnmsedec_sig(opj_smr_abs(*l_datap), \ + (OPJ_UINT32)bpno); \ +/* #ifdef DEBUG_ENC_SIG */ \ +/* fprintf(stderr, " ctxt2=%d\n", ctxt2); */ \ +/* #endif */ \ + opj_t1_setcurctx(curctx, ctxt2); \ + if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \ + opj_mqc_bypass_enc_macro(mqc, c, ct, v); \ + } else { \ + OPJ_UINT32 spb = opj_t1_getspb(lu); \ +/* #ifdef DEBUG_ENC_SIG */ \ +/* fprintf(stderr, " spb=%d\n", spb); */ \ +/* #endif */ \ + opj_mqc_encode_macro(mqc, curctx, a, c, ct, v ^ spb); \ + } \ + opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc); \ + } \ + *flagsp |= T1_PI_THIS << (ci * 3U); \ + } \ +} + +static INLINE void opj_t1_dec_sigpass_step_raw( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 oneplushalf, + OPJ_UINT32 vsc, + OPJ_UINT32 ci) +{ + OPJ_UINT32 v; + opj_mqc_t *mqc = &(t1->mqc); /* RAW component */ + + OPJ_UINT32 const flags = *flagsp; + + if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && + (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { + if (opj_mqc_raw_decode(mqc)) { + v = opj_mqc_raw_decode(mqc); + *datap = v ? -oneplushalf : oneplushalf; + opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc); + } + *flagsp |= T1_PI_THIS << (ci * 3U); + } +} + +#define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \ + data_stride, ci, mqc, curctx, \ + v, a, c, ct, oneplushalf, vsc) \ +{ \ + if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \ + (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \ + OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \ + opj_t1_setcurctx(curctx, ctxt1); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + if (v) { \ + OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \ + flags, \ + flagsp[-1], flagsp[1], \ + ci); \ + OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \ + OPJ_UINT32 spb = opj_t1_getspb(lu); \ + opj_t1_setcurctx(curctx, ctxt2); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + v = v ^ spb; \ + data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \ + opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \ + } \ + flags |= T1_PI_THIS << (ci * 3U); \ + } \ +} + +static INLINE void opj_t1_dec_sigpass_step_mqc( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 oneplushalf, + OPJ_UINT32 ci, + OPJ_UINT32 flags_stride, + OPJ_UINT32 vsc) +{ + OPJ_UINT32 v; + + opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ + opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap, + 0, ci, mqc, mqc->curctx, + v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc); +} + +static void opj_t1_enc_sigpass(opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 cblksty + ) +{ + OPJ_UINT32 i, k; + OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS); + opj_flag_t* f = &T1_FLAGS(0, 0); + OPJ_UINT32 const extra = 2; + opj_mqc_t* mqc = &(t1->mqc); + DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); + const OPJ_INT32* datap = t1->data; + + *nmsedec = 0; +#ifdef DEBUG_ENC_SIG + fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno); +#endif + for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) { + const OPJ_UINT32 w = t1->w; +#ifdef DEBUG_ENC_SIG + fprintf(stderr, " k=%d\n", k); +#endif + for (i = 0; i < w; ++i, ++f, datap += 4) { +#ifdef DEBUG_ENC_SIG + fprintf(stderr, " i=%d\n", i); +#endif + if (*f == 0U) { + /* Nothing to do for any of the 4 data points */ + continue; + } + opj_t1_enc_sigpass_step_macro( + mqc, curctx, a, c, ct, + f, + &datap[0], + bpno, + one, + nmsedec, + type, + 0, cblksty & J2K_CCP_CBLKSTY_VSC); + opj_t1_enc_sigpass_step_macro( + mqc, curctx, a, c, ct, + f, + &datap[1], + bpno, + one, + nmsedec, + type, + 1, 0); + opj_t1_enc_sigpass_step_macro( + mqc, curctx, a, c, ct, + f, + &datap[2], + bpno, + one, + nmsedec, + type, + 2, 0); + opj_t1_enc_sigpass_step_macro( + mqc, curctx, a, c, ct, + f, + &datap[3], + bpno, + one, + nmsedec, + type, + 3, 0); + } + } + + if (k < t1->h) { + OPJ_UINT32 j; +#ifdef DEBUG_ENC_SIG + fprintf(stderr, " k=%d\n", k); +#endif + for (i = 0; i < t1->w; ++i, ++f) { +#ifdef DEBUG_ENC_SIG + fprintf(stderr, " i=%d\n", i); +#endif + if (*f == 0U) { + /* Nothing to do for any of the 4 data points */ + datap += (t1->h - k); + continue; + } + for (j = k; j < t1->h; ++j, ++datap) { + opj_t1_enc_sigpass_step_macro( + mqc, curctx, a, c, ct, + f, + &datap[0], + bpno, + one, + nmsedec, + type, + j - k, + (j == k && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0)); + } + } + } + + UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); +} + +static void opj_t1_dec_sigpass_raw( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 cblksty) +{ + OPJ_INT32 one, half, oneplushalf; + OPJ_UINT32 i, j, k; + OPJ_INT32 *data = t1->data; + opj_flag_t *flagsp = &T1_FLAGS(0, 0); + const OPJ_UINT32 l_w = t1->w; + one = 1 << bpno; + half = one >> 1; + oneplushalf = one | half; + + for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) { + for (i = 0; i < l_w; ++i, ++flagsp, ++data) { + opj_flag_t flags = *flagsp; + if (flags != 0) { + opj_t1_dec_sigpass_step_raw( + t1, + flagsp, + data, + oneplushalf, + cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */ + 0U); + opj_t1_dec_sigpass_step_raw( + t1, + flagsp, + data + l_w, + oneplushalf, + OPJ_FALSE, /* vsc */ + 1U); + opj_t1_dec_sigpass_step_raw( + t1, + flagsp, + data + 2 * l_w, + oneplushalf, + OPJ_FALSE, /* vsc */ + 2U); + opj_t1_dec_sigpass_step_raw( + t1, + flagsp, + data + 3 * l_w, + oneplushalf, + OPJ_FALSE, /* vsc */ + 3U); + } + } + } + if (k < t1->h) { + for (i = 0; i < l_w; ++i, ++flagsp, ++data) { + for (j = 0; j < t1->h - k; ++j) { + opj_t1_dec_sigpass_step_raw( + t1, + flagsp, + data + j * l_w, + oneplushalf, + cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */ + j); + } + } + } +} + +#define opj_t1_dec_sigpass_mqc_internal(t1, bpno, vsc, w, h, flags_stride) \ +{ \ + OPJ_INT32 one, half, oneplushalf; \ + OPJ_UINT32 i, j, k; \ + register OPJ_INT32 *data = t1->data; \ + register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \ + const OPJ_UINT32 l_w = w; \ + opj_mqc_t* mqc = &(t1->mqc); \ + DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \ + register OPJ_UINT32 v; \ + one = 1 << bpno; \ + half = one >> 1; \ + oneplushalf = one | half; \ + for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \ + for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \ + opj_flag_t flags = *flagsp; \ + if( flags != 0 ) { \ + opj_t1_dec_sigpass_step_mqc_macro( \ + flags, flagsp, flags_stride, data, \ + l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf, vsc); \ + opj_t1_dec_sigpass_step_mqc_macro( \ + flags, flagsp, flags_stride, data, \ + l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \ + opj_t1_dec_sigpass_step_mqc_macro( \ + flags, flagsp, flags_stride, data, \ + l_w, 2, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \ + opj_t1_dec_sigpass_step_mqc_macro( \ + flags, flagsp, flags_stride, data, \ + l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \ + *flagsp = flags; \ + } \ + } \ + } \ + UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \ + if( k < h ) { \ + for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \ + for (j = 0; j < h - k; ++j) { \ + opj_t1_dec_sigpass_step_mqc(t1, flagsp, \ + data + j * l_w, oneplushalf, j, flags_stride, vsc); \ + } \ + } \ + } \ +} + +static void opj_t1_dec_sigpass_mqc_64x64_novsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, 64, 64, 66); +} + +static void opj_t1_dec_sigpass_mqc_64x64_vsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, 64, 64, 66); +} + +static void opj_t1_dec_sigpass_mqc_generic_novsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h, + t1->w + 2U); +} + +static void opj_t1_dec_sigpass_mqc_generic_vsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h, + t1->w + 2U); +} + +static void opj_t1_dec_sigpass_mqc( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 cblksty) +{ + if (t1->w == 64 && t1->h == 64) { + if (cblksty & J2K_CCP_CBLKSTY_VSC) { + opj_t1_dec_sigpass_mqc_64x64_vsc(t1, bpno); + } else { + opj_t1_dec_sigpass_mqc_64x64_novsc(t1, bpno); + } + } else { + if (cblksty & J2K_CCP_CBLKSTY_VSC) { + opj_t1_dec_sigpass_mqc_generic_vsc(t1, bpno); + } else { + opj_t1_dec_sigpass_mqc_generic_novsc(t1, bpno); + } + } +} + +/** +Encode refinement pass step +*/ +#define opj_t1_enc_refpass_step_macro(mqc, curctx, a, c, ct, flags, flagsUpdated, datap, bpno, one, nmsedec, type, ci) \ +{\ + OPJ_UINT32 v; \ + if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << ((ci) * 3U))) == (T1_SIGMA_THIS << ((ci) * 3U))) { \ + const OPJ_UINT32 shift_flags = (flags >> ((ci) * 3U)); \ + OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags); \ + OPJ_UINT32 abs_data = opj_smr_abs(*datap); \ + *nmsedec += opj_t1_getnmsedec_ref(abs_data, \ + (OPJ_UINT32)bpno); \ + v = ((OPJ_INT32)abs_data & one) ? 1 : 0; \ +/* #ifdef DEBUG_ENC_REF */ \ +/* fprintf(stderr, " ctxt=%d\n", ctxt); */ \ +/* #endif */ \ + opj_t1_setcurctx(curctx, ctxt); \ + if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ \ + opj_mqc_bypass_enc_macro(mqc, c, ct, v); \ + } else { \ + opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \ + } \ + flagsUpdated |= T1_MU_THIS << ((ci) * 3U); \ + } \ +} + + +static INLINE void opj_t1_dec_refpass_step_raw( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 poshalf, + OPJ_UINT32 ci) +{ + OPJ_UINT32 v; + + opj_mqc_t *mqc = &(t1->mqc); /* RAW component */ + + if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == + (T1_SIGMA_THIS << (ci * 3U))) { + v = opj_mqc_raw_decode(mqc); + *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf; + *flagsp |= T1_MU_THIS << (ci * 3U); + } +} + +#define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \ + mqc, curctx, v, a, c, ct, poshalf) \ +{ \ + if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \ + (T1_SIGMA_THIS << (ci * 3U))) { \ + OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \ + opj_t1_setcurctx(curctx, ctxt); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \ + flags |= T1_MU_THIS << (ci * 3U); \ + } \ +} + +static INLINE void opj_t1_dec_refpass_step_mqc( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 poshalf, + OPJ_UINT32 ci) +{ + OPJ_UINT32 v; + + opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ + opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci, + mqc, mqc->curctx, v, mqc->a, mqc->c, + mqc->ct, poshalf); +} + +static void opj_t1_enc_refpass( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_BYTE type) +{ + OPJ_UINT32 i, k; + const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS); + opj_flag_t* f = &T1_FLAGS(0, 0); + const OPJ_UINT32 extra = 2U; + opj_mqc_t* mqc = &(t1->mqc); + DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); + const OPJ_INT32* datap = t1->data; + + *nmsedec = 0; +#ifdef DEBUG_ENC_REF + fprintf(stderr, "enc_refpass: bpno=%d\n", bpno); +#endif + for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) { +#ifdef DEBUG_ENC_REF + fprintf(stderr, " k=%d\n", k); +#endif + for (i = 0; i < t1->w; ++i, f++, datap += 4) { + const OPJ_UINT32 flags = *f; + OPJ_UINT32 flagsUpdated = flags; +#ifdef DEBUG_ENC_REF + fprintf(stderr, " i=%d\n", i); +#endif + if ((flags & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) { + /* none significant */ + continue; + } + if ((flags & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) == + (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) { + /* all processed by sigpass */ + continue; + } + + opj_t1_enc_refpass_step_macro( + mqc, curctx, a, c, ct, + flags, flagsUpdated, + &datap[0], + bpno, + one, + nmsedec, + type, + 0); + opj_t1_enc_refpass_step_macro( + mqc, curctx, a, c, ct, + flags, flagsUpdated, + &datap[1], + bpno, + one, + nmsedec, + type, + 1); + opj_t1_enc_refpass_step_macro( + mqc, curctx, a, c, ct, + flags, flagsUpdated, + &datap[2], + bpno, + one, + nmsedec, + type, + 2); + opj_t1_enc_refpass_step_macro( + mqc, curctx, a, c, ct, + flags, flagsUpdated, + &datap[3], + bpno, + one, + nmsedec, + type, + 3); + *f = flagsUpdated; + } + } + + if (k < t1->h) { + OPJ_UINT32 j; + const OPJ_UINT32 remaining_lines = t1->h - k; +#ifdef DEBUG_ENC_REF + fprintf(stderr, " k=%d\n", k); +#endif + for (i = 0; i < t1->w; ++i, ++f) { +#ifdef DEBUG_ENC_REF + fprintf(stderr, " i=%d\n", i); +#endif + if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) { + /* none significant */ + datap += remaining_lines; + continue; + } + for (j = 0; j < remaining_lines; ++j, datap ++) { + opj_t1_enc_refpass_step_macro( + mqc, curctx, a, c, ct, + *f, *f, + &datap[0], + bpno, + one, + nmsedec, + type, + j); + } + } + } + + UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); +} + + +static void opj_t1_dec_refpass_raw( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + OPJ_INT32 one, poshalf; + OPJ_UINT32 i, j, k; + OPJ_INT32 *data = t1->data; + opj_flag_t *flagsp = &T1_FLAGS(0, 0); + const OPJ_UINT32 l_w = t1->w; + one = 1 << bpno; + poshalf = one >> 1; + for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) { + for (i = 0; i < l_w; ++i, ++flagsp, ++data) { + opj_flag_t flags = *flagsp; + if (flags != 0) { + opj_t1_dec_refpass_step_raw( + t1, + flagsp, + data, + poshalf, + 0U); + opj_t1_dec_refpass_step_raw( + t1, + flagsp, + data + l_w, + poshalf, + 1U); + opj_t1_dec_refpass_step_raw( + t1, + flagsp, + data + 2 * l_w, + poshalf, + 2U); + opj_t1_dec_refpass_step_raw( + t1, + flagsp, + data + 3 * l_w, + poshalf, + 3U); + } + } + } + if (k < t1->h) { + for (i = 0; i < l_w; ++i, ++flagsp, ++data) { + for (j = 0; j < t1->h - k; ++j) { + opj_t1_dec_refpass_step_raw( + t1, + flagsp, + data + j * l_w, + poshalf, + j); + } + } + } +} + +#define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \ +{ \ + OPJ_INT32 one, poshalf; \ + OPJ_UINT32 i, j, k; \ + register OPJ_INT32 *data = t1->data; \ + register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \ + const OPJ_UINT32 l_w = w; \ + opj_mqc_t* mqc = &(t1->mqc); \ + DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \ + register OPJ_UINT32 v; \ + one = 1 << bpno; \ + poshalf = one >> 1; \ + for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \ + for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \ + opj_flag_t flags = *flagsp; \ + if( flags != 0 ) { \ + opj_t1_dec_refpass_step_mqc_macro( \ + flags, data, l_w, 0, \ + mqc, curctx, v, a, c, ct, poshalf); \ + opj_t1_dec_refpass_step_mqc_macro( \ + flags, data, l_w, 1, \ + mqc, curctx, v, a, c, ct, poshalf); \ + opj_t1_dec_refpass_step_mqc_macro( \ + flags, data, l_w, 2, \ + mqc, curctx, v, a, c, ct, poshalf); \ + opj_t1_dec_refpass_step_mqc_macro( \ + flags, data, l_w, 3, \ + mqc, curctx, v, a, c, ct, poshalf); \ + *flagsp = flags; \ + } \ + } \ + } \ + UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \ + if( k < h ) { \ + for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \ + for (j = 0; j < h - k; ++j) { \ + opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \ + } \ + } \ + } \ +} + +static void opj_t1_dec_refpass_mqc_64x64( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66); +} + +static void opj_t1_dec_refpass_mqc_generic( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U); +} + +static void opj_t1_dec_refpass_mqc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + if (t1->w == 64 && t1->h == 64) { + opj_t1_dec_refpass_mqc_64x64(t1, bpno); + } else { + opj_t1_dec_refpass_mqc_generic(t1, bpno); + } +} + +/** +Encode clean-up pass step +*/ +#define opj_t1_enc_clnpass_step_macro(mqc, curctx, a, c, ct, flagspIn, datapIn, bpno, one, nmsedec, agg, runlen, lim, cblksty) \ +{ \ + OPJ_UINT32 v; \ + OPJ_UINT32 ci; \ + opj_flag_t* const flagsp = (flagspIn); \ + const OPJ_INT32* l_datap = (datapIn); \ + const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 | \ + T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \ + \ + if ((*flagsp & check) == check) { \ + if (runlen == 0) { \ + *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \ + } else if (runlen == 1) { \ + *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3); \ + } else if (runlen == 2) { \ + *flagsp &= ~(T1_PI_2 | T1_PI_3); \ + } else if (runlen == 3) { \ + *flagsp &= ~(T1_PI_3); \ + } \ + } \ + else \ + for (ci = runlen; ci < lim; ++ci) { \ + OPJ_BOOL goto_PARTIAL = OPJ_FALSE; \ + if ((agg != 0) && (ci == runlen)) { \ + goto_PARTIAL = OPJ_TRUE; \ + } \ + else if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) { \ + OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U)); \ +/* #ifdef DEBUG_ENC_CLN */ \ +/* printf(" ctxt1=%d\n", ctxt1); */ \ +/* #endif */ \ + opj_t1_setcurctx(curctx, ctxt1); \ + v = (opj_smr_abs(*l_datap) & (OPJ_UINT32)one) ? 1 : 0; \ + opj_mqc_encode_macro(mqc, curctx, a, c, ct, v); \ + if (v) { \ + goto_PARTIAL = OPJ_TRUE; \ + } \ + } \ + if( goto_PARTIAL ) { \ + OPJ_UINT32 vsc; \ + OPJ_UINT32 ctxt2, spb; \ + OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \ + *flagsp, \ + flagsp[-1], flagsp[1], \ + ci); \ + *nmsedec += opj_t1_getnmsedec_sig(opj_smr_abs(*l_datap), \ + (OPJ_UINT32)bpno); \ + ctxt2 = opj_t1_getctxno_sc(lu); \ +/* #ifdef DEBUG_ENC_CLN */ \ +/* printf(" ctxt2=%d\n", ctxt2); */ \ +/* #endif */ \ + opj_t1_setcurctx(curctx, ctxt2); \ + \ + v = opj_smr_sign(*l_datap); \ + spb = opj_t1_getspb(lu); \ +/* #ifdef DEBUG_ENC_CLN */ \ +/* printf(" spb=%d\n", spb); */\ +/* #endif */ \ + opj_mqc_encode_macro(mqc, curctx, a, c, ct, v ^ spb); \ + vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == 0)) ? 1 : 0; \ + opj_t1_update_flags(flagsp, ci, v, t1->w + 2U, vsc); \ + } \ + *flagsp &= ~(T1_PI_THIS << (3U * ci)); \ + l_datap ++; \ + } \ +} + +#define opj_t1_dec_clnpass_step_macro(check_flags, partial, \ + flags, flagsp, flags_stride, data, \ + data_stride, ci, mqc, curctx, \ + v, a, c, ct, oneplushalf, vsc) \ +{ \ + if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\ + do { \ + if( !partial ) { \ + OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \ + opj_t1_setcurctx(curctx, ctxt1); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + if( !v ) \ + break; \ + } \ + { \ + OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \ + flags, flagsp[-1], flagsp[1], \ + ci); \ + opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + v = v ^ opj_t1_getspb(lu); \ + data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \ + opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \ + } \ + } while(0); \ + } \ +} + +static void opj_t1_dec_clnpass_step( + opj_t1_t *t1, + opj_flag_t *flagsp, + OPJ_INT32 *datap, + OPJ_INT32 oneplushalf, + OPJ_UINT32 ci, + OPJ_UINT32 vsc) +{ + OPJ_UINT32 v; + + opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ + opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, + *flagsp, flagsp, t1->w + 2U, datap, + 0, ci, mqc, mqc->curctx, + v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc); +} + +static void opj_t1_enc_clnpass( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_UINT32 cblksty) +{ + OPJ_UINT32 i, k; + const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS); + opj_mqc_t* mqc = &(t1->mqc); + DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); + const OPJ_INT32* datap = t1->data; + opj_flag_t *f = &T1_FLAGS(0, 0); + const OPJ_UINT32 extra = 2U; + + *nmsedec = 0; +#ifdef DEBUG_ENC_CLN + printf("enc_clnpass: bpno=%d\n", bpno); +#endif + for (k = 0; k < (t1->h & ~3U); k += 4, f += extra) { +#ifdef DEBUG_ENC_CLN + printf(" k=%d\n", k); +#endif + for (i = 0; i < t1->w; ++i, f++) { + OPJ_UINT32 agg, runlen; +#ifdef DEBUG_ENC_CLN + printf(" i=%d\n", i); +#endif + agg = !*f; +#ifdef DEBUG_ENC_CLN + printf(" agg=%d\n", agg); +#endif + if (agg) { + for (runlen = 0; runlen < 4; ++runlen, ++datap) { + if (opj_smr_abs(*datap) & (OPJ_UINT32)one) { + break; + } + } + opj_t1_setcurctx(curctx, T1_CTXNO_AGG); + opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen != 4); + if (runlen == 4) { + continue; + } + opj_t1_setcurctx(curctx, T1_CTXNO_UNI); + opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen >> 1); + opj_mqc_encode_macro(mqc, curctx, a, c, ct, runlen & 1); + } else { + runlen = 0; + } + opj_t1_enc_clnpass_step_macro( + mqc, curctx, a, c, ct, + f, + datap, + bpno, + one, + nmsedec, + agg, + runlen, + 4U, + cblksty); + datap += 4 - runlen; + } + } + if (k < t1->h) { + const OPJ_UINT32 agg = 0; + const OPJ_UINT32 runlen = 0; +#ifdef DEBUG_ENC_CLN + printf(" k=%d\n", k); +#endif + for (i = 0; i < t1->w; ++i, f++) { +#ifdef DEBUG_ENC_CLN + printf(" i=%d\n", i); + printf(" agg=%d\n", agg); +#endif + opj_t1_enc_clnpass_step_macro( + mqc, curctx, a, c, ct, + f, + datap, + bpno, + one, + nmsedec, + agg, + runlen, + t1->h - k, + cblksty); + datap += t1->h - k; + } + } + + UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); +} + +#define opj_t1_dec_clnpass_internal(t1, bpno, vsc, w, h, flags_stride) \ +{ \ + OPJ_INT32 one, half, oneplushalf; \ + OPJ_UINT32 runlen; \ + OPJ_UINT32 i, j, k; \ + const OPJ_UINT32 l_w = w; \ + opj_mqc_t* mqc = &(t1->mqc); \ + register OPJ_INT32 *data = t1->data; \ + register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \ + DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \ + register OPJ_UINT32 v; \ + one = 1 << bpno; \ + half = one >> 1; \ + oneplushalf = one | half; \ + for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \ + for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \ + opj_flag_t flags = *flagsp; \ + if (flags == 0) { \ + OPJ_UINT32 partial = OPJ_TRUE; \ + opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + if (!v) { \ + continue; \ + } \ + opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \ + opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \ + opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \ + runlen = (runlen << 1) | v; \ + switch(runlen) { \ + case 0: \ + opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\ + flags, flagsp, flags_stride, data, \ + l_w, 0, mqc, curctx, \ + v, a, c, ct, oneplushalf, vsc); \ + partial = OPJ_FALSE; \ + /* FALLTHRU */ \ + case 1: \ + opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\ + flags, flagsp, flags_stride, data, \ + l_w, 1, mqc, curctx, \ + v, a, c, ct, oneplushalf, OPJ_FALSE); \ + partial = OPJ_FALSE; \ + /* FALLTHRU */ \ + case 2: \ + opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\ + flags, flagsp, flags_stride, data, \ + l_w, 2, mqc, curctx, \ + v, a, c, ct, oneplushalf, OPJ_FALSE); \ + partial = OPJ_FALSE; \ + /* FALLTHRU */ \ + case 3: \ + opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\ + flags, flagsp, flags_stride, data, \ + l_w, 3, mqc, curctx, \ + v, a, c, ct, oneplushalf, OPJ_FALSE); \ + break; \ + } \ + } else { \ + opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \ + flags, flagsp, flags_stride, data, \ + l_w, 0, mqc, curctx, \ + v, a, c, ct, oneplushalf, vsc); \ + opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \ + flags, flagsp, flags_stride, data, \ + l_w, 1, mqc, curctx, \ + v, a, c, ct, oneplushalf, OPJ_FALSE); \ + opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \ + flags, flagsp, flags_stride, data, \ + l_w, 2, mqc, curctx, \ + v, a, c, ct, oneplushalf, OPJ_FALSE); \ + opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \ + flags, flagsp, flags_stride, data, \ + l_w, 3, mqc, curctx, \ + v, a, c, ct, oneplushalf, OPJ_FALSE); \ + } \ + *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \ + } \ + } \ + UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct); \ + if( k < h ) { \ + for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \ + for (j = 0; j < h - k; ++j) { \ + opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j, vsc); \ + } \ + *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \ + } \ + } \ +} + +static void opj_t1_dec_clnpass_check_segsym(opj_t1_t *t1, OPJ_INT32 cblksty) +{ + if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) { + opj_mqc_t* mqc = &(t1->mqc); + OPJ_UINT32 v, v2; + opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); + opj_mqc_decode(v, mqc); + opj_mqc_decode(v2, mqc); + v = (v << 1) | v2; + opj_mqc_decode(v2, mqc); + v = (v << 1) | v2; + opj_mqc_decode(v2, mqc); + v = (v << 1) | v2; + /* + if (v!=0xa) { + opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); + } + */ + } +} + +static void opj_t1_dec_clnpass_64x64_novsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, 64, 64, 66); +} + +static void opj_t1_dec_clnpass_64x64_vsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, 64, 64, 66); +} + +static void opj_t1_dec_clnpass_generic_novsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h, + t1->w + 2U); +} + +static void opj_t1_dec_clnpass_generic_vsc( + opj_t1_t *t1, + OPJ_INT32 bpno) +{ + opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h, + t1->w + 2U); +} + +static void opj_t1_dec_clnpass( + opj_t1_t *t1, + OPJ_INT32 bpno, + OPJ_INT32 cblksty) +{ + if (t1->w == 64 && t1->h == 64) { + if (cblksty & J2K_CCP_CBLKSTY_VSC) { + opj_t1_dec_clnpass_64x64_vsc(t1, bpno); + } else { + opj_t1_dec_clnpass_64x64_novsc(t1, bpno); + } + } else { + if (cblksty & J2K_CCP_CBLKSTY_VSC) { + opj_t1_dec_clnpass_generic_vsc(t1, bpno); + } else { + opj_t1_dec_clnpass_generic_novsc(t1, bpno); + } + } + opj_t1_dec_clnpass_check_segsym(t1, cblksty); +} + + +/** mod fixed_quality */ +static OPJ_FLOAT64 opj_t1_getwmsedec( + OPJ_INT32 nmsedec, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 numcomps, + const OPJ_FLOAT64 * mct_norms, + OPJ_UINT32 mct_numcomps) +{ + OPJ_FLOAT64 w1 = 1, w2, wmsedec; + OPJ_ARG_NOT_USED(numcomps); + + if (mct_norms && (compno < mct_numcomps)) { + w1 = mct_norms[compno]; + } + + if (qmfbid == 1) { + w2 = opj_dwt_getnorm(level, orient); + } else { /* if (qmfbid == 0) */ + const OPJ_INT32 log2_gain = (orient == 0) ? 0 : + (orient == 3) ? 2 : 1; + w2 = opj_dwt_getnorm_real(level, orient); + /* Not sure this is right. But preserves past behaviour */ + stepsize /= (1 << log2_gain); + } + + wmsedec = w1 * w2 * stepsize * (1 << bpno); + wmsedec *= wmsedec * nmsedec / 8192.0; + + return wmsedec; +} + +static OPJ_BOOL opj_t1_allocate_buffers( + opj_t1_t *t1, + OPJ_UINT32 w, + OPJ_UINT32 h) +{ + OPJ_UINT32 flagssize; + OPJ_UINT32 flags_stride; + + /* No risk of overflow. Prior checks ensure those assert are met */ + /* They are per the specification */ + assert(w <= 1024); + assert(h <= 1024); + assert(w * h <= 4096); + + /* encoder uses tile buffer, so no need to allocate */ + { + OPJ_UINT32 datasize = w * h; + + if (datasize > t1->datasize) { + opj_aligned_free(t1->data); + t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32)); + if (!t1->data) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + t1->datasize = datasize; + } + /* memset first arg is declared to never be null by gcc */ + if (t1->data != NULL) { + memset(t1->data, 0, datasize * sizeof(OPJ_INT32)); + } + } + + flags_stride = w + 2U; /* can't be 0U */ + + flagssize = (h + 3U) / 4U + 2U; + + flagssize *= flags_stride; + { + opj_flag_t* p; + OPJ_UINT32 x; + OPJ_UINT32 flags_height = (h + 3U) / 4U; + + if (flagssize > t1->flagssize) { + + opj_aligned_free(t1->flags); + t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof( + opj_flag_t)); + if (!t1->flags) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + } + t1->flagssize = flagssize; + + memset(t1->flags, 0, flagssize * sizeof(opj_flag_t)); + + p = &t1->flags[0]; + for (x = 0; x < flags_stride; ++x) { + /* magic value to hopefully stop any passes being interested in this entry */ + *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); + } + + p = &t1->flags[((flags_height + 1) * flags_stride)]; + for (x = 0; x < flags_stride; ++x) { + /* magic value to hopefully stop any passes being interested in this entry */ + *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); + } + + if (h % 4) { + OPJ_UINT32 v = 0; + p = &t1->flags[((flags_height) * flags_stride)]; + if (h % 4 == 1) { + v |= T1_PI_1 | T1_PI_2 | T1_PI_3; + } else if (h % 4 == 2) { + v |= T1_PI_2 | T1_PI_3; + } else if (h % 4 == 3) { + v |= T1_PI_3; + } + for (x = 0; x < flags_stride; ++x) { + *p++ = v; + } + } + } + + t1->w = w; + t1->h = h; + + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ + +/* ----------------------------------------------------------------------- */ +/** + * Creates a new Tier 1 handle + * and initializes the look-up tables of the Tier-1 coder/decoder + * @return a new T1 handle if successful, returns NULL otherwise +*/ +opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder) +{ + opj_t1_t *l_t1 = 00; + + l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t)); + if (!l_t1) { + return 00; + } + + l_t1->encoder = isEncoder; + + return l_t1; +} + + +/** + * Destroys a previously created T1 handle + * + * @param p_t1 Tier 1 handle to destroy +*/ +void opj_t1_destroy(opj_t1_t *p_t1) +{ + if (! p_t1) { + return; + } + + if (p_t1->data) { + opj_aligned_free(p_t1->data); + p_t1->data = 00; + } + + if (p_t1->flags) { + opj_aligned_free(p_t1->flags); + p_t1->flags = 00; + } + + opj_free(p_t1->cblkdatabuffer); + + opj_free(p_t1); +} + +typedef struct { + OPJ_BOOL whole_tile_decoding; + OPJ_UINT32 resno; + opj_tcd_cblk_dec_t* cblk; + opj_tcd_band_t* band; + opj_tcd_tilecomp_t* tilec; + opj_tccp_t* tccp; + OPJ_BOOL mustuse_cblkdatabuffer; + volatile OPJ_BOOL* pret; + opj_event_mgr_t *p_manager; + opj_mutex_t* p_manager_mutex; + OPJ_BOOL check_pterm; +} opj_t1_cblk_decode_processing_job_t; + +static void opj_t1_destroy_wrapper(void* t1) +{ + opj_t1_destroy((opj_t1_t*) t1); +} + +static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls) +{ + opj_tcd_cblk_dec_t* cblk; + opj_tcd_band_t* band; + opj_tcd_tilecomp_t* tilec; + opj_tccp_t* tccp; + OPJ_INT32* OPJ_RESTRICT datap; + OPJ_UINT32 cblk_w, cblk_h; + OPJ_INT32 x, y; + OPJ_UINT32 i, j; + opj_t1_cblk_decode_processing_job_t* job; + opj_t1_t* t1; + OPJ_UINT32 resno; + OPJ_UINT32 tile_w; + + job = (opj_t1_cblk_decode_processing_job_t*) user_data; + + cblk = job->cblk; + + if (!job->whole_tile_decoding) { + cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0); + cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0); + + cblk->decoded_data = (OPJ_INT32*)opj_aligned_malloc(sizeof(OPJ_INT32) * + cblk_w * cblk_h); + if (cblk->decoded_data == NULL) { + if (job->p_manager_mutex) { + opj_mutex_lock(job->p_manager_mutex); + } + opj_event_msg(job->p_manager, EVT_ERROR, + "Cannot allocate cblk->decoded_data\n"); + if (job->p_manager_mutex) { + opj_mutex_unlock(job->p_manager_mutex); + } + *(job->pret) = OPJ_FALSE; + opj_free(job); + return; + } + /* Zero-init required */ + memset(cblk->decoded_data, 0, sizeof(OPJ_INT32) * cblk_w * cblk_h); + } else if (cblk->decoded_data) { + /* Not sure if that code path can happen, but better be */ + /* safe than sorry */ + opj_aligned_free(cblk->decoded_data); + cblk->decoded_data = NULL; + } + + resno = job->resno; + band = job->band; + tilec = job->tilec; + tccp = job->tccp; + tile_w = (OPJ_UINT32)(tilec->resolutions[tilec->minimum_num_resolutions - 1].x1 + - + tilec->resolutions[tilec->minimum_num_resolutions - 1].x0); + + if (!*(job->pret)) { + opj_free(job); + return; + } + + t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1); + if (t1 == NULL) { + t1 = opj_t1_create(OPJ_FALSE); + if (t1 == NULL) { + opj_event_msg(job->p_manager, EVT_ERROR, + "Cannot allocate Tier 1 handle\n"); + *(job->pret) = OPJ_FALSE; + opj_free(job); + return; + } + if (!opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper)) { + opj_event_msg(job->p_manager, EVT_ERROR, + "Unable to set t1 handle as TLS\n"); + opj_t1_destroy(t1); + *(job->pret) = OPJ_FALSE; + opj_free(job); + return; + } + } + t1->mustuse_cblkdatabuffer = job->mustuse_cblkdatabuffer; + + if (OPJ_FALSE == opj_t1_decode_cblk( + t1, + cblk, + band->bandno, + (OPJ_UINT32)tccp->roishift, + tccp->cblksty, + job->p_manager, + job->p_manager_mutex, + job->check_pterm)) { + *(job->pret) = OPJ_FALSE; + opj_free(job); + return; + } + + x = cblk->x0 - band->x0; + y = cblk->y0 - band->y0; + if (band->bandno & 1) { + opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1]; + x += pres->x1 - pres->x0; + } + if (band->bandno & 2) { + opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1]; + y += pres->y1 - pres->y0; + } + + datap = cblk->decoded_data ? cblk->decoded_data : t1->data; + cblk_w = t1->w; + cblk_h = t1->h; + + if (tccp->roishift) { + if (tccp->roishift >= 31) { + for (j = 0; j < cblk_h; ++j) { + for (i = 0; i < cblk_w; ++i) { + datap[(j * cblk_w) + i] = 0; + } + } + } else { + OPJ_INT32 thresh = 1 << tccp->roishift; + for (j = 0; j < cblk_h; ++j) { + for (i = 0; i < cblk_w; ++i) { + OPJ_INT32 val = datap[(j * cblk_w) + i]; + OPJ_INT32 mag = abs(val); + if (mag >= thresh) { + mag >>= tccp->roishift; + datap[(j * cblk_w) + i] = val < 0 ? -mag : mag; + } + } + } + } + } + + /* Both can be non NULL if for example decoding a full tile and then */ + /* partially a tile. In which case partial decoding should be the */ + /* priority */ + assert((cblk->decoded_data != NULL) || (tilec->data != NULL)); + + if (cblk->decoded_data) { + OPJ_UINT32 cblk_size = cblk_w * cblk_h; + if (tccp->qmfbid == 1) { + for (i = 0; i < cblk_size; ++i) { + datap[i] /= 2; + } + } else { /* if (tccp->qmfbid == 0) */ + const float stepsize = 0.5f * band->stepsize; + i = 0; +#ifdef __SSE2__ + { + const __m128 xmm_stepsize = _mm_set1_ps(stepsize); + for (; i < (cblk_size & ~15U); i += 16) { + __m128 xmm0_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)( + datap + 0))); + __m128 xmm1_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)( + datap + 4))); + __m128 xmm2_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)( + datap + 8))); + __m128 xmm3_data = _mm_cvtepi32_ps(_mm_load_si128((__m128i * const)( + datap + 12))); + _mm_store_ps((float*)(datap + 0), _mm_mul_ps(xmm0_data, xmm_stepsize)); + _mm_store_ps((float*)(datap + 4), _mm_mul_ps(xmm1_data, xmm_stepsize)); + _mm_store_ps((float*)(datap + 8), _mm_mul_ps(xmm2_data, xmm_stepsize)); + _mm_store_ps((float*)(datap + 12), _mm_mul_ps(xmm3_data, xmm_stepsize)); + datap += 16; + } + } +#endif + for (; i < cblk_size; ++i) { + OPJ_FLOAT32 tmp = ((OPJ_FLOAT32)(*datap)) * stepsize; + memcpy(datap, &tmp, sizeof(tmp)); + datap++; + } + } + } else if (tccp->qmfbid == 1) { + OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w + + (OPJ_SIZE_T)x]; + for (j = 0; j < cblk_h; ++j) { + i = 0; + for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) { + OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U]; + OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U]; + OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U]; + OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U]; + ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 0U] = tmp0 / 2; + ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 1U] = tmp1 / 2; + ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 2U] = tmp2 / 2; + ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i + 3U] = tmp3 / 2; + } + for (; i < cblk_w; ++i) { + OPJ_INT32 tmp = datap[(j * cblk_w) + i]; + ((OPJ_INT32*)tiledp)[(j * (OPJ_SIZE_T)tile_w) + i] = tmp / 2; + } + } + } else { /* if (tccp->qmfbid == 0) */ + const float stepsize = 0.5f * band->stepsize; + OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_SIZE_T)y * + tile_w + (OPJ_SIZE_T)x]; + for (j = 0; j < cblk_h; ++j) { + OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp; + for (i = 0; i < cblk_w; ++i) { + OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * stepsize; + *tiledp2 = tmp; + datap++; + tiledp2++; + } + tiledp += tile_w; + } + } + + opj_free(job); +} + + +void opj_t1_decode_cblks(opj_tcd_t* tcd, + volatile OPJ_BOOL* pret, + opj_tcd_tilecomp_t* tilec, + opj_tccp_t* tccp, + opj_event_mgr_t *p_manager, + opj_mutex_t* p_manager_mutex, + OPJ_BOOL check_pterm + ) +{ + opj_thread_pool_t* tp = tcd->thread_pool; + OPJ_UINT32 resno, bandno, precno, cblkno; + +#ifdef DEBUG_VERBOSE + OPJ_UINT32 codeblocks_decoded = 0; + printf("Enter opj_t1_decode_cblks()\n"); +#endif + + for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) { + opj_tcd_resolution_t* res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; ++bandno) { + opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno]; + + for (precno = 0; precno < res->pw * res->ph; ++precno) { + opj_tcd_precinct_t* precinct = &band->precincts[precno]; + + if (!opj_tcd_is_subband_area_of_interest(tcd, + tilec->compno, + resno, + band->bandno, + (OPJ_UINT32)precinct->x0, + (OPJ_UINT32)precinct->y0, + (OPJ_UINT32)precinct->x1, + (OPJ_UINT32)precinct->y1)) { + for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) { + opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno]; + if (cblk->decoded_data) { +#ifdef DEBUG_VERBOSE + printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n", + cblk->x0, cblk->y0, resno, bandno); +#endif + opj_aligned_free(cblk->decoded_data); + cblk->decoded_data = NULL; + } + } + continue; + } + + for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) { + opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno]; + opj_t1_cblk_decode_processing_job_t* job; + + if (!opj_tcd_is_subband_area_of_interest(tcd, + tilec->compno, + resno, + band->bandno, + (OPJ_UINT32)cblk->x0, + (OPJ_UINT32)cblk->y0, + (OPJ_UINT32)cblk->x1, + (OPJ_UINT32)cblk->y1)) { + if (cblk->decoded_data) { +#ifdef DEBUG_VERBOSE + printf("Discarding codeblock %d,%d at resno=%d, bandno=%d\n", + cblk->x0, cblk->y0, resno, bandno); +#endif + opj_aligned_free(cblk->decoded_data); + cblk->decoded_data = NULL; + } + continue; + } + + if (!tcd->whole_tile_decoding) { + OPJ_UINT32 cblk_w = (OPJ_UINT32)(cblk->x1 - cblk->x0); + OPJ_UINT32 cblk_h = (OPJ_UINT32)(cblk->y1 - cblk->y0); + if (cblk->decoded_data != NULL) { +#ifdef DEBUG_VERBOSE + printf("Reusing codeblock %d,%d at resno=%d, bandno=%d\n", + cblk->x0, cblk->y0, resno, bandno); +#endif + continue; + } + if (cblk_w == 0 || cblk_h == 0) { + continue; + } +#ifdef DEBUG_VERBOSE + printf("Decoding codeblock %d,%d at resno=%d, bandno=%d\n", + cblk->x0, cblk->y0, resno, bandno); +#endif + } + + job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1, + sizeof(opj_t1_cblk_decode_processing_job_t)); + if (!job) { + *pret = OPJ_FALSE; + return; + } + job->whole_tile_decoding = tcd->whole_tile_decoding; + job->resno = resno; + job->cblk = cblk; + job->band = band; + job->tilec = tilec; + job->tccp = tccp; + job->pret = pret; + job->p_manager_mutex = p_manager_mutex; + job->p_manager = p_manager; + job->check_pterm = check_pterm; + job->mustuse_cblkdatabuffer = opj_thread_pool_get_thread_count(tp) > 1; + opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job); +#ifdef DEBUG_VERBOSE + codeblocks_decoded ++; +#endif + if (!(*pret)) { + return; + } + } /* cblkno */ + } /* precno */ + } /* bandno */ + } /* resno */ + +#ifdef DEBUG_VERBOSE + printf("Leave opj_t1_decode_cblks(). Number decoded: %d\n", codeblocks_decoded); +#endif + return; +} + + +static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1, + opj_tcd_cblk_dec_t* cblk, + OPJ_UINT32 orient, + OPJ_UINT32 roishift, + OPJ_UINT32 cblksty, + opj_event_mgr_t *p_manager, + opj_mutex_t* p_manager_mutex, + OPJ_BOOL check_pterm) +{ + opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ + + OPJ_INT32 bpno_plus_one; + OPJ_UINT32 passtype; + OPJ_UINT32 segno, passno; + OPJ_BYTE* cblkdata = NULL; + OPJ_UINT32 cblkdataindex = 0; + OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */ + OPJ_INT32* original_t1_data = NULL; + + mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9); + + if (!opj_t1_allocate_buffers( + t1, + (OPJ_UINT32)(cblk->x1 - cblk->x0), + (OPJ_UINT32)(cblk->y1 - cblk->y0))) { + return OPJ_FALSE; + } + + bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps); + if (bpno_plus_one >= 31) { + if (p_manager_mutex) { + opj_mutex_lock(p_manager_mutex); + } + opj_event_msg(p_manager, EVT_WARNING, + "opj_t1_decode_cblk(): unsupported bpno_plus_one = %d >= 31\n", + bpno_plus_one); + if (p_manager_mutex) { + opj_mutex_unlock(p_manager_mutex); + } + return OPJ_FALSE; + } + passtype = 2; + + opj_mqc_resetstates(mqc); + opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); + opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); + opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); + + /* Even if we have a single chunk, in multi-threaded decoding */ + /* the insertion of our synthetic marker might potentially override */ + /* valid codestream of other codeblocks decoded in parallel. */ + if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) { + OPJ_UINT32 i; + OPJ_UINT32 cblk_len; + + /* Compute whole codeblock length from chunk lengths */ + cblk_len = 0; + for (i = 0; i < cblk->numchunks; i++) { + cblk_len += cblk->chunks[i].len; + } + + /* Allocate temporary memory if needed */ + if (cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA > t1->cblkdatabuffersize) { + cblkdata = (OPJ_BYTE*)opj_realloc(t1->cblkdatabuffer, + cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA); + if (cblkdata == NULL) { + return OPJ_FALSE; + } + t1->cblkdatabuffer = cblkdata; + memset(t1->cblkdatabuffer + cblk_len, 0, OPJ_COMMON_CBLK_DATA_EXTRA); + t1->cblkdatabuffersize = cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA; + } + + /* Concatenate all chunks */ + cblkdata = t1->cblkdatabuffer; + cblk_len = 0; + for (i = 0; i < cblk->numchunks; i++) { + memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len); + cblk_len += cblk->chunks[i].len; + } + } else if (cblk->numchunks == 1) { + cblkdata = cblk->chunks[0].data; + } else { + /* Not sure if that can happen in practice, but avoid Coverity to */ + /* think we will dereference a null cblkdta pointer */ + return OPJ_TRUE; + } + + /* For subtile decoding, directly decode in the decoded_data buffer of */ + /* the code-block. Hack t1->data to point to it, and restore it later */ + if (cblk->decoded_data) { + original_t1_data = t1->data; + t1->data = cblk->decoded_data; + } + + for (segno = 0; segno < cblk->real_num_segs; ++segno) { + opj_tcd_seg_t *seg = &cblk->segs[segno]; + + /* BYPASS mode */ + type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) && + (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; + + if (type == T1_TYPE_RAW) { + opj_mqc_raw_init_dec(mqc, cblkdata + cblkdataindex, seg->len, + OPJ_COMMON_CBLK_DATA_EXTRA); + } else { + opj_mqc_init_dec(mqc, cblkdata + cblkdataindex, seg->len, + OPJ_COMMON_CBLK_DATA_EXTRA); + } + cblkdataindex += seg->len; + + for (passno = 0; (passno < seg->real_num_passes) && + (bpno_plus_one >= 1); ++passno) { + switch (passtype) { + case 0: + if (type == T1_TYPE_RAW) { + opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty); + } else { + opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty); + } + break; + case 1: + if (type == T1_TYPE_RAW) { + opj_t1_dec_refpass_raw(t1, bpno_plus_one); + } else { + opj_t1_dec_refpass_mqc(t1, bpno_plus_one); + } + break; + case 2: + opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty); + break; + } + + if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) { + opj_mqc_resetstates(mqc); + opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); + opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); + opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); + } + if (++passtype == 3) { + passtype = 0; + bpno_plus_one--; + } + } + + opq_mqc_finish_dec(mqc); + } + + if (check_pterm) { + if (mqc->bp + 2 < mqc->end) { + if (p_manager_mutex) { + opj_mutex_lock(p_manager_mutex); + } + opj_event_msg(p_manager, EVT_WARNING, + "PTERM check failure: %d remaining bytes in code block (%d used / %d)\n", + (int)(mqc->end - mqc->bp) - 2, + (int)(mqc->bp - mqc->start), + (int)(mqc->end - mqc->start)); + if (p_manager_mutex) { + opj_mutex_unlock(p_manager_mutex); + } + } else if (mqc->end_of_byte_stream_counter > 2) { + if (p_manager_mutex) { + opj_mutex_lock(p_manager_mutex); + } + opj_event_msg(p_manager, EVT_WARNING, + "PTERM check failure: %d synthetized 0xFF markers read\n", + mqc->end_of_byte_stream_counter); + if (p_manager_mutex) { + opj_mutex_unlock(p_manager_mutex); + } + } + } + + /* Restore original t1->data is needed */ + if (cblk->decoded_data) { + t1->data = original_t1_data; + } + + return OPJ_TRUE; +} + + +typedef struct { + OPJ_UINT32 compno; + OPJ_UINT32 resno; + opj_tcd_cblk_enc_t* cblk; + opj_tcd_tile_t *tile; + opj_tcd_band_t* band; + opj_tcd_tilecomp_t* tilec; + opj_tccp_t* tccp; + const OPJ_FLOAT64 * mct_norms; + OPJ_UINT32 mct_numcomps; + volatile OPJ_BOOL* pret; + opj_mutex_t* mutex; +} opj_t1_cblk_encode_processing_job_t; + +/** Procedure to deal with a asynchronous code-block encoding job. + * + * @param user_data Pointer to a opj_t1_cblk_encode_processing_job_t* structure + * @param tls TLS handle. + */ +static void opj_t1_cblk_encode_processor(void* user_data, opj_tls_t* tls) +{ + opj_t1_cblk_encode_processing_job_t* job = + (opj_t1_cblk_encode_processing_job_t*)user_data; + opj_tcd_cblk_enc_t* cblk = job->cblk; + const opj_tcd_band_t* band = job->band; + const opj_tcd_tilecomp_t* tilec = job->tilec; + const opj_tccp_t* tccp = job->tccp; + const OPJ_UINT32 resno = job->resno; + opj_t1_t* t1; + const OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0); + + OPJ_INT32* OPJ_RESTRICT tiledp; + OPJ_UINT32 cblk_w; + OPJ_UINT32 cblk_h; + OPJ_UINT32 i, j; + + OPJ_INT32 x = cblk->x0 - band->x0; + OPJ_INT32 y = cblk->y0 - band->y0; + + if (!*(job->pret)) { + opj_free(job); + return; + } + + t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1); + if (t1 == NULL) { + t1 = opj_t1_create(OPJ_TRUE); /* OPJ_TRUE == T1 for encoding */ + opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper); + } + + if (band->bandno & 1) { + opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; + x += pres->x1 - pres->x0; + } + if (band->bandno & 2) { + opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; + y += pres->y1 - pres->y0; + } + + if (!opj_t1_allocate_buffers( + t1, + (OPJ_UINT32)(cblk->x1 - cblk->x0), + (OPJ_UINT32)(cblk->y1 - cblk->y0))) { + *(job->pret) = OPJ_FALSE; + opj_free(job); + return; + } + + cblk_w = t1->w; + cblk_h = t1->h; + + tiledp = &tilec->data[(OPJ_SIZE_T)y * tile_w + (OPJ_SIZE_T)x]; + + if (tccp->qmfbid == 1) { + /* Do multiplication on unsigned type, even if the + * underlying type is signed, to avoid potential + * int overflow on large value (the output will be + * incorrect in such situation, but whatever...) + * This assumes complement-to-2 signed integer + * representation + * Fixes https://github.com/uclouvain/openjpeg/issues/1053 + */ + OPJ_UINT32* OPJ_RESTRICT tiledp_u = (OPJ_UINT32*) tiledp; + OPJ_UINT32* OPJ_RESTRICT t1data = (OPJ_UINT32*) t1->data; + /* Change from "natural" order to "zigzag" order of T1 passes */ + for (j = 0; j < (cblk_h & ~3U); j += 4) { + for (i = 0; i < cblk_w; ++i) { + t1data[0] = tiledp_u[(j + 0) * tile_w + i] << T1_NMSEDEC_FRACBITS; + t1data[1] = tiledp_u[(j + 1) * tile_w + i] << T1_NMSEDEC_FRACBITS; + t1data[2] = tiledp_u[(j + 2) * tile_w + i] << T1_NMSEDEC_FRACBITS; + t1data[3] = tiledp_u[(j + 3) * tile_w + i] << T1_NMSEDEC_FRACBITS; + t1data += 4; + } + } + if (j < cblk_h) { + for (i = 0; i < cblk_w; ++i) { + OPJ_UINT32 k; + for (k = j; k < cblk_h; k++) { + t1data[0] = tiledp_u[k * tile_w + i] << T1_NMSEDEC_FRACBITS; + t1data ++; + } + } + } + } else { /* if (tccp->qmfbid == 0) */ + OPJ_FLOAT32* OPJ_RESTRICT tiledp_f = (OPJ_FLOAT32*) tiledp; + OPJ_INT32* OPJ_RESTRICT t1data = t1->data; + /* Change from "natural" order to "zigzag" order of T1 passes */ + for (j = 0; j < (cblk_h & ~3U); j += 4) { + for (i = 0; i < cblk_w; ++i) { + t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 0) * tile_w + i] / + band->stepsize) * (1 << T1_NMSEDEC_FRACBITS)); + t1data[1] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 1) * tile_w + i] / + band->stepsize) * (1 << T1_NMSEDEC_FRACBITS)); + t1data[2] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 2) * tile_w + i] / + band->stepsize) * (1 << T1_NMSEDEC_FRACBITS)); + t1data[3] = (OPJ_INT32)opj_lrintf((tiledp_f[(j + 3) * tile_w + i] / + band->stepsize) * (1 << T1_NMSEDEC_FRACBITS)); + t1data += 4; + } + } + if (j < cblk_h) { + for (i = 0; i < cblk_w; ++i) { + OPJ_UINT32 k; + for (k = j; k < cblk_h; k++) { + t1data[0] = (OPJ_INT32)opj_lrintf((tiledp_f[k * tile_w + i] / band->stepsize) + * (1 << T1_NMSEDEC_FRACBITS)); + t1data ++; + } + } + } + } + + { + OPJ_FLOAT64 cumwmsedec = + opj_t1_encode_cblk( + t1, + cblk, + band->bandno, + job->compno, + tilec->numresolutions - 1 - resno, + tccp->qmfbid, + band->stepsize, + tccp->cblksty, + job->tile->numcomps, + job->mct_norms, + job->mct_numcomps); + if (job->mutex) { + opj_mutex_lock(job->mutex); + } + job->tile->distotile += cumwmsedec; + if (job->mutex) { + opj_mutex_unlock(job->mutex); + } + } + + opj_free(job); +} + + +OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd, + opj_tcd_tile_t *tile, + opj_tcp_t *tcp, + const OPJ_FLOAT64 * mct_norms, + OPJ_UINT32 mct_numcomps + ) +{ + volatile OPJ_BOOL ret = OPJ_TRUE; + opj_thread_pool_t* tp = tcd->thread_pool; + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + opj_mutex_t* mutex = opj_mutex_create(); + + tile->distotile = 0; /* fixed_quality */ + + for (compno = 0; compno < tile->numcomps; ++compno) { + opj_tcd_tilecomp_t* tilec = &tile->comps[compno]; + opj_tccp_t* tccp = &tcp->tccps[compno]; + + for (resno = 0; resno < tilec->numresolutions; ++resno) { + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; ++bandno) { + opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno]; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + for (precno = 0; precno < res->pw * res->ph; ++precno) { + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) { + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + + opj_t1_cblk_encode_processing_job_t* job = + (opj_t1_cblk_encode_processing_job_t*) opj_calloc(1, + sizeof(opj_t1_cblk_encode_processing_job_t)); + if (!job) { + ret = OPJ_FALSE; + goto end; + } + job->compno = compno; + job->tile = tile; + job->resno = resno; + job->cblk = cblk; + job->band = band; + job->tilec = tilec; + job->tccp = tccp; + job->mct_norms = mct_norms; + job->mct_numcomps = mct_numcomps; + job->pret = &ret; + job->mutex = mutex; + opj_thread_pool_submit_job(tp, opj_t1_cblk_encode_processor, job); + + } /* cblkno */ + } /* precno */ + } /* bandno */ + } /* resno */ + } /* compno */ + +end: + opj_thread_pool_wait_completion(tcd->thread_pool, 0); + if (mutex) { + opj_mutex_destroy(mutex); + } + + return ret; +} + +/* Returns whether the pass (bpno, passtype) is terminated */ +static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk, + OPJ_UINT32 cblksty, + OPJ_INT32 bpno, + OPJ_UINT32 passtype) +{ + /* Is it the last cleanup pass ? */ + if (passtype == 2 && bpno == 0) { + return OPJ_TRUE; + } + + if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { + return OPJ_TRUE; + } + + if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) { + /* For bypass arithmetic bypass, terminate the 4th cleanup pass */ + if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) { + return OPJ_TRUE; + } + /* and beyond terminate all the magnitude refinement passes (in raw) */ + /* and cleanup passes (in MQC) */ + if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) { + return OPJ_TRUE; + } + } + + return OPJ_FALSE; +} + + +/** mod fixed_quality */ +static OPJ_FLOAT64 opj_t1_encode_cblk(opj_t1_t *t1, + opj_tcd_cblk_enc_t* cblk, + OPJ_UINT32 orient, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 cblksty, + OPJ_UINT32 numcomps, + const OPJ_FLOAT64 * mct_norms, + OPJ_UINT32 mct_numcomps) +{ + OPJ_FLOAT64 cumwmsedec = 0.0; + + opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ + + OPJ_UINT32 passno; + OPJ_INT32 bpno; + OPJ_UINT32 passtype; + OPJ_INT32 nmsedec = 0; + OPJ_INT32 max; + OPJ_UINT32 i, j; + OPJ_BYTE type = T1_TYPE_MQ; + OPJ_FLOAT64 tempwmsedec; + OPJ_INT32* datap; + +#ifdef EXTRA_DEBUG + printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n", + cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level); +#endif + + mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9); + + max = 0; + datap = t1->data; + for (j = 0; j < t1->h; ++j) { + const OPJ_UINT32 w = t1->w; + for (i = 0; i < w; ++i, ++datap) { + OPJ_INT32 tmp = *datap; + if (tmp < 0) { + OPJ_UINT32 tmp_unsigned; + max = opj_int_max(max, -tmp); + tmp_unsigned = opj_to_smr(tmp); + memcpy(datap, &tmp_unsigned, sizeof(OPJ_INT32)); + } else { + max = opj_int_max(max, tmp); + } + } + } + + cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - + T1_NMSEDEC_FRACBITS) : 0; + if (cblk->numbps == 0) { + cblk->totalpasses = 0; + return cumwmsedec; + } + + bpno = (OPJ_INT32)(cblk->numbps - 1); + passtype = 2; + + opj_mqc_resetstates(mqc); + opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); + opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); + opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); + opj_mqc_init_enc(mqc, cblk->data); + + for (passno = 0; bpno >= 0; ++passno) { + opj_tcd_pass_t *pass = &cblk->passes[passno]; + type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) && + (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; + + /* If the previous pass was terminating, we need to reset the encoder */ + if (passno > 0 && cblk->passes[passno - 1].term) { + if (type == T1_TYPE_RAW) { + opj_mqc_bypass_init_enc(mqc); + } else { + opj_mqc_restart_init_enc(mqc); + } + } + + switch (passtype) { + case 0: + opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty); + break; + case 1: + opj_t1_enc_refpass(t1, bpno, &nmsedec, type); + break; + case 2: + opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty); + /* code switch SEGMARK (i.e. SEGSYM) */ + if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) { + opj_mqc_segmark_enc(mqc); + } + break; + } + + /* fixed_quality */ + tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, + stepsize, numcomps, mct_norms, mct_numcomps) ; + cumwmsedec += tempwmsedec; + pass->distortiondec = cumwmsedec; + + if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) { + /* If it is a terminated pass, terminate it */ + if (type == T1_TYPE_RAW) { + opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM); + } else { + if (cblksty & J2K_CCP_CBLKSTY_PTERM) { + opj_mqc_erterm_enc(mqc); + } else { + opj_mqc_flush(mqc); + } + } + pass->term = 1; + pass->rate = opj_mqc_numbytes(mqc); + } else { + /* Non terminated pass */ + OPJ_UINT32 rate_extra_bytes; + if (type == T1_TYPE_RAW) { + rate_extra_bytes = opj_mqc_bypass_get_extra_bytes( + mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM)); + } else { + rate_extra_bytes = 3; + } + pass->term = 0; + pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes; + } + + if (++passtype == 3) { + passtype = 0; + bpno--; + } + + /* Code-switch "RESET" */ + if (cblksty & J2K_CCP_CBLKSTY_RESET) { + opj_mqc_reset_enc(mqc); + } + } + + cblk->totalpasses = passno; + + if (cblk->totalpasses) { + /* Make sure that pass rates are increasing */ + OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc); + for (passno = cblk->totalpasses; passno > 0;) { + opj_tcd_pass_t *pass = &cblk->passes[--passno]; + if (pass->rate > last_pass_rate) { + pass->rate = last_pass_rate; + } else { + last_pass_rate = pass->rate; + } + } + } + + for (passno = 0; passno < cblk->totalpasses; passno++) { + opj_tcd_pass_t *pass = &cblk->passes[passno]; + + /* Prevent generation of FF as last data byte of a pass*/ + /* For terminating passes, the flushing procedure ensured this already */ + assert(pass->rate > 0); + if (cblk->data[pass->rate - 1] == 0xFF) { + pass->rate--; + } + pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate); + } + +#ifdef EXTRA_DEBUG + printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0); + + /* Check that there not 0xff >=0x90 sequences */ + if (cblk->totalpasses) { + OPJ_UINT32 i; + OPJ_UINT32 len = opj_mqc_numbytes(mqc); + for (i = 1; i < len; ++i) { + if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) { + printf("0xff %02x at offset %d\n", cblk->data[i], i - 1); + abort(); + } + } + } +#endif + + return cumwmsedec; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/t1.h b/cpp/3rd_party/openjpeg/openjp2/t1.h new file mode 100644 index 0000000000..81ad0d00f1 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/t1.h @@ -0,0 +1,268 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2012, Carl Hetherington + * Copyright (c) 2017, IntoPIX SA + * 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 OPJ_T1_H +#define OPJ_T1_H +/** +@file t1.h +@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1) + +The functions in T1.C have for goal to realize the tier-1 coding operation. The functions +in T1.C are used by some function in TCD.C. +*/ + +/** @defgroup T1 T1 - Implementation of the tier-1 coding */ +/*@{*/ + +/* ----------------------------------------------------------------------- */ +#define T1_NMSEDEC_BITS 7 + +#define T1_NUMCTXS_ZC 9 +#define T1_NUMCTXS_SC 5 +#define T1_NUMCTXS_MAG 3 +#define T1_NUMCTXS_AGG 1 +#define T1_NUMCTXS_UNI 1 + +#define T1_CTXNO_ZC 0 +#define T1_CTXNO_SC (T1_CTXNO_ZC+T1_NUMCTXS_ZC) +#define T1_CTXNO_MAG (T1_CTXNO_SC+T1_NUMCTXS_SC) +#define T1_CTXNO_AGG (T1_CTXNO_MAG+T1_NUMCTXS_MAG) +#define T1_CTXNO_UNI (T1_CTXNO_AGG+T1_NUMCTXS_AGG) +#define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI) + +#define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1) + +#define T1_TYPE_MQ 0 /**< Normal coding using entropy coder */ +#define T1_TYPE_RAW 1 /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/ + +/* BEGINNING of flags that apply to opj_flag_t */ +/** We hold the state of individual data points for the T1 encoder using + * a single 32-bit flags word to hold the state of 4 data points. This corresponds + * to the 4-point-high columns that the data is processed in. + * + * These \#defines declare the layout of a 32-bit flags word. + * + * This is currently done for encoding only. + * The values must NOT be changed, otherwise this is going to break a lot of + * assumptions. + */ + +/* SIGMA: significance state (3 cols x 6 rows) + * CHI: state for negative sample value (1 col x 6 rows) + * MU: state for visited in refinement pass (1 col x 4 rows) + * PI: state for visited in significance pass (1 col * 4 rows) + */ + +#define T1_SIGMA_0 (1U << 0) +#define T1_SIGMA_1 (1U << 1) +#define T1_SIGMA_2 (1U << 2) +#define T1_SIGMA_3 (1U << 3) +#define T1_SIGMA_4 (1U << 4) +#define T1_SIGMA_5 (1U << 5) +#define T1_SIGMA_6 (1U << 6) +#define T1_SIGMA_7 (1U << 7) +#define T1_SIGMA_8 (1U << 8) +#define T1_SIGMA_9 (1U << 9) +#define T1_SIGMA_10 (1U << 10) +#define T1_SIGMA_11 (1U << 11) +#define T1_SIGMA_12 (1U << 12) +#define T1_SIGMA_13 (1U << 13) +#define T1_SIGMA_14 (1U << 14) +#define T1_SIGMA_15 (1U << 15) +#define T1_SIGMA_16 (1U << 16) +#define T1_SIGMA_17 (1U << 17) + +#define T1_CHI_0 (1U << 18) +#define T1_CHI_0_I 18 +#define T1_CHI_1 (1U << 19) +#define T1_CHI_1_I 19 +#define T1_MU_0 (1U << 20) +#define T1_PI_0 (1U << 21) +#define T1_CHI_2 (1U << 22) +#define T1_CHI_2_I 22 +#define T1_MU_1 (1U << 23) +#define T1_PI_1 (1U << 24) +#define T1_CHI_3 (1U << 25) +#define T1_MU_2 (1U << 26) +#define T1_PI_2 (1U << 27) +#define T1_CHI_4 (1U << 28) +#define T1_MU_3 (1U << 29) +#define T1_PI_3 (1U << 30) +#define T1_CHI_5 (1U << 31) +#define T1_CHI_5_I 31 + +/** As an example, the bits T1_SIGMA_3, T1_SIGMA_4 and T1_SIGMA_5 + * indicate the significance state of the west neighbour of data point zero + * of our four, the point itself, and its east neighbour respectively. + * Many of the bits are arranged so that given a flags word, you can + * look at the values for the data point 0, then shift the flags + * word right by 3 bits and look at the same bit positions to see the + * values for data point 1. + * + * The \#defines below help a bit with this; say you have a flags word + * f, you can do things like + * + * (f & T1_SIGMA_THIS) + * + * to see the significance bit of data point 0, then do + * + * ((f >> 3) & T1_SIGMA_THIS) + * + * to see the significance bit of data point 1. + */ + +#define T1_SIGMA_NW T1_SIGMA_0 +#define T1_SIGMA_N T1_SIGMA_1 +#define T1_SIGMA_NE T1_SIGMA_2 +#define T1_SIGMA_W T1_SIGMA_3 +#define T1_SIGMA_THIS T1_SIGMA_4 +#define T1_SIGMA_E T1_SIGMA_5 +#define T1_SIGMA_SW T1_SIGMA_6 +#define T1_SIGMA_S T1_SIGMA_7 +#define T1_SIGMA_SE T1_SIGMA_8 +#define T1_SIGMA_NEIGHBOURS (T1_SIGMA_NW | T1_SIGMA_N | T1_SIGMA_NE | T1_SIGMA_W | T1_SIGMA_E | T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE) + +#define T1_CHI_THIS T1_CHI_1 +#define T1_CHI_THIS_I T1_CHI_1_I +#define T1_MU_THIS T1_MU_0 +#define T1_PI_THIS T1_PI_0 +#define T1_CHI_S T1_CHI_2 + +#define T1_LUT_SGN_W (1U << 0) +#define T1_LUT_SIG_N (1U << 1) +#define T1_LUT_SGN_E (1U << 2) +#define T1_LUT_SIG_W (1U << 3) +#define T1_LUT_SGN_N (1U << 4) +#define T1_LUT_SIG_E (1U << 5) +#define T1_LUT_SGN_S (1U << 6) +#define T1_LUT_SIG_S (1U << 7) +/* END of flags that apply to opj_flag_t */ + +/* ----------------------------------------------------------------------- */ + +/** Flags for 4 consecutive rows of a column */ +typedef OPJ_UINT32 opj_flag_t; + +/** +Tier-1 coding (coding of code-block coefficients) +*/ +typedef struct opj_t1 { + + /** MQC component */ + opj_mqc_t mqc; + + OPJ_INT32 *data; + /** Flags used by decoder and encoder. + * Such that flags[1+0] is for state of col=0,row=0..3, + flags[1+1] for col=1, row=0..3, flags[1+flags_stride] for col=0,row=4..7, ... + This array avoids too much cache trashing when processing by 4 vertical samples + as done in the various decoding steps. */ + opj_flag_t *flags; + + OPJ_UINT32 w; + OPJ_UINT32 h; + OPJ_UINT32 datasize; + OPJ_UINT32 flagssize; + OPJ_BOOL encoder; + + /* Thre 3 variables below are only used by the decoder */ + /* set to TRUE in multithreaded context */ + OPJ_BOOL mustuse_cblkdatabuffer; + /* Temporary buffer to concatenate all chunks of a codebock */ + OPJ_BYTE *cblkdatabuffer; + /* Maximum size available in cblkdatabuffer */ + OPJ_UINT32 cblkdatabuffersize; +} opj_t1_t; + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Encode the code-blocks of a tile +@param tcd TCD handle +@param tile The tile to encode +@param tcp Tile coding parameters +@param mct_norms FIXME DOC +@param mct_numcomps Number of components used for MCT +*/ +OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd, + opj_tcd_tile_t *tile, + opj_tcp_t *tcp, + const OPJ_FLOAT64 * mct_norms, + OPJ_UINT32 mct_numcomps); + +/** +Decode the code-blocks of a tile +@param tcd TCD handle +@param pret Pointer to return value +@param tilec The tile to decode +@param tccp Tile coding parameters +@param p_manager the event manager +@param p_manager_mutex mutex for the event manager +@param check_pterm whether PTERM correct termination should be checked +*/ +void opj_t1_decode_cblks(opj_tcd_t* tcd, + volatile OPJ_BOOL* pret, + opj_tcd_tilecomp_t* tilec, + opj_tccp_t* tccp, + opj_event_mgr_t *p_manager, + opj_mutex_t* p_manager_mutex, + OPJ_BOOL check_pterm); + + + +/** + * Creates a new Tier 1 handle + * and initializes the look-up tables of the Tier-1 coder/decoder + * @return a new T1 handle if successful, returns NULL otherwise +*/ +opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder); + +/** + * Destroys a previously created T1 handle + * + * @param p_t1 Tier 1 handle to destroy +*/ +void opj_t1_destroy(opj_t1_t *p_t1); +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_T1_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/t1_luts.h b/cpp/3rd_party/openjpeg/openjp2/t1_luts.h new file mode 100644 index 0000000000..1a5e7844f7 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/t1_luts.h @@ -0,0 +1,175 @@ +/* This file was automatically generated by t1_generate_luts.c */ + +static const OPJ_BYTE lut_ctxno_zc[2048] = { + 0, 1, 3, 3, 1, 2, 3, 3, 5, 6, 7, 7, 6, 6, 7, 7, 0, 1, 3, 3, 1, 2, 3, 3, 5, 6, 7, 7, 6, 6, 7, 7, + 5, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 5, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, + 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, + 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 2, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, 2, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, + 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 0, 1, 5, 6, 1, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, 0, 1, 5, 6, 1, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, + 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, + 1, 2, 6, 6, 2, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, 1, 2, 6, 6, 2, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, + 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, + 5, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 5, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 6, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 6, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 1, 2, 6, 6, 2, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, 1, 2, 6, 6, 2, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, + 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, + 2, 2, 6, 6, 2, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, 2, 2, 6, 6, 2, 2, 6, 6, 3, 3, 7, 7, 3, 3, 7, 7, + 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, 3, 3, 7, 7, 3, 3, 7, 7, 4, 4, 7, 7, 4, 4, 7, 7, + 6, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 6, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 6, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 6, 6, 8, 8, 6, 6, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, 7, 7, 8, 8, + 0, 1, 3, 3, 1, 2, 3, 3, 5, 6, 7, 7, 6, 6, 7, 7, 0, 1, 3, 3, 1, 2, 3, 3, 5, 6, 7, 7, 6, 6, 7, 7, + 5, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 5, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, + 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, 1, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, + 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 2, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, 2, 2, 3, 3, 2, 2, 3, 3, 6, 6, 7, 7, 6, 6, 7, 7, + 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 4, 4, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, + 0, 3, 1, 4, 3, 6, 4, 7, 1, 4, 2, 5, 4, 7, 5, 7, 0, 3, 1, 4, 3, 6, 4, 7, 1, 4, 2, 5, 4, 7, 5, 7, + 1, 4, 2, 5, 4, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, 1, 4, 2, 5, 4, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, + 3, 6, 4, 7, 6, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, 3, 6, 4, 7, 6, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, + 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, + 1, 4, 2, 5, 4, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, 1, 4, 2, 5, 4, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, + 2, 5, 2, 5, 5, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, 2, 5, 2, 5, 5, 7, 5, 7, + 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, + 5, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, + 3, 6, 4, 7, 6, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, 3, 6, 4, 7, 6, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, + 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, + 6, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 6, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, + 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, + 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 4, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, + 5, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, 5, 7, 5, 7, 7, 8, 7, 8, + 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, + 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8 +}; + +static const OPJ_BYTE lut_ctxno_sc[256] = { + 0x9, 0x9, 0xa, 0xa, 0x9, 0x9, 0xa, 0xa, 0xc, 0xc, 0xd, 0xb, 0xc, 0xc, 0xd, 0xb, + 0x9, 0x9, 0xa, 0xa, 0x9, 0x9, 0xa, 0xa, 0xc, 0xc, 0xb, 0xd, 0xc, 0xc, 0xb, 0xd, + 0xc, 0xc, 0xd, 0xd, 0xc, 0xc, 0xb, 0xb, 0xc, 0x9, 0xd, 0xa, 0x9, 0xc, 0xa, 0xb, + 0xc, 0xc, 0xb, 0xb, 0xc, 0xc, 0xd, 0xd, 0xc, 0x9, 0xb, 0xa, 0x9, 0xc, 0xa, 0xd, + 0x9, 0x9, 0xa, 0xa, 0x9, 0x9, 0xa, 0xa, 0xc, 0xc, 0xd, 0xb, 0xc, 0xc, 0xd, 0xb, + 0x9, 0x9, 0xa, 0xa, 0x9, 0x9, 0xa, 0xa, 0xc, 0xc, 0xb, 0xd, 0xc, 0xc, 0xb, 0xd, + 0xc, 0xc, 0xd, 0xd, 0xc, 0xc, 0xb, 0xb, 0xc, 0x9, 0xd, 0xa, 0x9, 0xc, 0xa, 0xb, + 0xc, 0xc, 0xb, 0xb, 0xc, 0xc, 0xd, 0xd, 0xc, 0x9, 0xb, 0xa, 0x9, 0xc, 0xa, 0xd, + 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xd, 0xb, 0xd, 0xb, 0xd, 0xb, 0xd, 0xb, + 0xa, 0xa, 0x9, 0x9, 0xa, 0xa, 0x9, 0x9, 0xd, 0xb, 0xc, 0xc, 0xd, 0xb, 0xc, 0xc, + 0xd, 0xd, 0xd, 0xd, 0xb, 0xb, 0xb, 0xb, 0xd, 0xa, 0xd, 0xa, 0xa, 0xb, 0xa, 0xb, + 0xd, 0xd, 0xc, 0xc, 0xb, 0xb, 0xc, 0xc, 0xd, 0xa, 0xc, 0x9, 0xa, 0xb, 0x9, 0xc, + 0xa, 0xa, 0x9, 0x9, 0xa, 0xa, 0x9, 0x9, 0xb, 0xd, 0xc, 0xc, 0xb, 0xd, 0xc, 0xc, + 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xb, 0xd, 0xb, 0xd, 0xb, 0xd, 0xb, 0xd, + 0xb, 0xb, 0xc, 0xc, 0xd, 0xd, 0xc, 0xc, 0xb, 0xa, 0xc, 0x9, 0xa, 0xd, 0x9, 0xc, + 0xb, 0xb, 0xb, 0xb, 0xd, 0xd, 0xd, 0xd, 0xb, 0xa, 0xb, 0xa, 0xa, 0xd, 0xa, 0xd +}; + +static const OPJ_BYTE lut_spb[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, + 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1 +}; + +static const OPJ_INT16 lut_nmsedec_sig[1U << T1_NMSEDEC_BITS] = { + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0180, 0x0300, 0x0480, 0x0600, 0x0780, 0x0900, 0x0a80, + 0x0c00, 0x0d80, 0x0f00, 0x1080, 0x1200, 0x1380, 0x1500, 0x1680, + 0x1800, 0x1980, 0x1b00, 0x1c80, 0x1e00, 0x1f80, 0x2100, 0x2280, + 0x2400, 0x2580, 0x2700, 0x2880, 0x2a00, 0x2b80, 0x2d00, 0x2e80, + 0x3000, 0x3180, 0x3300, 0x3480, 0x3600, 0x3780, 0x3900, 0x3a80, + 0x3c00, 0x3d80, 0x3f00, 0x4080, 0x4200, 0x4380, 0x4500, 0x4680, + 0x4800, 0x4980, 0x4b00, 0x4c80, 0x4e00, 0x4f80, 0x5100, 0x5280, + 0x5400, 0x5580, 0x5700, 0x5880, 0x5a00, 0x5b80, 0x5d00, 0x5e80, + 0x6000, 0x6180, 0x6300, 0x6480, 0x6600, 0x6780, 0x6900, 0x6a80, + 0x6c00, 0x6d80, 0x6f00, 0x7080, 0x7200, 0x7380, 0x7500, 0x7680 +}; + +static const OPJ_INT16 lut_nmsedec_sig0[1U << T1_NMSEDEC_BITS] = { + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0080, + 0x0080, 0x0080, 0x0100, 0x0100, 0x0100, 0x0180, 0x0180, 0x0200, + 0x0200, 0x0280, 0x0280, 0x0300, 0x0300, 0x0380, 0x0400, 0x0400, + 0x0480, 0x0500, 0x0580, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780, + 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b80, 0x0c00, + 0x0c80, 0x0d00, 0x0e00, 0x0e80, 0x0f00, 0x1000, 0x1080, 0x1180, + 0x1200, 0x1300, 0x1380, 0x1480, 0x1500, 0x1600, 0x1700, 0x1780, + 0x1880, 0x1980, 0x1a80, 0x1b00, 0x1c00, 0x1d00, 0x1e00, 0x1f00, + 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2680, 0x2780, + 0x2880, 0x2980, 0x2b00, 0x2c00, 0x2d00, 0x2e80, 0x2f80, 0x3100, + 0x3200, 0x3380, 0x3480, 0x3600, 0x3700, 0x3880, 0x3a00, 0x3b00, + 0x3c80, 0x3e00, 0x3f80, 0x4080, 0x4200, 0x4380, 0x4500, 0x4680, + 0x4800, 0x4980, 0x4b00, 0x4c80, 0x4e00, 0x4f80, 0x5180, 0x5300, + 0x5480, 0x5600, 0x5800, 0x5980, 0x5b00, 0x5d00, 0x5e80, 0x6080, + 0x6200, 0x6400, 0x6580, 0x6780, 0x6900, 0x6b00, 0x6d00, 0x6e80, + 0x7080, 0x7280, 0x7480, 0x7600, 0x7800, 0x7a00, 0x7c00, 0x7e00 +}; + +static const OPJ_INT16 lut_nmsedec_ref[1U << T1_NMSEDEC_BITS] = { + 0x1800, 0x1780, 0x1700, 0x1680, 0x1600, 0x1580, 0x1500, 0x1480, + 0x1400, 0x1380, 0x1300, 0x1280, 0x1200, 0x1180, 0x1100, 0x1080, + 0x1000, 0x0f80, 0x0f00, 0x0e80, 0x0e00, 0x0d80, 0x0d00, 0x0c80, + 0x0c00, 0x0b80, 0x0b00, 0x0a80, 0x0a00, 0x0980, 0x0900, 0x0880, + 0x0800, 0x0780, 0x0700, 0x0680, 0x0600, 0x0580, 0x0500, 0x0480, + 0x0400, 0x0380, 0x0300, 0x0280, 0x0200, 0x0180, 0x0100, 0x0080, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0080, 0x0100, 0x0180, 0x0200, 0x0280, 0x0300, 0x0380, + 0x0400, 0x0480, 0x0500, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780, + 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b00, 0x0b80, + 0x0c00, 0x0c80, 0x0d00, 0x0d80, 0x0e00, 0x0e80, 0x0f00, 0x0f80, + 0x1000, 0x1080, 0x1100, 0x1180, 0x1200, 0x1280, 0x1300, 0x1380, + 0x1400, 0x1480, 0x1500, 0x1580, 0x1600, 0x1680, 0x1700, 0x1780 +}; + +static const OPJ_INT16 lut_nmsedec_ref0[1U << T1_NMSEDEC_BITS] = { + 0x2000, 0x1f00, 0x1e00, 0x1d00, 0x1c00, 0x1b00, 0x1a80, 0x1980, + 0x1880, 0x1780, 0x1700, 0x1600, 0x1500, 0x1480, 0x1380, 0x1300, + 0x1200, 0x1180, 0x1080, 0x1000, 0x0f00, 0x0e80, 0x0e00, 0x0d00, + 0x0c80, 0x0c00, 0x0b80, 0x0a80, 0x0a00, 0x0980, 0x0900, 0x0880, + 0x0800, 0x0780, 0x0700, 0x0680, 0x0600, 0x0580, 0x0580, 0x0500, + 0x0480, 0x0400, 0x0400, 0x0380, 0x0300, 0x0300, 0x0280, 0x0280, + 0x0200, 0x0200, 0x0180, 0x0180, 0x0100, 0x0100, 0x0100, 0x0080, + 0x0080, 0x0080, 0x0080, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0080, + 0x0080, 0x0080, 0x0100, 0x0100, 0x0100, 0x0180, 0x0180, 0x0200, + 0x0200, 0x0280, 0x0280, 0x0300, 0x0300, 0x0380, 0x0400, 0x0400, + 0x0480, 0x0500, 0x0580, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780, + 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b80, 0x0c00, + 0x0c80, 0x0d00, 0x0e00, 0x0e80, 0x0f00, 0x1000, 0x1080, 0x1180, + 0x1200, 0x1300, 0x1380, 0x1480, 0x1500, 0x1600, 0x1700, 0x1780, + 0x1880, 0x1980, 0x1a80, 0x1b00, 0x1c00, 0x1d00, 0x1e00, 0x1f00 +}; + diff --git a/cpp/3rd_party/openjpeg/openjp2/t2.c b/cpp/3rd_party/openjpeg/openjp2/t2.c new file mode 100644 index 0000000000..1481e16f46 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/t2.c @@ -0,0 +1,1617 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * Copyright (c) 2017, IntoPIX SA + * 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 "opj_common.h" + + +/** @defgroup T2 T2 - Implementation of a tier-2 coding */ +/*@{*/ + +/** @name Local static functions */ +/*@{*/ + +static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n); + +static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio); +/** +Variable length code for signalling delta Zil (truncation point) +@param bio Bit Input/Output component +@param n delta Zil +*/ +static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n); +static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio); + +/** +Encode a packet of a tile to a destination buffer +@param tileno Number of the tile encoded +@param tile Tile for which to write the packets +@param tcp Tile coding parameters +@param pi Packet identity +@param dest Destination buffer +@param p_data_written FIXME DOC +@param len Length of the destination buffer +@param cstr_info Codestream information structure +@param p_t2_mode If == THRESH_CALC In Threshold calculation ,If == FINAL_PASS Final pass +@param p_manager the user event manager +@return +*/ +static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno, + opj_tcd_tile_t *tile, + opj_tcp_t *tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info, + J2K_T2_MODE p_t2_mode, + opj_event_mgr_t *p_manager); + +/** +Decode a packet of a tile from a source buffer +@param t2 T2 handle +@param tile Tile for which to write the packets +@param tcp Tile coding parameters +@param pi Packet identity +@param src Source buffer +@param data_read FIXME DOC +@param max_length FIXME DOC +@param pack_info Packet information +@param p_manager the user event manager + +@return FIXME DOC +*/ +static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* t2, + opj_tcd_tile_t *tile, + opj_tcp_t *tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *src, + OPJ_UINT32 * data_read, + OPJ_UINT32 max_length, + opj_packet_info_t *pack_info, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BOOL * p_is_data_present, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info, + opj_event_mgr_t *p_manager); + +/** +@param cblk +@param index +@param cblksty +@param first +*/ +static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk, + OPJ_UINT32 index, + OPJ_UINT32 cblksty, + OPJ_UINT32 first); + +/*@}*/ + +/*@}*/ + +/* ----------------------------------------------------------------------- */ + +/* #define RESTART 0x04 */ +static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) +{ + while (--n >= 0) { + opj_bio_write(bio, 1, 1); + } + opj_bio_write(bio, 0, 1); +} + +static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio) +{ + OPJ_UINT32 n = 0; + while (opj_bio_read(bio, 1)) { + ++n; + } + return n; +} + +static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) +{ + if (n == 1) { + opj_bio_write(bio, 0, 1); + } else if (n == 2) { + opj_bio_write(bio, 2, 2); + } else if (n <= 5) { + opj_bio_write(bio, 0xc | (n - 3), 4); + } else if (n <= 36) { + opj_bio_write(bio, 0x1e0 | (n - 6), 9); + } else if (n <= 164) { + opj_bio_write(bio, 0xff80 | (n - 37), 16); + } +} + +static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) +{ + OPJ_UINT32 n; + if (!opj_bio_read(bio, 1)) { + return 1; + } + if (!opj_bio_read(bio, 1)) { + return 2; + } + if ((n = opj_bio_read(bio, 2)) != 3) { + return (3 + n); + } + if ((n = opj_bio_read(bio, 5)) != 31) { + return (6 + n); + } + return (37 + opj_bio_read(bio, 7)); +} + +/* ----------------------------------------------------------------------- */ + +OPJ_BOOL opj_t2_encode_packets(opj_t2_t* p_t2, + OPJ_UINT32 p_tile_no, + opj_tcd_tile_t *p_tile, + OPJ_UINT32 p_maxlayers, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_len, + opj_codestream_info_t *cstr_info, + opj_tcd_marker_info_t* p_marker_info, + OPJ_UINT32 p_tp_num, + OPJ_INT32 p_tp_pos, + OPJ_UINT32 p_pino, + J2K_T2_MODE p_t2_mode, + opj_event_mgr_t *p_manager) +{ + OPJ_BYTE *l_current_data = p_dest; + OPJ_UINT32 l_nb_bytes = 0; + OPJ_UINT32 compno; + OPJ_UINT32 poc; + opj_pi_iterator_t *l_pi = 00; + opj_pi_iterator_t *l_current_pi = 00; + opj_image_t *l_image = p_t2->image; + opj_cp_t *l_cp = p_t2->cp; + opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no]; + OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K) ? 2 : 1; + OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? + l_image->numcomps : 1; + OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; + + l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode, p_manager); + if (!l_pi) { + return OPJ_FALSE; + } + + * p_data_written = 0; + + if (p_t2_mode == THRESH_CALC) { /* Calculating threshold */ + l_current_pi = l_pi; + + for (compno = 0; compno < l_max_comp; ++compno) { + OPJ_UINT32 l_comp_len = 0; + l_current_pi = l_pi; + + for (poc = 0; poc < pocno ; ++poc) { + OPJ_UINT32 l_tp_num = compno; + + /* TODO MSD : check why this function cannot fail (cf. v1) */ + opj_pi_create_encode(l_pi, l_cp, p_tile_no, poc, l_tp_num, p_tp_pos, p_t2_mode); + + if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { + /* TODO ADE : add an error */ + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + while (opj_pi_next(l_current_pi)) { + if (l_current_pi->layno < p_maxlayers) { + l_nb_bytes = 0; + + if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi, + l_current_data, &l_nb_bytes, + p_max_len, cstr_info, + p_t2_mode, + p_manager)) { + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + + l_comp_len += l_nb_bytes; + l_current_data += l_nb_bytes; + p_max_len -= l_nb_bytes; + + * p_data_written += l_nb_bytes; + } + } + + if (l_cp->m_specific_param.m_enc.m_max_comp_size) { + if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) { + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + } + + ++l_current_pi; + } + } + } else { /* t2_mode == FINAL_PASS */ + opj_pi_create_encode(l_pi, l_cp, p_tile_no, p_pino, p_tp_num, p_tp_pos, + p_t2_mode); + + l_current_pi = &l_pi[p_pino]; + if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { + /* TODO ADE : add an error */ + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + + if (p_marker_info && p_marker_info->need_PLT) { + /* One time use intended */ + assert(p_marker_info->packet_count == 0); + assert(p_marker_info->p_packet_size == NULL); + + p_marker_info->p_packet_size = (OPJ_UINT32*) opj_malloc( + opj_get_encoding_packet_count(l_image, l_cp, p_tile_no) * sizeof(OPJ_UINT32)); + if (p_marker_info->p_packet_size == NULL) { + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + } + + while (opj_pi_next(l_current_pi)) { + if (l_current_pi->layno < p_maxlayers) { + l_nb_bytes = 0; + + if (! opj_t2_encode_packet(p_tile_no, p_tile, l_tcp, l_current_pi, + l_current_data, &l_nb_bytes, p_max_len, + cstr_info, p_t2_mode, p_manager)) { + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + + l_current_data += l_nb_bytes; + p_max_len -= l_nb_bytes; + + * p_data_written += l_nb_bytes; + + if (p_marker_info && p_marker_info->need_PLT) { + p_marker_info->p_packet_size[p_marker_info->packet_count] = l_nb_bytes; + p_marker_info->packet_count ++; + } + + /* INDEX >> */ + if (cstr_info) { + if (cstr_info->index_write) { + opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no]; + opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; + if (!cstr_info->packno) { + info_PK->start_pos = info_TL->end_header + 1; + } else { + info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC) && + info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - + 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1; + info_PK->end_ph_pos += info_PK->start_pos - + 1; /* End of packet header which now only represents the distance + to start of packet is incremented by value of start of packet*/ + } + + cstr_info->packno++; + } + /* << INDEX */ + ++p_tile->packno; + } + } + } + + opj_pi_destroy(l_pi, l_nb_pocs); + + return OPJ_TRUE; +} + +/* see issue 80 */ +#if 0 +#define JAS_FPRINTF fprintf +#else +/* issue 290 */ +static void opj_null_jas_fprintf(FILE* file, const char * format, ...) +{ + (void)file; + (void)format; +} +#define JAS_FPRINTF opj_null_jas_fprintf +#endif + +OPJ_BOOL opj_t2_decode_packets(opj_tcd_t* tcd, + opj_t2_t *p_t2, + OPJ_UINT32 p_tile_no, + opj_tcd_tile_t *p_tile, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_len, + opj_codestream_index_t *p_cstr_index, + opj_event_mgr_t *p_manager) +{ + OPJ_BYTE *l_current_data = p_src; + opj_pi_iterator_t *l_pi = 00; + OPJ_UINT32 pino; + opj_image_t *l_image = p_t2->image; + opj_cp_t *l_cp = p_t2->cp; + opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]); + OPJ_UINT32 l_nb_bytes_read; + OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; + opj_pi_iterator_t *l_current_pi = 00; +#ifdef TODO_MSD + OPJ_UINT32 curtp = 0; + OPJ_UINT32 tp_start_packno; +#endif + opj_packet_info_t *l_pack_info = 00; + opj_image_comp_t* l_img_comp = 00; + + OPJ_ARG_NOT_USED(p_cstr_index); + +#ifdef TODO_MSD + if (p_cstr_index) { + l_pack_info = p_cstr_index->tile_index[p_tile_no].packet; + } +#endif + + /* create a packet iterator */ + l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no, p_manager); + if (!l_pi) { + return OPJ_FALSE; + } + + + l_current_pi = l_pi; + + for (pino = 0; pino <= l_tcp->numpocs; ++pino) { + + /* if the resolution needed is too low, one dim of the tilec could be equal to zero + * and no packets are used to decode this resolution and + * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions + * and no l_img_comp->resno_decoded are computed + */ + OPJ_BOOL* first_pass_failed = NULL; + + if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { + /* TODO ADE : add an error */ + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + + first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL)); + if (!first_pass_failed) { + opj_pi_destroy(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL)); + + while (opj_pi_next(l_current_pi)) { + OPJ_BOOL skip_packet = OPJ_FALSE; + JAS_FPRINTF(stderr, + "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n", + l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, + l_current_pi->precno, l_current_pi->layno); + + /* If the packet layer is greater or equal than the maximum */ + /* number of layers, skip the packet */ + if (l_current_pi->layno >= l_tcp->num_layers_to_decode) { + skip_packet = OPJ_TRUE; + } + /* If the packet resolution number is greater than the minimum */ + /* number of resolution allowed, skip the packet */ + else if (l_current_pi->resno >= + p_tile->comps[l_current_pi->compno].minimum_num_resolutions) { + skip_packet = OPJ_TRUE; + } else { + /* If no precincts of any band intersects the area of interest, */ + /* skip the packet */ + OPJ_UINT32 bandno; + opj_tcd_tilecomp_t *tilec = &p_tile->comps[l_current_pi->compno]; + opj_tcd_resolution_t *res = &tilec->resolutions[l_current_pi->resno]; + + skip_packet = OPJ_TRUE; + for (bandno = 0; bandno < res->numbands; ++bandno) { + opj_tcd_band_t* band = &res->bands[bandno]; + opj_tcd_precinct_t* prec = &band->precincts[l_current_pi->precno]; + + if (opj_tcd_is_subband_area_of_interest(tcd, + l_current_pi->compno, + l_current_pi->resno, + band->bandno, + (OPJ_UINT32)prec->x0, + (OPJ_UINT32)prec->y0, + (OPJ_UINT32)prec->x1, + (OPJ_UINT32)prec->y1)) { + skip_packet = OPJ_FALSE; + break; + } + } + /* + printf("packet cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d -> %s\n", + l_current_pi->compno, l_current_pi->resno, + l_current_pi->precno, l_current_pi->layno, skip_packet ? "skipped" : "kept"); + */ + } + + if (!skip_packet) { + l_nb_bytes_read = 0; + + first_pass_failed[l_current_pi->compno] = OPJ_FALSE; + + if (! opj_t2_decode_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data, + &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) { + opj_pi_destroy(l_pi, l_nb_pocs); + opj_free(first_pass_failed); + return OPJ_FALSE; + } + + l_img_comp = &(l_image->comps[l_current_pi->compno]); + l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, + l_img_comp->resno_decoded); + } else { + l_nb_bytes_read = 0; + if (! opj_t2_skip_packet(p_t2, p_tile, l_tcp, l_current_pi, l_current_data, + &l_nb_bytes_read, p_max_len, l_pack_info, p_manager)) { + opj_pi_destroy(l_pi, l_nb_pocs); + opj_free(first_pass_failed); + return OPJ_FALSE; + } + } + + if (first_pass_failed[l_current_pi->compno]) { + l_img_comp = &(l_image->comps[l_current_pi->compno]); + if (l_img_comp->resno_decoded == 0) { + l_img_comp->resno_decoded = + p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1; + } + } + + l_current_data += l_nb_bytes_read; + p_max_len -= l_nb_bytes_read; + + /* INDEX >> */ +#ifdef TODO_MSD + if (p_cstr_info) { + opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no]; + opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno]; + tp_start_packno = 0; + if (!p_cstr_info->packno) { + info_PK->start_pos = info_TL->end_header + 1; + } else if (info_TL->packet[p_cstr_info->packno - 1].end_pos >= + (OPJ_INT32) + p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos) { /* New tile part */ + info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - + tp_start_packno; /* Number of packets in previous tile-part */ + tp_start_packno = p_cstr_info->packno; + curtp++; + info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header + 1; + } else { + info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on && + info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - + 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1; + info_PK->end_ph_pos += info_PK->start_pos - + 1; /* End of packet header which now only represents the distance */ + ++p_cstr_info->packno; + } +#endif + /* << INDEX */ + } + ++l_current_pi; + + opj_free(first_pass_failed); + } + /* INDEX >> */ +#ifdef TODO_MSD + if + (p_cstr_info) { + p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - + tp_start_packno; /* Number of packets in last tile-part */ + } +#endif + /* << INDEX */ + + /* don't forget to release pi */ + opj_pi_destroy(l_pi, l_nb_pocs); + *p_data_read = (OPJ_UINT32)(l_current_data - p_src); + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ + +/** + * Creates a Tier 2 handle + * + * @param p_image Source or destination image + * @param p_cp Image coding parameters. + * @return a new T2 handle if successful, NULL otherwise. +*/ +opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp) +{ + /* create the t2 structure */ + opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1, sizeof(opj_t2_t)); + if (!l_t2) { + return NULL; + } + + l_t2->image = p_image; + l_t2->cp = p_cp; + + return l_t2; +} + +void opj_t2_destroy(opj_t2_t *t2) +{ + if (t2) { + opj_free(t2); + } +} + +static OPJ_BOOL opj_t2_decode_packet(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info, + opj_event_mgr_t *p_manager) +{ + OPJ_BOOL l_read_data; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_nb_total_bytes_read = 0; + + *p_data_read = 0; + + if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src, + &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) { + return OPJ_FALSE; + } + + p_src += l_nb_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + p_max_length -= l_nb_bytes_read; + + /* we should read data for the packet */ + if (l_read_data) { + l_nb_bytes_read = 0; + + if (! opj_t2_read_packet_data(p_t2, p_tile, p_pi, p_src, &l_nb_bytes_read, + p_max_length, p_pack_info, p_manager)) { + return OPJ_FALSE; + } + + l_nb_total_bytes_read += l_nb_bytes_read; + } + + *p_data_read = l_nb_total_bytes_read; + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno, + opj_tcd_tile_t * tile, + opj_tcp_t * tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 length, + opj_codestream_info_t *cstr_info, + J2K_T2_MODE p_t2_mode, + opj_event_mgr_t *p_manager) +{ + OPJ_UINT32 bandno, cblkno; + OPJ_BYTE* c = dest; + OPJ_UINT32 l_nb_bytes; + OPJ_UINT32 compno = pi->compno; /* component value */ + OPJ_UINT32 resno = pi->resno; /* resolution level value */ + OPJ_UINT32 precno = pi->precno; /* precinct value */ + OPJ_UINT32 layno = pi->layno; /* quality layer value */ + OPJ_UINT32 l_nb_blocks; + opj_tcd_band_t *band = 00; + opj_tcd_cblk_enc_t* cblk = 00; + opj_tcd_pass_t *pass = 00; + + opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + + opj_bio_t *bio = 00; /* BIO component */ +#ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION + OPJ_BOOL packet_empty = OPJ_TRUE; +#else + OPJ_BOOL packet_empty = OPJ_FALSE; +#endif + +#ifdef DEBUG_VERBOSE + if (p_t2_mode == FINAL_PASS) { + fprintf(stderr, + "encode packet compono=%d, resno=%d, precno=%d, layno=%d\n", + compno, resno, precno, layno); + } +#endif + + /* */ + if (tcp->csty & J2K_CP_CSTY_SOP) { + if (length < 6) { + if (p_t2_mode == FINAL_PASS) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_t2_encode_packet(): only %u bytes remaining in " + "output buffer. %u needed.\n", + length, 6); + } + return OPJ_FALSE; + } + c[0] = 255; + c[1] = 145; + c[2] = 0; + c[3] = 4; +#if 0 + c[4] = (tile->packno % 65536) / 256; + c[5] = (tile->packno % 65536) % 256; +#else + c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */ + c[5] = tile->packno & 0xff; +#endif + c += 6; + length -= 6; + } + /* */ + + if (!layno) { + band = res->bands; + + for (bandno = 0; bandno < res->numbands; ++bandno, ++band) { + opj_tcd_precinct_t *prc; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1294 */ + /* but likely not a proper fix. */ + if (precno >= res->pw * res->ph) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_t2_encode_packet(): accessing precno=%u >= %u\n", + precno, res->pw * res->ph); + return OPJ_FALSE; + } + + prc = &band->precincts[precno]; + opj_tgt_reset(prc->incltree); + opj_tgt_reset(prc->imsbtree); + + l_nb_blocks = prc->cw * prc->ch; + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { + cblk = &prc->cblks.enc[cblkno]; + + cblk->numpasses = 0; + opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps); + } + } + } + + bio = opj_bio_create(); + if (!bio) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + opj_bio_init_enc(bio, c, length); + +#ifdef ENABLE_EMPTY_PACKET_OPTIMIZATION + /* WARNING: this code branch is disabled, since it has been reported that */ + /* such packets cause decoding issues with cinema J2K hardware */ + /* decoders: https://groups.google.com/forum/#!topic/openjpeg/M7M_fLX_Bco */ + + /* Check if the packet is empty */ + /* Note: we could also skip that step and always write a packet header */ + band = res->bands; + for (bandno = 0; bandno < res->numbands; ++bandno, ++band) { + opj_tcd_precinct_t *prc; + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + prc = &band->precincts[precno]; + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + for (cblkno = 0; cblkno < l_nb_blocks; cblkno++, ++cblk) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + + /* if cblk not included, go to the next cblk */ + if (!layer->numpasses) { + continue; + } + packet_empty = OPJ_FALSE; + break; + } + if (!packet_empty) { + break; + } + } +#endif + opj_bio_write(bio, packet_empty ? 0 : 1, 1); /* Empty header bit */ + + /* Writing Packet header */ + band = res->bands; + for (bandno = 0; !packet_empty && + bandno < res->numbands; ++bandno, ++band) { + opj_tcd_precinct_t *prc; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + /* Avoid out of bounds access of https://github.com/uclouvain/openjpeg/issues/1297 */ + /* but likely not a proper fix. */ + if (precno >= res->pw * res->ph) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_t2_encode_packet(): accessing precno=%u >= %u\n", + precno, res->pw * res->ph); + return OPJ_FALSE; + } + + prc = &band->precincts[precno]; + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + + if (!cblk->numpasses && layer->numpasses) { + opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno); + } + + ++cblk; + } + + cblk = prc->cblks.enc; + for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + OPJ_UINT32 increment = 0; + OPJ_UINT32 nump = 0; + OPJ_UINT32 len = 0, passno; + OPJ_UINT32 l_nb_passes; + + /* cblk inclusion bits */ + if (!cblk->numpasses) { + opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1)); + } else { + opj_bio_write(bio, layer->numpasses != 0, 1); + } + + /* if cblk not included, go to the next cblk */ + if (!layer->numpasses) { + ++cblk; + continue; + } + + /* if first instance of cblk --> zero bit-planes information */ + if (!cblk->numpasses) { + cblk->numlenbits = 3; + opj_tgt_encode(bio, prc->imsbtree, cblkno, 999); + } + + /* number of coding passes included */ + opj_t2_putnumpasses(bio, layer->numpasses); + l_nb_passes = cblk->numpasses + layer->numpasses; + pass = cblk->passes + cblk->numpasses; + + /* computation of the increase of the length indicator and insertion in the header */ + for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { + ++nump; + len += pass->len; + + if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, + opj_int_floorlog2((OPJ_INT32)len) + 1 + - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump))); + len = 0; + nump = 0; + } + + ++pass; + } + opj_t2_putcommacode(bio, (OPJ_INT32)increment); + + /* computation of the new Length indicator */ + cblk->numlenbits += increment; + + pass = cblk->passes + cblk->numpasses; + /* insertion of the codeword segment length */ + for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { + nump++; + len += pass->len; + + if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + opj_bio_write(bio, (OPJ_UINT32)len, + cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump)); + len = 0; + nump = 0; + } + ++pass; + } + + ++cblk; + } + } + + if (!opj_bio_flush(bio)) { + opj_bio_destroy(bio); + return OPJ_FALSE; /* modified to eliminate longjmp !! */ + } + + l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio); + c += l_nb_bytes; + length -= l_nb_bytes; + + opj_bio_destroy(bio); + + /* */ + if (tcp->csty & J2K_CP_CSTY_EPH) { + if (length < 2) { + if (p_t2_mode == FINAL_PASS) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_t2_encode_packet(): only %u bytes remaining in " + "output buffer. %u needed.\n", + length, 2); + } + return OPJ_FALSE; + } + c[0] = 255; + c[1] = 146; + c += 2; + length -= 2; + } + /* */ + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + Will be updated later by incrementing with packet start value*/ + if (cstr_info && cstr_info->index_write) { + opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; + info_PK->end_ph_pos = (OPJ_INT32)(c - dest); + } + /* INDEX >> */ + + /* Writing the packet body */ + band = res->bands; + for (bandno = 0; !packet_empty && bandno < res->numbands; bandno++, ++band) { + opj_tcd_precinct_t *prc; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + prc = &band->precincts[precno]; + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + + if (!layer->numpasses) { + ++cblk; + continue; + } + + if (layer->len > length) { + if (p_t2_mode == FINAL_PASS) { + opj_event_msg(p_manager, EVT_ERROR, + "opj_t2_encode_packet(): only %u bytes remaining in " + "output buffer. %u needed.\n", + length, layer->len); + } + return OPJ_FALSE; + } + + memcpy(c, layer->data, layer->len); + cblk->numpasses += layer->numpasses; + c += layer->len; + length -= layer->len; + + /* << INDEX */ + if (cstr_info && cstr_info->index_write) { + opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; + info_PK->disto += layer->disto; + if (cstr_info->D_max < info_PK->disto) { + cstr_info->D_max = info_PK->disto; + } + } + + ++cblk; + /* INDEX >> */ + } + } + + assert(c >= dest); + * p_data_written += (OPJ_UINT32)(c - dest); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_t2_skip_packet(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info, + opj_event_mgr_t *p_manager) +{ + OPJ_BOOL l_read_data; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_nb_total_bytes_read = 0; + + *p_data_read = 0; + + if (! opj_t2_read_packet_header(p_t2, p_tile, p_tcp, p_pi, &l_read_data, p_src, + &l_nb_bytes_read, p_max_length, p_pack_info, p_manager)) { + return OPJ_FALSE; + } + + p_src += l_nb_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + p_max_length -= l_nb_bytes_read; + + /* we should read data for the packet */ + if (l_read_data) { + l_nb_bytes_read = 0; + + if (! opj_t2_skip_packet_data(p_t2, p_tile, p_pi, &l_nb_bytes_read, + p_max_length, p_pack_info, p_manager)) { + return OPJ_FALSE; + } + + l_nb_total_bytes_read += l_nb_bytes_read; + } + *p_data_read = l_nb_total_bytes_read; + + return OPJ_TRUE; +} + + +static OPJ_BOOL opj_t2_read_packet_header(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BOOL * p_is_data_present, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info, + opj_event_mgr_t *p_manager) + +{ + /* loop */ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + OPJ_UINT32 l_remaining_length; + OPJ_UINT32 l_header_length; + OPJ_UINT32 * l_modified_length_ptr = 00; + OPJ_BYTE *l_current_data = p_src_data; + opj_cp_t *l_cp = p_t2->cp; + opj_bio_t *l_bio = 00; /* BIO component */ + opj_tcd_band_t *l_band = 00; + opj_tcd_cblk_dec_t* l_cblk = 00; + opj_tcd_resolution_t* l_res = + &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + OPJ_BYTE *l_header_data = 00; + OPJ_BYTE **l_header_data_start = 00; + + OPJ_UINT32 l_present; + + if (p_pi->layno == 0) { + l_band = l_res->bands; + + /* reset tagtrees */ + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + if (!opj_tcd_is_band_empty(l_band)) { + opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; + if (!(p_pi->precno < (l_band->precincts_data_size / sizeof( + opj_tcd_precinct_t)))) { + opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct\n"); + return OPJ_FALSE; + } + + + opj_tgt_reset(l_prc->incltree); + opj_tgt_reset(l_prc->imsbtree); + l_cblk = l_prc->cblks.dec; + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + l_cblk->numsegs = 0; + l_cblk->real_num_segs = 0; + ++l_cblk; + } + } + + ++l_band; + } + } + + /* SOP markers */ + + if (p_tcp->csty & J2K_CP_CSTY_SOP) { + if (p_max_length < 6) { + opj_event_msg(p_manager, EVT_WARNING, + "Not enough space for expected SOP marker\n"); + } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) { + opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n"); + } else { + l_current_data += 6; + } + + /** TODO : check the Nsop value */ + } + + /* + When the marker PPT/PPM is used the packet header are store in PPT/PPM marker + This part deal with this caracteristic + step 1: Read packet header in the saved structure + step 2: Return to codestream for decoding + */ + + l_bio = opj_bio_create(); + if (! l_bio) { + return OPJ_FALSE; + } + + if (l_cp->ppm == 1) { /* PPM */ + l_header_data_start = &l_cp->ppm_data; + l_header_data = *l_header_data_start; + l_modified_length_ptr = &(l_cp->ppm_len); + + } else if (p_tcp->ppt == 1) { /* PPT */ + l_header_data_start = &(p_tcp->ppt_data); + l_header_data = *l_header_data_start; + l_modified_length_ptr = &(p_tcp->ppt_len); + } else { /* Normal Case */ + l_header_data_start = &(l_current_data); + l_header_data = *l_header_data_start; + l_remaining_length = (OPJ_UINT32)(p_src_data + p_max_length - l_header_data); + l_modified_length_ptr = &(l_remaining_length); + } + + opj_bio_init_dec(l_bio, l_header_data, *l_modified_length_ptr); + + l_present = opj_bio_read(l_bio, 1); + JAS_FPRINTF(stderr, "present=%d \n", l_present); + if (!l_present) { + /* TODO MSD: no test to control the output of this function*/ + opj_bio_inalign(l_bio); + l_header_data += opj_bio_numbytes(l_bio); + opj_bio_destroy(l_bio); + + /* EPH markers */ + if (p_tcp->csty & J2K_CP_CSTY_EPH) { + if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - + *l_header_data_start)) < 2U) { + opj_event_msg(p_manager, EVT_WARNING, + "Not enough space for expected EPH marker\n"); + } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { + opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n"); + } else { + l_header_data += 2; + } + } + + l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start); + *l_modified_length_ptr -= l_header_length; + *l_header_data_start += l_header_length; + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + Will be updated later by incrementing with packet start value */ + if (p_pack_info) { + p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); + } + /* INDEX >> */ + + * p_is_data_present = OPJ_FALSE; + *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data); + return OPJ_TRUE; + } + + l_band = l_res->bands; + for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band) { + opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]); + + if (opj_tcd_is_band_empty(l_band)) { + continue; + } + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) { + OPJ_UINT32 l_included, l_increment, l_segno; + OPJ_INT32 n; + + /* if cblk not yet included before --> inclusion tagtree */ + if (!l_cblk->numsegs) { + l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, + (OPJ_INT32)(p_pi->layno + 1)); + /* else one bit */ + } else { + l_included = opj_bio_read(l_bio, 1); + } + + /* if cblk not included */ + if (!l_included) { + l_cblk->numnewpasses = 0; + ++l_cblk; + JAS_FPRINTF(stderr, "included=%d \n", l_included); + continue; + } + + /* if cblk not yet included --> zero-bitplane tagtree */ + if (!l_cblk->numsegs) { + OPJ_UINT32 i = 0; + + while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) { + ++i; + } + + l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i; + l_cblk->numlenbits = 3; + } + + /* number of coding passes */ + l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio); + l_increment = opj_t2_getcommacode(l_bio); + + /* length indicator increment */ + l_cblk->numlenbits += l_increment; + l_segno = 0; + + if (!l_cblk->numsegs) { + if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { + opj_bio_destroy(l_bio); + return OPJ_FALSE; + } + } else { + l_segno = l_cblk->numsegs - 1; + if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) { + ++l_segno; + if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { + opj_bio_destroy(l_bio); + return OPJ_FALSE; + } + } + } + n = (OPJ_INT32)l_cblk->numnewpasses; + + do { + OPJ_UINT32 bit_number; + l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)( + l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n); + bit_number = l_cblk->numlenbits + opj_uint_floorlog2( + l_cblk->segs[l_segno].numnewpasses); + if (bit_number > 32) { + opj_event_msg(p_manager, EVT_ERROR, + "Invalid bit number %d in opj_t2_read_packet_header()\n", + bit_number); + opj_bio_destroy(l_bio); + return OPJ_FALSE; + } + l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, bit_number); + JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", + l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, + l_cblk->segs[l_segno].newlen); + + n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses; + if (n > 0) { + ++l_segno; + + if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { + opj_bio_destroy(l_bio); + return OPJ_FALSE; + } + } + } while (n > 0); + + ++l_cblk; + } + } + + if (!opj_bio_inalign(l_bio)) { + opj_bio_destroy(l_bio); + return OPJ_FALSE; + } + + l_header_data += opj_bio_numbytes(l_bio); + opj_bio_destroy(l_bio); + + /* EPH markers */ + if (p_tcp->csty & J2K_CP_CSTY_EPH) { + if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - + *l_header_data_start)) < 2U) { + opj_event_msg(p_manager, EVT_WARNING, + "Not enough space for expected EPH marker\n"); + } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { + opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n"); + } else { + l_header_data += 2; + } + } + + l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start); + JAS_FPRINTF(stderr, "hdrlen=%d \n", l_header_length); + JAS_FPRINTF(stderr, "packet body\n"); + *l_modified_length_ptr -= l_header_length; + *l_header_data_start += l_header_length; + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + Will be updated later by incrementing with packet start value */ + if (p_pack_info) { + p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); + } + /* INDEX >> */ + + *p_is_data_present = OPJ_TRUE; + *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info, + opj_event_mgr_t* p_manager) +{ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + OPJ_BYTE *l_current_data = p_src_data; + opj_tcd_band_t *l_band = 00; + opj_tcd_cblk_dec_t* l_cblk = 00; + opj_tcd_resolution_t* l_res = + &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + OPJ_ARG_NOT_USED(p_t2); + OPJ_ARG_NOT_USED(pack_info); + + l_band = l_res->bands; + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; + + if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) { + ++l_band; + continue; + } + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + opj_tcd_seg_t *l_seg = 00; + + if (!l_cblk->numnewpasses) { + /* nothing to do */ + ++l_cblk; + continue; + } + + if (!l_cblk->numsegs) { + l_seg = l_cblk->segs; + ++l_cblk->numsegs; + } else { + l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; + + if (l_seg->numpasses == l_seg->maxpasses) { + ++l_seg; + ++l_cblk->numsegs; + } + } + + do { + /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */ + if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) < + (OPJ_SIZE_T)l_current_data) || + (l_current_data + l_seg->newlen > p_src_data + p_max_length)) { + opj_event_msg(p_manager, EVT_ERROR, + "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, + p_pi->compno); + return OPJ_FALSE; + } + +#ifdef USE_JPWL + /* we need here a j2k handle to verify if making a check to + the validity of cblocks parameters is selected from user (-W) */ + + /* let's check that we are not exceeding */ + if ((l_cblk->len + l_seg->newlen) > 8192) { + opj_event_msg(p_manager, EVT_WARNING, + "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + l_seg->newlen = 8192 - l_cblk->len; + opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n", + l_seg->newlen); + break; + }; + +#endif /* USE_JPWL */ + + if (l_cblk->numchunks == l_cblk->numchunksalloc) { + OPJ_UINT32 l_numchunksalloc = l_cblk->numchunksalloc * 2 + 1; + opj_tcd_seg_data_chunk_t* l_chunks = + (opj_tcd_seg_data_chunk_t*)opj_realloc(l_cblk->chunks, + l_numchunksalloc * sizeof(opj_tcd_seg_data_chunk_t)); + if (l_chunks == NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "cannot allocate opj_tcd_seg_data_chunk_t* array"); + return OPJ_FALSE; + } + l_cblk->chunks = l_chunks; + l_cblk->numchunksalloc = l_numchunksalloc; + } + + l_cblk->chunks[l_cblk->numchunks].data = l_current_data; + l_cblk->chunks[l_cblk->numchunks].len = l_seg->newlen; + l_cblk->numchunks ++; + + l_current_data += l_seg->newlen; + l_seg->len += l_seg->newlen; + l_seg->numpasses += l_seg->numnewpasses; + l_cblk->numnewpasses -= l_seg->numnewpasses; + + l_seg->real_num_passes = l_seg->numpasses; + + if (l_cblk->numnewpasses > 0) { + ++l_seg; + ++l_cblk->numsegs; + } + } while (l_cblk->numnewpasses > 0); + + l_cblk->real_num_segs = l_cblk->numsegs; + ++l_cblk; + } /* next code_block */ + + ++l_band; + } + + *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data); + + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info, + opj_event_mgr_t *p_manager) +{ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + opj_tcd_band_t *l_band = 00; + opj_tcd_cblk_dec_t* l_cblk = 00; + opj_tcd_resolution_t* l_res = + &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + OPJ_ARG_NOT_USED(p_t2); + OPJ_ARG_NOT_USED(pack_info); + + *p_data_read = 0; + l_band = l_res->bands; + + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; + + if ((l_band->x1 - l_band->x0 == 0) || (l_band->y1 - l_band->y0 == 0)) { + ++l_band; + continue; + } + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + opj_tcd_seg_t *l_seg = 00; + + if (!l_cblk->numnewpasses) { + /* nothing to do */ + ++l_cblk; + continue; + } + + if (!l_cblk->numsegs) { + l_seg = l_cblk->segs; + ++l_cblk->numsegs; + } else { + l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; + + if (l_seg->numpasses == l_seg->maxpasses) { + ++l_seg; + ++l_cblk->numsegs; + } + } + + do { + /* Check possible overflow then size */ + if (((*p_data_read + l_seg->newlen) < (*p_data_read)) || + ((*p_data_read + l_seg->newlen) > p_max_length)) { + opj_event_msg(p_manager, EVT_ERROR, + "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, + p_pi->compno); + return OPJ_FALSE; + } + +#ifdef USE_JPWL + /* we need here a j2k handle to verify if making a check to + the validity of cblocks parameters is selected from user (-W) */ + + /* let's check that we are not exceeding */ + if ((l_cblk->len + l_seg->newlen) > 8192) { + opj_event_msg(p_manager, EVT_WARNING, + "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); + if (!JPWL_ASSUME) { + opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return -999; + } + l_seg->newlen = 8192 - l_cblk->len; + opj_event_msg(p_manager, EVT_WARNING, " - truncating segment to %d\n", + l_seg->newlen); + break; + }; + +#endif /* USE_JPWL */ + JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, + l_seg->newlen); + *(p_data_read) += l_seg->newlen; + + l_seg->numpasses += l_seg->numnewpasses; + l_cblk->numnewpasses -= l_seg->numnewpasses; + if (l_cblk->numnewpasses > 0) { + ++l_seg; + ++l_cblk->numsegs; + } + } while (l_cblk->numnewpasses > 0); + + ++l_cblk; + } + + ++l_band; + } + + return OPJ_TRUE; +} + + +static OPJ_BOOL opj_t2_init_seg(opj_tcd_cblk_dec_t* cblk, + OPJ_UINT32 index, + OPJ_UINT32 cblksty, + OPJ_UINT32 first) +{ + opj_tcd_seg_t* seg = 00; + OPJ_UINT32 l_nb_segs = index + 1; + + if (l_nb_segs > cblk->m_current_max_segs) { + opj_tcd_seg_t* new_segs; + OPJ_UINT32 l_m_current_max_segs = cblk->m_current_max_segs + + OPJ_J2K_DEFAULT_NB_SEGS; + + new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, + l_m_current_max_segs * sizeof(opj_tcd_seg_t)); + if (! new_segs) { + /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */ + return OPJ_FALSE; + } + cblk->segs = new_segs; + memset(new_segs + cblk->m_current_max_segs, + 0, OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t)); + cblk->m_current_max_segs = l_m_current_max_segs; + } + + seg = &cblk->segs[index]; + opj_tcd_reinit_segment(seg); + + if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { + seg->maxpasses = 1; + } else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { + if (first) { + seg->maxpasses = 10; + } else { + seg->maxpasses = (((seg - 1)->maxpasses == 1) || + ((seg - 1)->maxpasses == 10)) ? 2 : 1; + } + } else { + /* See paragraph "B.10.6 Number of coding passes" of the standard. + * Probably that 109 must be interpreted a (Mb-1)*3 + 1 with Mb=37, + * Mb being the maximum number of bit-planes available for the + * representation of coefficients in the sub-band */ + seg->maxpasses = 109; + } + + return OPJ_TRUE; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/t2.h b/cpp/3rd_party/openjpeg/openjp2/t2.h new file mode 100644 index 0000000000..becfa91a4d --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/t2.h @@ -0,0 +1,142 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * Copyright (c) 2017, IntoPIX SA + * 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 OPJ_T2_H +#define OPJ_T2_H +/** +@file t2.h +@brief Implementation of a tier-2 coding (packetization of code-block data) (T2) + +*/ + +/** @defgroup T2 T2 - Implementation of a tier-2 coding */ +/*@{*/ + +/** +Tier-2 coding +*/ +typedef struct opj_t2 { + + /** Encoding: pointer to the src image. Decoding: pointer to the dst image. */ + opj_image_t *image; + /** pointer to the image coding parameters */ + opj_cp_t *cp; +} opj_t2_t; + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Encode the packets of a tile to a destination buffer +@param t2 T2 handle +@param tileno number of the tile encoded +@param tile the tile for which to write the packets +@param maxlayers maximum number of layers +@param dest the destination buffer +@param p_data_written FIXME DOC +@param len the length of the destination buffer +@param cstr_info Codestream information structure +@param p_marker_info Marker information structure +@param tpnum Tile part number of the current tile +@param tppos The position of the tile part flag in the progression order +@param pino FIXME DOC +@param t2_mode If == THRESH_CALC In Threshold calculation ,If == FINAL_PASS Final pass +@param p_manager the user event manager +*/ +OPJ_BOOL opj_t2_encode_packets(opj_t2_t* t2, + OPJ_UINT32 tileno, + opj_tcd_tile_t *tile, + OPJ_UINT32 maxlayers, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info, + opj_tcd_marker_info_t* p_marker_info, + OPJ_UINT32 tpnum, + OPJ_INT32 tppos, + OPJ_UINT32 pino, + J2K_T2_MODE t2_mode, + opj_event_mgr_t *p_manager); + +/** +Decode the packets of a tile from a source buffer +@param tcd TCD handle +@param t2 T2 handle +@param tileno number that identifies the tile for which to decode the packets +@param tile tile for which to decode the packets +@param src FIXME DOC +@param p_data_read the source buffer +@param len length of the source buffer +@param cstr_info FIXME DOC +@param p_manager the user event manager + +@return FIXME DOC + */ +OPJ_BOOL opj_t2_decode_packets(opj_tcd_t* tcd, + opj_t2_t *t2, + OPJ_UINT32 tileno, + opj_tcd_tile_t *tile, + OPJ_BYTE *src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 len, + opj_codestream_index_t *cstr_info, + opj_event_mgr_t *p_manager); + +/** + * Creates a Tier 2 handle + * + * @param p_image Source or destination image + * @param p_cp Image coding parameters. + * @return a new T2 handle if successful, NULL otherwise. +*/ +opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp); + +/** +Destroy a T2 handle +@param t2 T2 handle to destroy +*/ +void opj_t2_destroy(opj_t2_t *t2); + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_T2_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/tcd.c b/cpp/3rd_party/openjpeg/openjp2/tcd.c new file mode 100644 index 0000000000..6442669d60 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/tcd.c @@ -0,0 +1,2859 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * Copyright (c) 2017, IntoPIX SA + * 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 "opj_common.h" + +/* ----------------------------------------------------------------------- */ + +/* TODO MSD: */ +#ifdef TODO_MSD +void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) +{ + int tileno, compno, resno, bandno, precno;/*, cblkno;*/ + + fprintf(fd, "image {\n"); + fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", + img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, + tcd->image->y1); + + for (tileno = 0; tileno < img->th * img->tw; tileno++) { + opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno]; + fprintf(fd, " tile {\n"); + fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", + tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); + for (compno = 0; compno < tile->numcomps; compno++) { + opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; + fprintf(fd, " tilec {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", + tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + fprintf(fd, "\n res {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", + res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + fprintf(fd, " band {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n", + band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_t *prec = &band->precincts[precno]; + fprintf(fd, " prec {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", + prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); + /* + for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { + opj_tcd_cblk_t *cblk = &prec->cblks[cblkno]; + fprintf(fd, " cblk {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d\n", + cblk->x0, cblk->y0, cblk->x1, cblk->y1); + fprintf(fd, " }\n"); + } + */ + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, "}\n"); +} +#endif + +/** + * Initializes tile coding/decoding + */ +static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, + OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block, + opj_event_mgr_t* manager); + +/** +* Allocates memory for a decoding code block. +*/ +static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t * + p_code_block); + +/** + * Deallocates the decoding data of the given precinct. + */ +static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct); + +/** + * Allocates memory for an encoding code block (but not data). + */ +static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t * + p_code_block); + +/** + * Allocates data for an encoding code block + */ +static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t * + p_code_block); + +/** + * Deallocates the encoding data of the given precinct. + */ +static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct); + + +/** +Free the memory allocated for encoding +@param tcd TCD handle +*/ +static void opj_tcd_free_tile(opj_tcd_t *tcd); + + +static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd, + OPJ_BYTE * p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_src_size, + opj_codestream_index_t *p_cstr_index, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd); + +static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd); + + +static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd); + +static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd); + +static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd); + +static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd); + +static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info, + opj_tcd_marker_info_t* p_marker_info, + opj_event_mgr_t *p_manager); + +static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info, + opj_event_mgr_t *p_manager); + + +static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd, + OPJ_UINT32 compno); + +/* ----------------------------------------------------------------------- */ + +/** +Create a new TCD handle +*/ +opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder) +{ + opj_tcd_t *l_tcd = 00; + + /* create the tcd structure */ + l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t)); + if (!l_tcd) { + return 00; + } + + l_tcd->m_is_decoder = p_is_decoder ? 1 : 0; + + l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t)); + if (!l_tcd->tcd_image) { + opj_free(l_tcd); + return 00; + } + + return l_tcd; +} + + +/* ----------------------------------------------------------------------- */ + +void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) +{ + OPJ_UINT32 layno; + + for (layno = 0; layno < tcd->tcp->numlayers; layno++) { + opj_tcd_makelayer_fixed(tcd, layno, 1); + } +} + + +void opj_tcd_makelayer(opj_tcd_t *tcd, + OPJ_UINT32 layno, + OPJ_FLOAT64 thresh, + OPJ_UINT32 final) +{ + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + OPJ_UINT32 passno; + + opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles; + + tcd_tile->distolayer[layno] = 0; /* fixed_quality */ + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + OPJ_UINT32 n; + + if (layno == 0) { + cblk->numpassesinlayers = 0; + } + + n = cblk->numpassesinlayers; + + if (thresh < 0) { + /* Special value to indicate to use all passes */ + n = cblk->totalpasses; + } else { + for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) { + OPJ_UINT32 dr; + OPJ_FLOAT64 dd; + opj_tcd_pass_t *pass = &cblk->passes[passno]; + + if (n == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[n - 1].rate; + dd = pass->distortiondec - cblk->passes[n - 1].distortiondec; + } + + if (!dr) { + if (dd != 0) { + n = passno + 1; + } + continue; + } + if (thresh - (dd / dr) < + DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */ + n = passno + 1; + } + } + } + + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) { + layer->disto = 0; + continue; + } + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + layer->disto = cblk->passes[n - 1].distortiondec; + } else { + layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - + 1].rate; + layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->disto = cblk->passes[n - 1].distortiondec - + cblk->passes[cblk->numpassesinlayers - 1].distortiondec; + } + + tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */ + + if (final) { + cblk->numpassesinlayers = n; + } + } + } + } + } + } +} + +void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, + OPJ_UINT32 final) +{ + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */ + OPJ_INT32 matrice[10][10][3]; + OPJ_UINT32 i, j, k; + + opj_cp_t *cp = tcd->cp; + opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles; + opj_tcp_t *tcd_tcp = tcd->tcp; + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + + for (i = 0; i < tcd_tcp->numlayers; i++) { + for (j = 0; j < tilec->numresolutions; j++) { + for (k = 0; k < 3; k++) { + matrice[i][j][k] = + (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * + tilec->numresolutions * 3 + j * 3 + k] + * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0)); + } + } + } + + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + OPJ_UINT32 n; + OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - + cblk->numbps); /* number of bit-plan equal to zero */ + + /* Correction of the matrix of coefficient to include the IMSB information */ + if (layno == 0) { + value = matrice[layno][resno][bandno]; + if (imsb >= value) { + value = 0; + } else { + value -= imsb; + } + } else { + value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno]; + if (imsb >= matrice[layno - 1][resno][bandno]) { + value -= (imsb - matrice[layno - 1][resno][bandno]); + if (value < 0) { + value = 0; + } + } + } + + if (layno == 0) { + cblk->numpassesinlayers = 0; + } + + n = cblk->numpassesinlayers; + if (cblk->numpassesinlayers == 0) { + if (value != 0) { + n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers; + } else { + n = cblk->numpassesinlayers; + } + } else { + n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers; + } + + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) { + continue; + } + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + } else { + layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - + 1].rate; + layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; + } + + if (final) { + cblk->numpassesinlayers = n; + } + } + } + } + } + } +} + +OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info, + opj_event_mgr_t *p_manager) +{ + OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno; + OPJ_UINT32 passno; + OPJ_FLOAT64 min, max; + OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */ + const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */ + OPJ_FLOAT64 maxSE = 0; + + opj_cp_t *cp = tcd->cp; + opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles; + opj_tcp_t *tcd_tcp = tcd->tcp; + + min = DBL_MAX; + max = 0; + + tcd_tile->numpix = 0; /* fixed_quality */ + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + tilec->numpix = 0; + + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + + /* Skip empty bands */ + if (opj_tcd_is_band_empty(band)) { + continue; + } + + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno]; + + for (passno = 0; passno < cblk->totalpasses; passno++) { + opj_tcd_pass_t *pass = &cblk->passes[passno]; + OPJ_INT32 dr; + OPJ_FLOAT64 dd, rdslope; + + if (passno == 0) { + dr = (OPJ_INT32)pass->rate; + dd = pass->distortiondec; + } else { + dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate); + dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec; + } + + if (dr == 0) { + continue; + } + + rdslope = dd / dr; + if (rdslope < min) { + min = rdslope; + } + + if (rdslope > max) { + max = rdslope; + } + } /* passno */ + + /* fixed_quality */ + tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); + tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); + } /* cbklno */ + } /* precno */ + } /* bandno */ + } /* resno */ + + maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0) + * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)) + * ((OPJ_FLOAT64)(tilec->numpix)); + } /* compno */ + + /* index file */ + if (cstr_info) { + opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno]; + tile_info->numpix = tcd_tile->numpix; + tile_info->distotile = tcd_tile->distotile; + tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof( + OPJ_FLOAT64)); + if (!tile_info->thresh) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + } + + for (layno = 0; layno < tcd_tcp->numlayers; layno++) { + OPJ_FLOAT64 lo = min; + OPJ_FLOAT64 hi = max; + OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min((( + OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len; + OPJ_FLOAT64 goodthresh = 0; + OPJ_FLOAT64 stable_thresh = 0; + OPJ_UINT32 i; + OPJ_FLOAT64 distotarget; /* fixed_quality */ + + /* fixed_quality */ + distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, + tcd_tcp->distoratio[layno] / 10)); + + /* Don't try to find an optimal threshold but rather take everything not included yet, if + -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0) + -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0) + ==> possible to have some lossy layers and the last layer for sure lossless */ + if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) && + (tcd_tcp->rates[layno] > 0.0f)) || + ((cp->m_specific_param.m_enc.m_fixed_quality == 1) && + (tcd_tcp->distoratio[layno] > 0.0))) { + opj_t2_t*t2 = opj_t2_create(tcd->image, cp); + OPJ_FLOAT64 thresh = 0; + + if (t2 == 00) { + return OPJ_FALSE; + } + + for (i = 0; i < 128; ++i) { + OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */ + + thresh = (lo + hi) / 2; + + opj_tcd_makelayer(tcd, layno, thresh, 0); + + if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */ + if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) { + if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, + p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos, + tcd->cur_pino, + THRESH_CALC, p_manager)) { + + lo = thresh; + continue; + } else { + distoachieved = layno == 0 ? + tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; + + if (distoachieved < distotarget) { + hi = thresh; + stable_thresh = thresh; + continue; + } else { + lo = thresh; + } + } + } else { + distoachieved = (layno == 0) ? + tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); + + if (distoachieved < distotarget) { + hi = thresh; + stable_thresh = thresh; + continue; + } + lo = thresh; + } + } else { + if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, + p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos, + tcd->cur_pino, + THRESH_CALC, p_manager)) { + /* TODO: what to do with l ??? seek / tell ??? */ + /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ + lo = thresh; + continue; + } + + hi = thresh; + stable_thresh = thresh; + } + } + + goodthresh = stable_thresh == 0 ? thresh : stable_thresh; + + opj_t2_destroy(t2); + } else { + /* Special value to indicate to use all passes */ + goodthresh = -1; + } + + if (cstr_info) { /* Threshold for Marcela Index */ + cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh; + } + + opj_tcd_makelayer(tcd, layno, goodthresh, 1); + + /* fixed_quality */ + cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : + (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd, + opj_image_t * p_image, + opj_cp_t * p_cp, + opj_thread_pool_t* p_tp) +{ + p_tcd->image = p_image; + p_tcd->cp = p_cp; + + p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1, + sizeof(opj_tcd_tile_t)); + if (! p_tcd->tcd_image->tiles) { + return OPJ_FALSE; + } + + p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc( + p_image->numcomps, sizeof(opj_tcd_tilecomp_t)); + if (! p_tcd->tcd_image->tiles->comps) { + return OPJ_FALSE; + } + + p_tcd->tcd_image->tiles->numcomps = p_image->numcomps; + p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos; + p_tcd->thread_pool = p_tp; + + return OPJ_TRUE; +} + +/** +Destroy a previously created TCD handle +*/ +void opj_tcd_destroy(opj_tcd_t *tcd) +{ + if (tcd) { + opj_tcd_free_tile(tcd); + + if (tcd->tcd_image) { + opj_free(tcd->tcd_image); + tcd->tcd_image = 00; + } + + opj_free(tcd->used_component); + + opj_free(tcd); + } +} + +OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec) +{ + if ((l_tilec->data == 00) || + ((l_tilec->data_size_needed > l_tilec->data_size) && + (l_tilec->ownsData == OPJ_FALSE))) { + l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed); + if (!l_tilec->data && l_tilec->data_size_needed != 0) { + return OPJ_FALSE; + } + /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/ + l_tilec->data_size = l_tilec->data_size_needed; + l_tilec->ownsData = OPJ_TRUE; + } else if (l_tilec->data_size_needed > l_tilec->data_size) { + /* We don't need to keep old data */ + opj_image_data_free(l_tilec->data); + l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed); + if (! l_tilec->data) { + l_tilec->data_size = 0; + l_tilec->data_size_needed = 0; + l_tilec->ownsData = OPJ_FALSE; + return OPJ_FALSE; + } + /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/ + l_tilec->data_size = l_tilec->data_size_needed; + l_tilec->ownsData = OPJ_TRUE; + } + return OPJ_TRUE; +} + +/* ----------------------------------------------------------------------- */ + +static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, + OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block, + opj_event_mgr_t* manager) +{ + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + opj_tcp_t * l_tcp = 00; + opj_cp_t * l_cp = 00; + opj_tcd_tile_t * l_tile = 00; + opj_tccp_t *l_tccp = 00; + opj_tcd_tilecomp_t *l_tilec = 00; + opj_image_comp_t * l_image_comp = 00; + opj_tcd_resolution_t *l_res = 00; + opj_tcd_band_t *l_band = 00; + opj_stepsize_t * l_step_size = 00; + opj_tcd_precinct_t *l_current_precinct = 00; + opj_image_t *l_image = 00; + OPJ_UINT32 p, q; + OPJ_UINT32 l_level_no; + OPJ_UINT32 l_pdx, l_pdy; + OPJ_INT32 l_x0b, l_y0b; + OPJ_UINT32 l_tx0, l_ty0; + /* extent of precincts , top left, bottom right**/ + OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; + /* number of precinct for a resolution */ + OPJ_UINT32 l_nb_precincts; + /* room needed to store l_nb_precinct precinct for a resolution */ + OPJ_UINT32 l_nb_precinct_size; + /* number of code blocks for a precinct*/ + OPJ_UINT32 l_nb_code_blocks; + /* room needed to store l_nb_code_blocks code blocks for a precinct*/ + OPJ_UINT32 l_nb_code_blocks_size; + /* size of data for a tile */ + OPJ_UINT32 l_data_size; + + l_cp = p_tcd->cp; + l_tcp = &(l_cp->tcps[p_tile_no]); + l_tile = p_tcd->tcd_image->tiles; + l_tccp = l_tcp->tccps; + l_tilec = l_tile->comps; + l_image = p_tcd->image; + l_image_comp = p_tcd->image->comps; + + p = p_tile_no % l_cp->tw; /* tile coordinates */ + q = p_tile_no / l_cp->tw; + /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ + + /* 4 borders of the tile rescale on the image if necessary */ + l_tx0 = l_cp->tx0 + p * + l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */ + l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0); + l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx), + l_image->x1); + /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */ + if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) { + opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n"); + return OPJ_FALSE; + } + l_ty0 = l_cp->ty0 + q * + l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */ + l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0); + l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy), + l_image->y1); + /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */ + if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) { + opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n"); + return OPJ_FALSE; + } + + + /* testcase 1888.pdf.asan.35.988 */ + if (l_tccp->numresolutions == 0) { + opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n"); + return OPJ_FALSE; + } + /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ + + /*tile->numcomps = image->numcomps; */ + for (compno = 0; compno < l_tile->numcomps; ++compno) { + /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ + l_image_comp->resno_decoded = 0; + /* border of each l_tile component (global) */ + l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx); + l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy); + l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx); + l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy); + l_tilec->compno = compno; + /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ + + l_tilec->numresolutions = l_tccp->numresolutions; + if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { + l_tilec->minimum_num_resolutions = 1; + } else { + l_tilec->minimum_num_resolutions = l_tccp->numresolutions - + l_cp->m_specific_param.m_dec.m_reduce; + } + + if (isEncoder) { + OPJ_SIZE_T l_tile_data_size; + + /* compute l_data_size with overflow check */ + OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0); + OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0); + + /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */ + if (h > 0 && w > SIZE_MAX / h) { + opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_tile_data_size = w * h; + + if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) { + opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32); + + l_tilec->data_size_needed = l_tile_data_size; + } + + l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof( + opj_tcd_resolution_t); + + opj_image_data_free(l_tilec->data_win); + l_tilec->data_win = NULL; + l_tilec->win_x0 = 0; + l_tilec->win_y0 = 0; + l_tilec->win_x1 = 0; + l_tilec->win_y1 = 0; + + if (l_tilec->resolutions == 00) { + l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); + if (! l_tilec->resolutions) { + return OPJ_FALSE; + } + /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/ + l_tilec->resolutions_size = l_data_size; + memset(l_tilec->resolutions, 0, l_data_size); + } else if (l_data_size > l_tilec->resolutions_size) { + opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc( + l_tilec->resolutions, l_data_size); + if (! new_resolutions) { + opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n"); + opj_free(l_tilec->resolutions); + l_tilec->resolutions = NULL; + l_tilec->resolutions_size = 0; + return OPJ_FALSE; + } + l_tilec->resolutions = new_resolutions; + /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ + memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0, + l_data_size - l_tilec->resolutions_size); + l_tilec->resolutions_size = l_data_size; + } + + l_level_no = l_tilec->numresolutions; + l_res = l_tilec->resolutions; + l_step_size = l_tccp->stepsizes; + /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ + + for (resno = 0; resno < l_tilec->numresolutions; ++resno) { + /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ + OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/; + OPJ_UINT32 cbgwidthexpn, cbgheightexpn; + OPJ_UINT32 cblkwidthexpn, cblkheightexpn; + + --l_level_no; + + /* border for each resolution level (global) */ + l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); + l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); + l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); + l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); + + /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/ + /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ + l_pdx = l_tccp->prcw[resno]; + l_pdy = l_tccp->prch[resno]; + /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ + /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx; + l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy; + { + OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->x1, + (OPJ_INT32)l_pdx)) << l_pdx; + if (tmp > (OPJ_UINT32)INT_MAX) { + opj_event_msg(manager, EVT_ERROR, "Integer overflow\n"); + return OPJ_FALSE; + } + l_br_prc_x_end = (OPJ_INT32)tmp; + } + { + OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->y1, + (OPJ_INT32)l_pdy)) << l_pdy; + if (tmp > (OPJ_UINT32)INT_MAX) { + opj_event_msg(manager, EVT_ERROR, "Integer overflow\n"); + return OPJ_FALSE; + } + l_br_prc_y_end = (OPJ_INT32)tmp; + } + /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/ + + l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)(( + l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); + l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)(( + l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); + /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ + + if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) { + opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_nb_precincts = l_res->pw * l_res->ph; + + if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) < + l_nb_precincts) { + opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t); + + if (resno == 0) { + tlcbgxstart = l_tl_prc_x_start; + tlcbgystart = l_tl_prc_y_start; + /*brcbgxend = l_br_prc_x_end;*/ + /* brcbgyend = l_br_prc_y_end;*/ + cbgwidthexpn = l_pdx; + cbgheightexpn = l_pdy; + l_res->numbands = 1; + } else { + tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1); + tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1); + /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/ + /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/ + cbgwidthexpn = l_pdx - 1; + cbgheightexpn = l_pdy - 1; + l_res->numbands = 3; + } + + cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn); + cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn); + l_band = l_res->bands; + + for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) { + /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ + + if (resno == 0) { + l_band->bandno = 0 ; + l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); + l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); + l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); + l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); + } else { + l_band->bandno = bandno + 1; + /* x0b = 1 if bandno = 1 or 3 */ + l_x0b = l_band->bandno & 1; + /* y0b = 1 if bandno = 2 or 3 */ + l_y0b = (OPJ_INT32)((l_band->bandno) >> 1); + /* l_band border (global) */ + l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b << + l_level_no), (OPJ_INT32)(l_level_no + 1)); + l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b << + l_level_no), (OPJ_INT32)(l_level_no + 1)); + l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b << + l_level_no), (OPJ_INT32)(l_level_no + 1)); + l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b << + l_level_no), (OPJ_INT32)(l_level_no + 1)); + } + + if (isEncoder) { + /* Skip empty bands */ + if (opj_tcd_is_band_empty(l_band)) { + /* Do not zero l_band->precints to avoid leaks */ + /* but make sure we don't use it later, since */ + /* it will point to precincts of previous bands... */ + continue; + } + } + + { + /* Table E-1 - Sub-band gains */ + /* BUG_WEIRD_TWO_INVK (look for this identifier in dwt.c): */ + /* the test (!isEncoder && l_tccp->qmfbid == 0) is strongly */ + /* linked to the use of two_invK instead of invK */ + const OPJ_INT32 log2_gain = (!isEncoder && + l_tccp->qmfbid == 0) ? 0 : (l_band->bandno == 0) ? 0 : + (l_band->bandno == 3) ? 2 : 1; + + /* Nominal dynamic range. Equation E-4 */ + const OPJ_INT32 Rb = (OPJ_INT32)l_image_comp->prec + log2_gain; + + /* Delta_b value of Equation E-3 in "E.1 Inverse quantization + * procedure" of the standard */ + l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, + (OPJ_INT32)(Rb - l_step_size->expn)))); + } + + /* Mb value of Equation E-2 in "E.1 Inverse quantization + * procedure" of the standard */ + l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - + 1; + + if (!l_band->precincts && (l_nb_precincts > 0U)) { + l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */ + l_nb_precinct_size); + if (! l_band->precincts) { + opj_event_msg(manager, EVT_ERROR, + "Not enough memory to handle band precints\n"); + return OPJ_FALSE; + } + /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */ + memset(l_band->precincts, 0, l_nb_precinct_size); + l_band->precincts_data_size = l_nb_precinct_size; + } else if (l_band->precincts_data_size < l_nb_precinct_size) { + + opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc( + l_band->precincts,/*3 * */ l_nb_precinct_size); + if (! new_precincts) { + opj_event_msg(manager, EVT_ERROR, + "Not enough memory to handle band precints\n"); + opj_free(l_band->precincts); + l_band->precincts = NULL; + l_band->precincts_data_size = 0; + return OPJ_FALSE; + } + l_band->precincts = new_precincts; + /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/ + memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0, + l_nb_precinct_size - l_band->precincts_data_size); + l_band->precincts_data_size = l_nb_precinct_size; + } + + l_current_precinct = l_band->precincts; + for (precno = 0; precno < l_nb_precincts; ++precno) { + OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; + OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * + (1 << cbgwidthexpn); + OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * + (1 << cbgheightexpn); + OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); + OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); + /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ + /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ + + /* precinct size (global) */ + /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ + + l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0); + l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0); + l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1); + l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1); + /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/ + + tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, + (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; + /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ + tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, + (OPJ_INT32)cblkheightexpn) << cblkheightexpn; + /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ + brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, + (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; + /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ + brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, + (OPJ_INT32)cblkheightexpn) << cblkheightexpn; + /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ + l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> + cblkwidthexpn); + l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> + cblkheightexpn); + + l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; + /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ + if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) < + l_nb_code_blocks) { + opj_event_msg(manager, EVT_ERROR, + "Size of code block data exceeds system limits\n"); + return OPJ_FALSE; + } + l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block; + + if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) { + l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size); + if (! l_current_precinct->cblks.blocks) { + return OPJ_FALSE; + } + /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/ + + memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size); + + l_current_precinct->block_size = l_nb_code_blocks_size; + } else if (l_nb_code_blocks_size > l_current_precinct->block_size) { + void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks, + l_nb_code_blocks_size); + if (! new_blocks) { + opj_free(l_current_precinct->cblks.blocks); + l_current_precinct->cblks.blocks = NULL; + l_current_precinct->block_size = 0; + opj_event_msg(manager, EVT_ERROR, + "Not enough memory for current precinct codeblock element\n"); + return OPJ_FALSE; + } + l_current_precinct->cblks.blocks = new_blocks; + /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */ + + memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) + + l_current_precinct->block_size + , 0 + , l_nb_code_blocks_size - l_current_precinct->block_size); + + l_current_precinct->block_size = l_nb_code_blocks_size; + } + + if (! l_current_precinct->incltree) { + l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, + l_current_precinct->ch, manager); + } else { + l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, + l_current_precinct->cw, l_current_precinct->ch, manager); + } + + if (! l_current_precinct->imsbtree) { + l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw, + l_current_precinct->ch, manager); + } else { + l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree, + l_current_precinct->cw, l_current_precinct->ch, manager); + } + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % + l_current_precinct->cw) * (1 << cblkwidthexpn); + OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / + l_current_precinct->cw) * (1 << cblkheightexpn); + OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); + OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); + + if (isEncoder) { + opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno; + + if (! opj_tcd_code_block_enc_allocate(l_code_block)) { + return OPJ_FALSE; + } + /* code-block size (global) */ + l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); + l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); + l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); + l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); + + if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) { + return OPJ_FALSE; + } + } else { + opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno; + + if (! opj_tcd_code_block_dec_allocate(l_code_block)) { + return OPJ_FALSE; + } + /* code-block size (global) */ + l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); + l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); + l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); + l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); + } + } + ++l_current_precinct; + } /* precno */ + } /* bandno */ + ++l_res; + } /* resno */ + ++l_tccp; + ++l_tilec; + ++l_image_comp; + } /* compno */ + return OPJ_TRUE; +} + +OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, + opj_event_mgr_t* p_manager) +{ + return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, + sizeof(opj_tcd_cblk_enc_t), p_manager); +} + +OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, + opj_event_mgr_t* p_manager) +{ + return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, + sizeof(opj_tcd_cblk_dec_t), p_manager); +} + +/** + * Allocates memory for an encoding code block (but not data memory). + */ +static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t * + p_code_block) +{ + if (! p_code_block->layers) { + /* no memset since data */ + p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, + sizeof(opj_tcd_layer_t)); + if (! p_code_block->layers) { + return OPJ_FALSE; + } + } + if (! p_code_block->passes) { + p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, + sizeof(opj_tcd_pass_t)); + if (! p_code_block->passes) { + return OPJ_FALSE; + } + } + return OPJ_TRUE; +} + +/** + * Allocates data memory for an encoding code block. + */ +static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t * + p_code_block) +{ + OPJ_UINT32 l_data_size; + + /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */ + /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */ + /* and +7 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 3) */ + /* and +26 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 7) */ + /* and +28 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 44) */ + /* and +33 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4) */ + /* and +63 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -IMF 2K) */ + /* and +74 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -n 8 -s 7,7 -I) */ + /* TODO: is there a theoretical upper-bound for the compressed code */ + /* block size ? */ + l_data_size = 74 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) * + (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32)); + + if (l_data_size > p_code_block->data_size) { + if (p_code_block->data) { + /* We refer to data - 1 since below we incremented it */ + opj_free(p_code_block->data - 1); + } + p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1); + if (! p_code_block->data) { + p_code_block->data_size = 0U; + return OPJ_FALSE; + } + p_code_block->data_size = l_data_size; + + /* We reserve the initial byte as a fake byte to a non-FF value */ + /* and increment the data pointer, so that opj_mqc_init_enc() */ + /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */ + /* it. */ + p_code_block->data[0] = 0; + p_code_block->data += 1; /*why +1 ?*/ + } + return OPJ_TRUE; +} + + +void opj_tcd_reinit_segment(opj_tcd_seg_t* seg) +{ + memset(seg, 0, sizeof(opj_tcd_seg_t)); +} + +/** + * Allocates memory for a decoding code block. + */ +static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t * + p_code_block) +{ + if (! p_code_block->segs) { + + p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS, + sizeof(opj_tcd_seg_t)); + if (! p_code_block->segs) { + return OPJ_FALSE; + } + /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/ + + p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS; + /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/ + } else { + /* sanitize */ + opj_tcd_seg_t * l_segs = p_code_block->segs; + OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs; + opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks; + OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc; + OPJ_UINT32 i; + + opj_aligned_free(p_code_block->decoded_data); + p_code_block->decoded_data = 00; + + memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t)); + p_code_block->segs = l_segs; + p_code_block->m_current_max_segs = l_current_max_segs; + for (i = 0; i < l_current_max_segs; ++i) { + opj_tcd_reinit_segment(&l_segs[i]); + } + p_code_block->chunks = l_chunks; + p_code_block->numchunksalloc = l_numchunksalloc; + } + + return OPJ_TRUE; +} + +OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd, + OPJ_BOOL take_into_account_partial_decoding) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tile_comp = 00; + opj_tcd_resolution_t * l_res = 00; + OPJ_UINT32 l_size_comp, l_remaining; + OPJ_UINT32 l_temp; + + l_tile_comp = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + + for (i = 0; i < p_tcd->image->numcomps; ++i) { + OPJ_UINT32 w, h; + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1; + if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) { + w = l_res->win_x1 - l_res->win_x0; + h = l_res->win_y1 - l_res->win_y0; + } else { + w = (OPJ_UINT32)(l_res->x1 - l_res->x0); + h = (OPJ_UINT32)(l_res->y1 - l_res->y0); + } + if (h > 0 && UINT_MAX / w < h) { + return UINT_MAX; + } + l_temp = w * h; + if (l_size_comp && UINT_MAX / l_size_comp < l_temp) { + return UINT_MAX; + } + l_temp *= l_size_comp; + + if (l_temp > UINT_MAX - l_data_size) { + return UINT_MAX; + } + l_data_size += l_temp; + ++l_img_comp; + ++l_tile_comp; + } + + return l_data_size; +} + +OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd, + OPJ_UINT32 p_tile_no, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_length, + opj_codestream_info_t *p_cstr_info, + opj_tcd_marker_info_t* p_marker_info, + opj_event_mgr_t *p_manager) +{ + + if (p_tcd->cur_tp_num == 0) { + + p_tcd->tcd_tileno = p_tile_no; + p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no]; + + /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ + if (p_cstr_info) { + OPJ_UINT32 l_num_packs = 0; + OPJ_UINT32 i; + opj_tcd_tilecomp_t *l_tilec_idx = + &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */ + opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */ + + for (i = 0; i < l_tilec_idx->numresolutions; i++) { + opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i]; + + p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw; + p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph; + + l_num_packs += l_res_idx->pw * l_res_idx->ph; + p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i]; + p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i]; + } + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(( + OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers * + l_num_packs, + sizeof(opj_packet_info_t)); + if (!p_cstr_info->tile[p_tile_no].packet) { + /* FIXME event manager error callback */ + return OPJ_FALSE; + } + } + /* << INDEX */ + + /* FIXME _ProfStart(PGROUP_DC_SHIFT); */ + /*---------------TILE-------------------*/ + if (! opj_tcd_dc_level_shift_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DC_SHIFT); */ + + /* FIXME _ProfStart(PGROUP_MCT); */ + if (! opj_tcd_mct_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_MCT); */ + + /* FIXME _ProfStart(PGROUP_DWT); */ + if (! opj_tcd_dwt_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DWT); */ + + /* FIXME _ProfStart(PGROUP_T1); */ + if (! opj_tcd_t1_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T1); */ + + /* FIXME _ProfStart(PGROUP_RATE); */ + if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, + p_cstr_info, p_manager)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_RATE); */ + + } + /*--------------TIER2------------------*/ + + /* INDEX */ + if (p_cstr_info) { + p_cstr_info->index_write = 1; + } + /* FIXME _ProfStart(PGROUP_T2); */ + + if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length, + p_cstr_info, p_marker_info, p_manager)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T2); */ + + /*---------------CLEAN-------------------*/ + + return OPJ_TRUE; +} + +OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd, + OPJ_UINT32 win_x0, + OPJ_UINT32 win_y0, + OPJ_UINT32 win_x1, + OPJ_UINT32 win_y1, + OPJ_UINT32 numcomps_to_decode, + const OPJ_UINT32 *comps_indices, + OPJ_BYTE *p_src, + OPJ_UINT32 p_max_length, + OPJ_UINT32 p_tile_no, + opj_codestream_index_t *p_cstr_index, + opj_event_mgr_t *p_manager + ) +{ + OPJ_UINT32 l_data_read; + OPJ_UINT32 compno; + + p_tcd->tcd_tileno = p_tile_no; + p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]); + p_tcd->win_x0 = win_x0; + p_tcd->win_y0 = win_y0; + p_tcd->win_x1 = win_x1; + p_tcd->win_y1 = win_y1; + p_tcd->whole_tile_decoding = OPJ_TRUE; + + opj_free(p_tcd->used_component); + p_tcd->used_component = NULL; + + if (numcomps_to_decode) { + OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL), + p_tcd->image->numcomps); + if (used_component == NULL) { + return OPJ_FALSE; + } + for (compno = 0; compno < numcomps_to_decode; compno++) { + used_component[ comps_indices[compno] ] = OPJ_TRUE; + } + + p_tcd->used_component = used_component; + } + + for (compno = 0; compno < p_tcd->image->numcomps; compno++) { + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) { + p_tcd->whole_tile_decoding = OPJ_FALSE; + break; + } + } + + if (p_tcd->whole_tile_decoding) { + for (compno = 0; compno < p_tcd->image->numcomps; compno++) { + opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]); + opj_tcd_resolution_t *l_res = & + (tilec->resolutions[tilec->minimum_num_resolutions - 1]); + OPJ_SIZE_T l_data_size; + + /* compute l_data_size with overflow check */ + OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0); + OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0); + + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */ + if (res_h > 0 && res_w > SIZE_MAX / res_h) { + opj_event_msg(p_manager, EVT_ERROR, + "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_data_size = res_w * res_h; + + if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) { + opj_event_msg(p_manager, EVT_ERROR, + "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_data_size *= sizeof(OPJ_UINT32); + + tilec->data_size_needed = l_data_size; + + if (!opj_alloc_tile_component_data(tilec)) { + opj_event_msg(p_manager, EVT_ERROR, + "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + } + } else { + /* Compute restricted tile-component and tile-resolution coordinates */ + /* of the window of interest, but defer the memory allocation until */ + /* we know the resno_decoded */ + for (compno = 0; compno < p_tcd->image->numcomps; compno++) { + OPJ_UINT32 resno; + opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]); + opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]); + + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + /* Compute the intersection of the area of interest, expressed in tile coordinates */ + /* with the tile coordinates */ + tilec->win_x0 = opj_uint_max( + (OPJ_UINT32)tilec->x0, + opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx)); + tilec->win_y0 = opj_uint_max( + (OPJ_UINT32)tilec->y0, + opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy)); + tilec->win_x1 = opj_uint_min( + (OPJ_UINT32)tilec->x1, + opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx)); + tilec->win_y1 = opj_uint_min( + (OPJ_UINT32)tilec->y1, + opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy)); + if (tilec->win_x1 < tilec->win_x0 || + tilec->win_y1 < tilec->win_y0) { + /* We should not normally go there. The circumstance is when */ + /* the tile coordinates do not intersect the area of interest */ + /* Upper level logic should not even try to decode that tile */ + opj_event_msg(p_manager, EVT_ERROR, + "Invalid tilec->win_xxx values\n"); + return OPJ_FALSE; + } + + for (resno = 0; resno < tilec->numresolutions; ++resno) { + opj_tcd_resolution_t *res = tilec->resolutions + resno; + res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0, + tilec->numresolutions - 1 - resno); + res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0, + tilec->numresolutions - 1 - resno); + res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1, + tilec->numresolutions - 1 - resno); + res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1, + tilec->numresolutions - 1 - resno); + } + } + } + +#ifdef TODO_MSD /* FIXME */ + /* INDEX >> */ + if (p_cstr_info) { + OPJ_UINT32 resno, compno, numprec = 0; + for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) { + opj_tcp_t *tcp = &p_tcd->cp->tcps[0]; + opj_tccp_t *tccp = &tcp->tccps[compno]; + opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno]; + for (resno = 0; resno < tilec_idx->numresolutions; resno++) { + opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno]; + p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw; + p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph; + numprec += res_idx->pw * res_idx->ph; + p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno]; + p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno]; + } + } + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc( + p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t)); + p_cstr_info->packno = 0; + } + /* << INDEX */ +#endif + + /*--------------TIER2------------------*/ + /* FIXME _ProfStart(PGROUP_T2); */ + l_data_read = 0; + if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index, + p_manager)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T2); */ + + /*------------------TIER1-----------------*/ + + /* FIXME _ProfStart(PGROUP_T1); */ + if (! opj_tcd_t1_decode(p_tcd, p_manager)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T1); */ + + + /* For subtile decoding, now we know the resno_decoded, we can allocate */ + /* the tile data buffer */ + if (!p_tcd->whole_tile_decoding) { + for (compno = 0; compno < p_tcd->image->numcomps; compno++) { + opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]); + opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]); + opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded; + OPJ_SIZE_T w = res->win_x1 - res->win_x0; + OPJ_SIZE_T h = res->win_y1 - res->win_y0; + OPJ_SIZE_T l_data_size; + + opj_image_data_free(tilec->data_win); + tilec->data_win = NULL; + + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + if (w > 0 && h > 0) { + if (w > SIZE_MAX / h) { + opj_event_msg(p_manager, EVT_ERROR, + "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_data_size = w * h; + if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) { + opj_event_msg(p_manager, EVT_ERROR, + "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + l_data_size *= sizeof(OPJ_INT32); + + tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size); + if (tilec->data_win == NULL) { + opj_event_msg(p_manager, EVT_ERROR, + "Size of tile data exceeds system limits\n"); + return OPJ_FALSE; + } + } + } + } + + /*----------------DWT---------------------*/ + + /* FIXME _ProfStart(PGROUP_DWT); */ + if + (! opj_tcd_dwt_decode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DWT); */ + + /*----------------MCT-------------------*/ + /* FIXME _ProfStart(PGROUP_MCT); */ + if + (! opj_tcd_mct_decode(p_tcd, p_manager)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_MCT); */ + + /* FIXME _ProfStart(PGROUP_DC_SHIFT); */ + if + (! opj_tcd_dc_level_shift_decode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DC_SHIFT); */ + + + /*---------------TILE-------------------*/ + return OPJ_TRUE; +} + +OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest, + OPJ_UINT32 p_dest_length + ) +{ + OPJ_UINT32 i, j, k, l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + opj_tcd_resolution_t * l_res; + OPJ_UINT32 l_size_comp, l_remaining; + OPJ_UINT32 l_stride, l_width, l_height; + + l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE); + if (l_data_size == UINT_MAX || l_data_size > p_dest_length) { + return OPJ_FALSE; + } + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + + for (i = 0; i < p_tcd->image->numcomps; ++i) { + const OPJ_INT32* l_src_data; + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_res = l_tilec->resolutions + l_img_comp->resno_decoded; + if (p_tcd->whole_tile_decoding) { + l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0); + l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0); + l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions - + 1].x1 - + l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width; + l_src_data = l_tilec->data; + } else { + l_width = l_res->win_x1 - l_res->win_x0; + l_height = l_res->win_y1 - l_res->win_y0; + l_stride = 0; + l_src_data = l_tilec->data_win; + } + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + switch (l_size_comp) { + case 1: { + OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest; + const OPJ_INT32 * l_src_ptr = l_src_data; + + if (l_img_comp->sgnd) { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++)); + } + l_src_ptr += l_stride; + } + } else { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff); + } + l_src_ptr += l_stride; + } + } + + p_dest = (OPJ_BYTE *)l_dest_ptr; + } + break; + case 2: { + const OPJ_INT32 * l_src_ptr = l_src_data; + OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest; + + if (l_img_comp->sgnd) { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++)); + memcpy(l_dest_ptr, &val, sizeof(val)); + l_dest_ptr ++; + } + l_src_ptr += l_stride; + } + } else { + for (j = 0; j < l_height; ++j) { + for (k = 0; k < l_width; ++k) { + OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff); + memcpy(l_dest_ptr, &val, sizeof(val)); + l_dest_ptr ++; + } + l_src_ptr += l_stride; + } + } + + p_dest = (OPJ_BYTE*) l_dest_ptr; + } + break; + case 4: { + OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest; + const OPJ_INT32 * l_src_ptr = l_src_data; + + for (j = 0; j < l_height; ++j) { + memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32)); + l_dest_ptr += l_width; + l_src_ptr += l_width + l_stride; + } + + p_dest = (OPJ_BYTE*) l_dest_ptr; + } + break; + } + + ++l_img_comp; + ++l_tilec; + } + + return OPJ_TRUE; +} + + + + +static void opj_tcd_free_tile(opj_tcd_t *p_tcd) +{ + OPJ_UINT32 compno, resno, bandno, precno; + opj_tcd_tile_t *l_tile = 00; + opj_tcd_tilecomp_t *l_tile_comp = 00; + opj_tcd_resolution_t *l_res = 00; + opj_tcd_band_t *l_band = 00; + opj_tcd_precinct_t *l_precinct = 00; + OPJ_UINT32 l_nb_resolutions, l_nb_precincts; + void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00; + + if (! p_tcd) { + return; + } + + if (! p_tcd->tcd_image) { + return; + } + + if (p_tcd->m_is_decoder) { + l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate; + } else { + l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate; + } + + l_tile = p_tcd->tcd_image->tiles; + if (! l_tile) { + return; + } + + l_tile_comp = l_tile->comps; + + for (compno = 0; compno < l_tile->numcomps; ++compno) { + l_res = l_tile_comp->resolutions; + if (l_res) { + + l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof( + opj_tcd_resolution_t); + for (resno = 0; resno < l_nb_resolutions; ++resno) { + l_band = l_res->bands; + for (bandno = 0; bandno < 3; ++bandno) { + l_precinct = l_band->precincts; + if (l_precinct) { + + l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof( + opj_tcd_precinct_t); + for (precno = 0; precno < l_nb_precincts; ++precno) { + opj_tgt_destroy(l_precinct->incltree); + l_precinct->incltree = 00; + opj_tgt_destroy(l_precinct->imsbtree); + l_precinct->imsbtree = 00; + (*l_tcd_code_block_deallocate)(l_precinct); + ++l_precinct; + } + + opj_free(l_band->precincts); + l_band->precincts = 00; + } + ++l_band; + } /* for (resno */ + ++l_res; + } + + opj_free(l_tile_comp->resolutions); + l_tile_comp->resolutions = 00; + } + + if (l_tile_comp->ownsData && l_tile_comp->data) { + opj_image_data_free(l_tile_comp->data); + l_tile_comp->data = 00; + l_tile_comp->ownsData = 0; + l_tile_comp->data_size = 0; + l_tile_comp->data_size_needed = 0; + } + + opj_image_data_free(l_tile_comp->data_win); + + ++l_tile_comp; + } + + opj_free(l_tile->comps); + l_tile->comps = 00; + opj_free(p_tcd->tcd_image->tiles); + p_tcd->tcd_image->tiles = 00; +} + + +static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd, + OPJ_BYTE * p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_src_size, + opj_codestream_index_t *p_cstr_index, + opj_event_mgr_t *p_manager + ) +{ + opj_t2_t * l_t2; + + l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp); + if (l_t2 == 00) { + return OPJ_FALSE; + } + + if (! opj_t2_decode_packets( + p_tcd, + l_t2, + p_tcd->tcd_tileno, + p_tcd->tcd_image->tiles, + p_src_data, + p_data_read, + p_max_src_size, + p_cstr_index, + p_manager)) { + opj_t2_destroy(l_t2); + return OPJ_FALSE; + } + + opj_t2_destroy(l_t2); + + /*---------------CLEAN-------------------*/ + return OPJ_TRUE; +} + +static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager) +{ + OPJ_UINT32 compno; + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + volatile OPJ_BOOL ret = OPJ_TRUE; + OPJ_BOOL check_pterm = OPJ_FALSE; + opj_mutex_t* p_manager_mutex = NULL; + + p_manager_mutex = opj_mutex_create(); + + /* Only enable PTERM check if we decode all layers */ + if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers && + (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) { + check_pterm = OPJ_TRUE; + } + + for (compno = 0; compno < l_tile->numcomps; + ++compno, ++l_tile_comp, ++l_tccp) { + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp, + p_manager, p_manager_mutex, check_pterm); + if (!ret) { + break; + } + } + + opj_thread_pool_wait_completion(p_tcd->thread_pool, 0); + if (p_manager_mutex) { + opj_mutex_destroy(p_manager_mutex); + } + return ret; +} + + +static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd) +{ + OPJ_UINT32 compno; + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + opj_image_comp_t * l_img_comp = p_tcd->image->comps; + + for (compno = 0; compno < l_tile->numcomps; + compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) { + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + if (l_tccp->qmfbid == 1) { + if (! opj_dwt_decode(p_tcd, l_tile_comp, + l_img_comp->resno_decoded + 1)) { + return OPJ_FALSE; + } + } else { + if (! opj_dwt_decode_real(p_tcd, l_tile_comp, + l_img_comp->resno_decoded + 1)) { + return OPJ_FALSE; + } + } + + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager) +{ + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcp_t * l_tcp = p_tcd->tcp; + opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps; + OPJ_SIZE_T l_samples; + OPJ_UINT32 i; + + if (l_tcp->mct == 0 || p_tcd->used_component != NULL) { + return OPJ_TRUE; + } + + if (p_tcd->whole_tile_decoding) { + opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions + + l_tile_comp->minimum_num_resolutions - 1; + + /* A bit inefficient: we process more data than needed if */ + /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */ + /* but we would need to take into account a stride then */ + l_samples = (OPJ_SIZE_T)(res_comp0->x1 - res_comp0->x0) * + (OPJ_SIZE_T)(res_comp0->y1 - res_comp0->y0); + if (l_tile->numcomps >= 3) { + if (l_tile_comp->minimum_num_resolutions != + l_tile->comps[1].minimum_num_resolutions || + l_tile_comp->minimum_num_resolutions != + l_tile->comps[2].minimum_num_resolutions) { + opj_event_msg(p_manager, EVT_ERROR, + "Tiles don't all have the same dimension. Skip the MCT step.\n"); + return OPJ_FALSE; + } + } + if (l_tile->numcomps >= 3) { + opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions + + l_tile_comp->minimum_num_resolutions - 1; + opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions + + l_tile_comp->minimum_num_resolutions - 1; + /* testcase 1336.pdf.asan.47.376 */ + if (p_tcd->image->comps[0].resno_decoded != + p_tcd->image->comps[1].resno_decoded || + p_tcd->image->comps[0].resno_decoded != + p_tcd->image->comps[2].resno_decoded || + (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) * + (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples || + (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) * + (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) { + opj_event_msg(p_manager, EVT_ERROR, + "Tiles don't all have the same dimension. Skip the MCT step.\n"); + return OPJ_FALSE; + } + } + } else { + opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions + + p_tcd->image->comps[0].resno_decoded; + + l_samples = (OPJ_SIZE_T)(res_comp0->win_x1 - res_comp0->win_x0) * + (OPJ_SIZE_T)(res_comp0->win_y1 - res_comp0->win_y0); + if (l_tile->numcomps >= 3) { + opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions + + p_tcd->image->comps[1].resno_decoded; + opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions + + p_tcd->image->comps[2].resno_decoded; + /* testcase 1336.pdf.asan.47.376 */ + if (p_tcd->image->comps[0].resno_decoded != + p_tcd->image->comps[1].resno_decoded || + p_tcd->image->comps[0].resno_decoded != + p_tcd->image->comps[2].resno_decoded || + (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) * + (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples || + (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) * + (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) { + opj_event_msg(p_manager, EVT_ERROR, + "Tiles don't all have the same dimension. Skip the MCT step.\n"); + return OPJ_FALSE; + } + } + } + + if (l_tile->numcomps >= 3) { + if (l_tcp->mct == 2) { + OPJ_BYTE ** l_data; + + if (! l_tcp->m_mct_decoding_matrix) { + return OPJ_TRUE; + } + + l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*)); + if (! l_data) { + return OPJ_FALSE; + } + + for (i = 0; i < l_tile->numcomps; ++i) { + if (p_tcd->whole_tile_decoding) { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data; + } else { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win; + } + ++l_tile_comp; + } + + if (! opj_mct_decode_custom(/* MCT data */ + (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix, + /* size of components */ + l_samples, + /* components */ + l_data, + /* nb of components (i.e. size of pData) */ + l_tile->numcomps, + /* tells if the data is signed */ + p_tcd->image->comps->sgnd)) { + opj_free(l_data); + return OPJ_FALSE; + } + + opj_free(l_data); + } else { + if (l_tcp->tccps->qmfbid == 1) { + if (p_tcd->whole_tile_decoding) { + opj_mct_decode(l_tile->comps[0].data, + l_tile->comps[1].data, + l_tile->comps[2].data, + l_samples); + } else { + opj_mct_decode(l_tile->comps[0].data_win, + l_tile->comps[1].data_win, + l_tile->comps[2].data_win, + l_samples); + } + } else { + if (p_tcd->whole_tile_decoding) { + opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data, + (OPJ_FLOAT32*)l_tile->comps[1].data, + (OPJ_FLOAT32*)l_tile->comps[2].data, + l_samples); + } else { + opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win, + (OPJ_FLOAT32*)l_tile->comps[1].data_win, + (OPJ_FLOAT32*)l_tile->comps[2].data_win, + l_samples); + } + } + } + } else { + opj_event_msg(p_manager, EVT_ERROR, + "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n", + l_tile->numcomps); + } + + return OPJ_TRUE; +} + + +static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd) +{ + OPJ_UINT32 compno; + opj_tcd_tilecomp_t * l_tile_comp = 00; + opj_tccp_t * l_tccp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_resolution_t* l_res = 00; + opj_tcd_tile_t * l_tile; + OPJ_UINT32 l_width, l_height, i, j; + OPJ_INT32 * l_current_ptr; + OPJ_INT32 l_min, l_max; + OPJ_UINT32 l_stride; + + l_tile = p_tcd->tcd_image->tiles; + l_tile_comp = l_tile->comps; + l_tccp = p_tcd->tcp->tccps; + l_img_comp = p_tcd->image->comps; + + for (compno = 0; compno < l_tile->numcomps; + compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) { + + if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) { + continue; + } + + l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded; + + if (!p_tcd->whole_tile_decoding) { + l_width = l_res->win_x1 - l_res->win_x0; + l_height = l_res->win_y1 - l_res->win_y0; + l_stride = 0; + l_current_ptr = l_tile_comp->data_win; + } else { + l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0); + l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0); + l_stride = (OPJ_UINT32)( + l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 - + l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0) + - l_width; + l_current_ptr = l_tile_comp->data; + + assert(l_height == 0 || + l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/ + } + + if (l_img_comp->sgnd) { + l_min = -(1 << (l_img_comp->prec - 1)); + l_max = (1 << (l_img_comp->prec - 1)) - 1; + } else { + l_min = 0; + l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1); + } + + + if (l_tccp->qmfbid == 1) { + for (j = 0; j < l_height; ++j) { + for (i = 0; i < l_width; ++i) { + /* TODO: do addition on int64 ? */ + *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, + l_max); + ++l_current_ptr; + } + l_current_ptr += l_stride; + } + } else { + for (j = 0; j < l_height; ++j) { + for (i = 0; i < l_width; ++i) { + OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr); + if (l_value > INT_MAX) { + *l_current_ptr = l_max; + } else if (l_value < INT_MIN) { + *l_current_ptr = l_min; + } else { + /* Do addition on int64 to avoid overflows */ + OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value); + *l_current_ptr = (OPJ_INT32)opj_int64_clamp( + l_value_int + l_tccp->m_dc_level_shift, l_min, l_max); + } + ++l_current_ptr; + } + l_current_ptr += l_stride; + } + } + } + + return OPJ_TRUE; +} + + + +/** + * Deallocates the encoding data of the given precinct. + */ +static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct) +{ + OPJ_UINT32 cblkno, l_nb_code_blocks; + + opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec; + if (l_code_block) { + /*fprintf(stderr,"deallocate codeblock:{\n");*/ + /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/ + /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ", + l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/ + + + l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof( + opj_tcd_cblk_dec_t); + /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/ + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + + if (l_code_block->segs) { + opj_free(l_code_block->segs); + l_code_block->segs = 00; + } + + if (l_code_block->chunks) { + opj_free(l_code_block->chunks); + l_code_block->chunks = 00; + } + + opj_aligned_free(l_code_block->decoded_data); + l_code_block->decoded_data = NULL; + + ++l_code_block; + } + + opj_free(p_precinct->cblks.dec); + p_precinct->cblks.dec = 00; + } +} + +/** + * Deallocates the encoding data of the given precinct. + */ +static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct) +{ + OPJ_UINT32 cblkno, l_nb_code_blocks; + + opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc; + if (l_code_block) { + l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof( + opj_tcd_cblk_enc_t); + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + if (l_code_block->data) { + /* We refer to data - 1 since below we incremented it */ + /* in opj_tcd_code_block_enc_allocate_data() */ + opj_free(l_code_block->data - 1); + l_code_block->data = 00; + } + + if (l_code_block->layers) { + opj_free(l_code_block->layers); + l_code_block->layers = 00; + } + + if (l_code_block->passes) { + opj_free(l_code_block->passes); + l_code_block->passes = 00; + } + ++l_code_block; + } + + opj_free(p_precinct->cblks.enc); + + p_precinct->cblks.enc = 00; + } +} + +OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd) +{ + OPJ_UINT32 i; + OPJ_SIZE_T l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + OPJ_UINT32 l_size_comp, l_remaining; + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for (i = 0; i < p_tcd->image->numcomps; ++i) { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) * + (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0)); + ++l_img_comp; + ++l_tilec; + } + + return l_data_size; +} + +static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd) +{ + OPJ_UINT32 compno; + opj_tcd_tilecomp_t * l_tile_comp = 00; + opj_tccp_t * l_tccp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tile_t * l_tile; + OPJ_SIZE_T l_nb_elem, i; + OPJ_INT32 * l_current_ptr; + + l_tile = p_tcd->tcd_image->tiles; + l_tile_comp = l_tile->comps; + l_tccp = p_tcd->tcp->tccps; + l_img_comp = p_tcd->image->comps; + + for (compno = 0; compno < l_tile->numcomps; compno++) { + l_current_ptr = l_tile_comp->data; + l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) * + (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0); + + if (l_tccp->qmfbid == 1) { + for (i = 0; i < l_nb_elem; ++i) { + *l_current_ptr -= l_tccp->m_dc_level_shift ; + ++l_current_ptr; + } + } else { + for (i = 0; i < l_nb_elem; ++i) { + *((OPJ_FLOAT32 *) l_current_ptr) = (OPJ_FLOAT32)(*l_current_ptr - + l_tccp->m_dc_level_shift); + ++l_current_ptr; + } + } + + ++l_img_comp; + ++l_tccp; + ++l_tile_comp; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd) +{ + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; + OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) * + (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0); + OPJ_UINT32 i; + OPJ_BYTE ** l_data = 00; + opj_tcp_t * l_tcp = p_tcd->tcp; + + if (!p_tcd->tcp->mct) { + return OPJ_TRUE; + } + + if (p_tcd->tcp->mct == 2) { + if (! p_tcd->tcp->m_mct_coding_matrix) { + return OPJ_TRUE; + } + + l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*)); + if (! l_data) { + return OPJ_FALSE; + } + + for (i = 0; i < l_tile->numcomps; ++i) { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data; + ++l_tile_comp; + } + + if (! opj_mct_encode_custom(/* MCT data */ + (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix, + /* size of components */ + samples, + /* components */ + l_data, + /* nb of components (i.e. size of pData) */ + l_tile->numcomps, + /* tells if the data is signed */ + p_tcd->image->comps->sgnd)) { + opj_free(l_data); + return OPJ_FALSE; + } + + opj_free(l_data); + } else if (l_tcp->tccps->qmfbid == 0) { + opj_mct_encode_real( + (OPJ_FLOAT32*)l_tile->comps[0].data, + (OPJ_FLOAT32*)l_tile->comps[1].data, + (OPJ_FLOAT32*)l_tile->comps[2].data, + samples); + } else { + opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, + l_tile->comps[2].data, samples); + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd) +{ + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + OPJ_UINT32 compno; + + for (compno = 0; compno < l_tile->numcomps; ++compno) { + if (l_tccp->qmfbid == 1) { + if (! opj_dwt_encode(p_tcd, l_tile_comp)) { + return OPJ_FALSE; + } + } else if (l_tccp->qmfbid == 0) { + if (! opj_dwt_encode_real(p_tcd, l_tile_comp)) { + return OPJ_FALSE; + } + } + + ++l_tile_comp; + ++l_tccp; + } + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd) +{ + const OPJ_FLOAT64 * l_mct_norms; + OPJ_UINT32 l_mct_numcomps = 0U; + opj_tcp_t * l_tcp = p_tcd->tcp; + + if (l_tcp->mct == 1) { + l_mct_numcomps = 3U; + /* irreversible encoding */ + if (l_tcp->tccps->qmfbid == 0) { + l_mct_norms = opj_mct_get_mct_norms_real(); + } else { + l_mct_norms = opj_mct_get_mct_norms(); + } + } else { + l_mct_numcomps = p_tcd->image->numcomps; + l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms); + } + + return opj_t1_encode_cblks(p_tcd, + p_tcd->tcd_image->tiles, l_tcp, l_mct_norms, + l_mct_numcomps); + + return OPJ_TRUE; +} + +static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info, + opj_tcd_marker_info_t* p_marker_info, + opj_event_mgr_t *p_manager) +{ + opj_t2_t * l_t2; + + l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp); + if (l_t2 == 00) { + return OPJ_FALSE; + } + + if (! opj_t2_encode_packets( + l_t2, + p_tcd->tcd_tileno, + p_tcd->tcd_image->tiles, + p_tcd->tcp->numlayers, + p_dest_data, + p_data_written, + p_max_dest_size, + p_cstr_info, + p_marker_info, + p_tcd->tp_num, + p_tcd->tp_pos, + p_tcd->cur_pino, + FINAL_PASS, + p_manager)) { + opj_t2_destroy(l_t2); + return OPJ_FALSE; + } + + opj_t2_destroy(l_t2); + + /*---------------CLEAN-------------------*/ + return OPJ_TRUE; +} + + +static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info, + opj_event_mgr_t *p_manager) +{ + opj_cp_t * l_cp = p_tcd->cp; + OPJ_UINT32 l_nb_written = 0; + + if (p_cstr_info) { + p_cstr_info->index_write = 0; + } + + if (l_cp->m_specific_param.m_enc.m_disto_alloc || + l_cp->m_specific_param.m_enc.m_fixed_quality) { + /* fixed_quality */ + /* Normal Rate/distortion allocation */ + if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size, + p_cstr_info, p_manager)) { + return OPJ_FALSE; + } + } else { + /* Fixed layer allocation */ + opj_tcd_rateallocate_fixed(p_tcd); + } + + return OPJ_TRUE; +} + + +OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd, + OPJ_BYTE * p_src, + OPJ_SIZE_T p_src_length) +{ + OPJ_UINT32 i; + OPJ_SIZE_T j; + OPJ_SIZE_T l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + OPJ_UINT32 l_size_comp, l_remaining; + OPJ_SIZE_T l_nb_elem; + + l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd); + if (l_data_size != p_src_length) { + return OPJ_FALSE; + } + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for (i = 0; i < p_tcd->image->numcomps; ++i) { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) * + (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0); + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + switch (l_size_comp) { + case 1: { + OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src; + OPJ_INT32 * l_dest_ptr = l_tilec->data; + + if (l_img_comp->sgnd) { + for (j = 0; j < l_nb_elem; ++j) { + *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++)); + } + } else { + for (j = 0; j < l_nb_elem; ++j) { + *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff; + } + } + + p_src = (OPJ_BYTE*) l_src_ptr; + } + break; + case 2: { + OPJ_INT32 * l_dest_ptr = l_tilec->data; + OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src; + + if (l_img_comp->sgnd) { + for (j = 0; j < l_nb_elem; ++j) { + *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++)); + } + } else { + for (j = 0; j < l_nb_elem; ++j) { + *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff; + } + } + + p_src = (OPJ_BYTE*) l_src_ptr; + } + break; + case 4: { + OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src; + OPJ_INT32 * l_dest_ptr = l_tilec->data; + + for (j = 0; j < l_nb_elem; ++j) { + *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++)); + } + + p_src = (OPJ_BYTE*) l_src_ptr; + } + break; + } + + ++l_img_comp; + ++l_tilec; + } + + return OPJ_TRUE; +} + +OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band) +{ + return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0); +} + +OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd, + OPJ_UINT32 compno, + OPJ_UINT32 resno, + OPJ_UINT32 bandno, + OPJ_UINT32 band_x0, + OPJ_UINT32 band_y0, + OPJ_UINT32 band_x1, + OPJ_UINT32 band_y1) +{ + /* Note: those values for filter_margin are in part the result of */ + /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */ + /* to the maximum left/right extension given in tables F.2 and F.3 of the */ + /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */ + /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */ + /* needed to be bumped to 4, in case inconsistencies are found while */ + /* decoding parts of irreversible coded images. */ + /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */ + OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3; + opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]); + opj_image_comp_t* image_comp = &(tcd->image->comps[compno]); + /* Compute the intersection of the area of interest, expressed in tile coordinates */ + /* with the tile coordinates */ + OPJ_UINT32 tcx0 = opj_uint_max( + (OPJ_UINT32)tilec->x0, + opj_uint_ceildiv(tcd->win_x0, image_comp->dx)); + OPJ_UINT32 tcy0 = opj_uint_max( + (OPJ_UINT32)tilec->y0, + opj_uint_ceildiv(tcd->win_y0, image_comp->dy)); + OPJ_UINT32 tcx1 = opj_uint_min( + (OPJ_UINT32)tilec->x1, + opj_uint_ceildiv(tcd->win_x1, image_comp->dx)); + OPJ_UINT32 tcy1 = opj_uint_min( + (OPJ_UINT32)tilec->y1, + opj_uint_ceildiv(tcd->win_y1, image_comp->dy)); + /* Compute number of decomposition for this band. See table F-1 */ + OPJ_UINT32 nb = (resno == 0) ? + tilec->numresolutions - 1 : + tilec->numresolutions - resno; + /* Map above tile-based coordinates to sub-band-based coordinates per */ + /* equation B-15 of the standard */ + OPJ_UINT32 x0b = bandno & 1; + OPJ_UINT32 y0b = bandno >> 1; + OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 : + (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 : + opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb); + OPJ_UINT32 tby0 = (nb == 0) ? tcy0 : + (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 : + opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb); + OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 : + (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 : + opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb); + OPJ_UINT32 tby1 = (nb == 0) ? tcy1 : + (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 : + opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb); + OPJ_BOOL intersects; + + if (tbx0 < filter_margin) { + tbx0 = 0; + } else { + tbx0 -= filter_margin; + } + if (tby0 < filter_margin) { + tby0 = 0; + } else { + tby0 -= filter_margin; + } + tbx1 = opj_uint_adds(tbx1, filter_margin); + tby1 = opj_uint_adds(tby1, filter_margin); + + intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 && + band_y1 > tby0; + +#ifdef DEBUG_VERBOSE + printf("compno=%u resno=%u nb=%u bandno=%u x0b=%u y0b=%u band=%u,%u,%u,%u tb=%u,%u,%u,%u -> %u\n", + compno, resno, nb, bandno, x0b, y0b, + band_x0, band_y0, band_x1, band_y1, + tbx0, tby0, tbx1, tby1, intersects); +#endif + return intersects; +} + +/** Returns whether a tile componenent is fully decoded, taking into account + * p_tcd->win_* members. + * + * @param p_tcd TCD handle. + * @param compno Component number + * @return OPJ_TRUE whether the tile componenent is fully decoded + */ +static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd, + OPJ_UINT32 compno) +{ + opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]); + opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]); + /* Compute the intersection of the area of interest, expressed in tile coordinates */ + /* with the tile coordinates */ + OPJ_UINT32 tcx0 = opj_uint_max( + (OPJ_UINT32)tilec->x0, + opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx)); + OPJ_UINT32 tcy0 = opj_uint_max( + (OPJ_UINT32)tilec->y0, + opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy)); + OPJ_UINT32 tcx1 = opj_uint_min( + (OPJ_UINT32)tilec->x1, + opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx)); + OPJ_UINT32 tcy1 = opj_uint_min( + (OPJ_UINT32)tilec->y1, + opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy)); + + OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions; + /* Tolerate small margin within the reduced resolution factor to consider if */ + /* the whole tile path must be taken */ + return (tcx0 >= (OPJ_UINT32)tilec->x0 && + tcy0 >= (OPJ_UINT32)tilec->y0 && + tcx1 <= (OPJ_UINT32)tilec->x1 && + tcy1 <= (OPJ_UINT32)tilec->y1 && + (shift >= 32 || + (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 && + ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 && + (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 && + (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0))); +} + +/* ----------------------------------------------------------------------- */ + +opj_tcd_marker_info_t* opj_tcd_marker_info_create(OPJ_BOOL need_PLT) +{ + opj_tcd_marker_info_t *l_tcd_marker_info = + (opj_tcd_marker_info_t*) opj_calloc(1, sizeof(opj_tcd_marker_info_t)); + if (!l_tcd_marker_info) { + return NULL; + } + + l_tcd_marker_info->need_PLT = need_PLT; + + return l_tcd_marker_info; +} + +/* ----------------------------------------------------------------------- */ + +void opj_tcd_marker_info_destroy(opj_tcd_marker_info_t *p_tcd_marker_info) +{ + if (p_tcd_marker_info) { + opj_free(p_tcd_marker_info->p_packet_size); + opj_free(p_tcd_marker_info); + } +} + +/* ----------------------------------------------------------------------- */ diff --git a/cpp/3rd_party/openjpeg/openjp2/tcd.h b/cpp/3rd_party/openjpeg/openjp2/tcd.h new file mode 100644 index 0000000000..f1b52b8dac --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/tcd.h @@ -0,0 +1,523 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * Copyright (c) 2017, IntoPIX SA + * 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 OPJ_TCD_H +#define OPJ_TCD_H +/** +@file tcd.h +@brief Implementation of a tile coder/decoder (TCD) + +The functions in TCD.C encode or decode each tile independently from +each other. The functions in TCD.C are used by other functions in J2K.C. +*/ + +/** @defgroup TCD TCD - Implementation of a tile coder/decoder */ +/*@{*/ + + +/** +FIXME DOC +*/ +typedef struct opj_tcd_pass { + OPJ_UINT32 rate; + OPJ_FLOAT64 distortiondec; + OPJ_UINT32 len; + OPJ_BITFIELD term : 1; +} opj_tcd_pass_t; + +/** +FIXME DOC +*/ +typedef struct opj_tcd_layer { + OPJ_UINT32 numpasses; /* Number of passes in the layer */ + OPJ_UINT32 len; /* len of information */ + OPJ_FLOAT64 disto; /* add for index (Cfr. Marcela) */ + OPJ_BYTE *data; /* data */ +} opj_tcd_layer_t; + +/** +FIXME DOC +*/ +typedef struct opj_tcd_cblk_enc { + OPJ_BYTE* data; /* Data */ + opj_tcd_layer_t* layers; /* layer information */ + opj_tcd_pass_t* passes; /* information about the passes */ + OPJ_INT32 x0, y0, x1, + y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 numbps; + OPJ_UINT32 numlenbits; + OPJ_UINT32 data_size; /* Size of allocated data buffer */ + OPJ_UINT32 + numpasses; /* number of pass already done for the code-blocks */ + OPJ_UINT32 numpassesinlayers; /* number of passes in the layer */ + OPJ_UINT32 totalpasses; /* total number of passes */ +} opj_tcd_cblk_enc_t; + + +/** Chunk of codestream data that is part of a code block */ +typedef struct opj_tcd_seg_data_chunk { + /* Point to tilepart buffer. We don't make a copy ! + So the tilepart buffer must be kept alive + as long as we need to decode the codeblocks */ + OPJ_BYTE * data; + OPJ_UINT32 len; /* Usable length of data */ +} opj_tcd_seg_data_chunk_t; + +/** Segment of a code-block. + * A segment represent a number of consecutive coding passes, without termination + * of MQC or RAW between them. */ +typedef struct opj_tcd_seg { + OPJ_UINT32 len; /* Size of data related to this segment */ + /* Number of passes decoded. Including those that we skip */ + OPJ_UINT32 numpasses; + /* Number of passes actually to be decoded. To be used for code-block decoding */ + OPJ_UINT32 real_num_passes; + /* Maximum number of passes for this segment */ + OPJ_UINT32 maxpasses; + /* Number of new passes for current packed. Transitory value */ + OPJ_UINT32 numnewpasses; + /* Codestream length for this segment for current packed. Transitory value */ + OPJ_UINT32 newlen; +} opj_tcd_seg_t; + +/** Code-block for decoding */ +typedef struct opj_tcd_cblk_dec { + opj_tcd_seg_t* segs; /* segments information */ + opj_tcd_seg_data_chunk_t* chunks; /* Array of chunks */ + /* position of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_INT32 x0, y0, x1, y1; + OPJ_UINT32 numbps; + /* number of bits for len, for the current packet. Transitory value */ + OPJ_UINT32 numlenbits; + /* number of pass added to the code-blocks, for the current packet. Transitory value */ + OPJ_UINT32 numnewpasses; + /* number of segments, including those of packet we skip */ + OPJ_UINT32 numsegs; + /* number of segments, to be used for code block decoding */ + OPJ_UINT32 real_num_segs; + OPJ_UINT32 m_current_max_segs; /* allocated number of segs[] items */ + OPJ_UINT32 numchunks; /* Number of valid chunks items */ + OPJ_UINT32 numchunksalloc; /* Number of chunks item allocated */ + /* Decoded code-block. Only used for subtile decoding. Otherwise tilec->data is directly updated */ + OPJ_INT32* decoded_data; +} opj_tcd_cblk_dec_t; + +/** Precinct structure */ +typedef struct opj_tcd_precinct { + /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_INT32 x0, y0, x1, y1; + OPJ_UINT32 cw, ch; /* number of code-blocks, in width and height */ + union { /* code-blocks information */ + opj_tcd_cblk_enc_t* enc; + opj_tcd_cblk_dec_t* dec; + void* blocks; + } cblks; + OPJ_UINT32 block_size; /* size taken by cblks (in bytes) */ + opj_tgt_tree_t *incltree; /* inclusion tree */ + opj_tgt_tree_t *imsbtree; /* IMSB tree */ +} opj_tcd_precinct_t; + +/** Sub-band structure */ +typedef struct opj_tcd_band { + /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_INT32 x0, y0, x1, y1; + /* band number: for lowest resolution level (0=LL), otherwise (1=HL, 2=LH, 3=HH) */ + OPJ_UINT32 bandno; + /* precinct information */ + opj_tcd_precinct_t *precincts; + /* size of data taken by precincts */ + OPJ_UINT32 precincts_data_size; + OPJ_INT32 numbps; + OPJ_FLOAT32 stepsize; +} opj_tcd_band_t; + +/** Tile-component resolution structure */ +typedef struct opj_tcd_resolution { + /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_INT32 x0, y0, x1, y1; + /* number of precincts, in width and height, for this resolution level */ + OPJ_UINT32 pw, ph; + /* number of sub-bands for the resolution level (1 for lowest resolution level, 3 otherwise) */ + OPJ_UINT32 numbands; + /* subband information */ + opj_tcd_band_t bands[3]; + + /* dimension of the resolution limited to window of interest. Only valid if tcd->whole_tile_decoding is set */ + OPJ_UINT32 win_x0; + OPJ_UINT32 win_y0; + OPJ_UINT32 win_x1; + OPJ_UINT32 win_y1; +} opj_tcd_resolution_t; + +/** Tile-component structure */ +typedef struct opj_tcd_tilecomp { + /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_INT32 x0, y0, x1, y1; + /* component number */ + OPJ_UINT32 compno; + /* number of resolutions level */ + OPJ_UINT32 numresolutions; + /* number of resolutions level to decode (at max)*/ + OPJ_UINT32 minimum_num_resolutions; + /* resolutions information */ + opj_tcd_resolution_t *resolutions; + /* size of data for resolutions (in bytes) */ + OPJ_UINT32 resolutions_size; + + /* data of the component. For decoding, only valid if tcd->whole_tile_decoding is set (so exclusive of data_win member) */ + OPJ_INT32 *data; + /* if true, then need to free after usage, otherwise do not free */ + OPJ_BOOL ownsData; + /* we may either need to allocate this amount of data, or re-use image data and ignore this value */ + size_t data_size_needed; + /* size of the data of the component */ + size_t data_size; + + /** data of the component limited to window of interest. Only valid for decoding and if tcd->whole_tile_decoding is NOT set (so exclusive of data member) */ + OPJ_INT32 *data_win; + /* dimension of the component limited to window of interest. Only valid for decoding and if tcd->whole_tile_decoding is NOT set */ + OPJ_UINT32 win_x0; + OPJ_UINT32 win_y0; + OPJ_UINT32 win_x1; + OPJ_UINT32 win_y1; + + /* add fixed_quality */ + OPJ_INT32 numpix; +} opj_tcd_tilecomp_t; + + +/** +FIXME DOC +*/ +typedef struct opj_tcd_tile { + /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_INT32 x0, y0, x1, y1; + OPJ_UINT32 numcomps; /* number of components in tile */ + opj_tcd_tilecomp_t *comps; /* Components information */ + OPJ_INT32 numpix; /* add fixed_quality */ + OPJ_FLOAT64 distotile; /* add fixed_quality */ + OPJ_FLOAT64 distolayer[100]; /* add fixed_quality */ + OPJ_UINT32 packno; /* packet number */ +} opj_tcd_tile_t; + +/** +FIXME DOC +*/ +typedef struct opj_tcd_image { + opj_tcd_tile_t *tiles; /* Tiles information */ +} +opj_tcd_image_t; + + +/** +Tile coder/decoder +*/ +typedef struct opj_tcd { + /** Position of the tilepart flag in Progression order*/ + OPJ_INT32 tp_pos; + /** Tile part number*/ + OPJ_UINT32 tp_num; + /** Current tile part number*/ + OPJ_UINT32 cur_tp_num; + /** Total number of tileparts of the current tile*/ + OPJ_UINT32 cur_totnum_tp; + /** Current Packet iterator number */ + OPJ_UINT32 cur_pino; + /** info on each image tile */ + opj_tcd_image_t *tcd_image; + /** image header */ + opj_image_t *image; + /** coding parameters */ + opj_cp_t *cp; + /** coding/decoding parameters common to all tiles */ + opj_tcp_t *tcp; + /** current encoded/decoded tile */ + OPJ_UINT32 tcd_tileno; + /** tell if the tcd is a decoder. */ + OPJ_BITFIELD m_is_decoder : 1; + /** Thread pool */ + opj_thread_pool_t* thread_pool; + /** Coordinates of the window of interest, in grid reference space */ + OPJ_UINT32 win_x0; + OPJ_UINT32 win_y0; + OPJ_UINT32 win_x1; + OPJ_UINT32 win_y1; + /** Only valid for decoding. Whether the whole tile is decoded, or just the region in win_x0/win_y0/win_x1/win_y1 */ + OPJ_BOOL whole_tile_decoding; + /* Array of size image->numcomps indicating if a component must be decoded. NULL if all components must be decoded */ + OPJ_BOOL* used_component; +} opj_tcd_t; + +/** + * Structure to hold information needed to generate some markers. + * Used by encoder. + */ +typedef struct opj_tcd_marker_info { + /** In: Whether information to generate PLT markers in needed */ + OPJ_BOOL need_PLT; + + /** OUT: Number of elements in p_packet_size[] array */ + OPJ_UINT32 packet_count; + + /** OUT: Array of size packet_count, such that p_packet_size[i] is + * the size in bytes of the ith packet */ + OPJ_UINT32* p_packet_size; +} opj_tcd_marker_info_t; + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Dump the content of a tcd structure +*/ +/*void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);*/ /* TODO MSD shoul use the new v2 structures */ + +/** +Create a new TCD handle +@param p_is_decoder FIXME DOC +@return Returns a new TCD handle if successful returns NULL otherwise +*/ +opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder); + +/** +Destroy a previously created TCD handle +@param tcd TCD handle to destroy +*/ +void opj_tcd_destroy(opj_tcd_t *tcd); + + +/** + * Create a new opj_tcd_marker_info_t* structure + * @param need_PLT Whether information is needed to generate PLT markers. + */ +opj_tcd_marker_info_t* opj_tcd_marker_info_create(OPJ_BOOL need_PLT); + + +/** +Destroy a previously created opj_tcd_marker_info_t* structure +@param p_tcd_marker_info Structure to destroy +*/ +void opj_tcd_marker_info_destroy(opj_tcd_marker_info_t *p_tcd_marker_info); + + +/** + * Initialize the tile coder and may reuse some memory. + * @param p_tcd TCD handle. + * @param p_image raw image. + * @param p_cp coding parameters. + * @param p_tp thread pool + * + * @return true if the encoding values could be set (false otherwise). +*/ +OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd, + opj_image_t * p_image, + opj_cp_t * p_cp, + opj_thread_pool_t* p_tp); + +/** + * Allocates memory for decoding a specific tile. + * + * @param p_tcd the tile decoder. + * @param p_tile_no the index of the tile received in sequence. This not necessarily lead to the + * tile at index p_tile_no. + * @param p_manager the event manager. + * + * @return true if the remaining data is sufficient. + */ +OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, + opj_event_mgr_t* p_manager); + +void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, + OPJ_UINT32 final); + +void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd); + +void opj_tcd_makelayer(opj_tcd_t *tcd, + OPJ_UINT32 layno, + OPJ_FLOAT64 thresh, + OPJ_UINT32 final); + +OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info, + opj_event_mgr_t *p_manager); + +/** + * Gets the maximum tile size that will be taken by the tile once decoded. + */ +OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd, + OPJ_BOOL take_into_account_partial_decoding); + +/** + * Encodes a tile from the raw image into the given buffer. + * @param p_tcd Tile Coder handle + * @param p_tile_no Index of the tile to encode. + * @param p_dest Destination buffer + * @param p_data_written pointer to an int that is incremented by the number of bytes really written on p_dest + * @param p_len Maximum length of the destination buffer + * @param p_cstr_info Codestream information structure + * @param p_marker_info Marker information structure + * @param p_manager the user event manager + * @return true if the coding is successful. +*/ +OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd, + OPJ_UINT32 p_tile_no, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_len, + struct opj_codestream_info *p_cstr_info, + opj_tcd_marker_info_t* p_marker_info, + opj_event_mgr_t *p_manager); + + +/** +Decode a tile from a buffer into a raw image +@param tcd TCD handle +@param win_x0 Upper left x of region to decode (in grid coordinates) +@param win_y0 Upper left y of region to decode (in grid coordinates) +@param win_x1 Lower right x of region to decode (in grid coordinates) +@param win_y1 Lower right y of region to decode (in grid coordinates) +@param numcomps_to_decode Size of the comps_indices array, or 0 if decoding all components. +@param comps_indices Array of numcomps values representing the indices + of the components to decode (relative to the + codestream, starting at 0). Or NULL if decoding all components. +@param src Source buffer +@param len Length of source buffer +@param tileno Number that identifies one of the tiles to be decoded +@param cstr_info FIXME DOC +@param manager the event manager. +*/ +OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *tcd, + OPJ_UINT32 win_x0, + OPJ_UINT32 win_y0, + OPJ_UINT32 win_x1, + OPJ_UINT32 win_y1, + OPJ_UINT32 numcomps_to_decode, + const OPJ_UINT32 *comps_indices, + OPJ_BYTE *src, + OPJ_UINT32 len, + OPJ_UINT32 tileno, + opj_codestream_index_t *cstr_info, + opj_event_mgr_t *manager); + + +/** + * Copies tile data from the system onto the given memory block. + */ +OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest, + OPJ_UINT32 p_dest_length); + +/** + * Get the size in bytes of the input buffer provided before encoded. + * This must be the size provided to the p_src_length argument of + * opj_tcd_copy_tile_data() + */ +OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd); + +/** + * Initialize the tile coder and may reuse some meory. + * + * @param p_tcd TCD handle. + * @param p_tile_no current tile index to encode. + * @param p_manager the event manager. + * + * @return true if the encoding values could be set (false otherwise). +*/ +OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, + OPJ_UINT32 p_tile_no, opj_event_mgr_t* p_manager); + +/** + * Copies tile data from the given memory block onto the system. + * + * p_src_length must be equal to opj_tcd_get_encoder_input_buffer_size() + */ +OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd, + OPJ_BYTE * p_src, + OPJ_SIZE_T p_src_length); + +/** + * Allocates tile component data + * + * + */ +OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec); + +/** Returns whether a sub-band is empty (i.e. whether it has a null area) + * @param band Sub-band handle. + * @return OPJ_TRUE whether the sub-band is empty. + */ +OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band); + +/** Reinitialize a segment */ +void opj_tcd_reinit_segment(opj_tcd_seg_t* seg); + + +/** Returns whether a sub-band region contributes to the area of interest + * tcd->win_x0,tcd->win_y0,tcd->win_x1,tcd->win_y1. + * + * @param tcd TCD handle. + * @param compno Component number + * @param resno Resolution number + * @param bandno Band number (*not* band index, ie 0, 1, 2 or 3) + * @param x0 Upper left x in subband coordinates + * @param y0 Upper left y in subband coordinates + * @param x1 Lower right x in subband coordinates + * @param y1 Lower right y in subband coordinates + * @return OPJ_TRUE whether the sub-band region contributs to the area of + * interest. + */ +OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd, + OPJ_UINT32 compno, + OPJ_UINT32 resno, + OPJ_UINT32 bandno, + OPJ_UINT32 x0, + OPJ_UINT32 y0, + OPJ_UINT32 x1, + OPJ_UINT32 y1); + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_TCD_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/tgt.c b/cpp/3rd_party/openjpeg/openjp2/tgt.c new file mode 100644 index 0000000000..0cbad12c42 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/tgt.c @@ -0,0 +1,344 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR + * Copyright (c) 2012, CS Systemes d'Information, France + * 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" + +/* +========================================================== + Tag-tree coder interface +========================================================== +*/ + +opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv, + opj_event_mgr_t *p_manager) +{ + OPJ_INT32 nplh[32]; + OPJ_INT32 nplv[32]; + opj_tgt_node_t *node = 00; + opj_tgt_node_t *l_parent_node = 00; + opj_tgt_node_t *l_parent_node0 = 00; + opj_tgt_tree_t *tree = 00; + OPJ_UINT32 i; + OPJ_INT32 j, k; + OPJ_UINT32 numlvls; + OPJ_UINT32 n; + + tree = (opj_tgt_tree_t *) opj_calloc(1, sizeof(opj_tgt_tree_t)); + if (!tree) { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tag-tree\n"); + return 00; + } + + tree->numleafsh = numleafsh; + tree->numleafsv = numleafsv; + + numlvls = 0; + nplh[0] = (OPJ_INT32)numleafsh; + nplv[0] = (OPJ_INT32)numleafsv; + tree->numnodes = 0; + do { + n = (OPJ_UINT32)(nplh[numlvls] * nplv[numlvls]); + nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; + nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; + tree->numnodes += n; + ++numlvls; + } while (n > 1); + + /* ADD */ + if (tree->numnodes == 0) { + opj_free(tree); + return 00; + } + + tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, + sizeof(opj_tgt_node_t)); + if (!tree->nodes) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to create Tag-tree nodes\n"); + opj_free(tree); + return 00; + } + tree->nodes_size = tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t); + + node = tree->nodes; + l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv]; + l_parent_node0 = l_parent_node; + + for (i = 0; i < numlvls - 1; ++i) { + for (j = 0; j < nplv[i]; ++j) { + k = nplh[i]; + while (--k >= 0) { + node->parent = l_parent_node; + ++node; + if (--k >= 0) { + node->parent = l_parent_node; + ++node; + } + ++l_parent_node; + } + if ((j & 1) || j == nplv[i] - 1) { + l_parent_node0 = l_parent_node; + } else { + l_parent_node = l_parent_node0; + l_parent_node0 += nplh[i]; + } + } + } + node->parent = 0; + opj_tgt_reset(tree); + return tree; +} + +/** + * Reinitialises a tag-tree from an existing one. + * + * @param p_tree the tree to reinitialize. + * @param p_num_leafs_h the width of the array of leafs of the tree + * @param p_num_leafs_v the height of the array of leafs of the tree + * @return a new tag-tree if successful, NULL otherwise +*/ +opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree, OPJ_UINT32 p_num_leafs_h, + OPJ_UINT32 p_num_leafs_v, opj_event_mgr_t *p_manager) +{ + OPJ_INT32 l_nplh[32]; + OPJ_INT32 l_nplv[32]; + opj_tgt_node_t *l_node = 00; + opj_tgt_node_t *l_parent_node = 00; + opj_tgt_node_t *l_parent_node0 = 00; + OPJ_UINT32 i; + OPJ_INT32 j, k; + OPJ_UINT32 l_num_levels; + OPJ_UINT32 n; + OPJ_UINT32 l_node_size; + + if (! p_tree) { + return 00; + } + + if ((p_tree->numleafsh != p_num_leafs_h) || + (p_tree->numleafsv != p_num_leafs_v)) { + p_tree->numleafsh = p_num_leafs_h; + p_tree->numleafsv = p_num_leafs_v; + + l_num_levels = 0; + l_nplh[0] = (OPJ_INT32)p_num_leafs_h; + l_nplv[0] = (OPJ_INT32)p_num_leafs_v; + p_tree->numnodes = 0; + do { + n = (OPJ_UINT32)(l_nplh[l_num_levels] * l_nplv[l_num_levels]); + l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2; + l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2; + p_tree->numnodes += n; + ++l_num_levels; + } while (n > 1); + + /* ADD */ + if (p_tree->numnodes == 0) { + opj_tgt_destroy(p_tree); + return 00; + } + l_node_size = p_tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t); + + if (l_node_size > p_tree->nodes_size) { + opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, + l_node_size); + if (! new_nodes) { + opj_event_msg(p_manager, EVT_ERROR, + "Not enough memory to reinitialize the tag tree\n"); + opj_tgt_destroy(p_tree); + return 00; + } + p_tree->nodes = new_nodes; + memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0, + l_node_size - p_tree->nodes_size); + p_tree->nodes_size = l_node_size; + } + l_node = p_tree->nodes; + l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv]; + l_parent_node0 = l_parent_node; + + for (i = 0; i < l_num_levels - 1; ++i) { + for (j = 0; j < l_nplv[i]; ++j) { + k = l_nplh[i]; + while (--k >= 0) { + l_node->parent = l_parent_node; + ++l_node; + if (--k >= 0) { + l_node->parent = l_parent_node; + ++l_node; + } + ++l_parent_node; + } + if ((j & 1) || j == l_nplv[i] - 1) { + l_parent_node0 = l_parent_node; + } else { + l_parent_node = l_parent_node0; + l_parent_node0 += l_nplh[i]; + } + } + } + l_node->parent = 0; + } + opj_tgt_reset(p_tree); + + return p_tree; +} + +void opj_tgt_destroy(opj_tgt_tree_t *p_tree) +{ + if (! p_tree) { + return; + } + + if (p_tree->nodes) { + opj_free(p_tree->nodes); + p_tree->nodes = 00; + } + opj_free(p_tree); +} + +void opj_tgt_reset(opj_tgt_tree_t *p_tree) +{ + OPJ_UINT32 i; + opj_tgt_node_t * l_current_node = 00;; + + if (! p_tree) { + return; + } + + l_current_node = p_tree->nodes; + for (i = 0; i < p_tree->numnodes; ++i) { + l_current_node->value = 999; + l_current_node->low = 0; + l_current_node->known = 0; + ++l_current_node; + } +} + +void opj_tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value) +{ + opj_tgt_node_t *node; + node = &tree->nodes[leafno]; + while (node && node->value > value) { + node->value = value; + node = node->parent; + } +} + +void opj_tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, + OPJ_INT32 threshold) +{ + opj_tgt_node_t *stk[31]; + opj_tgt_node_t **stkptr; + opj_tgt_node_t *node; + OPJ_INT32 low; + + stkptr = stk; + node = &tree->nodes[leafno]; + while (node->parent) { + *stkptr++ = node; + node = node->parent; + } + + low = 0; + for (;;) { + if (low > node->low) { + node->low = low; + } else { + low = node->low; + } + + while (low < threshold) { + if (low >= node->value) { + if (!node->known) { + opj_bio_write(bio, 1, 1); + node->known = 1; + } + break; + } + opj_bio_write(bio, 0, 1); + ++low; + } + + node->low = low; + if (stkptr == stk) { + break; + } + node = *--stkptr; + } +} + +OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, + OPJ_UINT32 leafno, OPJ_INT32 threshold) +{ + opj_tgt_node_t *stk[31]; + opj_tgt_node_t **stkptr; + opj_tgt_node_t *node; + OPJ_INT32 low; + + stkptr = stk; + node = &tree->nodes[leafno]; + while (node->parent) { + *stkptr++ = node; + node = node->parent; + } + + low = 0; + for (;;) { + if (low > node->low) { + node->low = low; + } else { + low = node->low; + } + while (low < threshold && low < node->value) { + if (opj_bio_read(bio, 1)) { + node->value = low; + } else { + ++low; + } + } + node->low = low; + if (stkptr == stk) { + break; + } + node = *--stkptr; + } + + return (node->value < threshold) ? 1 : 0; +} diff --git a/cpp/3rd_party/openjpeg/openjp2/tgt.h b/cpp/3rd_party/openjpeg/openjp2/tgt.h new file mode 100644 index 0000000000..9818208b82 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/tgt.h @@ -0,0 +1,148 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2014, Professor Benoit Macq + * Copyright (c) 2001-2003, David Janssens + * Copyright (c) 2002-2003, Yannick Verschueren + * Copyright (c) 2003-2007, Francois-Olivier Devaux + * Copyright (c) 2003-2014, Antonin Descampe + * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France + * Copyright (c) 2012, CS Systemes d'Information, France + * 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 OPJ_TGT_H +#define OPJ_TGT_H +/** +@file tgt.h +@brief Implementation of a tag-tree coder (TGT) + +The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C +are used by some function in T2.C. +*/ + +/** @defgroup TGT TGT - Implementation of a tag-tree coder */ +/*@{*/ + +/** +Tag node +*/ +typedef struct opj_tgt_node { + struct opj_tgt_node *parent; + OPJ_INT32 value; + OPJ_INT32 low; + OPJ_UINT32 known; +} opj_tgt_node_t; + +/** +Tag tree +*/ +typedef struct opj_tgt_tree { + OPJ_UINT32 numleafsh; + OPJ_UINT32 numleafsv; + OPJ_UINT32 numnodes; + opj_tgt_node_t *nodes; + OPJ_UINT32 nodes_size; /* maximum size taken by nodes */ +} opj_tgt_tree_t; + + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ +/** +Create a tag-tree +@param numleafsh Width of the array of leafs of the tree +@param numleafsv Height of the array of leafs of the tree +@param p_manager the event manager +@return Returns a new tag-tree if successful, returns NULL otherwise +*/ +opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv, + opj_event_mgr_t *p_manager); + +/** + * Reinitialises a tag-tree from an exixting one. + * + * @param p_tree the tree to reinitialize. + * @param p_num_leafs_h the width of the array of leafs of the tree + * @param p_num_leafs_v the height of the array of leafs of the tree + * @param p_manager the event manager + * @return a new tag-tree if successful, NULL otherwise +*/ +opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree, + OPJ_UINT32 p_num_leafs_h, + OPJ_UINT32 p_num_leafs_v, opj_event_mgr_t *p_manager); +/** +Destroy a tag-tree, liberating memory +@param tree Tag-tree to destroy +*/ +void opj_tgt_destroy(opj_tgt_tree_t *tree); +/** +Reset a tag-tree (set all leaves to 0) +@param tree Tag-tree to reset +*/ +void opj_tgt_reset(opj_tgt_tree_t *tree); +/** +Set the value of a leaf of a tag-tree +@param tree Tag-tree to modify +@param leafno Number that identifies the leaf to modify +@param value New value of the leaf +*/ +void opj_tgt_setvalue(opj_tgt_tree_t *tree, + OPJ_UINT32 leafno, + OPJ_INT32 value); +/** +Encode the value of a leaf of the tag-tree up to a given threshold +@param bio Pointer to a BIO handle +@param tree Tag-tree to modify +@param leafno Number that identifies the leaf to encode +@param threshold Threshold to use when encoding value of the leaf +*/ +void opj_tgt_encode(opj_bio_t *bio, + opj_tgt_tree_t *tree, + OPJ_UINT32 leafno, + OPJ_INT32 threshold); +/** +Decode the value of a leaf of the tag-tree up to a given threshold +@param bio Pointer to a BIO handle +@param tree Tag-tree to decode +@param leafno Number that identifies the leaf to decode +@param threshold Threshold to use when decoding value of the leaf +@return Returns 1 if the node's value < threshold, returns 0 otherwise +*/ +OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio, + opj_tgt_tree_t *tree, + OPJ_UINT32 leafno, + OPJ_INT32 threshold); +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* OPJ_TGT_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/thread.c b/cpp/3rd_party/openjpeg/openjp2/thread.c new file mode 100644 index 0000000000..f2fca2ee4a --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/thread.c @@ -0,0 +1,954 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2016, Even Rouault + * 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 + +#ifdef MUTEX_win32 + +/* Some versions of x86_64-w64-mingw32-gc -m32 resolve InterlockedCompareExchange() */ +/* as __sync_val_compare_and_swap_4 but fails to link it. As this protects against */ +/* a rather unlikely race, skip it */ +#if !(defined(__MINGW32__) && defined(__i386__)) +#define HAVE_INTERLOCKED_COMPARE_EXCHANGE 1 +#endif + +#include +#include + +#include "opj_includes.h" + +OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void) +{ + return OPJ_TRUE; +} + +int OPJ_CALLCONV opj_get_num_cpus(void) +{ + SYSTEM_INFO info; + DWORD dwNum; + GetSystemInfo(&info); + dwNum = info.dwNumberOfProcessors; + if (dwNum < 1) { + return 1; + } + return (int)dwNum; +} + +struct opj_mutex_t { + CRITICAL_SECTION cs; +}; + +opj_mutex_t* opj_mutex_create(void) +{ + opj_mutex_t* mutex = (opj_mutex_t*) opj_malloc(sizeof(opj_mutex_t)); + if (!mutex) { + return NULL; + } + InitializeCriticalSectionAndSpinCount(&(mutex->cs), 4000); + return mutex; +} + +void opj_mutex_lock(opj_mutex_t* mutex) +{ + EnterCriticalSection(&(mutex->cs)); +} + +void opj_mutex_unlock(opj_mutex_t* mutex) +{ + LeaveCriticalSection(&(mutex->cs)); +} + +void opj_mutex_destroy(opj_mutex_t* mutex) +{ + if (!mutex) { + return; + } + DeleteCriticalSection(&(mutex->cs)); + opj_free(mutex); +} + +struct opj_cond_waiter_list_t { + HANDLE hEvent; + struct opj_cond_waiter_list_t* next; +}; +typedef struct opj_cond_waiter_list_t opj_cond_waiter_list_t; + +struct opj_cond_t { + opj_mutex_t *internal_mutex; + opj_cond_waiter_list_t *waiter_list; +}; + +static DWORD TLSKey = 0; +static volatile LONG inTLSLockedSection = 0; +static volatile int TLSKeyInit = OPJ_FALSE; + +opj_cond_t* opj_cond_create(void) +{ + opj_cond_t* cond = (opj_cond_t*) opj_malloc(sizeof(opj_cond_t)); + if (!cond) { + return NULL; + } + + /* Make sure that the TLS key is allocated in a thread-safe way */ + /* We cannot use a global mutex/critical section since its creation itself would not be */ + /* thread-safe, so use InterlockedCompareExchange trick */ + while (OPJ_TRUE) { + +#if HAVE_INTERLOCKED_COMPARE_EXCHANGE + if (InterlockedCompareExchange(&inTLSLockedSection, 1, 0) == 0) +#endif + { + if (!TLSKeyInit) { + TLSKey = TlsAlloc(); + TLSKeyInit = OPJ_TRUE; + } +#if HAVE_INTERLOCKED_COMPARE_EXCHANGE + InterlockedCompareExchange(&inTLSLockedSection, 0, 1); +#endif + break; + } + } + + if (TLSKey == TLS_OUT_OF_INDEXES) { + opj_free(cond); + return NULL; + } + cond->internal_mutex = opj_mutex_create(); + if (cond->internal_mutex == NULL) { + opj_free(cond); + return NULL; + } + cond->waiter_list = NULL; + return cond; +} + +void opj_cond_wait(opj_cond_t* cond, opj_mutex_t* mutex) +{ + opj_cond_waiter_list_t* item; + HANDLE hEvent = (HANDLE) TlsGetValue(TLSKey); + if (hEvent == NULL) { + hEvent = CreateEvent(NULL, /* security attributes */ + 0, /* manual reset = no */ + 0, /* initial state = unsignaled */ + NULL /* no name */); + assert(hEvent); + + TlsSetValue(TLSKey, hEvent); + } + + /* Insert the waiter into the waiter list of the condition */ + opj_mutex_lock(cond->internal_mutex); + + item = (opj_cond_waiter_list_t*)opj_malloc(sizeof(opj_cond_waiter_list_t)); + assert(item != NULL); + + item->hEvent = hEvent; + item->next = cond->waiter_list; + + cond->waiter_list = item; + + opj_mutex_unlock(cond->internal_mutex); + + /* Release the client mutex before waiting for the event being signaled */ + opj_mutex_unlock(mutex); + + /* Ideally we would check that we do not get WAIT_FAILED but it is hard */ + /* to report a failure. */ + WaitForSingleObject(hEvent, INFINITE); + + /* Reacquire the client mutex */ + opj_mutex_lock(mutex); +} + +void opj_cond_signal(opj_cond_t* cond) +{ + opj_cond_waiter_list_t* psIter; + + /* Signal the first registered event, and remove it from the list */ + opj_mutex_lock(cond->internal_mutex); + + psIter = cond->waiter_list; + if (psIter != NULL) { + SetEvent(psIter->hEvent); + cond->waiter_list = psIter->next; + opj_free(psIter); + } + + opj_mutex_unlock(cond->internal_mutex); +} + +void opj_cond_destroy(opj_cond_t* cond) +{ + if (!cond) { + return; + } + opj_mutex_destroy(cond->internal_mutex); + assert(cond->waiter_list == NULL); + opj_free(cond); +} + +struct opj_thread_t { + opj_thread_fn thread_fn; + void* user_data; + HANDLE hThread; +}; + +unsigned int __stdcall opj_thread_callback_adapter(void *info) +{ + opj_thread_t* thread = (opj_thread_t*) info; + HANDLE hEvent = NULL; + + thread->thread_fn(thread->user_data); + + /* Free the handle possible allocated by a cond */ + while (OPJ_TRUE) { + /* Make sure TLSKey is not being created just at that moment... */ +#if HAVE_INTERLOCKED_COMPARE_EXCHANGE + if (InterlockedCompareExchange(&inTLSLockedSection, 1, 0) == 0) +#endif + { + if (TLSKeyInit) { + hEvent = (HANDLE) TlsGetValue(TLSKey); + } +#if HAVE_INTERLOCKED_COMPARE_EXCHANGE + InterlockedCompareExchange(&inTLSLockedSection, 0, 1); +#endif + break; + } + } + if (hEvent) { + CloseHandle(hEvent); + } + + return 0; +} + +opj_thread_t* opj_thread_create(opj_thread_fn thread_fn, void* user_data) +{ + opj_thread_t* thread; + + assert(thread_fn); + + thread = (opj_thread_t*) opj_malloc(sizeof(opj_thread_t)); + if (!thread) { + return NULL; + } + thread->thread_fn = thread_fn; + thread->user_data = user_data; + + thread->hThread = (HANDLE)_beginthreadex(NULL, 0, + opj_thread_callback_adapter, thread, 0, NULL); + + if (thread->hThread == NULL) { + opj_free(thread); + return NULL; + } + return thread; +} + +void opj_thread_join(opj_thread_t* thread) +{ + WaitForSingleObject(thread->hThread, INFINITE); + CloseHandle(thread->hThread); + + opj_free(thread); +} + +#elif MUTEX_pthread + +#include +#include +#include + +/* Moved after all system includes, and in particular pthread.h, so as to */ +/* avoid poisoning issuing with malloc() use in pthread.h with ulibc (#1013) */ +#include "opj_includes.h" + +OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void) +{ + return OPJ_TRUE; +} + +int OPJ_CALLCONV opj_get_num_cpus(void) +{ +#ifdef _SC_NPROCESSORS_ONLN + return (int)sysconf(_SC_NPROCESSORS_ONLN); +#else + return 1; +#endif +} + +struct opj_mutex_t { + pthread_mutex_t mutex; +}; + +opj_mutex_t* opj_mutex_create(void) +{ + opj_mutex_t* mutex = (opj_mutex_t*) opj_calloc(1U, sizeof(opj_mutex_t)); + if (mutex != NULL) { + if (pthread_mutex_init(&mutex->mutex, NULL) != 0) { + opj_free(mutex); + mutex = NULL; + } + } + return mutex; +} + +void opj_mutex_lock(opj_mutex_t* mutex) +{ + pthread_mutex_lock(&(mutex->mutex)); +} + +void opj_mutex_unlock(opj_mutex_t* mutex) +{ + pthread_mutex_unlock(&(mutex->mutex)); +} + +void opj_mutex_destroy(opj_mutex_t* mutex) +{ + if (!mutex) { + return; + } + pthread_mutex_destroy(&(mutex->mutex)); + opj_free(mutex); +} + +struct opj_cond_t { + pthread_cond_t cond; +}; + +opj_cond_t* opj_cond_create(void) +{ + opj_cond_t* cond = (opj_cond_t*) opj_malloc(sizeof(opj_cond_t)); + if (!cond) { + return NULL; + } + if (pthread_cond_init(&(cond->cond), NULL) != 0) { + opj_free(cond); + return NULL; + } + return cond; +} + +void opj_cond_wait(opj_cond_t* cond, opj_mutex_t* mutex) +{ + pthread_cond_wait(&(cond->cond), &(mutex->mutex)); +} + +void opj_cond_signal(opj_cond_t* cond) +{ + int ret = pthread_cond_signal(&(cond->cond)); + (void)ret; + assert(ret == 0); +} + +void opj_cond_destroy(opj_cond_t* cond) +{ + if (!cond) { + return; + } + pthread_cond_destroy(&(cond->cond)); + opj_free(cond); +} + + +struct opj_thread_t { + opj_thread_fn thread_fn; + void* user_data; + pthread_t thread; +}; + +static void* opj_thread_callback_adapter(void* info) +{ + opj_thread_t* thread = (opj_thread_t*) info; + thread->thread_fn(thread->user_data); + return NULL; +} + +opj_thread_t* opj_thread_create(opj_thread_fn thread_fn, void* user_data) +{ + pthread_attr_t attr; + opj_thread_t* thread; + + assert(thread_fn); + + thread = (opj_thread_t*) opj_malloc(sizeof(opj_thread_t)); + if (!thread) { + return NULL; + } + thread->thread_fn = thread_fn; + thread->user_data = user_data; + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + if (pthread_create(&(thread->thread), &attr, + opj_thread_callback_adapter, (void *) thread) != 0) { + opj_free(thread); + return NULL; + } + return thread; +} + +void opj_thread_join(opj_thread_t* thread) +{ + void* status; + pthread_join(thread->thread, &status); + + opj_free(thread); +} + +#else +/* Stub implementation */ + +#include "opj_includes.h" + +OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void) +{ + return OPJ_FALSE; +} + +int OPJ_CALLCONV opj_get_num_cpus(void) +{ + return 1; +} + +opj_mutex_t* opj_mutex_create(void) +{ + return NULL; +} + +void opj_mutex_lock(opj_mutex_t* mutex) +{ + (void) mutex; +} + +void opj_mutex_unlock(opj_mutex_t* mutex) +{ + (void) mutex; +} + +void opj_mutex_destroy(opj_mutex_t* mutex) +{ + (void) mutex; +} + +opj_cond_t* opj_cond_create(void) +{ + return NULL; +} + +void opj_cond_wait(opj_cond_t* cond, opj_mutex_t* mutex) +{ + (void) cond; + (void) mutex; +} + +void opj_cond_signal(opj_cond_t* cond) +{ + (void) cond; +} + +void opj_cond_destroy(opj_cond_t* cond) +{ + (void) cond; +} + +opj_thread_t* opj_thread_create(opj_thread_fn thread_fn, void* user_data) +{ + (void) thread_fn; + (void) user_data; + return NULL; +} + +void opj_thread_join(opj_thread_t* thread) +{ + (void) thread; +} + +#endif + +typedef struct { + int key; + void* value; + opj_tls_free_func opj_free_func; +} opj_tls_key_val_t; + +struct opj_tls_t { + opj_tls_key_val_t* key_val; + int key_val_count; +}; + +static opj_tls_t* opj_tls_new(void) +{ + return (opj_tls_t*) opj_calloc(1, sizeof(opj_tls_t)); +} + +static void opj_tls_destroy(opj_tls_t* tls) +{ + int i; + if (!tls) { + return; + } + for (i = 0; i < tls->key_val_count; i++) { + if (tls->key_val[i].opj_free_func) { + tls->key_val[i].opj_free_func(tls->key_val[i].value); + } + } + opj_free(tls->key_val); + opj_free(tls); +} + +void* opj_tls_get(opj_tls_t* tls, int key) +{ + int i; + for (i = 0; i < tls->key_val_count; i++) { + if (tls->key_val[i].key == key) { + return tls->key_val[i].value; + } + } + return NULL; +} + +OPJ_BOOL opj_tls_set(opj_tls_t* tls, int key, void* value, + opj_tls_free_func opj_free_func) +{ + opj_tls_key_val_t* new_key_val; + int i; + + if (tls->key_val_count == INT_MAX) { + return OPJ_FALSE; + } + for (i = 0; i < tls->key_val_count; i++) { + if (tls->key_val[i].key == key) { + if (tls->key_val[i].opj_free_func) { + tls->key_val[i].opj_free_func(tls->key_val[i].value); + } + tls->key_val[i].value = value; + tls->key_val[i].opj_free_func = opj_free_func; + return OPJ_TRUE; + } + } + new_key_val = (opj_tls_key_val_t*) opj_realloc(tls->key_val, + ((size_t)tls->key_val_count + 1U) * sizeof(opj_tls_key_val_t)); + if (!new_key_val) { + return OPJ_FALSE; + } + tls->key_val = new_key_val; + new_key_val[tls->key_val_count].key = key; + new_key_val[tls->key_val_count].value = value; + new_key_val[tls->key_val_count].opj_free_func = opj_free_func; + tls->key_val_count ++; + return OPJ_TRUE; +} + + +typedef struct { + opj_job_fn job_fn; + void *user_data; +} opj_worker_thread_job_t; + +typedef struct { + opj_thread_pool_t *tp; + opj_thread_t *thread; + int marked_as_waiting; + + opj_mutex_t *mutex; + opj_cond_t *cond; +} opj_worker_thread_t; + +typedef enum { + OPJWTS_OK, + OPJWTS_STOP, + OPJWTS_ERROR +} opj_worker_thread_state; + +struct opj_job_list_t { + opj_worker_thread_job_t* job; + struct opj_job_list_t* next; +}; +typedef struct opj_job_list_t opj_job_list_t; + +struct opj_worker_thread_list_t { + opj_worker_thread_t* worker_thread; + struct opj_worker_thread_list_t* next; +}; +typedef struct opj_worker_thread_list_t opj_worker_thread_list_t; + +struct opj_thread_pool_t { + opj_worker_thread_t* worker_threads; + int worker_threads_count; + opj_cond_t* cond; + opj_mutex_t* mutex; + volatile opj_worker_thread_state state; + opj_job_list_t* job_queue; + volatile int pending_jobs_count; + opj_worker_thread_list_t* waiting_worker_thread_list; + int waiting_worker_thread_count; + opj_tls_t* tls; + int signaling_threshold; +}; + +static OPJ_BOOL opj_thread_pool_setup(opj_thread_pool_t* tp, int num_threads); +static opj_worker_thread_job_t* opj_thread_pool_get_next_job( + opj_thread_pool_t* tp, + opj_worker_thread_t* worker_thread, + OPJ_BOOL signal_job_finished); + +opj_thread_pool_t* opj_thread_pool_create(int num_threads) +{ + opj_thread_pool_t* tp; + + tp = (opj_thread_pool_t*) opj_calloc(1, sizeof(opj_thread_pool_t)); + if (!tp) { + return NULL; + } + tp->state = OPJWTS_OK; + + if (num_threads <= 0) { + tp->tls = opj_tls_new(); + if (!tp->tls) { + opj_free(tp); + tp = NULL; + } + return tp; + } + + tp->mutex = opj_mutex_create(); + if (!tp->mutex) { + opj_free(tp); + return NULL; + } + if (!opj_thread_pool_setup(tp, num_threads)) { + opj_thread_pool_destroy(tp); + return NULL; + } + return tp; +} + +static void opj_worker_thread_function(void* user_data) +{ + opj_worker_thread_t* worker_thread; + opj_thread_pool_t* tp; + opj_tls_t* tls; + OPJ_BOOL job_finished = OPJ_FALSE; + + worker_thread = (opj_worker_thread_t*) user_data; + tp = worker_thread->tp; + tls = opj_tls_new(); + + while (OPJ_TRUE) { + opj_worker_thread_job_t* job = opj_thread_pool_get_next_job(tp, worker_thread, + job_finished); + if (job == NULL) { + break; + } + + if (job->job_fn) { + job->job_fn(job->user_data, tls); + } + opj_free(job); + job_finished = OPJ_TRUE; + } + + opj_tls_destroy(tls); +} + +static OPJ_BOOL opj_thread_pool_setup(opj_thread_pool_t* tp, int num_threads) +{ + int i; + OPJ_BOOL bRet = OPJ_TRUE; + + assert(num_threads > 0); + + tp->cond = opj_cond_create(); + if (tp->cond == NULL) { + return OPJ_FALSE; + } + + tp->worker_threads = (opj_worker_thread_t*) opj_calloc((size_t)num_threads, + sizeof(opj_worker_thread_t)); + if (tp->worker_threads == NULL) { + return OPJ_FALSE; + } + tp->worker_threads_count = num_threads; + + for (i = 0; i < num_threads; i++) { + tp->worker_threads[i].tp = tp; + + tp->worker_threads[i].mutex = opj_mutex_create(); + if (tp->worker_threads[i].mutex == NULL) { + tp->worker_threads_count = i; + bRet = OPJ_FALSE; + break; + } + + tp->worker_threads[i].cond = opj_cond_create(); + if (tp->worker_threads[i].cond == NULL) { + opj_mutex_destroy(tp->worker_threads[i].mutex); + tp->worker_threads_count = i; + bRet = OPJ_FALSE; + break; + } + + tp->worker_threads[i].marked_as_waiting = OPJ_FALSE; + + tp->worker_threads[i].thread = opj_thread_create(opj_worker_thread_function, + &(tp->worker_threads[i])); + if (tp->worker_threads[i].thread == NULL) { + opj_mutex_destroy(tp->worker_threads[i].mutex); + opj_cond_destroy(tp->worker_threads[i].cond); + tp->worker_threads_count = i; + bRet = OPJ_FALSE; + break; + } + } + + /* Wait all threads to be started */ + /* printf("waiting for all threads to be started\n"); */ + opj_mutex_lock(tp->mutex); + while (tp->waiting_worker_thread_count < tp->worker_threads_count) { + opj_cond_wait(tp->cond, tp->mutex); + } + opj_mutex_unlock(tp->mutex); + /* printf("all threads started\n"); */ + + if (tp->state == OPJWTS_ERROR) { + bRet = OPJ_FALSE; + } + + return bRet; +} + +/* +void opj_waiting() +{ + printf("waiting!\n"); +} +*/ + +static opj_worker_thread_job_t* opj_thread_pool_get_next_job( + opj_thread_pool_t* tp, + opj_worker_thread_t* worker_thread, + OPJ_BOOL signal_job_finished) +{ + while (OPJ_TRUE) { + opj_job_list_t* top_job_iter; + + opj_mutex_lock(tp->mutex); + + if (signal_job_finished) { + signal_job_finished = OPJ_FALSE; + tp->pending_jobs_count --; + /*printf("tp=%p, remaining jobs: %d\n", tp, tp->pending_jobs_count);*/ + if (tp->pending_jobs_count <= tp->signaling_threshold) { + opj_cond_signal(tp->cond); + } + } + + if (tp->state == OPJWTS_STOP) { + opj_mutex_unlock(tp->mutex); + return NULL; + } + top_job_iter = tp->job_queue; + if (top_job_iter) { + opj_worker_thread_job_t* job; + tp->job_queue = top_job_iter->next; + + job = top_job_iter->job; + opj_mutex_unlock(tp->mutex); + opj_free(top_job_iter); + return job; + } + + /* opj_waiting(); */ + if (!worker_thread->marked_as_waiting) { + opj_worker_thread_list_t* item; + + worker_thread->marked_as_waiting = OPJ_TRUE; + tp->waiting_worker_thread_count ++; + assert(tp->waiting_worker_thread_count <= tp->worker_threads_count); + + item = (opj_worker_thread_list_t*) opj_malloc(sizeof(opj_worker_thread_list_t)); + if (item == NULL) { + tp->state = OPJWTS_ERROR; + opj_cond_signal(tp->cond); + + opj_mutex_unlock(tp->mutex); + return NULL; + } + + item->worker_thread = worker_thread; + item->next = tp->waiting_worker_thread_list; + tp->waiting_worker_thread_list = item; + } + + /* printf("signaling that worker thread is ready\n"); */ + opj_cond_signal(tp->cond); + + opj_mutex_lock(worker_thread->mutex); + opj_mutex_unlock(tp->mutex); + + /* printf("waiting for job\n"); */ + opj_cond_wait(worker_thread->cond, worker_thread->mutex); + + opj_mutex_unlock(worker_thread->mutex); + /* printf("got job\n"); */ + } +} + +OPJ_BOOL opj_thread_pool_submit_job(opj_thread_pool_t* tp, + opj_job_fn job_fn, + void* user_data) +{ + opj_worker_thread_job_t* job; + opj_job_list_t* item; + + if (tp->mutex == NULL) { + job_fn(user_data, tp->tls); + return OPJ_TRUE; + } + + job = (opj_worker_thread_job_t*)opj_malloc(sizeof(opj_worker_thread_job_t)); + if (job == NULL) { + return OPJ_FALSE; + } + job->job_fn = job_fn; + job->user_data = user_data; + + item = (opj_job_list_t*) opj_malloc(sizeof(opj_job_list_t)); + if (item == NULL) { + opj_free(job); + return OPJ_FALSE; + } + item->job = job; + + opj_mutex_lock(tp->mutex); + + tp->signaling_threshold = 100 * tp->worker_threads_count; + while (tp->pending_jobs_count > tp->signaling_threshold) { + /* printf("%d jobs enqueued. Waiting\n", tp->pending_jobs_count); */ + opj_cond_wait(tp->cond, tp->mutex); + /* printf("...%d jobs enqueued.\n", tp->pending_jobs_count); */ + } + + item->next = tp->job_queue; + tp->job_queue = item; + tp->pending_jobs_count ++; + + if (tp->waiting_worker_thread_list) { + opj_worker_thread_t* worker_thread; + opj_worker_thread_list_t* next; + opj_worker_thread_list_t* to_opj_free; + + worker_thread = tp->waiting_worker_thread_list->worker_thread; + + assert(worker_thread->marked_as_waiting); + worker_thread->marked_as_waiting = OPJ_FALSE; + + next = tp->waiting_worker_thread_list->next; + to_opj_free = tp->waiting_worker_thread_list; + tp->waiting_worker_thread_list = next; + tp->waiting_worker_thread_count --; + + opj_mutex_lock(worker_thread->mutex); + opj_mutex_unlock(tp->mutex); + opj_cond_signal(worker_thread->cond); + opj_mutex_unlock(worker_thread->mutex); + + opj_free(to_opj_free); + } else { + opj_mutex_unlock(tp->mutex); + } + + return OPJ_TRUE; +} + +void opj_thread_pool_wait_completion(opj_thread_pool_t* tp, + int max_remaining_jobs) +{ + if (tp->mutex == NULL) { + return; + } + + if (max_remaining_jobs < 0) { + max_remaining_jobs = 0; + } + opj_mutex_lock(tp->mutex); + tp->signaling_threshold = max_remaining_jobs; + while (tp->pending_jobs_count > max_remaining_jobs) { + /*printf("tp=%p, jobs before wait = %d, max_remaining_jobs = %d\n", tp, tp->pending_jobs_count, max_remaining_jobs);*/ + opj_cond_wait(tp->cond, tp->mutex); + /*printf("tp=%p, jobs after wait = %d\n", tp, tp->pending_jobs_count);*/ + } + opj_mutex_unlock(tp->mutex); +} + +int opj_thread_pool_get_thread_count(opj_thread_pool_t* tp) +{ + return tp->worker_threads_count; +} + +void opj_thread_pool_destroy(opj_thread_pool_t* tp) +{ + if (!tp) { + return; + } + if (tp->cond) { + int i; + opj_thread_pool_wait_completion(tp, 0); + + opj_mutex_lock(tp->mutex); + tp->state = OPJWTS_STOP; + opj_mutex_unlock(tp->mutex); + + for (i = 0; i < tp->worker_threads_count; i++) { + opj_mutex_lock(tp->worker_threads[i].mutex); + opj_cond_signal(tp->worker_threads[i].cond); + opj_mutex_unlock(tp->worker_threads[i].mutex); + opj_thread_join(tp->worker_threads[i].thread); + opj_cond_destroy(tp->worker_threads[i].cond); + opj_mutex_destroy(tp->worker_threads[i].mutex); + } + + opj_free(tp->worker_threads); + + while (tp->waiting_worker_thread_list != NULL) { + opj_worker_thread_list_t* next = tp->waiting_worker_thread_list->next; + opj_free(tp->waiting_worker_thread_list); + tp->waiting_worker_thread_list = next; + } + + opj_cond_destroy(tp->cond); + } + opj_mutex_destroy(tp->mutex); + opj_tls_destroy(tp->tls); + opj_free(tp); +} diff --git a/cpp/3rd_party/openjpeg/openjp2/thread.h b/cpp/3rd_party/openjpeg/openjp2/thread.h new file mode 100644 index 0000000000..c89e19b4a6 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/thread.h @@ -0,0 +1,256 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2016, Even Rouault + * 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 THREAD_H +#define THREAD_H + +#include "openjpeg.h" + +/** +@file thread.h +@brief Thread API + +The functions in thread.c have for goal to manage mutex, conditions, thread +creation and thread pools that accept jobs. +*/ + +/** @defgroup THREAD THREAD - Mutex, conditions, threads and thread pools */ +/*@{*/ + +/** @name Mutex */ +/*@{*/ + +/** Opaque type for a mutex */ +typedef struct opj_mutex_t opj_mutex_t; + +/** Creates a mutex. + * @return the mutex or NULL in case of error (can for example happen if the library + * is built without thread support) + */ +opj_mutex_t* opj_mutex_create(void); + +/** Lock/acquire the mutex. + * @param mutex the mutex to acquire. + */ +void opj_mutex_lock(opj_mutex_t* mutex); + +/** Unlock/release the mutex. + * @param mutex the mutex to release. + */ +void opj_mutex_unlock(opj_mutex_t* mutex); + +/** Destroy a mutex + * @param mutex the mutex to destroy. + */ +void opj_mutex_destroy(opj_mutex_t* mutex); + +/*@}*/ + +/** @name Condition */ +/*@{*/ + +/** Opaque type for a condition */ +typedef struct opj_cond_t opj_cond_t; + +/** Creates a condition. + * @return the condition or NULL in case of error (can for example happen if the library + * is built without thread support) + */ +opj_cond_t* opj_cond_create(void); + +/** Wait for the condition to be signaled. + * The semantics is the same as the POSIX pthread_cond_wait. + * The provided mutex *must* be acquired before calling this function, and + * released afterwards. + * The mutex will be released by this function while it must wait for the condition + * and reacquired afterwards. + * In some particular situations, the function might return even if the condition is not signaled + * with opj_cond_signal(), hence the need to check with an application level + * mechanism. + * + * Waiting thread : + * \code + * opj_mutex_lock(mutex); + * while( !some_application_level_condition ) + * { + * opj_cond_wait(cond, mutex); + * } + * opj_mutex_unlock(mutex); + * \endcode + * + * Signaling thread : + * \code + * opj_mutex_lock(mutex); + * some_application_level_condition = TRUE; + * opj_cond_signal(cond); + * opj_mutex_unlock(mutex); + * \endcode + * + * @param cond the condition to wait. + * @param mutex the mutex (in acquired state before calling this function) + */ +void opj_cond_wait(opj_cond_t* cond, opj_mutex_t* mutex); + +/** Signal waiting threads on a condition. + * One of the thread waiting with opj_cond_wait() will be waken up. + * It is strongly advised that this call is done with the mutex that is used + * by opj_cond_wait(), in a acquired state. + * @param cond the condition to signal. + */ +void opj_cond_signal(opj_cond_t* cond); + +/** Destroy a condition + * @param cond the condition to destroy. + */ +void opj_cond_destroy(opj_cond_t* cond); + +/*@}*/ + +/** @name Thread */ +/*@{*/ + +/** Opaque type for a thread handle */ +typedef struct opj_thread_t opj_thread_t; + +/** User function to execute in a thread + * @param user_data user data provided with opj_thread_create() + */ +typedef void (*opj_thread_fn)(void* user_data); + +/** Creates a new thread. + * @param thread_fn Function to run in the new thread. + * @param user_data user data provided to the thread function. Might be NULL. + * @return a thread handle or NULL in case of failure (can for example happen if the library + * is built without thread support) + */ +opj_thread_t* opj_thread_create(opj_thread_fn thread_fn, void* user_data); + +/** Wait for a thread to be finished and release associated resources to the + * thread handle. + * @param thread the thread to wait for being finished. + */ +void opj_thread_join(opj_thread_t* thread); + +/*@}*/ + +/** @name Thread local storage */ +/*@{*/ +/** Opaque type for a thread local storage */ +typedef struct opj_tls_t opj_tls_t; + +/** Get a thread local value corresponding to the provided key. + * @param tls thread local storage handle + * @param key key whose value to retrieve. + * @return value associated with the key, or NULL is missing. + */ +void* opj_tls_get(opj_tls_t* tls, int key); + +/** Type of the function used to free a TLS value */ +typedef void (*opj_tls_free_func)(void* value); + +/** Set a thread local value corresponding to the provided key. + * @param tls thread local storage handle + * @param key key whose value to set. + * @param value value to set (may be NULL). + * @param free_func function to call currently installed value. + * @return OPJ_TRUE if successful. + */ +OPJ_BOOL opj_tls_set(opj_tls_t* tls, int key, void* value, + opj_tls_free_func free_func); + +/*@}*/ + +/** @name Thread pool */ +/*@{*/ + +/** Opaque type for a thread pool */ +typedef struct opj_thread_pool_t opj_thread_pool_t; + +/** Create a new thread pool. + * num_thread must nominally be >= 1 to create a real thread pool. If num_threads + * is negative or null, then a dummy thread pool will be created. All functions + * operating on the thread pool will work, but job submission will be run + * synchronously in the calling thread. + * + * @param num_threads the number of threads to allocate for this thread pool. + * @return a thread pool handle, or NULL in case of failure (can for example happen if the library + * is built without thread support) + */ +opj_thread_pool_t* opj_thread_pool_create(int num_threads); + +/** User function to execute in a thread + * @param user_data user data provided with opj_thread_create() + * @param tls handle to thread local storage + */ +typedef void (*opj_job_fn)(void* user_data, opj_tls_t* tls); + + +/** Submit a new job to be run by one of the thread in the thread pool. + * The job ( thread_fn, user_data ) will be added in the queue of jobs managed + * by the thread pool, and run by the first thread that is no longer busy. + * + * @param tp the thread pool handle. + * @param job_fn Function to run. Must not be NULL. + * @param user_data User data provided to thread_fn. + * @return OPJ_TRUE if the job was successfully submitted. + */ +OPJ_BOOL opj_thread_pool_submit_job(opj_thread_pool_t* tp, opj_job_fn job_fn, + void* user_data); + +/** Wait that no more than max_remaining_jobs jobs are remaining in the queue of + * the thread pool. The aim of this function is to avoid submitting too many + * jobs while the thread pool cannot cope fast enough with them, which would + * result potentially in out-of-memory situations with too many job descriptions + * being queued. + * + * @param tp the thread pool handle + * @param max_remaining_jobs maximum number of jobs allowed to be queued without waiting. + */ +void opj_thread_pool_wait_completion(opj_thread_pool_t* tp, + int max_remaining_jobs); + +/** Return the number of threads associated with the thread pool. + * + * @param tp the thread pool handle. + * @return number of threads associated with the thread pool. + */ +int opj_thread_pool_get_thread_count(opj_thread_pool_t* tp); + +/** Destroy a thread pool. + * @param tp the thread pool handle. + */ +void opj_thread_pool_destroy(opj_thread_pool_t* tp); + +/*@}*/ + +/*@}*/ + +#endif /* THREAD_H */ diff --git a/cpp/3rd_party/openjpeg/openjp2/tls_keys.h b/cpp/3rd_party/openjpeg/openjp2/tls_keys.h new file mode 100644 index 0000000000..23f8475418 --- /dev/null +++ b/cpp/3rd_party/openjpeg/openjp2/tls_keys.h @@ -0,0 +1,37 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2016, Even Rouault + * 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 OPJ_TLS_KEYS_H +#define OPJ_TLS_KEYS_H + +#define OPJ_TLS_KEY_T1 0 + +#endif /* OPJ_TLS_KEY_H */ diff --git a/cpp/3rd_party/rnifti/CMakeLists.txt b/cpp/3rd_party/rnifti/CMakeLists.txt new file mode 100644 index 0000000000..fc7c2f1286 --- /dev/null +++ b/cpp/3rd_party/rnifti/CMakeLists.txt @@ -0,0 +1,7 @@ +project(argus) + +ADD_LIBRARY(rnifti1 "znzlib/znzlib.cpp" "niftilib/nifti1_io.cpp") +ADD_LIBRARY(rnifti2 "znzlib/znzlib.cpp" "niftilib/nifti2_io.cpp") + +target_include_directories(rnifti1 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIRS}) +target_include_directories(rnifti2 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIRS}) diff --git a/cpp/3rd_party/rnifti/RNifti.h b/cpp/3rd_party/rnifti/RNifti.h new file mode 100644 index 0000000000..2327b60124 --- /dev/null +++ b/cpp/3rd_party/rnifti/RNifti.h @@ -0,0 +1,64 @@ +#ifndef _RNIFTI_H_ +#define _RNIFTI_H_ + +// RNiftyReg and divest have used HAVE_R, so accept this variant for compatibility +#if !defined(USING_R) && defined(HAVE_R) +#define USING_R +#endif + +// Defined since RNifti v0.10.0, and equal to 100 * (major version) + (minor version). May not +// change if the API does not change, and in particular never changes with patch level +#define RNIFTI_VERSION 104 + +// Versions 1 and 2 of the NIfTI reference library are mutually incompatible, but RNifti does some +// work to get them to play nicely: +// +// - The compile-time constant RNIFTI_NIFTILIB_VERSION indicates which version of the library has +// precedence. nifti1_io.h sets this to 1, and nifti2.io.h to 2, so the first-included header +// wins unless the user sets a value explicitly. +// - nifti_image is aliased to the appropriate struct type according to the library version in use. +// - Library functions with the same name but different signatures in the two versions are renamed +// to use "nifti2" in place of "nifti" in the version 2 library. They are aliased back to their +// original names if RNIFTI_NIFTILIB_VERSION is 2 and NO_REMAP_NIFTI2_FUNCTIONS *is not* defined. +// - Library functions that are essentially the same in the two versions are fenced out of +// nifti1_io.c (if RNIFTI_NIFTILIB_DEDUPLICATE is defined), to avoid duplicate symbols in the +// compiled package library. +// +// There are therefore several possible modes of usage: +// +// 1. Standalone programs that include RNifti.h can *first* define RNIFTI_NIFTILIB_VERSION to +// choose the library version required (the default is 1). They should link against nifti1_io.o +// or nifti2_io.o, accordingly. (A mismatch will result in compiler/linker errors.) See the +// "standalone" directory for an example. +// 2. Standalone or linked R package C/C++ code can include "niftilib/nifti1_io.h" or "niftilib/ +// nifti2_io.h", use the appropriate version of the library, and not worry about the clash. This +// will make most sense for existing code already written for one or other version of the +// NIfTI library. Standalone code will again need to link to the appropriate object file; R will +// handle linkage for packages, but the API header "RNiftiAPI.h" must also be included. See the +// "clients" directory for an example of the latter. +// 3. Code that explicitly wants to handle both versions of the library should define +// NO_REMAP_NIFTI2_FUNCTIONS to avoid name clashes, include both library headers, and use +// nifti2_* functions explicitly when required. +#if !defined(RNIFTI_NIFTILIB_VERSION) || (RNIFTI_NIFTILIB_VERSION == 1) +#include "niftilib/nifti1_io.h" +#include "niftilib/nifti2_image.h" +#else +#include "niftilib/nifti2_io.h" +#endif + +#ifdef __cplusplus +#include "RNifti/NiftiImage.h" + +// Defined since RNifti v0.3.0 +#define HAVE_RNIFTI_NAMESPACE + +extern "C" { +#endif // __cplusplus + +extern void niftilib_register_all (void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif diff --git a/cpp/3rd_party/rnifti/RNifti/NiftiImage.h b/cpp/3rd_party/rnifti/RNifti/NiftiImage.h new file mode 100644 index 0000000000..a91a21f148 --- /dev/null +++ b/cpp/3rd_party/rnifti/RNifti/NiftiImage.h @@ -0,0 +1,1959 @@ +#ifndef _NIFTI_IMAGE_H_ +#define _NIFTI_IMAGE_H_ + + +#ifdef USING_R + +#include + +// Defined since R 3.1.0, according to Tomas Kalibera, but there's no reason to break +// compatibility with 3.0.x +#ifndef MAYBE_SHARED +#define MAYBE_SHARED(x) (NAMED(x) > 1) +#endif + +#else + +#define R_NegInf -INFINITY + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif + +/** + * @mainpage RNifti: Fast R and C++ Access to NIfTI Images + * A more extensive overview of the \c RNifti package, and its usage from R, is provided on the + * package's GitHub page at \c https://github.com/jonclayden/RNifti. The primary role of these + * pages is to document the \ref RNifti::NiftiImage and \ref RNifti::NiftiImageData C++ classes + * for package developers linking to \c RNifti. +**/ + +namespace RNifti { + +typedef std::complex complex64_t; +typedef std::complex complex128_t; + +/** + * Simple RGB(A) type encapsulating an 8-bit colour value with optional opacity, which can also be + * set or retrieved as a single 32-bit integer. The default value is equivalent to zero, a fully + * transparent black. + * @author Jon Clayden () +**/ +struct rgba32_t +{ + union ValueType { + int packed; + unsigned char bytes[4]; + }; + ValueType value; + rgba32_t () { value.packed = 0; } +}; + +/** + * Wrapper class encapsulating a NIfTI data blob, with responsibility for handling data scaling + * and polymorphism. This class provides read/write data access, iterators, etc., which internally + * handle conversion to and from the data's native type. It can be linked to the data in a + * \c nifti_image or used independently. + * @author Jon Clayden () +**/ +class NiftiImageData +{ +public: + double slope; /**< The slope term used to scale data values. Ignored if zero. */ + double intercept; /**< The intercept term used to scale data values */ + +protected: + /** + * Abstract inner class defining the type-specific functions required in concrete subclasses + **/ + struct TypeHandler + { + virtual ~TypeHandler() {} + virtual size_t size () const { return 0; } + virtual bool hasNaN () const { return false; } + virtual complex128_t getComplex (void *ptr) const { return complex128_t(0.0, 0.0); } + virtual double getDouble (void *ptr) const { return 0.0; } + virtual int getInt (void *ptr) const { return 0; } + virtual rgba32_t getRgb (void *ptr) const { return rgba32_t(); } + virtual void setComplex (void *ptr, const complex128_t value) const {} + virtual void setDouble (void *ptr, const double value) const {} + virtual void setInt (void *ptr, const int value) const {} + virtual void setRgb (void *ptr, const rgba32_t value) const {} + virtual void minmax (void *ptr, const size_t length, double *min, double *max) const { *min = 0.0; *max = 0.0; } + }; + + /** + * Concrete inner class template defining behaviour specific to individual data types + **/ + template + struct ConcreteTypeHandler : public TypeHandler + { + size_t size () const { return (sizeof(Type)); } + bool hasNaN () const { return std::numeric_limits::has_quiet_NaN; } + complex128_t getComplex (void *ptr) const { return complex128_t(static_cast(*static_cast(ptr)), 0.0); } + double getDouble (void *ptr) const { return static_cast(*static_cast(ptr)); } + int getInt (void *ptr) const { return static_cast(*static_cast(ptr)); } + void setComplex (void *ptr, const complex128_t value) const + { + *(static_cast(ptr)) = Type(value.real()); + *(static_cast(ptr) + 1) = Type(0); + } + void setDouble (void *ptr, const double value) const { *(static_cast(ptr)) = Type(value); } + void setInt (void *ptr, const int value) const { *(static_cast(ptr)) = Type(value); } + void minmax (void *ptr, const size_t length, double *min, double *max) const; + }; + + template + struct ConcreteTypeHandler,false> : public TypeHandler + { + size_t size () const { return (sizeof(ElementType) * 2); } + bool hasNaN () const { return std::numeric_limits::has_quiet_NaN; } + std::complex getNative (void *ptr) const + { + const ElementType real = *static_cast(ptr); + const ElementType imag = *(static_cast(ptr) + 1); + return std::complex(real, imag); + } + void setNative (void *ptr, const std::complex native) const + { + *(static_cast(ptr)) = native.real(); + *(static_cast(ptr) + 1) = native.imag(); + } + complex128_t getComplex (void *ptr) const { return complex128_t(getNative(ptr)); } + double getDouble (void *ptr) const { return static_cast(getNative(ptr).real()); } + int getInt (void *ptr) const { return static_cast(getNative(ptr).real()); } + void setComplex (void *ptr, const complex128_t value) const { setNative(ptr, std::complex(value)); } + void setDouble (void *ptr, const double value) const { setNative(ptr, std::complex(value, 0.0)); } + void setInt (void *ptr, const int value) const { setNative(ptr, std::complex(static_cast(value), 0.0)); } + void minmax (void *ptr, const size_t length, double *min, double *max) const; + }; + + template + struct ConcreteTypeHandler : public TypeHandler + { + size_t size () const { return alpha ? 4 : 3; } + int getInt (void *ptr) const { return getRgb(ptr).value.packed; } + rgba32_t getRgb (void *ptr) const + { + rgba32_t value; + unsigned char *source = static_cast(ptr); + std::copy(source, source + (alpha ? 4 : 3), value.value.bytes); + return value; + } + void setInt (void *ptr, const int value) const + { + rgba32_t native; + native.value.packed = value; + setRgb(ptr, native); + } + void setRgb (void *ptr, const rgba32_t value) const + { + unsigned char *target = static_cast(ptr); + std::copy(value.value.bytes, value.value.bytes + (alpha ? 4 : 3), target); + } + void minmax (void *ptr, const size_t length, double *min, double *max) const { *min = 0.0; *max = 255.0; } + }; + + /** + * Create a concrete type handler appropriate to the datatype code stored with the data + * @return The newly allocated type handler, or \c NULL + * @exception runtime_error If the current datatype is unsupported + **/ + TypeHandler * createHandler () + { + if (_datatype == DT_NONE) + return NULL; + + switch (_datatype) + { + case DT_UINT8: return new ConcreteTypeHandler(); break; + case DT_INT16: return new ConcreteTypeHandler(); break; + case DT_INT32: return new ConcreteTypeHandler(); break; + case DT_FLOAT32: return new ConcreteTypeHandler(); break; + case DT_FLOAT64: return new ConcreteTypeHandler(); break; + case DT_INT8: return new ConcreteTypeHandler(); break; + case DT_UINT16: return new ConcreteTypeHandler(); break; + case DT_UINT32: return new ConcreteTypeHandler(); break; + case DT_INT64: return new ConcreteTypeHandler(); break; + case DT_UINT64: return new ConcreteTypeHandler(); break; + case DT_COMPLEX64: return new ConcreteTypeHandler(); break; + case DT_COMPLEX128: return new ConcreteTypeHandler(); break; + case DT_RGB24: return new ConcreteTypeHandler(); break; + case DT_RGBA32: return new ConcreteTypeHandler(); break; + + default: + throw std::runtime_error("Unsupported data type (" + std::string(nifti_datatype_string(_datatype)) + ")"); + } + } + + void *dataPtr; /**< Opaque pointer to the underlying data blob */ + int _datatype; /**< Datatype code indicating the actual type of the elements */ + TypeHandler *handler; /**< Type handler, which is created to match the datatype */ + size_t _length; /**< The number of data elements in the blob */ + bool owner; /**< An indicator of whether this object is responsible for cleaning up the data */ + + /** + * Initialiser method, used by constructors + * @param data Pointer to a preallocated data blob, or \c NULL + * @param length Number of elements in the blob + * @param datatype NIfTI datatype code appropriate to the blob + * @param slope Slope parameter for scaling values + * @param intercept Intercept parameter for scaling values + * @param alloc If \c true, the default, and \c data is \c NULL, memory will be allocated for + * the blob. If \c false, the blob will be \c NULL in this case + **/ + void init (void *data, const size_t length, const int datatype, const double slope, const double intercept, const bool alloc = true) + { + this->_length = length; + this->_datatype = datatype; + this->slope = slope; + this->intercept = intercept; + + owner = false; + handler = createHandler(); + if (handler == NULL) + dataPtr = NULL; + else if (alloc && data == NULL) + { + dataPtr = calloc(length, handler->size()); + owner = true; + } + else + dataPtr = data; + } + + /** + * Update the slope and intercept to cover the range of another data object. If the current + * object's datatype can capture the required range without scaling, the slope and intercept + * are simply reset + * @param data Another data object + **/ + void calibrateFrom (const NiftiImageData &data) + { + slope = 1.0; + intercept = 0.0; + + if (this->isInteger()) + { + double dataMin, dataMax, typeMin, typeMax; + data.minmax(&dataMin, &dataMax); + handler->minmax(NULL, 0, &typeMin, &typeMax); + + // If the source type is floating-point but values are in range, we will just round them + if (dataMin < typeMin || dataMax > typeMax) + { + slope = (dataMax - dataMin) / (typeMax - typeMin); + intercept = dataMin - (slope) * typeMin; + } + } + } + +public: + /** + * Inner class representing a single element in the data blob + **/ + struct Element + { + private: + const NiftiImageData &parent; + void *ptr; + + public: + /** + * Primary constructor + * @param parent A reference to the parent object + * @param ptr An opaque pointer to the element. If \c NULL, the start of the data blob + * encapsulated by the parent will be used + **/ + Element (const NiftiImageData &parent, void *ptr = NULL) + : parent(parent) + { + this->ptr = (ptr == NULL ? parent.dataPtr : ptr); + } + + /** + * Copy assignment operator + * @param value The value to assign. Any basic numeric type supported by NIfTI-1 is + * allowed, but \c int is used as an intermediate type for all integers, so values + * unrepresentable in a signed 32-bit integer may overflow + * @return A reference to the callee + **/ + template + Element & operator= (const SourceType &value); + + /** + * Copy assignment operator + * @param other Another data element + * @return A reference to the callee + **/ + Element & operator= (const Element &other); + + /** + * Type-cast operator, suitable for implicit conversion to basic numeric types + **/ + template + operator TargetType() const + { + if (parent.isScaled()) + return TargetType(parent.handler->getDouble(ptr) * parent.slope + parent.intercept); + else if (std::numeric_limits::is_integer) + return TargetType(parent.handler->getInt(ptr)); + else + return TargetType(parent.handler->getDouble(ptr)); + } + + template + operator std::complex() const + { + if (parent.isScaled()) + return std::complex(parent.handler->getComplex(ptr) * parent.slope + complex128_t(parent.intercept, parent.intercept)); + else + return std::complex(parent.handler->getComplex(ptr)); + } + +#ifdef USING_R + /** + * \c Rcomplex type-cast operator, allowing data to be copied straight to a CPLXSXP + **/ + operator Rcomplex() const + { + const complex128_t value = parent.handler->getComplex(ptr); + Rcomplex rValue = { value.real(), value.imag() }; + if (parent.isScaled()) + { + rValue.r = rValue.r * parent.slope + parent.intercept; + rValue.i = rValue.i * parent.slope + parent.intercept; + } + return rValue; + } +#endif + + operator rgba32_t() const + { + return parent.handler->getRgb(ptr); + } + }; + + /** + * Iterator type for \c NiftiImageData, with \c Element as its value type + **/ + class Iterator : public std::iterator + { + private: + const NiftiImageData *parent; + void *ptr; + size_t step; + + public: + /** + * Primary constructor + * @param parent A reference to the parent object + * @param ptr An opaque pointer to the memory underpinning the iterator + * @param step The increment between elements within the blob, in bytes. If zero, the + * default, the width associated with the stored datatype will be used. + **/ + Iterator (const NiftiImageData *parent = NULL, void *ptr = NULL, const size_t step = 0) + : parent(parent) + { + this->ptr = (ptr == NULL ? parent->dataPtr : ptr); + this->step = (step == 0 ? parent->handler->size() : step); + } + + /** + * Copy constructor + * @param other Another iterator + **/ + Iterator (const Iterator &other) + : parent(other.parent), ptr(other.ptr), step(other.step) {} + + Iterator & operator++ () { ptr = static_cast(ptr) + step; return *this; } + Iterator operator++ (int) { Iterator copy(*this); ptr = static_cast(ptr) + step; return copy; } + Iterator operator+ (ptrdiff_t n) const + { + void *newptr = static_cast(ptr) + (n * step); + return Iterator(parent, newptr, step); + } + Iterator & operator-- () { ptr = static_cast(ptr) - step; return *this; } + Iterator operator-- (int) { Iterator copy(*this); ptr = static_cast(ptr) - step; return copy; } + Iterator operator- (ptrdiff_t n) const + { + void *newptr = static_cast(ptr) - (n * step); + return Iterator(parent, newptr, step); + } + + ptrdiff_t operator- (const Iterator &other) const + { + const ptrdiff_t difference = static_cast(ptr) - static_cast(other.ptr); + return difference / step; + } + + bool operator== (const Iterator &other) const { return (ptr==other.ptr && step==other.step); } + bool operator!= (const Iterator &other) const { return (ptr!=other.ptr || step!=other.step); } + bool operator> (const Iterator &other) const { return (ptr > other.ptr); } + bool operator< (const Iterator &other) const { return (ptr < other.ptr); } + + const Element operator* () const { return Element(*parent, ptr); } + Element operator* () { return Element(*parent, ptr); } + const Element operator[] (const size_t i) const { return Element(*parent, static_cast(ptr) + (i * step)); } + Element operator[] (const size_t i) { return Element(*parent, static_cast(ptr) + (i * step)); } + }; + + /** + * Default constructor, creating an empty data object + **/ + NiftiImageData () + : slope(1.0), intercept(0.0), dataPtr(NULL), _datatype(DT_NONE), handler(NULL), _length(0), owner(false) {} + + /** + * Primary constructor + * @param data A pointer to a pre-allocated data blob, or \c NULL. In the latter case, memory + * will be allocated by the object, and cleaned up at destruction unless it is disowned + * @param length The number of elements in the blob + * @param datatype The NIfTI datatype code corresponding to the type of the data elements + * @param slope The slope parameter to use for data scaling, if any + * @param intercept The intercept parameter to use for data scaling, if any + **/ + NiftiImageData (void *data, const size_t length, const int datatype, const double slope = 1.0, const double intercept = 0.0) + { + init(data, length, datatype, slope, intercept); + } + + /** + * Convenience constructor for a \c nifti_image + * @param image The image struct whose data the object will wrap + **/ + NiftiImageData (nifti_image *image) + { + if (image == NULL) + init(NULL, 0, DT_NONE, 0.0, 0.0, false); + else + init(image->data, image->nvox, image->datatype, static_cast(image->scl_slope), static_cast(image->scl_inter), false); + } + + /** + * Copy constructor with optional type conversion + * @param source Another \c NiftiImageData object to copy data from + * @param datatype The datatype to convert to, or \c DT_NONE, the default, for no conversion. + * If the range of the source data cannot be represented by the chosen type, the slope and + * intercept parameters will be set to adjust the range + **/ + NiftiImageData (const NiftiImageData &source, const int datatype = DT_NONE) + { + init(NULL, source.length(), datatype == DT_NONE ? source.datatype() : datatype, source.slope, source.intercept); + + if (datatype == DT_NONE || datatype == source.datatype()) + memcpy(dataPtr, source.dataPtr, source.totalBytes()); + else + { + calibrateFrom(source); + std::copy(source.begin(), source.end(), this->begin()); + } + } + + /** + * Iterator-based constructor + * @param from Iterator type representing the start of the source data to be copied + * @param to Iterator type representing the end of the source data to be copied + * @param datatype The NIfTI datatype to use within the data blob + **/ + template + NiftiImageData (InputIterator from, InputIterator to, const int datatype) + { + const size_t length = static_cast(std::distance(from, to)); + init(NULL, length, datatype, 1.0, 0.0); + std::copy(from, to, this->begin()); + } + + /** + * Destructor which frees the type handler, and the data blob if it is owned by this object + **/ + virtual ~NiftiImageData () + { + delete handler; + if (owner) + free(dataPtr); + } + + /** + * Copy assignment operator + * @param source Another \c NiftiImageData object, from which the data and metadata are copied + * @return A reference to the callee + **/ + NiftiImageData & operator= (const NiftiImageData &source) + { + if (source.dataPtr != NULL) + { + // Free the old data, if we allocated it + if (owner) + free(dataPtr); + init(NULL, source.length(), source.datatype(), source.slope, source.intercept); + memcpy(dataPtr, source.dataPtr, source.totalBytes()); + } + return *this; + } + + void * blob () const { return dataPtr; } /**< Return an opaque pointer to the blob */ + int datatype () const { return _datatype; } /**< Return stored datatype code */ + size_t length () const { return _length; } /**< Return the number of elements in the data */ + size_t size () const { return _length; } /**< Return the number of elements in the data */ + + /** Return the number of bytes used per element, or zero if the datatype is undefined or the blob is \c NULL */ + size_t bytesPerPixel () const { return (handler == NULL ? 0 : handler->size()); } + + /** Return the total size of the data blob, in bytes */ + size_t totalBytes () const { return _length * bytesPerPixel(); } + + /** + * Determine whether or not the object is empty + * @return \c true if the data pointer is \c NULL; \c false otherwise + **/ + bool isEmpty () const { return (dataPtr == NULL); } + + /** + * Determine whether the object uses data scaling + * @return \c true if the slope and intercept parameters are set to nontrivial values; + \c false otherwise + **/ + bool isScaled () const { return (slope != 0.0 && (slope != 1.0 || intercept != 0.0)); } + + /** + * Determine whether the datatype is complex + * @return \c true if the data represents complex floating point values; \c false otherwise + **/ + bool isComplex () const { return (_datatype == DT_COMPLEX64 || _datatype == DT_COMPLEX128); } + + /** + * Determine whether the datatype is floating point + * @return \c true if the data represents 32-bit or 64-bit floating point values; \c false + * otherwise + **/ + bool isFloatingPoint () const { return (_datatype == DT_FLOAT32 || _datatype == DT_FLOAT64); } + + /** + * Determine whether the datatype is an integer type + * @return \c true if the data represents integers; \c false otherwise + **/ + bool isInteger () const { return nifti_is_inttype(_datatype); } + + /** + * Determine whether the datatype corresponds to an RGB type + * @return \c true if the data represents RGB colour values; \c false otherwise + **/ + bool isRgb () const { return (_datatype == DT_RGB24 || _datatype == DT_RGBA32); } + + /** + * Return a similar object to the callee, but with the slope and intercept values reset + * @return A new \c NiftiImageData object, pointing to the same memory as the callee + **/ + NiftiImageData unscaled () const { return NiftiImageData(dataPtr, _length, _datatype); } + + /** + * Disown the data blob, removing responsibility for freeing it upon destruction + * @return A reference to the modified callee + **/ + NiftiImageData & disown () { this->owner = false; return *this; } + + /** Obtain a constant iterator corresponding to the start of the blob */ + const Iterator begin () const { return Iterator(this); } + + /** Obtain a constant iterator corresponding to the end of the blob */ + const Iterator end () const { return Iterator(this, static_cast(dataPtr) + totalBytes()); } + + /** Obtain a mutable iterator corresponding to the start of the blob */ + Iterator begin () { return Iterator(this); } + + /** Obtain a mutable iterator corresponding to the end of the blob */ + Iterator end () { return Iterator(this, static_cast(dataPtr) + totalBytes()); } + + /** + * Indexing operator, returning a constant element + * @param i Index value, where the first dimension moves fastest + * @return Constant element proxy type + **/ + const Element operator[] (const size_t i) const { return Element(*this, static_cast(dataPtr) + (i * bytesPerPixel())); } + + /** + * Indexing operator, returning a mutable element + * @param i Index value, where the first dimension moves fastest + * @return Mutable element proxy type + **/ + Element operator[] (const size_t i) { return Element(*this, static_cast(dataPtr) + (i * bytesPerPixel())); } + + /** + * Calculate the minimum and maximum values in the blob, as doubles + * @param min Pointer to the minimum value (output parameter). Will be set to zero if the + * datatype is unknown or the data is empty + * @param max Pointer to the maximum value (output parameter). Will be set to zero if the + * datatype is unknown or the data is empty + **/ + void minmax (double *min, double *max) const + { + if (handler == NULL) + { + *min = 0.0; + *max = 0.0; + } + else + handler->minmax(dataPtr, _length, min, max); + } +}; + + +// R provides an NaN (NA) value for integers +#ifdef USING_R +template <> +inline bool NiftiImageData::ConcreteTypeHandler::hasNaN () const { return true; } +#endif + + +/** + * A simple object-oriented wrapper around a fixed-length array. + * @author Jon Clayden () +**/ +template +class Vector +{ +protected: + ElementType elements[Length]; + +public: + /** + * Initialise with a fixed element value, defaulting to zero + **/ + Vector (const ElementType value = 0.0) + { + std::fill(elements, elements + Length, value); + } + + /** + * Initialise from a C-style array of the appropriate type and length + **/ + Vector (const ElementType * source) + { + std::copy(source, source + Length, this->elements); + } + + /** + * Unary negation operator, which reverses the signs of all elements + **/ + Vector operator- () const + { + Vector result; + for (int i=0; i) +**/ +template +class SquareMatrix +{ +protected: + ElementType elements[Order*Order]; /**< The underlying raw data elements, stored row-major for consistency with niftilib */ + + /** + * Obtain a pointer to a NIfTI-style \c mat44 or \c dmat44 encapsulating the same data as this + * object. + */ + NiftiType * niftiPointer () const { return (NiftiType *) elements; } + + /** + * Copy the data elements into a new NIfTI-style \c mat44 or \c dmat44. + */ + NiftiType niftiCopy () const + { + NiftiType value; + std::copy(elements, elements + Order*Order, *value.m); + return value; + } + +public: + typedef NiftiType NativeType; /**< The niftilib structure type corresponding to this matrix */ + typedef SquareMatrix MatrixType; /**< Type alias for the current specialisation */ + typedef Vector VectorType; /**< Type of vectors for which this matrix is a linear operator */ + + /** + * Initialise with a fixed element value, defaulting to zero + **/ + SquareMatrix (const ElementType value = 0.0) + { + std::fill(elements, elements + Order*Order, value); + } + + /** + * Initialise from a C-style array of the appropriate type and length + **/ + SquareMatrix (const ElementType * source) + { + std::copy(source, source + Order*Order, this->elements); + } + + /** + * Initialise from the appropriate niftilib type + **/ + SquareMatrix (const NiftiType &source) + { + const ElementType *castSource = (const ElementType *) *source.m; + std::copy(castSource, castSource + Order*Order, this->elements); + } + +#ifdef USING_R + /** + * Initialise from an R object representing a numeric matrix + **/ + SquareMatrix (SEXP source) + { + Rcpp::NumericMatrix matrix(source); + if (matrix.cols() != Order && matrix.rows() != Order) + throw std::runtime_error("Matrix does not have the expected dimensions"); + for (int i=0; i) +**/ +class NiftiImage +{ +public: +#if RNIFTI_NIFTILIB_VERSION == 1 + typedef int dim_t; /**< Type used for dimension elements */ + typedef float pixdim_t; /**< Type used for pixel dimension elements */ + typedef float scale_t; /**< Type used for scale elements */ +#elif RNIFTI_NIFTILIB_VERSION == 2 + typedef int64_t dim_t; /**< Type used for dimension elements */ + typedef double pixdim_t; /**< Type used for pixel dimension elements */ + typedef double scale_t; /**< Type used for scale elements */ +#endif + + /** + * Inner class referring to a subset of an image. Currently must refer to the last + * dimension in the image, i.e., a volume in a 4D parent image, or a slice in a 3D image + **/ + struct Block + { + const NiftiImage ℑ /**< The parent image */ + const int dimension; /**< The dimension along which the block applies (which should be the last) */ + const dim_t index; /**< The location along \c dimension */ + + /** + * Standard constructor for this class + * @param image The parent image + * @param dimension The dimension along which the block applies (which should be the last) + * @param index The location along \c dimension + * @exception runtime_error If \c dimension is not the last dimension in the image + **/ + Block (const NiftiImage &image, const int dimension, const dim_t index) + : image(image), dimension(dimension), index(index) + { + if (dimension != image->ndim) + throw std::runtime_error("Blocks must be along the last dimension in the image"); + } + + /** + * Copy assignment operator, which allows a block in one image to be replaced with + * the contents of another image + * @param source A \ref NiftiImage, containing the data to replace the block with + * @return A reference to the block + * @exception runtime_error If the \c source is incompatible with the block in size or + * datatype + **/ + Block & operator= (const NiftiImage &source) + { + if (source->datatype != image->datatype) + throw std::runtime_error("New data does not have the same datatype as the target block"); + if (source->scl_slope != image->scl_slope || source->scl_inter != image->scl_inter) + throw std::runtime_error("New data does not have the same scale parameters as the target block"); + + size_t blockSize = 1; + for (int i=1; idim[i]; + + if (blockSize != size_t(source->nvox)) + throw std::runtime_error("New data does not have the same size as the target block"); + + blockSize *= image->nbyper; + memcpy(static_cast(image->data) + blockSize*index, source->data, blockSize); + return *this; + } + + /** + * Obtain the data within the block + * @return A \c NiftiImageData object encapsulating the data + **/ + NiftiImageData data () const + { + if (image.isNull()) + return NiftiImageData(); + else + { + size_t blockSize = 1; + for (int i=1; idim[i]; + return NiftiImageData(static_cast(image->data) + blockSize * index * image->nbyper, blockSize, image->datatype, static_cast(image->scl_slope), static_cast(image->scl_inter)); + } + } + + /** + * Extract a vector of data from a block, casting it to any required element type + * @param useSlope If \c true, the default, then the data will be adjusted for the slope + * and intercept stored with the image, if any + * @note If the slope and intercept are applied, there is no guarantee that the adjusted + * values will fit within the requested type. No check is made for this + **/ + template + std::vector getData (const bool useSlope = true) const; + }; + + /** + * Inner class wrapping a NIfTI extension, a weakly-specified standard for attaching additional + * metadata to NIfTI-1 and NIfTI-2 images. + **/ + class Extension + { + protected: + nifti1_extension *ext; /**< The wrapped extension structure */ + + /** + * Copy an existing \c nifti1_extension structure into the object + * @param source A pointer to a \c nifti1_extension + **/ + void copy (const nifti1_extension *source); + + /** + * Copy the specified data buffer into the object + * @param data An array of data + * @param length The number of elements in \c data + * @param code The extension code to associate with the data + **/ + template + void copy (const SourceType *data, const size_t length, const int code); + + public: + /** + * Default constructor, wrapping \c NULL + **/ + Extension () + : ext(NULL) {} + + /** + * Initialise from an existing \c nifti1_extension (which is used by both NIfTI-1 and + * NIfTI-2 images), optionally copying the contents + * @param extension A pointer to a \c nifti1_extension + * @param copy If \c true, the contents of the extension are copied; otherwise the pointer + * is wrapped directly + **/ + Extension (nifti1_extension * const extension, const bool copy = false) + { + if (!copy || extension == NULL) + this->ext = extension; + else + this->copy(extension); + } + + /** + * Copy constructor + * @param source Another \c Extension object + **/ + Extension (const Extension &source) + { + copy(source.ext); + } + + /** + * Construct the object from its constituent parts + * @param data An array of data + * @param length The number of elements in \c data + * @param code The extension code to associate with the data + **/ + template + Extension (const SourceType *data, const size_t length, const int code) + { + copy(data, length, code); + } + +#ifdef USING_R + /** + * Construct the object from an atomic R object, copying the data into a new extension + * @param source An R object, which should be of an atomic type (integer, double, + * character, etc.) + * @param code The extension code to associate with the data. If -1, the default, a + * \c code attribute will be used, if available + **/ + Extension (SEXP source, int code = -1) + { + const Rcpp::RObject object(source); + if (code == -1 && object.hasAttribute("code")) + code = Rcpp::as(object.attr("code")); + + switch (object.sexp_type()) + { + case RAWSXP: copy(RAW(source), Rf_length(source), code); break; + case REALSXP: copy(REAL(source), Rf_length(source), code); break; + case CPLXSXP: copy(COMPLEX(source), Rf_length(source), code); break; + case INTSXP: copy(INTEGER(source), Rf_length(source), code); break; + case LGLSXP: copy(LOGICAL(source), Rf_length(source), code); break; + case STRSXP: + { + if (Rf_length(source) > 1) + Rf_warning("Character vector elements after the first will not be stored in a NIfTI extension"); + const char *string = CHAR(STRING_ELT(source, 0)); + copy(string, strlen(string), code); + break; + } + default: Rf_error("Unable to convert SEXP type %d to NIfTI extension", object.sexp_type()); + } + } +#endif + + /** + * Return the code associated with the extension + * @return An integer code giving the relevant code, or -1 if the extension is \c NULL + **/ + int code () const { return (ext == NULL ? -1 : ext->ecode); } + + /** + * Return the data blob associated with the extension + * @return The data, as a byte array + **/ + const char * data () const { return (ext == NULL ? NULL : ext->edata); } + + /** + * Return the length of the data array + * @return The length of the data array, in bytes + **/ + size_t length () const { return (ext == NULL || ext->esize < 8 ? 0 : size_t(ext->esize - 8)); } + + /** + * Return the length of the data array + * @return The length of the data array, in bytes + **/ + size_t size () const { return (ext == NULL || ext->esize < 8 ? 0 : size_t(ext->esize - 8)); } + +#ifdef USING_R + /** + * \c SEXP cast operator, which converts to R's raw vector type + **/ + operator SEXP () const + { + if (ext == NULL || ext->esize < 8) + return R_NilValue; + + const int length = ext->esize - 8; + Rcpp::RawVector result(length); + const Rbyte *source = (const Rbyte *) ext->edata; + std::copy(source, source+length, result.begin()); + result.attr("code") = ext->ecode; + return result; + } +#endif + }; + + /** + * Inner class representing an xform matrix, which indicates the orientation and other spatial + * properties of an image. Specifically, an xform is an affine transformation in 3D space, + * representing the conversion from the image's coordinate system to canonical "real-world" + * space. The header file \c nifti1.h contains authoritative documentation. + **/ + class Xform + { + public: +#if RNIFTI_NIFTILIB_VERSION == 1 + typedef float Element; /**< Scalar element type */ + typedef Vector Vector4; /**< 4-element vector type */ + typedef Vector Vector3; /**< 3-element vector type */ + typedef SquareMatrix Matrix; /**< 4x4 matrix type */ + typedef SquareMatrix Submatrix; /**< 3x3 matrix type */ +#elif RNIFTI_NIFTILIB_VERSION == 2 + typedef double Element; /**< Scalar element type */ + typedef Vector Vector4; /**< 4-element vector type */ + typedef Vector Vector3; /**< 3-element vector type */ + typedef SquareMatrix Matrix; /**< 4x4 matrix type */ + typedef SquareMatrix Submatrix; /**< 3x3 matrix type */ +#endif + + protected: + Element *forward, *inverse, *qparams; /**< Pointers to linked C-style arrays */ + Matrix mat; /**< The full xform matrix underpinning this object */ + + /** + * Replace the current matrix with a new one. This function propagates the changes to the + * linked arrays, if they are not \c NULL. + **/ + void replace (const Matrix &source); + + public: + /** + * Default constructor + **/ + Xform () + : forward(NULL), inverse(NULL), qparams(NULL), mat() {} + + /** + * Initialise from a 4x4 \ref SquareMatrix + **/ + Xform (const Matrix &source) + : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {} + + /** + * Initialise from a constant NIfTI \c mat44 or \c dmat44 + **/ + Xform (const Matrix::NativeType &source) + : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {} + + /** + * Initialise from a NIfTI \c mat44 or \c dmat44. The data in the linked matrix will be + * replaced if this object is updated. + **/ + Xform (Matrix::NativeType &source) + : forward(*source.m), inverse(NULL), qparams(NULL), mat(source) {} + + /** + * Initialise from forward and backward matrices, and optionally quaternion parameters. + * These will all be linked to the new object and replaced if it is updated. + **/ + Xform (Matrix::NativeType &source, Matrix::NativeType &inverse, Element *qparams = NULL) + : forward(*source.m), inverse(*inverse.m), qparams(qparams), mat(source) {} + +#ifdef USING_R + /** + * Initialise from an R numeric matrix object + **/ + Xform (SEXP source) + : forward(NULL), inverse(NULL), qparams(NULL), mat(Matrix(source)) {} +#endif + + /** + * Allows an \c Xform to be treated as a constant NIfTI matrix implicitly, making it + * directly compatible with API functions + **/ + operator const Matrix::NativeType () const { return mat; } + + /** + * Allows an \c Xform to be treated as a NIfTI matrix implicitly, making it directly + * compatible with API functions + **/ + operator Matrix::NativeType () { return mat; } + + /** + * Copy assignment operator, taking an \c Xform and replacing linked data + **/ + Xform & operator= (const Xform &source) + { + replace(source.mat); + return *this; + } + + /** + * Copy assignment operator, taking a \c SquareMatrix and replacing linked data + **/ + Xform & operator= (const Matrix &source) + { + replace(source); + return *this; + } + +#ifdef USING_R + /** + * Copy assignment operator, taking a \c SEXP and replacing linked data + **/ + Xform & operator= (SEXP source) + { + replace(Matrix(source)); + return *this; + } +#endif + + /** + * Access the xform matrix as an immutable \c SquareMatrix object + **/ + const Matrix & matrix () const { return mat; } + + /** + * Obtain the upper left 3x3 submatrix from the xform matrix + **/ + Submatrix submatrix () const; + + /** + * Obtain the 3x3 rotation matrix from the xform matrix, with scale and skew components + * removed + **/ + Submatrix rotation () const; + + /** + * Returns the \c qfac value, which should be 1 where the xform matrix represents a + * right-handed coordinate system (like \c RAS, the NIfTI default) and -1 for a left-handed + * system (like \c LAS, the ANALYZE default). Also see the \ref orientation method + **/ + Element handedness () const; + + /** + * Obtain the quaternion representation of the xform's rotation component + **/ + Vector4 quaternion () const; + + /** + * Obtain the translation component of the xform matrix + **/ + Vector3 offset () const; + + /** + * Obtain the pixel spacing of the image in each spatial dimension + **/ + Vector3 spacing () const; + + /** + * Obtain the approximate orientation of the image's coordinate frame, as a three-character + * string consisting of some permutation of the letters \c L or \c R (for left or right), + * \c P or \c A (for posterior or anterior) and \c I or \c S (for inferior or superior). + * These give the canonical axes most closely aligned with each of the three dimensions as + * stored + **/ + std::string orientation () const; + }; + +#ifdef USING_R + /** + * Convert between R \c SEXP object type and \c nifti_image datatype codes + * @param sexpType A numeric R \c SEXP type code + * @return A \c nifti_image datatype code + * @exception runtime_error If a non-numeric type is passed + **/ + static int sexpTypeToNiftiType (const int sexpType) + { + if (sexpType == INTSXP || sexpType == LGLSXP) + return DT_INT32; + else if (sexpType == REALSXP) + return DT_FLOAT64; + else if (sexpType == CPLXSXP) + return DT_COMPLEX128; + else + throw std::runtime_error("Array elements must be numeric"); + } +#endif + + /** + * Get the NIfTI format version used by the file at the specified path + * @param path A string specifying a file path + * @return An integer: -1 if the file is not present or not valid, 0 for ANALYZE-7.5, or + * a value greater than 0 for NIfTI + **/ + static int fileVersion (const std::string &path); + + +protected: + nifti_image *image; /**< The wrapped \c nifti_image pointer */ + int *refCount; /**< A reference counter, shared with other objects wrapping the same pointer */ + + /** + * Acquire the specified pointer to a \c nifti_image \c struct, taking (possibly shared) + * responsibility for freeing the associated memory. If the object currently wraps another + * pointer, it will be released + * @param image The pointer to wrap + **/ + void acquire (nifti_image * const image); + + /** + * Acquire the same pointer as another \c NiftiImage, incrementing the shared reference count + * @param source A reference to a \c NiftiImage + **/ + void acquire (const NiftiImage &source) + { + refCount = source.refCount; + acquire(source.image); + } + + /** + * Release the currently wrapped pointer, if it is not \c NULL, decrementing the reference + * count and releasing memory if there are no remaining references to the pointer + **/ + void release (); + + /** + * Copy the contents of a \c nifti_image to create a new image, acquiring the new pointer + * @param source A pointer to a \c nifti_image + **/ + void copy (const nifti_image *source); + + /** + * Copy the contents of another \c NiftiImage to create a new image, acquiring a new pointer + * @param source A reference to a \c NiftiImage + **/ + void copy (const NiftiImage &source); + + /** + * Copy the contents of a \ref Block to create a new image, acquiring a new pointer + * @param source A reference to a \ref Block + **/ + void copy (const Block &source); + + +#ifdef USING_R + + /** + * Initialise the object from an S4 object of class \c "nifti" + * @param object The source object + * @param copyData If \c true, the data are copied in; otherwise just the metadata is extracted + **/ + void initFromNiftiS4 (const Rcpp::RObject &object, const bool copyData = true); + + /** + * Initialise the object from a reference object of class \c "MriImage" + * @param object The source object + * @param copyData If \c true, the data are copied in; otherwise just the metadata is extracted + **/ + void initFromMriImage (const Rcpp::RObject &object, const bool copyData = true); + + /** + * Initialise the object from an R list with named elements, which can only contain metadata + * @param object The source object + **/ + void initFromList (const Rcpp::RObject &object); + + /** + * Initialise the object from an R array + * @param object The source object + * @param copyData If \c true, the data are copied in; otherwise just the metadata is extracted + **/ + void initFromArray (const Rcpp::RObject &object, const bool copyData = true); + +#endif + + /** + * Initialise an empty object from basic metadata + * @param dim A vector of image dimensions + * @param datatype A datatype code for the image data + **/ + void initFromDims (const std::vector &dim, const int datatype); + + /** + * Modify the pixel dimensions, and potentially the xform matrices to match + * @param pixdim Vector of new pixel dimensions + **/ + void updatePixdim (const std::vector &pixdim); + + /** + * Modify the pixel dimension units + * @param pixunits Vector of new pixel units, specified using their standard abbreviations + **/ + void setPixunits (const std::vector &pixunits); + +public: + /** + * Default constructor + **/ + NiftiImage () + : image(NULL), refCount(NULL) {} + + /** + * Copy constructor + * @param source Another \c NiftiImage object + * @param copy If \c true, the underlying \c nifti_image will be copied; otherwise the new + * object wraps the same \c nifti_image and increments the shared reference count + **/ + NiftiImage (const NiftiImage &source, const bool copy = true) + : image(NULL), refCount(NULL) + { + if (copy) + this->copy(source); + else + acquire(source); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from NiftiImage)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif + } + + /** + * Initialise from a block, copying in the data + * @param source A \c Block object, referring to part of another \c NiftiImage + **/ + NiftiImage (const Block &source) + : image(NULL), refCount(NULL) + { + this->copy(source); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from Block)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif + } + + /** + * Initialise using an existing \c nifti_image pointer + * @param image An existing \c nifti_image pointer, possibly \c NULL + * @param copy If \c true, the image data will be copied; otherwise this object just wraps + * the pointer passed to it + **/ + NiftiImage (nifti_image * const image, const bool copy = false) + : image(NULL), refCount(NULL) + { + if (copy) + this->copy(image); + else + acquire(image); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from pointer)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif + } + + /** + * Initialise from basic metadata, allocating and zeroing pixel data + * @param dim A vector of image dimensions + * @param datatype A datatype code for the image data + **/ + NiftiImage (const std::vector &dim, const int datatype); + + /** + * Initialise from basic metadata, allocating and zeroing pixel data + * @param dim A vector of image dimensions + * @param datatype A datatype string for the image data + **/ + NiftiImage (const std::vector &dim, const std::string &datatype); + + /** + * Initialise using a path string + * @param path A string specifying a path to a valid NIfTI-1 file, possibly gzipped + * @param readData If \c true, the data will be read as well as the metadata + * @exception runtime_error If reading from the file fails + **/ + NiftiImage (const std::string &path, const bool readData = true); + + /** + * Initialise using data + * @param data the pointer to the data buffer. + * @param size the size of the data buffer. + * @param gz 1 if the data is compressed otherwise 0. + * @param estimated_data_size estimated data size hint in bytes which is used for faster decomporession. + * Pass the volume * sizeof(elem). Example: `240 * 240 * 155 * sizeof(float)`. + **/ + NiftiImage (const uint8_t* data, const size_t size, int gz, const size_t estimated_data_size); + + /** + * Initialise using a path string and sequence of required volumes + * @param path A string specifying a path to a valid NIfTI-1 file, possibly gzipped + * @param volumes The volumes to read in (squashing all dimensions above the third together) + * @exception runtime_error If reading from the file fails, or \c volumes is empty + **/ + NiftiImage (const std::string &path, const std::vector &volumes); + +#ifdef USING_R + /** + * Initialise from an R object, retrieving an existing image from an external pointer attribute + * if available; otherwise constructing a new one from the R object itself + * @param object The source object + * @param readData If \c true, the data will be retrieved as well as the metadata + * @param readOnly If \c true, the caller asserts that its intent is read-only. Otherwise, if + * the \c SEXP may have multiple names at the R level (according to the \c MAYBE_SHARED R + * macro), an image retrieved from an external pointer will be duplicated to preserve R's usual + * semantics + **/ + NiftiImage (const SEXP object, const bool readData = true, const bool readOnly = false); +#endif + + /** + * Destructor which decrements the reference counter, and releases the wrapped pointer if the + * counter drops to zero + **/ + virtual ~NiftiImage () { release(); } + + /** + * Allows a \c NiftiImage object to be treated as a pointer to a \c const \c nifti_image + **/ + operator const nifti_image* () const { return image; } + + /** + * Allows a \c NiftiImage object to be treated as a pointer to a \c nifti_image + **/ + operator nifti_image* () { return image; } + + /** + * Allows a \c NiftiImage object to be treated as a pointer to a \c const \c nifti_image + **/ + const nifti_image * operator-> () const { return image; } + + /** + * Allows a \c NiftiImage object to be treated as a pointer to a \c nifti_image + **/ + nifti_image * operator-> () { return image; } + + /** + * Copy assignment operator, which copies from its argument + * @param source Another \c NiftiImage + **/ + NiftiImage & operator= (const NiftiImage &source) + { + copy(source); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d), with pointer %p (from NiftiImage)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif + return *this; + } + + /** + * Copy assignment operator, which allows a block to be used to replace the contents of a + * suitably sized image + * @param source A reference to a suitable \ref Block object + **/ + NiftiImage & operator= (const Block &source) + { + copy(source); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from Block)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif + return *this; + } + + /** + * Mark the image as persistent, so that it can be passed back to R + * @param persistent The new persistence state of the object + * @return A reference to the callee. + * @deprecated The persistence mechanism has been replaced with reference counting, so this + * function no longer has any effect. Instead it returns \c *this, unmodified. + **/ + NiftiImage & setPersistence (const bool persistent) { return *this; } + + /** + * Determine whether or not the wrapped pointer is \c NULL + * @return \c true if the wrapped pointer is \c NULL; \c false otherwise + **/ + bool isNull () const { return (image == NULL); } + + /** + * Determine whether the wrapped pointer is shared with another \c NiftiImage + * @return \c true if the reference count is greater than 1; \c false otherwise + **/ + bool isShared () const { return (refCount != NULL && *refCount > 1); } + + /** + * Determine whether or not the image is marked as persistent + * @return \c false, always + * @deprecated The persistence mechanism has been replaced with reference counting, so this + * function will always return \c false. Use \ref isShared instead. + **/ + bool isPersistent () const { return false; } + + /** + * Determine whether nontrivial scale and slope parameters are set + * @return \c true if the object wraps an image pointer, its slope is not zero and the slope + * and intercept are not exactly one and zero; \c false otherwise + **/ + bool isDataScaled () const { return (image != NULL && image->scl_slope != 0.0 && (image->scl_slope != 1.0 || image->scl_inter != 0.0)); } + + /** + * Return the number of dimensions in the image + * @return An integer giving the image dimensionality + **/ + int nDims () const + { + if (image == NULL) + return 0; + else + return image->ndim; + } + + /** + * Return the dimensions of the image + * @return A vector of integers giving the width in each dimension + **/ + std::vector dim () const + { + if (image == NULL) + return std::vector(); + else + return std::vector(image->dim+1, image->dim+image->ndim+1); + } + + /** + * Return the dimensions of the pixels or voxels in the image + * @return A vector of floating-point values giving the pixel width in each dimension + **/ + std::vector pixdim () const + { + if (image == NULL) + return std::vector(); + else + return std::vector(image->pixdim+1, image->pixdim+image->ndim+1); + } + + /** + * Drop unitary dimensions + * @return Self, after possibly reducing the dimensionality of the image + * @note This function differs from its R equivalent in only dropping unitary dimensions after + * the last nonunitary one + **/ + NiftiImage & drop () + { + int ndim = image->ndim; + while (image->dim[ndim] < 2) + ndim--; + image->dim[0] = image->ndim = ndim; + + return *this; + } + + /** + * Obtain the pixel data within the image + * @return A constant \c NiftiImageData object encapsulating the data + **/ + const NiftiImageData data () const { return NiftiImageData(image); } + + /** + * Obtain the pixel data within the image + * @return A mutable \c NiftiImageData object encapsulating the data + **/ + NiftiImageData data () { return NiftiImageData(image); } + + /** + * Extract a vector of data from the image, casting it to any required element type + * @param useSlope If \c true, the default, then the data will be adjusted for the slope and + * intercept stored with the image, if any + * @return A vector of data values, cast to the required type + * @note If the slope and intercept are applied, there is no guarantee that the adjusted values + * will fit within the requested type. No check is made for this + * @deprecated Use of the (ultimately more flexible) \ref data methods is now preferred + **/ + template + std::vector getData (const bool useSlope = true) const; + + /** + * Change the datatype of the image, casting the pixel data if present + * @param datatype A NIfTI datatype code + * @param useSlope If \c true, and conversion is to an integer type, the data will be rescaled + * and the image's slope and intercept set to capture the full range of original values + * @return Self, after changing the datatype + **/ + NiftiImage & changeDatatype (const int datatype, const bool useSlope = false); + + /** + * Change the datatype of the image, casting the pixel data if present + * @param datatype A string specifying the new datatype + * @param useSlope If \c true, and conversion is to an integer type, the data will be rescaled + * and the image's slope and intercept set to capture the full range of original values + * @return Self, after changing the datatype + **/ + NiftiImage & changeDatatype (const std::string &datatype, const bool useSlope = false); + + /** + * Replace the pixel data in the image with the contents of a vector + * @param data A data vector, whose elements will be used to replace the image data + * @param datatype The final datatype required. By default the existing datatype of the image + * is used + * @exception runtime_error If the length of the new data does not match the image + * @return Self, after replacing the data + **/ + template + NiftiImage & replaceData (const std::vector &data, const int datatype = DT_NONE); + + /** + * Replace the pixel data in the image with the contents of a \c NiftiImageData object + * @param data A data object, whose elements will be case to match the datatype of the image + * @exception runtime_error If the length of the new data does not match the image + * @return Self, after replacing the data + **/ + NiftiImage & replaceData (const NiftiImageData &data); + + /** + * Drop the data from the image, retaining only the metadata. This method invalidates any + * \ref NiftiImageData objects referencing the old data + * @return Self, after dropping the data + **/ + NiftiImage & dropData () + { +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image_unload(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_image_unload(image); +#endif + return *this; + } + + /** + * Rescale the image, changing its image dimensions and pixel dimensions + * @param scales Vector of scale factors along each dimension + * @return Self, after rescaling the metadata + * @note No interpolation is performed on the pixel data, which is simply dropped + **/ + NiftiImage & rescale (const std::vector &scales); + + /** + * Reorient the image by permuting dimensions and potentially reversing some + * @param i,j,k Constants such as \c NIFTI_L2R, \c NIFTI_P2A and \c NIFTI_I2S, giving the + * canonical axes to reorient to + * @return Self, after reorientation + * @note The pixel data is reordered, but not resampled. The xform matrices will also be + * adjusted in line with the transformation + **/ + NiftiImage & reorient (const int i, const int j, const int k); + + /** + * Reorient the image by permuting dimensions and potentially reversing some + * @param orientation A string containing some permutation of the letters \c L or \c R, + * \c P or \c A, \c I or \c S, giving the canonical axes to reorient to + * @return Self, after reorientation + * @note The pixel data is reordered, but not resampled. The xform matrices will also be + * adjusted in line with the transformation + **/ + NiftiImage & reorient (const std::string &orientation); + +#ifdef USING_R + /** + * Update the image from an R array + * @param object An R array or list object + * @return Self, after updating data and/or metadata + **/ + NiftiImage & update (const Rcpp::RObject &object); +#endif + + /** + * Obtain an xform matrix, indicating the orientation of the image + * @param preferQuaternion If \c true, use the qform matrix in preference to the sform; + * otherwise prefer the sform + * @return An \ref Xform object + **/ + const Xform xform (const bool preferQuaternion = true) const; + + /** + * Access the qform matrix + * @return An \ref Xform object + **/ + const Xform qform () const { return (image == NULL ? Xform() : Xform(image->qto_xyz)); } + + /** + * Access the qform matrix + * @return An \ref Xform object + **/ + Xform qform () { return (image == NULL ? Xform() : Xform(image->qto_xyz, image->qto_ijk, &image->quatern_b)); } + + /** + * Access the sform matrix + * @return An \ref Xform object + **/ + const Xform sform () const { return (image == NULL ? Xform() : Xform(image->sto_xyz)); } + + /** + * Access the sform matrix + * @return An \ref Xform object + **/ + Xform sform () { return (image == NULL ? Xform() : Xform(image->sto_xyz, image->sto_ijk)); } + + /** + * Return the number of blocks in the image + * @return An integer giving the number of blocks in the image + **/ + dim_t nBlocks () const { return (image == NULL ? 0 : image->dim[image->ndim]); } + + /** + * Extract a block from the image + * @param i The block number required + * @return A \ref Block object + * @note \ref slice and \ref volume are variants of this function specific to 3D and 4D images, + * respectively, which may be preferred in some cases for clarity + **/ + const Block block (const int i) const { return Block(*this, nDims(), i); } + + /** + * Extract a block from the image + * @param i The block number required + * @return A \ref Block object + * @note \ref slice and \ref volume are variants of this function specific to 3D and 4D images, + * respectively, which may be preferred in some cases for clarity + **/ + Block block (const int i) { return Block(*this, nDims(), i); } + + /** + * Extract a slice block from a 3D image + * @param i The slice number required + * @return A \ref Block object + **/ + const Block slice (const int i) const { return Block(*this, 3, i); } + + /** + * Extract a slice block from a 3D image + * @param i The slice number required + * @return A \ref Block object + **/ + Block slice (const int i) { return Block(*this, 3, i); } + + /** + * Extract a volume block from a 4D image + * @param i The volume number required + * @return A \ref Block object + **/ + const Block volume (const int i) const { return Block(*this, 4, i); } + + /** + * Extract a volume block from a 4D image + * @param i The volume number required + * @return A \ref Block object + **/ + Block volume (const int i) { return Block(*this, 4, i); } + + /** + * Return the number of colour channels used by the image + * @return An integer giving the number of channels: generally 1, exception for RGB datatypes, + * which have 3 or 4, or the empty datatype, which has 0. Also 0 for null images + **/ + int nChannels () const + { + if (image == NULL) + return 0; + else + { + switch (image->datatype) + { + case DT_NONE: return 0; + case DT_RGB24: return 3; + case DT_RGBA32: return 4; + default: return 1; + } + } + } + + /** + * Return the number of voxels in the image + * @return An integer giving the number of voxels in the image + **/ + size_t nVoxels () const { return (image == NULL ? 0 : image->nvox); } + + /** + * Return the number of extensions associated with the image + * @return An integer giving the number of extensions + **/ + int nExtensions () const { return (image == NULL ? 0 : image->num_ext); } + + /** + * Return a list of the extensions associated with the image + * @param code Integer specifying the code corresponding to the extensions required. If -1, the + * default, all extensions are returned. There may be more than one extension with a given code + * @return A list of \ref Extension objects + **/ + std::list extensions (const int code = -1) const + { + if (image == NULL) + return std::list(); + else + { + std::list result; + for (int i=0; inum_ext; i++) + { + const Extension extension(image->ext_list + i); + if (code < 0 || code == extension.code()) + result.push_back(extension); + } + return result; + } + } + + /** + * Add an extension to the image + * @param The new image extension, an \ref Extension object + * @return Self, with the extension appended + **/ + NiftiImage & addExtension (const Extension &extension) + { + if (image != NULL) +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_add_extension(image, extension.data(), int(extension.length()), extension.code()); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_add_extension(image, extension.data(), int(extension.length()), extension.code()); +#endif + return *this; + } + + /** + * Replace all extensions with new ones + * @param A list of \ref Extension objects + * @return Self, with the new extensions attached + **/ + NiftiImage & replaceExtensions (const std::list extensions) + { + dropExtensions(); + for (std::list::const_iterator it=extensions.begin(); it!=extensions.end(); ++it) + addExtension(*it); + return *this; + } + + /** + * Remove any extensions from the image + * @return Self, with extensions removed + **/ + NiftiImage & dropExtensions () + { + if (image != NULL) +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_free_extensions(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_free_extensions(image); +#endif + return *this; + } + + /** + * Write the image to a NIfTI-1 file + * @param fileName The file name to write to, with appropriate suffix (e.g. ".nii.gz") + * @param datatype The datatype to use when writing the file + * @param filetype The file type to create: a \c NIFTI_FTYPE constant or -1. In the latter case + * the file name is used to determine the file type + * @return A pair of strings, giving the final header and image paths in that order + **/ + std::pair toFile (const std::string fileName, const int datatype = DT_NONE, const int filetype = -1) const; + + /** + * Write the image to a NIfTI-1 file + * @param fileName The file name to write to, with appropriate suffix (e.g. ".nii.gz") + * @param datatype The datatype to use when writing the file, or "auto" + * @param filetype The file type to create: a \c NIFTI_FTYPE constant or -1. In the latter case + * the file name is used to determine the file type + * @return A pair of strings, giving the final header and image paths in that order + **/ + std::pair toFile (const std::string fileName, const std::string &datatype, const int filetype = -1) const; + +#ifdef USING_R + + /** + * Create an R array from the image + * @return A numeric array object with an external pointer attribute + **/ + Rcpp::RObject toArray () const; + + /** + * Create an internal image to pass back to R + * @param label A string labelling the image + * @return An R character string with additional attributes + **/ + Rcpp::RObject toPointer (const std::string label) const; + + /** + * A conditional method that calls either \ref toArray or \ref toPointer + * @param internal If \c true, \ref toPointer will be called; otherwise \ref toArray + * @param label A string labelling the image + * @return An R object + **/ + Rcpp::RObject toArrayOrPointer (const bool internal, const std::string label) const; + +#endif + +}; + +// Include image implementations +#include "RNifti/NiftiImage_impl.h" + +} // main namespace + +#endif diff --git a/cpp/3rd_party/rnifti/RNifti/NiftiImage_impl.h b/cpp/3rd_party/rnifti/RNifti/NiftiImage_impl.h new file mode 100644 index 0000000000..deeb75228f --- /dev/null +++ b/cpp/3rd_party/rnifti/RNifti/NiftiImage_impl.h @@ -0,0 +1,1881 @@ +#ifndef _NIFTI_IMAGE_IMPL_H_ +#define _NIFTI_IMAGE_IMPL_H_ + +namespace internal { + +// A poor man's NaN check, but should work whenever proper IEEE arithmetic is being used +template +inline bool isNaN(const Type x) +{ + return (x != x); +} + +#ifdef USING_R +// R offers the portable ISNAN macro for doubles, which is more robust +// Note that this tests for NaN and NA values +template <> +inline bool isNaN(const double x) +{ + return bool(ISNAN(x)); +} + +// For R specifically, we have to catch NA_INTEGER (a.k.a. INT_MIN) +template <> +inline bool isNaN(const int x) +{ + return (x == NA_INTEGER); +} + +template <> +inline bool isNaN(const rgba32_t x) +{ + return (x.value.packed == NA_INTEGER); +} + +// Specifically test for missingness - this is only relevant for R, and only when the distinction from NaN is important +template +inline bool isNA(const Type x) +{ + return false; +} + +template <> +inline bool isNA(const int x) +{ + return (x == NA_INTEGER); +} + +template <> +inline bool isNA(const double x) +{ + return ISNA(x); +} +#endif + +template +inline bool lessThan(Type a, Type b) +{ + return (!isNaN(a) && !isNaN(b) && a < b); +} + +inline double roundEven(const double value) +{ + if (isNaN(value)) + return value; + + double whole; + double frac = std::fabs(std::modf(value, &whole)); + double sign = (value < 0.0 ? -1.0 : 1.0); + + if (frac < 0.5) + return whole; + else if (frac > 0.5) + return whole + sign; + else if (std::fmod(whole, 2.0) < 0.0001) + return whole; + else + return whole + sign; +} + +inline int stringToDatatype(const std::string& datatype) +{ + static std::map datatypeCodes; + if (datatypeCodes.empty()) { + datatypeCodes["auto"] = DT_NONE; + datatypeCodes["none"] = DT_NONE; + datatypeCodes["unknown"] = DT_NONE; + datatypeCodes["uint8"] = DT_UINT8; + datatypeCodes["char"] = DT_UINT8; + datatypeCodes["int16"] = DT_INT16; + datatypeCodes["short"] = DT_INT16; + datatypeCodes["int32"] = DT_INT32; + datatypeCodes["int"] = DT_INT32; + datatypeCodes["float32"] = DT_FLOAT32; + datatypeCodes["float"] = DT_FLOAT32; + datatypeCodes["float64"] = DT_FLOAT64; + datatypeCodes["double"] = DT_FLOAT64; + datatypeCodes["int8"] = DT_INT8; + datatypeCodes["uint16"] = DT_UINT16; + datatypeCodes["uint32"] = DT_UINT32; + datatypeCodes["int64"] = DT_INT64; + datatypeCodes["uint64"] = DT_UINT64; + datatypeCodes["complex64"] = DT_COMPLEX64; + datatypeCodes["complex128"] = DT_COMPLEX128; + datatypeCodes["complex"] = DT_COMPLEX128; + datatypeCodes["rgb24"] = DT_RGB24; + datatypeCodes["rgb"] = DT_RGB24; + datatypeCodes["rgba32"] = DT_RGBA32; + datatypeCodes["rgba"] = DT_RGBA32; + } + + std::locale locale; + std::string lowerCaseDatatype = datatype; + for (std::string::size_type i = 0; i < lowerCaseDatatype.length(); i++) + lowerCaseDatatype[i] = std::tolower(lowerCaseDatatype[i], locale); + + if (datatypeCodes.count(lowerCaseDatatype) == 0) { + std::ostringstream message; + message << "Datatype \"" << datatype << "\" is not valid"; + Rf_warning(message.str().c_str()); + return DT_NONE; + } else + return datatypeCodes[lowerCaseDatatype]; +} + +template +struct ElementConverter +{ + template + TargetType operator()(const SourceType& source) + { + return static_cast(source); + } +}; + +#if RNIFTI_NIFTILIB_VERSION == 1 + +// Byte-by-byte conversion of nifti2_image struct to a nifti1_image +// By nature this is a risky operation, which has to make assumptions about the layout of the structs in memory +inline nifti1_image* convertImageV2to1(nifti2_image* image) +{ + if (image == NULL) + return NULL; + + nifti1_image* result = (nifti1_image*)calloc(1, sizeof(nifti1_image)); + +#ifndef NDEBUG + Rc_printf("Converting v2 image with pointer %p to v1 image with pointer %p\n", image, result); +#endif + + // We assume that each block of a given type is stored contiguously like an array - this should be the case, but may + // not be guaranteed + std::transform(&image->ndim, &image->ndim + 16, &result->ndim, ElementConverter()); + result->nvox = static_cast(image->nvox); + std::copy(&image->nbyper, &image->nbyper + 2, &result->nbyper); + std::transform(&image->dx, &image->dx + 19, &result->dx, ElementConverter()); + std::copy(&image->qform_code, &image->qform_code + 6, &result->qform_code); + std::transform(&image->slice_start, &image->slice_start + 2, &result->slice_start, ElementConverter()); + std::transform(&image->slice_duration, &image->slice_duration + 73, &result->slice_duration, + ElementConverter()); + std::copy(&image->xyz_units, &image->xyz_units + 4, &result->xyz_units); + std::transform(&image->intent_p1, &image->intent_p1 + 3, &result->intent_p1, ElementConverter()); + std::copy(static_cast(image->intent_name), static_cast(image->intent_name) + 120, + static_cast(result->intent_name)); + result->iname_offset = static_cast(image->iname_offset); + std::copy(&image->swapsize, &image->swapsize + 2, &result->swapsize); + result->analyze75_orient = image->analyze75_orient; + + // Copy buffers, since the memory-freeing logic isn't portable between struct versions + result->fname = nifti_strdup(image->fname); + result->iname = nifti_strdup(image->iname); + if (image->data != NULL) { + result->data = calloc(result->nvox, result->nbyper); + memcpy(result->data, image->data, result->nvox * result->nbyper); + } + + // Copy extensions + result->num_ext = image->num_ext; + result->ext_list = + result->num_ext == 0 ? NULL : (nifti1_extension*)calloc(result->num_ext, sizeof(nifti1_extension)); + for (int i = 0; i < result->num_ext; i++) { + result->ext_list[i].esize = image->ext_list[i].esize; + result->ext_list[i].ecode = image->ext_list[i].ecode; + result->ext_list[i].edata = (char*)calloc(result->ext_list[i].esize - 8, sizeof(char)); + memcpy(result->ext_list[i].edata, image->ext_list[i].edata, result->ext_list[i].esize - 8); + } + + // Check the result looks plausible + if (!nifti_nim_is_valid(result, 0)) + throw std::runtime_error("Conversion between image versions failed"); + + return result; +} + +#elif RNIFTI_NIFTILIB_VERSION == 2 + +// Byte-by-byte conversion of nifti1_image struct to a nifti2_image +inline nifti2_image* convertImageV1to2(nifti1_image* image) +{ + if (image == NULL) + return NULL; + + nifti2_image* result = (nifti2_image*)calloc(1, sizeof(nifti2_image)); + +#ifndef NDEBUG + Rc_printf("Converting v1 image with pointer %p to v2 image with pointer %p\n", image, result); +#endif + + std::transform(&image->ndim, &image->ndim + 16, &result->ndim, ElementConverter()); + result->nvox = static_cast(image->nvox); + std::copy(&image->nbyper, &image->nbyper + 2, &result->nbyper); + std::transform(&image->dx, &image->dx + 19, &result->dx, ElementConverter()); + std::copy(&image->qform_code, &image->qform_code + 6, &result->qform_code); + std::transform(&image->slice_start, &image->slice_start + 2, &result->slice_start, ElementConverter()); + std::transform(&image->slice_duration, &image->slice_duration + 73, &result->slice_duration, + ElementConverter()); + std::copy(&image->xyz_units, &image->xyz_units + 4, &result->xyz_units); + std::transform(&image->intent_p1, &image->intent_p1 + 3, &result->intent_p1, ElementConverter()); + std::copy(static_cast(image->intent_name), static_cast(image->intent_name) + 120, + static_cast(result->intent_name)); + result->iname_offset = static_cast(image->iname_offset); + std::copy(&image->swapsize, &image->swapsize + 2, &result->swapsize); + result->analyze75_orient = image->analyze75_orient; + + result->fname = nifti_strdup(image->fname); + result->iname = nifti_strdup(image->iname); + if (image->data != NULL) { + result->data = calloc(result->nvox, result->nbyper); + memcpy(result->data, image->data, result->nvox * result->nbyper); + } + + result->num_ext = image->num_ext; + result->ext_list = + result->num_ext == 0 ? NULL : (nifti1_extension*)calloc(result->num_ext, sizeof(nifti1_extension)); + for (int i = 0; i < result->num_ext; i++) { + result->ext_list[i].esize = image->ext_list[i].esize; + result->ext_list[i].ecode = image->ext_list[i].ecode; + result->ext_list[i].edata = (char*)calloc(result->ext_list[i].esize - 8, sizeof(char)); + memcpy(result->ext_list[i].edata, image->ext_list[i].edata, result->ext_list[i].esize - 8); + } + + if (!nifti2_nim_is_valid(result, 0)) + throw std::runtime_error("Conversion between image versions failed"); + + return result; +} + +#endif // RNIFTI_NIFTILIB_VERSION + +#ifdef USING_R +inline const char* stringToPath(const std::string& str) +{ + return R_ExpandFileName(str.c_str()); +} +#else +inline const char* stringToPath(const std::string& str) +{ + return str.c_str(); +} +#endif + +#ifdef USING_R + +template +inline void copyIfPresent(const Rcpp::List& list, const std::set names, const std::string& name, + TargetType& target) +{ + if (names.count(name) == 1) { + const Rcpp::RObject object = list[name]; + const int length = Rf_length(object); + if (length == 0) { + std::ostringstream message; + message << "Field \"" << name << "\" is empty and will be ignored"; + Rf_warning(message.str().c_str()); + } else if (length > 1) { + std::ostringstream message; + message << "Field \"" << name << "\" has " << length << "elements, but only the first will be used"; + Rf_warning(message.str().c_str()); + target = Rcpp::as>(object)[0]; + } else + target = Rcpp::as(object); + } +} + +// Special case for char, because Rcpp tries to be too clever and convert it to a string +template <> +inline void copyIfPresent(const Rcpp::List& list, const std::set names, const std::string& name, + char& target) +{ + if (names.count(name) == 1) { + int intValue = 0; + copyIfPresent(list, names, name, intValue); + target = static_cast(intValue); + } +} + +inline void updateHeader(nifti_1_header* header, const Rcpp::List& list, const bool ignoreDatatype = false) +{ + if (header == NULL || Rf_isNull(list.names())) + return; + + const Rcpp::CharacterVector _names = list.names(); + std::set names; + for (Rcpp::CharacterVector::const_iterator it = _names.begin(); it != _names.end(); it++) + names.insert(Rcpp::as(*it)); + + copyIfPresent(list, names, "sizeof_hdr", header->sizeof_hdr); + + copyIfPresent(list, names, "dim_info", header->dim_info); + if (names.count("dim") == 1) { + std::vector dim = list["dim"]; + if (dim.size() != 8) + throw std::runtime_error("Field \"dim\" must contain 8 elements"); + for (size_t i = 0; i < 8; i++) + header->dim[i] = dim[i]; + } + + copyIfPresent(list, names, "intent_p1", header->intent_p1); + copyIfPresent(list, names, "intent_p2", header->intent_p2); + copyIfPresent(list, names, "intent_p3", header->intent_p3); + copyIfPresent(list, names, "intent_code", header->intent_code); + + if (!ignoreDatatype) { + copyIfPresent(list, names, "datatype", header->datatype); + copyIfPresent(list, names, "bitpix", header->bitpix); + } + + copyIfPresent(list, names, "slice_start", header->slice_start); + if (names.count("pixdim") == 1) { + std::vector pixdim = list["pixdim"]; + if (pixdim.size() != 8) + throw std::runtime_error("Field \"pixdim\" must contain 8 elements"); + for (size_t i = 0; i < 8; i++) + header->pixdim[i] = pixdim[i]; + } + copyIfPresent(list, names, "vox_offset", header->vox_offset); + copyIfPresent(list, names, "scl_slope", header->scl_slope); + copyIfPresent(list, names, "scl_inter", header->scl_inter); + copyIfPresent(list, names, "slice_end", header->slice_end); + copyIfPresent(list, names, "slice_code", header->slice_code); + copyIfPresent(list, names, "xyzt_units", header->xyzt_units); + copyIfPresent(list, names, "cal_max", header->cal_max); + copyIfPresent(list, names, "cal_min", header->cal_min); + copyIfPresent(list, names, "slice_duration", header->slice_duration); + copyIfPresent(list, names, "toffset", header->toffset); + + if (names.count("descrip") == 1) + strcpy(header->descrip, Rcpp::as(list["descrip"]).substr(0, 79).c_str()); + if (names.count("aux_file") == 1) + strcpy(header->aux_file, Rcpp::as(list["aux_file"]).substr(0, 23).c_str()); + + copyIfPresent(list, names, "qform_code", header->qform_code); + copyIfPresent(list, names, "sform_code", header->sform_code); + copyIfPresent(list, names, "quatern_b", header->quatern_b); + copyIfPresent(list, names, "quatern_c", header->quatern_c); + copyIfPresent(list, names, "quatern_d", header->quatern_d); + copyIfPresent(list, names, "qoffset_x", header->qoffset_x); + copyIfPresent(list, names, "qoffset_y", header->qoffset_y); + copyIfPresent(list, names, "qoffset_z", header->qoffset_z); + + if (names.count("srow_x") == 1) { + std::vector srow_x = list["srow_x"]; + if (srow_x.size() != 4) + throw std::runtime_error("Field \"srow_x\" must contain 4 elements"); + for (size_t i = 0; i < 4; i++) + header->srow_x[i] = srow_x[i]; + } + if (names.count("srow_y") == 1) { + std::vector srow_y = list["srow_y"]; + if (srow_y.size() != 4) + throw std::runtime_error("Field \"srow_y\" must contain 4 elements"); + for (size_t i = 0; i < 4; i++) + header->srow_y[i] = srow_y[i]; + } + if (names.count("srow_z") == 1) { + std::vector srow_z = list["srow_z"]; + if (srow_z.size() != 4) + throw std::runtime_error("Field \"srow_z\" must contain 4 elements"); + for (size_t i = 0; i < 4; i++) + header->srow_z[i] = srow_z[i]; + } + + if (names.count("intent_name") == 1) + strcpy(header->intent_name, Rcpp::as(list["intent_name"]).substr(0, 15).c_str()); + if (names.count("magic") == 1) + strcpy(header->magic, Rcpp::as(list["magic"]).substr(0, 3).c_str()); +} + +inline void addAttributes(const SEXP pointer, const NiftiImage& source, const bool realDim = true, + const bool includeXptr = true, const bool keepData = true) +{ + const int nDims = source->dim[0]; + Rcpp::RObject object(pointer); + Rcpp::IntegerVector dim(source->dim + 1, source->dim + 1 + nDims); + + if (realDim) + object.attr("dim") = dim; + else + object.attr("imagedim") = dim; + + Rcpp::DoubleVector pixdim(nDims); + for (int i = 0; i < nDims; i++) + pixdim[i] = std::abs(static_cast(source->pixdim[i + 1])); + object.attr("pixdim") = pixdim; + + if (source->xyz_units == NIFTI_UNITS_UNKNOWN && source->time_units == NIFTI_UNITS_UNKNOWN) + object.attr("pixunits") = "Unknown"; + else { + Rcpp::CharacterVector pixunits(2); + pixunits[0] = nifti_units_string(source->xyz_units); + pixunits[1] = nifti_units_string(source->time_units); + object.attr("pixunits") = pixunits; + } + + if (includeXptr) { + NiftiImage* imagePtr = new NiftiImage(source, false); + if (!keepData) + imagePtr->dropData(); + Rcpp::XPtr xptr(imagePtr); + object.attr(".nifti_image_ptr") = xptr; + object.attr(".nifti_image_ver") = RNIFTI_NIFTILIB_VERSION; + } +} + +#endif // USING_R + +} // namespace internal + +template +inline void NiftiImageData::ConcreteTypeHandler::minmax(void* ptr, const size_t length, double* min, + double* max) const +{ + if (ptr == NULL || length < 1) { + *min = static_cast(std::numeric_limits::min()); + *max = static_cast(std::numeric_limits::max()); + } else { + Type* loc = static_cast(ptr); + Type currentMin = *loc, currentMax = *loc; + for (size_t i = 1; i < length; i++) { + loc++; + if (internal::lessThan(*loc, currentMin)) + currentMin = *loc; + if (internal::lessThan(currentMax, *loc)) + currentMax = *loc; + } + *min = static_cast(currentMin); + *max = static_cast(currentMax); + } +} + +template +inline void +NiftiImageData::ConcreteTypeHandler, false>::minmax(void* ptr, const size_t length, + double* min, double* max) const +{ + if (ptr == NULL || length < 1) { + *min = static_cast(std::numeric_limits::min()); + *max = static_cast(std::numeric_limits::max()); + } else { + ElementType* loc = static_cast(ptr); + ElementType currentMin = *loc, currentMax = *loc; + for (size_t i = 1; i < (2 * length); i++) { + loc++; + if (internal::lessThan(*loc, currentMin)) + currentMin = *loc; + if (internal::lessThan(currentMax, *loc)) + currentMax = *loc; + } + *min = static_cast(currentMin); + *max = static_cast(currentMax); + } +} + +template +inline NiftiImageData::Element& NiftiImageData::Element::operator=(const SourceType& value) +{ + if (internal::isNaN(value)) { + if (!parent.handler->hasNaN()) { + const double zeroValue = parent.isScaled() ? (-parent.intercept / parent.slope) : 0.0; + if (parent.isFloatingPoint()) + parent.handler->setDouble(ptr, zeroValue); + else + parent.handler->setInt(ptr, static_cast(internal::roundEven(zeroValue))); + } +#ifdef USING_R + // Only happens for integer types that admit an NaN/NA value. + // In practice this means int specifically for R, so we don't + // need to worry about the effect of casting INT_MIN to a wider + // or narrower type + else if (parent.isInteger()) + parent.handler->setInt(ptr, NA_INTEGER); + else if (internal::isNA(value)) + parent.handler->setDouble(ptr, NA_REAL); +#endif + else + parent.handler->setDouble(ptr, std::numeric_limits::quiet_NaN()); + } else if (parent.isScaled()) { + double reverseScaledValue = (static_cast(value) - parent.intercept) / parent.slope; + if (parent.isFloatingPoint()) + parent.handler->setDouble(ptr, reverseScaledValue); + else + parent.handler->setInt(ptr, static_cast(internal::roundEven(reverseScaledValue))); + } else if (std::numeric_limits::is_integer) + parent.handler->setInt(ptr, static_cast(value)); + else + parent.handler->setDouble(ptr, static_cast(value)); + return *this; +} + +inline NiftiImageData::Element& NiftiImageData::Element::operator=(const NiftiImageData::Element& other) +{ + if (other.parent.isScaled() || other.parent.isFloatingPoint()) { + const double value = other; + *this = value; + } else { + const int value = other; + *this = value; + } + return *this; +} + +inline void NiftiImage::Extension::copy(const nifti1_extension* source) +{ + if (source == NULL) + ext = NULL; + else { + ext = (nifti1_extension*)calloc(1, sizeof(nifti1_extension)); + ext->esize = source->esize; + ext->ecode = source->ecode; + if (source->edata != NULL && source->esize > 8) { + ext->edata = (char*)calloc(source->esize - 8, 1); + memcpy(ext->edata, source->edata, source->esize - 8); + } + } +} + +template +inline void NiftiImage::Extension::copy(const SourceType* data, const size_t length, const int code) +{ + if (data == NULL) + ext = NULL; + else { + const size_t bytes = length * sizeof(SourceType); + ext = (nifti1_extension*)calloc(1, sizeof(nifti1_extension)); + ext->esize = int(bytes + 8); + const int remainder = ext->esize % 16; + ext->esize += (remainder == 0 ? 0 : 16 - remainder); + ext->ecode = code; + ext->edata = (char*)calloc(ext->esize - 8, 1); + memcpy(ext->edata, data, bytes); + } +} + +inline void NiftiImage::Xform::replace(const Matrix& source) +{ + mat = source; + if (forward != NULL) + std::copy(source.begin(), source.end(), forward); + if (inverse != NULL) { + Matrix inv = source.inverse(); + std::copy(inv.begin(), inv.end(), inverse); + } + if (qparams != NULL) { +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_quatern(mat, qparams, qparams + 1, qparams + 2, qparams + 3, qparams + 4, qparams + 5, NULL, + NULL, NULL, qparams + 6); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_quatern(mat, qparams, qparams + 1, qparams + 2, qparams + 3, qparams + 4, qparams + 5, NULL, + NULL, NULL, qparams + 6); +#endif + } +} + +inline NiftiImage::Xform::Submatrix NiftiImage::Xform::submatrix() const +{ + NiftiImage::Xform::Submatrix result; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) + result(i, j) = mat(i, j); + } + return result; +} + +inline NiftiImage::Xform::Submatrix NiftiImage::Xform::rotation() const +{ + NiftiImage::Xform::Vector3 qbcd; + NiftiImage::Xform::Element qfac; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_quatern(mat, &qbcd[0], &qbcd[1], &qbcd[2], NULL, NULL, NULL, NULL, NULL, NULL, &qfac); + NiftiImage::Xform rotation = nifti_quatern_to_mat44(qbcd[0], qbcd[1], qbcd[2], 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, qfac); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_quatern(mat, &qbcd[0], &qbcd[1], &qbcd[2], NULL, NULL, NULL, NULL, NULL, NULL, &qfac); + NiftiImage::Xform rotation = nifti_quatern_to_dmat44(qbcd[0], qbcd[1], qbcd[2], 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, qfac); +#endif + return rotation.submatrix(); +} + +inline NiftiImage::Xform::Element NiftiImage::Xform::handedness() const +{ + NiftiImage::Xform::Element qfac; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_quatern(mat, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &qfac); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_quatern(mat, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &qfac); +#endif + return qfac; +} + +inline NiftiImage::Xform::Vector4 NiftiImage::Xform::quaternion() const +{ + NiftiImage::Xform::Vector4 q; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_quatern(mat, &q[1], &q[2], &q[3], NULL, NULL, NULL, NULL, NULL, NULL, NULL); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_quatern(mat, &q[1], &q[2], &q[3], NULL, NULL, NULL, NULL, NULL, NULL, NULL); +#endif + q[0] = 1.0 - (q[1] * q[1] + q[2] * q[2] + q[3] * q[3]); + return q; +} + +inline NiftiImage::Xform::Vector3 NiftiImage::Xform::offset() const +{ + NiftiImage::Xform::Vector3 vec; + for (int i = 0; i < 3; i++) + vec[i] = mat(i, 3); + return vec; +} + +inline NiftiImage::Xform::Vector3 NiftiImage::Xform::spacing() const +{ + NiftiImage::Xform::Vector3 vec; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_quatern(mat, NULL, NULL, NULL, NULL, NULL, NULL, &vec[0], &vec[1], &vec[2], NULL); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_quatern(mat, NULL, NULL, NULL, NULL, NULL, NULL, &vec[0], &vec[1], &vec[2], NULL); +#endif + return vec; +} + +inline std::string NiftiImage::Xform::orientation() const +{ + int icode, jcode, kcode; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_orientation(mat, &icode, &jcode, &kcode); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_orientation(mat, &icode, &jcode, &kcode); +#endif + + int codes[3] = {icode, jcode, kcode}; + std::string result("---"); + for (int i = 0; i < 3; i++) { + switch (codes[i]) { + case NIFTI_L2R: + result[i] = 'R'; + break; + case NIFTI_R2L: + result[i] = 'L'; + break; + case NIFTI_P2A: + result[i] = 'A'; + break; + case NIFTI_A2P: + result[i] = 'P'; + break; + case NIFTI_I2S: + result[i] = 'S'; + break; + case NIFTI_S2I: + result[i] = 'I'; + break; + } + } + return result; +} + +inline int NiftiImage::fileVersion(const std::string& path) +{ +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_1_header* header = nifti_read_header(internal::stringToPath(path), NULL, false); + if (header == NULL) + return -1; + else { + int version = NIFTI_VERSION(*header); + if (version == 0) { + // NIfTI-2 has a 540-byte header - check for this or its byte-swapped equivalent + if (header->sizeof_hdr == 540 || header->sizeof_hdr == 469893120) { + // The magic number has moved in NIfTI-2, so find it by byte offset + const char* magic = (char*)header + 4; + if (strncmp(magic, "ni2", 3) == 0 || strncmp(magic, "n+2", 3) == 0) + version = 2; + } else if (!nifti_hdr_looks_good(header)) { + // Not plausible as ANALYZE, so return -1 + version = -1; + } + } + free(header); + return version; + } +#elif RNIFTI_NIFTILIB_VERSION == 2 + int version; + void* header = nifti2_read_header(internal::stringToPath(path), &version, true); + if (header == NULL) + return -1; + free(header); + return version; +#endif +} + +inline void NiftiImage::acquire(nifti_image* const image) +{ + // If we're taking ownership of a new image, release the old one + if (this->image != NULL && this->image != image) + release(); + + // Set the internal pointer and create or update the reference counter + this->image = image; + if (image != NULL) { + if (this->refCount == NULL) + this->refCount = new int(1); + else + (*this->refCount)++; + +#ifndef NDEBUG + Rc_printf("Acquiring pointer %p (v%d; reference count is %d)\n", this->image, RNIFTI_NIFTILIB_VERSION, + *this->refCount); +#endif + } +} + +inline void NiftiImage::release() +{ + if (this->image != NULL) { + if (this->refCount != NULL) { + (*this->refCount)--; +#ifndef NDEBUG + Rc_printf("Releasing pointer %p (v%d; reference count is %d)\n", this->image, RNIFTI_NIFTILIB_VERSION, + *this->refCount); +#endif + if (*this->refCount < 1) { +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image_free(this->image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_image_free(this->image); +#endif + this->image = NULL; + delete this->refCount; + this->refCount = NULL; + } + } else + Rc_printf("Releasing untracked object %p", this->image); + } +} + +inline void NiftiImage::copy(const nifti_image* source) +{ + if (source == NULL) + acquire(NULL); + else { + bool can_share_data = !nifti_image_is_data_owner(source); +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_copy_nim_info(source)); + if (source->data != NULL) { + if (can_share_data) { + image->data = source->data; + } else { + size_t dataSize = nifti_get_volsize(source); + image->data = calloc(1, dataSize); + memcpy(image->data, source->data, dataSize); + } + } +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_copy_nim_info(source)); + if (source->data != NULL) { + if (can_share_data) { + image->data = source->data; + } else { + size_t dataSize = nifti2_get_volsize(source); + image->data = calloc(1, dataSize); + memcpy(image->data, source->data, dataSize); + } + } +#endif + } +} + +inline void NiftiImage::copy(const NiftiImage& source) +{ + const nifti_image* sourceStruct = source; + + copy(sourceStruct); +} + +inline void NiftiImage::copy(const Block& source) +{ + const nifti_image* sourceStruct = source.image; + ASSERT_MESSAGE(nifti_image_is_data_owner(sourceStruct), + "nifti image block copy is not handled for cases when source is not the data owner. i.e. nii.gz " + "from buffer case."); + if (sourceStruct == NULL) + acquire(NULL); + else { +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_copy_nim_info(sourceStruct)); + image->dim[0] = source.image->dim[0] - 1; + image->dim[source.dimension] = 1; + image->pixdim[source.dimension] = 1.0; + nifti_update_dims_from_array(image); + + if (sourceStruct->data != NULL) { + size_t blockSize = nifti_get_volsize(image); + image->data = calloc(1, blockSize); + memcpy(image->data, static_cast(source.image->data) + blockSize * source.index, blockSize); + } +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_copy_nim_info(sourceStruct)); + image->dim[0] = source.image->dim[0] - 1; + image->dim[source.dimension] = 1; + image->pixdim[source.dimension] = 1.0; + nifti2_update_dims_from_array(image); + + if (sourceStruct->data != NULL) { + size_t blockSize = nifti2_get_volsize(image); + image->data = calloc(1, blockSize); + memcpy(image->data, static_cast(source.image->data) + blockSize * source.index, blockSize); + } +#endif + } +} + +#ifdef USING_R + +// Convert an S4 "nifti" object, as defined in the oro.nifti package, to a "nifti_image" struct +inline void NiftiImage::initFromNiftiS4(const Rcpp::RObject& object, const bool copyData) +{ + nifti_1_header header; + header.sizeof_hdr = 348; + + const std::vector dims = object.slot("dim_"); + for (int i = 0; i < 8; i++) + header.dim[i] = dims[i]; + + header.intent_p1 = object.slot("intent_p1"); + header.intent_p2 = object.slot("intent_p2"); + header.intent_p3 = object.slot("intent_p3"); + header.intent_code = object.slot("intent_code"); + + header.datatype = object.slot("datatype"); + header.bitpix = object.slot("bitpix"); + + header.slice_start = object.slot("slice_start"); + header.slice_end = object.slot("slice_end"); + header.slice_code = Rcpp::as(object.slot("slice_code")); + header.slice_duration = object.slot("slice_duration"); + + const std::vector pixdims = object.slot("pixdim"); + for (int i = 0; i < 8; i++) + header.pixdim[i] = pixdims[i]; + header.xyzt_units = Rcpp::as(object.slot("xyzt_units")); + + header.vox_offset = object.slot("vox_offset"); + + // oro.nifti does its own data rescaling, so we ignore the slope and intercept fields + header.scl_slope = 0.0; + header.scl_inter = 0.0; + header.toffset = object.slot("toffset"); + + header.cal_max = object.slot("cal_max"); + header.cal_min = object.slot("cal_min"); + header.glmax = header.glmin = 0; + + strncpy(header.descrip, Rcpp::as(object.slot("descrip")).c_str(), 79); + header.descrip[79] = '\0'; + strncpy(header.aux_file, Rcpp::as(object.slot("aux_file")).c_str(), 23); + header.aux_file[23] = '\0'; + strncpy(header.intent_name, Rcpp::as(object.slot("intent_name")).c_str(), 15); + header.intent_name[15] = '\0'; + strncpy(header.magic, Rcpp::as(object.slot("magic")).c_str(), 3); + header.magic[3] = '\0'; + + header.qform_code = object.slot("qform_code"); + header.sform_code = object.slot("sform_code"); + + header.quatern_b = object.slot("quatern_b"); + header.quatern_c = object.slot("quatern_c"); + header.quatern_d = object.slot("quatern_d"); + header.qoffset_x = object.slot("qoffset_x"); + header.qoffset_y = object.slot("qoffset_y"); + header.qoffset_z = object.slot("qoffset_z"); + + const std::vector srow_x = object.slot("srow_x"); + const std::vector srow_y = object.slot("srow_y"); + const std::vector srow_z = object.slot("srow_z"); + for (int i = 0; i < 4; i++) { + header.srow_x[i] = srow_x[i]; + header.srow_y[i] = srow_y[i]; + header.srow_z[i] = srow_z[i]; + } + + // Ignoring complex and RGB types here because oro.nifti doesn't yet support them + if (header.datatype == DT_UINT8 || header.datatype == DT_INT16 || header.datatype == DT_INT32 || + header.datatype == DT_INT8 || header.datatype == DT_UINT16 || header.datatype == DT_UINT32) + header.datatype = DT_INT32; + else if (header.datatype == DT_FLOAT32 || header.datatype == DT_FLOAT64) + header.datatype = DT_FLOAT64; + else + throw std::runtime_error("Data type is not supported"); + +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_convert_nhdr2nim(header, NULL)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti_convert_n1hdr2nim(header, NULL)); +#endif + + const Rcpp::RObject data = object.slot(".Data"); + if (!copyData || Rf_length(data) <= 1) + this->image->data = NULL; + else if (header.datatype == DT_INT32) { + Rcpp::IntegerVector intData(data); + replaceData(NiftiImageData(intData.begin(), intData.end(), DT_INT32)); + } else { + Rcpp::DoubleVector doubleData(data); + replaceData(NiftiImageData(doubleData.begin(), doubleData.end(), DT_FLOAT64)); + } +} + +inline void NiftiImage::initFromMriImage(const Rcpp::RObject& object, const bool copyData) +{ + Rcpp::Reference mriImage(object); + Rcpp::Function getXform = mriImage.field("getXform"); + Rcpp::NumericMatrix xform = getXform(); + + acquire(NULL); + + if (Rf_length(mriImage.field("tags")) > 0) + initFromList(mriImage.field("tags")); + + Rcpp::RObject data = mriImage.field("data"); + if (data.inherits("SparseArray")) { + Rcpp::Language call("as.array", data); + data = call.eval(); + } + + const int datatype = (Rf_isNull(data) ? DT_INT32 : sexpTypeToNiftiType(data.sexp_type())); + + dim_t dims[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + const std::vector dimVector = mriImage.field("imageDims"); + const int nDims = std::min(7, int(dimVector.size())); + dims[0] = nDims; + size_t nVoxels = 1; + for (int i = 0; i < nDims; i++) { + dims[i + 1] = dimVector[i]; + nVoxels *= dimVector[i]; + } + + if (this->image == NULL) { +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_make_new_nim(dims, datatype, FALSE)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_make_new_nim(dims, datatype, FALSE)); +#endif + } else { + std::copy(dims, dims + 8, this->image->dim); + this->image->datatype = datatype; + nifti_datatype_sizes(image->datatype, &image->nbyper, NULL); + } + + if (copyData && !Rf_isNull(data)) { + // NB: nifti_get_volsize() will not be right here if there were tags + const size_t dataSize = nVoxels * image->nbyper; + this->image->data = calloc(1, dataSize); + if (datatype == DT_INT32) + memcpy(this->image->data, INTEGER(data), dataSize); + else + memcpy(this->image->data, REAL(data), dataSize); + } else + this->image->data = NULL; + + const std::vector pixdimVector = mriImage.field("voxelDims"); + const int pixdimLength = pixdimVector.size(); + for (int i = 0; i < std::min(pixdimLength, nDims); i++) + this->image->pixdim[i + 1] = std::abs(pixdimVector[i]); + + const std::vector pixunitsVector = mriImage.field("voxelDimUnits"); + setPixunits(pixunitsVector); + + if (xform.rows() != 4 || xform.cols() != 4) + this->image->qform_code = this->image->sform_code = 0; + else { + const Xform::Matrix xformMatrix(xform); + this->qform() = xformMatrix; + this->sform() = xformMatrix; + this->image->qform_code = this->image->sform_code = 2; + } +} + +inline void NiftiImage::initFromList(const Rcpp::RObject& object) +{ + Rcpp::List list(object); +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_1_header* header = nifti_make_new_header(NULL, DT_FLOAT64); + internal::updateHeader(header, list); + acquire(nifti_convert_nhdr2nim(*header, NULL)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_1_header* header = nifti_make_new_n1_header(NULL, DT_FLOAT64); + internal::updateHeader(header, list); + acquire(nifti_convert_n1hdr2nim(*header, NULL)); +#endif + this->image->data = NULL; + free(header); +} + +inline void NiftiImage::initFromArray(const Rcpp::RObject& object, const bool copyData) +{ + dim_t dims[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + const std::vector dimVector = object.attr("dim"); + + const int nDims = std::min(7, int(dimVector.size())); + dims[0] = nDims; + for (int i = 0; i < nDims; i++) + dims[i + 1] = dimVector[i]; + + int datatype = sexpTypeToNiftiType(object.sexp_type()); + if (object.inherits("rgbArray")) { + const int channels = (object.hasAttribute("channels") ? object.attr("channels") : 3); + datatype = (channels == 4 ? DT_RGBA32 : DT_RGB24); + } + +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_make_new_nim(dims, datatype, int(copyData))); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_make_new_nim(dims, datatype, int(copyData))); +#endif + + if (copyData) { +#if RNIFTI_NIFTILIB_VERSION == 1 + const size_t dataSize = nifti_get_volsize(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + const size_t dataSize = nifti2_get_volsize(image); +#endif + if (datatype == DT_INT32 || datatype == DT_RGBA32) + memcpy(this->image->data, INTEGER(object), dataSize); + else if (datatype == DT_RGB24) { + NiftiImageData data(image); + std::copy(INTEGER(object), INTEGER(object) + image->nvox, data.begin()); + } else if (datatype == DT_COMPLEX128) + memcpy(this->image->data, COMPLEX(object), dataSize); + else + memcpy(this->image->data, REAL(object), dataSize); + } else + this->image->data = NULL; + + if (object.hasAttribute("pixdim")) { + const std::vector pixdimVector = object.attr("pixdim"); + const int pixdimLength = pixdimVector.size(); + for (int i = 0; i < std::min(pixdimLength, nDims); i++) + this->image->pixdim[i + 1] = pixdimVector[i]; + } + + if (object.hasAttribute("pixunits")) { + const std::vector pixunitsVector = object.attr("pixunits"); + setPixunits(pixunitsVector); + } +} + +inline NiftiImage::NiftiImage(const SEXP object, const bool readData, const bool readOnly) : image(NULL), refCount(NULL) +{ + Rcpp::RObject imageObject(object); + bool resolved = false; + + if (imageObject.hasAttribute(".nifti_image_ptr")) { + Rcpp::XPtr imagePtr(SEXP(imageObject.attr(".nifti_image_ptr"))); + NiftiImage* ptr = imagePtr.get(); + if (ptr != NULL) { +#if RNIFTI_NIFTILIB_VERSION == 1 + if (imageObject.hasAttribute(".nifti_image_ver") && int(imageObject.attr(".nifti_image_ver")) == 2) + acquire(internal::convertImageV2to1(reinterpret_cast(ptr->image))); +#elif RNIFTI_NIFTILIB_VERSION == 2 + if (!imageObject.hasAttribute(".nifti_image_ver") || int(imageObject.attr(".nifti_image_ver")) == 1) + acquire(internal::convertImageV1to2(reinterpret_cast(ptr->image))); +#endif + // Copy if the object have multiple R-level references and we're not working read-only + else if (MAYBE_SHARED(object) && !readOnly) + copy(*ptr); + else + acquire(*ptr); + + resolved = true; + + if (imageObject.hasAttribute("dim")) + update(imageObject); + } else if (Rf_isString(object)) + throw std::runtime_error("Internal image is not valid"); + else + Rf_warning("Ignoring invalid internal pointer"); + } + + if (!resolved) { + if (Rf_isNull(object)) + acquire(NULL); + else if (Rf_isString(object)) { + const std::string path = Rcpp::as(object); +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_image_read(internal::stringToPath(path), readData)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_image_read(internal::stringToPath(path), readData)); +#endif + if (this->image == NULL) + throw std::runtime_error("Failed to read image from path " + path); + } else if (imageObject.inherits("nifti")) + initFromNiftiS4(imageObject, readData); + else if (imageObject.inherits("anlz")) + throw std::runtime_error("Cannot currently convert objects of class \"anlz\""); + else if (imageObject.inherits("MriImage")) + initFromMriImage(imageObject, readData); + else if (Rf_isVectorList(object)) + initFromList(imageObject); + else if (imageObject.hasAttribute("dim")) + initFromArray(imageObject, readData); + else if (imageObject.hasAttribute("class")) + throw std::runtime_error("Cannot convert object of class \"" + + Rcpp::as(imageObject.attr("class")) + "\" to a nifti_image"); + else + throw std::runtime_error("Cannot convert unclassed non-array object"); + } + + if (this->image != NULL) { +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_update_dims_from_array(this->image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_update_dims_from_array(this->image); +#endif + } + +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from SEXP)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif +} + +#endif // USING_R + +inline void NiftiImage::initFromDims(const std::vector& dim, const int datatype) +{ + const int nDims = std::min(7, int(dim.size())); + dim_t dims[8] = {nDims, 0, 0, 0, 0, 0, 0, 0}; + std::copy(dim.begin(), dim.begin() + nDims, &dims[1]); + +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_make_new_nim(dims, datatype, 1)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_make_new_nim(dims, datatype, 1)); +#endif + + if (image == NULL) + throw std::runtime_error("Failed to create image from scratch"); +} + +inline NiftiImage::NiftiImage(const std::vector& dim, const int datatype) : image(NULL), refCount(NULL) +{ + initFromDims(dim, datatype); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from dims)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif +} + +inline NiftiImage::NiftiImage(const std::vector& dim, const std::string& datatype) : image(NULL), refCount(NULL) +{ + initFromDims(dim, internal::stringToDatatype(datatype)); +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from dims)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif +} + +inline NiftiImage::NiftiImage(const std::string& path, const bool readData) : image(NULL), refCount(NULL) +{ +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_image_read(internal::stringToPath(path), readData)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_image_read(internal::stringToPath(path), readData)); +#endif + + if (image == NULL) + throw std::runtime_error("Failed to read image from path " + path); + +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from string)\n", RNIFTI_NIFTILIB_VERSION, this->image); +#endif +} + +inline NiftiImage::NiftiImage(const uint8_t* data, const size_t size, const int gz, const size_t estimated_data_size) + : image(NULL), refCount(NULL) +{ + // the header size for nifti_1 is 348 and for nifti_2 it is 348 + extra 192 bytes. + // the decoder tries to read first 348 to detect if it is the version 1 or version 2. + // For version 2 the first bytes are reused hence we need to assume the 540 bytes header size, + // to be sure that the hint is accuarate in case the nifti is version 2. + // Then we add extra 128 bytes for extensions as there is no size limit for extensions. +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_image_mem_read(data, size, gz, estimated_data_size + sizeof(nifti_2_header) + 128)); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_image_mem_read(data, size, gz, estimated_data_size + sizeof(nifti_2_header) + 128)); +#endif +} + +inline NiftiImage::NiftiImage(const std::string& path, const std::vector& volumes) : image(NULL), refCount(NULL) +{ + if (volumes.empty()) + throw std::runtime_error("The vector of volumes is empty"); + + nifti_brick_list brickList; + +#if RNIFTI_NIFTILIB_VERSION == 1 + acquire(nifti_image_read_bricks(internal::stringToPath(path), volumes.size(), &volumes.front(), &brickList)); + + if (image == NULL) + throw std::runtime_error("Failed to read image from path " + path); + + size_t brickSize = image->nbyper * image->nx * image->ny * image->nz; + image->data = calloc(1, nifti_get_volsize(image)); + for (dim_t i = 0; i < brickList.nbricks; i++) + memcpy((char*)image->data + i * brickSize, brickList.bricks[i], brickSize); + + nifti_free_NBL(&brickList); +#elif RNIFTI_NIFTILIB_VERSION == 2 + acquire(nifti2_image_read_bricks(internal::stringToPath(path), volumes.size(), &volumes.front(), &brickList)); + + if (image == NULL) + throw std::runtime_error("Failed to read image from path " + path); + + size_t brickSize = image->nbyper * image->nx * image->ny * image->nz; + image->data = calloc(1, nifti2_get_volsize(image)); + for (dim_t i = 0; i < brickList.nbricks; i++) + memcpy((char*)image->data + i * brickSize, brickList.bricks[i], brickSize); + + nifti2_free_NBL(&brickList); +#endif + +#ifndef NDEBUG + Rc_printf("Creating NiftiImage (v%d) with pointer %p (from string and volume vector)\n", RNIFTI_NIFTILIB_VERSION, + this->image); +#endif +} + +inline void NiftiImage::updatePixdim(const std::vector& pixdim) +{ + const int nDims = image->dim[0]; + const std::vector origPixdim(image->pixdim + 1, image->pixdim + 4); + + for (int i = 1; i < 8; i++) + image->pixdim[i] = 0.0; + + const int pixdimLength = pixdim.size(); + for (int i = 0; i < std::min(pixdimLength, nDims); i++) + image->pixdim[i + 1] = pixdim[i]; + + if (!std::equal(origPixdim.begin(), origPixdim.begin() + std::min(3, nDims), pixdim.begin())) { + Xform::Matrix scaleMatrix = Xform::Matrix::eye(); + for (int i = 0; i < std::min(pixdimLength, 3); i++) + scaleMatrix(i, i) = pixdim[i] / origPixdim[i]; + + if (image->qform_code > 0) + this->qform() = qform().matrix() * scaleMatrix; + if (image->sform_code > 0) + this->sform() = sform().matrix() * scaleMatrix; + } +} + +inline void NiftiImage::setPixunits(const std::vector& pixunits) +{ + for (size_t i = 0; i < pixunits.size(); i++) { + if (pixunits[i] == "m") + image->xyz_units = NIFTI_UNITS_METER; + else if (pixunits[i] == "mm") + image->xyz_units = NIFTI_UNITS_MM; + else if (pixunits[i] == "um") + image->xyz_units = NIFTI_UNITS_MICRON; + else if (pixunits[i] == "s") + image->time_units = NIFTI_UNITS_SEC; + else if (pixunits[i] == "ms") + image->time_units = NIFTI_UNITS_MSEC; + else if (pixunits[i] == "us") + image->time_units = NIFTI_UNITS_USEC; + else if (pixunits[i] == "Hz") + image->time_units = NIFTI_UNITS_HZ; + else if (pixunits[i] == "ppm") + image->time_units = NIFTI_UNITS_PPM; + else if (pixunits[i] == "rad/s") + image->time_units = NIFTI_UNITS_RADS; + } +} + +inline NiftiImage& NiftiImage::rescale(const std::vector& scales) +{ + std::vector pixdim(image->pixdim + 1, image->pixdim + 4); + + for (int i = 0; i < std::min(3, int(scales.size())); i++) { + if (scales[i] != 1.0) { + pixdim[i] /= scales[i]; + image->dim[i + 1] = static_cast(std::floor(image->dim[i + 1] * scales[i])); + } + } + + updatePixdim(pixdim); + + // Data vector is now the wrong size, so drop it +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_update_dims_from_array(image); + nifti_image_unload(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_update_dims_from_array(image); + nifti2_image_unload(image); +#endif + + image->scl_slope = 0.0; + image->scl_inter = 0.0; + + return *this; +} + +inline NiftiImage& NiftiImage::reorient(const int icode, const int jcode, const int kcode) +{ + if (this->isNull()) + return *this; + if (image->qform_code == 0 && image->sform_code == 0) { + Rf_warning("Image qform and sform codes are both zero, so it cannot be reoriented"); + return *this; + } + + int used[6] = {0, 0, 0, 0, 0, 0}; + used[icode - 1] = 1; + used[jcode - 1] = 1; + used[kcode - 1] = 1; + if (used[0] + used[1] != 1 || used[2] + used[3] != 1 || used[4] + used[5] != 1) + throw std::runtime_error("Each canonical axis should be used exactly once"); + + const int codes[3] = {icode, jcode, kcode}; + const Xform native = this->xform(); + + // Calculate the origin, which requires inverting the current xform + // Here we use a simplified formula that exploits blockwise inversion and the nature of xforms + Xform::Vector3 origin = -(native.submatrix().inverse() * native.offset()); + + // Create a target xform (rotation matrix only) + Xform::Submatrix target; + for (int j = 0; j < 3; j++) { + for (int i = 0; i < 3; i++) + target(i, j) = 0.0; + + switch (codes[j]) { + case NIFTI_L2R: + target(0, j) = 1.0; + break; + case NIFTI_R2L: + target(0, j) = -1.0; + break; + case NIFTI_P2A: + target(1, j) = 1.0; + break; + case NIFTI_A2P: + target(1, j) = -1.0; + break; + case NIFTI_I2S: + target(2, j) = 1.0; + break; + case NIFTI_S2I: + target(2, j) = -1.0; + break; + } + } + + // Extract (inverse of) canonical axis matrix from native xform + int nicode, njcode, nkcode; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_mat44_to_orientation(native, &nicode, &njcode, &nkcode); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_dmat44_to_orientation(native, &nicode, &njcode, &nkcode); +#endif + int ncodes[3] = {nicode, njcode, nkcode}; + Xform::Submatrix nativeAxesTransposed; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) + nativeAxesTransposed(i, j) = 0.0; + + switch (ncodes[i]) { + case NIFTI_L2R: + nativeAxesTransposed(i, 0) = 1.0; + break; + case NIFTI_R2L: + nativeAxesTransposed(i, 0) = -1.0; + break; + case NIFTI_P2A: + nativeAxesTransposed(i, 1) = 1.0; + break; + case NIFTI_A2P: + nativeAxesTransposed(i, 1) = -1.0; + break; + case NIFTI_I2S: + nativeAxesTransposed(i, 2) = 1.0; + break; + case NIFTI_S2I: + nativeAxesTransposed(i, 2) = -1.0; + break; + } + } + + // Check for no-op case + if (icode == nicode && jcode == njcode && kcode == nkcode) + return *this; + + // The transform is t(approx_old_xform) %*% target_xform + // The new xform is old_xform %*% transform + // NB: "transform" is really 4x4, but the last row is simple and the last column is filled below + const Xform::Matrix& nativeMat = native.matrix(); + Xform::Submatrix transform = nativeAxesTransposed * target; + Xform::Matrix result; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 3; j++) + result(i, j) = nativeMat(i, 0) * transform(0, j) + nativeMat(i, 1) * transform(1, j) + + nativeMat(i, 2) * transform(2, j); + + result(3, i) = (i == 3 ? 1.0 : 0.0); + } + + // Extract the mapping between dimensions and the signs + // These vectors are all indexed in the target space, except "revsigns" + dim_t locs[3], signs[3], newdim[3], revsigns[3]; + pixdim_t newpixdim[3]; + double maxes[3] = {R_NegInf, R_NegInf, R_NegInf}; + Xform::Vector3 offset; + for (int j = 0; j < 3; j++) { + // Find the largest absolute value in each column, which gives the old dimension corresponding to each new + // dimension + for (int i = 0; i < 3; i++) { + const double value = static_cast(transform(i, j)); + if (fabs(value) > maxes[j]) { + maxes[j] = fabs(value); + signs[j] = value > 0.0 ? 1 : -1; + locs[j] = i; + } + } + + // Obtain the sign for the reverse mapping + revsigns[locs[j]] = signs[j]; + + // Permute dim and pixdim + newdim[j] = image->dim[locs[j] + 1]; + newpixdim[j] = image->pixdim[locs[j] + 1]; + + // Flip and/or permute the origin + if (signs[j] < 0) + offset[j] = image->dim[locs[j] + 1] - origin[locs[j]] - 1.0; + else + offset[j] = origin[locs[j]]; + } + + // Convert the origin back to an xform offset and insert it + offset = -(Xform(result).submatrix() * offset); + for (int i = 0; i < 3; i++) + result(i, 3) = offset[i]; + + // Update the xforms with nonzero codes + if (image->qform_code > 0) + this->qform() = result; + if (image->sform_code > 0) + this->sform() = result; + + // Calculate strides: the step in target space associated with each dimension in source space + ptrdiff_t strides[3]; + strides[locs[0]] = 1; + strides[locs[1]] = strides[locs[0]] * image->dim[locs[0] + 1]; + strides[locs[2]] = strides[locs[1]] * image->dim[locs[1] + 1]; + + // Permute the data (if present) + if (image->data != NULL) { + size_t volSize = size_t(image->nx * image->ny * image->nz); + size_t nVolumes = std::max(size_t(1), size_t(image->nvox) / volSize); + + const NiftiImageData oldData = this->data(); + NiftiImageData newData(oldData); + + // Where the sign is negative we need to start at the end of the dimension + size_t volStart = 0; + for (int i = 0; i < 3; i++) { + if (revsigns[i] < 0) + volStart += (image->dim[i + 1] - 1) * strides[i]; + } + + // Iterate over the data and place it into a new vector + NiftiImageData::Iterator it = oldData.begin(); + for (size_t v = 0; v < nVolumes; v++) { + for (dim_t k = 0; k < image->nz; k++) { + ptrdiff_t offset = k * strides[2] * revsigns[2]; + for (dim_t j = 0; j < image->ny; j++) { + for (dim_t i = 0; i < image->nx; i++) { + newData[volStart + offset] = *it++; + offset += strides[0] * revsigns[0]; + } + offset += strides[1] * revsigns[1] - image->nx * strides[0] * revsigns[0]; + } + } + volStart += volSize; + } + + // Vector data needs to be reoriented to match the xform + if (image->intent_code == NIFTI_INTENT_VECTOR && image->dim[5] == 3) { + Xform::Vector3 oldVec; + const size_t supervolSize = volSize * image->nt; + NiftiImageData::Iterator it = newData.begin(); + for (size_t i = 0; i < supervolSize; i++, ++it) { + for (int j = 0; j < 3; j++) + oldVec[j] = double(*(it + j * supervolSize)); + const Xform::Vector3 newVec = transform * oldVec; + for (int j = 0; j < 3; j++) + *(it + j * supervolSize) = newVec[j]; + } + } + + // Replace the existing data in the image + this->replaceData(newData); + } + + // Copy new dims and pixdims in + // NB: Old dims are used above, so this must happen last + std::copy(newdim, newdim + 3, image->dim + 1); + std::copy(newpixdim, newpixdim + 3, image->pixdim + 1); +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_update_dims_from_array(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_update_dims_from_array(image); +#endif + + return *this; +} + +inline NiftiImage& NiftiImage::reorient(const std::string& orientation) +{ + if (orientation.length() != 3) + throw std::runtime_error("Orientation string should have exactly three characters"); + + int codes[3]; + for (int i = 0; i < 3; i++) { + switch (orientation[i]) { + case 'r': + case 'R': + codes[i] = NIFTI_L2R; + break; + case 'l': + case 'L': + codes[i] = NIFTI_R2L; + break; + case 'a': + case 'A': + codes[i] = NIFTI_P2A; + break; + case 'p': + case 'P': + codes[i] = NIFTI_A2P; + break; + case 's': + case 'S': + codes[i] = NIFTI_I2S; + break; + case 'i': + case 'I': + codes[i] = NIFTI_S2I; + break; + + default: + throw std::runtime_error("Orientation string is invalid"); + } + } + + return reorient(codes[0], codes[1], codes[2]); +} + +#ifdef USING_R + +inline NiftiImage& NiftiImage::update(const Rcpp::RObject& object) +{ + if (Rf_isVectorList(object)) { + Rcpp::List list(object); + nifti_1_header* header = NULL; + if (this->isNull()) { +#if RNIFTI_NIFTILIB_VERSION == 1 + header = nifti_make_new_header(NULL, DT_FLOAT64); +#elif RNIFTI_NIFTILIB_VERSION == 2 + header = nifti_make_new_n1_header(NULL, DT_FLOAT64); +#endif + internal::updateHeader(header, list, true); + } else { + header = (nifti_1_header*)calloc(1, sizeof(nifti_1_header)); +#if RNIFTI_NIFTILIB_VERSION == 1 + *header = nifti_convert_nim2nhdr(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_convert_nim2n1hdr(image, header); +#endif + internal::updateHeader(header, list, true); + } + + if (header != NULL) { + // Retain the data pointer, but otherwise overwrite the stored object with one created from the header + // The file names can't be preserved through the round-trip, so free them + void* dataPtr = image->data; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image* tempImage = nifti_convert_nhdr2nim(*header, NULL); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti_image* tempImage = nifti_convert_n1hdr2nim(*header, NULL); +#endif + + if (image->fname != NULL) + free(image->fname); + if (image->iname != NULL) + free(image->iname); + + memcpy(image, tempImage, sizeof(nifti_image)); + image->num_ext = 0; + image->ext_list = NULL; + image->data = dataPtr; + +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image_free(tempImage); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_image_free(tempImage); +#endif + free(header); + } + } else if (object.hasAttribute("dim")) { + for (int i = 0; i < 8; i++) + image->dim[i] = 0; + const std::vector dimVector = object.attr("dim"); + + const int nDims = std::min(7, int(dimVector.size())); + image->dim[0] = nDims; + for (int i = 0; i < nDims; i++) + image->dim[i + 1] = dimVector[i]; + + if (object.hasAttribute("pixdim")) { + const std::vector pixdimVector = object.attr("pixdim"); + updatePixdim(pixdimVector); + } + + if (object.hasAttribute("pixunits")) { + const std::vector pixunitsVector = object.attr("pixunits"); + setPixunits(pixunitsVector); + } + + // This library function clobbers dim[0] if the last dimension is unitary; we undo that here +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_update_dims_from_array(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_update_dims_from_array(image); +#endif + image->dim[0] = image->ndim = nDims; + + image->datatype = NiftiImage::sexpTypeToNiftiType(object.sexp_type()); + if (object.inherits("rgbArray")) { + const int channels = object.attr("channels"); + image->datatype = (channels == 4 ? DT_RGBA32 : DT_RGB24); + } + nifti_datatype_sizes(image->datatype, &image->nbyper, NULL); + +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image_unload(image); + const size_t dataSize = nifti_get_volsize(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_image_unload(image); + const size_t dataSize = nifti2_get_volsize(image); +#endif + + image->data = calloc(1, dataSize); + if (image->datatype == DT_INT32 || image->datatype == DT_RGBA32) + memcpy(image->data, INTEGER(object), dataSize); + else if (image->datatype == DT_RGB24) + std::copy(INTEGER(object), INTEGER(object) + image->nvox, this->data().begin()); + else if (image->datatype == DT_COMPLEX128) + memcpy(image->data, COMPLEX(object), dataSize); + else + memcpy(image->data, REAL(object), dataSize); + + image->scl_slope = 0.0; + image->scl_inter = 0.0; + } + + return *this; +} + +#endif // USING_R + +inline const NiftiImage::Xform NiftiImage::xform(const bool preferQuaternion) const +{ + if (image == NULL) + return Xform(); + else if (image->qform_code <= 0 && image->sform_code <= 0) { + // No qform or sform so use pixdim (NB: other software may assume differently) + Xform::Matrix matrix; + for (int i = 0; i < 3; i++) + matrix(i, i) = (image->pixdim[i + 1] == 0.0 ? 1.0 : image->pixdim[i + 1]); + matrix(3, 3) = 1.0; + return Xform(matrix); + } else if ((preferQuaternion && image->qform_code > 0) || image->sform_code <= 0) + return qform(); + else + return sform(); +} + +template +inline std::vector NiftiImage::Block::getData(const bool useSlope) const +{ + NiftiImageData data = this->data(); + if (!useSlope) + data = data.unscaled(); + + if (image.isNull() || data.isEmpty()) + return std::vector(); + else { + std::vector result(data.size()); + std::copy(data.begin(), data.end(), result.begin()); + return result; + } +} + +template +inline std::vector NiftiImage::getData(const bool useSlope) const +{ + NiftiImageData data = this->data(); + if (!useSlope) + data = data.unscaled(); + + if (this->isNull() || data.isEmpty()) + return std::vector(); + else { + std::vector result(data.size()); + std::copy(data.begin(), data.end(), result.begin()); + return result; + } +} + +inline NiftiImage& NiftiImage::changeDatatype(const int datatype, const bool useSlope) +{ + if (this->isNull() || image->datatype == datatype) + return *this; + + if (useSlope && this->isDataScaled()) + throw std::runtime_error( + "Resetting the slope and intercept for an image with them already set is not supported"); + + const NiftiImageData data(useSlope ? this->data() : this->data().unscaled(), datatype); + return replaceData(data); +} + +inline NiftiImage& NiftiImage::changeDatatype(const std::string& datatype, const bool useSlope) +{ + return changeDatatype(internal::stringToDatatype(datatype), useSlope); +} + +template +inline NiftiImage& NiftiImage::replaceData(const std::vector& data, const int datatype) +{ + replaceData(NiftiImageData(data.begin(), data.end(), datatype)); + return *this; +} + +inline NiftiImage& NiftiImage::replaceData(const NiftiImageData& data) +{ + if (this->isNull()) + return *this; + else if (data.isEmpty()) { +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image_unload(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_image_unload(image); +#endif + return *this; + } else if (data.length() != size_t(image->nvox)) + throw std::runtime_error("New data length does not match the number of voxels in the image"); + + // Copy the data + NiftiImageData copy = data; +#if RNIFTI_NIFTILIB_VERSION == 1 + nifti_image_unload(image); +#elif RNIFTI_NIFTILIB_VERSION == 2 + nifti2_image_unload(image); +#endif + image->data = copy.blob(); + image->datatype = copy.datatype(); + image->scl_slope = static_cast(copy.slope); + image->scl_inter = static_cast(copy.intercept); + nifti_datatype_sizes(image->datatype, &image->nbyper, &image->swapsize); + + double min, max; + copy.minmax(&min, &max); + image->cal_min = static_cast(min); + image->cal_max = static_cast(max); + + copy.disown(); + + return *this; +} + +inline std::pair NiftiImage::toFile(const std::string fileName, const int datatype, + const int filetype) const +{ + const bool changingDatatype = (datatype != DT_NONE && !this->isNull() && datatype != image->datatype); + + // Copy the source image only if the datatype will be changed + NiftiImage imageToWrite(*this, changingDatatype); + + if (changingDatatype) + imageToWrite.changeDatatype(datatype, true); + if (filetype >= 0 && filetype <= NIFTI_MAX_FTYPE) + imageToWrite->nifti_type = filetype; + +#if RNIFTI_NIFTILIB_VERSION == 1 + const int status = nifti_set_filenames(imageToWrite, internal::stringToPath(fileName), false, true); + if (status != 0) + throw std::runtime_error("Failed to set filenames for NIfTI object"); + nifti_image_write(imageToWrite); +#elif RNIFTI_NIFTILIB_VERSION == 2 + const int status = nifti2_set_filenames(imageToWrite, internal::stringToPath(fileName), false, true); + if (status != 0) + throw std::runtime_error("Failed to set filenames for NIfTI object"); + nifti2_image_write(imageToWrite); +#endif + + return std::pair(std::string(imageToWrite->fname), std::string(imageToWrite->iname)); +} + +inline std::pair NiftiImage::toFile(const std::string fileName, const std::string& datatype, + const int filetype) const +{ + return toFile(fileName, internal::stringToDatatype(datatype), filetype); +} + +#ifdef USING_R + +inline Rcpp::RObject NiftiImage::toArray() const +{ + Rcpp::RObject array; + + if (this->isNull()) + return array; + else { + NiftiImageData data = this->data(); + if (data.isEmpty()) { + Rf_warning("Internal image contains no data - filling array with NAs"); + array = Rcpp::LogicalVector(image->nvox, NA_LOGICAL); + } else if (data.isComplex()) + array = Rcpp::ComplexVector(data.begin(), data.end()); + else if (data.isFloatingPoint() || data.isScaled()) + array = Rcpp::NumericVector(data.begin(), data.end()); + else + array = Rcpp::IntegerVector(data.begin(), data.end()); + + internal::addAttributes(array, *this, true, true, false); + if (data.isRgb()) { + array.attr("class") = Rcpp::CharacterVector::create("niftiImage", "rgbArray", "array"); + array.attr("channels") = (data.datatype() == DT_RGBA32 ? 4 : 3); + } else + array.attr("class") = Rcpp::CharacterVector::create("niftiImage", "array"); + return array; + } +} + +inline Rcpp::RObject NiftiImage::toPointer(const std::string label) const +{ + if (this->isNull()) + return Rcpp::RObject(); + else { + Rcpp::RObject string = Rcpp::wrap(label); + internal::addAttributes(string, *this, false); + string.attr("class") = Rcpp::CharacterVector::create("internalImage", "niftiImage"); + return string; + } +} + +inline Rcpp::RObject NiftiImage::toArrayOrPointer(const bool internal, const std::string label) const +{ + return (internal ? toPointer(label) : toArray()); +} + +#endif // USING_R + +#endif diff --git a/cpp/3rd_party/rnifti/RNifti/NiftiImage_matrix.h b/cpp/3rd_party/rnifti/RNifti/NiftiImage_matrix.h new file mode 100644 index 0000000000..e89695db74 --- /dev/null +++ b/cpp/3rd_party/rnifti/RNifti/NiftiImage_matrix.h @@ -0,0 +1,135 @@ +#ifndef _NIFTI_IMAGE_MATRIX_H_ +#define _NIFTI_IMAGE_MATRIX_H_ + +template <> +inline SquareMatrix SquareMatrix::inverse () const +{ + return SquareMatrix(nifti_mat33_inverse(*niftiPointer())); +} + +template <> +inline SquareMatrix SquareMatrix::polar () const +{ + return SquareMatrix(nifti_mat33_polar(*niftiPointer())); +} + +template <> +inline float SquareMatrix::colnorm () const +{ + return nifti_mat33_colnorm(*niftiPointer()); +} + +template <> +inline float SquareMatrix::rownorm () const +{ + return nifti_mat33_rownorm(*niftiPointer()); +} + +template <> +inline float SquareMatrix::determ () const +{ + return nifti_mat33_determ(*niftiPointer()); +} + +template <> +inline SquareMatrix SquareMatrix::multiply (const SquareMatrix &other) const +{ + return SquareMatrix(nifti_mat33_mul(*niftiPointer(), *other.niftiPointer())); +} + +template <> +inline SquareMatrix SquareMatrix::inverse () const +{ + return SquareMatrix(nifti_mat44_inverse(*niftiPointer())); +} + +#if RNIFTI_NIFTILIB_VERSION == 1 + +// NB: niftilib v1 does not define nifti_mat44_mul +template <> +inline SquareMatrix SquareMatrix::multiply (const SquareMatrix &other) const +{ + SquareMatrix result; + for (int i=0; i < 4; i++) + { + for (int j=0; j < 4; j++) + { + result(i,j) = 0.0; + for (int k=0; k<4; k++) + result(i,j) += (*this)(i,k) * other(k,j); + } + } + return result; +} + +#elif RNIFTI_NIFTILIB_VERSION == 2 + +template <> +inline SquareMatrix SquareMatrix::inverse () const +{ + return SquareMatrix(nifti_dmat33_inverse(*niftiPointer())); +} + +template <> +inline SquareMatrix SquareMatrix::polar () const +{ + return SquareMatrix(nifti_dmat33_polar(*niftiPointer())); +} + +template <> +inline double SquareMatrix::colnorm () const +{ + return nifti_dmat33_colnorm(*niftiPointer()); +} + +template <> +inline double SquareMatrix::rownorm () const +{ + return nifti_dmat33_rownorm(*niftiPointer()); +} + +template <> +inline double SquareMatrix::determ () const +{ + return nifti_dmat33_determ(*niftiPointer()); +} + +template <> +inline SquareMatrix SquareMatrix::multiply (const SquareMatrix &other) const +{ + return SquareMatrix(nifti_dmat33_mul(*niftiPointer(), *other.niftiPointer())); +} + +template <> +inline SquareMatrix SquareMatrix::multiply (const SquareMatrix &other) const +{ + return SquareMatrix(nifti_mat44_mul(*niftiPointer(), *other.niftiPointer())); +} + +template <> +inline SquareMatrix SquareMatrix::inverse () const +{ + return SquareMatrix(nifti_dmat44_inverse(*niftiPointer())); +} + +template <> +inline SquareMatrix SquareMatrix::multiply (const SquareMatrix &other) const +{ + return SquareMatrix(nifti_dmat44_mul(*niftiPointer(), *other.niftiPointer())); +} + +#endif + +template +inline Vector SquareMatrix::multiply (const Vector &vec) const +{ + Vector result; + for (int i=0; i +#include + +#define Rc_printf Rprintf +#define Rc_fprintf_stdout(...) Rprintf(__VA_ARGS__) +#define Rc_fprintf_stderr(...) REprintf(__VA_ARGS__) +#define Rc_fputs_stdout(str) Rprintf(str) +#define Rc_fputs_stderr(str) REprintf(str) +#define Rc_fputc_stdout(ch) Rprintf("%c", ch) +#define Rc_fputc_stderr(ch) REprintf("%c", ch) + +#else + +#include + +#define Rc_printf printf +#define Rc_fprintf_stdout(...) fprintf(stdout, __VA_ARGS__) +#define Rc_fprintf_stderr(...) fprintf(stderr, __VA_ARGS__) +#define Rc_fputs_stdout(str) fputs(str, stdout) +#define Rc_fputs_stderr(str) fputs(str, stderr) +#define Rc_fputc_stdout(ch) fputc(ch, stdout) +#define Rc_fputc_stderr(ch) fputc(ch, stderr) +#define Rf_warning(str) fprintf(stderr, "%s\n", str) +#define Rprintf(...) fprintf(stderr, __VA_ARGS__) + +#endif // USING_R + +#endif // _PRINT_H_ diff --git a/cpp/3rd_party/rnifti/niftilib/nifti1.h b/cpp/3rd_party/rnifti/niftilib/nifti1.h new file mode 100644 index 0000000000..cf4bef2556 --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti1.h @@ -0,0 +1,1517 @@ +/** \file nifti1.h + \brief Official definition of the nifti1 header. Written by Bob Cox, SSCC, NIMH. + + HISTORY: + + 29 Nov 2007 [rickr] + - added DT_RGBA32 and NIFTI_TYPE_RGBA32 + - added NIFTI_INTENT codes: + TIME_SERIES, NODE_INDEX, RGB_VECTOR, RGBA_VECTOR, SHAPE + + 08 Mar 2019 [PT,DRG] + - Updated to include [qs]form_code = 5 + + */ + +#ifndef _NIFTI_HEADER_ +#define _NIFTI_HEADER_ + +/***************************************************************************** + ** This file defines the "NIFTI-1" header format. ** + ** It is derived from 2 meetings at the NIH (31 Mar 2003 and ** + ** 02 Sep 2003) of the Data Format Working Group (DFWG), ** + ** chartered by the NIfTI (Neuroimaging Informatics Technology ** + ** Initiative) at the National Institutes of Health (NIH). ** + **--------------------------------------------------------------** + ** Neither the National Institutes of Health (NIH), the DFWG, ** + ** nor any of the members or employees of these institutions ** + ** imply any warranty of usefulness of this material for any ** + ** purpose, and do not assume any liability for damages, ** + ** incidental or otherwise, caused by any use of this document. ** + ** If these conditions are not acceptable, do not use this! ** + **--------------------------------------------------------------** + ** Author: Robert W Cox (NIMH, Bethesda) ** + ** Advisors: John Ashburner (FIL, London), ** + ** Stephen Smith (FMRIB, Oxford), ** + ** Mark Jenkinson (FMRIB, Oxford) ** +******************************************************************************/ + +/*---------------------------------------------------------------------------*/ +/* Note that the ANALYZE 7.5 file header (dbh.h) is + (c) Copyright 1986-1995 + Biomedical Imaging Resource + Mayo Foundation + Incorporation of components of dbh.h are by permission of the + Mayo Foundation. + + Changes from the ANALYZE 7.5 file header in this file are released to the + public domain, including the functional comments and any amusing asides. +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/*! INTRODUCTION TO NIFTI-1: + ------------------------ + The twin (and somewhat conflicting) goals of this modified ANALYZE 7.5 + format are: + (a) To add information to the header that will be useful for functional + neuroimaging data analysis and display. These additions include: + - More basic data types. + - Two affine transformations to specify voxel coordinates. + - "Intent" codes and parameters to describe the meaning of the data. + - Affine scaling of the stored data values to their "true" values. + - Optional storage of the header and image data in one file (.nii). + (b) To maintain compatibility with non-NIFTI-aware ANALYZE 7.5 compatible + software (i.e., such a program should be able to do something useful + with a NIFTI-1 dataset -- at least, with one stored in a traditional + .img/.hdr file pair). + + Most of the unused fields in the ANALYZE 7.5 header have been taken, + and some of the lesser-used fields have been co-opted for other purposes. + Notably, most of the data_history substructure has been co-opted for + other purposes, since the ANALYZE 7.5 format describes this substructure + as "not required". + + NIFTI-1 FLAG (MAGIC STRINGS): + ---------------------------- + To flag such a struct as being conformant to the NIFTI-1 spec, the last 4 + bytes of the header must be either the C String "ni1" or "n+1"; + in hexadecimal, the 4 bytes + 6E 69 31 00 or 6E 2B 31 00 + (in any future version of this format, the '1' will be upgraded to '2', + etc.). Normally, such a "magic number" or flag goes at the start of the + file, but trying to avoid clobbering widely-used ANALYZE 7.5 fields led to + putting this marker last. However, recall that "the last shall be first" + (Matthew 20:16). + + If a NIFTI-aware program reads a header file that is NOT marked with a + NIFTI magic string, then it should treat the header as an ANALYZE 7.5 + structure. + + NIFTI-1 FILE STORAGE: + -------------------- + "ni1" means that the image data is stored in the ".img" file corresponding + to the header file (starting at file offset 0). + + "n+1" means that the image data is stored in the same file as the header + information. We recommend that the combined header+data filename suffix + be ".nii". When the dataset is stored in one file, the first byte of image + data is stored at byte location (int)vox_offset in this combined file. + The minimum allowed value of vox_offset is 352; for compatibility with + some software, vox_offset should be an integral multiple of 16. + + GRACE UNDER FIRE: + ---------------- + Most NIFTI-aware programs will only be able to handle a subset of the full + range of datasets possible with this format. All NIFTI-aware programs + should take care to check if an input dataset conforms to the program's + needs and expectations (e.g., check datatype, intent_code, etc.). If the + input dataset can't be handled by the program, the program should fail + gracefully (e.g., print a useful warning; not crash). + + SAMPLE CODES: + ------------ + The associated files nifti1_io.h and nifti1_io.c provide a sample + implementation in C of a set of functions to read, write, and manipulate + NIFTI-1 files. The file nifti1_test.c is a sample program that uses + the nifti1_io.c functions. +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* HEADER STRUCT DECLARATION: + ------------------------- + In the comments below for each field, only NIFTI-1 specific requirements + or changes from the ANALYZE 7.5 format are described. For convenience, + the 348 byte header is described as a single struct, rather than as the + ANALYZE 7.5 group of 3 substructs. + + Further comments about the interpretation of various elements of this + header are after the data type definition itself. Fields that are + marked as ++UNUSED++ have no particular interpretation in this standard. + (Also see the UNUSED FIELDS comment section, far below.) + + The presumption below is that the various C types have particular sizes: + sizeof(int) = sizeof(float) = 4 ; sizeof(short) = 2 +-----------------------------------------------------------------------------*/ + + +/*! \struct nifti_1_header + \brief Data structure defining the fields in the nifti1 header. + This binary header should be found at the beginning of a valid + NIFTI-1 header file. + */ + /*************************/ /************************/ +struct nifti_1_header { /* NIFTI-1 usage */ /* ANALYZE 7.5 field(s) */ + /*************************/ /************************/ + + /*--- was header_key substruct ---*/ + int sizeof_hdr; /*!< MUST be 348 */ /* int sizeof_hdr; */ + char data_type[10]; /*!< ++UNUSED++ */ /* char data_type[10]; */ + char db_name[18]; /*!< ++UNUSED++ */ /* char db_name[18]; */ + int extents; /*!< ++UNUSED++ */ /* int extents; */ + short session_error; /*!< ++UNUSED++ */ /* short session_error; */ + char regular; /*!< ++UNUSED++ */ /* char regular; */ + char dim_info; /*!< MRI slice ordering. */ /* char hkey_un0; */ + + /*--- was image_dimension substruct ---*/ + short dim[8]; /*!< Data array dimensions.*/ /* short dim[8]; */ + float intent_p1 ; /*!< 1st intent parameter. */ /* short unused8; */ + /* short unused9; */ + float intent_p2 ; /*!< 2nd intent parameter. */ /* short unused10; */ + /* short unused11; */ + float intent_p3 ; /*!< 3rd intent parameter. */ /* short unused12; */ + /* short unused13; */ + short intent_code ; /*!< NIFTI_INTENT_* code. */ /* short unused14; */ + short datatype; /*!< Defines data type! */ /* short datatype; */ + short bitpix; /*!< Number bits/voxel. */ /* short bitpix; */ + short slice_start; /*!< First slice index. */ /* short dim_un0; */ + float pixdim[8]; /*!< Grid spacings. */ /* float pixdim[8]; */ + float vox_offset; /*!< Offset into .nii file */ /* float vox_offset; */ + float scl_slope ; /*!< Data scaling: slope. */ /* float funused1; */ + float scl_inter ; /*!< Data scaling: offset. */ /* float funused2; */ + short slice_end; /*!< Last slice index. */ /* float funused3; */ + char slice_code ; /*!< Slice timing order. */ + char xyzt_units ; /*!< Units of pixdim[1..4] */ + float cal_max; /*!< Max display intensity */ /* float cal_max; */ + float cal_min; /*!< Min display intensity */ /* float cal_min; */ + float slice_duration;/*!< Time for 1 slice. */ /* float compressed; */ + float toffset; /*!< Time axis shift. */ /* float verified; */ + int glmax; /*!< ++UNUSED++ */ /* int glmax; */ + int glmin; /*!< ++UNUSED++ */ /* int glmin; */ + + /*--- was data_history substruct ---*/ + char descrip[80]; /*!< any text you like. */ /* char descrip[80]; */ + char aux_file[24]; /*!< auxiliary filename. */ /* char aux_file[24]; */ + + short qform_code ; /*!< NIFTI_XFORM_* code. */ /*-- all ANALYZE 7.5 ---*/ + short sform_code ; /*!< NIFTI_XFORM_* code. */ /* fields below here */ + /* are replaced */ + float quatern_b ; /*!< Quaternion b param. */ + float quatern_c ; /*!< Quaternion c param. */ + float quatern_d ; /*!< Quaternion d param. */ + float qoffset_x ; /*!< Quaternion x shift. */ + float qoffset_y ; /*!< Quaternion y shift. */ + float qoffset_z ; /*!< Quaternion z shift. */ + + float srow_x[4] ; /*!< 1st row affine transform. */ + float srow_y[4] ; /*!< 2nd row affine transform. */ + float srow_z[4] ; /*!< 3rd row affine transform. */ + + char intent_name[16];/*!< 'name' or meaning of data. */ + + char magic[4] ; /*!< MUST be "ni1\0" or "n+1\0". */ + +} ; /**** 348 bytes total ****/ + +typedef struct nifti_1_header nifti_1_header ; + +/*---------------------------------------------------------------------------*/ +/* HEADER EXTENSIONS: + ----------------- + After the end of the 348 byte header (e.g., after the magic field), + the next 4 bytes are a char array field named "extension". By default, + all 4 bytes of this array should be set to zero. In a .nii file, these + 4 bytes will always be present, since the earliest start point for + the image data is byte #352. In a separate .hdr file, these bytes may + or may not be present. If not present (i.e., if the length of the .hdr + file is 348 bytes), then a NIfTI-1 compliant program should use the + default value of extension={0,0,0,0}. The first byte (extension[0]) + is the only value of this array that is specified at present. The other + 3 bytes are reserved for future use. + + If extension[0] is nonzero, it indicates that extended header information + is present in the bytes following the extension array. In a .nii file, + this extended header data is before the image data (and vox_offset + must be set correctly to allow for this). In a .hdr file, this extended + data follows extension and proceeds (potentially) to the end of the file. + + The format of extended header data is weakly specified. Each extension + must be an integer multiple of 16 bytes long. The first 8 bytes of each + extension comprise 2 integers: + int esize , ecode ; + These values may need to be byte-swapped, as indicated by dim[0] for + the rest of the header. + * esize is the number of bytes that form the extended header data + + esize must be a positive integral multiple of 16 + + this length includes the 8 bytes of esize and ecode themselves + * ecode is a non-negative integer that indicates the format of the + extended header data that follows + + different ecode values are assigned to different developer groups + + at present, the "registered" values for code are + = 0 = unknown private format (not recommended!) + = 2 = DICOM format (i.e., attribute tags and values) + = 4 = AFNI group (i.e., ASCII XML-ish elements) + In the interests of interoperability (a primary rationale for NIfTI), + groups developing software that uses this extension mechanism are + encouraged to document and publicize the format of their extensions. + To this end, the NIfTI DFWG will assign even numbered codes upon request + to groups submitting at least rudimentary documentation for the format + of their extension; at present, the contact is mailto:rwcox@nih.gov. + The assigned codes and documentation will be posted on the NIfTI + website. All odd values of ecode (and 0) will remain unassigned; + at least, until the even ones are used up, when we get to 2,147,483,646. + + Note that the other contents of the extended header data section are + totally unspecified by the NIfTI-1 standard. In particular, if binary + data is stored in such a section, its byte order is not necessarily + the same as that given by examining dim[0]; it is incumbent on the + programs dealing with such data to determine the byte order of binary + extended header data. + + Multiple extended header sections are allowed, each starting with an + esize,ecode value pair. The first esize value, as described above, + is at bytes #352-355 in the .hdr or .nii file (files start at byte #0). + If this value is positive, then the second (esize2) will be found + starting at byte #352+esize1 , the third (esize3) at byte #352+esize1+esize2, + et cetera. Of course, in a .nii file, the value of vox_offset must + be compatible with these extensions. If a malformed file indicates + that an extended header data section would run past vox_offset, then + the entire extended header section should be ignored. In a .hdr file, + if an extended header data section would run past the end-of-file, + that extended header data should also be ignored. + + With the above scheme, a program can successively examine the esize + and ecode values, and skip over each extended header section if the + program doesn't know how to interpret the data within. Of course, any + program can simply ignore all extended header sections simply by jumping + straight to the image data using vox_offset. +-----------------------------------------------------------------------------*/ + +/*! \struct nifti1_extender + \brief This structure represents a 4-byte string that should follow the + binary nifti_1_header data in a NIFTI-1 header file. If the char + values are {1,0,0,0}, the file is expected to contain extensions, + values of {0,0,0,0} imply the file does not contain extensions. + Other sequences of values are not currently defined. + */ +struct nifti1_extender { char extension[4] ; } ; +typedef struct nifti1_extender nifti1_extender ; + +/*! \struct nifti1_extension + \brief Data structure defining the fields of a header extension. + */ +struct nifti1_extension { + int esize ; /*!< size of extension, in bytes (must be multiple of 16) */ + int ecode ; /*!< extension code, one of the NIFTI_ECODE_ values */ + char * edata ; /*!< raw data, with no byte swapping (length is esize-8) */ +} ; +typedef struct nifti1_extension nifti1_extension ; + +/*---------------------------------------------------------------------------*/ +/* DATA DIMENSIONALITY (as in ANALYZE 7.5): + --------------------------------------- + dim[0] = number of dimensions; + - if dim[0] is outside range 1..7, then the header information + needs to be byte swapped appropriately + - ANALYZE supports dim[0] up to 7, but NIFTI-1 reserves + dimensions 1,2,3 for space (x,y,z), 4 for time (t), and + 5,6,7 for anything else needed. + + dim[i] = length of dimension #i, for i=1..dim[0] (must be positive) + - also see the discussion of intent_code, far below + + pixdim[i] = voxel width along dimension #i, i=1..dim[0] (positive) + - cf. ORIENTATION section below for use of pixdim[0] + - the units of pixdim can be specified with the xyzt_units + field (also described far below). + + Number of bits per voxel value is in bitpix, which MUST correspond with + the datatype field. The total number of bytes in the image data is + dim[1] * ... * dim[dim[0]] * bitpix / 8 + + In NIFTI-1 files, dimensions 1,2,3 are for space, dimension 4 is for time, + and dimension 5 is for storing multiple values at each spatiotemporal + voxel. Some examples: + - A typical whole-brain FMRI experiment's time series: + - dim[0] = 4 + - dim[1] = 64 pixdim[1] = 3.75 xyzt_units = NIFTI_UNITS_MM + - dim[2] = 64 pixdim[2] = 3.75 | NIFTI_UNITS_SEC + - dim[3] = 20 pixdim[3] = 5.0 + - dim[4] = 120 pixdim[4] = 2.0 + - A typical T1-weighted anatomical volume: + - dim[0] = 3 + - dim[1] = 256 pixdim[1] = 1.0 xyzt_units = NIFTI_UNITS_MM + - dim[2] = 256 pixdim[2] = 1.0 + - dim[3] = 128 pixdim[3] = 1.1 + - A single slice EPI time series: + - dim[0] = 4 + - dim[1] = 64 pixdim[1] = 3.75 xyzt_units = NIFTI_UNITS_MM + - dim[2] = 64 pixdim[2] = 3.75 | NIFTI_UNITS_SEC + - dim[3] = 1 pixdim[3] = 5.0 + - dim[4] = 1200 pixdim[4] = 0.2 + - A 3-vector stored at each point in a 3D volume: + - dim[0] = 5 + - dim[1] = 256 pixdim[1] = 1.0 xyzt_units = NIFTI_UNITS_MM + - dim[2] = 256 pixdim[2] = 1.0 + - dim[3] = 128 pixdim[3] = 1.1 + - dim[4] = 1 pixdim[4] = 0.0 + - dim[5] = 3 intent_code = NIFTI_INTENT_VECTOR + - A single time series with a 3x3 matrix at each point: + - dim[0] = 5 + - dim[1] = 1 xyzt_units = NIFTI_UNITS_SEC + - dim[2] = 1 + - dim[3] = 1 + - dim[4] = 1200 pixdim[4] = 0.2 + - dim[5] = 9 intent_code = NIFTI_INTENT_GENMATRIX + - intent_p1 = intent_p2 = 3.0 (indicates matrix dimensions) +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* DATA STORAGE: + ------------ + If the magic field is "n+1", then the voxel data is stored in the + same file as the header. In this case, the voxel data starts at offset + (int)vox_offset into the header file. Thus, vox_offset=352.0 means that + the data starts immediately after the NIFTI-1 header. If vox_offset is + greater than 352, the NIFTI-1 format does not say much about the + contents of the dataset file between the end of the header and the + start of the data. + + FILES: + ----- + If the magic field is "ni1", then the voxel data is stored in the + associated ".img" file, starting at offset 0 (i.e., vox_offset is not + used in this case, and should be set to 0.0). + + When storing NIFTI-1 datasets in pairs of files, it is customary to name + the files in the pattern "name.hdr" and "name.img", as in ANALYZE 7.5. + When storing in a single file ("n+1"), the file name should be in + the form "name.nii" (the ".nft" and ".nif" suffixes are already taken; + cf. http://www.icdatamaster.com/n.html ). + + BYTE ORDERING: + ------------- + The byte order of the data arrays is presumed to be the same as the byte + order of the header (which is determined by examining dim[0]). + + Floating point types are presumed to be stored in IEEE-754 format. +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* DETAILS ABOUT vox_offset: + ------------------------ + In a .nii file, the vox_offset field value is interpreted as the start + location of the image data bytes in that file. In a .hdr/.img file pair, + the vox_offset field value is the start location of the image data + bytes in the .img file. + * If vox_offset is less than 352 in a .nii file, it is equivalent + to 352 (i.e., image data never starts before byte #352 in a .nii file). + * The default value for vox_offset in a .nii file is 352. + * In a .hdr file, the default value for vox_offset is 0. + * vox_offset should be an integer multiple of 16; otherwise, some + programs may not work properly (e.g., SPM). This is to allow + memory-mapped input to be properly byte-aligned. + Note that since vox_offset is an IEEE-754 32 bit float (for compatibility + with the ANALYZE-7.5 format), it effectively has a 24 bit mantissa. All + integers from 0 to 2^24 can be represented exactly in this format, but not + all larger integers are exactly storable as IEEE-754 32 bit floats. However, + unless you plan to have vox_offset be potentially larger than 16 MB, this + should not be an issue. (Actually, any integral multiple of 16 up to 2^27 + can be represented exactly in this format, which allows for up to 128 MB + of random information before the image data. If that isn't enough, then + perhaps this format isn't right for you.) + + In a .img file (i.e., image data stored separately from the NIfTI-1 + header), data bytes between #0 and #vox_offset-1 (inclusive) are completely + undefined and unregulated by the NIfTI-1 standard. One potential use of + having vox_offset > 0 in the .hdr/.img file pair storage method is to make + the .img file be a copy of (or link to) a pre-existing image file in some + other format, such as DICOM; then vox_offset would be set to the offset of + the image data in this file. (It may not be possible to follow the + "multiple-of-16 rule" with an arbitrary external file; using the NIfTI-1 + format in such a case may lead to a file that is incompatible with software + that relies on vox_offset being a multiple of 16.) + + In a .nii file, data bytes between #348 and #vox_offset-1 (inclusive) may + be used to store user-defined extra information; similarly, in a .hdr file, + any data bytes after byte #347 are available for user-defined extra + information. The (very weak) regulation of this extra header data is + described elsewhere. +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* DATA SCALING: + ------------ + If the scl_slope field is nonzero, then each voxel value in the dataset + should be scaled as + y = scl_slope * x + scl_inter + where x = voxel value stored + y = "true" voxel value + Normally, we would expect this scaling to be used to store "true" floating + values in a smaller integer datatype, but that is not required. That is, + it is legal to use scaling even if the datatype is a float type (crazy, + perhaps, but legal). + - However, the scaling is to be ignored if datatype is DT_RGB24. + - If datatype is a complex type, then the scaling is to be + applied to both the real and imaginary parts. + + The cal_min and cal_max fields (if nonzero) are used for mapping (possibly + scaled) dataset values to display colors: + - Minimum display intensity (black) corresponds to dataset value cal_min. + - Maximum display intensity (white) corresponds to dataset value cal_max. + - Dataset values below cal_min should display as black also, and values + above cal_max as white. + - Colors "black" and "white", of course, may refer to any scalar display + scheme (e.g., a color lookup table specified via aux_file). + - cal_min and cal_max only make sense when applied to scalar-valued + datasets (i.e., dim[0] < 5 or dim[5] = 1). +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* TYPE OF DATA (acceptable values for datatype field): + --------------------------------------------------- + Values of datatype smaller than 256 are ANALYZE 7.5 compatible. + Larger values are NIFTI-1 additions. These are all multiples of 256, so + that no bits below position 8 are set in datatype. But there is no need + to use only powers-of-2, as the original ANALYZE 7.5 datatype codes do. + + The additional codes are intended to include a complete list of basic + scalar types, including signed and unsigned integers from 8 to 64 bits, + floats from 32 to 128 bits, and complex (float pairs) from 64 to 256 bits. + + Note that most programs will support only a few of these datatypes! + A NIFTI-1 program should fail gracefully (e.g., print a warning message) + when it encounters a dataset with a type it doesn't like. +-----------------------------------------------------------------------------*/ + +#undef DT_UNKNOWN /* defined in dirent.h on some Unix systems */ + +/*! \defgroup NIFTI1_DATATYPES + \brief nifti1 datatype codes + @{ + */ + /*--- the original ANALYZE 7.5 type codes ---*/ +#define DT_NONE 0 +#define DT_UNKNOWN 0 /* what it says, dude */ +#define DT_BINARY 1 /* binary (1 bit/voxel) */ +#define DT_UNSIGNED_CHAR 2 /* unsigned char (8 bits/voxel) */ +#define DT_SIGNED_SHORT 4 /* signed short (16 bits/voxel) */ +#define DT_SIGNED_INT 8 /* signed int (32 bits/voxel) */ +#define DT_FLOAT 16 /* float (32 bits/voxel) */ +#define DT_COMPLEX 32 /* complex (64 bits/voxel) */ +#define DT_DOUBLE 64 /* double (64 bits/voxel) */ +#define DT_RGB 128 /* RGB triple (24 bits/voxel) */ +#define DT_ALL 255 /* not very useful (?) */ + + /*----- another set of names for the same ---*/ +#define DT_UINT8 2 +#define DT_INT16 4 +#define DT_INT32 8 +#define DT_FLOAT32 16 +#define DT_COMPLEX64 32 +#define DT_FLOAT64 64 +#define DT_RGB24 128 + + /*------------------- new codes for NIFTI ---*/ +#define DT_INT8 256 /* signed char (8 bits) */ +#define DT_UINT16 512 /* unsigned short (16 bits) */ +#define DT_UINT32 768 /* unsigned int (32 bits) */ +#define DT_INT64 1024 /* long long (64 bits) */ +#define DT_UINT64 1280 /* unsigned long long (64 bits) */ +#define DT_FLOAT128 1536 /* long double (128 bits) */ +#define DT_COMPLEX128 1792 /* double pair (128 bits) */ +#define DT_COMPLEX256 2048 /* long double pair (256 bits) */ +#define DT_RGBA32 2304 /* 4 byte RGBA (32 bits/voxel) */ +/* @} */ + + + /*------- aliases for all the above codes ---*/ + +/*! \defgroup NIFTI1_DATATYPE_ALIASES + \brief aliases for the nifti1 datatype codes + @{ + */ + /*! unsigned char. */ +#define NIFTI_TYPE_UINT8 2 + /*! signed short. */ +#define NIFTI_TYPE_INT16 4 + /*! signed int. */ +#define NIFTI_TYPE_INT32 8 + /*! 32 bit float. */ +#define NIFTI_TYPE_FLOAT32 16 + /*! 64 bit complex = 2 32 bit floats. */ +#define NIFTI_TYPE_COMPLEX64 32 + /*! 64 bit float = double. */ +#define NIFTI_TYPE_FLOAT64 64 + /*! 3 8 bit bytes. */ +#define NIFTI_TYPE_RGB24 128 + /*! signed char. */ +#define NIFTI_TYPE_INT8 256 + /*! unsigned short. */ +#define NIFTI_TYPE_UINT16 512 + /*! unsigned int. */ +#define NIFTI_TYPE_UINT32 768 + /*! signed long long. */ +#define NIFTI_TYPE_INT64 1024 + /*! unsigned long long. */ +#define NIFTI_TYPE_UINT64 1280 + /*! 128 bit float = long double. */ +#define NIFTI_TYPE_FLOAT128 1536 + /*! 128 bit complex = 2 64 bit floats. */ +#define NIFTI_TYPE_COMPLEX128 1792 + /*! 256 bit complex = 2 128 bit floats */ +#define NIFTI_TYPE_COMPLEX256 2048 + /*! 4 8 bit bytes. */ +#define NIFTI_TYPE_RGBA32 2304 +/* @} */ + + /*-------- sample typedefs for complicated types ---*/ +#if 0 +typedef struct { float r,i; } complex_float ; +typedef struct { double r,i; } complex_double ; +typedef struct { long double r,i; } complex_longdouble ; +typedef struct { unsigned char r,g,b; } rgb_byte ; +#endif + +/*---------------------------------------------------------------------------*/ +/* INTERPRETATION OF VOXEL DATA: + ---------------------------- + The intent_code field can be used to indicate that the voxel data has + some particular meaning. In particular, a large number of codes is + given to indicate that the the voxel data should be interpreted as + being drawn from a given probability distribution. + + VECTOR-VALUED DATASETS: + ---------------------- + The 5th dimension of the dataset, if present (i.e., dim[0]=5 and + dim[5] > 1), contains multiple values (e.g., a vector) to be stored + at each spatiotemporal location. For example, the header values + - dim[0] = 5 + - dim[1] = 64 + - dim[2] = 64 + - dim[3] = 20 + - dim[4] = 1 (indicates no time axis) + - dim[5] = 3 + - datatype = DT_FLOAT + - intent_code = NIFTI_INTENT_VECTOR + mean that this dataset should be interpreted as a 3D volume (64x64x20), + with a 3-vector of floats defined at each point in the 3D grid. + + A program reading a dataset with a 5th dimension may want to reformat + the image data to store each voxels' set of values together in a struct + or array. This programming detail, however, is beyond the scope of the + NIFTI-1 file specification! Uses of dimensions 6 and 7 are also not + specified here. + + STATISTICAL PARAMETRIC DATASETS (i.e., SPMs): + -------------------------------------------- + Values of intent_code from NIFTI_FIRST_STATCODE to NIFTI_LAST_STATCODE + (inclusive) indicate that the numbers in the dataset should be interpreted + as being drawn from a given distribution. Most such distributions have + auxiliary parameters (e.g., NIFTI_INTENT_TTEST has 1 DOF parameter). + + If the dataset DOES NOT have a 5th dimension, then the auxiliary parameters + are the same for each voxel, and are given in header fields intent_p1, + intent_p2, and intent_p3. + + If the dataset DOES have a 5th dimension, then the auxiliary parameters + are different for each voxel. For example, the header values + - dim[0] = 5 + - dim[1] = 128 + - dim[2] = 128 + - dim[3] = 1 (indicates a single slice) + - dim[4] = 1 (indicates no time axis) + - dim[5] = 2 + - datatype = DT_FLOAT + - intent_code = NIFTI_INTENT_TTEST + mean that this is a 2D dataset (128x128) of t-statistics, with the + t-statistic being in the first "plane" of data and the degrees-of-freedom + parameter being in the second "plane" of data. + + If the dataset 5th dimension is used to store the voxel-wise statistical + parameters, then dim[5] must be 1 plus the number of parameters required + by that distribution (e.g., intent_code=NIFTI_INTENT_TTEST implies dim[5] + must be 2, as in the example just above). + + Note: intent_code values 2..10 are compatible with AFNI 1.5x (which is + why there is no code with value=1, which is obsolescent in AFNI). + + OTHER INTENTIONS: + ---------------- + The purpose of the intent_* fields is to help interpret the values + stored in the dataset. Some non-statistical values for intent_code + and conventions are provided for storing other complex data types. + + The intent_name field provides space for a 15 character (plus 0 byte) + 'name' string for the type of data stored. Examples: + - intent_code = NIFTI_INTENT_ESTIMATE; intent_name = "T1"; + could be used to signify that the voxel values are estimates of the + NMR parameter T1. + - intent_code = NIFTI_INTENT_TTEST; intent_name = "House"; + could be used to signify that the voxel values are t-statistics + for the significance of 'activation' response to a House stimulus. + - intent_code = NIFTI_INTENT_DISPVECT; intent_name = "ToMNI152"; + could be used to signify that the voxel values are a displacement + vector that transforms each voxel (x,y,z) location to the + corresponding location in the MNI152 standard brain. + - intent_code = NIFTI_INTENT_SYMMATRIX; intent_name = "DTI"; + could be used to signify that the voxel values comprise a diffusion + tensor image. + + If no data name is implied or needed, intent_name[0] should be set to 0. +-----------------------------------------------------------------------------*/ + + /*! default: no intention is indicated in the header. */ + +#define NIFTI_INTENT_NONE 0 + + /*-------- These codes are for probability distributions ---------------*/ + /* Most distributions have a number of parameters, + below denoted by p1, p2, and p3, and stored in + - intent_p1, intent_p2, intent_p3 if dataset doesn't have 5th dimension + - image data array if dataset does have 5th dimension + + Functions to compute with many of the distributions below can be found + in the CDF library from U Texas. + + Formulas for and discussions of these distributions can be found in the + following books: + + [U] Univariate Discrete Distributions, + NL Johnson, S Kotz, AW Kemp. + + [C1] Continuous Univariate Distributions, vol. 1, + NL Johnson, S Kotz, N Balakrishnan. + + [C2] Continuous Univariate Distributions, vol. 2, + NL Johnson, S Kotz, N Balakrishnan. */ + /*----------------------------------------------------------------------*/ + + /*! [C2, chap 32] Correlation coefficient R (1 param): + p1 = degrees of freedom + R/sqrt(1-R*R) is t-distributed with p1 DOF. */ + +/*! \defgroup NIFTI1_INTENT_CODES + \brief nifti1 intent codes, to describe intended meaning of dataset contents + @{ + */ +#define NIFTI_INTENT_CORREL 2 + + /*! [C2, chap 28] Student t statistic (1 param): p1 = DOF. */ + +#define NIFTI_INTENT_TTEST 3 + + /*! [C2, chap 27] Fisher F statistic (2 params): + p1 = numerator DOF, p2 = denominator DOF. */ + +#define NIFTI_INTENT_FTEST 4 + + /*! [C1, chap 13] Standard normal (0 params): Density = N(0,1). */ + +#define NIFTI_INTENT_ZSCORE 5 + + /*! [C1, chap 18] Chi-squared (1 param): p1 = DOF. + Density(x) proportional to exp(-x/2) * x^(p1/2-1). */ + +#define NIFTI_INTENT_CHISQ 6 + + /*! [C2, chap 25] Beta distribution (2 params): p1=a, p2=b. + Density(x) proportional to x^(a-1) * (1-x)^(b-1). */ + +#define NIFTI_INTENT_BETA 7 + + /*! [U, chap 3] Binomial distribution (2 params): + p1 = number of trials, p2 = probability per trial. + Prob(x) = (p1 choose x) * p2^x * (1-p2)^(p1-x), for x=0,1,...,p1. */ + +#define NIFTI_INTENT_BINOM 8 + + /*! [C1, chap 17] Gamma distribution (2 params): + p1 = shape, p2 = scale. + Density(x) proportional to x^(p1-1) * exp(-p2*x). */ + +#define NIFTI_INTENT_GAMMA 9 + + /*! [U, chap 4] Poisson distribution (1 param): p1 = mean. + Prob(x) = exp(-p1) * p1^x / x! , for x=0,1,2,.... */ + +#define NIFTI_INTENT_POISSON 10 + + /*! [C1, chap 13] Normal distribution (2 params): + p1 = mean, p2 = standard deviation. */ + +#define NIFTI_INTENT_NORMAL 11 + + /*! [C2, chap 30] Noncentral F statistic (3 params): + p1 = numerator DOF, p2 = denominator DOF, + p3 = numerator noncentrality parameter. */ + +#define NIFTI_INTENT_FTEST_NONC 12 + + /*! [C2, chap 29] Noncentral chi-squared statistic (2 params): + p1 = DOF, p2 = noncentrality parameter. */ + +#define NIFTI_INTENT_CHISQ_NONC 13 + + /*! [C2, chap 23] Logistic distribution (2 params): + p1 = location, p2 = scale. + Density(x) proportional to sech^2((x-p1)/(2*p2)). */ + +#define NIFTI_INTENT_LOGISTIC 14 + + /*! [C2, chap 24] Laplace distribution (2 params): + p1 = location, p2 = scale. + Density(x) proportional to exp(-abs(x-p1)/p2). */ + +#define NIFTI_INTENT_LAPLACE 15 + + /*! [C2, chap 26] Uniform distribution: p1 = lower end, p2 = upper end. */ + +#define NIFTI_INTENT_UNIFORM 16 + + /*! [C2, chap 31] Noncentral t statistic (2 params): + p1 = DOF, p2 = noncentrality parameter. */ + +#define NIFTI_INTENT_TTEST_NONC 17 + + /*! [C1, chap 21] Weibull distribution (3 params): + p1 = location, p2 = scale, p3 = power. + Density(x) proportional to + ((x-p1)/p2)^(p3-1) * exp(-((x-p1)/p2)^p3) for x > p1. */ + +#define NIFTI_INTENT_WEIBULL 18 + + /*! [C1, chap 18] Chi distribution (1 param): p1 = DOF. + Density(x) proportional to x^(p1-1) * exp(-x^2/2) for x > 0. + p1 = 1 = 'half normal' distribution + p1 = 2 = Rayleigh distribution + p1 = 3 = Maxwell-Boltzmann distribution. */ + +#define NIFTI_INTENT_CHI 19 + + /*! [C1, chap 15] Inverse Gaussian (2 params): + p1 = mu, p2 = lambda + Density(x) proportional to + exp(-p2*(x-p1)^2/(2*p1^2*x)) / x^3 for x > 0. */ + +#define NIFTI_INTENT_INVGAUSS 20 + + /*! [C2, chap 22] Extreme value type I (2 params): + p1 = location, p2 = scale + cdf(x) = exp(-exp(-(x-p1)/p2)). */ + +#define NIFTI_INTENT_EXTVAL 21 + + /*! Data is a 'p-value' (no params). */ + +#define NIFTI_INTENT_PVAL 22 + + /*! Data is ln(p-value) (no params). + To be safe, a program should compute p = exp(-abs(this_value)). + The nifti_stats.c library returns this_value + as positive, so that this_value = -log(p). */ + + +#define NIFTI_INTENT_LOGPVAL 23 + + /*! Data is log10(p-value) (no params). + To be safe, a program should compute p = pow(10.,-abs(this_value)). + The nifti_stats.c library returns this_value + as positive, so that this_value = -log10(p). */ + +#define NIFTI_INTENT_LOG10PVAL 24 + + /*! Smallest intent_code that indicates a statistic. */ + +#define NIFTI_FIRST_STATCODE 2 + + /*! Largest intent_code that indicates a statistic. */ + +#define NIFTI_LAST_STATCODE 24 + + /*---------- these values for intent_code aren't for statistics ----------*/ + + /*! To signify that the value at each voxel is an estimate + of some parameter, set intent_code = NIFTI_INTENT_ESTIMATE. + The name of the parameter may be stored in intent_name. */ + +#define NIFTI_INTENT_ESTIMATE 1001 + + /*! To signify that the value at each voxel is an index into + some set of labels, set intent_code = NIFTI_INTENT_LABEL. + The filename with the labels may stored in aux_file. */ + +#define NIFTI_INTENT_LABEL 1002 + + /*! To signify that the value at each voxel is an index into the + NeuroNames labels set, set intent_code = NIFTI_INTENT_NEURONAME. */ + +#define NIFTI_INTENT_NEURONAME 1003 + + /*! To store an M x N matrix at each voxel: + - dataset must have a 5th dimension (dim[0]=5 and dim[5]>1) + - intent_code must be NIFTI_INTENT_GENMATRIX + - dim[5] must be M*N + - intent_p1 must be M (in float format) + - intent_p2 must be N (ditto) + - the matrix values A[i][[j] are stored in row-order: + - A[0][0] A[0][1] ... A[0][N-1] + - A[1][0] A[1][1] ... A[1][N-1] + - etc., until + - A[M-1][0] A[M-1][1] ... A[M-1][N-1] */ + +#define NIFTI_INTENT_GENMATRIX 1004 + + /*! To store an NxN symmetric matrix at each voxel: + - dataset must have a 5th dimension + - intent_code must be NIFTI_INTENT_SYMMATRIX + - dim[5] must be N*(N+1)/2 + - intent_p1 must be N (in float format) + - the matrix values A[i][[j] are stored in row-order: + - A[0][0] + - A[1][0] A[1][1] + - A[2][0] A[2][1] A[2][2] + - etc.: row-by-row */ + +#define NIFTI_INTENT_SYMMATRIX 1005 + + /*! To signify that the vector value at each voxel is to be taken + as a displacement field or vector: + - dataset must have a 5th dimension + - intent_code must be NIFTI_INTENT_DISPVECT + - dim[5] must be the dimensionality of the displacment + vector (e.g., 3 for spatial displacement, 2 for in-plane) */ + +#define NIFTI_INTENT_DISPVECT 1006 /* specifically for displacements */ +#define NIFTI_INTENT_VECTOR 1007 /* for any other type of vector */ + + /*! To signify that the vector value at each voxel is really a + spatial coordinate (e.g., the vertices or nodes of a surface mesh): + - dataset must have a 5th dimension + - intent_code must be NIFTI_INTENT_POINTSET + - dim[0] = 5 + - dim[1] = number of points + - dim[2] = dim[3] = dim[4] = 1 + - dim[5] must be the dimensionality of space (e.g., 3 => 3D space). + - intent_name may describe the object these points come from + (e.g., "pial", "gray/white" , "EEG", "MEG"). */ + +#define NIFTI_INTENT_POINTSET 1008 + + /*! To signify that the vector value at each voxel is really a triple + of indexes (e.g., forming a triangle) from a pointset dataset: + - dataset must have a 5th dimension + - intent_code must be NIFTI_INTENT_TRIANGLE + - dim[0] = 5 + - dim[1] = number of triangles + - dim[2] = dim[3] = dim[4] = 1 + - dim[5] = 3 + - datatype should be an integer type (preferably DT_INT32) + - the data values are indexes (0,1,...) into a pointset dataset. */ + +#define NIFTI_INTENT_TRIANGLE 1009 + + /*! To signify that the vector value at each voxel is a quaternion: + - dataset must have a 5th dimension + - intent_code must be NIFTI_INTENT_QUATERNION + - dim[0] = 5 + - dim[5] = 4 + - datatype should be a floating point type */ + +#define NIFTI_INTENT_QUATERNION 1010 + + /*! Dimensionless value - no params - although, as in _ESTIMATE + the name of the parameter may be stored in intent_name. */ + +#define NIFTI_INTENT_DIMLESS 1011 + + /*---------- these values apply to GIFTI datasets ----------*/ + + /*! To signify that the value at each location is from a time series. */ + +#define NIFTI_INTENT_TIME_SERIES 2001 + + /*! To signify that the value at each location is a node index, from + a complete surface dataset. */ + +#define NIFTI_INTENT_NODE_INDEX 2002 + + /*! To signify that the vector value at each location is an RGB triplet, + of whatever type. + - dataset must have a 5th dimension + - dim[0] = 5 + - dim[1] = number of nodes + - dim[2] = dim[3] = dim[4] = 1 + - dim[5] = 3 + */ + +#define NIFTI_INTENT_RGB_VECTOR 2003 + + /*! To signify that the vector value at each location is a 4 valued RGBA + vector, of whatever type. + - dataset must have a 5th dimension + - dim[0] = 5 + - dim[1] = number of nodes + - dim[2] = dim[3] = dim[4] = 1 + - dim[5] = 4 + */ + +#define NIFTI_INTENT_RGBA_VECTOR 2004 + + /*! To signify that the value at each location is a shape value, such + as the curvature. */ + +#define NIFTI_INTENT_SHAPE 2005 + + /*! The following intent codes have been used by FSL FNIRT for + displacement/coefficient files. + + These codes are included to prevent clashes in community-created + extensions to NIfTI. Encoding and decoding behavior for these + intents is not specified by the standard, and support is OPTIONAL + for conforming implementations. + */ + +#define NIFTI_INTENT_FSL_FNIRT_DISPLACEMENT_FIELD 2006 +#define NIFTI_INTENT_FSL_CUBIC_SPLINE_COEFFICIENTS 2007 +#define NIFTI_INTENT_FSL_DCT_COEFFICIENTS 2008 +#define NIFTI_INTENT_FSL_QUADRATIC_SPLINE_COEFFICIENTS 2009 + + /*! The following intent codes have been used by FSL TOPUP for + displacement/coefficient files. + + These codes are included to prevent clashes in community-created + extensions to NIfTI. Encoding and decoding behavior for these + intents is not specified by the standard, and support is OPTIONAL + for conforming implementations. + */ + +#define NIFTI_INTENT_FSL_TOPUP_CUBIC_SPLINE_COEFFICIENTS 2016 +#define NIFTI_INTENT_FSL_TOPUP_QUADRATIC_SPLINE_COEFFICIENTS 2017 +#define NIFTI_INTENT_FSL_TOPUP_FIELD 2018 + +/* @} */ + +/*---------------------------------------------------------------------------*/ +/* 3D IMAGE (VOLUME) ORIENTATION AND LOCATION IN SPACE: + --------------------------------------------------- + There are 3 different methods by which continuous coordinates can + attached to voxels. The discussion below emphasizes 3D volumes, and + the continuous coordinates are referred to as (x,y,z). The voxel + index coordinates (i.e., the array indexes) are referred to as (i,j,k), + with valid ranges: + i = 0 .. dim[1]-1 + j = 0 .. dim[2]-1 (if dim[0] >= 2) + k = 0 .. dim[3]-1 (if dim[0] >= 3) + The (x,y,z) coordinates refer to the CENTER of a voxel. In methods + 2 and 3, the (x,y,z) axes refer to a subject-based coordinate system, + with + +x = Right +y = Anterior +z = Superior. + This is a right-handed coordinate system. However, the exact direction + these axes point with respect to the subject depends on qform_code + (Method 2) and sform_code (Method 3). + + N.B.: The i index varies most rapidly, j index next, k index slowest. + Thus, voxel (i,j,k) is stored starting at location + (i + j*dim[1] + k*dim[1]*dim[2]) * (bitpix/8) + into the dataset array. + + N.B.: The ANALYZE 7.5 coordinate system is + +x = Left +y = Anterior +z = Superior + which is a left-handed coordinate system. This backwardness is + too difficult to tolerate, so this NIFTI-1 standard specifies the + coordinate order which is most common in functional neuroimaging. + + N.B.: The 3 methods below all give the locations of the voxel centers + in the (x,y,z) coordinate system. In many cases, programs will wish + to display image data on some other grid. In such a case, the program + will need to convert its desired (x,y,z) values into (i,j,k) values + in order to extract (or interpolate) the image data. This operation + would be done with the inverse transformation to those described below. + + N.B.: Method 2 uses a factor 'qfac' which is either -1 or 1; qfac is + stored in the otherwise unused pixdim[0]. If pixdim[0]=0.0 (which + should not occur), we take qfac=1. Of course, pixdim[0] is only used + when reading a NIFTI-1 header, not when reading an ANALYZE 7.5 header. + + N.B.: The units of (x,y,z) can be specified using the xyzt_units field. + + METHOD 1 (the "old" way, used only when qform_code = 0): + ------------------------------------------------------- + The coordinate mapping from (i,j,k) to (x,y,z) is the ANALYZE + 7.5 way. This is a simple scaling relationship: + + x = pixdim[1] * i + y = pixdim[2] * j + z = pixdim[3] * k + + No particular spatial orientation is attached to these (x,y,z) + coordinates. (NIFTI-1 does not have the ANALYZE 7.5 orient field, + which is not general and is often not set properly.) This method + is not recommended, and is present mainly for compatibility with + ANALYZE 7.5 files. + + METHOD 2 (used when qform_code > 0, which should be the "normal" case): + --------------------------------------------------------------------- + The (x,y,z) coordinates are given by the pixdim[] scales, a rotation + matrix, and a shift. This method is intended to represent + "scanner-anatomical" coordinates, which are often embedded in the + image header (e.g., DICOM fields (0020,0032), (0020,0037), (0028,0030), + and (0018,0050)), and represent the nominal orientation and location of + the data. This method can also be used to represent "aligned" + coordinates, which would typically result from some post-acquisition + alignment of the volume to a standard orientation (e.g., the same + subject on another day, or a rigid rotation to true anatomical + orientation from the tilted position of the subject in the scanner). + The formula for (x,y,z) in terms of header parameters and (i,j,k) is: + + [ x ] [ R11 R12 R13 ] [ pixdim[1] * i ] [ qoffset_x ] + [ y ] = [ R21 R22 R23 ] [ pixdim[2] * j ] + [ qoffset_y ] + [ z ] [ R31 R32 R33 ] [ qfac * pixdim[3] * k ] [ qoffset_z ] + + The qoffset_* shifts are in the NIFTI-1 header. Note that the center + of the (i,j,k)=(0,0,0) voxel (first value in the dataset array) is + just (x,y,z)=(qoffset_x,qoffset_y,qoffset_z). + + The rotation matrix R is calculated from the quatern_* parameters. + This calculation is described below. + + The scaling factor qfac is either 1 or -1. The rotation matrix R + defined by the quaternion parameters is "proper" (has determinant 1). + This may not fit the needs of the data; for example, if the image + grid is + i increases from Left-to-Right + j increases from Anterior-to-Posterior + k increases from Inferior-to-Superior + Then (i,j,k) is a left-handed triple. In this example, if qfac=1, + the R matrix would have to be + + [ 1 0 0 ] + [ 0 -1 0 ] which is "improper" (determinant = -1). + [ 0 0 1 ] + + If we set qfac=-1, then the R matrix would be + + [ 1 0 0 ] + [ 0 -1 0 ] which is proper. + [ 0 0 -1 ] + + This R matrix is represented by quaternion [a,b,c,d] = [0,1,0,0] + (which encodes a 180 degree rotation about the x-axis). + + METHOD 3 (used when sform_code > 0): + ----------------------------------- + The (x,y,z) coordinates are given by a general affine transformation + of the (i,j,k) indexes: + + x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3] + y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3] + z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3] + + The srow_* vectors are in the NIFTI_1 header. Note that no use is + made of pixdim[] in this method. + + WHY 3 METHODS? + -------------- + Method 1 is provided only for backwards compatibility. The intention + is that Method 2 (qform_code > 0) represents the nominal voxel locations + as reported by the scanner, or as rotated to some fiducial orientation and + location. Method 3, if present (sform_code > 0), is to be used to give + the location of the voxels in some standard space. The sform_code + indicates which standard space is present. Both methods 2 and 3 can be + present, and be useful in different contexts (method 2 for displaying the + data on its original grid; method 3 for displaying it on a standard grid). + + In this scheme, a dataset would originally be set up so that the + Method 2 coordinates represent what the scanner reported. Later, + a registration to some standard space can be computed and inserted + in the header. Image display software can use either transform, + depending on its purposes and needs. + + In Method 2, the origin of coordinates would generally be whatever + the scanner origin is; for example, in MRI, (0,0,0) is the center + of the gradient coil. + + In Method 3, the origin of coordinates would depend on the value + of sform_code; for example, for the Talairach coordinate system, + (0,0,0) corresponds to the Anterior Commissure. + + QUATERNION REPRESENTATION OF ROTATION MATRIX (METHOD 2) + ------------------------------------------------------- + The orientation of the (x,y,z) axes relative to the (i,j,k) axes + in 3D space is specified using a unit quaternion [a,b,c,d], where + a*a+b*b+c*c+d*d=1. The (b,c,d) values are all that is needed, since + we require that a = sqrt(1.0-(b*b+c*c+d*d)) be nonnegative. The (b,c,d) + values are stored in the (quatern_b,quatern_c,quatern_d) fields. + + The quaternion representation is chosen for its compactness in + representing rotations. The (proper) 3x3 rotation matrix that + corresponds to [a,b,c,d] is + + [ a*a+b*b-c*c-d*d 2*b*c-2*a*d 2*b*d+2*a*c ] + R = [ 2*b*c+2*a*d a*a+c*c-b*b-d*d 2*c*d-2*a*b ] + [ 2*b*d-2*a*c 2*c*d+2*a*b a*a+d*d-c*c-b*b ] + + [ R11 R12 R13 ] + = [ R21 R22 R23 ] + [ R31 R32 R33 ] + + If (p,q,r) is a unit 3-vector, then rotation of angle h about that + direction is represented by the quaternion + + [a,b,c,d] = [cos(h/2), p*sin(h/2), q*sin(h/2), r*sin(h/2)]. + + Requiring a >= 0 is equivalent to requiring -Pi <= h <= Pi. (Note that + [-a,-b,-c,-d] represents the same rotation as [a,b,c,d]; there are 2 + quaternions that can be used to represent a given rotation matrix R.) + To rotate a 3-vector (x,y,z) using quaternions, we compute the + quaternion product + + [0,x',y',z'] = [a,b,c,d] * [0,x,y,z] * [a,-b,-c,-d] + + which is equivalent to the matrix-vector multiply + + [ x' ] [ x ] + [ y' ] = R [ y ] (equivalence depends on a*a+b*b+c*c+d*d=1) + [ z' ] [ z ] + + Multiplication of 2 quaternions is defined by the following: + + [a,b,c,d] = a*1 + b*I + c*J + d*K + where + I*I = J*J = K*K = -1 (I,J,K are square roots of -1) + I*J = K J*K = I K*I = J + J*I = -K K*J = -I I*K = -J (not commutative!) + For example + [a,b,0,0] * [0,0,0,1] = [0,0,-b,a] + since this expands to + (a+b*I)*(K) = (a*K+b*I*K) = (a*K-b*J). + + The above formula shows how to go from quaternion (b,c,d) to + rotation matrix and direction cosines. Conversely, given R, + we can compute the fields for the NIFTI-1 header by + + a = 0.5 * sqrt(1+R11+R22+R33) (not stored) + b = 0.25 * (R32-R23) / a => quatern_b + c = 0.25 * (R13-R31) / a => quatern_c + d = 0.25 * (R21-R12) / a => quatern_d + + If a=0 (a 180 degree rotation), alternative formulas are needed. + See the nifti1_io.c function mat44_to_quatern() for an implementation + of the various cases in converting R to [a,b,c,d]. + + Note that R-transpose (= R-inverse) would lead to the quaternion + [a,-b,-c,-d]. + + The choice to specify the qoffset_x (etc.) values in the final + coordinate system is partly to make it easy to convert DICOM images to + this format. The DICOM attribute "Image Position (Patient)" (0020,0032) + stores the (Xd,Yd,Zd) coordinates of the center of the first voxel. + Here, (Xd,Yd,Zd) refer to DICOM coordinates, and Xd=-x, Yd=-y, Zd=z, + where (x,y,z) refers to the NIFTI coordinate system discussed above. + (i.e., DICOM +Xd is Left, +Yd is Posterior, +Zd is Superior, + whereas +x is Right, +y is Anterior , +z is Superior. ) + Thus, if the (0020,0032) DICOM attribute is extracted into (px,py,pz), then + qoffset_x = -px qoffset_y = -py qoffset_z = pz + is a reasonable setting when qform_code=NIFTI_XFORM_SCANNER_ANAT. + + That is, DICOM's coordinate system is 180 degrees rotated about the z-axis + from the neuroscience/NIFTI coordinate system. To transform between DICOM + and NIFTI, you just have to negate the x- and y-coordinates. + + The DICOM attribute (0020,0037) "Image Orientation (Patient)" gives the + orientation of the x- and y-axes of the image data in terms of 2 3-vectors. + The first vector is a unit vector along the x-axis, and the second is + along the y-axis. If the (0020,0037) attribute is extracted into the + value (xa,xb,xc,ya,yb,yc), then the first two columns of the R matrix + would be + [ -xa -ya ] + [ -xb -yb ] + [ xc yc ] + The negations are because DICOM's x- and y-axes are reversed relative + to NIFTI's. The third column of the R matrix gives the direction of + displacement (relative to the subject) along the slice-wise direction. + This orientation is not encoded in the DICOM standard in a simple way; + DICOM is mostly concerned with 2D images. The third column of R will be + either the cross-product of the first 2 columns or its negative. It is + possible to infer the sign of the 3rd column by examining the coordinates + in DICOM attribute (0020,0032) "Image Position (Patient)" for successive + slices. However, this method occasionally fails for reasons that I + (RW Cox) do not understand. +-----------------------------------------------------------------------------*/ + + /* [qs]form_code value: */ /* x,y,z coordinate system refers to: */ + /*-----------------------*/ /*---------------------------------------*/ + +/*! \defgroup NIFTI1_XFORM_CODES + \brief nifti1 xform codes to describe the "standard" coordinate system + @{ + */ + /*! Arbitrary coordinates (Method 1). */ + +#define NIFTI_XFORM_UNKNOWN 0 + + /*! Scanner-based anatomical coordinates */ + +#define NIFTI_XFORM_SCANNER_ANAT 1 + + /*! Coordinates aligned to another file's, + or to anatomical "truth". */ + +#define NIFTI_XFORM_ALIGNED_ANAT 2 + + /*! Coordinates aligned to Talairach- + Tournoux Atlas; (0,0,0)=AC, etc. */ + +#define NIFTI_XFORM_TALAIRACH 3 + + /*! MNI 152 normalized coordinates. */ + +#define NIFTI_XFORM_MNI_152 4 + + /*! Normalized coordinates (for + any general standard template + space). Added March 8, 2019. */ + +#define NIFTI_XFORM_TEMPLATE_OTHER 5 + +/* @} */ + +/*---------------------------------------------------------------------------*/ +/* UNITS OF SPATIAL AND TEMPORAL DIMENSIONS: + ---------------------------------------- + The codes below can be used in xyzt_units to indicate the units of pixdim. + As noted earlier, dimensions 1,2,3 are for x,y,z; dimension 4 is for + time (t). + - If dim[4]=1 or dim[0] < 4, there is no time axis. + - A single time series (no space) would be specified with + - dim[0] = 4 (for scalar data) or dim[0] = 5 (for vector data) + - dim[1] = dim[2] = dim[3] = 1 + - dim[4] = number of time points + - pixdim[4] = time step + - xyzt_units indicates units of pixdim[4] + - dim[5] = number of values stored at each time point + + Bits 0..2 of xyzt_units specify the units of pixdim[1..3] + (e.g., spatial units are values 1..7). + Bits 3..5 of xyzt_units specify the units of pixdim[4] + (e.g., temporal units are multiples of 8). + + This compression of 2 distinct concepts into 1 byte is due to the + limited space available in the 348 byte ANALYZE 7.5 header. The + macros XYZT_TO_SPACE and XYZT_TO_TIME can be used to mask off the + undesired bits from the xyzt_units fields, leaving "pure" space + and time codes. Inversely, the macro SPACE_TIME_TO_XYZT can be + used to assemble a space code (0,1,2,...,7) with a time code + (0,8,16,32,...,56) into the combined value for xyzt_units. + + Note that codes are provided to indicate the "time" axis units are + actually frequency in Hertz (_HZ), in part-per-million (_PPM) + or in radians-per-second (_RADS). + + The toffset field can be used to indicate a nonzero start point for + the time axis. That is, time point #m is at t=toffset+m*pixdim[4] + for m=0..dim[4]-1. +-----------------------------------------------------------------------------*/ + +/*! \defgroup NIFTI1_UNITS + \brief nifti1 units codes to describe the unit of measurement for + each dimension of the dataset + @{ + */ + /*! NIFTI code for unspecified units. */ +#define NIFTI_UNITS_UNKNOWN 0 + + /** Space codes are multiples of 1. **/ + /*! NIFTI code for meters. */ +#define NIFTI_UNITS_METER 1 + /*! NIFTI code for millimeters. */ +#define NIFTI_UNITS_MM 2 + /*! NIFTI code for micrometers. */ +#define NIFTI_UNITS_MICRON 3 + + /** Time codes are multiples of 8. **/ + /*! NIFTI code for seconds. */ +#define NIFTI_UNITS_SEC 8 + /*! NIFTI code for milliseconds. */ +#define NIFTI_UNITS_MSEC 16 + /*! NIFTI code for microseconds. */ +#define NIFTI_UNITS_USEC 24 + + /*** These units are for spectral data: ***/ + /*! NIFTI code for Hertz. */ +#define NIFTI_UNITS_HZ 32 + /*! NIFTI code for ppm. */ +#define NIFTI_UNITS_PPM 40 + /*! NIFTI code for radians per second. */ +#define NIFTI_UNITS_RADS 48 +/* @} */ + +#undef XYZT_TO_SPACE +#undef XYZT_TO_TIME +#define XYZT_TO_SPACE(xyzt) ( (xyzt) & 0x07 ) +#define XYZT_TO_TIME(xyzt) ( (xyzt) & 0x38 ) + +#undef SPACE_TIME_TO_XYZT +#define SPACE_TIME_TO_XYZT(ss,tt) ( (((char)(ss)) & 0x07) \ + | (((char)(tt)) & 0x38) ) + +/*---------------------------------------------------------------------------*/ +/* MRI-SPECIFIC SPATIAL AND TEMPORAL INFORMATION: + --------------------------------------------- + A few fields are provided to store some extra information + that is sometimes important when storing the image data + from an FMRI time series experiment. (After processing such + data into statistical images, these fields are not likely + to be useful.) + + { freq_dim } = These fields encode which spatial dimension (1,2, or 3) + { phase_dim } = corresponds to which acquisition dimension for MRI data. + { slice_dim } = + Examples: + Rectangular scan multi-slice EPI: + freq_dim = 1 phase_dim = 2 slice_dim = 3 (or some permutation) + Spiral scan multi-slice EPI: + freq_dim = phase_dim = 0 slice_dim = 3 + since the concepts of frequency- and phase-encoding directions + don't apply to spiral scan + + slice_duration = If this is positive, AND if slice_dim is nonzero, + indicates the amount of time used to acquire 1 slice. + slice_duration*dim[slice_dim] can be less than pixdim[4] + with a clustered acquisition method, for example. + + slice_code = If this is nonzero, AND if slice_dim is nonzero, AND + if slice_duration is positive, indicates the timing + pattern of the slice acquisition. The following codes + are defined: + NIFTI_SLICE_SEQ_INC == sequential increasing + NIFTI_SLICE_SEQ_DEC == sequential decreasing + NIFTI_SLICE_ALT_INC == alternating increasing + NIFTI_SLICE_ALT_DEC == alternating decreasing + NIFTI_SLICE_ALT_INC2 == alternating increasing #2 + NIFTI_SLICE_ALT_DEC2 == alternating decreasing #2 + { slice_start } = Indicates the start and end of the slice acquisition + { slice_end } = pattern, when slice_code is nonzero. These values + are present to allow for the possible addition of + "padded" slices at either end of the volume, which + don't fit into the slice timing pattern. If there + are no padding slices, then slice_start=0 and + slice_end=dim[slice_dim]-1 are the correct values. + For these values to be meaningful, slice_start must + be non-negative and slice_end must be greater than + slice_start. Otherwise, they should be ignored. + + The following table indicates the slice timing pattern, relative to + time=0 for the first slice acquired, for some sample cases. Here, + dim[slice_dim]=7 (there are 7 slices, labeled 0..6), slice_duration=0.1, + and slice_start=1, slice_end=5 (1 padded slice on each end). + + slice + index SEQ_INC SEQ_DEC ALT_INC ALT_DEC ALT_INC2 ALT_DEC2 + 6 : n/a n/a n/a n/a n/a n/a n/a = not applicable + 5 : 0.4 0.0 0.2 0.0 0.4 0.2 (slice time offset + 4 : 0.3 0.1 0.4 0.3 0.1 0.0 doesn't apply to + 3 : 0.2 0.2 0.1 0.1 0.3 0.3 slices outside + 2 : 0.1 0.3 0.3 0.4 0.0 0.1 the range + 1 : 0.0 0.4 0.0 0.2 0.2 0.4 slice_start .. + 0 : n/a n/a n/a n/a n/a n/a slice_end) + + The SEQ slice_codes are sequential ordering (uncommon but not unknown), + either increasing in slice number or decreasing (INC or DEC), as + illustrated above. + + The ALT slice codes are alternating ordering. The 'standard' way for + these to operate (without the '2' on the end) is for the slice timing + to start at the edge of the slice_start .. slice_end group (at slice_start + for INC and at slice_end for DEC). For the 'ALT_*2' slice_codes, the + slice timing instead starts at the first slice in from the edge (at + slice_start+1 for INC2 and at slice_end-1 for DEC2). This latter + acquisition scheme is found on some Siemens scanners. + + The fields freq_dim, phase_dim, slice_dim are all squished into the single + byte field dim_info (2 bits each, since the values for each field are + limited to the range 0..3). This unpleasantness is due to lack of space + in the 348 byte allowance. + + The macros DIM_INFO_TO_FREQ_DIM, DIM_INFO_TO_PHASE_DIM, and + DIM_INFO_TO_SLICE_DIM can be used to extract these values from the + dim_info byte. + + The macro FPS_INTO_DIM_INFO can be used to put these 3 values + into the dim_info byte. +-----------------------------------------------------------------------------*/ + +#undef DIM_INFO_TO_FREQ_DIM +#undef DIM_INFO_TO_PHASE_DIM +#undef DIM_INFO_TO_SLICE_DIM + +#define DIM_INFO_TO_FREQ_DIM(di) ( ((di) ) & 0x03 ) +#define DIM_INFO_TO_PHASE_DIM(di) ( ((di) >> 2) & 0x03 ) +#define DIM_INFO_TO_SLICE_DIM(di) ( ((di) >> 4) & 0x03 ) + +#undef FPS_INTO_DIM_INFO +#define FPS_INTO_DIM_INFO(fd,pd,sd) ( ( ( ((char)(fd)) & 0x03) ) | \ + ( ( ((char)(pd)) & 0x03) << 2 ) | \ + ( ( ((char)(sd)) & 0x03) << 4 ) ) + +/*! \defgroup NIFTI1_SLICE_ORDER + \brief nifti1 slice order codes, describing the acquisition order + of the slices + @{ + */ +#define NIFTI_SLICE_UNKNOWN 0 +#define NIFTI_SLICE_SEQ_INC 1 +#define NIFTI_SLICE_SEQ_DEC 2 +#define NIFTI_SLICE_ALT_INC 3 +#define NIFTI_SLICE_ALT_DEC 4 +#define NIFTI_SLICE_ALT_INC2 5 /* 05 May 2005: RWCox */ +#define NIFTI_SLICE_ALT_DEC2 6 /* 05 May 2005: RWCox */ +/* @} */ + +/*---------------------------------------------------------------------------*/ +/* UNUSED FIELDS: + ------------- + Some of the ANALYZE 7.5 fields marked as ++UNUSED++ may need to be set + to particular values for compatibility with other programs. The issue + of interoperability of ANALYZE 7.5 files is a murky one -- not all + programs require exactly the same set of fields. (Unobscuring this + murkiness is a principal motivation behind NIFTI-1.) + + Some of the fields that may need to be set for other (non-NIFTI aware) + software to be happy are: + + extents dbh.h says this should be 16384 + regular dbh.h says this should be the character 'r' + glmin, } dbh.h says these values should be the min and max voxel + glmax } values for the entire dataset + + It is best to initialize ALL fields in the NIFTI-1 header to 0 + (e.g., with calloc()), then fill in what is needed. +-----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*/ +/* MISCELLANEOUS C MACROS +-----------------------------------------------------------------------------*/ + +/*.................*/ +/*! Given a nifti_1_header struct, check if it has a good magic number. + Returns NIFTI version number (1..9) if magic is good, 0 if it is not. */ + +#define NIFTI_VERSION(h) \ + ( ( (h).magic[0]=='n' && (h).magic[3]=='\0' && \ + ( (h).magic[1]=='i' || (h).magic[1]=='+' ) && \ + ( (h).magic[2]>='1' && (h).magic[2]<='9' ) ) \ + ? (h).magic[2]-'0' : 0 ) + +/*.................*/ +/*! Check if a nifti_1_header struct says if the data is stored in the + same file or in a separate file. Returns 1 if the data is in the same + file as the header, 0 if it is not. */ + +#define NIFTI_ONEFILE(h) ( (h).magic[1] == '+' ) + +/*.................*/ +/*! Check if a nifti_1_header struct needs to be byte swapped. + Returns 1 if it needs to be swapped, 0 if it does not. */ + +#define NIFTI_NEEDS_SWAP(h) ( (h).dim[0] < 0 || (h).dim[0] > 7 ) + +/*.................*/ +/*! Check if a nifti_1_header struct contains a 5th (vector) dimension. + Returns size of 5th dimension if > 1, returns 0 otherwise. */ + +#define NIFTI_5TH_DIM(h) ( ((h).dim[0]>4 && (h).dim[5]>1) ? (h).dim[5] : 0 ) + +/*****************************************************************************/ + +#endif /* _NIFTI_HEADER_ */ diff --git a/cpp/3rd_party/rnifti/niftilib/nifti1_io.cpp b/cpp/3rd_party/rnifti/niftilib/nifti1_io.cpp new file mode 100644 index 0000000000..bd6821cfdb --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti1_io.cpp @@ -0,0 +1,7689 @@ +#define _NIFTI1_IO_C_ + +#include "niftilib/nifti1_io.h" /* typedefs, prototypes, macros, etc. */ + +/*****===================================================================*****/ +/***** Sample functions to deal with NIFTI-1 and ANALYZE files *****/ +/*****...................................................................*****/ +/***** This code is released to the public domain. *****/ +/*****...................................................................*****/ +/***** Author: Robert W Cox, SSCC/DIRP/NIMH/NIH/DHHS/USA/EARTH *****/ +/***** Date: August 2003 *****/ +/*****...................................................................*****/ +/***** Neither the National Institutes of Health (NIH), nor any of its *****/ +/***** employees imply any warranty of usefulness of this software for *****/ +/***** any purpose, and do not assume any liability for damages, *****/ +/***** incidental or otherwise, caused by any use of this document. *****/ +/*****===================================================================*****/ + +/** \file nifti1_io.c + \brief main collection of nifti1 i/o routines + - written by Bob Cox, SSCC NIMH + - revised by Mark Jenkinson, FMRIB + - revised by Rick Reynolds, SSCC, NIMH + - revised by Kate Fissell, University of Pittsburgh + + The library history can be viewed via "nifti_tool -nifti_hist". +
The library version can be viewed via "nifti_tool -nifti_ver". + */ + +/*! global history and version strings, for printing */ +static char const * const gni_history[] = +{ + "----------------------------------------------------------------------\n" + "history (of nifti library changes):\n" + "\n", + "0.0 August, 2003 [rwcox]\n" + " (Robert W Cox of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - initial version\n" + "\n", + "0.1 July/August, 2004 [Mark Jenkinson]\n" + " (FMRIB Centre, University of Oxford, UK)\n" + " - Mainly adding low-level IO and changing things to allow gzipped\n" + " files to be read and written\n" + " - Full backwards compatability should have been maintained\n" + "\n", + "0.2 16 Nov 2004 [rickr]\n" + " (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - included Mark's changes in the AFNI distribution (including znzlib/)\n" + " (HAVE_ZLIB is commented out for the standard distribution)\n" + " - modified nifti_validfilename() and nifti_makebasename()\n" + " - added nifti_find_file_extension()\n" + "\n", + "0.3 3 Dec 2004 [rickr]\n" + " - note: header extensions are not yet checked for\n" + " - added formatted history as global string, for printing\n" + " - added nifti_disp_lib_hist(), to display the nifti library history\n" + " - added nifti_disp_lib_version(), to display the nifti library history\n", + " - re-wrote nifti_findhdrname()\n" + " o used nifti_find_file_extension()\n" + " o changed order of file tests (default is .nii, depends on input)\n" + " o free hdrname on failure\n" + " - made similar changes to nifti_findimgname()\n" + " - check for NULL return from nifti_findhdrname() calls\n", + " - removed most of ERREX() macros\n" + " - modified nifti_image_read()\n" + " o added debug info and error checking (on gni_debug > 0, only)\n" + " o fail if workingname is NULL\n" + " o check for failure to open header file\n" + " o free workingname on failure\n" + " o check for failure of nifti_image_load()\n" + " o check for failure of nifti_convert_nhdr2nim()\n", + " - changed nifti_image_load() to int, and check nifti_read_buffer return\n" + " - changed nifti_read_buffer() to fail on short read, and to count float\n" + " fixes (to print on debug)\n" + " - changed nifti_image_infodump to print to stderr\n" + " - updated function header comments, or moved comments above header\n" + " - removed const keyword\n" + " - added LNI_FERR() macro for error reporting on input files\n" + "\n", + "0.4 10 Dec 2004 [rickr] - added header extensions\n" + " - in nifti1_io.h:\n" + " o added num_ext and ext_list to the definition of nifti_image\n" + " o made many functions static (more to follow)\n" + " o added LNI_MAX_NIA_EXT_LEN, for max nifti_type 3 extension length\n", + " - added __DATE__ to version output in nifti_disp_lib_version()\n" + " - added nifti_disp_matrix_orient() to print orientation information\n" + " - added '.nia' as a valid file extension in nifti_find_file_extension()\n" + " - added much more debug output\n" + " - in nifti_image_read(), in the case of an ASCII header, check for\n" + " extensions after the end of the header\n", + " - added nifti_read_extensions() function\n" + " - added nifti_read_next_extension() function\n" + " - added nifti_add_exten_to_list() function\n" + " - added nifti_check_extension() function\n" + " - added nifti_write_extensions() function\n" + " - added nifti_extension_size() function\n" + " - in nifti_set_iname_offest():\n" + " o adjust offset by the extension size and the extender size\n", + " o fixed the 'ceiling modulo 16' computation\n" + " - in nifti_image_write_hdr_img2(): \n" + " o added extension writing\n" + " o check for NULL return from nifti_findimgname()\n" + " - include number of extensions in nifti_image_to_ascii() output\n" + " - in nifti_image_from_ascii():\n" + " o return bytes_read as a parameter, computed from the final spos\n" + " o extract num_ext from ASCII header\n" + "\n", + "0.5 14 Dec 2004 [rickr] - added sub-brick reading functions\n" + " - added nifti_brick_list type to nifti1_io.h, along with new prototypes\n" + " - added main nifti_image_read_bricks() function, with description\n" + " - added nifti_image_load_bricks() - library function (requires nim)\n" + " - added valid_nifti_brick_list() - library function\n" + " - added free_NBL() - library function\n", + " - added update_nifti_image_for_brick_list() for dimension update\n" + " - added nifti_load_NBL_bricks(), nifti_alloc_NBL_mem(),\n" + " nifti_copynsort() and force_positive() (static functions)\n" + " - in nifti_image_read(), check for failed load only if read_data is set\n" + " - broke most of nifti_image_load() into nifti_image_load_prep()\n" + "\n", + "0.6 15 Dec 2004 [rickr] - added sub-brick writing functionality\n" + " - in nifti1_io.h, removed znzlib directory from include - all nifti\n" + " library files are now under the nifti directory\n" + " - nifti_read_extensions(): print no offset warning for nifti_type 3\n" + " - nifti_write_all_data():\n" + " o pass nifti_brick_list * NBL, for optional writing\n" + " o if NBL, write each sub-brick, sequentially\n", + " - nifti_set_iname_offset(): case 1 must have sizeof() cast to int\n" + " - pass NBL to nifti_image_write_hdr_img2(), and allow NBL or data\n" + " - added nifti_image_write_bricks() wrapper for ...write_hdr_img2()\n" + " - included compression abilities\n" + "\n", + "0.7 16 Dec 2004 [rickr] - minor changes to extension reading\n" + "\n", + "0.8 21 Dec 2004 [rickr] - restrict extension reading, and minor changes\n" + " - in nifti_image_read(), compute bytes for extensions (see remaining)\n" + " - in nifti_read_extensions(), pass 'remain' as space for extensions,\n" + " pass it to nifti_read_next_ext(), and update for each one read \n" + " - in nifti_check_extension(), require (size <= remain)\n", + " - in update_nifti_image_brick_list(), update nvox\n" + " - in nifti_image_load_bricks(), make explicit check for nbricks <= 0\n" + " - in int_force_positive(), check for (!list)\n" + " - in swap_nifti_header(), swap sizeof_hdr, and reorder to struct order\n" + " - change get_filesize functions to signed ( < 0 is no file or error )\n", + " - in nifti_validfilename(), lose redundant (len < 0) check\n" + " - make print_hex_vals() static\n" + " - in disp_nifti_1_header, restrict string field widths\n" + "\n", + "0.9 23 Dec 2004 [rickr] - minor changes\n" + " - broke ASCII header reading out of nifti_image_read(), into new\n" + " functions has_ascii_header() and read_ascii_image()\n", + " - check image_read failure and znzseek failure\n" + " - altered some debug output\n" + " - nifti_write_all_data() now returns an int\n" + "\n", + "0.10 29 Dec 2004 [rickr]\n" + " - renamed nifti_valid_extension() to nifti_check_extension()\n" + " - added functions nifti_makehdrname() and nifti_makeimgname()\n" + " - added function valid_nifti_extensions()\n" + " - in nifti_write_extensions(), check for validity before writing\n", + " - rewrote nifti_image_write_hdr_img2():\n" + " o set write_data and leave_open flags from write_opts\n" + " o add debug print statements\n" + " o use nifti_write_ascii_image() for the ascii case\n" + " o rewrote the logic of all cases to be easier to follow\n", + " - broke out code as nifti_write_ascii_image() function\n" + " - added debug to top-level write functions, and free the znzFile\n" + " - removed unused internal function nifti_image_open()\n" + "\n", + "0.11 30 Dec 2004 [rickr] - small mods\n" + " - moved static function prototypes from header to C file\n" + " - free extensions in nifti_image_free()\n" + "\n", + "1.0 07 Jan 2005 [rickr] - INITIAL RELEASE VERSION\n" + " - added function nifti_set_filenames()\n" + " - added function nifti_read_header()\n" + " - added static function nhdr_looks_good()\n" + " - added static function need_nhdr_swap()\n" + " - exported nifti_add_exten_to_list symbol\n", + " - fixed #bytes written in nifti_write_extensions()\n" + " - only modify offset if it is too small (nifti_set_iname_offset)\n" + " - added nifti_type 3 to nifti_makehdrname and nifti_makeimgname\n" + " - added function nifti_set_filenames()\n" + "\n", + "1.1 07 Jan 2005 [rickr]\n" + " - in nifti_read_header(), swap if needed\n" + "\n", + "1.2 07 Feb 2005 [kate fissell c/o rickr] \n" + " - nifti1.h: added doxygen comments for main struct and #define groups\n" + " - nifti1_io.h: added doxygen comments for file and nifti_image struct\n" + " - nifti1_io.h: added doxygen comments for file and some functions\n" + " - nifti1_io.c: changed nifti_copy_nim_info to use memcpy\n" + "\n", + "1.3 09 Feb 2005 [rickr]\n" + " - nifti1.h: added doxygen comments for extension structs\n" + " - nifti1_io.h: put most #defines in #ifdef _NIFTI1_IO_C_ block\n" + " - added a doxygen-style description to every exported function\n" + " - added doxygen-style comments within some functions\n" + " - re-exported many znzFile functions that I had made static\n" + " - re-added nifti_image_open (sorry, Mark)\n" + " - every exported function now has 'nifti' in the name (19 functions)\n", + " - made sure every alloc() has a failure test\n" + " - added nifti_copy_extensions function, for use in nifti_copy_nim_info\n" + " - nifti_is_gzfile: added initial strlen test\n" + " - nifti_set_filenames: added set_byte_order parameter option\n" + " (it seems appropriate to set the BO when new files are associated)\n" + " - disp_nifti_1_header: prints to stdout (a.o.t. stderr), with fflush\n" + "\n", + "1.4 23 Feb 2005 [rickr] - sourceforge merge\n" + " - merged into the nifti_io CVS directory structure at sourceforge.net\n" + " - merged in 4 changes by Mark, and re-added his const keywords\n" + " - cast some pointers to (void *) for -pedantic compile option\n" + " - added nifti_free_extensions()\n" + "\n", + "1.5 02 Mar 2005 [rickr] - started nifti global options\n" + " - gni_debug is now g_opts.debug\n" + " - added validity check parameter to nifti_read_header\n" + " - need_nhdr_swap no longer does test swaps on the stack\n" + "\n", + "1.6 05 April 2005 [rickr] - validation and collapsed_image_read\n" + " - added nifti_read_collapsed_image(), an interface for reading partial\n" + " datasets, specifying a subset of array indices\n" + " - for read_collapsed_image, added static functions: rci_read_data(),\n" + " rci_alloc_mem(), and make_pivot_list()\n", + " - added nifti_nim_is_valid() to check for consistency (more to do)\n" + " - added nifti_nim_has_valid_dims() to do many dimensions tests\n" + "\n", + "1.7 08 April 2005 [rickr]\n" + " - added nifti_update_dims_from_array() - to update dimensions\n" + " - modified nifti_makehdrname() and nifti_makeimgname():\n" + " if prefix has a valid extension, use it (else make one up)\n" + " - added nifti_get_intlist - for making an array of ints\n" + " - fixed init of NBL->bsize in nifti_alloc_NBL_mem() {thanks, Bob}\n" + "\n", + "1.8 14 April 2005 [rickr]\n" + " - added nifti_set_type_from_names(), for nifti_set_filenames()\n" + " (only updates type if number of files does not match it)\n" + " - added is_valid_nifti_type(), just to be sure\n" + " - updated description of nifti_read_collapsed_image() for *data change\n" + " (if *data is already set, assume memory exists for results)\n" + " - modified rci_alloc_mem() to allocate only if *data is NULL\n" + "\n", + "1.9 19 April 2005 [rickr]\n" + " - added extension codes NIFTI_ECODE_COMMENT and NIFTI_ECODE_XCEDE\n" + " - added nifti_type codes NIFTI_MAX_ECODE and NIFTI_MAX_FTYPE\n" + " - added nifti_add_extension() {exported}\n" + " - added nifti_fill_extension() as a static function\n" + " - added nifti_is_valid_ecode() {exported}\n", + " - nifti_type values are now NIFTI_FTYPE_* file codes\n" + " - in nifti_read_extensions(), decrement 'remain' by extender size, 4\n" + " - in nifti_set_iname_offset(), case 1, update if offset differs\n" + " - only output '-d writing nifti file' if debug > 1\n" + "\n", + "1.10 10 May 2005 [rickr]\n" + " - files are read using ZLIB only if they end in '.gz'\n" + "\n", + "1.11 12 August 2005 [kate fissell]\n" + " - Kate's 0.2 release packaging, for sourceforge\n" + "\n", + "1.12 17 August 2005 [rickr] - comment (doxygen) updates\n" + " - updated comments for most functions (2 updates from Cinly Ooi)\n" + " - added nifti_type_and_names_match()\n" + "\n", + "1.12a 24 August 2005 [rickr] - remove all tabs from Clibs/*/*.[ch]\n", + "1.12b 25 August 2005 [rickr] - changes by Hans Johnson\n", + "1.13 25 August 2005 [rickr]\n", + " - finished changes by Hans for Insight\n" + " - added const in all appropraite parameter locations (30-40)\n" + " (any pointer referencing data that will not change)\n" + " - shortened all string constants below 509 character limit\n" + "1.14 28 October 2005 [HJohnson]\n", + " - use nifti_set_filenames() in nifti_convert_nhdr2nim()\n" + "1.15 02 November 2005 [rickr]\n", + " - added skip_blank_ext to nifti_global_options\n" + " - added nifti_set_skip_blank_ext(), to set option\n" + " - if skip_blank_ext and no extensions, do not read/write extender\n" + "1.16 18 November 2005 [rickr]\n", + " - removed any test or access of dim[i], i>dim[0]\n" + " - do not set pixdim for collapsed dims to 1.0, leave them as they are\n" + " - added magic and dim[i] tests in nifti_hdr_looks_good()\n" + " - added 2 size_t casts\n" + "1.17 22 November 2005 [rickr]\n", + " - in hdr->nim, for i > dim[0], pass 0 or 1, else set to 1\n" + "1.18 02 March 2006 [rickr]\n", + " - in nifti_alloc_NBL_mem(), fixed nt=0 case from 1.17 change\n" + "1.19 23 May 2006 [HJohnson,rickr]\n", + " - nifti_write_ascii_image(): free(hstr)\n" + " - nifti_copy_extensions(): clear num_ext and ext_list\n" + "1.20 27 Jun 2006 [rickr]\n", + " - nifti_findhdrname(): fixed assign of efirst to match stated logic\n" + " (problem found by Atle Bjørnerud)\n" + "1.21 05 Sep 2006 [rickr] update for nifticlib-0.4 release\n", + " - was reminded to actually add nifti_set_skip_blank_ext()\n" + " - init g_opts.skip_blank_ext to 0\n" + "1.22 01 Jun 2007 nifticlib-0.5 release\n", + "1.23 05 Jun 2007 nifti_add_exten_to_list: revert on failure, free old list\n" + "1.24 07 Jun 2007 nifti_copy_extensions: use esize-8 for data size\n" + "1.25 12 Jun 2007 [rickr] EMPTY_IMAGE creation\n", + " - added nifti_make_new_header() - to create from dims/dtype\n" + " - added nifti_make_new_nim() - to create from dims/dtype/fill\n" + " - added nifti_is_valid_datatype(), and more debug info\n", + "1.26 27 Jul 2007 [rickr] handle single volumes > 2^31 bytes (but < 2^32)\n", + "1.27 28 Jul 2007 [rickr] nim->nvox, NBL-bsize are now type size_t\n" + "1.28 30 Jul 2007 [rickr] size_t updates\n", + "1.29 08 Aug 2007 [rickr] for list, valid_nifti_brick_list requires 3 dims\n" + "1.30 08 Nov 2007 [Yaroslav/rickr]\n" + " - fix ARM struct alignment problem in byte-swapping routines\n", + "1.31 29 Nov 2007 [rickr] for nifticlib-1.0.0\n" + " - added nifti_datatype_to/from_string routines\n" + " - added DT_RGBA32/NIFTI_TYPE_RGBA32 datatype macros (2304)\n" + " - added NIFTI_ECODE_FREESURFER (14)\n", + "1.32 08 Dec 2007 [rickr]\n" + " - nifti_hdr_looks_good() allows ANALYZE headers (req. by V. Luccio)\n" + " - added nifti_datatype_is_valid()\n", + "1.33 05 Feb 2008 [hansj,rickr] - block nia.gz use\n" + "1.34 13 Jun 2008 [rickr] - added nifti_compiled_with_zlib()\n" + "1.35 03 Aug 2008 [rickr]\n", + " - deal with swapping, so that CPU type does not affect output\n" + " (motivated by C Burns)\n" + " - added nifti_analyze75 structure and nifti_swap_as_analyze()\n" + " - previous swap_nifti_header is saved as old_swap_nifti_header\n" + " - also swap UNUSED fields in nifti_1_header struct\n", + "1.36 07 Oct 2008 [rickr]\n", + " - added nifti_NBL_matches_nim() check for write_bricks()\n" + "1.37 10 Mar 2009 [rickr]\n", + " - H Johnson cast updates (06 Feb)\n" + " - added NIFTI_ECODE_PYPICKLE for PyNIfTI (06 Feb)\n" + " - added NIFTI_ECODEs 18-28 for the LONI MiND group\n" + "1.38 28 Apr 2009 [rickr]\n", + " - uppercase extensions are now valid (requested by M. Coursolle)\n" + " - nifti_set_allow_upper_fext controls this option (req by C. Ooi)\n" + "1.39 23 Jun 2009 [rickr]: added 4 checks of alloc() returns\n", + "1.40 16 Mar 2010 [rickr]: added NIFTI_ECODE_VOXBO for D. Kimberg\n", + "1.41 28 Apr 2010 [rickr]: added NIFTI_ECODE_CARET for J. Harwell\n", + "1.42 06 Jul 2010 [rickr]: trouble with large (gz) files\n", + " - noted/investigated by M Hanke and Y Halchenko\n" + " - fixed znzread/write, noting example by M Adler\n" + " - changed nifti_swap_* routines/calls to take size_t (6)\n" + "1.43 07 Jul 2010 [rickr]: fixed znzR/W to again return nmembers\n", + "1.44 19 Jul 2013 [rickr]: ITK compatibility updates from H Johnson\n", + "1.45 10 May 2019 [rickr]: added NIFTI_ECODE_QUANTIPHYSE\n", + "1.46 26 Sep 2019 [rickr]:\n" + " - nifti_read_ascii_image no longer closes fp or free's fname\n", + "----------------------------------------------------------------------\n" +}; +static const char gni_version[] = "nifti library version 1.46 (26 Sep, 2019)"; + +/*! global nifti options structure - init with defaults */ +static nifti_global_options g_opts = { + 1, /* debug level */ + 0, /* skip_blank_ext - skip extender if no extensions */ + 1 /* allow_upper_fext - allow uppercase file extensions */ +}; + +/*! global nifti types structure list (per type, ordered oldest to newest) */ +static const nifti_type_ele nifti_type_list[] = { + /* type nbyper swapsize name */ + { 0, 0, 0, "DT_UNKNOWN" }, + { 0, 0, 0, "DT_NONE" }, + { 1, 0, 0, "DT_BINARY" }, /* not usable */ + { 2, 1, 0, "DT_UNSIGNED_CHAR" }, + { 2, 1, 0, "DT_UINT8" }, + { 2, 1, 0, "NIFTI_TYPE_UINT8" }, + { 4, 2, 2, "DT_SIGNED_SHORT" }, + { 4, 2, 2, "DT_INT16" }, + { 4, 2, 2, "NIFTI_TYPE_INT16" }, + { 8, 4, 4, "DT_SIGNED_INT" }, + { 8, 4, 4, "DT_INT32" }, + { 8, 4, 4, "NIFTI_TYPE_INT32" }, + { 16, 4, 4, "DT_FLOAT" }, + { 16, 4, 4, "DT_FLOAT32" }, + { 16, 4, 4, "NIFTI_TYPE_FLOAT32" }, + { 32, 8, 4, "DT_COMPLEX" }, + { 32, 8, 4, "DT_COMPLEX64" }, + { 32, 8, 4, "NIFTI_TYPE_COMPLEX64" }, + { 64, 8, 8, "DT_DOUBLE" }, + { 64, 8, 8, "DT_FLOAT64" }, + { 64, 8, 8, "NIFTI_TYPE_FLOAT64" }, + { 128, 3, 0, "DT_RGB" }, + { 128, 3, 0, "DT_RGB24" }, + { 128, 3, 0, "NIFTI_TYPE_RGB24" }, + { 255, 0, 0, "DT_ALL" }, + { 256, 1, 0, "DT_INT8" }, + { 256, 1, 0, "NIFTI_TYPE_INT8" }, + { 512, 2, 2, "DT_UINT16" }, + { 512, 2, 2, "NIFTI_TYPE_UINT16" }, + { 768, 4, 4, "DT_UINT32" }, + { 768, 4, 4, "NIFTI_TYPE_UINT32" }, + { 1024, 8, 8, "DT_INT64" }, + { 1024, 8, 8, "NIFTI_TYPE_INT64" }, + { 1280, 8, 8, "DT_UINT64" }, + { 1280, 8, 8, "NIFTI_TYPE_UINT64" }, + { 1536, 16, 16, "DT_FLOAT128" }, + { 1536, 16, 16, "NIFTI_TYPE_FLOAT128" }, + { 1792, 16, 8, "DT_COMPLEX128" }, + { 1792, 16, 8, "NIFTI_TYPE_COMPLEX128" }, + { 2048, 32, 16, "DT_COMPLEX256" }, + { 2048, 32, 16, "NIFTI_TYPE_COMPLEX256" }, + { 2304, 4, 0, "DT_RGBA32" }, + { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, +}; + +/*---------------------------------------------------------------------------*/ +/* prototypes for internal functions - not part of exported library */ + +/* extension routines */ +static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain ); +static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, int remain, znzFile fp ); +static int nifti_check_extension(nifti_image *nim, int size,int code, int rem); +static void update_nifti_image_for_brick_list(nifti_image * nim , int nbricks); +static int nifti_add_exten_to_list(nifti1_extension * new_ext, + nifti1_extension ** list, int new_length); +static int nifti_fill_extension(nifti1_extension * ext, const char * data, + int len, int ecode); + +/* NBL routines */ +static int nifti_load_NBL_bricks(nifti_image * nim , const int * slist, const int * sindex, nifti_brick_list * NBL, znzFile fp ); +static int nifti_alloc_NBL_mem( nifti_image * nim, int nbricks, + nifti_brick_list * nbl); +static int nifti_copynsort(int nbricks, const int *blist, int **slist, + int **sindex); +static int nifti_NBL_matches_nim(const nifti_image *nim, + const nifti_brick_list *NBL); + +/* for nifti_read_collapsed_image: */ +static int rci_read_data(nifti_image *nim, int *pivots, int *prods, int nprods, + const int dims[], char *data, znzFile fp, size_t base_offset); +static int rci_alloc_mem(void ** data, const int prods[8], int nprods, int nbyper ); +static int make_pivot_list(nifti_image * nim, const int dims[], int pivots[], + int prods[], int * nprods ); + +/* misc */ +static int compare_strlist (const char * str, char ** strlist, int len); +static int fileext_compare (const char * test_ext, const char * known_ext); +static int fileext_n_compare (const char * test_ext, + const char * known_ext, size_t maxlen); +static int is_mixedcase (const char * str); +static int is_uppercase (const char * str); +static int make_lowercase (char * str); +static int make_uppercase (char * str); +static int need_nhdr_swap (short dim0, int hdrsize); +static int print_hex_vals (const char * data, size_t nbytes, FILE * fp); +static int unescape_string (char *str); /* string utility functions */ +static char *escapize_string (const char *str); + +/* internal I/O routines */ +static znzFile nifti_image_load_prep( nifti_image *nim ); +static int has_ascii_header(znzFile fp); +/*---------------------------------------------------------------------------*/ + + +/* for calling from some main program */ +/*----------------------------------------------------------------------*/ +/*! display the nifti library module history (via stdout) +*//*--------------------------------------------------------------------*/ +void nifti_disp_lib_hist( void ) +{ + int c, len = sizeof(gni_history)/sizeof(char *); + for( c = 0; c < len; c++ ) + Rc_fputs_stdout(gni_history[c]); +} + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE + +/*----------------------------------------------------------------------*/ +/*! display the nifti library version (via stdout) +*//*--------------------------------------------------------------------*/ +void nifti_disp_lib_version( void ) +{ + Rc_printf("%s, compiled %s\n", gni_version, __DATE__); +} +#endif + +/*----------------------------------------------------------------------*/ +/*! nifti_image_read_bricks - read nifti data as array of bricks + * + * 13 Dec 2004 [rickr] + * + * \param hname - filename of dataset to read (must be valid) + * \param nbricks - number of sub-bricks to read + * (if blist is valid, nbricks must be > 0) + * \param blist - list of sub-bricks to read + * (can be NULL; if NULL, read complete dataset) + * \param NBL - pointer to empty nifti_brick_list struct + * (must be a valid pointer) + * + * \return + *
nim - same as nifti_image_read, but + * nim->nt = NBL->nbricks (or nt*nu*nv*nw) + * nim->nu,nv,nw = 1 + * nim->data = NULL + *
NBL - filled with data volumes + * + * By default, this function will read the nifti dataset and break the data + * into a list of nt*nu*nv*nw sub-bricks, each having size nx*ny*nz elements. + * That is to say, instead of reading the entire dataset as a single array, + * break it up into sub-bricks (volumes), each of size nx*ny*nz elements. + * + * Note: in the returned nifti_image, nu, nv and nw will always be 1. The + * intention of this function is to collapse the dataset into a single + * array of volumes (of length nbricks or nt*nu*nv*nw). + * + * If 'blist' is valid, it is taken to be a list of sub-bricks, of length + * 'nbricks'. The data will still be separated into sub-bricks of size + * nx*ny*nz elements, but now 'nbricks' sub-bricks will be returned, of the + * caller's choosing via 'blist'. + * + * E.g. consider a dataset with 12 sub-bricks (numbered 0..11), and the + * following code: + * + *
+ * { nifti_brick_list   NB_orig, NB_select;
+ *   nifti_image      * nim_orig, * nim_select;
+ *   int                blist[5] = { 7, 0, 5, 5, 9 };
+ *
+ *   nim_orig   = nifti_image_read_bricks("myfile.nii", 0, NULL,  &NB_orig);
+ *   nim_select = nifti_image_read_bricks("myfile.nii", 5, blist, &NB_select);
+ * }
+ * 
+ * + * Here, nim_orig gets the entire dataset, where NB_orig.nbricks = 12. But + * nim_select has NB_select.nbricks = 5. + * + * Note that the first case is not quite the same as just calling the + * nifti_image_read function, as here the data is separated into sub-bricks. + * + * Note that valid blist elements are in [0..nt*nu*nv*nw-1], + * or written [ 0 .. (dim[4]*dim[5]*dim[6]*dim[7] - 1) ]. + * + * Note that, as is the case with all of the reading functions, the + * data will be allocated, read in, and properly byte-swapped, if + * necessary. + * + * \sa nifti_image_load_bricks, nifti_free_NBL, valid_nifti_brick_list, + nifti_image_read +*//*----------------------------------------------------------------------*/ +nifti_image *nifti_image_read_bricks(const char * hname, int nbricks, + const int * blist, nifti_brick_list * NBL) +{ + nifti_image * nim; + + if( !hname || !NBL ){ + Rc_fprintf_stderr("** nifti_image_read_bricks: bad params (%p,%p)\n", + hname, (void *)NBL); + return NULL; + } + + if( blist && nbricks <= 0 ){ + Rc_fprintf_stderr("** nifti_image_read_bricks: bad nbricks, %d\n", nbricks); + return NULL; + } + + nim = nifti_image_read(hname, 0); /* read header, but not data */ + + if( !nim ) return NULL; /* errors were already printed */ + + /* if we fail, free image and return */ + if( nifti_image_load_bricks(nim, nbricks, blist, NBL) <= 0 ){ + nifti_image_free(nim); + return NULL; + } + + if( blist ) update_nifti_image_for_brick_list(nim, nbricks); + + return nim; +} + + +/*---------------------------------------------------------------------- + * update_nifti_image_for_brick_list - update nifti_image + * + * When loading a specific brick list, the distinction between + * nt, nu, nv and nw is lost. So put everything in t, and set + * dim[0] = 4. + *----------------------------------------------------------------------*/ +static void update_nifti_image_for_brick_list( nifti_image * nim , int nbricks ) +{ + int ndim; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d updating image dimensions for %d bricks in list\n", + nbricks); + Rc_fprintf_stderr(" ndim = %d\n",nim->ndim); + Rc_fprintf_stderr(" nx,ny,nz,nt,nu,nv,nw: (%d,%d,%d,%d,%d,%d,%d)\n", + nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); + } + + nim->nt = nbricks; + nim->nu = nim->nv = nim->nw = 1; + nim->dim[4] = nbricks; + nim->dim[5] = nim->dim[6] = nim->dim[7] = 1; + + /* compute nvox */ + /* do not rely on dimensions above dim[0] 16 Nov 2005 [rickr] */ + for( nim->nvox = 1, ndim = 1; ndim <= nim->dim[0]; ndim++ ) + nim->nvox *= nim->dim[ndim]; + + /* update the dimensions to 4 or lower */ + for( ndim = 4; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) + ; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d ndim = %d -> %d\n",nim->ndim, ndim); + Rc_fprintf_stderr(" --> (%d,%d,%d,%d,%d,%d,%d)\n", + nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); + } + + nim->dim[0] = nim->ndim = ndim; +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_update_dims_from_array - update nx, ny, ... from nim->dim[] + + Fix all the dimension information, based on a new nim->dim[]. + + Note: we assume that dim[0] will not increase. + + Check for updates to pixdim[], dx,..., nx,..., nvox, ndim, dim[0]. +*//*--------------------------------------------------------------------*/ +int nifti_update_dims_from_array( nifti_image * nim ) +{ + int c, ndim; + + if( !nim ){ + Rc_fprintf_stderr("** update_dims: missing nim\n"); + return 1; + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d updating image dimensions given nim->dim:"); + for( c = 0; c < 8; c++ ) Rc_fprintf_stderr(" %d", nim->dim[c]); + Rc_fputc_stderr('\n'); + } + + /* verify dim[0] first */ + if(nim->dim[0] < 1 || nim->dim[0] > 7){ + Rc_fprintf_stderr("** invalid dim[0], dim[] = "); + for( c = 0; c < 8; c++ ) Rc_fprintf_stderr(" %d", nim->dim[c]); + Rc_fputc_stderr('\n'); + return 1; + } + + /* set nx, ny ..., dx, dy, ..., one by one */ + + /* less than 1, set to 1, else copy */ + if(nim->dim[1] < 1) nim->nx = nim->dim[1] = 1; + else nim->nx = nim->dim[1]; + nim->dx = nim->pixdim[1]; + + /* if undefined, or less than 1, set to 1 */ + if(nim->dim[0] < 2 || (nim->dim[0] >= 2 && nim->dim[2] < 1)) + nim->ny = nim->dim[2] = 1; + else + nim->ny = nim->dim[2]; + /* copy delta values, in any case */ + nim->dy = nim->pixdim[2]; + + if(nim->dim[0] < 3 || (nim->dim[0] >= 3 && nim->dim[3] < 1)) + nim->nz = nim->dim[3] = 1; + else /* just copy vals from arrays */ + nim->nz = nim->dim[3]; + nim->dz = nim->pixdim[3]; + + if(nim->dim[0] < 4 || (nim->dim[0] >= 4 && nim->dim[4] < 1)) + nim->nt = nim->dim[4] = 1; + else /* just copy vals from arrays */ + nim->nt = nim->dim[4]; + nim->dt = nim->pixdim[4]; + + if(nim->dim[0] < 5 || (nim->dim[0] >= 5 && nim->dim[5] < 1)) + nim->nu = nim->dim[5] = 1; + else /* just copy vals from arrays */ + nim->nu = nim->dim[5]; + nim->du = nim->pixdim[5]; + + if(nim->dim[0] < 6 || (nim->dim[0] >= 6 && nim->dim[6] < 1)) + nim->nv = nim->dim[6] = 1; + else /* just copy vals from arrays */ + nim->nv = nim->dim[6]; + nim->dv = nim->pixdim[6]; + + if(nim->dim[0] < 7 || (nim->dim[0] >= 7 && nim->dim[7] < 1)) + nim->nw = nim->dim[7] = 1; + else /* just copy vals from arrays */ + nim->nw = nim->dim[7]; + nim->dw = nim->pixdim[7]; + + for( c = 1, nim->nvox = 1; c <= nim->dim[0]; c++ ) + nim->nvox *= nim->dim[c]; + + /* compute ndim, assuming it can be no larger than the old one */ + for( ndim = nim->dim[0]; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) + ; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d ndim = %d -> %d\n",nim->ndim, ndim); + Rc_fprintf_stderr(" --> (%d,%d,%d,%d,%d,%d,%d)\n", + nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); + } + + nim->dim[0] = nim->ndim = ndim; + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! Load the image data from disk into an already-prepared image struct. + * + * \param nim - initialized nifti_image, without data + * \param nbricks - the length of blist (must be 0 if blist is NULL) + * \param blist - an array of xyz volume indices to read (can be NULL) + * \param NBL - pointer to struct where resulting data will be stored + * + * If blist is NULL, read all sub-bricks. + * + * \return the number of loaded bricks (NBL->nbricks), + * 0 on failure, < 0 on error + * + * NOTE: it is likely that another function will copy the data pointers + * out of NBL, in which case the only pointer the calling function + * will want to free is NBL->bricks (not each NBL->bricks[i]). +*//*--------------------------------------------------------------------*/ +int nifti_image_load_bricks( nifti_image * nim , int nbricks, + const int * blist, nifti_brick_list * NBL ) +{ + int * slist = NULL, * sindex = NULL, rv; + znzFile fp; + + /* we can have blist == NULL */ + if( !nim || !NBL ){ + Rc_fprintf_stderr("** nifti_image_load_bricks, bad params (%p,%p)\n", + (void *)nim, (void *)NBL); + return -1; + } + + if( blist && nbricks <= 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d load_bricks: received blist with nbricks = %d," + "ignoring blist\n", nbricks); + blist = NULL; /* pretend nothing was passed */ + } + + if( blist && ! valid_nifti_brick_list(nim, nbricks, blist, g_opts.debug>0) ) + return -1; + + /* for efficiency, let's read the file in order */ + if( blist && nifti_copynsort( nbricks, blist, &slist, &sindex ) != 0 ) + return -1; + + /* open the file and position the FILE pointer */ + fp = nifti_image_load_prep( nim ); + if( !fp ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_image_load_bricks, failed load_prep\n"); + if( blist ){ free(slist); free(sindex); } + return -1; + } + + /* this will flag to allocate defaults */ + if( !blist ) nbricks = 0; + if( nifti_alloc_NBL_mem( nim, nbricks, NBL ) != 0 ){ + if( blist ){ free(slist); free(sindex); } + znzclose(fp); + return -1; + } + + rv = nifti_load_NBL_bricks(nim, slist, sindex, NBL, fp); + + if( rv != 0 ){ + nifti_free_NBL( NBL ); /* failure! */ + NBL->nbricks = 0; /* repetative, but clear */ + } + + if( slist ){ free(slist); free(sindex); } + + znzclose(fp); + + return NBL->nbricks; +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_free_NBL - free all pointers and clear structure + * + * note: this does not presume to free the structure pointer +*//*--------------------------------------------------------------------*/ +void nifti_free_NBL( nifti_brick_list * NBL ) +{ + int c; + + if( NBL->bricks ){ + for( c = 0; c < NBL->nbricks; c++ ) + if( NBL->bricks[c] ) free(NBL->bricks[c]); + free(NBL->bricks); + NBL->bricks = NULL; + } + + NBL->bsize = NBL->nbricks = 0; +} + + +/*---------------------------------------------------------------------- + * nifti_load_NBL_bricks - read the file data into the NBL struct + * + * return 0 on success, -1 on failure + *----------------------------------------------------------------------*/ +static int nifti_load_NBL_bricks( nifti_image * nim , const int * slist, const int * sindex, + nifti_brick_list * NBL, znzFile fp ) +{ + size_t oposn, fposn; /* orig and current file positions */ + size_t rv; + long test; + int c; + int prev, isrc, idest; /* previous and current sub-brick, and new index */ + + test = znztell(fp); /* store current file position */ + if( test < 0 ){ + Rc_fprintf_stderr("** load bricks: ztell failed??\n"); + return -1; + } + fposn = oposn = test; + + /* first, handle the default case, no passed blist */ + if( !slist ){ + for( c = 0; c < NBL->nbricks; c++ ) { + rv = nifti_read_buffer(fp, NBL->bricks[c], NBL->bsize, nim); + if( rv != NBL->bsize ){ + Rc_fprintf_stderr("** load bricks: cannot read brick %d from '%s'\n", + c, nim->iname ? nim->iname : nim->fname); + return -1; + } + } + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d read %d default %u-byte bricks from file %s\n", + NBL->nbricks, (unsigned int)NBL->bsize, + nim->iname ? nim->iname:nim->fname ); + return 0; + } + + if( !sindex ){ + Rc_fprintf_stderr("** load_NBL_bricks: missing index list\n"); + return -1; + } + + prev = -1; /* use prev for previous sub-brick */ + for( c = 0; c < NBL->nbricks; c++ ){ + isrc = slist[c]; /* this is original brick index (c is new one) */ + idest = sindex[c]; /* this is the destination index for this data */ + + /* if this sub-brick is not the previous, we must read from disk */ + if( isrc != prev ){ + + /* if we are not looking at the correct sub-brick, scan forward */ + if( fposn != (oposn + isrc*NBL->bsize) ){ + fposn = oposn + isrc*NBL->bsize; + if( znzseek(fp, (long)fposn, SEEK_SET) < 0 ){ + Rc_fprintf_stderr("** failed to locate brick %d in file '%s'\n", + isrc, nim->iname ? nim->iname : nim->fname); + return -1; + } + } + + /* only 10,000 lines later and we're actually reading something! */ + rv = nifti_read_buffer(fp, NBL->bricks[idest], NBL->bsize, nim); + if( rv != NBL->bsize ){ + Rc_fprintf_stderr("** failed to read brick %d from file '%s'\n", + isrc, nim->iname ? nim->iname : nim->fname); + if( g_opts.debug > 1 ) + Rc_fprintf_stderr(" (read %u of %u bytes)\n", + (unsigned int)rv, (unsigned int)NBL->bsize); + return -1; + } + fposn += NBL->bsize; + } else { + /* we have already read this sub-brick, just copy the previous one */ + /* note that this works because they are sorted */ + memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], NBL->bsize); + } + + prev = isrc; /* in any case, note the now previous sub-brick */ + } + + return 0; +} + + +/*---------------------------------------------------------------------- + * nifti_alloc_NBL_mem - allocate memory for bricks + * + * return 0 on success, -1 on failure + *----------------------------------------------------------------------*/ +static int nifti_alloc_NBL_mem(nifti_image * nim, int nbricks, + nifti_brick_list * nbl) +{ + int c; + + /* if nbricks is not specified, use the default */ + if( nbricks > 0 ) nbl->nbricks = nbricks; + else { /* I missed this one with the 1.17 change 02 Mar 2006 [rickr] */ + nbl->nbricks = 1; + for( c = 4; c <= nim->ndim; c++ ) + nbl->nbricks *= nim->dim[c]; + } + + nbl->bsize = (size_t)nim->nx * nim->ny * nim->nz * nim->nbyper;/* bytes */ + nbl->bricks = (void **)malloc(nbl->nbricks * sizeof(void *)); + + if( ! nbl->bricks ){ + Rc_fprintf_stderr("** NANM: failed to alloc %d void ptrs\n",nbricks); + return -1; + } + + for( c = 0; c < nbl->nbricks; c++ ){ + nbl->bricks[c] = (void *)malloc(nbl->bsize); + if( ! nbl->bricks[c] ){ + Rc_fprintf_stderr("** NANM: failed to alloc %u bytes for brick %d\n", + (unsigned int)nbl->bsize, c); + /* so free and clear everything before returning */ + while( c > 0 ){ + c--; + free(nbl->bricks[c]); + } + free(nbl->bricks); + nbl->bricks = NULL; + nbl->bsize = nbl->nbricks = 0; + return -1; + } + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d NANM: alloc'd %d bricks of %u bytes for NBL\n", + nbl->nbricks, (unsigned int)nbl->bsize); + + return 0; +} + + +/*---------------------------------------------------------------------- + * nifti_copynsort - copy int list, and sort with indices + * + * 1. duplicate the incoming list + * 2. create an sindex list, and init with 0..nbricks-1 + * 3. do a slow insertion sort on the small slist, along with sindex list + * 4. check results, just to be positive + * + * So slist is sorted, and sindex hold original positions. + * + * return 0 on success, -1 on failure + *----------------------------------------------------------------------*/ +static int nifti_copynsort(int nbricks, const int * blist, int ** slist, + int ** sindex) +{ + int * stmp, * itmp; /* for ease of typing/reading */ + int c1, c2, spos, tmp; + + *slist = (int *)malloc(nbricks * sizeof(int)); + *sindex = (int *)malloc(nbricks * sizeof(int)); + + if( !*slist || !*sindex ){ + Rc_fprintf_stderr("** NCS: failed to alloc %d ints for sorting\n",nbricks); + if(*slist) free(*slist); /* maybe one succeeded */ + if(*sindex) free(*sindex); + return -1; + } + + /* init the lists */ + memcpy(*slist, blist, nbricks*sizeof(int)); + for( c1 = 0; c1 < nbricks; c1++ ) (*sindex)[c1] = c1; + + /* now actually sort slist */ + stmp = *slist; + itmp = *sindex; + for( c1 = 0; c1 < nbricks-1; c1++ ) { + /* find smallest value, init to current */ + spos = c1; + for( c2 = c1+1; c2 < nbricks; c2++ ) + if( stmp[c2] < stmp[spos] ) spos = c2; + if( spos != c1 ) /* swap: fine, don't maintain sub-order, see if I care */ + { + tmp = stmp[c1]; /* first swap the sorting values */ + stmp[c1] = stmp[spos]; + stmp[spos] = tmp; + + tmp = itmp[c1]; /* then swap the index values */ + itmp[c1] = itmp[spos]; + itmp[spos] = tmp; + } + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d sorted indexing list:\n"); + Rc_fprintf_stderr(" orig : "); + for( c1 = 0; c1 < nbricks; c1++ ) Rc_fprintf_stderr(" %d",blist[c1]); + Rc_fprintf_stderr("\n new : "); + for( c1 = 0; c1 < nbricks; c1++ ) Rc_fprintf_stderr(" %d",stmp[c1]); + Rc_fprintf_stderr("\n indices: "); + for( c1 = 0; c1 < nbricks; c1++ ) Rc_fprintf_stderr(" %d",itmp[c1]); + Rc_fputc_stderr('\n'); + } + + /* check the sort (why not? I've got time...) */ + for( c1 = 0; c1 < nbricks-1; c1++ ){ + if( (stmp[c1] > stmp[c1+1]) || (blist[itmp[c1]] != stmp[c1]) ){ + Rc_fprintf_stderr("** sorting screw-up, way to go, rick!\n"); + free(stmp); free(itmp); *slist = NULL; *sindex = NULL; + return -1; + } + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d sorting is okay\n"); + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! valid_nifti_brick_list - check sub-brick list for image + * + * This function verifies that nbricks and blist are appropriate + * for use with this nim, based on the dimensions. + * + * \param nim nifti_image to check against + * \param nbricks number of brick indices in blist + * \param blist list of brick indices to check in nim + * \param disp_error if this flag is set, report errors to user + * + * \return 1 if valid, 0 if not +*//*--------------------------------------------------------------------*/ +int valid_nifti_brick_list(nifti_image * nim , int nbricks, + const int * blist, int disp_error) +{ + int c, nsubs; + + if( !nim ){ + if( disp_error || g_opts.debug > 0 ) + Rc_fprintf_stderr("** valid_nifti_brick_list: missing nifti image\n"); + return 0; + } + + if( nbricks <= 0 || !blist ){ + if( disp_error || g_opts.debug > 1 ) + Rc_fprintf_stderr("** valid_nifti_brick_list: no brick list to check\n"); + return 0; + } + + if( nim->dim[0] < 3 ){ + if( disp_error || g_opts.debug > 1 ) + Rc_fprintf_stderr("** cannot read explict brick list from %d-D dataset\n", + nim->dim[0]); + return 0; + } + + /* nsubs sub-brick is nt*nu*nv*nw */ + for( c = 4, nsubs = 1; c <= nim->dim[0]; c++ ) + nsubs *= nim->dim[c]; + + if( nsubs <= 0 ){ + Rc_fprintf_stderr("** VNBL warning: bad dim list (%d,%d,%d,%d)\n", + nim->dim[4], nim->dim[5], nim->dim[6], nim->dim[7]); + return 0; + } + + for( c = 0; c < nbricks; c++ ) + if( (blist[c] < 0) || (blist[c] >= nsubs) ){ + if( disp_error || g_opts.debug > 1 ) + Rc_fprintf_stderr( + "** volume index %d (#%d) is out of range [0,%d]\n", + blist[c], c, nsubs-1); + return 0; + } + + return 1; /* all is well */ +} + +/*----------------------------------------------------------------------*/ +/* verify that NBL struct is a valid data source for the image + * + * return 1 if so, 0 otherwise +*//*--------------------------------------------------------------------*/ +static int nifti_NBL_matches_nim(const nifti_image *nim, + const nifti_brick_list *NBL) +{ + size_t volbytes = 0; /* bytes per volume */ + int ind, errs = 0, nvols = 0; + + + if( !nim || !NBL ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_NBL_matches_nim: NULL pointer(s)\n"); + return 0; + } + + /* for nim, compute volbytes and nvols */ + if( nim->ndim > 0 ) { + /* first 3 indices are over a single volume */ + volbytes = (size_t)nim->nbyper; + for( ind = 1; ind <= nim->ndim && ind < 4; ind++ ) + volbytes *= (size_t)nim->dim[ind]; + + for( ind = 4, nvols = 1; ind <= nim->ndim; ind++ ) + nvols *= nim->dim[ind]; + } + + if( volbytes != NBL->bsize ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** NBL/nim mismatch, volbytes = %u, %u\n", + (unsigned)NBL->bsize, (unsigned)volbytes); + errs++; + } + + if( nvols != NBL->nbricks ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** NBL/nim mismatch, nvols = %d, %d\n", + NBL->nbricks, nvols); + errs++; + } + + if( errs ) return 0; + else if ( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- nim/NBL agree: nvols = %d, nbytes = %u\n", + nvols, (unsigned)volbytes); + + return 1; +} + +/* end of new nifti_image_read_bricks() functionality */ + +/*----------------------------------------------------------------------*/ +/*! display the orientation from the quaternian fields + * + * \param mesg if non-NULL, display this message first + * \param mat the matrix to convert to "nearest" orientation + * + * \return -1 if results cannot be determined, 0 if okay +*//*--------------------------------------------------------------------*/ +int nifti_disp_matrix_orient( const char * mesg, mat44 mat ) +{ + int i, j, k; + + if ( mesg ) Rc_fputs_stderr( mesg ); /* use stdout? */ + + nifti_mat44_to_orientation( mat, &i,&j,&k ); + if ( i <= 0 || j <= 0 || k <= 0 ) return -1; + + /* so we have good codes */ + Rc_fprintf_stderr( " i orientation = '%s'\n" + " j orientation = '%s'\n" + " k orientation = '%s'\n", + nifti_orientation_string(i), + nifti_orientation_string(j), + nifti_orientation_string(k) ); + return 0; +} + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +/*----------------------------------------------------------------------*/ +/*! duplicate the given string (alloc length+1) + * + * \return allocated pointer (or NULL on failure) +*//*--------------------------------------------------------------------*/ +char *nifti_strdup(const char *str) +{ + char *dup; + + if( !str ) return NULL; /* allow calls passing NULL */ + + dup = (char *)malloc(strlen(str) + 1); + + /* check for failure */ + if( dup ) strcpy(dup, str); + else Rc_fprintf_stderr("** nifti_strdup: failed to alloc %u bytes\n", + (unsigned int)strlen(str)+1); + + return dup; +} + + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI datatype. + + \param dt NIfTI-1 datatype + + \return pointer to static string holding the datatype name + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_DATATYPES group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const * nifti_datatype_string( int dt ) +{ + switch( dt ){ + case DT_UNKNOWN: return "UNKNOWN" ; + case DT_BINARY: return "BINARY" ; + case DT_INT8: return "INT8" ; + case DT_UINT8: return "UINT8" ; + case DT_INT16: return "INT16" ; + case DT_UINT16: return "UINT16" ; + case DT_INT32: return "INT32" ; + case DT_UINT32: return "UINT32" ; + case DT_INT64: return "INT64" ; + case DT_UINT64: return "UINT64" ; + case DT_FLOAT32: return "FLOAT32" ; + case DT_FLOAT64: return "FLOAT64" ; + case DT_FLOAT128: return "FLOAT128" ; + case DT_COMPLEX64: return "COMPLEX64" ; + case DT_COMPLEX128: return "COMPLEX128" ; + case DT_COMPLEX256: return "COMPLEX256" ; + case DT_RGB24: return "RGB24" ; + case DT_RGBA32: return "RGBA32" ; + } + return "**ILLEGAL**" ; +} + +/*----------------------------------------------------------------------*/ +/*! Determine if the datatype code dt is an integer type (1=YES, 0=NO). + + \return whether the given NIfTI-1 datatype code is valid + + \sa NIFTI1_DATATYPES group in nifti1.h +*//*--------------------------------------------------------------------*/ +int nifti_is_inttype( int dt ) +{ + switch( dt ){ + case DT_UNKNOWN: return 0 ; + case DT_BINARY: return 0 ; + case DT_INT8: return 1 ; + case DT_UINT8: return 1 ; + case DT_INT16: return 1 ; + case DT_UINT16: return 1 ; + case DT_INT32: return 1 ; + case DT_UINT32: return 1 ; + case DT_INT64: return 1 ; + case DT_UINT64: return 1 ; + case DT_FLOAT32: return 0 ; + case DT_FLOAT64: return 0 ; + case DT_FLOAT128: return 0 ; + case DT_COMPLEX64: return 0 ; + case DT_COMPLEX128: return 0 ; + case DT_COMPLEX256: return 0 ; + case DT_RGB24: return 1 ; + case DT_RGBA32: return 1 ; + } + return 0 ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI units type. + + \param uu NIfTI-1 unit code + + \return pointer to static string for the given unit type + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_UNITS group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_units_string( int uu ) +{ + switch( uu ){ + case NIFTI_UNITS_METER: return "m" ; + case NIFTI_UNITS_MM: return "mm" ; + case NIFTI_UNITS_MICRON: return "um" ; + case NIFTI_UNITS_SEC: return "s" ; + case NIFTI_UNITS_MSEC: return "ms" ; + case NIFTI_UNITS_USEC: return "us" ; + case NIFTI_UNITS_HZ: return "Hz" ; + case NIFTI_UNITS_PPM: return "ppm" ; + case NIFTI_UNITS_RADS: return "rad/s" ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI transform type. + + \param xx NIfTI-1 xform code + + \return pointer to static string describing xform code + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_XFORM_CODES group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_xform_string( int xx ) +{ + switch( xx ){ + case NIFTI_XFORM_SCANNER_ANAT: return "Scanner Anat" ; + case NIFTI_XFORM_ALIGNED_ANAT: return "Aligned Anat" ; + case NIFTI_XFORM_TALAIRACH: return "Talairach" ; + case NIFTI_XFORM_MNI_152: return "MNI_152" ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI intent type. + + \param ii NIfTI-1 intent code + + \return pointer to static string describing code + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_INTENT_CODES group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_intent_string( int ii ) +{ + switch( ii ){ + case NIFTI_INTENT_CORREL: return "Correlation statistic" ; + case NIFTI_INTENT_TTEST: return "T-statistic" ; + case NIFTI_INTENT_FTEST: return "F-statistic" ; + case NIFTI_INTENT_ZSCORE: return "Z-score" ; + case NIFTI_INTENT_CHISQ: return "Chi-squared distribution" ; + case NIFTI_INTENT_BETA: return "Beta distribution" ; + case NIFTI_INTENT_BINOM: return "Binomial distribution" ; + case NIFTI_INTENT_GAMMA: return "Gamma distribution" ; + case NIFTI_INTENT_POISSON: return "Poisson distribution" ; + case NIFTI_INTENT_NORMAL: return "Normal distribution" ; + case NIFTI_INTENT_FTEST_NONC: return "F-statistic noncentral" ; + case NIFTI_INTENT_CHISQ_NONC: return "Chi-squared noncentral" ; + case NIFTI_INTENT_LOGISTIC: return "Logistic distribution" ; + case NIFTI_INTENT_LAPLACE: return "Laplace distribution" ; + case NIFTI_INTENT_UNIFORM: return "Uniform distribition" ; + case NIFTI_INTENT_TTEST_NONC: return "T-statistic noncentral" ; + case NIFTI_INTENT_WEIBULL: return "Weibull distribution" ; + case NIFTI_INTENT_CHI: return "Chi distribution" ; + case NIFTI_INTENT_INVGAUSS: return "Inverse Gaussian distribution" ; + case NIFTI_INTENT_EXTVAL: return "Extreme Value distribution" ; + case NIFTI_INTENT_PVAL: return "P-value" ; + + case NIFTI_INTENT_LOGPVAL: return "Log P-value" ; + case NIFTI_INTENT_LOG10PVAL: return "Log10 P-value" ; + + case NIFTI_INTENT_ESTIMATE: return "Estimate" ; + case NIFTI_INTENT_LABEL: return "Label index" ; + case NIFTI_INTENT_NEURONAME: return "NeuroNames index" ; + case NIFTI_INTENT_GENMATRIX: return "General matrix" ; + case NIFTI_INTENT_SYMMATRIX: return "Symmetric matrix" ; + case NIFTI_INTENT_DISPVECT: return "Displacement vector" ; + case NIFTI_INTENT_VECTOR: return "Vector" ; + case NIFTI_INTENT_POINTSET: return "Pointset" ; + case NIFTI_INTENT_TRIANGLE: return "Triangle" ; + case NIFTI_INTENT_QUATERNION: return "Quaternion" ; + + case NIFTI_INTENT_DIMLESS: return "Dimensionless number" ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI slice_code. + + \param ss NIfTI-1 slice order code + + \return pointer to static string describing code + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_SLICE_ORDER group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_slice_string( int ss ) +{ + switch( ss ){ + case NIFTI_SLICE_SEQ_INC: return "sequential_increasing" ; + case NIFTI_SLICE_SEQ_DEC: return "sequential_decreasing" ; + case NIFTI_SLICE_ALT_INC: return "alternating_increasing" ; + case NIFTI_SLICE_ALT_DEC: return "alternating_decreasing" ; + case NIFTI_SLICE_ALT_INC2: return "alternating_increasing_2" ; + case NIFTI_SLICE_ALT_DEC2: return "alternating_decreasing_2" ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI orientation. + + \param ii orientation code + + \return pointer to static string holding the orientation information + + \warning Do not free() or modify the return string! + It points to static storage. + + \sa NIFTI_L2R in nifti1_io.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_orientation_string( int ii ) +{ + switch( ii ){ + case NIFTI_L2R: return "Left-to-Right" ; + case NIFTI_R2L: return "Right-to-Left" ; + case NIFTI_P2A: return "Posterior-to-Anterior" ; + case NIFTI_A2P: return "Anterior-to-Posterior" ; + case NIFTI_I2S: return "Inferior-to-Superior" ; + case NIFTI_S2I: return "Superior-to-Inferior" ; + } + return "Unknown" ; +} + +/*--------------------------------------------------------------------------*/ +/*! Given a datatype code, set number of bytes per voxel and the swapsize. + + \param datatype nifti1 datatype code + \param nbyper pointer to return value: number of bytes per voxel + \param swapsize pointer to return value: size of swap blocks + + \return appropriate values at nbyper and swapsize + + The swapsize is set to 0 if this datatype doesn't ever need swapping. + + \sa NIFTI1_DATATYPES in nifti1.h +*//*------------------------------------------------------------------------*/ +void nifti_datatype_sizes( int datatype , int *nbyper, int *swapsize ) +{ + int nb=0, ss=0 ; + switch( datatype ){ + case DT_INT8: + case DT_UINT8: nb = 1 ; ss = 0 ; break ; + + case DT_INT16: + case DT_UINT16: nb = 2 ; ss = 2 ; break ; + + case DT_RGB24: nb = 3 ; ss = 0 ; break ; + case DT_RGBA32: nb = 4 ; ss = 0 ; break ; + + case DT_INT32: + case DT_UINT32: + case DT_FLOAT32: nb = 4 ; ss = 4 ; break ; + + case DT_COMPLEX64: nb = 8 ; ss = 4 ; break ; + + case DT_FLOAT64: + case DT_INT64: + case DT_UINT64: nb = 8 ; ss = 8 ; break ; + + case DT_FLOAT128: nb = 16 ; ss = 16 ; break ; + + case DT_COMPLEX128: nb = 16 ; ss = 8 ; break ; + + case DT_COMPLEX256: nb = 32 ; ss = 16 ; break ; + } + + ASSIF(nbyper,nb) ; ASSIF(swapsize,ss) ; } + +/*---------------------------------------------------------------------------*/ +/*! Given the quaternion parameters (etc.), compute a transformation matrix. + + See comments in nifti1.h for details. + - qb,qc,qd = quaternion parameters + - qx,qy,qz = offset parameters + - dx,dy,dz = grid stepsizes (non-negative inputs are set to 1.0) + - qfac = sign of dz step (< 0 is negative; >= 0 is positive) + +
+   If qx=qy=qz=0, dx=dy=dz=1, then the output is a rotation matrix.
+   For qfac >= 0, the rotation is proper.
+   For qfac <  0, the rotation is improper.
+   
+ + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + \see nifti_mat44_to_quatern, nifti_make_orthog_mat44, + nifti_mat44_to_orientation + +*//*-------------------------------------------------------------------------*/ +mat44 nifti_quatern_to_mat44( float qb, float qc, float qd, + float qx, float qy, float qz, + float dx, float dy, float dz, float qfac ) +{ + mat44 R ; + double a,b=qb,c=qc,d=qd , xd,yd,zd ; + + /* last row is always [ 0 0 0 1 ] */ + + R.m[3][0]=R.m[3][1]=R.m[3][2] = 0.0f ; R.m[3][3]= 1.0f ; + + /* compute a parameter from b,c,d */ + + a = 1.0l - (b*b + c*c + d*d) ; + if( a < 1.e-7l ){ /* special case */ + a = 1.0l / sqrt(b*b+c*c+d*d) ; + b *= a ; c *= a ; d *= a ; /* normalize (b,c,d) vector */ + a = 0.0l ; /* a = 0 ==> 180 degree rotation */ + } else{ + a = sqrt(a) ; /* angle = 2*arccos(a) */ + } + + /* load rotation matrix, including scaling factors for voxel sizes */ + + xd = (dx > 0.0) ? dx : 1.0l ; /* make sure are positive */ + yd = (dy > 0.0) ? dy : 1.0l ; + zd = (dz > 0.0) ? dz : 1.0l ; + + if( qfac < 0.0 ) zd = -zd ; /* left handedness? */ + + R.m[0][0] = (float)( (a*a+b*b-c*c-d*d) * xd) ; + R.m[0][1] = 2.0l * (b*c-a*d ) * yd ; + R.m[0][2] = 2.0l * (b*d+a*c ) * zd ; + R.m[1][0] = 2.0l * (b*c+a*d ) * xd ; + R.m[1][1] = (float)( (a*a+c*c-b*b-d*d) * yd) ; + R.m[1][2] = 2.0l * (c*d-a*b ) * zd ; + R.m[2][0] = 2.0l * (b*d-a*c ) * xd ; + R.m[2][1] = 2.0l * (c*d+a*b ) * yd ; + R.m[2][2] = (float)( (a*a+d*d-c*c-b*b) * zd) ; + + /* load offsets */ + + R.m[0][3] = qx ; R.m[1][3] = qy ; R.m[2][3] = qz ; + + return R ; +} + +/*---------------------------------------------------------------------------*/ +/*! Given the 3x4 upper corner of the matrix R, compute the quaternion + parameters that fit it. + + - Any NULL pointer on input won't get assigned (e.g., if you don't want + dx,dy,dz, just pass NULL in for those pointers). + - If the 3 input matrix columns are NOT orthogonal, they will be + orthogonalized prior to calculating the parameters, using + the polar decomposition to find the orthogonal matrix closest + to the column-normalized input matrix. + - However, if the 3 input matrix columns are NOT orthogonal, then + the matrix produced by nifti_quatern_to_mat44 WILL have orthogonal + columns, so it won't be the same as the matrix input here. + This "feature" is because the NIFTI 'qform' transform is + deliberately not fully general -- it is intended to model a volume + with perpendicular axes. + - If the 3 input matrix columns are not even linearly independent, + you'll just have to take your luck, won't you? + + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + + \see nifti_quatern_to_mat44, nifti_make_orthog_mat44, + nifti_mat44_to_orientation +*//*-------------------------------------------------------------------------*/ +void nifti_mat44_to_quatern( mat44 R , + float *qb, float *qc, float *qd, + float *qx, float *qy, float *qz, + float *dx, float *dy, float *dz, float *qfac ) +{ + double r11,r12,r13 , r21,r22,r23 , r31,r32,r33 ; + double xd,yd,zd , a,b,c,d ; + mat33 P,Q ; + + /* offset outputs are read write out of input matrix */ + + ASSIF(qx,R.m[0][3]) ; ASSIF(qy,R.m[1][3]) ; ASSIF(qz,R.m[2][3]) ; + + /* load 3x3 matrix into local variables */ + + r11 = R.m[0][0] ; r12 = R.m[0][1] ; r13 = R.m[0][2] ; + r21 = R.m[1][0] ; r22 = R.m[1][1] ; r23 = R.m[1][2] ; + r31 = R.m[2][0] ; r32 = R.m[2][1] ; r33 = R.m[2][2] ; + + /* compute lengths of each column; these determine grid spacings */ + + xd = sqrt( r11*r11 + r21*r21 + r31*r31 ) ; + yd = sqrt( r12*r12 + r22*r22 + r32*r32 ) ; + zd = sqrt( r13*r13 + r23*r23 + r33*r33 ) ; + + /* if a column length is zero, patch the trouble */ + + if( xd == 0.0l ){ r11 = 1.0l ; r21 = r31 = 0.0l ; xd = 1.0l ; } + if( yd == 0.0l ){ r22 = 1.0l ; r12 = r32 = 0.0l ; yd = 1.0l ; } + if( zd == 0.0l ){ r33 = 1.0l ; r13 = r23 = 0.0l ; zd = 1.0l ; } + + /* assign the output lengths */ + + ASSIF(dx,(float)xd) ; ASSIF(dy,(float)yd) ; ASSIF(dz,(float)zd) ; + + /* normalize the columns */ + + r11 /= xd ; r21 /= xd ; r31 /= xd ; + r12 /= yd ; r22 /= yd ; r32 /= yd ; + r13 /= zd ; r23 /= zd ; r33 /= zd ; + + /* At this point, the matrix has normal columns, but we have to allow + for the fact that the hideous user may not have given us a matrix + with orthogonal columns. + + So, now find the orthogonal matrix closest to the current matrix. + + One reason for using the polar decomposition to get this + orthogonal matrix, rather than just directly orthogonalizing + the columns, is so that inputting the inverse matrix to R + will result in the inverse orthogonal matrix at this point. + If we just orthogonalized the columns, this wouldn't necessarily hold. */ + + Q.m[0][0] = (float)r11 ; Q.m[0][1] = (float)r12 ; Q.m[0][2] = (float)r13 ; /* load Q */ + Q.m[1][0] = (float)r21 ; Q.m[1][1] = (float)r22 ; Q.m[1][2] = (float)r23 ; + Q.m[2][0] = (float)r31 ; Q.m[2][1] = (float)r32 ; Q.m[2][2] = (float)r33 ; + + P = nifti_mat33_polar(Q) ; /* P is orthog matrix closest to Q */ + + r11 = P.m[0][0] ; r12 = P.m[0][1] ; r13 = P.m[0][2] ; /* unload */ + r21 = P.m[1][0] ; r22 = P.m[1][1] ; r23 = P.m[1][2] ; + r31 = P.m[2][0] ; r32 = P.m[2][1] ; r33 = P.m[2][2] ; + + /* [ r11 r12 r13 ] */ + /* at this point, the matrix [ r21 r22 r23 ] is orthogonal */ + /* [ r31 r32 r33 ] */ + + /* compute the determinant to determine if it is proper */ + + zd = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; /* should be -1 or 1 */ + + if( zd > 0 ){ /* proper */ + ASSIF(qfac,1.0f) ; + } else { /* improper ==> flip 3rd column */ + ASSIF(qfac,-1.0f) ; + r13 = -r13 ; r23 = -r23 ; r33 = -r33 ; + } + + /* now, compute quaternion parameters */ + + a = r11 + r22 + r33 + 1.0l ; + + if( a > 0.5l ){ /* simplest case */ + a = 0.5l * sqrt(a) ; + b = 0.25l * (r32-r23) / a ; + c = 0.25l * (r13-r31) / a ; + d = 0.25l * (r21-r12) / a ; + } else { /* trickier case */ + xd = 1.0 + r11 - (r22+r33) ; /* 4*b*b */ + yd = 1.0 + r22 - (r11+r33) ; /* 4*c*c */ + zd = 1.0 + r33 - (r11+r22) ; /* 4*d*d */ + if( xd > 1.0 ){ + b = 0.5l * sqrt(xd) ; + c = 0.25l* (r12+r21) / b ; + d = 0.25l* (r13+r31) / b ; + a = 0.25l* (r32-r23) / b ; + } else if( yd > 1.0 ){ + c = 0.5l * sqrt(yd) ; + b = 0.25l* (r12+r21) / c ; + d = 0.25l* (r23+r32) / c ; + a = 0.25l* (r13-r31) / c ; + } else { + d = 0.5l * sqrt(zd) ; + b = 0.25l* (r13+r31) / d ; + c = 0.25l* (r23+r32) / d ; + a = 0.25l* (r21-r12) / d ; + } + if( a < 0.0l ){ b=-b ; c=-c ; d=-d;} + } + + ASSIF(qb,(float)b) ; ASSIF(qc,(float)c) ; ASSIF(qd,(float)d); +} + +/*---------------------------------------------------------------------------*/ +/*! Compute the inverse of a bordered 4x4 matrix. + +
+   - Some numerical code fragments were generated by Maple 8.
+   - If a singular matrix is input, the output matrix will be all zero.
+   - You can check for this by examining the [3][3] element, which will
+     be 1.0 for the normal case and 0.0 for the bad case.
+
+     The input matrix should have the form:
+        [ r11 r12 r13 v1 ]
+        [ r21 r22 r23 v2 ]
+        [ r31 r32 r33 v3 ]
+        [  0   0   0   1 ]
+     
+*//*-------------------------------------------------------------------------*/ +mat44 nifti_mat44_inverse( mat44 R ) +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33,v1,v2,v3 , deti ; + mat44 Q ; + /* INPUT MATRIX IS: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 v1 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 v2 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 v3 ] */ + v1 = R.m[0][3]; v2 = R.m[1][3]; v3 = R.m[2][3]; /* [ 0 0 0 1 ] */ + + deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + + if( deti != 0.0l ) deti = 1.0l / deti ; + + Q.m[0][0] = (float)( deti*( r22*r33-r32*r23) ) ; + Q.m[0][1] = (float)( deti*(-r12*r33+r32*r13) ) ; + Q.m[0][2] = (float)( deti*( r12*r23-r22*r13) ) ; + Q.m[0][3] = (float)( deti*(-r12*r23*v3+r12*v2*r33+r22*r13*v3 + -r22*v1*r33-r32*r13*v2+r32*v1*r23) ) ; + + Q.m[1][0] = (float)( deti*(-r21*r33+r31*r23) ) ; + Q.m[1][1] = (float)( deti*( r11*r33-r31*r13) ) ; + Q.m[1][2] = (float)( deti*(-r11*r23+r21*r13) ) ; + Q.m[1][3] = (float)( deti*( r11*r23*v3-r11*v2*r33-r21*r13*v3 + +r21*v1*r33+r31*r13*v2-r31*v1*r23) ) ; + + Q.m[2][0] = (float)( deti*( r21*r32-r31*r22) ) ; + Q.m[2][1] = (float)( deti*(-r11*r32+r31*r12) ) ; + Q.m[2][2] = (float)( deti*( r11*r22-r21*r12) ) ; + Q.m[2][3] = (float)( deti*(-r11*r22*v3+r11*r32*v2+r21*r12*v3 + -r21*r32*v1-r31*r12*v2+r31*r22*v1) ) ; + + Q.m[3][0] = Q.m[3][1] = Q.m[3][2] = 0.0l ; + Q.m[3][3] = (deti == 0.0l) ? 0.0l : 1.0l ; /* failure flag if deti == 0 */ + + return Q ; +} + +/*---------------------------------------------------------------------------*/ +/*! Input 9 floats and make an orthgonal mat44 out of them. + + Each row is normalized, then nifti_mat33_polar() is used to orthogonalize + them. If row #3 (r31,r32,r33) is input as zero, then it will be taken to + be the cross product of rows #1 and #2. + + This function can be used to create a rotation matrix for transforming + an oblique volume to anatomical coordinates. For this application: + - row #1 (r11,r12,r13) is the direction vector along the image i-axis + - row #2 (r21,r22,r23) is the direction vector along the image j-axis + - row #3 (r31,r32,r33) is the direction vector along the slice direction + (if available; otherwise enter it as 0's) + + The first 2 rows can be taken from the DICOM attribute (0020,0037) + "Image Orientation (Patient)". + + After forming the rotation matrix, the complete affine transformation from + (i,j,k) grid indexes to (x,y,z) spatial coordinates can be computed by + multiplying each column by the appropriate grid spacing: + - column #1 (R.m[0][0],R.m[1][0],R.m[2][0]) by delta-x + - column #2 (R.m[0][1],R.m[1][1],R.m[2][1]) by delta-y + - column #3 (R.m[0][2],R.m[1][2],R.m[2][2]) by delta-z + + and by then placing the center (x,y,z) coordinates of voxel (0,0,0) into + the column #4 (R.m[0][3],R.m[1][3],R.m[2][3]). + + \sa nifti_quatern_to_mat44, nifti_mat44_to_quatern, + nifti_mat44_to_orientation +*//*-------------------------------------------------------------------------*/ +mat44 nifti_make_orthog_mat44( float r11, float r12, float r13 , + float r21, float r22, float r23 , + float r31, float r32, float r33 ) +{ + mat44 R ; + mat33 Q , P ; + double val ; + + R.m[3][0] = R.m[3][1] = R.m[3][2] = 0.0l ; R.m[3][3] = 1.0l ; + + Q.m[0][0] = r11 ; Q.m[0][1] = r12 ; Q.m[0][2] = r13 ; /* load Q */ + Q.m[1][0] = r21 ; Q.m[1][1] = r22 ; Q.m[1][2] = r23 ; + Q.m[2][0] = r31 ; Q.m[2][1] = r32 ; Q.m[2][2] = r33 ; + + /* normalize row 1 */ + + val = Q.m[0][0]*Q.m[0][0] + Q.m[0][1]*Q.m[0][1] + Q.m[0][2]*Q.m[0][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[0][0] *= (float)val ; Q.m[0][1] *= (float)val ; Q.m[0][2] *= (float)val ; + } else { + Q.m[0][0] = 1.0l ; Q.m[0][1] = 0.0l ; Q.m[0][2] = 0.0l ; + } + + /* normalize row 2 */ + + val = Q.m[1][0]*Q.m[1][0] + Q.m[1][1]*Q.m[1][1] + Q.m[1][2]*Q.m[1][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[1][0] *= (float)val ; Q.m[1][1] *= (float)val ; Q.m[1][2] *= (float)val ; + } else { + Q.m[1][0] = 0.0l ; Q.m[1][1] = 1.0l ; Q.m[1][2] = 0.0l ; + } + + /* normalize row 3 */ + + val = Q.m[2][0]*Q.m[2][0] + Q.m[2][1]*Q.m[2][1] + Q.m[2][2]*Q.m[2][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[2][0] *= (float)val ; Q.m[2][1] *= (float)val ; Q.m[2][2] *= (float)val ; + } else { + Q.m[2][0] = Q.m[0][1]*Q.m[1][2] - Q.m[0][2]*Q.m[1][1] ; /* cross */ + Q.m[2][1] = Q.m[0][2]*Q.m[1][0] - Q.m[0][0]*Q.m[1][2] ; /* product */ + Q.m[2][2] = Q.m[0][0]*Q.m[1][1] - Q.m[0][1]*Q.m[1][0] ; + } + + P = nifti_mat33_polar(Q) ; /* P is orthog matrix closest to Q */ + + R.m[0][0] = P.m[0][0] ; R.m[0][1] = P.m[0][1] ; R.m[0][2] = P.m[0][2] ; + R.m[1][0] = P.m[1][0] ; R.m[1][1] = P.m[1][1] ; R.m[1][2] = P.m[1][2] ; + R.m[2][0] = P.m[2][0] ; R.m[2][1] = P.m[2][1] ; R.m[2][2] = P.m[2][2] ; + + R.m[0][3] = R.m[1][3] = R.m[2][3] = 0.0f ; return R ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the inverse of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +mat33 nifti_mat33_inverse( mat33 R ) /* inverse of 3x3 matrix */ +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33 , deti ; + mat33 Q ; + /* INPUT MATRIX: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */ + + deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + + if( deti != 0.0l ) deti = 1.0l / deti ; + + Q.m[0][0] = (float)( deti*( r22*r33-r32*r23) ) ; + Q.m[0][1] = (float)( deti*(-r12*r33+r32*r13) ) ; + Q.m[0][2] = (float)( deti*( r12*r23-r22*r13) ) ; + + Q.m[1][0] = (float)( deti*(-r21*r33+r31*r23) ) ; + Q.m[1][1] = (float)( deti*( r11*r33-r31*r13) ) ; + Q.m[1][2] = (float)( deti*(-r11*r23+r21*r13) ) ; + + Q.m[2][0] = (float)( deti*( r21*r32-r31*r22) ) ; + Q.m[2][1] = (float)( deti*(-r11*r32+r31*r12) ) ; + Q.m[2][2] = (float)( deti*( r11*r22-r21*r12) ) ; + + return Q ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the determinant of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +float nifti_mat33_determ( mat33 R ) /* determinant of 3x3 matrix */ +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33 ; + /* INPUT MATRIX: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */ + + return (float)(r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13) ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the max row norm of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +float nifti_mat33_rownorm( mat33 A ) /* max row norm of 3x3 matrix */ +{ + float r1,r2,r3 ; + + r1 = (float)( fabs(A.m[0][0])+fabs(A.m[0][1])+fabs(A.m[0][2]) ) ; + r2 = (float)( fabs(A.m[1][0])+fabs(A.m[1][1])+fabs(A.m[1][2]) ) ; + r3 = (float)( fabs(A.m[2][0])+fabs(A.m[2][1])+fabs(A.m[2][2]) ) ; + if( r1 < r2 ) r1 = r2 ; + if( r1 < r3 ) r1 = r3 ; + return r1 ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the max column norm of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +float nifti_mat33_colnorm( mat33 A ) /* max column norm of 3x3 matrix */ +{ + float r1,r2,r3 ; + + r1 = (float)( fabs(A.m[0][0])+fabs(A.m[1][0])+fabs(A.m[2][0]) ) ; + r2 = (float)( fabs(A.m[0][1])+fabs(A.m[1][1])+fabs(A.m[2][1]) ) ; + r3 = (float)( fabs(A.m[0][2])+fabs(A.m[1][2])+fabs(A.m[2][2]) ) ; + if( r1 < r2 ) r1 = r2 ; + if( r1 < r3 ) r1 = r3 ; + return r1 ; +} + +/*----------------------------------------------------------------------*/ +/*! multiply 2 3x3 matrices +*//*--------------------------------------------------------------------*/ +mat33 nifti_mat33_mul( mat33 A , mat33 B ) /* multiply 2 3x3 matrices */ +{ + mat33 C ; int i,j ; + for( i=0 ; i < 3 ; i++ ) + for( j=0 ; j < 3 ; j++ ) + C.m[i][j] = A.m[i][0] * B.m[0][j] + + A.m[i][1] * B.m[1][j] + + A.m[i][2] * B.m[2][j] ; + return C ; +} + +/*---------------------------------------------------------------------------*/ +/*! polar decomposition of a 3x3 matrix + + This finds the closest orthogonal matrix to input A + (in both the Frobenius and L2 norms). + + Algorithm is that from NJ Higham, SIAM J Sci Stat Comput, 7:1160-1174. +*//*-------------------------------------------------------------------------*/ +mat33 nifti_mat33_polar( mat33 A ) +{ + mat33 X , Y , Z ; + float alp,bet,gam,gmi , dif=1.0f ; + int k=0 ; + + X = A ; + + /* force matrix to be nonsingular */ + + gam = nifti_mat33_determ(X) ; + while( gam == 0.0 ){ /* perturb matrix */ + gam = (float)( 0.00001 * ( 0.001 + nifti_mat33_rownorm(X) ) ) ; + X.m[0][0] += gam ; X.m[1][1] += gam ; X.m[2][2] += gam ; + gam = nifti_mat33_determ(X) ; + } + + while(1){ + Y = nifti_mat33_inverse(X) ; + if( dif > 0.3 ){ /* far from convergence */ + alp = (float)( sqrt( nifti_mat33_rownorm(X) * nifti_mat33_colnorm(X) ) ) ; + bet = (float)( sqrt( nifti_mat33_rownorm(Y) * nifti_mat33_colnorm(Y) ) ) ; + gam = (float)( sqrt( bet / alp ) ) ; + gmi = (float)( 1.0 / gam ) ; + } else { + gam = gmi = 1.0f ; /* close to convergence */ + } + Z.m[0][0] = (float)( 0.5 * ( gam*X.m[0][0] + gmi*Y.m[0][0] ) ) ; + Z.m[0][1] = (float)( 0.5 * ( gam*X.m[0][1] + gmi*Y.m[1][0] ) ) ; + Z.m[0][2] = (float)( 0.5 * ( gam*X.m[0][2] + gmi*Y.m[2][0] ) ) ; + Z.m[1][0] = (float)( 0.5 * ( gam*X.m[1][0] + gmi*Y.m[0][1] ) ) ; + Z.m[1][1] = (float)( 0.5 * ( gam*X.m[1][1] + gmi*Y.m[1][1] ) ) ; + Z.m[1][2] = (float)( 0.5 * ( gam*X.m[1][2] + gmi*Y.m[2][1] ) ) ; + Z.m[2][0] = (float)( 0.5 * ( gam*X.m[2][0] + gmi*Y.m[0][2] ) ) ; + Z.m[2][1] = (float)( 0.5 * ( gam*X.m[2][1] + gmi*Y.m[1][2] ) ) ; + Z.m[2][2] = (float)( 0.5 * ( gam*X.m[2][2] + gmi*Y.m[2][2] ) ) ; + + dif = (float)( fabs(Z.m[0][0]-X.m[0][0])+fabs(Z.m[0][1]-X.m[0][1]) + +fabs(Z.m[0][2]-X.m[0][2])+fabs(Z.m[1][0]-X.m[1][0]) + +fabs(Z.m[1][1]-X.m[1][1])+fabs(Z.m[1][2]-X.m[1][2]) + +fabs(Z.m[2][0]-X.m[2][0])+fabs(Z.m[2][1]-X.m[2][1]) + +fabs(Z.m[2][2]-X.m[2][2]) ); + + k = k+1 ; + if( k > 100 || dif < 3.e-6 ) break ; /* convergence or exhaustion */ + X = Z ; + } + + return Z ; +} + +/*---------------------------------------------------------------------------*/ +/*! compute the (closest) orientation from a 4x4 ijk->xyz tranformation matrix + +
+   Input:  4x4 matrix that transforms (i,j,k) indexes to (x,y,z) coordinates,
+           where +x=Right, +y=Anterior, +z=Superior.
+           (Only the upper-left 3x3 corner of R is used herein.)
+   Output: 3 orientation codes that correspond to the closest "standard"
+           anatomical orientation of the (i,j,k) axes.
+   Method: Find which permutation of (x,y,z) has the smallest angle to the
+           (i,j,k) axes directions, which are the columns of the R matrix.
+   Errors: The codes returned will be zero.
+
+   For example, an axial volume might get return values of
+     *icod = NIFTI_R2L   (i axis is mostly Right to Left)
+     *jcod = NIFTI_P2A   (j axis is mostly Posterior to Anterior)
+     *kcod = NIFTI_I2S   (k axis is mostly Inferior to Superior)
+   
+ + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + + \see nifti_quatern_to_mat44, nifti_mat44_to_quatern, + nifti_make_orthog_mat44 +*//*-------------------------------------------------------------------------*/ +void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) +{ + float xi,xj,xk , yi,yj,yk , zi,zj,zk , val,detQ,detP ; + mat33 P , Q , M ; + int i,j,k=0,p,q,r , ibest,jbest,kbest,pbest,qbest,rbest ; + float vbest ; + + if( icod == NULL || jcod == NULL || kcod == NULL ) return ; /* bad */ + + *icod = *jcod = *kcod = 0 ; /* error returns, if sh*t happens */ + + /* load column vectors for each (i,j,k) direction from matrix */ + + /*-- i axis --*/ /*-- j axis --*/ /*-- k axis --*/ + + xi = R.m[0][0] ; xj = R.m[0][1] ; xk = R.m[0][2] ; + yi = R.m[1][0] ; yj = R.m[1][1] ; yk = R.m[1][2] ; + zi = R.m[2][0] ; zj = R.m[2][1] ; zk = R.m[2][2] ; + + /* normalize column vectors to get unit vectors along each ijk-axis */ + + /* normalize i axis */ + + val = (float)sqrt( xi*xi + yi*yi + zi*zi ) ; + if( val == 0.0 ) return ; /* stupid input */ + xi /= val ; yi /= val ; zi /= val ; + + /* normalize j axis */ + + val = (float)sqrt( xj*xj + yj*yj + zj*zj ) ; + if( val == 0.0 ) return ; /* stupid input */ + xj /= val ; yj /= val ; zj /= val ; + + /* orthogonalize j axis to i axis, if needed */ + + val = xi*xj + yi*yj + zi*zj ; /* dot product between i and j */ + if( fabs(val) > 1.e-4 ){ + xj -= val*xi ; yj -= val*yi ; zj -= val*zi ; + val = (float)sqrt( xj*xj + yj*yj + zj*zj ) ; /* must renormalize */ + if( val == 0.0 ) return ; /* j was parallel to i? */ + xj /= val ; yj /= val ; zj /= val ; + } + + /* normalize k axis; if it is zero, make it the cross product i x j */ + + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ){ xk = yi*zj-zi*yj; yk = zi*xj-zj*xi ; zk=xi*yj-yi*xj ; } + else { xk /= val ; yk /= val ; zk /= val ; } + + /* orthogonalize k to i */ + + val = xi*xk + yi*yk + zi*zk ; /* dot product between i and k */ + if( fabs(val) > 1.e-4 ){ + xk -= val*xi ; yk -= val*yi ; zk -= val*zi ; + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; + } + + /* orthogonalize k to j */ + + val = xj*xk + yj*yk + zj*zk ; /* dot product between j and k */ + if( fabs(val) > 1.e-4 ){ + xk -= val*xj ; yk -= val*yj ; zk -= val*zj ; + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; + } + + Q.m[0][0] = xi ; Q.m[0][1] = xj ; Q.m[0][2] = xk ; + Q.m[1][0] = yi ; Q.m[1][1] = yj ; Q.m[1][2] = yk ; + Q.m[2][0] = zi ; Q.m[2][1] = zj ; Q.m[2][2] = zk ; + + /* at this point, Q is the rotation matrix from the (i,j,k) to (x,y,z) axes */ + + detQ = nifti_mat33_determ( Q ) ; + if( detQ == 0.0 ) return ; /* shouldn't happen unless user is a DUFIS */ + + /* Build and test all possible +1/-1 coordinate permutation matrices P; + then find the P such that the rotation matrix M=PQ is closest to the + identity, in the sense of M having the smallest total rotation angle. */ + + /* Despite the formidable looking 6 nested loops, there are + only 3*3*3*2*2*2 = 216 passes, which will run very quickly. */ + + vbest = -666.0f ; ibest=pbest=qbest=rbest=1 ; jbest=2 ; kbest=3 ; + for( i=1 ; i <= 3 ; i++ ){ /* i = column number to use for row #1 */ + for( j=1 ; j <= 3 ; j++ ){ /* j = column number to use for row #2 */ + if( i == j ) continue ; + for( k=1 ; k <= 3 ; k++ ){ /* k = column number to use for row #3 */ + if( i == k || j == k ) continue ; + P.m[0][0] = P.m[0][1] = P.m[0][2] = + P.m[1][0] = P.m[1][1] = P.m[1][2] = + P.m[2][0] = P.m[2][1] = P.m[2][2] = 0.0f ; + for( p=-1 ; p <= 1 ; p+=2 ){ /* p,q,r are -1 or +1 */ + for( q=-1 ; q <= 1 ; q+=2 ){ /* and go into rows #1,2,3 */ + for( r=-1 ; r <= 1 ; r+=2 ){ + P.m[0][i-1] = p ; P.m[1][j-1] = q ; P.m[2][k-1] = r ; + detP = nifti_mat33_determ(P) ; /* sign of permutation */ + if( detP * detQ <= 0.0 ) continue ; /* doesn't match sign of Q */ + M = nifti_mat33_mul(P,Q) ; + + /* angle of M rotation = 2.0*acos(0.5*sqrt(1.0+trace(M))) */ + /* we want largest trace(M) == smallest angle == M nearest to I */ + + val = M.m[0][0] + M.m[1][1] + M.m[2][2] ; /* trace */ + if( val > vbest ){ + vbest = val ; + ibest = i ; jbest = j ; kbest = k ; + pbest = p ; qbest = q ; rbest = r ; + } + }}}}}} + + /* At this point ibest is 1 or 2 or 3; pbest is -1 or +1; etc. + + The matrix P that corresponds is the best permutation approximation + to Q-inverse; that is, P (approximately) takes (x,y,z) coordinates + to the (i,j,k) axes. + + For example, the first row of P (which contains pbest in column ibest) + determines the way the i axis points relative to the anatomical + (x,y,z) axes. If ibest is 2, then the i axis is along the y axis, + which is direction P2A (if pbest > 0) or A2P (if pbest < 0). + + So, using ibest and pbest, we can assign the output code for + the i axis. Mutatis mutandis for the j and k axes, of course. */ + + switch( ibest*pbest ){ + case 1: i = NIFTI_L2R ; break ; + case -1: i = NIFTI_R2L ; break ; + case 2: i = NIFTI_P2A ; break ; + case -2: i = NIFTI_A2P ; break ; + case 3: i = NIFTI_I2S ; break ; + case -3: i = NIFTI_S2I ; break ; + } + + switch( jbest*qbest ){ + case 1: j = NIFTI_L2R ; break ; + case -1: j = NIFTI_R2L ; break ; + case 2: j = NIFTI_P2A ; break ; + case -2: j = NIFTI_A2P ; break ; + case 3: j = NIFTI_I2S ; break ; + case -3: j = NIFTI_S2I ; break ; + } + + switch( kbest*rbest ){ + case 1: k = NIFTI_L2R ; break ; + case -1: k = NIFTI_R2L ; break ; + case 2: k = NIFTI_P2A ; break ; + case -2: k = NIFTI_A2P ; break ; + case 3: k = NIFTI_I2S ; break ; + case -3: k = NIFTI_S2I ; break ; + } + + *icod = i ; *jcod = j ; *kcod = k ; } + +/*---------------------------------------------------------------------------*/ +/* Routines to swap byte arrays in various ways: + - 2 at a time: ab -> ba [short] + - 4 at a time: abcd -> dcba [int, float] + - 8 at a time: abcdDCBA -> ABCDdcba [long long, double] + - 16 at a time: abcdefghHGFEDCBA -> ABCDEFGHhgfedcba [long double] +-----------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------*/ +/*! swap each byte pair from the given list of n pairs + * + * Due to alignment of structures at some architectures (e.g. on ARM), + * stick to char varaibles. + * Fixes http://bugs.debian.org/446893 Yaroslav + * +*//*--------------------------------------------------------------------*/ +void nifti_swap_2bytes( size_t n , void *ar ) /* 2 bytes at a time */ +{ + size_t ii ; + unsigned char * cp1 = (unsigned char *)ar, * cp2 ; + unsigned char tval; + + for( ii=0 ; ii < n ; ii++ ){ + cp2 = cp1 + 1; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp1 += 2; + } + } + +/*----------------------------------------------------------------------*/ +/*! swap 4 bytes at a time from the given list of n sets of 4 bytes +*//*--------------------------------------------------------------------*/ +void nifti_swap_4bytes( size_t n , void *ar ) /* 4 bytes at a time */ +{ + size_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+3; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp1++; cp2--; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp0 += 4; + } + } + +/*----------------------------------------------------------------------*/ +/*! swap 8 bytes at a time from the given list of n sets of 8 bytes + * + * perhaps use this style for the general Nbytes, as Yaroslav suggests +*//*--------------------------------------------------------------------*/ +void nifti_swap_8bytes( size_t n , void *ar ) /* 8 bytes at a time */ +{ + size_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+7; + while ( cp2 > cp1 ) /* unroll? */ + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += 8; + } + } + +/*----------------------------------------------------------------------*/ +/*! swap 16 bytes at a time from the given list of n sets of 16 bytes +*//*--------------------------------------------------------------------*/ +void nifti_swap_16bytes( size_t n , void *ar ) /* 16 bytes at a time */ +{ + size_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+15; + while ( cp2 > cp1 ) + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += 16; + } + } + +#if 0 /* not important: save for version update 6 Jul 2010 [rickr] */ + +/*----------------------------------------------------------------------*/ +/*! generic: swap siz bytes at a time from the given list of n sets +*//*--------------------------------------------------------------------*/ +void nifti_swap_bytes( size_t n , int siz , void *ar ) +{ + size_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+(siz-1); + while ( cp2 > cp1 ) + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += siz; + } + return ; +} +#endif + +/*---------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------*/ +/*! based on siz, call the appropriate nifti_swap_Nbytes() function +*//*--------------------------------------------------------------------*/ +void nifti_swap_Nbytes( size_t n , int siz , void *ar ) /* subsuming case */ +{ + switch( siz ){ + case 2: nifti_swap_2bytes ( n , ar ) ; break ; + case 4: nifti_swap_4bytes ( n , ar ) ; break ; + case 8: nifti_swap_8bytes ( n , ar ) ; break ; + case 16: nifti_swap_16bytes( n , ar ) ; break ; + default: /* nifti_swap_bytes ( n , siz, ar ) ; */ + Rc_fprintf_stderr("** NIfTI: cannot swap in %d byte blocks\n", siz); + break ; + } + } + + +/*-------------------------------------------------------------------------*/ +/*! Byte swap NIFTI-1 file header in various places and ways. + + If is_nifti, swap all (even UNUSED) fields of NIfTI header. + Else, swap as a nifti_analyze75 struct. +*//*---------------------------------------------------------------------- */ +void swap_nifti_header( struct nifti_1_header *h , int is_nifti ) +{ + + /* if ANALYZE, swap as such and return */ + if( ! is_nifti ) { + nifti_swap_as_analyze((nifti_analyze75 *)h); + return; + } + + /* otherwise, swap all NIFTI fields */ + + nifti_swap_4bytes(1, &h->sizeof_hdr); + nifti_swap_4bytes(1, &h->extents); + nifti_swap_2bytes(1, &h->session_error); + + nifti_swap_2bytes(8, h->dim); + nifti_swap_4bytes(1, &h->intent_p1); + nifti_swap_4bytes(1, &h->intent_p2); + nifti_swap_4bytes(1, &h->intent_p3); + + nifti_swap_2bytes(1, &h->intent_code); + nifti_swap_2bytes(1, &h->datatype); + nifti_swap_2bytes(1, &h->bitpix); + nifti_swap_2bytes(1, &h->slice_start); + + nifti_swap_4bytes(8, h->pixdim); + + nifti_swap_4bytes(1, &h->vox_offset); + nifti_swap_4bytes(1, &h->scl_slope); + nifti_swap_4bytes(1, &h->scl_inter); + nifti_swap_2bytes(1, &h->slice_end); + + nifti_swap_4bytes(1, &h->cal_max); + nifti_swap_4bytes(1, &h->cal_min); + nifti_swap_4bytes(1, &h->slice_duration); + nifti_swap_4bytes(1, &h->toffset); + nifti_swap_4bytes(1, &h->glmax); + nifti_swap_4bytes(1, &h->glmin); + + nifti_swap_2bytes(1, &h->qform_code); + nifti_swap_2bytes(1, &h->sform_code); + + nifti_swap_4bytes(1, &h->quatern_b); + nifti_swap_4bytes(1, &h->quatern_c); + nifti_swap_4bytes(1, &h->quatern_d); + nifti_swap_4bytes(1, &h->qoffset_x); + nifti_swap_4bytes(1, &h->qoffset_y); + nifti_swap_4bytes(1, &h->qoffset_z); + + nifti_swap_4bytes(4, h->srow_x); + nifti_swap_4bytes(4, h->srow_y); + nifti_swap_4bytes(4, h->srow_z); +} + +/*-------------------------------------------------------------------------*/ +/*! Byte swap as an ANALYZE 7.5 header + * + * return non-zero on failure +*//*---------------------------------------------------------------------- */ +int nifti_swap_as_analyze( nifti_analyze75 * h ) +{ + if( !h ) return 1; + + nifti_swap_4bytes(1, &h->sizeof_hdr); + nifti_swap_4bytes(1, &h->extents); + nifti_swap_2bytes(1, &h->session_error); + + nifti_swap_2bytes(8, h->dim); + nifti_swap_2bytes(1, &h->unused8); + nifti_swap_2bytes(1, &h->unused9); + nifti_swap_2bytes(1, &h->unused10); + nifti_swap_2bytes(1, &h->unused11); + nifti_swap_2bytes(1, &h->unused12); + nifti_swap_2bytes(1, &h->unused13); + nifti_swap_2bytes(1, &h->unused14); + + nifti_swap_2bytes(1, &h->datatype); + nifti_swap_2bytes(1, &h->bitpix); + nifti_swap_2bytes(1, &h->dim_un0); + + nifti_swap_4bytes(8, h->pixdim); + + nifti_swap_4bytes(1, &h->vox_offset); + nifti_swap_4bytes(1, &h->funused1); + nifti_swap_4bytes(1, &h->funused2); + nifti_swap_4bytes(1, &h->funused3); + + nifti_swap_4bytes(1, &h->cal_max); + nifti_swap_4bytes(1, &h->cal_min); + nifti_swap_4bytes(1, &h->compressed); + nifti_swap_4bytes(1, &h->verified); + + nifti_swap_4bytes(1, &h->glmax); + nifti_swap_4bytes(1, &h->glmin); + + nifti_swap_4bytes(1, &h->views); + nifti_swap_4bytes(1, &h->vols_added); + nifti_swap_4bytes(1, &h->start_field); + nifti_swap_4bytes(1, &h->field_skip); + + nifti_swap_4bytes(1, &h->omax); + nifti_swap_4bytes(1, &h->omin); + nifti_swap_4bytes(1, &h->smax); + nifti_swap_4bytes(1, &h->smin); + + return 0; +} + +/*-------------------------------------------------------------------------*/ +/*! OLD VERSION of swap_nifti_header (left for undo/compare operations) + + Byte swap NIFTI-1 file header in various places and ways. + + If is_nifti is nonzero, will also swap the NIFTI-specific + components of the header; otherwise, only the components + common to NIFTI and ANALYZE will be swapped. +*//*---------------------------------------------------------------------- */ +void old_swap_nifti_header( struct nifti_1_header *h , int is_nifti ) +{ + /* this stuff is always present, for ANALYZE and NIFTI */ + + swap_4(h->sizeof_hdr) ; + nifti_swap_2bytes( 8 , h->dim ) ; + nifti_swap_4bytes( 8 , h->pixdim ) ; + + swap_2(h->datatype) ; + swap_2(h->bitpix) ; + + swap_4(h->vox_offset); swap_4(h->cal_max); swap_4(h->cal_min); + + /* this stuff is NIFTI specific */ + + if( is_nifti ){ + swap_4(h->intent_p1); swap_4(h->intent_p2); swap_4(h->intent_p3); + swap_2(h->intent_code); + + swap_2(h->slice_start); swap_2(h->slice_end); + swap_4(h->scl_slope); swap_4(h->scl_inter); + swap_4(h->slice_duration); swap_4(h->toffset); + + swap_2(h->qform_code); swap_2(h->sform_code); + swap_4(h->quatern_b); swap_4(h->quatern_c); swap_4(h->quatern_d); + swap_4(h->qoffset_x); swap_4(h->qoffset_y); swap_4(h->qoffset_z); + nifti_swap_4bytes(4,h->srow_x); + nifti_swap_4bytes(4,h->srow_y); + nifti_swap_4bytes(4,h->srow_z); + } + } + +#endif /* RNIFTI_NIFTILIB_DEDUPLICATE */ + +#define USE_STAT +#ifdef USE_STAT +/*---------------------------------------------------------------------------*/ +/* Return the file length (0 if file not found or has no contents). + This is a Unix-specific function, since it uses stat(). +-----------------------------------------------------------------------------*/ +#include +#include + +/*---------------------------------------------------------------------------*/ +/*! return the size of a file, in bytes + + \return size of file on success, -1 on error or no file + + changed to return int, -1 means no file or error 20 Dec 2004 [rickr] +*//*-------------------------------------------------------------------------*/ +int nifti_get_filesize( const char *pathname ) +{ + struct stat buf ; int ii ; + + if( pathname == NULL || *pathname == '\0' ) return -1 ; + ii = stat( pathname , &buf ); if( ii != 0 ) return -1 ; + return (unsigned int)buf.st_size ; +} + +#else /*---------- non-Unix version of the above, less efficient -----------*/ + +int nifti_get_filesize( const char *pathname ) +{ + znzFile fp ; int len ; + + if( pathname == NULL || *pathname == '\0' ) return -1 ; + fp = znzopen(pathname,"rb",0); if( znz_isnull(fp) ) return -1 ; + znzseek(fp,0L,SEEK_END) ; len = znztell(fp) ; + znzclose(fp) ; return len ; +} + +#endif /* USE_STAT */ + +/*----------------------------------------------------------------------*/ +/*! return the total volume size, in bytes + + This is computed as nvox * nbyper. +*//*--------------------------------------------------------------------*/ +size_t nifti_get_volsize(const nifti_image *nim) +{ + return (size_t)(nim->nbyper) * (size_t)(nim->nvox) ; /* total bytes */ +} + + +/*--------------------------------------------------------------------------*/ +/* Support functions for filenames in read and write + - allows for gzipped files +*/ + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +/*----------------------------------------------------------------------*/ +/*! simple check for file existence + + \return 1 on existence, 0 otherwise +*//*--------------------------------------------------------------------*/ +int nifti_fileexists(const char* fname) +{ + znzFile fp; + fp = znzopen( fname , "rb" , nifti_is_gzfile(fname) ) ; + if( !znz_isnull(fp) ) { znzclose(fp); return 1; } + return 0; /* fp is NULL */ +} + +/*----------------------------------------------------------------------*/ +/*! return whether the filename is valid + + Note: uppercase extensions are now valid. 27 Apr 2009 [rickr] + + The name is considered valid if the file basename has length greater than + zero, AND one of the valid nifti extensions is provided. + fname input | return | + =============================== + "myimage" | 0 | + "myimage.tif" | 0 | + "myimage.tif.gz" | 0 | + "myimage.nii" | 1 | + ".nii" | 0 | + ".myhiddenimage" | 0 | + ".myhiddenimage.nii" | 1 | +*//*--------------------------------------------------------------------*/ +int nifti_is_complete_filename(const char* fname) +{ + const char * ext; + + /* check input file(s) for sanity */ + if( fname == NULL || *fname == '\0' ){ + if ( g_opts.debug > 1 ) + Rc_fprintf_stderr("-- empty filename in nifti_validfilename()\n"); + return 0; + } + + ext = nifti_find_file_extension(fname); + if ( ext == NULL ) { /*Invalid extension given */ + if ( g_opts.debug > 0 ) + Rc_fprintf_stderr("-- no nifti valid extension for filename '%s'\n", fname); + return 0; + } + + if ( ext && ext == fname ) { /* then no filename prefix */ + if ( g_opts.debug > 0 ) + Rc_fprintf_stderr("-- no prefix for filename '%s'\n", fname); + return 0; + } + return 1; +} + +/*----------------------------------------------------------------------*/ +/*! return whether the filename is valid + + Allow uppercase extensions as valid. 27 Apr 2009 [rickr] + Any .gz extension case must match the base extension case. + + The name is considered valid if its length is positive, excluding + any nifti filename extension. + fname input | return | result of nifti_makebasename + ==================================================================== + "myimage" | 1 | "myimage" + "myimage.tif" | 1 | "myimage.tif" + "myimage.tif.gz" | 1 | "myimage.tif" + "myimage.nii" | 1 | "myimage" + ".nii" | 0 | + ".myhiddenimage" | 1 | ".myhiddenimage" + ".myhiddenimage.nii | 1 | ".myhiddenimage" +*//*--------------------------------------------------------------------*/ +int nifti_validfilename(const char* fname) +{ + const char * ext; + + /* check input file(s) for sanity */ + if( fname == NULL || *fname == '\0' ){ + if ( g_opts.debug > 1 ) + Rc_fprintf_stderr("-- empty filename in nifti_validfilename()\n"); + return 0; + } + + ext = nifti_find_file_extension(fname); + + if ( ext && ext == fname ) { /* then no filename prefix */ + if ( g_opts.debug > 0 ) + Rc_fprintf_stderr("-- no prefix for filename '%s'\n", fname); + return 0; + } + + return 1; +} + +/*----------------------------------------------------------------------*/ +/*! check the end of the filename for a valid nifti extension + + Valid extensions are currently .nii, .hdr, .img, .nia, + or any of them followed by .gz. Note that '.' is part of + the extension. + + Uppercase extensions are also valid, but not mixed case. + + \return a pointer to the extension substring within the original + function input parameter name, or NULL if not found. + \caution Note that if the input parameter is is immutabale + (i.e. a const char *) then this function performs an + implicit casting away of the mutability constraint and + the return parameter will appear as a mutable + even though it is part of the immuttable string. +*//*--------------------------------------------------------------------*/ +char * nifti_find_file_extension( const char * name ) +{ + const char * ext; + char extcopy[8]; + int len; + char extnii[8] = ".nii"; /* modifiable, for possible uppercase */ + char exthdr[8] = ".hdr"; /* (leave space for .gz) */ + char extimg[8] = ".img"; + char extnia[8] = ".nia"; + char extgz[4] = ".gz"; + char * elist[4] = { NULL, NULL, NULL, NULL}; + + /* stupid compiler... */ + elist[0] = extnii; elist[1] = exthdr; elist[2] = extimg; elist[3] = extnia; + + if ( ! name ) return NULL; + + len = (int)strlen(name); + if ( len < 4 ) return NULL; + + ext = name + len - 4; + + /* make manipulation copy, and possibly convert to lowercase */ + strcpy(extcopy, ext); + if( g_opts.allow_upper_fext ) make_lowercase(extcopy); + + /* if it look like a basic extension, fail or return it */ + if( compare_strlist(extcopy, elist, 4) >= 0 ) { + if( is_mixedcase(ext) ) { + Rc_fprintf_stderr("** mixed case extension '%s' is not valid\n", ext); + return NULL; + } + else return (char *)ext; /* Cast away the constness of the input parameter */ + } + +#ifdef HAVE_ZLIB + if ( len < 7 ) return NULL; + + ext = name + len - 7; + + /* make manipulation copy, and possibly convert to lowercase */ + strcpy(extcopy, ext); + if( g_opts.allow_upper_fext ) make_lowercase(extcopy); + + /* go after .gz extensions using the modifiable strings */ + strcat(elist[0], extgz); strcat(elist[1], extgz); strcat(elist[2], extgz); + + if( compare_strlist(extcopy, elist, 3) >= 0 ) { + if( is_mixedcase(ext) ) { + Rc_fprintf_stderr("** mixed case extension '%s' is not valid\n", ext); + return NULL; + } + else return (char *)ext; /* Cast away the constness of the input parameter */ + } + +#endif + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** find_file_ext: failed for name '%s'\n", name); + + return NULL; +} + +/*----------------------------------------------------------------------*/ +/*! return whether the filename ends in ".gz" +*//*--------------------------------------------------------------------*/ +int nifti_is_gzfile(const char* fname) +{ + /* return true if the filename ends with .gz */ + if (fname == NULL) { return 0; } +#ifdef HAVE_ZLIB + { /* just so len doesn't generate compile warning */ + int len; + len = (int)strlen(fname); + if (len < 3) return 0; /* so we don't search before the name */ + if (fileext_compare(fname + strlen(fname) - 3,".gz")==0) { return 1; } + } +#endif + return 0; +} + +/*----------------------------------------------------------------------*/ +/*! return whether the given library was compiled with HAVE_ZLIB set +*//*--------------------------------------------------------------------*/ +int nifti_compiled_with_zlib(void) +{ +#ifdef HAVE_ZLIB + return 1; +#else + return 0; +#endif +} + +/*----------------------------------------------------------------------*/ +/*! duplicate the filename, while clearing any extension + + This allocates memory for basename which should eventually be freed. +*//*--------------------------------------------------------------------*/ +char * nifti_makebasename(const char* fname) +{ + char *basename; + const char *ext; + + basename=nifti_strdup(fname); + + ext = nifti_find_file_extension(basename); + if ( ext ) + { + basename[strlen(basename)-strlen(ext)] = '\0'; /* clear out extension */ + } + + return basename; /* in either case */ +} + +/*----------------------------------------------------------------------*/ +/*! set nifti's global debug level, for status reporting + + - 0 : quiet, nothing is printed to the terminal, but errors + - 1 : normal execution (the default) + - 2, 3 : more details +*//*--------------------------------------------------------------------*/ +void nifti_set_debug_level( int level ) +{ + g_opts.debug = level; +} + +/*----------------------------------------------------------------------*/ +/*! set nifti's global skip_blank_ext flag 5 Sep 2006 [rickr] + + explicitly set to 0 or 1 +*//*--------------------------------------------------------------------*/ +void nifti_set_skip_blank_ext( int skip ) +{ + g_opts.skip_blank_ext = skip ? 1 : 0; +} + +/*----------------------------------------------------------------------*/ +/*! set nifti's global allow_upper_fext flag 28 Apr 2009 [rickr] + + explicitly set to 0 or 1 +*//*--------------------------------------------------------------------*/ +void nifti_set_allow_upper_fext( int allow ) +{ + g_opts.allow_upper_fext = allow ? 1 : 0; +} + +/*----------------------------------------------------------------------*/ +/*! check current directory for existing header file + + \return filename of header on success and NULL if no appropriate file + could be found + + If fname has an uppercase extension, check for uppercase files. + + NB: it allocates memory for hdrname which should be freed + when no longer required +*//*-------------------------------------------------------------------*/ +char * nifti_findhdrname(const char* fname) +{ + char *basename, *hdrname; + const char *ext; + char elist[2][5] = { ".hdr", ".nii" }; + char extzip[4] = ".gz"; + int efirst = 1; /* init to .nii extension */ + int eisupper = 0; /* init to lowercase extensions */ + + /**- check input file(s) for sanity */ + if( !nifti_validfilename(fname) ) return NULL; + + basename = nifti_makebasename(fname); + if( !basename ) return NULL; /* only on string alloc failure */ + + /**- return filename if it has a valid extension and exists + (except if it is an .img file (and maybe .gz)) */ + ext = nifti_find_file_extension(fname); + + if( ext ) eisupper = is_uppercase(ext); /* do we look for uppercase? */ + + /* if the file exists and is a valid header name (not .img), return it */ + if ( ext && nifti_fileexists(fname) ) { + /* allow for uppercase extension */ + if ( fileext_n_compare(ext,".img",4) != 0 ){ + hdrname = nifti_strdup(fname); + free(basename); + return hdrname; + } else + efirst = 0; /* note for below */ + } + + /* So the requested name is a basename, contains .img, or does not exist. */ + /* In any case, use basename. */ + + /**- if .img, look for .hdr, .hdr.gz, .nii, .nii.gz, in that order */ + /**- else, look for .nii, .nii.gz, .hdr, .hdr.gz, in that order */ + + /* if we get more extension choices, this could be a loop */ + + /* note: efirst is 0 in the case of ".img" */ + + /* if the user passed an uppercase entension (.IMG), search for uppercase */ + if( eisupper ) { + make_uppercase(elist[0]); + make_uppercase(elist[1]); + make_uppercase(extzip); + } + + hdrname = (char *)calloc(sizeof(char),strlen(basename)+8); + if( !hdrname ){ + Rc_fprintf_stderr("** nifti_findhdrname: failed to alloc hdrname\n"); + free(basename); + return NULL; + } + + strcpy(hdrname,basename); + strcat(hdrname,elist[efirst]); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#ifdef HAVE_ZLIB + strcat(hdrname,extzip); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#endif + + /* okay, try the other possibility */ + + efirst = 1 - efirst; + + strcpy(hdrname,basename); + strcat(hdrname,elist[efirst]); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#ifdef HAVE_ZLIB + strcat(hdrname,extzip); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#endif + + /**- if nothing has been found, return NULL */ + free(basename); + free(hdrname); + return NULL; +} + + +/*------------------------------------------------------------------------*/ +/*! check current directory for existing image file + + \param fname filename to check for + \nifti_type nifti_type for dataset - this determines whether to + first check for ".nii" or ".img" (since both may exist) + + \return filename of data/img file on success and NULL if no appropriate + file could be found + + If fname has a valid, uppercase extension, apply all extensions as + uppercase. + + NB: it allocates memory for the image filename, which should be freed + when no longer required +*//*---------------------------------------------------------------------*/ +char * nifti_findimgname(const char* fname , int nifti_type) +{ + /* store all extensions as strings, in case we need to go uppercase */ + char *basename, *imgname, elist[2][5] = { ".nii", ".img" }; + char extzip[4] = ".gz"; + char extnia[5] = ".nia"; + const char *ext; + int first; /* first extension to use */ + + /* check input file(s) for sanity */ + if( !nifti_validfilename(fname) ) return NULL; + + basename = nifti_makebasename(fname); + imgname = (char *)calloc(sizeof(char),strlen(basename)+8); + if( !imgname ){ + Rc_fprintf_stderr("** nifti_findimgname: failed to alloc imgname\n"); + free(basename); + return NULL; + } + + /* if we are looking for uppercase, apply the fact now */ + ext = nifti_find_file_extension(fname); + if( ext && is_uppercase(ext) ) { + make_uppercase(elist[0]); + make_uppercase(elist[1]); + make_uppercase(extzip); + make_uppercase(extnia); + } + + /* only valid extension for ASCII type is .nia, handle first */ + if( nifti_type == NIFTI_FTYPE_ASCII ){ + strcpy(imgname,basename); + strcat(imgname,extnia); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } + + } else { + + /**- test for .nii and .img (don't assume input type from image type) */ + /**- if nifti_type = 1, check for .nii first, else .img first */ + + /* if we get 3 or more extensions, can make a loop here... */ + + if (nifti_type == NIFTI_FTYPE_NIFTI1_1) first = 0; /* should match .nii */ + else first = 1; /* should match .img */ + + strcpy(imgname,basename); + strcat(imgname,elist[first]); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#ifdef HAVE_ZLIB /* then also check for .gz */ + strcat(imgname,extzip); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#endif + + /* failed to find image file with expected extension, try the other */ + + strcpy(imgname,basename); + strcat(imgname,elist[1-first]); /* can do this with only 2 choices */ + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#ifdef HAVE_ZLIB /* then also check for .gz */ + strcat(imgname,extzip); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#endif + } + + /**- if nothing has been found, return NULL */ + free(basename); + free(imgname); + return NULL; +} + + +/*----------------------------------------------------------------------*/ +/*! creates a filename for storing the header, based on nifti_type + + \param prefix - this will be copied before the suffix is added + \param nifti_type - determines the extension, unless one is in prefix + \param check - check for existence (fail condition) + \param comp - add .gz for compressed name + + Note that if prefix provides a file suffix, nifti_type is not used. + + NB: this allocates memory which should be freed + + \sa nifti_set_filenames +*//*-------------------------------------------------------------------*/ +char * nifti_makehdrname(const char * prefix, int nifti_type, int check, + int comp) +{ + char * iname; + const char * ext; + char extnii[5] = ".nii"; /* modifiable, for possible uppercase */ + char exthdr[5] = ".hdr"; + char extimg[5] = ".img"; + char extnia[5] = ".nia"; + char extgz[5] = ".gz"; + + if( !nifti_validfilename(prefix) ) return NULL; + + /* add space for extension, optional ".gz", and null char */ + iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + if( !iname ){ Rc_fprintf_stderr("** small malloc failure!\n"); return NULL; } + strcpy(iname, prefix); + + /* use any valid extension */ + if( (ext = nifti_find_file_extension(iname)) != NULL ){ + /* if uppercase, convert all extensions */ + if( is_uppercase(ext) ) { + make_uppercase(extnii); + make_uppercase(exthdr); + make_uppercase(extimg); + make_uppercase(extnia); + make_uppercase(extgz); + } + + if( strncmp(ext,extimg,4) == 0 ) + { + memcpy(&(iname[strlen(iname)-strlen(ext)]),exthdr,4); /* then convert img name to hdr */ + } + } + /* otherwise, make one up */ + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); + else strcat(iname, exthdr); + +#ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ + if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); +#endif + + /* check for existence failure */ + if( check && nifti_fileexists(iname) ){ + Rc_fprintf_stderr("** failure: header file '%s' already exists\n",iname); + free(iname); + return NULL; + } + + if(g_opts.debug > 2) Rc_fprintf_stderr("+d made header filename '%s'\n", iname); + + return iname; +} + + +/*----------------------------------------------------------------------*/ +/*! creates a filename for storing the image, based on nifti_type + + \param prefix - this will be copied before the suffix is added + \param nifti_type - determines the extension, unless provided by prefix + \param check - check for existence (fail condition) + \param comp - add .gz for compressed name + + Note that if prefix provides a file suffix, nifti_type is not used. + + NB: it allocates memory which should be freed + + \sa nifti_set_filenames +*//*-------------------------------------------------------------------*/ +char * nifti_makeimgname(const char * prefix, int nifti_type, int check, + int comp) +{ + char * iname; + const char * ext; + char extnii[5] = ".nii"; /* modifiable, for possible uppercase */ + char exthdr[5] = ".hdr"; + char extimg[5] = ".img"; + char extnia[5] = ".nia"; + char extgz[5] = ".gz"; + + if( !nifti_validfilename(prefix) ) return NULL; + + /* add space for extension, optional ".gz", and null char */ + iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + if( !iname ){ Rc_fprintf_stderr("** small malloc failure!\n"); return NULL; } + strcpy(iname, prefix); + + /* use any valid extension */ + if( (ext = nifti_find_file_extension(iname)) != NULL ){ + /* if uppercase, convert all extensions */ + if( is_uppercase(ext) ) { + make_uppercase(extnii); + make_uppercase(exthdr); + make_uppercase(extimg); + make_uppercase(extnia); + make_uppercase(extgz); + } + + if( strncmp(ext,exthdr,4) == 0 ) + { + memcpy(&(iname[strlen(iname)-strlen(ext)]),extimg,4); /* then convert hdr name to img */ + } + } + /* otherwise, make one up */ + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); + else strcat(iname, extimg); + +#ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ + if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); +#endif + + /* check for existence failure */ + if( check && nifti_fileexists(iname) ){ + Rc_fprintf_stderr("** failure: image file '%s' already exists\n",iname); + free(iname); + return NULL; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("+d made image filename '%s'\n",iname); + + return iname; +} +#endif + +/*----------------------------------------------------------------------*/ +/*! create and set new filenames, based on prefix and image type + + \param nim pointer to nifti_image in which to set filenames + \param prefix (required) prefix for output filenames + \param check check for previous existence of filename + (existence is an error condition) + \param set_byte_order flag to set nim->byteorder here + (this is probably a logical place to do so) + + \return 0 on successful update + + \warning this will free() any existing names and create new ones + + \sa nifti_makeimgname, nifti_makehdrname, nifti_type_and_names_match +*//*--------------------------------------------------------------------*/ +int nifti_set_filenames( nifti_image * nim, const char * prefix, int check, + int set_byte_order ) +{ + int comp = nifti_is_gzfile(prefix); + + if( !nim || !prefix ){ + Rc_fprintf_stderr("** nifti_set_filenames, bad params %p, %p\n", + (void *)nim,prefix); + return -1; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d modifying output filenames using prefix %s\n", prefix); + + if( nim->fname ) free(nim->fname); + if( nim->iname ) free(nim->iname); + nim->fname = nifti_makehdrname(prefix, nim->nifti_type, check, comp); + nim->iname = nifti_makeimgname(prefix, nim->nifti_type, check, comp); + if( !nim->fname || !nim->iname ){ + LNI_FERR("nifti_set_filename","failed to set prefix for",prefix); + return -1; + } + + if( set_byte_order ) nim->byteorder = nifti_short_order() ; + + if( nifti_set_type_from_names(nim) < 0 ) + return -1; + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d have new filenames %s and %s\n",nim->fname,nim->iname); + + return 0; +} + + +/*--------------------------------------------------------------------------*/ +/*! check whether nifti_type matches fname and iname for the nifti_image + + - if type 0 or 2, expect .hdr/.img pair + - if type 1, expect .nii (and names must match) + + \param nim given nifti_image + \param show_warn if set, print a warning message for any mis-match + + \return + - 1 if the values seem to match + - 0 if there is a mis-match + - -1 if there is not sufficient information to create file(s) + + \sa NIFTI_FTYPE_* codes in nifti1_io.h + \sa nifti_set_type_from_names, is_valid_nifti_type +*//*------------------------------------------------------------------------*/ +int nifti_type_and_names_match( nifti_image * nim, int show_warn ) +{ + char func[] = "nifti_type_and_names_match"; + const char * ext_h; /* header filename extension */ + const char * ext_i; /* image filename extension */ + int errs = 0; /* error counter */ + + /* sanity checks */ + if( !nim ){ + if( show_warn ) Rc_fprintf_stderr("** %s: missing nifti_image\n", func); + return -1; + } + if( !nim->fname ){ + if( show_warn ) Rc_fprintf_stderr("** %s: missing header filename\n", func); + errs++; + } + if( !nim->iname ){ + if( show_warn ) Rc_fprintf_stderr("** %s: missing image filename\n", func); + errs++; + } + if( !is_valid_nifti_type(nim->nifti_type) ){ + if( show_warn ) + Rc_fprintf_stderr("** %s: bad nifti_type %d\n", func, nim->nifti_type); + errs++; + } + + if( errs ) return -1; /* then do not proceed */ + + /* get pointers to extensions */ + ext_h = nifti_find_file_extension( nim->fname ); + ext_i = nifti_find_file_extension( nim->iname ); + + /* check for filename extensions */ + if( !ext_h ){ + if( show_warn ) + Rc_fprintf_stderr("-d missing NIFTI extension in header filename, %s\n", + nim->fname); + errs++; + } + if( !ext_i ){ + if( show_warn ) + Rc_fprintf_stderr("-d missing NIFTI extension in image filename, %s\n", + nim->iname); + errs++; + } + + if( errs ) return 0; /* do not proceed, but this is just a mis-match */ + + /* general tests */ + if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ){ /* .nii */ + if( fileext_n_compare(ext_h,".nii",4) ) { + if( show_warn ) + Rc_fprintf_stderr( + "-d NIFTI_FTYPE 1, but no .nii extension in header filename, %s\n", + nim->fname); + errs++; + } + if( fileext_n_compare(ext_i,".nii",4) ) { + if( show_warn ) + Rc_fprintf_stderr( + "-d NIFTI_FTYPE 1, but no .nii extension in image filename, %s\n", + nim->iname); + errs++; + } + if( strcmp(nim->fname, nim->iname) != 0 ){ + if( show_warn ) + Rc_fprintf_stderr( + "-d NIFTI_FTYPE 1, but header and image filenames differ: %s, %s\n", + nim->fname, nim->iname); + errs++; + } + } + else if( (nim->nifti_type == NIFTI_FTYPE_NIFTI1_2) || /* .hdr/.img */ + (nim->nifti_type == NIFTI_FTYPE_ANALYZE) ) + { + if( fileext_n_compare(ext_h,".hdr",4) != 0 ){ + if( show_warn ) + Rc_fprintf_stderr("-d no '.hdr' extension, but NIFTI type is %d, %s\n", + nim->nifti_type, nim->fname); + errs++; + } + if( fileext_n_compare(ext_i,".img",4) != 0 ){ + if( show_warn ) + Rc_fprintf_stderr("-d no '.img' extension, but NIFTI type is %d, %s\n", + nim->nifti_type, nim->iname); + errs++; + } + } + /* ignore any other nifti_type */ + + if( errs ) return 0; /* types do not match */ + + return 1; +} + +/* like strcmp, but also check against capitalization of known_ext + * (test as local string, with max length 7) */ +static int fileext_compare(const char * test_ext, const char * known_ext) +{ + char caps[8] = ""; + size_t c,len; + /* if equal, don't need to check case (store to avoid multiple calls) */ + const int cmp = strcmp(test_ext, known_ext); + if( cmp == 0 ) return cmp; + + /* if anything odd, use default */ + if( !test_ext || !known_ext ) return cmp; + + len = strlen(known_ext); + if( len > 7 ) return cmp; + + /* if here, strings are different but need to check upper-case */ + + for(c = 0; c < len; c++ ) caps[c] = toupper((int) known_ext[c]); + caps[c] = '\0'; + + return strcmp(test_ext, caps); +} + +/* like strncmp, but also check against capitalization of known_ext + * (test as local string, with max length 7) */ +static int fileext_n_compare(const char * test_ext, + const char * known_ext, size_t maxlen) +{ + char caps[8] = ""; + size_t c,len; + /* if equal, don't need to check case (store to avoid multiple calls) */ + const int cmp = strncmp(test_ext, known_ext, maxlen); + if( cmp == 0 ) return cmp; + + /* if anything odd, use default */ + if( !test_ext || !known_ext ) return cmp; + + len = strlen(known_ext); + if( len > maxlen ) len = maxlen; /* ignore anything past maxlen */ + if( len > 7 ) return cmp; + + /* if here, strings are different but need to check upper-case */ + for(c = 0; c < len; c++ ) caps[c] = toupper((int) known_ext[c]); + caps[c] = '\0'; + + return strncmp(test_ext, caps, maxlen); +} + +/* return 1 if there are uppercase but no lowercase */ +static int is_uppercase(const char * str) +{ + size_t c; + int hasupper = 0; + + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) { + if( islower((int) str[c]) ) return 0; + if( !hasupper && isupper((int) str[c]) ) hasupper = 1; + } + + return hasupper; +} + +/* return 1 if there are both uppercase and lowercase characters */ +static int is_mixedcase(const char * str) +{ + size_t c; + int hasupper = 0, haslower = 0; + + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) { + if( !haslower && islower((int) str[c]) ) haslower = 1; + if( !hasupper && isupper((int) str[c]) ) hasupper = 1; + + if( haslower && hasupper ) return 1; + } + + return 0; +} + +/* convert any lowercase chars to uppercase */ +static int make_uppercase(char * str) +{ + size_t c; + + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) + if( islower((int) str[c]) ) str[c] = toupper((int) str[c]); + + return 0; +} + +/* convert any uppercase chars to lowercase */ +static int make_lowercase(char * str) +{ + size_t c; + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) + if( isupper((int) str[c]) ) str[c] = tolower((int) str[c]); + + return 0; +} + +/* run strcmp against of list of strings + * return index of equality, if found + * else return -1 */ +static int compare_strlist(const char * str, char ** strlist, int len) +{ + int c; + if( len <= 0 || !str || !strlist ) return -1; + for( c = 0; c < len; c++ ) + if( strlist[c] && !strcmp(str, strlist[c]) ) return c; + return -1; +} + +/*--------------------------------------------------------------------------*/ +/*! check whether the given type is on the "approved" list + + The code is valid if it is non-negative, and does not exceed + NIFTI_MAX_FTYPE. + + \return 1 if nifti_type is valid, 0 otherwise + \sa NIFTI_FTYPE_* codes in nifti1_io.h +*//*------------------------------------------------------------------------*/ +int is_valid_nifti_type( int nifti_type ) +{ + if( nifti_type >= NIFTI_FTYPE_ANALYZE && /* smallest type, 0 */ + nifti_type <= NIFTI_MAX_FTYPE ) + return 1; + return 0; +} + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +/*--------------------------------------------------------------------------*/ +/*! check whether the given type is on the "approved" list + + The type is explicitly checked against the NIFTI_TYPE_* list + in nifti1.h. + + \return 1 if dtype is valid, 0 otherwise + \sa NIFTI_TYPE_* codes in nifti1.h +*//*------------------------------------------------------------------------*/ +int nifti_is_valid_datatype( int dtype ) +{ + if( dtype == NIFTI_TYPE_UINT8 || + dtype == NIFTI_TYPE_INT16 || + dtype == NIFTI_TYPE_INT32 || + dtype == NIFTI_TYPE_FLOAT32 || + dtype == NIFTI_TYPE_COMPLEX64 || + dtype == NIFTI_TYPE_FLOAT64 || + dtype == NIFTI_TYPE_RGB24 || + dtype == NIFTI_TYPE_RGBA32 || + dtype == NIFTI_TYPE_INT8 || + dtype == NIFTI_TYPE_UINT16 || + dtype == NIFTI_TYPE_UINT32 || + dtype == NIFTI_TYPE_INT64 || + dtype == NIFTI_TYPE_UINT64 || + dtype == NIFTI_TYPE_FLOAT128 || + dtype == NIFTI_TYPE_COMPLEX128 || + dtype == NIFTI_TYPE_COMPLEX256 ) return 1; + return 0; +} +#endif + +/*--------------------------------------------------------------------------*/ +/*! set the nifti_type field based on fname and iname + + Note that nifti_type is changed only when it does not match + the filenames. + + \return 0 on success, -1 on error + + \sa is_valid_nifti_type, nifti_type_and_names_match +*//*------------------------------------------------------------------------*/ +int nifti_set_type_from_names( nifti_image * nim ) +{ + /* error checking first */ + if( !nim ){ Rc_fprintf_stderr("** NSTFN: no nifti_image\n"); return -1; } + + if( !nim->fname || !nim->iname ){ + Rc_fprintf_stderr("** NSTFN: missing filename(s) fname @ %p, iname @ %p\n", + nim->fname, nim->iname); + return -1; + } + + if( ! nifti_validfilename ( nim->fname ) || + ! nifti_validfilename ( nim->iname ) || + ! nifti_find_file_extension( nim->fname ) || + ! nifti_find_file_extension( nim->iname ) + ) { + Rc_fprintf_stderr("** NSTFN: invalid filename(s) fname='%s', iname='%s'\n", + nim->fname, nim->iname); + return -1; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d verify nifti_type from filenames: %d",nim->nifti_type); + + /* type should be NIFTI_FTYPE_ASCII if extension is .nia */ + if( (fileext_compare(nifti_find_file_extension(nim->fname),".nia")==0)){ + nim->nifti_type = NIFTI_FTYPE_ASCII; + } else { + /* not too picky here, do what must be done, and then verify */ + if( strcmp(nim->fname, nim->iname) == 0 ) /* one file, type 1 */ + nim->nifti_type = NIFTI_FTYPE_NIFTI1_1; + else if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) /* cannot be type 1 */ + nim->nifti_type = NIFTI_FTYPE_NIFTI1_2; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr(" -> %d\n",nim->nifti_type); + + if( g_opts.debug > 1 ) /* warn user about anything strange */ + nifti_type_and_names_match(nim, 1); + + if( is_valid_nifti_type(nim->nifti_type) ) return 0; /* success! */ + + Rc_fprintf_stderr("** NSTFN: bad nifti_type %d, for '%s' and '%s'\n", + nim->nifti_type, nim->fname, nim->iname); + + return -1; +} + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +/*--------------------------------------------------------------------------*/ +/*! Determine if this is a NIFTI-formatted file. + +
+   \return  0 if file looks like ANALYZE 7.5 [checks sizeof_hdr field == 348]
+            1 if file marked as NIFTI (header+data in 1 file)
+            2 if file marked as NIFTI (header+data in 2 files)
+           -1 if it can't tell, file doesn't exist, etc.
+   
+*//*------------------------------------------------------------------------*/ +int is_nifti_file( const char *hname ) +{ + struct nifti_1_header nhdr ; + znzFile fp ; + int ii ; + char *tmpname; + + /* bad input name? */ + + if( !nifti_validfilename(hname) ) return -1 ; + + /* open file */ + + tmpname = nifti_findhdrname(hname); + if( tmpname == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** no header file found for '%s'\n",hname); + return -1; + } + fp = znzopen( tmpname , "rb" , nifti_is_gzfile(tmpname) ) ; + free(tmpname); + if (znz_isnull(fp)) return -1 ; /* bad open? */ + + /* read header, close file */ + + ii = (int)znzread( &nhdr , 1 , sizeof(nhdr) , fp ) ; + znzclose( fp ) ; + if( ii < (int) sizeof(nhdr) ) return -1 ; /* bad read? */ + + /* check for NIFTI-ness */ + + if( NIFTI_VERSION(nhdr) != 0 ){ + return ( NIFTI_ONEFILE(nhdr) ) ? 1 : 2 ; + } + + /* check for ANALYZE-ness (sizeof_hdr field == 348) */ + + ii = nhdr.sizeof_hdr ; + if( ii == (int)sizeof(nhdr) ) return 0 ; /* matches */ + + /* try byte-swapping header */ + + swap_4(ii) ; + if( ii == (int)sizeof(nhdr) ) return 0 ; /* matches */ + + return -1 ; /* not good */ +} + +static int print_hex_vals( const char * data, size_t nbytes, FILE * fp ) +{ + size_t c; + + if ( !data || nbytes < 1 || !fp ) return -1; + + fputs("0x", fp); + for ( c = 0; c < nbytes; c++ ) + fprintf(fp, " %x", data[c]); + + return 0; +} + +/*----------------------------------------------------------------------*/ +/*! display the contents of the nifti_1_header (send to stdout) + + \param info if non-NULL, print this character string + \param hp pointer to nifti_1_header +*//*--------------------------------------------------------------------*/ +int disp_nifti_1_header( const char * info, const nifti_1_header * hp ) +{ + int c; + + Rc_fputs_stdout( "-------------------------------------------------------\n" ); + if ( info ) Rc_fputs_stdout( info ); + if ( !hp ){ Rc_fputs_stdout(" ** no nifti_1_header to display!\n"); return 1; } + + Rc_fprintf_stdout(" nifti_1_header :\n" + " sizeof_hdr = %d\n" + " data_type[10] = ", hp->sizeof_hdr); +#ifndef USING_R + print_hex_vals(hp->data_type, 10, stdout); +#endif + Rc_fprintf_stdout( "\n" + " db_name[18] = "); +#ifndef USING_R + print_hex_vals(hp->db_name, 18, stdout); +#endif + Rc_fprintf_stdout( "\n" + " extents = %d\n" + " session_error = %d\n" + " regular = 0x%x\n" + " dim_info = 0x%x\n", + hp->extents, hp->session_error, hp->regular, hp->dim_info ); + Rc_fprintf_stdout( " dim[8] ="); + for ( c = 0; c < 8; c++ ) Rc_fprintf_stdout(" %d", hp->dim[c]); + Rc_fprintf_stdout( "\n" + " intent_p1 = %f\n" + " intent_p2 = %f\n" + " intent_p3 = %f\n" + " intent_code = %d\n" + " datatype = %d\n" + " bitpix = %d\n" + " slice_start = %d\n" + " pixdim[8] =", + hp->intent_p1, hp->intent_p2, hp->intent_p3, hp->intent_code, + hp->datatype, hp->bitpix, hp->slice_start); + /* break pixdim over 2 lines */ + for ( c = 0; c < 4; c++ ) Rc_fprintf_stdout(" %f", hp->pixdim[c]); + Rc_fprintf_stdout( "\n "); + for ( c = 4; c < 8; c++ ) Rc_fprintf_stdout(" %f", hp->pixdim[c]); + Rc_fprintf_stdout( "\n" + " vox_offset = %f\n" + " scl_slope = %f\n" + " scl_inter = %f\n" + " slice_end = %d\n" + " slice_code = %d\n" + " xyzt_units = 0x%x\n" + " cal_max = %f\n" + " cal_min = %f\n" + " slice_duration = %f\n" + " toffset = %f\n" + " glmax = %d\n" + " glmin = %d\n", + hp->vox_offset, hp->scl_slope, hp->scl_inter, hp->slice_end, + hp->slice_code, hp->xyzt_units, hp->cal_max, hp->cal_min, + hp->slice_duration, hp->toffset, hp->glmax, hp->glmin); + Rc_fprintf_stdout( + " descrip = '%.80s'\n" + " aux_file = '%.24s'\n" + " qform_code = %d\n" + " sform_code = %d\n" + " quatern_b = %f\n" + " quatern_c = %f\n" + " quatern_d = %f\n" + " qoffset_x = %f\n" + " qoffset_y = %f\n" + " qoffset_z = %f\n" + " srow_x[4] = %f, %f, %f, %f\n" + " srow_y[4] = %f, %f, %f, %f\n" + " srow_z[4] = %f, %f, %f, %f\n" + " intent_name = '%-.16s'\n" + " magic = '%-.4s'\n", + hp->descrip, hp->aux_file, hp->qform_code, hp->sform_code, + hp->quatern_b, hp->quatern_c, hp->quatern_d, + hp->qoffset_x, hp->qoffset_y, hp->qoffset_z, + hp->srow_x[0], hp->srow_x[1], hp->srow_x[2], hp->srow_x[3], + hp->srow_y[0], hp->srow_y[1], hp->srow_y[2], hp->srow_y[3], + hp->srow_z[0], hp->srow_z[1], hp->srow_z[2], hp->srow_z[3], + hp->intent_name, hp->magic); + Rc_fprintf_stdout( "-------------------------------------------------------\n" ); + + return 0; +} +#endif + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_convert_nhdr2nim: %s\n", (msg) ) ; \ + return NULL ; } while(0) + +/*----------------------------------------------------------------------*/ +/*! convert a nifti_1_header into a nift1_image + + \return an allocated nifti_image, or NULL on failure +*//*--------------------------------------------------------------------*/ +nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, + const char * fname) +{ + int ii , doswap , ioff ; + int is_nifti , is_onefile ; + nifti_image *nim; + + nim = (nifti_image *)calloc( 1 , sizeof(nifti_image) ) ; + if( !nim ) ERREX("failed to allocate nifti image"); + + /* be explicit with pointers */ + nim->fname = NULL; + nim->iname = NULL; + nim->data = NULL; + + /**- check if we must swap bytes */ + + doswap = need_nhdr_swap(nhdr.dim[0], nhdr.sizeof_hdr); /* swap data flag */ + + if( doswap < 0 ){ + free(nim); + if( doswap == -1 ) ERREX("bad dim[0]") ; + ERREX("bad sizeof_hdr") ; /* else */ + } + + /**- determine if this is a NIFTI-1 compliant header */ + + is_nifti = NIFTI_VERSION(nhdr) ; + /* + * before swapping header, record the Analyze75 orient code + */ + if(!is_nifti) + { + /**- in analyze75, the orient code is at the same address as + * qform_code, but it's just one byte + * the qform_code will be zero, at which point you can check + * analyze75_orient if you care to. + */ + unsigned char c = *((char *)(&nhdr.qform_code)); + nim->analyze75_orient = (analyze_75_orient_code)c; + } + if( doswap ) { + if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); + swap_nifti_header( &nhdr , is_nifti ) ; + } + + if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr); + + if( nhdr.datatype == DT_BINARY || nhdr.datatype == DT_UNKNOWN ) + { + free(nim); + ERREX("bad datatype") ; + } + + if( nhdr.dim[1] <= 0 ) + { + free(nim); + ERREX("bad dim[1]") ; + } + + /* fix bad dim[] values in the defined dimension range */ + for( ii=2 ; ii <= nhdr.dim[0] ; ii++ ) + if( nhdr.dim[ii] <= 0 ) nhdr.dim[ii] = 1 ; + + /* fix any remaining bad dim[] values, so garbage does not propagate */ + /* (only values 0 or 1 seem rational, otherwise set to arbirary 1) */ + for( ii=nhdr.dim[0]+1 ; ii <= 7 ; ii++ ) + if( nhdr.dim[ii] != 1 && nhdr.dim[ii] != 0) nhdr.dim[ii] = 1 ; + +#if 0 /* rely on dim[0], do not attempt to modify it 16 Nov 2005 [rickr] */ + + /**- get number of dimensions (ignoring dim[0] now) */ + for( ii=7 ; ii >= 2 ; ii-- ) /* loop backwards until we */ + if( nhdr.dim[ii] > 1 ) break ; /* find a dim bigger than 1 */ + ndim = ii ; +#endif + + /**- set bad grid spacings to 1.0 */ + + for( ii=1 ; ii <= nhdr.dim[0] ; ii++ ){ + if( nhdr.pixdim[ii] == 0.0 || + !IS_GOOD_FLOAT(nhdr.pixdim[ii]) ) nhdr.pixdim[ii] = 1.0f ; + } + + is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ; + + if( is_nifti ) nim->nifti_type = (is_onefile) ? NIFTI_FTYPE_NIFTI1_1 + : NIFTI_FTYPE_NIFTI1_2 ; + else nim->nifti_type = NIFTI_FTYPE_ANALYZE ; + + ii = nifti_short_order() ; + if( doswap ) nim->byteorder = REVERSE_ORDER(ii) ; + else nim->byteorder = ii ; + + + /**- set dimensions of data array */ + + nim->ndim = nim->dim[0] = nhdr.dim[0]; + nim->nx = nim->dim[1] = nhdr.dim[1]; + nim->ny = nim->dim[2] = nhdr.dim[2]; + nim->nz = nim->dim[3] = nhdr.dim[3]; + nim->nt = nim->dim[4] = nhdr.dim[4]; + nim->nu = nim->dim[5] = nhdr.dim[5]; + nim->nv = nim->dim[6] = nhdr.dim[6]; + nim->nw = nim->dim[7] = nhdr.dim[7]; + + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + nim->nvox *= nhdr.dim[ii]; + + /**- set the type of data in voxels and how many bytes per voxel */ + + nim->datatype = nhdr.datatype ; + + nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; + if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + + /**- set the grid spacings */ + + nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; + nim->dy = nim->pixdim[2] = nhdr.pixdim[2] ; + nim->dz = nim->pixdim[3] = nhdr.pixdim[3] ; + nim->dt = nim->pixdim[4] = nhdr.pixdim[4] ; + nim->du = nim->pixdim[5] = nhdr.pixdim[5] ; + nim->dv = nim->pixdim[6] = nhdr.pixdim[6] ; + nim->dw = nim->pixdim[7] = nhdr.pixdim[7] ; + + /**- compute qto_xyz transformation from pixel indexes (i,j,k) to (x,y,z) */ + + if( !is_nifti || nhdr.qform_code <= 0 ){ + /**- if not nifti or qform_code <= 0, use grid spacing for qto_xyz */ + + nim->qto_xyz.m[0][0] = nim->dx ; /* grid spacings */ + nim->qto_xyz.m[1][1] = nim->dy ; /* along diagonal */ + nim->qto_xyz.m[2][2] = nim->dz ; + + /* off diagonal is zero */ + + nim->qto_xyz.m[0][1]=nim->qto_xyz.m[0][2]=nim->qto_xyz.m[0][3] = 0.0f; + nim->qto_xyz.m[1][0]=nim->qto_xyz.m[1][2]=nim->qto_xyz.m[1][3] = 0.0f; + nim->qto_xyz.m[2][0]=nim->qto_xyz.m[2][1]=nim->qto_xyz.m[2][3] = 0.0f; + + /* last row is always [ 0 0 0 1 ] */ + + nim->qto_xyz.m[3][0]=nim->qto_xyz.m[3][1]=nim->qto_xyz.m[3][2] = 0.0f; + nim->qto_xyz.m[3][3]= 1.0f ; + + nim->qform_code = NIFTI_XFORM_UNKNOWN ; + + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d no qform provided\n"); + } else { + /**- else NIFTI: use the quaternion-specified transformation */ + + nim->quatern_b = FIXED_FLOAT( nhdr.quatern_b ) ; + nim->quatern_c = FIXED_FLOAT( nhdr.quatern_c ) ; + nim->quatern_d = FIXED_FLOAT( nhdr.quatern_d ) ; + + nim->qoffset_x = FIXED_FLOAT(nhdr.qoffset_x) ; + nim->qoffset_y = FIXED_FLOAT(nhdr.qoffset_y) ; + nim->qoffset_z = FIXED_FLOAT(nhdr.qoffset_z) ; + + nim->qfac = (nhdr.pixdim[0] < 0.0) ? -1.0f : 1.0f ; /* left-handedness? */ + + nim->qto_xyz = nifti_quatern_to_mat44( + nim->quatern_b, nim->quatern_c, nim->quatern_d, + nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, + nim->dx , nim->dy , nim->dz , + nim->qfac ) ; + + nim->qform_code = nhdr.qform_code ; + + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d qform orientations:\n", nim->qto_xyz); + } + + /**- load inverse transformation (x,y,z) -> (i,j,k) */ + + nim->qto_ijk = nifti_mat44_inverse( nim->qto_xyz ) ; + + /**- load sto_xyz affine transformation, if present */ + + if( !is_nifti || nhdr.sform_code <= 0 ){ + /**- if not nifti or sform_code <= 0, then no sto transformation */ + + nim->sform_code = NIFTI_XFORM_UNKNOWN ; + + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d no sform provided\n"); + + } else { + /**- else set the sto transformation from srow_*[] */ + + nim->sto_xyz.m[0][0] = nhdr.srow_x[0] ; + nim->sto_xyz.m[0][1] = nhdr.srow_x[1] ; + nim->sto_xyz.m[0][2] = nhdr.srow_x[2] ; + nim->sto_xyz.m[0][3] = nhdr.srow_x[3] ; + + nim->sto_xyz.m[1][0] = nhdr.srow_y[0] ; + nim->sto_xyz.m[1][1] = nhdr.srow_y[1] ; + nim->sto_xyz.m[1][2] = nhdr.srow_y[2] ; + nim->sto_xyz.m[1][3] = nhdr.srow_y[3] ; + + nim->sto_xyz.m[2][0] = nhdr.srow_z[0] ; + nim->sto_xyz.m[2][1] = nhdr.srow_z[1] ; + nim->sto_xyz.m[2][2] = nhdr.srow_z[2] ; + nim->sto_xyz.m[2][3] = nhdr.srow_z[3] ; + + /* last row is always [ 0 0 0 1 ] */ + + nim->sto_xyz.m[3][0]=nim->sto_xyz.m[3][1]=nim->sto_xyz.m[3][2] = 0.0f; + nim->sto_xyz.m[3][3]= 1.0f ; + + nim->sto_ijk = nifti_mat44_inverse( nim->sto_xyz ) ; + + nim->sform_code = nhdr.sform_code ; + + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d sform orientations:\n", nim->sto_xyz); + } + + /**- set miscellaneous NIFTI stuff */ + + if( is_nifti ){ + nim->scl_slope = FIXED_FLOAT( nhdr.scl_slope ) ; + nim->scl_inter = FIXED_FLOAT( nhdr.scl_inter ) ; + + nim->intent_code = nhdr.intent_code ; + + nim->intent_p1 = FIXED_FLOAT( nhdr.intent_p1 ) ; + nim->intent_p2 = FIXED_FLOAT( nhdr.intent_p2 ) ; + nim->intent_p3 = FIXED_FLOAT( nhdr.intent_p3 ) ; + + nim->toffset = FIXED_FLOAT( nhdr.toffset ) ; + + memcpy(nim->intent_name,nhdr.intent_name,15); nim->intent_name[15] = '\0'; + + nim->xyz_units = XYZT_TO_SPACE(nhdr.xyzt_units) ; + nim->time_units = XYZT_TO_TIME (nhdr.xyzt_units) ; + + nim->freq_dim = DIM_INFO_TO_FREQ_DIM ( nhdr.dim_info ) ; + nim->phase_dim = DIM_INFO_TO_PHASE_DIM( nhdr.dim_info ) ; + nim->slice_dim = DIM_INFO_TO_SLICE_DIM( nhdr.dim_info ) ; + + nim->slice_code = nhdr.slice_code ; + nim->slice_start = nhdr.slice_start ; + nim->slice_end = nhdr.slice_end ; + nim->slice_duration = FIXED_FLOAT(nhdr.slice_duration) ; + } + + /**- set Miscellaneous ANALYZE stuff */ + + nim->cal_min = FIXED_FLOAT(nhdr.cal_min) ; + nim->cal_max = FIXED_FLOAT(nhdr.cal_max) ; + + memcpy(nim->descrip ,nhdr.descrip ,79) ; nim->descrip [79] = '\0' ; + memcpy(nim->aux_file,nhdr.aux_file,23) ; nim->aux_file[23] = '\0' ; + + /**- set ioff from vox_offset (but at least sizeof(header)) */ + + is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ; + + if( is_onefile ){ + ioff = (int)nhdr.vox_offset ; + if( ioff < (int) sizeof(nhdr) ) ioff = (int) sizeof(nhdr) ; + } else { + ioff = (int)nhdr.vox_offset ; + } + nim->iname_offset = ioff ; + + + /**- deal with file names if set */ + if (fname!=NULL) { + nifti_set_filenames(nim,fname,0,0); + if (nim->iname==NULL) { ERREX("bad filename"); } + } else { + nim->fname = NULL; + nim->iname = NULL; + } + + /* clear extension fields */ + nim->num_ext = 0; + nim->ext_list = NULL; + + return nim; +} + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_image_open(%s): %s\n", \ + (hname != NULL) ? hname : "(null)" , (msg) ) ; \ + return fptr ; } while(0) + +/*************************************************************** + * nifti_image_open + ***************************************************************/ +/*! znzFile nifti_image_open( char *hname, char *opts , nifti_image **nim) + \brief Read in NIFTI-1 or ANALYZE-7.5 file (pair) header information into a nifti_image struct. + + - The image data is not read from disk (it may be read later using + nifti_image_load(), for example). + - The image data will be stored in whatever data format the + input data is; no scaling will be applied. + - DT_BINARY data is not supported. + - nifti_image_free() can be used to delete the returned struct, + when you are done with it. + + \param hname filename of dataset .hdr or .nii file + \param opts options string for opening the header file + \param nim pointer to pointer to nifti_image struct + (this routine allocates the nifti_image struct) + \return file pointer (gzippable) to the file with the image data, + ready for reading. +
NULL if something fails badly. + \sa nifti_image_load, nifti_image_free + */ +znzFile nifti_image_open(const char * hname, const char * opts, nifti_image ** nim) +{ + znzFile fptr=NULL; + /* open the hdr and reading it in, but do not load the data */ + *nim = nifti_image_read(hname,0); + /* open the image file, ready for reading (compressed works for all reads) */ + if( ((*nim) == NULL) || ((*nim)->iname == NULL) || + ((*nim)->nbyper <= 0) || ((*nim)->nvox <= 0) ) + ERREX("bad header info") ; + + /* open image data file */ + fptr = znzopen( (*nim)->iname, opts, nifti_is_gzfile((*nim)->iname) ); + if( znz_isnull(fptr) ) ERREX("Can't open data file") ; + + return fptr; +} + + +/*----------------------------------------------------------------------*/ +/*! return an allocated and filled nifti_1_header struct + + Read the binary header from disk, and swap bytes if necessary. + + \return an allocated nifti_1_header struct, or NULL on failure + + \param hname name of file containing header + \param swapped if not NULL, return whether header bytes were swapped + \param check flag to check for invalid nifti_1_header + + \warning ASCII header type is not supported + + \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks +*//*--------------------------------------------------------------------*/ +nifti_1_header * nifti_read_header(const char * hname, int * swapped, int check) +{ + nifti_1_header nhdr, * hptr; + znzFile fp; + int bytes, lswap; + char * hfile; + char fname[] = { "nifti_read_header" }; + + /* determine file name to use for header */ + hfile = nifti_findhdrname(hname); + if( hfile == NULL ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"failed to find header file for", hname); + return NULL; + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d %s: found header filename '%s'\n",fname,hfile); + + fp = znzopen( hfile, "rb", nifti_is_gzfile(hfile) ); + if( znz_isnull(fp) ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"failed to open header file",hfile); + free(hfile); + return NULL; + } + + free(hfile); /* done with filename */ + + if( has_ascii_header(fp) == 1 ){ + znzclose( fp ); + if( g_opts.debug > 0 ) + LNI_FERR(fname,"ASCII header type not supported",hname); + return NULL; + } + + /* read the binary header */ + bytes = (int)znzread( &nhdr, 1, sizeof(nhdr), fp ); + znzclose( fp ); /* we are done with the file now */ + + if( bytes < (int)sizeof(nhdr) ){ + if( g_opts.debug > 0 ){ + LNI_FERR(fname,"bad binary header read for file", hname); + Rc_fprintf_stderr(" - read %d of %d bytes\n",bytes, (int)sizeof(nhdr)); + } + return NULL; + } + + /* now just decide on byte swapping */ + lswap = need_nhdr_swap(nhdr.dim[0], nhdr.sizeof_hdr); /* swap data flag */ + if( check && lswap < 0 ){ + LNI_FERR(fname,"bad nifti_1_header for file", hname); + return NULL; + } else if ( lswap < 0 ) { + lswap = 0; /* if swapping does not help, don't do it */ + if(g_opts.debug > 1) Rc_fprintf_stderr("-- swap failure, none applied\n"); + } + + if( lswap ) { + if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr); + swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ; + } + + if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr); + + if ( check && ! nifti_hdr_looks_good(&nhdr) ){ + LNI_FERR(fname,"nifti_1_header looks bad for file", hname); + return NULL; + } + + /* all looks good, so allocate memory for and return the header */ + hptr = (nifti_1_header *)malloc(sizeof(nifti_1_header)); + if( ! hptr ){ + Rc_fprintf_stderr("** nifti_read_hdr: failed to alloc nifti_1_header\n"); + return NULL; + } + + if( swapped ) *swapped = lswap; /* only if they care */ + + memcpy(hptr, &nhdr, sizeof(nifti_1_header)); + + return hptr; +} + + +/*----------------------------------------------------------------------*/ +/*! decide if this nifti_1_header structure looks reasonable + + Check dim[0], dim[1], sizeof_hdr, and datatype. + Check magic string for "n+1". + Maybe more tests will follow. + + \return 1 if the header seems valid, 0 otherwise + + \sa nifti_nim_is_valid, valid_nifti_extensions +*//*--------------------------------------------------------------------*/ +int nifti_hdr_looks_good(const nifti_1_header * hdr) +{ + int is_nifti, c, errs = 0; + + /* check dim[0] and sizeof_hdr */ + if( need_nhdr_swap(hdr->dim[0], hdr->sizeof_hdr) < 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** bad nhdr fields: dim0, sizeof_hdr = %d, %d\n", + hdr->dim[0], hdr->sizeof_hdr); + errs++; + } + + /* check the valid dimension sizes (maybe dim[0] is bad) */ + for( c = 1; c <= hdr->dim[0] && c <= 7; c++ ) + if( hdr->dim[c] <= 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** bad nhdr field: dim[%d] = %d\n",c,hdr->dim[c]); + errs++; + } + + is_nifti = NIFTI_VERSION(*hdr); /* determine header type */ + + if( is_nifti ){ /* NIFTI */ + + if( ! nifti_datatype_is_valid(hdr->datatype, 1) ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** bad NIFTI datatype in hdr, %d\n",hdr->datatype); + errs++; + } + + } else { /* ANALYZE 7.5 */ + + if( g_opts.debug > 1 ) /* maybe tell user it's an ANALYZE hdr */ + Rc_fprintf_stderr( + "-- nhdr magic field implies ANALYZE: magic = '%.4s'\n",hdr->magic); + + if( ! nifti_datatype_is_valid(hdr->datatype, 0) ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** bad ANALYZE datatype in hdr, %d\n",hdr->datatype); + errs++; + } + } + + if( errs ) return 0; /* problems */ + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d nifti header looks good\n"); + + return 1; /* looks good */ +} + + +/*---------------------------------------------------------------------- + * check whether byte swapping is needed + * + * dim[0] should be in [0,7], and sizeof_hdr should be accurate + * + * \returns > 0 : needs swap + * 0 : does not need swap + * < 0 : error condition + *----------------------------------------------------------------------*/ +static int need_nhdr_swap( short dim0, int hdrsize ) +{ + short d0 = dim0; /* so we won't have to swap them on the stack */ + int hsize = hdrsize; + + if( d0 != 0 ){ /* then use it for the check */ + if( d0 > 0 && d0 <= 7 ) return 0; + + nifti_swap_2bytes(1, &d0); /* swap? */ + if( d0 > 0 && d0 <= 7 ) return 1; + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("** NIFTI: bad swapped d0 = %d, unswapped = ", d0); + nifti_swap_2bytes(1, &d0); /* swap? */ + Rc_fprintf_stderr("%d\n", d0); + } + + return -1; /* bad, naughty d0 */ + } + + /* dim[0] == 0 should not happen, but could, so try hdrsize */ + if( hsize == sizeof(nifti_1_header) ) return 0; + + nifti_swap_4bytes(1, &hsize); /* swap? */ + if( hsize == sizeof(nifti_1_header) ) return 1; + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("** NIFTI: bad swapped hsize = %d, unswapped = ", hsize); + nifti_swap_4bytes(1, &hsize); /* swap? */ + Rc_fprintf_stderr("%d\n", hsize); + } + + return -2; /* bad, naughty hsize */ +} + + +/* use macro LNI_FILE_ERROR instead of ERREX() +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_image_read(%s): %s\n", \ + (hname != NULL) ? hname : "(null)" , (msg) ) ; \ + return NULL ; } while(0) +*/ + + +/*************************************************************** + * nifti_image_read + ***************************************************************/ +/*! \brief Read a nifti header and optionally the data, creating a nifti_image. + + - The data buffer will be byteswapped if necessary. + - The data buffer will not be scaled. + - The data buffer is allocated with calloc(). + + \param hname filename of the nifti dataset + \param read_data Flag, true=read data blob, false=don't read blob. + \return A pointer to the nifti_image data structure. + + \sa nifti_image_free, nifti_free_extensions, nifti_image_read_bricks +*/ +nifti_image *nifti_image_read( const char *hname , int read_data ) +{ + struct nifti_1_header nhdr ; + nifti_image *nim ; + znzFile fp ; + int rv, ii , filesize, remaining; + char fname[] = { "nifti_image_read" }; + char *hfile=NULL; + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("-d image_read from '%s', read_data = %d",hname,read_data); +#ifdef HAVE_ZLIB + Rc_fprintf_stderr(", HAVE_ZLIB = 1\n"); +#else + Rc_fprintf_stderr(", HAVE_ZLIB = 0\n"); +#endif + } + + /**- determine filename to use for header */ + hfile = nifti_findhdrname(hname); + if( hfile == NULL ){ + if(g_opts.debug > 0) + LNI_FERR(fname,"failed to find header file for", hname); + return NULL; /* check return */ + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d %s: found header filename '%s'\n",fname,hfile); + + if( nifti_is_gzfile(hfile) ) filesize = -1; /* unknown */ + else filesize = nifti_get_filesize(hfile); + + fp = znzopen(hfile, "rb", nifti_is_gzfile(hfile)); + if( znz_isnull(fp) ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"failed to open header file",hfile); + free(hfile); + return NULL; + } + + rv = has_ascii_header( fp ); + if( rv < 0 ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"short header read",hfile); + znzclose( fp ); + free(hfile); + return NULL; + } + else if ( rv == 1 ){ /* process special file type */ + nim = nifti_read_ascii_image( fp, hfile, filesize, read_data ); + znzclose(fp); + free(hfile); + return nim; + } + + /* else, just process normally */ + + /**- read binary header */ + + ii = (int)znzread( &nhdr , 1 , sizeof(nhdr) , fp ) ; /* read the thing */ + + /* keep file open so we can check for exts. after nifti_convert_nhdr2nim() */ + + if( ii < (int) sizeof(nhdr) ){ + if( g_opts.debug > 0 ){ + LNI_FERR(fname,"bad binary header read for file", hfile); + Rc_fprintf_stderr(" - read %d of %d bytes\n",ii, (int)sizeof(nhdr)); + } + znzclose(fp) ; + free(hfile); + return NULL; + } + + /* create output image struct and set it up */ + + /**- convert all nhdr fields to nifti_image fields */ + nim = nifti_convert_nhdr2nim(nhdr,hfile); + + if( nim == NULL ){ + znzclose( fp ) ; /* close the file */ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"cannot create nifti image from header",hfile); + free(hfile); /* had to save this for debug message */ + return NULL; + } + + if( g_opts.debug > 3 ){ + Rc_fprintf_stderr("+d nifti_image_read(), have nifti image:\n"); + if( g_opts.debug > 2 ) nifti_image_infodump(nim); + } + + /**- check for extensions (any errors here means no extensions) */ + if( NIFTI_ONEFILE(nhdr) ) remaining = nim->iname_offset - sizeof(nhdr); + else remaining = filesize - sizeof(nhdr); + + (void)nifti_read_extensions(nim, fp, remaining); + + znzclose( fp ) ; /* close the file */ + free(hfile); + + /**- read the data if desired, then bug out */ + if( read_data ){ + if( nifti_image_load( nim ) < 0 ){ + nifti_image_free(nim); /* take ball, go home. */ + return NULL; + } + } + else nim->data = NULL ; + + return nim ; +} + +nifti_image* nifti_image_mem_read(const uint8_t* data, const size_t size, int gz, const size_t estimated_size) +{ + return NULL; +} + +/*---------------------------------------------------------------------- + * has_ascii_header - see if the NIFTI header is an ASCII format + * + * If the file starts with the ASCII string " 1 ) + Rc_fprintf_stderr("-d %s: have ASCII NIFTI file of size %d\n",fname,slen); + + if( slen > 65530 ) slen = 65530 ; + sbuf = (char *)calloc(sizeof(char),slen+1) ; + if( !sbuf ){ + Rc_fprintf_stderr("** %s: failed to alloc %d bytes for sbuf",lfunc,65530); + return NULL; + } + znzread( sbuf , 1 , slen , fp ) ; + nim = nifti_image_from_ascii( sbuf, &txt_size ) ; free( sbuf ) ; + if( nim == NULL ){ + LNI_FERR(lfunc,"failed nifti_image_from_ascii()",fname); + return NULL; + } + nim->nifti_type = NIFTI_FTYPE_ASCII ; + + /* compute remaining space for extensions */ + remain = flen - txt_size - (int)nifti_get_volsize(nim); + if( remain > 4 ){ + /* read extensions (reposition file pointer, first) */ + znzseek(fp, txt_size, SEEK_SET); + (void) nifti_read_extensions(nim, fp, remain); + } + + nim->iname_offset = -1 ; /* check from the end of the file */ + + if( read_data ) rv = nifti_image_load( nim ) ; + else nim->data = NULL ; + + /* check for nifti_image_load() failure, maybe bail out */ + if( read_data && rv != 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d failed image_load, free nifti image struct\n"); + free(nim); + return NULL; + } + + return nim ; +} + + +/*---------------------------------------------------------------------- + * Read the extensions into the nifti_image struct 08 Dec 2004 [rickr] + * + * This function is called just after the header struct is read in, and + * it is assumed the file pointer has not moved. The value in remain + * is assumed to be accurate, reflecting the bytes of space for potential + * extensions. + * + * return the number of extensions read in, or < 0 on error + *----------------------------------------------------------------------*/ +static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain ) +{ + nifti1_extender extdr; /* defines extension existence */ + nifti1_extension extn; /* single extension to process */ + nifti1_extension * Elist; /* list of processed extensions */ + int posn, count; + + if( !nim || znz_isnull(fp) ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_read_extensions: bad inputs (%p,%p)\n", + (void *)nim, (void *)fp); + return -1; + } + + posn = znztell(fp); + + if( (posn != sizeof(nifti_1_header)) && + (nim->nifti_type != NIFTI_FTYPE_ASCII) ) + Rc_fprintf_stderr("** WARNING: posn not header size (%d, %d)\n", + posn, (int)sizeof(nifti_1_header)); + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nre: posn = %d, offset = %d, type = %d, remain = %d\n", + posn, nim->iname_offset, nim->nifti_type, remain); + + if( remain < 16 ){ + if( g_opts.debug > 2 ){ + if( g_opts.skip_blank_ext ) + Rc_fprintf_stderr("-d no extender in '%s' is okay, as " + "skip_blank_ext is set\n",nim->fname); + else + Rc_fprintf_stderr("-d remain=%d, no space for extensions\n",remain); + } + return 0; + } + + count = (int)znzread( extdr.extension, 1, 4, fp ); /* get extender */ + + if( count < 4 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d file '%s' is too short for an extender\n", + nim->fname); + return 0; + } + + if( extdr.extension[0] != 1 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d extender[0] (%d) shows no extensions for '%s'\n", + extdr.extension[0], nim->fname); + return 0; + } + + remain -= 4; + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d found valid 4-byte extender, remain = %d\n", remain); + + /* so we expect extensions, but have no idea of how many there may be */ + + count = 0; + Elist = NULL; + while (nifti_read_next_extension(&extn, nim, remain, fp) > 0) + { + if( nifti_add_exten_to_list(&extn, &Elist, count+1) < 0 ){ + free(Elist); + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** failed adding ext %d to list\n", count); + return -1; + } + + /* we have a new extension */ + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("+d found extension #%d, code = 0x%x, size = %d\n", + count, extn.ecode, extn.esize); + if( extn.ecode == NIFTI_ECODE_AFNI && g_opts.debug > 2 ) /* ~XML */ + Rc_fprintf_stderr(" AFNI extension: %.*s\n", + extn.esize-8,extn.edata); + else if( extn.ecode == NIFTI_ECODE_COMMENT && g_opts.debug > 2 ) + Rc_fprintf_stderr(" COMMENT extension: %.*s\n", /* TEXT */ + extn.esize-8,extn.edata); + } + remain -= extn.esize; + count++; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("+d found %d extension(s)\n", count); + + nim->num_ext = count; + nim->ext_list = Elist; + + return count; +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_add_extension - add an extension, with a copy of the data + + Add an extension to the nim->ext_list array. + Fill this extension with a copy of the data, noting the + length and extension code. + + \param nim - nifti_image to add extension to + \param data - raw extension data + \param length - length of raw extension data + \param ecode - extension code + + \sa extension codes NIFTI_ECODE_* in nifti1_io.h + \sa nifti_free_extensions, valid_nifti_extensions, nifti_copy_extensions + + \return 0 on success, -1 on error (and free the entire list) +*//*--------------------------------------------------------------------*/ +int nifti_add_extension(nifti_image *nim, const char * data, int len, int ecode) +{ + nifti1_extension ext; + + /* error are printed in functions */ + if( nifti_fill_extension(&ext, data, len, ecode) ) {free(ext.edata); return -1;} + if( nifti_add_exten_to_list(&ext, &nim->ext_list, nim->num_ext+1)) {free(ext.edata); return -1;} + + nim->num_ext++; /* success, so increment */ + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/* nifti_add_exten_to_list - add a new nifti1_extension to the list + + We will append via "malloc, copy and free", because on an error, + the list will revert to the previous one (sorry realloc(), only + quality dolphins get to become part of St@rk!st brand tunafish). + + return 0 on success, -1 on error (and free the entire list) +*//*--------------------------------------------------------------------*/ +static int nifti_add_exten_to_list( nifti1_extension * new_ext, + nifti1_extension ** list, int new_length ) +{ + nifti1_extension * tmplist; + + tmplist = *list; + *list = (nifti1_extension *)malloc(new_length * sizeof(nifti1_extension)); + + /* check for failure first */ + if( ! *list ){ + Rc_fprintf_stderr("** failed to alloc %d extension structs (%d bytes)\n", + new_length, new_length*(int)sizeof(nifti1_extension)); + if( !tmplist ) return -1; /* no old list to lose */ + + *list = tmplist; /* reset list to old one */ + return -1; + } + + /* if an old list exists, copy the pointers and free the list */ + if( tmplist ){ + memcpy(*list, tmplist, (new_length-1)*sizeof(nifti1_extension)); + free(tmplist); + } + + /* for some reason, I just don't like struct copy... */ + (*list)[new_length-1].esize = new_ext->esize; + (*list)[new_length-1].ecode = new_ext->ecode; + (*list)[new_length-1].edata = new_ext->edata; + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d allocated and appended extension #%d to list\n", + new_length); + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/* nifti_fill_extension - given data and length, fill an extension struct + + Allocate memory for data, copy data, set the size and code. + + return 0 on success, -1 on error (and free the entire list) +*//*--------------------------------------------------------------------*/ +static int nifti_fill_extension( nifti1_extension *ext, const char * data, + int len, int ecode) +{ + int esize; + + if( !ext || !data || len < 0 ){ + Rc_fprintf_stderr("** fill_ext: bad params (%p,%p,%d)\n", + (void *)ext, data, len); + return -1; + } else if( ! nifti_is_valid_ecode(ecode) ){ + Rc_fprintf_stderr("** warning: writing unknown ecode %d\n", ecode); + /* should not be fatal 29 Apr 2015 [rickr] */ + } + + /* compute esize, first : len+8, and take ceiling up to a mult of 16 */ + esize = len+8; + if( esize & 0xf ) esize = (esize + 0xf) & ~0xf; + ext->esize = esize; + + /* allocate esize-8 (maybe more than len), using calloc for fill */ + ext->edata = (char *)calloc(esize-8, sizeof(char)); + if( !ext->edata ){ + Rc_fprintf_stderr("** NFE: failed to alloc %d bytes for extension\n",len); + return -1; + } + + memcpy(ext->edata, data, len); /* copy the data, using len */ + ext->ecode = ecode; /* set the ecode */ + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d alloc %d bytes for ext len %d, ecode %d, esize %d\n", + esize-8, len, ecode, esize); + + return 0; +} + + +/*---------------------------------------------------------------------- + * nifti_read_next_extension - read a single extension from the file + * + * return (>= 0 is okay): + * + * success : esize + * no extension : 0 + * error : -1 + *----------------------------------------------------------------------*/ +static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, + int remain, znzFile fp ) +{ + int swap = nim->byteorder != nifti_short_order(); + int count, size, code = NIFTI_ECODE_IGNORE; + + /* first clear nex */ + nex->esize = nex->ecode = 0; + nex->edata = NULL; + + if( remain < 16 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d only %d bytes remain, so no extension\n", remain); + return 0; + } + + /* must start with 4-byte size and code */ + count = (int)znzread( &size, 4, 1, fp ); + if( count == 1 ) count += (int)znzread( &code, 4, 1, fp ); + + if( count != 2 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d current extension read failed\n"); + znzseek(fp, -4*count, SEEK_CUR); /* back up past any read */ + return 0; /* no extension, no error condition */ + } + + if( swap ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d pre-swap exts: code %d, size %d\n", code, size); + + nifti_swap_4bytes(1, &size); + nifti_swap_4bytes(1, &code); + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d potential extension: code %d, size %d\n", code, size); + + if( !nifti_check_extension(nim, size, code, remain) ){ + if( znzseek(fp, -8, SEEK_CUR) < 0 ){ /* back up past any read */ + Rc_fprintf_stderr("** failure to back out of extension read!\n"); + return -1; + } + return 0; + } + + /* now get the actual data */ + nex->esize = size; + nex->ecode = code; + + size -= 8; /* subtract space for size and code in extension */ + nex->edata = (char *)malloc(size * sizeof(char)); + if( !nex->edata ){ + Rc_fprintf_stderr("** failed to allocate %d bytes for extension\n",size); + return -1; + } + + count = (int)znzread(nex->edata, 1, size, fp); + if( count < size ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("-d read only %d (of %d) bytes for extension\n", + count, size); + free(nex->edata); + nex->edata = NULL; + return -1; + } + + /* success! */ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d successfully read extension, code %d, size %d\n", + nex->ecode, nex->esize); + + return nex->esize; +} + + +/*----------------------------------------------------------------------*/ +/*! for each extension, check code, size and data pointer +*//*--------------------------------------------------------------------*/ +int valid_nifti_extensions(const nifti_image * nim) +{ + nifti1_extension * ext; + int c, errs; + + if( nim->num_ext <= 0 || nim->ext_list == NULL ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d empty extension list\n"); + return 0; + } + + /* for each extension, check code, size and data pointer */ + ext = nim->ext_list; + errs = 0; + for ( c = 0; c < nim->num_ext; c++ ){ + if( ! nifti_is_valid_ecode(ext->ecode) ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d ext %d, unknown code %d\n", c, ext->ecode); + /* should not be fatal 29 Apr 2015 [rickr] */ + } + + if( ext->esize <= 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d ext %d, bad size = %d\n", c, ext->esize); + errs++; + } else if( ext->esize & 0xf ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d ext %d, size %d not multiple of 16\n", + c, ext->esize); + errs++; + } + + if( ext->edata == NULL ){ + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d ext %d, missing data\n", c); + errs++; + } + + ext++; + } + + if( errs > 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("-d had %d extension errors, none will be written\n", + errs); + return 0; + } + + /* if we're here, we're good */ + return 1; +} + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +/*----------------------------------------------------------------------*/ +/*! check whether the extension code is valid + + \return 1 if valid, 0 otherwise +*//*--------------------------------------------------------------------*/ +int nifti_is_valid_ecode( int ecode ) +{ + if( ecode < NIFTI_ECODE_IGNORE || /* minimum code number (0) */ + ecode > NIFTI_MAX_ECODE || /* maximum code number */ + ecode & 1 ) /* cannot be odd */ + return 0; + + return 1; +} +#endif + +/*---------------------------------------------------------------------- + * check for valid size and code, as well as can be done + *----------------------------------------------------------------------*/ +static int nifti_check_extension(nifti_image *nim, int size, int code, int rem) +{ + /* check for bad code before bad size */ + if( ! nifti_is_valid_ecode(code) ) { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d invalid extension code %d\n",code); + /* should not be fatal 29 Apr 2015 [rickr] */ + } + + if( size < 16 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d ext size %d, no extension\n",size); + return 0; + } + + if( size > rem ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d ext size %d, space %d, no extension\n", size, rem); + return 0; + } + + if( size & 0xf ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nifti extension size %d not multiple of 16\n",size); + return 0; + } + + if( nim->nifti_type == NIFTI_FTYPE_ASCII && size > LNI_MAX_NIA_EXT_LEN ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d NVE, bad nifti_type 3 size %d\n", size); + return 0; + } + + return 1; +} + + +/*---------------------------------------------------------------------- + * nifti_image_load_prep - prepare to read data + * + * Check nifti_image fields, open the file and seek to the appropriate + * offset for reading. + * + * return NULL on failure + *----------------------------------------------------------------------*/ +static znzFile nifti_image_load_prep( nifti_image *nim ) +{ + /* set up data space, open data file and seek, then call nifti_read_buffer */ + size_t ntot , ii , ioff; + znzFile fp; + char *tmpimgname; + char fname[] = { "nifti_image_load_prep" }; + + /**- perform sanity checks */ + if( nim == NULL || nim->iname == NULL || + nim->nbyper <= 0 || nim->nvox <= 0 ) + { + if ( g_opts.debug > 0 ){ + if( !nim ) Rc_fprintf_stderr("** ERROR: N_image_load: no nifti image\n"); + else Rc_fprintf_stderr("** ERROR: N_image_load: bad params (%p,%d,%u)\n", + nim->iname, nim->nbyper, (unsigned)nim->nvox); + } + return NULL; + } + + ntot = nifti_get_volsize(nim) ; /* total bytes to read */ + + /**- open image data file */ + + tmpimgname = nifti_findimgname(nim->iname , nim->nifti_type); + if( tmpimgname == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** no image file found for '%s'\n",nim->iname); + return NULL; + } + + fp = znzopen(tmpimgname, "rb", nifti_is_gzfile(tmpimgname)); + if (znz_isnull(fp)){ + if(g_opts.debug > 0) LNI_FERR(fname,"cannot open data file",tmpimgname); + free(tmpimgname); + return NULL; /* bad open? */ + } + free(tmpimgname); + + /**- get image offset: a negative offset means to figure from end of file */ + if( nim->iname_offset < 0 ){ + if( nifti_is_gzfile(nim->iname) ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"negative offset for compressed file",nim->iname); + znzclose(fp); + return NULL; + } + ii = nifti_get_filesize( nim->iname ) ; + if( ii <= 0 ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); + znzclose(fp); + return NULL; + } + ioff = (ii > ntot) ? ii-ntot : 0 ; + } else { /* non-negative offset */ + ioff = nim->iname_offset ; /* means use it directly */ + } + + /**- seek to the appropriate read position */ + if( znzseek(fp , (long)ioff , SEEK_SET) < 0 ){ + Rc_fprintf_stderr("** could not seek to offset %u in file '%s'\n", + (unsigned)ioff, nim->iname); + znzclose(fp); + return NULL; + } + + /**- and return the File pointer */ + return fp; +} + + +/*---------------------------------------------------------------------- + * nifti_image_load + *----------------------------------------------------------------------*/ +/*! \fn int nifti_image_load( nifti_image *nim ) + \brief Load the image blob into a previously initialized nifti_image. + + - If not yet set, the data buffer is allocated with calloc(). + - The data buffer will be byteswapped if necessary. + - The data buffer will not be scaled. + + This function is used to read the image from disk. It should be used + after a function such as nifti_image_read(), so that the nifti_image + structure is already initialized. + + \param nim pointer to a nifti_image (previously initialized) + \return 0 on success, -1 on failure + \sa nifti_image_read, nifti_image_free, nifti_image_unload +*/ +int nifti_image_load( nifti_image *nim ) +{ + /* set up data space, open data file and seek, then call nifti_read_buffer */ + size_t ntot , ii ; + znzFile fp ; + + /**- open the file and position the FILE pointer */ + fp = nifti_image_load_prep( nim ); + + if( fp == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_image_load, failed load_prep\n"); + return -1; + } + + ntot = nifti_get_volsize(nim); + + /**- if the data pointer is not yet set, get memory space for the image */ + + if( nim->data == NULL ) + { + nim->data = (void *)calloc(1,ntot) ; /* create image memory */ + if( nim->data == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** failed to alloc %d bytes for image data\n", + (int)ntot); + znzclose(fp); + return -1; + } + } + + /**- now that everything is set up, do the reading */ + ii = nifti_read_buffer(fp,nim->data,ntot,nim); + if( ii < ntot ){ + znzclose(fp) ; + free(nim->data) ; + nim->data = NULL ; + return -1 ; /* errors were printed in nifti_read_buffer() */ + } + + /**- close the file */ + znzclose( fp ) ; + + return 0 ; +} + + +/* 30 Nov 2004 [rickr] +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_read_buffer: %s\n",(msg)) ; \ + return 0; } while(0) +*/ + +/*----------------------------------------------------------------------*/ +/*! read ntot bytes of data from an open file and byte swaps if necessary + + note that nifti_image is required for information on datatype, bsize + (for any needed byte swapping), etc. + + This function does not allocate memory, so dataptr must be valid. +*//*--------------------------------------------------------------------*/ +size_t nifti_read_buffer(znzFile fp, void* dataptr, size_t ntot, + nifti_image *nim) +{ + size_t ii; + + if( dataptr == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** ERROR: nifti_read_buffer: NULL dataptr\n"); + return -1; + } + + ii = znzread( dataptr , 1 , ntot , fp ) ; /* data input */ + + /* if read was short, fail */ + if( ii < ntot ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("++ WARNING: nifti_read_buffer(%s):\n" + " data bytes needed = %u\n" + " data bytes input = %u\n" + " number missing = %u (set to 0)\n", + nim->iname , (unsigned int)ntot , + (unsigned int)ii , (unsigned int)(ntot-ii) ) ; + /* memset( (char *)(dataptr)+ii , 0 , ntot-ii ) ; now failure [rickr] */ + return -1 ; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d nifti_read_buffer: read %u bytes\n", (unsigned)ii); + + /* byte swap array if needed */ + + /* ntot/swapsize might not fit as int, use size_t 6 Jul 2010 [rickr] */ + if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_read_buffer: swapping data bytes...\n"); + nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; + } + +#if defined(isfinite) && !defined(USING_R) +{ + /* check input float arrays for goodness, and fix bad floats */ + int fix_count = 0 ; + + switch( nim->datatype ){ + + case NIFTI_TYPE_FLOAT32: + case NIFTI_TYPE_COMPLEX64:{ + float *far = (float *)dataptr ; size_t jj,nj ; + nj = ntot / sizeof(float) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + case NIFTI_TYPE_FLOAT64: + case NIFTI_TYPE_COMPLEX128:{ + double *far = (double *)dataptr ; size_t jj,nj ; + nj = ntot / sizeof(double) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d in image, %d bad floats were set to 0\n", fix_count); +} +#endif + + return ii; +} + +size_t nifti_assign_buffer(znzFile fp, void** data, size_t ntot, nifti_image *nim) +{ + size_t ii; + + if( data == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** ERROR: nifti_read_buffer: NULL dataptr\n"); + return -1; + } + + if (!fp->withz) { + Rc_fprintf_stderr("** ERROR: nifti_assign_buffer: should be called only for compressed data\n"); + return -1; + } + + #if HAVE_ZLIB + if (fp->zmemptr == NULL) { + Rc_fprintf_stderr("** ERROR: nifti_assign_buffer: should be called only for zmemptr type. e.g. memory buffer\n"); + return -1; + } + #endif + + ii = znzassign( data , 1 , ntot , fp ) ; /* data input */ + + + void *dataptr = *data; + if (dataptr == NULL) { + Rc_fprintf_stderr("buffer assing failed\n"); + return -1; + } + + /* if read was short, fail */ + if( ii < ntot ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("++ WARNING: nifti_read_buffer(%s):\n" + " data bytes needed = %u\n" + " data bytes input = %u\n" + " number missing = %u (set to 0)\n", + nim->iname , (unsigned int)ntot , + (unsigned int)ii , (unsigned int)(ntot-ii) ) ; + /* memset( (char *)(dataptr)+ii , 0 , ntot-ii ) ; now failure [rickr] */ + return -1 ; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d nifti_read_buffer: read %u bytes\n", (unsigned)ii); + + /* byte swap array if needed */ + + /* ntot/swapsize might not fit as int, use size_t 6 Jul 2010 [rickr] */ + if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_read_buffer: swapping data bytes...\n"); + nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; + } + +#if defined(isfinite) && !defined(USING_R) +{ + /* check input float arrays for goodness, and fix bad floats */ + int fix_count = 0 ; + + switch( nim->datatype ){ + + case NIFTI_TYPE_FLOAT32: + case NIFTI_TYPE_COMPLEX64:{ + float *far = (float *)dataptr ; size_t jj,nj ; + nj = ntot / sizeof(float) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + case NIFTI_TYPE_FLOAT64: + case NIFTI_TYPE_COMPLEX128:{ + double *far = (double *)dataptr ; size_t jj,nj ; + nj = ntot / sizeof(double) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d in image, %d bad floats were set to 0\n", fix_count); +} +#endif + + return ii; +} + +int nifti_image_is_data_owner(const nifti_image* nim) +{ + if( nim != NULL && nim->data != NULL ) { + return nim->decompressed_memory_buffer == nullptr; + } + return 0; +} + +/*--------------------------------------------------------------------------*/ +/*! Unload the data in a nifti_image struct, but keep the metadata. +*//*------------------------------------------------------------------------*/ +void nifti_image_unload( nifti_image *nim ) +{ + if( nim != NULL && nim->data != NULL ){ + if ( nifti_image_is_data_owner( nim ) ) { + free(nim->data); + } + nim->data = NULL; + nim->decompressed_memory_buffer = nullptr; + } + } + +/*--------------------------------------------------------------------------*/ +/*! free 'everything' about a nifti_image struct (including the passed struct) + + free (only fields which are not NULL): + - fname and iname + - data + - any ext_list[i].edata + - ext_list + - nim +*//*------------------------------------------------------------------------*/ +void nifti_image_free( nifti_image *nim ) +{ + if( nim == NULL ) return ; + if( nim->fname != NULL ) free(nim->fname) ; + if( nim->iname != NULL ) free(nim->iname) ; + if( nim->data != NULL ) { + if ( nifti_image_is_data_owner(nim) ) { + free(nim->data); + } + } + (void)nifti_free_extensions( nim ) ; + free(nim) ; +} + + +/*--------------------------------------------------------------------------*/ +/*! free the nifti extensions + + - If any edata pointer is set in the extension list, free() it. + - Free ext_list, if it is set. + - Clear num_ext and ext_list from nim. + + \return 0 on success, -1 on error + + \sa nifti_add_extension, nifti_copy_extensions +*//*------------------------------------------------------------------------*/ +int nifti_free_extensions( nifti_image *nim ) +{ + int c ; + if( nim == NULL ) return -1; + if( nim->num_ext > 0 && nim->ext_list ){ + for( c = 0; c < nim->num_ext; c++ ) + if ( nim->ext_list[c].edata ) free(nim->ext_list[c].edata); + free(nim->ext_list); + } + /* or if it is inconsistent, warn the user (if we are not in quiet mode) */ + else if ( (nim->num_ext > 0 || nim->ext_list != NULL) && (g_opts.debug > 0) ) + Rc_fprintf_stderr("** warning: nifti extension num/ptr mismatch (%d,%p)\n", + nim->num_ext, (void *)nim->ext_list); + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d free'd %d extension(s)\n", nim->num_ext); + + nim->num_ext = 0; + nim->ext_list = NULL; + + return 0; +} + + +/*--------------------------------------------------------------------------*/ +/*! Print to stdout some info about a nifti_image struct. +*//*------------------------------------------------------------------------*/ +void nifti_image_infodump( const nifti_image *nim ) +{ + char *str = nifti_image_to_ascii( nim ) ; + /* stdout -> stderr 2 Dec 2004 [rickr] */ + if( str != NULL ){ Rc_fputs_stderr(str) ; free(str) ; } + } + + +/*-------------------------------------------------------------------------- + * nifti_write_buffer just check for a null znzFile and call znzwrite + *--------------------------------------------------------------------------*/ +/*! \fn size_t nifti_write_buffer(znzFile fp, void *buffer, size_t numbytes) + \brief write numbytes of buffer to file, fp + + \param fp File pointer (from znzopen) to gzippable nifti datafile + \param buffer data buffer to be written + \param numbytes number of bytes in buffer to write + \return number of bytes successfully written +*/ +size_t nifti_write_buffer(znzFile fp, const void *buffer, size_t numbytes) +{ + /* Write all the image data at once (no swapping here) */ + size_t ss; + if (znz_isnull(fp)){ + Rc_fprintf_stderr("** ERROR: nifti_write_buffer: null file pointer\n"); + return 0; + } + ss = znzwrite( (const void*)buffer , 1 , numbytes , fp ) ; + return ss; +} + + +/*----------------------------------------------------------------------*/ +/*! write the nifti_image data to file (from nim->data or from NBL) + + If NBL is not NULL, write the data from that structure. Otherwise, + write it out from nim->data. No swapping is done here. + + \param fp : File pointer + \param nim : nifti_image corresponding to the data + \param NBL : optional source of write data (if NULL use nim->data) + + \return 0 on success, -1 on failure + + Note: the nifti_image byte_order is set as that of the current CPU. + This is because such a conversion was made to the data upon + reading, while byte_order was not set (so the programs would + know what format the data was on disk). Effectively, since + byte_order should match what is on disk, it should bet set to + that of the current CPU whenever new filenames are assigned. +*//*--------------------------------------------------------------------*/ +int nifti_write_all_data(znzFile fp, nifti_image * nim, + const nifti_brick_list * NBL) +{ + size_t ss; + int bnum; + + if( !NBL ){ /* just write one buffer and get out of here */ + if( nim->data == NULL ){ + Rc_fprintf_stderr("** NWAD: no image data to write\n"); + return -1; + } + + ss = nifti_write_buffer(fp,nim->data,nim->nbyper * nim->nvox); + if (ss < nim->nbyper * nim->nvox){ + Rc_fprintf_stderr( + "** ERROR: NWAD: wrote only %u of %u bytes to file\n", + (unsigned)ss, (unsigned)(nim->nbyper * nim->nvox)); + return -1; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d wrote single image of %u bytes\n", (unsigned)ss); + } else { + if( ! NBL->bricks || NBL->nbricks <= 0 || NBL->bsize <= 0 ){ + Rc_fprintf_stderr("** NWAD: no brick data to write (%p,%d,%u)\n", + (void *)NBL->bricks, NBL->nbricks, (unsigned)NBL->bsize); + return -1; + } + + for( bnum = 0; bnum < NBL->nbricks; bnum++ ){ + ss = nifti_write_buffer(fp, NBL->bricks[bnum], NBL->bsize); + if( ss < NBL->bsize ){ + Rc_fprintf_stderr( + "** NWAD ERROR: wrote %u of %u bytes of brick %d of %d to file", + (unsigned)ss, (unsigned)NBL->bsize, bnum+1, NBL->nbricks); + return -1; + } + } + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d wrote image of %d brick(s), each of %u bytes\n", + NBL->nbricks, (unsigned int)NBL->bsize); + } + + /* mark as being in this CPU byte order */ + nim->byteorder = nifti_short_order() ; + + return 0; +} + +/* return number of extensions written, or -1 on error */ +static int nifti_write_extensions(znzFile fp, nifti_image *nim) +{ + nifti1_extension * list; + char extdr[4] = { 0, 0, 0, 0 }; + int c, size, ok = 1; + + if( znz_isnull(fp) || !nim || nim->num_ext < 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_write_extensions, bad params\n"); + return -1; + } + + /* if no extensions and user requests it, skip extender */ + if( g_opts.skip_blank_ext && (nim->num_ext == 0 || ! nim->ext_list ) ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d no exts and skip_blank_ext set, " + "so skipping 4-byte extender\n"); + return 0; + } + + /* if invalid extension list, clear num_ext */ + if( ! valid_nifti_extensions(nim) ) nim->num_ext = 0; + + /* write out extender block */ + if( nim->num_ext > 0 ) extdr[0] = 1; + if( nifti_write_buffer(fp, extdr, 4) != 4 ){ + Rc_fprintf_stderr("** failed to write extender\n"); + return -1; + } + + list = nim->ext_list; + for ( c = 0; c < nim->num_ext; c++ ){ + size = (int)nifti_write_buffer(fp, &list->esize, sizeof(int)); + ok = (size == (int)sizeof(int)); + if( ok ){ + size = (int)nifti_write_buffer(fp, &list->ecode, sizeof(int)); + ok = (size == (int)sizeof(int)); + } + if( ok ){ + size = (int)nifti_write_buffer(fp, list->edata, list->esize - 8); + ok = (size == list->esize - 8); + } + + if( !ok ){ + Rc_fprintf_stderr("** failed while writing extension #%d\n",c); + return -1; + } else if ( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d wrote extension %d of %d bytes\n", c, size); + + list++; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d wrote out %d extension(s)\n", nim->num_ext); + + return nim->num_ext; +} + + +/*----------------------------------------------------------------------*/ +/*! basic initialization of a nifti_image struct (to a 1x1x1 image) +*//*--------------------------------------------------------------------*/ +nifti_image* nifti_simple_init_nim(void) +{ + nifti_image *nim; + struct nifti_1_header nhdr; + int nbyper, swapsize; + + memset(&nhdr,0,sizeof(nhdr)) ; /* zero out header, to be safe */ + + nhdr.sizeof_hdr = sizeof(nhdr) ; + nhdr.regular = 'r' ; /* for some stupid reason */ + + nhdr.dim[0] = 3 ; + nhdr.dim[1] = 1 ; nhdr.dim[2] = 1 ; nhdr.dim[3] = 1 ; + nhdr.dim[4] = 0 ; + + nhdr.pixdim[0] = 0.0f ; + nhdr.pixdim[1] = 1.0f ; nhdr.pixdim[2] = 1.0f ; + nhdr.pixdim[3] = 1.0f ; + + nhdr.datatype = DT_FLOAT32 ; + nifti_datatype_sizes( nhdr.datatype , &nbyper, &swapsize ); + nhdr.bitpix = 8 * nbyper ; + + strcpy(nhdr.magic, "n+1"); /* init to single file */ + + nim = nifti_convert_nhdr2nim(nhdr,NULL); + nim->fname = NULL; + nim->iname = NULL; + return nim; +} + + +/*----------------------------------------------------------------------*/ +/*! basic initialization of a nifti_1_header struct (with given dimensions) + + Return an allocated nifti_1_header struct, based on the given + dimensions and datatype. + + \param arg_dims : optional dim[8] array (default {3,1,1,1,0,0,0,0}) + \param arg_dtype : optional datatype (default DT_FLOAT32) + + \return pointer to allocated nifti_1_header struct +*//*--------------------------------------------------------------------*/ +nifti_1_header * nifti_make_new_header(const int arg_dims[], int arg_dtype) +{ + nifti_1_header * nhdr; + const int default_dims[8] = { 3, 1, 1, 1, 0, 0, 0, 0 }; + const int * dim; /* either passed or default dims */ + int dtype; /* either passed or default dtype */ + int c, nbyper, swapsize; + + /* if arg_dims is passed, apply it */ + if( arg_dims ) dim = arg_dims; + else dim = default_dims; + + /* validate dim: if there is any problem, apply default_dims */ + if( dim[0] < 1 || dim[0] > 7 ) { + Rc_fprintf_stderr("** nifti_simple_hdr_with_dims: bad dim[0]=%d\n",dim[0]); + dim = default_dims; + } else { + for( c = 1; c <= dim[0]; c++ ) + if( dim[c] < 1 ) + { + Rc_fprintf_stderr( + "** nifti_simple_hdr_with_dims: bad dim[%d]=%d\n",c,dim[c]); + dim = default_dims; + break; + } + } + + /* validate dtype, too */ + dtype = arg_dtype; + if( ! nifti_is_valid_datatype(dtype) ) { + Rc_fprintf_stderr("** nifti_simple_hdr_with_dims: bad dtype %d\n",dtype); + dtype = DT_FLOAT32; + } + + /* now populate the header struct */ + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_make_new_header, dim[0] = %d, datatype = %d\n", + dim[0], dtype); + + nhdr = (nifti_1_header *)calloc(1,sizeof(nifti_1_header)); + if( !nhdr ){ + Rc_fprintf_stderr("** nifti_make_new_header: failed to alloc hdr\n"); + return NULL; + } + + nhdr->sizeof_hdr = sizeof(nifti_1_header) ; + nhdr->regular = 'r' ; /* for some stupid reason */ + + /* init dim and pixdim */ + nhdr->dim[0] = dim[0] ; + nhdr->pixdim[0] = 0.0f; + for( c = 1; c <= dim[0]; c++ ) { + nhdr->dim[c] = dim[c]; + nhdr->pixdim[c] = 1.0f; + } + + nhdr->datatype = dtype ; + nifti_datatype_sizes( nhdr->datatype , &nbyper, &swapsize ); + nhdr->bitpix = 8 * nbyper ; + + strcpy(nhdr->magic, "n+1"); /* init to single file */ + + return nhdr; +} + + +/*----------------------------------------------------------------------*/ +/*! basic creation of a nifti_image struct + + Create a nifti_image from the given dimensions and data type. + Optinally, allocate zero-filled data. + + \param dims : optional dim[8] (default {3,1,1,1,0,0,0,0}) + \param datatype : optional datatype (default DT_FLOAT32) + \param data_fill : if flag is set, allocate zero-filled data for image + + \return pointer to allocated nifti_image struct +*//*--------------------------------------------------------------------*/ +nifti_image * nifti_make_new_nim(const int dims[], int datatype, int data_fill) +{ + nifti_image * nim; + nifti_1_header * nhdr; + + nhdr = nifti_make_new_header(dims, datatype); + if( !nhdr ) return NULL; /* error already printed */ + + nim = nifti_convert_nhdr2nim(*nhdr,NULL); + free(nhdr); /* in any case, we are done with this */ + if( !nim ){ + Rc_fprintf_stderr("** NMNN: nifti_convert_nhdr2nim failure\n"); + return NULL; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_make_new_nim, data_fill = %d\n",data_fill); + + if( data_fill ) { + nim->data = calloc(nim->nvox, nim->nbyper); + + /* if we cannot allocate data, take ball and go home */ + if( !nim->data ) { + Rc_fprintf_stderr("** NMNN: failed to alloc %u bytes for data\n", + (unsigned)(nim->nvox*nim->nbyper)); + nifti_image_free(nim); + nim = NULL; + } + } + + return nim; +} + + +/*----------------------------------------------------------------------*/ +/*! convert a nifti_image structure to a nifti_1_header struct + + No allocation is done, this should be used via structure copy. + As in: +
+    nifti_1_header my_header;
+    my_header = nifti_convert_nim2nhdr(my_nim_pointer);
+    
+*//*--------------------------------------------------------------------*/ +struct nifti_1_header nifti_convert_nim2nhdr(const nifti_image * nim) +{ + struct nifti_1_header nhdr; + + memset(&nhdr,0,sizeof(nhdr)) ; /* zero out header, to be safe */ + + + /**- load the ANALYZE-7.5 generic parts of the header struct */ + + nhdr.sizeof_hdr = sizeof(nhdr) ; + nhdr.regular = 'r' ; /* for some stupid reason */ + + nhdr.dim[0] = nim->ndim ; + nhdr.dim[1] = nim->nx ; nhdr.dim[2] = nim->ny ; nhdr.dim[3] = nim->nz ; + nhdr.dim[4] = nim->nt ; nhdr.dim[5] = nim->nu ; nhdr.dim[6] = nim->nv ; + nhdr.dim[7] = nim->nw ; + + nhdr.pixdim[0] = 0.0f ; + nhdr.pixdim[1] = nim->dx ; nhdr.pixdim[2] = nim->dy ; + nhdr.pixdim[3] = nim->dz ; nhdr.pixdim[4] = nim->dt ; + nhdr.pixdim[5] = nim->du ; nhdr.pixdim[6] = nim->dv ; + nhdr.pixdim[7] = nim->dw ; + + nhdr.datatype = nim->datatype ; + nhdr.bitpix = 8 * nim->nbyper ; + + if( nim->cal_max > nim->cal_min ){ + nhdr.cal_max = nim->cal_max ; + nhdr.cal_min = nim->cal_min ; + } + + if( nim->scl_slope != 0.0 ){ + nhdr.scl_slope = nim->scl_slope ; + nhdr.scl_inter = nim->scl_inter ; + } + + if( nim->descrip[0] != '\0' ){ + memcpy(nhdr.descrip ,nim->descrip ,79) ; nhdr.descrip[79] = '\0' ; + } + if( nim->aux_file[0] != '\0' ){ + memcpy(nhdr.aux_file ,nim->aux_file ,23) ; nhdr.aux_file[23] = '\0' ; + } + + /**- Load NIFTI specific stuff into the header */ + + if( nim->nifti_type > NIFTI_FTYPE_ANALYZE ){ /* then not ANALYZE */ + + if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcpy(nhdr.magic,"n+1") ; + else strcpy(nhdr.magic,"ni1") ; + + nhdr.pixdim[1] = (float)fabs(nhdr.pixdim[1]) ; nhdr.pixdim[2] = (float)fabs(nhdr.pixdim[2]) ; + nhdr.pixdim[3] = (float)fabs(nhdr.pixdim[3]) ; nhdr.pixdim[4] = (float)fabs(nhdr.pixdim[4]) ; + nhdr.pixdim[5] = (float)fabs(nhdr.pixdim[5]) ; nhdr.pixdim[6] = (float)fabs(nhdr.pixdim[6]) ; + nhdr.pixdim[7] = (float)fabs(nhdr.pixdim[7]) ; + + nhdr.intent_code = nim->intent_code ; + nhdr.intent_p1 = nim->intent_p1 ; + nhdr.intent_p2 = nim->intent_p2 ; + nhdr.intent_p3 = nim->intent_p3 ; + if( nim->intent_name[0] != '\0' ){ + memcpy(nhdr.intent_name,nim->intent_name,15) ; + nhdr.intent_name[15] = '\0' ; + } + + nhdr.vox_offset = (float) nim->iname_offset ; + nhdr.xyzt_units = SPACE_TIME_TO_XYZT( nim->xyz_units, nim->time_units ) ; + nhdr.toffset = nim->toffset ; + + if( nim->qform_code > 0 ){ + nhdr.qform_code = nim->qform_code ; + nhdr.quatern_b = nim->quatern_b ; + nhdr.quatern_c = nim->quatern_c ; + nhdr.quatern_d = nim->quatern_d ; + nhdr.qoffset_x = nim->qoffset_x ; + nhdr.qoffset_y = nim->qoffset_y ; + nhdr.qoffset_z = nim->qoffset_z ; + nhdr.pixdim[0] = (nim->qfac >= 0.0) ? 1.0f : -1.0f ; + } + + if( nim->sform_code > 0 ){ + nhdr.sform_code = nim->sform_code ; + nhdr.srow_x[0] = nim->sto_xyz.m[0][0] ; + nhdr.srow_x[1] = nim->sto_xyz.m[0][1] ; + nhdr.srow_x[2] = nim->sto_xyz.m[0][2] ; + nhdr.srow_x[3] = nim->sto_xyz.m[0][3] ; + nhdr.srow_y[0] = nim->sto_xyz.m[1][0] ; + nhdr.srow_y[1] = nim->sto_xyz.m[1][1] ; + nhdr.srow_y[2] = nim->sto_xyz.m[1][2] ; + nhdr.srow_y[3] = nim->sto_xyz.m[1][3] ; + nhdr.srow_z[0] = nim->sto_xyz.m[2][0] ; + nhdr.srow_z[1] = nim->sto_xyz.m[2][1] ; + nhdr.srow_z[2] = nim->sto_xyz.m[2][2] ; + nhdr.srow_z[3] = nim->sto_xyz.m[2][3] ; + } + + nhdr.dim_info = FPS_INTO_DIM_INFO( nim->freq_dim , + nim->phase_dim , nim->slice_dim ) ; + nhdr.slice_code = nim->slice_code ; + nhdr.slice_start = nim->slice_start ; + nhdr.slice_end = nim->slice_end ; + nhdr.slice_duration = nim->slice_duration ; + } + + return nhdr; +} + + +/*----------------------------------------------------------------------*/ +/*! \fn int nifti_copy_extensions(nifti_image * nim_dest, nifti_image * nim_src) + \brief copy the nifti1_extension list from src to dest + + Duplicate the list of nifti1_extensions. The dest structure must + be clear of extensions. + \return 0 on success, -1 on failure + + \sa nifti_add_extension, nifti_free_extensions +*/ +int nifti_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) +{ + char * data; + size_t bytes; + int c, size, old_size; + + if( nim_dest->num_ext > 0 || nim_dest->ext_list != NULL ){ + Rc_fprintf_stderr("** will not copy extensions over existing ones\n"); + return -1; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d duplicating %d extension(s)\n", nim_src->num_ext); + + if( nim_src->num_ext <= 0 ) return 0; + + bytes = nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ + nim_dest->ext_list = (nifti1_extension *)malloc(bytes); + if( !nim_dest->ext_list ){ + Rc_fprintf_stderr("** failed to allocate %d nifti1_extension structs\n", + nim_src->num_ext); + return -1; + } + + /* copy the extension data */ + nim_dest->num_ext = 0; + for( c = 0; c < nim_src->num_ext; c++ ){ + size = old_size = nim_src->ext_list[c].esize; + if( size & 0xf ) size = (size + 0xf) & ~0xf; /* make multiple of 16 */ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d dup'ing ext #%d of size %d (from size %d)\n", + c, size, old_size); + /* data length is size-8, as esize includes space for esize and ecode */ + data = (char *)calloc(size-8,sizeof(char)); /* maybe size > old */ + if( !data ){ + Rc_fprintf_stderr("** failed to alloc %d bytes for extention\n", size); + if( c == 0 ) { free(nim_dest->ext_list); nim_dest->ext_list = NULL; } + /* otherwise, keep what we have (a.o.t. deleting them all) */ + return -1; + } + /* finally, fill the new structure */ + nim_dest->ext_list[c].esize = size; + nim_dest->ext_list[c].ecode = nim_src->ext_list[c].ecode; + nim_dest->ext_list[c].edata = data; + memcpy(data, nim_src->ext_list[c].edata, old_size-8); + + nim_dest->num_ext++; + } + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! compute the total size of all extensions + + \return the total of all esize fields + + Note that each esize includes 4 bytes for ecode, 4 bytes for esize, + and the bytes used for the data. Each esize also needs to be a + multiple of 16, so it may be greater than the sum of its 3 parts. +*//*--------------------------------------------------------------------*/ +int nifti_extension_size(nifti_image *nim) +{ + int c, size = 0; + + if( !nim || nim->num_ext <= 0 ) return 0; + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d ext sizes:"); + + for ( c = 0; c < nim->num_ext; c++ ){ + size += nim->ext_list[c].esize; + if( g_opts.debug > 2 ) Rc_fprintf_stderr(" %d",nim->ext_list[c].esize); + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr(" (total = %d)\n",size); + + return size; +} + + +/*----------------------------------------------------------------------*/ +/*! set the nifti_image iname_offset field, based on nifti_type + + - if writing to 2 files, set offset to 0 + - if writing to a single NIFTI-1 file, set the offset to + 352 + total extension size, then align to 16-byte boundary + - if writing an ASCII header, set offset to -1 +*//*--------------------------------------------------------------------*/ +void nifti_set_iname_offset(nifti_image *nim) +{ + int offset; + + switch( nim->nifti_type ){ + + default: /* writing into 2 files */ + /* we only write files with 0 offset in the 2 file format */ + nim->iname_offset = 0 ; + break ; + + /* NIFTI-1 single binary file - always update */ + case NIFTI_FTYPE_NIFTI1_1: + offset = nifti_extension_size(nim)+sizeof(struct nifti_1_header)+4; + /* be sure offset is aligned to a 16 byte boundary */ + if ( ( offset % 16 ) != 0 ) offset = ((offset + 0xf) & ~0xf); + if( nim->iname_offset != offset ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d changing offset from %d to %d\n", + nim->iname_offset, offset); + nim->iname_offset = offset; + } + break ; + + /* non-standard case: NIFTI-1 ASCII header + binary data (single file) */ + case NIFTI_FTYPE_ASCII: + nim->iname_offset = -1 ; /* compute offset from filesize */ + break ; + } +} + + +/*----------------------------------------------------------------------*/ +/*! write the nifti_image dataset to disk, optionally including data + + This is just a front-end for nifti_image_write_hdr_img2. + + \param nim nifti_image to write to disk + \param write_data write options (see nifti_image_write_hdr_img2) + \param opts file open options ("wb" from nifti_image_write) + + \sa nifti_image_write, nifti_image_write_hdr_img2, nifti_image_free, + nifti_set_filenames +*//*--------------------------------------------------------------------*/ +znzFile nifti_image_write_hdr_img( nifti_image *nim , int write_data , + const char* opts ) +{ + return nifti_image_write_hdr_img2(nim,write_data,opts,NULL,NULL); +} + + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_image_write_hdr_img: %s\n",(msg)) ; \ + return fp ; } while(0) + + +/* ----------------------------------------------------------------------*/ +/*! This writes the header (and optionally the image data) to file + * + * If the image data file is left open it returns a valid znzFile handle. + * It also uses imgfile as the open image file is not null, and modifies + * it inside. + * + * \param nim nifti_image to write to disk + * \param write_opts flags whether to write data and/or close file (see below) + * \param opts file-open options, probably "wb" from nifti_image_write() + * \param imgfile optional open znzFile struct, for writing image data + (may be NULL) + * \param NBL optional nifti_brick_list, containing the image data + (may be NULL) + * + * Values for write_opts mode are based on two binary flags + * ( 0/1 for no-write/write data, and 0/2 for close/leave-open files ) : + * - 0 = do not write data and close (do not open data file) + * - 1 = write data and close + * - 2 = do not write data and leave data file open + * - 3 = write data and leave data file open + * + * \sa nifti_image_write, nifti_image_write_hdr_img, nifti_image_free, + * nifti_set_filenames +*//*---------------------------------------------------------------------*/ +znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, + const char * opts, znzFile imgfile, const nifti_brick_list * NBL) +{ + struct nifti_1_header nhdr ; + znzFile fp=NULL; + size_t ss ; + int write_data, leave_open; + char func[] = { "nifti_image_write_hdr_img2" }; + + write_data = write_opts & 1; /* just separate the bits now */ + leave_open = write_opts & 2; + + if( ! nim ) ERREX("NULL input") ; + if( ! nifti_validfilename(nim->fname) ) ERREX("bad fname input") ; + if( write_data && ! nim->data && ! NBL ) ERREX("no image data") ; + + if( write_data && NBL && ! nifti_NBL_matches_nim(nim, NBL) ) + ERREX("NBL does not match nim"); + + nifti_set_iname_offset(nim); + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("-d writing nifti file '%s'...\n", nim->fname); + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nifti type %d, offset %d\n", + nim->nifti_type, nim->iname_offset); + } + + if( nim->nifti_type == NIFTI_FTYPE_ASCII ) /* non-standard case */ + return nifti_write_ascii_image(nim,NBL,opts,write_data,leave_open); + + nhdr = nifti_convert_nim2nhdr(nim); /* create the nifti1_header struct */ + + /* if writing to 2 files, make sure iname is set and different from fname */ + if( nim->nifti_type != NIFTI_FTYPE_NIFTI1_1 ){ + if( nim->iname && strcmp(nim->iname,nim->fname) == 0 ){ + free(nim->iname) ; nim->iname = NULL ; + } + if( nim->iname == NULL ){ /* then make a new one */ + nim->iname = nifti_makeimgname(nim->fname,nim->nifti_type,0,0); + if( nim->iname == NULL ) return NULL; + } + } + + /* if we have an imgfile and will write the header there, use it */ + if( ! znz_isnull(imgfile) && nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("+d using passed file for hdr\n"); + fp = imgfile; + } + else { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d opening output file %s [%s]\n",nim->fname,opts); + fp = znzopen( nim->fname , opts , nifti_is_gzfile(nim->fname) ) ; + if( znz_isnull(fp) ){ + LNI_FERR(func,"cannot open output file",nim->fname); + return fp; + } + } + + /* write the header and extensions */ + + ss = znzwrite(&nhdr , 1 , sizeof(nhdr) , fp); /* write header */ + if( ss < sizeof(nhdr) ){ + LNI_FERR(func,"bad header write to output file",nim->fname); + znzclose(fp); return fp; + } + + /* partial file exists, and errors have been printed, so ignore return */ + if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + (void)nifti_write_extensions(fp,nim); + + /* if the header is all we want, we are done */ + if( ! write_data && ! leave_open ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d header is all we want: done\n"); + znzclose(fp); return(fp); + } + + if( nim->nifti_type != NIFTI_FTYPE_NIFTI1_1 ){ /* get a new file pointer */ + znzclose(fp); /* first, close header file */ + if( ! znz_isnull(imgfile) ){ + if(g_opts.debug > 2) Rc_fprintf_stderr("+d using passed file for img\n"); + fp = imgfile; + } + else { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d opening img file '%s'\n", nim->iname); + fp = znzopen( nim->iname , opts , nifti_is_gzfile(nim->iname) ) ; + if( znz_isnull(fp) ) ERREX("cannot open image file") ; + } + } + + znzseek(fp, nim->iname_offset, SEEK_SET); /* in any case, seek to offset */ + + if( write_data ) nifti_write_all_data(fp,nim,NBL); + if( ! leave_open ) znzclose(fp); + + return fp; +} + + +/*----------------------------------------------------------------------*/ +/*! write a nifti_image to disk in ASCII format +*//*--------------------------------------------------------------------*/ +znzFile nifti_write_ascii_image(nifti_image *nim, const nifti_brick_list * NBL, + const char *opts, int write_data, int leave_open) +{ + znzFile fp; + char * hstr; + + hstr = nifti_image_to_ascii( nim ) ; /* get header in ASCII form */ + if( ! hstr ){ Rc_fprintf_stderr("** failed image_to_ascii()\n"); return NULL; } + + fp = znzopen( nim->fname , opts , nifti_is_gzfile(nim->fname) ) ; + if( znz_isnull(fp) ){ + free(hstr); + Rc_fprintf_stderr("** failed to open '%s' for ascii write\n",nim->fname); + return fp; + } + + znzputs(hstr,fp); /* header */ + nifti_write_extensions(fp,nim); /* extensions */ + + if ( write_data ) { nifti_write_all_data(fp,nim,NBL); } /* data */ + if ( ! leave_open ) { znzclose(fp); } + free(hstr); + return fp; /* returned but may be closed */ +} + + +/*--------------------------------------------------------------------------*/ +/*! Write a nifti_image to disk. + + Since data is properly byte-swapped upon reading, it is assumed + to be in the byte-order of the current CPU at write time. Thus, + nim->byte_order should match that of the current CPU. Note that + the nifti_set_filenames() function takes the flag, set_byte_order. + + The following fields of nim affect how the output appears: + - nifti_type = 0 ==> ANALYZE-7.5 format file pair will be written + - nifti_type = 1 ==> NIFTI-1 format single file will be written + (data offset will be 352+extensions) + - nifti_type = 2 ==> NIFTI_1 format file pair will be written + - nifti_type = 3 ==> NIFTI_1 ASCII single file will be written + - fname is the name of the output file (header or header+data) + - if a file pair is being written, iname is the name of the data file + - existing files WILL be overwritten with extreme prejudice + - if qform_code > 0, the quatern_*, qoffset_*, and qfac fields determine + the qform output, NOT the qto_xyz matrix; if you want to compute these + fields from the qto_xyz matrix, you can use the utility function + nifti_mat44_to_quatern() + + \sa nifti_image_write_bricks, nifti_image_free, nifti_set_filenames, + nifti_image_write_hdr_img +*//*------------------------------------------------------------------------*/ +void nifti_image_write( nifti_image *nim ) +{ + znzFile fp = nifti_image_write_hdr_img(nim,1,"wb"); + if( fp ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d niw: done with znzFile\n"); + free(fp); + } + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d nifti_image_write: done\n"); +} + + +/*----------------------------------------------------------------------*/ +/*! similar to nifti_image_write, but data is in NBL struct, not nim->data + + \sa nifti_image_write, nifti_image_free, nifti_set_filenames, nifti_free_NBL +*//*--------------------------------------------------------------------*/ +void nifti_image_write_bricks( nifti_image *nim, const nifti_brick_list * NBL ) +{ + znzFile fp = nifti_image_write_hdr_img2(nim,1,"wb",NULL,NBL); + if( fp ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d niwb: done with znzFile\n"); + free(fp); + } + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d niwb: done writing bricks\n"); +} + +size_t nifti_image_impl_reserved_bytes_offset() +{ + return (size_t)&(((nifti_image*)(NULL))->decompressed_memory_buffer); +} + +/*----------------------------------------------------------------------*/ +/*! copy the nifti_image structure, without data + + Duplicate the structure, including fname, iname and extensions. + Leave the data pointer as NULL. +*//*--------------------------------------------------------------------*/ +nifti_image * nifti_copy_nim_info(const nifti_image * src) +{ + nifti_image *dest; + dest = (nifti_image *)calloc(1,sizeof(nifti_image)); + if( !dest ){ + Rc_fprintf_stderr("** NCNI: failed to alloc nifti_image\n"); + return NULL; + } + + // copy nifti_image up to impl reserved bytes + memcpy(dest, src, nifti_image_impl_reserved_bytes_offset()); + if( src->fname ) dest->fname = nifti_strdup(src->fname); + if( src->iname ) dest->iname = nifti_strdup(src->iname); + dest->num_ext = 0; + dest->ext_list = NULL; + /* errors will be printed in NCE(), continue in either case */ + (void)nifti_copy_extensions(dest, src); + + dest->data = NULL; + // retain decompressed_memory_buffer if any, to keep alive. + dest->decompressed_memory_buffer = src->decompressed_memory_buffer; + + return dest; +} + + +/*------------------------------------------------------------------------*/ +/* Un-escape a C string in place -- that is, convert XML escape sequences + back into their characters. (This can be done in place since the + replacement is always smaller than the input.) Escapes recognized are: + - < -> < + - > -> > + - " -> " + - ' -> ' + - & -> & + Also replace CR LF pair (Microsoft), or CR alone (Macintosh) with + LF (Unix), per the XML standard. + Return value is number of replacements made (if you care). +--------------------------------------------------------------------------*/ + +#undef CR +#undef LF +#define CR 0x0D +#define LF 0x0A + +static int unescape_string( char *str ) +{ + int ii,jj , nn,ll ; + + if( str == NULL ) return 0 ; /* no string? */ + ll = (int)strlen(str) ; if( ll == 0 ) return 0 ; + + /* scan for escapes: &something; */ + + for( ii=jj=nn=0 ; ii': lout += 4 ; break ; /* replace '<' with "<" */ + + case '"' : + case '\'': lout += 6 ; break ; /* replace '"' with """ */ + + case CR: + case LF: lout += 6 ; break ; /* replace CR with " " + LF with " " */ + + default: lout++ ; break ; /* copy all other chars */ + } + } + out = (char *)calloc(1,lout) ; /* allocate output string */ + if( !out ){ + Rc_fprintf_stderr("** escapize_string: failed to alloc %d bytes\n",lout); + return NULL; + } + out[0] = '\'' ; /* opening quote mark */ + for( ii=0,jj=1 ; ii < lstr ; ii++ ){ + switch( str[ii] ){ + default: out[jj++] = str[ii] ; break ; /* normal characters */ + + case '&': memcpy(out+jj,"&",5) ; jj+=5 ; break ; + + case '<': memcpy(out+jj,"<",4) ; jj+=4 ; break ; + case '>': memcpy(out+jj,">",4) ; jj+=4 ; break ; + + case '"' : memcpy(out+jj,""",6) ; jj+=6 ; break ; + + case '\'': memcpy(out+jj,"'",6) ; jj+=6 ; break ; + + case CR: memcpy(out+jj," ",6) ; jj+=6 ; break ; + case LF: memcpy(out+jj," ",6) ; jj+=6 ; break ; + } + } + out[jj++] = '\'' ; /* closing quote mark */ + out[jj] = '\0' ; /* terminate the string */ + return out ; +} + +/*---------------------------------------------------------------------------*/ +/*! Dump the information in a NIFTI image header to an XML-ish ASCII string + that can later be converted back into a NIFTI header in + nifti_image_from_ascii(). + + The resulting string can be free()-ed when you are done with it. +*//*-------------------------------------------------------------------------*/ +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +#endif // __clang__ +char *nifti_image_to_ascii( const nifti_image *nim ) +{ + char *buf , *ebuf ; int nbuf ; + + if( nim == NULL ) return NULL ; /* stupid caller */ + + buf = (char *)calloc(1,65534); /* longer than needed, to be safe */ + if( !buf ){ + Rc_fprintf_stderr("** NITA: failed to alloc %d bytes\n",65534); + return NULL; + } + + sprintf( buf , "nifti_type == NIFTI_FTYPE_NIFTI1_1) ? "NIFTI-1+" + :(nim->nifti_type == NIFTI_FTYPE_NIFTI1_2) ? "NIFTI-1" + :(nim->nifti_type == NIFTI_FTYPE_ASCII ) ? "NIFTI-1A" + : "ANALYZE-7.5" ) ; + + /** Strings that we don't control (filenames, etc.) that might + contain "weird" characters (like quotes) are "escaped": + - A few special characters are replaced by XML-style escapes, using + the function escapize_string(). + - On input, function unescape_string() reverses this process. + - The result is that the NIFTI ASCII-format header is XML-compliant. */ + + ebuf = escapize_string(nim->fname) ; + sprintf( buf+strlen(buf) , " header_filename = %s\n",ebuf); free(ebuf); + + ebuf = escapize_string(nim->iname) ; + sprintf( buf+strlen(buf) , " image_filename = %s\n", ebuf); free(ebuf); + + sprintf( buf+strlen(buf) , " image_offset = '%d'\n" , nim->iname_offset ); + + sprintf(buf + strlen(buf), " ndim = '%d'\n", nim->ndim); + sprintf(buf + strlen(buf), " nx = '%d'\n", nim->nx); + if (nim->ndim > 1) + sprintf(buf + strlen(buf), " ny = '%d'\n", nim->ny); + if (nim->ndim > 2) + sprintf(buf + strlen(buf), " nz = '%d'\n", nim->nz); + if (nim->ndim > 3) + sprintf(buf + strlen(buf), " nt = '%d'\n", nim->nt); + if (nim->ndim > 4) + sprintf(buf + strlen(buf), " nu = '%d'\n", nim->nu); + if (nim->ndim > 5) + sprintf(buf + strlen(buf), " nv = '%d'\n", nim->nv); + if (nim->ndim > 6) + sprintf(buf + strlen(buf), " nw = '%d'\n", nim->nw); + sprintf(buf + strlen(buf), " dx = '%g'\n", nim->dx); + if (nim->ndim > 1) + sprintf(buf + strlen(buf), " dy = '%g'\n", nim->dy); + if (nim->ndim > 2) + sprintf(buf + strlen(buf), " dz = '%g'\n", nim->dz); + if (nim->ndim > 3) + sprintf(buf + strlen(buf), " dt = '%g'\n", nim->dt); + if (nim->ndim > 4) + sprintf(buf + strlen(buf), " du = '%g'\n", nim->du); + if (nim->ndim > 5) + sprintf(buf + strlen(buf), " dv = '%g'\n", nim->dv); + if (nim->ndim > 6) + sprintf(buf + strlen(buf), " dw = '%g'\n", nim->dw); + + sprintf( buf+strlen(buf) , " datatype = '%d'\n" , nim->datatype ) ; + sprintf( buf+strlen(buf) , " datatype_name = '%s'\n" , + nifti_datatype_string(nim->datatype) ) ; + + sprintf( buf+strlen(buf) , " nvox = '%u'\n" , (unsigned)nim->nvox ) ; + sprintf( buf+strlen(buf) , " nbyper = '%d'\n" , nim->nbyper ) ; + + sprintf( buf+strlen(buf) , " byteorder = '%s'\n" , + (nim->byteorder==MSB_FIRST) ? "MSB_FIRST" : "LSB_FIRST" ) ; + + if( nim->cal_min < nim->cal_max ){ + sprintf( buf+strlen(buf) , " cal_min = '%g'\n", nim->cal_min ) ; + sprintf( buf+strlen(buf) , " cal_max = '%g'\n", nim->cal_max ) ; + } + + if( nim->scl_slope != 0.0 ){ + sprintf( buf+strlen(buf) , " scl_slope = '%g'\n" , nim->scl_slope ) ; + sprintf( buf+strlen(buf) , " scl_inter = '%g'\n" , nim->scl_inter ) ; + } + + if( nim->intent_code > 0 ){ + sprintf( buf+strlen(buf) , " intent_code = '%d'\n", nim->intent_code ) ; + sprintf( buf+strlen(buf) , " intent_code_name = '%s'\n" , + nifti_intent_string(nim->intent_code) ) ; + sprintf( buf+strlen(buf) , " intent_p1 = '%g'\n" , nim->intent_p1 ) ; + sprintf( buf+strlen(buf) , " intent_p2 = '%g'\n" , nim->intent_p2 ) ; + sprintf( buf+strlen(buf) , " intent_p3 = '%g'\n" , nim->intent_p3 ) ; + + if( nim->intent_name[0] != '\0' ){ + ebuf = escapize_string(nim->intent_name) ; + sprintf( buf+strlen(buf) , " intent_name = %s\n",ebuf) ; + free(ebuf) ; + } + } + + if( nim->toffset != 0.0 ) + sprintf( buf+strlen(buf) , " toffset = '%g'\n",nim->toffset ) ; + + if( nim->xyz_units > 0 ) + sprintf( buf+strlen(buf) , + " xyz_units = '%d'\n" + " xyz_units_name = '%s'\n" , + nim->xyz_units , nifti_units_string(nim->xyz_units) ) ; + + if( nim->time_units > 0 ) + sprintf( buf+strlen(buf) , + " time_units = '%d'\n" + " time_units_name = '%s'\n" , + nim->time_units , nifti_units_string(nim->time_units) ) ; + + if( nim->freq_dim > 0 ) + sprintf( buf+strlen(buf) , " freq_dim = '%d'\n",nim->freq_dim ) ; + if( nim->phase_dim > 0 ) + sprintf( buf+strlen(buf) , " phase_dim = '%d'\n",nim->phase_dim ) ; + if( nim->slice_dim > 0 ) + sprintf( buf+strlen(buf) , " slice_dim = '%d'\n",nim->slice_dim ) ; + if( nim->slice_code > 0 ) + sprintf( buf+strlen(buf) , + " slice_code = '%d'\n" + " slice_code_name = '%s'\n" , + nim->slice_code , nifti_slice_string(nim->slice_code) ) ; + if( nim->slice_start >= 0 && nim->slice_end > nim->slice_start ) + sprintf( buf+strlen(buf) , + " slice_start = '%d'\n" + " slice_end = '%d'\n" , nim->slice_start , nim->slice_end ) ; + if( nim->slice_duration != 0.0 ) + sprintf( buf+strlen(buf) , " slice_duration = '%g'\n", + nim->slice_duration ) ; + + if( nim->descrip[0] != '\0' ){ + ebuf = escapize_string(nim->descrip) ; + sprintf( buf+strlen(buf) , " descrip = %s\n",ebuf) ; + free(ebuf) ; + } + + if( nim->aux_file[0] != '\0' ){ + ebuf = escapize_string(nim->aux_file) ; + sprintf( buf+strlen(buf) , " aux_file = %s\n",ebuf) ; + free(ebuf) ; + } + + if( nim->qform_code > 0 ){ + int i,j,k ; + + sprintf( buf+strlen(buf) , + " qform_code = '%d'\n" + " qform_code_name = '%s'\n" + " qto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->qform_code , nifti_xform_string(nim->qform_code) , + nim->qto_xyz.m[0][0] , nim->qto_xyz.m[0][1] , + nim->qto_xyz.m[0][2] , nim->qto_xyz.m[0][3] , + nim->qto_xyz.m[1][0] , nim->qto_xyz.m[1][1] , + nim->qto_xyz.m[1][2] , nim->qto_xyz.m[1][3] , + nim->qto_xyz.m[2][0] , nim->qto_xyz.m[2][1] , + nim->qto_xyz.m[2][2] , nim->qto_xyz.m[2][3] , + nim->qto_xyz.m[3][0] , nim->qto_xyz.m[3][1] , + nim->qto_xyz.m[3][2] , nim->qto_xyz.m[3][3] ) ; + + sprintf( buf+strlen(buf) , + " qto_ijk_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->qto_ijk.m[0][0] , nim->qto_ijk.m[0][1] , + nim->qto_ijk.m[0][2] , nim->qto_ijk.m[0][3] , + nim->qto_ijk.m[1][0] , nim->qto_ijk.m[1][1] , + nim->qto_ijk.m[1][2] , nim->qto_ijk.m[1][3] , + nim->qto_ijk.m[2][0] , nim->qto_ijk.m[2][1] , + nim->qto_ijk.m[2][2] , nim->qto_ijk.m[2][3] , + nim->qto_ijk.m[3][0] , nim->qto_ijk.m[3][1] , + nim->qto_ijk.m[3][2] , nim->qto_ijk.m[3][3] ) ; + + sprintf( buf+strlen(buf) , + " quatern_b = '%g'\n" + " quatern_c = '%g'\n" + " quatern_d = '%g'\n" + " qoffset_x = '%g'\n" + " qoffset_y = '%g'\n" + " qoffset_z = '%g'\n" + " qfac = '%g'\n" , + nim->quatern_b , nim->quatern_c , nim->quatern_d , + nim->qoffset_x , nim->qoffset_y , nim->qoffset_z , nim->qfac ) ; + + nifti_mat44_to_orientation( nim->qto_xyz , &i,&j,&k ) ; + if( i > 0 && j > 0 && k > 0 ) + sprintf( buf+strlen(buf) , + " qform_i_orientation = '%s'\n" + " qform_j_orientation = '%s'\n" + " qform_k_orientation = '%s'\n" , + nifti_orientation_string(i) , + nifti_orientation_string(j) , + nifti_orientation_string(k) ) ; + } + + if( nim->sform_code > 0 ){ + int i,j,k ; + + sprintf( buf+strlen(buf) , + " sform_code = '%d'\n" + " sform_code_name = '%s'\n" + " sto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->sform_code , nifti_xform_string(nim->sform_code) , + nim->sto_xyz.m[0][0] , nim->sto_xyz.m[0][1] , + nim->sto_xyz.m[0][2] , nim->sto_xyz.m[0][3] , + nim->sto_xyz.m[1][0] , nim->sto_xyz.m[1][1] , + nim->sto_xyz.m[1][2] , nim->sto_xyz.m[1][3] , + nim->sto_xyz.m[2][0] , nim->sto_xyz.m[2][1] , + nim->sto_xyz.m[2][2] , nim->sto_xyz.m[2][3] , + nim->sto_xyz.m[3][0] , nim->sto_xyz.m[3][1] , + nim->sto_xyz.m[3][2] , nim->sto_xyz.m[3][3] ) ; + + sprintf( buf+strlen(buf) , + " sto_ijk matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->sto_ijk.m[0][0] , nim->sto_ijk.m[0][1] , + nim->sto_ijk.m[0][2] , nim->sto_ijk.m[0][3] , + nim->sto_ijk.m[1][0] , nim->sto_ijk.m[1][1] , + nim->sto_ijk.m[1][2] , nim->sto_ijk.m[1][3] , + nim->sto_ijk.m[2][0] , nim->sto_ijk.m[2][1] , + nim->sto_ijk.m[2][2] , nim->sto_ijk.m[2][3] , + nim->sto_ijk.m[3][0] , nim->sto_ijk.m[3][1] , + nim->sto_ijk.m[3][2] , nim->sto_ijk.m[3][3] ) ; + + nifti_mat44_to_orientation( nim->sto_xyz , &i,&j,&k ) ; + if( i > 0 && j > 0 && k > 0 ) + sprintf( buf+strlen(buf) , + " sform_i_orientation = '%s'\n" + " sform_j_orientation = '%s'\n" + " sform_k_orientation = '%s'\n" , + nifti_orientation_string(i) , + nifti_orientation_string(j) , + nifti_orientation_string(k) ) ; + } + + sprintf( buf+strlen(buf) , " num_ext = '%d'\n", nim->num_ext ) ; + + sprintf( buf+strlen(buf) , "/>\n" ) ; /* XML-ish closer */ + + nbuf = (int)strlen(buf) ; + buf = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */ + if( !buf ) Rc_fprintf_stderr("** NITA: failed to realloc %d bytes\n",nbuf+1); + return buf ; +} +#ifdef __clang__ +#pragma clang diagnostic pop +#endif // __clang__ + +/*---------------------------------------------------------------------------*/ + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +/*----------------------------------------------------------------------*/ +/*! get the byte order for this CPU + + - LSB_FIRST means least significant byte, first (little endian) + - MSB_FIRST means most significant byte, first (big endian) +*//*--------------------------------------------------------------------*/ +int nifti_short_order(void) /* determine this CPU's byte order */ +{ + union { unsigned char bb[2] ; + short ss ; } fred ; + + fred.bb[0] = 1 ; fred.bb[1] = 0 ; + + return (fred.ss == 1) ? LSB_FIRST : MSB_FIRST ; +} +#endif + +/*---------------------------------------------------------------------------*/ + +#undef QQNUM +#undef QNUM +#undef QSTR + +/* macro to check lhs string against "n1"; if it matches, + interpret rhs string as a number, and put it into nim->"n2" */ + +#define QQNUM(n1,n2,tt) if( strcmp(lhs,#n1)==0 ) nim->n2=(tt)strtod(rhs,NULL) + +/* same, but where "n1" == "n2" */ + +#define QNUM(nam,tt) QQNUM(nam,nam,tt) + +/* macro to check lhs string against "nam"; if it matches, + put rhs string into nim->"nam" string, with max length = "ml" */ + +#define QSTR(nam,ml) if( strcmp(lhs,#nam) == 0 ) \ + strncpy(nim->nam,rhs,ml), nim->nam[ml]='\0' + +/*---------------------------------------------------------------------------*/ +/*! Take an XML-ish ASCII string and create a NIFTI image header to match. + + NULL is returned if enough information isn't present in the input string. + - The image data can later be loaded with nifti_image_load(). + - The struct returned here can be liberated with nifti_image_free(). + - Not a lot of error checking is done here to make sure that the + input values are reasonable! +*//*-------------------------------------------------------------------------*/ +nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) +{ + char lhs[1024] , rhs[1024] ; + int ii , spos, nn ; + nifti_image *nim ; /* will be output */ + + if( str == NULL || *str == '\0' ) return NULL ; /* bad input!? */ + + /* scan for opening string */ + + spos = 0 ; + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"nx = nim->ny = nim->nz = nim->nt + = nim->nu = nim->nv = nim->nw = 1 ; + nim->dx = nim->dy = nim->dz = nim->dt + = nim->du = nim->dv = nim->dw = 0 ; + nim->qfac = 1.0f ; + + nim->byteorder = nifti_short_order() ; + + /* starting at str[spos], scan for "equations" of the form + lhs = 'rhs' + and assign rhs values into the struct component named by lhs */ + + while(1){ + + while( isspace((int) str[spos]) ) spos++ ; /* skip whitespace */ + if( str[spos] == '\0' ) break ; /* end of string? */ + + /* get lhs string */ + + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ + + /* skip whitespace and the '=' marker */ + + while( isspace((int) str[spos]) || str[spos] == '=' ) spos++ ; + if( str[spos] == '\0' ) break ; /* end of string? */ + + /* if next character is a quote ', copy everything up to next ' + otherwise, copy everything up to next nonblank */ + + if( str[spos] == '\'' ){ + ii = spos+1 ; + while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ; + nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ; + memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; + spos = (str[ii] == '\'') ? ii+1 : ii ; + } else { + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; + if( ii == 0 ) break ; /* nothing found? */ + } + unescape_string(rhs) ; /* remove any XML escape sequences */ + + /* Now can do the assignment, based on lhs string. + Start with special cases that don't fit the QNUM/QSTR macros. */ + + if( strcmp(lhs,"nifti_type") == 0 ){ + if( strcmp(rhs,"ANALYZE-7.5") == 0 ) + nim->nifti_type = NIFTI_FTYPE_ANALYZE ; + else if( strcmp(rhs,"NIFTI-1+") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI1_1 ; + else if( strcmp(rhs,"NIFTI-1") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI1_2 ; + else if( strcmp(rhs,"NIFTI-1A") == 0 ) + nim->nifti_type = NIFTI_FTYPE_ASCII ; + } + else if( strcmp(lhs,"header_filename") == 0 ){ + nim->fname = nifti_strdup(rhs) ; + } + else if( strcmp(lhs,"image_filename") == 0 ){ + nim->iname = nifti_strdup(rhs) ; + } + else if( strcmp(lhs,"sto_xyz_matrix") == 0 ){ + sscanf( rhs , "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f" , + &(nim->sto_xyz.m[0][0]) , &(nim->sto_xyz.m[0][1]) , + &(nim->sto_xyz.m[0][2]) , &(nim->sto_xyz.m[0][3]) , + &(nim->sto_xyz.m[1][0]) , &(nim->sto_xyz.m[1][1]) , + &(nim->sto_xyz.m[1][2]) , &(nim->sto_xyz.m[1][3]) , + &(nim->sto_xyz.m[2][0]) , &(nim->sto_xyz.m[2][1]) , + &(nim->sto_xyz.m[2][2]) , &(nim->sto_xyz.m[2][3]) , + &(nim->sto_xyz.m[3][0]) , &(nim->sto_xyz.m[3][1]) , + &(nim->sto_xyz.m[3][2]) , &(nim->sto_xyz.m[3][3]) ) ; + } + else if( strcmp(lhs,"byteorder") == 0 ){ + if( strcmp(rhs,"MSB_FIRST") == 0 ) nim->byteorder = MSB_FIRST ; + if( strcmp(rhs,"LSB_FIRST") == 0 ) nim->byteorder = LSB_FIRST ; + } + else QQNUM(image_offset,iname_offset,int) ; + else QNUM(datatype,short int) ; + else QNUM(ndim,int) ; + else QNUM(nx,int) ; + else QNUM(ny,int) ; + else QNUM(nz,int) ; + else QNUM(nt,int) ; + else QNUM(nu,int) ; + else QNUM(nv,int) ; + else QNUM(nw,int) ; + else QNUM(dx,float) ; + else QNUM(dy,float) ; + else QNUM(dz,float) ; + else QNUM(dt,float) ; + else QNUM(du,float) ; + else QNUM(dv,float) ; + else QNUM(dw,float) ; + else QNUM(cal_min,float) ; + else QNUM(cal_max,float) ; + else QNUM(scl_slope,float) ; + else QNUM(scl_inter,float) ; + else QNUM(intent_code,short) ; + else QNUM(intent_p1,float) ; + else QNUM(intent_p2,float) ; + else QNUM(intent_p3,float) ; + else QSTR(intent_name,15) ; + else QNUM(toffset,float) ; + else QNUM(xyz_units,int) ; + else QNUM(time_units,int) ; + else QSTR(descrip,79) ; + else QSTR(aux_file,23) ; + else QNUM(qform_code,int) ; + else QNUM(quatern_b,float) ; + else QNUM(quatern_c,float) ; + else QNUM(quatern_d,float) ; + else QNUM(qoffset_x,float) ; + else QNUM(qoffset_y,float) ; + else QNUM(qoffset_z,float) ; + else QNUM(qfac,float) ; + else QNUM(sform_code,int) ; + else QNUM(freq_dim,int) ; + else QNUM(phase_dim,int) ; + else QNUM(slice_dim,int) ; + else QNUM(slice_code,int) ; + else QNUM(slice_start,int) ; + else QNUM(slice_end,int) ; + else QNUM(slice_duration,float) ; + else QNUM(num_ext,int) ; + + } /* end of while loop */ + + if( bytes_read ) *bytes_read = spos+1; /* "process" last '\n' */ + + /* do miscellaneous checking and cleanup */ + + if( nim->ndim <= 0 ){ nifti_image_free(nim); return NULL; } /* bad! */ + + nifti_datatype_sizes( nim->datatype, &(nim->nbyper), &(nim->swapsize) ); + if( nim->nbyper == 0 ){ nifti_image_free(nim); return NULL; } /* bad! */ + + nim->dim[0] = nim->ndim ; + nim->dim[1] = nim->nx ; nim->pixdim[1] = nim->dx ; + nim->dim[2] = nim->ny ; nim->pixdim[2] = nim->dy ; + nim->dim[3] = nim->nz ; nim->pixdim[3] = nim->dz ; + nim->dim[4] = nim->nt ; nim->pixdim[4] = nim->dt ; + nim->dim[5] = nim->nu ; nim->pixdim[5] = nim->du ; + nim->dim[6] = nim->nv ; nim->pixdim[6] = nim->dv ; + nim->dim[7] = nim->nw ; nim->pixdim[7] = nim->dw ; + + nim->nvox = (size_t)nim->nx * nim->ny * nim->nz + * nim->nt * nim->nu * nim->nv * nim->nw ; + + if( nim->qform_code > 0 ) + nim->qto_xyz = nifti_quatern_to_mat44( + nim->quatern_b, nim->quatern_c, nim->quatern_d, + nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, + nim->dx , nim->dy , nim->dz , + nim->qfac ) ; + else + nim->qto_xyz = nifti_quatern_to_mat44( + 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , + nim->dx , nim->dy , nim->dz , 0.0f ) ; + + + nim->qto_ijk = nifti_mat44_inverse( nim->qto_xyz ) ; + + if( nim->sform_code > 0 ) + nim->sto_ijk = nifti_mat44_inverse( nim->sto_xyz ) ; + + return nim ; +} + + +/*---------------------------------------------------------------------------*/ +/*! validate the nifti_image + + \return 1 if the structure seems valid, otherwise 0 + + \sa nifti_nim_has_valid_dims, nifti_hdr_looks_good +*//*-------------------------------------------------------------------------*/ +int nifti_nim_is_valid(nifti_image * nim, int complain) +{ + int errs = 0; + + if( !nim ){ + Rc_fprintf_stderr("** is_valid_nim: nim is NULL\n"); + return 0; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d nim_is_valid check...\n"); + + /**- check that dim[] matches the individual values ndim, nx, ny, ... */ + if( ! nifti_nim_has_valid_dims(nim,complain) ){ + if( !complain ) return 0; + errs++; + } + + /* might check nbyper, pixdim, q/sforms, swapsize, nifti_type, ... */ + + /**- be explicit in return of 0 or 1 */ + if( errs > 0 ) return 0; + else return 1; +} + +/*---------------------------------------------------------------------------*/ +/*! validate nifti dimensions + + \return 1 if valid, 0 if not + + \sa nifti_nim_is_valid, nifti_hdr_looks_good + + rely on dim[] as the master +*//*-------------------------------------------------------------------------*/ +int nifti_nim_has_valid_dims(nifti_image * nim, int complain) +{ + size_t prod; + int c, errs = 0; + + /**- start with dim[0]: failure here is considered terminal */ + if( nim->dim[0] <= 0 || nim->dim[0] > 7 ){ + errs++; + if( complain ) + Rc_fprintf_stderr("** NVd: dim[0] (%d) out of range [1,7]\n",nim->dim[0]); + return 0; + } + + /**- check whether ndim equals dim[0] */ + if( nim->ndim != nim->dim[0] ){ + errs++; + if( ! complain ) return 0; + Rc_fprintf_stderr("** NVd: ndim != dim[0] (%d,%d)\n",nim->ndim,nim->dim[0]); + } + + /**- compare each dim[i] to the proper nx, ny, ... */ + if( ( (nim->dim[0] >= 1) && (nim->dim[1] != nim->nx) ) || + ( (nim->dim[0] >= 2) && (nim->dim[2] != nim->ny) ) || + ( (nim->dim[0] >= 3) && (nim->dim[3] != nim->nz) ) || + ( (nim->dim[0] >= 4) && (nim->dim[4] != nim->nt) ) || + ( (nim->dim[0] >= 5) && (nim->dim[5] != nim->nu) ) || + ( (nim->dim[0] >= 6) && (nim->dim[6] != nim->nv) ) || + ( (nim->dim[0] >= 7) && (nim->dim[7] != nim->nw) ) ){ + errs++; + if( !complain ) return 0; + Rc_fprintf_stderr("** NVd mismatch: dims = %d,%d,%d,%d,%d,%d,%d\n" + " nxyz... = %d,%d,%d,%d,%d,%d,%d\n", + nim->dim[1], nim->dim[2], nim->dim[3], + nim->dim[4], nim->dim[5], nim->dim[6], nim->dim[7], + nim->nx, nim->ny, nim->nz, + nim->nt, nim->nu, nim->nv, nim->nw ); + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("-d check dim[%d] =", nim->dim[0]); + for( c = 0; c < 7; c++ ) Rc_fprintf_stderr(" %d", nim->dim[c]); + Rc_fputc_stderr('\n'); + } + + /**- check the dimensions, and that their product matches nvox */ + prod = 1; + for( c = 1; c <= nim->dim[0]; c++ ){ + if( nim->dim[c] > 0) + prod *= nim->dim[c]; + else if( nim->dim[c] <= 0 ){ + if( !complain ) return 0; + Rc_fprintf_stderr("** NVd: dim[%d] (=%d) <= 0\n",c, nim->dim[c]); + errs++; + } + } + if( prod != nim->nvox ){ + if( ! complain ) return 0; + Rc_fprintf_stderr("** NVd: nvox does not match %d-dim product (%u, %u)\n", + nim->dim[0], (unsigned)nim->nvox, (unsigned)prod); + errs++; + } + + /**- if debug, warn about any remaining dim that is neither 0, nor 1 */ + /* (values in dims above dim[0] are undefined, as reminded by Cinly + Ooi and Alle Meije Wink) 16 Nov 2005 [rickr] */ + if( g_opts.debug > 1 ) + for( c = nim->dim[0]+1; c <= 7; c++ ) + if( nim->dim[c] != 0 && nim->dim[c] != 1 ) + Rc_fprintf_stderr("** NVd warning: dim[%d] = %d, but ndim = %d\n", + c, nim->dim[c], nim->dim[0]); + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nim_has_valid_dims check, errs = %d\n", errs); + + /**- return invalid or valid */ + if( errs > 0 ) return 0; + else return 1; +} + + +/*---------------------------------------------------------------------------*/ +/*! read a nifti image, collapsed across dimensions according to dims[8]
+
+    This function may be used to read parts of a nifti dataset, such as
+    the time series for a single voxel, or perhaps a slice.  It is similar
+    to nifti_image_load(), though the passed 'data' parameter is used for
+    returning the image, not nim->data.
+
+    \param nim  given nifti_image struct, corresponding to the data file
+    \param dims given list of dimensions (see below)
+    \param data pointer to data pointer (if *data is NULL, data will be
+                allocated, otherwise not)
+
+    Here, dims is an array of 8 ints, similar to nim->dim[8].  While dims[0]
+    is unused at this point, the other indices specify which dimensions to
+    collapse (and at which index), and which not to collapse.  If dims[i] is
+    set to -1, then that entire dimension will be read in, from index 0 to
+    index (nim->dim[i] - 1).  If dims[i] >= 0, then only that index will be
+    read in (so dims[i] must also be < nim->dim[i]).
+
+    Example: given  nim->dim[8] = { 4, 64, 64, 21, 80, 1, 1, 1 } (4-D dataset)
+
+      if dims[8] = { 0,  5,  4, 17, -1, -1, -1, -1 }
+         -> read time series for voxel i,j,k = 5,4,17
+
+      if dims[8] = { 0, -1, -1, -1, 17, -1, -1, -1 }
+         -> read single volume at time point 17
+
+    Example: given  nim->dim[8] = { 6, 64, 64, 21, 80, 4, 3, 1 } (6-D dataset)
+
+      if dims[8] = { 0, 5, 4, 17, -1, 2, 1, 0 }
+         -> read time series for the voxel i,j,k = 5,4,17, and dim 5,6 = 2,1
+
+      if dims[8] = { 0, 5, 4, -1, -1, 0, 0, 0 }
+         -> read time series for slice at i,j = 5,4, and dim 5,6,7 = 0,0,0
+            (note that dims[7] is not relevant, but must be 0 or -1)
+
+    If *data is NULL, then *data will be set as a pointer to new memory,
+    allocated here for the resulting collapsed image data.
+
+      e.g. { int    dims[8] = { 0,  5,  4, 17, -1, -1, -1, -1 };
+             void * data    = NULL;
+             ret_val = nifti_read_collapsed_image(nim, dims, &data);
+             if( ret_val > 0 ){
+                process_time_series(data);
+                if( data != NULL ) free(data);
+             }
+           }
+
+    NOTE: If *data is not NULL, then it will be assumed that it points to
+          valid memory, sufficient to hold the results.  This is done for
+          speed and possibly repeated calls to this function.
+
+      e.g. { int    dims[8] = { 0,  -1, -1, -1, -1, -1, -1, -1 };
+             void * data    = NULL;
+             for( zslice = 0; zslice < nzslices; zslice++ ){
+                dims[3] = zslice;
+                ret_val = nifti_read_collapsed_image(nim, dims, &data);
+                if( ret_val > 0 ) process_slice(zslice, data);
+             }
+             if( data != NULL ) free(data);
+           }
+
+    \return
+        -  the total number of bytes read, or < 0 on failure
+        -  the read and byte-swapped data, in 'data'            
+ + \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks + nifti_image_load +*//*-------------------------------------------------------------------------*/ +int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], + void ** data ) +{ + znzFile fp; + int pivots[8], prods[8], nprods; /* sizes are bounded by dims[], so 8 */ + int c, bytes; + + /** - check pointers for sanity */ + if( !nim || !dims || !data ){ + Rc_fprintf_stderr("** nifti_RCI: bad params %p, %p, %p\n", + (void *)nim, (const void *)dims, (void *)data); + return -1; + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("-d read_collapsed_image:\n dims ="); + for(c = 0; c < 8; c++) Rc_fprintf_stderr(" %3d", dims[c]); + Rc_fprintf_stderr("\n nim->dims ="); + for(c = 0; c < 8; c++) Rc_fprintf_stderr(" %3d", nim->dim[c]); + Rc_fputc_stderr('\n'); + } + + /** - verify that dim[] makes sense */ + if( ! nifti_nim_is_valid(nim, g_opts.debug > 0) ){ + Rc_fprintf_stderr("** invalid nim (file is '%s')\n", nim->fname ); + return -1; + } + + /** - verify that dims[] makes sense for this dataset */ + for( c = 1; c <= nim->dim[0]; c++ ){ + if( dims[c] >= nim->dim[c] ){ + Rc_fprintf_stderr("** nifti_RCI: dims[%d] >= nim->dim[%d] (%d,%d)\n", + c, c, dims[c], nim->dim[c]); + return -1; + } + } + + /** - prepare pivot list - pivots are fixed indices */ + if( make_pivot_list(nim, dims, pivots, prods, &nprods) < 0 ) return -1; + + bytes = rci_alloc_mem(data, prods, nprods, nim->nbyper); + if( bytes < 0 ) return -1; + + /** - open the image file for reading at the appropriate offset */ + fp = nifti_image_load_prep( nim ); + if( ! fp ){ free(*data); *data = NULL; return -1; } /* failure */ + + /** - call the recursive reading function, passing nim, the pivot info, + location to store memory, and file pointer and position */ + c = rci_read_data(nim, pivots,prods,nprods,dims, + (char *)*data, fp, znztell(fp)); + znzclose(fp); /* in any case, close the file */ + if( c < 0 ){ free(*data); *data = NULL; return -1; } /* failure */ + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d read %d bytes of collapsed image from %s\n", + bytes, nim->fname); + + return bytes; +} + + +/* local function to find strides per dimension. assumes 7D size and +** stride array. +*/ +static void +compute_strides(int *strides,const int *size,int nbyper) +{ + int i; + strides[0] = nbyper; + for(i = 1; i < 7; i++) + { + strides[i] = size[i-1] * strides[i-1]; + } +} + +/*---------------------------------------------------------------------------*/ +/*! read an arbitrary subregion from a nifti image + + This function may be used to read a single arbitary subregion of any + rectangular size from a nifti dataset, such as a small 5x5x5 subregion + around the center of a 3D image. + + \param nim given nifti_image struct, corresponding to the data file + \param start_index the index location of the first voxel that will be returned + \param region_size the size of the subregion to be returned + \param data pointer to data pointer (if *data is NULL, data will be + allocated, otherwise not) + + Example: given nim->dim[8] = {3, 64, 64, 64, 1, 1, 1, 1 } (3-D dataset) + + if start_index[7] = { 29, 29, 29, 0, 0, 0, 0 } and + region_size[7] = { 5, 5, 5, 1, 1, 1, 1 } + -> read 5x5x5 region starting with the first voxel location at (29,29,29) + + NOTE: If *data is not NULL, then it will be assumed that it points to + valid memory, sufficient to hold the results. This is done for + speed and possibly repeated calls to this function. + \return + - the total number of bytes read, or < 0 on failure + - the read and byte-swapped data, in 'data' + + \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks + nifti_image_load, nifti_read_collapsed_image +*//*-------------------------------------------------------------------------*/ +int nifti_read_subregion_image( nifti_image * nim, + const int *start_index, + const int *region_size, + void ** data ) +{ + znzFile fp; /* file to read */ + int i,j,k,l,m,n; /* indices for dims */ + long int bytes = 0; /* total # bytes read */ + int total_alloc_size; /* size of buffer allocation */ + char *readptr; /* where in *data to read next */ + int strides[7]; /* strides between dimensions */ + int collapsed_dims[8]; /* for read_collapsed_image */ + int *image_size; /* pointer to dimensions in header */ + long int initial_offset; + long int offset; /* seek offset for reading current row */ + + /* probably ignored, but set to ndim for consistency*/ + collapsed_dims[0] = nim->ndim; + + /* build a dims array for collapsed image read */ + for(i = 0; i < nim->ndim; i++) + { + /* if you take the whole extent in this dimension */ + if(start_index[i] == 0 && + region_size[i] == nim->dim[i+1]) + { + collapsed_dims[i+1] = -1; + } + /* if you specify a single element in this dimension */ + else if(region_size[i] == 1) + { + collapsed_dims[i+1] = start_index[i]; + } + else + { + collapsed_dims[i+1] = -2; /* sentinel value */ + } + } + /* fill out end of collapsed_dims */ + for(i = nim->ndim ; i < 7; i++) + { + collapsed_dims[i+1] = -1; + } + + /* check to see whether collapsed read is possible */ + for(i = 1; i <= nim->ndim; i++) + { + if(collapsed_dims[i] == -2) + { + break; + } + } + + /* if you get through all the dimensions without hitting + ** a subrange of size > 1, a collapsed read is possible + */ + if(i > nim->ndim) + { + return nifti_read_collapsed_image(nim, collapsed_dims, data); + } + + /* point past first element of dim, which holds nim->ndim */ + image_size = &(nim->dim[1]); + + /* check region sizes for sanity */ + for(i = 0; i < nim->ndim; i++) + { + if(start_index[i] + region_size[i] > image_size[i]) + { + if(g_opts.debug > 1) + { + Rc_fprintf_stderr("region doesn't fit within image size\n"); + } + return -1; + } + } + + /* get the file open */ + fp = nifti_image_load_prep( nim ); + /* the current offset is just past the nifti header, save + * location so that SEEK_SET can be used below + */ + initial_offset = znztell(fp); + /* get strides*/ + compute_strides(strides,image_size,nim->nbyper); + + total_alloc_size = nim->nbyper; /* size of pixel */ + + /* find alloc size */ + for(i = 0; i < nim->ndim; i++) + { + total_alloc_size *= region_size[i]; + } + /* allocate buffer, if necessary */ + if(*data == 0) + { + *data = (void *)malloc(total_alloc_size); + } + + if(*data == 0) + { + if(g_opts.debug > 1) + { + Rc_fprintf_stderr("allocation of %d bytes failed\n",total_alloc_size); + return -1; + } + } + + /* point to start of data buffer as char * */ + readptr = *((char **)data); + { + /* can't assume that start_index and region_size have any more than + ** nim->ndim elements so make local copies, filled out to seven elements + */ + int si[7], rs[7]; + for(i = 0; i < nim->ndim; i++) + { + si[i] = start_index[i]; + rs[i] = region_size[i]; + } + for(i = nim->ndim; i < 7; i++) + { + si[i] = 0; + rs[i] = 1; + } + /* loop through subregion and read a row at a time */ + for(i = si[6]; i < (si[6] + rs[6]); i++) + { + for(j = si[5]; j < (si[5] + rs[5]); j++) + { + for(k = si[4]; k < (si[4] + rs[4]); k++) + { + for(l = si[3]; l < (si[3] + rs[3]); l++) + { + for(m = si[2]; m < (si[2] + rs[2]); m++) + { + for(n = si[1]; n < (si[1] + rs[1]); n++) + { + int nread,read_amount; + offset = initial_offset + + (i * strides[6]) + + (j * strides[5]) + + (k * strides[4]) + + (l * strides[3]) + + (m * strides[2]) + + (n * strides[1]) + + (si[0] * strides[0]); + znzseek(fp, offset, SEEK_SET); /* seek to current row */ + read_amount = rs[0] * nim->nbyper; /* read a row of the subregion*/ + nread = (int)nifti_read_buffer(fp, readptr, read_amount, nim); + if(nread != read_amount) + { + if(g_opts.debug > 1) + { + Rc_fprintf_stderr("read of %d bytes failed\n",read_amount); + return -1; + } + } + bytes += nread; + readptr += read_amount; + } + } + } + } + } + } + } + return bytes; +} + + +/* read the data from the file pointed to by fp + + - this a recursive function, so start with the base case + - data is now (char *) for easy incrementing + + return 0 on success, < 0 on failure +*/ +static int rci_read_data(nifti_image * nim, int * pivots, int * prods, + int nprods, const int dims[], char * data, znzFile fp, size_t base_offset) +{ + size_t sublen, offset, read_size; + int c; + + /* bad check first - base_offset may not have been checked */ + if( nprods <= 0 ){ + Rc_fprintf_stderr("** rci_read_data, bad prods, %d\n", nprods); + return -1; + } + + /* base case: actually read the data */ + if( nprods == 1 ){ + size_t nread, bytes; + + /* make sure things look good here */ + if( *pivots != 0 ){ + Rc_fprintf_stderr("** rciRD: final pivot == %d!\n", *pivots); + return -1; + } + + /* so just seek and read (prods[0] * nbyper) bytes from the file */ + znzseek(fp, (long)base_offset, SEEK_SET); + bytes = (size_t)prods[0] * nim->nbyper; + nread = nifti_read_buffer(fp, data, bytes, nim); + if( nread != bytes ){ + Rc_fprintf_stderr("** rciRD: read only %u of %u bytes from '%s'\n", + (unsigned)nread, (unsigned)bytes, nim->fname); + return -1; + } else if( g_opts.debug > 3 ) + Rc_fprintf_stderr("+d successful read of %u bytes at offset %u\n", + (unsigned)bytes, (unsigned)base_offset); + + return 0; /* done with base case - return success */ + } + + /* not the base case, so do a set of reduced reads */ + + /* compute size of sub-brick: all dimensions below pivot */ + for( c = 1, sublen = 1; c < *pivots; c++ ) sublen *= nim->dim[c]; + + /* compute number of values to read, i.e. remaining prods */ + for( c = 1, read_size = 1; c < nprods; c++ ) read_size *= prods[c]; + read_size *= nim->nbyper; /* and multiply by bytes per voxel */ + + /* now repeatedly compute offsets, and recursively read */ + for( c = 0; c < prods[0]; c++ ){ + /* offset is (c * sub-block size (including pivot dim)) */ + /* + (dims[] index into pivot sub-block) */ + /* the unneeded multiplication is to make this more clear */ + offset = (size_t)c * sublen * nim->dim[*pivots] + + (size_t)sublen * dims[*pivots]; + offset *= nim->nbyper; + + if( g_opts.debug > 3 ) + Rc_fprintf_stderr("-d reading %u bytes, foff %u + %u, doff %u\n", + (unsigned)read_size, (unsigned)base_offset, (unsigned)offset, + (unsigned)(c*read_size)); + + /* now read the next level down, adding this offset */ + if( rci_read_data(nim, pivots+1, prods+1, nprods-1, dims, + data + c * read_size, fp, base_offset + offset) < 0 ) + return -1; + } + + return 0; +} + + +/* allocate memory for all collapsed image data + + If *data is already set, do not allocate, but still calculate + size for debug report. + + return total size on success, and < 0 on failure +*/ +static int rci_alloc_mem(void ** data, const int prods[8], int nprods, int nbyper ) +{ + int size, memindex; + + if( nbyper < 0 || nprods < 1 || nprods > 8 ){ + Rc_fprintf_stderr("** rci_am: bad params, %d, %d\n", nbyper, nprods); + return -1; + } + + for( memindex = 0, size = 1; memindex < nprods; memindex++ ) + size *= prods[memindex]; + + size *= nbyper; + + if( ! *data ){ /* then allocate what is needed */ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d alloc %d (= %d x %d) bytes for collapsed image\n", + size, size/nbyper, nbyper); + + *data = malloc(size); /* actually allocate the memory */ + if( ! *data ){ + Rc_fprintf_stderr("** rci_am: failed to alloc %d bytes for data\n", size); + return -1; + } + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d rci_am: *data already set, need %d (%d x %d) bytes\n", + size, size/nbyper, nbyper); + + return size; +} + + +/* prepare a pivot list for reading + + The pivot points are the indices into dims where the calling function + wants to collapse a dimension. The last pivot should always be zero + (note that we have space for that in the lists). +*/ +static int make_pivot_list(nifti_image * nim, const int dims[], int pivots[], + int prods[], int * nprods ) +{ + int len, dim_index; + + len = 0; + dim_index = nim->dim[0]; + while( dim_index > 0 ){ + prods[len] = 1; + while( dim_index > 0 && + (nim->dim[dim_index] == 1 || dims[dim_index] == -1) ){ + prods[len] *= nim->dim[dim_index]; + dim_index--; + } + pivots[len] = dim_index; + len++; + dim_index--; /* fine, let it drop out at -1 */ + } + + /* make sure to include 0 as a pivot (instead of just 1, if it is) */ + /* (check len, though we have already validated nifti_image) */ + if( len > 0 && pivots[len-1] != 0 ){ + pivots[len] = 0; + prods[len] = 1; + len++; + } + + *nprods = len; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d pivot list created, pivots :"); + for(dim_index = 0; dim_index < len; dim_index++) + Rc_fprintf_stderr(" %d", pivots[dim_index]); + Rc_fprintf_stderr(", prods :"); + for(dim_index = 0; dim_index < len; dim_index++) + Rc_fprintf_stderr(" %d", prods[dim_index]); + Rc_fputc_stderr('\n'); + } + + return 0; +} + + +#ifndef RNIFTI_NIFTILIB_DEDUPLICATE +#undef ISEND +#define ISEND(c) ( (c)==']' || (c)=='}' || (c)=='\0' ) + +/*---------------------------------------------------------------------*/ +/*! Get an integer list in the range 0..(nvals-1), from the + character string str. If we call the output pointer fred, + then fred[0] = number of integers in the list (> 0), and + fred[i] = i-th integer in the list for i=1..fred[0]. + If on return, fred == NULL or fred[0] == 0, then something is + wrong, and the caller must deal with that. + + Syntax of input string: + - initial '{' or '[' is skipped, if present + - ends when '}' or ']' or end of string is found + - contains entries separated by commas + - entries have one of these forms: + - a single number + - a dollar sign '$', which means nvals-1 + - a sequence of consecutive numbers in the form "a..b" or + "a-b", where "a" and "b" are single numbers (or '$') + - a sequence of evenly spaced numbers in the form + "a..b(c)" or "a-b(c)", where "c" encodes the step + - Example: "[2,7..4,3..9(2)]" decodes to the list + 2 7 6 5 4 3 5 7 9 + - entries should be in the range 0..nvals-1 + + (borrowed, with permission, from thd_intlist.c) +*//*-------------------------------------------------------------------*/ +int * nifti_get_intlist( int nvals , const char * str ) +{ + int *subv = NULL ; + int *subv_realloc = NULL; + int ii , ipos , nout , slen ; + int ibot,itop,istep , nused ; + char *cpt ; + + /* Meaningless input? */ + if( nvals < 1 ) return NULL ; + + /* No selection list? */ + if( str == NULL || str[0] == '\0' ) return NULL ; + + /* skip initial '[' or '{' */ + subv = (int *)malloc( sizeof(int) * 2 ) ; + if( !subv ) { + Rc_fprintf_stderr("** nifti_get_intlist: failed alloc of 2 ints\n"); + return NULL; + } + subv[0] = nout = 0 ; + + ipos = 0 ; + if( str[ipos] == '[' || str[ipos] == '{' ) ipos++ ; + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d making int_list (vals = %d) from '%s'\n", nvals, str); + + /**- for each sub-selector until end of input... */ + + slen = (int)strlen(str) ; + while( ipos < slen && !ISEND(str[ipos]) ){ + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + if( ISEND(str[ipos]) ) break ; /* done */ + + /**- get starting value */ + + if( str[ipos] == '$' ){ /* special case */ + ibot = nvals-1 ; ipos++ ; + } else { /* decode an integer */ + ibot = strtol( str+ipos , &cpt , 10 ) ; + if( ibot < 0 ){ + Rc_fprintf_stderr("** ERROR: list index %d is out of range 0..%d\n", + ibot,nvals-1) ; + free(subv) ; return NULL ; + } + if( ibot >= nvals ){ + Rc_fprintf_stderr("** ERROR: list index %d is out of range 0..%d\n", + ibot,nvals-1) ; + free(subv) ; return NULL ; + } + nused = (cpt-(str+ipos)) ; + if( ibot == 0 && nused == 0 ){ + Rc_fprintf_stderr("** ERROR: list syntax error '%s'\n",str+ipos) ; + free(subv) ; return NULL ; + } + ipos += nused ; + } + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + + /**- if that's it for this sub-selector, add one value to list */ + + if( str[ipos] == ',' || ISEND(str[ipos]) ){ + nout++ ; + subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; + if( !subv_realloc ) { + free(subv); + Rc_fprintf_stderr("** nifti_get_intlist: failed realloc of %d ints\n", + nout+1); + return NULL; + } + subv=subv_realloc; + + subv[0] = nout ; + subv[nout] = ibot ; + if( ISEND(str[ipos]) ) break ; /* done */ + ipos++ ; continue ; /* re-start loop at next sub-selector */ + } + + /**- otherwise, must have '..' or '-' as next inputs */ + + if( str[ipos] == '-' ){ + ipos++ ; + } else if( str[ipos] == '.' && str[ipos+1] == '.' ){ + ipos++ ; ipos++ ; + } else { + Rc_fprintf_stderr("** ERROR: index list syntax is bad: '%s'\n", + str+ipos) ; + free(subv) ; return NULL ; + } + + /**- get ending value for loop now */ + + if( str[ipos] == '$' ){ /* special case */ + itop = nvals-1 ; ipos++ ; + } else { /* decode an integer */ + itop = strtol( str+ipos , &cpt , 10 ) ; + if( itop < 0 ){ + Rc_fprintf_stderr("** ERROR: index %d is out of range 0..%d\n", + itop,nvals-1) ; + free(subv) ; return NULL ; + } + if( itop >= nvals ){ + Rc_fprintf_stderr("** ERROR: index %d is out of range 0..%d\n", + itop,nvals-1) ; + free(subv) ; return NULL ; + } + nused = (cpt-(str+ipos)) ; + if( itop == 0 && nused == 0 ){ + Rc_fprintf_stderr("** ERROR: index list syntax error '%s'\n",str+ipos) ; + free(subv) ; return NULL ; + } + ipos += nused ; + } + + /**- set default loop step */ + + istep = (ibot <= itop) ? 1 : -1 ; + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + + /**- check if we have a non-default loop step */ + + if( str[ipos] == '(' ){ /* decode an integer */ + ipos++ ; + istep = strtol( str+ipos , &cpt , 10 ) ; + if( istep == 0 ){ + Rc_fprintf_stderr("** ERROR: index loop step is 0!\n") ; + free(subv) ; return NULL ; + } + nused = (cpt-(str+ipos)) ; + ipos += nused ; + if( str[ipos] == ')' ) ipos++ ; + if( (ibot-itop)*istep > 0 ){ + Rc_fprintf_stderr("** WARNING: index list '%d..%d(%d)' means nothing\n", + ibot,itop,istep ) ; + } + } + + /**- add values to output */ + + for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ + nout++ ; + subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; + if( !subv_realloc ) { + free(subv); + Rc_fprintf_stderr("** nifti_get_intlist: failed realloc of %d ints\n", + nout+1); + return NULL; + } + subv=subv_realloc; + subv[0] = nout ; + subv[nout] = ii ; + } + + /**- check if we have a comma to skip over */ + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + if( str[ipos] == ',' ) ipos++ ; /* skip commas */ + + } /* end of loop through selector string */ + + if( g_opts.debug > 1 ) { + Rc_fprintf_stderr("+d int_list (vals = %d): ", subv[0]); + for( ii = 1; ii <= subv[0]; ii++ ) Rc_fprintf_stderr("%d ", subv[ii]); + Rc_fputc_stderr('\n'); + } + + if( subv[0] == 0 ){ free(subv); subv = NULL; } + return subv ; +} + +/*---------------------------------------------------------------------*/ +/*! Given a NIFTI_TYPE string, such as "NIFTI_TYPE_INT16", return the + * corresponding integral type code. The type code is the macro + * value defined in nifti1.h. +*//*-------------------------------------------------------------------*/ +int nifti_datatype_from_string( const char * name ) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; + + if( !name ) return DT_UNKNOWN; + + for( c = tablen-1; c > 0; c-- ) + if( !strcmp(name, nifti_type_list[c].name) ) + break; + + return nifti_type_list[c].type; +} + + +/*---------------------------------------------------------------------*/ +/*! Given a NIFTI_TYPE value, such as NIFTI_TYPE_INT16, return the + * corresponding macro label as a string. The dtype code is the + * macro value defined in nifti1.h. +*//*-------------------------------------------------------------------*/ +const char * nifti_datatype_to_string( int dtype ) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; + + for( c = tablen-1; c > 0; c-- ) + if( nifti_type_list[c].type == dtype ) + break; + + return nifti_type_list[c].name; +} + + +/*---------------------------------------------------------------------*/ +/*! Determine whether dtype is a valid NIFTI_TYPE. + * + * DT_UNKNOWN is considered invalid + * + * The only difference 'for_nifti' makes is that DT_BINARY + * should be invalid for a NIfTI dataset. +*//*-------------------------------------------------------------------*/ +int nifti_datatype_is_valid( int dtype, int for_nifti ) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; + + /* special case */ + if( for_nifti && dtype == DT_BINARY ) return 0; + + for( c = tablen-1; c > 0; c-- ) + if( nifti_type_list[c].type == dtype ) + return 1; + + return 0; +} + + +/*---------------------------------------------------------------------*/ +/*! Only as a test, verify that the new nifti_type_list table matches + * the the usage of nifti_datatype_sizes (which could be changed to + * use the table, if there were interest). + * + * return the number of errors (so 0 is success, as usual) +*//*-------------------------------------------------------------------*/ +int nifti_test_datatype_sizes(int verb) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int nbyper, ssize; + int c, errs = 0; + + for( c = 0; c < tablen; c++ ) + { + nbyper = ssize = -1; + nifti_datatype_sizes(nifti_type_list[c].type, &nbyper, &ssize); + if( nbyper < 0 || ssize < 0 || + nbyper != nifti_type_list[c].nbyper || + ssize != nifti_type_list[c].swapsize ) + { + if( verb || g_opts.debug > 2 ) + Rc_fprintf_stderr( "** type mismatch: %s, %d, %d, %d : %d, %d\n", + nifti_type_list[c].name, nifti_type_list[c].type, + nifti_type_list[c].nbyper, nifti_type_list[c].swapsize, + nbyper, ssize); + errs++; + } + } + + if( errs ) + Rc_fprintf_stderr("** nifti_test_datatype_sizes: found %d errors\n",errs); + else if( verb || g_opts.debug > 1 ) + Rc_fprintf_stderr("-- nifti_test_datatype_sizes: all OK\n"); + + return errs; +} + + +/*---------------------------------------------------------------------*/ +/*! Display the nifti_type_list table. + * + * if which == 1 : display DT_* + * if which == 2 : display NIFTI_TYPE* + * else : display all +*//*-------------------------------------------------------------------*/ +int nifti_disp_type_list( int which ) +{ + const char * style; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int lwhich, c; + + if ( which == 1 ){ lwhich = 1; style = "DT_"; } + else if( which == 2 ){ lwhich = 2; style = "NIFTI_TYPE_"; } + else { lwhich = 3; style = "ALL"; } + + Rc_printf("nifti_type_list entries (%s) :\n" + " name type nbyper swapsize\n" + " --------------------- ---- ------ --------\n", style); + + for( c = 0; c < tablen; c++ ) + if( (lwhich & 1 && nifti_type_list[c].name[0] == 'D') || + (lwhich & 2 && nifti_type_list[c].name[0] == 'N') ) + Rc_printf(" %-22s %5d %3d %5d\n", + nifti_type_list[c].name, + nifti_type_list[c].type, + nifti_type_list[c].nbyper, + nifti_type_list[c].swapsize); + + return 0; +} +#endif diff --git a/cpp/3rd_party/rnifti/niftilib/nifti1_io.h b/cpp/3rd_party/rnifti/niftilib/nifti1_io.h new file mode 100644 index 0000000000..2bbdf6d785 --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti1_io.h @@ -0,0 +1,583 @@ +/** \file nifti1_io.h + \brief Data structures for using nifti1_io API. + - Written by Bob Cox, SSCC NIMH + - Revisions by Rick Reynolds, SSCC NIMH + */ +#ifndef _NIFTI_IO_HEADER_ +#define _NIFTI_IO_HEADER_ + +#include +#include +#include +#include +#include + +#ifndef DONT_INCLUDE_ANALYZE_STRUCT +#define DONT_INCLUDE_ANALYZE_STRUCT /*** not needed herein ***/ +#endif +#include "niftilib/nifti1.h" /*** NIFTI-1 header specification ***/ + +#ifndef RNIFTI_NIFTILIB_VERSION +#define RNIFTI_NIFTILIB_VERSION 1 +#endif + +#include "RNifti/NiftiImage_print.h" +#include + +/*****===================================================================*****/ +/***** File nifti1_io.h == Declarations for nifti1_io.c *****/ +/*****...................................................................*****/ +/***** This code is released to the public domain. *****/ +/*****...................................................................*****/ +/***** Author: Robert W Cox, SSCC/DIRP/NIMH/NIH/DHHS/USA/EARTH *****/ +/***** Date: August 2003 *****/ +/*****...................................................................*****/ +/***** Neither the National Institutes of Health (NIH), nor any of its *****/ +/***** employees imply any warranty of usefulness of this software for *****/ +/***** any purpose, and do not assume any liability for damages, *****/ +/***** incidental or otherwise, caused by any use of this document. *****/ +/*****===================================================================*****/ + +/* + Modified by: Mark Jenkinson (FMRIB Centre, University of Oxford, UK) + Date: July/August 2004 + + Mainly adding low-level IO and changing things to allow gzipped files + to be read and written + Full backwards compatability should have been maintained + + Modified by: Rick Reynolds (SSCC/DIRP/NIMH, National Institutes of Health) + Date: December 2004 + + Modified and added many routines for I/O. +*/ + +/********************** Some sample data structures **************************/ + +#if RNIFTI_NIFTILIB_VERSION == 1 + +typedef struct { /** 4x4 matrix struct **/ + float m[4][4] ; +} mat44 ; + +typedef struct { /** 3x3 matrix struct **/ + float m[3][3] ; +} mat33 ; + +/*...........................................................................*/ + +/*! \enum analyze_75_orient_code + * \brief Old-style analyze75 orientation + * codes. + */ +typedef enum _analyze75_orient_code { + a75_transverse_unflipped = 0, + a75_coronal_unflipped = 1, + a75_sagittal_unflipped = 2, + a75_transverse_flipped = 3, + a75_coronal_flipped = 4, + a75_sagittal_flipped = 5, + a75_orient_unknown = 6 +} analyze_75_orient_code; + +/*! \struct nifti_image + \brief High level data structure for open nifti datasets in the + nifti1_io API. Note that this structure is not part of the + nifti1 format definition; it is used to implement one API + for reading/writing formats in the nifti1 format. + */ +typedef struct { /*!< Image storage struct **/ + + int ndim ; /*!< last dimension greater than 1 (1..7) */ + int nx ; /*!< dimensions of grid array */ + int ny ; /*!< dimensions of grid array */ + int nz ; /*!< dimensions of grid array */ + int nt ; /*!< dimensions of grid array */ + int nu ; /*!< dimensions of grid array */ + int nv ; /*!< dimensions of grid array */ + int nw ; /*!< dimensions of grid array */ + int dim[8] ; /*!< dim[0]=ndim, dim[1]=nx, etc. */ + size_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */ + int nbyper ; /*!< bytes per voxel, matches datatype */ + int datatype ; /*!< type of data in voxels: DT_* code */ + + float dx ; /*!< grid spacings */ + float dy ; /*!< grid spacings */ + float dz ; /*!< grid spacings */ + float dt ; /*!< grid spacings */ + float du ; /*!< grid spacings */ + float dv ; /*!< grid spacings */ + float dw ; /*!< grid spacings */ + float pixdim[8] ; /*!< pixdim[1]=dx, etc. */ + + float scl_slope ; /*!< scaling parameter - slope */ + float scl_inter ; /*!< scaling parameter - intercept */ + + float cal_min ; /*!< calibration parameter, minimum */ + float cal_max ; /*!< calibration parameter, maximum */ + + int qform_code ; /*!< codes for (x,y,z) space meaning */ + int sform_code ; /*!< codes for (x,y,z) space meaning */ + + int freq_dim ; /*!< indexes (1,2,3, or 0) for MRI */ + int phase_dim ; /*!< directions in dim[]/pixdim[] */ + int slice_dim ; /*!< directions in dim[]/pixdim[] */ + + int slice_code ; /*!< code for slice timing pattern */ + int slice_start ; /*!< index for start of slices */ + int slice_end ; /*!< index for end of slices */ + float slice_duration ; /*!< time between individual slices */ + + /*! quaternion transform parameters + [when writing a dataset, these are used for qform, NOT qto_xyz] */ + float quatern_b , quatern_c , quatern_d , + qoffset_x , qoffset_y , qoffset_z , + qfac ; + + mat44 qto_xyz ; /*!< qform: transform (i,j,k) to (x,y,z) */ + mat44 qto_ijk ; /*!< qform: transform (x,y,z) to (i,j,k) */ + + mat44 sto_xyz ; /*!< sform: transform (i,j,k) to (x,y,z) */ + mat44 sto_ijk ; /*!< sform: transform (x,y,z) to (i,j,k) */ + + float toffset ; /*!< time coordinate offset */ + + int xyz_units ; /*!< dx,dy,dz units: NIFTI_UNITS_* code */ + int time_units ; /*!< dt units: NIFTI_UNITS_* code */ + + int nifti_type ; /*!< 0==ANALYZE, 1==NIFTI-1 (1 file), + 2==NIFTI-1 (2 files), + 3==NIFTI-ASCII (1 file) */ + int intent_code ; /*!< statistic type (or something) */ + float intent_p1 ; /*!< intent parameters */ + float intent_p2 ; /*!< intent parameters */ + float intent_p3 ; /*!< intent parameters */ + char intent_name[16] ; /*!< optional description of intent data */ + + char descrip[80] ; /*!< optional text to describe dataset */ + char aux_file[24] ; /*!< auxiliary filename */ + + char *fname ; /*!< header filename (.hdr or .nii) */ + char *iname ; /*!< image filename (.img or .nii) */ + int iname_offset ; /*!< offset into iname where data starts */ + int swapsize ; /*!< swap unit in image data (might be 0) */ + int byteorder ; /*!< byte order on disk (MSB_ or LSB_FIRST) */ + void *data ; /*!< pointer to data: nbyper*nvox bytes */ + + int num_ext ; /*!< number of extensions in ext_list */ + nifti1_extension * ext_list ; /*!< array of extension structs (with data) */ + analyze_75_orient_code analyze75_orient; /*!< for old analyze files, orient */ + + // reserved impl bytes. Is not part of nifti format + std::shared_ptr decompressed_memory_buffer; + +} nifti1_image ; +#endif + + +/* struct for return from nifti_image_read_bricks() */ +typedef struct { + int nbricks; /* the number of allocated pointers in 'bricks' */ + size_t bsize; /* the length of each data block, in bytes */ + void ** bricks; /* array of pointers to data blocks */ +} nifti1_brick_list; + +#if RNIFTI_NIFTILIB_VERSION == 1 +typedef nifti1_image nifti_image; +typedef nifti1_brick_list nifti_brick_list; +#endif + +/*****************************************************************************/ +/*------------------ NIfTI version of ANALYZE 7.5 structure -----------------*/ + +/* (based on fsliolib/dbh.h, but updated for version 7.5) */ + +#if RNIFTI_NIFTILIB_VERSION == 1 +typedef struct { + /* header info fields - describes the header overlap with NIfTI */ + /* ------------------ */ + int sizeof_hdr; /* 0 + 4 same */ + char data_type[10]; /* 4 + 10 same */ + char db_name[18]; /* 14 + 18 same */ + int extents; /* 32 + 4 same */ + short int session_error; /* 36 + 2 same */ + char regular; /* 38 + 1 same */ + char hkey_un0; /* 39 + 1 40 bytes */ + + /* image dimension fields - describes image sizes */ + short int dim[8]; /* 0 + 16 same */ + short int unused8; /* 16 + 2 intent_p1... */ + short int unused9; /* 18 + 2 ... */ + short int unused10; /* 20 + 2 intent_p2... */ + short int unused11; /* 22 + 2 ... */ + short int unused12; /* 24 + 2 intent_p3... */ + short int unused13; /* 26 + 2 ... */ + short int unused14; /* 28 + 2 intent_code */ + short int datatype; /* 30 + 2 same */ + short int bitpix; /* 32 + 2 same */ + short int dim_un0; /* 34 + 2 slice_start */ + float pixdim[8]; /* 36 + 32 same */ + + float vox_offset; /* 68 + 4 same */ + float funused1; /* 72 + 4 scl_slope */ + float funused2; /* 76 + 4 scl_inter */ + float funused3; /* 80 + 4 slice_end, */ + /* slice_code, */ + /* xyzt_units */ + float cal_max; /* 84 + 4 same */ + float cal_min; /* 88 + 4 same */ + float compressed; /* 92 + 4 slice_duration */ + float verified; /* 96 + 4 toffset */ + int glmax,glmin; /* 100 + 8 108 bytes */ + + /* data history fields - optional */ + char descrip[80]; /* 0 + 80 same */ + char aux_file[24]; /* 80 + 24 same */ + char orient; /* 104 + 1 NO GOOD OVERLAP */ + char originator[10]; /* 105 + 10 FROM HERE DOWN... */ + char generated[10]; /* 115 + 10 */ + char scannum[10]; /* 125 + 10 */ + char patient_id[10]; /* 135 + 10 */ + char exp_date[10]; /* 145 + 10 */ + char exp_time[10]; /* 155 + 10 */ + char hist_un0[3]; /* 165 + 3 */ + int views; /* 168 + 4 */ + int vols_added; /* 172 + 4 */ + int start_field; /* 176 + 4 */ + int field_skip; /* 180 + 4 */ + int omax, omin; /* 184 + 8 */ + int smax, smin; /* 192 + 8 200 bytes */ +} nifti_analyze75; /* total: 348 bytes */ +#endif + +size_t nifti_image_impl_reserved_bytes_offset(); + +/*****************************************************************************/ +/*--------------- Prototypes of functions defined in this file --------------*/ + +char const * nifti_datatype_string ( int dt ) ; +char const *nifti_units_string ( int uu ) ; +char const *nifti_intent_string ( int ii ) ; +char const *nifti_xform_string ( int xx ) ; +char const *nifti_slice_string ( int ss ) ; +char const *nifti_orientation_string( int ii ) ; + +int nifti_is_inttype( int dt ) ; + +mat44 nifti_mat44_inverse( mat44 R ) ; + +mat33 nifti_mat33_inverse( mat33 R ) ; +mat33 nifti_mat33_polar ( mat33 A ) ; +float nifti_mat33_rownorm( mat33 A ) ; +float nifti_mat33_colnorm( mat33 A ) ; +float nifti_mat33_determ ( mat33 R ) ; +mat33 nifti_mat33_mul ( mat33 A , mat33 B ) ; + +#if RNIFTI_NIFTILIB_VERSION == 1 +void nifti_swap_2bytes ( size_t n , void *ar ) ; +void nifti_swap_4bytes ( size_t n , void *ar ) ; +void nifti_swap_8bytes ( size_t n , void *ar ) ; +void nifti_swap_16bytes( size_t n , void *ar ) ; +void nifti_swap_Nbytes ( size_t n , int siz , void *ar ) ; +#endif + +int nifti_datatype_is_valid (int dtype, int for_nifti); +int nifti_datatype_from_string(const char * name); +const char * nifti_datatype_to_string (int dtype); + +int nifti_get_filesize( const char *pathname ) ; +#if RNIFTI_NIFTILIB_VERSION == 1 +void swap_nifti_header ( struct nifti_1_header *h , int is_nifti ) ; +#endif +void old_swap_nifti_header( struct nifti_1_header *h , int is_nifti ); +#if RNIFTI_NIFTILIB_VERSION == 1 +int nifti_swap_as_analyze( nifti_analyze75 *h ); +#endif + + +/* main read/write routines */ + +nifti_image *nifti_image_read_bricks(const char *hname , int nbricks, + const int *blist, nifti_brick_list * NBL); +int nifti_image_load_bricks(nifti_image *nim , int nbricks, + const int *blist, nifti_brick_list * NBL); +void nifti_free_NBL( nifti_brick_list * NBL ); + +nifti_image *nifti_image_read ( const char *hname , int read_data ) ; +nifti_image *nifti_image_mem_read( const uint8_t *data , const size_t size , int gz, const size_t estimated_size ) ; + +int nifti_image_load ( nifti_image *nim ) ; +void nifti_image_unload ( nifti_image *nim ) ; +void nifti_image_free ( nifti_image *nim ) ; +int nifti_image_is_data_owner(const nifti_image* nim) ; + +int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], + void ** data ); + +int nifti_read_subregion_image( nifti_image * nim, + const int *start_index, const int *region_size, + void ** data ); + +void nifti_image_write ( nifti_image * nim ) ; +void nifti_image_write_bricks(nifti_image * nim, + const nifti_brick_list * NBL); +void nifti_image_infodump( const nifti_image * nim ) ; + +void nifti_disp_lib_hist( void ) ; /* to display library history */ +void nifti_disp_lib_version( void ) ; /* to display library version */ +int nifti_disp_matrix_orient( const char * mesg, mat44 mat ); +int nifti_disp_type_list( int which ); + + +char * nifti_image_to_ascii ( const nifti_image * nim ) ; +nifti_image *nifti_image_from_ascii( const char * str, int * bytes_read ) ; + +size_t nifti_get_volsize(const nifti_image *nim) ; + +/* basic file operations */ +int nifti_set_filenames(nifti_image * nim, const char * prefix, int check, + int set_byte_order); +char * nifti_makehdrname (const char * prefix, int nifti_type, int check, + int comp); +char * nifti_makeimgname (const char * prefix, int nifti_type, int check, + int comp); +int is_nifti_file (const char *hname); +char * nifti_find_file_extension(const char * name); +int nifti_is_complete_filename(const char* fname); +int nifti_validfilename(const char* fname); + +int disp_nifti_1_header(const char * info, const nifti_1_header * hp ) ; +void nifti_set_debug_level( int level ) ; +void nifti_set_skip_blank_ext( int skip ) ; +void nifti_set_allow_upper_fext( int allow ) ; + +int valid_nifti_brick_list(nifti_image * nim , int nbricks, + const int * blist, int disp_error); + +/* znzFile operations */ +znzFile nifti_image_open(const char * hname, const char * opts, nifti_image ** nim); +znzFile nifti_image_write_hdr_img(nifti_image *nim, int write_data, + const char* opts); +znzFile nifti_image_write_hdr_img2( nifti_image *nim , int write_opts , + const char* opts, znzFile imgfile, const nifti_brick_list * NBL); +size_t nifti_read_buffer(znzFile fp, void* dataptr, size_t ntot, nifti_image *nim); +size_t nifti_assign_buffer(znzFile fp, void** dataptr, size_t ntot, nifti_image *nim); +int nifti_write_all_data(znzFile fp, nifti_image * nim, + const nifti_brick_list * NBL); +size_t nifti_write_buffer(znzFile fp, const void * buffer, size_t numbytes); +nifti_image *nifti_read_ascii_image(znzFile fp, char *fname, int flen, + int read_data); +znzFile nifti_write_ascii_image(nifti_image *nim, const nifti_brick_list * NBL, + const char * opts, int write_data, int leave_open); + + +void nifti_datatype_sizes( int datatype , int *nbyper, int *swapsize ) ; + +void nifti_mat44_to_quatern( mat44 R , + float *qb, float *qc, float *qd, + float *qx, float *qy, float *qz, + float *dx, float *dy, float *dz, float *qfac ) ; + +mat44 nifti_quatern_to_mat44( float qb, float qc, float qd, + float qx, float qy, float qz, + float dx, float dy, float dz, float qfac ); + +mat44 nifti_make_orthog_mat44( float r11, float r12, float r13 , + float r21, float r22, float r23 , + float r31, float r32, float r33 ) ; + +int nifti_short_order(void) ; /* CPU byte order */ + + +/* Orientation codes that might be returned from nifti_mat44_to_orientation().*/ + +#define NIFTI_L2R 1 /* Left to Right */ +#define NIFTI_R2L 2 /* Right to Left */ +#define NIFTI_P2A 3 /* Posterior to Anterior */ +#define NIFTI_A2P 4 /* Anterior to Posterior */ +#define NIFTI_I2S 5 /* Inferior to Superior */ +#define NIFTI_S2I 6 /* Superior to Inferior */ + +void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) ; + +/*--------------------- Low level IO routines ------------------------------*/ + +char * nifti_findhdrname (const char* fname); +char * nifti_findimgname (const char* fname , int nifti_type); +int nifti_is_gzfile (const char* fname); + +char * nifti_makebasename(const char* fname); + + +/* other routines */ +struct nifti_1_header nifti_convert_nim2nhdr(const nifti_image* nim); +nifti_1_header * nifti_make_new_header(const int arg_dims[], int arg_dtype); +nifti_1_header * nifti_read_header(const char *hname, int *swapped, int check); +nifti_image * nifti_copy_nim_info(const nifti_image * src); +nifti_image * nifti_make_new_nim(const int dims[], int datatype, + int data_fill); +nifti_image * nifti_simple_init_nim(void); +nifti_image * nifti_convert_nhdr2nim(struct nifti_1_header nhdr, + const char * fname); + +int nifti_hdr_looks_good (const nifti_1_header * hdr); +int nifti_is_valid_datatype (int dtype); +int nifti_is_valid_ecode (int ecode); +int nifti_nim_is_valid (nifti_image * nim, int complain); +int nifti_nim_has_valid_dims (nifti_image * nim, int complain); +int is_valid_nifti_type (int nifti_type); +int nifti_test_datatype_sizes (int verb); +int nifti_type_and_names_match (nifti_image * nim, int show_warn); +int nifti_update_dims_from_array(nifti_image * nim); +void nifti_set_iname_offset (nifti_image *nim); +int nifti_set_type_from_names (nifti_image * nim); +int nifti_add_extension(nifti_image * nim, const char * data, int len, + int ecode ); +int nifti_compiled_with_zlib (void); +int nifti_copy_extensions (nifti_image *nim_dest,const nifti_image *nim_src); +int nifti_free_extensions (nifti_image *nim); +int * nifti_get_intlist (int nvals , const char *str); +char * nifti_strdup (const char *str); +int valid_nifti_extensions(const nifti_image *nim); + + +/*-------------------- Some C convenience macros ----------------------------*/ + +/* NIfTI-1.1 extension codes: + see http://nifti.nimh.nih.gov/nifti-1/documentation/faq#Q21 */ + +#define NIFTI_ECODE_IGNORE 0 /* changed from UNKNOWN, 29 June 2005 */ + +#define NIFTI_ECODE_DICOM 2 /* intended for raw DICOM attributes */ + +#define NIFTI_ECODE_AFNI 4 /* Robert W Cox: rwcox@nih.gov + https://afni.nimh.nih.gov/afni */ + +#define NIFTI_ECODE_COMMENT 6 /* plain ASCII text only */ + +#define NIFTI_ECODE_XCEDE 8 /* David B Keator: dbkeator@uci.edu + http://www.nbirn.net/Resources + /Users/Applications/ + /xcede/index.htm */ + +#define NIFTI_ECODE_JIMDIMINFO 10 /* Mark A Horsfield: + mah5@leicester.ac.uk + http://someplace/something */ + +#define NIFTI_ECODE_WORKFLOW_FWDS 12 /* Kate Fissell: fissell@pitt.edu + http://kraepelin.wpic.pitt.edu + /~fissell/NIFTI_ECODE_WORKFLOW_FWDS + /NIFTI_ECODE_WORKFLOW_FWDS.html */ + +#define NIFTI_ECODE_FREESURFER 14 /* http://surfer.nmr.mgh.harvard.edu */ + +#define NIFTI_ECODE_PYPICKLE 16 /* embedded Python objects + http://niftilib.sourceforge.net + /pynifti */ + + /* LONI MiND codes: http://www.loni.ucla.edu/twiki/bin/view/Main/MiND */ +#define NIFTI_ECODE_MIND_IDENT 18 /* Vishal Patel: vishal.patel@ucla.edu*/ +#define NIFTI_ECODE_B_VALUE 20 +#define NIFTI_ECODE_SPHERICAL_DIRECTION 22 +#define NIFTI_ECODE_DT_COMPONENT 24 +#define NIFTI_ECODE_SHC_DEGREEORDER 26 /* end LONI MiND codes */ + +#define NIFTI_ECODE_VOXBO 28 /* Dan Kimberg: www.voxbo.org */ + +#define NIFTI_ECODE_CARET 30 /* John Harwell: john@brainvis.wustl.edu + http://brainvis.wustl.edu/wiki + /index.php/Caret:Documentation + :CaretNiftiExtension */ + +#define NIFTI_ECODE_CIFTI 32 /* CIFTI-2_Main_FINAL_1March2014.pdf */ + +#define NIFTI_ECODE_VARIABLE_FRAME_TIMING 34 + +/* 36 is currently unassigned, waiting on NIFTI_ECODE_AGILENT_PROCPAR */ + +#define NIFTI_ECODE_EVAL 38 /* Munster University Hospital */ + +/* http://www.mathworks.com/matlabcentral/fileexchange/42997-dicom-to-nifti-converter */ +#define NIFTI_ECODE_MATLAB 40 /* MATLAB extension */ + +/* Quantiphyse extension + https://quantiphyse.readthedocs.io/en/latest/advanced/nifti_extension.html*/ +#define NIFTI_ECODE_QUANTIPHYSE 42 /* Quantiphyse extension */ + +/* Magnetic Resonance Spectroscopy (MRS) + link to come... */ +#define NIFTI_ECODE_MRS 44 /* MRS extension */ + +#define NIFTI_MAX_ECODE 44 /******* maximum extension code *******/ + +/* nifti_type file codes */ +#if RNIFTI_NIFTILIB_VERSION == 1 +#define NIFTI_FTYPE_ANALYZE 0 +#define NIFTI_FTYPE_NIFTI1_1 1 +#define NIFTI_FTYPE_NIFTI1_2 2 +#define NIFTI_FTYPE_ASCII 3 +#define NIFTI_MAX_FTYPE 3 /* this should match the maximum code */ +#endif + +/*------------------------------------------------------------------------*/ +/*-- the rest of these apply only to nifti1_io.c, check for _NIFTI1_IO_C_ */ +/* Feb 9, 2005 [rickr] */ +#ifdef _NIFTI1_IO_C_ + +typedef struct { + int debug; /*!< debug level for status reports */ + int skip_blank_ext; /*!< skip extender if no extensions */ + int allow_upper_fext; /*!< allow uppercase file extensions */ +} nifti_global_options; + +typedef struct { + int type; /* should match the NIFTI_TYPE_ #define */ + int nbyper; /* bytes per value, matches nifti_image */ + int swapsize; /* bytes per swap piece, matches nifti_image */ + char const * const name; /* text string to match #define */ +} nifti_type_ele; + +#undef LNI_FERR /* local nifti file error, to be compact and repetative */ +#ifdef USING_R +#define LNI_FERR(func,msg,file) \ + Rf_warning("%s: %s '%s'\n",func,msg,file) +#else +#define LNI_FERR(func,msg,file) \ + Rc_fprintf_stderr("** ERROR (%s): %s '%s'\n",func,msg,file) +#endif + +#undef swap_2 +#undef swap_4 +#define swap_2(s) nifti_swap_2bytes(1,&(s)) /* s: 2-byte short; swap in place */ +#define swap_4(v) nifti_swap_4bytes(1,&(v)) /* v: 4-byte value; swap in place */ + + /***** isfinite() is a C99 macro, which is + present in many C implementations already *****/ + +#undef IS_GOOD_FLOAT +#undef FIXED_FLOAT + +#ifdef isfinite /* use isfinite() to check floats/doubles for goodness */ +# define IS_GOOD_FLOAT(x) isfinite(x) /* check if x is a "good" float */ +# define FIXED_FLOAT(x) (isfinite(x) ? (x) : 0) /* fixed if bad */ +#else +# define IS_GOOD_FLOAT(x) 1 /* don't check it */ +# define FIXED_FLOAT(x) (x) /* don't fix it */ +#endif + +#undef ASSIF /* assign v to *p, if possible */ +#define ASSIF(p,v) if( (p)!=NULL ) *(p) = (v) + +#undef MSB_FIRST +#undef LSB_FIRST +#undef REVERSE_ORDER +#define LSB_FIRST 1 +#define MSB_FIRST 2 +#define REVERSE_ORDER(x) (3-(x)) /* convert MSB_FIRST <--> LSB_FIRST */ + +#define LNI_MAX_NIA_EXT_LEN 100000 /* consider a longer extension invalid */ + +#endif /* _NIFTI1_IO_C_ section */ +/*------------------------------------------------------------------------*/ + +#endif /* _NIFTI_IO_HEADER_ */ diff --git a/cpp/3rd_party/rnifti/niftilib/nifti2.h b/cpp/3rd_party/rnifti/niftilib/nifti2.h new file mode 100644 index 0000000000..556e4c66b0 --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti2.h @@ -0,0 +1,106 @@ +/** \file nifti2.h + \brief Header structure for NIFTI-2 format. + */ + +#ifndef __NIFTI2_HEADER +#define __NIFTI2_HEADER + +/*---------------------------------------------------------------------------*/ +/* Changes to the header from NIFTI-1 to NIFTI-2 are intended to allow for + larger and more accurate fields. The changes are as follows: + + - short dim[8] -> int64_t dim[8] + - float intent_p1,2,3 -> double intent_p1,2,3 (3 fields) + - float pixdim[8] -> double pixdim[8] + - float vox_offset -> int64_t vox_offset + - float scl_slope -> double scl_slope + - float scl_inter -> double scl_inter + - float cal_max -> double cal_max + - float cal_min -> double cal_min + - float slice_duration -> double slice_duration + - float toffset -> double toffset + - short slice_start -> int64_t slice_start + - short slice_end -> int64_t slice_end + - char slice_code -> int32_t slice_code + - char xyzt_units -> int32_t xyzt_units + - short intent_code -> int32_t intent_code + - short qform_code -> int32_t qform_code + - short sform_code -> int32_t sform_code + - float quatern_b,c,d -> double quatern_b,c,d (3 fields) + - float srow_x,y,z[4] -> double srow_x,y,z[4] (3 fields) + - char magic[4] -> char magic[8] + - char unused_str[15] -> padding added at the end of the header + + - previously unused fields have been removed: + data_type, db_name, extents, session_error, regular, glmax, glmin + + - the field order has been changed, notably with magic after sizeof_hdr + + 2 Jan, 2014 [rickr] +-----------------------------------------------------------------------------*/ + +#include + +/*! \struct nifti_2_header + \brief Data structure defining the fields in the nifti2 header. + This binary header should be found at the beginning of a valid + NIFTI-2 header file. + */ + +/* hopefully cross-platform solution to byte padding added by some compilers */ +#pragma pack(push) +#pragma pack(1) + + /*****************************/ /***********************/ /************/ +struct nifti_2_header { /* NIFTI-2 usage */ /* NIFTI-1 usage */ /* offset */ + /*****************************/ /***********************/ /************/ + int32_t sizeof_hdr; /*!< MUST be 540 */ /* MUST be 348 */ /* 0 */ + char magic[8]; /*!< MUST be valid signature */ /* char magic[4] */ /* 4 */ + int16_t datatype; /*!< Defines data type! */ /* short datatype */ /* 12 */ + int16_t bitpix; /*!< Number bits/voxel */ /* short bitpix */ /* 14 */ + int64_t dim[8]; /*!< Data array dimensions */ /* short dim[8] */ /* 16 */ + double intent_p1; /*!< 1st intent parameter */ /* float intent_p1 */ /* 80 */ + double intent_p2; /*!< 2nd intent parameter */ /* float intent_p2 */ /* 88 */ + double intent_p3; /*!< 3rd intent parameter */ /* float intent_p3 */ /* 96 */ + double pixdim[8]; /*!< Grid spacings */ /* float pixdim[8] */ /* 104 */ + int64_t vox_offset; /*!< Offset into .nii file */ /* float vox_offset */ /* 168 */ + double scl_slope; /*!< Data scaling: slope */ /* float scl_slope */ /* 176 */ + double scl_inter; /*!< Data scaling: offset */ /* float scl_inter */ /* 184 */ + double cal_max; /*!< Max display intensity */ /* float cal_max */ /* 192 */ + double cal_min; /*!< Min display intensity */ /* float cal_min */ /* 200 */ + double slice_duration; /*!< Time for 1 slice */ /* float slice_duration*/ /* 208 */ + double toffset; /*!< Time axis shift */ /* float toffset */ /* 216 */ + int64_t slice_start; /*!< First slice index */ /* short slice_start */ /* 224 */ + int64_t slice_end; /*!< Last slice index */ /* short slice_end */ /* 232 */ + char descrip[80]; /*!< any text you like */ /* char descrip[80] */ /* 240 */ + char aux_file[24]; /*!< auxiliary filename */ /* char aux_file[24] */ /* 320 */ + int32_t qform_code; /*!< NIFTI_XFORM_* code */ /* short qform_code */ /* 344 */ + int32_t sform_code; /*!< NIFTI_XFORM_* code */ /* short sform_code */ /* 348 */ + double quatern_b; /*!< Quaternion b param */ /* float quatern_b */ /* 352 */ + double quatern_c; /*!< Quaternion c param */ /* float quatern_c */ /* 360 */ + double quatern_d; /*!< Quaternion d param */ /* float quatern_d */ /* 368 */ + double qoffset_x; /*!< Quaternion x shift */ /* float qoffset_x */ /* 376 */ + double qoffset_y; /*!< Quaternion y shift */ /* float qoffset_y */ /* 384 */ + double qoffset_z; /*!< Quaternion z shift */ /* float qoffset_z */ /* 392 */ + double srow_x[4]; /*!< 1st row affine transform*/ /* float srow_x[4] */ /* 400 */ + double srow_y[4]; /*!< 2nd row affine transform*/ /* float srow_y[4] */ /* 432 */ + double srow_z[4]; /*!< 3rd row affine transform*/ /* float srow_z[4] */ /* 464 */ + int32_t slice_code; /*!< Slice timing order */ /* char slice_code */ /* 496 */ + int32_t xyzt_units; /*!< Units of pixdim[1..4] */ /* char xyzt_units */ /* 500 */ + int32_t intent_code; /*!< NIFTI_INTENT_* code */ /* short intent_code */ /* 504 */ + char intent_name[16];/*!< name or meaning of data */ /* char intent_name[16]*/ /* 508 */ + char dim_info; /*!< MRI slice ordering */ /* char dim_info */ /* 524 */ + char unused_str[15]; /*!< unused, filled with \0 */ /* 525 */ +}; /****** total bytes: 540 */ +typedef struct nifti_2_header nifti_2_header; + +/* restore packing behavior */ +#pragma pack(pop) + +/* base swap test on the suggested version check, rather than dim[0] + swap4(348)==1543569408, swap4(540)==469893120 */ +#define NIFTI2_NEEDS_SWAP(h) \ + ((h).sizeof_hdr == 1543569408 || (h).sizeof_hdr == 469893120) + + +#endif /* __NIFTI2_HEADER */ diff --git a/cpp/3rd_party/rnifti/niftilib/nifti2_image.h b/cpp/3rd_party/rnifti/niftilib/nifti2_image.h new file mode 100644 index 0000000000..9b63c2f6ab --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti2_image.h @@ -0,0 +1,109 @@ +#ifndef _NIFTI2_IMAGE_H_ +#define _NIFTI2_IMAGE_H_ + +#include + +// This is repetitious and inelegant, but a definition for nifti2_image is needed to allow +// conversion to/from nifti1_image. This is a straight copy of the relevant parts of nifti2_io.h. +#if RNIFTI_NIFTILIB_VERSION == 1 + +typedef struct { /** 4x4 matrix struct (double) **/ + double m[4][4] ; +} nifti_dmat44 ; + + +typedef struct { /*!< Image storage struct **/ + + int64_t ndim ; /*!< last dimension greater than 1 (1..7) */ + int64_t nx ; /*!< dimensions of grid array */ + int64_t ny ; /*!< dimensions of grid array */ + int64_t nz ; /*!< dimensions of grid array */ + int64_t nt ; /*!< dimensions of grid array */ + int64_t nu ; /*!< dimensions of grid array */ + int64_t nv ; /*!< dimensions of grid array */ + int64_t nw ; /*!< dimensions of grid array */ + int64_t dim[8] ; /*!< dim[0]=ndim, dim[1]=nx, etc. */ + int64_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */ + int nbyper ; /*!< bytes per voxel, matches datatype */ + int datatype ; /*!< type of data in voxels: DT_* code */ + + double dx ; /*!< grid spacings */ + double dy ; /*!< grid spacings */ + double dz ; /*!< grid spacings */ + double dt ; /*!< grid spacings */ + double du ; /*!< grid spacings */ + double dv ; /*!< grid spacings */ + double dw ; /*!< grid spacings */ + double pixdim[8] ; /*!< pixdim[1]=dx, etc. */ + + double scl_slope ; /*!< scaling parameter - slope */ + double scl_inter ; /*!< scaling parameter - intercept */ + + double cal_min ; /*!< calibration parameter, minimum */ + double cal_max ; /*!< calibration parameter, maximum */ + + int qform_code ; /*!< codes for (x,y,z) space meaning */ + int sform_code ; /*!< codes for (x,y,z) space meaning */ + + int freq_dim ; /*!< indexes (1,2,3, or 0) for MRI */ + int phase_dim ; /*!< directions in dim[]/pixdim[] */ + int slice_dim ; /*!< directions in dim[]/pixdim[] */ + + int slice_code ; /*!< code for slice timing pattern */ + int64_t slice_start ; /*!< index for start of slices */ + int64_t slice_end ; /*!< index for end of slices */ + double slice_duration ; /*!< time between individual slices */ + + /*! quaternion transform parameters + [when writing a dataset, these are used for qform, NOT qto_xyz] */ + double quatern_b , quatern_c , quatern_d , + qoffset_x , qoffset_y , qoffset_z , + qfac ; + + nifti_dmat44 qto_xyz ; /*!< qform: transform (i,j,k) to (x,y,z) */ + nifti_dmat44 qto_ijk ; /*!< qform: transform (x,y,z) to (i,j,k) */ + + nifti_dmat44 sto_xyz ; /*!< sform: transform (i,j,k) to (x,y,z) */ + nifti_dmat44 sto_ijk ; /*!< sform: transform (x,y,z) to (i,j,k) */ + + double toffset ; /*!< time coordinate offset */ + + int xyz_units ; /*!< dx,dy,dz units: NIFTI_UNITS_* code */ + int time_units ; /*!< dt units: NIFTI_UNITS_* code */ + + int nifti_type ; /*!< see NIFTI_FTYPE_* codes, below: + 0==ANALYZE, + 1==NIFTI-1 (1 file), + 2==NIFTI-1 (2 files), + 3==NIFTI-ASCII (1 file) + 4==NIFTI-2 (1 file), + 5==NIFTI-2 (2 files) */ + + int intent_code ; /*!< statistic type (or something) */ + double intent_p1 ; /*!< intent parameters */ + double intent_p2 ; /*!< intent parameters */ + double intent_p3 ; /*!< intent parameters */ + char intent_name[16] ; /*!< optional description of intent data */ + + char descrip[80] ; /*!< optional text to describe dataset */ + char aux_file[24] ; /*!< auxiliary filename */ + + char *fname ; /*!< header filename (.hdr or .nii) */ + char *iname ; /*!< image filename (.img or .nii) */ + int64_t iname_offset ; /*!< offset into iname where data starts */ + int swapsize ; /*!< swap unit in image data (might be 0) */ + int byteorder ; /*!< byte order on disk (MSB_ or LSB_FIRST) */ + void *data ; /*!< pointer to data: nbyper*nvox bytes */ + + int num_ext ; /*!< number of extensions in ext_list */ + nifti1_extension * ext_list ; /*!< array of extension structs (with data) */ + analyze_75_orient_code analyze75_orient; /*!< for old analyze files, orient */ + + // reserved impl bytes. Is not part of nifti format. + std::shared_ptr decompressed_memory_buffer; + +} nifti2_image ; + +#endif // RNIFTI_NIFTILIB_VERSION + +#endif diff --git a/cpp/3rd_party/rnifti/niftilib/nifti2_io.cpp b/cpp/3rd_party/rnifti/niftilib/nifti2_io.cpp new file mode 100644 index 0000000000..991a95035c --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti2_io.cpp @@ -0,0 +1,10154 @@ +#define _NIFTI2_IO_C_ + +#include "niftilib/nifti2_io.h" /* typedefs, prototypes, macros, etc. */ + +/*****===================================================================*****/ +/***** Sample functions to deal with NIFTI-1,2 and ANALYZE files *****/ +/*****...................................................................*****/ +/***** This code is released to the public domain. *****/ +/*****...................................................................*****/ +/***** Author: Robert W Cox, SSCC/DIRP/NIMH/NIH/DHHS/USA/EARTH *****/ +/***** Date: August 2003 *****/ +/*****...................................................................*****/ +/***** Neither the National Institutes of Health (NIH), nor any of its *****/ +/***** employees imply any warranty of usefulness of this software for *****/ +/***** any purpose, and do not assume any liability for damages, *****/ +/***** incidental or otherwise, caused by any use of this document. *****/ +/*****===================================================================*****/ + +/** \file nifti1_io.c + \brief main collection of nifti1 i/o routines + - written by Bob Cox, SSCC NIMH + - revised by Mark Jenkinson, FMRIB + - revised by Rick Reynolds, SSCC, NIMH + - revised by Kate Fissell, University of Pittsburgh + + The library history can be viewed via "nifti_tool -nifti_hist". +
The library version can be viewed via "nifti_tool -nifti_ver". + */ + +/*! global history and version strings, for printing */ +static char const * const gni1_history[] = +{ + "----------------------------------------------------------------------\n" + "history (of nifti-1 library changes):\n" + "\n", + "0.0 August, 2003 [rwcox]\n" + " (Robert W Cox of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - initial version\n" + "\n", + "0.1 July/August, 2004 [Mark Jenkinson]\n" + " (FMRIB Centre, University of Oxford, UK)\n" + " - Mainly adding low-level IO and changing things to allow gzipped\n" + " files to be read and written\n" + " - Full backwards compatability should have been maintained\n" + "\n", + "0.2 16 Nov 2004 [rickr]\n" + " (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - included Mark's changes in the AFNI distribution (including znzlib/)\n" + " (HAVE_ZLIB is commented out for the standard distribution)\n" + " - modified nifti_validfilename() and nifti_makebasename()\n" + " - added nifti_find_file_extension()\n" + "\n", + "0.3 3 Dec 2004 [rickr]\n" + " - note: header extensions are not yet checked for\n" + " - added formatted history as global string, for printing\n" + " - added nifti_disp_lib_hist(), to display the nifti library history\n" + " - added nifti_disp_lib_version(), to display the nifti library history\n", + " - re-wrote nifti_findhdrname()\n" + " o used nifti_find_file_extension()\n" + " o changed order of file tests (default is .nii, depends on input)\n" + " o free hdrname on failure\n" + " - made similar changes to nifti_findimgname()\n" + " - check for NULL return from nifti_findhdrname() calls\n", + " - removed most of ERREX() macros\n" + " - modified nifti_image_read()\n" + " o added debug info and error checking (on gni_debug > 0, only)\n" + " o fail if workingname is NULL\n" + " o check for failure to open header file\n" + " o free workingname on failure\n" + " o check for failure of nifti_image_load()\n" + " o check for failure of nifti_convert_nhdr2nim()\n", + " - changed nifti_image_load() to int, and check nifti_read_buffer return\n" + " - changed nifti_read_buffer() to fail on short read, and to count float\n" + " fixes (to print on debug)\n" + " - changed nifti_image_infodump to print to stderr\n" + " - updated function header comments, or moved comments above header\n" + " - removed const keyword\n" + " - added LNI_FERR() macro for error reporting on input files\n" + "\n", + "0.4 10 Dec 2004 [rickr] - added header extensions\n" + " - in nifti1_io.h:\n" + " o added num_ext and ext_list to the definition of nifti_image\n" + " o made many functions static (more to follow)\n" + " o added LNI_MAX_NIA_EXT_LEN, for max nifti_type 3 extension length\n", + " - added __DATE__ to version output in nifti_disp_lib_version()\n" + " - added nifti_disp_matrix_orient() to print orientation information\n" + " - added '.nia' as a valid file extension in nifti_find_file_extension()\n" + " - added much more debug output\n" + " - in nifti_image_read(), in the case of an ASCII header, check for\n" + " extensions after the end of the header\n", + " - added nifti_read_extensions() function\n" + " - added nifti_read_next_extension() function\n" + " - added nifti_add_exten_to_list() function\n" + " - added nifti_check_extension() function\n" + " - added nifti_write_extensions() function\n" + " - added nifti_extension_size() function\n" + " - in nifti_set_iname_offest():\n" + " o adjust offset by the extension size and the extender size\n", + " o fixed the 'ceiling modulo 16' computation\n" + " - in nifti_image_write_hdr_img2(): \n" + " o added extension writing\n" + " o check for NULL return from nifti_findimgname()\n" + " - include number of extensions in nifti_image_to_ascii() output\n" + " - in nifti_image_from_ascii():\n" + " o return bytes_read as a parameter, computed from the final spos\n" + " o extract num_ext from ASCII header\n" + "\n", + "0.5 14 Dec 2004 [rickr] - added sub-brick reading functions\n" + " - added nifti_brick_list type to nifti1_io.h, along with new prototypes\n" + " - added main nifti_image_read_bricks() function, with description\n" + " - added nifti_image_load_bricks() - library function (requires nim)\n" + " - added valid_nifti_brick_list() - library function\n" + " - added free_NBL() - library function\n", + " - added update_nifti_image_for_brick_list() for dimension update\n" + " - added nifti_load_NBL_bricks(), nifti_alloc_NBL_mem(),\n" + " nifti_copynsort() and force_positive() (static functions)\n" + " - in nifti_image_read(), check for failed load only if read_data is set\n" + " - broke most of nifti_image_load() into nifti_image_load_prep()\n" + "\n", + "0.6 15 Dec 2004 [rickr] - added sub-brick writing functionality\n" + " - in nifti1_io.h, removed znzlib directory from include - all nifti\n" + " library files are now under the nifti directory\n" + " - nifti_read_extensions(): print no offset warning for nifti_type 3\n" + " - nifti_write_all_data():\n" + " o pass nifti_brick_list * NBL, for optional writing\n" + " o if NBL, write each sub-brick, sequentially\n", + " - nifti_set_iname_offset(): case 1 must have sizeof() cast to int\n" + " - pass NBL to nifti_image_write_hdr_img2(), and allow NBL or data\n" + " - added nifti_image_write_bricks() wrapper for ...write_hdr_img2()\n" + " - included compression abilities\n" + "\n", + "0.7 16 Dec 2004 [rickr] - minor changes to extension reading\n" + "\n", + "0.8 21 Dec 2004 [rickr] - restrict extension reading, and minor changes\n" + " - in nifti_image_read(), compute bytes for extensions (see remaining)\n" + " - in nifti_read_extensions(), pass 'remain' as space for extensions,\n" + " pass it to nifti_read_next_ext(), and update for each one read \n" + " - in nifti_check_extension(), require (size <= remain)\n", + " - in update_nifti_image_brick_list(), update nvox\n" + " - in nifti_image_load_bricks(), make explicit check for nbricks <= 0\n" + " - in int_force_positive(), check for (!list)\n" + " - in swap_nifti_header(), swap sizeof_hdr, and reorder to struct order\n" + " - change get_filesize functions to signed ( < 0 is no file or error )\n", + " - in nifti_validfilename(), lose redundant (len < 0) check\n" + " - make print_hex_vals() static\n" + " - in disp_nifti_1_header, restrict string field widths\n" + "\n", + "0.9 23 Dec 2004 [rickr] - minor changes\n" + " - broke ASCII header reading out of nifti_image_read(), into new\n" + " functions has_ascii_header() and read_ascii_image()\n", + " - check image_read failure and znzseek failure\n" + " - altered some debug output\n" + " - nifti_write_all_data() now returns an int\n" + "\n", + "0.10 29 Dec 2004 [rickr]\n" + " - renamed nifti_valid_extension() to nifti_check_extension()\n" + " - added functions nifti_makehdrname() and nifti_makeimgname()\n" + " - added function valid_nifti_extensions()\n" + " - in nifti_write_extensions(), check for validity before writing\n", + " - rewrote nifti_image_write_hdr_img2():\n" + " o set write_data and leave_open flags from write_opts\n" + " o add debug print statements\n" + " o use nifti_write_ascii_image() for the ascii case\n" + " o rewrote the logic of all cases to be easier to follow\n", + " - broke out code as nifti_write_ascii_image() function\n" + " - added debug to top-level write functions, and free the znzFile\n" + " - removed unused internal function nifti_image_open()\n" + "\n", + "0.11 30 Dec 2004 [rickr] - small mods\n" + " - moved static function prototypes from header to C file\n" + " - free extensions in nifti_image_free()\n" + "\n", + "1.0 07 Jan 2005 [rickr] - INITIAL RELEASE VERSION\n" + " - added function nifti_set_filenames()\n" + " - added function nifti_read_header()\n" + " - added static function nhdr_looks_good()\n" + " - added static function need_nhdr_swap()\n" + " - exported nifti_add_exten_to_list symbol\n", + " - fixed #bytes written in nifti_write_extensions()\n" + " - only modify offset if it is too small (nifti_set_iname_offset)\n" + " - added nifti_type 3 to nifti_makehdrname and nifti_makeimgname\n" + " - added function nifti_set_filenames()\n" + "\n", + "1.1 07 Jan 2005 [rickr]\n" + " - in nifti_read_header(), swap if needed\n" + "\n", + "1.2 07 Feb 2005 [kate fissell c/o rickr] \n" + " - nifti1.h: added doxygen comments for main struct and #define groups\n" + " - nifti1_io.h: added doxygen comments for file and nifti_image struct\n" + " - nifti1_io.h: added doxygen comments for file and some functions\n" + " - nifti1_io.c: changed nifti_copy_nim_info to use memcpy\n" + "\n", + "1.3 09 Feb 2005 [rickr]\n" + " - nifti1.h: added doxygen comments for extension structs\n" + " - nifti1_io.h: put most #defines in #ifdef _NIFTI1_IO_C_ block\n" + " - added a doxygen-style description to every exported function\n" + " - added doxygen-style comments within some functions\n" + " - re-exported many znzFile functions that I had made static\n" + " - re-added nifti_image_open (sorry, Mark)\n" + " - every exported function now has 'nifti' in the name (19 functions)\n", + " - made sure every alloc() has a failure test\n" + " - added nifti_copy_extensions function, for use in nifti_copy_nim_info\n" + " - nifti_is_gzfile: added initial strlen test\n" + " - nifti_set_filenames: added set_byte_order parameter option\n" + " (it seems appropriate to set the BO when new files are associated)\n" + " - disp_nifti_1_header: prints to stdout (a.o.t. stderr), with fflush\n" + "\n", + "1.4 23 Feb 2005 [rickr] - sourceforge merge\n" + " - merged into the nifti_io CVS directory structure at sourceforge.net\n" + " - merged in 4 changes by Mark, and re-added his const keywords\n" + " - cast some pointers to (void *) for -pedantic compile option\n" + " - added nifti_free_extensions()\n" + "\n", + "1.5 02 Mar 2005 [rickr] - started nifti global options\n" + " - gni_debug is now g_opts.debug\n" + " - added validity check parameter to nifti_read_header\n" + " - need_nhdr_swap no longer does test swaps on the stack\n" + "\n", + "1.6 05 April 2005 [rickr] - validation and collapsed_image_read\n" + " - added nifti_read_collapsed_image(), an interface for reading partial\n" + " datasets, specifying a subset of array indices\n" + " - for read_collapsed_image, added static functions: rci_read_data(),\n" + " rci_alloc_mem(), and make_pivot_list()\n", + " - added nifti_nim_is_valid() to check for consistency (more to do)\n" + " - added nifti_nim_has_valid_dims() to do many dimensions tests\n" + "\n", + "1.7 08 April 2005 [rickr]\n" + " - added nifti_update_dims_from_array() - to update dimensions\n" + " - modified nifti_makehdrname() and nifti_makeimgname():\n" + " if prefix has a valid extension, use it (else make one up)\n" + " - added nifti_get_intlist - for making an array of ints\n" + " - fixed init of NBL->bsize in nifti_alloc_NBL_mem() {thanks, Bob}\n" + "\n", + "1.8 14 April 2005 [rickr]\n" + " - added nifti_set_type_from_names(), for nifti_set_filenames()\n" + " (only updates type if number of files does not match it)\n" + " - added is_valid_nifti_type(), just to be sure\n" + " - updated description of nifti_read_collapsed_image() for *data change\n" + " (if *data is already set, assume memory exists for results)\n" + " - modified rci_alloc_mem() to allocate only if *data is NULL\n" + "\n", + "1.9 19 April 2005 [rickr]\n" + " - added extension codes NIFTI_ECODE_COMMENT and NIFTI_ECODE_XCEDE\n" + " - added nifti_type codes NIFTI_MAX_ECODE and NIFTI_MAX_FTYPE\n" + " - added nifti_add_extension() {exported}\n" + " - added nifti_fill_extension() as a static function\n" + " - added nifti_is_valid_ecode() {exported}\n", + " - nifti_type values are now NIFTI_FTYPE_* file codes\n" + " - in nifti_read_extensions(), decrement 'remain' by extender size, 4\n" + " - in nifti_set_iname_offset(), case 1, update if offset differs\n" + " - only output '-d writing nifti file' if debug > 1\n" + "\n", + "1.10 10 May 2005 [rickr]\n" + " - files are read using ZLIB only if they end in '.gz'\n" + "\n", + "1.11 12 August 2005 [kate fissell]\n" + " - Kate's 0.2 release packaging, for sourceforge\n" + "\n", + "1.12 17 August 2005 [rickr] - comment (doxygen) updates\n" + " - updated comments for most functions (2 updates from Cinly Ooi)\n" + " - added nifti_type_and_names_match()\n" + "\n", + "1.12a 24 August 2005 [rickr] - remove all tabs from Clibs/*/*.[ch]\n", + "1.12b 25 August 2005 [rickr] - changes by Hans Johnson\n", + "1.13 25 August 2005 [rickr]\n", + " - finished changes by Hans for Insight\n" + " - added const in all appropraite parameter locations (30-40)\n" + " (any pointer referencing data that will not change)\n" + " - shortened all string constants below 509 character limit\n" + "1.14 28 October 2005 [HJohnson]\n", + " - use nifti_set_filenames() in nifti_convert_nhdr2nim()\n" + "1.15 02 November 2005 [rickr]\n", + " - added skip_blank_ext to nifti_global_options\n" + " - added nifti_set_skip_blank_ext(), to set option\n" + " - if skip_blank_ext and no extensions, do not read/write extender\n" + "1.16 18 November 2005 [rickr]\n", + " - removed any test or access of dim[i], i>dim[0]\n" + " - do not set pixdim for collapsed dims to 1.0, leave them as they are\n" + " - added magic and dim[i] tests in nifti_hdr_looks_good()\n" + " - added 2 size_t casts\n" + "1.17 22 November 2005 [rickr]\n", + " - in hdr->nim, for i > dim[0], pass 0 or 1, else set to 1\n" + "1.18 02 March 2006 [rickr]\n", + " - in nifti_alloc_NBL_mem(), fixed nt=0 case from 1.17 change\n" + "1.19 23 May 2006 [HJohnson,rickr]\n", + " - nifti_write_ascii_image(): free(hstr)\n" + " - nifti_copy_extensions(): clear num_ext and ext_list\n" + "1.20 27 Jun 2006 [rickr]\n", + " - nifti_findhdrname(): fixed assign of efirst to match stated logic\n" + " (problem found by Atle Bjørnerud)\n" + "1.21 05 Sep 2006 [rickr] update for nifticlib-0.4 release\n", + " - was reminded to actually add nifti_set_skip_blank_ext()\n" + " - init g_opts.skip_blank_ext to 0\n" + "1.22 01 Jun 2007 nifticlib-0.5 release\n", + "1.23 05 Jun 2007 nifti_add_exten_to_list: revert on failure, free old list\n" + "1.24 07 Jun 2007 nifti_copy_extensions: use esize-8 for data size\n" + "1.25 12 Jun 2007 [rickr] EMPTY_IMAGE creation\n", + " - added nifti_make_new_header() - to create from dims/dtype\n" + " - added nifti_make_new_nim() - to create from dims/dtype/fill\n" + " - added nifti_is_valid_datatype(), and more debug info\n", + "1.26 27 Jul 2007 [rickr] handle single volumes > 2^31 bytes (but < 2^32)\n", + "1.27 28 Jul 2007 [rickr] nim->nvox, NBL-bsize are now type size_t\n" + "1.28 30 Jul 2007 [rickr] size_t updates\n", + "1.29 08 Aug 2007 [rickr] for list, valid_nifti_brick_list requires 3 dims\n" + "1.30 08 Nov 2007 [Yaroslav/rickr]\n" + " - fix ARM struct alignment problem in byte-swapping routines\n", + "1.31 29 Nov 2007 [rickr] for nifticlib-1.0.0\n" + " - added nifti_datatype_to/from_string routines\n" + " - added DT_RGBA32/NIFTI_TYPE_RGBA32 datatype macros (2304)\n" + " - added NIFTI_ECODE_FREESURFER (14)\n", + "1.32 08 Dec 2007 [rickr]\n" + " - nifti_hdr_looks_good() allows ANALYZE headers (req. by V. Luccio)\n" + " - added nifti_datatype_is_valid()\n", + "1.33 05 Feb 2008 [hansj,rickr] - block nia.gz use\n" + "1.34 13 Jun 2008 [rickr] - added nifti_compiled_with_zlib()\n" + "1.35 03 Aug 2008 [rickr]\n", + " - deal with swapping, so that CPU type does not affect output\n" + " (motivated by C Burns)\n" + " - added nifti_analyze75 structure and nifti_swap_as_analyze()\n" + " - previous swap_nifti_header is saved as old_swap_nifti_header\n" + " - also swap UNUSED fields in nifti_1_header struct\n", + "1.36 07 Oct 2008 [rickr]\n", + " - added nifti_NBL_matches_nim() check for write_bricks()\n" + "1.37 10 Mar 2009 [rickr]\n", + " - H Johnson cast updates (06 Feb)\n" + " - added NIFTI_ECODE_PYPICKLE for PyNIfTI (06 Feb)\n" + " - added NIFTI_ECODEs 18-28 for the LONI MiND group\n" + "1.38 28 Apr 2009 [rickr]\n", + " - uppercase extensions are now valid (requested by M. Coursolle)\n" + " - nifti_set_allow_upper_fext controls this option (req by C. Ooi)\n" + "1.39 23 Jun 2009 [rickr]: added 4 checks of alloc() returns\n", + "1.40 16 Mar 2010 [rickr]: added NIFTI_ECODE_VOXBO for D. Kimberg\n", + "1.41 28 Apr 2010 [rickr]: added NIFTI_ECODE_CARET for J. Harwell\n", + "1.42 06 Jul 2010 [rickr]: trouble with large (gz) files\n", + " - noted/investigated by M Hanke and Y Halchenko\n" + " - fixed znzread/write, noting example by M Adler\n" + " - changed nifti_swap_* routines/calls to take size_t (6)\n" + "1.43 07 Jul 2010 [rickr]: fixed znzR/W to again return nmembers\n", + "1.44 19 Jul 2013 [rickr]: ITK compatibility updates from H Johnson\n", + "1.45 10 May 2019 [rickr]: added NIFTI_ECODE_QUANTIPHYSE\n", + "1.46 26 Sep 2019 [rickr]:\n" + " - nifti_read_ascii_image no longer closes fp or free's fname\n" + "----------------------------------------------------------------------\n" +}; + +/* rcr - todo + + - nifti_tool -copy_sform SFORM_DSET.nii -infile ORIG.nii -prefix PP + -copy_orient SFORM_DSET.nii -infile ORIG.nii -prefix PP + + - check converting nim 2 n2hdr + - update for n2 (and/or split from n1) + - is_nifti_file (maybe use nifti_header_version), nifti_hdr_looks_good + - extensions + - nifti_make_new_n1_header: check that dims are small enough (<2^15) + - nifti_convert_nim2nhdr: rename to nim2n1hdr and write nim2n2hdr + (maybe have nifti_convert_nim2nhdr wrap current version) + - nifti_set_iname_offset: n2 update via nifti_type + - track use of nifti_type + - nifti_image_write_hdr_img2: write nifti_2_header + */ + +static char const * const gni2_history[] = +{ + "----------------------------------------------------------------------\n" + "history (of nifti-2 library changes):\n" + "\n", + "2.00 02 Jan, 2014 [rickr]\n" + " Richard Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH\n" + " - initial version - change types to 64-bit based on new nifti_image\n", + "2.01 04 Apr, 2014 [rickr]\n" + " - added functionality for both nifti-1 and -2 headers\n" + " (read/display/swap/convert2nim/make_new_n?_hdr)\n" + " - still needs much nifti-2 functionality\n", + "2.02 11 May, 2015 [rickr]\n" + " - added to repository 28 Apr, 2015\n" + " - nifti_read_header() now returns found header struct\n" + "2.03 23 Jul, 2015 [rickr]\n" + " - possibly alter dimensions on CIFTI read\n" + " - return N-1 headers in unknown version cases\n", + "2.04 05 Aug, 2015 [rickr]\n" + " - have writing try NIFTI-2 if NIFTI-1 seems insufficient\n" + "2.05 15 Apr, 2016 [rickr]\n" + " - print int64_t using PRId64 macro, (ugly, but no warnings)\n" + "2.06 01 Oct, 2018 [rickr]\n" + " - errors should all mention NIFTI, slight additional clarity\n" + "2.07 18 Dec, 2018 [hmjohnson]\n", + " - added some const qualifiers\n" + " - removed register keywords\n" + " - fixed potential memory leaks in error conditions\n" + " - appeased compilers\n" + " - duped nifti1.h under nifti2, so directories do not cross reference\n" + "2.08 02 Jan, 2019 [rickr]\n" + " - fixed CIFTI extension reading if not first\n" + " - re-allow reading of ASCII headers (not part of standard)\n" + " - nifti_set_iname_offset() now takes nifti_ver, to adjust for size\n", + "2.09 10 May, 2019 [rickr]: added NIFTI_ECODE_QUANTIPHYSE\n" + "2.10 26 Sep, 2019 [rickr]: nifti_read_ascii_image no longer closes fp\n", + "2.11 3 Oct, 2019 [rickr]: added nifti_[d]mat33_mul\n", + "----------------------------------------------------------------------\n" +}; + +static const char gni_version[] + = "nifti-2 library version 2.11 (3 Oct, 2019)"; + +/*! global nifti options structure - init with defaults */ +/* see 'option accessor functions' */ +static nifti_global_options g_opts = { + 1, /* debug level */ + 0, /* skip_blank_ext - skip extender if no extensions */ + 1, /* allow_upper_fext - allow uppercase file extensions */ + 0, /* alter_cifti - alter CIFTI dims to use nx,t,u,v*/ +}; + +char nifti1_magic[4] = { 'n', '+', '1', '\0' }; +char nifti2_magic[8] = { 'n', '+', '2', '\0', '\r', '\n', '\032', '\n' }; + +/*! global nifti types structure list (per type, ordered oldest to newest) */ +static const nifti_type_ele nifti_type_list[] = { + /* type nbyper swapsize name */ + { 0, 0, 0, "DT_UNKNOWN" }, + { 0, 0, 0, "DT_NONE" }, + { 1, 0, 0, "DT_BINARY" }, /* not usable */ + { 2, 1, 0, "DT_UNSIGNED_CHAR" }, + { 2, 1, 0, "DT_UINT8" }, + { 2, 1, 0, "NIFTI_TYPE_UINT8" }, + { 4, 2, 2, "DT_SIGNED_SHORT" }, + { 4, 2, 2, "DT_INT16" }, + { 4, 2, 2, "NIFTI_TYPE_INT16" }, + { 8, 4, 4, "DT_SIGNED_INT" }, + { 8, 4, 4, "DT_INT32" }, + { 8, 4, 4, "NIFTI_TYPE_INT32" }, + { 16, 4, 4, "DT_FLOAT" }, + { 16, 4, 4, "DT_FLOAT32" }, + { 16, 4, 4, "NIFTI_TYPE_FLOAT32" }, + { 32, 8, 4, "DT_COMPLEX" }, + { 32, 8, 4, "DT_COMPLEX64" }, + { 32, 8, 4, "NIFTI_TYPE_COMPLEX64" }, + { 64, 8, 8, "DT_DOUBLE" }, + { 64, 8, 8, "DT_FLOAT64" }, + { 64, 8, 8, "NIFTI_TYPE_FLOAT64" }, + { 128, 3, 0, "DT_RGB" }, + { 128, 3, 0, "DT_RGB24" }, + { 128, 3, 0, "NIFTI_TYPE_RGB24" }, + { 255, 0, 0, "DT_ALL" }, + { 256, 1, 0, "DT_INT8" }, + { 256, 1, 0, "NIFTI_TYPE_INT8" }, + { 512, 2, 2, "DT_UINT16" }, + { 512, 2, 2, "NIFTI_TYPE_UINT16" }, + { 768, 4, 4, "DT_UINT32" }, + { 768, 4, 4, "NIFTI_TYPE_UINT32" }, + { 1024, 8, 8, "DT_INT64" }, + { 1024, 8, 8, "NIFTI_TYPE_INT64" }, + { 1280, 8, 8, "DT_UINT64" }, + { 1280, 8, 8, "NIFTI_TYPE_UINT64" }, + { 1536, 16, 16, "DT_FLOAT128" }, + { 1536, 16, 16, "NIFTI_TYPE_FLOAT128" }, + { 1792, 16, 8, "DT_COMPLEX128" }, + { 1792, 16, 8, "NIFTI_TYPE_COMPLEX128" }, + { 2048, 32, 16, "DT_COMPLEX256" }, + { 2048, 32, 16, "NIFTI_TYPE_COMPLEX256" }, + { 2304, 4, 0, "DT_RGBA32" }, + { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, +}; + +/*---------------------------------------------------------------------------*/ +/* prototypes for internal functions - not part of exported library */ + +/* extension routines */ +static int nifti_read_extensions(nifti_image *nim, znzFile fp, int64_t remain); +static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, int remain, znzFile fp ); +static int nifti_check_extension(nifti_image *nim, int size,int code, int rem); +static void update_nifti_image_for_brick_list(nifti_image * nim, + int64_t nbricks); +static int nifti_add_exten_to_list(nifti1_extension * new_ext, + nifti1_extension ** list, int new_length); +static int nifti_fill_extension(nifti1_extension * ext, const char * data, + int len, int ecode); +static void compute_strides(int64_t *strides,const int64_t *size,int nbyper); + +/* NBL routines */ +static int nifti_load_NBL_bricks(nifti_image * nim , const int64_t * slist, + const int64_t * sindex, nifti_brick_list * NBL, znzFile fp ); +static int nifti_alloc_NBL_mem( nifti_image * nim, int64_t nbricks, + nifti_brick_list * nbl); +static int nifti_copynsort(int64_t nbricks, const int64_t *blist, + int64_t **slist, int64_t **sindex); +static int nifti_NBL_matches_nim(const nifti_image *nim, + const nifti_brick_list *NBL); + +/* for nifti_read_collapsed_image: */ +static int rci_read_data(nifti_image *nim, int *pivots, int64_t *prods, + int nprods, const int64_t dims[], char *data, + znzFile fp, int64_t base_offset); +static int rci_alloc_mem(void **data, const int64_t prods[8], int nprods, int nbyper); +static int make_pivot_list(nifti_image * nim, const int64_t dims[], + int pivots[], int64_t prods[], int * nprods ); + +/* misc */ +static int compare_strlist (const char * str, char ** strlist, int len); +static int fileext_compare (const char * test_ext, const char * known_ext); +static int fileext_n_compare (const char * test_ext, + const char * known_ext, size_t maxlen); +static int is_mixedcase (const char * str); +static int is_uppercase (const char * str); +static int make_lowercase (char * str); +static int make_uppercase (char * str); +static int need_nhdr_swap (short dim0, int hdrsize); +static int print_hex_vals (const char * data, size_t nbytes, FILE * fp); +static int unescape_string (char *str); /* string utility functions */ +static char *escapize_string (const char *str); + +/* consider for export */ +static int nifti_ext_type_index(nifti_image * nim, int ecode); + +/* internal I/O routines */ +static znzFile nifti_image_load_prep( nifti_image *nim ); +static void nifti_image_mem_load_prep( znzFile fp, nifti_image *nim, const uint8_t *data , const size_t size, int gz, const size_t estimated_size); +static int has_ascii_header(znzFile fp); +/*---------------------------------------------------------------------------*/ + + +/* for calling from some main program */ + +/*----------------------------------------------------------------------*/ +/*! display the nifti library module history (via stdout) +*//*--------------------------------------------------------------------*/ +void nifti2_disp_lib_hist( int ver ) +{ + int c, len; + + switch ( ver ) { + default: { + Rc_fprintf_stderr("** NIFTI disp_lib_list: bad ver %d\n", ver); + break; + } + + case 0: + case 2: { + len = sizeof(gni2_history)/sizeof(char *); + for( c = 0; c < len; c++ ) + Rc_fputs_stdout(gni2_history[c]); + break; + } + case 1: { + len = sizeof(gni1_history)/sizeof(char *); + for( c = 0; c < len; c++ ) + Rc_fputs_stdout(gni1_history[c]); + break; + } + } +} + +/*----------------------------------------------------------------------*/ +/*! display the nifti library version (via stdout) +*//*--------------------------------------------------------------------*/ +void nifti_disp_lib_version( void ) +{ + Rc_printf("%s, compiled %s\n", gni_version, __DATE__); +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_image_read_bricks - read nifti data as array of bricks + * + * 13 Dec 2004 [rickr] + * + * \param hname - filename of dataset to read (must be valid) + * \param nbricks - number of sub-bricks to read + * (if blist is valid, nbricks must be > 0) + * \param blist - list of sub-bricks to read + * (can be NULL; if NULL, read complete dataset) + * \param NBL - pointer to empty nifti_brick_list struct + * (must be a valid pointer) + * + * \return + *
nim - same as nifti_image_read, but + * nim->nt = NBL->nbricks (or nt*nu*nv*nw) + * nim->nu,nv,nw = 1 + * nim->data = NULL + *
NBL - filled with data volumes + * + * By default, this function will read the nifti dataset and break the data + * into a list of nt*nu*nv*nw sub-bricks, each having size nx*ny*nz elements. + * That is to say, instead of reading the entire dataset as a single array, + * break it up into sub-bricks (volumes), each of size nx*ny*nz elements. + * + * Note: in the returned nifti_image, nu, nv and nw will always be 1. The + * intention of this function is to collapse the dataset into a single + * array of volumes (of length nbricks or nt*nu*nv*nw). + * + * If 'blist' is valid, it is taken to be a list of sub-bricks, of length + * 'nbricks'. The data will still be separated into sub-bricks of size + * nx*ny*nz elements, but now 'nbricks' sub-bricks will be returned, of the + * caller's choosing via 'blist'. + * + * E.g. consider a dataset with 12 sub-bricks (numbered 0..11), and the + * following code: + * + *
+ * { nifti_brick_list   NB_orig, NB_select;
+ *   nifti_image      * nim_orig, * nim_select;
+ *   int                blist[5] = { 7, 0, 5, 5, 9 };
+ *
+ *   nim_orig   = nifti_image_read_bricks("myfile.nii", 0, NULL,  &NB_orig);
+ *   nim_select = nifti_image_read_bricks("myfile.nii", 5, blist, &NB_select);
+ * }
+ * 
+ * + * Here, nim_orig gets the entire dataset, where NB_orig.nbricks = 12. But + * nim_select has NB_select.nbricks = 5. + * + * Note that the first case is not quite the same as just calling the + * nifti_image_read function, as here the data is separated into sub-bricks. + * + * Note that valid blist elements are in [0..nt*nu*nv*nw-1], + * or written [ 0 .. (dim[4]*dim[5]*dim[6]*dim[7] - 1) ]. + * + * Note that, as is the case with all of the reading functions, the + * data will be allocated, read in, and properly byte-swapped, if + * necessary. + * + * \sa nifti_image_load_bricks, nifti_free_NBL, valid_nifti_brick_list, + nifti_image_read +*//*----------------------------------------------------------------------*/ +nifti_image *nifti2_image_read_bricks(const char * hname, int64_t nbricks, + const int64_t * blist, nifti_brick_list * NBL) +{ + nifti_image * nim; + + if( !hname || !NBL ){ + Rc_fprintf_stderr("** nifti_image_read_bricks: bad params (%p,%p)\n", + hname, (void *)NBL); + return NULL; + } + + if( blist && nbricks <= 0 ){ + /* use PRId64 for printing int64_t 14 Apr 2016 */ + Rc_fprintf_stderr("** nifti_image_read_bricks: bad nbricks, %" PRId64 "\n", + nbricks); + return NULL; + } + + nim = nifti_image_read(hname, 0); /* read header, but not data */ + + if( !nim ) return NULL; /* errors were already printed */ + + /* if we fail, free image and return */ + if( nifti_image_load_bricks(nim, nbricks, blist, NBL) <= 0 ){ + nifti_image_free(nim); + return NULL; + } + + if( blist ) update_nifti_image_for_brick_list(nim, nbricks); + + return nim; +} + + +/*---------------------------------------------------------------------- + * update_nifti_image_for_brick_list - update nifti_image + * + * When loading a specific brick list, the distinction between + * nt, nu, nv and nw is lost. So put everything in t, and set + * dim[0] = 4. + *----------------------------------------------------------------------*/ +static void update_nifti_image_for_brick_list( nifti_image * nim , + int64_t nbricks ) +{ + int64_t ndim; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d updating image dimensions for %" PRId64 + " bricks in list\n", nbricks); + Rc_fprintf_stderr(" ndim = %" PRId64 "\n",nim->ndim); + Rc_fprintf_stderr(" nx,ny,nz,nt,nu,nv,nw: (%" PRId64 ",%" PRId64 + ",%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 ")\n", + nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); + } + + nim->nt = nbricks; + nim->nu = nim->nv = nim->nw = 1; + nim->dim[4] = nbricks; + nim->dim[5] = nim->dim[6] = nim->dim[7] = 1; + + /* compute nvox */ + /* do not rely on dimensions above dim[0] 16 Nov 2005 [rickr] */ + for( nim->nvox = 1, ndim = 1; ndim <= nim->dim[0]; ndim++ ) + nim->nvox *= nim->dim[ndim]; + + /* update the dimensions to 4 or lower */ + for( ndim = 4; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) + ; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d ndim = %" PRId64 " -> %" PRId64 "\n",nim->ndim, ndim); + Rc_fprintf_stderr(" --> (%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 + ",%" PRId64 ",%" PRId64 ",%" PRId64 ")\n", + nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); + } + + nim->dim[0] = nim->ndim = ndim; +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_update_dims_from_array - update nx, ny, ... from nim->dim[] + + Fix all the dimension information, based on a new nim->dim[]. + + Note: we assume that dim[0] will not increase. + + Check for updates to pixdim[], dx,..., nx,..., nvox, ndim, dim[0]. +*//*--------------------------------------------------------------------*/ +int nifti2_update_dims_from_array( nifti_image * nim ) +{ + int c; + int64_t ndim; + + if( !nim ){ + Rc_fprintf_stderr("** NIFTI update_dims: missing nim\n"); + return 1; + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d updating image dimensions given nim->dim:"); + for( c = 0; c < 8; c++ ) Rc_fprintf_stderr(" %" PRId64, nim->dim[c]); + Rc_fputc_stderr('\n'); + } + + /* verify dim[0] first */ + if(nim->dim[0] < 1 || nim->dim[0] > 7){ + Rc_fprintf_stderr("** NIFTI: invalid dim[0], dim[] = "); + for( c = 0; c < 8; c++ ) Rc_fprintf_stderr(" %" PRId64, nim->dim[c]); + Rc_fputc_stderr('\n'); + return 1; + } + + /* set nx, ny ..., dx, dy, ..., one by one */ + + /* less than 1, set to 1, else copy */ + if(nim->dim[1] < 1) nim->nx = nim->dim[1] = 1; + else nim->nx = nim->dim[1]; + nim->dx = nim->pixdim[1]; + + /* if undefined, or less than 1, set to 1 */ + if(nim->dim[0] < 2 || (nim->dim[0] >= 2 && nim->dim[2] < 1)) + nim->ny = nim->dim[2] = 1; + else + nim->ny = nim->dim[2]; + /* copy delta values, in any case */ + nim->dy = nim->pixdim[2]; + + if(nim->dim[0] < 3 || (nim->dim[0] >= 3 && nim->dim[3] < 1)) + nim->nz = nim->dim[3] = 1; + else /* just copy vals from arrays */ + nim->nz = nim->dim[3]; + nim->dz = nim->pixdim[3]; + + if(nim->dim[0] < 4 || (nim->dim[0] >= 4 && nim->dim[4] < 1)) + nim->nt = nim->dim[4] = 1; + else /* just copy vals from arrays */ + nim->nt = nim->dim[4]; + nim->dt = nim->pixdim[4]; + + if(nim->dim[0] < 5 || (nim->dim[0] >= 5 && nim->dim[5] < 1)) + nim->nu = nim->dim[5] = 1; + else /* just copy vals from arrays */ + nim->nu = nim->dim[5]; + nim->du = nim->pixdim[5]; + + if(nim->dim[0] < 6 || (nim->dim[0] >= 6 && nim->dim[6] < 1)) + nim->nv = nim->dim[6] = 1; + else /* just copy vals from arrays */ + nim->nv = nim->dim[6]; + nim->dv = nim->pixdim[6]; + + if(nim->dim[0] < 7 || (nim->dim[0] >= 7 && nim->dim[7] < 1)) + nim->nw = nim->dim[7] = 1; + else /* just copy vals from arrays */ + nim->nw = nim->dim[7]; + nim->dw = nim->pixdim[7]; + + for( c = 1, nim->nvox = 1; c <= nim->dim[0]; c++ ) + nim->nvox *= nim->dim[c]; + + /* compute ndim, assuming it can be no larger than the old one */ + for( ndim = nim->dim[0]; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) + ; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d ndim = %" PRId64 " -> %" PRId64 "\n",nim->ndim, ndim); + Rc_fprintf_stderr(" --> (%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 + ",%" PRId64 ",%" PRId64 ",%" PRId64 ")\n", + nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); + } + + nim->dim[0] = nim->ndim = ndim; + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! Load the image data from disk into an already-prepared image struct. + * + * \param nim - initialized nifti_image, without data + * \param nbricks - the length of blist (must be 0 if blist is NULL) + * \param blist - an array of xyz volume indices to read (can be NULL) + * \param NBL - pointer to struct where resulting data will be stored + * + * If blist is NULL, read all sub-bricks. + * + * \return the number of loaded bricks (NBL->nbricks), + * 0 on failure, < 0 on error + * + * NOTE: it is likely that another function will copy the data pointers + * out of NBL, in which case the only pointer the calling function + * will want to free is NBL->bricks (not each NBL->bricks[i]). +*//*--------------------------------------------------------------------*/ +int nifti2_image_load_bricks( nifti_image * nim , int64_t nbricks, + const int64_t * blist, nifti_brick_list * NBL ) +{ + int64_t * slist = NULL, * sindex = NULL; + int rv; + znzFile fp; + + /* we can have blist == NULL */ + if( !nim || !NBL ){ + Rc_fprintf_stderr("** nifti_image_load_bricks, bad params (%p,%p)\n", + (void *)nim, (void *)NBL); + return -1; + } + + if( blist && nbricks <= 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d load_bricks: received blist with nbricks = " + "%" PRId64 "," "ignoring blist\n", nbricks); + blist = NULL; /* pretend nothing was passed */ + } + + if( blist && ! valid_nifti_brick_list(nim, nbricks, blist, g_opts.debug>0) ) + return -1; + + /* for efficiency, let's read the file in order */ + if( blist && nifti_copynsort( nbricks, blist, &slist, &sindex ) != 0 ) + return -1; + + /* open the file and position the FILE pointer */ + fp = nifti_image_load_prep( nim ); + if( !fp ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_image_load_bricks, failed load_prep\n"); + if( blist ){ free(slist); free(sindex); } + return -1; + } + + /* this will flag to allocate defaults */ + if( !blist ) nbricks = 0; + if( nifti_alloc_NBL_mem( nim, nbricks, NBL ) != 0 ){ + if( blist ){ free(slist); free(sindex); } + znzclose(fp); + return -1; + } + + rv = nifti_load_NBL_bricks(nim, slist, sindex, NBL, fp); + + if( rv != 0 ){ + nifti_free_NBL( NBL ); /* failure! */ + NBL->nbricks = 0; /* repetative, but clear */ + } + + if( slist ){ free(slist); free(sindex); } + + znzclose(fp); + + return NBL->nbricks; +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_free_NBL - free all pointers and clear structure + * + * note: this does not presume to free the structure pointer +*//*--------------------------------------------------------------------*/ +void nifti2_free_NBL( nifti_brick_list * NBL ) +{ + int c; + + if( NBL->bricks ){ + for( c = 0; c < NBL->nbricks; c++ ) + if( NBL->bricks[c] ) free(NBL->bricks[c]); + free(NBL->bricks); + NBL->bricks = NULL; + } + + NBL->bsize = NBL->nbricks = 0; +} + + +/*---------------------------------------------------------------------- + * nifti_load_NBL_bricks - read the file data into the NBL struct + * + * return 0 on success, -1 on failure + *----------------------------------------------------------------------*/ +static int nifti_load_NBL_bricks( nifti_image * nim , const int64_t * slist, + const int64_t * sindex, nifti_brick_list * NBL, znzFile fp ) +{ + int64_t oposn, fposn; /* orig and current file positions */ + int64_t rv, test; + int64_t c; + int64_t prev, isrc, idest; /* previous/current sub-brick, and new index */ + + test = znztell(fp); /* store current file position */ + if( test < 0 ){ + Rc_fprintf_stderr("** NIFTI load bricks: ztell failed??\n"); + return -1; + } + fposn = oposn = test; + + /* first, handle the default case, no passed blist */ + if( !slist ){ + for( c = 0; c < NBL->nbricks; c++ ) { + rv = nifti_read_buffer(fp, NBL->bricks[c], NBL->bsize, nim); + if( rv != NBL->bsize ){ + Rc_fprintf_stderr("** NIFTI load bricks: cannot read brick %" PRId64 + " from '%s'\n", + c, nim->iname ? nim->iname : nim->fname); + return -1; + } + } + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d read %" PRId64 " default %" PRId64 + "-byte bricks from file %s\n", + NBL->nbricks, NBL->bsize, + nim->iname ? nim->iname:nim->fname ); + return 0; + } + + if( !sindex ){ + Rc_fprintf_stderr("** NIFTI load_NBL_bricks: missing index list\n"); + return -1; + } + + prev = -1; /* use prev for previous sub-brick */ + for( c = 0; c < NBL->nbricks; c++ ){ + isrc = slist[c]; /* this is original brick index (c is new one) */ + idest = sindex[c]; /* this is the destination index for this data */ + + /* if this sub-brick is not the previous, we must read from disk */ + if( isrc != prev ){ + + /* if we are not looking at the correct sub-brick, scan forward */ + if( fposn != (oposn + isrc*NBL->bsize) ){ + fposn = oposn + isrc*NBL->bsize; + /* rcr - znz functions need to handle 64-bit cases, */ + /* see setting _FILE_OFFSET_BITS */ + if( znzseek(fp, fposn, SEEK_SET) < 0 ){ + Rc_fprintf_stderr("** NIFTI: failed to locate brick %" PRId64 + " in file '%s'\n", + isrc, nim->iname ? nim->iname : nim->fname); + return -1; + } + } + + /* only 10,000 lines later and we're actually reading something! */ + rv = nifti_read_buffer(fp, NBL->bricks[idest], NBL->bsize, nim); + if( rv != NBL->bsize ){ + Rc_fprintf_stderr("** NIFTI: failed to read brick %" PRId64 + " from file '%s'\n", + isrc, nim->iname ? nim->iname : nim->fname); + if( g_opts.debug > 1 ) + Rc_fprintf_stderr(" (read %" PRId64 " of %" PRId64 " bytes)\n", + rv, NBL->bsize); + return -1; + } + fposn += NBL->bsize; + } else { + /* we have already read this sub-brick, just copy the previous one */ + /* note that this works because they are sorted */ + memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], NBL->bsize); + } + + prev = isrc; /* in any case, note the now previous sub-brick */ + } + + return 0; +} + + +/*---------------------------------------------------------------------- + * nifti_alloc_NBL_mem - allocate memory for bricks + * + * return 0 on success, -1 on failure + *----------------------------------------------------------------------*/ +static int nifti_alloc_NBL_mem(nifti_image * nim, int64_t nbricks, + nifti_brick_list * nbl) +{ + int64_t c; + + /* if nbricks is not specified, use the default */ + if( nbricks > 0 ) nbl->nbricks = nbricks; + else { /* I missed this one with the 1.17 change 02 Mar 2006 [rickr] */ + nbl->nbricks = 1; + for( c = 4; c <= nim->ndim; c++ ) + nbl->nbricks *= nim->dim[c]; + } + + nbl->bsize = nim->nx * nim->ny * nim->nz * nim->nbyper; /* bytes */ + nbl->bricks = (void **)malloc(nbl->nbricks * sizeof(void *)); + + if( ! nbl->bricks ){ + Rc_fprintf_stderr("** NIFTI NANM: failed to alloc %" PRId64 + " void ptrs\n",nbricks); + return -1; + } + + for( c = 0; c < nbl->nbricks; c++ ){ + nbl->bricks[c] = malloc(nbl->bsize); + if( ! nbl->bricks[c] ){ + Rc_fprintf_stderr("** NIFTI NANM: failed to alloc %" PRId64 + " bytes for brick %" PRId64 "\n", nbl->bsize, c); + /* so free and clear everything before returning */ + while( c > 0 ){ + c--; + free(nbl->bricks[c]); + } + free(nbl->bricks); + nbl->bricks = NULL; + nbl->bsize = nbl->nbricks = 0; + return -1; + } + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d NANM: alloc'd %" PRId64 " bricks of %" PRId64 + " bytes for NBL\n", nbl->nbricks, nbl->bsize); + + return 0; +} + + +/*---------------------------------------------------------------------- + * nifti_copynsort - copy int list, and sort with indices + * + * 1. duplicate the incoming list + * 2. create an sindex list, and init with 0..nbricks-1 + * 3. do a slow insertion sort on the small slist, along with sindex list + * 4. check results, just to be positive + * + * So slist is sorted, and sindex hold original positions. + * + * return 0 on success, -1 on failure + *----------------------------------------------------------------------*/ +static int nifti_copynsort(int64_t nbricks, const int64_t *blist, + int64_t ** slist, int64_t ** sindex) +{ + int64_t * stmp, * itmp; /* for ease of typing/reading */ + int64_t c1, c2, spos, tmp; + + *slist = (int64_t *)malloc(nbricks * sizeof(int64_t)); + *sindex = (int64_t *)malloc(nbricks * sizeof(int64_t)); + + if( !*slist || !*sindex ){ + Rc_fprintf_stderr("** NIFTI NCS: failed to alloc %" PRId64 + " ints for sorting\n", nbricks); + if(*slist) free(*slist); /* maybe one succeeded */ + if(*sindex) free(*sindex); + return -1; + } + + /* init the lists */ + for( c1 = 0; c1 < nbricks; c1++ ) { + (*slist)[c1] = blist[c1]; + (*sindex)[c1] = c1; + } + + /* now actually sort slist */ + stmp = *slist; + itmp = *sindex; + for( c1 = 0; c1 < nbricks-1; c1++ ) { + /* find smallest value, init to current */ + spos = c1; + for( c2 = c1+1; c2 < nbricks; c2++ ) + if( stmp[c2] < stmp[spos] ) spos = c2; + if( spos != c1 ) /* swap: fine, don't maintain sub-order, see if I care */ + { + tmp = stmp[c1]; /* first swap the sorting values */ + stmp[c1] = stmp[spos]; + stmp[spos] = tmp; + + tmp = itmp[c1]; /* then swap the index values */ + itmp[c1] = itmp[spos]; + itmp[spos] = tmp; + } + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr( "+d sorted indexing list:\n"); + Rc_fprintf_stderr( " orig : "); + for( c1 = 0; c1 < nbricks; c1++ ) Rc_fprintf_stderr(" %" PRId64, blist[c1]); + Rc_fprintf_stderr("\n new : "); + for( c1 = 0; c1 < nbricks; c1++ ) Rc_fprintf_stderr(" %" PRId64, stmp[c1]); + Rc_fprintf_stderr("\n indices: "); + for( c1 = 0; c1 < nbricks; c1++ ) Rc_fprintf_stderr(" %" PRId64, itmp[c1]); + Rc_fputc_stderr('\n'); + } + + /* check the sort (why not? I've got time...) */ + for( c1 = 0; c1 < nbricks-1; c1++ ){ + if( (stmp[c1] > stmp[c1+1]) || (blist[itmp[c1]] != stmp[c1]) ){ + Rc_fprintf_stderr("** NIFTI sorting screw-up, way to go, rick!\n"); + free(stmp); free(itmp); *slist = NULL; *sindex = NULL; + return -1; + } + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d sorting is okay\n"); + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! valid_nifti_brick_list - check sub-brick list for image + * + * This function verifies that nbricks and blist are appropriate + * for use with this nim, based on the dimensions. + * + * \param nim nifti_image to check against + * \param nbricks number of brick indices in blist + * \param blist list of brick indices to check in nim + * \param disp_error if this flag is set, report errors to user + * + * \return 1 if valid, 0 if not +*//*--------------------------------------------------------------------*/ +int valid_nifti2_brick_list(nifti_image * nim , int64_t nbricks, + const int64_t * blist, int disp_error) +{ + int64_t c, nsubs; + + if( !nim ){ + if( disp_error || g_opts.debug > 0 ) + Rc_fprintf_stderr("** valid_nifti_brick_list: missing nifti image\n"); + return 0; + } + + if( nbricks <= 0 || !blist ){ + if( disp_error || g_opts.debug > 1 ) + Rc_fprintf_stderr("** valid_nifti_brick_list: no brick list to check\n"); + return 0; + } + + if( nim->dim[0] < 3 ){ + if( disp_error || g_opts.debug > 1 ) + Rc_fprintf_stderr("** NIFTI: cannot read explict brick list from %" PRId64 + "-D dataset\n", nim->dim[0]); + return 0; + } + + /* nsubs sub-brick is nt*nu*nv*nw */ + for( c = 4, nsubs = 1; c <= nim->dim[0]; c++ ) + nsubs *= nim->dim[c]; + + if( nsubs <= 0 ){ + Rc_fprintf_stderr("** NIFTI VNBL warning: bad dim list (%" PRId64 ",%" + PRId64 ",%" PRId64 ",%" PRId64 ")\n", + nim->dim[4], nim->dim[5], nim->dim[6], nim->dim[7]); + return 0; + } + + for( c = 0; c < nbricks; c++ ) + if( (blist[c] < 0) || (blist[c] >= nsubs) ){ + if( disp_error || g_opts.debug > 1 ) + Rc_fprintf_stderr( + "** NIFTI volume index %" PRId64 " (#%" PRId64 ")" + " is out of range [0,%" PRId64 "]\n", blist[c], c, nsubs-1); + return 0; + } + + return 1; /* all is well */ +} + +/*----------------------------------------------------------------------*/ +/* verify that NBL struct is a valid data source for the image + * + * return 1 if so, 0 otherwise +*//*--------------------------------------------------------------------*/ +static int nifti_NBL_matches_nim(const nifti_image *nim, + const nifti_brick_list *NBL) +{ + int64_t volbytes = 0; /* bytes per volume */ + int64_t nvols = 0; + int ind, errs = 0; + + + if( !nim || !NBL ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_NBL_matches_nim: NULL pointer(s)\n"); + return 0; + } + + /* for nim, compute volbytes and nvols */ + if( nim->ndim > 0 ) { + /* first 3 indices are over a single volume */ + volbytes = (int64_t)nim->nbyper; + for( ind = 1; ind <= nim->ndim && ind < 4; ind++ ) + volbytes *= nim->dim[ind]; + + for( ind = 4, nvols = 1; ind <= nim->ndim; ind++ ) + nvols *= nim->dim[ind]; + } + + if( volbytes != NBL->bsize ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** NIFTI NBL/nim mismatch, volbytes = %" PRId64 + ", %" PRId64 "\n", NBL->bsize, volbytes); + errs++; + } + + if( nvols != NBL->nbricks ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** NIFTI NBL/nim mismatch, nvols = %" PRId64 + ", %" PRId64 "\n", NBL->nbricks, nvols); + errs++; + } + + if( errs ) return 0; + else if ( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- nim/NBL agree: nvols = %" PRId64 + ", nbytes = %" PRId64 "\n", nvols, volbytes); + + return 1; +} + +/* end of new nifti_image_read_bricks() functionality */ + +/*----------------------------------------------------------------------*/ +/*! display the orientation from the quaternian fields + * + * \param mesg if non-NULL, display this message first + * \param mat the matrix to convert to "nearest" orientation + * + * \return -1 if results cannot be determined, 0 if okay +*//*--------------------------------------------------------------------*/ +int nifti2_disp_matrix_orient( const char * mesg, nifti_dmat44 mat ) +{ + int i, j, k; + + if ( mesg ) Rc_fputs_stderr( mesg ); /* use stdout? */ + + nifti_dmat44_to_orientation( mat, &i,&j,&k ); + if ( i <= 0 || j <= 0 || k <= 0 ) return -1; + + /* so we have good codes */ + Rc_fprintf_stderr(" i orientation = '%s'\n" + " j orientation = '%s'\n" + " k orientation = '%s'\n", + nifti_orientation_string(i), + nifti_orientation_string(j), + nifti_orientation_string(k) ); + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! duplicate the given string (alloc length+1) + * + * \return allocated pointer (or NULL on failure) +*//*--------------------------------------------------------------------*/ +char *nifti_strdup(const char *str) +{ + char *dup; + + if( !str ) return NULL; /* allow calls passing NULL */ + + dup = (char *)malloc(strlen(str) + 1); + + /* check for failure */ + if( dup ) strcpy(dup, str); + else Rc_fprintf_stderr("** nifti_strdup: failed to alloc %" PRId64 + " bytes\n", (int64_t)(strlen(str)+1)); + + return dup; +} + + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI datatype. + + \param dt NIfTI-1 datatype + + \return pointer to static string holding the datatype name + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_DATATYPES group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const * nifti_datatype_string( int dt ) +{ + switch( dt ){ + case DT_UNKNOWN: return "UNKNOWN" ; + case DT_BINARY: return "BINARY" ; + case DT_INT8: return "INT8" ; + case DT_UINT8: return "UINT8" ; + case DT_INT16: return "INT16" ; + case DT_UINT16: return "UINT16" ; + case DT_INT32: return "INT32" ; + case DT_UINT32: return "UINT32" ; + case DT_INT64: return "INT64" ; + case DT_UINT64: return "UINT64" ; + case DT_FLOAT32: return "FLOAT32" ; + case DT_FLOAT64: return "FLOAT64" ; + case DT_FLOAT128: return "FLOAT128" ; + case DT_COMPLEX64: return "COMPLEX64" ; + case DT_COMPLEX128: return "COMPLEX128" ; + case DT_COMPLEX256: return "COMPLEX256" ; + case DT_RGB24: return "RGB24" ; + case DT_RGBA32: return "RGBA32" ; + default: break ; + } + return "**ILLEGAL**" ; +} + +/*----------------------------------------------------------------------*/ +/*! Determine if the datatype code dt is an integer type (1=YES, 0=NO). + + \return whether the given NIfTI-1 datatype code is valid + + \sa NIFTI1_DATATYPES group in nifti1.h +*//*--------------------------------------------------------------------*/ +int nifti_is_inttype( int dt ) +{ + switch( dt ){ + case DT_UNKNOWN: return 0 ; + case DT_BINARY: return 0 ; + case DT_INT8: return 1 ; + case DT_UINT8: return 1 ; + case DT_INT16: return 1 ; + case DT_UINT16: return 1 ; + case DT_INT32: return 1 ; + case DT_UINT32: return 1 ; + case DT_INT64: return 1 ; + case DT_UINT64: return 1 ; + case DT_FLOAT32: return 0 ; + case DT_FLOAT64: return 0 ; + case DT_FLOAT128: return 0 ; + case DT_COMPLEX64: return 0 ; + case DT_COMPLEX128: return 0 ; + case DT_COMPLEX256: return 0 ; + case DT_RGB24: return 1 ; + case DT_RGBA32: return 1 ; + default: break ; + } + return 0 ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI units type. + + \param uu NIfTI-1 unit code + + \return pointer to static string for the given unit type + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_UNITS group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_units_string( int uu ) +{ + switch( uu ){ + case NIFTI_UNITS_METER: return "m" ; + case NIFTI_UNITS_MM: return "mm" ; + case NIFTI_UNITS_MICRON: return "um" ; + case NIFTI_UNITS_SEC: return "s" ; + case NIFTI_UNITS_MSEC: return "ms" ; + case NIFTI_UNITS_USEC: return "us" ; + case NIFTI_UNITS_HZ: return "Hz" ; + case NIFTI_UNITS_PPM: return "ppm" ; + case NIFTI_UNITS_RADS: return "rad/s" ; + default: break ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI transform type. + + \param xx NIfTI-1 xform code + + \return pointer to static string describing xform code + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_XFORM_CODES group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_xform_string( int xx ) +{ + switch( xx ){ + case NIFTI_XFORM_SCANNER_ANAT: return "Scanner Anat" ; + case NIFTI_XFORM_ALIGNED_ANAT: return "Aligned Anat" ; + case NIFTI_XFORM_TALAIRACH: return "Talairach" ; + case NIFTI_XFORM_MNI_152: return "MNI_152" ; + default: break ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI intent type. + + \param ii NIfTI-1 intent code + + \return pointer to static string describing code + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_INTENT_CODES group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_intent_string( int ii ) +{ + switch( ii ){ + case NIFTI_INTENT_CORREL: return "Correlation statistic" ; + case NIFTI_INTENT_TTEST: return "T-statistic" ; + case NIFTI_INTENT_FTEST: return "F-statistic" ; + case NIFTI_INTENT_ZSCORE: return "Z-score" ; + case NIFTI_INTENT_CHISQ: return "Chi-squared distribution" ; + case NIFTI_INTENT_BETA: return "Beta distribution" ; + case NIFTI_INTENT_BINOM: return "Binomial distribution" ; + case NIFTI_INTENT_GAMMA: return "Gamma distribution" ; + case NIFTI_INTENT_POISSON: return "Poisson distribution" ; + case NIFTI_INTENT_NORMAL: return "Normal distribution" ; + case NIFTI_INTENT_FTEST_NONC: return "F-statistic noncentral" ; + case NIFTI_INTENT_CHISQ_NONC: return "Chi-squared noncentral" ; + case NIFTI_INTENT_LOGISTIC: return "Logistic distribution" ; + case NIFTI_INTENT_LAPLACE: return "Laplace distribution" ; + case NIFTI_INTENT_UNIFORM: return "Uniform distribition" ; + case NIFTI_INTENT_TTEST_NONC: return "T-statistic noncentral" ; + case NIFTI_INTENT_WEIBULL: return "Weibull distribution" ; + case NIFTI_INTENT_CHI: return "Chi distribution" ; + case NIFTI_INTENT_INVGAUSS: return "Inverse Gaussian distribution" ; + case NIFTI_INTENT_EXTVAL: return "Extreme Value distribution" ; + case NIFTI_INTENT_PVAL: return "P-value" ; + + case NIFTI_INTENT_LOGPVAL: return "Log P-value" ; + case NIFTI_INTENT_LOG10PVAL: return "Log10 P-value" ; + + case NIFTI_INTENT_ESTIMATE: return "Estimate" ; + case NIFTI_INTENT_LABEL: return "Label index" ; + case NIFTI_INTENT_NEURONAME: return "NeuroNames index" ; + case NIFTI_INTENT_GENMATRIX: return "General matrix" ; + case NIFTI_INTENT_SYMMATRIX: return "Symmetric matrix" ; + case NIFTI_INTENT_DISPVECT: return "Displacement vector" ; + case NIFTI_INTENT_VECTOR: return "Vector" ; + case NIFTI_INTENT_POINTSET: return "Pointset" ; + case NIFTI_INTENT_TRIANGLE: return "Triangle" ; + case NIFTI_INTENT_QUATERNION: return "Quaternion" ; + + case NIFTI_INTENT_DIMLESS: return "Dimensionless number" ; + default: break ; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI slice_code. + + \param ss NIfTI-1 slice order code + + \return pointer to static string describing code + + \warning Do not free() or modify this string! + It points to static storage. + + \sa NIFTI1_SLICE_ORDER group in nifti1.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_slice_string( int ss ) +{ + switch( ss ){ + case NIFTI_SLICE_SEQ_INC: return "sequential_increasing" ; + case NIFTI_SLICE_SEQ_DEC: return "sequential_decreasing" ; + case NIFTI_SLICE_ALT_INC: return "alternating_increasing" ; + case NIFTI_SLICE_ALT_DEC: return "alternating_decreasing" ; + case NIFTI_SLICE_ALT_INC2: return "alternating_increasing_2" ; + case NIFTI_SLICE_ALT_DEC2: return "alternating_decreasing_2" ; + default: break; + } + return "Unknown" ; +} + +/*---------------------------------------------------------------------------*/ +/*! Return a pointer to a string holding the name of a NIFTI orientation. + + \param ii orientation code + + \return pointer to static string holding the orientation information + + \warning Do not free() or modify the return string! + It points to static storage. + + \sa NIFTI_L2R in nifti1_io.h +*//*-------------------------------------------------------------------------*/ +char const *nifti_orientation_string( int ii ) +{ + switch( ii ){ + case NIFTI_L2R: return "Left-to-Right" ; + case NIFTI_R2L: return "Right-to-Left" ; + case NIFTI_P2A: return "Posterior-to-Anterior" ; + case NIFTI_A2P: return "Anterior-to-Posterior" ; + case NIFTI_I2S: return "Inferior-to-Superior" ; + case NIFTI_S2I: return "Superior-to-Inferior" ; + default: break; + } + return "Unknown" ; +} + +/*--------------------------------------------------------------------------*/ +/*! Given a datatype code, set number of bytes per voxel and the swapsize. + + \param datatype nifti1 datatype code + \param nbyper pointer to return value: number of bytes per voxel + \param swapsize pointer to return value: size of swap blocks + + \return appropriate values at nbyper and swapsize + + The swapsize is set to 0 if this datatype doesn't ever need swapping. + + \sa NIFTI1_DATATYPES in nifti1.h +*//*------------------------------------------------------------------------*/ +void nifti_datatype_sizes( int datatype , int *nbyper, int *swapsize ) +{ + int nb=0, ss=0 ; + switch( datatype ){ + case DT_INT8: + case DT_UINT8: nb = 1 ; ss = 0 ; break ; + + case DT_INT16: + case DT_UINT16: nb = 2 ; ss = 2 ; break ; + + case DT_RGB24: nb = 3 ; ss = 0 ; break ; + case DT_RGBA32: nb = 4 ; ss = 0 ; break ; + + case DT_INT32: + case DT_UINT32: + case DT_FLOAT32: nb = 4 ; ss = 4 ; break ; + + case DT_COMPLEX64: nb = 8 ; ss = 4 ; break ; + + case DT_FLOAT64: + case DT_INT64: + case DT_UINT64: nb = 8 ; ss = 8 ; break ; + + case DT_FLOAT128: nb = 16 ; ss = 16 ; break ; + + case DT_COMPLEX128: nb = 16 ; ss = 8 ; break ; + + case DT_COMPLEX256: nb = 32 ; ss = 16 ; break ; + default: break; + } + + ASSIF(nbyper,nb) ; ASSIF(swapsize,ss) ; } + + +/*-----------------------------------------------------------------*/ +/*! copy between float and double mat44 types 10 Jul, 2015 [rickr] */ + +int nifti_mat44_to_dmat44(mat44 * fm, nifti_dmat44 * dm) +{ + int i, j; + if( !dm || !fm ) return 1; + for( i=0; i<4; i++ ) + for( j=0; j<4; j++ ) + dm->m[i][j] = (double)fm->m[i][j]; + return 0; +} + +int nifti_dmat44_to_mat44(nifti_dmat44 * dm, mat44 * fm) +{ + int i, j; + if( !dm || !fm ) return 1; + for( i=0; i<4; i++ ) + for( j=0; j<4; j++ ) + fm->m[i][j] = (float)dm->m[i][j]; + return 0; +} + + +/*---------------------------------------------------------------------------*/ +/*! Given the quaternion parameters (etc.), compute a transformation matrix + of doubles. + + See comments in nifti1.h for details. + - qb,qc,qd = quaternion parameters + - qx,qy,qz = offset parameters + - dx,dy,dz = grid stepsizes (non-negative inputs are set to 1.0) + - qfac = sign of dz step (< 0 is negative; >= 0 is positive) + +
+   If qx=qy=qz=0, dx=dy=dz=1, then the output is a rotation matrix.
+   For qfac >= 0, the rotation is proper.
+   For qfac <  0, the rotation is improper.
+   
+ + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + \see nifti_mat44_to_quatern, nifti_make_orthog_mat44, + nifti_mat44_to_orientation + +*//*-------------------------------------------------------------------------*/ +nifti_dmat44 nifti_quatern_to_dmat44( double qb, double qc, double qd, + double qx, double qy, double qz, + double dx, double dy, double dz, double qfac ) +{ + nifti_dmat44 R ; + double a,b=qb,c=qc,d=qd , xd,yd,zd ; + + /* last row is always [ 0 0 0 1 ] */ + + R.m[3][0]=R.m[3][1]=R.m[3][2] = 0.0 ; R.m[3][3]= 1.0 ; + + /* compute a parameter from b,c,d */ + + a = 1.0l - (b*b + c*c + d*d) ; + if( a < 1.e-7l ){ /* special case */ + a = 1.0l / sqrt(b*b+c*c+d*d) ; + b *= a ; c *= a ; d *= a ; /* normalize (b,c,d) vector */ + a = 0.0l ; /* a = 0 ==> 180 degree rotation */ + } else{ + a = sqrt(a) ; /* angle = 2*arccos(a) */ + } + + /* load rotation matrix, including scaling factors for voxel sizes */ + + xd = (dx > 0.0) ? dx : 1.0l ; /* make sure are positive */ + yd = (dy > 0.0) ? dy : 1.0l ; + zd = (dz > 0.0) ? dz : 1.0l ; + + if( qfac < 0.0 ) zd = -zd ; /* left handedness? */ + + R.m[0][0] = (a*a+b*b-c*c-d*d) * xd; + R.m[0][1] = 2.0l * (b*c-a*d ) * yd ; + R.m[0][2] = 2.0l * (b*d+a*c ) * zd ; + R.m[1][0] = 2.0l * (b*c+a*d ) * xd ; + R.m[1][1] = (a*a+c*c-b*b-d*d) * yd; + R.m[1][2] = 2.0l * (c*d-a*b ) * zd ; + R.m[2][0] = 2.0l * (b*d-a*c ) * xd ; + R.m[2][1] = 2.0l * (c*d+a*b ) * yd ; + R.m[2][2] = (a*a+d*d-c*c-b*b) * zd; + + /* load offsets */ + + R.m[0][3] = qx ; R.m[1][3] = qy ; R.m[2][3] = qz ; + + return R ; +} + +/*---------------------------------------------------------------------------*/ +/*! Given the quaternion parameters (etc.), compute a transformation matrix. + + See comments in nifti1.h for details. + - qb,qc,qd = quaternion parameters + - qx,qy,qz = offset parameters + - dx,dy,dz = grid stepsizes (non-negative inputs are set to 1.0) + - qfac = sign of dz step (< 0 is negative; >= 0 is positive) + +
+   If qx=qy=qz=0, dx=dy=dz=1, then the output is a rotation matrix.
+   For qfac >= 0, the rotation is proper.
+   For qfac <  0, the rotation is improper.
+   
+ + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + \see nifti_mat44_to_quatern, nifti_make_orthog_mat44, + nifti_mat44_to_orientation + +*//*-------------------------------------------------------------------------*/ +mat44 nifti_quatern_to_mat44( float qb, float qc, float qd, + float qx, float qy, float qz, + float dx, float dy, float dz, float qfac ) +{ + mat44 R ; + double a,b=qb,c=qc,d=qd , xd,yd,zd ; + + /* last row is always [ 0 0 0 1 ] */ + + R.m[3][0]=R.m[3][1]=R.m[3][2] = 0.0f ; R.m[3][3]= 1.0f ; + + /* compute a parameter from b,c,d */ + + a = 1.0l - (b*b + c*c + d*d) ; + if( a < 1.e-7l ){ /* special case */ + a = 1.0l / sqrt(b*b+c*c+d*d) ; + b *= a ; c *= a ; d *= a ; /* normalize (b,c,d) vector */ + a = 0.0l ; /* a = 0 ==> 180 degree rotation */ + } else{ + a = sqrt(a) ; /* angle = 2*arccos(a) */ + } + + /* load rotation matrix, including scaling factors for voxel sizes */ + + xd = (dx > 0.0) ? dx : 1.0l ; /* make sure are positive */ + yd = (dy > 0.0) ? dy : 1.0l ; + zd = (dz > 0.0) ? dz : 1.0l ; + + if( qfac < 0.0 ) zd = -zd ; /* left handedness? */ + + R.m[0][0] = (float)( (a*a+b*b-c*c-d*d) * xd) ; + R.m[0][1] = 2.0l * (b*c-a*d ) * yd ; + R.m[0][2] = 2.0l * (b*d+a*c ) * zd ; + R.m[1][0] = 2.0l * (b*c+a*d ) * xd ; + R.m[1][1] = (float)( (a*a+c*c-b*b-d*d) * yd) ; + R.m[1][2] = 2.0l * (c*d-a*b ) * zd ; + R.m[2][0] = 2.0l * (b*d-a*c ) * xd ; + R.m[2][1] = 2.0l * (c*d+a*b ) * yd ; + R.m[2][2] = (float)( (a*a+d*d-c*c-b*b) * zd) ; + + /* load offsets */ + + R.m[0][3] = qx ; R.m[1][3] = qy ; R.m[2][3] = qz ; + + return R ; +} + +/*---------------------------------------------------------------------------*/ +/*! Given the 3x4 upper corner of the matrix R, compute the quaternion + parameters that fit it. + + - Any NULL pointer on input won't get assigned (e.g., if you don't want + dx,dy,dz, just pass NULL in for those pointers). + - If the 3 input matrix columns are NOT orthogonal, they will be + orthogonalized prior to calculating the parameters, using + the polar decomposition to find the orthogonal matrix closest + to the column-normalized input matrix. + - However, if the 3 input matrix columns are NOT orthogonal, then + the matrix produced by nifti_quatern_to_dmat44 WILL have orthogonal + columns, so it won't be the same as the matrix input here. + This "feature" is because the NIFTI 'qform' transform is + deliberately not fully general -- it is intended to model a volume + with perpendicular axes. + - If the 3 input matrix columns are not even linearly independent, + you'll just have to take your luck, won't you? + + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + + \see nifti_quatern_to_dmat44, nifti_make_orthog_dmat44, + nifti_dmat44_to_orientation +*//*-------------------------------------------------------------------------*/ +void nifti_dmat44_to_quatern(nifti_dmat44 R , + double *qb, double *qc, double *qd, + double *qx, double *qy, double *qz, + double *dx, double *dy, double *dz, double *qfac ) +{ + double r11,r12,r13 , r21,r22,r23 , r31,r32,r33 ; + double xd,yd,zd , a,b,c,d ; + nifti_dmat33 P,Q ; + + /* offset outputs are read write out of input matrix */ + + ASSIF(qx,R.m[0][3]) ; ASSIF(qy,R.m[1][3]) ; ASSIF(qz,R.m[2][3]) ; + + /* load 3x3 matrix into local variables */ + + r11 = R.m[0][0] ; r12 = R.m[0][1] ; r13 = R.m[0][2] ; + r21 = R.m[1][0] ; r22 = R.m[1][1] ; r23 = R.m[1][2] ; + r31 = R.m[2][0] ; r32 = R.m[2][1] ; r33 = R.m[2][2] ; + + /* compute lengths of each column; these determine grid spacings */ + + xd = sqrt( r11*r11 + r21*r21 + r31*r31 ) ; + yd = sqrt( r12*r12 + r22*r22 + r32*r32 ) ; + zd = sqrt( r13*r13 + r23*r23 + r33*r33 ) ; + + /* if a column length is zero, patch the trouble */ + + if( xd == 0.0l ){ r11 = 1.0l ; r21 = r31 = 0.0l ; xd = 1.0l ; } + if( yd == 0.0l ){ r22 = 1.0l ; r12 = r32 = 0.0l ; yd = 1.0l ; } + if( zd == 0.0l ){ r33 = 1.0l ; r13 = r23 = 0.0l ; zd = 1.0l ; } + + /* assign the output lengths */ + + ASSIF(dx,xd) ; ASSIF(dy,yd) ; ASSIF(dz,zd) ; + + /* normalize the columns */ + + r11 /= xd ; r21 /= xd ; r31 /= xd ; + r12 /= yd ; r22 /= yd ; r32 /= yd ; + r13 /= zd ; r23 /= zd ; r33 /= zd ; + + /* At this point, the matrix has normal columns, but we have to allow + for the fact that the hideous user may not have given us a matrix + with orthogonal columns. + + So, now find the orthogonal matrix closest to the current matrix. + + One reason for using the polar decomposition to get this + orthogonal matrix, rather than just directly orthogonalizing + the columns, is so that inputting the inverse matrix to R + will result in the inverse orthogonal matrix at this point. + If we just orthogonalized the columns, this wouldn't necessarily hold. */ + + Q.m[0][0] = r11 ; Q.m[0][1] = r12 ; Q.m[0][2] = r13 ; /* load Q */ + Q.m[1][0] = r21 ; Q.m[1][1] = r22 ; Q.m[1][2] = r23 ; + Q.m[2][0] = r31 ; Q.m[2][1] = r32 ; Q.m[2][2] = r33 ; + + P = nifti_dmat33_polar(Q) ; /* P is orthog matrix closest to Q */ + + r11 = P.m[0][0] ; r12 = P.m[0][1] ; r13 = P.m[0][2] ; /* unload */ + r21 = P.m[1][0] ; r22 = P.m[1][1] ; r23 = P.m[1][2] ; + r31 = P.m[2][0] ; r32 = P.m[2][1] ; r33 = P.m[2][2] ; + + /* [ r11 r12 r13 ] */ + /* at this point, the matrix [ r21 r22 r23 ] is orthogonal */ + /* [ r31 r32 r33 ] */ + + /* compute the determinant to determine if it is proper */ + + zd = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; /* should be -1 or 1 */ + + if( zd > 0 ){ /* proper */ + ASSIF(qfac,1.0) ; + } else { /* improper ==> flip 3rd column */ + ASSIF(qfac,-1.0) ; + r13 = -r13 ; r23 = -r23 ; r33 = -r33 ; + } + + /* now, compute quaternion parameters */ + + a = r11 + r22 + r33 + 1.0l ; + + if( a > 0.5l ){ /* simplest case */ + a = 0.5l * sqrt(a) ; + b = 0.25l * (r32-r23) / a ; + c = 0.25l * (r13-r31) / a ; + d = 0.25l * (r21-r12) / a ; + } else { /* trickier case */ + xd = 1.0 + r11 - (r22+r33) ; /* 4*b*b */ + yd = 1.0 + r22 - (r11+r33) ; /* 4*c*c */ + zd = 1.0 + r33 - (r11+r22) ; /* 4*d*d */ + if( xd > 1.0 ){ + b = 0.5l * sqrt(xd) ; + c = 0.25l* (r12+r21) / b ; + d = 0.25l* (r13+r31) / b ; + a = 0.25l* (r32-r23) / b ; + } else if( yd > 1.0 ){ + c = 0.5l * sqrt(yd) ; + b = 0.25l* (r12+r21) / c ; + d = 0.25l* (r23+r32) / c ; + a = 0.25l* (r13-r31) / c ; + } else { + d = 0.5l * sqrt(zd) ; + b = 0.25l* (r13+r31) / d ; + c = 0.25l* (r23+r32) / d ; + a = 0.25l* (r21-r12) / d ; + } + /* to be mathematically consistent, this would include a = -a */ + if( a < 0.0l ){ b=-b ; c=-c ; d=-d; } + } + + ASSIF(qb,b) ; ASSIF(qc,c) ; ASSIF(qd,d) ; +} + +/*---------------------------------------------------------------------------*/ +/*! Given the 3x4 upper corner of the matrix R, compute the quaternion + parameters that fit it. + + - Any NULL pointer on input won't get assigned (e.g., if you don't want + dx,dy,dz, just pass NULL in for those pointers). + - If the 3 input matrix columns are NOT orthogonal, they will be + orthogonalized prior to calculating the parameters, using + the polar decomposition to find the orthogonal matrix closest + to the column-normalized input matrix. + - However, if the 3 input matrix columns are NOT orthogonal, then + the matrix produced by nifti_quatern_to_mat44 WILL have orthogonal + columns, so it won't be the same as the matrix input here. + This "feature" is because the NIFTI 'qform' transform is + deliberately not fully general -- it is intended to model a volume + with perpendicular axes. + - If the 3 input matrix columns are not even linearly independent, + you'll just have to take your luck, won't you? + + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + + \see nifti_quatern_to_mat44, nifti_make_orthog_mat44, + nifti_mat44_to_orientation +*//*-------------------------------------------------------------------------*/ +void nifti_mat44_to_quatern( mat44 R , + float *qb, float *qc, float *qd, + float *qx, float *qy, float *qz, + float *dx, float *dy, float *dz, float *qfac ) +{ + double r11,r12,r13 , r21,r22,r23 , r31,r32,r33 ; + double xd,yd,zd , a,b,c,d ; + mat33 P,Q ; + + /* offset outputs are read write out of input matrix */ + + ASSIF(qx,R.m[0][3]) ; ASSIF(qy,R.m[1][3]) ; ASSIF(qz,R.m[2][3]) ; + + /* load 3x3 matrix into local variables */ + + r11 = R.m[0][0] ; r12 = R.m[0][1] ; r13 = R.m[0][2] ; + r21 = R.m[1][0] ; r22 = R.m[1][1] ; r23 = R.m[1][2] ; + r31 = R.m[2][0] ; r32 = R.m[2][1] ; r33 = R.m[2][2] ; + + /* compute lengths of each column; these determine grid spacings */ + + xd = sqrt( r11*r11 + r21*r21 + r31*r31 ) ; + yd = sqrt( r12*r12 + r22*r22 + r32*r32 ) ; + zd = sqrt( r13*r13 + r23*r23 + r33*r33 ) ; + + /* if a column length is zero, patch the trouble */ + + if( xd == 0.0l ){ r11 = 1.0l ; r21 = r31 = 0.0l ; xd = 1.0l ; } + if( yd == 0.0l ){ r22 = 1.0l ; r12 = r32 = 0.0l ; yd = 1.0l ; } + if( zd == 0.0l ){ r33 = 1.0l ; r13 = r23 = 0.0l ; zd = 1.0l ; } + + /* assign the output lengths */ + + ASSIF(dx,(float)xd) ; ASSIF(dy,(float)yd) ; ASSIF(dz,(float)zd) ; + + /* normalize the columns */ + + r11 /= xd ; r21 /= xd ; r31 /= xd ; + r12 /= yd ; r22 /= yd ; r32 /= yd ; + r13 /= zd ; r23 /= zd ; r33 /= zd ; + + /* At this point, the matrix has normal columns, but we have to allow + for the fact that the hideous user may not have given us a matrix + with orthogonal columns. + + So, now find the orthogonal matrix closest to the current matrix. + + One reason for using the polar decomposition to get this + orthogonal matrix, rather than just directly orthogonalizing + the columns, is so that inputting the inverse matrix to R + will result in the inverse orthogonal matrix at this point. + If we just orthogonalized the columns, this wouldn't necessarily hold. */ + + Q.m[0][0] = (float)r11 ; Q.m[0][1] = (float)r12 ; Q.m[0][2] = (float)r13 ; /* load Q */ + Q.m[1][0] = (float)r21 ; Q.m[1][1] = (float)r22 ; Q.m[1][2] = (float)r23 ; + Q.m[2][0] = (float)r31 ; Q.m[2][1] = (float)r32 ; Q.m[2][2] = (float)r33 ; + + P = nifti_mat33_polar(Q) ; /* P is orthog matrix closest to Q */ + + r11 = P.m[0][0] ; r12 = P.m[0][1] ; r13 = P.m[0][2] ; /* unload */ + r21 = P.m[1][0] ; r22 = P.m[1][1] ; r23 = P.m[1][2] ; + r31 = P.m[2][0] ; r32 = P.m[2][1] ; r33 = P.m[2][2] ; + + /* [ r11 r12 r13 ] */ + /* at this point, the matrix [ r21 r22 r23 ] is orthogonal */ + /* [ r31 r32 r33 ] */ + + /* compute the determinant to determine if it is proper */ + + zd = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; /* should be -1 or 1 */ + + if( zd > 0 ){ /* proper */ + ASSIF(qfac,1.0f) ; + } else { /* improper ==> flip 3rd column */ + ASSIF(qfac,-1.0f) ; + r13 = -r13 ; r23 = -r23 ; r33 = -r33 ; + } + + /* now, compute quaternion parameters */ + + a = r11 + r22 + r33 + 1.0l ; + + if( a > 0.5l ){ /* simplest case */ + a = 0.5l * sqrt(a) ; + b = 0.25l * (r32-r23) / a ; + c = 0.25l * (r13-r31) / a ; + d = 0.25l * (r21-r12) / a ; + } else { /* trickier case */ + xd = 1.0 + r11 - (r22+r33) ; /* 4*b*b */ + yd = 1.0 + r22 - (r11+r33) ; /* 4*c*c */ + zd = 1.0 + r33 - (r11+r22) ; /* 4*d*d */ + if( xd > 1.0 ){ + b = 0.5l * sqrt(xd) ; + c = 0.25l* (r12+r21) / b ; + d = 0.25l* (r13+r31) / b ; + a = 0.25l* (r32-r23) / b ; + } else if( yd > 1.0 ){ + c = 0.5l * sqrt(yd) ; + b = 0.25l* (r12+r21) / c ; + d = 0.25l* (r23+r32) / c ; + a = 0.25l* (r13-r31) / c ; + } else { + d = 0.5l * sqrt(zd) ; + b = 0.25l* (r13+r31) / d ; + c = 0.25l* (r23+r32) / d ; + a = 0.25l* (r21-r12) / d ; + } + /* to be mathematically consistent, this would include a = -a */ + if( a < 0.0l ){ b=-b ; c=-c ; d=-d; } + } + + ASSIF(qb,(float)b) ; ASSIF(qc,(float)c) ; ASSIF(qd,(float)d) ; +} + +/*---------------------------------------------------------------------------*/ +/*! Compute the inverse of a bordered 4x4 matrix. + +
+   - Some numerical code fragments were generated by Maple 8.
+   - If a singular matrix is input, the output matrix will be all zero.
+   - You can check for this by examining the [3][3] element, which will
+     be 1.0 for the normal case and 0.0 for the bad case.
+
+     The input matrix should have the form:
+        [ r11 r12 r13 v1 ]
+        [ r21 r22 r23 v2 ]
+        [ r31 r32 r33 v3 ]
+        [  0   0   0   1 ]
+     
+*//*-------------------------------------------------------------------------*/ +nifti_dmat44 nifti_dmat44_inverse( nifti_dmat44 R ) +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33,v1,v2,v3 , deti ; + nifti_dmat44 Q ; + /* INPUT MATRIX IS: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 v1 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 v2 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 v3 ] */ + v1 = R.m[0][3]; v2 = R.m[1][3]; v3 = R.m[2][3]; /* [ 0 0 0 1 ] */ + + deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + + if( deti != 0.0l ) deti = 1.0l / deti ; + + Q.m[0][0] = deti*( r22*r33-r32*r23); + Q.m[0][1] = deti*(-r12*r33+r32*r13); + Q.m[0][2] = deti*( r12*r23-r22*r13); + Q.m[0][3] = deti*(-r12*r23*v3+r12*v2*r33+r22*r13*v3 + -r22*v1*r33-r32*r13*v2+r32*v1*r23); + + Q.m[1][0] = deti*(-r21*r33+r31*r23); + Q.m[1][1] = deti*( r11*r33-r31*r13); + Q.m[1][2] = deti*(-r11*r23+r21*r13); + Q.m[1][3] = deti*( r11*r23*v3-r11*v2*r33-r21*r13*v3 + +r21*v1*r33+r31*r13*v2-r31*v1*r23); + + Q.m[2][0] = deti*( r21*r32-r31*r22); + Q.m[2][1] = deti*(-r11*r32+r31*r12); + Q.m[2][2] = deti*( r11*r22-r21*r12); + Q.m[2][3] = deti*(-r11*r22*v3+r11*r32*v2+r21*r12*v3 + -r21*r32*v1-r31*r12*v2+r31*r22*v1); + + Q.m[3][0] = Q.m[3][1] = Q.m[3][2] = 0.0l ; + Q.m[3][3] = (deti == 0.0l) ? 0.0l : 1.0l ; /* failure flag if deti == 0 */ + + return Q ; +} + +/*---------------------------------------------------------------------------*/ +/*! Compute the inverse of a bordered 4x4 matrix. + +
+   - Some numerical code fragments were generated by Maple 8.
+   - If a singular matrix is input, the output matrix will be all zero.
+   - You can check for this by examining the [3][3] element, which will
+     be 1.0 for the normal case and 0.0 for the bad case.
+
+     The input matrix should have the form:
+        [ r11 r12 r13 v1 ]
+        [ r21 r22 r23 v2 ]
+        [ r31 r32 r33 v3 ]
+        [  0   0   0   1 ]
+     
+*//*-------------------------------------------------------------------------*/ +mat44 nifti_mat44_inverse( mat44 R ) +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33,v1,v2,v3 , deti ; + mat44 Q ; + /* INPUT MATRIX IS: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 v1 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 v2 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 v3 ] */ + v1 = R.m[0][3]; v2 = R.m[1][3]; v3 = R.m[2][3]; /* [ 0 0 0 1 ] */ + + deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + + if( deti != 0.0l ) deti = 1.0l / deti ; + + Q.m[0][0] = (float)( deti*( r22*r33-r32*r23) ) ; + Q.m[0][1] = (float)( deti*(-r12*r33+r32*r13) ) ; + Q.m[0][2] = (float)( deti*( r12*r23-r22*r13) ) ; + Q.m[0][3] = (float)( deti*(-r12*r23*v3+r12*v2*r33+r22*r13*v3 + -r22*v1*r33-r32*r13*v2+r32*v1*r23) ) ; + + Q.m[1][0] = (float)( deti*(-r21*r33+r31*r23) ) ; + Q.m[1][1] = (float)( deti*( r11*r33-r31*r13) ) ; + Q.m[1][2] = (float)( deti*(-r11*r23+r21*r13) ) ; + Q.m[1][3] = (float)( deti*( r11*r23*v3-r11*v2*r33-r21*r13*v3 + +r21*v1*r33+r31*r13*v2-r31*v1*r23) ) ; + + Q.m[2][0] = (float)( deti*( r21*r32-r31*r22) ) ; + Q.m[2][1] = (float)( deti*(-r11*r32+r31*r12) ) ; + Q.m[2][2] = (float)( deti*( r11*r22-r21*r12) ) ; + Q.m[2][3] = (float)( deti*(-r11*r22*v3+r11*r32*v2+r21*r12*v3 + -r21*r32*v1-r31*r12*v2+r31*r22*v1) ) ; + + Q.m[3][0] = Q.m[3][1] = Q.m[3][2] = 0.0l ; + Q.m[3][3] = (deti == 0.0l) ? 0.0l : 1.0l ; /* failure flag if deti == 0 */ + + return Q ; +} + +/*---------------------------------------------------------------------------*/ +/*! Input 9 floats and make an orthgonal nifti_dmat44 out of them. + + Each row is normalized, then nifti_mat33_polar() is used to orthogonalize + them. If row #3 (r31,r32,r33) is input as zero, then it will be taken to + be the cross product of rows #1 and #2. + + This function can be used to create a rotation matrix for transforming + an oblique volume to anatomical coordinates. For this application: + - row #1 (r11,r12,r13) is the direction vector along the image i-axis + - row #2 (r21,r22,r23) is the direction vector along the image j-axis + - row #3 (r31,r32,r33) is the direction vector along the slice direction + (if available; otherwise enter it as 0's) + + The first 2 rows can be taken from the DICOM attribute (0020,0037) + "Image Orientation (Patient)". + + After forming the rotation matrix, the complete affine transformation from + (i,j,k) grid indexes to (x,y,z) spatial coordinates can be computed by + multiplying each column by the appropriate grid spacing: + - column #1 (R.m[0][0],R.m[1][0],R.m[2][0]) by delta-x + - column #2 (R.m[0][1],R.m[1][1],R.m[2][1]) by delta-y + - column #3 (R.m[0][2],R.m[1][2],R.m[2][2]) by delta-z + + and by then placing the center (x,y,z) coordinates of voxel (0,0,0) into + the column #4 (R.m[0][3],R.m[1][3],R.m[2][3]). + + \sa nifti_quatern_to_dmat44, nifti_dmat44_to_quatern, + nifti_dmat44_to_orientation +*//*-------------------------------------------------------------------------*/ +nifti_dmat44 nifti_make_orthog_dmat44( double r11, double r12, double r13 , + double r21, double r22, double r23 , + double r31, double r32, double r33 ) +{ + nifti_dmat44 R ; + nifti_dmat33 Q , P ; + double val ; + + R.m[3][0] = R.m[3][1] = R.m[3][2] = 0.0l ; R.m[3][3] = 1.0l ; + + Q.m[0][0] = r11 ; Q.m[0][1] = r12 ; Q.m[0][2] = r13 ; /* load Q */ + Q.m[1][0] = r21 ; Q.m[1][1] = r22 ; Q.m[1][2] = r23 ; + Q.m[2][0] = r31 ; Q.m[2][1] = r32 ; Q.m[2][2] = r33 ; + + /* normalize row 1 */ + + val = Q.m[0][0]*Q.m[0][0] + Q.m[0][1]*Q.m[0][1] + Q.m[0][2]*Q.m[0][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[0][0] *= val ; Q.m[0][1] *= val ; Q.m[0][2] *= val ; + } else { + Q.m[0][0] = 1.0l ; Q.m[0][1] = 0.0l ; Q.m[0][2] = 0.0l ; + } + + /* normalize row 2 */ + + val = Q.m[1][0]*Q.m[1][0] + Q.m[1][1]*Q.m[1][1] + Q.m[1][2]*Q.m[1][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[1][0] *= val ; Q.m[1][1] *= val ; Q.m[1][2] *= val ; + } else { + Q.m[1][0] = 0.0l ; Q.m[1][1] = 1.0l ; Q.m[1][2] = 0.0l ; + } + + /* normalize row 3 */ + + val = Q.m[2][0]*Q.m[2][0] + Q.m[2][1]*Q.m[2][1] + Q.m[2][2]*Q.m[2][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[2][0] *= val ; Q.m[2][1] *= val ; Q.m[2][2] *= val ; + } else { + Q.m[2][0] = Q.m[0][1]*Q.m[1][2] - Q.m[0][2]*Q.m[1][1] ; /* cross */ + Q.m[2][1] = Q.m[0][2]*Q.m[1][0] - Q.m[0][0]*Q.m[1][2] ; /* product */ + Q.m[2][2] = Q.m[0][0]*Q.m[1][1] - Q.m[0][1]*Q.m[1][0] ; + } + + P = nifti_dmat33_polar(Q) ; /* P is orthog matrix closest to Q */ + + R.m[0][0] = P.m[0][0] ; R.m[0][1] = P.m[0][1] ; R.m[0][2] = P.m[0][2] ; + R.m[1][0] = P.m[1][0] ; R.m[1][1] = P.m[1][1] ; R.m[1][2] = P.m[1][2] ; + R.m[2][0] = P.m[2][0] ; R.m[2][1] = P.m[2][1] ; R.m[2][2] = P.m[2][2] ; + + R.m[0][3] = R.m[1][3] = R.m[2][3] = 0.0f ; return R ; +} + +/*---------------------------------------------------------------------------*/ +/*! Input 9 floats and make an orthgonal mat44 out of them. + + Each row is normalized, then nifti_mat33_polar() is used to orthogonalize + them. If row #3 (r31,r32,r33) is input as zero, then it will be taken to + be the cross product of rows #1 and #2. + + This function can be used to create a rotation matrix for transforming + an oblique volume to anatomical coordinates. For this application: + - row #1 (r11,r12,r13) is the direction vector along the image i-axis + - row #2 (r21,r22,r23) is the direction vector along the image j-axis + - row #3 (r31,r32,r33) is the direction vector along the slice direction + (if available; otherwise enter it as 0's) + + The first 2 rows can be taken from the DICOM attribute (0020,0037) + "Image Orientation (Patient)". + + After forming the rotation matrix, the complete affine transformation from + (i,j,k) grid indexes to (x,y,z) spatial coordinates can be computed by + multiplying each column by the appropriate grid spacing: + - column #1 (R.m[0][0],R.m[1][0],R.m[2][0]) by delta-x + - column #2 (R.m[0][1],R.m[1][1],R.m[2][1]) by delta-y + - column #3 (R.m[0][2],R.m[1][2],R.m[2][2]) by delta-z + + and by then placing the center (x,y,z) coordinates of voxel (0,0,0) into + the column #4 (R.m[0][3],R.m[1][3],R.m[2][3]). + + \sa nifti_quatern_to_mat44, nifti_mat44_to_quatern, + nifti_mat44_to_orientation +*//*-------------------------------------------------------------------------*/ +mat44 nifti_make_orthog_mat44( float r11, float r12, float r13 , + float r21, float r22, float r23 , + float r31, float r32, float r33 ) +{ + mat44 R ; + mat33 Q , P ; + double val ; + + R.m[3][0] = R.m[3][1] = R.m[3][2] = 0.0l ; R.m[3][3] = 1.0l ; + + Q.m[0][0] = r11 ; Q.m[0][1] = r12 ; Q.m[0][2] = r13 ; /* load Q */ + Q.m[1][0] = r21 ; Q.m[1][1] = r22 ; Q.m[1][2] = r23 ; + Q.m[2][0] = r31 ; Q.m[2][1] = r32 ; Q.m[2][2] = r33 ; + + /* normalize row 1 */ + + val = Q.m[0][0]*Q.m[0][0] + Q.m[0][1]*Q.m[0][1] + Q.m[0][2]*Q.m[0][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[0][0] *= (float)val ; Q.m[0][1] *= (float)val ; Q.m[0][2] *= (float)val ; + } else { + Q.m[0][0] = 1.0l ; Q.m[0][1] = 0.0l ; Q.m[0][2] = 0.0l ; + } + + /* normalize row 2 */ + + val = Q.m[1][0]*Q.m[1][0] + Q.m[1][1]*Q.m[1][1] + Q.m[1][2]*Q.m[1][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[1][0] *= (float)val ; Q.m[1][1] *= (float)val ; Q.m[1][2] *= (float)val ; + } else { + Q.m[1][0] = 0.0l ; Q.m[1][1] = 1.0l ; Q.m[1][2] = 0.0l ; + } + + /* normalize row 3 */ + + val = Q.m[2][0]*Q.m[2][0] + Q.m[2][1]*Q.m[2][1] + Q.m[2][2]*Q.m[2][2] ; + if( val > 0.0l ){ + val = 1.0l / sqrt(val) ; + Q.m[2][0] *= (float)val ; Q.m[2][1] *= (float)val ; Q.m[2][2] *= (float)val ; + } else { + Q.m[2][0] = Q.m[0][1]*Q.m[1][2] - Q.m[0][2]*Q.m[1][1] ; /* cross */ + Q.m[2][1] = Q.m[0][2]*Q.m[1][0] - Q.m[0][0]*Q.m[1][2] ; /* product */ + Q.m[2][2] = Q.m[0][0]*Q.m[1][1] - Q.m[0][1]*Q.m[1][0] ; + } + + P = nifti_mat33_polar(Q) ; /* P is orthog matrix closest to Q */ + + R.m[0][0] = P.m[0][0] ; R.m[0][1] = P.m[0][1] ; R.m[0][2] = P.m[0][2] ; + R.m[1][0] = P.m[1][0] ; R.m[1][1] = P.m[1][1] ; R.m[1][2] = P.m[1][2] ; + R.m[2][0] = P.m[2][0] ; R.m[2][1] = P.m[2][1] ; R.m[2][2] = P.m[2][2] ; + + R.m[0][3] = R.m[1][3] = R.m[2][3] = 0.0f ; return R ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the inverse of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +nifti_dmat33 nifti_dmat33_inverse( nifti_dmat33 R ) /* inverse of 3x3 matrix */ +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33 , deti ; + nifti_dmat33 Q ; + /* INPUT MATRIX: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */ + + deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + + if( deti != 0.0l ) deti = 1.0l / deti ; + + Q.m[0][0] = deti*( r22*r33-r32*r23); + Q.m[0][1] = deti*(-r12*r33+r32*r13); + Q.m[0][2] = deti*( r12*r23-r22*r13); + + Q.m[1][0] = deti*(-r21*r33+r31*r23); + Q.m[1][1] = deti*( r11*r33-r31*r13); + Q.m[1][2] = deti*(-r11*r23+r21*r13); + + Q.m[2][0] = deti*( r21*r32-r31*r22); + Q.m[2][1] = deti*(-r11*r32+r31*r12); + Q.m[2][2] = deti*( r11*r22-r21*r12); + + return Q ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the inverse of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +mat33 nifti_mat33_inverse( mat33 R ) /* inverse of 3x3 matrix */ +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33 , deti ; + mat33 Q ; + /* INPUT MATRIX: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */ + + deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + + if( deti != 0.0l ) deti = 1.0l / deti ; + + Q.m[0][0] = (float)( deti*( r22*r33-r32*r23) ) ; + Q.m[0][1] = (float)( deti*(-r12*r33+r32*r13) ) ; + Q.m[0][2] = (float)( deti*( r12*r23-r22*r13) ) ; + + Q.m[1][0] = (float)( deti*(-r21*r33+r31*r23) ) ; + Q.m[1][1] = (float)( deti*( r11*r33-r31*r13) ) ; + Q.m[1][2] = (float)( deti*(-r11*r23+r21*r13) ) ; + + Q.m[2][0] = (float)( deti*( r21*r32-r31*r22) ) ; + Q.m[2][1] = (float)( deti*(-r11*r32+r31*r12) ) ; + Q.m[2][2] = (float)( deti*( r11*r22-r21*r12) ) ; + + return Q ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the determinant of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +double nifti_dmat33_determ( nifti_dmat33 R ) /* determinant of 3x3 matrix */ +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33 ; + /* INPUT MATRIX: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */ + + return (r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13) ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the determinant of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +float nifti_mat33_determ( mat33 R ) /* determinant of 3x3 matrix */ +{ + double r11,r12,r13,r21,r22,r23,r31,r32,r33 ; + /* INPUT MATRIX: */ + r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */ + r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */ + r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */ + + return (float)(r11*r22*r33-r11*r32*r23-r21*r12*r33 + +r21*r32*r13+r31*r12*r23-r31*r22*r13) ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the max row norm of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +double nifti_dmat33_rownorm( nifti_dmat33 A ) /* max row norm of 3x3 matrix */ +{ + double r1,r2,r3 ; + + r1 = fabs(A.m[0][0])+fabs(A.m[0][1])+fabs(A.m[0][2]); + r2 = fabs(A.m[1][0])+fabs(A.m[1][1])+fabs(A.m[1][2]); + r3 = fabs(A.m[2][0])+fabs(A.m[2][1])+fabs(A.m[2][2]); + if( r1 < r2 ) r1 = r2 ; + if( r1 < r3 ) r1 = r3 ; + return r1 ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the max row norm of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +float nifti_mat33_rownorm( mat33 A ) /* max row norm of 3x3 matrix */ +{ + float r1,r2,r3 ; + + r1 = (float)( fabs(A.m[0][0])+fabs(A.m[0][1])+fabs(A.m[0][2]) ) ; + r2 = (float)( fabs(A.m[1][0])+fabs(A.m[1][1])+fabs(A.m[1][2]) ) ; + r3 = (float)( fabs(A.m[2][0])+fabs(A.m[2][1])+fabs(A.m[2][2]) ) ; + if( r1 < r2 ) r1 = r2 ; + if( r1 < r3 ) r1 = r3 ; + return r1 ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the max column norm of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +double nifti_dmat33_colnorm( nifti_dmat33 A )/* max column norm of 3x3 matrix */ +{ + double r1,r2,r3 ; + + r1 = fabs(A.m[0][0])+fabs(A.m[1][0])+fabs(A.m[2][0]); + r2 = fabs(A.m[0][1])+fabs(A.m[1][1])+fabs(A.m[2][1]); + r3 = fabs(A.m[0][2])+fabs(A.m[1][2])+fabs(A.m[2][2]); + if( r1 < r2 ) r1 = r2 ; + if( r1 < r3 ) r1 = r3 ; + return r1 ; +} + +/*----------------------------------------------------------------------*/ +/*! compute the max column norm of a 3x3 matrix +*//*--------------------------------------------------------------------*/ +float nifti_mat33_colnorm( mat33 A ) /* max column norm of 3x3 matrix */ +{ + float r1,r2,r3 ; + + r1 = (float)( fabs(A.m[0][0])+fabs(A.m[1][0])+fabs(A.m[2][0]) ) ; + r2 = (float)( fabs(A.m[0][1])+fabs(A.m[1][1])+fabs(A.m[2][1]) ) ; + r3 = (float)( fabs(A.m[0][2])+fabs(A.m[1][2])+fabs(A.m[2][2]) ) ; + if( r1 < r2 ) r1 = r2 ; + if( r1 < r3 ) r1 = r3 ; + return r1 ; +} + +/*----------------------------------------------------------------------*/ +/*! multiply 2 3x3 matrices +*//*--------------------------------------------------------------------*/ +nifti_dmat33 nifti_dmat33_mul( nifti_dmat33 A , nifti_dmat33 B ) +/* multiply 2 3x3 matrices */ +{ + nifti_dmat33 C ; int i,j ; + for( i=0 ; i < 3 ; i++ ) + for( j=0 ; j < 3 ; j++ ) + C.m[i][j] = A.m[i][0] * B.m[0][j] + + A.m[i][1] * B.m[1][j] + + A.m[i][2] * B.m[2][j] ; + return C ; +} + +/*----------------------------------------------------------------------*/ +/*! multiply 2 3x3 matrices +*//*--------------------------------------------------------------------*/ +mat33 nifti_mat33_mul( mat33 A , mat33 B ) /* multiply 2 3x3 matrices */ +{ + mat33 C ; int i,j ; + for( i=0 ; i < 3 ; i++ ) + for( j=0 ; j < 3 ; j++ ) + C.m[i][j] = A.m[i][0] * B.m[0][j] + + A.m[i][1] * B.m[1][j] + + A.m[i][2] * B.m[2][j] ; + return C ; +} + +/*----------------------------------------------------------------------*/ +/*! multiply 2 4x4 matrices +*//*--------------------------------------------------------------------*/ +nifti_dmat44 nifti_dmat44_mul( nifti_dmat44 A , nifti_dmat44 B ) +{ + nifti_dmat44 C ; int i,j,k ; + for( i=0 ; i < 4 ; i++ ) + for( j=0 ; j < 4 ; j++ ) { + C.m[i][j] = 0.0; + for( k=0; k < 4; k++ ) + C.m[i][j] += A.m[i][k] * B.m[k][j]; + } + return C ; +} + +/*----------------------------------------------------------------------*/ +/*! multiply 2 4x4 matrices +*//*--------------------------------------------------------------------*/ +mat44 nifti_mat44_mul( mat44 A , mat44 B ) +{ + mat44 C ; int i,j,k ; + for( i=0 ; i < 4 ; i++ ) + for( j=0 ; j < 4 ; j++ ) { + C.m[i][j] = 0.0; + for( k=0; k < 4; k++ ) + C.m[i][j] += A.m[i][k] * B.m[k][j]; + } + return C ; +} + +/*---------------------------------------------------------------------------*/ +/*! polar decomposition of a 3x3 matrix + + This finds the closest orthogonal matrix to input A + (in both the Frobenius and L2 norms). + + Algorithm is that from NJ Higham, SIAM J Sci Stat Comput, 7:1160-1174. +*//*-------------------------------------------------------------------------*/ +nifti_dmat33 nifti_dmat33_polar( nifti_dmat33 A ) +{ + nifti_dmat33 X , Y , Z ; + double alp,bet,gam,gmi , dif=1.0 ; + int k=0 ; + + X = A ; + + /* force matrix to be nonsingular */ + + gam = nifti_dmat33_determ(X) ; + while( gam == 0.0 ){ /* perturb matrix */ + gam = 0.00001 * ( 0.001 + nifti_dmat33_rownorm(X) ); + X.m[0][0] += gam ; X.m[1][1] += gam ; X.m[2][2] += gam ; + gam = nifti_dmat33_determ(X) ; + } + + while(1){ + Y = nifti_dmat33_inverse(X) ; + if( dif > 0.3 ){ /* far from convergence */ + alp = sqrt( nifti_dmat33_rownorm(X) * nifti_dmat33_colnorm(X) ); + bet = sqrt( nifti_dmat33_rownorm(Y) * nifti_dmat33_colnorm(Y) ); + gam = sqrt( bet / alp ); + gmi = 1.0 / gam; + } else { + gam = gmi = 1.0f ; /* close to convergence */ + } + Z.m[0][0] = 0.5 * ( gam*X.m[0][0] + gmi*Y.m[0][0] ); + Z.m[0][1] = 0.5 * ( gam*X.m[0][1] + gmi*Y.m[1][0] ); + Z.m[0][2] = 0.5 * ( gam*X.m[0][2] + gmi*Y.m[2][0] ); + Z.m[1][0] = 0.5 * ( gam*X.m[1][0] + gmi*Y.m[0][1] ); + Z.m[1][1] = 0.5 * ( gam*X.m[1][1] + gmi*Y.m[1][1] ); + Z.m[1][2] = 0.5 * ( gam*X.m[1][2] + gmi*Y.m[2][1] ); + Z.m[2][0] = 0.5 * ( gam*X.m[2][0] + gmi*Y.m[0][2] ); + Z.m[2][1] = 0.5 * ( gam*X.m[2][1] + gmi*Y.m[1][2] ); + Z.m[2][2] = 0.5 * ( gam*X.m[2][2] + gmi*Y.m[2][2] ); + + dif = fabs(Z.m[0][0]-X.m[0][0])+fabs(Z.m[0][1]-X.m[0][1]) + +fabs(Z.m[0][2]-X.m[0][2])+fabs(Z.m[1][0]-X.m[1][0]) + +fabs(Z.m[1][1]-X.m[1][1])+fabs(Z.m[1][2]-X.m[1][2]) + +fabs(Z.m[2][0]-X.m[2][0])+fabs(Z.m[2][1]-X.m[2][1]) + +fabs(Z.m[2][2]-X.m[2][2]); + + k = k+1 ; + if( k > 100 || dif < 3.e-6 ) break ; /* convergence or exhaustion */ + X = Z ; + } + + return Z ; +} + +/*---------------------------------------------------------------------------*/ +/*! polar decomposition of a 3x3 matrix + + This finds the closest orthogonal matrix to input A + (in both the Frobenius and L2 norms). + + Algorithm is that from NJ Higham, SIAM J Sci Stat Comput, 7:1160-1174. +*//*-------------------------------------------------------------------------*/ +mat33 nifti_mat33_polar( mat33 A ) +{ + mat33 X , Y , Z ; + float alp,bet,gam,gmi , dif=1.0f ; + int k=0 ; + + X = A ; + + /* force matrix to be nonsingular */ + + gam = nifti_mat33_determ(X) ; + while( gam == 0.0 ){ /* perturb matrix */ + gam = (float)( 0.00001 * ( 0.001 + nifti_mat33_rownorm(X) ) ) ; + X.m[0][0] += gam ; X.m[1][1] += gam ; X.m[2][2] += gam ; + gam = nifti_mat33_determ(X) ; + } + + while(1){ + Y = nifti_mat33_inverse(X) ; + if( dif > 0.3 ){ /* far from convergence */ + alp = (float)( sqrt( nifti_mat33_rownorm(X) * nifti_mat33_colnorm(X) ) ) ; + bet = (float)( sqrt( nifti_mat33_rownorm(Y) * nifti_mat33_colnorm(Y) ) ) ; + gam = (float)( sqrt( bet / alp ) ) ; + gmi = (float)( 1.0 / gam ) ; + } else { + gam = gmi = 1.0f ; /* close to convergence */ + } + Z.m[0][0] = (float)( 0.5 * ( gam*X.m[0][0] + gmi*Y.m[0][0] ) ) ; + Z.m[0][1] = (float)( 0.5 * ( gam*X.m[0][1] + gmi*Y.m[1][0] ) ) ; + Z.m[0][2] = (float)( 0.5 * ( gam*X.m[0][2] + gmi*Y.m[2][0] ) ) ; + Z.m[1][0] = (float)( 0.5 * ( gam*X.m[1][0] + gmi*Y.m[0][1] ) ) ; + Z.m[1][1] = (float)( 0.5 * ( gam*X.m[1][1] + gmi*Y.m[1][1] ) ) ; + Z.m[1][2] = (float)( 0.5 * ( gam*X.m[1][2] + gmi*Y.m[2][1] ) ) ; + Z.m[2][0] = (float)( 0.5 * ( gam*X.m[2][0] + gmi*Y.m[0][2] ) ) ; + Z.m[2][1] = (float)( 0.5 * ( gam*X.m[2][1] + gmi*Y.m[1][2] ) ) ; + Z.m[2][2] = (float)( 0.5 * ( gam*X.m[2][2] + gmi*Y.m[2][2] ) ) ; + + dif = (float)( fabs(Z.m[0][0]-X.m[0][0])+fabs(Z.m[0][1]-X.m[0][1]) + +fabs(Z.m[0][2]-X.m[0][2])+fabs(Z.m[1][0]-X.m[1][0]) + +fabs(Z.m[1][1]-X.m[1][1])+fabs(Z.m[1][2]-X.m[1][2]) + +fabs(Z.m[2][0]-X.m[2][0])+fabs(Z.m[2][1]-X.m[2][1]) + +fabs(Z.m[2][2]-X.m[2][2]) ); + + k = k+1 ; + if( k > 100 || dif < 3.e-6 ) break ; /* convergence or exhaustion */ + X = Z ; + } + + return Z ; +} + +/*---------------------------------------------------------------------------*/ +/*! compute the (closest) orientation from a 4x4 ijk->xyz tranformation matrix + +
+   Input:  4x4 matrix that transforms (i,j,k) indexes to (x,y,z) coordinates,
+           where +x=Right, +y=Anterior, +z=Superior.
+           (Only the upper-left 3x3 corner of R is used herein.)
+   Output: 3 orientation codes that correspond to the closest "standard"
+           anatomical orientation of the (i,j,k) axes.
+   Method: Find which permutation of (x,y,z) has the smallest angle to the
+           (i,j,k) axes directions, which are the columns of the R matrix.
+   Errors: The codes returned will be zero.
+
+   For example, an axial volume might get return values of
+     *icod = NIFTI_R2L   (i axis is mostly Right to Left)
+     *jcod = NIFTI_P2A   (j axis is mostly Posterior to Anterior)
+     *kcod = NIFTI_I2S   (k axis is mostly Inferior to Superior)
+   
+ + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + + \see nifti_quatern_to_mat44, nifti_mat44_to_quatern, + nifti_make_orthog_mat44 +*//*-------------------------------------------------------------------------*/ +void nifti_dmat44_to_orientation( nifti_dmat44 R , + int *icod, int *jcod, int *kcod ) +{ + double xi,xj,xk , yi,yj,yk , zi,zj,zk , val,detQ,detP ; + nifti_dmat33 P , Q , M ; + int i,j,k=0,p,q,r , ibest,jbest,kbest,pbest,qbest,rbest ; + double vbest ; + + if( icod == NULL || jcod == NULL || kcod == NULL ) return ; /* bad */ + + *icod = *jcod = *kcod = 0 ; /* error returns, if sh*t happens */ + + /* load column vectors for each (i,j,k) direction from matrix */ + + /*-- i axis --*/ /*-- j axis --*/ /*-- k axis --*/ + + xi = R.m[0][0] ; xj = R.m[0][1] ; xk = R.m[0][2] ; + yi = R.m[1][0] ; yj = R.m[1][1] ; yk = R.m[1][2] ; + zi = R.m[2][0] ; zj = R.m[2][1] ; zk = R.m[2][2] ; + + /* normalize column vectors to get unit vectors along each ijk-axis */ + + /* normalize i axis */ + + val = sqrt( xi*xi + yi*yi + zi*zi ) ; + if( val == 0.0 ) return ; /* stupid input */ + xi /= val ; yi /= val ; zi /= val ; + + /* normalize j axis */ + + val = sqrt( xj*xj + yj*yj + zj*zj ) ; + if( val == 0.0 ) return ; /* stupid input */ + xj /= val ; yj /= val ; zj /= val ; + + /* orthogonalize j axis to i axis, if needed */ + + val = xi*xj + yi*yj + zi*zj ; /* dot product between i and j */ + if( fabs(val) > 1.e-4 ){ + xj -= val*xi ; yj -= val*yi ; zj -= val*zi ; + val = sqrt( xj*xj + yj*yj + zj*zj ) ; /* must renormalize */ + if( val == 0.0 ) return ; /* j was parallel to i? */ + xj /= val ; yj /= val ; zj /= val ; + } + + /* normalize k axis; if it is zero, make it the cross product i x j */ + + val = sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ){ xk = yi*zj-zi*yj; yk = zi*xj-zj*xi ; zk=xi*yj-yi*xj ; } + else { xk /= val ; yk /= val ; zk /= val ; } + + /* orthogonalize k to i */ + + val = xi*xk + yi*yk + zi*zk ; /* dot product between i and k */ + if( fabs(val) > 1.e-4 ){ + xk -= val*xi ; yk -= val*yi ; zk -= val*zi ; + val = sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; + } + + /* orthogonalize k to j */ + + val = xj*xk + yj*yk + zj*zk ; /* dot product between j and k */ + if( fabs(val) > 1.e-4 ){ + xk -= val*xj ; yk -= val*yj ; zk -= val*zj ; + val = sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; + } + + Q.m[0][0] = xi ; Q.m[0][1] = xj ; Q.m[0][2] = xk ; + Q.m[1][0] = yi ; Q.m[1][1] = yj ; Q.m[1][2] = yk ; + Q.m[2][0] = zi ; Q.m[2][1] = zj ; Q.m[2][2] = zk ; + + /* at this point, Q is the rotation matrix from (i,j,k) to (x,y,z) axes */ + + detQ = nifti_dmat33_determ( Q ) ; + if( detQ == 0.0 ) return ; /* shouldn't happen unless user is a DUFIS */ + + /* Build and test all possible +1/-1 coordinate permutation matrices P; + then find the P such that the rotation matrix M=PQ is closest to the + identity, in the sense of M having the smallest total rotation angle. */ + + /* Despite the formidable looking 6 nested loops, there are + only 3*3*3*2*2*2 = 216 passes, which will run very quickly. */ + + vbest = -666.0 ; ibest=pbest=qbest=rbest=1 ; jbest=2 ; kbest=3 ; + for( i=1 ; i <= 3 ; i++ ){ /* i = column number to use for row #1 */ + for( j=1 ; j <= 3 ; j++ ){ /* j = column number to use for row #2 */ + if( i == j ) continue ; + for( k=1 ; k <= 3 ; k++ ){ /* k = column number to use for row #3 */ + if( i == k || j == k ) continue ; + P.m[0][0] = P.m[0][1] = P.m[0][2] = + P.m[1][0] = P.m[1][1] = P.m[1][2] = + P.m[2][0] = P.m[2][1] = P.m[2][2] = 0.0 ; + for( p=-1 ; p <= 1 ; p+=2 ){ /* p,q,r are -1 or +1 */ + for( q=-1 ; q <= 1 ; q+=2 ){ /* and go into rows #1,2,3 */ + for( r=-1 ; r <= 1 ; r+=2 ){ + P.m[0][i-1] = p ; P.m[1][j-1] = q ; P.m[2][k-1] = r ; + detP = nifti_dmat33_determ(P) ; /* sign of permutation */ + if( detP * detQ <= 0.0 ) continue ; /* doesn't match sign of Q */ + M = nifti_dmat33_mul(P,Q) ; + + /* angle of M rotation = 2.0*acos(0.5*sqrt(1.0+trace(M))) */ + /* we want largest trace(M) == smallest angle == M nearest to I */ + + val = M.m[0][0] + M.m[1][1] + M.m[2][2] ; /* trace */ + if( val > vbest ){ + vbest = val ; + ibest = i ; jbest = j ; kbest = k ; + pbest = p ; qbest = q ; rbest = r ; + } + }}}}}} + + /* At this point ibest is 1 or 2 or 3; pbest is -1 or +1; etc. + + The matrix P that corresponds is the best permutation approximation + to Q-inverse; that is, P (approximately) takes (x,y,z) coordinates + to the (i,j,k) axes. + + For example, the first row of P (which contains pbest in column ibest) + determines the way the i axis points relative to the anatomical + (x,y,z) axes. If ibest is 2, then the i axis is along the y axis, + which is direction P2A (if pbest > 0) or A2P (if pbest < 0). + + So, using ibest and pbest, we can assign the output code for + the i axis. Mutatis mutandis for the j and k axes, of course. */ + + switch( ibest*pbest ){ + case 1: i = NIFTI_L2R ; break ; + case -1: i = NIFTI_R2L ; break ; + case 2: i = NIFTI_P2A ; break ; + case -2: i = NIFTI_A2P ; break ; + case 3: i = NIFTI_I2S ; break ; + case -3: i = NIFTI_S2I ; break ; + default: break; + } + + switch( jbest*qbest ){ + case 1: j = NIFTI_L2R ; break ; + case -1: j = NIFTI_R2L ; break ; + case 2: j = NIFTI_P2A ; break ; + case -2: j = NIFTI_A2P ; break ; + case 3: j = NIFTI_I2S ; break ; + case -3: j = NIFTI_S2I ; break ; + default: break; + } + + switch( kbest*rbest ){ + case 1: k = NIFTI_L2R ; break ; + case -1: k = NIFTI_R2L ; break ; + case 2: k = NIFTI_P2A ; break ; + case -2: k = NIFTI_A2P ; break ; + case 3: k = NIFTI_I2S ; break ; + case -3: k = NIFTI_S2I ; break ; + default: break; + } + + *icod = i ; *jcod = j ; *kcod = k ; } + +/*---------------------------------------------------------------------------*/ +/*! compute the (closest) orientation from a 4x4 ijk->xyz tranformation matrix + +
+   Input:  4x4 matrix that transforms (i,j,k) indexes to (x,y,z) coordinates,
+           where +x=Right, +y=Anterior, +z=Superior.
+           (Only the upper-left 3x3 corner of R is used herein.)
+   Output: 3 orientation codes that correspond to the closest "standard"
+           anatomical orientation of the (i,j,k) axes.
+   Method: Find which permutation of (x,y,z) has the smallest angle to the
+           (i,j,k) axes directions, which are the columns of the R matrix.
+   Errors: The codes returned will be zero.
+
+   For example, an axial volume might get return values of
+     *icod = NIFTI_R2L   (i axis is mostly Right to Left)
+     *jcod = NIFTI_P2A   (j axis is mostly Posterior to Anterior)
+     *kcod = NIFTI_I2S   (k axis is mostly Inferior to Superior)
+   
+ + \see "QUATERNION REPRESENTATION OF ROTATION MATRIX" in nifti1.h + + \see nifti_quatern_to_mat44, nifti_mat44_to_quatern, + nifti_make_orthog_mat44 +*//*-------------------------------------------------------------------------*/ +void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) +{ + float xi,xj,xk , yi,yj,yk , zi,zj,zk , val,detQ,detP ; + mat33 P , Q , M ; + int i,j,k=0,p,q,r , ibest,jbest,kbest,pbest,qbest,rbest ; + float vbest ; + + if( icod == NULL || jcod == NULL || kcod == NULL ) return ; /* bad */ + + *icod = *jcod = *kcod = 0 ; /* error returns, if sh*t happens */ + + /* load column vectors for each (i,j,k) direction from matrix */ + + /*-- i axis --*/ /*-- j axis --*/ /*-- k axis --*/ + + xi = R.m[0][0] ; xj = R.m[0][1] ; xk = R.m[0][2] ; + yi = R.m[1][0] ; yj = R.m[1][1] ; yk = R.m[1][2] ; + zi = R.m[2][0] ; zj = R.m[2][1] ; zk = R.m[2][2] ; + + /* normalize column vectors to get unit vectors along each ijk-axis */ + + /* normalize i axis */ + + val = (float)sqrt( xi*xi + yi*yi + zi*zi ) ; + if( val == 0.0 ) return ; /* stupid input */ + xi /= val ; yi /= val ; zi /= val ; + + /* normalize j axis */ + + val = (float)sqrt( xj*xj + yj*yj + zj*zj ) ; + if( val == 0.0 ) return ; /* stupid input */ + xj /= val ; yj /= val ; zj /= val ; + + /* orthogonalize j axis to i axis, if needed */ + + val = xi*xj + yi*yj + zi*zj ; /* dot product between i and j */ + if( fabs(val) > 1.e-4 ){ + xj -= val*xi ; yj -= val*yi ; zj -= val*zi ; + val = (float)sqrt( xj*xj + yj*yj + zj*zj ) ; /* must renormalize */ + if( val == 0.0 ) return ; /* j was parallel to i? */ + xj /= val ; yj /= val ; zj /= val ; + } + + /* normalize k axis; if it is zero, make it the cross product i x j */ + + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ){ xk = yi*zj-zi*yj; yk = zi*xj-zj*xi ; zk=xi*yj-yi*xj ; } + else { xk /= val ; yk /= val ; zk /= val ; } + + /* orthogonalize k to i */ + + val = xi*xk + yi*yk + zi*zk ; /* dot product between i and k */ + if( fabs(val) > 1.e-4 ){ + xk -= val*xi ; yk -= val*yi ; zk -= val*zi ; + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; + } + + /* orthogonalize k to j */ + + val = xj*xk + yj*yk + zj*zk ; /* dot product between j and k */ + if( fabs(val) > 1.e-4 ){ + xk -= val*xj ; yk -= val*yj ; zk -= val*zj ; + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; + } + + Q.m[0][0] = xi ; Q.m[0][1] = xj ; Q.m[0][2] = xk ; + Q.m[1][0] = yi ; Q.m[1][1] = yj ; Q.m[1][2] = yk ; + Q.m[2][0] = zi ; Q.m[2][1] = zj ; Q.m[2][2] = zk ; + + /* at this point, Q is the rotation matrix from the (i,j,k) to (x,y,z) axes */ + + detQ = nifti_mat33_determ( Q ) ; + if( detQ == 0.0 ) return ; /* shouldn't happen unless user is a DUFIS */ + + /* Build and test all possible +1/-1 coordinate permutation matrices P; + then find the P such that the rotation matrix M=PQ is closest to the + identity, in the sense of M having the smallest total rotation angle. */ + + /* Despite the formidable looking 6 nested loops, there are + only 3*3*3*2*2*2 = 216 passes, which will run very quickly. */ + + vbest = -666.0f ; ibest=pbest=qbest=rbest=1 ; jbest=2 ; kbest=3 ; + for( i=1 ; i <= 3 ; i++ ){ /* i = column number to use for row #1 */ + for( j=1 ; j <= 3 ; j++ ){ /* j = column number to use for row #2 */ + if( i == j ) continue ; + for( k=1 ; k <= 3 ; k++ ){ /* k = column number to use for row #3 */ + if( i == k || j == k ) continue ; + P.m[0][0] = P.m[0][1] = P.m[0][2] = + P.m[1][0] = P.m[1][1] = P.m[1][2] = + P.m[2][0] = P.m[2][1] = P.m[2][2] = 0.0f ; + for( p=-1 ; p <= 1 ; p+=2 ){ /* p,q,r are -1 or +1 */ + for( q=-1 ; q <= 1 ; q+=2 ){ /* and go into rows #1,2,3 */ + for( r=-1 ; r <= 1 ; r+=2 ){ + P.m[0][i-1] = p ; P.m[1][j-1] = q ; P.m[2][k-1] = r ; + detP = nifti_mat33_determ(P) ; /* sign of permutation */ + if( detP * detQ <= 0.0 ) continue ; /* doesn't match sign of Q */ + M = nifti_mat33_mul(P,Q) ; + + /* angle of M rotation = 2.0*acos(0.5*sqrt(1.0+trace(M))) */ + /* we want largest trace(M) == smallest angle == M nearest to I */ + + val = M.m[0][0] + M.m[1][1] + M.m[2][2] ; /* trace */ + if( val > vbest ){ + vbest = val ; + ibest = i ; jbest = j ; kbest = k ; + pbest = p ; qbest = q ; rbest = r ; + } + }}}}}} + + /* At this point ibest is 1 or 2 or 3; pbest is -1 or +1; etc. + + The matrix P that corresponds is the best permutation approximation + to Q-inverse; that is, P (approximately) takes (x,y,z) coordinates + to the (i,j,k) axes. + + For example, the first row of P (which contains pbest in column ibest) + determines the way the i axis points relative to the anatomical + (x,y,z) axes. If ibest is 2, then the i axis is along the y axis, + which is direction P2A (if pbest > 0) or A2P (if pbest < 0). + + So, using ibest and pbest, we can assign the output code for + the i axis. Mutatis mutandis for the j and k axes, of course. */ + + switch( ibest*pbest ){ + case 1: i = NIFTI_L2R ; break ; + case -1: i = NIFTI_R2L ; break ; + case 2: i = NIFTI_P2A ; break ; + case -2: i = NIFTI_A2P ; break ; + case 3: i = NIFTI_I2S ; break ; + case -3: i = NIFTI_S2I ; break ; + default: break; + } + + switch( jbest*qbest ){ + case 1: j = NIFTI_L2R ; break ; + case -1: j = NIFTI_R2L ; break ; + case 2: j = NIFTI_P2A ; break ; + case -2: j = NIFTI_A2P ; break ; + case 3: j = NIFTI_I2S ; break ; + case -3: j = NIFTI_S2I ; break ; + default: break; + } + + switch( kbest*rbest ){ + case 1: k = NIFTI_L2R ; break ; + case -1: k = NIFTI_R2L ; break ; + case 2: k = NIFTI_P2A ; break ; + case -2: k = NIFTI_A2P ; break ; + case 3: k = NIFTI_I2S ; break ; + case -3: k = NIFTI_S2I ; break ; + default: break; + } + + *icod = i ; *jcod = j ; *kcod = k ; } + +/*---------------------------------------------------------------------------*/ +/* Routines to swap byte arrays in various ways: + - 2 at a time: ab -> ba [short] + - 4 at a time: abcd -> dcba [int, float] + - 8 at a time: abcdDCBA -> ABCDdcba [long long, double] + - 16 at a time: abcdefghHGFEDCBA -> ABCDEFGHhgfedcba [long double] +-----------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------*/ +/*! swap each byte pair from the given list of n pairs + * + * Due to alignment of structures at some architectures (e.g. on ARM), + * stick to char varaibles. + * Fixes http://bugs.debian.org/446893 Yaroslav + * +*//*--------------------------------------------------------------------*/ +void nifti_swap_2bytes( int64_t n , void *ar ) /* 2 bytes at a time */ +{ + int64_t ii ; + unsigned char * cp1 = (unsigned char *)ar, * cp2 ; + unsigned char tval; + + for( ii=0 ; ii < n ; ii++ ){ + cp2 = cp1 + 1; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp1 += 2; + } + } + +/*----------------------------------------------------------------------*/ +/*! swap 4 bytes at a time from the given list of n sets of 4 bytes +*//*--------------------------------------------------------------------*/ +void nifti_swap_4bytes( int64_t n , void *ar ) /* 4 bytes at a time */ +{ + int64_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+3; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp1++; cp2--; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp0 += 4; + } + } + +/*----------------------------------------------------------------------*/ +/*! swap 8 bytes at a time from the given list of n sets of 8 bytes + * + * perhaps use this style for the general Nbytes, as Yaroslav suggests +*//*--------------------------------------------------------------------*/ +void nifti_swap_8bytes( int64_t n , void *ar ) /* 8 bytes at a time */ +{ + int64_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+7; + while ( cp2 > cp1 ) /* unroll? */ + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += 8; + } + } + +/*----------------------------------------------------------------------*/ +/*! swap 16 bytes at a time from the given list of n sets of 16 bytes +*//*--------------------------------------------------------------------*/ +void nifti_swap_16bytes( int64_t n , void *ar ) /* 16 bytes at a time */ +{ + int64_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+15; + while ( cp2 > cp1 ) + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += 16; + } + } + +#if 0 /* not important: save for version update 6 Jul 2010 [rickr] */ + +/*----------------------------------------------------------------------*/ +/*! generic: swap siz bytes at a time from the given list of n sets +*//*--------------------------------------------------------------------*/ +void nifti_swap_bytes( int64_t n , int siz , void *ar ) +{ + int64_t ii ; + unsigned char * cp0 = (unsigned char *)ar, * cp1, * cp2 ; + unsigned char tval ; + + for( ii=0 ; ii < n ; ii++ ){ + cp1 = cp0; cp2 = cp0+(siz-1); + while ( cp2 > cp1 ) + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += siz; + } + return ; +} +#endif + +/*---------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------*/ +/*! based on siz, call the appropriate nifti_swap_Nbytes() function +*//*--------------------------------------------------------------------*/ +void nifti_swap_Nbytes( int64_t n , int siz , void *ar ) /* subsuming case */ +{ + switch( siz ){ + case 2: nifti_swap_2bytes ( n , ar ) ; break ; + case 4: nifti_swap_4bytes ( n , ar ) ; break ; + case 8: nifti_swap_8bytes ( n , ar ) ; break ; + case 16: nifti_swap_16bytes( n , ar ) ; break ; + default: /* nifti_swap_bytes ( n , siz, ar ) ; */ + Rc_fprintf_stderr("** NIfTI: cannot swap in %d byte blocks\n", siz); + break ; + } + } + + +/*-------------------------------------------------------------------------*/ +/*! Byte swap NIFTI file header, depending on the version. +*//*---------------------------------------------------------------------- */ +void swap_nifti_header( void * hdr , int ni_ver ) +{ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("++ swapping NIFTI header via ni_ver %d\n", ni_ver); + + if ( ni_ver == 0 ) nifti_swap_as_analyze((nifti_analyze75 *)hdr); + else if( ni_ver == 1 ) nifti_swap_as_nifti1((nifti_1_header *)hdr); + else if( ni_ver == 2 ) nifti_swap_as_nifti2((nifti_2_header *)hdr); + else if( ni_ver >= 0 && ni_ver <= 9 ) { + Rc_fprintf_stderr("** swap_nifti_header: not ready for version %d\n",ni_ver); + } else { + Rc_fprintf_stderr("** swap_nifti_header: illegal version %d\n", ni_ver); + } +} + + +/*-------------------------------------------------------------------------*/ +/*! Byte swap NIFTI-2 file header. +*//*---------------------------------------------------------------------- */ +void nifti_swap_as_nifti2( nifti_2_header * h ) +{ + if ( ! h ) { + Rc_fprintf_stderr("** nifti_swap_as_nifti2: NULL pointer\n"); + return; + } + + nifti_swap_4bytes(1, &h->sizeof_hdr); + + nifti_swap_2bytes(1, &h->datatype); + nifti_swap_2bytes(1, &h->bitpix); + + nifti_swap_8bytes(8, h->dim); + nifti_swap_8bytes(1, &h->intent_p1); + nifti_swap_8bytes(1, &h->intent_p2); + nifti_swap_8bytes(1, &h->intent_p3); + nifti_swap_8bytes(8, h->pixdim); + + nifti_swap_8bytes(1, &h->vox_offset); + nifti_swap_8bytes(1, &h->scl_slope); + nifti_swap_8bytes(1, &h->scl_inter); + nifti_swap_8bytes(1, &h->cal_max); + nifti_swap_8bytes(1, &h->cal_min); + nifti_swap_8bytes(1, &h->slice_duration); + nifti_swap_8bytes(1, &h->toffset); + nifti_swap_8bytes(1, &h->slice_start); + nifti_swap_8bytes(1, &h->slice_end); + + nifti_swap_4bytes(1, &h->qform_code); + nifti_swap_4bytes(1, &h->sform_code); + + nifti_swap_8bytes(1, &h->quatern_b); + nifti_swap_8bytes(1, &h->quatern_c); + nifti_swap_8bytes(1, &h->quatern_d); + nifti_swap_8bytes(1, &h->qoffset_x); + nifti_swap_8bytes(1, &h->qoffset_y); + nifti_swap_8bytes(1, &h->qoffset_z); + + nifti_swap_8bytes(4, h->srow_x); + nifti_swap_8bytes(4, h->srow_y); + nifti_swap_8bytes(4, h->srow_z); + + nifti_swap_4bytes(1, &h->slice_code); + nifti_swap_4bytes(1, &h->xyzt_units); + nifti_swap_4bytes(1, &h->intent_code); +} + +/*-------------------------------------------------------------------------*/ +/*! Byte swap NIFTI-1 file header in various places and ways. + * return 0 on success +*//*---------------------------------------------------------------------- */ +void nifti_swap_as_nifti1( nifti_1_header * h ) +{ + if ( ! h ) { + Rc_fprintf_stderr("** nifti_swap_as_nifti1: NULL pointer\n"); + return; + } + + nifti_swap_4bytes(1, &h->sizeof_hdr); + nifti_swap_4bytes(1, &h->extents); + nifti_swap_2bytes(1, &h->session_error); + + nifti_swap_2bytes(8, h->dim); + nifti_swap_4bytes(1, &h->intent_p1); + nifti_swap_4bytes(1, &h->intent_p2); + nifti_swap_4bytes(1, &h->intent_p3); + + nifti_swap_2bytes(1, &h->intent_code); + nifti_swap_2bytes(1, &h->datatype); + nifti_swap_2bytes(1, &h->bitpix); + nifti_swap_2bytes(1, &h->slice_start); + + nifti_swap_4bytes(8, h->pixdim); + + nifti_swap_4bytes(1, &h->vox_offset); + nifti_swap_4bytes(1, &h->scl_slope); + nifti_swap_4bytes(1, &h->scl_inter); + nifti_swap_2bytes(1, &h->slice_end); + + nifti_swap_4bytes(1, &h->cal_max); + nifti_swap_4bytes(1, &h->cal_min); + nifti_swap_4bytes(1, &h->slice_duration); + nifti_swap_4bytes(1, &h->toffset); + nifti_swap_4bytes(1, &h->glmax); + nifti_swap_4bytes(1, &h->glmin); + + nifti_swap_2bytes(1, &h->qform_code); + nifti_swap_2bytes(1, &h->sform_code); + + nifti_swap_4bytes(1, &h->quatern_b); + nifti_swap_4bytes(1, &h->quatern_c); + nifti_swap_4bytes(1, &h->quatern_d); + nifti_swap_4bytes(1, &h->qoffset_x); + nifti_swap_4bytes(1, &h->qoffset_y); + nifti_swap_4bytes(1, &h->qoffset_z); + + nifti_swap_4bytes(4, h->srow_x); + nifti_swap_4bytes(4, h->srow_y); + nifti_swap_4bytes(4, h->srow_z); +} + +/*-------------------------------------------------------------------------*/ +/*! Byte swap as an ANALYZE 7.5 header + * + * return non-zero on failure +*//*---------------------------------------------------------------------- */ +void nifti_swap_as_analyze( nifti_analyze75 * h ) +{ + if ( ! h ) { + Rc_fprintf_stderr("** nifti_swap_as_analyze: NULL pointer\n"); + return; + } + + nifti_swap_4bytes(1, &h->sizeof_hdr); + nifti_swap_4bytes(1, &h->extents); + nifti_swap_2bytes(1, &h->session_error); + + nifti_swap_2bytes(8, h->dim); + nifti_swap_2bytes(1, &h->unused8); + nifti_swap_2bytes(1, &h->unused9); + nifti_swap_2bytes(1, &h->unused10); + nifti_swap_2bytes(1, &h->unused11); + nifti_swap_2bytes(1, &h->unused12); + nifti_swap_2bytes(1, &h->unused13); + nifti_swap_2bytes(1, &h->unused14); + + nifti_swap_2bytes(1, &h->datatype); + nifti_swap_2bytes(1, &h->bitpix); + nifti_swap_2bytes(1, &h->dim_un0); + + nifti_swap_4bytes(8, h->pixdim); + + nifti_swap_4bytes(1, &h->vox_offset); + nifti_swap_4bytes(1, &h->funused1); + nifti_swap_4bytes(1, &h->funused2); + nifti_swap_4bytes(1, &h->funused3); + + nifti_swap_4bytes(1, &h->cal_max); + nifti_swap_4bytes(1, &h->cal_min); + nifti_swap_4bytes(1, &h->compressed); + nifti_swap_4bytes(1, &h->verified); + nifti_swap_4bytes(1, &h->glmax); + nifti_swap_4bytes(1, &h->glmin); + + nifti_swap_4bytes(1, &h->views); + nifti_swap_4bytes(1, &h->vols_added); + nifti_swap_4bytes(1, &h->start_field); + nifti_swap_4bytes(1, &h->field_skip); + + nifti_swap_4bytes(1, &h->omax); + nifti_swap_4bytes(1, &h->omin); + nifti_swap_4bytes(1, &h->smax); + nifti_swap_4bytes(1, &h->smin); +} + +/*-------------------------------------------------------------------------*/ +/*! OLD VERSION of swap_nifti_header (left for undo/compare operations) + + Byte swap NIFTI-1 file header in various places and ways. + + If is_nifti is nonzero, will also swap the NIFTI-specific + components of the header; otherwise, only the components + common to NIFTI and ANALYZE will be swapped. +*//*---------------------------------------------------------------------- */ +void old_swap_nifti_header( nifti_1_header *h , int is_nifti ) +{ + /* this stuff is always present, for ANALYZE and NIFTI */ + + swap_4(h->sizeof_hdr) ; + nifti_swap_2bytes( 8 , h->dim ) ; + nifti_swap_4bytes( 8 , h->pixdim ) ; + + swap_2(h->datatype) ; + swap_2(h->bitpix) ; + + swap_4(h->vox_offset); swap_4(h->cal_max); swap_4(h->cal_min); + + /* this stuff is NIFTI specific */ + + if( is_nifti ){ + swap_4(h->intent_p1); swap_4(h->intent_p2); swap_4(h->intent_p3); + swap_2(h->intent_code); + + swap_2(h->slice_start); swap_2(h->slice_end); + swap_4(h->scl_slope); swap_4(h->scl_inter); + swap_4(h->slice_duration); swap_4(h->toffset); + + swap_2(h->qform_code); swap_2(h->sform_code); + swap_4(h->quatern_b); swap_4(h->quatern_c); swap_4(h->quatern_d); + swap_4(h->qoffset_x); swap_4(h->qoffset_y); swap_4(h->qoffset_z); + nifti_swap_4bytes(4,h->srow_x); + nifti_swap_4bytes(4,h->srow_y); + nifti_swap_4bytes(4,h->srow_z); + } + } + + +#define USE_STAT +#ifdef USE_STAT +/*---------------------------------------------------------------------------*/ +/* Return the file length (0 if file not found or has no contents). + This is a Unix-specific function, since it uses stat(). +-----------------------------------------------------------------------------*/ +#include +#include + +/*---------------------------------------------------------------------------*/ +/*! return the size of a file, in bytes + + \return size of file on success, -1 on error or no file + + changed to return int, -1 means no file or error 20 Dec 2004 [rickr] +*//*-------------------------------------------------------------------------*/ +int64_t nifti2_get_filesize( const char *pathname ) +{ + struct stat buf ; int ii ; + + if( pathname == NULL || *pathname == '\0' ) return -1 ; + ii = stat( pathname , &buf ); if( ii != 0 ) return -1 ; + return buf.st_size ; +} + +#else /*---------- non-Unix version of the above, less efficient -----------*/ + +int64_t nifti2_get_filesize( const char *pathname ) +{ + znzFile fp ; int64_t len ; + + if( pathname == NULL || *pathname == '\0' ) return -1 ; + fp = znzopen(pathname,"rb",0); if( znz_isnull(fp) ) return -1 ; + znzseek(fp,0L,SEEK_END) ; len = znztell(fp) ; + znzclose(fp) ; return len ; +} + +#endif /* USE_STAT */ + + +/*----------------------------------------------------------------------*/ +/*! return the total volume size, in bytes + + This is computed as nvox * nbyper. +*//*--------------------------------------------------------------------*/ +int64_t nifti2_get_volsize(const nifti_image *nim) +{ + return (int64_t)nim->nbyper * nim->nvox ; /* total bytes */ +} + + +/*--------------------------------------------------------------------------*/ +/* Support functions for filenames in read and write + - allows for gzipped files +*/ + + +/*----------------------------------------------------------------------*/ +/*! simple check for file existence + + \return 1 on existence, 0 otherwise +*//*--------------------------------------------------------------------*/ +int nifti_fileexists(const char* fname) +{ + znzFile fp; + fp = znzopen( fname , "rb" , nifti_is_gzfile(fname) ) ; + if( !znz_isnull(fp) ) { znzclose(fp); return 1; } + return 0; /* fp is NULL */ +} + +/*----------------------------------------------------------------------*/ +/*! return whether the filename is valid + + Note: uppercase extensions are now valid. 27 Apr 2009 [rickr] + + The name is considered valid if the file basename has length greater than + zero, AND one of the valid nifti extensions is provided. + fname input | return | + =============================== + "myimage" | 0 | + "myimage.tif" | 0 | + "myimage.tif.gz" | 0 | + "myimage.nii" | 1 | + ".nii" | 0 | + ".myhiddenimage" | 0 | + ".myhiddenimage.nii" | 1 | +*//*--------------------------------------------------------------------*/ +int nifti_is_complete_filename(const char* fname) +{ + const char * ext; + + /* check input file(s) for sanity */ + if( fname == NULL || *fname == '\0' ){ + if ( g_opts.debug > 1 ) + Rc_fprintf_stderr("-- empty filename in nifti_validfilename()\n"); + return 0; + } + + ext = nifti_find_file_extension(fname); + if ( ext == NULL ) { /*Invalid extension given */ + if ( g_opts.debug > 0 ) + Rc_fprintf_stderr("-- no nifti valid extension for filename '%s'\n", fname); + return 0; + } + + if ( ext && ext == fname ) { /* then no filename prefix */ + if ( g_opts.debug > 0 ) + Rc_fprintf_stderr("-- no prefix for filename '%s'\n", fname); + return 0; + } + return 1; +} + +/*----------------------------------------------------------------------*/ +/*! return whether the filename is valid + + Allow uppercase extensions as valid. 27 Apr 2009 [rickr] + Any .gz extension case must match the base extension case. + + The name is considered valid if its length is positive, excluding + any nifti filename extension. + fname input | return | result of nifti_makebasename + ==================================================================== + "myimage" | 1 | "myimage" + "myimage.tif" | 1 | "myimage.tif" + "myimage.tif.gz" | 1 | "myimage.tif" + "myimage.nii" | 1 | "myimage" + ".nii" | 0 | + ".myhiddenimage" | 1 | ".myhiddenimage" + ".myhiddenimage.nii | 1 | ".myhiddenimage" +*//*--------------------------------------------------------------------*/ +int nifti_validfilename(const char* fname) +{ + const char * ext; + + /* check input file(s) for sanity */ + if( fname == NULL || *fname == '\0' ){ + if ( g_opts.debug > 1 ) + Rc_fprintf_stderr("-- empty filename in nifti_validfilename()\n"); + return 0; + } + + ext = nifti_find_file_extension(fname); + + if ( ext && ext == fname ) { /* then no filename prefix */ + if ( g_opts.debug > 0 ) + Rc_fprintf_stderr("-- no prefix for filename '%s'\n", fname); + return 0; + } + + return 1; +} + +/*----------------------------------------------------------------------*/ +/*! check the end of the filename for a valid nifti extension + + Valid extensions are currently .nii, .hdr, .img, .nia, + or any of them followed by .gz. Note that '.' is part of + the extension. + + Uppercase extensions are also valid, but not mixed case. + + \return a pointer to the extension substring within the original + function input parameter name, or NULL if not found. + \caution Note that if the input parameter is is immutabale + (i.e. a const char *) then this function performs an + implicit casting away of the mutability constraint and + the return parameter will appear as a mutable + even though it is part of the immuttable string. +*//*--------------------------------------------------------------------*/ +char * nifti_find_file_extension( const char * name ) +{ + const char * ext; + char extcopy[8]; + int len; + char extnii[8] = ".nii"; /* modifiable, for possible uppercase */ + char exthdr[8] = ".hdr"; /* (leave space for .gz) */ + char extimg[8] = ".img"; + char extnia[8] = ".nia"; + char extgz[4] = ".gz"; + char * elist[4] = { NULL, NULL, NULL, NULL}; + + /* stupid compiler... */ + elist[0] = extnii; elist[1] = exthdr; elist[2] = extimg; elist[3] = extnia; + + if ( ! name ) return NULL; + + len = (int)strlen(name); + if ( len < 4 ) return NULL; + + ext = name + len - 4; + + /* make manipulation copy, and possibly convert to lowercase */ + strcpy(extcopy, ext); + if( g_opts.allow_upper_fext ) make_lowercase(extcopy); + + /* if it look like a basic extension, fail or return it */ + if( compare_strlist(extcopy, elist, 4) >= 0 ) { + if( is_mixedcase(ext) ) { + Rc_fprintf_stderr("** NIFTI: mixed case extension '%s' is not valid\n", + ext); + return NULL; + } + else return (char *)ext; /* Cast away the constness of the input parameter */ + } + +#ifdef HAVE_ZLIB + if ( len < 7 ) return NULL; + + ext = name + len - 7; + + /* make manipulation copy, and possibly convert to lowercase */ + strcpy(extcopy, ext); + if( g_opts.allow_upper_fext ) make_lowercase(extcopy); + + /* go after .gz extensions using the modifiable strings */ + strcat(elist[0], extgz); strcat(elist[1], extgz); strcat(elist[2], extgz); + + if( compare_strlist(extcopy, elist, 3) >= 0 ) { + if( is_mixedcase(ext) ) { + Rc_fprintf_stderr("** NIFTI: mixed case extension '%s' is not valid\n", + ext); + return NULL; + } + else return (char *)ext; /* Cast away the constness of the input parameter */ + } + +#endif + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** find_file_ext: failed for name '%s'\n", name); + + return NULL; +} + +/*----------------------------------------------------------------------*/ +/*! return whether the filename ends in ".gz" +*//*--------------------------------------------------------------------*/ +int nifti_is_gzfile(const char* fname) +{ + /* return true if the filename ends with .gz */ + if (fname == NULL) { return 0; } +#ifdef HAVE_ZLIB + { /* just so len doesn't generate compile warning */ + int len; + len = (int)strlen(fname); + if (len < 3) return 0; /* so we don't search before the name */ + if (fileext_compare(fname + strlen(fname) - 3,".gz")==0) { return 1; } + } +#endif + return 0; +} + +/*----------------------------------------------------------------------*/ +/*! return whether the given library was compiled with HAVE_ZLIB set +*//*--------------------------------------------------------------------*/ +int nifti_compiled_with_zlib(void) +{ +#ifdef HAVE_ZLIB + return 1; +#else + return 0; +#endif +} + +/*----------------------------------------------------------------------*/ +/*! duplicate the filename, while clearing any extension + + This allocates memory for basename which should eventually be freed. +*//*--------------------------------------------------------------------*/ +char * nifti_makebasename(const char* fname) +{ + char *basename; + const char *ext; + + basename=nifti_strdup(fname); + + ext = nifti_find_file_extension(basename); + if ( ext ) + { + basename[strlen(basename)-strlen(ext)] = '\0'; /* clear out extension */ + } + + return basename; /* in either case */ +} + +/*----------------------------------------------------------------------*/ +/* option accessor functions */ +/*----------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------*/ +/*! set nifti's global debug level, for status reporting + + - 0 : quiet, nothing is printed to the terminal, but errors + - 1 : normal execution (the default) + - 2, 3 : more details +*//*--------------------------------------------------------------------*/ +void nifti_set_debug_level( int level ) +{ + g_opts.debug = level; +} + +/*----------------------------------------------------------------------*/ +/*! set nifti's global skip_blank_ext flag 5 Sep 2006 [rickr] + + explicitly set to 0 or 1 +*//*--------------------------------------------------------------------*/ +void nifti_set_skip_blank_ext( int skip ) +{ + g_opts.skip_blank_ext = skip ? 1 : 0; +} + +/*----------------------------------------------------------------------*/ +/*! set nifti's global allow_upper_fext flag 28 Apr 2009 [rickr] + + explicitly set to 0 or 1 +*//*--------------------------------------------------------------------*/ +void nifti_set_allow_upper_fext( int allow ) +{ + g_opts.allow_upper_fext = allow ? 1 : 0; +} + +/*----------------------------------------------------------------------*/ +/*! get nifti's global alter_cifti flag 22 Jul 2015 [rickr] +*//*--------------------------------------------------------------------*/ +int nifti_get_alter_cifti( void ) +{ + return g_opts.alter_cifti; +} + +/*----------------------------------------------------------------------*/ +/*! set nifti's global alter_cifti flag 22 Jul 2015 [rickr] + + explicitly set to 0 or 1 +*//*--------------------------------------------------------------------*/ +void nifti_set_alter_cifti( int alter_cifti ) +{ + g_opts.alter_cifti = alter_cifti ? 1 : 0; +} + +/*----------------------------------------------------------------------*/ +/*! check current directory for existing header file + + \return filename of header on success and NULL if no appropriate file + could be found + + If fname has an uppercase extension, check for uppercase files. + + NB: it allocates memory for hdrname which should be freed + when no longer required +*//*-------------------------------------------------------------------*/ +char * nifti_findhdrname(const char* fname) +{ + char *basename, *hdrname; + const char *ext; + char elist[2][5] = { ".hdr", ".nii" }; + char extzip[4] = ".gz"; + int efirst = 1; /* init to .nii extension */ + int eisupper = 0; /* init to lowercase extensions */ + + /**- check input file(s) for sanity */ + if( !nifti_validfilename(fname) ) return NULL; + + basename = nifti_makebasename(fname); + if( !basename ) return NULL; /* only on string alloc failure */ + + /**- return filename if it has a valid extension and exists + (except if it is an .img file (and maybe .gz)) */ + ext = nifti_find_file_extension(fname); + + if( ext ) eisupper = is_uppercase(ext); /* do we look for uppercase? */ + + /* if the file exists and is a valid header name (not .img), return it */ + if ( ext && nifti_fileexists(fname) ) { + /* allow for uppercase extension */ + if ( fileext_n_compare(ext,".img",4) != 0 ){ + hdrname = nifti_strdup(fname); + free(basename); + return hdrname; + } else + efirst = 0; /* note for below */ + } + + /* So the requested name is a basename, contains .img, or does not exist. */ + /* In any case, use basename. */ + + /**- if .img, look for .hdr, .hdr.gz, .nii, .nii.gz, in that order */ + /**- else, look for .nii, .nii.gz, .hdr, .hdr.gz, in that order */ + + /* if we get more extension choices, this could be a loop */ + + /* note: efirst is 0 in the case of ".img" */ + + /* if the user passed an uppercase entension (.IMG), search for uppercase */ + if( eisupper ) { + make_uppercase(elist[0]); + make_uppercase(elist[1]); + make_uppercase(extzip); + } + + hdrname = (char *)calloc(sizeof(char),strlen(basename)+8); + if( !hdrname ){ + Rc_fprintf_stderr("** nifti_findhdrname: failed to alloc hdrname\n"); + free(basename); + return NULL; + } + + strcpy(hdrname,basename); + strcat(hdrname,elist[efirst]); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#ifdef HAVE_ZLIB + strcat(hdrname,extzip); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#endif + + /* okay, try the other possibility */ + + efirst = 1 - efirst; + + strcpy(hdrname,basename); + strcat(hdrname,elist[efirst]); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#ifdef HAVE_ZLIB + strcat(hdrname,extzip); + if (nifti_fileexists(hdrname)) { free(basename); return hdrname; } +#endif + + /**- if nothing has been found, return NULL */ + free(basename); + free(hdrname); + return NULL; +} + + +/*------------------------------------------------------------------------*/ +/*! check current directory for existing image file + + \param fname filename to check for + \nifti_type nifti_type for dataset - this determines whether to + first check for ".nii" or ".img" (since both may exist) + + \return filename of data/img file on success and NULL if no appropriate + file could be found + + If fname has a valid, uppercase extension, apply all extensions as + uppercase. + + NB: it allocates memory for the image filename, which should be freed + when no longer required +*//*---------------------------------------------------------------------*/ +char * nifti_findimgname(const char* fname , int nifti_type) +{ + /* store all extensions as strings, in case we need to go uppercase */ + char *basename, *imgname, elist[2][5] = { ".nii", ".img" }; + char extzip[4] = ".gz"; + char extnia[5] = ".nia"; + const char *ext; + int first; /* first extension to use */ + + /* check input file(s) for sanity */ + if( !nifti_validfilename(fname) ) return NULL; + + basename = nifti_makebasename(fname); + imgname = (char *)calloc(sizeof(char),strlen(basename)+8); + if( !imgname ){ + Rc_fprintf_stderr("** nifti_findimgname: failed to alloc imgname\n"); + free(basename); + return NULL; + } + + /* if we are looking for uppercase, apply the fact now */ + ext = nifti_find_file_extension(fname); + if( ext && is_uppercase(ext) ) { + make_uppercase(elist[0]); + make_uppercase(elist[1]); + make_uppercase(extzip); + make_uppercase(extnia); + } + + /* only valid extension for ASCII type is .nia, handle first */ + if( nifti_type == NIFTI_FTYPE_ASCII ){ + strcpy(imgname,basename); + strcat(imgname,extnia); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } + + } else { + + /**- test for .nii and .img (don't assume input type from image type) */ + /**- if nifti_type = 1, check for .nii first, else .img first */ + + /* if we get 3 or more extensions, can make a loop here... */ + + if (nifti_type == NIFTI_FTYPE_NIFTI1_1) first = 0; /* should match .nii */ + else if (nifti_type == NIFTI_FTYPE_NIFTI2_1) first = 0; + else first = 1; /* should match .img */ + + strcpy(imgname,basename); + strcat(imgname,elist[first]); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#ifdef HAVE_ZLIB /* then also check for .gz */ + strcat(imgname,extzip); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#endif + + /* failed to find image file with expected extension, try the other */ + + strcpy(imgname,basename); + strcat(imgname,elist[1-first]); /* can do this with only 2 choices */ + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#ifdef HAVE_ZLIB /* then also check for .gz */ + strcat(imgname,extzip); + if (nifti_fileexists(imgname)) { free(basename); return imgname; } +#endif + } + + /**- if nothing has been found, return NULL */ + free(basename); + free(imgname); + return NULL; +} + + +/*----------------------------------------------------------------------*/ +/*! creates a filename for storing the header, based on nifti_type + + \param prefix - this will be copied before the suffix is added + \param nifti_type - determines the extension, unless one is in prefix + \param check - check for existence (fail condition) + \param comp - add .gz for compressed name + + Note that if prefix provides a file suffix, nifti_type is not used. + + NB: this allocates memory which should be freed + + \sa nifti_set_filenames +*//*-------------------------------------------------------------------*/ +char * nifti_makehdrname(const char * prefix, int nifti_type, int check, + int comp) +{ + char * iname; + const char * ext; + char extnii[5] = ".nii"; /* modifiable, for possible uppercase */ + char exthdr[5] = ".hdr"; + char extimg[5] = ".img"; + char extnia[5] = ".nia"; + char extgz[5] = ".gz"; + + if( !nifti_validfilename(prefix) ) return NULL; + + /* add space for extension, optional ".gz", and null char */ + iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + if( !iname ){ + Rc_fprintf_stderr("** NIFTI small malloc failure!\n"); + return NULL; + } + strcpy(iname, prefix); + + /* use any valid extension */ + if( (ext = nifti_find_file_extension(iname)) != NULL ){ + /* if uppercase, convert all extensions */ + if( is_uppercase(ext) ) { + make_uppercase(extnii); + make_uppercase(exthdr); + make_uppercase(extimg); + make_uppercase(extnia); + make_uppercase(extgz); + } + + if( strncmp(ext,extimg,4) == 0 ) + { + memcpy(&(iname[strlen(iname)-strlen(ext)]),exthdr,4); /* then convert img name to hdr */ + } + } + /* otherwise, make one up */ + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); + else if( nifti_type == NIFTI_FTYPE_NIFTI2_1 ) strcat(iname, extnii); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); + else strcat(iname, exthdr); + +#ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ + if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); +#endif + + /* check for existence failure */ + if( check && nifti_fileexists(iname) ){ + Rc_fprintf_stderr("** failure: NIFTI header file '%s' already exists\n", + iname); + free(iname); + return NULL; + } + + if(g_opts.debug > 2) Rc_fprintf_stderr("+d made header filename '%s'\n", iname); + + return iname; +} + + +/*----------------------------------------------------------------------*/ +/*! creates a filename for storing the image, based on nifti_type + + \param prefix - this will be copied before the suffix is added + \param nifti_type - determines the extension, unless provided by prefix + \param check - check for existence (fail condition) + \param comp - add .gz for compressed name + + Note that if prefix provides a file suffix, nifti_type is not used. + + NB: it allocates memory which should be freed + + \sa nifti_set_filenames +*//*-------------------------------------------------------------------*/ +char * nifti_makeimgname(const char * prefix, int nifti_type, int check, + int comp) +{ + char * iname; + const char * ext; + char extnii[5] = ".nii"; /* modifiable, for possible uppercase */ + char exthdr[5] = ".hdr"; + char extimg[5] = ".img"; + char extnia[5] = ".nia"; + char extgz[5] = ".gz"; + + if( !nifti_validfilename(prefix) ) return NULL; + + /* add space for extension, optional ".gz", and null char */ + iname = (char *)calloc(sizeof(char),strlen(prefix)+8); + if( !iname ){ + Rc_fprintf_stderr("** NIFTI: small malloc failure!\n"); + return NULL; + } + strcpy(iname, prefix); + + /* use any valid extension */ + if( (ext = nifti_find_file_extension(iname)) != NULL ){ + /* if uppercase, convert all extensions */ + if( is_uppercase(ext) ) { + make_uppercase(extnii); + make_uppercase(exthdr); + make_uppercase(extimg); + make_uppercase(extnia); + make_uppercase(extgz); + } + + if( strncmp(ext,exthdr,4) == 0 ) + { + memcpy(&(iname[strlen(iname)-strlen(ext)]),extimg,4); /* then convert hdr name to img */ + } + } + /* otherwise, make one up */ + else if( nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcat(iname, extnii); + else if( nifti_type == NIFTI_FTYPE_NIFTI2_1 ) strcat(iname, extnii); + else if( nifti_type == NIFTI_FTYPE_ASCII ) strcat(iname, extnia); + else strcat(iname, extimg); + +#ifdef HAVE_ZLIB /* if compression is requested, make sure of suffix */ + if( comp && (!ext || !strstr(iname,extgz)) ) strcat(iname,extgz); +#endif + + /* check for existence failure */ + if( check && nifti_fileexists(iname) ){ + Rc_fprintf_stderr("** NIFTI failure: image file '%s' already exists\n", + iname); + free(iname); + return NULL; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("+d made image filename '%s'\n",iname); + + return iname; +} + + +/*----------------------------------------------------------------------*/ +/*! create and set new filenames, based on prefix and image type + + \param nim pointer to nifti_image in which to set filenames + \param prefix (required) prefix for output filenames + \param check check for previous existence of filename + (existence is an error condition) + \param set_byte_order flag to set nim->byteorder here + (this is probably a logical place to do so) + + \return 0 on successful update + + \warning this will free() any existing names and create new ones + + \sa nifti_makeimgname, nifti_makehdrname, nifti_type_and_names_match +*//*--------------------------------------------------------------------*/ +int nifti2_set_filenames( nifti_image * nim, const char * prefix, int check, + int set_byte_order ) +{ + int comp = nifti_is_gzfile(prefix); + + if( !nim || !prefix ){ + Rc_fprintf_stderr("** nifti_set_filenames, bad params %p, %p\n", + (void *)nim,prefix); + return -1; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d modifying output filenames using prefix %s\n", prefix); + + /* set and test output filenames */ + if( nim->fname ) free(nim->fname); + if( nim->iname ) free(nim->iname); + nim->iname = NULL; + nim->fname = nifti_makehdrname(prefix, nim->nifti_type, check, comp); + if( nim->fname ) + nim->iname = nifti_makeimgname(prefix, nim->nifti_type, check, comp); + if( !nim->fname || !nim->iname ) return -1; /* failure */ + + if( set_byte_order ) nim->byteorder = nifti_short_order() ; + + if( nifti_set_type_from_names(nim) < 0 ) + return -1; + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d have new filenames %s and %s\n",nim->fname,nim->iname); + + return 0; +} + + +/*--------------------------------------------------------------------------*/ +/*! check whether nifti_type matches fname and iname for the nifti_image + + - if type 0 or 2, expect .hdr/.img pair + - if type 1, expect .nii (and names must match) + + \param nim given nifti_image + \param show_warn if set, print a warning message for any mis-match + + \return + - 1 if the values seem to match + - 0 if there is a mis-match + - -1 if there is not sufficient information to create file(s) + + \sa NIFTI_FTYPE_* codes in nifti1_io.h + \sa nifti_set_type_from_names, is_valid_nifti_type +*//*------------------------------------------------------------------------*/ +int nifti2_type_and_names_match( nifti_image * nim, int show_warn ) +{ + char func[] = "nifti_type_and_names_match"; + const char * ext_h; /* header filename extension */ + const char * ext_i; /* image filename extension */ + int errs = 0; /* error counter */ + + /* sanity checks */ + if( !nim ){ + if( show_warn ) Rc_fprintf_stderr("** %s: missing nifti_image\n", func); + return -1; + } + if( !nim->fname ){ + if( show_warn ) Rc_fprintf_stderr("** %s: missing header filename\n", func); + errs++; + } + if( !nim->iname ){ + if( show_warn ) Rc_fprintf_stderr("** %s: missing image filename\n", func); + errs++; + } + if( !is_valid_nifti_type(nim->nifti_type) ){ + if( show_warn ) + Rc_fprintf_stderr("** %s: bad nifti_type %d\n", func, nim->nifti_type); + errs++; + } + + if( errs ) return -1; /* then do not proceed */ + + /* get pointers to extensions */ + ext_h = nifti_find_file_extension( nim->fname ); + ext_i = nifti_find_file_extension( nim->iname ); + + /* check for filename extensions */ + if( !ext_h ){ + if( show_warn ) + Rc_fprintf_stderr("-d missing NIFTI extension in header filename, %s\n", + nim->fname); + errs++; + } + if( !ext_i ){ + if( show_warn ) + Rc_fprintf_stderr("-d missing NIFTI extension in image filename, %s\n", + nim->iname); + errs++; + } + + if( errs ) return 0; /* do not proceed, but this is just a mis-match */ + + /* general tests */ + if( (nim->nifti_type == NIFTI_FTYPE_NIFTI1_1) || + (nim->nifti_type == NIFTI_FTYPE_NIFTI2_1) ){ /* .nii */ + if( fileext_n_compare(ext_h,".nii",4) ) { + if( show_warn ) + Rc_fprintf_stderr( + "-d NIFTI_FTYPE 1, but no .nii extension in header filename, %s\n", + nim->fname); + errs++; + } + if( fileext_n_compare(ext_i,".nii",4) ) { + if( show_warn ) + Rc_fprintf_stderr( + "-d NIFTI_FTYPE 1, but no .nii extension in image filename, %s\n", + nim->iname); + errs++; + } + if( strcmp(nim->fname, nim->iname) != 0 ){ + if( show_warn ) + Rc_fprintf_stderr( + "-d NIFTI_FTYPE 1, but header and image filenames differ: %s, %s\n", + nim->fname, nim->iname); + errs++; + } + } + else if( (nim->nifti_type == NIFTI_FTYPE_NIFTI1_2) || /* .hdr/.img */ + (nim->nifti_type == NIFTI_FTYPE_NIFTI2_2) || + (nim->nifti_type == NIFTI_FTYPE_ANALYZE) ) + { + if( fileext_n_compare(ext_h,".hdr",4) != 0 ){ + if( show_warn ) + Rc_fprintf_stderr("-d no '.hdr' extension, but NIFTI type is %d, %s\n", + nim->nifti_type, nim->fname); + errs++; + } + if( fileext_n_compare(ext_i,".img",4) != 0 ){ + if( show_warn ) + Rc_fprintf_stderr("-d no '.img' extension, but NIFTI type is %d, %s\n", + nim->nifti_type, nim->iname); + errs++; + } + } + /* ignore any other nifti_type */ + + if( errs ) return 0; /* types do not match */ + + return 1; +} + +/* like strcmp, but also check against capitalization of known_ext + * (test as local string, with max length 7) */ +static int fileext_compare(const char * test_ext, const char * known_ext) +{ + char caps[8] = ""; + size_t c,len; + /* if equal, don't need to check case (store to avoid multiple calls) */ + const int cmp = strcmp(test_ext, known_ext); + if( cmp == 0 ) return cmp; + + /* if anything odd, use default */ + if( !test_ext || !known_ext ) return cmp; + + len = strlen(known_ext); + if( len > 7 ) return cmp; + + /* if here, strings are different but need to check upper-case */ + + for(c = 0; c < len; c++ ) caps[c] = toupper((int) known_ext[c]); + caps[c] = '\0'; + + return strcmp(test_ext, caps); +} + +/* like strncmp, but also check against capitalization of known_ext + * (test as local string, with max length 7) */ +static int fileext_n_compare(const char * test_ext, + const char * known_ext, size_t maxlen) +{ + char caps[8] = ""; + size_t c,len; + /* if equal, don't need to check case (store to avoid multiple calls) */ + const int cmp = strncmp(test_ext, known_ext, maxlen); + if( cmp == 0 ) return cmp; + + /* if anything odd, use default */ + if( !test_ext || !known_ext ) return cmp; + + len = strlen(known_ext); + if( len > maxlen ) len = maxlen; /* ignore anything past maxlen */ + if( len > 7 ) return cmp; + + /* if here, strings are different but need to check upper-case */ + for(c = 0; c < len; c++ ) caps[c] = toupper((int) known_ext[c]); + caps[c] = '\0'; + + return strncmp(test_ext, caps, maxlen); +} + +/* return 1 if there are uppercase but no lowercase */ +static int is_uppercase(const char * str) +{ + size_t c; + int hasupper = 0; + + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) { + if( islower((int) str[c]) ) return 0; + if( !hasupper && isupper((int) str[c]) ) hasupper = 1; + } + + return hasupper; +} + +/* return 1 if there are both uppercase and lowercase characters */ +static int is_mixedcase(const char * str) +{ + size_t c; + int hasupper = 0, haslower = 0; + + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) { + if( !haslower && islower((int) str[c]) ) haslower = 1; + if( !hasupper && isupper((int) str[c]) ) hasupper = 1; + + if( haslower && hasupper ) return 1; + } + + return 0; +} + +/* convert any lowercase chars to uppercase */ +static int make_uppercase(char * str) +{ + size_t c; + + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) + if( islower((int) str[c]) ) str[c] = toupper((int) str[c]); + + return 0; +} + +/* convert any uppercase chars to lowercase */ +static int make_lowercase(char * str) +{ + size_t c; + if( !str || !*str ) return 0; + + for(c = 0; c < strlen(str); c++ ) + if( isupper((int) str[c]) ) str[c] = tolower((int) str[c]); + + return 0; +} + +/* run strcmp against of list of strings + * return index of equality, if found + * else return -1 */ +static int compare_strlist(const char * str, char ** strlist, int len) +{ + int c; + if( len <= 0 || !str || !strlist ) return -1; + for( c = 0; c < len; c++ ) + if( strlist[c] && !strcmp(str, strlist[c]) ) return c; + return -1; +} + +/*--------------------------------------------------------------------------*/ +/*! check whether the given type is on the "approved" list + + The code is valid if it is non-negative, and does not exceed + NIFTI_MAX_FTYPE. + + \return 1 if nifti_type is valid, 0 otherwise + \sa NIFTI_FTYPE_* codes in nifti1_io.h +*//*------------------------------------------------------------------------*/ +int is_valid_nifti2_type( int nifti_type ) +{ + if( nifti_type >= NIFTI_FTYPE_ANALYZE && /* smallest type, 0 */ + nifti_type <= NIFTI_MAX_FTYPE ) + return 1; + return 0; +} + + +/*--------------------------------------------------------------------------*/ +/*! check whether the given type is on the "approved" list + + The type is explicitly checked against the NIFTI_TYPE_* list + in nifti1.h. + + \return 1 if dtype is valid, 0 otherwise + \sa NIFTI_TYPE_* codes in nifti1.h +*//*------------------------------------------------------------------------*/ +int nifti_is_valid_datatype( int dtype ) +{ + if( dtype == NIFTI_TYPE_UINT8 || + dtype == NIFTI_TYPE_INT16 || + dtype == NIFTI_TYPE_INT32 || + dtype == NIFTI_TYPE_FLOAT32 || + dtype == NIFTI_TYPE_COMPLEX64 || + dtype == NIFTI_TYPE_FLOAT64 || + dtype == NIFTI_TYPE_RGB24 || + dtype == NIFTI_TYPE_RGBA32 || + dtype == NIFTI_TYPE_INT8 || + dtype == NIFTI_TYPE_UINT16 || + dtype == NIFTI_TYPE_UINT32 || + dtype == NIFTI_TYPE_INT64 || + dtype == NIFTI_TYPE_UINT64 || + dtype == NIFTI_TYPE_FLOAT128 || + dtype == NIFTI_TYPE_COMPLEX128 || + dtype == NIFTI_TYPE_COMPLEX256 ) return 1; + return 0; +} + + +/*--------------------------------------------------------------------------*/ +/*! set the nifti_type field based on fname and iname + + Note that nifti_type is changed only when it does not match + the filenames. + + \return 0 on success, -1 on error + + \sa is_valid_nifti_type, nifti_type_and_names_match +*//*------------------------------------------------------------------------*/ +int nifti2_set_type_from_names( nifti_image * nim ) +{ + /* error checking first */ + if( !nim ){ Rc_fprintf_stderr("** NSTFN: no nifti_image\n"); return -1; } + + if( !nim->fname || !nim->iname ){ + Rc_fprintf_stderr("** NIFTI_STFN: NULL filename(s) fname @ %p, iname @ %p\n", + nim->fname, nim->iname); + return -1; + } + + if( ! nifti_validfilename ( nim->fname ) || + ! nifti_validfilename ( nim->iname ) || + ! nifti_find_file_extension( nim->fname ) || + ! nifti_find_file_extension( nim->iname ) + ) { + Rc_fprintf_stderr("** NIFTI_STFN: invalid filename(s) " + "fname='%s', iname='%s'\n", + nim->fname, nim->iname); + return -1; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d verify nifti_type from filenames: %d",nim->nifti_type); + + /* type should be NIFTI_FTYPE_ASCII if extension is .nia */ + if( (fileext_compare(nifti_find_file_extension(nim->fname),".nia")==0)){ + nim->nifti_type = NIFTI_FTYPE_ASCII; + } else { + /* not too picky here, do what must be done, and then verify */ + if( strcmp(nim->fname, nim->iname) == 0 ) /* one file, type 1 */ + nim->nifti_type = (nim->nifti_type >= NIFTI_FTYPE_NIFTI2_1) ? NIFTI_FTYPE_NIFTI2_1 : NIFTI_FTYPE_NIFTI1_1; + else if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) /* cannot be type 1 */ + nim->nifti_type = NIFTI_FTYPE_NIFTI1_2; + else if( nim->nifti_type == NIFTI_FTYPE_NIFTI2_1 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI2_2; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr(" -> %d\n",nim->nifti_type); + + if( g_opts.debug > 1 ) /* warn user about anything strange */ + nifti_type_and_names_match(nim, 1); + + if( is_valid_nifti_type(nim->nifti_type) ) return 0; /* success! */ + + Rc_fprintf_stderr("** NSTFN: bad nifti_type %d, for '%s' and '%s'\n", + nim->nifti_type, nim->fname, nim->iname); + + return -1; +} + + +/*--------------------------------------------------------------------------*/ +/*! Determine if this is a NIFTI-formatted file. + +
+   \return  0 if file looks like ANALYZE 7.5 [checks sizeof_hdr field == 348]
+            1 if file marked as NIFTI (header+data in 1 file)
+            2 if file marked as NIFTI (header+data in 2 files)
+           -1 if it can't tell, file doesn't exist, etc.
+   
+*//*------------------------------------------------------------------------*/ +int is_nifti_file( const char *hname ) +{ + nifti_1_header nhdr ; + znzFile fp ; + int ii ; + char *tmpname; + +/* rcr - update to check for nifti-1 or -2 */ + + /* bad input name? */ + + if( !nifti_validfilename(hname) ) return -1 ; + + /* open file */ + + tmpname = nifti_findhdrname(hname); + if( tmpname == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: no header file found for '%s'\n",hname); + return -1; + } + fp = znzopen( tmpname , "rb" , nifti_is_gzfile(tmpname) ) ; + free(tmpname); + if (znz_isnull(fp)) return -1 ; /* bad open? */ + + /* read header, close file */ + + ii = (int)znzread( &nhdr , 1 , sizeof(nhdr) , fp ) ; + znzclose( fp ) ; + if( ii < (int) sizeof(nhdr) ) return -1 ; /* bad read? */ + + /* check for NIFTI-ness */ + + if( NIFTI_VERSION(nhdr) != 0 ){ + return ( NIFTI_ONEFILE(nhdr) ) ? 1 : 2 ; + } + + /* check for ANALYZE-ness (sizeof_hdr field == 348) */ + + ii = nhdr.sizeof_hdr ; + if( ii == (int)sizeof(nhdr) ) return 0 ; /* matches */ + + /* try byte-swapping header */ + + swap_4(ii) ; + if( ii == (int)sizeof(nhdr) ) return 0 ; /* matches */ + + return -1 ; /* not good */ +} + +static int print_hex_vals( const char * data, size_t nbytes, FILE * fp ) +{ + size_t c; + + if ( !data || nbytes < 1 || !fp ) return -1; + + fputs("0x", fp); + for ( c = 0; c < nbytes; c++ ) + fprintf(fp, " %02x", data[c]); + + return 0; +} + +/*----------------------------------------------------------------------*/ +/*! display the contents of the nifti_1_header (send to stdout) + + \param info if non-NULL, print this character string + \param hp pointer to nifti_1_header +*//*--------------------------------------------------------------------*/ +int disp_nifti_1_header( const char * info, const nifti_1_header * hp ) +{ + int c; + + Rc_fputs_stdout( "-------------------------------------------------------\n" ); + if ( info ) Rc_fputs_stdout( info ); + if ( !hp ){ Rc_fputs_stdout(" ** no nifti_1_header to display!\n"); return 1; } + + Rc_fprintf_stdout(" nifti_1_header :\n" + " sizeof_hdr = %d\n" + " data_type[10] = ", hp->sizeof_hdr); +#ifndef USING_R + print_hex_vals(hp->data_type, 10, stdout); +#endif + Rc_fprintf_stdout("\n" + " db_name[18] = "); +#ifndef USING_R + print_hex_vals(hp->db_name, 18, stdout); +#endif + Rc_fprintf_stdout("\n" + " extents = %d\n" + " session_error = %d\n" + " regular = 0x%x\n" + " dim_info = 0x%x\n", + hp->extents, hp->session_error, hp->regular, hp->dim_info ); + Rc_fprintf_stdout(" dim[8] ="); + for ( c = 0; c < 8; c++ ) Rc_fprintf_stdout(" %d", hp->dim[c]); + Rc_fprintf_stdout("\n" + " intent_p1 = %f\n" + " intent_p2 = %f\n" + " intent_p3 = %f\n" + " intent_code = %d\n" + " datatype = %d\n" + " bitpix = %d\n" + " slice_start = %d\n" + " pixdim[8] =", + hp->intent_p1, hp->intent_p2, hp->intent_p3, hp->intent_code, + hp->datatype, hp->bitpix, hp->slice_start); + /* break pixdim over 2 lines */ + for ( c = 0; c < 4; c++ ) Rc_fprintf_stdout(" %f", hp->pixdim[c]); + Rc_fprintf_stdout("\n "); + for ( c = 4; c < 8; c++ ) Rc_fprintf_stdout(" %f", hp->pixdim[c]); + Rc_fprintf_stdout("\n" + " vox_offset = %f\n" + " scl_slope = %f\n" + " scl_inter = %f\n" + " slice_end = %d\n" + " slice_code = %d\n" + " xyzt_units = 0x%x\n" + " cal_max = %f\n" + " cal_min = %f\n" + " slice_duration = %f\n" + " toffset = %f\n" + " glmax = %d\n" + " glmin = %d\n", + hp->vox_offset, hp->scl_slope, hp->scl_inter, hp->slice_end, + hp->slice_code, hp->xyzt_units, hp->cal_max, hp->cal_min, + hp->slice_duration, hp->toffset, hp->glmax, hp->glmin); + Rc_fprintf_stdout( + " descrip = '%.80s'\n" + " aux_file = '%.24s'\n" + " qform_code = %d\n" + " sform_code = %d\n" + " quatern_b = %f\n" + " quatern_c = %f\n" + " quatern_d = %f\n" + " qoffset_x = %f\n" + " qoffset_y = %f\n" + " qoffset_z = %f\n" + " srow_x[4] = %f, %f, %f, %f\n" + " srow_y[4] = %f, %f, %f, %f\n" + " srow_z[4] = %f, %f, %f, %f\n" + " intent_name = '%-.16s'\n" + " magic = '%-.4s'\n", + hp->descrip, hp->aux_file, hp->qform_code, hp->sform_code, + hp->quatern_b, hp->quatern_c, hp->quatern_d, + hp->qoffset_x, hp->qoffset_y, hp->qoffset_z, + hp->srow_x[0], hp->srow_x[1], hp->srow_x[2], hp->srow_x[3], + hp->srow_y[0], hp->srow_y[1], hp->srow_y[2], hp->srow_y[3], + hp->srow_z[0], hp->srow_z[1], hp->srow_z[2], hp->srow_z[3], + hp->intent_name, hp->magic); + Rc_fputs_stdout( "-------------------------------------------------------\n" ); +#ifndef USING_R + fflush(stdout); +#endif + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! display the contents of the nifti_2_header (send to stdout) + + \param info if non-NULL, print this character string + \param hp pointer to nifti_2_header +*//*--------------------------------------------------------------------*/ +int disp_nifti_2_header( const char * info, const nifti_2_header * hp ) +{ + int c; + + Rc_fputs_stdout( "-------------------------------------------------------\n" ); + if ( info ) Rc_fputs_stdout( info ); + if ( !hp ){ Rc_fputs_stdout(" ** no nifti_2_header to display!\n"); return 1; } + + /* print fields one by one, makes changing order and copying easier */ + + Rc_fprintf_stdout(" nifti_2_header :\n"); + Rc_fprintf_stdout(" sizeof_hdr = %d\n", hp->sizeof_hdr); + Rc_fprintf_stdout(" magic[8] = '%-.4s' + ", hp->magic); +#ifndef USING_R + print_hex_vals(hp->magic+4, 4, stdout); +#endif + Rc_fputc_stdout('\n'); + + Rc_fprintf_stdout(" datatype = %d (%s)\n", + hp->datatype, nifti_datatype_to_string(hp->datatype)); + Rc_fprintf_stdout(" bitpix = %d\n", hp->bitpix); + Rc_fprintf_stdout( " dim[8] ="); + for ( c = 0; c < 8; c++ ) Rc_fprintf_stdout(" %" PRId64, hp->dim[c]); + Rc_fputc_stdout('\n'); + + Rc_fprintf_stdout( " intent_p1 = %lf\n", hp->intent_p1); + Rc_fprintf_stdout( " intent_p2 = %lf\n", hp->intent_p2); + Rc_fprintf_stdout( " intent_p3 = %lf\n", hp->intent_p3); + Rc_fprintf_stdout( " pixdim[8] ="); + for ( c = 0; c < 8; c++ ) Rc_fprintf_stdout(" %lf", hp->pixdim[c]); + Rc_fputc_stdout('\n'); + + Rc_fprintf_stdout( " vox_offset = %" PRId64 "\n", hp->vox_offset); + + Rc_fprintf_stdout( " scl_slope = %lf\n", hp->scl_slope); + Rc_fprintf_stdout( " scl_inter = %lf\n", hp->scl_inter); + Rc_fprintf_stdout( " cal_max = %lf\n", hp->cal_max); + Rc_fprintf_stdout( " cal_min = %lf\n", hp->cal_min); + Rc_fprintf_stdout( " slice_duration = %lf\n", hp->slice_duration); + Rc_fprintf_stdout( " toffset = %lf\n", hp->toffset); + + Rc_fprintf_stdout( " slice_start = %" PRId64 "\n", hp->slice_start); + Rc_fprintf_stdout( " slice_end = %" PRId64 "\n", hp->slice_end); + + Rc_fprintf_stdout( " descrip = '%.80s'\n", hp->descrip); + Rc_fprintf_stdout( " aux_file = '%.24s'\n", hp->aux_file); + + Rc_fprintf_stdout( " qform_code = %d\n", hp->qform_code); + Rc_fprintf_stdout( " sform_code = %d\n", hp->sform_code); + + Rc_fprintf_stdout( " quatern_b = %lf\n", hp->quatern_b); + Rc_fprintf_stdout( " quatern_c = %lf\n", hp->quatern_c); + Rc_fprintf_stdout( " quatern_d = %lf\n", hp->quatern_d); + Rc_fprintf_stdout( " qoffset_x = %lf\n", hp->qoffset_x); + Rc_fprintf_stdout( " qoffset_y = %lf\n", hp->qoffset_y); + Rc_fprintf_stdout( " qoffset_z = %lf\n", hp->qoffset_z); + Rc_fprintf_stdout( " srow_x[4] = %lf, %lf, %lf, %lf\n", + hp->srow_x[0], hp->srow_x[1], hp->srow_x[2], hp->srow_x[3]); + Rc_fprintf_stdout( " srow_y[4] = %lf, %lf, %lf, %lf\n", + hp->srow_y[0], hp->srow_y[1], hp->srow_y[2], hp->srow_y[3]); + Rc_fprintf_stdout( " srow_z[4] = %lf, %lf, %lf, %lf\n", + hp->srow_z[0], hp->srow_z[1], hp->srow_z[2], hp->srow_z[3]); + + Rc_fprintf_stdout( " slice_code = %d\n", hp->slice_code); + Rc_fprintf_stdout( " xyzt_units = %d\n", hp->xyzt_units); + Rc_fprintf_stdout( " intent_code = %d\n", hp->intent_code); + + Rc_fprintf_stdout( " intent_name = '%-.16s'\n", hp->intent_name); + Rc_fprintf_stdout( " dim_info = 0x%02x\n",(unsigned char)hp->dim_info); + Rc_fprintf_stdout( " unused_str = 0x "); + for ( c = 0; c < 15; c++ ) Rc_fprintf_stdout(" %02x", hp->unused_str[c]); + Rc_fputc_stdout('\n'); + + Rc_fputs_stdout( "-------------------------------------------------------\n" ); +#ifndef USING_R + fflush(stdout); +#endif + + return 0; +} + + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_convert_n1hdr2nim: %s\n", (msg) ) ; \ + return NULL ; } while(0) + +/*----------------------------------------------------------------------*/ +/*! convert a nifti_1_header into a nift1_image + + \return an allocated nifti_image, or NULL on failure +*//*--------------------------------------------------------------------*/ +nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) +{ + int ii , doswap , ioff ; + int ni_ver , is_onefile ; + nifti_image *nim; + + nim = (nifti_image *)calloc( 1 , sizeof(nifti_image) ) ; + if( !nim ) ERREX("failed to allocate nifti image"); + + /* be explicit with pointers */ + nim->fname = NULL; + nim->iname = NULL; + nim->data = NULL; + + /**- check if we must swap bytes */ + + doswap = need_nhdr_swap(nhdr.dim[0], nhdr.sizeof_hdr); /* swap data flag */ + + if( doswap < 0 ){ + free(nim); + if( doswap == -1 ) ERREX("bad dim[0]") ; + ERREX("bad sizeof_hdr") ; /* else */ + } + + /**- determine if this is a NIFTI-1 compliant header */ + + ni_ver = NIFTI_VERSION(nhdr) ; + /* + * before swapping header, record the Analyze75 orient code + */ + if(ni_ver == 0) + { + /**- in analyze75, the orient code is at the same address as + * qform_code, but it's just one byte + * the qform_code will be zero, at which point you can check + * analyze75_orient if you care to. + */ + unsigned char c = *((char *)(&nhdr.qform_code)); + nim->analyze75_orient = (analyze_75_orient_code)c; + } + if( doswap ) { + if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); + swap_nifti_header( &nhdr , ni_ver ) ; + } + + if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr); + + if( nhdr.datatype == DT_BINARY || nhdr.datatype == DT_UNKNOWN ) + { + free(nim); + ERREX("bad datatype") ; + } + + if( nhdr.dim[1] <= 0 ) + { + free(nim); + ERREX("bad dim[1]") ; + } + + /* fix bad dim[] values in the defined dimension range */ + for( ii=2 ; ii <= nhdr.dim[0] ; ii++ ) + if( nhdr.dim[ii] <= 0 ) nhdr.dim[ii] = 1 ; + + /* fix any remaining bad dim[] values, so garbage does not propagate */ + /* (only values 0 or 1 seem rational, otherwise set to arbirary 1) */ + for( ii=nhdr.dim[0]+1 ; ii <= 7 ; ii++ ) + if( nhdr.dim[ii] != 1 && nhdr.dim[ii] != 0) nhdr.dim[ii] = 1 ; + +#if 0 /* rely on dim[0], do not attempt to modify it 16 Nov 2005 [rickr] */ + + /**- get number of dimensions (ignoring dim[0] now) */ + for( ii=7 ; ii >= 2 ; ii-- ) /* loop backwards until we */ + if( nhdr.dim[ii] > 1 ) break ; /* find a dim bigger than 1 */ + ndim = ii ; +#endif + + /**- set bad grid spacings to 1.0 */ + + for( ii=1 ; ii <= nhdr.dim[0] ; ii++ ){ + if( nhdr.pixdim[ii] == 0.0 || + !IS_GOOD_FLOAT(nhdr.pixdim[ii]) ) nhdr.pixdim[ii] = 1.0f ; + } + + is_onefile = (ni_ver > 0) && NIFTI_ONEFILE(nhdr) ; + + if( ni_ver ) nim->nifti_type = (is_onefile) ? NIFTI_FTYPE_NIFTI1_1 + : NIFTI_FTYPE_NIFTI1_2 ; + else nim->nifti_type = NIFTI_FTYPE_ANALYZE ; + + ii = nifti_short_order() ; + if( doswap ) nim->byteorder = REVERSE_ORDER(ii) ; + else nim->byteorder = ii ; + + + /**- set dimensions of data array */ + + nim->ndim = nim->dim[0] = nhdr.dim[0]; + nim->nx = nim->dim[1] = nhdr.dim[1]; + nim->ny = nim->dim[2] = nhdr.dim[2]; + nim->nz = nim->dim[3] = nhdr.dim[3]; + nim->nt = nim->dim[4] = nhdr.dim[4]; + nim->nu = nim->dim[5] = nhdr.dim[5]; + nim->nv = nim->dim[6] = nhdr.dim[6]; + nim->nw = nim->dim[7] = nhdr.dim[7]; + + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + nim->nvox *= nhdr.dim[ii]; + + /**- set the type of data in voxels and how many bytes per voxel */ + + nim->datatype = nhdr.datatype ; + + nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; + if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + + /**- set the grid spacings */ + + nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; + nim->dy = nim->pixdim[2] = nhdr.pixdim[2] ; + nim->dz = nim->pixdim[3] = nhdr.pixdim[3] ; + nim->dt = nim->pixdim[4] = nhdr.pixdim[4] ; + nim->du = nim->pixdim[5] = nhdr.pixdim[5] ; + nim->dv = nim->pixdim[6] = nhdr.pixdim[6] ; + nim->dw = nim->pixdim[7] = nhdr.pixdim[7] ; + + /**- compute qto_xyz transformation from pixel indexes (i,j,k) to (x,y,z) */ + + if( !ni_ver || nhdr.qform_code <= 0 ){ + /**- if not nifti or qform_code <= 0, use grid spacing for qto_xyz */ + + nim->qto_xyz.m[0][0] = nim->dx ; /* grid spacings */ + nim->qto_xyz.m[1][1] = nim->dy ; /* along diagonal */ + nim->qto_xyz.m[2][2] = nim->dz ; + + /* off diagonal is zero */ + + nim->qto_xyz.m[0][1]=nim->qto_xyz.m[0][2]=nim->qto_xyz.m[0][3] = 0.0f; + nim->qto_xyz.m[1][0]=nim->qto_xyz.m[1][2]=nim->qto_xyz.m[1][3] = 0.0f; + nim->qto_xyz.m[2][0]=nim->qto_xyz.m[2][1]=nim->qto_xyz.m[2][3] = 0.0f; + + /* last row is always [ 0 0 0 1 ] */ + + nim->qto_xyz.m[3][0]=nim->qto_xyz.m[3][1]=nim->qto_xyz.m[3][2] = 0.0f; + nim->qto_xyz.m[3][3]= 1.0f ; + + nim->qform_code = NIFTI_XFORM_UNKNOWN ; + + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d no qform provided\n"); + } else { + /**- else NIFTI: use the quaternion-specified transformation */ + + nim->quatern_b = FIXED_FLOAT( nhdr.quatern_b ) ; + nim->quatern_c = FIXED_FLOAT( nhdr.quatern_c ) ; + nim->quatern_d = FIXED_FLOAT( nhdr.quatern_d ) ; + + nim->qoffset_x = FIXED_FLOAT(nhdr.qoffset_x) ; + nim->qoffset_y = FIXED_FLOAT(nhdr.qoffset_y) ; + nim->qoffset_z = FIXED_FLOAT(nhdr.qoffset_z) ; + + nim->qfac = (nhdr.pixdim[0] < 0.0) ? -1.0f : 1.0f ; /* left-handedness? */ + + nim->qto_xyz = nifti_quatern_to_dmat44( + nim->quatern_b, nim->quatern_c, nim->quatern_d, + nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, + nim->dx , nim->dy , nim->dz , + nim->qfac ) ; + + nim->qform_code = nhdr.qform_code ; + + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d qform orientations:\n", nim->qto_xyz); + } + + /**- load inverse transformation (x,y,z) -> (i,j,k) */ + + nim->qto_ijk = nifti_dmat44_inverse( nim->qto_xyz ) ; + + /**- load sto_xyz affine transformation, if present */ + + if( !ni_ver || nhdr.sform_code <= 0 ){ + /**- if not nifti or sform_code <= 0, then no sto transformation */ + + nim->sform_code = NIFTI_XFORM_UNKNOWN ; + + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d no sform provided\n"); + + } else { + /**- else set the sto transformation from srow_*[] */ + + nim->sto_xyz.m[0][0] = nhdr.srow_x[0] ; + nim->sto_xyz.m[0][1] = nhdr.srow_x[1] ; + nim->sto_xyz.m[0][2] = nhdr.srow_x[2] ; + nim->sto_xyz.m[0][3] = nhdr.srow_x[3] ; + + nim->sto_xyz.m[1][0] = nhdr.srow_y[0] ; + nim->sto_xyz.m[1][1] = nhdr.srow_y[1] ; + nim->sto_xyz.m[1][2] = nhdr.srow_y[2] ; + nim->sto_xyz.m[1][3] = nhdr.srow_y[3] ; + + nim->sto_xyz.m[2][0] = nhdr.srow_z[0] ; + nim->sto_xyz.m[2][1] = nhdr.srow_z[1] ; + nim->sto_xyz.m[2][2] = nhdr.srow_z[2] ; + nim->sto_xyz.m[2][3] = nhdr.srow_z[3] ; + + /* last row is always [ 0 0 0 1 ] */ + + nim->sto_xyz.m[3][0]=nim->sto_xyz.m[3][1]=nim->sto_xyz.m[3][2] = 0.0f; + nim->sto_xyz.m[3][3]= 1.0f ; + + nim->sto_ijk = nifti_dmat44_inverse( nim->sto_xyz ) ; + + nim->sform_code = nhdr.sform_code ; + + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d sform orientations:\n", nim->sto_xyz); + } + + /**- set miscellaneous NIFTI stuff */ + + if( ni_ver ){ + nim->scl_slope = FIXED_FLOAT( nhdr.scl_slope ) ; + nim->scl_inter = FIXED_FLOAT( nhdr.scl_inter ) ; + + nim->intent_code = nhdr.intent_code ; + + nim->intent_p1 = FIXED_FLOAT( nhdr.intent_p1 ) ; + nim->intent_p2 = FIXED_FLOAT( nhdr.intent_p2 ) ; + nim->intent_p3 = FIXED_FLOAT( nhdr.intent_p3 ) ; + + nim->toffset = FIXED_FLOAT( nhdr.toffset ) ; + + memcpy(nim->intent_name,nhdr.intent_name,15); nim->intent_name[15] = '\0'; + + nim->xyz_units = XYZT_TO_SPACE(nhdr.xyzt_units) ; + nim->time_units = XYZT_TO_TIME (nhdr.xyzt_units) ; + + nim->freq_dim = DIM_INFO_TO_FREQ_DIM ( nhdr.dim_info ) ; + nim->phase_dim = DIM_INFO_TO_PHASE_DIM( nhdr.dim_info ) ; + nim->slice_dim = DIM_INFO_TO_SLICE_DIM( nhdr.dim_info ) ; + + nim->slice_code = nhdr.slice_code ; + nim->slice_start = nhdr.slice_start ; + nim->slice_end = nhdr.slice_end ; + nim->slice_duration = FIXED_FLOAT(nhdr.slice_duration) ; + } + + /**- set Miscellaneous ANALYZE stuff */ + + nim->cal_min = FIXED_FLOAT(nhdr.cal_min) ; + nim->cal_max = FIXED_FLOAT(nhdr.cal_max) ; + + memcpy(nim->descrip ,nhdr.descrip ,79) ; nim->descrip [79] = '\0' ; + memcpy(nim->aux_file,nhdr.aux_file,23) ; nim->aux_file[23] = '\0' ; + + /**- set ioff from vox_offset (but at least sizeof(header)) */ + + is_onefile = ni_ver && NIFTI_ONEFILE(nhdr) ; + + if( is_onefile ){ + ioff = (int)nhdr.vox_offset ; + if( ioff < (int) sizeof(nhdr) ) ioff = (int) sizeof(nhdr) ; + } else { + ioff = (int)nhdr.vox_offset ; + } + nim->iname_offset = ioff ; + + + /**- deal with file names if set */ + if (fname!=NULL) { + nifti_set_filenames(nim,fname,0,0); + if (nim->iname==NULL) { ERREX("bad filename"); } + } else { + nim->fname = NULL; + nim->iname = NULL; + } + + /* clear extension fields */ + nim->num_ext = 0; + nim->ext_list = NULL; + + return nim; +} + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_convert_n2hdr2nim: %s\n", (msg) ) ; \ + return NULL ; } while(0) + +/*----------------------------------------------------------------------*/ +/*! convert a nifti_2_header into a nifti_image + + \return an allocated nifti_image, or NULL on failure +*//*--------------------------------------------------------------------*/ +nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) +{ + int ii, doswap, ni_ver, is_onefile; + nifti_image *nim; + + nim = (nifti_image *)calloc( 1 , sizeof(nifti_image) ) ; + if( !nim ) ERREX("failed to allocate nifti image"); + + /* be explicit with pointers */ + nim->fname = NULL; + nim->iname = NULL; + nim->data = NULL; + + /**- check if we must swap bytes */ + + doswap = NIFTI2_NEEDS_SWAP(nhdr); /* swap data flag */ + + /**- determine if this is a NIFTI-2 compliant header */ + + ni_ver = NIFTI_VERSION(nhdr) ; + if(ni_ver != 2) { + free(nim); + Rc_fprintf_stderr("** convert NIFTI-2 hdr2nim: bad version %d\n", ni_ver); + return NULL; + } + + if( doswap ) { + if ( g_opts.debug > 3 ) disp_nifti_2_header("-d n2 pre-swap: ", &nhdr); + swap_nifti_header( &nhdr , ni_ver ) ; + } else if ( g_opts.debug > 3 ) Rc_fprintf_stderr("-- n2hdr2nim: no swap\n"); + + if ( g_opts.debug > 2 ) disp_nifti_2_header("-d n2hdr2nim : ", &nhdr); + + if( nhdr.datatype == DT_BINARY || nhdr.datatype == DT_UNKNOWN ) + { + free(nim); + ERREX("bad datatype") ; + } + + if( nhdr.dim[1] <= 0 ) + { + free(nim); + ERREX("bad dim[1]") ; + } + + /* fix bad dim[] values in the defined dimension range */ + for( ii=2 ; ii <= nhdr.dim[0] ; ii++ ) + if( nhdr.dim[ii] <= 0 ) nhdr.dim[ii] = 1 ; + + /* fix any remaining bad dim[] values, so garbage does not propagate */ + /* (only values 0 or 1 seem rational, otherwise set to arbirary 1) */ + for( ii=nhdr.dim[0]+1 ; ii <= 7 ; ii++ ) + if( nhdr.dim[ii] != 1 && nhdr.dim[ii] != 0) nhdr.dim[ii] = 1 ; + + /**- set bad grid spacings to 1.0 */ + for( ii=1 ; ii <= nhdr.dim[0] ; ii++ ){ + if( nhdr.pixdim[ii] == 0.0 || + !IS_GOOD_FLOAT(nhdr.pixdim[ii]) ) nhdr.pixdim[ii] = 1.0 ; + } + + is_onefile = (ni_ver > 0) && NIFTI_ONEFILE(nhdr) ; + + nim->nifti_type = (is_onefile) ? NIFTI_FTYPE_NIFTI2_1 : NIFTI_FTYPE_NIFTI2_2; + + ii = nifti_short_order() ; + if( doswap ) nim->byteorder = REVERSE_ORDER(ii) ; + else nim->byteorder = ii ; + + + /**- set dimensions of data array */ + + nim->ndim = nim->dim[0] = nhdr.dim[0]; + nim->nx = nim->dim[1] = nhdr.dim[1]; + nim->ny = nim->dim[2] = nhdr.dim[2]; + nim->nz = nim->dim[3] = nhdr.dim[3]; + nim->nt = nim->dim[4] = nhdr.dim[4]; + nim->nu = nim->dim[5] = nhdr.dim[5]; + nim->nv = nim->dim[6] = nhdr.dim[6]; + nim->nw = nim->dim[7] = nhdr.dim[7]; + + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + nim->nvox *= nhdr.dim[ii]; + + /**- set the type of data in voxels and how many bytes per voxel */ + + nim->datatype = nhdr.datatype ; + + nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; + if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + + /**- set the grid spacings */ + + nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; + nim->dy = nim->pixdim[2] = nhdr.pixdim[2] ; + nim->dz = nim->pixdim[3] = nhdr.pixdim[3] ; + nim->dt = nim->pixdim[4] = nhdr.pixdim[4] ; + nim->du = nim->pixdim[5] = nhdr.pixdim[5] ; + nim->dv = nim->pixdim[6] = nhdr.pixdim[6] ; + nim->dw = nim->pixdim[7] = nhdr.pixdim[7] ; + + /**- compute qto_xyz transformation from pixel indexes (i,j,k) to (x,y,z) */ + + if( !ni_ver || nhdr.qform_code <= 0 ){ + /**- if not nifti or qform_code <= 0, use grid spacing for qto_xyz */ + + nim->qto_xyz.m[0][0] = nim->dx ; /* grid spacings */ + nim->qto_xyz.m[1][1] = nim->dy ; /* along diagonal */ + nim->qto_xyz.m[2][2] = nim->dz ; + + /* off diagonal is zero */ + + nim->qto_xyz.m[0][1]=nim->qto_xyz.m[0][2]=nim->qto_xyz.m[0][3] = 0.0f; + nim->qto_xyz.m[1][0]=nim->qto_xyz.m[1][2]=nim->qto_xyz.m[1][3] = 0.0f; + nim->qto_xyz.m[2][0]=nim->qto_xyz.m[2][1]=nim->qto_xyz.m[2][3] = 0.0f; + + /* last row is always [ 0 0 0 1 ] */ + + nim->qto_xyz.m[3][0]=nim->qto_xyz.m[3][1]=nim->qto_xyz.m[3][2] = 0.0f; + nim->qto_xyz.m[3][3]= 1.0f ; + + nim->qform_code = NIFTI_XFORM_UNKNOWN ; + + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d no qform provided\n"); + } else { + /**- else NIFTI: use the quaternion-specified transformation */ + + nim->quatern_b = FIXED_FLOAT( nhdr.quatern_b ) ; + nim->quatern_c = FIXED_FLOAT( nhdr.quatern_c ) ; + nim->quatern_d = FIXED_FLOAT( nhdr.quatern_d ) ; + + nim->qoffset_x = FIXED_FLOAT(nhdr.qoffset_x) ; + nim->qoffset_y = FIXED_FLOAT(nhdr.qoffset_y) ; + nim->qoffset_z = FIXED_FLOAT(nhdr.qoffset_z) ; + + nim->qfac = (nhdr.pixdim[0] < 0.0) ? -1.0 : 1.0 ; /* left-handedness? */ + + nim->qto_xyz = nifti_quatern_to_dmat44( + nim->quatern_b, nim->quatern_c, nim->quatern_d, + nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, + nim->dx , nim->dy , nim->dz , + nim->qfac ) ; + + nim->qform_code = nhdr.qform_code ; + + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d qform orientations:\n", nim->qto_xyz); + } + + /**- load inverse transformation (x,y,z) -> (i,j,k) */ + + nim->qto_ijk = nifti_dmat44_inverse( nim->qto_xyz ) ; + + /**- load sto_xyz affine transformation, if present */ + + if( !ni_ver || nhdr.sform_code <= 0 ){ + /**- if not nifti or sform_code <= 0, then no sto transformation */ + + nim->sform_code = NIFTI_XFORM_UNKNOWN ; + + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d no sform provided\n"); + + } else { + /**- else set the sto transformation from srow_*[] */ + + nim->sto_xyz.m[0][0] = nhdr.srow_x[0] ; + nim->sto_xyz.m[0][1] = nhdr.srow_x[1] ; + nim->sto_xyz.m[0][2] = nhdr.srow_x[2] ; + nim->sto_xyz.m[0][3] = nhdr.srow_x[3] ; + + nim->sto_xyz.m[1][0] = nhdr.srow_y[0] ; + nim->sto_xyz.m[1][1] = nhdr.srow_y[1] ; + nim->sto_xyz.m[1][2] = nhdr.srow_y[2] ; + nim->sto_xyz.m[1][3] = nhdr.srow_y[3] ; + + nim->sto_xyz.m[2][0] = nhdr.srow_z[0] ; + nim->sto_xyz.m[2][1] = nhdr.srow_z[1] ; + nim->sto_xyz.m[2][2] = nhdr.srow_z[2] ; + nim->sto_xyz.m[2][3] = nhdr.srow_z[3] ; + + /* last row is always [ 0 0 0 1 ] */ + + nim->sto_xyz.m[3][0]=nim->sto_xyz.m[3][1]=nim->sto_xyz.m[3][2] = 0.0f; + nim->sto_xyz.m[3][3]= 1.0f ; + + nim->sto_ijk = nifti_dmat44_inverse( nim->sto_xyz ) ; + + nim->sform_code = nhdr.sform_code ; + + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d sform orientations:\n", nim->sto_xyz); + } + + /**- set miscellaneous NIFTI stuff */ + + if( ni_ver ){ + nim->scl_slope = FIXED_FLOAT( nhdr.scl_slope ) ; + nim->scl_inter = FIXED_FLOAT( nhdr.scl_inter ) ; + + nim->intent_code = nhdr.intent_code ; + + nim->intent_p1 = FIXED_FLOAT( nhdr.intent_p1 ) ; + nim->intent_p2 = FIXED_FLOAT( nhdr.intent_p2 ) ; + nim->intent_p3 = FIXED_FLOAT( nhdr.intent_p3 ) ; + + nim->toffset = FIXED_FLOAT( nhdr.toffset ) ; + + memcpy(nim->intent_name,nhdr.intent_name,15); nim->intent_name[15] = '\0'; + + nim->xyz_units = XYZT_TO_SPACE(nhdr.xyzt_units) ; + nim->time_units = XYZT_TO_TIME (nhdr.xyzt_units) ; + + nim->freq_dim = DIM_INFO_TO_FREQ_DIM ( nhdr.dim_info ) ; + nim->phase_dim = DIM_INFO_TO_PHASE_DIM( nhdr.dim_info ) ; + nim->slice_dim = DIM_INFO_TO_SLICE_DIM( nhdr.dim_info ) ; + + nim->slice_code = nhdr.slice_code ; + nim->slice_start = nhdr.slice_start ; + nim->slice_end = nhdr.slice_end ; + nim->slice_duration = FIXED_FLOAT(nhdr.slice_duration) ; + } + + /**- set Miscellaneous ANALYZE stuff */ + + nim->cal_min = FIXED_FLOAT(nhdr.cal_min) ; + nim->cal_max = FIXED_FLOAT(nhdr.cal_max) ; + + memcpy(nim->descrip ,nhdr.descrip ,79) ; nim->descrip [79] = '\0' ; + memcpy(nim->aux_file,nhdr.aux_file,23) ; nim->aux_file[23] = '\0' ; + + /**- set ioff from vox_offset (but at least sizeof(header)) */ + + nim->iname_offset = nhdr.vox_offset; + if( is_onefile && nhdr.vox_offset < (int64_t)sizeof(nhdr) ) + nim->iname_offset = (int64_t)sizeof(nhdr); + + /**- deal with file names if set */ + if (fname!=NULL) { + nifti_set_filenames(nim,fname,0,0); + if (nim->iname==NULL) { ERREX("bad filename"); } + } else { + nim->fname = NULL; + nim->iname = NULL; + } + + /* clear extension fields */ + nim->num_ext = 0; + nim->ext_list = NULL; + + return nim; +} + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_image_open(%s): %s\n", \ + (hname != NULL) ? hname : "(null)" , (msg) ) ; \ + return fptr ; } while(0) + +/*************************************************************** + * nifti_image_open + ***************************************************************/ +/*! znzFile nifti_image_open( char *hname, char *opts , nifti_image **nim) + \brief Read in NIFTI-1 or ANALYZE-7.5 file (pair) header information into a nifti_image struct. + + - The image data is not read from disk (it may be read later using + nifti_image_load(), for example). + - The image data will be stored in whatever data format the + input data is; no scaling will be applied. + - DT_BINARY data is not supported. + - nifti_image_free() can be used to delete the returned struct, + when you are done with it. + + \param hname filename of dataset .hdr or .nii file + \param opts options string for opening the header file + \param nim pointer to pointer to nifti_image struct + (this routine allocates the nifti_image struct) + \return file pointer (gzippable) to the file with the image data, + ready for reading. +
NULL if something fails badly. + \sa nifti_image_load, nifti_image_free + */ +znzFile nifti2_image_open(const char * hname, char * opts, nifti_image ** nim) +{ + znzFile fptr=NULL; + /* open the hdr and reading it in, but do not load the data */ + *nim = nifti_image_read(hname,0); + /* open the image file, ready for reading (compressed works for all reads) */ + if( ((*nim) == NULL) || ((*nim)->iname == NULL) || + ((*nim)->nbyper <= 0) || ((*nim)->nvox <= 0) ) + ERREX("bad header info") ; + + /* open image data file */ + fptr = znzopen( (*nim)->iname, opts, nifti_is_gzfile((*nim)->iname) ); + if( znz_isnull(fptr) ) ERREX("Can't open data file") ; + + return fptr; +} + + +/*----------------------------------------------------------------------*/ +/*! return an allocated and filled nifti_1_header struct + + Read the binary header from disk, and swap bytes if necessary. + + \return an allocated nifti_1_header struct, or NULL on failure + + \param hname name of file containing header + \param swapped if not NULL, return whether header bytes were swapped + \param check flag to check for invalid nifti_1_header + + \warning ASCII header type is not supported + + \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks +*//*--------------------------------------------------------------------*/ +nifti_1_header * nifti_read_n1_hdr(const char * hname, int *swapped, int check) +{ + nifti_1_header nhdr, * hptr; + znzFile fp; + int bytes, lswap; + char * hfile; + char fname[] = { "nifti_read_n1_hdr" }; + + /* determine file name to use for header */ + hfile = nifti_findhdrname(hname); + if( hfile == NULL ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"failed to find header file for", hname); + return NULL; + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d %s: found header filename '%s'\n",fname,hfile); + + fp = znzopen( hfile, "rb", nifti_is_gzfile(hfile) ); + if( znz_isnull(fp) ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"failed to open header file",hfile); + free(hfile); + return NULL; + } + + free(hfile); /* done with filename */ + + if( has_ascii_header(fp) == 1 ){ + znzclose( fp ); + if( g_opts.debug > 0 ) + LNI_FERR(fname,"ASCII header type not supported",hname); + return NULL; + } + + /* read the binary header */ + bytes = (int)znzread( &nhdr, 1, sizeof(nhdr), fp ); + znzclose( fp ); /* we are done with the file now */ + + if( bytes < (int)sizeof(nhdr) ){ + if( g_opts.debug > 0 ){ + LNI_FERR(fname,"bad binary header read for file", hname); + Rc_fprintf_stderr(" - read %d of %d bytes\n",bytes, (int)sizeof(nhdr)); + } + return NULL; + } + + /* now just decide on byte swapping */ + lswap = need_nhdr_swap(nhdr.dim[0], nhdr.sizeof_hdr); /* swap data flag */ + if( check && lswap < 0 ){ + LNI_FERR(fname,"bad nifti_1_header for file", hname); + return NULL; + } else if ( lswap < 0 ) { + lswap = 0; /* if swapping does not help, don't do it */ + if(g_opts.debug > 1) Rc_fprintf_stderr("-- swap failure, none applied\n"); + } + + if( lswap ) { + if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr); + swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ; + } + + if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr); + + if ( check && ! nifti_hdr1_looks_good(&nhdr) ){ + LNI_FERR(fname,"nifti_1_header looks bad for file", hname); + return NULL; + } + + /* all looks good, so allocate memory for and return the header */ + hptr = (nifti_1_header *)malloc(sizeof(nifti_1_header)); + if( ! hptr ){ + Rc_fprintf_stderr("** nifti_read_hdr: failed to alloc nifti_1_header\n"); + return NULL; + } + + if( swapped ) *swapped = lswap; /* only if they care */ + + memcpy(hptr, &nhdr, sizeof(nifti_1_header)); + + return hptr; +} + + +/*----------------------------------------------------------------------*/ +/*! return an allocated and filled nifti_2_header struct + + Read the binary header from disk, and swap bytes if necessary. + + \return an allocated nifti_2_header struct, or NULL on failure + + \param hname name of file containing header + \param swapped if not NULL, return whether header bytes were swapped + \param check flag to check for invalid nifti_2_header + + \warning ASCII header type is not supported + allow now, convert nim 2 hdr [02 Jan 2019 rickr] + + \sa nifti_read_header, nifti_read_n1_hdr, + nifti_image_read, nifti_image_read_bricks +*//*--------------------------------------------------------------------*/ +nifti_2_header * nifti_read_n2_hdr(const char * hname, int * swapped, + int check) +{ + nifti_2_header nhdr, * hptr; + nifti_image * nim=NULL; + znzFile fp; + int bytes, lswap, rv; + char * hfile; + char fname[] = { "nifti_read_n2_hdr" }; + + /* determine file name to use for header */ + hfile = nifti_findhdrname(hname); + if( hfile == NULL ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"failed to find header file for", hname); + return NULL; + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d %s: found N2 header filename '%s'\n",fname,hfile); + + fp = znzopen( hfile, "rb", nifti_is_gzfile(hfile) ); + if( znz_isnull(fp) ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"failed to open N2 header file",hfile); + free(hfile); + return NULL; + } + + free(hfile); /* done with filename */ + + /* ASCII is not part of standard, but allow */ + if( has_ascii_header(fp) == 1 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("++ reading ASCII header via NIFTI-2 in %s\n", hname); + nim = nifti_read_ascii_image(fp, hname, -1, 0); + znzclose(fp) ; + if( ! nim ) return NULL; + + hptr = (nifti_2_header *)malloc(sizeof(nifti_2_header)); + rv = nifti_convert_nim2n2hdr(nim, hptr); + free(nim); + + if( rv ) { free(hptr); return NULL; } + return hptr; + } + + /* read the binary header */ + bytes = (int)znzread( &nhdr, 1, sizeof(nhdr), fp ); + znzclose( fp ); /* we are done with the file now */ + + if( bytes < (int)sizeof(nhdr) ){ + if( g_opts.debug > 0 ){ + LNI_FERR(fname,"bad binary header read for N2 file", hname); + Rc_fprintf_stderr(" - read %d of %d bytes\n",bytes, (int)sizeof(nhdr)); + } + return NULL; + } + + /* now just decide on byte swapping */ + lswap = NIFTI2_NEEDS_SWAP(nhdr); + if( lswap ) { + if ( g_opts.debug > 3 ) disp_nifti_2_header("-d n2hdr pre-swap: ", &nhdr); + swap_nifti_header( &nhdr , 2 ); /* use explicit version */ + } + + if ( g_opts.debug > 2 ) disp_nifti_2_header("-d nhdr post-swap: ", &nhdr); + + if ( check && ! nifti_hdr2_looks_good(&nhdr) ){ + LNI_FERR(fname,"nifti_2_header looks bad for file", hname); + return NULL; + } + + /* all looks good, so allocate memory for and return the header */ + hptr = (nifti_2_header *)malloc(sizeof(nifti_2_header)); + if( ! hptr ){ + Rc_fprintf_stderr("** nifti2_read_hdr: failed to alloc nifti_2_header\n"); + return NULL; + } + + if( swapped ) *swapped = lswap; /* only if they care */ + + memcpy(hptr, &nhdr, sizeof(nifti_2_header)); + + return hptr; +} + + +/*----------------------------------------------------------------------*/ +/*! decide if this nifti_1_header structure looks reasonable + + Check dim[0], dim[1], sizeof_hdr, and datatype. + Check magic string for "n+1". + Maybe more tests will follow. + + \return 1 if the header seems valid, 0 otherwise + + \sa nifti_nim_is_valid, valid_nifti_extensions +*//*--------------------------------------------------------------------*/ +int nifti_hdr1_looks_good(const nifti_1_header * hdr) +{ + int ni_ver, c, errs = 0; + + /* check dim[0] and sizeof_hdr */ + if( need_nhdr_swap(hdr->dim[0], hdr->sizeof_hdr) < 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: bad hdr1 fields: dim0, sizeof_hdr = %d, %d\n", + hdr->dim[0], hdr->sizeof_hdr); + errs++; + } + + /* check the valid dimension sizes (maybe dim[0] is bad) */ + for( c = 1; c <= hdr->dim[0] && c <= 7; c++ ) + if( hdr->dim[c] <= 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: bad nhdr field: dim[%d] = %d\n", + c,hdr->dim[c]); + errs++; + } + + ni_ver = NIFTI_VERSION(*hdr); /* determine header type */ + + if( ni_ver > 0 ){ /* NIFTI */ + + if( ! nifti_datatype_is_valid(hdr->datatype, 1) ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** bad NIFTI datatype in hdr, %d\n",hdr->datatype); + errs++; + } + + } else { /* ANALYZE 7.5 */ + + if( g_opts.debug > 1 ) { /* maybe tell user it's an ANALYZE hdr */ + Rc_fprintf_stderr( + "-- nhdr magic field implies ANALYZE: magic = '%.4s' : ",hdr->magic); +#ifndef USING_R + print_hex_vals(hdr->magic, 4, stderr); +#endif + Rc_fputc_stderr('\n'); + } + + if( ! nifti_datatype_is_valid(hdr->datatype, 0) ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: bad ANALYZE datatype in hdr, %d\n", + hdr->datatype); + errs++; + } + } + + if( errs ) return 0; /* problems */ + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d nifti header looks good\n"); + + return 1; /* looks good */ +} + + +/*----------------------------------------------------------------------*/ +/*! check that sizeof() returns the proper size + * + * if ni_ver is valid (1 or 2 right now), check those sizes + * if ni_ver == 0, check all known sizes + * else whine and fail +*//*--------------------------------------------------------------------*/ +int nifti_valid_header_size(int ni_ver, int whine) +{ + int size, errs=0, checks=0; + + if ( !ni_ver || (ni_ver == 1) ) { + size = 348; + checks++; + if( sizeof(nifti_1_header) != size ) { + if( whine ) + Rc_fprintf_stderr( + "** warning: sizeof(nifti_1_header) = %d, expected %d\n", + (int)sizeof(nifti_1_header), size); + errs++; + } + } + + if ( !ni_ver || (ni_ver == 2) ) { + size = 540; + checks++; + if( sizeof(nifti_2_header) != size ) { + if( whine ) + Rc_fprintf_stderr( + "** warning: sizeof(nifti_2_header) = %d, expected %d\n", + (int)sizeof(nifti_2_header), size); + errs++; + } + } + + if ( ! checks ) { + Rc_fprintf_stderr("** nifti_valid_header_size: bad ni_ver = %d\n",ni_ver); + return 0; + } + + return errs ? 0 : 1; /* though !errs seems more fun */ +} + + +/*----------------------------------------------------------------------*/ +/*! decide if this nifti_2_header structure looks reasonable + * swapping should have already happened + + Check sizeof() and sizeof_hdr. + Check dim[0], dim[i], and datatype. + Check magic string for "n+2". + + \return 1 if the header seems valid, 0 otherwise + + \sa nifti_nim_is_valid, valid_nifti_extensions +*//*--------------------------------------------------------------------*/ +int nifti_hdr2_looks_good(const nifti_2_header * hdr) +{ + int ni_ver, c, errs = 0; + int64_t d0; + + if( !hdr ) { Rc_fprintf_stderr("** NIFTI n2hdr: hdr is NULL\n"); return 0; } + + /* for now, just warn if the header sizes are not right */ + if( g_opts.debug > 0 ) (void)nifti_valid_header_size(0, 1); + + if( hdr->sizeof_hdr != sizeof(nifti_2_header) ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI bad n2hdr: sizeof_hdr = %d\n", + hdr->sizeof_hdr); + errs++; + } + + /* check the valid dimension sizes (maybe dim[0] is bad) */ + d0 = hdr->dim[0]; + if( d0 < 0 || d0 > 7 ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: bad n2hdr: dim0 = %" PRId64 "\n", d0); + errs++; + } else { /* only check dims if d0 is okay */ + for( c = 1; c <= d0; c++ ) + if( hdr->dim[c] <= 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: bad nhdr field: dim[%d] = %" PRId64 "\n", + c, hdr->dim[c]); + errs++; + } + } + + ni_ver = NIFTI_VERSION(*hdr); /* note version */ + + if( ! nifti_datatype_is_valid(hdr->datatype, ni_ver) ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** bad %s NIFTI datatype in hdr, %d\n", + ni_ver ? "NIFTI" : "ANALYZE", hdr->datatype); + errs++; + } + + /* NIFTI_VERSION must return 2, or else sizes will not match */ + if( ni_ver != 2 || memcmp((hdr->magic+4), nifti2_magic+4, 4) ) { + if( g_opts.debug > 0 ) { + Rc_fprintf_stderr("-- header magic not NIFTI-2, magic = '%.4s' + ", + hdr->magic); +#ifndef USING_R + print_hex_vals(hdr->magic+4, 4, stderr); +#endif + Rc_fputc_stderr('\n'); + } + errs++; + } + + if( errs ) return 0; /* problems */ + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d nifti header looks good\n"); + + return 1; /* looks good */ +} + + +/*---------------------------------------------------------------------- + * check whether byte swapping is needed + * + * dim[0] should be in [0,7], and sizeof_hdr should be accurate + * + * \returns > 0 : needs swap + * 0 : does not need swap + * < 0 : error condition + *----------------------------------------------------------------------*/ +static int need_nhdr_swap( short dim0, int hdrsize ) +{ + short d0 = dim0; /* so we won't have to swap them on the stack */ + int hsize = hdrsize; + + if( d0 != 0 ){ /* then use it for the check */ + if( d0 > 0 && d0 <= 7 ) return 0; + + nifti_swap_2bytes(1, &d0); /* swap? */ + if( d0 > 0 && d0 <= 7 ) return 1; + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("** NIFTI: bad swapped d0 = %d, unswapped = ", d0); + nifti_swap_2bytes(1, &d0); /* swap? */ + Rc_fprintf_stderr("%d\n", d0); + } + + return -1; /* bad, naughty d0 */ + } + + /* dim[0] == 0 should not happen, but could, so try hdrsize */ + if( hsize == sizeof(nifti_1_header) ) return 0; + + nifti_swap_4bytes(1, &hsize); /* swap? */ + if( hsize == sizeof(nifti_1_header) ) return 1; + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("** NIFTI: bad swapped hsize = %d, unswapped = ", hsize); + nifti_swap_4bytes(1, &hsize); /* swap? */ + Rc_fprintf_stderr("%d\n", hsize); + } + + return -2; /* bad, naughty hsize */ +} + + +/* use macro LNI_FILE_ERROR instead of ERREX() +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_image_read(%s): %s\n", \ + (hname != NULL) ? hname : "(null)" , (msg) ) ; \ + return NULL ; } while(0) +*/ + + +/*************************************************************** + * nifti_read_header + ***************************************************************/ +/*! \brief Read and return a nifti header, along with the found type + + - The data buffer will be byteswapped if necessary. + - The data buffer will not be scaled. + - The data buffer is allocated with calloc(). + + \param hname filename of the nifti dataset + \param nver : + \return A void pointer, which should be cast based on the returned nver. + It points to an allocated header struct. +*/ +void * nifti2_read_header( const char *hname, int *nver, int check ) +{ + nifti_1_header n1hdr; + nifti_2_header n2hdr; + znzFile fp; + void * hresult = NULL; + int64_t remain, h1size=0, h2size=0; + char fname[] = { "nifti_read_header" }; + char *hfile=NULL, *posn; + int ii, ni_ver; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("-d reading header from '%s'",hname); + Rc_fprintf_stderr(", HAVE_ZLIB = %d\n", nifti_compiled_with_zlib()); + } + + /**- determine filename to use for header */ + hfile = nifti_findhdrname(hname); + if( hfile == NULL ){ + if(g_opts.debug > 0) + LNI_FERR(fname,"failed to find header file for", hname); + return NULL; /* check return */ + } else if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d %s: found header filename '%s'\n",fname,hfile); + + h1size = sizeof(nifti_1_header); + h2size = sizeof(nifti_2_header); + + /**- open file, separate reading of header, extensions and data */ + fp = znzopen(hfile, "rb", nifti_is_gzfile(hfile)); + if( znz_isnull(fp) ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"failed to open header file",hfile); + free(hfile); + return NULL; + } + + /**- first try to read dataset as ASCII (and return NIFTI2 if so) */ + if( has_ascii_header( fp ) ) { + znzclose(fp) ; + free(hfile); + if( nver ) *nver = 2; + return nifti_read_n2_hdr(hname, NULL, check); + } + + /**- next read into nifti_1_header and determine nifti type */ + ii = (int)znzread(&n1hdr, 1, h1size, fp); + + if( ii < (int)h1size ){ /* failure? */ + if( g_opts.debug > 0 ){ + LNI_FERR(fname,"bad binary header read for file", hfile); + Rc_fprintf_stderr(" - read %d of %d bytes\n",ii, (int)h1size); + } + znzclose(fp) ; + free(hfile); + return NULL; + } + + /* find out what type of header we have */ + ni_ver = nifti_header_version((char *)&n1hdr, h1size); + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- %s: NIFTI version = %d\n", fname, ni_ver); + + /* maybe set return NIFTI version */ + if( nver ) *nver = ni_ver; + + /* if NIFTI-2, copy and finish reading header */ + if ( ni_ver == 2 ) { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- %s: copying and filling NIFTI-2 header...\n",fname); + memcpy(&n2hdr, &n1hdr, h1size); /* copy first part */ + remain = h2size - h1size; + posn = (char *)&n2hdr + h1size; + ii = (int)znzread(posn, 1, remain, fp); /* read remaining part */ + if( ii < (int)remain) { + LNI_FERR(fname,"short NIFTI-2 header read for file", hfile); + znzclose(fp); free(hfile); return NULL; + } + } + + /* clean up */ + znzclose(fp); + free(hfile); + + /* allocate header space and return */ + if( ni_ver == 0 || ni_ver == 1 ) { + hresult = malloc(h1size); + if( ! hresult ) { + LNI_FERR(fname,"failed to alloc NIFTI-1 header for file", hname); + return NULL; + } + memcpy(hresult, (void *)&n1hdr, h1size); + + if ( check && ! nifti_hdr1_looks_good(static_cast(hresult)) ){ + LNI_FERR(fname,"nifti_1_header looks bad for file", hname); + return hresult; + } + } else if ( ni_ver == 2 ) { + hresult = malloc(h2size); + if( ! hresult ) { + LNI_FERR(fname,"failed to alloc NIFTI-2 header for file", hname); + return NULL; + } + memcpy(hresult, &n2hdr, h2size); + + if ( check && ! nifti_hdr2_looks_good(static_cast(hresult)) ){ + LNI_FERR(fname,"nifti_2_header looks bad for file", hname); + return hresult; + } + } else { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** %s: bad nifti header version %d\n", hname, ni_ver); + + /* return a nifti-1 header anyway */ + hresult = malloc(h1size); + if( ! hresult ) { + LNI_FERR(fname,"failed to alloc NIFTI-?? header for file", hname); + return NULL; + } + memcpy(hresult, (void *)&n1hdr, h1size); + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-- returning NIFTI-%d header in %s\n", ni_ver, hname); + + return hresult; +} + + +/*************************************************************** + * nifti_image_read + ***************************************************************/ +/*! \brief Read a nifti header and optionally the data, creating a nifti_image. + + - The data buffer will be byteswapped if necessary. + - The data buffer will not be scaled. + - The data buffer is allocated with calloc(). + + \param hname filename of the nifti dataset + \param read_data Flag, true=read data blob, false=don't read blob. + \return A pointer to the nifti_image data structure. + + \sa nifti_image_free, nifti_free_extensions, nifti_image_read_bricks +*/ +nifti_image *nifti2_image_read( const char *hname , int read_data ) +{ + nifti_1_header n1hdr; + nifti_2_header n2hdr; + nifti_image *nim; + znzFile fp; + int rv, ii, ni_ver, onefile=0; + int64_t filesize, remain, h1size=0, h2size=0; + char fname[] = { "nifti_image_read" }; + char *hfile=NULL, *posn; + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("-d image_read from '%s', read_data = %d",hname,read_data); + Rc_fprintf_stderr(", HAVE_ZLIB = %d\n", nifti_compiled_with_zlib()); + } + + /**- determine filename to use for header */ + hfile = nifti_findhdrname(hname); + if( hfile == NULL ){ + if(g_opts.debug > 0) + LNI_FERR(fname,"failed to find header file for", hname); + return NULL; /* check return */ + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d %s: found header filename '%s'\n",fname,hfile); + + if( nifti_is_gzfile(hfile) ) filesize = -1; /* unknown */ + else filesize = nifti_get_filesize(hfile); + + /**- open file, separate reading of header, extensions and data */ + fp = znzopen(hfile, "rb", nifti_is_gzfile(hfile)); + if( znz_isnull(fp) ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"failed to open header file",hfile); + free(hfile); + return NULL; + } + + /**- first try to read dataset as ASCII (and return if so) */ + rv = has_ascii_header( fp ); + if( rv < 0 ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"short header read",hfile); + znzclose( fp ); + free(hfile); + return NULL; + } + else if ( rv == 1 ) { /* process special file type */ + nim = nifti_read_ascii_image( fp, hfile, filesize, read_data ); + znzclose(fp); + free(hfile); + return nim; + } + + h1size = sizeof(nifti_1_header); + h2size = sizeof(nifti_2_header); + + /**- next read into nifti_1_header and determine nifti type */ + ii = (int)znzread(&n1hdr, 1, h1size, fp); + + if( ii < (int)h1size ){ /* failure? */ + if( g_opts.debug > 0 ){ + LNI_FERR(fname,"bad binary header read for file", hfile); + Rc_fprintf_stderr(" - read %d of %d bytes\n",ii, (int)h1size); + } + znzclose(fp) ; + free(hfile); + return NULL; + } + + /* find out what type of header we have */ + ni_ver = nifti_header_version((char *)&n1hdr, h1size); + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- %s: NIFTI version = %d\n", fname, ni_ver); + + if( ni_ver == 0 || ni_ver == 1 ) { + nim = nifti_convert_n1hdr2nim(n1hdr,hfile); + onefile = NIFTI_ONEFILE(n1hdr); + } else if ( ni_ver == 2 ) { + /* fill nifti-2 header and convert */ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- %s: copying and filling NIFTI-2 header...\n",fname); + memcpy(&n2hdr, &n1hdr, h1size); /* copy first part */ + remain = h2size - h1size; + posn = (char *)&n2hdr + h1size; + ii = (int)znzread(posn, 1, remain, fp); /* read remaining part */ + if( ii < (int)remain) { + LNI_FERR(fname,"short NIFTI-2 header read for file", hfile); + znzclose(fp); free(hfile); return NULL; + } + nim = nifti_convert_n2hdr2nim(n2hdr,hfile); + onefile = NIFTI_ONEFILE(n2hdr); + } else { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** %s: bad nifti im header version %d\n",fname,ni_ver); + znzclose(fp); free(hfile); return NULL; + } + + if( nim == NULL ){ + znzclose( fp ) ; /* close the file */ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"cannot create nifti image from header",hfile); + free(hfile); /* had to save this for debug message */ + return NULL; + } + + if( g_opts.debug > 3 ){ + Rc_fprintf_stderr("+d nifti_image_read(), have nifti image:\n"); + if( g_opts.debug > 2 ) nifti_image_infodump(nim); + } + + /**- check for extensions (any errors here means no extensions) */ + if ( onefile ) remain = nim->iname_offset; + else remain = filesize; + + if ( ni_ver <= 1 ) remain -= h1size; + else remain -= h2size; + + (void)nifti_read_extensions(nim, fp, remain); + + znzclose( fp ) ; /* close the file */ + free(hfile); + + if ( g_opts.alter_cifti && nifti_looks_like_cifti(nim) ) + nifti_alter_cifti_dims(nim); + + /**- read the data if desired, then bug out */ + if( read_data ){ + if( nifti_image_load( nim ) < 0 ){ + nifti_image_free(nim); /* take ball, go home. */ + return NULL; + } + } + else nim->data = NULL ; + + return nim ; +} + +nifti_image *nifti2_image_mem_read( const uint8_t *data , const size_t size, int gz, const size_t estimated_size ) +{ + nifti_1_header n1hdr; + nifti_2_header n2hdr; + nifti_image *nim; + znzFile fp; + int rv, ii, ni_ver, onefile=0; + int64_t filesize, remain, h1size=0, h2size=0; + char fname[] = { "nifti_image_read" }; + char *posn; + + // if( g_opts.debug > 1 ){ + // Rc_fprintf_stderr("-d image_read from '%s', read_data = %d",hname,read_data); + // Rc_fprintf_stderr(", HAVE_ZLIB = %d\n", nifti_compiled_with_zlib()); + // } + + /**- determine filename to use for header */ + // hfile = nifti_findhdrname(hname); + // if (hfile == NULL) { + // if(g_opts.debug > 0) + // LNI_FERR(fname,"failed to find header file for", hname); + // return NULL; /* check return */ + // } else if( g_opts.debug > 1 ) + // Rc_fprintf_stderr("-d %s: found header filename '%s'\n",fname,hfile); + + if( gz ) filesize = -1; /* unknown */ + else filesize = size; + + /**- open file, separate reading of header, extensions and data */ + fp = znzmemopen(NULL, data, size, "rb", gz, estimated_size); + if( znz_isnull(fp) ){ + // if( g_opts.debug > 0 ) LNI_FERR(fname,"failed to open header file",hfile); + // free(hfile); + return NULL; + } + + /**- first try to read dataset as ASCII (and return if so) */ + rv = has_ascii_header( fp ); + if( rv < 0 ){ + // if( g_opts.debug > 0 ) LNI_FERR(fname,"short header read",hfile); + znzclose( fp ); + // free(hfile); + return NULL; + } + else if ( rv == 1 ) { /* process special file type */ + nim = nifti2_mem_read_ascii_image( fp, filesize, gz ); + znzclose(fp); + // free(hfile); + return nim; + } + + h1size = sizeof(nifti_1_header); + h2size = sizeof(nifti_2_header); + + /**- next read into nifti_1_header and determine nifti type */ + ii = (int)znzread(&n1hdr, 1, h1size, fp); + + if( ii < (int)h1size ){ /* failure? */ + // if( g_opts.debug > 0 ){ + // LNI_FERR(fname,"bad binary header read for file", hfile); + // Rc_fprintf_stderr(" - read %d of %d bytes\n",ii, (int)h1size); + // } + znzclose(fp) ; + // free(hfile); + return NULL; + } + + /* find out what type of header we have */ + ni_ver = nifti_header_version((char *)&n1hdr, h1size); + // if( g_opts.debug > 2 ) + // Rc_fprintf_stderr("-- %s: NIFTI version = %d\n", fname, ni_ver); + + if( ni_ver == 0 || ni_ver == 1 ) { + nim = nifti_convert_n1hdr2nim(n1hdr , gz ? "_.gz" : "_.nii"); + onefile = NIFTI_ONEFILE(n1hdr); + } else if ( ni_ver == 2 ) { + /* fill nifti-2 header and convert */ + // if( g_opts.debug > 2 ) + // Rc_fprintf_stderr("-- %s: copying and filling NIFTI-2 header...\n",fname); + memcpy(&n2hdr, &n1hdr, h1size); /* copy first part */ + remain = h2size - h1size; + posn = (char *)&n2hdr + h1size; + ii = (int)znzread(posn, 1, remain, fp); /* read remaining part */ + if( ii < (int)remain) { + // LNI_FERR(fname,"short NIFTI-2 header read for file", hfile); + znzclose(fp); + // free(hfile); + return NULL; + } + nim = nifti_convert_n2hdr2nim(n2hdr , gz ? "_.gz" : "_.nii"); + onefile = NIFTI_ONEFILE(n2hdr); + } else { + // if( g_opts.debug > 0 ) + // Rc_fprintf_stderr("** %s: bad nifti im header version %d\n",fname,ni_ver); + znzclose(fp); + // free(hfile); + return NULL; + } + + if( nim == NULL ){ + znzclose( fp ) ; /* close the file */ + // if( g_opts.debug > 0 ) + // LNI_FERR(fname,"cannot create nifti image from header",hfile); + // free(hfile); /* had to save this for debug message */ + return NULL; + } + + if( g_opts.debug > 3 ){ + Rc_fprintf_stderr("+d nifti_image_read(), have nifti image:\n"); + if( g_opts.debug > 2 ) nifti_image_infodump(nim); + } + + /**- check for extensions (any errors here means no extensions) */ + if ( onefile ) remain = nim->iname_offset; + else remain = filesize; + + if ( ni_ver <= 1 ) remain -= h1size; + else remain -= h2size; + + (void)nifti_read_extensions(nim, fp, remain); + + // znzclose( fp ) ; /* close the file */ + // free(hfile); + + if ( g_opts.alter_cifti && nifti_looks_like_cifti(nim) ) + nifti_alter_cifti_dims(nim); + + /**- read the data if desired, then bug out */ + // if( read_data ){ + if( nifti2_image_mem_load( fp, nim, data, size, gz, estimated_size ) < 0 ) { + znzclose(fp); + nifti_image_free(nim); /* take ball, go home. */ + return NULL; + } + znzclose(fp); + // } + // else nim->data = NULL ; + + return nim ; +} + +/*---------------------------------------------------------------------- + # return the index of the first occurrence of the given ecode, else -1 + *----------------------------------------------------------------------*/ +static int nifti_ext_type_index(nifti_image * nim, int ecode) +{ + int ind; + + if ( !nim || ecode < 0 ) return -1; + + for( ind = 0; ind < nim->num_ext; ind++ ) + if( nim->ext_list[ind].ecode == ecode ) + return ind; + + return -1; +} + +/*---------------------------------------------------------------------- + *! does this dataset look like CIFTI? + * + * check dimensions and extension ecodes for CIFTI + * + * should have - nx=ny=nz=nt=1, nu,nv>1, nw optional + * - CIFTI extension + *----------------------------------------------------------------------*/ +int nifti_looks_like_cifti(nifti_image * nim) +{ + if( ! nim ) return 0; + + if( nifti_ext_type_index(nim, NIFTI_ECODE_CIFTI) < 0 ) return 0; + + if( nim->nx > 1 || nim->ny > 1 || nim->nz > 1 || nim->nt > 1 ) return 0; + + if( nim->nu > 1 || nim->nv > 1 ) return 1; /* looks like it */ + + return 0; +} + +/*---------------------------------------------------------------------- + *! alter the dims[] from CIFTI style + * + * convert nu -> nx, nv -> nt/nu, nw -> nv + *----------------------------------------------------------------------*/ +int nifti_alter_cifti_dims(nifti_image * nim) +{ + if( ! nifti_looks_like_cifti(nim) ) return 0; + + /* the main effect, move position axis to x ... */ + if( nim->nu > 1 || nim->dim[5] ) { + nim->nx = nim->nu; + nim->nu = 1; + + nim->dim[1] = nim->dim[5]; + nim->dim[5] = 1; + } + + return 0; +} + + +/*---------------------------------------------------------------------- + * has_ascii_header - see if the NIFTI header is an ASCII format + * + * If the file starts with the ASCII string " 1 ) + Rc_fprintf_stderr("-d %s: have ASCII NIFTI file of size %d\n",fname,slen); + + if( slen > 65530 ) slen = 65530 ; + sbuf = (char *)calloc(sizeof(char),slen+1) ; + if( !sbuf ){ + Rc_fprintf_stderr("** %s: failed to alloc %d bytes for sbuf",lfunc,65530); + return NULL; + } + znzread( sbuf , 1 , slen , fp ) ; + nim = nifti_image_from_ascii( sbuf, &txt_size ) ; free( sbuf ) ; + if( nim == NULL ){ + LNI_FERR(lfunc,"failed nifti_image_from_ascii()",fname); + return NULL; + } + nim->nifti_type = NIFTI_FTYPE_ASCII ; + + /* compute remaining space for extensions */ + remain = flen - txt_size - (int)nifti_get_volsize(nim); + if( remain > 4 ){ + /* read extensions (reposition file pointer, first) */ + znzseek(fp, txt_size, SEEK_SET); + (void) nifti_read_extensions(nim, fp, (int64_t)remain); + } + + nim->iname_offset = -1 ; /* check from the end of the file */ + + if( read_data ) rv = nifti_image_load( nim ) ; + else nim->data = NULL ; + + /* check for nifti_image_load() failure, maybe bail out */ + if( read_data && rv != 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d failed image_load, free nifti image struct\n"); + free(nim); + return NULL; + } + + return nim ; +} + +nifti_image* nifti2_mem_read_ascii_image(znzFile fp, int flen, int compressed) +{ + nifti_image * nim; + int slen, txt_size, remain, rv = 0; + char * sbuf, lfunc[25] = { "nifti_read_ascii_image" }; + + if( compressed ){ + // LNI_FERR(lfunc,"compression not supported for file type NIFTI_FTYPE_ASCII", + // fname); + return NULL; + } + slen = flen; /* slen will be our buffer length */ + // if( slen <= 0 ) slen = nifti_get_filesize(fname); + + // if( g_opts.debug > 1 ) + // Rc_fprintf_stderr("-d %s: have ASCII NIFTI file of size %d\n",fname,slen); + + if( slen > 65530 ) slen = 65530 ; + sbuf = (char *)calloc(sizeof(char),slen+1) ; + if( !sbuf ){ + Rc_fprintf_stderr("** %s: failed to alloc %d bytes for sbuf",lfunc,65530); + return NULL; + } + znzread( sbuf , 1 , slen , fp ) ; + nim = nifti_image_from_ascii( sbuf, &txt_size ) ; free( sbuf ) ; + if( nim == NULL ){ + // LNI_FERR(lfunc,"failed nifti_image_from_ascii()",fname); + return NULL; + } + nim->nifti_type = NIFTI_FTYPE_ASCII ; + + /* compute remaining space for extensions */ + remain = flen - txt_size - (int)nifti_get_volsize(nim); + if( remain > 4 ){ + /* read extensions (reposition file pointer, first) */ + znzseek(fp, txt_size, SEEK_SET); + (void) nifti_read_extensions(nim, fp, (int64_t)remain); + } + + nim->iname_offset = -1 ; /* check from the end of the file */ + + if( 1 ) rv = nifti_image_load( nim ) ; + else nim->data = NULL ; + + /* check for nifti_image_load() failure, maybe bail out */ + if( 1 && rv != 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d failed image_load, free nifti image struct\n"); + free(nim); + return NULL; + } + + return nim ; +} + + +/*---------------------------------------------------------------------- + * Read the extensions into the nifti_image struct 08 Dec 2004 [rickr] + * + * This function is called just after the header struct is read in, and + * it is assumed the file pointer has not moved. The value in remain + * is assumed to be accurate, reflecting the bytes of space for potential + * extensions. + * + * return the number of extensions read in, or < 0 on error + *----------------------------------------------------------------------*/ +static int nifti_read_extensions( nifti_image *nim, znzFile fp, int64_t remain ) +{ + nifti1_extender extdr; /* defines extension existence */ + nifti1_extension extn; /* single extension to process */ + nifti1_extension * Elist; /* list of processed extensions */ + int64_t posn, count; + + /* rcr n2 - add and use nifti2_extension type? */ + + if( !nim || znz_isnull(fp) ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_read_extensions: bad inputs (%p,%p)\n", + (void *)nim, (void *)fp); + return -1; + } + + posn = znztell(fp); + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nre: posn=%" PRId64 ", offset=%" PRId64 + ", type=%d, remain=%" PRId64 "\n", + posn, nim->iname_offset, nim->nifti_type, remain); + + if( remain < 16 ){ + if( g_opts.debug > 2 ){ + if( g_opts.skip_blank_ext ) + Rc_fprintf_stderr("-d no extender in '%s' is okay, as " + "skip_blank_ext is set\n",nim->fname); + else + Rc_fprintf_stderr("-d remain=%" PRId64 ", no space for extensions\n", + remain); + } + return 0; + } + + count = znzread( extdr.extension, 1, 4, fp ); /* get extender */ + + if( count < 4 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d file '%s' is too short for an extender\n", + nim->fname); + return 0; + } + + if( extdr.extension[0] != 1 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d extender[0] (%d) shows no extensions for '%s'\n", + extdr.extension[0], nim->fname); + return 0; + } + + remain -= 4; + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d found valid 4-byte extender, remain = %" PRId64 "\n", + remain); + + /* so we expect extensions, but have no idea of how many there may be */ + + count = 0; + Elist = NULL; + while (nifti_read_next_extension(&extn, nim, remain, fp) > 0) + { + if( nifti_add_exten_to_list(&extn, &Elist, (int)count+1) < 0 ){ + free(Elist); + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: failed adding ext %" PRId64 " to list\n", + count); + return -1; + } + + /* we have a new extension */ + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("+d found extension #%" PRId64 + ", code = 0x%x, size = %d\n", + count, extn.ecode, extn.esize); + if( extn.ecode == NIFTI_ECODE_AFNI && g_opts.debug > 2 ) /* ~XML */ + Rc_fprintf_stderr(" AFNI extension: %.*s\n", + extn.esize-8,extn.edata); + else if( extn.ecode == NIFTI_ECODE_COMMENT && g_opts.debug > 2 ) + Rc_fprintf_stderr(" COMMENT extension: %.*s\n", /* TEXT */ + extn.esize-8,extn.edata); + } + remain -= extn.esize; + count++; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d found %" PRId64 " extension(s)\n", count); + /* rcr n2 - allow int64_t num ext? */ + nim->num_ext = (int)count; + nim->ext_list = Elist; + + return count; +} + + +/*----------------------------------------------------------------------*/ +/*! nifti_add_extension - add an extension, with a copy of the data + + Add an extension to the nim->ext_list array. + Fill this extension with a copy of the data, noting the + length and extension code. + + \param nim - nifti_image to add extension to + \param data - raw extension data + \param length - length of raw extension data + \param ecode - extension code + + \sa extension codes NIFTI_ECODE_* in nifti1_io.h + \sa nifti_free_extensions, valid_nifti_extensions, nifti_copy_extensions + + \return 0 on success, -1 on error (and free the entire list) +*//*--------------------------------------------------------------------*/ +int nifti2_add_extension(nifti_image *nim, const char * data, int len, int ecode) +{ + nifti1_extension ext; + + /* error are printed in functions */ + if( nifti_fill_extension(&ext, data, len, ecode) ) { free(ext.edata); return -1; } + if( nifti_add_exten_to_list(&ext, &nim->ext_list, nim->num_ext+1)) { free(ext.edata); return -1; } + + nim->num_ext++; /* success, so increment */ + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/* nifti_add_exten_to_list - add a new nifti1_extension to the list + + We will append via "malloc, copy and free", because on an error, + the list will revert to the previous one (sorry realloc(), only + quality dolphins get to become part of St@rk!st brand tunafish). + + return 0 on success, -1 on error (and free the entire list) +*//*--------------------------------------------------------------------*/ +static int nifti_add_exten_to_list( nifti1_extension * new_ext, + nifti1_extension ** list, int new_length ) +{ + nifti1_extension * tmplist; + + tmplist = *list; + *list = (nifti1_extension *)malloc(new_length * sizeof(nifti1_extension)); + + /* check for failure first */ + if( ! *list ){ + Rc_fprintf_stderr("** NIFTI: failed to alloc %d ext structs (%d bytes)\n", + new_length, new_length*(int)sizeof(nifti1_extension)); + if( !tmplist ) return -1; /* no old list to lose */ + + *list = tmplist; /* reset list to old one */ + return -1; + } + + /* if an old list exists, copy the pointers and free the list */ + if( tmplist ){ + memcpy(*list, tmplist, (new_length-1)*sizeof(nifti1_extension)); + free(tmplist); + } + + /* for some reason, I just don't like struct copy... */ + (*list)[new_length-1].esize = new_ext->esize; + (*list)[new_length-1].ecode = new_ext->ecode; + (*list)[new_length-1].edata = new_ext->edata; + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d allocated and appended extension #%d to list\n", + new_length); + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/* nifti_fill_extension - given data and length, fill an extension struct + + Allocate memory for data, copy data, set the size and code. + + return 0 on success, -1 on error (and free the entire list) +*//*--------------------------------------------------------------------*/ +static int nifti_fill_extension( nifti1_extension *ext, const char * data, + int len, int ecode) +{ + int esize; + + if( !ext || !data || len < 0 ){ + Rc_fprintf_stderr("** NIFTI fill_ext: bad params (%p,%p,%d)\n", + (void *)ext, (void *)data, len); + return -1; + } else if( ! nifti_is_valid_ecode(ecode) ){ + Rc_fprintf_stderr("** NIFTI fill_ext: invalid ecode %d\n", ecode); + /* should not be fatal 29 Apr 2015 [rickr] */ + } + + /* compute esize, first : len+8, and take ceiling up to a mult of 16 */ + esize = len+8; + if( esize & 0xf ) esize = (esize + 0xf) & ~0xf; + ext->esize = esize; + + /* allocate esize-8 (maybe more than len), using calloc for fill */ + ext->edata = (char *)calloc(esize-8, sizeof(char)); + if( !ext->edata ){ + Rc_fprintf_stderr("** NIFTI NFE: failed to alloc %d bytes for extension\n", + len); + return -1; + } + + memcpy(ext->edata, data, len); /* copy the data, using len */ + ext->ecode = ecode; /* set the ecode */ + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d alloc %d bytes for ext len %d, ecode %d, esize %d\n", + esize-8, len, ecode, esize); + + return 0; +} + + +/*---------------------------------------------------------------------- + * nifti_read_next_extension - read a single extension from the file + * + * return (>= 0 is okay): + * + * success : esize + * no extension : 0 + * error : -1 + *----------------------------------------------------------------------*/ +static int nifti_read_next_extension( nifti1_extension * nex, nifti_image *nim, + int remain, znzFile fp ) +{ + int swap = nim->byteorder != nifti_short_order(); + int count, size, code = -1; + + /* first clear nex */ + nex->esize = nex->ecode = 0; + nex->edata = NULL; + + if( remain < 16 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d only %d bytes remain, so no extension\n", remain); + return 0; + } + + /* must start with 4-byte size and code */ + count = (int)znzread( &size, 4, 1, fp ); + if( count == 1 ) count += (int)znzread( &code, 4, 1, fp ); + + if( count != 2 || code == -1 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d current extension read failed\n"); + znzseek(fp, -4*count, SEEK_CUR); /* back up past any read */ + return 0; /* no extension, no error condition */ + } + + if( swap ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d pre-swap exts: code %d, size %d\n", code, size); + + nifti_swap_4bytes(1, &size); + nifti_swap_4bytes(1, &code); + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d potential extension: code %d, size %d\n", code, size); + + if( !nifti_check_extension(nim, size, code, remain) ){ + if( znzseek(fp, -8, SEEK_CUR) < 0 ){ /* back up past any read */ + Rc_fprintf_stderr("** NIFTI: failure to back out of extension read!\n"); + return -1; + } + return 0; + } + + /* now get the actual data */ + nex->esize = size; + nex->ecode = code; + + size -= 8; /* subtract space for size and code in extension */ + nex->edata = (char *)malloc(size * sizeof(char)); + if( !nex->edata ){ + Rc_fprintf_stderr("** NIFTI: failed to allocate %d bytes for extension\n", + size); + return -1; + } + + count = (int)znzread(nex->edata, 1, size, fp); + if( count < size ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("-d read only %d (of %d) bytes for extension\n", + count, size); + free(nex->edata); + nex->edata = NULL; + return -1; + } + + /* success! */ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d successfully read extension, code %d, size %d\n", + nex->ecode, nex->esize); + + return nex->esize; +} + + +/*----------------------------------------------------------------------*/ +/*! for each extension, check code, size and data pointer +*//*--------------------------------------------------------------------*/ +int valid_nifti2_extensions(const nifti_image * nim) +{ + nifti1_extension * ext; + int c, errs; + + if( nim->num_ext <= 0 || nim->ext_list == NULL ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d empty extension list\n"); + return 0; + } + + /* for each extension, check code, size and data pointer */ + ext = nim->ext_list; + errs = 0; + for ( c = 0; c < nim->num_ext; c++ ){ + if( ! nifti_is_valid_ecode(ext->ecode) ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d ext %d, invalid code %d\n", c, ext->ecode); + /* should not be fatal 29 Apr 2015 [rickr] */ + } + + if( ext->esize <= 0 ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d ext %d, bad size = %d\n", c, ext->esize); + errs++; + } else if( ext->esize & 0xf ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d ext %d, size %d not multiple of 16\n", + c, ext->esize); + errs++; + } + + if( ext->edata == NULL ){ + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d ext %d, missing data\n", c); + errs++; + } + + ext++; + } + + if( errs > 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("-d had %d extension errors, none will be written\n", + errs); + return 0; + } + + /* if we're here, we're good */ + return 1; +} + +/*----------------------------------------------------------------------*/ +/*! determine NIFTI version from buffer (check sizeof_hdr and magic) + + \return -1 on error, else NIFTI version + *//*--------------------------------------------------------------------*/ +int nifti_header_version(const char * buf, size_t nbytes){ + nifti_1_header *n1p = (nifti_1_header *)buf; + nifti_2_header *n2p = (nifti_2_header *)buf; + char fname[] = { "nifti_header_version" }; + int sizeof_hdr, sver, nver; + + if( !buf ) { + if(g_opts.debug > 0) + Rc_fprintf_stderr("** %s: have NULL buffer pointer", fname); + return -1; + } + + if( nbytes < sizeof(nifti_1_header) ) { + if(g_opts.debug > 0) + Rc_fprintf_stderr("** %s: nbytes=%zu, too small for test", fname, nbytes); + return -1; + } + + /* try to determine the version based on sizeof_hdr */ + sver = -1; + sizeof_hdr = n1p->sizeof_hdr; + if ( sizeof_hdr == (int)sizeof(nifti_1_header) ) sver = 1; + else if( sizeof_hdr == (int)sizeof(nifti_2_header) ) sver = 2; + else { /* try swapping */ + nifti_swap_4bytes(1, &sizeof_hdr); + if ( sizeof_hdr == (int)sizeof(nifti_1_header) ) sver = 1; + else if( sizeof_hdr == (int)sizeof(nifti_2_header) ) sver = 2; + } + + /* and check magic field */ + if ( sver == 1 ) nver = NIFTI_VERSION(*n1p); + else if ( sver == 2 ) nver = NIFTI_VERSION(*n2p); + else nver = -1; + + /* now compare and return */ + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-- %s: size ver = %d, ni ver = %d\n", fname, sver, nver); + + if( sver == 1 ) { + nver = NIFTI_VERSION(*n1p); + if( nver == 0 ) return 0; /* ANALYZE */ + if( nver == 1 ) return 1; /* NIFTI-1 */ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** %s: bad NIFTI-1 magic= %.4s", fname, n1p->magic); + return -1; + } else if ( sver == 2 ) { + nver = NIFTI_VERSION(*n2p); + if( nver == 2 ) return 2; /* NIFTI-2 */ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("** %s: bad NIFTI-2 magic4= %.4s", fname, n2p->magic); + return -1; + } + + /* failure */ + + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** %s: bad sizeof_hdr = %d\n", fname, n1p->sizeof_hdr); + + return -1; +} + + + +/*----------------------------------------------------------------------*/ +/*! check whether the extension code is valid + + \return 1 if valid, 0 otherwise +*//*--------------------------------------------------------------------*/ +int nifti_is_valid_ecode( int ecode ) +{ + if( ecode < NIFTI_ECODE_IGNORE || /* minimum code number (0) */ + ecode > NIFTI_MAX_ECODE || /* maximum code number */ + ecode & 1 ) /* cannot be odd */ + return 0; + + return 1; +} + + +/*---------------------------------------------------------------------- + * check for valid size and code, as well as can be done + *----------------------------------------------------------------------*/ +static int nifti_check_extension(nifti_image *nim, int size, int code, int rem) +{ + /* check for bad code before bad size */ + if( ! nifti_is_valid_ecode(code) ) { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d invalid extension code %d\n",code); + /* should not be fatal 29 Apr 2015 [rickr] */ + } + + if( size < 16 ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d ext size %d, no extension\n",size); + return 0; + } + + if( size > rem ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d ext size %d, space %d, no extension\n", size, rem); + return 0; + } + + if( size & 0xf ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nifti extension size %d not multiple of 16\n",size); + return 0; + } + + if( nim->nifti_type == NIFTI_FTYPE_ASCII && size > LNI_MAX_NIA_EXT_LEN ){ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d NVE, bad nifti_type 3 size %d\n", size); + return 0; + } + + return 1; +} + + +/*---------------------------------------------------------------------- + * nifti_image_load_prep - prepare to read data + * + * Check nifti_image fields, open the file and seek to the appropriate + * offset for reading. + * + * return NULL on failure + *----------------------------------------------------------------------*/ +static znzFile nifti_image_load_prep( nifti_image *nim ) +{ + /* set up data space, open data file and seek, then call nifti_read_buffer */ + int64_t ntot , ii , ioff; + znzFile fp; + char *tmpimgname; + char fname[] = { "nifti_image_load_prep" }; + + /**- perform sanity checks */ + if( nim == NULL || nim->iname == NULL || + nim->nbyper <= 0 || nim->nvox <= 0 ) + { + if ( g_opts.debug > 0 ){ + if( !nim ) Rc_fprintf_stderr("** ERROR: N_image_load: no nifti image\n"); + else Rc_fprintf_stderr("** ERROR: nifti_image_load: bad params (%p,%d," + "%" PRId64 ")\n", nim->iname, nim->nbyper, nim->nvox); + } + return NULL; + } + + ntot = nifti_get_volsize(nim) ; /* total bytes to read */ + + /**- open image data file */ + + tmpimgname = nifti_findimgname(nim->iname , nim->nifti_type); + if( tmpimgname == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: no image file found for '%s'\n",nim->iname); + return NULL; + } + + fp = znzopen(tmpimgname, "rb", nifti_is_gzfile(tmpimgname)); + if (znz_isnull(fp)){ + if(g_opts.debug > 0) LNI_FERR(fname,"cannot open data file",tmpimgname); + free(tmpimgname); + return NULL; /* bad open? */ + } + free(tmpimgname); + + /**- get image offset: a negative offset means to figure from end of file */ + if( nim->iname_offset < 0 ){ + if( nifti_is_gzfile(nim->iname) ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"negative offset for compressed file",nim->iname); + znzclose(fp); + return NULL; + } + ii = nifti_get_filesize( nim->iname ) ; + if( ii <= 0 ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); + znzclose(fp); + return NULL; + } + ioff = (ii > ntot) ? ii-ntot : 0 ; + } else { /* non-negative offset */ + ioff = nim->iname_offset ; /* means use it directly */ + } + + /**- seek to the appropriate read position */ + if( znzseek(fp , (long)ioff , SEEK_SET) < 0 ){ + Rc_fprintf_stderr("** NIFTI: could not seek to offset %" PRId64 + " in file '%s'\n", + ioff, nim->iname); + znzclose(fp); + return NULL; + } + + /**- and return the File pointer */ + return fp; +} + +static void nifti_image_mem_load_prep(znzFile fp, nifti_image* nim, const uint8_t *data , const size_t size, int gz, const size_t estimated_size) +{ + if (znz_isnull(fp)){ + // if(g_opts.debug > 0) LNI_FERR(fname,"cannot open data file",tmpimgname); + // free(tmpimgname); + return; /* bad open? */ + } + + /* set up data space, open data file and seek, then call nifti_read_buffer */ + int64_t ntot , ii , ioff; + char *tmpimgname; + char fname[] = { "nifti_image_load_prep" }; + + /**- perform sanity checks */ + if( nim == NULL || /*nim->iname == NULL || */ + nim->nbyper <= 0 || nim->nvox <= 0 ) + { + // if ( g_opts.debug > 0 ){ + // if( !nim ) Rc_fprintf_stderr("** ERROR: N_image_load: no nifti image\n"); + // else Rc_fprintf_stderr("** ERROR: nifti_image_load: bad params (%p,%d," + // "%" PRId64 ")\n", nim->iname, nim->nbyper, nim->nvox); + // } + return; + } + + ntot = nifti_get_volsize(nim) ; /* total bytes to read */ + + /**- open image data file */ + + // tmpimgname = nifti_findimgname(nim->iname , nim->nifti_type); + // if( tmpimgname == NULL ){ + // if( g_opts.debug > 0 ) + // Rc_fprintf_stderr("** NIFTI: no image file found for '%s'\n",nim->iname); + // return NULL; + // } + + // free(tmpimgname); + + /**- get image offset: a negative offset means to figure from end of file */ + if( nim->iname_offset < 0 ){ + if( gz ){ + // if( g_opts.debug > 0 ) + // LNI_FERR(fname,"negative offset for compressed file",nim->iname); + return; + } + ii = size ; + if( ii <= 0 ){ + // if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); + return; + } + ioff = (ii > ntot) ? ii-ntot : 0 ; + } else { /* non-negative offset */ + ioff = nim->iname_offset ; /* means use it directly */ + } + + /**- seek to the appropriate read position */ + if( znzseek(fp , (long)ioff , SEEK_SET) < 0 ){ + // Rc_fprintf_stderr("** NIFTI: could not seek to offset %" PRId64 + // " in file '%s'\n", + // ioff, nim->iname); + return; + } +} + + +/*---------------------------------------------------------------------- + * nifti_image_load + *----------------------------------------------------------------------*/ +/*! \fn int nifti_image_load( nifti_image *nim ) + \brief Load the image blob into a previously initialized nifti_image. + + - If not yet set, the data buffer is allocated with calloc(). + - The data buffer will be byteswapped if necessary. + - The data buffer will not be scaled. + + This function is used to read the image from disk. It should be used + after a function such as nifti_image_read(), so that the nifti_image + structure is already initialized. + + \param nim pointer to a nifti_image (previously initialized) + \return 0 on success, -1 on failure + \sa nifti_image_read, nifti_image_free, nifti_image_unload +*/ +int nifti2_image_load( nifti_image *nim ) +{ + /* set up data space, open data file and seek, then call nifti_read_buffer */ + int64_t ntot , ii ; + znzFile fp ; + + /**- open the file and position the FILE pointer */ + fp = nifti_image_load_prep( nim ); + + if( fp == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_image_load, failed load_prep\n"); + return -1; + } + + ntot = nifti_get_volsize(nim); + + /**- if the data pointer is not yet set, get memory space for the image */ + + if( nim->data == NULL ) + { + nim->data = calloc(1,ntot) ; /* create image memory */ + if( nim->data == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** NIFTI: failed to alloc %d bytes for image data\n", + (int)ntot); + znzclose(fp); + return -1; + } + } + + /**- now that everything is set up, do the reading */ + ii = nifti_read_buffer(fp,nim->data,ntot,nim); + if( ii < ntot ){ + znzclose(fp) ; + free(nim->data) ; + nim->data = NULL ; + return -1 ; /* errors were printed in nifti_read_buffer() */ + } + + /**- close the file */ + znzclose( fp ) ; + + return 0 ; +} + +int nifti2_image_mem_load(znzFile fp, nifti_image* nim, const uint8_t *data , const size_t size, int gz, const size_t estimated_size) +{ + /* set up data space, open data file and seek, then call nifti_read_buffer */ + int64_t ntot , ii ; + + /**- open the file and position the FILE pointer */ + nifti_image_mem_load_prep( fp, nim, data, size, gz, estimated_size ); + + if( fp == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_image_load, failed load_prep\n"); + return -1; + } + + ntot = nifti_get_volsize(nim); + + int should_assign_data = 0; +#if HAVE_ZLIB + if (fp->withz) { + if (fp->zmemptr != NULL) { + should_assign_data = 1; + } + } +#endif + if (should_assign_data) { + /**- now that everything is set up, assign the data */ + ii = nifti_assign_buffer(fp,&nim->data,ntot,nim); + } else { + /**- if the data pointer is not yet set, get memory space for the image */ + if (nim->data == NULL) { + nim->data = calloc(1, ntot); /* create image memory */ + if (nim->data == NULL) { + if (g_opts.debug > 0) + Rc_fprintf_stderr("** NIFTI: failed to alloc %d bytes for image data\n", (int)ntot); + return -1; + } + } + /**- now that everything is set up, do the reading */ + ii = nifti_read_buffer(fp,nim->data,ntot,nim); + } + + + + if( ii < ntot ) { + if (!should_assign_data) { + free(nim->data); + } + nim->data = NULL; + nim->decompressed_memory_buffer = NULL; + return -1; /* errors were printed in nifti_read_buffer() */ + } + + #if HAVE_ZLIB + if (fp->withz) { + if (fp->zmemptr != NULL) { + // retain the use of decompressed buffer. + // so when we remove the fp we still have the access to the data. + nim->decompressed_memory_buffer = fp->zmemptr->buffer; + } + } +#endif + return 0; +} + + +/* 30 Nov 2004 [rickr] +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_read_buffer: %s\n",(msg)) ; \ + return 0; } while(0) +*/ + +/*----------------------------------------------------------------------*/ +/*! read ntot bytes of data from an open file and byte swaps if necessary + + note that nifti_image is required for information on datatype, bsize + (for any needed byte swapping), etc. + + This function does not allocate memory, so dataptr must be valid. +*//*--------------------------------------------------------------------*/ +int64_t nifti2_read_buffer(znzFile fp, void* dataptr, int64_t ntot, + nifti_image *nim) +{ + int64_t ii; + + if( dataptr == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** ERROR: nifti_read_buffer: NULL dataptr\n"); + return -1; + } + + ii = znzread( dataptr , 1 , ntot , fp ) ; /* data input */ + + /* if read was short, fail */ + if( ii < ntot ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("++ WARNING: nifti_read_buffer(%s):\n" + " data bytes needed = %" PRId64 "\n" + " data bytes input = %" PRId64 "\n" + " number missing = %" PRId64 " (set to 0)\n", + nim->iname , ntot , ii , (ntot-ii) ) ; + /* memset( (char *)(dataptr)+ii , 0 , ntot-ii ) ; now failure [rickr] */ + return -1 ; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d nifti_read_buffer: read %" PRId64 " bytes\n", ii); + + /* byte swap array if needed */ + + /* ntot/swapsize might not fit as int, use int64_t 6 Jul 2010 [rickr] */ + if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_read_buffer: swapping data bytes...\n"); + nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; + } + +#if defined(isfinite) && !defined(USING_R) +{ + /* check input float arrays for goodness, and fix bad floats */ + int fix_count = 0 ; + + switch( nim->datatype ){ + + case NIFTI_TYPE_FLOAT32: + case NIFTI_TYPE_COMPLEX64:{ + float *far = (float *)dataptr ; int64_t jj,nj ; + nj = ntot / sizeof(float) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + case NIFTI_TYPE_FLOAT64: + case NIFTI_TYPE_COMPLEX128:{ + double *far = (double *)dataptr ; int64_t jj,nj ; + nj = ntot / sizeof(double) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d in image, %d bad floats were set to 0\n", fix_count); +} +#endif + + return ii; +} + +int64_t nifti2_assign_buffer(znzFile fp, void** data, int64_t ntot, nifti_image *nim) +{ + int64_t ii; + + if( data == NULL ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** ERROR: nifti_read_buffer: NULL dataptr\n"); + return -1; + } + + if (!fp->withz) { + Rc_fprintf_stderr("** ERROR: nifti_assign_buffer: should be called only for compressed data\n"); + return -1; + } + + #if HAVE_ZLIB + if (fp->zmemptr == NULL) { + Rc_fprintf_stderr("** ERROR: nifti_assign_buffer: should be called only for zmemptr type. e.g. memory buffer\n"); + return -1; + } + #endif + ii = znzassign(data, 1, ntot, fp); /* data input */ + + void *dataptr = *data; + if (dataptr == NULL) { + Rc_fprintf_stderr("buffer assing failed\n"); + return -1; + } + + /* if read was short, fail */ + if (ii < ntot) { + if (g_opts.debug > 0) + Rc_fprintf_stderr("++ WARNING: nifti_read_buffer(%s):\n" + " data bytes needed = %" PRId64 "\n" + " data bytes input = %" PRId64 "\n" + " number missing = %" PRId64 " (set to 0)\n", + nim->iname, ntot, ii, (ntot - ii)); + /* memset( (char *)(dataptr)+ii , 0 , ntot-ii ) ; now failure [rickr] */ + return -1; + } + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d nifti_read_buffer: read %" PRId64 " bytes\n", ii); + + /* byte swap array if needed */ + + /* ntot/swapsize might not fit as int, use int64_t 6 Jul 2010 [rickr] */ + if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_read_buffer: swapping data bytes...\n"); + nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; + } + +#if defined(isfinite) && !defined(USING_R) +{ + /* check input float arrays for goodness, and fix bad floats */ + int fix_count = 0 ; + + switch( nim->datatype ){ + + case NIFTI_TYPE_FLOAT32: + case NIFTI_TYPE_COMPLEX64:{ + float *far = (float *)dataptr ; int64_t jj,nj ; + nj = ntot / sizeof(float) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + case NIFTI_TYPE_FLOAT64: + case NIFTI_TYPE_COMPLEX128:{ + double *far = (double *)dataptr ; int64_t jj,nj ; + nj = ntot / sizeof(double) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d in image, %d bad floats were set to 0\n", fix_count); +} +#endif + + return ii; +} + +size_t nifti2_image_impl_reserved_bytes_offset() +{ + return (size_t)&(((nifti_image*)(NULL))->decompressed_memory_buffer); +} + +int nifti2_image_is_data_owner(const nifti_image* nim) +{ + if( nim != NULL && nim->data != NULL ) { + return nim->decompressed_memory_buffer == nullptr; + } + return 0; +} + +/*--------------------------------------------------------------------------*/ +/*! Unload the data in a nifti_image struct, but keep the metadata. +*//*------------------------------------------------------------------------*/ +void nifti2_image_unload( nifti_image *nim ) +{ + if( nim != NULL && nim->data != NULL ) { + if ( nifti2_image_is_data_owner(nim) ) { + free(nim->data); + } + nim->data = NULL; + nim->decompressed_memory_buffer = nullptr; + } +} + +/*--------------------------------------------------------------------------*/ +/*! free 'everything' about a nifti_image struct (including the passed struct) + + free (only fields which are not NULL): + - fname and iname + - data + - any ext_list[i].edata + - ext_list + - nim +*//*------------------------------------------------------------------------*/ +void nifti2_image_free( nifti_image *nim ) +{ + if( nim == NULL ) return ; + if( nim->fname != NULL ) free(nim->fname) ; + if( nim->iname != NULL ) free(nim->iname) ; + if( nim->data != NULL ) { + if ( nifti2_image_is_data_owner(nim) ) { + free(nim->data); + } + nim->data = nullptr; + nim->decompressed_memory_buffer = nullptr; + } + (void)nifti_free_extensions( nim ) ; + free(nim) ; +} + +/*--------------------------------------------------------------------------*/ +/*! free the nifti extensions + + - If any edata pointer is set in the extension list, free() it. + - Free ext_list, if it is set. + - Clear num_ext and ext_list from nim. + + \return 0 on success, -1 on error + + \sa nifti_add_extension, nifti_copy_extensions +*//*------------------------------------------------------------------------*/ +int nifti2_free_extensions( nifti_image *nim ) +{ + int c ; + if( nim == NULL ) return -1; + if( nim->num_ext > 0 && nim->ext_list ){ + for( c = 0; c < nim->num_ext; c++ ) + if ( nim->ext_list[c].edata ) free(nim->ext_list[c].edata); + free(nim->ext_list); + } + /* or if it is inconsistent, warn the user (if we are not in quiet mode) */ + else if ( (nim->num_ext > 0 || nim->ext_list != NULL) && (g_opts.debug > 0) ) + Rc_fprintf_stderr("** warning: nifti extension num/ptr mismatch (%d,%p)\n", + nim->num_ext, (void *)nim->ext_list); + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d free'd %d extension(s)\n", nim->num_ext); + + nim->num_ext = 0; + nim->ext_list = NULL; + + return 0; +} + + +/*--------------------------------------------------------------------------*/ +/*! Print to stdout some info about a nifti_image struct. +*//*------------------------------------------------------------------------*/ +void nifti2_image_infodump( const nifti_image *nim ) +{ + char *str = nifti_image_to_ascii( nim ) ; + /* stdout -> stderr 2 Dec 2004 [rickr] */ + if( str != NULL ){ Rc_fputs_stderr(str) ; free(str) ; } + } + + +/*-------------------------------------------------------------------------- + * nifti_write_buffer just check for a null znzFile and call znzwrite + *--------------------------------------------------------------------------*/ +/*! \fn int64_t nifti_write_buffer(znzFile fp, void *buffer, int64_t numbytes) + \brief write numbytes of buffer to file, fp + + \param fp File pointer (from znzopen) to gzippable nifti datafile + \param buffer data buffer to be written + \param numbytes number of bytes in buffer to write + \return number of bytes successfully written +*/ +int64_t nifti_write_buffer(znzFile fp, const void *buffer, int64_t numbytes) +{ + /* Write all the image data at once (no swapping here) */ + int64_t ss; + if (znz_isnull(fp)){ + Rc_fprintf_stderr("** ERROR: nifti_write_buffer: null file pointer\n"); + return 0; + } + ss = znzwrite( buffer , 1 , numbytes , fp ) ; + return ss; +} + + +/*----------------------------------------------------------------------*/ +/*! write the nifti_image data to file (from nim->data or from NBL) + + If NBL is not NULL, write the data from that structure. Otherwise, + write it out from nim->data. No swapping is done here. + + \param fp : File pointer + \param nim : nifti_image corresponding to the data + \param NBL : optional source of write data (if NULL use nim->data) + + \return 0 on success, -1 on failure + + Note: the nifti_image byte_order is set as that of the current CPU. + This is because such a conversion was made to the data upon + reading, while byte_order was not set (so the programs would + know what format the data was on disk). Effectively, since + byte_order should match what is on disk, it should bet set to + that of the current CPU whenever new filenames are assigned. +*//*--------------------------------------------------------------------*/ +int nifti2_write_all_data(znzFile fp, nifti_image * nim, + const nifti_brick_list * NBL) +{ + int64_t ss, bnum; + + if( !NBL ){ /* just write one buffer and get out of here */ + if( nim->data == NULL ){ + Rc_fprintf_stderr("** NIFTI ERROR (NWAD): no image data to write\n"); + return -1; + } + + ss = nifti_write_buffer(fp,nim->data,nim->nbyper * nim->nvox); + if (ss < nim->nbyper * nim->nvox){ + Rc_fprintf_stderr( + "** NIFTI ERROR (NWAD): wrote only %" PRId64 " of %" PRId64 + " bytes to file\n", + ss, nim->nbyper * nim->nvox); + return -1; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d wrote single image of %" PRId64 " bytes\n", ss); + } else { + if( ! NBL->bricks || NBL->nbricks <= 0 || NBL->bsize <= 0 ){ + Rc_fprintf_stderr("** NIFTI error (NWAD): no brick data to write (%p,%" + PRId64 ",%" PRId64 ")\n", + (void *)NBL->bricks, NBL->nbricks, NBL->bsize); + return -1; + } + + for( bnum = 0; bnum < NBL->nbricks; bnum++ ){ + ss = nifti_write_buffer(fp, NBL->bricks[bnum], NBL->bsize); + if( ss < NBL->bsize ){ + Rc_fprintf_stderr( + "** NIFTI ERROR (NWAD): wrote only %" PRId64 " of %" PRId64 + " bytes of brick %" PRId64 " of %" PRId64 " to file\n", + ss, NBL->bsize, bnum+1, NBL->nbricks); + return -1; + } + } + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d wrote image of %" PRId64 + " brick(s), each of %" PRId64 " bytes\n", + NBL->nbricks, NBL->bsize); + } + + /* mark as being in this CPU byte order */ + nim->byteorder = nifti_short_order() ; + + return 0; +} + +/* return number of extensions written, or -1 on error */ +static int nifti_write_extensions(znzFile fp, nifti_image *nim) +{ + nifti1_extension * list; + char extdr[4] = { 0, 0, 0, 0 }; + int c, size, ok = 1; + + if( znz_isnull(fp) || !nim || nim->num_ext < 0 ){ + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** nifti_write_extensions, bad params\n"); + return -1; + } + + /* if no extensions and user requests it, skip extender */ + if( g_opts.skip_blank_ext && (nim->num_ext == 0 || ! nim->ext_list ) ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d no exts and skip_blank_ext set, " + "so skipping 4-byte extender\n"); + return 0; + } + + /* if invalid extension list, clear num_ext */ + if( ! valid_nifti_extensions(nim) ) nim->num_ext = 0; + + /* write out extender block */ + if( nim->num_ext > 0 ) extdr[0] = 1; + if( nifti_write_buffer(fp, extdr, 4) != 4 ){ + Rc_fprintf_stderr("** NIFTI ERROR: failed to write extender\n"); + return -1; + } + + list = nim->ext_list; + for ( c = 0; c < nim->num_ext; c++ ){ + size = (int)nifti_write_buffer(fp, &list->esize, sizeof(int)); + ok = (size == (int)sizeof(int)); + if( ok ){ + size = (int)nifti_write_buffer(fp, &list->ecode, sizeof(int)); + ok = (size == (int)sizeof(int)); + } + if( ok ){ + size = (int)nifti_write_buffer(fp, list->edata, list->esize - 8); + ok = (size == list->esize - 8); + } + + if( !ok ){ + Rc_fprintf_stderr("** NIFTI: failed while writing extension #%d\n",c); + return -1; + } else if ( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d wrote extension %d of %d bytes\n", c, size); + + list++; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d wrote out %d extension(s)\n", nim->num_ext); + + return nim->num_ext; +} + + +/*----------------------------------------------------------------------*/ +/*! basic initialization of a nifti_image struct (to a 1x1x1 image) +*//*--------------------------------------------------------------------*/ +nifti_image* nifti2_simple_init_nim(void) +{ + nifti_image *nim; + nifti_2_header nhdr; + int nbyper, swapsize; + + memset(&nhdr,0,sizeof(nhdr)) ; /* zero out header, to be safe */ + + nhdr.sizeof_hdr = sizeof(nhdr) ; + + nhdr.dim[0] = 3 ; + nhdr.dim[1] = 1 ; nhdr.dim[2] = 1 ; nhdr.dim[3] = 1 ; + nhdr.dim[4] = 0 ; + + nhdr.pixdim[0] = 0.0 ; + nhdr.pixdim[1] = 1.0 ; nhdr.pixdim[2] = 1.0 ; nhdr.pixdim[3] = 1.0 ; + + nhdr.datatype = DT_FLOAT32 ; + nifti_datatype_sizes( nhdr.datatype , &nbyper, &swapsize ); + nhdr.bitpix = 8 * nbyper ; + + memcpy(nhdr.magic, nifti2_magic, 8); /* init to single file */ + + nim = nifti_convert_n2hdr2nim(nhdr,NULL); + nim->fname = NULL; + nim->iname = NULL; + return nim; +} + + +/*----------------------------------------------------------------------*/ +/*! basic initialization of a nifti_2_header struct (with given dimensions) + + Return an allocated nifti_2_header struct, based on the given + dimensions and datatype. + + \param arg_dims : optional dim[8] array (default {3,1,1,1,0,0,0,0}) + \param arg_dtype : optional datatype (default DT_FLOAT32) + + \return pointer to allocated nifti_2_header struct +*//*--------------------------------------------------------------------*/ +nifti_2_header * nifti_make_new_n2_header(const int64_t arg_dims[], + int arg_dtype) +{ + nifti_2_header * nhdr; + const int64_t default_dims[8] = { 3, 1, 1, 1, 0, 0, 0, 0 }; + const int64_t * dim; /* either passed or default dims */ + int dtype; /* either passed or default dtype */ + int c, nbyper, swapsize; + + /* if arg_dims is passed, apply it */ + if( arg_dims ) dim = arg_dims; + else dim = default_dims; + + /* validate dim: if there is any problem, apply default_dims */ + if( dim[0] < 1 || dim[0] > 7 ) { + Rc_fprintf_stderr("** nifti_simple_hdr_with_dims: bad dim[0]=%" PRId64 "\n", + dim[0]); + dim = default_dims; + } else { + for( c = 1; c <= dim[0]; c++ ) + if( dim[c] < 1 ) + { + Rc_fprintf_stderr( + "** nifti_simple_hdr_with_dims: bad dim[%d]=%" PRId64 "\n", + c, dim[c]); + dim = default_dims; + break; + } + } + + /* validate dtype, too */ + dtype = arg_dtype; + if( ! nifti_is_valid_datatype(dtype) ) { + Rc_fprintf_stderr("** nifti_simple_hdr_with_dims: bad dtype %d\n",dtype); + dtype = DT_FLOAT32; + } + + /* now populate the header struct */ + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d make_new_n2_header, dim[0] = %" PRId64 + ", datatype = %d\n", + dim[0], dtype); + + nhdr = (nifti_2_header *)calloc(1,sizeof(nifti_2_header)); + if( !nhdr ){ + Rc_fprintf_stderr("** NIFTI make_new_n2_header: failed to alloc hdr\n"); + return NULL; + } + + nhdr->sizeof_hdr = sizeof(nifti_2_header) ; + + /* init dim and pixdim */ + nhdr->dim[0] = dim[0]; + nhdr->pixdim[0] = 0.0; + for( c = 1; c <= dim[0]; c++ ) { + nhdr->dim[c] = dim[c]; + nhdr->pixdim[c] = 1.0; + } + + nhdr->datatype = dtype ; + nifti_datatype_sizes( nhdr->datatype , &nbyper, &swapsize ); + nhdr->bitpix = 8 * nbyper ; + + memcpy(nhdr->magic, nifti2_magic, 8); /* init to single file */ + + return nhdr; +} + + +/*----------------------------------------------------------------------*/ +/*! basic initialization of a nifti_1_header struct (with given dimensions) + + Return an allocated nifti_1_header struct, based on the given + dimensions and datatype. + + \param arg_dims : optional dim[8] array (default {3,1,1,1,0,0,0,0}) + \param arg_dtype : optional datatype (default DT_FLOAT32) + + \return pointer to allocated nifti_1_header struct +*//*--------------------------------------------------------------------*/ +nifti_1_header * nifti_make_new_n1_header(const int64_t arg_dims[], + int arg_dtype) +{ + nifti_1_header * nhdr; + const int64_t default_dims[8] = { 3, 1, 1, 1, 0, 0, 0, 0 }; + const int64_t * dim; /* either passed or default dims */ + int dtype; /* either passed or default dtype */ + int c, nbyper, swapsize; + + /* if arg_dims is passed, apply it */ + if( arg_dims ) dim = arg_dims; + else dim = default_dims; + + /* validate dim: if there is any problem, apply default_dims */ + if( dim[0] < 1 || dim[0] > 7 ) { + Rc_fprintf_stderr("** nifti_simple_hdr_with_dims: bad dim[0]=%" PRId64 "\n", + dim[0]); + dim = default_dims; + } else { + for( c = 1; c <= dim[0]; c++ ) + if( dim[c] < 1 ) + { + Rc_fprintf_stderr( + "** nifti_simple_hdr_with_dims: bad dim[%d]=%" PRId64 "\n", c, dim[c]); + dim = default_dims; + break; + } + } + + /* validate dtype, too */ + dtype = arg_dtype; + if( ! nifti_is_valid_datatype(dtype) ) { + Rc_fprintf_stderr("** nifti_simple_hdr_with_dims: bad dtype %d\n",dtype); + dtype = DT_FLOAT32; + } + + /* now populate the header struct */ + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d make_new_n1_header, dim[0] = %" PRId64 + ", datatype = %d\n", + dim[0], dtype); + + nhdr = (nifti_1_header *)calloc(1,sizeof(nifti_1_header)); + if( !nhdr ){ + Rc_fprintf_stderr("** NIFTI make_new_n1_header: failed to alloc hdr\n"); + return NULL; + } + + nhdr->sizeof_hdr = sizeof(nifti_1_header) ; + nhdr->regular = 'r' ; /* for some stupid reason */ + + /* init dim and pixdim */ + nhdr->dim[0] = (int)dim[0]; /* rcr n2 - check dim sizes for nifti-1 */ + /* (verify vals are < 2^15) */ + nhdr->pixdim[0] = 0.0f; + for( c = 1; c <= dim[0]; c++ ) { + nhdr->dim[c] = (int)dim[c]; + nhdr->pixdim[c] = 1.0f; + } + + nhdr->datatype = dtype ; + nifti_datatype_sizes( nhdr->datatype , &nbyper, &swapsize ); + nhdr->bitpix = 8 * nbyper ; + + strcpy(nhdr->magic, "n+1"); /* init to single file */ + + return nhdr; +} + + +/*----------------------------------------------------------------------*/ +/*! basic creation of a nifti_image struct + + Create a nifti_image from the given dimensions and data type. + Optinally, allocate zero-filled data. + + \param dims : optional dim[8] (default {3,1,1,1,0,0,0,0}) + \param datatype : optional datatype (default DT_FLOAT32) + \param data_fill : if flag is set, allocate zero-filled data for image + + \return pointer to allocated nifti_image struct +*//*--------------------------------------------------------------------*/ +nifti_image * nifti2_make_new_nim(const int64_t dims[], int datatype, + int data_fill) +{ + nifti_image * nim; + nifti_2_header * nhdr; + + nhdr = nifti_make_new_n2_header(dims, datatype); + if( !nhdr ) return NULL; /* error already printed */ + + nim = nifti_convert_n2hdr2nim(*nhdr,NULL); + free(nhdr); /* in any case, we are done with this */ + if( !nim ){ + Rc_fprintf_stderr("** NMNN: nifti_convert_n2hdr2nim failure\n"); + return NULL; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d nifti_make_new_nim, data_fill = %d\n",data_fill); + + if( data_fill ) { + nim->data = calloc(nim->nvox, nim->nbyper); + + /* if we cannot allocate data, take ball and go home */ + if( !nim->data ) { + Rc_fprintf_stderr("** NIFTI NMNN: failed to alloc %" PRId64 + " bytes for data\n", nim->nvox*nim->nbyper); + nifti_image_free(nim); + nim = NULL; + } + } + + return nim; +} + +#undef N_CHECK_2BYTE_VAL +#define N_CHECK_2BYTE_VAL(fn) do { if( ! NIFTI_IS_16_BIT_INT(nim->fn) ) { \ + Rc_fprintf_stderr("** nim->%s = %" PRId64 \ + " does not fit into NIFTI-1 header\n", \ + #fn, (int64_t)nim->fn); return 1; } } while(0) + + +/*----------------------------------------------------------------------*/ +/*! convert a nifti_image structure to a nifti_1_header struct + + No allocation is done, this should be used via structure copy. + As in: +
+    nifti_1_header my_header;
+    my_header = nifti_convert_nim2n1hdr(my_nim_pointer);
+    
+*//*--------------------------------------------------------------------*/ +int nifti_convert_nim2n1hdr(const nifti_image * nim, nifti_1_header * hdr) +{ + nifti_1_header nhdr; + + if( !hdr ) { + Rc_fprintf_stderr("** nifti_CN2N1hdr: no hdr to fill\n"); + return 1; + } + + memset(&nhdr,0,sizeof(nhdr)) ; /* zero out header, to be safe */ + + + /**- load the ANALYZE-7.5 generic parts of the header struct */ + + nhdr.sizeof_hdr = sizeof(nhdr) ; + nhdr.regular = 'r' ; /* for some stupid reason */ + + N_CHECK_2BYTE_VAL(ndim); + N_CHECK_2BYTE_VAL(nx); + N_CHECK_2BYTE_VAL(ny); + N_CHECK_2BYTE_VAL(nz); + N_CHECK_2BYTE_VAL(nt); + N_CHECK_2BYTE_VAL(nu); + N_CHECK_2BYTE_VAL(nv); + N_CHECK_2BYTE_VAL(nw); + N_CHECK_2BYTE_VAL(datatype); + N_CHECK_2BYTE_VAL(nbyper); + + nhdr.dim[0] = nim->ndim ; + nhdr.dim[1] = nim->nx ; nhdr.dim[2] = nim->ny ; nhdr.dim[3] = nim->nz ; + nhdr.dim[4] = nim->nt ; nhdr.dim[5] = nim->nu ; nhdr.dim[6] = nim->nv ; + nhdr.dim[7] = nim->nw ; + + nhdr.pixdim[0] = 0.0f ; + nhdr.pixdim[1] = nim->dx ; nhdr.pixdim[2] = nim->dy ; + nhdr.pixdim[3] = nim->dz ; nhdr.pixdim[4] = nim->dt ; + nhdr.pixdim[5] = nim->du ; nhdr.pixdim[6] = nim->dv ; + nhdr.pixdim[7] = nim->dw ; + + nhdr.datatype = nim->datatype ; + nhdr.bitpix = 8 * nim->nbyper ; + + if( nim->cal_max > nim->cal_min ){ + nhdr.cal_max = nim->cal_max ; + nhdr.cal_min = nim->cal_min ; + } + + if( nim->scl_slope != 0.0 ){ + nhdr.scl_slope = nim->scl_slope ; + nhdr.scl_inter = nim->scl_inter ; + } + + if( nim->descrip[0] != '\0' ){ + memcpy(nhdr.descrip ,nim->descrip ,79) ; nhdr.descrip[79] = '\0' ; + } + if( nim->aux_file[0] != '\0' ){ + memcpy(nhdr.aux_file ,nim->aux_file ,23) ; nhdr.aux_file[23] = '\0' ; + } + + /**- Load NIFTI specific stuff into the header */ + + if( nim->nifti_type > NIFTI_FTYPE_ANALYZE ){ /* then not ANALYZE */ + + if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcpy(nhdr.magic,"n+1") ; + else strcpy(nhdr.magic,"ni1") ; + + nhdr.pixdim[1] = (float)fabs(nhdr.pixdim[1]) ; + nhdr.pixdim[2] = (float)fabs(nhdr.pixdim[2]) ; + nhdr.pixdim[3] = (float)fabs(nhdr.pixdim[3]) ; + nhdr.pixdim[4] = (float)fabs(nhdr.pixdim[4]) ; + nhdr.pixdim[5] = (float)fabs(nhdr.pixdim[5]) ; + nhdr.pixdim[6] = (float)fabs(nhdr.pixdim[6]) ; + nhdr.pixdim[7] = (float)fabs(nhdr.pixdim[7]) ; + + N_CHECK_2BYTE_VAL(intent_code); + N_CHECK_2BYTE_VAL(qform_code); + N_CHECK_2BYTE_VAL(sform_code); + + nhdr.intent_code = nim->intent_code ; + nhdr.intent_p1 = nim->intent_p1 ; + nhdr.intent_p2 = nim->intent_p2 ; + nhdr.intent_p3 = nim->intent_p3 ; + if( nim->intent_name[0] != '\0' ){ + memcpy(nhdr.intent_name,nim->intent_name,15) ; + nhdr.intent_name[15] = '\0' ; + } + + nhdr.vox_offset = (float) nim->iname_offset ; + nhdr.xyzt_units = SPACE_TIME_TO_XYZT( nim->xyz_units, nim->time_units ) ; + nhdr.toffset = nim->toffset ; + + if( nim->qform_code > 0 ){ + nhdr.qform_code = nim->qform_code ; + nhdr.quatern_b = nim->quatern_b ; + nhdr.quatern_c = nim->quatern_c ; + nhdr.quatern_d = nim->quatern_d ; + nhdr.qoffset_x = nim->qoffset_x ; + nhdr.qoffset_y = nim->qoffset_y ; + nhdr.qoffset_z = nim->qoffset_z ; + nhdr.pixdim[0] = (nim->qfac >= 0.0) ? 1.0f : -1.0f ; + } + + if( nim->sform_code > 0 ){ + nhdr.sform_code = nim->sform_code ; + nhdr.srow_x[0] = nim->sto_xyz.m[0][0] ; + nhdr.srow_x[1] = nim->sto_xyz.m[0][1] ; + nhdr.srow_x[2] = nim->sto_xyz.m[0][2] ; + nhdr.srow_x[3] = nim->sto_xyz.m[0][3] ; + nhdr.srow_y[0] = nim->sto_xyz.m[1][0] ; + nhdr.srow_y[1] = nim->sto_xyz.m[1][1] ; + nhdr.srow_y[2] = nim->sto_xyz.m[1][2] ; + nhdr.srow_y[3] = nim->sto_xyz.m[1][3] ; + nhdr.srow_z[0] = nim->sto_xyz.m[2][0] ; + nhdr.srow_z[1] = nim->sto_xyz.m[2][1] ; + nhdr.srow_z[2] = nim->sto_xyz.m[2][2] ; + nhdr.srow_z[3] = nim->sto_xyz.m[2][3] ; + } + + N_CHECK_2BYTE_VAL(sform_code); + N_CHECK_2BYTE_VAL(slice_start); + N_CHECK_2BYTE_VAL(slice_end); + + nhdr.dim_info = FPS_INTO_DIM_INFO( nim->freq_dim , + nim->phase_dim , nim->slice_dim ) ; + nhdr.slice_code = nim->slice_code ; + nhdr.slice_start = nim->slice_start ; + nhdr.slice_end = nim->slice_end ; + nhdr.slice_duration = nim->slice_duration ; + } + + memcpy(hdr, &nhdr, sizeof(nhdr)); + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! convert a nifti_image structure to a nifti_2_header struct + + No allocation is done, this should be used via structure copy. + As in: +
+    nifti_2_header my_header;
+    my_header = nifti_convert_nim2n2hdr(my_nim_pointer);
+    
+*//*--------------------------------------------------------------------*/ +int nifti_convert_nim2n2hdr(const nifti_image * nim, nifti_2_header * hdr) +{ + nifti_2_header nhdr; + + if( !hdr ) { + Rc_fprintf_stderr("** nifti_CN2N2hdr: no hdr to fill\n"); + return 1; + } + + memset(&nhdr,0,sizeof(nhdr)) ; /* zero out header, to be safe */ + + + /**- load the ANALYZE-7.5 generic parts of the header struct */ + + nhdr.sizeof_hdr = sizeof(nhdr) ; + memcpy(nhdr.magic, nifti2_magic, 8); + if( nim->nifti_type == NIFTI_FTYPE_NIFTI2_2 ) nhdr.magic[1] = 'i'; + + nhdr.datatype = nim->datatype ; + nhdr.bitpix = 8 * nim->nbyper ; + + nhdr.dim[0] = nim->ndim ; + nhdr.dim[1] = nim->nx ; nhdr.dim[2] = nim->ny ; nhdr.dim[3] = nim->nz ; + nhdr.dim[4] = nim->nt ; nhdr.dim[5] = nim->nu ; nhdr.dim[6] = nim->nv ; + nhdr.dim[7] = nim->nw ; + + nhdr.intent_p1 = nim->intent_p1 ; + nhdr.intent_p2 = nim->intent_p2 ; + nhdr.intent_p3 = nim->intent_p3 ; + + nhdr.pixdim[0] = 0.0 ; + nhdr.pixdim[1] = fabs(nim->dx) ; nhdr.pixdim[2] = fabs(nim->dy) ; + nhdr.pixdim[3] = fabs(nim->dz) ; nhdr.pixdim[4] = fabs(nim->dt) ; + nhdr.pixdim[5] = fabs(nim->du) ; nhdr.pixdim[6] = fabs(nim->dv) ; + nhdr.pixdim[7] = fabs(nim->dw) ; + + nhdr.vox_offset = nim->iname_offset ; + + nhdr.scl_slope = nim->scl_slope ; + nhdr.scl_inter = nim->scl_inter ; + + nhdr.cal_max = nim->cal_max ; + nhdr.cal_min = nim->cal_min ; + + nhdr.slice_duration = nim->slice_duration ; + nhdr.toffset = nim->toffset ; + nhdr.slice_start = nim->slice_start ; + nhdr.slice_end = nim->slice_end ; + + if( nim->descrip[0] != '\0' ){ + memcpy(nhdr.descrip ,nim->descrip ,79) ; nhdr.descrip[79] = '\0' ; + } + if( nim->aux_file[0] != '\0' ){ + memcpy(nhdr.aux_file ,nim->aux_file ,23) ; nhdr.aux_file[23] = '\0' ; + } + + if( nim->qform_code > 0 ){ + nhdr.qform_code = nim->qform_code ; + nhdr.quatern_b = nim->quatern_b ; + nhdr.quatern_c = nim->quatern_c ; + nhdr.quatern_d = nim->quatern_d ; + nhdr.qoffset_x = nim->qoffset_x ; + nhdr.qoffset_y = nim->qoffset_y ; + nhdr.qoffset_z = nim->qoffset_z ; + nhdr.pixdim[0] = (nim->qfac >= 0.0) ? 1.0f : -1.0f ; + } + + if( nim->sform_code > 0 ){ + nhdr.sform_code = nim->sform_code ; + nhdr.srow_x[0] = nim->sto_xyz.m[0][0] ; + nhdr.srow_x[1] = nim->sto_xyz.m[0][1] ; + nhdr.srow_x[2] = nim->sto_xyz.m[0][2] ; + nhdr.srow_x[3] = nim->sto_xyz.m[0][3] ; + nhdr.srow_y[0] = nim->sto_xyz.m[1][0] ; + nhdr.srow_y[1] = nim->sto_xyz.m[1][1] ; + nhdr.srow_y[2] = nim->sto_xyz.m[1][2] ; + nhdr.srow_y[3] = nim->sto_xyz.m[1][3] ; + nhdr.srow_z[0] = nim->sto_xyz.m[2][0] ; + nhdr.srow_z[1] = nim->sto_xyz.m[2][1] ; + nhdr.srow_z[2] = nim->sto_xyz.m[2][2] ; + nhdr.srow_z[3] = nim->sto_xyz.m[2][3] ; + } + + nhdr.slice_code = nim->slice_code ; + nhdr.xyzt_units = SPACE_TIME_TO_XYZT( nim->xyz_units, nim->time_units ) ; + nhdr.intent_code = nim->intent_code ; + if( nim->intent_name[0] != '\0' ){ + memcpy(nhdr.intent_name,nim->intent_name,15) ; + nhdr.intent_name[15] = '\0' ; + } + + nhdr.dim_info = FPS_INTO_DIM_INFO( nim->freq_dim , + nim->phase_dim , nim->slice_dim ) ; + + nhdr.unused_str[0] = '\0' ; /* not needed, but complete */ + + memcpy(hdr, &nhdr, sizeof(nhdr)); + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! \fn int nifti_copy_extensions(nifti_image * nim_dest, nifti_image * nim_src) + \brief copy the nifti1_extension list from src to dest + + Duplicate the list of nifti1_extensions. The dest structure must + be clear of extensions. + \return 0 on success, -1 on failure + + \sa nifti_add_extension, nifti_free_extensions +*/ +int nifti2_copy_extensions(nifti_image * nim_dest, const nifti_image * nim_src) +{ + char * data; + int64_t bytes; + int c, size, old_size; + + if( nim_dest->num_ext > 0 || nim_dest->ext_list != NULL ){ + Rc_fprintf_stderr("** NIFTI: will not copy over existing extensions\n"); + return -1; + } + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d duplicating %d extension(s)\n", nim_src->num_ext); + + if( nim_src->num_ext <= 0 ) return 0; + + bytes = nim_src->num_ext * sizeof(nifti1_extension); /* I'm lazy */ + nim_dest->ext_list = (nifti1_extension *)malloc(bytes); + if( !nim_dest->ext_list ){ + Rc_fprintf_stderr("** failed to allocate %d nifti1_extension structs\n", + nim_src->num_ext); + return -1; + } + + /* copy the extension data */ + nim_dest->num_ext = 0; + for( c = 0; c < nim_src->num_ext; c++ ){ + size = old_size = nim_src->ext_list[c].esize; + if( size & 0xf ) size = (size + 0xf) & ~0xf; /* make multiple of 16 */ + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d dup'ing ext #%d of size %d (from size %d)\n", + c, size, old_size); + /* data length is size-8, as esize includes space for esize and ecode */ + data = (char *)calloc(size-8,sizeof(char)); /* maybe size > old */ + if( !data ){ + Rc_fprintf_stderr("** NIFTI: failed to alloc %d bytes for extention\n", + size); + if( c == 0 ) { free(nim_dest->ext_list); nim_dest->ext_list = NULL; } + /* otherwise, keep what we have (a.o.t. deleting them all) */ + return -1; + } + /* finally, fill the new structure */ + nim_dest->ext_list[c].esize = size; + nim_dest->ext_list[c].ecode = nim_src->ext_list[c].ecode; + nim_dest->ext_list[c].edata = data; + memcpy(data, nim_src->ext_list[c].edata, old_size-8); + + nim_dest->num_ext++; + } + + return 0; +} + + +/*----------------------------------------------------------------------*/ +/*! compute the total size of all extensions + + \return the total of all esize fields + + Note that each esize includes 4 bytes for ecode, 4 bytes for esize, + and the bytes used for the data. Each esize also needs to be a + multiple of 16, so it may be greater than the sum of its 3 parts. +*//*--------------------------------------------------------------------*/ +static int nifti_extension_size(nifti_image *nim) +{ + int c, size = 0; + + if( !nim || nim->num_ext <= 0 ) return 0; + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d ext sizes:"); + + for ( c = 0; c < nim->num_ext; c++ ){ + size += nim->ext_list[c].esize; + if( g_opts.debug > 2 ) Rc_fprintf_stderr(" %d",nim->ext_list[c].esize); + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr(" (total = %d)\n",size); + + return size; +} + + +/*----------------------------------------------------------------------*/ +/*! set the nifti_image iname_offset field, based on nifti_type + + - use nifti_ver to determine the size of the header + (0: default, else NIFTI-version) + - if writing to 2 files, set offset to 0 + - if writing to a single NIFTI-1 file, set the offset to + 352 + total extension size, then align to 16-byte boundary + - if writing an ASCII header, set offset to -1 +*//*--------------------------------------------------------------------*/ +void nifti2_set_iname_offset(nifti_image *nim, int nifti_ver) +{ + int64_t offset; + int64_t hsize = sizeof(nifti_1_header); /* default */ + + if( nifti_ver < 0 || nifti_ver > 2 ) { + if( g_opts.debug > 0 ) + Rc_fprintf_stderr("** invalid nifti_ver = %d for set_iname_offset\n", + nifti_ver); + /* but stick with the default */ + } else if( nifti_ver == 2 ) { + hsize = sizeof(nifti_2_header); + } + + switch( nim->nifti_type ){ + + default: /* writing into 2 files */ + /* we only write files with 0 offset in the 2 file format */ + nim->iname_offset = 0 ; + break ; + + /* NIFTI-1 single binary file - always update */ + case NIFTI_FTYPE_NIFTI1_1: + case NIFTI_FTYPE_NIFTI2_1: + offset = nifti_extension_size(nim) + hsize + 4; + /* be sure offset is aligned to a 16 byte boundary */ + if ( ( offset % 16 ) != 0 ) offset = ((offset + 0xf) & ~0xf); + if( nim->iname_offset != offset ){ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d changing offset from %" PRId64 " to %" PRId64 + "\n", nim->iname_offset, offset); + nim->iname_offset = offset; + } + break ; + + /* non-standard case: NIFTI-1 ASCII header + binary data (single file) */ + case NIFTI_FTYPE_ASCII: + nim->iname_offset = -1 ; /* compute offset from filesize */ + break ; + } +} + + +/*----------------------------------------------------------------------*/ +/*! write the nifti_image dataset to disk, optionally including data + + This is just a front-end for nifti_image_write_hdr_img2. + + \param nim nifti_image to write to disk + \param write_data write options (see nifti_image_write_hdr_img2) + \param opts file open options ("wb" from nifti_image_write) + + \sa nifti_image_write, nifti_image_write_hdr_img2, nifti_image_free, + nifti_set_filenames +*//*--------------------------------------------------------------------*/ +znzFile nifti2_image_write_hdr_img( nifti_image *nim , int write_data , + const char* opts ) +{ + return nifti_image_write_hdr_img2(nim,write_data,opts,NULL,NULL); +} + + +#undef ERREX +#define ERREX(msg) \ + do{ Rc_fprintf_stderr("** ERROR: nifti_image_write_hdr_img: %s\n",(msg)) ; \ + return fp ; } while(0) + + +/* ----------------------------------------------------------------------*/ +/*! This writes the header (and optionally the image data) to file + * + * If the image data file is left open it returns a valid znzFile handle. + * It also uses imgfile as the open image file is not null, and modifies + * it inside. + * + * \param nim nifti_image to write to disk + * \param write_opts flags whether to write data and/or close file (see below) + * \param opts file-open options, probably "wb" from nifti_image_write() + * \param imgfile optional open znzFile struct, for writing image data + (may be NULL) + * \param NBL optional nifti_brick_list, containing the image data + (may be NULL) + * + * Values for write_opts mode are based on two binary flags + * ( 0/1 for no-write/write data, and 0/2 for close/leave-open files ) : + * - 0 = do not write data and close (do not open data file) + * - 1 = write data and close + * - 2 = do not write data and leave data file open + * - 3 = write data and leave data file open + * + * \sa nifti_image_write, nifti_image_write_hdr_img, nifti_image_free, + * nifti_set_filenames +*//*---------------------------------------------------------------------*/ +znzFile nifti2_image_write_hdr_img2(nifti_image *nim, int write_opts, + const char * opts, znzFile imgfile, const nifti_brick_list * NBL) +{ + nifti_1_header n1hdr ; + nifti_2_header n2hdr ; + znzFile fp=NULL; + int64_t ss ; + int write_data, leave_open; + int nver=1, hsize=(int)sizeof(nifti_1_header); /* 5 Aug 2015 */ + char func[] = { "nifti_image_write_hdr_img2" }; + + write_data = write_opts & 1; /* just separate the bits now */ + leave_open = write_opts & 2; + + if( ! nim ) ERREX("NULL input") ; + if( ! nifti_validfilename(nim->fname) ) ERREX("bad fname input") ; + if( write_data && ! nim->data && ! NBL ) ERREX("no image data") ; + + if( write_data && NBL && ! nifti_NBL_matches_nim(nim, NBL) ) + ERREX("NBL does not match nim"); + + if( g_opts.debug > 1 ){ + Rc_fprintf_stderr("-d writing nifti file '%s'...\n", nim->fname); + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nifti type %d, offset %" PRId64 "\n", + nim->nifti_type, nim->iname_offset); + } + + if( nim->nifti_type == NIFTI_FTYPE_ASCII ) /* non-standard case */ + return nifti_write_ascii_image(nim,NBL,opts,write_data,leave_open); + else if( nim->nifti_type == NIFTI_FTYPE_NIFTI2_1 || nim->nifti_type == NIFTI_FTYPE_NIFTI2_2 ) { + nifti_set_iname_offset(nim, 2); + if( nifti_convert_nim2n2hdr(nim, &n2hdr) ) return NULL; + nver = 2; + hsize = (int)sizeof(nifti_2_header); + } + else { + nifti_set_iname_offset(nim, 1); + if( nifti_convert_nim2n1hdr(nim, &n1hdr) ) return NULL; + } + + /* if writing to 2 files, make sure iname is set and different from fname */ + if( (nim->nifti_type != NIFTI_FTYPE_NIFTI1_1) && (nim->nifti_type != NIFTI_FTYPE_NIFTI2_1) ){ + if( nim->iname && strcmp(nim->iname,nim->fname) == 0 ){ + free(nim->iname) ; nim->iname = NULL ; + } + if( nim->iname == NULL ){ /* then make a new one */ + nim->iname = nifti_makeimgname(nim->fname,nim->nifti_type,0,0); + if( nim->iname == NULL ) return NULL; + } + } + + /* if we have an imgfile and will write the header there, use it */ + if( ! znz_isnull(imgfile) && (nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 || nim->nifti_type == NIFTI_FTYPE_NIFTI2_1) ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("+d using passed file for hdr\n"); + fp = imgfile; + } + else { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d opening output file %s [%s]\n",nim->fname,opts); + fp = znzopen( nim->fname , opts , nifti_is_gzfile(nim->fname) ) ; + if( znz_isnull(fp) ){ + LNI_FERR(func,"cannot open output file",nim->fname); + return fp; + } + } + + /* write the header and extensions */ + + if( nver == 2 ) ss = znzwrite(&n2hdr , 1 , hsize , fp); /* write header */ + else ss = znzwrite(&n1hdr , 1 , hsize , fp); /* write header */ + + if( ss < hsize ){ + LNI_FERR(func,"bad header write to output file",nim->fname); + znzclose(fp); return fp; + } + + /* partial file exists, and errors have been printed, so ignore return */ + if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + (void)nifti_write_extensions(fp,nim); + + /* if the header is all we want, we are done */ + if( ! write_data && ! leave_open ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d header is all we want: done\n"); + znzclose(fp); return(fp); + } + + if( (nim->nifti_type != NIFTI_FTYPE_NIFTI1_1) && (nim->nifti_type != NIFTI_FTYPE_NIFTI2_1) ){ /* get a new file pointer */ + znzclose(fp); /* first, close header file */ + if( ! znz_isnull(imgfile) ){ + if(g_opts.debug > 2) Rc_fprintf_stderr("+d using passed file for img\n"); + fp = imgfile; + } + else { + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d opening img file '%s'\n", nim->iname); + fp = znzopen( nim->iname , opts , nifti_is_gzfile(nim->iname) ) ; + if( znz_isnull(fp) ) ERREX("cannot open image file") ; + } + } + + znzseek(fp, nim->iname_offset, SEEK_SET); /* in any case, seek to offset */ + + if( write_data ) nifti_write_all_data(fp,nim,NBL); + if( ! leave_open ) znzclose(fp); + + return fp; +} + + +/*----------------------------------------------------------------------*/ +/*! write a nifti_image to disk in ASCII format +*//*--------------------------------------------------------------------*/ +znzFile nifti2_write_ascii_image(nifti_image *nim, const nifti_brick_list * NBL, + const char *opts, int write_data, int leave_open) +{ + znzFile fp; + char * hstr; + + hstr = nifti_image_to_ascii( nim ) ; /* get header in ASCII form */ + if( ! hstr ){ Rc_fprintf_stderr("** failed image_to_ascii()\n"); return NULL; } + + fp = znzopen( nim->fname , opts , nifti_is_gzfile(nim->fname) ) ; + if( znz_isnull(fp) ){ + free(hstr); + Rc_fprintf_stderr("** NIFTI: failed to open '%s' for ascii write\n", + nim->fname); + return fp; + } + + znzputs(hstr,fp); /* header */ + nifti_write_extensions(fp,nim); /* extensions */ + + if ( write_data ) { nifti_write_all_data(fp,nim,NBL); } /* data */ + if ( ! leave_open ) { znzclose(fp); } + free(hstr); + return fp; /* returned but may be closed */ +} + + +/*--------------------------------------------------------------------------*/ +/*! Write a nifti_image to disk. + + Since data is properly byte-swapped upon reading, it is assumed + to be in the byte-order of the current CPU at write time. Thus, + nim->byte_order should match that of the current CPU. Note that + the nifti_set_filenames() function takes the flag, set_byte_order. + + The following fields of nim affect how the output appears: + - nifti_type = 0 ==> ANALYZE-7.5 format file pair will be written + - nifti_type = 1 ==> NIFTI-1 format single file will be written + (data offset will be 352+extensions) + - nifti_type = 2 ==> NIFTI_1 format file pair will be written + - nifti_type = 3 ==> NIFTI_1 ASCII single file will be written + - fname is the name of the output file (header or header+data) + - if a file pair is being written, iname is the name of the data file + - existing files WILL be overwritten with extreme prejudice + - if qform_code > 0, the quatern_*, qoffset_*, and qfac fields determine + the qform output, NOT the qto_xyz matrix; if you want to compute these + fields from the qto_xyz matrix, you can use the utility function + nifti_mat44_to_quatern() + + \sa nifti_image_write_bricks, nifti_image_free, nifti_set_filenames, + nifti_image_write_hdr_img +*//*------------------------------------------------------------------------*/ +void nifti2_image_write( nifti_image *nim ) +{ + znzFile fp = nifti_image_write_hdr_img(nim,1,"wb"); + if( fp ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d niw: done with znzFile\n"); + free(fp); + } + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d nifti_image_write: done\n"); +} + + +/*----------------------------------------------------------------------*/ +/*! similar to nifti_image_write, but data is in NBL struct, not nim->data + + \sa nifti_image_write, nifti_image_free, nifti_set_filenames, nifti_free_NBL +*//*--------------------------------------------------------------------*/ +void nifti2_image_write_bricks( nifti_image *nim, const nifti_brick_list * NBL ) +{ + znzFile fp = nifti_image_write_hdr_img2(nim,1,"wb",NULL,NBL); + if( fp ){ + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d niwb: done with znzFile\n"); + free(fp); + } + if( g_opts.debug > 1 ) Rc_fprintf_stderr("-d niwb: done writing bricks\n"); +} + + +/*----------------------------------------------------------------------*/ +/*! copy the nifti_image structure, without data + + Duplicate the structure, including fname, iname and extensions. + Leave the data pointer as NULL. +*//*--------------------------------------------------------------------*/ +nifti_image * nifti2_copy_nim_info(const nifti_image * src) +{ + nifti_image *dest; + dest = (nifti_image *)calloc(1,sizeof(nifti_image)); + if( !dest ){ + Rc_fprintf_stderr("** NCNI: failed to alloc nifti_image\n"); + return NULL; + } + memcpy(dest, src, nifti2_image_impl_reserved_bytes_offset()); + if( src->fname ) dest->fname = nifti_strdup(src->fname); + if( src->iname ) dest->iname = nifti_strdup(src->iname); + dest->num_ext = 0; + dest->ext_list = NULL; + /* errors will be printed in NCE(), continue in either case */ + (void)nifti_copy_extensions(dest, src); + + dest->data = NULL; + // retain decompressed_memory_buffer if any, to keep alive. + dest->decompressed_memory_buffer = src->decompressed_memory_buffer; + + return dest; +} + + +/*------------------------------------------------------------------------*/ +/* Un-escape a C string in place -- that is, convert XML escape sequences + back into their characters. (This can be done in place since the + replacement is always smaller than the input.) Escapes recognized are: + - < -> < + - > -> > + - " -> " + - ' -> ' + - & -> & + Also replace CR LF pair (Microsoft), or CR alone (Macintosh) with + LF (Unix), per the XML standard. + Return value is number of replacements made (if you care). +--------------------------------------------------------------------------*/ + +#undef CR +#undef LF +#define CR 0x0D +#define LF 0x0A + +static int unescape_string( char *str ) +{ + int ii,jj , nn,ll ; + + if( str == NULL ) return 0 ; /* no string? */ + ll = (int)strlen(str) ; if( ll == 0 ) return 0 ; + + /* scan for escapes: &something; */ + + for( ii=jj=nn=0 ; ii': lout += 4 ; break ; /* replace '<' with "<" */ + + case '"' : + case '\'': lout += 6 ; break ; /* replace '"' with """ */ + + case CR: + case LF: lout += 6 ; break ; /* replace CR with " " + LF with " " */ + + default: lout++ ; break ; /* copy all other chars */ + } + } + out = (char *)calloc(1,lout) ; /* allocate output string */ + if( !out ){ + Rc_fprintf_stderr("** NIFTI escapize_string: failed to alloc %d bytes\n", + lout); + return NULL; + } + out[0] = '\'' ; /* opening quote mark */ + for( ii=0,jj=1 ; ii < lstr ; ii++ ){ + switch( str[ii] ){ + default: out[jj++] = str[ii] ; break ; /* normal characters */ + + case '&': memcpy(out+jj,"&",5) ; jj+=5 ; break ; + + case '<': memcpy(out+jj,"<",4) ; jj+=4 ; break ; + case '>': memcpy(out+jj,">",4) ; jj+=4 ; break ; + + case '"' : memcpy(out+jj,""",6) ; jj+=6 ; break ; + + case '\'': memcpy(out+jj,"'",6) ; jj+=6 ; break ; + + case CR: memcpy(out+jj," ",6) ; jj+=6 ; break ; + case LF: memcpy(out+jj," ",6) ; jj+=6 ; break ; + } + } + out[jj++] = '\'' ; /* closing quote mark */ + out[jj] = '\0' ; /* terminate the string */ + return out ; +} + +/*---------------------------------------------------------------------------*/ +/*! Dump the information in a NIFTI image header to an XML-ish ASCII string + that can later be converted back into a NIFTI header in + nifti_image_from_ascii(). + + The resulting string can be free()-ed when you are done with it. +*//*-------------------------------------------------------------------------*/ +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +#endif // __clang__ +char *nifti2_image_to_ascii( const nifti_image *nim ) +{ +#ifdef USING_R + Rf_error("nifti2_image_to_ascii is currently unimplemented for R packages, for portability reasons"); + return NULL; +#else + char *buf , *ebuf ; int nbuf ; + + if( nim == NULL ) return NULL ; /* stupid caller */ + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("+d converting %s to ASCII\n",nim->fname); + + buf = (char *)calloc(1,65534); /* longer than needed, to be safe */ + if( !buf ){ + Rc_fprintf_stderr("** NIFTI NITA: failed to alloc %d bytes\n",65534); + return NULL; + } + + sprintf( buf , "nifti_type == NIFTI_FTYPE_NIFTI1_1) ? "NIFTI-1+" + :(nim->nifti_type == NIFTI_FTYPE_NIFTI1_2) ? "NIFTI-1" + :(nim->nifti_type == NIFTI_FTYPE_ASCII ) ? "NIFTI-1A" + :(nim->nifti_type == NIFTI_FTYPE_NIFTI2_1) ? "NIFTI-2+" + :(nim->nifti_type == NIFTI_FTYPE_NIFTI2_2) ? "NIFTI-2" + : "ANALYZE-7.5" ) ; + + /** Strings that we don't control (filenames, etc.) that might + contain "weird" characters (like quotes) are "escaped": + - A few special characters are replaced by XML-style escapes, using + the function escapize_string(). + - On input, function unescape_string() reverses this process. + - The result is that the NIFTI ASCII-format header is XML-compliant. */ + + ebuf = escapize_string(nim->fname) ; + sprintf( buf+strlen(buf) , " header_filename = %s\n",ebuf); free(ebuf); + + ebuf = escapize_string(nim->iname) ; + sprintf( buf+strlen(buf) , " image_filename = %s\n", ebuf); free(ebuf); + + sprintf( buf+strlen(buf) , " image_offset = '%" PRId64 "'\n" , + nim->iname_offset ); + + sprintf( buf+strlen(buf), " ndim = '%" PRId64 "'\n",nim->ndim); + sprintf( buf+strlen(buf), " nx = '%" PRId64 "'\n", nim->nx ); + if( nim->ndim > 1 ) + sprintf( buf+strlen(buf), " ny = '%" PRId64 "'\n", nim->ny ); + if( nim->ndim > 2 ) + sprintf( buf+strlen(buf), " nz = '%" PRId64 "'\n", nim->nz ); + if( nim->ndim > 3 ) + sprintf( buf+strlen(buf), " nt = '%" PRId64 "'\n", nim->nt ); + if( nim->ndim > 4 ) + sprintf( buf+strlen(buf), " nu = '%" PRId64 "'\n", nim->nu ); + if( nim->ndim > 5 ) + sprintf( buf+strlen(buf), " nv = '%" PRId64 "'\n", nim->nv ); + if( nim->ndim > 6 ) + sprintf( buf+strlen(buf), " nw = '%" PRId64 "'\n", nim->nw ); + + sprintf( buf+strlen(buf), " dx = '%g'\n", nim->dx ); + if( nim->ndim > 1 ) sprintf( buf+strlen(buf), " dy = '%g'\n", nim->dy ); + if( nim->ndim > 2 ) sprintf( buf+strlen(buf), " dz = '%g'\n", nim->dz ); + if( nim->ndim > 3 ) sprintf( buf+strlen(buf), " dt = '%g'\n", nim->dt ); + if( nim->ndim > 4 ) sprintf( buf+strlen(buf), " du = '%g'\n", nim->du ); + if( nim->ndim > 5 ) sprintf( buf+strlen(buf), " dv = '%g'\n", nim->dv ); + if( nim->ndim > 6 ) sprintf( buf+strlen(buf), " dw = '%g'\n", nim->dw ); + + sprintf( buf+strlen(buf) , " datatype = '%d'\n" , nim->datatype ) ; + sprintf( buf+strlen(buf) , " datatype_name = '%s'\n" , + nifti_datatype_string(nim->datatype) ) ; + + sprintf( buf+strlen(buf) , " nvox = '%" PRId64 "'\n" , nim->nvox ) ; + sprintf( buf+strlen(buf) , " nbyper = '%d'\n" , nim->nbyper ) ; + + sprintf( buf+strlen(buf) , " byteorder = '%s'\n" , + (nim->byteorder==MSB_FIRST) ? "MSB_FIRST" : "LSB_FIRST" ) ; + + if( nim->cal_min < nim->cal_max ){ + sprintf( buf+strlen(buf) , " cal_min = '%g'\n", nim->cal_min ) ; + sprintf( buf+strlen(buf) , " cal_max = '%g'\n", nim->cal_max ) ; + } + + if( nim->scl_slope != 0.0 ){ + sprintf( buf+strlen(buf) , " scl_slope = '%g'\n" , nim->scl_slope ) ; + sprintf( buf+strlen(buf) , " scl_inter = '%g'\n" , nim->scl_inter ) ; + } + + if( nim->intent_code > 0 ){ + sprintf( buf+strlen(buf) , " intent_code = '%d'\n", nim->intent_code ) ; + sprintf( buf+strlen(buf) , " intent_code_name = '%s'\n" , + nifti_intent_string(nim->intent_code) ) ; + sprintf( buf+strlen(buf) , " intent_p1 = '%g'\n" , nim->intent_p1 ) ; + sprintf( buf+strlen(buf) , " intent_p2 = '%g'\n" , nim->intent_p2 ) ; + sprintf( buf+strlen(buf) , " intent_p3 = '%g'\n" , nim->intent_p3 ) ; + + if( nim->intent_name[0] != '\0' ){ + ebuf = escapize_string(nim->intent_name) ; + sprintf( buf+strlen(buf) , " intent_name = %s\n",ebuf) ; + free(ebuf) ; + } + } + + if( nim->toffset != 0.0 ) + sprintf( buf+strlen(buf) , " toffset = '%g'\n",nim->toffset ) ; + + if( nim->xyz_units > 0 ) + sprintf( buf+strlen(buf) , + " xyz_units = '%d'\n" + " xyz_units_name = '%s'\n" , + nim->xyz_units , nifti_units_string(nim->xyz_units) ) ; + + if( nim->time_units > 0 ) + sprintf( buf+strlen(buf) , + " time_units = '%d'\n" + " time_units_name = '%s'\n" , + nim->time_units , nifti_units_string(nim->time_units) ) ; + + if( nim->freq_dim > 0 ) + sprintf( buf+strlen(buf) , " freq_dim = '%d'\n",nim->freq_dim ) ; + if( nim->phase_dim > 0 ) + sprintf( buf+strlen(buf) , " phase_dim = '%d'\n",nim->phase_dim ) ; + if( nim->slice_dim > 0 ) + sprintf( buf+strlen(buf) , " slice_dim = '%d'\n",nim->slice_dim ) ; + if( nim->slice_code > 0 ) + sprintf( buf+strlen(buf) , + " slice_code = '%d'\n" + " slice_code_name = '%s'\n" , + nim->slice_code , nifti_slice_string(nim->slice_code) ) ; + if( nim->slice_start >= 0 && nim->slice_end > nim->slice_start ) + sprintf( buf+strlen(buf) , + " slice_start = '%" PRId64 "'\n" + " slice_end = '%" PRId64 "'\n", + nim->slice_start , nim->slice_end ) ; + if( nim->slice_duration != 0.0 ) + sprintf( buf+strlen(buf) , " slice_duration = '%g'\n", + nim->slice_duration ) ; + + if( nim->descrip[0] != '\0' ){ + ebuf = escapize_string(nim->descrip) ; + sprintf( buf+strlen(buf) , " descrip = %s\n",ebuf) ; + free(ebuf) ; + } + + if( nim->aux_file[0] != '\0' ){ + ebuf = escapize_string(nim->aux_file) ; + sprintf( buf+strlen(buf) , " aux_file = %s\n",ebuf) ; + free(ebuf) ; + } + + if( nim->qform_code > 0 ){ + int i,j,k ; + + sprintf( buf+strlen(buf) , + " qform_code = '%d'\n" + " qform_code_name = '%s'\n" + " qto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->qform_code , nifti_xform_string(nim->qform_code) , + nim->qto_xyz.m[0][0] , nim->qto_xyz.m[0][1] , + nim->qto_xyz.m[0][2] , nim->qto_xyz.m[0][3] , + nim->qto_xyz.m[1][0] , nim->qto_xyz.m[1][1] , + nim->qto_xyz.m[1][2] , nim->qto_xyz.m[1][3] , + nim->qto_xyz.m[2][0] , nim->qto_xyz.m[2][1] , + nim->qto_xyz.m[2][2] , nim->qto_xyz.m[2][3] , + nim->qto_xyz.m[3][0] , nim->qto_xyz.m[3][1] , + nim->qto_xyz.m[3][2] , nim->qto_xyz.m[3][3] ) ; + + sprintf( buf+strlen(buf) , + " qto_ijk_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->qto_ijk.m[0][0] , nim->qto_ijk.m[0][1] , + nim->qto_ijk.m[0][2] , nim->qto_ijk.m[0][3] , + nim->qto_ijk.m[1][0] , nim->qto_ijk.m[1][1] , + nim->qto_ijk.m[1][2] , nim->qto_ijk.m[1][3] , + nim->qto_ijk.m[2][0] , nim->qto_ijk.m[2][1] , + nim->qto_ijk.m[2][2] , nim->qto_ijk.m[2][3] , + nim->qto_ijk.m[3][0] , nim->qto_ijk.m[3][1] , + nim->qto_ijk.m[3][2] , nim->qto_ijk.m[3][3] ) ; + + sprintf( buf+strlen(buf) , + " quatern_b = '%g'\n" + " quatern_c = '%g'\n" + " quatern_d = '%g'\n" + " qoffset_x = '%g'\n" + " qoffset_y = '%g'\n" + " qoffset_z = '%g'\n" + " qfac = '%g'\n" , + nim->quatern_b , nim->quatern_c , nim->quatern_d , + nim->qoffset_x , nim->qoffset_y , nim->qoffset_z , nim->qfac ) ; + + nifti_dmat44_to_orientation( nim->qto_xyz , &i,&j,&k ) ; + if( i > 0 && j > 0 && k > 0 ) + sprintf( buf+strlen(buf) , + " qform_i_orientation = '%s'\n" + " qform_j_orientation = '%s'\n" + " qform_k_orientation = '%s'\n" , + nifti_orientation_string(i) , + nifti_orientation_string(j) , + nifti_orientation_string(k) ) ; + } + + if( nim->sform_code > 0 ){ + int i,j,k ; + + sprintf( buf+strlen(buf) , + " sform_code = '%d'\n" + " sform_code_name = '%s'\n" + " sto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->sform_code , nifti_xform_string(nim->sform_code) , + nim->sto_xyz.m[0][0] , nim->sto_xyz.m[0][1] , + nim->sto_xyz.m[0][2] , nim->sto_xyz.m[0][3] , + nim->sto_xyz.m[1][0] , nim->sto_xyz.m[1][1] , + nim->sto_xyz.m[1][2] , nim->sto_xyz.m[1][3] , + nim->sto_xyz.m[2][0] , nim->sto_xyz.m[2][1] , + nim->sto_xyz.m[2][2] , nim->sto_xyz.m[2][3] , + nim->sto_xyz.m[3][0] , nim->sto_xyz.m[3][1] , + nim->sto_xyz.m[3][2] , nim->sto_xyz.m[3][3] ) ; + + sprintf( buf+strlen(buf) , + " sto_ijk matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , + nim->sto_ijk.m[0][0] , nim->sto_ijk.m[0][1] , + nim->sto_ijk.m[0][2] , nim->sto_ijk.m[0][3] , + nim->sto_ijk.m[1][0] , nim->sto_ijk.m[1][1] , + nim->sto_ijk.m[1][2] , nim->sto_ijk.m[1][3] , + nim->sto_ijk.m[2][0] , nim->sto_ijk.m[2][1] , + nim->sto_ijk.m[2][2] , nim->sto_ijk.m[2][3] , + nim->sto_ijk.m[3][0] , nim->sto_ijk.m[3][1] , + nim->sto_ijk.m[3][2] , nim->sto_ijk.m[3][3] ) ; + + nifti_dmat44_to_orientation( nim->sto_xyz , &i,&j,&k ) ; + if( i > 0 && j > 0 && k > 0 ) + sprintf( buf+strlen(buf) , + " sform_i_orientation = '%s'\n" + " sform_j_orientation = '%s'\n" + " sform_k_orientation = '%s'\n" , + nifti_orientation_string(i) , + nifti_orientation_string(j) , + nifti_orientation_string(k) ) ; + } + + sprintf( buf+strlen(buf) , " num_ext = '%d'\n", nim->num_ext ) ; + + sprintf( buf+strlen(buf) , "/>\n" ) ; /* XML-ish closer */ + + nbuf = (int)strlen(buf) ; + buf = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */ + if( !buf ) Rc_fprintf_stderr("** NIFTI NITA: failed to realloc %d bytes\n", + nbuf+1); + return buf ; +#endif +} +#ifdef __clang__ +#pragma clang diagnostic pop +#endif // __clang__ + +/*---------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------*/ +/*! get the byte order for this CPU + + - LSB_FIRST means least significant byte, first (little endian) + - MSB_FIRST means most significant byte, first (big endian) +*//*--------------------------------------------------------------------*/ +int nifti_short_order(void) /* determine this CPU's byte order */ +{ + union { unsigned char bb[2] ; + short ss ; } fred ; + + fred.bb[0] = 1 ; fred.bb[1] = 0 ; + + return (fred.ss == 1) ? LSB_FIRST : MSB_FIRST ; +} + +/*---------------------------------------------------------------------------*/ + +#undef QQNUM +#undef QNUM +#undef QSTR + +/* macro to check lhs string against "n1"; if it matches, + interpret rhs string as a number, and put it into nim->"n2" */ + +#define QQNUM(n1,n2,tt) if( strcmp(lhs,#n1)==0 ) nim->n2=(tt)strtod(rhs,NULL) + +/* same, but where "n1" == "n2" */ + +#define QNUM(nam,tt) QQNUM(nam,nam,tt) + +/* macro to check lhs string against "nam"; if it matches, + put rhs string into nim->"nam" string, with max length = "ml" */ + +#define QSTR(nam,ml) if( strcmp(lhs,#nam) == 0 ) \ + strncpy(nim->nam,rhs,ml), nim->nam[ml]='\0' + +/*---------------------------------------------------------------------------*/ +/*! Take an XML-ish ASCII string and create a NIFTI image header to match. + + NULL is returned if enough information isn't present in the input string. + - The image data can later be loaded with nifti_image_load(). + - The struct returned here can be liberated with nifti_image_free(). + - Not a lot of error checking is done here to make sure that the + input values are reasonable! +*//*-------------------------------------------------------------------------*/ +nifti_image *nifti2_image_from_ascii( const char *str, int * bytes_read ) +{ + char lhs[1024] , rhs[1024] ; + int ii , spos, nn ; + nifti_image *nim ; /* will be output */ + + if( str == NULL || *str == '\0' ) return NULL ; /* bad input!? */ + + /* scan for opening string */ + + spos = 0 ; + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"nx = nim->ny = nim->nz = nim->nt + = nim->nu = nim->nv = nim->nw = 1 ; + nim->dx = nim->dy = nim->dz = nim->dt + = nim->du = nim->dv = nim->dw = 0 ; + nim->qfac = 1.0f ; + + nim->byteorder = nifti_short_order() ; + + /* starting at str[spos], scan for "equations" of the form + lhs = 'rhs' + and assign rhs values into the struct component named by lhs */ + + while(1){ + + while( isspace((int) str[spos]) ) spos++ ; /* skip whitespace */ + if( str[spos] == '\0' ) break ; /* end of string? */ + + /* get lhs string */ + + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ + + /* skip whitespace and the '=' marker */ + + while( isspace((int) str[spos]) || str[spos] == '=' ) spos++ ; + if( str[spos] == '\0' ) break ; /* end of string? */ + + /* if next character is a quote ', copy everything up to next ' + otherwise, copy everything up to next nonblank */ + + if( str[spos] == '\'' ){ + ii = spos+1 ; + while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ; + nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ; + memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; + spos = (str[ii] == '\'') ? ii+1 : ii ; + } else { + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; + if( ii == 0 ) break ; /* nothing found? */ + } + unescape_string(rhs) ; /* remove any XML escape sequences */ + + /* Now can do the assignment, based on lhs string. + Start with special cases that don't fit the QNUM/QSTR macros. */ + + if( strcmp(lhs,"nifti_type") == 0 ){ + if( strcmp(rhs,"ANALYZE-7.5") == 0 ) + nim->nifti_type = NIFTI_FTYPE_ANALYZE ; + else if( strcmp(rhs,"NIFTI-1+") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI1_1 ; + else if( strcmp(rhs,"NIFTI-1") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI1_2 ; + else if( strcmp(rhs,"NIFTI-1A") == 0 ) + nim->nifti_type = NIFTI_FTYPE_ASCII ; + else if( strcmp(rhs,"NIFTI-2+") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI2_1 ; + else if( strcmp(rhs,"NIFTI-2") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI2_2 ; + } + else if( strcmp(lhs,"header_filename") == 0 ){ + nim->fname = nifti_strdup(rhs) ; + } + else if( strcmp(lhs,"image_filename") == 0 ){ + nim->iname = nifti_strdup(rhs) ; + } + else if( strcmp(lhs,"sto_xyz_matrix") == 0 ){ + sscanf( rhs , "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf" , + &(nim->sto_xyz.m[0][0]) , &(nim->sto_xyz.m[0][1]) , + &(nim->sto_xyz.m[0][2]) , &(nim->sto_xyz.m[0][3]) , + &(nim->sto_xyz.m[1][0]) , &(nim->sto_xyz.m[1][1]) , + &(nim->sto_xyz.m[1][2]) , &(nim->sto_xyz.m[1][3]) , + &(nim->sto_xyz.m[2][0]) , &(nim->sto_xyz.m[2][1]) , + &(nim->sto_xyz.m[2][2]) , &(nim->sto_xyz.m[2][3]) , + &(nim->sto_xyz.m[3][0]) , &(nim->sto_xyz.m[3][1]) , + &(nim->sto_xyz.m[3][2]) , &(nim->sto_xyz.m[3][3]) ) ; + } + else if( strcmp(lhs,"byteorder") == 0 ){ + if( strcmp(rhs,"MSB_FIRST") == 0 ) nim->byteorder = MSB_FIRST ; + if( strcmp(rhs,"LSB_FIRST") == 0 ) nim->byteorder = LSB_FIRST ; + } + else QQNUM(image_offset,iname_offset,int) ; + else QNUM(datatype,short int) ; + else QNUM(ndim,int) ; + else QNUM(nx,int) ; + else QNUM(ny,int) ; + else QNUM(nz,int) ; + else QNUM(nt,int) ; + else QNUM(nu,int) ; + else QNUM(nv,int) ; + else QNUM(nw,int) ; + else QNUM(dx,float) ; + else QNUM(dy,float) ; + else QNUM(dz,float) ; + else QNUM(dt,float) ; + else QNUM(du,float) ; + else QNUM(dv,float) ; + else QNUM(dw,float) ; + else QNUM(cal_min,float) ; + else QNUM(cal_max,float) ; + else QNUM(scl_slope,float) ; + else QNUM(scl_inter,float) ; + else QNUM(intent_code,short) ; + else QNUM(intent_p1,float) ; + else QNUM(intent_p2,float) ; + else QNUM(intent_p3,float) ; + else QSTR(intent_name,15) ; + else QNUM(toffset,float) ; + else QNUM(xyz_units,int) ; + else QNUM(time_units,int) ; + else QSTR(descrip,79) ; + else QSTR(aux_file,23) ; + else QNUM(qform_code,int) ; + else QNUM(quatern_b,float) ; + else QNUM(quatern_c,float) ; + else QNUM(quatern_d,float) ; + else QNUM(qoffset_x,float) ; + else QNUM(qoffset_y,float) ; + else QNUM(qoffset_z,float) ; + else QNUM(qfac,float) ; + else QNUM(sform_code,int) ; + else QNUM(freq_dim,int) ; + else QNUM(phase_dim,int) ; + else QNUM(slice_dim,int) ; + else QNUM(slice_code,int) ; + else QNUM(slice_start,int) ; + else QNUM(slice_end,int) ; + else QNUM(slice_duration,float) ; + else QNUM(num_ext,int) ; + + } /* end of while loop */ + + if( bytes_read ) *bytes_read = spos+1; /* "process" last '\n' */ + + /* do miscellaneous checking and cleanup */ + + if( nim->ndim <= 0 ){ nifti_image_free(nim); return NULL; } /* bad! */ + + nifti_datatype_sizes( nim->datatype, &(nim->nbyper), &(nim->swapsize) ); + if( nim->nbyper == 0 ){ nifti_image_free(nim); return NULL; } /* bad! */ + + nim->dim[0] = nim->ndim ; + nim->dim[1] = nim->nx ; nim->pixdim[1] = nim->dx ; + nim->dim[2] = nim->ny ; nim->pixdim[2] = nim->dy ; + nim->dim[3] = nim->nz ; nim->pixdim[3] = nim->dz ; + nim->dim[4] = nim->nt ; nim->pixdim[4] = nim->dt ; + nim->dim[5] = nim->nu ; nim->pixdim[5] = nim->du ; + nim->dim[6] = nim->nv ; nim->pixdim[6] = nim->dv ; + nim->dim[7] = nim->nw ; nim->pixdim[7] = nim->dw ; + + nim->nvox = (int64_t)nim->nx * nim->ny * nim->nz + * nim->nt * nim->nu * nim->nv * nim->nw ; + + if( nim->qform_code > 0 ) + nim->qto_xyz = nifti_quatern_to_dmat44( + nim->quatern_b, nim->quatern_c, nim->quatern_d, + nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, + nim->dx , nim->dy , nim->dz , + nim->qfac ) ; + else + nim->qto_xyz = nifti_quatern_to_dmat44( + 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , + nim->dx , nim->dy , nim->dz , 0.0 ) ; + + + nim->qto_ijk = nifti_dmat44_inverse( nim->qto_xyz ) ; + + if( nim->sform_code > 0 ) + nim->sto_ijk = nifti_dmat44_inverse( nim->sto_xyz ) ; + + return nim ; +} + + +/*---------------------------------------------------------------------------*/ +/*! validate the nifti_image + + \return 1 if the structure seems valid, otherwise 0 + + \sa nifti_nim_has_valid_dims, nifti_hdr1_looks_good +*//*-------------------------------------------------------------------------*/ +int nifti2_nim_is_valid(nifti_image * nim, int complain) +{ + int errs = 0; + + if( !nim ){ + Rc_fprintf_stderr("** NIFTI is_valid_nim: nim is NULL\n"); + return 0; + } + + if( g_opts.debug > 2 ) Rc_fprintf_stderr("-d nim_is_valid check...\n"); + + /**- check that dim[] matches the individual values ndim, nx, ny, ... */ + if( ! nifti_nim_has_valid_dims(nim,complain) ){ + if( !complain ) return 0; + errs++; + } + + /* might check nbyper, pixdim, q/sforms, swapsize, nifti_type, ... */ + + /**- be explicit in return of 0 or 1 */ + if( errs > 0 ) return 0; + else return 1; +} + +/*---------------------------------------------------------------------------*/ +/*! validate nifti dimensions + + \return 1 if valid, 0 if not + + \sa nifti_nim_is_valid, nifti_hdr1_looks_good + + rely on dim[] as the master +*//*-------------------------------------------------------------------------*/ +int nifti2_nim_has_valid_dims(nifti_image * nim, int complain) +{ + int64_t prod, c; + int errs = 0; + + /**- start with dim[0]: failure here is considered terminal */ + if( nim->dim[0] <= 0 || nim->dim[0] > 7 ){ + errs++; + if( complain ) + Rc_fprintf_stderr("** NIFTI NVd: dim[0] (%" PRId64 + ") out of range [1,7]\n", nim->dim[0]); + return 0; + } + + /**- check whether ndim equals dim[0] */ + if( nim->ndim != nim->dim[0] ){ + errs++; + if( ! complain ) return 0; + Rc_fprintf_stderr("** NIFTI NVd: ndim != dim[0] (%" PRId64 ",%" PRId64 ")\n", + nim->ndim,nim->dim[0]); + } + + /**- compare each dim[i] to the proper nx, ny, ... */ + if( ( (nim->dim[0] >= 1) && (nim->dim[1] != nim->nx) ) || + ( (nim->dim[0] >= 2) && (nim->dim[2] != nim->ny) ) || + ( (nim->dim[0] >= 3) && (nim->dim[3] != nim->nz) ) || + ( (nim->dim[0] >= 4) && (nim->dim[4] != nim->nt) ) || + ( (nim->dim[0] >= 5) && (nim->dim[5] != nim->nu) ) || + ( (nim->dim[0] >= 6) && (nim->dim[6] != nim->nv) ) || + ( (nim->dim[0] >= 7) && (nim->dim[7] != nim->nw) ) ){ + errs++; + if( !complain ) return 0; + Rc_fprintf_stderr("** NIFTI NVd mismatch: dims = %" PRId64 ",%" PRId64 + ",%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 "\n" + " nxyz... = %" PRId64 ",%" PRId64 ",%" PRId64 + ",%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 "\n", + nim->dim[1], nim->dim[2], nim->dim[3], + nim->dim[4], nim->dim[5], nim->dim[6], nim->dim[7], + nim->nx, nim->ny, nim->nz, + nim->nt, nim->nu, nim->nv, nim->nw ); + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("-d check dim[%" PRId64 "] =", nim->dim[0]); + for( c = 0; c < 7; c++ ) Rc_fprintf_stderr(" %" PRId64 "", nim->dim[c]); + Rc_fputc_stderr('\n'); + } + + /**- check the dimensions, and that their product matches nvox */ + prod = 1; + for( c = 1; c <= nim->dim[0]; c++ ){ + if( nim->dim[c] > 0) + prod *= nim->dim[c]; + else if( nim->dim[c] <= 0 ){ + if( !complain ) return 0; + Rc_fprintf_stderr("** NIFTI NVd: dim[%" PRId64 "] (=%" PRId64 ") <= 0\n", + c, nim->dim[c]); + errs++; + } + } + if( prod != nim->nvox ){ + if( ! complain ) return 0; + Rc_fprintf_stderr("** NIFTI NVd: nvox does not match %" PRId64 + "-dim product (%" PRId64 ", %" PRId64 ")\n", + nim->dim[0], nim->nvox, prod); + errs++; + } + + /**- if debug, warn about any remaining dim that is neither 0, nor 1 */ + /* (values in dims above dim[0] are undefined, as reminded by Cinly + Ooi and Alle Meije Wink) 16 Nov 2005 [rickr] */ + if( g_opts.debug > 1 ) + for( c = nim->dim[0]+1; c <= 7; c++ ) + if( nim->dim[c] != 0 && nim->dim[c] != 1 ) + Rc_fprintf_stderr("** NIFTI NVd warning: dim[%" PRId64 "] = %" PRId64 + ", but ndim = %" PRId64 "\n", + c, nim->dim[c], nim->dim[0]); + + if( g_opts.debug > 2 ) + Rc_fprintf_stderr("-d nim_has_valid_dims check, errs = %d\n", errs); + + /**- return invalid or valid */ + if( errs > 0 ) return 0; + else return 1; +} + + +/*---------------------------------------------------------------------------*/ +/*! read a nifti image, collapsed across dimensions according to dims[8]
+
+    This function may be used to read parts of a nifti dataset, such as
+    the time series for a single voxel, or perhaps a slice.  It is similar
+    to nifti_image_load(), though the passed 'data' parameter is used for
+    returning the image, not nim->data.
+
+    \param nim  given nifti_image struct, corresponding to the data file
+    \param dims given list of dimensions (see below)
+    \param data pointer to data pointer (if *data is NULL, data will be
+                allocated, otherwise not)
+
+    Here, dims is an array of 8 ints, similar to nim->dim[8].  While dims[0]
+    is unused at this point, the other indices specify which dimensions to
+    collapse (and at which index), and which not to collapse.  If dims[i] is
+    set to -1, then that entire dimension will be read in, from index 0 to
+    index (nim->dim[i] - 1).  If dims[i] >= 0, then only that index will be
+    read in (so dims[i] must also be < nim->dim[i]).
+
+    Example: given  nim->dim[8] = { 4, 64, 64, 21, 80, 1, 1, 1 } (4-D dataset)
+
+      if dims[8] = { 0,  5,  4, 17, -1, -1, -1, -1 }
+         -> read time series for voxel i,j,k = 5,4,17
+
+      if dims[8] = { 0, -1, -1, -1, 17, -1, -1, -1 }
+         -> read single volume at time point 17
+
+    Example: given  nim->dim[8] = { 6, 64, 64, 21, 80, 4, 3, 1 } (6-D dataset)
+
+      if dims[8] = { 0, 5, 4, 17, -1, 2, 1, 0 }
+         -> read time series for the voxel i,j,k = 5,4,17, and dim 5,6 = 2,1
+
+      if dims[8] = { 0, 5, 4, -1, -1, 0, 0, 0 }
+         -> read time series for slice at i,j = 5,4, and dim 5,6,7 = 0,0,0
+            (note that dims[7] is not relevant, but must be 0 or -1)
+
+    If *data is NULL, then *data will be set as a pointer to new memory,
+    allocated here for the resulting collapsed image data.
+
+      e.g. { int    dims[8] = { 0,  5,  4, 17, -1, -1, -1, -1 };
+             void * data    = NULL;
+             ret_val = nifti_read_collapsed_image(nim, dims, &data);
+             if( ret_val > 0 ){
+                process_time_series(data);
+                if( data != NULL ) free(data);
+             }
+           }
+
+    NOTE: If *data is not NULL, then it will be assumed that it points to
+          valid memory, sufficient to hold the results.  This is done for
+          speed and possibly repeated calls to this function.
+
+      e.g. { int64_t dims[8] = { 0,  -1, -1, -1, -1, -1, -1, -1 };
+             void  * data    = NULL;
+             for( zslice = 0; zslice < nzslices; zslice++ ){
+                dims[3] = zslice;
+                ret_val = nifti_read_collapsed_image(nim, dims, &data);
+                if( ret_val > 0 ) process_slice(zslice, data);
+             }
+             if( data != NULL ) free(data);
+           }
+
+    \return
+        -  the total number of bytes read, or < 0 on failure
+        -  the read and byte-swapped data, in 'data'            
+ + \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks + nifti_image_load +*//*-------------------------------------------------------------------------*/ +int64_t nifti2_read_collapsed_image( nifti_image * nim, const int64_t dims [8], + void ** data ) +{ + znzFile fp; + int64_t prods[8]; /* sizes are bounded by dims[], so 8 */ + int pivots[8], nprods; /* sizes are bounded by dims[], so 8 */ + int64_t c, bytes; + + /** - check pointers for sanity */ + if( !nim || !dims || !data ){ + Rc_fprintf_stderr("** nifti_RCI: bad params %p, %p, %p\n", + (void *)nim, (const void *)dims, (void *)data); + return -1; + } + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("-d read_collapsed_image:\n dims ="); + for(c = 0; c < 8; c++) Rc_fprintf_stderr(" %3" PRId64 "", dims[c]); + Rc_fprintf_stderr("\n nim->dims ="); + for(c = 0; c < 8; c++) Rc_fprintf_stderr(" %3" PRId64 "", nim->dim[c]); + Rc_fputc_stderr('\n'); + } + + /** - verify that dim[] makes sense */ + if( ! nifti_nim_is_valid(nim, g_opts.debug > 0) ){ + Rc_fprintf_stderr("** NIFTI: invalid nim (file is '%s')\n", nim->fname ); + return -1; + } + + /** - verify that dims[] makes sense for this dataset */ + for( c = 1; c <= nim->dim[0]; c++ ){ + if( dims[c] >= nim->dim[c] ){ + Rc_fprintf_stderr("** nifti_RCI: dims[%" PRId64 "] >= nim->dim[%" PRId64 + "] (%" PRId64 ",%" PRId64 ")\n", + c, c, dims[c], nim->dim[c]); + return -1; + } + } + + /** - prepare pivot list - pivots are fixed indices */ + if( make_pivot_list(nim, dims, pivots, prods, &nprods) < 0 ) return -1; + + bytes = rci_alloc_mem(data, prods, nprods, nim->nbyper); + if( bytes < 0 ) return -1; + + /** - open the image file for reading at the appropriate offset */ + fp = nifti_image_load_prep( nim ); + if( ! fp ){ free(*data); *data = NULL; return -1; } /* failure */ + + /** - call the recursive reading function, passing nim, the pivot info, + location to store memory, and file pointer and position */ + c = rci_read_data(nim, pivots, prods, nprods, dims, (char *)*data, fp, + znztell(fp)); + znzclose(fp); /* in any case, close the file */ + if( c < 0 ){ free(*data); *data = NULL; return -1; } /* failure */ + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d read %" PRId64 " bytes of collapsed image from %s\n", + bytes, nim->fname); + + return bytes; +} + + +/* local function to find strides per dimension. assumes 7D size and +** stride array. +*/ +static void +compute_strides(int64_t *strides,const int64_t *size,int nbyper) +{ + int i; + strides[0] = nbyper; + for(i = 1; i < 7; i++) + { + strides[i] = size[i-1] * strides[i-1]; + } +} + +/*---------------------------------------------------------------------------*/ +/*! read an arbitrary subregion from a nifti image + + This function may be used to read a single arbitary subregion of any + rectangular size from a nifti dataset, such as a small 5x5x5 subregion + around the center of a 3D image. + + \param nim given nifti_image struct, corresponding to the data file + \param start_index the index location of first voxel that will be returned + \param region_size the size of the subregion to be returned + \param data pointer to data pointer (if *data is NULL, data will be + allocated, otherwise not) + + Example: given nim->dim[8] = {3, 64, 64, 64, 1, 1, 1, 1 } (3-D dataset) + + if start_index[7] = { 29, 29, 29, 0, 0, 0, 0 } and + region_size[7] = { 5, 5, 5, 1, 1, 1, 1 } + -> read 5x5x5 region starting with the first voxel at (29,29,29) + + NOTE: If *data is not NULL, then it will be assumed that it points to + valid memory, sufficient to hold the results. This is done for + speed and possibly repeated calls to this function. + \return + - the total number of bytes read, or < 0 on failure + - the read and byte-swapped data, in 'data' + + \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks + nifti_image_load, nifti_read_collapsed_image +*//*-------------------------------------------------------------------------*/ +int64_t nifti2_read_subregion_image( nifti_image * nim, + const int64_t *start_index, + const int64_t *region_size, + void ** data ) +{ + znzFile fp; /* file to read */ + int64_t i,j,k,l,m,n; /* indices for dims */ + int64_t bytes = 0; /* total # bytes read */ + int64_t total_alloc_size; /* size of buffer allocation */ + char *readptr; /* where in *data to read next */ + int64_t strides[7]; /* strides between dimensions */ + int64_t collapsed_dims[8]; /* for read_collapsed_image */ + int64_t *image_size; /* pointer to dimensions in header */ + int64_t initial_offset; + int64_t offset; /* seek offset for reading current row */ + + /* probably ignored, but set to ndim for consistency*/ + collapsed_dims[0] = nim->ndim; + + /* build a dims array for collapsed image read */ + for(i = 0; i < nim->ndim; i++) { + /* if you take the whole extent in this dimension */ + if(start_index[i] == 0 && region_size[i] == nim->dim[i+1]) + collapsed_dims[i+1] = -1; + /* if you specify a single element in this dimension */ + else if(region_size[i] == 1) + collapsed_dims[i+1] = start_index[i]; + else + collapsed_dims[i+1] = -2; /* sentinel value */ + } + /* fill out end of collapsed_dims */ + for(i = nim->ndim ; i < 7; i++) + collapsed_dims[i+1] = -1; + + /* check to see whether collapsed read is possible */ + for(i = 1; i <= nim->ndim; i++) + if(collapsed_dims[i] == -2) break; + + /* if you get through all the dimensions without hitting + ** a subrange of size > 1, a collapsed read is possible + */ + if(i > nim->ndim) + return nifti_read_collapsed_image(nim, collapsed_dims, data); + + /* point past first element of dim, which holds nim->ndim */ + image_size = &(nim->dim[1]); + + /* check region sizes for sanity */ + for(i = 0; i < nim->ndim; i++) + if(start_index[i] + region_size[i] > image_size[i]) { + if(g_opts.debug > 1) + Rc_fprintf_stderr("region doesn't fit within image size\n"); + return -1; + } + + /* get the file open */ + fp = nifti_image_load_prep( nim ); + /* the current offset is just past the nifti header, save + * location so that SEEK_SET can be used below + */ + initial_offset = znztell(fp); + /* get strides*/ + compute_strides(strides,image_size,nim->nbyper); + + total_alloc_size = nim->nbyper; /* size of pixel */ + + /* find alloc size */ + for(i = 0; i < nim->ndim; i++) total_alloc_size *= region_size[i]; + + /* allocate buffer, if necessary */ + if(! *data) *data = malloc(total_alloc_size); + + if(! *data) { + if(g_opts.debug > 1) + Rc_fprintf_stderr("allocation of %" PRId64 " bytes failed\n", + total_alloc_size); + return -1; + } + + /* point to start of data buffer as char * */ + readptr = *((char **)data); + { + /* can't assume that start_index and region_size have any more than + ** nim->ndim elements so make local copies, filled out to seven elements + */ + int64_t si[7], rs[7]; + for(i = 0; i < nim->ndim; i++) { + si[i] = start_index[i]; + rs[i] = region_size[i]; + } + for(i = nim->ndim; i < 7; i++) { + si[i] = 0; + rs[i] = 1; + } + + /* loop through subregion and read a row at a time */ + for(i = si[6]; i < (si[6] + rs[6]); i++) { + for(j = si[5]; j < (si[5] + rs[5]); j++) { + for(k = si[4]; k < (si[4] + rs[4]); k++) { + for(l = si[3]; l < (si[3] + rs[3]); l++) { + for(m = si[2]; m < (si[2] + rs[2]); m++) { + for(n = si[1]; n < (si[1] + rs[1]); n++) { + int64_t nread,read_amount; + offset = initial_offset + + (i * strides[6]) + + (j * strides[5]) + + (k * strides[4]) + + (l * strides[3]) + + (m * strides[2]) + + (n * strides[1]) + + (si[0] * strides[0]); + znzseek(fp, offset, SEEK_SET); /* seek to current row */ + read_amount = rs[0] * nim->nbyper; /* read a row of subregion */ + nread = nifti_read_buffer(fp, readptr, read_amount, nim); + if(nread != read_amount) { + if(g_opts.debug > 1) { + Rc_fprintf_stderr("read of %" PRId64 " bytes failed\n", + read_amount); + return -1; + } + } + bytes += nread; + readptr += read_amount; + } + } + } + } + } + } + } + znzclose(fp); + return bytes; +} + + +/* read the data from the file pointed to by fp + + - this a recursive function, so start with the base case + - data is now (char *) for easy incrementing + + return 0 on success, < 0 on failure +*/ +static int rci_read_data(nifti_image * nim, int * pivots, int64_t * prods, + int nprods, const int64_t dims[], char * data, + znzFile fp, int64_t base_offset) +{ + int64_t sublen, offset, read_size; + int c; + + /* bad check first - base_offset may not have been checked */ + if( nprods <= 0 ){ + Rc_fprintf_stderr("** NIFTI rci_read_data, bad prods, %d\n", nprods); + return -1; + } + + /* base case: actually read the data */ + if( nprods == 1 ){ + int64_t nread, bytes; + + /* make sure things look good here */ + if( *pivots != 0 ){ + Rc_fprintf_stderr("** NIFTI rciRD: final pivot == %d!\n", *pivots); + return -1; + } + + /* so just seek and read (prods[0] * nbyper) bytes from the file */ + znzseek(fp, base_offset, SEEK_SET); + bytes = prods[0] * nim->nbyper; + nread = nifti_read_buffer(fp, data, bytes, nim); + if( nread != bytes ){ + Rc_fprintf_stderr("** NIFTI rciRD: read only %" PRId64 " of %" PRId64 + " bytes from '%s'\n", + nread, bytes, nim->fname); + return -1; + } else if( g_opts.debug > 3 ) + Rc_fprintf_stderr("+d successful read of %" PRId64 + " bytes at offset %" PRId64 "\n", + bytes, base_offset); + + return 0; /* done with base case - return success */ + } + + /* not the base case, so do a set of reduced reads */ + + /* compute size of sub-brick: all dimensions below pivot */ + for( c = 1, sublen = 1; c < *pivots; c++ ) sublen *= nim->dim[c]; + + /* compute number of values to read, i.e. remaining prods */ + for( c = 1, read_size = 1; c < nprods; c++ ) read_size *= prods[c]; + read_size *= nim->nbyper; /* and multiply by bytes per voxel */ + + /* now repeatedly compute offsets, and recursively read */ + for( c = 0; c < prods[0]; c++ ){ + /* offset is (c * sub-block size (including pivot dim)) */ + /* + (dims[] index into pivot sub-block) */ + /* the unneeded multiplication is to make this more clear */ + offset = (int64_t)c * sublen * nim->dim[*pivots] + + (int64_t)sublen * dims[*pivots]; + offset *= nim->nbyper; + + if( g_opts.debug > 3 ) + Rc_fprintf_stderr("-d reading %" PRId64 " bytes, foff %" PRId64 + " + %" PRId64 ", doff %" PRId64 "\n", + read_size, base_offset, offset, c*read_size); + + /* now read the next level down, adding this offset */ + if( rci_read_data(nim, pivots+1, prods+1, nprods-1, dims, + data + c * read_size, fp, base_offset + offset) < 0 ) + return -1; + } + + return 0; +} + + +/* allocate memory for all collapsed image data + + If *data is already set, do not allocate, but still calculate + size for debug report. + + return total size on success, and < 0 on failure +*/ +static int rci_alloc_mem(void **data, const int64_t prods[8], int nprods, int nbyper ) +{ + int64_t size; + int memindex; + + if( nbyper < 0 || nprods < 1 || nprods > 8 ){ + Rc_fprintf_stderr("** NIFTI rci_am: bad params, %d, %d\n", nbyper, nprods); + return -1; + } + + for( memindex = 0, size = 1; memindex < nprods; memindex++ ) + size *= prods[memindex]; + + size *= nbyper; + + if( ! *data ){ /* then allocate what is needed */ + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("+d alloc %" PRId64 + " (%" PRId64 " x %d) bytes for collapsed image\n", + size, size/nbyper, nbyper); + + *data = malloc(size); /* actually allocate the memory */ + if( ! *data ){ + Rc_fprintf_stderr("** NIFTI rci_am: failed to alloc %" PRId64 + " bytes for data\n", size); + return -1; + } + } else if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d rci_am: *data already set, need %" PRId64 + " x %d bytes\n", + size/nbyper, nbyper); + + return size; +} + + +/* prepare a pivot list for reading + + The pivot points are the indices into dims where the calling function + wants to collapse a dimension. The last pivot should always be zero + (note that we have space for that in the lists). +*/ +static int make_pivot_list(nifti_image *nim, const int64_t dims[], int pivots[], + int64_t prods[], int * nprods ) +{ + int len, dind; + + len = 0; + dind = nim->dim[0]; + while( dind > 0 ){ + prods[len] = 1; + while( dind > 0 && (nim->dim[dind] == 1 || dims[dind] == -1) ){ + prods[len] *= nim->dim[dind]; + dind--; + } + pivots[len] = dind; + len++; + dind--; /* fine, let it drop out at -1 */ + } + + /* make sure to include 0 as a pivot (instead of just 1, if it is) */ + if( len > 0 && pivots[len-1] != 0 ){ + pivots[len] = 0; + prods[len] = 1; + len++; + } + + *nprods = len; + + if( g_opts.debug > 2 ){ + Rc_fprintf_stderr("+d pivot list created, pivots :"); + for(dind = 0; dind < len; dind++) + Rc_fprintf_stderr(" %d", pivots[dind]); + Rc_fprintf_stderr(", prods :"); + for(dind = 0; dind < len; dind++) + Rc_fprintf_stderr(" %" PRId64 "", prods[dind]); + Rc_fputc_stderr('\n'); + } + + return 0; +} + + +#undef ISEND +#define ISEND(c) ( (c)==']' || (c)=='}' || (c)=='\0' ) + +/*---------------------------------------------------------------------*/ +/*! Get an integer list in the range 0..(nvals-1), from the + character string str. If we call the output pointer fred, + then fred[0] = number of integers in the list (> 0), and + fred[i] = i-th integer in the list for i=1..fred[0]. + If on return, fred == NULL or fred[0] == 0, then something is + wrong, and the caller must deal with that. + + Syntax of input string: + - initial '{' or '[' is skipped, if present + - ends when '}' or ']' or end of string is found + - contains entries separated by commas + - entries have one of these forms: + - a single number + - a dollar sign '$', which means nvals-1 + - a sequence of consecutive numbers in the form "a..b" or + "a-b", where "a" and "b" are single numbers (or '$') + - a sequence of evenly spaced numbers in the form + "a..b(c)" or "a-b(c)", where "c" encodes the step + - Example: "[2,7..4,3..9(2)]" decodes to the list + 2 7 6 5 4 3 5 7 9 + - entries should be in the range 0..nvals-1 + + (borrowed, with permission, from thd_intlist.c) +*//*-------------------------------------------------------------------*/ +int64_t * nifti_get_int64list( int64_t nvals , const char * str ) +{ + int64_t *subv = NULL ; + int64_t *subv_realloc = NULL; + int64_t ii , nout ; + int64_t ibot,itop,istep , nused ; + int ipos , slen ; + char *cpt ; + + /* Meaningless input? */ + if( nvals < 1 ) return NULL ; + + /* No selection list? */ + if( str == NULL || str[0] == '\0' ) return NULL ; + + /* skip initial '[' or '{' */ + subv = (int64_t *)malloc( sizeof(int64_t) * 2 ) ; + if( !subv ) { + Rc_fprintf_stderr("** nifti_get_intlist: failed alloc of 2 ints\n"); + return NULL; + } + subv[0] = nout = 0 ; + + ipos = 0 ; + if( str[ipos] == '[' || str[ipos] == '{' ) ipos++ ; + + if( g_opts.debug > 1 ) + Rc_fprintf_stderr("-d making int_list (vals = %" PRId64 ") from '%s'\n", + nvals, str); + + /**- for each sub-selector until end of input... */ + + slen = (int)strlen(str) ; + while( ipos < slen && !ISEND(str[ipos]) ){ + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + if( ISEND(str[ipos]) ) break ; /* done */ + + /**- get starting value */ + + if( str[ipos] == '$' ){ /* special case */ + ibot = nvals-1 ; ipos++ ; + } else { /* decode an integer */ + ibot = strtoll( str+ipos , &cpt , 10 ) ; + if( ibot < 0 ){ + Rc_fprintf_stderr("** NIFTI ERROR: list index %" PRId64 + " is out of range 0..%" PRId64 "\n", + ibot,nvals-1) ; + free(subv) ; return NULL ; + } + if( ibot >= nvals ){ + Rc_fprintf_stderr("** NIFTI ERROR: list index %" PRId64 + " is out of range 0..%" PRId64 "\n", + ibot,nvals-1) ; + free(subv) ; return NULL ; + } + nused = (cpt-(str+ipos)) ; + if( ibot == 0 && nused == 0 ){ + Rc_fprintf_stderr("** NIFTI : list syntax error '%s'\n",str+ipos) ; + free(subv) ; return NULL ; + } + ipos += nused ; + } + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + + /**- if that's it for this sub-selector, add one value to list */ + + if( str[ipos] == ',' || ISEND(str[ipos]) ){ + nout++ ; + subv_realloc = (int64_t *)realloc( (char *)subv , sizeof(int64_t)*(nout+1) ) ; + if( !subv_realloc ) { + free(subv); + Rc_fprintf_stderr("** nifti_get_intlist: failed realloc of %" PRId64 + " ints\n", nout+1); + return NULL; + } + subv = subv_realloc; + subv[0] = nout ; + subv[nout] = ibot ; + if( ISEND(str[ipos]) ) break ; /* done */ + ipos++ ; continue ; /* re-start loop at next sub-selector */ + } + + /**- otherwise, must have '..' or '-' as next inputs */ + + if( str[ipos] == '-' ){ + ipos++ ; + } else if( str[ipos] == '.' && str[ipos+1] == '.' ){ + ipos++ ; ipos++ ; + } else { + Rc_fprintf_stderr("** NIFTI ERROR: index list syntax is bad: '%s'\n", + str+ipos) ; + free(subv) ; return NULL ; + } + + /**- get ending value for loop now */ + + if( str[ipos] == '$' ){ /* special case */ + itop = nvals-1 ; ipos++ ; + } else { /* decode an integer */ + itop = strtoll( str+ipos , &cpt , 10 ) ; + if( itop < 0 ){ + Rc_fprintf_stderr("** NIFTI ERROR: index %" PRId64 + " is out of range 0..%" PRId64 "\n", + itop,nvals-1) ; + free(subv) ; return NULL ; + } + if( itop >= nvals ){ + Rc_fprintf_stderr("** NIFTI ERROR: index %" PRId64 + " is out of range 0..%" PRId64 "\n", + itop,nvals-1) ; + free(subv) ; return NULL ; + } + nused = (cpt-(str+ipos)) ; + if( itop == 0 && nused == 0 ){ + Rc_fprintf_stderr("** NIFTI: index list syntax error '%s'\n", + str+ipos) ; + free(subv) ; return NULL ; + } + ipos += nused ; + } + + /**- set default loop step */ + + istep = (ibot <= itop) ? 1 : -1 ; + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + + /**- check if we have a non-default loop step */ + + if( str[ipos] == '(' ){ /* decode an integer */ + ipos++ ; + istep = strtoll( str+ipos , &cpt , 10 ) ; + if( istep == 0 ){ + Rc_fprintf_stderr("** NIFTI ERROR: index loop step is 0!\n") ; + free(subv) ; return NULL ; + } + nused = (cpt-(str+ipos)) ; + ipos += nused ; + if( str[ipos] == ')' ) ipos++ ; + if( (ibot-itop)*istep > 0 ){ + Rc_fprintf_stderr("** NIFTI WARNING: index list '%" PRId64 "..%" PRId64 + "(%" PRId64 ")' means nothing\n", + ibot,itop,istep ) ; + } + } + + /**- add values to output */ + + for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ + nout++ ; + subv_realloc = (int64_t *)realloc( (char *)subv , sizeof(int64_t)*(nout+1) ) ; + if( !subv_realloc ) { + free(subv); + Rc_fprintf_stderr("** nifti_get_intlist: failed realloc of %" PRId64 + " ints\n", nout+1); + return NULL; + } + subv = subv_realloc; + subv[0] = nout ; + subv[nout] = ii ; + } + + /**- check if we have a comma to skip over */ + + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + if( str[ipos] == ',' ) ipos++ ; /* skip commas */ + + } /* end of loop through selector string */ + + if( g_opts.debug > 1 ) { + Rc_fprintf_stderr("+d int_list (vals = %" PRId64 "): ", subv[0]); + for( ii = 1; ii <= subv[0]; ii++ ) + Rc_fprintf_stderr("%" PRId64 " ", subv[ii]); + Rc_fputc_stderr('\n'); + } + + if( subv[0] == 0 ){ free(subv); subv = NULL; } + return subv ; +} + +/*! a 32-bit version of nifti_get_int64list */ +int * nifti_get_intlist( int nvals , const char * str ) +{ + int *ilist=NULL; + int64_t *i64list=NULL, nints, index; + + i64list = nifti_get_int64list((int64_t)nvals, str); + if( !i64list ) return NULL; + + /* check that the length is between 1 and INT_MAX */ + nints = i64list[0]; + if( nints <= 0 ) { free(i64list); return NULL; } + + if( nints > INT_MAX ) { + Rc_fprintf_stderr("** nifti_get_intlist: %" PRId64 + " ints is too long for 32-bits\n", nints); + free(i64list); + return NULL; + } + + /* have a valid result, copy as ints */ + ilist = (int *)malloc((nints+1) * sizeof(int)); + if( !ilist ) { + Rc_fprintf_stderr("** nifti_get_intlist: failed to alloc %" PRId64 " ints\n", + nints); + free(i64list); + return NULL; + } + + /* copy list, including length at index 0 */ + for( index=0; index <= nints; index++ ) { + if( i64list[index] > INT_MAX ) { + Rc_fprintf_stderr("** nifti_get_intlist: value %" PRId64 + " too big for 32-bits\n", + i64list[index]); + free(ilist); + free(i64list); + return NULL; + } + ilist[index] = (int)i64list[index]; + } + + free(i64list); + + return ilist; +} + +/*---------------------------------------------------------------------*/ +/*! Given a NIFTI_TYPE string, such as "NIFTI_TYPE_INT16", return the + * corresponding integral type code. The type code is the macro + * value defined in nifti1.h. +*//*-------------------------------------------------------------------*/ +int nifti_datatype_from_string( const char * name ) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; + + if( !name ) return DT_UNKNOWN; + + for( c = tablen-1; c > 0; c-- ) + if( !strcmp(name, nifti_type_list[c].name) ) + break; + + return nifti_type_list[c].type; +} + + +/*---------------------------------------------------------------------*/ +/*! Given a NIFTI_TYPE value, such as NIFTI_TYPE_INT16, return the + * corresponding macro label as a string. The dtype code is the + * macro value defined in nifti1.h. +*//*-------------------------------------------------------------------*/ +const char * nifti_datatype_to_string( int dtype ) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; + + for( c = tablen-1; c > 0; c-- ) + if( nifti_type_list[c].type == dtype ) + break; + + return nifti_type_list[c].name; +} + + +/*---------------------------------------------------------------------*/ +/*! Determine whether dtype is a valid NIFTI_TYPE. + * + * DT_UNKNOWN is considered invalid + * + * The only difference 'for_nifti' makes is that DT_BINARY + * should be invalid for a NIfTI dataset. +*//*-------------------------------------------------------------------*/ +int nifti_datatype_is_valid( int dtype, int for_nifti ) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; + + /* special case */ + if( for_nifti && dtype == DT_BINARY ) return 0; + + for( c = tablen-1; c > 0; c-- ) + if( nifti_type_list[c].type == dtype ) + return 1; + + return 0; +} + + +/*---------------------------------------------------------------------*/ +/*! Only as a test, verify that the new nifti_type_list table matches + * the the usage of nifti_datatype_sizes (which could be changed to + * use the table, if there were interest). + * + * return the number of errors (so 0 is success, as usual) +*//*-------------------------------------------------------------------*/ +int nifti_test_datatype_sizes(int verb) +{ + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int nbyper, ssize; + int c, errs = 0; + + for( c = 0; c < tablen; c++ ) + { + nbyper = ssize = -1; + nifti_datatype_sizes(nifti_type_list[c].type, &nbyper, &ssize); + if( nbyper < 0 || ssize < 0 || + nbyper != nifti_type_list[c].nbyper || + ssize != nifti_type_list[c].swapsize ) + { + if( verb || g_opts.debug > 2 ) + Rc_fprintf_stderr("** NIFTI type mismatch: " + "%s, %d, %d, %d : %d, %d\n", + nifti_type_list[c].name, nifti_type_list[c].type, + nifti_type_list[c].nbyper, nifti_type_list[c].swapsize, + nbyper, ssize); + errs++; + } + } + + if( errs ) + Rc_fprintf_stderr("** nifti_test_datatype_sizes: found %d errors\n",errs); + else if( verb || g_opts.debug > 1 ) + Rc_fprintf_stderr("-- nifti_test_datatype_sizes: all OK\n"); + + return errs; +} + + +/*---------------------------------------------------------------------*/ +/*! Display the nifti_type_list table. + * + * if which == 1 : display DT_* + * if which == 2 : display NIFTI_TYPE* + * else : display all +*//*-------------------------------------------------------------------*/ +int nifti_disp_type_list( int which ) +{ + const char * style; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int lwhich, c; + + if ( which == 1 ){ lwhich = 1; style = "DT_"; } + else if( which == 2 ){ lwhich = 2; style = "NIFTI_TYPE_"; } + else { lwhich = 3; style = "ALL"; } + + Rc_printf("nifti_type_list entries (%s) :\n" + " name type nbyper swapsize\n" + " --------------------- ---- ------ --------\n", style); + + for( c = 0; c < tablen; c++ ) + if( (lwhich & 1 && nifti_type_list[c].name[0] == 'D') || + (lwhich & 2 && nifti_type_list[c].name[0] == 'N') ) + Rc_printf(" %-22s %5d %3d %5d\n", + nifti_type_list[c].name, + nifti_type_list[c].type, + nifti_type_list[c].nbyper, + nifti_type_list[c].swapsize); + + return 0; +} diff --git a/cpp/3rd_party/rnifti/niftilib/nifti2_io.h b/cpp/3rd_party/rnifti/niftilib/nifti2_io.h new file mode 100644 index 0000000000..dcb933e884 --- /dev/null +++ b/cpp/3rd_party/rnifti/niftilib/nifti2_io.h @@ -0,0 +1,833 @@ +/** \file nifti2_io.h + \brief Data structures for using nifti2_io API. + - Written by Bob Cox, SSCC NIMH + - Revisions by Rick Reynolds, SSCC NIMH + */ +#ifndef _NIFTI2_IO_HEADER_ +#define _NIFTI2_IO_HEADER_ + +#include +#include +#include +#include +#include +#include +#include + +#ifndef DONT_INCLUDE_ANALYZE_STRUCT +#define DONT_INCLUDE_ANALYZE_STRUCT /*** not needed herein ***/ +#endif +#include "niftilib/nifti1.h" /*** NIFTI-1 header specification ***/ +#include "niftilib/nifti2.h" /*** NIFTI-2 header specification ***/ + +#ifndef RNIFTI_NIFTILIB_VERSION +#define RNIFTI_NIFTILIB_VERSION 2 +#endif + +#include "RNifti/NiftiImage_print.h" +#include + + +/*****===================================================================*****/ +/***** File nifti2_io.h == Declarations for nifti2_io.c *****/ +/*****...................................................................*****/ +/***** This code is a modification of nifti1_io.h. *****/ +/*****...................................................................*****/ +/***** This code is released to the public domain. *****/ +/*****...................................................................*****/ +/***** Author: Robert W Cox, SSCC/DIRP/NIMH/NIH/DHHS/USA/EARTH *****/ +/***** Date: August 2003 *****/ +/*****...................................................................*****/ +/***** Neither the National Institutes of Health (NIH), nor any of its *****/ +/***** employees imply any warranty of usefulness of this software for *****/ +/***** any purpose, and do not assume any liability for damages, *****/ +/***** incidental or otherwise, caused by any use of this document. *****/ +/*****===================================================================*****/ + +/* ...................................................................... + Modified by: Mark Jenkinson (FMRIB Centre, University of Oxford, UK) + Date: July/August 2004 + + Mainly adding low-level IO and changing things to allow gzipped files + to be read and written + Full backwards compatability should have been maintained + + ...................................................................... + Modified by: Rick Reynolds (SSCC/DIRP/NIMH, National Institutes of Health) + Date: December 2004 + + Modified and added many routines for I/O, particularly involving + extensions and nifti_brick_list. + + ...................................................................... + Modified by: Rick Reynolds (SSCC/DIRP/NIMH, National Institutes of Health) + Date: August 2013 + + Converted to be based on nifti_2_header. + + ** NOT BACKWARD COMPATABLE ** + + These routines will read/write both NIFTI-1 and NIFTI-2 image files, + but modification to the _calling_ routies is necessary, since: + + a. the main nifti_image type has changed (to nifti2_image) + b. some image field types have been altered (to have larger size) + c. some routines have been changed to apply to multiple NIFTI types +*/ + +/********************** Some sample data structures **************************/ + +#if RNIFTI_NIFTILIB_VERSION == 2 +typedef struct { /** 4x4 matrix struct **/ + float m[4][4] ; +} mat44 ; + +typedef struct { /** 3x3 matrix struct **/ + float m[3][3] ; +} mat33 ; +#endif + +typedef struct { /** 4x4 matrix struct (double) **/ + double m[4][4] ; +} nifti_dmat44 ; + +typedef struct { /** 3x3 matrix struct (double) **/ + double m[3][3] ; +} nifti_dmat33 ; + +/*...........................................................................*/ + +/*! \enum analyze_75_orient_code + * \brief Old-style analyze75 orientation + * codes. + */ +#if RNIFTI_NIFTILIB_VERSION == 2 +typedef enum _analyze75_orient_code { + a75_transverse_unflipped = 0, + a75_coronal_unflipped = 1, + a75_sagittal_unflipped = 2, + a75_transverse_flipped = 3, + a75_coronal_flipped = 4, + a75_sagittal_flipped = 5, + a75_orient_unknown = 6 +} analyze_75_orient_code; +#endif + +/*! \struct nifti_image + \brief High level data structure for open nifti datasets in the + nifti2_io API. Note that this structure is not part of the + nifti2 format definition; it is used to implement one API + for reading/writing datasets in the nifti1 or nifti2 formats. + + Field types changed for NIFTI-2 (note: ALL floats to doubles): + nx, ny, ..., nw, dim, nvox, + dx, dy, ..., dw, pixdim, + scl_slope, scl_inter, cal_min, cal_max, + slice_start, slice_end, slice_duration, + quatern_b,c,d, qoffset_x,y,z, qfac, + qto_xyz,ijk, sto_xyz,ijk, + toffset, intent_p1,2,3, iname_offset + */ +typedef struct { /*!< Image storage struct **/ + + int64_t ndim ; /*!< last dimension greater than 1 (1..7) */ + int64_t nx ; /*!< dimensions of grid array */ + int64_t ny ; /*!< dimensions of grid array */ + int64_t nz ; /*!< dimensions of grid array */ + int64_t nt ; /*!< dimensions of grid array */ + int64_t nu ; /*!< dimensions of grid array */ + int64_t nv ; /*!< dimensions of grid array */ + int64_t nw ; /*!< dimensions of grid array */ + int64_t dim[8] ; /*!< dim[0]=ndim, dim[1]=nx, etc. */ + int64_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */ + int nbyper ; /*!< bytes per voxel, matches datatype */ + int datatype ; /*!< type of data in voxels: DT_* code */ + + double dx ; /*!< grid spacings */ + double dy ; /*!< grid spacings */ + double dz ; /*!< grid spacings */ + double dt ; /*!< grid spacings */ + double du ; /*!< grid spacings */ + double dv ; /*!< grid spacings */ + double dw ; /*!< grid spacings */ + double pixdim[8] ; /*!< pixdim[1]=dx, etc. */ + + double scl_slope ; /*!< scaling parameter - slope */ + double scl_inter ; /*!< scaling parameter - intercept */ + + double cal_min ; /*!< calibration parameter, minimum */ + double cal_max ; /*!< calibration parameter, maximum */ + + int qform_code ; /*!< codes for (x,y,z) space meaning */ + int sform_code ; /*!< codes for (x,y,z) space meaning */ + + int freq_dim ; /*!< indexes (1,2,3, or 0) for MRI */ + int phase_dim ; /*!< directions in dim[]/pixdim[] */ + int slice_dim ; /*!< directions in dim[]/pixdim[] */ + + int slice_code ; /*!< code for slice timing pattern */ + int64_t slice_start ; /*!< index for start of slices */ + int64_t slice_end ; /*!< index for end of slices */ + double slice_duration ; /*!< time between individual slices */ + + /*! quaternion transform parameters + [when writing a dataset, these are used for qform, NOT qto_xyz] */ + double quatern_b , quatern_c , quatern_d , + qoffset_x , qoffset_y , qoffset_z , + qfac ; + + nifti_dmat44 qto_xyz ; /*!< qform: transform (i,j,k) to (x,y,z) */ + nifti_dmat44 qto_ijk ; /*!< qform: transform (x,y,z) to (i,j,k) */ + + nifti_dmat44 sto_xyz ; /*!< sform: transform (i,j,k) to (x,y,z) */ + nifti_dmat44 sto_ijk ; /*!< sform: transform (x,y,z) to (i,j,k) */ + + double toffset ; /*!< time coordinate offset */ + + int xyz_units ; /*!< dx,dy,dz units: NIFTI_UNITS_* code */ + int time_units ; /*!< dt units: NIFTI_UNITS_* code */ + + int nifti_type ; /*!< see NIFTI_FTYPE_* codes, below: + 0==ANALYZE, + 1==NIFTI-1 (1 file), + 2==NIFTI-1 (2 files), + 3==NIFTI-ASCII (1 file) + 4==NIFTI-2 (1 file), + 5==NIFTI-2 (2 files) */ + + int intent_code ; /*!< statistic type (or something) */ + double intent_p1 ; /*!< intent parameters */ + double intent_p2 ; /*!< intent parameters */ + double intent_p3 ; /*!< intent parameters */ + char intent_name[16] ; /*!< optional description of intent data */ + + char descrip[80] ; /*!< optional text to describe dataset */ + char aux_file[24] ; /*!< auxiliary filename */ + + char *fname ; /*!< header filename (.hdr or .nii) */ + char *iname ; /*!< image filename (.img or .nii) */ + int64_t iname_offset ; /*!< offset into iname where data starts */ + int swapsize ; /*!< swap unit in image data (might be 0) */ + int byteorder ; /*!< byte order on disk (MSB_ or LSB_FIRST) */ + void *data ; /*!< pointer to data: nbyper*nvox bytes */ + + int num_ext ; /*!< number of extensions in ext_list */ + nifti1_extension * ext_list ; /*!< array of extension structs (with data) */ + analyze_75_orient_code analyze75_orient; /*!< for old analyze files, orient */ + + std::shared_ptr decompressed_memory_buffer; + +} nifti2_image ; + +#if RNIFTI_NIFTILIB_VERSION == 2 +typedef struct { + + int ndim ; /*!< last dimension greater than 1 (1..7) */ + int nx ; /*!< dimensions of grid array */ + int ny ; /*!< dimensions of grid array */ + int nz ; /*!< dimensions of grid array */ + int nt ; /*!< dimensions of grid array */ + int nu ; /*!< dimensions of grid array */ + int nv ; /*!< dimensions of grid array */ + int nw ; /*!< dimensions of grid array */ + int dim[8] ; /*!< dim[0]=ndim, dim[1]=nx, etc. */ + size_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */ + int nbyper ; /*!< bytes per voxel, matches datatype */ + int datatype ; /*!< type of data in voxels: DT_* code */ + + float dx ; /*!< grid spacings */ + float dy ; /*!< grid spacings */ + float dz ; /*!< grid spacings */ + float dt ; /*!< grid spacings */ + float du ; /*!< grid spacings */ + float dv ; /*!< grid spacings */ + float dw ; /*!< grid spacings */ + float pixdim[8] ; /*!< pixdim[1]=dx, etc. */ + + float scl_slope ; /*!< scaling parameter - slope */ + float scl_inter ; /*!< scaling parameter - intercept */ + + float cal_min ; /*!< calibration parameter, minimum */ + float cal_max ; /*!< calibration parameter, maximum */ + + int qform_code ; /*!< codes for (x,y,z) space meaning */ + int sform_code ; /*!< codes for (x,y,z) space meaning */ + + int freq_dim ; /*!< indexes (1,2,3, or 0) for MRI */ + int phase_dim ; /*!< directions in dim[]/pixdim[] */ + int slice_dim ; /*!< directions in dim[]/pixdim[] */ + + int slice_code ; /*!< code for slice timing pattern */ + int slice_start ; /*!< index for start of slices */ + int slice_end ; /*!< index for end of slices */ + float slice_duration ; /*!< time between individual slices */ + + /*! quaternion transform parameters + [when writing a dataset, these are used for qform, NOT qto_xyz] */ + float quatern_b , quatern_c , quatern_d , + qoffset_x , qoffset_y , qoffset_z , + qfac ; + + mat44 qto_xyz ; /*!< qform: transform (i,j,k) to (x,y,z) */ + mat44 qto_ijk ; /*!< qform: transform (x,y,z) to (i,j,k) */ + + mat44 sto_xyz ; /*!< sform: transform (i,j,k) to (x,y,z) */ + mat44 sto_ijk ; /*!< sform: transform (x,y,z) to (i,j,k) */ + + float toffset ; /*!< time coordinate offset */ + + int xyz_units ; /*!< dx,dy,dz units: NIFTI_UNITS_* code */ + int time_units ; /*!< dt units: NIFTI_UNITS_* code */ + + int nifti_type ; /*!< 0==ANALYZE, 1==NIFTI-1 (1 file), + 2==NIFTI-1 (2 files), + 3==NIFTI-ASCII (1 file) */ + int intent_code ; /*!< statistic type (or something) */ + float intent_p1 ; /*!< intent parameters */ + float intent_p2 ; /*!< intent parameters */ + float intent_p3 ; /*!< intent parameters */ + char intent_name[16] ; /*!< optional description of intent data */ + + char descrip[80] ; /*!< optional text to describe dataset */ + char aux_file[24] ; /*!< auxiliary filename */ + + char *fname ; /*!< header filename (.hdr or .nii) */ + char *iname ; /*!< image filename (.img or .nii) */ + int iname_offset ; /*!< offset into iname where data starts */ + int swapsize ; /*!< swap unit in image data (might be 0) */ + int byteorder ; /*!< byte order on disk (MSB_ or LSB_FIRST) */ + void *data ; /*!< pointer to data: nbyper*nvox bytes */ + + int num_ext ; /*!< number of extensions in ext_list */ + nifti1_extension * ext_list ; /*!< array of extension structs (with data) */ + analyze_75_orient_code analyze75_orient; /*!< for old analyze files, orient */ + + // impl reserved bytes. Is not part of nifti. + std::shared_ptr decompressed_memory_buffer; + +} nifti1_image ; +#endif + +/* struct for return from nifti_image_read_bricks() */ +typedef struct { + int64_t nbricks; /* the number of allocated pointers in 'bricks' */ + int64_t bsize; /* the length of each data block, in bytes */ + void ** bricks; /* array of pointers to data blocks */ +} nifti2_brick_list; + +#if RNIFTI_NIFTILIB_VERSION == 2 +typedef nifti2_image nifti_image; +typedef nifti2_brick_list nifti_brick_list; +#endif + +/*****************************************************************************/ +/*------------------ NIfTI version of ANALYZE 7.5 structure -----------------*/ + +/* (based on fsliolib/dbh.h, but updated for version 7.5) */ + +#if RNIFTI_NIFTILIB_VERSION == 2 +typedef struct { + /* header info fields - describes the header overlap with NIfTI */ + /* ------------------ */ + int sizeof_hdr; /* 0 + 4 same */ + char data_type[10]; /* 4 + 10 same */ + char db_name[18]; /* 14 + 18 same */ + int extents; /* 32 + 4 same */ + short int session_error; /* 36 + 2 same */ + char regular; /* 38 + 1 same */ + char hkey_un0; /* 39 + 1 40 bytes */ + + /* image dimension fields - describes image sizes */ + short int dim[8]; /* 0 + 16 same */ + short int unused8; /* 16 + 2 intent_p1... */ + short int unused9; /* 18 + 2 ... */ + short int unused10; /* 20 + 2 intent_p2... */ + short int unused11; /* 22 + 2 ... */ + short int unused12; /* 24 + 2 intent_p3... */ + short int unused13; /* 26 + 2 ... */ + short int unused14; /* 28 + 2 intent_code */ + short int datatype; /* 30 + 2 same */ + short int bitpix; /* 32 + 2 same */ + short int dim_un0; /* 34 + 2 slice_start */ + float pixdim[8]; /* 36 + 32 same */ + + float vox_offset; /* 68 + 4 same */ + float funused1; /* 72 + 4 scl_slope */ + float funused2; /* 76 + 4 scl_inter */ + float funused3; /* 80 + 4 slice_end, */ + /* slice_code, */ + /* xyzt_units */ + float cal_max; /* 84 + 4 same */ + float cal_min; /* 88 + 4 same */ + float compressed; /* 92 + 4 slice_duration */ + float verified; /* 96 + 4 toffset */ + int glmax,glmin; /* 100 + 8 108 bytes */ + + /* data history fields - optional */ + char descrip[80]; /* 0 + 80 same */ + char aux_file[24]; /* 80 + 24 same */ + char orient; /* 104 + 1 NO GOOD OVERLAP */ + char originator[10]; /* 105 + 10 FROM HERE DOWN... */ + char generated[10]; /* 115 + 10 */ + char scannum[10]; /* 125 + 10 */ + char patient_id[10]; /* 135 + 10 */ + char exp_date[10]; /* 145 + 10 */ + char exp_time[10]; /* 155 + 10 */ + char hist_un0[3]; /* 165 + 3 */ + int views; /* 168 + 4 */ + int vols_added; /* 172 + 4 */ + int start_field; /* 176 + 4 */ + int field_skip; /* 180 + 4 */ + int omax, omin; /* 184 + 8 */ + int smax, smin; /* 192 + 8 200 bytes */ +} nifti_analyze75; /* total: 348 bytes */ +#endif + +size_t nifti2_image_impl_reserved_bytes_offset(); + +/*****************************************************************************/ +/*--------------- Prototypes of functions defined in this file --------------*/ + +char const * nifti_datatype_string ( int dt ) ; +char const *nifti_units_string ( int uu ) ; +char const *nifti_intent_string ( int ii ) ; +char const *nifti_xform_string ( int xx ) ; +char const *nifti_slice_string ( int ss ) ; +char const *nifti_orientation_string( int ii ) ; + +int nifti_is_inttype( int dt ) ; + +mat44 nifti_mat44_inverse ( mat44 R ) ; +mat44 nifti_mat44_mul ( mat44 A , mat44 B ); +nifti_dmat44 nifti_dmat44_inverse( nifti_dmat44 R ) ; +int nifti_mat44_to_dmat44(mat44 * fm, nifti_dmat44 * dm); +int nifti_dmat44_to_mat44(nifti_dmat44 * dm, mat44 * fm); +nifti_dmat44 nifti_dmat44_mul ( nifti_dmat44 A , nifti_dmat44 B ); + + + +nifti_dmat33 nifti_dmat33_inverse( nifti_dmat33 R ) ; +nifti_dmat33 nifti_dmat33_polar ( nifti_dmat33 A ) ; +double nifti_dmat33_rownorm( nifti_dmat33 A ) ; +double nifti_dmat33_colnorm( nifti_dmat33 A ) ; +double nifti_dmat33_determ ( nifti_dmat33 R ) ; +nifti_dmat33 nifti_dmat33_mul ( nifti_dmat33 A , nifti_dmat33 B ) ; + +mat33 nifti_mat33_inverse( mat33 R ) ; +mat33 nifti_mat33_polar ( mat33 A ) ; +float nifti_mat33_rownorm( mat33 A ) ; +float nifti_mat33_colnorm( mat33 A ) ; +float nifti_mat33_determ ( mat33 R ) ; +mat33 nifti_mat33_mul ( mat33 A , mat33 B ) ; + +#if RNIFTI_NIFTILIB_VERSION == 2 +void nifti_swap_2bytes ( int64_t n , void *ar ) ; +void nifti_swap_4bytes ( int64_t n , void *ar ) ; +void nifti_swap_8bytes ( int64_t n , void *ar ) ; +void nifti_swap_16bytes( int64_t n , void *ar ) ; +void nifti_swap_Nbytes ( int64_t n , int siz , void *ar ) ; +#endif + +int nifti_datatype_is_valid (int dtype, int for_nifti); +int nifti_datatype_from_string (const char * name); +const char * nifti_datatype_to_string(int dtype); +int nifti_header_version (const char * buf, size_t nbytes); + +int64_t nifti2_get_filesize( const char *pathname ) ; +#if RNIFTI_NIFTILIB_VERSION == 2 +void swap_nifti_header ( void * hdr , int ni_ver ) ; +#endif +void old_swap_nifti_header( struct nifti_1_header *h , int is_nifti ); +#if RNIFTI_NIFTILIB_VERSION == 2 +void nifti_swap_as_analyze( nifti_analyze75 *h ); +#endif +void nifti_swap_as_nifti1( nifti_1_header *h ); +void nifti_swap_as_nifti2( nifti_2_header *h ); + + +/* main read/write routines */ + +nifti_image *nifti2_image_read_bricks(const char *hname , int64_t nbricks, + const int64_t *blist, nifti_brick_list * NBL); +int nifti2_image_load_bricks(nifti_image *nim , int64_t nbricks, + const int64_t *blist, nifti_brick_list * NBL); +void nifti2_free_NBL( nifti_brick_list * NBL ); + +nifti_image *nifti2_image_read ( const char *hname , int read_data); +nifti_image *nifti2_image_mem_read( const uint8_t *data , const size_t size, int gz, const size_t estimated_size); +int nifti2_image_load ( nifti_image *nim); +int nifti2_image_mem_load( znzFile fp, nifti_image *nim, const uint8_t *data , const size_t size, int gz, const size_t estimated_size); +void nifti2_image_unload ( nifti_image *nim); +void nifti2_image_free ( nifti_image *nim); +int nifti2_image_is_data_owner( const nifti_image* nim ); + +int64_t nifti2_read_collapsed_image( nifti_image * nim, + const int64_t dims[8], void ** data); + +int64_t nifti2_read_subregion_image(nifti_image *nim, const int64_t *start_index, + const int64_t *region_size, void ** data); + +void nifti2_image_write ( nifti_image * nim ) ; +void nifti2_image_write_bricks(nifti_image * nim, + const nifti_brick_list * NBL); +void nifti2_image_infodump( const nifti_image * nim ) ; + +void nifti2_disp_lib_hist( int ver ) ; /* to display library history */ +void nifti_disp_lib_version( void ) ; /* to display library version */ +int nifti2_disp_matrix_orient( const char * mesg, nifti_dmat44 mat ); +int nifti_disp_type_list( int which ); + + +char * nifti2_image_to_ascii ( const nifti_image * nim ) ; +nifti_image *nifti2_image_from_ascii( const char * str, int * bytes_read ) ; + +int64_t nifti2_get_volsize(const nifti_image *nim) ; + +/* basic file operations */ +int nifti2_set_filenames(nifti_image * nim, const char * prefix, int check, + int set_byte_order); +char * nifti_makehdrname (const char * prefix, int nifti_type, int check, + int comp); +char * nifti_makeimgname (const char * prefix, int nifti_type, int check, + int comp); +int is_nifti_file (const char *hname); +char * nifti_find_file_extension(const char * name); +int nifti_is_complete_filename(const char* fname); +int nifti_validfilename(const char* fname); + + +int disp_nifti_1_header(const char * info, const nifti_1_header * hp ) ; +int disp_nifti_2_header( const char * info, const nifti_2_header * hp ) ; +void nifti_set_debug_level( int level ) ; +void nifti_set_skip_blank_ext( int skip ) ; +void nifti_set_allow_upper_fext( int allow ) ; +int nifti_get_alter_cifti( void ); +void nifti_set_alter_cifti( int alter_cifti ); + +int nifti_alter_cifti_dims(nifti_image * nim); + + +int valid_nifti2_brick_list(nifti_image * nim , int64_t nbricks, + const int64_t * blist, int disp_error); + +/* znzFile operations */ +znzFile nifti2_image_open(const char * hname, char * opts, nifti_image ** nim); +znzFile nifti2_image_write_hdr_img(nifti_image *nim, int write_data, + const char* opts); +znzFile nifti2_image_write_hdr_img2( nifti_image *nim , int write_opts , + const char* opts, znzFile imgfile, const nifti_brick_list * NBL); +int64_t nifti2_read_buffer(znzFile fp, void* dataptr, int64_t ntot, + nifti_image *nim); +int64_t nifti2_assign_buffer(znzFile fp, void **dataptr, int64_t ntot, nifti_image *nim); +int nifti2_write_all_data(znzFile fp, nifti_image * nim, + const nifti_brick_list * NBL); +int64_t nifti2_write_buffer(znzFile fp, const void * buffer, int64_t numbytes); +nifti_image *nifti2_read_ascii_image(znzFile fp, const char *fname, int flen, + int read_data); +nifti_image *nifti2_mem_read_ascii_image(znzFile fp, int flen, int compressed); +znzFile nifti2_write_ascii_image(nifti_image *nim, const nifti_brick_list * NBL, + const char * opts, int write_data, int leave_open); + + +void nifti_datatype_sizes( int datatype , int *nbyper, int *swapsize ) ; + +void nifti_dmat44_to_quatern(nifti_dmat44 R , + double *qb, double *qc, double *qd, + double *qx, double *qy, double *qz, + double *dx, double *dy, double *dz, double *qfac); + +nifti_dmat44 nifti_quatern_to_dmat44( double qb, double qc, double qd, + double qx, double qy, double qz, + double dx, double dy, double dz, double qfac ); + +nifti_dmat44 nifti_make_orthog_dmat44( double r11, double r12, double r13 , + double r21, double r22, double r23 , + double r31, double r32, double r33 ) ; + +void nifti_mat44_to_quatern( mat44 R , + float *qb, float *qc, float *qd, + float *qx, float *qy, float *qz, + float *dx, float *dy, float *dz, float *qfac ) ; + +mat44 nifti_quatern_to_mat44( float qb, float qc, float qd, + float qx, float qy, float qz, + float dx, float dy, float dz, float qfac ); + +mat44 nifti_make_orthog_mat44( float r11, float r12, float r13 , + float r21, float r22, float r23 , + float r31, float r32, float r33 ) ; + +int nifti_short_order(void) ; /* CPU byte order */ + + +/* Orientation codes that might be returned from nifti_mat44_to_orientation().*/ + +#define NIFTI_L2R 1 /* Left to Right */ +#define NIFTI_R2L 2 /* Right to Left */ +#define NIFTI_P2A 3 /* Posterior to Anterior */ +#define NIFTI_A2P 4 /* Anterior to Posterior */ +#define NIFTI_I2S 5 /* Inferior to Superior */ +#define NIFTI_S2I 6 /* Superior to Inferior */ + +void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) ; +void nifti_dmat44_to_orientation( nifti_dmat44 R, + int *icod, int *jcod, int *kcod ) ; + +/*--------------------- Low level IO routines ------------------------------*/ + +char * nifti_findhdrname (const char* fname); +char * nifti_findimgname (const char* fname , int nifti_type); +int nifti_is_gzfile (const char* fname); + +char * nifti_makebasename(const char* fname); + + +/* other routines */ +int nifti_convert_nim2n1hdr(const nifti_image* nim, nifti_1_header * hdr); +int nifti_convert_nim2n2hdr(const nifti_image* nim, nifti_2_header * hdr); +nifti_1_header * nifti_make_new_n1_header(const int64_t arg_dims[], int arg_dtype); +nifti_2_header * nifti_make_new_n2_header(const int64_t arg_dims[], int arg_dtype); +void * nifti2_read_header(const char *hname, int *nver, int check); +nifti_1_header * nifti_read_n1_hdr(const char *hname, int *swapped, int check); +nifti_2_header * nifti_read_n2_hdr(const char *hname, int *swapped, int check); +nifti_image * nifti2_copy_nim_info(const nifti_image * src); +nifti_image * nifti2_make_new_nim(const int64_t dims[], int datatype, + int data_fill); + + +nifti_image * nifti2_simple_init_nim(void); +nifti_image * nifti_convert_n1hdr2nim(nifti_1_header nhdr,const char *fname); +nifti_image * nifti_convert_n2hdr2nim(nifti_2_header nhdr,const char *fname); + +int nifti_looks_like_cifti(nifti_image * nim); + +int nifti_hdr1_looks_good (const nifti_1_header * hdr); +int nifti_hdr2_looks_good (const nifti_2_header * hdr); +int nifti_is_valid_datatype (int dtype); +int nifti_is_valid_ecode (int ecode); +int nifti2_nim_is_valid (nifti_image * nim, int complain); +int nifti2_nim_has_valid_dims (nifti_image * nim, int complain); +int is_valid_nifti2_type (int nifti_type); +int nifti_test_datatype_sizes (int verb); +int nifti2_type_and_names_match (nifti_image * nim, int show_warn); +int nifti2_update_dims_from_array(nifti_image * nim); +void nifti2_set_iname_offset (nifti_image *nim, int nifti_ver); +int nifti2_set_type_from_names (nifti_image * nim); +int nifti2_add_extension(nifti_image * nim, const char * data, int len, + int ecode ); +int nifti_compiled_with_zlib (void); +int nifti2_copy_extensions (nifti_image *nim_dest,const nifti_image *nim_src); +int nifti2_free_extensions (nifti_image *nim); +int64_t * nifti_get_int64list(int64_t nvals , const char *str); +int * nifti_get_intlist (int nvals , const char *str); +char * nifti_strdup (const char *str); +int valid_nifti2_extensions(const nifti_image *nim); +int nifti_valid_header_size(int ni_ver, int whine); + + +// Remap functions names that have NIfTI-2 variants +#if (RNIFTI_NIFTILIB_VERSION == 2) && !defined(NO_REMAP_NIFTI2_FUNCTIONS) + +#define nifti_get_filesize nifti2_get_filesize + +#define nifti_image_read_bricks nifti2_image_read_bricks +#define nifti_image_load_bricks nifti2_image_load_bricks +#define nifti_free_NBL nifti2_free_NBL + +#define nifti_image_read nifti2_image_read +#define nifti_image_load nifti2_image_load +#define nifti_image_unload nifti2_image_unload +#define nifti_image_free nifti2_image_free +#define nifti_image_is_data_owner nifti2_image_is_data_owner + +#define nifti_read_collapsed_image nifti2_read_collapsed_image +#define nifti_read_subregion_image nifti2_read_subregion_image + +#define nifti_image_write nifti2_image_write +#define nifti_image_write_bricks nifti2_image_write_bricks +#define nifti_image_infodump nifti2_image_infodump + +#define nifti_disp_lib_hist nifti2_disp_lib_hist +#define nifti_disp_matrix_orient nifti2_disp_matrix_orient +#define nifti_image_to_ascii nifti2_image_to_ascii +#define nifti_image_from_ascii nifti2_image_from_ascii + +#define nifti_get_volsize nifti2_get_volsize + +#define nifti_set_filenames nifti2_set_filenames +#define valid_nifti_brick_list valid_nifti2_brick_list +#define nifti_image_open nifti2_image_open +#define nifti_image_write_hdr_img nifti2_image_write_hdr_img +#define nifti_image_write_hdr_img2 nifti2_image_write_hdr_img2 +#define nifti_read_buffer nifti2_read_buffer +#define nifti_assign_buffer nifti2_assign_buffer +#define nifti_write_all_data nifti2_write_all_data +#define nifti_write_buffer nifti2_write_buffer +#define nifti_read_ascii_image nifti2_read_ascii_image +#define nifti_write_ascii_image nifti2_write_ascii_image + +#define nifti_read_header nifti2_read_header +#define nifti_copy_nim_info nifti2_copy_nim_info +#define nifti_make_new_nim nifti2_make_new_nim +#define nifti_simple_init_nim nifti2_simple_init_nim + +#define nifti_nim_is_valid nifti2_nim_is_valid +#define nifti_nim_has_valid_dims nifti2_nim_has_valid_dims +#define is_valid_nifti_type is_valid_nifti2_type +#define nifti_type_and_names_match nifti2_type_and_names_match +#define nifti_update_dims_from_array nifti2_update_dims_from_array +#define nifti_set_iname_offset nifti2_set_iname_offset +#define nifti_set_type_from_names nifti2_set_type_from_names +#define nifti_add_extension nifti2_add_extension +#define nifti_copy_extensions nifti2_copy_extensions +#define nifti_free_extensions nifti2_free_extensions +#define valid_nifti_extensions valid_nifti2_extensions + +#endif + +/*-------------------- Some C convenience macros ----------------------------*/ + +/* NIfTI-1.1 extension codes: + see http://nifti.nimh.nih.gov/nifti-1/documentation/faq#Q21 */ + +#define NIFTI_ECODE_IGNORE 0 /* changed from UNKNOWN, 29 June 2005 */ + +#define NIFTI_ECODE_DICOM 2 /* intended for raw DICOM attributes */ + +#define NIFTI_ECODE_AFNI 4 /* Robert W Cox: rwcox@nih.gov + https://afni.nimh.nih.gov/afni */ + +#define NIFTI_ECODE_COMMENT 6 /* plain ASCII text only */ + +#define NIFTI_ECODE_XCEDE 8 /* David B Keator: dbkeator@uci.edu + http://www.nbirn.net/Resources + /Users/Applications/ + /xcede/index.htm */ + +#define NIFTI_ECODE_JIMDIMINFO 10 /* Mark A Horsfield: + mah5@leicester.ac.uk + http://someplace/something */ + +#define NIFTI_ECODE_WORKFLOW_FWDS 12 /* Kate Fissell: fissell@pitt.edu + http://kraepelin.wpic.pitt.edu + /~fissell/NIFTI_ECODE_WORKFLOW_FWDS + /NIFTI_ECODE_WORKFLOW_FWDS.html */ + +#define NIFTI_ECODE_FREESURFER 14 /* http://surfer.nmr.mgh.harvard.edu */ + +#define NIFTI_ECODE_PYPICKLE 16 /* embedded Python objects + http://niftilib.sourceforge.net + /pynifti */ + + /* LONI MiND codes: http://www.loni.ucla.edu/twiki/bin/view/Main/MiND */ +#define NIFTI_ECODE_MIND_IDENT 18 /* Vishal Patel: vishal.patel@ucla.edu*/ +#define NIFTI_ECODE_B_VALUE 20 +#define NIFTI_ECODE_SPHERICAL_DIRECTION 22 +#define NIFTI_ECODE_DT_COMPONENT 24 +#define NIFTI_ECODE_SHC_DEGREEORDER 26 /* end LONI MiND codes */ + +#define NIFTI_ECODE_VOXBO 28 /* Dan Kimberg: www.voxbo.org */ + +#define NIFTI_ECODE_CARET 30 /* John Harwell: john@brainvis.wustl.edu + http://brainvis.wustl.edu/wiki + /index.php/Caret:Documentation + :CaretNiftiExtension */ + +#define NIFTI_ECODE_CIFTI 32 /* CIFTI-2_Main_FINAL_1March2014.pdf */ + +#define NIFTI_ECODE_VARIABLE_FRAME_TIMING 34 + +/* 36 is currently unassigned, waiting on NIFTI_ECODE_AGILENT_PROCPAR */ + +#define NIFTI_ECODE_EVAL 38 /* Munster University Hospital */ + +/* http://www.mathworks.com/matlabcentral/fileexchange/42997-dicom-to-nifti-converter */ +#define NIFTI_ECODE_MATLAB 40 /* MATLAB extension */ + +/* Quantiphyse extension + https://quantiphyse.readthedocs.io/en/latest/advanced/nifti_extension.html*/ +#define NIFTI_ECODE_QUANTIPHYSE 42 /* Quantiphyse extension */ + +/* Magnetic Resonance Spectroscopy (MRS) + link to come... */ +#define NIFTI_ECODE_MRS 44 /* MRS extension */ + +#define NIFTI_MAX_ECODE 44 /******* maximum extension code *******/ + +/* nifti_type file codes */ +#if RNIFTI_NIFTILIB_VERSION == 2 +#define NIFTI_FTYPE_ANALYZE 0 /* old ANALYZE */ +#define NIFTI_FTYPE_NIFTI1_1 1 /* NIFTI-1 */ +#define NIFTI_FTYPE_NIFTI1_2 2 +#define NIFTI_FTYPE_ASCII 3 +#define NIFTI_FTYPE_NIFTI2_1 4 /* NIFTI-2 */ +#define NIFTI_FTYPE_NIFTI2_2 5 +#define NIFTI_MAX_FTYPE 5 /* this should match the maximum code */ +#endif + +/*------------------------------------------------------------------------*/ +/*-- the rest of these apply only to nifti2_io.c, check for _NIFTI2_IO_C_ */ + +#ifdef _NIFTI2_IO_C_ + +typedef struct { + int debug; /*!< debug level for status reports */ + int skip_blank_ext; /*!< skip extender if no extensions */ + int allow_upper_fext; /*!< allow uppercase file extensions */ + int alter_cifti; /*!< convert CIFTI dimensions */ +} nifti_global_options; + +typedef struct { + int type; /* should match the NIFTI_TYPE_ #define */ + int nbyper; /* bytes per value, matches nifti_image */ + int swapsize; /* bytes per swap piece, matches nifti_image */ + char const * const name; /* text string to match #define */ +} nifti_type_ele; + +#undef LNI_FERR /* local nifti file error, to be compact and repetative */ +#ifdef USING_R +#define LNI_FERR(func,msg,file) \ + Rf_warning("%s: %s '%s'\n",func,msg,file) +#else +#define LNI_FERR(func,msg,file) \ + Rc_fprintf_stderr("** ERROR (%s): %s '%s'\n",func,msg,file) +#endif + +#undef swap_2 +#undef swap_4 +#define swap_2(s) nifti_swap_2bytes(1,&(s)) /* s: 2-byte short; swap in place */ +#define swap_4(v) nifti_swap_4bytes(1,&(v)) /* v: 4-byte value; swap in place */ + + /***** isfinite() is a C99 macro, which is + present in many C implementations already *****/ + +#undef IS_GOOD_FLOAT +#undef FIXED_FLOAT + +#ifdef isfinite /* use isfinite() to check floats/doubles for goodness */ +# define IS_GOOD_FLOAT(x) isfinite(x) /* check if x is a "good" float */ +# define FIXED_FLOAT(x) (isfinite(x) ? (x) : 0) /* fixed if bad */ +#else +# define IS_GOOD_FLOAT(x) 1 /* don't check it */ +# define FIXED_FLOAT(x) (x) /* don't fix it */ +#endif + +#undef ASSIF /* assign v to *p, if possible */ +#define ASSIF(p,v) if( (p)!=NULL ) *(p) = (v) + +#undef MSB_FIRST +#undef LSB_FIRST +#undef REVERSE_ORDER +#define LSB_FIRST 1 +#define MSB_FIRST 2 +#define REVERSE_ORDER(x) (3-(x)) /* convert MSB_FIRST <--> LSB_FIRST */ + +#define LNI_MAX_NIA_EXT_LEN 100000 /* consider a longer extension invalid */ + +#undef NIFTI_IS_16_BIT_INT +#define NIFTI_IS_16_BIT_INT(x) ((x) <= 32767 && (x) >= -32768) + +#endif /* _NIFTI2_IO_C_ section */ +/*------------------------------------------------------------------------*/ + +#endif /* _NIFTI2_IO_HEADER_ */ diff --git a/cpp/3rd_party/rnifti/znzlib/znzlib.cpp b/cpp/3rd_party/rnifti/znzlib/znzlib.cpp new file mode 100644 index 0000000000..e7ebe7645a --- /dev/null +++ b/cpp/3rd_party/rnifti/znzlib/znzlib.cpp @@ -0,0 +1,463 @@ +/** \file znzlib.c + \brief Low level i/o interface to compressed and noncompressed files. + Written by Mark Jenkinson, FMRIB + +This library provides an interface to both compressed (gzip/zlib) and +uncompressed (normal) file IO. The functions are written to have the +same interface as the standard file IO functions. + +To use this library instead of normal file IO, the following changes +are required: + - replace all instances of FILE* with znzFile + - change the name of all function calls, replacing the initial character + f with the znz (e.g. fseek becomes znzseek) + one exception is rewind() -> znzrewind() + - add a third parameter to all calls to znzopen (previously fopen) + that specifies whether to use compression (1) or not (0) + - use znz_isnull rather than any (pointer == NULL) comparisons in the code + for znzfile types (normally done after a return from znzopen) + +NB: seeks for writable files with compression are quite restricted + + */ + +#include "znzlib/znzlib.h" +#include "RNifti/NiftiImage_print.h" + +/* +znzlib.c (zipped or non-zipped library) + +***** This code is released to the public domain. ***** + +***** Author: Mark Jenkinson, FMRIB Centre, University of Oxford ***** +***** Date: September 2004 ***** + +***** Neither the FMRIB Centre, the University of Oxford, nor any of ***** +***** its employees imply any warranty of usefulness of this software ***** +***** for any purpose, and do not assume any liability for damages, ***** +***** incidental or otherwise, caused by any use of this document. ***** + +*/ + +/* Note extra argument (use_compression) where + use_compression==0 is no compression + use_compression!=0 uses zlib (gzip) compression +*/ + +namespace { +#ifdef HAVE_ZLIB +#define CHUNK_SIZE 16384 +int decompress_gzip(const unsigned char* compressed_data, + unsigned long compressed_size, + unsigned long estimated_size, + unsigned char** decompressed_data, + unsigned long* decompressed_size) +{ + int ret; + z_stream strm; + unsigned long total_size = 0; + unsigned long offset = 0; + unsigned long current_buffer_size = estimated_size ? estimated_size : 1; + + // Allocate initial memory for decompressed data + *decompressed_data = (unsigned char*)malloc(current_buffer_size); + if (*decompressed_data == NULL) { + Rc_fprintf_stderr("Memory allocation failed\n"); + return -1; + } + + // Initialize zlib stream + memset(&strm, 0, sizeof(z_stream)); + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + + // Initialize inflate + ret = inflateInit2(&strm, 15 + 32); // 15 + 32 for automatic detection of gzip format + if (ret != Z_OK) { + Rc_fprintf_stderr("Failed to initialize zlib: %s\n", zError(ret)); + free(*decompressed_data); + *decompressed_data = NULL; + return ret; + } + + strm.avail_in = compressed_size; + strm.next_in = const_cast(compressed_data); + + // Decompress loop + do { + unsigned long avail_to_write = current_buffer_size - offset; + strm.avail_out = avail_to_write; + strm.next_out = *decompressed_data + offset; + + ret = inflate(&strm, Z_NO_FLUSH); + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: + Rc_fprintf_stderr("Decompression failed: %s\n", zError(ret)); + inflateEnd(&strm); + free(*decompressed_data); + *decompressed_data = NULL; + return ret; + } + + // Calculate total size of decompressed data + long output_size = avail_to_write - strm.avail_out; + total_size += output_size; + + if (!strm.avail_out && strm.avail_in) { + // Reallocate memory for decompressed data + unsigned long prev_buf_size = current_buffer_size; + current_buffer_size = total_size + strm.avail_in; + *decompressed_data = (unsigned char*)realloc(*decompressed_data, current_buffer_size); + if (*decompressed_data == NULL) { + Rc_fprintf_stderr("Memory reallocation failed\n"); + inflateEnd(&strm); + return -1; + } + } + + offset += output_size; + } while (ret != Z_STREAM_END); + + // Set the final decompressed size + *decompressed_size = total_size; + + // Clean up + inflateEnd(&strm); + + return Z_OK; +} +#endif // HAVE_ZLIB +} // namespace + +// Windows compatibility: fmemopen alternative using tmpfile +#ifdef _WIN32 +static FILE* fmemopen_windows(void* buf, size_t size, const char* mode) +{ + // Create a temporary file + FILE* tmpf = tmpfile(); + if (tmpf == NULL) { + Rc_fprintf_stderr("** ERROR: Failed to create temporary file\n"); + return NULL; + } + + // Write the buffer contents to the temporary file + if (fwrite(buf, 1, size, tmpf) != size) { + Rc_fprintf_stderr("** ERROR: Failed to write buffer to temporary file\n"); + fclose(tmpf); + return NULL; + } + + // Rewind to the beginning for reading + rewind(tmpf); + return tmpf; +} +#endif + +znzFile znzopen(const char* path, const char* mode, int use_compression) +{ + znzFile file; + file = (znzFile)calloc(1, sizeof(struct znzptr)); + if (file == NULL) { + Rc_fprintf_stderr("** ERROR: znzopen failed to alloc znzptr\n"); + return NULL; + } + + file->nzfptr = NULL; + +#ifdef HAVE_ZLIB + file->zfptr = NULL; + file->zmemptr = NULL; + + if (use_compression) { + file->withz = 1; + if ((file->zfptr = gzopen(path, mode)) == NULL) { + free(file); + file = NULL; + } + } else { +#endif + + file->withz = 0; + if ((file->nzfptr = fopen(path, mode)) == NULL) { + free(file); + file = NULL; + } + +#ifdef HAVE_ZLIB + } +#endif + + return file; +} + +znzFile znzmemopen(znzFile fp, + const uint8_t* data, + const size_t size, + const char* mode, + int use_compression, + const size_t estimated_output_size) +{ + znzFile file = fp; + if (file == NULL) { + file = (znzFile)calloc(1, sizeof(struct znzptr)); + if (file == NULL) { + Rc_fprintf_stderr("** ERROR: znzopen failed to alloc znzptr\n"); + return NULL; + } + } + + file->nzfptr = NULL; + +#ifdef HAVE_ZLIB + file->zfptr = NULL; + file->zmemptr = NULL; + + if (use_compression) { + unsigned char* decompressed_data = NULL; + unsigned long decompressed_size = 0; + + decompress_gzip( + (const unsigned char*)data, size, estimated_output_size, &decompressed_data, &decompressed_size); + + if (decompressed_data == NULL) { + Rc_fprintf_stderr("** ERROR: znzmemopen failed to decompress memory buffer\n"); + } else { + file->withz = 1; + file->zmemptr = new zfmem(); + file->zmemptr->pos = 0; + file->zmemptr->buffer = std::make_shared(decompressed_data); + file->zmemptr->size = decompressed_size; + } + } else { +#endif + + file->withz = 0; +#ifdef _WIN32 + // Use Windows-compatible alternative + if ((file->nzfptr = fmemopen_windows((void*)data, size, mode)) == NULL) { +#else + // Use standard POSIX fmemopen on Unix/Linux/Mac + if ((file->nzfptr = fmemopen((void*)data, size, mode)) == NULL) { +#endif + if (fp == NULL) { + free(file); + } + file = NULL; + } + +#ifdef HAVE_ZLIB + } +#endif + + return file; +} + +int Xznzclose(znzFile* file) +{ + int retval = 0; + if (*file != NULL) { +#ifdef HAVE_ZLIB + if ((*file)->zfptr != NULL) { + retval = gzclose((*file)->zfptr); + } + if ((*file)->zmemptr != nullptr) { + (*file)->zmemptr->buffer = nullptr; + delete (*file)->zmemptr; + (*file)->zmemptr = NULL; + } +#endif + if ((*file)->nzfptr != NULL) { + retval = fclose((*file)->nzfptr); + } + + free(*file); + *file = NULL; + } + return retval; +} + +/* we already assume ints are 4 bytes */ +#undef ZNZ_MAX_BLOCK_SIZE +#define ZNZ_MAX_BLOCK_SIZE (1 << 30) + +size_t znzread(void* buf, size_t size, size_t nmemb, znzFile file) +{ + size_t remain = size * nmemb; + char* cbuf = (char*)buf; + unsigned n2read; + int nread; + + if (file == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + if (file->zfptr != NULL) { + /* gzread/write take unsigned int length, so maybe read in int pieces + (noted by M Hanke, example given by M Adler) 6 July 2010 [rickr] */ + while (remain > 0) { + n2read = (remain < ZNZ_MAX_BLOCK_SIZE) ? remain : ZNZ_MAX_BLOCK_SIZE; + nread = gzread(file->zfptr, (void*)cbuf, n2read); + if (nread < 0) + return nread; /* returns -1 on error */ + + remain -= nread; + cbuf += nread; + + /* require reading n2read bytes, so we don't get stuck */ + if (nread < (int)n2read) + break; /* return will be short */ + } + + /* warn of a short read that will seem complete */ + if (remain > 0 && remain < size) + Rc_fprintf_stderr("** znzread: read short by %u bytes\n", (unsigned)remain); + + return nmemb - remain / size; /* return number of members processed */ + } + if (file->zmemptr != NULL) { + memcpy(buf, file->zmemptr->buffer->data() + file->zmemptr->pos, remain); + file->zmemptr->pos += remain; + return remain; + } +#endif + return fread(buf, size, nmemb, file->nzfptr); +} + +// this method is meant to be used only for cases when `zmemptr` is not null. +// this provides an access to the underlying memory_buffer for znzFile. +// the buffer is shared and is used whenever the decoder gets the data +size_t znzassign(void** buf, size_t size, size_t nmemb, znzFile file) +{ + if (file == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + if (file->zfptr != NULL) { + Rc_fprintf_stderr("** znzassign is not supported for file decompressor, use znzread instead\n"); + return -1; + } + if (file->zmemptr != NULL) { + size_t remain = size * nmemb; + *buf = file->zmemptr->buffer->data() + file->zmemptr->pos; + file->zmemptr->pos += remain; + return remain; + } +#endif + Rc_fprintf_stderr("** znzassign is not supported for file decompressor, use znzread instead\n"); + return -1; +} + +size_t znzwrite(const void* buf, size_t size, size_t nmemb, znzFile file) +{ + size_t remain = size * nmemb; + const char* cbuf = (const char*)buf; + unsigned n2write; + int nwritten; + + if (file == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + if (file->zfptr != NULL) { + while (remain > 0) { + n2write = (remain < ZNZ_MAX_BLOCK_SIZE) ? remain : ZNZ_MAX_BLOCK_SIZE; + nwritten = gzwrite(file->zfptr, (const void*)cbuf, n2write); + + /* gzread returns 0 on error, but in case that ever changes... */ + if (nwritten < 0) + return nwritten; + + remain -= nwritten; + cbuf += nwritten; + + /* require writing n2write bytes, so we don't get stuck */ + if (nwritten < (int)n2write) + break; + } + + /* warn of a short write that will seem complete */ + if (remain > 0 && remain < size) + Rc_fprintf_stderr("** znzwrite: write short by %u bytes\n", (unsigned)remain); + + return nmemb - remain / size; /* return number of members processed */ + } +#endif + return fwrite(buf, size, nmemb, file->nzfptr); +} + +long znzseek(znzFile file, long offset, int whence) +{ + if (file == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + if (file->zfptr != NULL) + return (long)gzseek(file->zfptr, offset, whence); + if (file->zmemptr != NULL) { + if (whence == SEEK_SET) { + file->zmemptr->pos = offset; + } else if (whence == SEEK_END) { + file->zmemptr->pos = file->zmemptr->size - 1 - offset; + } else { + file->zmemptr->pos += offset; + } + + return file->zmemptr->pos; + } +#endif + return fseek(file->nzfptr, offset, whence); +} + +int znzrewind(znzFile stream) +{ + if (stream == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + /* On some systems, gzrewind() fails for uncompressed files. + Use gzseek(), instead. 10, May 2005 [rickr] + + if (stream->zfptr!=NULL) return gzrewind(stream->zfptr); + */ + + if (stream->zfptr != NULL) + return (int)gzseek(stream->zfptr, 0L, SEEK_SET); + if (stream->zmemptr != NULL) { + stream->zmemptr->pos = 0; + return 0; + } +#endif + rewind(stream->nzfptr); + return 0; +} + +long znztell(znzFile file) +{ + if (file == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + if (file->zfptr != NULL) + return (long)gztell(file->zfptr); + if (file->zmemptr != NULL) + return file->zmemptr->pos; +#endif + return ftell(file->nzfptr); +} + +int znzputs(const char* str, znzFile file) +{ + if (file == NULL) { + return 0; + } +#ifdef HAVE_ZLIB + if (file->zfptr != NULL) + return gzputs(file->zfptr, str); +#endif + return fputs(str, file->nzfptr); +} \ No newline at end of file diff --git a/cpp/3rd_party/rnifti/znzlib/znzlib.h b/cpp/3rd_party/rnifti/znzlib/znzlib.h new file mode 100644 index 0000000000..ba92371be0 --- /dev/null +++ b/cpp/3rd_party/rnifti/znzlib/znzlib.h @@ -0,0 +1,123 @@ +#ifndef _ZNZLIB_H_ +#define _ZNZLIB_H_ + +/* +znzlib.h (zipped or non-zipped library) + +***** This code is released to the public domain. ***** + +***** Author: Mark Jenkinson, FMRIB Centre, University of Oxford ***** +***** Date: September 2004 ***** + +***** Neither the FMRIB Centre, the University of Oxford, nor any of ***** +***** its employees imply any warranty of usefulness of this software ***** +***** for any purpose, and do not assume any liability for damages, ***** +***** incidental or otherwise, caused by any use of this document. ***** + +*/ + +/* + +This library provides an interface to both compressed (gzip/zlib) and +uncompressed (normal) file IO. The functions are written to have the +same interface as the standard file IO functions. + +To use this library instead of normal file IO, the following changes +are required: + - replace all instances of FILE* with znzFile + - change the name of all function calls, replacing the initial character + f with the znz (e.g. fseek becomes znzseek) + - add a third parameter to all calls to znzopen (previously fopen) + that specifies whether to use compression (1) or not (0) + - use znz_isnull rather than any (pointer == NULL) comparisons in the code + +NB: seeks for writable files with compression are quite restricted + +*/ + + +#include +#include +#include +#include +#include + +#include + +/* include optional check for HAVE_FDOPEN here, from deleted config.h: + + uncomment the following line if fdopen() exists for your compiler and + compiler options +*/ +/* #define HAVE_FDOPEN */ + + +#ifdef HAVE_ZLIB + +#if defined(ITKZLIB) && !defined(ITK_USE_SYSTEM_ZLIB) +#include "itk_zlib.h" +#else +#include "zlib.h" +#endif +#endif + +#ifdef HAVE_ZLIB +class zfmembuf { +public: + zfmembuf(unsigned char* data): data_(data) {} + ~zfmembuf() { + free(data_); + } + unsigned char* data() const { + return data_; + } +private: + unsigned char* data_ = NULL; +}; + +typedef struct zfmem_ { + size_t pos; + std::shared_ptr buffer; + size_t size; +} zfmem; +#endif // HAVE_ZLIB + +struct znzptr { + int withz; + FILE* nzfptr; +#ifdef HAVE_ZLIB + gzFile zfptr; + zfmem* zmemptr; +#endif +}; + +/* the type for all file pointers */ +typedef struct znzptr * znzFile; + +#define znz_isnull(f) ((f) == NULL) +#define znzclose(f) Xznzclose(&(f)) + +/* Note extra argument (use_compression) where + use_compression==0 is no compression + use_compression!=0 uses zlib (gzip) compression +*/ + +znzFile znzopen(const char *path, const char *mode, int use_compression); +znzFile znzmemopen(znzFile fp, const uint8_t* data, const size_t size, const char *mode, int use_compression, const size_t estimated_output_size); + +int Xznzclose(znzFile * file); + +size_t znzread(void* buf, size_t size, size_t nmemb, znzFile file); +size_t znzassign(void** buf, size_t size, size_t nmemb, znzFile file); + +size_t znzwrite(const void* buf, size_t size, size_t nmemb, znzFile file); + +long znzseek(znzFile file, long offset, int whence); + +int znzrewind(znzFile stream); + +long znztell(znzFile file); + +int znzputs(const char *str, znzFile file); + +#endif diff --git a/cpp/3rd_party/span/span b/cpp/3rd_party/span/span new file mode 100644 index 0000000000..f3eab9704e --- /dev/null +++ b/cpp/3rd_party/span/span @@ -0,0 +1,10 @@ +#pragma once + +#include "tcb.hpp" + +namespace std { + +using tcb::span; + +} + diff --git a/cpp/3rd_party/span/tcb.hpp b/cpp/3rd_party/span/tcb.hpp new file mode 100644 index 0000000000..9716030f28 --- /dev/null +++ b/cpp/3rd_party/span/tcb.hpp @@ -0,0 +1,618 @@ + +/* +This is an implementation of C++20's std::span +http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf +*/ + +// Copyright Tristan Brindle 2018. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file ../../LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +#ifndef TCB_SPAN_HPP_INCLUDED +#define TCB_SPAN_HPP_INCLUDED + +#include +#include +#include +#include + +#ifndef TCB_SPAN_NO_EXCEPTIONS +// Attempt to discover whether we're being compiled with exception support +#if !(defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) +#define TCB_SPAN_NO_EXCEPTIONS +#endif +#endif + +#ifndef TCB_SPAN_NO_EXCEPTIONS +#include +#include +#endif + +// Various feature test macros + +#ifndef TCB_SPAN_NAMESPACE_NAME +#define TCB_SPAN_NAMESPACE_NAME tcb +#endif + +#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) +#define TCB_SPAN_HAVE_CPP17 +#endif + +#if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) +#define TCB_SPAN_HAVE_CPP14 +#endif + +namespace TCB_SPAN_NAMESPACE_NAME { + +// Establish default contract checking behavior +#if !defined(TCB_SPAN_THROW_ON_CONTRACT_VIOLATION) && \ + !defined(TCB_SPAN_TERMINATE_ON_CONTRACT_VIOLATION) && \ + !defined(TCB_SPAN_NO_CONTRACT_CHECKING) +#if defined(NDEBUG) || !defined(TCB_SPAN_HAVE_CPP14) +#define TCB_SPAN_NO_CONTRACT_CHECKING +#else +#define TCB_SPAN_TERMINATE_ON_CONTRACT_VIOLATION +#endif +#endif + +#if defined(TCB_SPAN_THROW_ON_CONTRACT_VIOLATION) +struct contract_violation_error : std::logic_error { + explicit contract_violation_error(const char* msg) : std::logic_error(msg) + {} +}; + +inline void contract_violation(const char* msg) +{ + throw contract_violation_error(msg); +} + +#elif defined(TCB_SPAN_TERMINATE_ON_CONTRACT_VIOLATION) +[[noreturn]] inline void contract_violation(const char* /*unused*/) +{ + std::terminate(); +} +#endif + +#if !defined(TCB_SPAN_NO_CONTRACT_CHECKING) +#define TCB_SPAN_STRINGIFY(cond) #cond +#define TCB_SPAN_EXPECT(cond) \ + cond ? (void) 0 : contract_violation("Expected " TCB_SPAN_STRINGIFY(cond)) +#else +#define TCB_SPAN_EXPECT(cond) +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_inline_variables) +#define TCB_SPAN_INLINE_VAR inline +#else +#define TCB_SPAN_INLINE_VAR +#endif + +#if defined(TCB_SPAN_HAVE_CPP14) || \ + (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) +#define TCB_SPAN_HAVE_CPP14_CONSTEXPR +#endif + +#if defined(TCB_SPAN_HAVE_CPP14_CONSTEXPR) +#define TCB_SPAN_CONSTEXPR14 constexpr +#else +#define TCB_SPAN_CONSTEXPR14 +#endif + +#if defined(TCB_SPAN_HAVE_CPP14_CONSTEXPR) && \ + (!defined(_MSC_VER) || _MSC_VER > 1900) +#define TCB_SPAN_CONSTEXPR_ASSIGN constexpr +#else +#define TCB_SPAN_CONSTEXPR_ASSIGN +#endif + +#if defined(TCB_SPAN_NO_CONTRACT_CHECKING) +#define TCB_SPAN_CONSTEXPR11 constexpr +#else +#define TCB_SPAN_CONSTEXPR11 TCB_SPAN_CONSTEXPR14 +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_deduction_guides) +#define TCB_SPAN_HAVE_DEDUCTION_GUIDES +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_lib_byte) +#define TCB_SPAN_HAVE_STD_BYTE +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_lib_array_constexpr) +#define TCB_SPAN_HAVE_CONSTEXPR_STD_ARRAY_ETC +#endif + +#if defined(TCB_SPAN_HAVE_CONSTEXPR_STD_ARRAY_ETC) +#define TCB_SPAN_ARRAY_CONSTEXPR constexpr +#else +#define TCB_SPAN_ARRAY_CONSTEXPR +#endif + +#ifdef TCB_SPAN_HAVE_STD_BYTE +using byte = std::byte; +#else +using byte = unsigned char; +#endif + +#if defined(TCB_SPAN_HAVE_CPP17) +#define TCB_SPAN_NODISCARD [[nodiscard]] +#else +#define TCB_SPAN_NODISCARD +#endif + +TCB_SPAN_INLINE_VAR constexpr std::size_t dynamic_extent = SIZE_MAX; + +template +class span; + +namespace detail { + +template +struct span_storage { + constexpr span_storage() noexcept = default; + + constexpr span_storage(E* p_ptr, std::size_t /*unused*/) noexcept + : ptr(p_ptr) + {} + + E* ptr = nullptr; + static constexpr std::size_t size = S; +}; + +template +struct span_storage { + constexpr span_storage() noexcept = default; + + constexpr span_storage(E* p_ptr, std::size_t p_size) noexcept + : ptr(p_ptr), size(p_size) + {} + + E* ptr = nullptr; + std::size_t size = 0; +}; + +// Reimplementation of C++17 std::size() and std::data() +#if defined(TCB_SPAN_HAVE_CPP17) || \ + defined(__cpp_lib_nonmember_container_access) +using std::data; +using std::size; +#else +template +constexpr auto size(const C& c) -> decltype(c.size()) +{ + return c.size(); +} + +template +constexpr std::size_t size(const T (&)[N]) noexcept +{ + return N; +} + +template +constexpr auto data(C& c) -> decltype(c.data()) +{ + return c.data(); +} + +template +constexpr auto data(const C& c) -> decltype(c.data()) +{ + return c.data(); +} + +template +constexpr T* data(T (&array)[N]) noexcept +{ + return array; +} + +template +constexpr const E* data(std::initializer_list il) noexcept +{ + return il.begin(); +} +#endif // TCB_SPAN_HAVE_CPP17 + +#if defined(TCB_SPAN_HAVE_CPP17) || defined(__cpp_lib_void_t) +using std::void_t; +#else +template +using void_t = void; +#endif + +template +using uncvref_t = + typename std::remove_cv::type>::type; + +template +struct is_span : std::false_type {}; + +template +struct is_span> : std::true_type {}; + +template +struct is_std_array : std::false_type {}; + +template +struct is_std_array> : std::true_type {}; + +template +struct has_size_and_data : std::false_type {}; + +template +struct has_size_and_data())), + decltype(detail::data(std::declval()))>> + : std::true_type {}; + +template > +struct is_container { + static constexpr bool value = + !is_span::value && !is_std_array::value && + !std::is_array::value && has_size_and_data::value; +}; + +template +using remove_pointer_t = typename std::remove_pointer::type; + +template +struct is_container_element_type_compatible : std::false_type {}; + +template +struct is_container_element_type_compatible< + T, E, + typename std::enable_if< + !std::is_same< + typename std::remove_cv()))>::type, + void>::value && + std::is_convertible< + remove_pointer_t()))> (*)[], + E (*)[]>::value + >::type> + : std::true_type {}; + +template +struct is_complete : std::false_type {}; + +template +struct is_complete : std::true_type {}; + +} // namespace detail + +template +class span { + static_assert(std::is_object::value, + "A span's ElementType must be an object type (not a " + "reference type or void)"); + static_assert(detail::is_complete::value, + "A span's ElementType must be a complete type (not a forward " + "declaration)"); + static_assert(!std::is_abstract::value, + "A span's ElementType cannot be an abstract class type"); + + using storage_type = detail::span_storage; + +public: + // constants and types + using element_type = ElementType; + using value_type = typename std::remove_cv::type; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = element_type*; + using const_pointer = const element_type*; + using reference = element_type&; + using const_reference = const element_type&; + using iterator = pointer; + using reverse_iterator = std::reverse_iterator; + + static constexpr size_type extent = Extent; + + // [span.cons], span constructors, copy, assignment, and destructor + template < + std::size_t E = Extent, + typename std::enable_if<(E == dynamic_extent || E <= 0), int>::type = 0> + constexpr span() noexcept + {} + + TCB_SPAN_CONSTEXPR11 span(pointer ptr, size_type count) + : storage_(ptr, count) + { + TCB_SPAN_EXPECT(extent == dynamic_extent || count == extent); + } + + TCB_SPAN_CONSTEXPR11 span(pointer first_elem, pointer last_elem) + : storage_(first_elem, last_elem - first_elem) + { + TCB_SPAN_EXPECT(extent == dynamic_extent || + last_elem - first_elem == + static_cast(extent)); + } + + template ::value, + int>::type = 0> + constexpr span(element_type (&arr)[N]) noexcept : storage_(arr, N) + {} + + template &, ElementType>::value, + int>::type = 0> + TCB_SPAN_ARRAY_CONSTEXPR span(std::array& arr) noexcept + : storage_(arr.data(), N) + {} + + template &, ElementType>::value, + int>::type = 0> + TCB_SPAN_ARRAY_CONSTEXPR span(const std::array& arr) noexcept + : storage_(arr.data(), N) + {} + + template < + typename Container, std::size_t E = Extent, + typename std::enable_if< + E == dynamic_extent && detail::is_container::value && + detail::is_container_element_type_compatible< + Container&, ElementType>::value, + int>::type = 0> + constexpr span(Container& cont) + : storage_(detail::data(cont), detail::size(cont)) + {} + + template < + typename Container, std::size_t E = Extent, + typename std::enable_if< + E == dynamic_extent && detail::is_container::value && + detail::is_container_element_type_compatible< + const Container&, ElementType>::value, + int>::type = 0> + constexpr span(const Container& cont) + : storage_(detail::data(cont), detail::size(cont)) + {} + + constexpr span(const span& other) noexcept = default; + + template ::value, + int>::type = 0> + constexpr span(const span& other) noexcept + : storage_(other.data(), other.size()) + {} + + ~span() noexcept = default; + + TCB_SPAN_CONSTEXPR_ASSIGN span& + operator=(const span& other) noexcept = default; + + // [span.sub], span subviews + template + TCB_SPAN_CONSTEXPR11 span first() const + { + TCB_SPAN_EXPECT(Count <= size()); + return {data(), Count}; + } + + template + TCB_SPAN_CONSTEXPR11 span last() const + { + TCB_SPAN_EXPECT(Count <= size()); + return {data() + (size() - Count), Count}; + } + + template + using subspan_return_t = + span; + + template + TCB_SPAN_CONSTEXPR11 subspan_return_t subspan() const + { + TCB_SPAN_EXPECT(Offset <= size() && + (Count == dynamic_extent || Offset + Count <= size())); + return {data() + Offset, + Count != dynamic_extent ? Count : size() - Offset}; + } + + TCB_SPAN_CONSTEXPR11 span + first(size_type count) const + { + TCB_SPAN_EXPECT(count <= size()); + return {data(), count}; + } + + TCB_SPAN_CONSTEXPR11 span + last(size_type count) const + { + TCB_SPAN_EXPECT(count <= size()); + return {data() + (size() - count), count}; + } + + TCB_SPAN_CONSTEXPR11 span + subspan(size_type offset, size_type count = dynamic_extent) const + { + TCB_SPAN_EXPECT(offset <= size() && + (count == dynamic_extent || offset + count <= size())); + return {data() + offset, + count == dynamic_extent ? size() - offset : count}; + } + + // [span.obs], span observers + constexpr size_type size() const noexcept { return storage_.size; } + + constexpr size_type size_bytes() const noexcept + { + return size() * sizeof(element_type); + } + + TCB_SPAN_NODISCARD constexpr bool empty() const noexcept + { + return size() == 0; + } + + // [span.elem], span element access + TCB_SPAN_CONSTEXPR11 reference operator[](size_type idx) const + { + TCB_SPAN_EXPECT(idx < size()); + return *(data() + idx); + } + + TCB_SPAN_CONSTEXPR11 reference front() const + { + TCB_SPAN_EXPECT(!empty()); + return *data(); + } + + TCB_SPAN_CONSTEXPR11 reference back() const + { + TCB_SPAN_EXPECT(!empty()); + return *(data() + (size() - 1)); + } + + constexpr pointer data() const noexcept { return storage_.ptr; } + + // [span.iterators], span iterator support + constexpr iterator begin() const noexcept { return data(); } + + constexpr iterator end() const noexcept { return data() + size(); } + + TCB_SPAN_ARRAY_CONSTEXPR reverse_iterator rbegin() const noexcept + { + return reverse_iterator(end()); + } + + TCB_SPAN_ARRAY_CONSTEXPR reverse_iterator rend() const noexcept + { + return reverse_iterator(begin()); + } + +private: + storage_type storage_{}; +}; + +#ifdef TCB_SPAN_HAVE_DEDUCTION_GUIDES + +/* Deduction Guides */ +template +span(T (&)[N])->span; + +template +span(std::array&)->span; + +template +span(const std::array&)->span; + +template +span(Container&)->span()))>::type>; + +template +span(const Container&)->span; + +#endif // TCB_HAVE_DEDUCTION_GUIDES + +template +constexpr span +make_span(span s) noexcept +{ + return s; +} + +template +constexpr span make_span(T (&arr)[N]) noexcept +{ + return {arr}; +} + +template +TCB_SPAN_ARRAY_CONSTEXPR span make_span(std::array& arr) noexcept +{ + return {arr}; +} + +template +TCB_SPAN_ARRAY_CONSTEXPR span +make_span(const std::array& arr) noexcept +{ + return {arr}; +} + +template +constexpr span()))>::type> +make_span(Container& cont) +{ + return {cont}; +} + +template +constexpr span +make_span(const Container& cont) +{ + return {cont}; +} + +template +span +as_bytes(span s) noexcept +{ + return {reinterpret_cast(s.data()), s.size_bytes()}; +} + +template < + class ElementType, size_t Extent, + typename std::enable_if::value, int>::type = 0> +span +as_writable_bytes(span s) noexcept +{ + return {reinterpret_cast(s.data()), s.size_bytes()}; +} + +template +constexpr auto get(span s) -> decltype(s[N]) +{ + return s[N]; +} + +} // namespace TCB_SPAN_NAMESPACE_NAME + +namespace std { + +template +class tuple_size> + : public integral_constant {}; + +template +class tuple_size>; // not defined + +template +class tuple_element> { +public: + static_assert(Extent != TCB_SPAN_NAMESPACE_NAME::dynamic_extent && + I < Extent, + ""); + using type = ElementType; +}; + +} // end namespace std + +#endif // TCB_SPAN_HPP_INCLUDED \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/CMakeLists.txt b/cpp/3rd_party/sql-parser/CMakeLists.txt new file mode 100644 index 0000000000..6c489c3a4a --- /dev/null +++ b/cpp/3rd_party/sql-parser/CMakeLists.txt @@ -0,0 +1,7 @@ +project(hsql) + +file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.c") + +ADD_LIBRARY(hsql ${SOURCES}) + +target_link_libraries(hsql PRIVATE fmt::fmt-header-only) \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/LICENSE b/cpp/3rd_party/sql-parser/LICENSE new file mode 100644 index 0000000000..92b3a42844 --- /dev/null +++ b/cpp/3rd_party/sql-parser/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2012-2017 Hasso-Plattner-Institut + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cpp/3rd_party/sql-parser/Makefile b/cpp/3rd_party/sql-parser/Makefile new file mode 100644 index 0000000000..2564a9c0a6 --- /dev/null +++ b/cpp/3rd_party/sql-parser/Makefile @@ -0,0 +1,157 @@ +all: library + +####################################### +############# Directories ############# +####################################### +BIN = bin +SRC = src +SRCPARSER = src/parser + +INSTALL = /usr/local + +###################################### +############ Compile Mode ############ +###################################### +# Set compile mode to -g or -O3. +# Debug mode: make mode=debug + +mode ?= release +MODE_LOG = "" +OPT_FLAG = +ifeq ($(mode), debug) + OPT_FLAG = -g + MODE_LOG = "Building in \033[1;31mdebug\033[0m mode" +else + OPT_FLAG = -O3 + MODE_LOG = "Building in \033[0;32mrelease\033[0m mode ('make mode=debug' for debug mode)" +endif + +GMAKE = make mode=$(mode) + + + +####################################### +############### Library ############### +####################################### +NAME := sqlparser +PARSER_CPP = $(SRCPARSER)/bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp +PARSER_H = $(SRCPARSER)/bison_parser.h $(SRCPARSER)/flex_lexer.h +LIB_CFLAGS = -std=c++1z -Wall -Werror $(OPT_FLAG) + +static ?= no +ifeq ($(static), yes) + LIB_BUILD = lib$(NAME).a + LIBLINKER = $(AR) + LIB_LFLAGS = rs +else + LIB_BUILD = lib$(NAME).so + LIBLINKER = $(CXX) + LIB_CFLAGS += -fPIC + LIB_LFLAGS = -shared -o +endif +LIB_CPP = $(sort $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(PARSER_CPP)) +LIB_H = $(shell find $(SRC) -name '*.h' -not -path "$(SRCPARSER)/*") $(PARSER_H) +LIB_ALL = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(shell find $(SRC) -name '*.h' -not -path "$(SRCPARSER)/*") +LIB_OBJ = $(LIB_CPP:%.cpp=%.o) + +library: $(LIB_BUILD) + +$(LIB_BUILD): $(LIB_OBJ) + $(LIBLINKER) $(LIB_LFLAGS) $(LIB_BUILD) $(LIB_OBJ) + +$(SRCPARSER)/flex_lexer.o: $(SRCPARSER)/flex_lexer.cpp $(SRCPARSER)/bison_parser.cpp + $(CXX) $(LIB_CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-register + +%.o: %.cpp $(PARSER_CPP) $(LIB_H) + $(CXX) $(LIB_CFLAGS) -c -o $@ $< + +$(SRCPARSER)/bison_parser.cpp: $(SRCPARSER)/bison_parser.y + $(GMAKE) -C $(SRCPARSER)/ bison_parser.cpp + +$(SRCPARSER)/flex_lexer.cpp: $(SRCPARSER)/flex_lexer.l + $(GMAKE) -C $(SRCPARSER)/ flex_lexer.cpp + +$(SRCPARSER)/bison_parser.h: $(SRCPARSER)/bison_parser.cpp +$(SRCPARSER)/flex_lexer.h: $(SRCPARSER)/flex_lexer.cpp + +clean: + rm -f lib$(NAME).a lib$(NAME).so + rm -rf $(BIN) + find $(SRC) -type f -name '*.o' -delete + +cleanparser: + $(GMAKE) -C $(SRCPARSER)/ clean + +cleanall: clean cleanparser + +install: + cp $(LIB_BUILD) $(INSTALL)/lib/$(LIB_BUILD) + rm -rf $(INSTALL)/include/hsql + cp -r src $(INSTALL)/include/hsql + find $(INSTALL)/include/hsql -not -name '*.h' -type f | xargs rm + + + +####################################### +############## Benchmark ############## +####################################### +BM_BUILD = $(BIN)/benchmark +BM_CFLAGS = -std=c++17 -Wall -Isrc/ -L./ $(OPT_FLAG) +BM_PATH = benchmark +BM_CPP = $(shell find $(BM_PATH)/ -name '*.cpp') +BM_ALL = $(shell find $(BM_PATH)/ -name '*.cpp' -or -name '*.h') + +benchmark: $(BM_BUILD) + +run_benchmarks: benchmark + ./$(BM_BUILD) --benchmark_counters_tabular=true + # --benchmark_filter="abc + +save_benchmarks: benchmark + ./$(BM_BUILD) --benchmark_format=csv > benchmarks.csv + +$(BM_BUILD): $(BM_ALL) $(LIB_BUILD) + @mkdir -p $(BIN)/ + $(CXX) $(BM_CFLAGS) $(BM_CPP) -o $(BM_BUILD) -lbenchmark -lpthread -lsqlparser -lstdc++ -lstdc++fs + + + +######################################## +############ Test & Example ############ +######################################## +TEST_BUILD = $(BIN)/tests +TEST_CFLAGS = -std=c++1z -Wall -Werror -Isrc/ -Itest/ -L./ $(OPT_FLAG) +TEST_CPP = $(shell find test/ -name '*.cpp') +TEST_ALL = $(shell find test/ -name '*.cpp') $(shell find test/ -name '*.h') +EXAMPLE_SRC = $(shell find example/ -name '*.cpp') $(shell find example/ -name '*.h') + +test: $(TEST_BUILD) + bash test/test.sh + +$(TEST_BUILD): $(TEST_ALL) $(LIB_BUILD) + @mkdir -p $(BIN)/ + $(CXX) $(TEST_CFLAGS) $(TEST_CPP) -o $(TEST_BUILD) -lsqlparser -lstdc++ + +test_example: + $(GMAKE) -C example/ + LD_LIBRARY_PATH=./ \ + ./example/example "SELECT * FROM students WHERE name = 'Max Mustermann';" + +test_format: + @! astyle --options=astyle.options $(LIB_ALL) | grep -q "Formatted" + @! astyle --options=astyle.options $(TEST_ALL) | grep -q "Formatted" + + + +######################################## +################# Misc ################# +######################################## + +format: + astyle --options=astyle.options $(LIB_ALL) + astyle --options=astyle.options $(TEST_ALL) + astyle --options=astyle.options $(EXAMPLE_SRC) + +log_mode: + @echo $(MODE_LOG) + diff --git a/cpp/3rd_party/sql-parser/README.md b/cpp/3rd_party/sql-parser/README.md new file mode 100644 index 0000000000..52765af77b --- /dev/null +++ b/cpp/3rd_party/sql-parser/README.md @@ -0,0 +1,75 @@ +C++ SQL Parser +========================= +[![Build Status](https://app.travis-ci.com/hyrise/sql-parser.svg?branch=master)](https://app.travis-ci.com/github/hyrise/sql-parser) + + +This is a SQL Parser for C++. It parses the given SQL query into C++ objects. +It has been developed for integration in [Hyrise](https://github.com/hyrise/hyrise), but can be used perfectly well in other environments as well. + +In March 2015 we've also written a short paper outlining discussing some development details and the integration into our database Hyrise. You can find the paper [here](docs/technical_documentation.pdf). + + +## Usage + +**Note:** You can also find a detailed usage description [here](docs/basic-usage.md). + +**Requirements:** + * gcc 5+ (or clang 5+) + +To use the SQL parser in your own projects you simply have to follow these few steps. The only requirement for is gcc 4.8+. Older versions of gcc/clang might also work, but are untested. + + 1. Download the [latest release here](https://github.com/hyrise/sql-parser/releases) + 2. Compile the library `make` to create `libsqlparser.so` + 3. *(Optional, Recommended)* Run `make install` to copy the library to `/usr/local/lib/` + 4. Run the tests `make test` to make sure everything worked + 5. Include the `SQLParser.h` from `src/` (or from `/usr/local/lib/hsql/` if you installed it) and link the library in your project + 6. Take a look at the [example project here](https://github.com/hyrise/sql-parser/tree/master/example) + +```cpp +#include "hsql/SQLParser.h" + +/* ... */ + +{ + // Basic Usage Example + + const std::string query = "..."; + hsql::SQLParserResult result; + hsql::SQLParser::parse(query, &result); + + if (result.isValid() && result.size() > 0) { + const hsql::SQLStatement* statement = result.getStatement(0); + + if (statement->isType(hsql::kStmtSelect)) { + const auto* select = static_cast(statement); + /* ... */ + } + } +} +``` + +Quick Links: + + * [SQLParser.h](src/SQLParser.h) + * [SQLParserResult.h](src/SQLParserResult.h) + * [SelectStatement.h](src/sql/SelectStatement.h) + +## How to Contribute + +**[Developer Documentation](docs/)** + +We strongly encourage you to contribute to this project! If you want to contribute to this project there are several options. If you've noticed a bug or would like an improvement let us know by creating a [new issue](https://github.com/hyrise/sql-parser/issues). If you want to develop a new feature yourself or just improve the quality of the system, feel free to fork the reposistory and implement your changes. Open a pull request as soon as your done and we will look over it. If we think it's good then your pull request will be merged into this repository. + + +## License + +HYRISE sql-parser is licensed as open source after the MIT License which is declared in the LICENSE file of this project. + + +## Contributors + +The following people contributed to HYRISE sql-parser in various forms. + +* Pedro Flemming ([@torpedro](https://github.com/torpedro)) +* David Schwalb ([@schwald](https://github.com/schwald)) +* Markus Dreseler ([@mrks](https://github.com/mrks)) diff --git a/cpp/3rd_party/sql-parser/benchmark/README.md b/cpp/3rd_party/sql-parser/benchmark/README.md new file mode 100644 index 0000000000..6c038baa0c --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/README.md @@ -0,0 +1,14 @@ +# Benchmark + +This directory contains the scripts to execute benchmarks of the parser. We use [Google Benchmark](https://github.com/google/benchmark) to define and run benchmarks. + +## Install Google Benchmark + +```bash +cmake -DCMAKE_BUILD_TYPE=Release + +make + +make install +``` + diff --git a/cpp/3rd_party/sql-parser/benchmark/benchmark.cpp b/cpp/3rd_party/sql-parser/benchmark/benchmark.cpp new file mode 100644 index 0000000000..e1435504b8 --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/benchmark.cpp @@ -0,0 +1,28 @@ +#include "benchmark/benchmark.h" + +#include "benchmark_utils.h" +#include "queries.h" + +int main(int argc, char** argv) { + // Create parse and tokenize benchmarks for TPC-H queries. + const auto tpch_queries = getTPCHQueries(); + for (const auto& query : tpch_queries) { + std::string p_name = query.first + "-parse"; + benchmark::RegisterBenchmark(p_name.c_str(), &BM_ParseBenchmark, query.second); + std::string t_name = query.first + "-tokenize"; + benchmark::RegisterBenchmark(t_name.c_str(), &BM_TokenizeBenchmark, query.second); + } + + // Create parse and tokenize benchmarks for all queries in sql_queries array. + for (unsigned i = 0; i < sql_queries.size(); ++i) { + const auto& query = sql_queries[i]; + std::string p_name = getQueryName(i) + "-parse"; + benchmark::RegisterBenchmark(p_name.c_str(), &BM_ParseBenchmark, query.second); + + std::string t_name = getQueryName(i) + "-tokenize"; + benchmark::RegisterBenchmark(t_name.c_str(), &BM_TokenizeBenchmark, query.second); + } + + benchmark::Initialize(&argc, argv); + benchmark::RunSpecifiedBenchmarks(); +} diff --git a/cpp/3rd_party/sql-parser/benchmark/benchmark_utils.cpp b/cpp/3rd_party/sql-parser/benchmark/benchmark_utils.cpp new file mode 100644 index 0000000000..27fb66c4ee --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/benchmark_utils.cpp @@ -0,0 +1,44 @@ +#include "benchmark_utils.h" + +#include +#include + +#include "SQLParser.h" + +size_t getNumTokens(const std::string& query) { + std::vector tokens; + hsql::SQLParser::tokenize(query, &tokens); + return tokens.size(); +} + +void BM_TokenizeBenchmark(benchmark::State& st, const std::string& query) { + st.counters["num_tokens"] = getNumTokens(query); + st.counters["num_chars"] = query.size(); + + while (st.KeepRunning()) { + std::vector tokens(512); + hsql::SQLParser::tokenize(query, &tokens); + } +} + +void BM_ParseBenchmark(benchmark::State& st, const std::string& query) { + st.counters["num_tokens"] = getNumTokens(query); + st.counters["num_chars"] = query.size(); + + while (st.KeepRunning()) { + hsql::SQLParserResult result; + hsql::SQLParser::parse(query, &result); + if (!result.isValid()) { + std::cout << query << std::endl; + std::cout << result.errorMsg() << std::endl; + st.SkipWithError("Parsing failed!"); + } + } +} + +std::string readFileContents(const std::string& file_path) { + std::ifstream t(file_path.c_str()); + std::string text((std::istreambuf_iterator(t)), + std::istreambuf_iterator()); + return text; +} diff --git a/cpp/3rd_party/sql-parser/benchmark/benchmark_utils.h b/cpp/3rd_party/sql-parser/benchmark/benchmark_utils.h new file mode 100644 index 0000000000..7eb54d8090 --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/benchmark_utils.h @@ -0,0 +1,41 @@ +#ifndef __BENCHMARK_UTILS_H__ +#define __BENCHMARK_UTILS_H__ + +#include "benchmark/benchmark.h" + +size_t getNumTokens(const std::string& query); + +void BM_TokenizeBenchmark(benchmark::State& st, const std::string& query); + +void BM_ParseBenchmark(benchmark::State& st, const std::string& query); + +std::string readFileContents(const std::string& file_path); + + + + +#define TIME_DIFF(end, start)\ + std::chrono::duration_cast>(end - start); + +#define NOW()\ + std::chrono::high_resolution_clock::now(); + +#define PARSE_QUERY_BENCHMARK(name, query)\ + static void name(benchmark::State& st) {\ + BM_ParseBenchmark(st, query);\ + }\ + BENCHMARK(name); + +#define TOKENIZE_QUERY_BENCHMARK(name, query)\ + static void name(benchmark::State& st) {\ + BM_TokenizeBenchmark(st, query);\ + }\ + BENCHMARK(name); + + +#define BENCHMARK_QUERY(test_name, query)\ + TOKENIZE_QUERY_BENCHMARK(test_name##Tokenize, query)\ + PARSE_QUERY_BENCHMARK(test_name##Parse, query) + + +#endif \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/benchmark/parser_benchmark.cpp b/cpp/3rd_party/sql-parser/benchmark/parser_benchmark.cpp new file mode 100644 index 0000000000..47928f0a7b --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/parser_benchmark.cpp @@ -0,0 +1,87 @@ + +#include +#include +#include "benchmark/benchmark.h" + +#include "SQLParser.h" +#include "parser/bison_parser.h" +#include "parser/flex_lexer.h" + +#include "benchmark_utils.h" + +// Benchmark the influence of increasing size of the query, while +// the number of tokens remains unchanged. +static void BM_CharacterCount(benchmark::State& st) { + const size_t querySize = st.range(0); + + // Base query has size of 18 characters. + std::string query = "SELECT %name% FROM test;"; + + const uint pad = querySize - 18; + const std::string filler = std::string(pad, 'a'); + query.replace(7, 6, filler); + + st.counters["num_tokens"] = getNumTokens(query); + st.counters["num_chars"] = query.size(); + while (st.KeepRunning()) { + hsql::SQLParserResult result; + hsql::SQLParser::parse(query, &result); + } +} +BENCHMARK(BM_CharacterCount) + ->RangeMultiplier(1 << 2) + ->Ranges({{1 << 5, 1 << 15}, + {5, 5}}); + +// Benchmark the influence of increasing number of tokens, while +// the number of characters remains unchanged. +static void BM_ConditionalTokens(benchmark::State& st) { + const size_t targetSize = st.range(0); + const size_t numTokens = st.range(1); + + // Base query contains 6 tokens. + std::string query = "SELECT * FROM test"; + + // Create conditional. + std::stringstream condStream; + size_t missingTokens = numTokens - 4; + if (missingTokens > 0) { + condStream << " WHERE a"; + missingTokens -= 2; + + while (missingTokens > 0) { + condStream << " AND a"; + missingTokens -= 2; + } + } + + query += condStream.str(); + + if (targetSize >= query.size()) { + const size_t pad = targetSize - query.size(); + const std::string filler = std::string(pad, 'a'); + query.replace(7, 1, filler); + + } else { + // Query can't be the same length as in the other benchmarks. + // Running this will result in unusable data. + fprintf(stderr, "Too many tokens. Query too long for benchmark char limit (%lu > %lu).\n", + query.size(), targetSize); + return; + } + + st.counters["num_tokens"] = getNumTokens(query); + st.counters["num_chars"] = query.size(); + while (st.KeepRunning()) { + hsql::SQLParserResult result; + hsql::SQLParser::parse(query, &result); + if (!result.isValid()) st.SkipWithError("Parsing failed!"); + } +} +BENCHMARK(BM_ConditionalTokens) + ->RangeMultiplier(1 << 2) + ->Ranges({{1 << 14, 1 << 14}, + {1 << 2, 1 << 11}}); + + + diff --git a/cpp/3rd_party/sql-parser/benchmark/queries.cpp b/cpp/3rd_party/sql-parser/benchmark/queries.cpp new file mode 100644 index 0000000000..f26187d4d3 --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/queries.cpp @@ -0,0 +1,47 @@ +#include "queries.h" + +#include +#include +#include +#include + +#include "benchmark_utils.h" + +namespace filesystem = std::filesystem; + +std::string getQueryName(unsigned i) { + if (sql_queries[i].first.empty()) { + std::string name = "#" + std::to_string(i + 1); + return name; + } + return std::string("") + sql_queries[i].first; +} + +std::vector getQueriesFromDirectory(const std::string& dir_path) { + std::regex query_file_regex("\\.sql$"); + std::vector files; + + for (auto& entry : filesystem::directory_iterator(dir_path)) { + if (filesystem::is_regular_file(entry)) { + std::string path_str = filesystem::path(entry); + + if (std::regex_search(path_str, query_file_regex)) { + files.push_back(path_str); + } + } + } + + std::sort(files.begin(), files.end()); + + std::vector queries; + for (const std::string& file_path : files) { + const filesystem::path p(file_path); + const std::string query = readFileContents(file_path); + queries.emplace_back(p.filename(), query); + } + return queries; +} + +std::vector getTPCHQueries() { + return getQueriesFromDirectory("test/queries/"); +} diff --git a/cpp/3rd_party/sql-parser/benchmark/queries.h b/cpp/3rd_party/sql-parser/benchmark/queries.h new file mode 100644 index 0000000000..357bee61fc --- /dev/null +++ b/cpp/3rd_party/sql-parser/benchmark/queries.h @@ -0,0 +1,56 @@ +#ifndef __QUERIES_H__ +#define __QUERIES_H__ + +#include +#include + +typedef std::pair SQLQuery; + +// name, query +static std::vector sql_queries = { + {"Q1", "SELECT * FROM test;"}, + {"Q2", "SELECT a, b AS address FROM (SELECT * FROM test WHERE c < 100 AND b > 3) t1 WHERE a < 10 AND b < 100;"}, + {"Q3", "SELECT \"left\".a, \"left\".b, \"right\".a, \"right\".b FROM table_a AS \"left\" JOIN table_b AS \"right\" ON \"left\".a = \"right\".a;"}, + {"Q4", "" +"SELECT" +" l_orderkey," +" SUM(l_extendedprice * (1 - l_discount)) AS revenue," +" o_orderdate," +" o_shippriority" +" FROM" +" customer," +" orders," +" lineitem" +" WHERE" +" c_mktsegment = '%s'" +" and c_custkey = o_custkey" +" and l_orderkey = o_orderkey" +" and o_orderdate < '%s'" +" and l_shipdate > '%s'" +" GROUP BY" +" l_orderkey," +" o_orderdate," +" o_shippriority" +" ORDER BY" +" revenue DESC," +" o_orderdate;" +}, + + {"LongSelectList26", "SELECT a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z FROM test;"}, + {"LongSelectElement26", "SELECT abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy FROM test;"}, + {"LongSelectList52", "SELECT a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z FROM test;"}, + {"LongSelectElement52", "SELECT abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy FROM test;"}, + {"TwoSelects", "SELECT * FROM test; SELECT age, street AS address FROM data;"}, + {"ExecuteNoParams", "EXECUTE procedure;"}, + {"Execute2Params", "EXECUTE procedure(11, 'test');"}, + {"Execute10Params", "EXECUTE procedure(11, 'test', 5.6, 4.2, 'abc', 6, 7, 8, 9, 10000);"}, + // {"name", "query"}, +}; + +std::string getQueryName(unsigned i); + +std::vector getQueriesFromDirectory(const std::string& dir_path); + +std::vector getTPCHQueries(); + +#endif diff --git a/cpp/3rd_party/sql-parser/docs/.gitignore b/cpp/3rd_party/sql-parser/docs/.gitignore new file mode 100644 index 0000000000..a3925495ee --- /dev/null +++ b/cpp/3rd_party/sql-parser/docs/.gitignore @@ -0,0 +1 @@ +__doxygen__/ \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/docs/README.md b/cpp/3rd_party/sql-parser/docs/README.md new file mode 100644 index 0000000000..81d1e117c9 --- /dev/null +++ b/cpp/3rd_party/sql-parser/docs/README.md @@ -0,0 +1,14 @@ +Documentation +============= + +Internal Links: + + * [Developer Documentation](dev-docs.md) + * [Supported SQL Queries](syntax-support.md) + * [Known Limitations & Missing Features](known-limitations.md) + * [Basic Usage](basic-usage.md) + + +External Resources: + + * [Original Dev-Paper (2015)](http://torpedro.com/paper/HyriseSQL-03-2015.pdf) diff --git a/cpp/3rd_party/sql-parser/docs/basic-usage.md b/cpp/3rd_party/sql-parser/docs/basic-usage.md new file mode 100755 index 0000000000..350ff59b22 --- /dev/null +++ b/cpp/3rd_party/sql-parser/docs/basic-usage.md @@ -0,0 +1,70 @@ +Using the Library +======================= + +Using the SQL parser library is very simple. First step will be to download and build the library. Either get the latest sources from the repository or download the latest release. The only requirement is a modern C++ compiler. Versions that are definitely working are gcc 4.8 and clang 3.4, but older versions might work also or only need small modifications. To build it simply go into the directory and run + +``` +make # creates libsqlparser.so +make install # copies the library to /usr/local/lib/ +``` +To include it in your own code you only need to include one header file: SQLParser.h. The entire framework is wrapped in the namespace hsql. To parse a SQL string you have to call the static method `hsql::SQLParser::parseSQLString(std::string query)`. + +The `parseSQLString` method will return an object of type `SQLParserResult*`. When the query was valid SQL the result will contain a list of `SQLStatement` objects that represent the statements in your query. To check whether the query was valid, you can check the `result->isValid` flag. The successfully parsed statements are stored at `result->statements` which is of type `std::vector`. + +This is a list of the currently available statement types, each being a subclass of `SQLStatement`: + +``` +CreateStatement +DeleteStatement +DropStatement +ExecuteStatement +ImportStatement +PrepareStatement +SelectStatement +UpdateStatement +``` + +To find out what type of statement a certain `SQLStatement` is, you can check the `stmt->type()`, which will return an enum value. This `enum StatementType` is defined in `SQLStatement.h`. There you can see all the available values. Some of these do not match to statement classes though, because they are not implemented yet. + +Probably the best way to get familiar with the properties is to look at the class definitions itself in the repository here. The statement definitions are simply structs holding the data from the query. You could also take a look at the utility code in `sqlhelper.cpp` which contains code that prints information about statements to the console. + +## Example Code + +example.cpp + +``` +// include the sql parser +#include "SQLParser.h" + +int main(int argc, char *argv[]) { + if (argc <= 1) { + fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n"); + return -1; + } + std::string query = argv[1]; + + // parse a given query + hsql::SQLParserResult* result = hsql::SQLParser::parseSQLString(query); + + // check whether the parsing was successful + if (result->isValid) { + printf("Parsed successfully!\n"); + printf("Number of statements: %lu\n", result->size()); + // process the statements... + } else { + printf("The SQL string is invalid!\n"); + return -1; + } + + return 0; +} +``` + +Makefile + +``` +CFLAGS = -std=c++11 -lstdc++ -Wall -I../src/ -L../ + +all: + $(CXX) $(CFLAGS) example.cpp -o example -lsqlparser +``` diff --git a/cpp/3rd_party/sql-parser/docs/dev-docs.md b/cpp/3rd_party/sql-parser/docs/dev-docs.md new file mode 100644 index 0000000000..00050dd065 --- /dev/null +++ b/cpp/3rd_party/sql-parser/docs/dev-docs.md @@ -0,0 +1,63 @@ +Developer Documentation +======================= + +## Basic Requirements + +**Requirements for development:** + * gcc 4.8+ (or clang 3.4+) + * [bison](https://www.gnu.org/software/bison/) (v3.0.2+) + * [flex](http://flex.sourceforge.net/) (v2.5.5+) + +First step to extending this parser is cloning the repository `git clone git@github.com:hyrise/sql-parser.git` and making sure everything works by running the following steps: + +```bash +make parser # builds the bison parser and flex lexer +make library # builds the libsqlparser.so +make test # runs the tests with the library +``` + +Rerun these steps whenever you change part of the parse. To execute the entire pipeline automatically you can run: + +```bash +make cleanall # cleans the parser build and library build +make test # build parser, library and runs the tests +``` + + +## Developing New Functionality + +This section contains information about how to extend this parser with new functionalities. + + +### Implementing a new Statement + +Create a new file and class in `src/sql/` or extend any of the existing Statements. Every statement needs to have the base class SQLStatement and needs to call its super constructor with its type. If you're defining a new statement type, you need to define a new StatementType in `SQLStatement.h`. + +It is important that you create an appropriate constructor for your statement that zero-initializes all its pointer variables and that you create an appropriate destructor. + +Finally you will need to include your new file in `src/sql/statements.h`. + + +### Extending the Grammar + +Related files: +``` +src/parser/bison_parser.y +src/parser/flex_lexer.l +src/parser/keywordlist_generator.py +src/parser/sql_keywords.txt +``` + +To extend the grammar the file you will mostly have to deal with is the bison grammar definition in `src/parser/bison_parser.y`. + +If you're extending an existing statement, skip to the non-terminal definition for that statement. I.e. for an InsertStatement the non-terminal insert_statement. + +If you're defining a new statement, you will need to define your type in the \%union directive `hsql::ExampleStatement example_stmt`. Next you need to associate this type with a non-terminal `\%type example_statement`. Then you have to define the non-terminal `example_statement`. Look the other non-terminals for statements to figure out how. + + + +## Implementing Tests + +All test related files are in `test/`. Take a look to see how tests are implemented. + + diff --git a/cpp/3rd_party/sql-parser/docs/known-limitations.md b/cpp/3rd_party/sql-parser/docs/known-limitations.md new file mode 100644 index 0000000000..0c91b044ab --- /dev/null +++ b/cpp/3rd_party/sql-parser/docs/known-limitations.md @@ -0,0 +1,18 @@ +Known Limitations & Missing Features +==================================== + +This page contains an overview of known missing limitations and missing features in our SQL parser project. In general, we would like to see all of these features being supported at some point. If you are particularly interested in a specific feature, feel free to contribute to this project through a pull request. + +### Completely Missing Statement Types + + * EXPLAIN + * EXPORT + * RENAME + * ALTER + +Additionally, there are a lot of statement types that are specific to certain database systems. Supporting all of these is not on our roadmap, but if someone implements support for such a statement, we can also integrate it. + +### Other SQL Limitations + + * Tables names ignore the schema name (see grammar rule `table_name`). This affects, for example, `INSERT, IMPORT, DROP, DELETE`. + * Column data types only support `INT, DOUBLE, TEXT`. diff --git a/cpp/3rd_party/sql-parser/docs/syntax-support.md b/cpp/3rd_party/sql-parser/docs/syntax-support.md new file mode 100644 index 0000000000..59d08b9404 --- /dev/null +++ b/cpp/3rd_party/sql-parser/docs/syntax-support.md @@ -0,0 +1,53 @@ +Supported SQL Queries +===================== + +This page contains a short list of queries that can be correctly parsed with our parser. If you are interested in finding out if a certain feature is supported, it is probably the easiest to checkout the repository and try the example project or check our [list of known limitations](known-limitations.md). Also the file [queries-good.sql](../test/queries/queries-good.sql) shows a list of queries which are parsable with the current version. + + +## Select Statements + +We implement a broad support for the most common elements for `SELECT` statements. Following are a few examples of basic constructs that are supported. + +```sql +SELECT name, city, * + FROM students AS t1 JOIN students AS t2 ON t1.city = t2.city + WHERE t1.grade < 2.0 AND + t2.grade > 2.0 AND + t1.city = 'Frohnau' + ORDER BY t1.grade DESC; + +SELECT city, AVG(grade) AS average, + MIN(grade) AS best, MAX(grade) AS worst + FROM students + GROUP BY city; +``` + +## Data Definition & Modification + +**Create Tables** +```sql +CREATE TABLE students ( + name TEXT, + student_number INTEGER, + city TEXT, + grade DOUBLE +); +``` + +**Update and Delete** +```sql +UPDATE students SET name='Max Mustermann' WHERE name = 'Ralf Mustermann'; + +DELETE FROM students WHERE name = 'Max Mustermann'; +``` + + +## Prepared Statements + +The definition and execution of prepared statements is supported using the following syntax. + +```sql +PREPARE select_test FROM 'SELECT * FROM customer WHERE c_name = ?;'; + +EXECUTE select_test('Max Mustermann'); +``` diff --git a/cpp/3rd_party/sql-parser/docs/technical_documentation.pdf b/cpp/3rd_party/sql-parser/docs/technical_documentation.pdf new file mode 100644 index 0000000000..1a3cf62263 Binary files /dev/null and b/cpp/3rd_party/sql-parser/docs/technical_documentation.pdf differ diff --git a/cpp/3rd_party/sql-parser/example/.gitignore b/cpp/3rd_party/sql-parser/example/.gitignore new file mode 100644 index 0000000000..96236f8158 --- /dev/null +++ b/cpp/3rd_party/sql-parser/example/.gitignore @@ -0,0 +1 @@ +example \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/example/Makefile b/cpp/3rd_party/sql-parser/example/Makefile new file mode 100644 index 0000000000..387b553efb --- /dev/null +++ b/cpp/3rd_party/sql-parser/example/Makefile @@ -0,0 +1,6 @@ + +CFLAGS = -std=c++1z -lstdc++ -Wall -Werror -I../src/ -L../ + +all: + $(CXX) $(CFLAGS) example.cpp -o example -lsqlparser + diff --git a/cpp/3rd_party/sql-parser/example/example.cpp b/cpp/3rd_party/sql-parser/example/example.cpp new file mode 100644 index 0000000000..943de4b259 --- /dev/null +++ b/cpp/3rd_party/sql-parser/example/example.cpp @@ -0,0 +1,41 @@ + +#include +#include + +// include the sql parser +#include "SQLParser.h" + +// contains printing utilities +#include "util/sqlhelper.h" + +int main(int argc, char* argv[]) { + if (argc <= 1) { + fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n"); + return -1; + } + std::string query = argv[1]; + + // parse a given query + hsql::SQLParserResult result; + hsql::SQLParser::parse(query, &result); + + // check whether the parsing was successful + + if (result.isValid()) { + printf("Parsed successfully!\n"); + printf("Number of statements: %lu\n", result.size()); + + for (auto i = 0u; i < result.size(); ++i) { + // Print a statement summary. + hsql::printStatementInfo(result.getStatement(i)); + } + return 0; + } else { + fprintf(stderr, "Given string is not a valid SQL query.\n"); + fprintf(stderr, "%s (L%d:%d)\n", + result.errorMsg(), + result.errorLine(), + result.errorColumn()); + return -1; + } +} diff --git a/cpp/3rd_party/sql-parser/format.sh b/cpp/3rd_party/sql-parser/format.sh new file mode 100755 index 0000000000..5786dc2aaa --- /dev/null +++ b/cpp/3rd_party/sql-parser/format.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +unamestr=$(uname) +if [[ "$unamestr" == 'Darwin' ]]; then + clang_format="/usr/local/opt/llvm/bin/clang-format" + format_cmd="$clang_format -i -style=file '{}'" +elif [[ "$unamestr" == 'Linux' ]]; then + format_cmd="clang-format -i -style=file '{}'" +fi + +source_regex="^(src|test).*\.(cpp|h|y)" + +if [ "${1}" = "all" ]; then + find src test | grep -E "$source_regex" | xargs -I{} sh -c "${format_cmd}" +elif [ "$1" = "modified" ]; then + # Run on all changed as well as untracked cpp/hpp files, as compared to the current HEAD. Skip deleted files. + { git diff --diff-filter=d --name-only & git ls-files --others --exclude-standard; } | grep -E "$source_regex" | xargs -I{} sh -c "${format_cmd}" +elif [ "$1" = "staged" ]; then + # Run on all files that are staged to be committed. + git diff --diff-filter=d --cached --name-only | grep -E "$source_regex" | xargs -I{} sh -c "${format_cmd}" +else + # Run on all changed as well as untracked cpp/hpp files, as compared to the current master. Skip deleted files. + { git diff --diff-filter=d --name-only master & git ls-files --others --exclude-standard; } | grep -E "$source_regex" | xargs -I{} sh -c "${format_cmd}" +fi diff --git a/cpp/3rd_party/sql-parser/hyrise/benchmark/benchmark.py b/cpp/3rd_party/sql-parser/hyrise/benchmark/benchmark.py new file mode 100644 index 0000000000..3336e3e24c --- /dev/null +++ b/cpp/3rd_party/sql-parser/hyrise/benchmark/benchmark.py @@ -0,0 +1,169 @@ +#!/bin/python +from __future__ import print_function + +import urllib, urllib2 +import json + +class HyriseConnection(object): + def __init__(self, host, port): + super(HyriseConnection, self).__init__() + self.host = host + self.port = port + + def __hyriseurl(self): + return 'http://%s:%d/query' % (self.host, self.port) + + def __parseResponse(self, response): + result = json.loads(response) + if 'error' in result: + print('An error occurred: %s' % result['error'][0]) + return None + + if 'performanceData' in result: + pf_data = result['performanceData'] + total_time = 0 + parse_time = 0 + querytask_time = 0 + + for operator in pf_data: + time_ms = operator['endTime'] - operator['startTime'] + total_time += time_ms + if operator['name'] == 'RequestParseTask': + parse_time += time_ms + if operator['name'] == 'SQLQueryTask': + querytask_time += time_ms + + return { + 'total_ms': total_time, + 'parse_ms': parse_time, + 'querytask_ms': querytask_time, + 'preparation_ms': parse_time + querytask_time + } + + def __sendRequest(self, params): + url = self.__hyriseurl() + params['performance'] = 'true' + data = urllib.urlencode(params) + req = urllib2.Request(url, data) + try: + rsp = urllib2.urlopen(req) + response = rsp.read(); + return self.__parseResponse(response) + except TypeError as e: + print("An error occurred") + return None + except Exception as e: + return self.__parseResponse(e.read()) + + def __aggregatePerfArray(self, perfArray): + perf = perfArray[0] + for data in perfArray[1:]: + for key in data: + perf[key] += data[key] + + for key in perf: + perf[key] /= len(perfArray) + + return perf + + def executeSingleSQL(self, sql): + params = {'sql': sql} + return self.__sendRequest(params) + + def executeSingleJSON(self, jsonString): + params = {'query': jsonString} + return self.__sendRequest(params) + + def executeSQL(self, sql, times=1): + perf = [self.executeSingleSQL(sql) for _ in range(times)] + return self.__aggregatePerfArray(perf) + + def executeJSON(self, json, times=1): + perf = [self.executeSingleJSON(json) for _ in range(times)] + return self.__aggregatePerfArray(perf) + + + +queries = { + 'select-1': { + 'sql': "SELECT name, city FROM students WHERE grade <= 2.0", + 'json': """{"operators":{"0":{"type":"GetTable","name":"students"},"1":{"type":"SimpleTableScan","predicates":[{"type":"LTE_V","in":0,"f":"grade","value":2,"vtype":1}]},"2":{"type":"ProjectionScan","fields":["name","city"]}},"edges":[["0","1"],["1","2"]]}""", + + 'prepare': "PREPARE sel_test: SELECT name, city FROM students WHERE grade <= ?", + 'execute': "EXECUTE sel_test(2.0);" + + }, + + 'insert-1': { + 'sql': "INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3);", + 'json': """{"operators":{"0":{"type":"GetTable","name":"students"},"1":{"type":"InsertScan","data":[["Max",42,"Musterhausen",2.3]]},"commit":{"type":"Commit"}},"edges":[["0","1"],["1","commit"]]}""" + }, + + 'insert-2': { + 'sql': """ + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + """, + 'json': """{ + "operators": { + "0": { + "type": "GetTable", + "name": "students" + }, + "1": { + "type": "InsertScan", + "data": [ + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3], + ["Max", 42, "Musterhausen", 2.3] + ] + }, + "commit" : { + "type" : "Commit" + } + }, + "edges": [["0","1"],["1","commit"]] + }""", + 'prepare': """PREPARE batch_insert { + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + INSERT INTO students VALUES ('Max', 42, 'Musterhausen', 2.3); + }""", + 'execute': "EXECUTE batch_insert;" + } +} + +if __name__ == '__main__': + hyrise = HyriseConnection('localhost', 5000) + + # Load Table + hyrise.executeSQL("CREATE TABLE IF NOT EXISTS students FROM TBL FILE 'test/students.tbl';") + + query = queries['insert-2'] + + times = 50 + + + # if 'prepare' in query: hyrise.executeSQL(query['prepare']) + + if 'sql' in query: print('SQL: ', hyrise.executeSQL(query['sql'], times)) + + if 'execute' in query: print('Prepared: ', hyrise.executeSQL(query['execute'], times)) + + # if 'json' in query: print('JSON: ', hyrise.executeJSON(query['json'], times)) diff --git a/cpp/3rd_party/sql-parser/hyrise/deploy_to_hyrise.sh b/cpp/3rd_party/sql-parser/hyrise/deploy_to_hyrise.sh new file mode 100755 index 0000000000..ab5e592992 --- /dev/null +++ b/cpp/3rd_party/sql-parser/hyrise/deploy_to_hyrise.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Usage: deploy_to_hyrise.sh path/to/hyrise.git + + +BUILD_PATH=$(readlink -f $(dirname $0))/build + +HYRISE_PATH=$1 + +SQL_PATH=${HYRISE_PATH}/src/lib/access/sql + +if [ ! -d $SQL_PATH ]; then + echo "Could not verify Hyrise path! ${HYRISE_PATH}" + exit +fi + + +make -C src/ build + +rm ${SQL_PATH}/parser/* +cp ${BUILD_PATH}/* ${SQL_PATH}/parser/ diff --git a/cpp/3rd_party/sql-parser/hyrise/web-interface/bootstrap.min.css b/cpp/3rd_party/sql-parser/hyrise/web-interface/bootstrap.min.css new file mode 100644 index 0000000000..4af8905e5b --- /dev/null +++ b/cpp/3rd_party/sql-parser/hyrise/web-interface/bootstrap.min.css @@ -0,0 +1,5 @@ +/*! + * Bootstrap v3.3.0 (http://getbootstrap.com) + * Copyright 2011-2014 Twitter, Inc. + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + *//*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{margin:.67em 0;font-size:2em}mark{color:#000;background:#ff0}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{height:0;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{margin:0;font:inherit;color:inherit}button{overflow:visible}button,select{text-transform:none}button,html input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}input{line-height:normal}input[type=checkbox],input[type=radio]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:0}input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{height:auto}input[type=search]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}fieldset{padding:.35em .625em .75em;margin:0 2px;border:1px solid silver}legend{padding:0;border:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-spacing:0;border-collapse:collapse}td,th{padding:0}/*! Source: https://github.com/h5bp/html5-boilerplate/blob/master/src/css/main.css */@media print{*,:before,:after{color:#000!important;text-shadow:none!important;background:transparent!important;-webkit-box-shadow:none!important;box-shadow:none!important}a,a:visited{text-decoration:underline}a[href]:after{content:" (" attr(href) ")"}abbr[title]:after{content:" (" attr(title) ")"}a[href^="#"]:after,a[href^="javascript:"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100%!important}p,h2,h3{orphans:3;widows:3}h2,h3{page-break-after:avoid}select{background:#fff!important}.navbar{display:none}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table{border-collapse:collapse!important}.table td,.table th{background-color:#fff!important}.table-bordered th,.table-bordered td{border:1px solid #ddd!important}}@font-face{font-family:'Glyphicons Halflings';src:url(../fonts/glyphicons-halflings-regular.eot);src:url(../fonts/glyphicons-halflings-regular.eot?#iefix) format('embedded-opentype'),url(../fonts/glyphicons-halflings-regular.woff) format('woff'),url(../fonts/glyphicons-halflings-regular.ttf) format('truetype'),url(../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular) format('svg')}.glyphicon{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:400;line-height:1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.glyphicon-asterisk:before{content:"\2a"}.glyphicon-plus:before{content:"\2b"}.glyphicon-euro:before,.glyphicon-eur:before{content:"\20ac"}.glyphicon-minus:before{content:"\2212"}.glyphicon-cloud:before{content:"\2601"}.glyphicon-envelope:before{content:"\2709"}.glyphicon-pencil:before{content:"\270f"}.glyphicon-glass:before{content:"\e001"}.glyphicon-music:before{content:"\e002"}.glyphicon-search:before{content:"\e003"}.glyphicon-heart:before{content:"\e005"}.glyphicon-star:before{content:"\e006"}.glyphicon-star-empty:before{content:"\e007"}.glyphicon-user:before{content:"\e008"}.glyphicon-film:before{content:"\e009"}.glyphicon-th-large:before{content:"\e010"}.glyphicon-th:before{content:"\e011"}.glyphicon-th-list:before{content:"\e012"}.glyphicon-ok:before{content:"\e013"}.glyphicon-remove:before{content:"\e014"}.glyphicon-zoom-in:before{content:"\e015"}.glyphicon-zoom-out:before{content:"\e016"}.glyphicon-off:before{content:"\e017"}.glyphicon-signal:before{content:"\e018"}.glyphicon-cog:before{content:"\e019"}.glyphicon-trash:before{content:"\e020"}.glyphicon-home:before{content:"\e021"}.glyphicon-file:before{content:"\e022"}.glyphicon-time:before{content:"\e023"}.glyphicon-road:before{content:"\e024"}.glyphicon-download-alt:before{content:"\e025"}.glyphicon-download:before{content:"\e026"}.glyphicon-upload:before{content:"\e027"}.glyphicon-inbox:before{content:"\e028"}.glyphicon-play-circle:before{content:"\e029"}.glyphicon-repeat:before{content:"\e030"}.glyphicon-refresh:before{content:"\e031"}.glyphicon-list-alt:before{content:"\e032"}.glyphicon-lock:before{content:"\e033"}.glyphicon-flag:before{content:"\e034"}.glyphicon-headphones:before{content:"\e035"}.glyphicon-volume-off:before{content:"\e036"}.glyphicon-volume-down:before{content:"\e037"}.glyphicon-volume-up:before{content:"\e038"}.glyphicon-qrcode:before{content:"\e039"}.glyphicon-barcode:before{content:"\e040"}.glyphicon-tag:before{content:"\e041"}.glyphicon-tags:before{content:"\e042"}.glyphicon-book:before{content:"\e043"}.glyphicon-bookmark:before{content:"\e044"}.glyphicon-print:before{content:"\e045"}.glyphicon-camera:before{content:"\e046"}.glyphicon-font:before{content:"\e047"}.glyphicon-bold:before{content:"\e048"}.glyphicon-italic:before{content:"\e049"}.glyphicon-text-height:before{content:"\e050"}.glyphicon-text-width:before{content:"\e051"}.glyphicon-align-left:before{content:"\e052"}.glyphicon-align-center:before{content:"\e053"}.glyphicon-align-right:before{content:"\e054"}.glyphicon-align-justify:before{content:"\e055"}.glyphicon-list:before{content:"\e056"}.glyphicon-indent-left:before{content:"\e057"}.glyphicon-indent-right:before{content:"\e058"}.glyphicon-facetime-video:before{content:"\e059"}.glyphicon-picture:before{content:"\e060"}.glyphicon-map-marker:before{content:"\e062"}.glyphicon-adjust:before{content:"\e063"}.glyphicon-tint:before{content:"\e064"}.glyphicon-edit:before{content:"\e065"}.glyphicon-share:before{content:"\e066"}.glyphicon-check:before{content:"\e067"}.glyphicon-move:before{content:"\e068"}.glyphicon-step-backward:before{content:"\e069"}.glyphicon-fast-backward:before{content:"\e070"}.glyphicon-backward:before{content:"\e071"}.glyphicon-play:before{content:"\e072"}.glyphicon-pause:before{content:"\e073"}.glyphicon-stop:before{content:"\e074"}.glyphicon-forward:before{content:"\e075"}.glyphicon-fast-forward:before{content:"\e076"}.glyphicon-step-forward:before{content:"\e077"}.glyphicon-eject:before{content:"\e078"}.glyphicon-chevron-left:before{content:"\e079"}.glyphicon-chevron-right:before{content:"\e080"}.glyphicon-plus-sign:before{content:"\e081"}.glyphicon-minus-sign:before{content:"\e082"}.glyphicon-remove-sign:before{content:"\e083"}.glyphicon-ok-sign:before{content:"\e084"}.glyphicon-question-sign:before{content:"\e085"}.glyphicon-info-sign:before{content:"\e086"}.glyphicon-screenshot:before{content:"\e087"}.glyphicon-remove-circle:before{content:"\e088"}.glyphicon-ok-circle:before{content:"\e089"}.glyphicon-ban-circle:before{content:"\e090"}.glyphicon-arrow-left:before{content:"\e091"}.glyphicon-arrow-right:before{content:"\e092"}.glyphicon-arrow-up:before{content:"\e093"}.glyphicon-arrow-down:before{content:"\e094"}.glyphicon-share-alt:before{content:"\e095"}.glyphicon-resize-full:before{content:"\e096"}.glyphicon-resize-small:before{content:"\e097"}.glyphicon-exclamation-sign:before{content:"\e101"}.glyphicon-gift:before{content:"\e102"}.glyphicon-leaf:before{content:"\e103"}.glyphicon-fire:before{content:"\e104"}.glyphicon-eye-open:before{content:"\e105"}.glyphicon-eye-close:before{content:"\e106"}.glyphicon-warning-sign:before{content:"\e107"}.glyphicon-plane:before{content:"\e108"}.glyphicon-calendar:before{content:"\e109"}.glyphicon-random:before{content:"\e110"}.glyphicon-comment:before{content:"\e111"}.glyphicon-magnet:before{content:"\e112"}.glyphicon-chevron-up:before{content:"\e113"}.glyphicon-chevron-down:before{content:"\e114"}.glyphicon-retweet:before{content:"\e115"}.glyphicon-shopping-cart:before{content:"\e116"}.glyphicon-folder-close:before{content:"\e117"}.glyphicon-folder-open:before{content:"\e118"}.glyphicon-resize-vertical:before{content:"\e119"}.glyphicon-resize-horizontal:before{content:"\e120"}.glyphicon-hdd:before{content:"\e121"}.glyphicon-bullhorn:before{content:"\e122"}.glyphicon-bell:before{content:"\e123"}.glyphicon-certificate:before{content:"\e124"}.glyphicon-thumbs-up:before{content:"\e125"}.glyphicon-thumbs-down:before{content:"\e126"}.glyphicon-hand-right:before{content:"\e127"}.glyphicon-hand-left:before{content:"\e128"}.glyphicon-hand-up:before{content:"\e129"}.glyphicon-hand-down:before{content:"\e130"}.glyphicon-circle-arrow-right:before{content:"\e131"}.glyphicon-circle-arrow-left:before{content:"\e132"}.glyphicon-circle-arrow-up:before{content:"\e133"}.glyphicon-circle-arrow-down:before{content:"\e134"}.glyphicon-globe:before{content:"\e135"}.glyphicon-wrench:before{content:"\e136"}.glyphicon-tasks:before{content:"\e137"}.glyphicon-filter:before{content:"\e138"}.glyphicon-briefcase:before{content:"\e139"}.glyphicon-fullscreen:before{content:"\e140"}.glyphicon-dashboard:before{content:"\e141"}.glyphicon-paperclip:before{content:"\e142"}.glyphicon-heart-empty:before{content:"\e143"}.glyphicon-link:before{content:"\e144"}.glyphicon-phone:before{content:"\e145"}.glyphicon-pushpin:before{content:"\e146"}.glyphicon-usd:before{content:"\e148"}.glyphicon-gbp:before{content:"\e149"}.glyphicon-sort:before{content:"\e150"}.glyphicon-sort-by-alphabet:before{content:"\e151"}.glyphicon-sort-by-alphabet-alt:before{content:"\e152"}.glyphicon-sort-by-order:before{content:"\e153"}.glyphicon-sort-by-order-alt:before{content:"\e154"}.glyphicon-sort-by-attributes:before{content:"\e155"}.glyphicon-sort-by-attributes-alt:before{content:"\e156"}.glyphicon-unchecked:before{content:"\e157"}.glyphicon-expand:before{content:"\e158"}.glyphicon-collapse-down:before{content:"\e159"}.glyphicon-collapse-up:before{content:"\e160"}.glyphicon-log-in:before{content:"\e161"}.glyphicon-flash:before{content:"\e162"}.glyphicon-log-out:before{content:"\e163"}.glyphicon-new-window:before{content:"\e164"}.glyphicon-record:before{content:"\e165"}.glyphicon-save:before{content:"\e166"}.glyphicon-open:before{content:"\e167"}.glyphicon-saved:before{content:"\e168"}.glyphicon-import:before{content:"\e169"}.glyphicon-export:before{content:"\e170"}.glyphicon-send:before{content:"\e171"}.glyphicon-floppy-disk:before{content:"\e172"}.glyphicon-floppy-saved:before{content:"\e173"}.glyphicon-floppy-remove:before{content:"\e174"}.glyphicon-floppy-save:before{content:"\e175"}.glyphicon-floppy-open:before{content:"\e176"}.glyphicon-credit-card:before{content:"\e177"}.glyphicon-transfer:before{content:"\e178"}.glyphicon-cutlery:before{content:"\e179"}.glyphicon-header:before{content:"\e180"}.glyphicon-compressed:before{content:"\e181"}.glyphicon-earphone:before{content:"\e182"}.glyphicon-phone-alt:before{content:"\e183"}.glyphicon-tower:before{content:"\e184"}.glyphicon-stats:before{content:"\e185"}.glyphicon-sd-video:before{content:"\e186"}.glyphicon-hd-video:before{content:"\e187"}.glyphicon-subtitles:before{content:"\e188"}.glyphicon-sound-stereo:before{content:"\e189"}.glyphicon-sound-dolby:before{content:"\e190"}.glyphicon-sound-5-1:before{content:"\e191"}.glyphicon-sound-6-1:before{content:"\e192"}.glyphicon-sound-7-1:before{content:"\e193"}.glyphicon-copyright-mark:before{content:"\e194"}.glyphicon-registration-mark:before{content:"\e195"}.glyphicon-cloud-download:before{content:"\e197"}.glyphicon-cloud-upload:before{content:"\e198"}.glyphicon-tree-conifer:before{content:"\e199"}.glyphicon-tree-deciduous:before{content:"\e200"}*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}:before,:after{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}html{font-size:10px;-webkit-tap-highlight-color:rgba(0,0,0,0)}body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;line-height:1.42857143;color:#333;background-color:#fff}input,button,select,textarea{font-family:inherit;font-size:inherit;line-height:inherit}a{color:#428bca;text-decoration:none}a:hover,a:focus{color:#2a6496;text-decoration:underline}a:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}figure{margin:0}img{vertical-align:middle}.img-responsive,.thumbnail>img,.thumbnail a>img,.carousel-inner>.item>img,.carousel-inner>.item>a>img{display:block;max-width:100%;height:auto}.img-rounded{border-radius:6px}.img-thumbnail{display:inline-block;max-width:100%;height:auto;padding:4px;line-height:1.42857143;background-color:#fff;border:1px solid #ddd;border-radius:4px;-webkit-transition:all .2s ease-in-out;-o-transition:all .2s ease-in-out;transition:all .2s ease-in-out}.img-circle{border-radius:50%}hr{margin-top:20px;margin-bottom:20px;border:0;border-top:1px solid #eee}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}.sr-only-focusable:active,.sr-only-focusable:focus{position:static;width:auto;height:auto;margin:0;overflow:visible;clip:auto}h1,h2,h3,h4,h5,h6,.h1,.h2,.h3,.h4,.h5,.h6{font-family:inherit;font-weight:500;line-height:1.1;color:inherit}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small,.h1 small,.h2 small,.h3 small,.h4 small,.h5 small,.h6 small,h1 .small,h2 .small,h3 .small,h4 .small,h5 .small,h6 .small,.h1 .small,.h2 .small,.h3 .small,.h4 .small,.h5 .small,.h6 .small{font-weight:400;line-height:1;color:#777}h1,.h1,h2,.h2,h3,.h3{margin-top:20px;margin-bottom:10px}h1 small,.h1 small,h2 small,.h2 small,h3 small,.h3 small,h1 .small,.h1 .small,h2 .small,.h2 .small,h3 .small,.h3 .small{font-size:65%}h4,.h4,h5,.h5,h6,.h6{margin-top:10px;margin-bottom:10px}h4 small,.h4 small,h5 small,.h5 small,h6 small,.h6 small,h4 .small,.h4 .small,h5 .small,.h5 .small,h6 .small,.h6 .small{font-size:75%}h1,.h1{font-size:36px}h2,.h2{font-size:30px}h3,.h3{font-size:24px}h4,.h4{font-size:18px}h5,.h5{font-size:14px}h6,.h6{font-size:12px}p{margin:0 0 10px}.lead{margin-bottom:20px;font-size:16px;font-weight:300;line-height:1.4}@media (min-width:768px){.lead{font-size:21px}}small,.small{font-size:85%}mark,.mark{padding:.2em;background-color:#fcf8e3}.text-left{text-align:left}.text-right{text-align:right}.text-center{text-align:center}.text-justify{text-align:justify}.text-nowrap{white-space:nowrap}.text-lowercase{text-transform:lowercase}.text-uppercase{text-transform:uppercase}.text-capitalize{text-transform:capitalize}.text-muted{color:#777}.text-primary{color:#428bca}a.text-primary:hover{color:#3071a9}.text-success{color:#3c763d}a.text-success:hover{color:#2b542c}.text-info{color:#31708f}a.text-info:hover{color:#245269}.text-warning{color:#8a6d3b}a.text-warning:hover{color:#66512c}.text-danger{color:#a94442}a.text-danger:hover{color:#843534}.bg-primary{color:#fff;background-color:#428bca}a.bg-primary:hover{background-color:#3071a9}.bg-success{background-color:#dff0d8}a.bg-success:hover{background-color:#c1e2b3}.bg-info{background-color:#d9edf7}a.bg-info:hover{background-color:#afd9ee}.bg-warning{background-color:#fcf8e3}a.bg-warning:hover{background-color:#f7ecb5}.bg-danger{background-color:#f2dede}a.bg-danger:hover{background-color:#e4b9b9}.page-header{padding-bottom:9px;margin:40px 0 20px;border-bottom:1px solid #eee}ul,ol{margin-top:0;margin-bottom:10px}ul ul,ol ul,ul ol,ol ol{margin-bottom:0}.list-unstyled{padding-left:0;list-style:none}.list-inline{padding-left:0;margin-left:-5px;list-style:none}.list-inline>li{display:inline-block;padding-right:5px;padding-left:5px}dl{margin-top:0;margin-bottom:20px}dt,dd{line-height:1.42857143}dt{font-weight:700}dd{margin-left:0}@media (min-width:768px){.dl-horizontal dt{float:left;width:160px;overflow:hidden;clear:left;text-align:right;text-overflow:ellipsis;white-space:nowrap}.dl-horizontal dd{margin-left:180px}}abbr[title],abbr[data-original-title]{cursor:help;border-bottom:1px dotted #777}.initialism{font-size:90%;text-transform:uppercase}blockquote{padding:10px 20px;margin:0 0 20px;font-size:17.5px;border-left:5px solid #eee}blockquote p:last-child,blockquote ul:last-child,blockquote ol:last-child{margin-bottom:0}blockquote footer,blockquote small,blockquote .small{display:block;font-size:80%;line-height:1.42857143;color:#777}blockquote footer:before,blockquote small:before,blockquote .small:before{content:'\2014 \00A0'}.blockquote-reverse,blockquote.pull-right{padding-right:15px;padding-left:0;text-align:right;border-right:5px solid #eee;border-left:0}.blockquote-reverse footer:before,blockquote.pull-right footer:before,.blockquote-reverse small:before,blockquote.pull-right small:before,.blockquote-reverse .small:before,blockquote.pull-right .small:before{content:''}.blockquote-reverse footer:after,blockquote.pull-right footer:after,.blockquote-reverse small:after,blockquote.pull-right small:after,.blockquote-reverse .small:after,blockquote.pull-right .small:after{content:'\00A0 \2014'}address{margin-bottom:20px;font-style:normal;line-height:1.42857143}code,kbd,pre,samp{font-family:Menlo,Monaco,Consolas,"Courier New",monospace}code{padding:2px 4px;font-size:90%;color:#c7254e;background-color:#f9f2f4;border-radius:4px}kbd{padding:2px 4px;font-size:90%;color:#fff;background-color:#333;border-radius:3px;-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,.25);box-shadow:inset 0 -1px 0 rgba(0,0,0,.25)}kbd kbd{padding:0;font-size:100%;font-weight:700;-webkit-box-shadow:none;box-shadow:none}pre{display:block;padding:9.5px;margin:0 0 10px;font-size:13px;line-height:1.42857143;color:#333;word-break:break-all;word-wrap:break-word;background-color:#f5f5f5;border:1px solid #ccc;border-radius:4px}pre code{padding:0;font-size:inherit;color:inherit;white-space:pre-wrap;background-color:transparent;border-radius:0}.pre-scrollable{max-height:340px;overflow-y:scroll}.container{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}@media (min-width:768px){.container{width:750px}}@media (min-width:992px){.container{width:970px}}@media (min-width:1200px){.container{width:1170px}}.container-fluid{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}.row{margin-right:-15px;margin-left:-15px}.col-xs-1,.col-sm-1,.col-md-1,.col-lg-1,.col-xs-2,.col-sm-2,.col-md-2,.col-lg-2,.col-xs-3,.col-sm-3,.col-md-3,.col-lg-3,.col-xs-4,.col-sm-4,.col-md-4,.col-lg-4,.col-xs-5,.col-sm-5,.col-md-5,.col-lg-5,.col-xs-6,.col-sm-6,.col-md-6,.col-lg-6,.col-xs-7,.col-sm-7,.col-md-7,.col-lg-7,.col-xs-8,.col-sm-8,.col-md-8,.col-lg-8,.col-xs-9,.col-sm-9,.col-md-9,.col-lg-9,.col-xs-10,.col-sm-10,.col-md-10,.col-lg-10,.col-xs-11,.col-sm-11,.col-md-11,.col-lg-11,.col-xs-12,.col-sm-12,.col-md-12,.col-lg-12{position:relative;min-height:1px;padding-right:15px;padding-left:15px}.col-xs-1,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9,.col-xs-10,.col-xs-11,.col-xs-12{float:left}.col-xs-12{width:100%}.col-xs-11{width:91.66666667%}.col-xs-10{width:83.33333333%}.col-xs-9{width:75%}.col-xs-8{width:66.66666667%}.col-xs-7{width:58.33333333%}.col-xs-6{width:50%}.col-xs-5{width:41.66666667%}.col-xs-4{width:33.33333333%}.col-xs-3{width:25%}.col-xs-2{width:16.66666667%}.col-xs-1{width:8.33333333%}.col-xs-pull-12{right:100%}.col-xs-pull-11{right:91.66666667%}.col-xs-pull-10{right:83.33333333%}.col-xs-pull-9{right:75%}.col-xs-pull-8{right:66.66666667%}.col-xs-pull-7{right:58.33333333%}.col-xs-pull-6{right:50%}.col-xs-pull-5{right:41.66666667%}.col-xs-pull-4{right:33.33333333%}.col-xs-pull-3{right:25%}.col-xs-pull-2{right:16.66666667%}.col-xs-pull-1{right:8.33333333%}.col-xs-pull-0{right:auto}.col-xs-push-12{left:100%}.col-xs-push-11{left:91.66666667%}.col-xs-push-10{left:83.33333333%}.col-xs-push-9{left:75%}.col-xs-push-8{left:66.66666667%}.col-xs-push-7{left:58.33333333%}.col-xs-push-6{left:50%}.col-xs-push-5{left:41.66666667%}.col-xs-push-4{left:33.33333333%}.col-xs-push-3{left:25%}.col-xs-push-2{left:16.66666667%}.col-xs-push-1{left:8.33333333%}.col-xs-push-0{left:auto}.col-xs-offset-12{margin-left:100%}.col-xs-offset-11{margin-left:91.66666667%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-0{margin-left:0}@media (min-width:768px){.col-sm-1,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-sm-10,.col-sm-11,.col-sm-12{float:left}.col-sm-12{width:100%}.col-sm-11{width:91.66666667%}.col-sm-10{width:83.33333333%}.col-sm-9{width:75%}.col-sm-8{width:66.66666667%}.col-sm-7{width:58.33333333%}.col-sm-6{width:50%}.col-sm-5{width:41.66666667%}.col-sm-4{width:33.33333333%}.col-sm-3{width:25%}.col-sm-2{width:16.66666667%}.col-sm-1{width:8.33333333%}.col-sm-pull-12{right:100%}.col-sm-pull-11{right:91.66666667%}.col-sm-pull-10{right:83.33333333%}.col-sm-pull-9{right:75%}.col-sm-pull-8{right:66.66666667%}.col-sm-pull-7{right:58.33333333%}.col-sm-pull-6{right:50%}.col-sm-pull-5{right:41.66666667%}.col-sm-pull-4{right:33.33333333%}.col-sm-pull-3{right:25%}.col-sm-pull-2{right:16.66666667%}.col-sm-pull-1{right:8.33333333%}.col-sm-pull-0{right:auto}.col-sm-push-12{left:100%}.col-sm-push-11{left:91.66666667%}.col-sm-push-10{left:83.33333333%}.col-sm-push-9{left:75%}.col-sm-push-8{left:66.66666667%}.col-sm-push-7{left:58.33333333%}.col-sm-push-6{left:50%}.col-sm-push-5{left:41.66666667%}.col-sm-push-4{left:33.33333333%}.col-sm-push-3{left:25%}.col-sm-push-2{left:16.66666667%}.col-sm-push-1{left:8.33333333%}.col-sm-push-0{left:auto}.col-sm-offset-12{margin-left:100%}.col-sm-offset-11{margin-left:91.66666667%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-0{margin-left:0}}@media (min-width:992px){.col-md-1,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-md-10,.col-md-11,.col-md-12{float:left}.col-md-12{width:100%}.col-md-11{width:91.66666667%}.col-md-10{width:83.33333333%}.col-md-9{width:75%}.col-md-8{width:66.66666667%}.col-md-7{width:58.33333333%}.col-md-6{width:50%}.col-md-5{width:41.66666667%}.col-md-4{width:33.33333333%}.col-md-3{width:25%}.col-md-2{width:16.66666667%}.col-md-1{width:8.33333333%}.col-md-pull-12{right:100%}.col-md-pull-11{right:91.66666667%}.col-md-pull-10{right:83.33333333%}.col-md-pull-9{right:75%}.col-md-pull-8{right:66.66666667%}.col-md-pull-7{right:58.33333333%}.col-md-pull-6{right:50%}.col-md-pull-5{right:41.66666667%}.col-md-pull-4{right:33.33333333%}.col-md-pull-3{right:25%}.col-md-pull-2{right:16.66666667%}.col-md-pull-1{right:8.33333333%}.col-md-pull-0{right:auto}.col-md-push-12{left:100%}.col-md-push-11{left:91.66666667%}.col-md-push-10{left:83.33333333%}.col-md-push-9{left:75%}.col-md-push-8{left:66.66666667%}.col-md-push-7{left:58.33333333%}.col-md-push-6{left:50%}.col-md-push-5{left:41.66666667%}.col-md-push-4{left:33.33333333%}.col-md-push-3{left:25%}.col-md-push-2{left:16.66666667%}.col-md-push-1{left:8.33333333%}.col-md-push-0{left:auto}.col-md-offset-12{margin-left:100%}.col-md-offset-11{margin-left:91.66666667%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-9{margin-left:75%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-6{margin-left:50%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-3{margin-left:25%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-0{margin-left:0}}@media (min-width:1200px){.col-lg-1,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-lg-10,.col-lg-11,.col-lg-12{float:left}.col-lg-12{width:100%}.col-lg-11{width:91.66666667%}.col-lg-10{width:83.33333333%}.col-lg-9{width:75%}.col-lg-8{width:66.66666667%}.col-lg-7{width:58.33333333%}.col-lg-6{width:50%}.col-lg-5{width:41.66666667%}.col-lg-4{width:33.33333333%}.col-lg-3{width:25%}.col-lg-2{width:16.66666667%}.col-lg-1{width:8.33333333%}.col-lg-pull-12{right:100%}.col-lg-pull-11{right:91.66666667%}.col-lg-pull-10{right:83.33333333%}.col-lg-pull-9{right:75%}.col-lg-pull-8{right:66.66666667%}.col-lg-pull-7{right:58.33333333%}.col-lg-pull-6{right:50%}.col-lg-pull-5{right:41.66666667%}.col-lg-pull-4{right:33.33333333%}.col-lg-pull-3{right:25%}.col-lg-pull-2{right:16.66666667%}.col-lg-pull-1{right:8.33333333%}.col-lg-pull-0{right:auto}.col-lg-push-12{left:100%}.col-lg-push-11{left:91.66666667%}.col-lg-push-10{left:83.33333333%}.col-lg-push-9{left:75%}.col-lg-push-8{left:66.66666667%}.col-lg-push-7{left:58.33333333%}.col-lg-push-6{left:50%}.col-lg-push-5{left:41.66666667%}.col-lg-push-4{left:33.33333333%}.col-lg-push-3{left:25%}.col-lg-push-2{left:16.66666667%}.col-lg-push-1{left:8.33333333%}.col-lg-push-0{left:auto}.col-lg-offset-12{margin-left:100%}.col-lg-offset-11{margin-left:91.66666667%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-0{margin-left:0}}table{background-color:transparent}caption{padding-top:8px;padding-bottom:8px;color:#777;text-align:left}th{text-align:left}.table{width:100%;max-width:100%;margin-bottom:20px}.table>thead>tr>th,.table>tbody>tr>th,.table>tfoot>tr>th,.table>thead>tr>td,.table>tbody>tr>td,.table>tfoot>tr>td{padding:8px;line-height:1.42857143;vertical-align:top;border-top:1px solid #ddd}.table>thead>tr>th{vertical-align:bottom;border-bottom:2px solid #ddd}.table>caption+thead>tr:first-child>th,.table>colgroup+thead>tr:first-child>th,.table>thead:first-child>tr:first-child>th,.table>caption+thead>tr:first-child>td,.table>colgroup+thead>tr:first-child>td,.table>thead:first-child>tr:first-child>td{border-top:0}.table>tbody+tbody{border-top:2px solid #ddd}.table .table{background-color:#fff}.table-condensed>thead>tr>th,.table-condensed>tbody>tr>th,.table-condensed>tfoot>tr>th,.table-condensed>thead>tr>td,.table-condensed>tbody>tr>td,.table-condensed>tfoot>tr>td{padding:5px}.table-bordered{border:1px solid #ddd}.table-bordered>thead>tr>th,.table-bordered>tbody>tr>th,.table-bordered>tfoot>tr>th,.table-bordered>thead>tr>td,.table-bordered>tbody>tr>td,.table-bordered>tfoot>tr>td{border:1px solid #ddd}.table-bordered>thead>tr>th,.table-bordered>thead>tr>td{border-bottom-width:2px}.table-striped>tbody>tr:nth-child(odd){background-color:#f9f9f9}.table-hover>tbody>tr:hover{background-color:#f5f5f5}table col[class*=col-]{position:static;display:table-column;float:none}table td[class*=col-],table th[class*=col-]{position:static;display:table-cell;float:none}.table>thead>tr>td.active,.table>tbody>tr>td.active,.table>tfoot>tr>td.active,.table>thead>tr>th.active,.table>tbody>tr>th.active,.table>tfoot>tr>th.active,.table>thead>tr.active>td,.table>tbody>tr.active>td,.table>tfoot>tr.active>td,.table>thead>tr.active>th,.table>tbody>tr.active>th,.table>tfoot>tr.active>th{background-color:#f5f5f5}.table-hover>tbody>tr>td.active:hover,.table-hover>tbody>tr>th.active:hover,.table-hover>tbody>tr.active:hover>td,.table-hover>tbody>tr:hover>.active,.table-hover>tbody>tr.active:hover>th{background-color:#e8e8e8}.table>thead>tr>td.success,.table>tbody>tr>td.success,.table>tfoot>tr>td.success,.table>thead>tr>th.success,.table>tbody>tr>th.success,.table>tfoot>tr>th.success,.table>thead>tr.success>td,.table>tbody>tr.success>td,.table>tfoot>tr.success>td,.table>thead>tr.success>th,.table>tbody>tr.success>th,.table>tfoot>tr.success>th{background-color:#dff0d8}.table-hover>tbody>tr>td.success:hover,.table-hover>tbody>tr>th.success:hover,.table-hover>tbody>tr.success:hover>td,.table-hover>tbody>tr:hover>.success,.table-hover>tbody>tr.success:hover>th{background-color:#d0e9c6}.table>thead>tr>td.info,.table>tbody>tr>td.info,.table>tfoot>tr>td.info,.table>thead>tr>th.info,.table>tbody>tr>th.info,.table>tfoot>tr>th.info,.table>thead>tr.info>td,.table>tbody>tr.info>td,.table>tfoot>tr.info>td,.table>thead>tr.info>th,.table>tbody>tr.info>th,.table>tfoot>tr.info>th{background-color:#d9edf7}.table-hover>tbody>tr>td.info:hover,.table-hover>tbody>tr>th.info:hover,.table-hover>tbody>tr.info:hover>td,.table-hover>tbody>tr:hover>.info,.table-hover>tbody>tr.info:hover>th{background-color:#c4e3f3}.table>thead>tr>td.warning,.table>tbody>tr>td.warning,.table>tfoot>tr>td.warning,.table>thead>tr>th.warning,.table>tbody>tr>th.warning,.table>tfoot>tr>th.warning,.table>thead>tr.warning>td,.table>tbody>tr.warning>td,.table>tfoot>tr.warning>td,.table>thead>tr.warning>th,.table>tbody>tr.warning>th,.table>tfoot>tr.warning>th{background-color:#fcf8e3}.table-hover>tbody>tr>td.warning:hover,.table-hover>tbody>tr>th.warning:hover,.table-hover>tbody>tr.warning:hover>td,.table-hover>tbody>tr:hover>.warning,.table-hover>tbody>tr.warning:hover>th{background-color:#faf2cc}.table>thead>tr>td.danger,.table>tbody>tr>td.danger,.table>tfoot>tr>td.danger,.table>thead>tr>th.danger,.table>tbody>tr>th.danger,.table>tfoot>tr>th.danger,.table>thead>tr.danger>td,.table>tbody>tr.danger>td,.table>tfoot>tr.danger>td,.table>thead>tr.danger>th,.table>tbody>tr.danger>th,.table>tfoot>tr.danger>th{background-color:#f2dede}.table-hover>tbody>tr>td.danger:hover,.table-hover>tbody>tr>th.danger:hover,.table-hover>tbody>tr.danger:hover>td,.table-hover>tbody>tr:hover>.danger,.table-hover>tbody>tr.danger:hover>th{background-color:#ebcccc}.table-responsive{min-height:.01%;overflow-x:auto}@media screen and (max-width:767px){.table-responsive{width:100%;margin-bottom:15px;overflow-y:hidden;-ms-overflow-style:-ms-autohiding-scrollbar;border:1px solid #ddd}.table-responsive>.table{margin-bottom:0}.table-responsive>.table>thead>tr>th,.table-responsive>.table>tbody>tr>th,.table-responsive>.table>tfoot>tr>th,.table-responsive>.table>thead>tr>td,.table-responsive>.table>tbody>tr>td,.table-responsive>.table>tfoot>tr>td{white-space:nowrap}.table-responsive>.table-bordered{border:0}.table-responsive>.table-bordered>thead>tr>th:first-child,.table-responsive>.table-bordered>tbody>tr>th:first-child,.table-responsive>.table-bordered>tfoot>tr>th:first-child,.table-responsive>.table-bordered>thead>tr>td:first-child,.table-responsive>.table-bordered>tbody>tr>td:first-child,.table-responsive>.table-bordered>tfoot>tr>td:first-child{border-left:0}.table-responsive>.table-bordered>thead>tr>th:last-child,.table-responsive>.table-bordered>tbody>tr>th:last-child,.table-responsive>.table-bordered>tfoot>tr>th:last-child,.table-responsive>.table-bordered>thead>tr>td:last-child,.table-responsive>.table-bordered>tbody>tr>td:last-child,.table-responsive>.table-bordered>tfoot>tr>td:last-child{border-right:0}.table-responsive>.table-bordered>tbody>tr:last-child>th,.table-responsive>.table-bordered>tfoot>tr:last-child>th,.table-responsive>.table-bordered>tbody>tr:last-child>td,.table-responsive>.table-bordered>tfoot>tr:last-child>td{border-bottom:0}}fieldset{min-width:0;padding:0;margin:0;border:0}legend{display:block;width:100%;padding:0;margin-bottom:20px;font-size:21px;line-height:inherit;color:#333;border:0;border-bottom:1px solid #e5e5e5}label{display:inline-block;max-width:100%;margin-bottom:5px;font-weight:700}input[type=search]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}input[type=radio],input[type=checkbox]{margin:4px 0 0;margin-top:1px \9;line-height:normal}input[type=file]{display:block}input[type=range]{display:block;width:100%}select[multiple],select[size]{height:auto}input[type=file]:focus,input[type=radio]:focus,input[type=checkbox]:focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}output{display:block;padding-top:7px;font-size:14px;line-height:1.42857143;color:#555}.form-control{display:block;width:100%;height:34px;padding:6px 12px;font-size:14px;line-height:1.42857143;color:#555;background-color:#fff;background-image:none;border:1px solid #ccc;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075);-webkit-transition:border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;-o-transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s;transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s}.form-control:focus{border-color:#66afe9;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)}.form-control::-moz-placeholder{color:#999;opacity:1}.form-control:-ms-input-placeholder{color:#999}.form-control::-webkit-input-placeholder{color:#999}.form-control[disabled],.form-control[readonly],fieldset[disabled] .form-control{cursor:not-allowed;background-color:#eee;opacity:1}textarea.form-control{height:auto}input[type=search]{-webkit-appearance:none}input[type=date],input[type=time],input[type=datetime-local],input[type=month]{line-height:34px;line-height:1.42857143 \0}input[type=date].input-sm,input[type=time].input-sm,input[type=datetime-local].input-sm,input[type=month].input-sm{line-height:30px;line-height:1.5 \0}input[type=date].input-lg,input[type=time].input-lg,input[type=datetime-local].input-lg,input[type=month].input-lg{line-height:46px;line-height:1.33 \0}_:-ms-fullscreen,:root input[type=date],_:-ms-fullscreen,:root input[type=time],_:-ms-fullscreen,:root input[type=datetime-local],_:-ms-fullscreen,:root input[type=month]{line-height:1.42857143}_:-ms-fullscreen.input-sm,:root input[type=date].input-sm,_:-ms-fullscreen.input-sm,:root input[type=time].input-sm,_:-ms-fullscreen.input-sm,:root input[type=datetime-local].input-sm,_:-ms-fullscreen.input-sm,:root input[type=month].input-sm{line-height:1.5}_:-ms-fullscreen.input-lg,:root input[type=date].input-lg,_:-ms-fullscreen.input-lg,:root input[type=time].input-lg,_:-ms-fullscreen.input-lg,:root input[type=datetime-local].input-lg,_:-ms-fullscreen.input-lg,:root input[type=month].input-lg{line-height:1.33}.form-group{margin-bottom:15px}.radio,.checkbox{position:relative;display:block;margin-top:10px;margin-bottom:10px}.radio label,.checkbox label{min-height:20px;padding-left:20px;margin-bottom:0;font-weight:400;cursor:pointer}.radio input[type=radio],.radio-inline input[type=radio],.checkbox input[type=checkbox],.checkbox-inline input[type=checkbox]{position:absolute;margin-top:4px \9;margin-left:-20px}.radio+.radio,.checkbox+.checkbox{margin-top:-5px}.radio-inline,.checkbox-inline{display:inline-block;padding-left:20px;margin-bottom:0;font-weight:400;vertical-align:middle;cursor:pointer}.radio-inline+.radio-inline,.checkbox-inline+.checkbox-inline{margin-top:0;margin-left:10px}input[type=radio][disabled],input[type=checkbox][disabled],input[type=radio].disabled,input[type=checkbox].disabled,fieldset[disabled] input[type=radio],fieldset[disabled] input[type=checkbox]{cursor:not-allowed}.radio-inline.disabled,.checkbox-inline.disabled,fieldset[disabled] .radio-inline,fieldset[disabled] .checkbox-inline{cursor:not-allowed}.radio.disabled label,.checkbox.disabled label,fieldset[disabled] .radio label,fieldset[disabled] .checkbox label{cursor:not-allowed}.form-control-static{padding-top:7px;padding-bottom:7px;margin-bottom:0}.form-control-static.input-lg,.form-control-static.input-sm{padding-right:0;padding-left:0}.input-sm,.form-group-sm .form-control{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.input-sm,select.form-group-sm .form-control{height:30px;line-height:30px}textarea.input-sm,textarea.form-group-sm .form-control,select[multiple].input-sm,select[multiple].form-group-sm .form-control{height:auto}.input-lg,.form-group-lg .form-control{height:46px;padding:10px 16px;font-size:18px;line-height:1.33;border-radius:6px}select.input-lg,select.form-group-lg .form-control{height:46px;line-height:46px}textarea.input-lg,textarea.form-group-lg .form-control,select[multiple].input-lg,select[multiple].form-group-lg .form-control{height:auto}.has-feedback{position:relative}.has-feedback .form-control{padding-right:42.5px}.form-control-feedback{position:absolute;top:0;right:0;z-index:2;display:block;width:34px;height:34px;line-height:34px;text-align:center;pointer-events:none}.input-lg+.form-control-feedback{width:46px;height:46px;line-height:46px}.input-sm+.form-control-feedback{width:30px;height:30px;line-height:30px}.has-success .help-block,.has-success .control-label,.has-success .radio,.has-success .checkbox,.has-success .radio-inline,.has-success .checkbox-inline,.has-success.radio label,.has-success.checkbox label,.has-success.radio-inline label,.has-success.checkbox-inline label{color:#3c763d}.has-success .form-control{border-color:#3c763d;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-success .form-control:focus{border-color:#2b542c;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #67b168;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #67b168}.has-success .input-group-addon{color:#3c763d;background-color:#dff0d8;border-color:#3c763d}.has-success .form-control-feedback{color:#3c763d}.has-warning .help-block,.has-warning .control-label,.has-warning .radio,.has-warning .checkbox,.has-warning .radio-inline,.has-warning .checkbox-inline,.has-warning.radio label,.has-warning.checkbox label,.has-warning.radio-inline label,.has-warning.checkbox-inline label{color:#8a6d3b}.has-warning .form-control{border-color:#8a6d3b;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-warning .form-control:focus{border-color:#66512c;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #c0a16b;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #c0a16b}.has-warning .input-group-addon{color:#8a6d3b;background-color:#fcf8e3;border-color:#8a6d3b}.has-warning .form-control-feedback{color:#8a6d3b}.has-error .help-block,.has-error .control-label,.has-error .radio,.has-error .checkbox,.has-error .radio-inline,.has-error .checkbox-inline,.has-error.radio label,.has-error.checkbox label,.has-error.radio-inline label,.has-error.checkbox-inline label{color:#a94442}.has-error .form-control{border-color:#a94442;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075)}.has-error .form-control:focus{border-color:#843534;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #ce8483;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 6px #ce8483}.has-error .input-group-addon{color:#a94442;background-color:#f2dede;border-color:#a94442}.has-error .form-control-feedback{color:#a94442}.has-feedback label~.form-control-feedback{top:25px}.has-feedback label.sr-only~.form-control-feedback{top:0}.help-block{display:block;margin-top:5px;margin-bottom:10px;color:#737373}@media (min-width:768px){.form-inline .form-group{display:inline-block;margin-bottom:0;vertical-align:middle}.form-inline .form-control{display:inline-block;width:auto;vertical-align:middle}.form-inline .form-control-static{display:inline-block}.form-inline .input-group{display:inline-table;vertical-align:middle}.form-inline .input-group .input-group-addon,.form-inline .input-group .input-group-btn,.form-inline .input-group .form-control{width:auto}.form-inline .input-group>.form-control{width:100%}.form-inline .control-label{margin-bottom:0;vertical-align:middle}.form-inline .radio,.form-inline .checkbox{display:inline-block;margin-top:0;margin-bottom:0;vertical-align:middle}.form-inline .radio label,.form-inline .checkbox label{padding-left:0}.form-inline .radio input[type=radio],.form-inline .checkbox input[type=checkbox]{position:relative;margin-left:0}.form-inline .has-feedback .form-control-feedback{top:0}}.form-horizontal .radio,.form-horizontal .checkbox,.form-horizontal .radio-inline,.form-horizontal .checkbox-inline{padding-top:7px;margin-top:0;margin-bottom:0}.form-horizontal .radio,.form-horizontal .checkbox{min-height:27px}.form-horizontal .form-group{margin-right:-15px;margin-left:-15px}@media (min-width:768px){.form-horizontal .control-label{padding-top:7px;margin-bottom:0;text-align:right}}.form-horizontal .has-feedback .form-control-feedback{right:15px}@media (min-width:768px){.form-horizontal .form-group-lg .control-label{padding-top:14.3px}}@media (min-width:768px){.form-horizontal .form-group-sm .control-label{padding-top:6px}}.btn{display:inline-block;padding:6px 12px;margin-bottom:0;font-size:14px;font-weight:400;line-height:1.42857143;text-align:center;white-space:nowrap;vertical-align:middle;-ms-touch-action:manipulation;touch-action:manipulation;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-image:none;border:1px solid transparent;border-radius:4px}.btn:focus,.btn:active:focus,.btn.active:focus,.btn.focus,.btn:active.focus,.btn.active.focus{outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}.btn:hover,.btn:focus,.btn.focus{color:#333;text-decoration:none}.btn:active,.btn.active{background-image:none;outline:0;-webkit-box-shadow:inset 0 3px 5px rgba(0,0,0,.125);box-shadow:inset 0 3px 5px rgba(0,0,0,.125)}.btn.disabled,.btn[disabled],fieldset[disabled] .btn{pointer-events:none;cursor:not-allowed;filter:alpha(opacity=65);-webkit-box-shadow:none;box-shadow:none;opacity:.65}.btn-default{color:#333;background-color:#fff;border-color:#ccc}.btn-default:hover,.btn-default:focus,.btn-default.focus,.btn-default:active,.btn-default.active,.open>.dropdown-toggle.btn-default{color:#333;background-color:#e6e6e6;border-color:#adadad}.btn-default:active,.btn-default.active,.open>.dropdown-toggle.btn-default{background-image:none}.btn-default.disabled,.btn-default[disabled],fieldset[disabled] .btn-default,.btn-default.disabled:hover,.btn-default[disabled]:hover,fieldset[disabled] .btn-default:hover,.btn-default.disabled:focus,.btn-default[disabled]:focus,fieldset[disabled] .btn-default:focus,.btn-default.disabled.focus,.btn-default[disabled].focus,fieldset[disabled] .btn-default.focus,.btn-default.disabled:active,.btn-default[disabled]:active,fieldset[disabled] .btn-default:active,.btn-default.disabled.active,.btn-default[disabled].active,fieldset[disabled] .btn-default.active{background-color:#fff;border-color:#ccc}.btn-default .badge{color:#fff;background-color:#333}.btn-primary{color:#fff;background-color:#428bca;border-color:#357ebd}.btn-primary:hover,.btn-primary:focus,.btn-primary.focus,.btn-primary:active,.btn-primary.active,.open>.dropdown-toggle.btn-primary{color:#fff;background-color:#3071a9;border-color:#285e8e}.btn-primary:active,.btn-primary.active,.open>.dropdown-toggle.btn-primary{background-image:none}.btn-primary.disabled,.btn-primary[disabled],fieldset[disabled] .btn-primary,.btn-primary.disabled:hover,.btn-primary[disabled]:hover,fieldset[disabled] .btn-primary:hover,.btn-primary.disabled:focus,.btn-primary[disabled]:focus,fieldset[disabled] .btn-primary:focus,.btn-primary.disabled.focus,.btn-primary[disabled].focus,fieldset[disabled] .btn-primary.focus,.btn-primary.disabled:active,.btn-primary[disabled]:active,fieldset[disabled] .btn-primary:active,.btn-primary.disabled.active,.btn-primary[disabled].active,fieldset[disabled] .btn-primary.active{background-color:#428bca;border-color:#357ebd}.btn-primary .badge{color:#428bca;background-color:#fff}.btn-success{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.btn-success:hover,.btn-success:focus,.btn-success.focus,.btn-success:active,.btn-success.active,.open>.dropdown-toggle.btn-success{color:#fff;background-color:#449d44;border-color:#398439}.btn-success:active,.btn-success.active,.open>.dropdown-toggle.btn-success{background-image:none}.btn-success.disabled,.btn-success[disabled],fieldset[disabled] .btn-success,.btn-success.disabled:hover,.btn-success[disabled]:hover,fieldset[disabled] .btn-success:hover,.btn-success.disabled:focus,.btn-success[disabled]:focus,fieldset[disabled] .btn-success:focus,.btn-success.disabled.focus,.btn-success[disabled].focus,fieldset[disabled] .btn-success.focus,.btn-success.disabled:active,.btn-success[disabled]:active,fieldset[disabled] .btn-success:active,.btn-success.disabled.active,.btn-success[disabled].active,fieldset[disabled] .btn-success.active{background-color:#5cb85c;border-color:#4cae4c}.btn-success .badge{color:#5cb85c;background-color:#fff}.btn-info{color:#fff;background-color:#5bc0de;border-color:#46b8da}.btn-info:hover,.btn-info:focus,.btn-info.focus,.btn-info:active,.btn-info.active,.open>.dropdown-toggle.btn-info{color:#fff;background-color:#31b0d5;border-color:#269abc}.btn-info:active,.btn-info.active,.open>.dropdown-toggle.btn-info{background-image:none}.btn-info.disabled,.btn-info[disabled],fieldset[disabled] .btn-info,.btn-info.disabled:hover,.btn-info[disabled]:hover,fieldset[disabled] .btn-info:hover,.btn-info.disabled:focus,.btn-info[disabled]:focus,fieldset[disabled] .btn-info:focus,.btn-info.disabled.focus,.btn-info[disabled].focus,fieldset[disabled] .btn-info.focus,.btn-info.disabled:active,.btn-info[disabled]:active,fieldset[disabled] .btn-info:active,.btn-info.disabled.active,.btn-info[disabled].active,fieldset[disabled] .btn-info.active{background-color:#5bc0de;border-color:#46b8da}.btn-info .badge{color:#5bc0de;background-color:#fff}.btn-warning{color:#fff;background-color:#f0ad4e;border-color:#eea236}.btn-warning:hover,.btn-warning:focus,.btn-warning.focus,.btn-warning:active,.btn-warning.active,.open>.dropdown-toggle.btn-warning{color:#fff;background-color:#ec971f;border-color:#d58512}.btn-warning:active,.btn-warning.active,.open>.dropdown-toggle.btn-warning{background-image:none}.btn-warning.disabled,.btn-warning[disabled],fieldset[disabled] .btn-warning,.btn-warning.disabled:hover,.btn-warning[disabled]:hover,fieldset[disabled] .btn-warning:hover,.btn-warning.disabled:focus,.btn-warning[disabled]:focus,fieldset[disabled] .btn-warning:focus,.btn-warning.disabled.focus,.btn-warning[disabled].focus,fieldset[disabled] .btn-warning.focus,.btn-warning.disabled:active,.btn-warning[disabled]:active,fieldset[disabled] .btn-warning:active,.btn-warning.disabled.active,.btn-warning[disabled].active,fieldset[disabled] .btn-warning.active{background-color:#f0ad4e;border-color:#eea236}.btn-warning .badge{color:#f0ad4e;background-color:#fff}.btn-danger{color:#fff;background-color:#d9534f;border-color:#d43f3a}.btn-danger:hover,.btn-danger:focus,.btn-danger.focus,.btn-danger:active,.btn-danger.active,.open>.dropdown-toggle.btn-danger{color:#fff;background-color:#c9302c;border-color:#ac2925}.btn-danger:active,.btn-danger.active,.open>.dropdown-toggle.btn-danger{background-image:none}.btn-danger.disabled,.btn-danger[disabled],fieldset[disabled] .btn-danger,.btn-danger.disabled:hover,.btn-danger[disabled]:hover,fieldset[disabled] .btn-danger:hover,.btn-danger.disabled:focus,.btn-danger[disabled]:focus,fieldset[disabled] .btn-danger:focus,.btn-danger.disabled.focus,.btn-danger[disabled].focus,fieldset[disabled] .btn-danger.focus,.btn-danger.disabled:active,.btn-danger[disabled]:active,fieldset[disabled] .btn-danger:active,.btn-danger.disabled.active,.btn-danger[disabled].active,fieldset[disabled] .btn-danger.active{background-color:#d9534f;border-color:#d43f3a}.btn-danger .badge{color:#d9534f;background-color:#fff}.btn-link{font-weight:400;color:#428bca;border-radius:0}.btn-link,.btn-link:active,.btn-link.active,.btn-link[disabled],fieldset[disabled] .btn-link{background-color:transparent;-webkit-box-shadow:none;box-shadow:none}.btn-link,.btn-link:hover,.btn-link:focus,.btn-link:active{border-color:transparent}.btn-link:hover,.btn-link:focus{color:#2a6496;text-decoration:underline;background-color:transparent}.btn-link[disabled]:hover,fieldset[disabled] .btn-link:hover,.btn-link[disabled]:focus,fieldset[disabled] .btn-link:focus{color:#777;text-decoration:none}.btn-lg,.btn-group-lg>.btn{padding:10px 16px;font-size:18px;line-height:1.33;border-radius:6px}.btn-sm,.btn-group-sm>.btn{padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}.btn-xs,.btn-group-xs>.btn{padding:1px 5px;font-size:12px;line-height:1.5;border-radius:3px}.btn-block{display:block;width:100%}.btn-block+.btn-block{margin-top:5px}input[type=submit].btn-block,input[type=reset].btn-block,input[type=button].btn-block{width:100%}.fade{opacity:0;-webkit-transition:opacity .15s linear;-o-transition:opacity .15s linear;transition:opacity .15s linear}.fade.in{opacity:1}.collapse{display:none;visibility:hidden}.collapse.in{display:block;visibility:visible}tr.collapse.in{display:table-row}tbody.collapse.in{display:table-row-group}.collapsing{position:relative;height:0;overflow:hidden;-webkit-transition-timing-function:ease;-o-transition-timing-function:ease;transition-timing-function:ease;-webkit-transition-duration:.35s;-o-transition-duration:.35s;transition-duration:.35s;-webkit-transition-property:height,visibility;-o-transition-property:height,visibility;transition-property:height,visibility}.caret{display:inline-block;width:0;height:0;margin-left:2px;vertical-align:middle;border-top:4px solid;border-right:4px solid transparent;border-left:4px solid transparent}.dropdown{position:relative}.dropdown-toggle:focus{outline:0}.dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;display:none;float:left;min-width:160px;padding:5px 0;margin:2px 0 0;font-size:14px;text-align:left;list-style:none;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.15);border-radius:4px;-webkit-box-shadow:0 6px 12px rgba(0,0,0,.175);box-shadow:0 6px 12px rgba(0,0,0,.175)}.dropdown-menu.pull-right{right:0;left:auto}.dropdown-menu .divider{height:1px;margin:9px 0;overflow:hidden;background-color:#e5e5e5}.dropdown-menu>li>a{display:block;padding:3px 20px;clear:both;font-weight:400;line-height:1.42857143;color:#333;white-space:nowrap}.dropdown-menu>li>a:hover,.dropdown-menu>li>a:focus{color:#262626;text-decoration:none;background-color:#f5f5f5}.dropdown-menu>.active>a,.dropdown-menu>.active>a:hover,.dropdown-menu>.active>a:focus{color:#fff;text-decoration:none;background-color:#428bca;outline:0}.dropdown-menu>.disabled>a,.dropdown-menu>.disabled>a:hover,.dropdown-menu>.disabled>a:focus{color:#777}.dropdown-menu>.disabled>a:hover,.dropdown-menu>.disabled>a:focus{text-decoration:none;cursor:not-allowed;background-color:transparent;background-image:none;filter:progid:DXImageTransform.Microsoft.gradient(enabled=false)}.open>.dropdown-menu{display:block}.open>a{outline:0}.dropdown-menu-right{right:0;left:auto}.dropdown-menu-left{right:auto;left:0}.dropdown-header{display:block;padding:3px 20px;font-size:12px;line-height:1.42857143;color:#777;white-space:nowrap}.dropdown-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;z-index:990}.pull-right>.dropdown-menu{right:0;left:auto}.dropup .caret,.navbar-fixed-bottom .dropdown .caret{content:"";border-top:0;border-bottom:4px solid}.dropup .dropdown-menu,.navbar-fixed-bottom .dropdown .dropdown-menu{top:auto;bottom:100%;margin-bottom:1px}@media (min-width:768px){.navbar-right .dropdown-menu{right:0;left:auto}.navbar-right .dropdown-menu-left{right:auto;left:0}}.btn-group,.btn-group-vertical{position:relative;display:inline-block;vertical-align:middle}.btn-group>.btn,.btn-group-vertical>.btn{position:relative;float:left}.btn-group>.btn:hover,.btn-group-vertical>.btn:hover,.btn-group>.btn:focus,.btn-group-vertical>.btn:focus,.btn-group>.btn:active,.btn-group-vertical>.btn:active,.btn-group>.btn.active,.btn-group-vertical>.btn.active{z-index:2}.btn-group>.btn:focus,.btn-group-vertical>.btn:focus{outline:0}.btn-group .btn+.btn,.btn-group .btn+.btn-group,.btn-group .btn-group+.btn,.btn-group .btn-group+.btn-group{margin-left:-1px}.btn-toolbar{margin-left:-5px}.btn-toolbar .btn-group,.btn-toolbar .input-group{float:left}.btn-toolbar>.btn,.btn-toolbar>.btn-group,.btn-toolbar>.input-group{margin-left:5px}.btn-group>.btn:not(:first-child):not(:last-child):not(.dropdown-toggle){border-radius:0}.btn-group>.btn:first-child{margin-left:0}.btn-group>.btn:first-child:not(:last-child):not(.dropdown-toggle){border-top-right-radius:0;border-bottom-right-radius:0}.btn-group>.btn:last-child:not(:first-child),.btn-group>.dropdown-toggle:not(:first-child){border-top-left-radius:0;border-bottom-left-radius:0}.btn-group>.btn-group{float:left}.btn-group>.btn-group:not(:first-child):not(:last-child)>.btn{border-radius:0}.btn-group>.btn-group:first-child>.btn:last-child,.btn-group>.btn-group:first-child>.dropdown-toggle{border-top-right-radius:0;border-bottom-right-radius:0}.btn-group>.btn-group:last-child>.btn:first-child{border-top-left-radius:0;border-bottom-left-radius:0}.btn-group .dropdown-toggle:active,.btn-group.open .dropdown-toggle{outline:0}.btn-group>.btn+.dropdown-toggle{padding-right:8px;padding-left:8px}.btn-group>.btn-lg+.dropdown-toggle{padding-right:12px;padding-left:12px}.btn-group.open .dropdown-toggle{-webkit-box-shadow:inset 0 3px 5px rgba(0,0,0,.125);box-shadow:inset 0 3px 5px rgba(0,0,0,.125)}.btn-group.open .dropdown-toggle.btn-link{-webkit-box-shadow:none;box-shadow:none}.btn .caret{margin-left:0}.btn-lg .caret{border-width:5px 5px 0;border-bottom-width:0}.dropup .btn-lg .caret{border-width:0 5px 5px}.btn-group-vertical>.btn,.btn-group-vertical>.btn-group,.btn-group-vertical>.btn-group>.btn{display:block;float:none;width:100%;max-width:100%}.btn-group-vertical>.btn-group>.btn{float:none}.btn-group-vertical>.btn+.btn,.btn-group-vertical>.btn+.btn-group,.btn-group-vertical>.btn-group+.btn,.btn-group-vertical>.btn-group+.btn-group{margin-top:-1px;margin-left:0}.btn-group-vertical>.btn:not(:first-child):not(:last-child){border-radius:0}.btn-group-vertical>.btn:first-child:not(:last-child){border-top-right-radius:4px;border-bottom-right-radius:0;border-bottom-left-radius:0}.btn-group-vertical>.btn:last-child:not(:first-child){border-top-left-radius:0;border-top-right-radius:0;border-bottom-left-radius:4px}.btn-group-vertical>.btn-group:not(:first-child):not(:last-child)>.btn{border-radius:0}.btn-group-vertical>.btn-group:first-child:not(:last-child)>.btn:last-child,.btn-group-vertical>.btn-group:first-child:not(:last-child)>.dropdown-toggle{border-bottom-right-radius:0;border-bottom-left-radius:0}.btn-group-vertical>.btn-group:last-child:not(:first-child)>.btn:first-child{border-top-left-radius:0;border-top-right-radius:0}.btn-group-justified{display:table;width:100%;table-layout:fixed;border-collapse:separate}.btn-group-justified>.btn,.btn-group-justified>.btn-group{display:table-cell;float:none;width:1%}.btn-group-justified>.btn-group .btn{width:100%}.btn-group-justified>.btn-group .dropdown-menu{left:auto}[data-toggle=buttons]>.btn input[type=radio],[data-toggle=buttons]>.btn-group>.btn input[type=radio],[data-toggle=buttons]>.btn input[type=checkbox],[data-toggle=buttons]>.btn-group>.btn input[type=checkbox]{position:absolute;clip:rect(0,0,0,0);pointer-events:none}.input-group{position:relative;display:table;border-collapse:separate}.input-group[class*=col-]{float:none;padding-right:0;padding-left:0}.input-group .form-control{position:relative;z-index:2;float:left;width:100%;margin-bottom:0}.input-group-lg>.form-control,.input-group-lg>.input-group-addon,.input-group-lg>.input-group-btn>.btn{height:46px;padding:10px 16px;font-size:18px;line-height:1.33;border-radius:6px}select.input-group-lg>.form-control,select.input-group-lg>.input-group-addon,select.input-group-lg>.input-group-btn>.btn{height:46px;line-height:46px}textarea.input-group-lg>.form-control,textarea.input-group-lg>.input-group-addon,textarea.input-group-lg>.input-group-btn>.btn,select[multiple].input-group-lg>.form-control,select[multiple].input-group-lg>.input-group-addon,select[multiple].input-group-lg>.input-group-btn>.btn{height:auto}.input-group-sm>.form-control,.input-group-sm>.input-group-addon,.input-group-sm>.input-group-btn>.btn{height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:3px}select.input-group-sm>.form-control,select.input-group-sm>.input-group-addon,select.input-group-sm>.input-group-btn>.btn{height:30px;line-height:30px}textarea.input-group-sm>.form-control,textarea.input-group-sm>.input-group-addon,textarea.input-group-sm>.input-group-btn>.btn,select[multiple].input-group-sm>.form-control,select[multiple].input-group-sm>.input-group-addon,select[multiple].input-group-sm>.input-group-btn>.btn{height:auto}.input-group-addon,.input-group-btn,.input-group .form-control{display:table-cell}.input-group-addon:not(:first-child):not(:last-child),.input-group-btn:not(:first-child):not(:last-child),.input-group .form-control:not(:first-child):not(:last-child){border-radius:0}.input-group-addon,.input-group-btn{width:1%;white-space:nowrap;vertical-align:middle}.input-group-addon{padding:6px 12px;font-size:14px;font-weight:400;line-height:1;color:#555;text-align:center;background-color:#eee;border:1px solid #ccc;border-radius:4px}.input-group-addon.input-sm{padding:5px 10px;font-size:12px;border-radius:3px}.input-group-addon.input-lg{padding:10px 16px;font-size:18px;border-radius:6px}.input-group-addon input[type=radio],.input-group-addon input[type=checkbox]{margin-top:0}.input-group .form-control:first-child,.input-group-addon:first-child,.input-group-btn:first-child>.btn,.input-group-btn:first-child>.btn-group>.btn,.input-group-btn:first-child>.dropdown-toggle,.input-group-btn:last-child>.btn:not(:last-child):not(.dropdown-toggle),.input-group-btn:last-child>.btn-group:not(:last-child)>.btn{border-top-right-radius:0;border-bottom-right-radius:0}.input-group-addon:first-child{border-right:0}.input-group .form-control:last-child,.input-group-addon:last-child,.input-group-btn:last-child>.btn,.input-group-btn:last-child>.btn-group>.btn,.input-group-btn:last-child>.dropdown-toggle,.input-group-btn:first-child>.btn:not(:first-child),.input-group-btn:first-child>.btn-group:not(:first-child)>.btn{border-top-left-radius:0;border-bottom-left-radius:0}.input-group-addon:last-child{border-left:0}.input-group-btn{position:relative;font-size:0;white-space:nowrap}.input-group-btn>.btn{position:relative}.input-group-btn>.btn+.btn{margin-left:-1px}.input-group-btn>.btn:hover,.input-group-btn>.btn:focus,.input-group-btn>.btn:active{z-index:2}.input-group-btn:first-child>.btn,.input-group-btn:first-child>.btn-group{margin-right:-1px}.input-group-btn:last-child>.btn,.input-group-btn:last-child>.btn-group{margin-left:-1px}.nav{padding-left:0;margin-bottom:0;list-style:none}.nav>li{position:relative;display:block}.nav>li>a{position:relative;display:block;padding:10px 15px}.nav>li>a:hover,.nav>li>a:focus{text-decoration:none;background-color:#eee}.nav>li.disabled>a{color:#777}.nav>li.disabled>a:hover,.nav>li.disabled>a:focus{color:#777;text-decoration:none;cursor:not-allowed;background-color:transparent}.nav .open>a,.nav .open>a:hover,.nav .open>a:focus{background-color:#eee;border-color:#428bca}.nav .nav-divider{height:1px;margin:9px 0;overflow:hidden;background-color:#e5e5e5}.nav>li>a>img{max-width:none}.nav-tabs{border-bottom:1px solid #ddd}.nav-tabs>li{float:left;margin-bottom:-1px}.nav-tabs>li>a{margin-right:2px;line-height:1.42857143;border:1px solid transparent;border-radius:4px 4px 0 0}.nav-tabs>li>a:hover{border-color:#eee #eee #ddd}.nav-tabs>li.active>a,.nav-tabs>li.active>a:hover,.nav-tabs>li.active>a:focus{color:#555;cursor:default;background-color:#fff;border:1px solid #ddd;border-bottom-color:transparent}.nav-tabs.nav-justified{width:100%;border-bottom:0}.nav-tabs.nav-justified>li{float:none}.nav-tabs.nav-justified>li>a{margin-bottom:5px;text-align:center}.nav-tabs.nav-justified>.dropdown .dropdown-menu{top:auto;left:auto}@media (min-width:768px){.nav-tabs.nav-justified>li{display:table-cell;width:1%}.nav-tabs.nav-justified>li>a{margin-bottom:0}}.nav-tabs.nav-justified>li>a{margin-right:0;border-radius:4px}.nav-tabs.nav-justified>.active>a,.nav-tabs.nav-justified>.active>a:hover,.nav-tabs.nav-justified>.active>a:focus{border:1px solid #ddd}@media (min-width:768px){.nav-tabs.nav-justified>li>a{border-bottom:1px solid #ddd;border-radius:4px 4px 0 0}.nav-tabs.nav-justified>.active>a,.nav-tabs.nav-justified>.active>a:hover,.nav-tabs.nav-justified>.active>a:focus{border-bottom-color:#fff}}.nav-pills>li{float:left}.nav-pills>li>a{border-radius:4px}.nav-pills>li+li{margin-left:2px}.nav-pills>li.active>a,.nav-pills>li.active>a:hover,.nav-pills>li.active>a:focus{color:#fff;background-color:#428bca}.nav-stacked>li{float:none}.nav-stacked>li+li{margin-top:2px;margin-left:0}.nav-justified{width:100%}.nav-justified>li{float:none}.nav-justified>li>a{margin-bottom:5px;text-align:center}.nav-justified>.dropdown .dropdown-menu{top:auto;left:auto}@media (min-width:768px){.nav-justified>li{display:table-cell;width:1%}.nav-justified>li>a{margin-bottom:0}}.nav-tabs-justified{border-bottom:0}.nav-tabs-justified>li>a{margin-right:0;border-radius:4px}.nav-tabs-justified>.active>a,.nav-tabs-justified>.active>a:hover,.nav-tabs-justified>.active>a:focus{border:1px solid #ddd}@media (min-width:768px){.nav-tabs-justified>li>a{border-bottom:1px solid #ddd;border-radius:4px 4px 0 0}.nav-tabs-justified>.active>a,.nav-tabs-justified>.active>a:hover,.nav-tabs-justified>.active>a:focus{border-bottom-color:#fff}}.tab-content>.tab-pane{display:none;visibility:hidden}.tab-content>.active{display:block;visibility:visible}.nav-tabs .dropdown-menu{margin-top:-1px;border-top-left-radius:0;border-top-right-radius:0}.navbar{position:relative;min-height:50px;margin-bottom:20px;border:1px solid transparent}@media (min-width:768px){.navbar{border-radius:4px}}@media (min-width:768px){.navbar-header{float:left}}.navbar-collapse{padding-right:15px;padding-left:15px;overflow-x:visible;-webkit-overflow-scrolling:touch;border-top:1px solid transparent;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.1);box-shadow:inset 0 1px 0 rgba(255,255,255,.1)}.navbar-collapse.in{overflow-y:auto}@media (min-width:768px){.navbar-collapse{width:auto;border-top:0;-webkit-box-shadow:none;box-shadow:none}.navbar-collapse.collapse{display:block!important;height:auto!important;padding-bottom:0;overflow:visible!important;visibility:visible!important}.navbar-collapse.in{overflow-y:visible}.navbar-fixed-top .navbar-collapse,.navbar-static-top .navbar-collapse,.navbar-fixed-bottom .navbar-collapse{padding-right:0;padding-left:0}}.navbar-fixed-top .navbar-collapse,.navbar-fixed-bottom .navbar-collapse{max-height:340px}@media (max-device-width:480px) and (orientation:landscape){.navbar-fixed-top .navbar-collapse,.navbar-fixed-bottom .navbar-collapse{max-height:200px}}.container>.navbar-header,.container-fluid>.navbar-header,.container>.navbar-collapse,.container-fluid>.navbar-collapse{margin-right:-15px;margin-left:-15px}@media (min-width:768px){.container>.navbar-header,.container-fluid>.navbar-header,.container>.navbar-collapse,.container-fluid>.navbar-collapse{margin-right:0;margin-left:0}}.navbar-static-top{z-index:1000;border-width:0 0 1px}@media (min-width:768px){.navbar-static-top{border-radius:0}}.navbar-fixed-top,.navbar-fixed-bottom{position:fixed;right:0;left:0;z-index:1030}@media (min-width:768px){.navbar-fixed-top,.navbar-fixed-bottom{border-radius:0}}.navbar-fixed-top{top:0;border-width:0 0 1px}.navbar-fixed-bottom{bottom:0;margin-bottom:0;border-width:1px 0 0}.navbar-brand{float:left;height:50px;padding:15px 15px;font-size:18px;line-height:20px}.navbar-brand:hover,.navbar-brand:focus{text-decoration:none}.navbar-brand>img{display:block}@media (min-width:768px){.navbar>.container .navbar-brand,.navbar>.container-fluid .navbar-brand{margin-left:-15px}}.navbar-toggle{position:relative;float:right;padding:9px 10px;margin-top:8px;margin-right:15px;margin-bottom:8px;background-color:transparent;background-image:none;border:1px solid transparent;border-radius:4px}.navbar-toggle:focus{outline:0}.navbar-toggle .icon-bar{display:block;width:22px;height:2px;border-radius:1px}.navbar-toggle .icon-bar+.icon-bar{margin-top:4px}@media (min-width:768px){.navbar-toggle{display:none}}.navbar-nav{margin:7.5px -15px}.navbar-nav>li>a{padding-top:10px;padding-bottom:10px;line-height:20px}@media (max-width:767px){.navbar-nav .open .dropdown-menu{position:static;float:none;width:auto;margin-top:0;background-color:transparent;border:0;-webkit-box-shadow:none;box-shadow:none}.navbar-nav .open .dropdown-menu>li>a,.navbar-nav .open .dropdown-menu .dropdown-header{padding:5px 15px 5px 25px}.navbar-nav .open .dropdown-menu>li>a{line-height:20px}.navbar-nav .open .dropdown-menu>li>a:hover,.navbar-nav .open .dropdown-menu>li>a:focus{background-image:none}}@media (min-width:768px){.navbar-nav{float:left;margin:0}.navbar-nav>li{float:left}.navbar-nav>li>a{padding-top:15px;padding-bottom:15px}}.navbar-form{padding:10px 15px;margin-top:8px;margin-right:-15px;margin-bottom:8px;margin-left:-15px;border-top:1px solid transparent;border-bottom:1px solid transparent;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1);box-shadow:inset 0 1px 0 rgba(255,255,255,.1),0 1px 0 rgba(255,255,255,.1)}@media (min-width:768px){.navbar-form .form-group{display:inline-block;margin-bottom:0;vertical-align:middle}.navbar-form .form-control{display:inline-block;width:auto;vertical-align:middle}.navbar-form .form-control-static{display:inline-block}.navbar-form .input-group{display:inline-table;vertical-align:middle}.navbar-form .input-group .input-group-addon,.navbar-form .input-group .input-group-btn,.navbar-form .input-group .form-control{width:auto}.navbar-form .input-group>.form-control{width:100%}.navbar-form .control-label{margin-bottom:0;vertical-align:middle}.navbar-form .radio,.navbar-form .checkbox{display:inline-block;margin-top:0;margin-bottom:0;vertical-align:middle}.navbar-form .radio label,.navbar-form .checkbox label{padding-left:0}.navbar-form .radio input[type=radio],.navbar-form .checkbox input[type=checkbox]{position:relative;margin-left:0}.navbar-form .has-feedback .form-control-feedback{top:0}}@media (max-width:767px){.navbar-form .form-group{margin-bottom:5px}.navbar-form .form-group:last-child{margin-bottom:0}}@media (min-width:768px){.navbar-form{width:auto;padding-top:0;padding-bottom:0;margin-right:0;margin-left:0;border:0;-webkit-box-shadow:none;box-shadow:none}}.navbar-nav>li>.dropdown-menu{margin-top:0;border-top-left-radius:0;border-top-right-radius:0}.navbar-fixed-bottom .navbar-nav>li>.dropdown-menu{border-bottom-right-radius:0;border-bottom-left-radius:0}.navbar-btn{margin-top:8px;margin-bottom:8px}.navbar-btn.btn-sm{margin-top:10px;margin-bottom:10px}.navbar-btn.btn-xs{margin-top:14px;margin-bottom:14px}.navbar-text{margin-top:15px;margin-bottom:15px}@media (min-width:768px){.navbar-text{float:left;margin-right:15px;margin-left:15px}}@media (min-width:768px){.navbar-left{float:left!important}.navbar-right{float:right!important;margin-right:-15px}.navbar-right~.navbar-right{margin-right:0}}.navbar-default{background-color:#f8f8f8;border-color:#e7e7e7}.navbar-default .navbar-brand{color:#777}.navbar-default .navbar-brand:hover,.navbar-default .navbar-brand:focus{color:#5e5e5e;background-color:transparent}.navbar-default .navbar-text{color:#777}.navbar-default .navbar-nav>li>a{color:#777}.navbar-default .navbar-nav>li>a:hover,.navbar-default .navbar-nav>li>a:focus{color:#333;background-color:transparent}.navbar-default .navbar-nav>.active>a,.navbar-default .navbar-nav>.active>a:hover,.navbar-default .navbar-nav>.active>a:focus{color:#555;background-color:#e7e7e7}.navbar-default .navbar-nav>.disabled>a,.navbar-default .navbar-nav>.disabled>a:hover,.navbar-default .navbar-nav>.disabled>a:focus{color:#ccc;background-color:transparent}.navbar-default .navbar-toggle{border-color:#ddd}.navbar-default .navbar-toggle:hover,.navbar-default .navbar-toggle:focus{background-color:#ddd}.navbar-default .navbar-toggle .icon-bar{background-color:#888}.navbar-default .navbar-collapse,.navbar-default .navbar-form{border-color:#e7e7e7}.navbar-default .navbar-nav>.open>a,.navbar-default .navbar-nav>.open>a:hover,.navbar-default .navbar-nav>.open>a:focus{color:#555;background-color:#e7e7e7}@media (max-width:767px){.navbar-default .navbar-nav .open .dropdown-menu>li>a{color:#777}.navbar-default .navbar-nav .open .dropdown-menu>li>a:hover,.navbar-default .navbar-nav .open .dropdown-menu>li>a:focus{color:#333;background-color:transparent}.navbar-default .navbar-nav .open .dropdown-menu>.active>a,.navbar-default .navbar-nav .open .dropdown-menu>.active>a:hover,.navbar-default .navbar-nav .open .dropdown-menu>.active>a:focus{color:#555;background-color:#e7e7e7}.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a,.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a:hover,.navbar-default .navbar-nav .open .dropdown-menu>.disabled>a:focus{color:#ccc;background-color:transparent}}.navbar-default .navbar-link{color:#777}.navbar-default .navbar-link:hover{color:#333}.navbar-default .btn-link{color:#777}.navbar-default .btn-link:hover,.navbar-default .btn-link:focus{color:#333}.navbar-default .btn-link[disabled]:hover,fieldset[disabled] .navbar-default .btn-link:hover,.navbar-default .btn-link[disabled]:focus,fieldset[disabled] .navbar-default .btn-link:focus{color:#ccc}.navbar-inverse{background-color:#222;border-color:#080808}.navbar-inverse .navbar-brand{color:#9d9d9d}.navbar-inverse .navbar-brand:hover,.navbar-inverse .navbar-brand:focus{color:#fff;background-color:transparent}.navbar-inverse .navbar-text{color:#9d9d9d}.navbar-inverse .navbar-nav>li>a{color:#9d9d9d}.navbar-inverse .navbar-nav>li>a:hover,.navbar-inverse .navbar-nav>li>a:focus{color:#fff;background-color:transparent}.navbar-inverse .navbar-nav>.active>a,.navbar-inverse .navbar-nav>.active>a:hover,.navbar-inverse .navbar-nav>.active>a:focus{color:#fff;background-color:#080808}.navbar-inverse .navbar-nav>.disabled>a,.navbar-inverse .navbar-nav>.disabled>a:hover,.navbar-inverse .navbar-nav>.disabled>a:focus{color:#444;background-color:transparent}.navbar-inverse .navbar-toggle{border-color:#333}.navbar-inverse .navbar-toggle:hover,.navbar-inverse .navbar-toggle:focus{background-color:#333}.navbar-inverse .navbar-toggle .icon-bar{background-color:#fff}.navbar-inverse .navbar-collapse,.navbar-inverse .navbar-form{border-color:#101010}.navbar-inverse .navbar-nav>.open>a,.navbar-inverse .navbar-nav>.open>a:hover,.navbar-inverse .navbar-nav>.open>a:focus{color:#fff;background-color:#080808}@media (max-width:767px){.navbar-inverse .navbar-nav .open .dropdown-menu>.dropdown-header{border-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu .divider{background-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu>li>a{color:#9d9d9d}.navbar-inverse .navbar-nav .open .dropdown-menu>li>a:hover,.navbar-inverse .navbar-nav .open .dropdown-menu>li>a:focus{color:#fff;background-color:transparent}.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a:hover,.navbar-inverse .navbar-nav .open .dropdown-menu>.active>a:focus{color:#fff;background-color:#080808}.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a:hover,.navbar-inverse .navbar-nav .open .dropdown-menu>.disabled>a:focus{color:#444;background-color:transparent}}.navbar-inverse .navbar-link{color:#9d9d9d}.navbar-inverse .navbar-link:hover{color:#fff}.navbar-inverse .btn-link{color:#9d9d9d}.navbar-inverse .btn-link:hover,.navbar-inverse .btn-link:focus{color:#fff}.navbar-inverse .btn-link[disabled]:hover,fieldset[disabled] .navbar-inverse .btn-link:hover,.navbar-inverse .btn-link[disabled]:focus,fieldset[disabled] .navbar-inverse .btn-link:focus{color:#444}.breadcrumb{padding:8px 15px;margin-bottom:20px;list-style:none;background-color:#f5f5f5;border-radius:4px}.breadcrumb>li{display:inline-block}.breadcrumb>li+li:before{padding:0 5px;color:#ccc;content:"/\00a0"}.breadcrumb>.active{color:#777}.pagination{display:inline-block;padding-left:0;margin:20px 0;border-radius:4px}.pagination>li{display:inline}.pagination>li>a,.pagination>li>span{position:relative;float:left;padding:6px 12px;margin-left:-1px;line-height:1.42857143;color:#428bca;text-decoration:none;background-color:#fff;border:1px solid #ddd}.pagination>li:first-child>a,.pagination>li:first-child>span{margin-left:0;border-top-left-radius:4px;border-bottom-left-radius:4px}.pagination>li:last-child>a,.pagination>li:last-child>span{border-top-right-radius:4px;border-bottom-right-radius:4px}.pagination>li>a:hover,.pagination>li>span:hover,.pagination>li>a:focus,.pagination>li>span:focus{color:#2a6496;background-color:#eee;border-color:#ddd}.pagination>.active>a,.pagination>.active>span,.pagination>.active>a:hover,.pagination>.active>span:hover,.pagination>.active>a:focus,.pagination>.active>span:focus{z-index:2;color:#fff;cursor:default;background-color:#428bca;border-color:#428bca}.pagination>.disabled>span,.pagination>.disabled>span:hover,.pagination>.disabled>span:focus,.pagination>.disabled>a,.pagination>.disabled>a:hover,.pagination>.disabled>a:focus{color:#777;cursor:not-allowed;background-color:#fff;border-color:#ddd}.pagination-lg>li>a,.pagination-lg>li>span{padding:10px 16px;font-size:18px}.pagination-lg>li:first-child>a,.pagination-lg>li:first-child>span{border-top-left-radius:6px;border-bottom-left-radius:6px}.pagination-lg>li:last-child>a,.pagination-lg>li:last-child>span{border-top-right-radius:6px;border-bottom-right-radius:6px}.pagination-sm>li>a,.pagination-sm>li>span{padding:5px 10px;font-size:12px}.pagination-sm>li:first-child>a,.pagination-sm>li:first-child>span{border-top-left-radius:3px;border-bottom-left-radius:3px}.pagination-sm>li:last-child>a,.pagination-sm>li:last-child>span{border-top-right-radius:3px;border-bottom-right-radius:3px}.pager{padding-left:0;margin:20px 0;text-align:center;list-style:none}.pager li{display:inline}.pager li>a,.pager li>span{display:inline-block;padding:5px 14px;background-color:#fff;border:1px solid #ddd;border-radius:15px}.pager li>a:hover,.pager li>a:focus{text-decoration:none;background-color:#eee}.pager .next>a,.pager .next>span{float:right}.pager .previous>a,.pager .previous>span{float:left}.pager .disabled>a,.pager .disabled>a:hover,.pager .disabled>a:focus,.pager .disabled>span{color:#777;cursor:not-allowed;background-color:#fff}.label{display:inline;padding:.2em .6em .3em;font-size:75%;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;border-radius:.25em}a.label:hover,a.label:focus{color:#fff;text-decoration:none;cursor:pointer}.label:empty{display:none}.btn .label{position:relative;top:-1px}.label-default{background-color:#777}.label-default[href]:hover,.label-default[href]:focus{background-color:#5e5e5e}.label-primary{background-color:#428bca}.label-primary[href]:hover,.label-primary[href]:focus{background-color:#3071a9}.label-success{background-color:#5cb85c}.label-success[href]:hover,.label-success[href]:focus{background-color:#449d44}.label-info{background-color:#5bc0de}.label-info[href]:hover,.label-info[href]:focus{background-color:#31b0d5}.label-warning{background-color:#f0ad4e}.label-warning[href]:hover,.label-warning[href]:focus{background-color:#ec971f}.label-danger{background-color:#d9534f}.label-danger[href]:hover,.label-danger[href]:focus{background-color:#c9302c}.badge{display:inline-block;min-width:10px;padding:3px 7px;font-size:12px;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;background-color:#777;border-radius:10px}.badge:empty{display:none}.btn .badge{position:relative;top:-1px}.btn-xs .badge{top:0;padding:1px 5px}a.badge:hover,a.badge:focus{color:#fff;text-decoration:none;cursor:pointer}a.list-group-item.active>.badge,.nav-pills>.active>a>.badge{color:#428bca;background-color:#fff}.nav-pills>li>a>.badge{margin-left:3px}.jumbotron{padding:30px 15px;margin-bottom:30px;color:inherit;background-color:#eee}.jumbotron h1,.jumbotron .h1{color:inherit}.jumbotron p{margin-bottom:15px;font-size:21px;font-weight:200}.jumbotron>hr{border-top-color:#d5d5d5}.container .jumbotron,.container-fluid .jumbotron{border-radius:6px}.jumbotron .container{max-width:100%}@media screen and (min-width:768px){.jumbotron{padding:48px 0}.container .jumbotron{padding-right:60px;padding-left:60px}.jumbotron h1,.jumbotron .h1{font-size:63px}}.thumbnail{display:block;padding:4px;margin-bottom:20px;line-height:1.42857143;background-color:#fff;border:1px solid #ddd;border-radius:4px;-webkit-transition:border .2s ease-in-out;-o-transition:border .2s ease-in-out;transition:border .2s ease-in-out}.thumbnail>img,.thumbnail a>img{margin-right:auto;margin-left:auto}a.thumbnail:hover,a.thumbnail:focus,a.thumbnail.active{border-color:#428bca}.thumbnail .caption{padding:9px;color:#333}.alert{padding:15px;margin-bottom:20px;border:1px solid transparent;border-radius:4px}.alert h4{margin-top:0;color:inherit}.alert .alert-link{font-weight:700}.alert>p,.alert>ul{margin-bottom:0}.alert>p+p{margin-top:5px}.alert-dismissable,.alert-dismissible{padding-right:35px}.alert-dismissable .close,.alert-dismissible .close{position:relative;top:-2px;right:-21px;color:inherit}.alert-success{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.alert-success hr{border-top-color:#c9e2b3}.alert-success .alert-link{color:#2b542c}.alert-info{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.alert-info hr{border-top-color:#a6e1ec}.alert-info .alert-link{color:#245269}.alert-warning{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.alert-warning hr{border-top-color:#f7e1b5}.alert-warning .alert-link{color:#66512c}.alert-danger{color:#a94442;background-color:#f2dede;border-color:#ebccd1}.alert-danger hr{border-top-color:#e4b9c0}.alert-danger .alert-link{color:#843534}@-webkit-keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}@-o-keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}@keyframes progress-bar-stripes{from{background-position:40px 0}to{background-position:0 0}}.progress{height:20px;margin-bottom:20px;overflow:hidden;background-color:#f5f5f5;border-radius:4px;-webkit-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);box-shadow:inset 0 1px 2px rgba(0,0,0,.1)}.progress-bar{float:left;width:0;height:100%;font-size:12px;line-height:20px;color:#fff;text-align:center;background-color:#428bca;-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,.15);box-shadow:inset 0 -1px 0 rgba(0,0,0,.15);-webkit-transition:width .6s ease;-o-transition:width .6s ease;transition:width .6s ease}.progress-striped .progress-bar,.progress-bar-striped{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);-webkit-background-size:40px 40px;background-size:40px 40px}.progress.active .progress-bar,.progress-bar.active{-webkit-animation:progress-bar-stripes 2s linear infinite;-o-animation:progress-bar-stripes 2s linear infinite;animation:progress-bar-stripes 2s linear infinite}.progress-bar-success{background-color:#5cb85c}.progress-striped .progress-bar-success{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-info{background-color:#5bc0de}.progress-striped .progress-bar-info{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-warning{background-color:#f0ad4e}.progress-striped .progress-bar-warning{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.progress-bar-danger{background-color:#d9534f}.progress-striped .progress-bar-danger{background-image:-webkit-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:-o-linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);background-image:linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent)}.media{margin-top:15px}.media:first-child{margin-top:0}.media-right,.media>.pull-right{padding-left:10px}.media-left,.media>.pull-left{padding-right:10px}.media-left,.media-right,.media-body{display:table-cell;vertical-align:top}.media-middle{vertical-align:middle}.media-bottom{vertical-align:bottom}.media-heading{margin-top:0;margin-bottom:5px}.media-list{padding-left:0;list-style:none}.list-group{padding-left:0;margin-bottom:20px}.list-group-item{position:relative;display:block;padding:10px 15px;margin-bottom:-1px;background-color:#fff;border:1px solid #ddd}.list-group-item:first-child{border-top-left-radius:4px;border-top-right-radius:4px}.list-group-item:last-child{margin-bottom:0;border-bottom-right-radius:4px;border-bottom-left-radius:4px}.list-group-item>.badge{float:right}.list-group-item>.badge+.badge{margin-right:5px}a.list-group-item{color:#555}a.list-group-item .list-group-item-heading{color:#333}a.list-group-item:hover,a.list-group-item:focus{color:#555;text-decoration:none;background-color:#f5f5f5}.list-group-item.disabled,.list-group-item.disabled:hover,.list-group-item.disabled:focus{color:#777;cursor:not-allowed;background-color:#eee}.list-group-item.disabled .list-group-item-heading,.list-group-item.disabled:hover .list-group-item-heading,.list-group-item.disabled:focus .list-group-item-heading{color:inherit}.list-group-item.disabled .list-group-item-text,.list-group-item.disabled:hover .list-group-item-text,.list-group-item.disabled:focus .list-group-item-text{color:#777}.list-group-item.active,.list-group-item.active:hover,.list-group-item.active:focus{z-index:2;color:#fff;background-color:#428bca;border-color:#428bca}.list-group-item.active .list-group-item-heading,.list-group-item.active:hover .list-group-item-heading,.list-group-item.active:focus .list-group-item-heading,.list-group-item.active .list-group-item-heading>small,.list-group-item.active:hover .list-group-item-heading>small,.list-group-item.active:focus .list-group-item-heading>small,.list-group-item.active .list-group-item-heading>.small,.list-group-item.active:hover .list-group-item-heading>.small,.list-group-item.active:focus .list-group-item-heading>.small{color:inherit}.list-group-item.active .list-group-item-text,.list-group-item.active:hover .list-group-item-text,.list-group-item.active:focus .list-group-item-text{color:#e1edf7}.list-group-item-success{color:#3c763d;background-color:#dff0d8}a.list-group-item-success{color:#3c763d}a.list-group-item-success .list-group-item-heading{color:inherit}a.list-group-item-success:hover,a.list-group-item-success:focus{color:#3c763d;background-color:#d0e9c6}a.list-group-item-success.active,a.list-group-item-success.active:hover,a.list-group-item-success.active:focus{color:#fff;background-color:#3c763d;border-color:#3c763d}.list-group-item-info{color:#31708f;background-color:#d9edf7}a.list-group-item-info{color:#31708f}a.list-group-item-info .list-group-item-heading{color:inherit}a.list-group-item-info:hover,a.list-group-item-info:focus{color:#31708f;background-color:#c4e3f3}a.list-group-item-info.active,a.list-group-item-info.active:hover,a.list-group-item-info.active:focus{color:#fff;background-color:#31708f;border-color:#31708f}.list-group-item-warning{color:#8a6d3b;background-color:#fcf8e3}a.list-group-item-warning{color:#8a6d3b}a.list-group-item-warning .list-group-item-heading{color:inherit}a.list-group-item-warning:hover,a.list-group-item-warning:focus{color:#8a6d3b;background-color:#faf2cc}a.list-group-item-warning.active,a.list-group-item-warning.active:hover,a.list-group-item-warning.active:focus{color:#fff;background-color:#8a6d3b;border-color:#8a6d3b}.list-group-item-danger{color:#a94442;background-color:#f2dede}a.list-group-item-danger{color:#a94442}a.list-group-item-danger .list-group-item-heading{color:inherit}a.list-group-item-danger:hover,a.list-group-item-danger:focus{color:#a94442;background-color:#ebcccc}a.list-group-item-danger.active,a.list-group-item-danger.active:hover,a.list-group-item-danger.active:focus{color:#fff;background-color:#a94442;border-color:#a94442}.list-group-item-heading{margin-top:0;margin-bottom:5px}.list-group-item-text{margin-bottom:0;line-height:1.3}.panel{margin-bottom:20px;background-color:#fff;border:1px solid transparent;border-radius:4px;-webkit-box-shadow:0 1px 1px rgba(0,0,0,.05);box-shadow:0 1px 1px rgba(0,0,0,.05)}.panel-body{padding:15px}.panel-heading{padding:10px 15px;border-bottom:1px solid transparent;border-top-left-radius:3px;border-top-right-radius:3px}.panel-heading>.dropdown .dropdown-toggle{color:inherit}.panel-title{margin-top:0;margin-bottom:0;font-size:16px;color:inherit}.panel-title>a{color:inherit}.panel-footer{padding:10px 15px;background-color:#f5f5f5;border-top:1px solid #ddd;border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.list-group,.panel>.panel-collapse>.list-group{margin-bottom:0}.panel>.list-group .list-group-item,.panel>.panel-collapse>.list-group .list-group-item{border-width:1px 0;border-radius:0}.panel>.list-group:first-child .list-group-item:first-child,.panel>.panel-collapse>.list-group:first-child .list-group-item:first-child{border-top:0;border-top-left-radius:3px;border-top-right-radius:3px}.panel>.list-group:last-child .list-group-item:last-child,.panel>.panel-collapse>.list-group:last-child .list-group-item:last-child{border-bottom:0;border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel-heading+.list-group .list-group-item:first-child{border-top-width:0}.list-group+.panel-footer{border-top-width:0}.panel>.table,.panel>.table-responsive>.table,.panel>.panel-collapse>.table{margin-bottom:0}.panel>.table caption,.panel>.table-responsive>.table caption,.panel>.panel-collapse>.table caption{padding-right:15px;padding-left:15px}.panel>.table:first-child,.panel>.table-responsive:first-child>.table:first-child{border-top-left-radius:3px;border-top-right-radius:3px}.panel>.table:first-child>thead:first-child>tr:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child{border-top-left-radius:3px;border-top-right-radius:3px}.panel>.table:first-child>thead:first-child>tr:first-child td:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child td:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child td:first-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child td:first-child,.panel>.table:first-child>thead:first-child>tr:first-child th:first-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child th:first-child,.panel>.table:first-child>tbody:first-child>tr:first-child th:first-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child th:first-child{border-top-left-radius:3px}.panel>.table:first-child>thead:first-child>tr:first-child td:last-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child td:last-child,.panel>.table:first-child>tbody:first-child>tr:first-child td:last-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child td:last-child,.panel>.table:first-child>thead:first-child>tr:first-child th:last-child,.panel>.table-responsive:first-child>.table:first-child>thead:first-child>tr:first-child th:last-child,.panel>.table:first-child>tbody:first-child>tr:first-child th:last-child,.panel>.table-responsive:first-child>.table:first-child>tbody:first-child>tr:first-child th:last-child{border-top-right-radius:3px}.panel>.table:last-child,.panel>.table-responsive:last-child>.table:last-child{border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.table:last-child>tbody:last-child>tr:last-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child{border-bottom-right-radius:3px;border-bottom-left-radius:3px}.panel>.table:last-child>tbody:last-child>tr:last-child td:first-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child td:first-child,.panel>.table:last-child>tfoot:last-child>tr:last-child td:first-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child td:first-child,.panel>.table:last-child>tbody:last-child>tr:last-child th:first-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child th:first-child,.panel>.table:last-child>tfoot:last-child>tr:last-child th:first-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child th:first-child{border-bottom-left-radius:3px}.panel>.table:last-child>tbody:last-child>tr:last-child td:last-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child td:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child td:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child td:last-child,.panel>.table:last-child>tbody:last-child>tr:last-child th:last-child,.panel>.table-responsive:last-child>.table:last-child>tbody:last-child>tr:last-child th:last-child,.panel>.table:last-child>tfoot:last-child>tr:last-child th:last-child,.panel>.table-responsive:last-child>.table:last-child>tfoot:last-child>tr:last-child th:last-child{border-bottom-right-radius:3px}.panel>.panel-body+.table,.panel>.panel-body+.table-responsive,.panel>.table+.panel-body,.panel>.table-responsive+.panel-body{border-top:1px solid #ddd}.panel>.table>tbody:first-child>tr:first-child th,.panel>.table>tbody:first-child>tr:first-child td{border-top:0}.panel>.table-bordered,.panel>.table-responsive>.table-bordered{border:0}.panel>.table-bordered>thead>tr>th:first-child,.panel>.table-responsive>.table-bordered>thead>tr>th:first-child,.panel>.table-bordered>tbody>tr>th:first-child,.panel>.table-responsive>.table-bordered>tbody>tr>th:first-child,.panel>.table-bordered>tfoot>tr>th:first-child,.panel>.table-responsive>.table-bordered>tfoot>tr>th:first-child,.panel>.table-bordered>thead>tr>td:first-child,.panel>.table-responsive>.table-bordered>thead>tr>td:first-child,.panel>.table-bordered>tbody>tr>td:first-child,.panel>.table-responsive>.table-bordered>tbody>tr>td:first-child,.panel>.table-bordered>tfoot>tr>td:first-child,.panel>.table-responsive>.table-bordered>tfoot>tr>td:first-child{border-left:0}.panel>.table-bordered>thead>tr>th:last-child,.panel>.table-responsive>.table-bordered>thead>tr>th:last-child,.panel>.table-bordered>tbody>tr>th:last-child,.panel>.table-responsive>.table-bordered>tbody>tr>th:last-child,.panel>.table-bordered>tfoot>tr>th:last-child,.panel>.table-responsive>.table-bordered>tfoot>tr>th:last-child,.panel>.table-bordered>thead>tr>td:last-child,.panel>.table-responsive>.table-bordered>thead>tr>td:last-child,.panel>.table-bordered>tbody>tr>td:last-child,.panel>.table-responsive>.table-bordered>tbody>tr>td:last-child,.panel>.table-bordered>tfoot>tr>td:last-child,.panel>.table-responsive>.table-bordered>tfoot>tr>td:last-child{border-right:0}.panel>.table-bordered>thead>tr:first-child>td,.panel>.table-responsive>.table-bordered>thead>tr:first-child>td,.panel>.table-bordered>tbody>tr:first-child>td,.panel>.table-responsive>.table-bordered>tbody>tr:first-child>td,.panel>.table-bordered>thead>tr:first-child>th,.panel>.table-responsive>.table-bordered>thead>tr:first-child>th,.panel>.table-bordered>tbody>tr:first-child>th,.panel>.table-responsive>.table-bordered>tbody>tr:first-child>th{border-bottom:0}.panel>.table-bordered>tbody>tr:last-child>td,.panel>.table-responsive>.table-bordered>tbody>tr:last-child>td,.panel>.table-bordered>tfoot>tr:last-child>td,.panel>.table-responsive>.table-bordered>tfoot>tr:last-child>td,.panel>.table-bordered>tbody>tr:last-child>th,.panel>.table-responsive>.table-bordered>tbody>tr:last-child>th,.panel>.table-bordered>tfoot>tr:last-child>th,.panel>.table-responsive>.table-bordered>tfoot>tr:last-child>th{border-bottom:0}.panel>.table-responsive{margin-bottom:0;border:0}.panel-group{margin-bottom:20px}.panel-group .panel{margin-bottom:0;border-radius:4px}.panel-group .panel+.panel{margin-top:5px}.panel-group .panel-heading{border-bottom:0}.panel-group .panel-heading+.panel-collapse>.panel-body,.panel-group .panel-heading+.panel-collapse>.list-group{border-top:1px solid #ddd}.panel-group .panel-footer{border-top:0}.panel-group .panel-footer+.panel-collapse .panel-body{border-bottom:1px solid #ddd}.panel-default{border-color:#ddd}.panel-default>.panel-heading{color:#333;background-color:#f5f5f5;border-color:#ddd}.panel-default>.panel-heading+.panel-collapse>.panel-body{border-top-color:#ddd}.panel-default>.panel-heading .badge{color:#f5f5f5;background-color:#333}.panel-default>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#ddd}.panel-primary{border-color:#428bca}.panel-primary>.panel-heading{color:#fff;background-color:#428bca;border-color:#428bca}.panel-primary>.panel-heading+.panel-collapse>.panel-body{border-top-color:#428bca}.panel-primary>.panel-heading .badge{color:#428bca;background-color:#fff}.panel-primary>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#428bca}.panel-success{border-color:#d6e9c6}.panel-success>.panel-heading{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.panel-success>.panel-heading+.panel-collapse>.panel-body{border-top-color:#d6e9c6}.panel-success>.panel-heading .badge{color:#dff0d8;background-color:#3c763d}.panel-success>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#d6e9c6}.panel-info{border-color:#bce8f1}.panel-info>.panel-heading{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.panel-info>.panel-heading+.panel-collapse>.panel-body{border-top-color:#bce8f1}.panel-info>.panel-heading .badge{color:#d9edf7;background-color:#31708f}.panel-info>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#bce8f1}.panel-warning{border-color:#faebcc}.panel-warning>.panel-heading{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.panel-warning>.panel-heading+.panel-collapse>.panel-body{border-top-color:#faebcc}.panel-warning>.panel-heading .badge{color:#fcf8e3;background-color:#8a6d3b}.panel-warning>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#faebcc}.panel-danger{border-color:#ebccd1}.panel-danger>.panel-heading{color:#a94442;background-color:#f2dede;border-color:#ebccd1}.panel-danger>.panel-heading+.panel-collapse>.panel-body{border-top-color:#ebccd1}.panel-danger>.panel-heading .badge{color:#f2dede;background-color:#a94442}.panel-danger>.panel-footer+.panel-collapse>.panel-body{border-bottom-color:#ebccd1}.embed-responsive{position:relative;display:block;height:0;padding:0;overflow:hidden}.embed-responsive .embed-responsive-item,.embed-responsive iframe,.embed-responsive embed,.embed-responsive object,.embed-responsive video{position:absolute;top:0;bottom:0;left:0;width:100%;height:100%;border:0}.embed-responsive.embed-responsive-16by9{padding-bottom:56.25%}.embed-responsive.embed-responsive-4by3{padding-bottom:75%}.well{min-height:20px;padding:19px;margin-bottom:20px;background-color:#f5f5f5;border:1px solid #e3e3e3;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.05);box-shadow:inset 0 1px 1px rgba(0,0,0,.05)}.well blockquote{border-color:#ddd;border-color:rgba(0,0,0,.15)}.well-lg{padding:24px;border-radius:6px}.well-sm{padding:9px;border-radius:3px}.close{float:right;font-size:21px;font-weight:700;line-height:1;color:#000;text-shadow:0 1px 0 #fff;filter:alpha(opacity=20);opacity:.2}.close:hover,.close:focus{color:#000;text-decoration:none;cursor:pointer;filter:alpha(opacity=50);opacity:.5}button.close{-webkit-appearance:none;padding:0;cursor:pointer;background:0 0;border:0}.modal-open{overflow:hidden}.modal{position:fixed;top:0;right:0;bottom:0;left:0;z-index:1040;display:none;overflow:hidden;-webkit-overflow-scrolling:touch;outline:0}.modal.fade .modal-dialog{-webkit-transition:-webkit-transform .3s ease-out;-o-transition:-o-transform .3s ease-out;transition:transform .3s ease-out;-webkit-transform:translate(0,-25%);-ms-transform:translate(0,-25%);-o-transform:translate(0,-25%);transform:translate(0,-25%)}.modal.in .modal-dialog{-webkit-transform:translate(0,0);-ms-transform:translate(0,0);-o-transform:translate(0,0);transform:translate(0,0)}.modal-open .modal{overflow-x:hidden;overflow-y:auto}.modal-dialog{position:relative;width:auto;margin:10px}.modal-content{position:relative;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #999;border:1px solid rgba(0,0,0,.2);border-radius:6px;outline:0;-webkit-box-shadow:0 3px 9px rgba(0,0,0,.5);box-shadow:0 3px 9px rgba(0,0,0,.5)}.modal-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;background-color:#000}.modal-backdrop.fade{filter:alpha(opacity=0);opacity:0}.modal-backdrop.in{filter:alpha(opacity=50);opacity:.5}.modal-header{min-height:16.43px;padding:15px;border-bottom:1px solid #e5e5e5}.modal-header .close{margin-top:-2px}.modal-title{margin:0;line-height:1.42857143}.modal-body{position:relative;padding:15px}.modal-footer{padding:15px;text-align:right;border-top:1px solid #e5e5e5}.modal-footer .btn+.btn{margin-bottom:0;margin-left:5px}.modal-footer .btn-group .btn+.btn{margin-left:-1px}.modal-footer .btn-block+.btn-block{margin-left:0}.modal-scrollbar-measure{position:absolute;top:-9999px;width:50px;height:50px;overflow:scroll}@media (min-width:768px){.modal-dialog{width:600px;margin:30px auto}.modal-content{-webkit-box-shadow:0 5px 15px rgba(0,0,0,.5);box-shadow:0 5px 15px rgba(0,0,0,.5)}.modal-sm{width:300px}}@media (min-width:992px){.modal-lg{width:900px}}.tooltip{position:absolute;z-index:1070;display:block;font-size:12px;line-height:1.4;visibility:visible;filter:alpha(opacity=0);opacity:0}.tooltip.in{filter:alpha(opacity=90);opacity:.9}.tooltip.top{padding:5px 0;margin-top:-3px}.tooltip.right{padding:0 5px;margin-left:3px}.tooltip.bottom{padding:5px 0;margin-top:3px}.tooltip.left{padding:0 5px;margin-left:-3px}.tooltip-inner{max-width:200px;padding:3px 8px;color:#fff;text-align:center;text-decoration:none;background-color:#000;border-radius:4px}.tooltip-arrow{position:absolute;width:0;height:0;border-color:transparent;border-style:solid}.tooltip.top .tooltip-arrow{bottom:0;left:50%;margin-left:-5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.top-left .tooltip-arrow{bottom:0;left:5px;border-width:5px 5px 0;border-top-color:#000}.tooltip.top-right .tooltip-arrow{right:5px;bottom:0;border-width:5px 5px 0;border-top-color:#000}.tooltip.right .tooltip-arrow{top:50%;left:0;margin-top:-5px;border-width:5px 5px 5px 0;border-right-color:#000}.tooltip.left .tooltip-arrow{top:50%;right:0;margin-top:-5px;border-width:5px 0 5px 5px;border-left-color:#000}.tooltip.bottom .tooltip-arrow{top:0;left:50%;margin-left:-5px;border-width:0 5px 5px;border-bottom-color:#000}.tooltip.bottom-left .tooltip-arrow{top:0;left:5px;border-width:0 5px 5px;border-bottom-color:#000}.tooltip.bottom-right .tooltip-arrow{top:0;right:5px;border-width:0 5px 5px;border-bottom-color:#000}.popover{position:absolute;top:0;left:0;z-index:1060;display:none;max-width:276px;padding:1px;font-size:14px;font-weight:400;line-height:1.42857143;text-align:left;white-space:normal;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.2);border-radius:6px;-webkit-box-shadow:0 5px 10px rgba(0,0,0,.2);box-shadow:0 5px 10px rgba(0,0,0,.2)}.popover.top{margin-top:-10px}.popover.right{margin-left:10px}.popover.bottom{margin-top:10px}.popover.left{margin-left:-10px}.popover-title{padding:8px 14px;margin:0;font-size:14px;background-color:#f7f7f7;border-bottom:1px solid #ebebeb;border-radius:5px 5px 0 0}.popover-content{padding:9px 14px}.popover>.arrow,.popover>.arrow:after{position:absolute;display:block;width:0;height:0;border-color:transparent;border-style:solid}.popover>.arrow{border-width:11px}.popover>.arrow:after{content:"";border-width:10px}.popover.top>.arrow{bottom:-11px;left:50%;margin-left:-11px;border-top-color:#999;border-top-color:rgba(0,0,0,.25);border-bottom-width:0}.popover.top>.arrow:after{bottom:1px;margin-left:-10px;content:" ";border-top-color:#fff;border-bottom-width:0}.popover.right>.arrow{top:50%;left:-11px;margin-top:-11px;border-right-color:#999;border-right-color:rgba(0,0,0,.25);border-left-width:0}.popover.right>.arrow:after{bottom:-10px;left:1px;content:" ";border-right-color:#fff;border-left-width:0}.popover.bottom>.arrow{top:-11px;left:50%;margin-left:-11px;border-top-width:0;border-bottom-color:#999;border-bottom-color:rgba(0,0,0,.25)}.popover.bottom>.arrow:after{top:1px;margin-left:-10px;content:" ";border-top-width:0;border-bottom-color:#fff}.popover.left>.arrow{top:50%;right:-11px;margin-top:-11px;border-right-width:0;border-left-color:#999;border-left-color:rgba(0,0,0,.25)}.popover.left>.arrow:after{right:1px;bottom:-10px;content:" ";border-right-width:0;border-left-color:#fff}.carousel{position:relative}.carousel-inner{position:relative;width:100%;overflow:hidden}.carousel-inner>.item{position:relative;display:none;-webkit-transition:.6s ease-in-out left;-o-transition:.6s ease-in-out left;transition:.6s ease-in-out left}.carousel-inner>.item>img,.carousel-inner>.item>a>img{line-height:1}@media all and (transform-3d),(-webkit-transform-3d){.carousel-inner>.item{-webkit-transition:-webkit-transform .6s ease-in-out;-o-transition:-o-transform .6s ease-in-out;transition:transform .6s ease-in-out;-webkit-backface-visibility:hidden;backface-visibility:hidden;-webkit-perspective:1000;perspective:1000}.carousel-inner>.item.next,.carousel-inner>.item.active.right{left:0;-webkit-transform:translate3d(100%,0,0);transform:translate3d(100%,0,0)}.carousel-inner>.item.prev,.carousel-inner>.item.active.left{left:0;-webkit-transform:translate3d(-100%,0,0);transform:translate3d(-100%,0,0)}.carousel-inner>.item.next.left,.carousel-inner>.item.prev.right,.carousel-inner>.item.active{left:0;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}}.carousel-inner>.active,.carousel-inner>.next,.carousel-inner>.prev{display:block}.carousel-inner>.active{left:0}.carousel-inner>.next,.carousel-inner>.prev{position:absolute;top:0;width:100%}.carousel-inner>.next{left:100%}.carousel-inner>.prev{left:-100%}.carousel-inner>.next.left,.carousel-inner>.prev.right{left:0}.carousel-inner>.active.left{left:-100%}.carousel-inner>.active.right{left:100%}.carousel-control{position:absolute;top:0;bottom:0;left:0;width:15%;font-size:20px;color:#fff;text-align:center;text-shadow:0 1px 2px rgba(0,0,0,.6);filter:alpha(opacity=50);opacity:.5}.carousel-control.left{background-image:-webkit-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);background-image:-o-linear-gradient(left,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);background-image:-webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.5)),to(rgba(0,0,0,.0001)));background-image:linear-gradient(to right,rgba(0,0,0,.5) 0,rgba(0,0,0,.0001) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#00000000', GradientType=1);background-repeat:repeat-x}.carousel-control.right{right:0;left:auto;background-image:-webkit-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);background-image:-o-linear-gradient(left,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);background-image:-webkit-gradient(linear,left top,right top,from(rgba(0,0,0,.0001)),to(rgba(0,0,0,.5)));background-image:linear-gradient(to right,rgba(0,0,0,.0001) 0,rgba(0,0,0,.5) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#80000000', GradientType=1);background-repeat:repeat-x}.carousel-control:hover,.carousel-control:focus{color:#fff;text-decoration:none;filter:alpha(opacity=90);outline:0;opacity:.9}.carousel-control .icon-prev,.carousel-control .icon-next,.carousel-control .glyphicon-chevron-left,.carousel-control .glyphicon-chevron-right{position:absolute;top:50%;z-index:5;display:inline-block}.carousel-control .icon-prev,.carousel-control .glyphicon-chevron-left{left:50%;margin-left:-10px}.carousel-control .icon-next,.carousel-control .glyphicon-chevron-right{right:50%;margin-right:-10px}.carousel-control .icon-prev,.carousel-control .icon-next{width:20px;height:20px;margin-top:-10px;font-family:serif}.carousel-control .icon-prev:before{content:'\2039'}.carousel-control .icon-next:before{content:'\203a'}.carousel-indicators{position:absolute;bottom:10px;left:50%;z-index:15;width:60%;padding-left:0;margin-left:-30%;text-align:center;list-style:none}.carousel-indicators li{display:inline-block;width:10px;height:10px;margin:1px;text-indent:-999px;cursor:pointer;background-color:#000 \9;background-color:rgba(0,0,0,0);border:1px solid #fff;border-radius:10px}.carousel-indicators .active{width:12px;height:12px;margin:0;background-color:#fff}.carousel-caption{position:absolute;right:15%;bottom:20px;left:15%;z-index:10;padding-top:20px;padding-bottom:20px;color:#fff;text-align:center;text-shadow:0 1px 2px rgba(0,0,0,.6)}.carousel-caption .btn{text-shadow:none}@media screen and (min-width:768px){.carousel-control .glyphicon-chevron-left,.carousel-control .glyphicon-chevron-right,.carousel-control .icon-prev,.carousel-control .icon-next{width:30px;height:30px;margin-top:-15px;font-size:30px}.carousel-control .glyphicon-chevron-left,.carousel-control .icon-prev{margin-left:-15px}.carousel-control .glyphicon-chevron-right,.carousel-control .icon-next{margin-right:-15px}.carousel-caption{right:20%;left:20%;padding-bottom:30px}.carousel-indicators{bottom:20px}}.clearfix:before,.clearfix:after,.dl-horizontal dd:before,.dl-horizontal dd:after,.container:before,.container:after,.container-fluid:before,.container-fluid:after,.row:before,.row:after,.form-horizontal .form-group:before,.form-horizontal .form-group:after,.btn-toolbar:before,.btn-toolbar:after,.btn-group-vertical>.btn-group:before,.btn-group-vertical>.btn-group:after,.nav:before,.nav:after,.navbar:before,.navbar:after,.navbar-header:before,.navbar-header:after,.navbar-collapse:before,.navbar-collapse:after,.pager:before,.pager:after,.panel-body:before,.panel-body:after,.modal-footer:before,.modal-footer:after{display:table;content:" "}.clearfix:after,.dl-horizontal dd:after,.container:after,.container-fluid:after,.row:after,.form-horizontal .form-group:after,.btn-toolbar:after,.btn-group-vertical>.btn-group:after,.nav:after,.navbar:after,.navbar-header:after,.navbar-collapse:after,.pager:after,.panel-body:after,.modal-footer:after{clear:both}.center-block{display:block;margin-right:auto;margin-left:auto}.pull-right{float:right!important}.pull-left{float:left!important}.hide{display:none!important}.show{display:block!important}.invisible{visibility:hidden}.text-hide{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0}.hidden{display:none!important;visibility:hidden!important}.affix{position:fixed}@-ms-viewport{width:device-width}.visible-xs,.visible-sm,.visible-md,.visible-lg{display:none!important}.visible-xs-block,.visible-xs-inline,.visible-xs-inline-block,.visible-sm-block,.visible-sm-inline,.visible-sm-inline-block,.visible-md-block,.visible-md-inline,.visible-md-inline-block,.visible-lg-block,.visible-lg-inline,.visible-lg-inline-block{display:none!important}@media (max-width:767px){.visible-xs{display:block!important}table.visible-xs{display:table}tr.visible-xs{display:table-row!important}th.visible-xs,td.visible-xs{display:table-cell!important}}@media (max-width:767px){.visible-xs-block{display:block!important}}@media (max-width:767px){.visible-xs-inline{display:inline!important}}@media (max-width:767px){.visible-xs-inline-block{display:inline-block!important}}@media (min-width:768px) and (max-width:991px){.visible-sm{display:block!important}table.visible-sm{display:table}tr.visible-sm{display:table-row!important}th.visible-sm,td.visible-sm{display:table-cell!important}}@media (min-width:768px) and (max-width:991px){.visible-sm-block{display:block!important}}@media (min-width:768px) and (max-width:991px){.visible-sm-inline{display:inline!important}}@media (min-width:768px) and (max-width:991px){.visible-sm-inline-block{display:inline-block!important}}@media (min-width:992px) and (max-width:1199px){.visible-md{display:block!important}table.visible-md{display:table}tr.visible-md{display:table-row!important}th.visible-md,td.visible-md{display:table-cell!important}}@media (min-width:992px) and (max-width:1199px){.visible-md-block{display:block!important}}@media (min-width:992px) and (max-width:1199px){.visible-md-inline{display:inline!important}}@media (min-width:992px) and (max-width:1199px){.visible-md-inline-block{display:inline-block!important}}@media (min-width:1200px){.visible-lg{display:block!important}table.visible-lg{display:table}tr.visible-lg{display:table-row!important}th.visible-lg,td.visible-lg{display:table-cell!important}}@media (min-width:1200px){.visible-lg-block{display:block!important}}@media (min-width:1200px){.visible-lg-inline{display:inline!important}}@media (min-width:1200px){.visible-lg-inline-block{display:inline-block!important}}@media (max-width:767px){.hidden-xs{display:none!important}}@media (min-width:768px) and (max-width:991px){.hidden-sm{display:none!important}}@media (min-width:992px) and (max-width:1199px){.hidden-md{display:none!important}}@media (min-width:1200px){.hidden-lg{display:none!important}}.visible-print{display:none!important}@media print{.visible-print{display:block!important}table.visible-print{display:table}tr.visible-print{display:table-row!important}th.visible-print,td.visible-print{display:table-cell!important}}.visible-print-block{display:none!important}@media print{.visible-print-block{display:block!important}}.visible-print-inline{display:none!important}@media print{.visible-print-inline{display:inline!important}}.visible-print-inline-block{display:none!important}@media print{.visible-print-inline-block{display:inline-block!important}}@media print{.hidden-print{display:none!important}} \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/hyrise/web-interface/hyrise-connector.js b/cpp/3rd_party/sql-parser/hyrise/web-interface/hyrise-connector.js new file mode 100644 index 0000000000..8d0bed8c32 --- /dev/null +++ b/cpp/3rd_party/sql-parser/hyrise/web-interface/hyrise-connector.js @@ -0,0 +1,129 @@ + + +var HyriseConnector = function(endpointUrl) { + this._endpointUrl = endpointUrl; +} + +HyriseConnector.prototype.executeSQL = function(query, callback, errorCallback) { + var url = encodeURI(this._endpointUrl); + var self = this; + + jQuery.ajax({ + type: "POST", + url: url, + dataType: 'json', + data: { + performance: true, + sql: query + }, + success: function(result) { + if (typeof result.real_size === "undefined") { + result.real_size = 0; + result.rows = []; + result.header = []; + if (!result.performanceData) result.performanceData = []; + } + + self._formatPerformanceData(result); + callback(result); + }, + error: errorCallback + }); + + return this; +}; + + +HyriseConnector.prototype._formatPerformanceData = function(object) { + var performanceData = object.performanceData; + + var totalTime = 0; + var queryTaskTime = 0; + var parseTime = 0; + + $.each(performanceData, function(i, data) { + data.time_ms = data.endTime - data.startTime; + + totalTime += data.time_ms; + if (data.name === 'SQLQueryTask') queryTaskTime += data.time_ms; + if (data.name === 'RequestParseTask') parseTime += data.time_ms; + }); + + object.performanceData = { + totalTime: totalTime, + queryTaskTime: queryTaskTime, + parseTime: parseTime, + operators: performanceData, + } +}; + + +HyriseConnector.prototype.benchmarkSQL = function(query, numRuns, callback) { + var self = this; + + function __aggregateData(allData) { + var result = { + totalTime: 0, + queryTaskTime: 0, + parseTime: 0, + numRuns: allData.length, + operators: [] + }; + + var operatorMap = {}; + + $.each(allData, function(i, run) { + var perfData = run.performanceData; + result.totalTime += perfData.totalTime; + result.queryTaskTime += perfData.queryTaskTime; + result.parseTime += perfData.parseTime; + + $.each(perfData.operators, function(i, data) { + if (!(data.id in operatorMap)) { + operatorMap[data.id] = data; + } else { + operatorMap[data.id].duration += data.duration; + operatorMap[data.id].startTime += data.startTime; + operatorMap[data.id].endTime += data.endTime; + operatorMap[data.id].time_ms += data.time_ms; + } + }); + }); + + + // Calc average and Transform into array + result.totalTime /= result.numRuns; + result.queryTaskTime /= result.numRuns; + result.parseTime /= result.numRuns; + + $.each(operatorMap, function(id, data) { + data.duration /= result.numRuns; + data.startTime /= result.numRuns; + data.time_ms /= result.numRuns; + result.operators.push(data); + }); + + callback({ + performanceData: result + }); + } + + var allData = []; + var num_completed = 0; + + function __run() { + self.executeSQL(query, function(result) { + allData.push(result); + + // Run again or return aggregated Data + num_completed++; + if (num_completed == numRuns) __aggregateData(allData); + }); + } + + for (var i = 0; i < numRuns; ++i) { + __run(); + } + + __run(); +}; \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/hyrise/web-interface/index.html b/cpp/3rd_party/sql-parser/hyrise/web-interface/index.html new file mode 100644 index 0000000000..dd1f8435ed --- /dev/null +++ b/cpp/3rd_party/sql-parser/hyrise/web-interface/index.html @@ -0,0 +1,95 @@ + + + + + + + + + Hyrise SQL Frontend + + +
+
+
+ +
+
+ +
+
+ + +
+
+ +
+ + +
+
+ +
+
+ + +
+
+ +
+
+
+ # times + + + + +
+
+
+ +
+ + + + +
+ +
+
+ +

Performance Data

+

+ + + + + + + + + + + + + +
IDNameDurationTime (ms)Start TimeEnd Time
+
+
+ +
+
+ +

Results (first 100 rows)

+ +
+
+
+ +
+
+
+ + \ No newline at end of file diff --git a/cpp/3rd_party/sql-parser/hyrise/web-interface/jquery-1.11.1.min.js b/cpp/3rd_party/sql-parser/hyrise/web-interface/jquery-1.11.1.min.js new file mode 100644 index 0000000000..ab28a24729 --- /dev/null +++ b/cpp/3rd_party/sql-parser/hyrise/web-interface/jquery-1.11.1.min.js @@ -0,0 +1,4 @@ +/*! jQuery v1.11.1 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license */ +!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){var c=[],d=c.slice,e=c.concat,f=c.push,g=c.indexOf,h={},i=h.toString,j=h.hasOwnProperty,k={},l="1.11.1",m=function(a,b){return new m.fn.init(a,b)},n=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,o=/^-ms-/,p=/-([\da-z])/gi,q=function(a,b){return b.toUpperCase()};m.fn=m.prototype={jquery:l,constructor:m,selector:"",length:0,toArray:function(){return d.call(this)},get:function(a){return null!=a?0>a?this[a+this.length]:this[a]:d.call(this)},pushStack:function(a){var b=m.merge(this.constructor(),a);return b.prevObject=this,b.context=this.context,b},each:function(a,b){return m.each(this,a,b)},map:function(a){return this.pushStack(m.map(this,function(b,c){return a.call(b,c,b)}))},slice:function(){return this.pushStack(d.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(a){var b=this.length,c=+a+(0>a?b:0);return this.pushStack(c>=0&&b>c?[this[c]]:[])},end:function(){return this.prevObject||this.constructor(null)},push:f,sort:c.sort,splice:c.splice},m.extend=m.fn.extend=function(){var a,b,c,d,e,f,g=arguments[0]||{},h=1,i=arguments.length,j=!1;for("boolean"==typeof g&&(j=g,g=arguments[h]||{},h++),"object"==typeof g||m.isFunction(g)||(g={}),h===i&&(g=this,h--);i>h;h++)if(null!=(e=arguments[h]))for(d in e)a=g[d],c=e[d],g!==c&&(j&&c&&(m.isPlainObject(c)||(b=m.isArray(c)))?(b?(b=!1,f=a&&m.isArray(a)?a:[]):f=a&&m.isPlainObject(a)?a:{},g[d]=m.extend(j,f,c)):void 0!==c&&(g[d]=c));return g},m.extend({expando:"jQuery"+(l+Math.random()).replace(/\D/g,""),isReady:!0,error:function(a){throw new Error(a)},noop:function(){},isFunction:function(a){return"function"===m.type(a)},isArray:Array.isArray||function(a){return"array"===m.type(a)},isWindow:function(a){return null!=a&&a==a.window},isNumeric:function(a){return!m.isArray(a)&&a-parseFloat(a)>=0},isEmptyObject:function(a){var b;for(b in a)return!1;return!0},isPlainObject:function(a){var b;if(!a||"object"!==m.type(a)||a.nodeType||m.isWindow(a))return!1;try{if(a.constructor&&!j.call(a,"constructor")&&!j.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}if(k.ownLast)for(b in a)return j.call(a,b);for(b in a);return void 0===b||j.call(a,b)},type:function(a){return null==a?a+"":"object"==typeof a||"function"==typeof a?h[i.call(a)]||"object":typeof a},globalEval:function(b){b&&m.trim(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(o,"ms-").replace(p,q)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toLowerCase()===b.toLowerCase()},each:function(a,b,c){var d,e=0,f=a.length,g=r(a);if(c){if(g){for(;f>e;e++)if(d=b.apply(a[e],c),d===!1)break}else for(e in a)if(d=b.apply(a[e],c),d===!1)break}else if(g){for(;f>e;e++)if(d=b.call(a[e],e,a[e]),d===!1)break}else for(e in a)if(d=b.call(a[e],e,a[e]),d===!1)break;return a},trim:function(a){return null==a?"":(a+"").replace(n,"")},makeArray:function(a,b){var c=b||[];return null!=a&&(r(Object(a))?m.merge(c,"string"==typeof a?[a]:a):f.call(c,a)),c},inArray:function(a,b,c){var d;if(b){if(g)return g.call(b,a,c);for(d=b.length,c=c?0>c?Math.max(0,d+c):c:0;d>c;c++)if(c in b&&b[c]===a)return c}return-1},merge:function(a,b){var c=+b.length,d=0,e=a.length;while(c>d)a[e++]=b[d++];if(c!==c)while(void 0!==b[d])a[e++]=b[d++];return a.length=e,a},grep:function(a,b,c){for(var d,e=[],f=0,g=a.length,h=!c;g>f;f++)d=!b(a[f],f),d!==h&&e.push(a[f]);return e},map:function(a,b,c){var d,f=0,g=a.length,h=r(a),i=[];if(h)for(;g>f;f++)d=b(a[f],f,c),null!=d&&i.push(d);else for(f in a)d=b(a[f],f,c),null!=d&&i.push(d);return e.apply([],i)},guid:1,proxy:function(a,b){var c,e,f;return"string"==typeof b&&(f=a[b],b=a,a=f),m.isFunction(a)?(c=d.call(arguments,2),e=function(){return a.apply(b||this,c.concat(d.call(arguments)))},e.guid=a.guid=a.guid||m.guid++,e):void 0},now:function(){return+new Date},support:k}),m.each("Boolean Number String Function Array Date RegExp Object Error".split(" "),function(a,b){h["[object "+b+"]"]=b.toLowerCase()});function r(a){var b=a.length,c=m.type(a);return"function"===c||m.isWindow(a)?!1:1===a.nodeType&&b?!0:"array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a}var s=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u="sizzle"+-new Date,v=a.document,w=0,x=0,y=gb(),z=gb(),A=gb(),B=function(a,b){return a===b&&(l=!0),0},C="undefined",D=1<<31,E={}.hasOwnProperty,F=[],G=F.pop,H=F.push,I=F.push,J=F.slice,K=F.indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(this[b]===a)return b;return-1},L="checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",M="[\\x20\\t\\r\\n\\f]",N="(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+",O=N.replace("w","w#"),P="\\["+M+"*("+N+")(?:"+M+"*([*^$|!~]?=)"+M+"*(?:'((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\"|("+O+"))|)"+M+"*\\]",Q=":("+N+")(?:\\((('((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\")|((?:\\\\.|[^\\\\()[\\]]|"+P+")*)|.*)\\)|)",R=new RegExp("^"+M+"+|((?:^|[^\\\\])(?:\\\\.)*)"+M+"+$","g"),S=new RegExp("^"+M+"*,"+M+"*"),T=new RegExp("^"+M+"*([>+~]|"+M+")"+M+"*"),U=new RegExp("="+M+"*([^\\]'\"]*?)"+M+"*\\]","g"),V=new RegExp(Q),W=new RegExp("^"+O+"$"),X={ID:new RegExp("^#("+N+")"),CLASS:new RegExp("^\\.("+N+")"),TAG:new RegExp("^("+N.replace("w","w*")+")"),ATTR:new RegExp("^"+P),PSEUDO:new RegExp("^"+Q),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+L+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},Y=/^(?:input|select|textarea|button)$/i,Z=/^h\d$/i,$=/^[^{]+\{\s*\[native \w/,_=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,ab=/[+~]/,bb=/'|\\/g,cb=new RegExp("\\\\([\\da-f]{1,6}"+M+"?|("+M+")|.)","ig"),db=function(a,b,c){var d="0x"+b-65536;return d!==d||c?b:0>d?String.fromCharCode(d+65536):String.fromCharCode(d>>10|55296,1023&d|56320)};try{I.apply(F=J.call(v.childNodes),v.childNodes),F[v.childNodes.length].nodeType}catch(eb){I={apply:F.length?function(a,b){H.apply(a,J.call(b))}:function(a,b){var c=a.length,d=0;while(a[c++]=b[d++]);a.length=c-1}}}function fb(a,b,d,e){var f,h,j,k,l,o,r,s,w,x;if((b?b.ownerDocument||b:v)!==n&&m(b),b=b||n,d=d||[],!a||"string"!=typeof a)return d;if(1!==(k=b.nodeType)&&9!==k)return[];if(p&&!e){if(f=_.exec(a))if(j=f[1]){if(9===k){if(h=b.getElementById(j),!h||!h.parentNode)return d;if(h.id===j)return d.push(h),d}else if(b.ownerDocument&&(h=b.ownerDocument.getElementById(j))&&t(b,h)&&h.id===j)return d.push(h),d}else{if(f[2])return I.apply(d,b.getElementsByTagName(a)),d;if((j=f[3])&&c.getElementsByClassName&&b.getElementsByClassName)return I.apply(d,b.getElementsByClassName(j)),d}if(c.qsa&&(!q||!q.test(a))){if(s=r=u,w=b,x=9===k&&a,1===k&&"object"!==b.nodeName.toLowerCase()){o=g(a),(r=b.getAttribute("id"))?s=r.replace(bb,"\\$&"):b.setAttribute("id",s),s="[id='"+s+"'] ",l=o.length;while(l--)o[l]=s+qb(o[l]);w=ab.test(a)&&ob(b.parentNode)||b,x=o.join(",")}if(x)try{return I.apply(d,w.querySelectorAll(x)),d}catch(y){}finally{r||b.removeAttribute("id")}}}return i(a.replace(R,"$1"),b,d,e)}function gb(){var a=[];function b(c,e){return a.push(c+" ")>d.cacheLength&&delete b[a.shift()],b[c+" "]=e}return b}function hb(a){return a[u]=!0,a}function ib(a){var b=n.createElement("div");try{return!!a(b)}catch(c){return!1}finally{b.parentNode&&b.parentNode.removeChild(b),b=null}}function jb(a,b){var c=a.split("|"),e=a.length;while(e--)d.attrHandle[c[e]]=b}function kb(a,b){var c=b&&a,d=c&&1===a.nodeType&&1===b.nodeType&&(~b.sourceIndex||D)-(~a.sourceIndex||D);if(d)return d;if(c)while(c=c.nextSibling)if(c===b)return-1;return a?1:-1}function lb(a){return function(b){var c=b.nodeName.toLowerCase();return"input"===c&&b.type===a}}function mb(a){return function(b){var c=b.nodeName.toLowerCase();return("input"===c||"button"===c)&&b.type===a}}function nb(a){return hb(function(b){return b=+b,hb(function(c,d){var e,f=a([],c.length,b),g=f.length;while(g--)c[e=f[g]]&&(c[e]=!(d[e]=c[e]))})})}function ob(a){return a&&typeof a.getElementsByTagName!==C&&a}c=fb.support={},f=fb.isXML=function(a){var b=a&&(a.ownerDocument||a).documentElement;return b?"HTML"!==b.nodeName:!1},m=fb.setDocument=function(a){var b,e=a?a.ownerDocument||a:v,g=e.defaultView;return e!==n&&9===e.nodeType&&e.documentElement?(n=e,o=e.documentElement,p=!f(e),g&&g!==g.top&&(g.addEventListener?g.addEventListener("unload",function(){m()},!1):g.attachEvent&&g.attachEvent("onunload",function(){m()})),c.attributes=ib(function(a){return a.className="i",!a.getAttribute("className")}),c.getElementsByTagName=ib(function(a){return a.appendChild(e.createComment("")),!a.getElementsByTagName("*").length}),c.getElementsByClassName=$.test(e.getElementsByClassName)&&ib(function(a){return a.innerHTML="
",a.firstChild.className="i",2===a.getElementsByClassName("i").length}),c.getById=ib(function(a){return o.appendChild(a).id=u,!e.getElementsByName||!e.getElementsByName(u).length}),c.getById?(d.find.ID=function(a,b){if(typeof b.getElementById!==C&&p){var c=b.getElementById(a);return c&&c.parentNode?[c]:[]}},d.filter.ID=function(a){var b=a.replace(cb,db);return function(a){return a.getAttribute("id")===b}}):(delete d.find.ID,d.filter.ID=function(a){var b=a.replace(cb,db);return function(a){var c=typeof a.getAttributeNode!==C&&a.getAttributeNode("id");return c&&c.value===b}}),d.find.TAG=c.getElementsByTagName?function(a,b){return typeof b.getElementsByTagName!==C?b.getElementsByTagName(a):void 0}:function(a,b){var c,d=[],e=0,f=b.getElementsByTagName(a);if("*"===a){while(c=f[e++])1===c.nodeType&&d.push(c);return d}return f},d.find.CLASS=c.getElementsByClassName&&function(a,b){return typeof b.getElementsByClassName!==C&&p?b.getElementsByClassName(a):void 0},r=[],q=[],(c.qsa=$.test(e.querySelectorAll))&&(ib(function(a){a.innerHTML="",a.querySelectorAll("[msallowclip^='']").length&&q.push("[*^$]="+M+"*(?:''|\"\")"),a.querySelectorAll("[selected]").length||q.push("\\["+M+"*(?:value|"+L+")"),a.querySelectorAll(":checked").length||q.push(":checked")}),ib(function(a){var b=e.createElement("input");b.setAttribute("type","hidden"),a.appendChild(b).setAttribute("name","D"),a.querySelectorAll("[name=d]").length&&q.push("name"+M+"*[*^$|!~]?="),a.querySelectorAll(":enabled").length||q.push(":enabled",":disabled"),a.querySelectorAll("*,:x"),q.push(",.*:")})),(c.matchesSelector=$.test(s=o.matches||o.webkitMatchesSelector||o.mozMatchesSelector||o.oMatchesSelector||o.msMatchesSelector))&&ib(function(a){c.disconnectedMatch=s.call(a,"div"),s.call(a,"[s!='']:x"),r.push("!=",Q)}),q=q.length&&new RegExp(q.join("|")),r=r.length&&new RegExp(r.join("|")),b=$.test(o.compareDocumentPosition),t=b||$.test(o.contains)?function(a,b){var c=9===a.nodeType?a.documentElement:a,d=b&&b.parentNode;return a===d||!(!d||1!==d.nodeType||!(c.contains?c.contains(d):a.compareDocumentPosition&&16&a.compareDocumentPosition(d)))}:function(a,b){if(b)while(b=b.parentNode)if(b===a)return!0;return!1},B=b?function(a,b){if(a===b)return l=!0,0;var d=!a.compareDocumentPosition-!b.compareDocumentPosition;return d?d:(d=(a.ownerDocument||a)===(b.ownerDocument||b)?a.compareDocumentPosition(b):1,1&d||!c.sortDetached&&b.compareDocumentPosition(a)===d?a===e||a.ownerDocument===v&&t(v,a)?-1:b===e||b.ownerDocument===v&&t(v,b)?1:k?K.call(k,a)-K.call(k,b):0:4&d?-1:1)}:function(a,b){if(a===b)return l=!0,0;var c,d=0,f=a.parentNode,g=b.parentNode,h=[a],i=[b];if(!f||!g)return a===e?-1:b===e?1:f?-1:g?1:k?K.call(k,a)-K.call(k,b):0;if(f===g)return kb(a,b);c=a;while(c=c.parentNode)h.unshift(c);c=b;while(c=c.parentNode)i.unshift(c);while(h[d]===i[d])d++;return d?kb(h[d],i[d]):h[d]===v?-1:i[d]===v?1:0},e):n},fb.matches=function(a,b){return fb(a,null,null,b)},fb.matchesSelector=function(a,b){if((a.ownerDocument||a)!==n&&m(a),b=b.replace(U,"='$1']"),!(!c.matchesSelector||!p||r&&r.test(b)||q&&q.test(b)))try{var d=s.call(a,b);if(d||c.disconnectedMatch||a.document&&11!==a.document.nodeType)return d}catch(e){}return fb(b,n,null,[a]).length>0},fb.contains=function(a,b){return(a.ownerDocument||a)!==n&&m(a),t(a,b)},fb.attr=function(a,b){(a.ownerDocument||a)!==n&&m(a);var e=d.attrHandle[b.toLowerCase()],f=e&&E.call(d.attrHandle,b.toLowerCase())?e(a,b,!p):void 0;return void 0!==f?f:c.attributes||!p?a.getAttribute(b):(f=a.getAttributeNode(b))&&f.specified?f.value:null},fb.error=function(a){throw new Error("Syntax error, unrecognized expression: "+a)},fb.uniqueSort=function(a){var b,d=[],e=0,f=0;if(l=!c.detectDuplicates,k=!c.sortStable&&a.slice(0),a.sort(B),l){while(b=a[f++])b===a[f]&&(e=d.push(f));while(e--)a.splice(d[e],1)}return k=null,a},e=fb.getText=function(a){var b,c="",d=0,f=a.nodeType;if(f){if(1===f||9===f||11===f){if("string"==typeof a.textContent)return a.textContent;for(a=a.firstChild;a;a=a.nextSibling)c+=e(a)}else if(3===f||4===f)return a.nodeValue}else while(b=a[d++])c+=e(b);return c},d=fb.selectors={cacheLength:50,createPseudo:hb,match:X,attrHandle:{},find:{},relative:{">":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(a){return a[1]=a[1].replace(cb,db),a[3]=(a[3]||a[4]||a[5]||"").replace(cb,db),"~="===a[2]&&(a[3]=" "+a[3]+" "),a.slice(0,4)},CHILD:function(a){return a[1]=a[1].toLowerCase(),"nth"===a[1].slice(0,3)?(a[3]||fb.error(a[0]),a[4]=+(a[4]?a[5]+(a[6]||1):2*("even"===a[3]||"odd"===a[3])),a[5]=+(a[7]+a[8]||"odd"===a[3])):a[3]&&fb.error(a[0]),a},PSEUDO:function(a){var b,c=!a[6]&&a[2];return X.CHILD.test(a[0])?null:(a[3]?a[2]=a[4]||a[5]||"":c&&V.test(c)&&(b=g(c,!0))&&(b=c.indexOf(")",c.length-b)-c.length)&&(a[0]=a[0].slice(0,b),a[2]=c.slice(0,b)),a.slice(0,3))}},filter:{TAG:function(a){var b=a.replace(cb,db).toLowerCase();return"*"===a?function(){return!0}:function(a){return a.nodeName&&a.nodeName.toLowerCase()===b}},CLASS:function(a){var b=y[a+" "];return b||(b=new RegExp("(^|"+M+")"+a+"("+M+"|$)"))&&y(a,function(a){return b.test("string"==typeof a.className&&a.className||typeof a.getAttribute!==C&&a.getAttribute("class")||"")})},ATTR:function(a,b,c){return function(d){var e=fb.attr(d,a);return null==e?"!="===b:b?(e+="","="===b?e===c:"!="===b?e!==c:"^="===b?c&&0===e.indexOf(c):"*="===b?c&&e.indexOf(c)>-1:"$="===b?c&&e.slice(-c.length)===c:"~="===b?(" "+e+" ").indexOf(c)>-1:"|="===b?e===c||e.slice(0,c.length+1)===c+"-":!1):!0}},CHILD:function(a,b,c,d,e){var f="nth"!==a.slice(0,3),g="last"!==a.slice(-4),h="of-type"===b;return 1===d&&0===e?function(a){return!!a.parentNode}:function(b,c,i){var j,k,l,m,n,o,p=f!==g?"nextSibling":"previousSibling",q=b.parentNode,r=h&&b.nodeName.toLowerCase(),s=!i&&!h;if(q){if(f){while(p){l=b;while(l=l[p])if(h?l.nodeName.toLowerCase()===r:1===l.nodeType)return!1;o=p="only"===a&&!o&&"nextSibling"}return!0}if(o=[g?q.firstChild:q.lastChild],g&&s){k=q[u]||(q[u]={}),j=k[a]||[],n=j[0]===w&&j[1],m=j[0]===w&&j[2],l=n&&q.childNodes[n];while(l=++n&&l&&l[p]||(m=n=0)||o.pop())if(1===l.nodeType&&++m&&l===b){k[a]=[w,n,m];break}}else if(s&&(j=(b[u]||(b[u]={}))[a])&&j[0]===w)m=j[1];else while(l=++n&&l&&l[p]||(m=n=0)||o.pop())if((h?l.nodeName.toLowerCase()===r:1===l.nodeType)&&++m&&(s&&((l[u]||(l[u]={}))[a]=[w,m]),l===b))break;return m-=e,m===d||m%d===0&&m/d>=0}}},PSEUDO:function(a,b){var c,e=d.pseudos[a]||d.setFilters[a.toLowerCase()]||fb.error("unsupported pseudo: "+a);return e[u]?e(b):e.length>1?(c=[a,a,"",b],d.setFilters.hasOwnProperty(a.toLowerCase())?hb(function(a,c){var d,f=e(a,b),g=f.length;while(g--)d=K.call(a,f[g]),a[d]=!(c[d]=f[g])}):function(a){return e(a,0,c)}):e}},pseudos:{not:hb(function(a){var b=[],c=[],d=h(a.replace(R,"$1"));return d[u]?hb(function(a,b,c,e){var f,g=d(a,null,e,[]),h=a.length;while(h--)(f=g[h])&&(a[h]=!(b[h]=f))}):function(a,e,f){return b[0]=a,d(b,null,f,c),!c.pop()}}),has:hb(function(a){return function(b){return fb(a,b).length>0}}),contains:hb(function(a){return function(b){return(b.textContent||b.innerText||e(b)).indexOf(a)>-1}}),lang:hb(function(a){return W.test(a||"")||fb.error("unsupported lang: "+a),a=a.replace(cb,db).toLowerCase(),function(b){var c;do if(c=p?b.lang:b.getAttribute("xml:lang")||b.getAttribute("lang"))return c=c.toLowerCase(),c===a||0===c.indexOf(a+"-");while((b=b.parentNode)&&1===b.nodeType);return!1}}),target:function(b){var c=a.location&&a.location.hash;return c&&c.slice(1)===b.id},root:function(a){return a===o},focus:function(a){return a===n.activeElement&&(!n.hasFocus||n.hasFocus())&&!!(a.type||a.href||~a.tabIndex)},enabled:function(a){return a.disabled===!1},disabled:function(a){return a.disabled===!0},checked:function(a){var b=a.nodeName.toLowerCase();return"input"===b&&!!a.checked||"option"===b&&!!a.selected},selected:function(a){return a.parentNode&&a.parentNode.selectedIndex,a.selected===!0},empty:function(a){for(a=a.firstChild;a;a=a.nextSibling)if(a.nodeType<6)return!1;return!0},parent:function(a){return!d.pseudos.empty(a)},header:function(a){return Z.test(a.nodeName)},input:function(a){return Y.test(a.nodeName)},button:function(a){var b=a.nodeName.toLowerCase();return"input"===b&&"button"===a.type||"button"===b},text:function(a){var b;return"input"===a.nodeName.toLowerCase()&&"text"===a.type&&(null==(b=a.getAttribute("type"))||"text"===b.toLowerCase())},first:nb(function(){return[0]}),last:nb(function(a,b){return[b-1]}),eq:nb(function(a,b,c){return[0>c?c+b:c]}),even:nb(function(a,b){for(var c=0;b>c;c+=2)a.push(c);return a}),odd:nb(function(a,b){for(var c=1;b>c;c+=2)a.push(c);return a}),lt:nb(function(a,b,c){for(var d=0>c?c+b:c;--d>=0;)a.push(d);return a}),gt:nb(function(a,b,c){for(var d=0>c?c+b:c;++db;b++)d+=a[b].value;return d}function rb(a,b,c){var d=b.dir,e=c&&"parentNode"===d,f=x++;return b.first?function(b,c,f){while(b=b[d])if(1===b.nodeType||e)return a(b,c,f)}:function(b,c,g){var h,i,j=[w,f];if(g){while(b=b[d])if((1===b.nodeType||e)&&a(b,c,g))return!0}else while(b=b[d])if(1===b.nodeType||e){if(i=b[u]||(b[u]={}),(h=i[d])&&h[0]===w&&h[1]===f)return j[2]=h[2];if(i[d]=j,j[2]=a(b,c,g))return!0}}}function sb(a){return a.length>1?function(b,c,d){var e=a.length;while(e--)if(!a[e](b,c,d))return!1;return!0}:a[0]}function tb(a,b,c){for(var d=0,e=b.length;e>d;d++)fb(a,b[d],c);return c}function ub(a,b,c,d,e){for(var f,g=[],h=0,i=a.length,j=null!=b;i>h;h++)(f=a[h])&&(!c||c(f,d,e))&&(g.push(f),j&&b.push(h));return g}function vb(a,b,c,d,e,f){return d&&!d[u]&&(d=vb(d)),e&&!e[u]&&(e=vb(e,f)),hb(function(f,g,h,i){var j,k,l,m=[],n=[],o=g.length,p=f||tb(b||"*",h.nodeType?[h]:h,[]),q=!a||!f&&b?p:ub(p,m,a,h,i),r=c?e||(f?a:o||d)?[]:g:q;if(c&&c(q,r,h,i),d){j=ub(r,n),d(j,[],h,i),k=j.length;while(k--)(l=j[k])&&(r[n[k]]=!(q[n[k]]=l))}if(f){if(e||a){if(e){j=[],k=r.length;while(k--)(l=r[k])&&j.push(q[k]=l);e(null,r=[],j,i)}k=r.length;while(k--)(l=r[k])&&(j=e?K.call(f,l):m[k])>-1&&(f[j]=!(g[j]=l))}}else r=ub(r===g?r.splice(o,r.length):r),e?e(null,g,r,i):I.apply(g,r)})}function wb(a){for(var b,c,e,f=a.length,g=d.relative[a[0].type],h=g||d.relative[" "],i=g?1:0,k=rb(function(a){return a===b},h,!0),l=rb(function(a){return K.call(b,a)>-1},h,!0),m=[function(a,c,d){return!g&&(d||c!==j)||((b=c).nodeType?k(a,c,d):l(a,c,d))}];f>i;i++)if(c=d.relative[a[i].type])m=[rb(sb(m),c)];else{if(c=d.filter[a[i].type].apply(null,a[i].matches),c[u]){for(e=++i;f>e;e++)if(d.relative[a[e].type])break;return vb(i>1&&sb(m),i>1&&qb(a.slice(0,i-1).concat({value:" "===a[i-2].type?"*":""})).replace(R,"$1"),c,e>i&&wb(a.slice(i,e)),f>e&&wb(a=a.slice(e)),f>e&&qb(a))}m.push(c)}return sb(m)}function xb(a,b){var c=b.length>0,e=a.length>0,f=function(f,g,h,i,k){var l,m,o,p=0,q="0",r=f&&[],s=[],t=j,u=f||e&&d.find.TAG("*",k),v=w+=null==t?1:Math.random()||.1,x=u.length;for(k&&(j=g!==n&&g);q!==x&&null!=(l=u[q]);q++){if(e&&l){m=0;while(o=a[m++])if(o(l,g,h)){i.push(l);break}k&&(w=v)}c&&((l=!o&&l)&&p--,f&&r.push(l))}if(p+=q,c&&q!==p){m=0;while(o=b[m++])o(r,s,g,h);if(f){if(p>0)while(q--)r[q]||s[q]||(s[q]=G.call(i));s=ub(s)}I.apply(i,s),k&&!f&&s.length>0&&p+b.length>1&&fb.uniqueSort(i)}return k&&(w=v,j=t),r};return c?hb(f):f}return h=fb.compile=function(a,b){var c,d=[],e=[],f=A[a+" "];if(!f){b||(b=g(a)),c=b.length;while(c--)f=wb(b[c]),f[u]?d.push(f):e.push(f);f=A(a,xb(e,d)),f.selector=a}return f},i=fb.select=function(a,b,e,f){var i,j,k,l,m,n="function"==typeof a&&a,o=!f&&g(a=n.selector||a);if(e=e||[],1===o.length){if(j=o[0]=o[0].slice(0),j.length>2&&"ID"===(k=j[0]).type&&c.getById&&9===b.nodeType&&p&&d.relative[j[1].type]){if(b=(d.find.ID(k.matches[0].replace(cb,db),b)||[])[0],!b)return e;n&&(b=b.parentNode),a=a.slice(j.shift().value.length)}i=X.needsContext.test(a)?0:j.length;while(i--){if(k=j[i],d.relative[l=k.type])break;if((m=d.find[l])&&(f=m(k.matches[0].replace(cb,db),ab.test(j[0].type)&&ob(b.parentNode)||b))){if(j.splice(i,1),a=f.length&&qb(j),!a)return I.apply(e,f),e;break}}}return(n||h(a,o))(f,b,!p,e,ab.test(a)&&ob(b.parentNode)||b),e},c.sortStable=u.split("").sort(B).join("")===u,c.detectDuplicates=!!l,m(),c.sortDetached=ib(function(a){return 1&a.compareDocumentPosition(n.createElement("div"))}),ib(function(a){return a.innerHTML="","#"===a.firstChild.getAttribute("href")})||jb("type|href|height|width",function(a,b,c){return c?void 0:a.getAttribute(b,"type"===b.toLowerCase()?1:2)}),c.attributes&&ib(function(a){return a.innerHTML="",a.firstChild.setAttribute("value",""),""===a.firstChild.getAttribute("value")})||jb("value",function(a,b,c){return c||"input"!==a.nodeName.toLowerCase()?void 0:a.defaultValue}),ib(function(a){return null==a.getAttribute("disabled")})||jb(L,function(a,b,c){var d;return c?void 0:a[b]===!0?b.toLowerCase():(d=a.getAttributeNode(b))&&d.specified?d.value:null}),fb}(a);m.find=s,m.expr=s.selectors,m.expr[":"]=m.expr.pseudos,m.unique=s.uniqueSort,m.text=s.getText,m.isXMLDoc=s.isXML,m.contains=s.contains;var t=m.expr.match.needsContext,u=/^<(\w+)\s*\/?>(?:<\/\1>|)$/,v=/^.[^:#\[\.,]*$/;function w(a,b,c){if(m.isFunction(b))return m.grep(a,function(a,d){return!!b.call(a,d,a)!==c});if(b.nodeType)return m.grep(a,function(a){return a===b!==c});if("string"==typeof b){if(v.test(b))return m.filter(b,a,c);b=m.filter(b,a)}return m.grep(a,function(a){return m.inArray(a,b)>=0!==c})}m.filter=function(a,b,c){var d=b[0];return c&&(a=":not("+a+")"),1===b.length&&1===d.nodeType?m.find.matchesSelector(d,a)?[d]:[]:m.find.matches(a,m.grep(b,function(a){return 1===a.nodeType}))},m.fn.extend({find:function(a){var b,c=[],d=this,e=d.length;if("string"!=typeof a)return this.pushStack(m(a).filter(function(){for(b=0;e>b;b++)if(m.contains(d[b],this))return!0}));for(b=0;e>b;b++)m.find(a,d[b],c);return c=this.pushStack(e>1?m.unique(c):c),c.selector=this.selector?this.selector+" "+a:a,c},filter:function(a){return this.pushStack(w(this,a||[],!1))},not:function(a){return this.pushStack(w(this,a||[],!0))},is:function(a){return!!w(this,"string"==typeof a&&t.test(a)?m(a):a||[],!1).length}});var x,y=a.document,z=/^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,A=m.fn.init=function(a,b){var c,d;if(!a)return this;if("string"==typeof a){if(c="<"===a.charAt(0)&&">"===a.charAt(a.length-1)&&a.length>=3?[null,a,null]:z.exec(a),!c||!c[1]&&b)return!b||b.jquery?(b||x).find(a):this.constructor(b).find(a);if(c[1]){if(b=b instanceof m?b[0]:b,m.merge(this,m.parseHTML(c[1],b&&b.nodeType?b.ownerDocument||b:y,!0)),u.test(c[1])&&m.isPlainObject(b))for(c in b)m.isFunction(this[c])?this[c](b[c]):this.attr(c,b[c]);return this}if(d=y.getElementById(c[2]),d&&d.parentNode){if(d.id!==c[2])return x.find(a);this.length=1,this[0]=d}return this.context=y,this.selector=a,this}return a.nodeType?(this.context=this[0]=a,this.length=1,this):m.isFunction(a)?"undefined"!=typeof x.ready?x.ready(a):a(m):(void 0!==a.selector&&(this.selector=a.selector,this.context=a.context),m.makeArray(a,this))};A.prototype=m.fn,x=m(y);var B=/^(?:parents|prev(?:Until|All))/,C={children:!0,contents:!0,next:!0,prev:!0};m.extend({dir:function(a,b,c){var d=[],e=a[b];while(e&&9!==e.nodeType&&(void 0===c||1!==e.nodeType||!m(e).is(c)))1===e.nodeType&&d.push(e),e=e[b];return d},sibling:function(a,b){for(var c=[];a;a=a.nextSibling)1===a.nodeType&&a!==b&&c.push(a);return c}}),m.fn.extend({has:function(a){var b,c=m(a,this),d=c.length;return this.filter(function(){for(b=0;d>b;b++)if(m.contains(this,c[b]))return!0})},closest:function(a,b){for(var c,d=0,e=this.length,f=[],g=t.test(a)||"string"!=typeof a?m(a,b||this.context):0;e>d;d++)for(c=this[d];c&&c!==b;c=c.parentNode)if(c.nodeType<11&&(g?g.index(c)>-1:1===c.nodeType&&m.find.matchesSelector(c,a))){f.push(c);break}return this.pushStack(f.length>1?m.unique(f):f)},index:function(a){return a?"string"==typeof a?m.inArray(this[0],m(a)):m.inArray(a.jquery?a[0]:a,this):this[0]&&this[0].parentNode?this.first().prevAll().length:-1},add:function(a,b){return this.pushStack(m.unique(m.merge(this.get(),m(a,b))))},addBack:function(a){return this.add(null==a?this.prevObject:this.prevObject.filter(a))}});function D(a,b){do a=a[b];while(a&&1!==a.nodeType);return a}m.each({parent:function(a){var b=a.parentNode;return b&&11!==b.nodeType?b:null},parents:function(a){return m.dir(a,"parentNode")},parentsUntil:function(a,b,c){return m.dir(a,"parentNode",c)},next:function(a){return D(a,"nextSibling")},prev:function(a){return D(a,"previousSibling")},nextAll:function(a){return m.dir(a,"nextSibling")},prevAll:function(a){return m.dir(a,"previousSibling")},nextUntil:function(a,b,c){return m.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return m.dir(a,"previousSibling",c)},siblings:function(a){return m.sibling((a.parentNode||{}).firstChild,a)},children:function(a){return m.sibling(a.firstChild)},contents:function(a){return m.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:m.merge([],a.childNodes)}},function(a,b){m.fn[a]=function(c,d){var e=m.map(this,b,c);return"Until"!==a.slice(-5)&&(d=c),d&&"string"==typeof d&&(e=m.filter(d,e)),this.length>1&&(C[a]||(e=m.unique(e)),B.test(a)&&(e=e.reverse())),this.pushStack(e)}});var E=/\S+/g,F={};function G(a){var b=F[a]={};return m.each(a.match(E)||[],function(a,c){b[c]=!0}),b}m.Callbacks=function(a){a="string"==typeof a?F[a]||G(a):m.extend({},a);var b,c,d,e,f,g,h=[],i=!a.once&&[],j=function(l){for(c=a.memory&&l,d=!0,f=g||0,g=0,e=h.length,b=!0;h&&e>f;f++)if(h[f].apply(l[0],l[1])===!1&&a.stopOnFalse){c=!1;break}b=!1,h&&(i?i.length&&j(i.shift()):c?h=[]:k.disable())},k={add:function(){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this},remove:function(){return h&&m.each(arguments,function(a,c){var d;while((d=m.inArray(c,h,d))>-1)h.splice(d,1),b&&(e>=d&&e--,f>=d&&f--)}),this},has:function(a){return a?m.inArray(a,h)>-1:!(!h||!h.length)},empty:function(){return h=[],e=0,this},disable:function(){return h=i=c=void 0,this},disabled:function(){return!h},lock:function(){return i=void 0,c||k.disable(),this},locked:function(){return!i},fireWith:function(a,c){return!h||d&&!i||(c=c||[],c=[a,c.slice?c.slice():c],b?i.push(c):j(c)),this},fire:function(){return k.fireWith(this,arguments),this},fired:function(){return!!d}};return k},m.extend({Deferred:function(a){var b=[["resolve","done",m.Callbacks("once memory"),"resolved"],["reject","fail",m.Callbacks("once memory"),"rejected"],["notify","progress",m.Callbacks("memory")]],c="pending",d={state:function(){return c},always:function(){return e.done(arguments).fail(arguments),this},then:function(){var a=arguments;return m.Deferred(function(c){m.each(b,function(b,f){var g=m.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&m.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()},promise:function(a){return null!=a?m.extend(a,d):d}},e={};return d.pipe=d.then,m.each(b,function(a,f){var g=f[2],h=f[3];d[f[1]]=g.add,h&&g.add(function(){c=h},b[1^a][2].disable,b[2][2].lock),e[f[0]]=function(){return e[f[0]+"With"](this===e?d:this,arguments),this},e[f[0]+"With"]=g.fireWith}),d.promise(e),a&&a.call(e,e),e},when:function(a){var b=0,c=d.call(arguments),e=c.length,f=1!==e||a&&m.isFunction(a.promise)?e:0,g=1===f?a:m.Deferred(),h=function(a,b,c){return function(e){b[a]=this,c[a]=arguments.length>1?d.call(arguments):e,c===i?g.notifyWith(b,c):--f||g.resolveWith(b,c)}},i,j,k;if(e>1)for(i=new Array(e),j=new Array(e),k=new Array(e);e>b;b++)c[b]&&m.isFunction(c[b].promise)?c[b].promise().done(h(b,k,c)).fail(g.reject).progress(h(b,j,i)):--f;return f||g.resolveWith(k,c),g.promise()}});var H;m.fn.ready=function(a){return m.ready.promise().done(a),this},m.extend({isReady:!1,readyWait:1,holdReady:function(a){a?m.readyWait++:m.ready(!0)},ready:function(a){if(a===!0?!--m.readyWait:!m.isReady){if(!y.body)return setTimeout(m.ready);m.isReady=!0,a!==!0&&--m.readyWait>0||(H.resolveWith(y,[m]),m.fn.triggerHandler&&(m(y).triggerHandler("ready"),m(y).off("ready")))}}});function I(){y.addEventListener?(y.removeEventListener("DOMContentLoaded",J,!1),a.removeEventListener("load",J,!1)):(y.detachEvent("onreadystatechange",J),a.detachEvent("onload",J))}function J(){(y.addEventListener||"load"===event.type||"complete"===y.readyState)&&(I(),m.ready())}m.ready.promise=function(b){if(!H)if(H=m.Deferred(),"complete"===y.readyState)setTimeout(m.ready);else if(y.addEventListener)y.addEventListener("DOMContentLoaded",J,!1),a.addEventListener("load",J,!1);else{y.attachEvent("onreadystatechange",J),a.attachEvent("onload",J);var c=!1;try{c=null==a.frameElement&&y.documentElement}catch(d){}c&&c.doScroll&&!function e(){if(!m.isReady){try{c.doScroll("left")}catch(a){return setTimeout(e,50)}I(),m.ready()}}()}return H.promise(b)};var K="undefined",L;for(L in m(k))break;k.ownLast="0"!==L,k.inlineBlockNeedsLayout=!1,m(function(){var a,b,c,d;c=y.getElementsByTagName("body")[0],c&&c.style&&(b=y.createElement("div"),d=y.createElement("div"),d.style.cssText="position:absolute;border:0;width:0;height:0;top:0;left:-9999px",c.appendChild(d).appendChild(b),typeof b.style.zoom!==K&&(b.style.cssText="display:inline;margin:0;border:0;padding:1px;width:1px;zoom:1",k.inlineBlockNeedsLayout=a=3===b.offsetWidth,a&&(c.style.zoom=1)),c.removeChild(d))}),function(){var a=y.createElement("div");if(null==k.deleteExpando){k.deleteExpando=!0;try{delete a.test}catch(b){k.deleteExpando=!1}}a=null}(),m.acceptData=function(a){var b=m.noData[(a.nodeName+" ").toLowerCase()],c=+a.nodeType||1;return 1!==c&&9!==c?!1:!b||b!==!0&&a.getAttribute("classid")===b};var M=/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,N=/([A-Z])/g;function O(a,b,c){if(void 0===c&&1===a.nodeType){var d="data-"+b.replace(N,"-$1").toLowerCase();if(c=a.getAttribute(d),"string"==typeof c){try{c="true"===c?!0:"false"===c?!1:"null"===c?null:+c+""===c?+c:M.test(c)?m.parseJSON(c):c}catch(e){}m.data(a,b,c)}else c=void 0}return c}function P(a){var b;for(b in a)if(("data"!==b||!m.isEmptyObject(a[b]))&&"toJSON"!==b)return!1;return!0}function Q(a,b,d,e){if(m.acceptData(a)){var f,g,h=m.expando,i=a.nodeType,j=i?m.cache:a,k=i?a[h]:a[h]&&h; +if(k&&j[k]&&(e||j[k].data)||void 0!==d||"string"!=typeof b)return k||(k=i?a[h]=c.pop()||m.guid++:h),j[k]||(j[k]=i?{}:{toJSON:m.noop}),("object"==typeof b||"function"==typeof b)&&(e?j[k]=m.extend(j[k],b):j[k].data=m.extend(j[k].data,b)),g=j[k],e||(g.data||(g.data={}),g=g.data),void 0!==d&&(g[m.camelCase(b)]=d),"string"==typeof b?(f=g[b],null==f&&(f=g[m.camelCase(b)])):f=g,f}}function R(a,b,c){if(m.acceptData(a)){var d,e,f=a.nodeType,g=f?m.cache:a,h=f?a[m.expando]:m.expando;if(g[h]){if(b&&(d=c?g[h]:g[h].data)){m.isArray(b)?b=b.concat(m.map(b,m.camelCase)):b in d?b=[b]:(b=m.camelCase(b),b=b in d?[b]:b.split(" ")),e=b.length;while(e--)delete d[b[e]];if(c?!P(d):!m.isEmptyObject(d))return}(c||(delete g[h].data,P(g[h])))&&(f?m.cleanData([a],!0):k.deleteExpando||g!=g.window?delete g[h]:g[h]=null)}}}m.extend({cache:{},noData:{"applet ":!0,"embed ":!0,"object ":"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"},hasData:function(a){return a=a.nodeType?m.cache[a[m.expando]]:a[m.expando],!!a&&!P(a)},data:function(a,b,c){return Q(a,b,c)},removeData:function(a,b){return R(a,b)},_data:function(a,b,c){return Q(a,b,c,!0)},_removeData:function(a,b){return R(a,b,!0)}}),m.fn.extend({data:function(a,b){var c,d,e,f=this[0],g=f&&f.attributes;if(void 0===a){if(this.length&&(e=m.data(f),1===f.nodeType&&!m._data(f,"parsedAttrs"))){c=g.length;while(c--)g[c]&&(d=g[c].name,0===d.indexOf("data-")&&(d=m.camelCase(d.slice(5)),O(f,d,e[d])));m._data(f,"parsedAttrs",!0)}return e}return"object"==typeof a?this.each(function(){m.data(this,a)}):arguments.length>1?this.each(function(){m.data(this,a,b)}):f?O(f,a,m.data(f,a)):void 0},removeData:function(a){return this.each(function(){m.removeData(this,a)})}}),m.extend({queue:function(a,b,c){var d;return a?(b=(b||"fx")+"queue",d=m._data(a,b),c&&(!d||m.isArray(c)?d=m._data(a,b,m.makeArray(c)):d.push(c)),d||[]):void 0},dequeue:function(a,b){b=b||"fx";var c=m.queue(a,b),d=c.length,e=c.shift(),f=m._queueHooks(a,b),g=function(){m.dequeue(a,b)};"inprogress"===e&&(e=c.shift(),d--),e&&("fx"===b&&c.unshift("inprogress"),delete f.stop,e.call(a,g,f)),!d&&f&&f.empty.fire()},_queueHooks:function(a,b){var c=b+"queueHooks";return m._data(a,c)||m._data(a,c,{empty:m.Callbacks("once memory").add(function(){m._removeData(a,b+"queue"),m._removeData(a,c)})})}}),m.fn.extend({queue:function(a,b){var c=2;return"string"!=typeof a&&(b=a,a="fx",c--),arguments.lengthh;h++)b(a[h],c,g?d:d.call(a[h],h,b(a[h],c)));return e?a:j?b.call(a):i?b(a[0],c):f},W=/^(?:checkbox|radio)$/i;!function(){var a=y.createElement("input"),b=y.createElement("div"),c=y.createDocumentFragment();if(b.innerHTML="
a",k.leadingWhitespace=3===b.firstChild.nodeType,k.tbody=!b.getElementsByTagName("tbody").length,k.htmlSerialize=!!b.getElementsByTagName("link").length,k.html5Clone="<:nav>"!==y.createElement("nav").cloneNode(!0).outerHTML,a.type="checkbox",a.checked=!0,c.appendChild(a),k.appendChecked=a.checked,b.innerHTML="",k.noCloneChecked=!!b.cloneNode(!0).lastChild.defaultValue,c.appendChild(b),b.innerHTML="",k.checkClone=b.cloneNode(!0).cloneNode(!0).lastChild.checked,k.noCloneEvent=!0,b.attachEvent&&(b.attachEvent("onclick",function(){k.noCloneEvent=!1}),b.cloneNode(!0).click()),null==k.deleteExpando){k.deleteExpando=!0;try{delete b.test}catch(d){k.deleteExpando=!1}}}(),function(){var b,c,d=y.createElement("div");for(b in{submit:!0,change:!0,focusin:!0})c="on"+b,(k[b+"Bubbles"]=c in a)||(d.setAttribute(c,"t"),k[b+"Bubbles"]=d.attributes[c].expando===!1);d=null}();var X=/^(?:input|select|textarea)$/i,Y=/^key/,Z=/^(?:mouse|pointer|contextmenu)|click/,$=/^(?:focusinfocus|focusoutblur)$/,_=/^([^.]*)(?:\.(.+)|)$/;function ab(){return!0}function bb(){return!1}function cb(){try{return y.activeElement}catch(a){}}m.event={global:{},add:function(a,b,c,d,e){var f,g,h,i,j,k,l,n,o,p,q,r=m._data(a);if(r){c.handler&&(i=c,c=i.handler,e=i.selector),c.guid||(c.guid=m.guid++),(g=r.events)||(g=r.events={}),(k=r.handle)||(k=r.handle=function(a){return typeof m===K||a&&m.event.triggered===a.type?void 0:m.event.dispatch.apply(k.elem,arguments)},k.elem=a),b=(b||"").match(E)||[""],h=b.length;while(h--)f=_.exec(b[h])||[],o=q=f[1],p=(f[2]||"").split(".").sort(),o&&(j=m.event.special[o]||{},o=(e?j.delegateType:j.bindType)||o,j=m.event.special[o]||{},l=m.extend({type:o,origType:q,data:d,handler:c,guid:c.guid,selector:e,needsContext:e&&m.expr.match.needsContext.test(e),namespace:p.join(".")},i),(n=g[o])||(n=g[o]=[],n.delegateCount=0,j.setup&&j.setup.call(a,d,p,k)!==!1||(a.addEventListener?a.addEventListener(o,k,!1):a.attachEvent&&a.attachEvent("on"+o,k))),j.add&&(j.add.call(a,l),l.handler.guid||(l.handler.guid=c.guid)),e?n.splice(n.delegateCount++,0,l):n.push(l),m.event.global[o]=!0);a=null}},remove:function(a,b,c,d,e){var f,g,h,i,j,k,l,n,o,p,q,r=m.hasData(a)&&m._data(a);if(r&&(k=r.events)){b=(b||"").match(E)||[""],j=b.length;while(j--)if(h=_.exec(b[j])||[],o=q=h[1],p=(h[2]||"").split(".").sort(),o){l=m.event.special[o]||{},o=(d?l.delegateType:l.bindType)||o,n=k[o]||[],h=h[2]&&new RegExp("(^|\\.)"+p.join("\\.(?:.*\\.|)")+"(\\.|$)"),i=f=n.length;while(f--)g=n[f],!e&&q!==g.origType||c&&c.guid!==g.guid||h&&!h.test(g.namespace)||d&&d!==g.selector&&("**"!==d||!g.selector)||(n.splice(f,1),g.selector&&n.delegateCount--,l.remove&&l.remove.call(a,g));i&&!n.length&&(l.teardown&&l.teardown.call(a,p,r.handle)!==!1||m.removeEvent(a,o,r.handle),delete k[o])}else for(o in k)m.event.remove(a,o+b[j],c,d,!0);m.isEmptyObject(k)&&(delete r.handle,m._removeData(a,"events"))}},trigger:function(b,c,d,e){var f,g,h,i,k,l,n,o=[d||y],p=j.call(b,"type")?b.type:b,q=j.call(b,"namespace")?b.namespace.split("."):[];if(h=l=d=d||y,3!==d.nodeType&&8!==d.nodeType&&!$.test(p+m.event.triggered)&&(p.indexOf(".")>=0&&(q=p.split("."),p=q.shift(),q.sort()),g=p.indexOf(":")<0&&"on"+p,b=b[m.expando]?b:new m.Event(p,"object"==typeof b&&b),b.isTrigger=e?2:3,b.namespace=q.join("."),b.namespace_re=b.namespace?new RegExp("(^|\\.)"+q.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,b.result=void 0,b.target||(b.target=d),c=null==c?[b]:m.makeArray(c,[b]),k=m.event.special[p]||{},e||!k.trigger||k.trigger.apply(d,c)!==!1)){if(!e&&!k.noBubble&&!m.isWindow(d)){for(i=k.delegateType||p,$.test(i+p)||(h=h.parentNode);h;h=h.parentNode)o.push(h),l=h;l===(d.ownerDocument||y)&&o.push(l.defaultView||l.parentWindow||a)}n=0;while((h=o[n++])&&!b.isPropagationStopped())b.type=n>1?i:k.bindType||p,f=(m._data(h,"events")||{})[b.type]&&m._data(h,"handle"),f&&f.apply(h,c),f=g&&h[g],f&&f.apply&&m.acceptData(h)&&(b.result=f.apply(h,c),b.result===!1&&b.preventDefault());if(b.type=p,!e&&!b.isDefaultPrevented()&&(!k._default||k._default.apply(o.pop(),c)===!1)&&m.acceptData(d)&&g&&d[p]&&!m.isWindow(d)){l=d[g],l&&(d[g]=null),m.event.triggered=p;try{d[p]()}catch(r){}m.event.triggered=void 0,l&&(d[g]=l)}return b.result}},dispatch:function(a){a=m.event.fix(a);var b,c,e,f,g,h=[],i=d.call(arguments),j=(m._data(this,"events")||{})[a.type]||[],k=m.event.special[a.type]||{};if(i[0]=a,a.delegateTarget=this,!k.preDispatch||k.preDispatch.call(this,a)!==!1){h=m.event.handlers.call(this,a,j),b=0;while((f=h[b++])&&!a.isPropagationStopped()){a.currentTarget=f.elem,g=0;while((e=f.handlers[g++])&&!a.isImmediatePropagationStopped())(!a.namespace_re||a.namespace_re.test(e.namespace))&&(a.handleObj=e,a.data=e.data,c=((m.event.special[e.origType]||{}).handle||e.handler).apply(f.elem,i),void 0!==c&&(a.result=c)===!1&&(a.preventDefault(),a.stopPropagation()))}return k.postDispatch&&k.postDispatch.call(this,a),a.result}},handlers:function(a,b){var c,d,e,f,g=[],h=b.delegateCount,i=a.target;if(h&&i.nodeType&&(!a.button||"click"!==a.type))for(;i!=this;i=i.parentNode||this)if(1===i.nodeType&&(i.disabled!==!0||"click"!==a.type)){for(e=[],f=0;h>f;f++)d=b[f],c=d.selector+" ",void 0===e[c]&&(e[c]=d.needsContext?m(c,this).index(i)>=0:m.find(c,this,null,[i]).length),e[c]&&e.push(d);e.length&&g.push({elem:i,handlers:e})}return h]","i"),hb=/^\s+/,ib=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,jb=/<([\w:]+)/,kb=/\s*$/g,rb={option:[1,""],legend:[1,"
","
"],area:[1,"",""],param:[1,"",""],thead:[1,"","
"],tr:[2,"","
"],col:[2,"","
"],td:[3,"","
"],_default:k.htmlSerialize?[0,"",""]:[1,"X
","
"]},sb=db(y),tb=sb.appendChild(y.createElement("div"));rb.optgroup=rb.option,rb.tbody=rb.tfoot=rb.colgroup=rb.caption=rb.thead,rb.th=rb.td;function ub(a,b){var c,d,e=0,f=typeof a.getElementsByTagName!==K?a.getElementsByTagName(b||"*"):typeof a.querySelectorAll!==K?a.querySelectorAll(b||"*"):void 0;if(!f)for(f=[],c=a.childNodes||a;null!=(d=c[e]);e++)!b||m.nodeName(d,b)?f.push(d):m.merge(f,ub(d,b));return void 0===b||b&&m.nodeName(a,b)?m.merge([a],f):f}function vb(a){W.test(a.type)&&(a.defaultChecked=a.checked)}function wb(a,b){return m.nodeName(a,"table")&&m.nodeName(11!==b.nodeType?b:b.firstChild,"tr")?a.getElementsByTagName("tbody")[0]||a.appendChild(a.ownerDocument.createElement("tbody")):a}function xb(a){return a.type=(null!==m.find.attr(a,"type"))+"/"+a.type,a}function yb(a){var b=pb.exec(a.type);return b?a.type=b[1]:a.removeAttribute("type"),a}function zb(a,b){for(var c,d=0;null!=(c=a[d]);d++)m._data(c,"globalEval",!b||m._data(b[d],"globalEval"))}function Ab(a,b){if(1===b.nodeType&&m.hasData(a)){var c,d,e,f=m._data(a),g=m._data(b,f),h=f.events;if(h){delete g.handle,g.events={};for(c in h)for(d=0,e=h[c].length;e>d;d++)m.event.add(b,c,h[c][d])}g.data&&(g.data=m.extend({},g.data))}}function Bb(a,b){var c,d,e;if(1===b.nodeType){if(c=b.nodeName.toLowerCase(),!k.noCloneEvent&&b[m.expando]){e=m._data(b);for(d in e.events)m.removeEvent(b,d,e.handle);b.removeAttribute(m.expando)}"script"===c&&b.text!==a.text?(xb(b).text=a.text,yb(b)):"object"===c?(b.parentNode&&(b.outerHTML=a.outerHTML),k.html5Clone&&a.innerHTML&&!m.trim(b.innerHTML)&&(b.innerHTML=a.innerHTML)):"input"===c&&W.test(a.type)?(b.defaultChecked=b.checked=a.checked,b.value!==a.value&&(b.value=a.value)):"option"===c?b.defaultSelected=b.selected=a.defaultSelected:("input"===c||"textarea"===c)&&(b.defaultValue=a.defaultValue)}}m.extend({clone:function(a,b,c){var d,e,f,g,h,i=m.contains(a.ownerDocument,a);if(k.html5Clone||m.isXMLDoc(a)||!gb.test("<"+a.nodeName+">")?f=a.cloneNode(!0):(tb.innerHTML=a.outerHTML,tb.removeChild(f=tb.firstChild)),!(k.noCloneEvent&&k.noCloneChecked||1!==a.nodeType&&11!==a.nodeType||m.isXMLDoc(a)))for(d=ub(f),h=ub(a),g=0;null!=(e=h[g]);++g)d[g]&&Bb(e,d[g]);if(b)if(c)for(h=h||ub(a),d=d||ub(f),g=0;null!=(e=h[g]);g++)Ab(e,d[g]);else Ab(a,f);return d=ub(f,"script"),d.length>0&&zb(d,!i&&ub(a,"script")),d=h=e=null,f},buildFragment:function(a,b,c,d){for(var e,f,g,h,i,j,l,n=a.length,o=db(b),p=[],q=0;n>q;q++)if(f=a[q],f||0===f)if("object"===m.type(f))m.merge(p,f.nodeType?[f]:f);else if(lb.test(f)){h=h||o.appendChild(b.createElement("div")),i=(jb.exec(f)||["",""])[1].toLowerCase(),l=rb[i]||rb._default,h.innerHTML=l[1]+f.replace(ib,"<$1>")+l[2],e=l[0];while(e--)h=h.lastChild;if(!k.leadingWhitespace&&hb.test(f)&&p.push(b.createTextNode(hb.exec(f)[0])),!k.tbody){f="table"!==i||kb.test(f)?""!==l[1]||kb.test(f)?0:h:h.firstChild,e=f&&f.childNodes.length;while(e--)m.nodeName(j=f.childNodes[e],"tbody")&&!j.childNodes.length&&f.removeChild(j)}m.merge(p,h.childNodes),h.textContent="";while(h.firstChild)h.removeChild(h.firstChild);h=o.lastChild}else p.push(b.createTextNode(f));h&&o.removeChild(h),k.appendChecked||m.grep(ub(p,"input"),vb),q=0;while(f=p[q++])if((!d||-1===m.inArray(f,d))&&(g=m.contains(f.ownerDocument,f),h=ub(o.appendChild(f),"script"),g&&zb(h),c)){e=0;while(f=h[e++])ob.test(f.type||"")&&c.push(f)}return h=null,o},cleanData:function(a,b){for(var d,e,f,g,h=0,i=m.expando,j=m.cache,l=k.deleteExpando,n=m.event.special;null!=(d=a[h]);h++)if((b||m.acceptData(d))&&(f=d[i],g=f&&j[f])){if(g.events)for(e in g.events)n[e]?m.event.remove(d,e):m.removeEvent(d,e,g.handle);j[f]&&(delete j[f],l?delete d[i]:typeof d.removeAttribute!==K?d.removeAttribute(i):d[i]=null,c.push(f))}}}),m.fn.extend({text:function(a){return V(this,function(a){return void 0===a?m.text(this):this.empty().append((this[0]&&this[0].ownerDocument||y).createTextNode(a))},null,a,arguments.length)},append:function(){return this.domManip(arguments,function(a){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var b=wb(this,a);b.appendChild(a)}})},prepend:function(){return this.domManip(arguments,function(a){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var b=wb(this,a);b.insertBefore(a,b.firstChild)}})},before:function(){return this.domManip(arguments,function(a){this.parentNode&&this.parentNode.insertBefore(a,this)})},after:function(){return this.domManip(arguments,function(a){this.parentNode&&this.parentNode.insertBefore(a,this.nextSibling)})},remove:function(a,b){for(var c,d=a?m.filter(a,this):this,e=0;null!=(c=d[e]);e++)b||1!==c.nodeType||m.cleanData(ub(c)),c.parentNode&&(b&&m.contains(c.ownerDocument,c)&&zb(ub(c,"script")),c.parentNode.removeChild(c));return this},empty:function(){for(var a,b=0;null!=(a=this[b]);b++){1===a.nodeType&&m.cleanData(ub(a,!1));while(a.firstChild)a.removeChild(a.firstChild);a.options&&m.nodeName(a,"select")&&(a.options.length=0)}return this},clone:function(a,b){return a=null==a?!1:a,b=null==b?a:b,this.map(function(){return m.clone(this,a,b)})},html:function(a){return V(this,function(a){var b=this[0]||{},c=0,d=this.length;if(void 0===a)return 1===b.nodeType?b.innerHTML.replace(fb,""):void 0;if(!("string"!=typeof a||mb.test(a)||!k.htmlSerialize&&gb.test(a)||!k.leadingWhitespace&&hb.test(a)||rb[(jb.exec(a)||["",""])[1].toLowerCase()])){a=a.replace(ib,"<$1>");try{for(;d>c;c++)b=this[c]||{},1===b.nodeType&&(m.cleanData(ub(b,!1)),b.innerHTML=a);b=0}catch(e){}}b&&this.empty().append(a)},null,a,arguments.length)},replaceWith:function(){var a=arguments[0];return this.domManip(arguments,function(b){a=this.parentNode,m.cleanData(ub(this)),a&&a.replaceChild(b,this)}),a&&(a.length||a.nodeType)?this:this.remove()},detach:function(a){return this.remove(a,!0)},domManip:function(a,b){a=e.apply([],a);var c,d,f,g,h,i,j=0,l=this.length,n=this,o=l-1,p=a[0],q=m.isFunction(p);if(q||l>1&&"string"==typeof p&&!k.checkClone&&nb.test(p))return this.each(function(c){var d=n.eq(c);q&&(a[0]=p.call(this,c,d.html())),d.domManip(a,b)});if(l&&(i=m.buildFragment(a,this[0].ownerDocument,!1,this),c=i.firstChild,1===i.childNodes.length&&(i=c),c)){for(g=m.map(ub(i,"script"),xb),f=g.length;l>j;j++)d=i,j!==o&&(d=m.clone(d,!0,!0),f&&m.merge(g,ub(d,"script"))),b.call(this[j],d,j);if(f)for(h=g[g.length-1].ownerDocument,m.map(g,yb),j=0;f>j;j++)d=g[j],ob.test(d.type||"")&&!m._data(d,"globalEval")&&m.contains(h,d)&&(d.src?m._evalUrl&&m._evalUrl(d.src):m.globalEval((d.text||d.textContent||d.innerHTML||"").replace(qb,"")));i=c=null}return this}}),m.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(a,b){m.fn[a]=function(a){for(var c,d=0,e=[],g=m(a),h=g.length-1;h>=d;d++)c=d===h?this:this.clone(!0),m(g[d])[b](c),f.apply(e,c.get());return this.pushStack(e)}});var Cb,Db={};function Eb(b,c){var d,e=m(c.createElement(b)).appendTo(c.body),f=a.getDefaultComputedStyle&&(d=a.getDefaultComputedStyle(e[0]))?d.display:m.css(e[0],"display");return e.detach(),f}function Fb(a){var b=y,c=Db[a];return c||(c=Eb(a,b),"none"!==c&&c||(Cb=(Cb||m(" + + diff --git a/docs/docs/api/.pages b/docs/docs/api/.pages new file mode 100644 index 0000000000..3a8b28cbf3 --- /dev/null +++ b/docs/docs/api/.pages @@ -0,0 +1,8 @@ +nav: + - Dataset: dataset.md + - Column: column.md + - Types: types.md + - Query: query.md + - Version Control: version_control.md + - Schemas: schemas.md + - Metadata: metadata.md \ No newline at end of file diff --git a/docs/docs/api/column.md b/docs/docs/api/column.md new file mode 100644 index 0000000000..2d3b7f0459 --- /dev/null +++ b/docs/docs/api/column.md @@ -0,0 +1,449 @@ +--- +seo_title: "Deep Lake API Columns" +description: "Access Deep Lake Documentation For Complete Setup, API Reference, Guides On Efficient Multi-Modal AI Search, Dataset Management, Cost-Efficient Training, And Retrieval-Augmented Generation." +toc_depth: 2 +--- +# Column Classes + +Deep Lake provides two column classes for different access levels: + + +| Class | Description | +| -------------------- | ------------------------------------------------------------ | +| Column | Full read-write access to column data | +| ColumnView | Read-only access to column data | +| ColumnDefinition | Schema definition for columns with modification capabilities | +| ColumnDefinitionView | Read-only schema definition for columns | + +## Column Class + +::: deeplake.Column + options: + heading_level: 3 + members: + - __getitem__ + - __setitem__ + - create_index + - drop_index + - dtype + - get_async + - get_bytes + - get_bytes_async + - indexes + - metadata + - name + - set_async + +## ColumnView Class + +::: deeplake.ColumnView + options: + heading_level: 3 + members: + - __getitem__ + - dtype + - get_async + - get_bytes + - get_bytes_async + - indexes + - metadata + - name + +## ColumnDefinition Class + +::: deeplake.ColumnDefinition + options: + heading_level: 3 + members: + - dtype + - drop + - name + - rename + +## ColumnDefinitionView Class + +::: deeplake.ColumnDefinitionView + options: + heading_level: 3 + members: + - dtype + - name + +## Class Comparison + +### Column +- Provides read-write access +- Can modify data and metadata +- Can create/drop indexes for search optimization +- Access to column schema and data type information +- Supports both sync and async operations +- Raw bytes access for binary data +- Available in Dataset + + + +```python +# Get mutable column +ds = deeplake.open("s3://bucket/dataset") +column = ds["images"] + +# Read data +image = column[0] +batch = column[0:100] + +# Write data +column[0] = new_image +column[0:100] = new_batch + +# Async operations +future = column.set_async(0, new_image) +future.wait() +``` + +### ColumnView +- Read-only access +- Cannot modify data +- Can read metadata and schema information +- Access to indexes and data type information +- Supports both sync and async operations +- Raw bytes access for binary data +- Available in ReadOnlyDataset and DatasetView + +### ColumnDefinition +- Schema-level operations for columns +- Can rename and drop columns +- Access to column data type definitions +- Available through dataset schema + +### ColumnDefinitionView +- Read-only schema information +- Access to column data type definitions +- Cannot modify column schema +- Available through read-only dataset schemas + +```python +# Get read-only column +ro_ds = deeplake.open_read_only("s3://bucket/dataset") +ro_column = ro_ds["images"] + +# Read data +image = ro_column[0] +batch = ro_column[0:100] + +# Async read +future = ro_column.get_async(slice(0, 100)) +batch = future.result() +``` + +## Examples + +### Data Access + +```python +# Direct indexing +single_item = column[0] +batch = column[0:100] +selected = column[[1, 5, 10]] + +# Async data access +future = column.get_async(slice(0, 1000)) +data = future.result() +``` + +### Metadata and Schema Information + +```python +# Read metadata from any column type +name = column.name +metadata = column.metadata +data_type = column.dtype + +# Update metadata (Column only) +column.metadata["mean"] = [0.485, 0.456, 0.406] +column.metadata["std"] = [0.229, 0.224, 0.225] + +# Check column indexes +indexes = column.indexes +print(f"Available indexes: {indexes}") +``` + +### Index Management + +```python +# Create text search index (Column only) +column.create_index(deeplake.types.TextIndex(deeplake.types.BM25)) + +# Create embedding similarity index (Column only) +column.create_index(deeplake.types.EmbeddingIndex()) + +# List existing indexes +print(f"Current indexes: {column.indexes}") + +# Drop an index +column.drop_index(deeplake.types.TextIndex(deeplake.types.BM25)) +``` + +### Binary Data Access + +```python +# Access raw bytes data (useful for images, audio, etc.) +bytes_data = column.get_bytes(0) +batch_bytes = column.get_bytes(slice(0, 10)) + +# Async bytes access +future = column.get_bytes_async(slice(0, 100)) +bytes_batch = future.result() +``` + +### Schema Operations + +```python +# Access column schema information +schema = ds.schema +col_def = schema["images"] +print(f"Column type: {col_def.dtype}") +print(f"Column name: {col_def.name}") + +# Modify column schema (Dataset only) +col_def.rename("processed_images") +# col_def.drop() # Removes entire column +``` diff --git a/docs/docs/api/dataset.md b/docs/docs/api/dataset.md new file mode 100644 index 0000000000..5971b74c60 --- /dev/null +++ b/docs/docs/api/dataset.md @@ -0,0 +1,467 @@ +--- +seo_title: "Activeloop Deep Lake Docs Dataset" +description: "Access Deep Lake Documentation For Complete Setup, API Reference, Guides On Efficient Multi-Modal AI Search, Dataset Management, Cost-Efficient Training, And Retrieval-Augmented Generation." +toc_depth: 4 +--- + +# Dataset Classes + +Deep Lake provides three dataset classes with different access levels: + + +| Class | Description | +| --------------- | ------------------------------------------ | +| Dataset | Full read-write access with all operations | +| ReadOnlyDataset | Read-only access to prevent modifications | +| DatasetView | Read-only view of query results | + +## Creation Methods + +::: deeplake + options: + heading_level: 3 + members: + - create + - like + - from_parquet + - from_csv + - open + - open_read_only + - delete + - delete_async + - exists + - query + - query_async + - explain_query + - prepare_query + +## Dataset + +The main class providing full read-write access. + +::: deeplake.Dataset + options: + heading_level: 3 + members: + - add_column + - remove_column + - rename_column + - append + - auto_commit_enabled + - branch + - branches + - tag + - tags + - commit + - commit_async + - history + - version + - created_time + - current_branch + - delete + - description + - id + - indexing_mode + - merge + - metadata + - name + - pull + - pull_async + - push + - push_async + - query + - query_async + - prepare_query + - explain_query + - refresh + - refresh_async + - schema + - summary + - set_creds_key + - creds_key + - to_csv + - batches + - tensorflow + - pytorch + + +Read-only version of Dataset. Cannot modify data but provides access to all data and metadata. + +::: deeplake.ReadOnlyDataset + options: + heading_level: 3 + members: + - branches + - current_branch + - tags + - tag + - description + - history + - version + - created_time + - id + - metadata + - name + - query + - query_async + - refresh + - refresh_async + - schema + - explain_query + - prepare_query + - summary + - to_csv + - tensorflow + - pytorch + - batches + + +Lightweight view returned by queries. Provides read-only access to query results. + +::: deeplake.DatasetView + options: + heading_level: 3 + members: + - batches + - pytorch + - query + - query_async + - schema + - summary + - tag + - tensorflow + - to_csv + +## Class Comparison + +### Dataset +- Full read-write access +- Can create/modify columns +- Can append/update/delete data +- Can commit changes (sync and async) +- Can create version tags and branches +- Can push/pull changes (sync and async) +- Can merge branches +- Auto-commit functionality +- Dataset refresh capabilities +- Full metadata access + + + +```python +ds = deeplake.create("s3://bucket/dataset") +# or +ds = deeplake.open("s3://bucket/dataset") + +# Can modify +ds.add_column("images", deeplake.types.Image()) +ds.add_column("labels", deeplake.types.ClassLabel("int32")) +ds.add_column("confidence", "float32") +ds["labels"].metadata["class_names"] = ["cat", "dog"] +ds.append([{"images": image_array, "labels": 0, "confidence": 0.9}]) +ds.commit() +``` + +### ReadOnlyDataset +- Read-only access +- Cannot modify data or schema +- Can view all data and metadata +- Can execute queries (sync and async) +- Can refresh dataset state +- Access to version history and branches +- Full schema and property access +- Returned by `open_read_only()` + +```python +ds = deeplake.open_read_only("s3://bucket/dataset") + +# Can read +image = ds["images"][0] +metadata = ds.metadata + +# Cannot modify +# ds.append([...]) # Would raise error +``` + +### DatasetView +- Read-only access +- Cannot modify data +- Optimized for query results +- Direct integration with ML frameworks (PyTorch, TensorFlow) +- Batch processing capabilities +- Query chaining support +- Export to CSV functionality +- Schema access +- Returned by `query()` and tag operations + +```python +# Get view through query +view = ds.query("SELECT *") + +# Access data +image = view["images"][0] + +# ML framework integration +torch_dataset = view.pytorch() +tf_dataset = view.tensorflow() +``` + +## Examples + +### Querying Data + +```python +# Using Dataset +ds = deeplake.open("s3://bucket/dataset") +results = ds.query("SELECT * WHERE labels = 'cat'") + +# Using ReadOnlyDataset +ds = deeplake.open_read_only("s3://bucket/dataset") +results = ds.query("SELECT * WHERE labels = 'cat'") + +# Using DatasetView +view = ds.query("SELECT * WHERE labels = 'cat'") +subset = view.query("SELECT * WHERE confidence > 0.9") +``` + +### Data Access + +```python +# Common access patterns work on all types +for row in ds: # Works for Dataset, ReadOnlyDataset, and DatasetView + image = row["images"] + label = row["labels"] + +# Column access works on all types +images = ds["images"][:] +labels = ds["labels"][:] +``` + +### Import/Export Data + +```python +# Import from Parquet file +ds = deeplake.from_parquet("data.parquet") +# or from bytes +f = open("data.parquet", "rb") +ds = deeplake.from_parquet(f.read()) + +# Import from CSV file +ds = deeplake.from_csv("data.csv") +# or from bytes +f = open("data.csv", "rb") +ds = deeplake.from_csv(f.read()) + +# Export query results to CSV +view = ds.query("SELECT * WHERE labels = 'cat'") +import io +output = io.StringIO() +view.to_csv(output) +csv_data = output.getvalue() +``` + +### Async Operations + +```python +# Async query works on all types +future = ds.query_async("SELECT * WHERE labels = 'cat'") +results = future.result() + +# Async data access +future = ds["images"].get_async(slice(0, 1000)) +images = future.result() + +# # Async dataset operations +# future = ds.commit_async("Updated model predictions") +# future.wait() + +# # Async push/pull +# ds.push_async("s3://backup/dataset").wait() +# ds.pull_async("s3://upstream/dataset").wait() +``` + +### Dataset Management + +```python +# Check if dataset exists +if deeplake.exists("s3://bucket/dataset"): + ds = deeplake.open("s3://bucket/dataset") +else: + ds = deeplake.create("s3://bucket/dataset") + +# Auto-commit functionality +ds.auto_commit_enabled = True # Enable automatic commits + +# Refresh dataset to get latest changes +ds.refresh() + +# Delete dataset (irreversible!) +deeplake.delete("s3://old-bucket/dataset") + +``` + +### Advanced Query Operations + +```python +# Global query functions +results = deeplake.query("SELECT * FROM 's3://dataset' WHERE confidence > 0.9") + +# Async global queries +future = deeplake.query_async("SELECT * FROM 's3://dataset' LIMIT 1000") +results = future.result() + +# Explain query execution plan +plan = deeplake.explain_query("SELECT * FROM 's3://dataset' WHERE labels = 'cat'") +print(plan) + +# Prepare reusable query executor +executor = deeplake.prepare_query("SELECT * FROM 's3://dataset' WHERE score > ?") +``` diff --git a/docs/docs/api/index.md b/docs/docs/api/index.md new file mode 100644 index 0000000000..2a3c298efd --- /dev/null +++ b/docs/docs/api/index.md @@ -0,0 +1,45 @@ +--- +seo_title: "Deep Lake API Reference | Multi-Modal AI Data Management" +description: "Complete API Reference For Deep Lake, Including Dataset Creation, Multi-Modal Query Engine, Vector Search, And ML Framework Integrations." +--- + +# API Reference + +This reference documents the Python API of Deep Lake. + +## Core Components + +- [Dataset Classes](dataset.md): Documentation for `Dataset`, `ReadOnlyDataset`, and `DatasetView` classes +- [Column Classes](column.md): Documentation for `Column` and `ColumnView` classes +- [Types](types.md): Available data types including basic numeric types and ML-optimized types +- [Schemas](schemas.md): Pre-built schema templates for common data structures +- [Query Language](query.md): Complete TQL syntax and operations +- [Metadata](metadata.md): Dataset and column metadata management +- [Version Control](version_control.md): Version control, history, branches, and tags +- [Miscellaneous](misc.md): Additional auxiliary functionality + +## Index Management + +Deep Lake supports various index types for optimizing search and query performance: + +### Text Indexes +- **BM25**: Full-text search with BM25 similarity scoring +- **Inverted**: Keyword-based text search +- **Exact**: Exact text matching + +### Embedding Indexes +- **Clustered**: Default clustering-based embedding search +- **ClusteredQuantized**: Memory-efficient quantized embedding search + +### Numeric Indexes +- **Inverted**: Numeric value lookup optimization + +Indexes can be created and managed through the `Column` class methods `create_index()` and `drop_index()`. See [Column Classes](column.md) for detailed examples. + +## Getting Started + +For implementation guidance and examples, please refer to: + +- [Quickstart Guide](../getting-started/quickstart.md) +- [Storage Options](../getting-started/storage-and-creds/storage-options.md) +- [Authentication](../getting-started/authentication.md) diff --git a/docs/docs/api/metadata.md b/docs/docs/api/metadata.md new file mode 100644 index 0000000000..9333444893 --- /dev/null +++ b/docs/docs/api/metadata.md @@ -0,0 +1,157 @@ +--- +seo_title: "Activeloop Deep Lake Docs Metadata" +description: "Access Deep Lake Documentation For Metadata." +toc_depth: 2 +--- +# Metadata + +Metadata provides key-value storage for datasets and columns. + + +## Dataset Metadata + +::: deeplake.Metadata + options: + heading_level: 4 + members: + - __getitem__ + - __setitem__ + - __contains__ + - keys + + + +```python +# Set dataset metadata +ds.metadata["description"] = "Training dataset" +ds.metadata["version"] = "1.0" +ds.metadata["params"] = { + "image_size": 224, + "mean": [0.485, 0.456, 0.406], + "std": [0.229, 0.224, 0.225] +} + +# Read dataset metadata +description = ds.metadata["description"] +params = ds.metadata["params"] + +# List all metadata keys +for key in ds.metadata.keys(): + print(f"{key}: {ds.metadata[key]}") +``` + +## Column Metadata + +::: deeplake.ReadOnlyMetadata + options: + heading_level: 4 + members: + - __getitem__ + - __contains__ + - keys + +```python +# Set column metadata +ds["images"].metadata["mean"] = [0.485, 0.456, 0.406] +ds["images"].metadata["std"] = [0.229, 0.224, 0.225] +ds["labels"].metadata["class_names"] = ["cat", "dog", "bird"] + +# Read column metadata +mean = ds["images"].metadata["mean"] +class_names = ds["labels"].metadata["class_names"] + +# Check if metadata key exists +if "mean" in ds["images"].metadata: + print("Mean values are available") + +# List all metadata keys for a column +print("Available metadata keys:") +for key in ds["images"].metadata.keys(): + print(f" {key}: {ds['images'].metadata[key]}") + +ds.commit() # Commit the changes to the dataset +``` + +## Advanced Metadata Operations + +```python +# Dataset-level metadata operations +dataset_metadata = ds.metadata + +# Check if key exists before accessing +if "training_config" in dataset_metadata: + config = dataset_metadata["training_config"] +else: + # Set default configuration + dataset_metadata["training_config"] = { + "epochs": 100, + "batch_size": 32, + "learning_rate": 0.001 + } + +# List all dataset metadata +print("Dataset metadata:") +for key in dataset_metadata.keys(): + print(f" {key}: {dataset_metadata[key]}") + +# Column-level metadata operations +image_metadata = ds["images"].metadata + +# Store preprocessing parameters +image_metadata["normalization"] = { + "mean": [0.485, 0.456, 0.406], + "std": [0.229, 0.224, 0.225] +} +image_metadata["resize_dimensions"] = [224, 224] + +# Store data statistics +image_metadata["data_info"] = { + "total_samples": len(ds), + "channels": 3, + "format": "RGB" +} +``` + +## Read-Only Metadata Access + +```python +# Access metadata in read-only datasets +ro_ds = deeplake.open_read_only("s3://bucket/dataset") + +# Read dataset metadata (read-only) +if "model_version" in ro_ds.metadata: + version = ro_ds.metadata["model_version"] + print(f"Model version: {version}") + +# Read column metadata (read-only) +if "class_names" in ro_ds["labels"].metadata: + classes = ro_ds["labels"].metadata["class_names"] + print(f"Available classes: {classes}") + +# List all available metadata keys +print("Dataset metadata keys:", ro_ds.metadata.keys()) +print("Labels metadata keys:", ro_ds["labels"].metadata.keys()) +``` diff --git a/docs/docs/api/query.md b/docs/docs/api/query.md new file mode 100644 index 0000000000..0f8cd46af4 --- /dev/null +++ b/docs/docs/api/query.md @@ -0,0 +1,373 @@ +--- +seo_title: "Deep Lake Query API | Efficient Multi-Modal Search" +description: "Deep Lake Query Engine Documentation and Examples." +toc_depth: 2 +--- +# Query + +Deep Lake provides powerful query capabilities through its Tensor Query Language (TQL), with special focus on vector similarity search, text search, and operations on multidimensional arrays. + + +## Query Functions + +::: deeplake.query + options: + heading_level: 3 + +::: deeplake.query_async + options: + heading_level: 3 + +::: deeplake.prepare_query + options: + heading_level: 3 + +::: deeplake.explain_query + options: + heading_level: 3 + +## Query Classes + +### Executor + +Prepared query execution object. + +::: deeplake.Executor + options: + heading_level: 4 + members: + - get_query_string + - run_single + - run_single_async + - run_batch + - run_batch_async + +### ExplainQueryResult + +Query explanation and analysis result. + +::: deeplake.ExplainQueryResult + options: + heading_level: 4 + members: + - to_dict + +## Vector Search + +Search by vector similarity: + + + +```python +# Cosine similarity search +text_vector = ','.join(str(x) for x in search_vector) +results = deeplake.query(f""" + SELECT * + FROM "s3://bucket/embeddings" + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[{text_vector}]) DESC + LIMIT 100 +""") +``` + +## Text Search + +Text search using BM25 or keyword matching: + +```python +# Semantic search using BM25 +results = deeplake.query(""" + SELECT * + FROM "s3://bucket/documents" + ORDER BY BM25_SIMILARITY(text, 'search query') DESC + LIMIT 10 +""") + +# Keyword search using CONTAINS +results = deeplake.query(""" + SELECT * + FROM "s3://bucket/metadata" + WHERE CONTAINS(keywords, 'specific term') +""") +``` + +## Array Operations + +Operate on multidimensional arrays: + +```python +# Select specific array dimensions +results = deeplake.query(""" + SELECT features[:, 0:10] + FROM "s3://bucket/features" +""") + +# Filter by array values +results = deeplake.query(""" + SELECT * + FROM "s3://bucket/features" + WHERE features[0] > 0.5 +""") + +# Aggregate array operations +results = deeplake.query(""" + SELECT AVG(features, axis=0) + FROM "s3://bucket/features" +""") +``` + +## Joining Datasets + +Join data across different datasets and across different clouds: + +```python +# Join datasets from different storage +results = deeplake.query(""" + SELECT i.image, i.embedding, m.labels, m.metadata + FROM "s3://bucket1/images" AS i + JOIN "s3://bucket2/metadata" AS m + ON i.id = m.image_id + WHERE m.verified = true +""") + +# Complex join with filtering +results = deeplake.query(""" + SELECT + i.image, + e.embedding, + l.label + FROM "s3://bucket1/images" AS i + JOIN "gcs://bucket2/embeddings" AS e ON i.id = e.image_id + JOIN "azure://container/labels" AS l ON i.id = l.image_id + WHERE l.confidence > 0.9 + ORDER BY COSINE_SIMILARITY(e.embedding, ARRAY[0.1, 0.2, 0.3]) DESC + LIMIT 100 +""") +``` + +## Filtering + +Filter data using WHERE clauses: + +```python +# Simple filters +results = deeplake.query(""" + SELECT * + FROM "s3://bucket/dataset" + WHERE label = 'cat' + AND confidence > 0.9 +""") + +# Combine with vector search +results = deeplake.query(""" + SELECT * + FROM "s3://bucket/dataset" + WHERE label IN ('cat', 'dog') + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[0.1, 0.2, 0.3]) DESC + LIMIT 100 +""") +``` + +## Query Results + +Process query results: + +```python +# Iterate through results +for item in results: + image = item["images"] + label = item["label"] + +# Direct column access (recommended for performance) +images = results["images"][:] +labels = results["labels"][:] +``` + +## Async Queries + +Execute queries asynchronously: + +```python +# Run query asynchronously +future = deeplake.query_async(""" + SELECT * + FROM "s3://bucket/dataset" + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[0.1,0.2, 0.3]) DESC +""") + +# Get results when ready +results = future.result() + +# Check completion +if future.is_completed(): + results = future.result() +else: + print("Query still running") +``` + +## Querying Views + +Chain queries on views: + +```python +# Initial query +view = deeplake.query("SELECT * FROM \"s3://bucket/animals\"") + +# Query on view +cats = view.query("SELECT * WHERE species = 'cat'") + +# Further filter +large_cats = cats.query('SELECT * WHERE weight > 10') +``` + +## Prepared Queries + +Prepare queries for reuse with different parameters: + +```python +# Prepare a parameterized query +s_executor = deeplake.prepare_query(""" + SELECT * + FROM "s3://bucket/dataset" + WHERE label = 'cat' + AND confidence > 0.5 +""") + +m_executor = deeplake.prepare_query(""" + SELECT * FROM "s3://bucket/dataset" + WHERE label = ? AND confidence > ? +""") + +# Execute with different parameters +cats_high = s_executor.run_single() + +# Batch execution +results = m_executor.run_batch([ + ["cat", 0.9], + ["dog", 0.8], + ["bird", 0.7] +]) + +# Async execution +future = m_executor.run_single_async(["cat", 0.95]) +result = future.result() + +# Get the query string +print(f"Query: {m_executor.get_query_string()}") +``` + +## Query Explanation + +Analyze query execution plans: + +```python +# Explain query performance +explanation = deeplake.explain_query(""" + SELECT * FROM "s3://bucket/large_dataset" + WHERE category = 'cat' + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[0.1, 0.2, 0.3]) DESC + LIMIT 1000 +""") + +# Print explanation +print(explanation) + +# Get explanation as dictionary +explain_dict = explanation.to_dict() +print(f"Execution plan: {explain_dict}") + +# Use explanation to optimize queries +if "index_used" in explain_dict: + print("Query will use indexes for optimization") +``` diff --git a/docs/docs/api/schemas.md b/docs/docs/api/schemas.md new file mode 100644 index 0000000000..ae3fa96405 --- /dev/null +++ b/docs/docs/api/schemas.md @@ -0,0 +1,331 @@ +--- +seo_title: "Deep Lake Schemas | Structured Data Templates" +description: "Deeplake builtin schemas to create dataset." +toc_depth: 2 +--- +# Schemas + +Deep Lake provides pre-built schema templates for common data structures. + + +## Schema Classes + +### Schema + +Mutable schema definition for datasets. + +::: deeplake.Schema + options: + heading_level: 4 + members: + - __getitem__ + - __len__ + - columns + +### SchemaView + +Read-only schema definition for datasets. + +::: deeplake.SchemaView + options: + heading_level: 4 + members: + - __getitem__ + - __len__ + - columns + +## Schema Templates + +Schema templates are Python dictionaries that define the structure of the dataset. Each schema template is a dictionary with field names as keys and field types as values. + +## Text Embeddings Schema + +::: deeplake.schemas.TextEmbeddings + options: + heading_level: 3 + + + +```python +# Create dataset with text embeddings schema +ds = deeplake.create("s3://bucket/dataset", + schema=deeplake.schemas.TextEmbeddings(768)) + +# Customize before creation +schema = deeplake.schemas.TextEmbeddings(768) +schema["text_embedding"] = schema.pop("embedding") +schema["source"] = deeplake.types.Text() +ds = deeplake.create("s3://bucket/dataset", schema=schema) + +# Add field to existing schema +schema = deeplake.schemas.TextEmbeddings(768) +schema["language"] = deeplake.types.Text() +ds = deeplake.create("s3://bucket/dataset", schema=schema) +``` + +## COCO Images Schema + +::: deeplake.schemas.COCOImages + options: + heading_level: 3 + +```python +# Basic COCO dataset +ds = deeplake.create("s3://bucket/dataset", + schema=deeplake.schemas.COCOImages(768)) + +# With keypoints and object detection +ds = deeplake.create("s3://bucket/dataset", + schema=deeplake.schemas.COCOImages( + embedding_size=768, + keypoints=True, + objects=True + )) + +# Customize schema +schema = deeplake.schemas.COCOImages(768) +schema["raw_image"] = schema.pop("image") +schema["camera_id"] = deeplake.types.Text() +ds = deeplake.create("s3://bucket/dataset", schema=schema) +``` + +## Working with Schema Objects + +Access and manipulate dataset schemas: + +```python +# Access dataset schema +ds = deeplake.open("s3://bucket/dataset") +schema = ds.schema + +# Get column definition +image_col = schema["images"] +print(f"Image column type: {image_col.dtype}") + +# Get number of columns +num_columns = len(schema) +print(f"Dataset has {num_columns} columns") + +# Read-only schema access +ro_ds = deeplake.open_read_only("s3://bucket/dataset") +ro_schema = ro_ds.schema + +# Access column definition (read-only) +label_col = ro_schema["labels"] +print(f"Label column type: {label_col.dtype}") +``` + +## Custom Schema Template + +Create custom schema templates: + +```python +# Define custom schema +schema = { + "id": deeplake.types.UInt64(), + "image": deeplake.types.Image(), + "embedding": deeplake.types.Embedding(512), + "metadata": deeplake.types.Dict() +} + +# Create dataset with custom schema +ds = deeplake.create("s3://bucket/dataset", schema=schema) + +# Modify schema +schema["timestamp"] = deeplake.types.UInt64() +schema.pop("metadata") +schema["image_embedding"] = schema.pop("embedding") +``` + +## Creating datasets from predefined data formats + +### from_coco + +Deep Lake provides a pre-built function to translate COCO format datasets into Deep Lake format. + +#### Key Features + +- **Multiple Annotation Support**: Handles instances, keypoints, and stuff annotations +- **Flexible Storage**: Works with both cloud and local storage +- **Data Preservation**: + - Converts segmentation to binary masks + - Preserves category hierarchies + - Maintains COCO metadata +- **Development Features**: + - Progress tracking during ingestion + - Configurable tensor and group mappings + +#### Basic Usage + +The basic flow for COCO ingestion is shown below: + +```python +import deeplake + +ds = deeplake.from_coco( + images_directory=images_directory, + annotation_files={ + "instances": instances_annotation, + "keypoints": keypoints_annotation, + "stuff": stuff_annotation, + }, + dest="al:///" +) +``` + + +#### Advanced Configuration + +You can customize group names and column mappings using `file_to_group_mapping` and `key_to_column_mapping`: + +```python +import deeplake + +ds = deeplake.from_coco( + images_directory=images_directory, + annotation_files={ + "instances": instances_annotation, + "keypoints": keypoints_annotation, + }, + dest="al:///", + file_to_group_mapping={ + "instances": "new_instances", + "keypoints": "new_keypoints1", + } +) +``` + +#### Important Notes + +- All segmentation polygons and RLEs are converted to stacked binary masks +- The code assumes all annotation files share the same image IDs +- Supports storage options are + - Deep Lake cloud storage + - S3 + - Azure Blob Storage + - Google Cloud Storage + - Local File System +- Provides progress tracking through optional tqdm integration diff --git a/docs/docs/api/types.md b/docs/docs/api/types.md new file mode 100644 index 0000000000..e5ca02d04a --- /dev/null +++ b/docs/docs/api/types.md @@ -0,0 +1,538 @@ +--- +toc_depth: 0 +seo_title: "Deep Lake Types | Multi-Modal Data Type System" +description: "Definition and Examples of types used in Deeplake Datasets and Columns" +--- + +# Types + +Deep Lake provides a comprehensive type system designed for efficient data storage and retrieval. The type system includes basic numeric types as well as specialized types optimized for common data formats like images, embeddings, and text. + + +Each type can be specified either using the full type class or a string shorthand: + + + +```python +# Using type class +ds.add_column("col1", deeplake.types.Float32()) + +# Using string shorthand +ds.add_column("col2", "float32") +``` + +#### Types determine: +- How data is stored and compressed +- What operations are available +- How the data can be queried and indexed +- Integration with external libraries and frameworks + +## Numeric Types + +All basic numeric types: + +```python +import deeplake + +# Integers +ds.add_column("int8", deeplake.types.Int8()) # -128 to 127 +ds.add_column("int16", deeplake.types.Int16()) # -32,768 to 32,767 +ds.add_column("int32", deeplake.types.Int32()) # -2^31 to 2^31-1 +ds.add_column("int64", deeplake.types.Int64()) # -2^63 to 2^63-1 + +# Unsigned Integers +ds.add_column("uint8", deeplake.types.UInt8()) # 0 to 255 +ds.add_column("uint16", deeplake.types.UInt16()) # 0 to 65,535 +ds.add_column("uint32", deeplake.types.UInt32()) # 0 to 2^32-1 +ds.add_column("uint64", deeplake.types.UInt64()) # 0 to 2^64-1 + +# Floating Point +ds.add_column("float16", deeplake.types.Float16()) # Half precision +ds.add_column("float32", deeplake.types.Float32()) # Single precision +ds.add_column("float64", deeplake.types.Float64()) # Double precision + +# Boolean +ds.add_column("is_valid", deeplake.types.Bool()) # True/False values +``` + +## Basic Type Functions + +::: deeplake.types.Int8 + options: + heading_level: 3 + +::: deeplake.types.Int16 + options: + heading_level: 3 + +::: deeplake.types.Int32 + options: + heading_level: 3 + +::: deeplake.types.Int64 + options: + heading_level: 3 + +::: deeplake.types.UInt8 + options: + heading_level: 3 + +::: deeplake.types.UInt16 + options: + heading_level: 3 + +::: deeplake.types.UInt32 + options: + heading_level: 3 + +::: deeplake.types.UInt64 + options: + heading_level: 3 + +::: deeplake.types.Float16 + options: + heading_level: 3 + +::: deeplake.types.Float32 + options: + heading_level: 3 + +::: deeplake.types.Float64 + options: + heading_level: 3 + +::: deeplake.types.Bool + options: + heading_level: 3 + +::: deeplake.types.ClassLabel + options: + heading_level: 3 + +### Numeric Indexing + +Numeric columns support indexing for efficient comparison operations: + +```python +# Create numeric column with inverted index for range queries +ds.add_column("timestamp", deeplake.types.UInt64()) + +# Create the index manually +ds["timestamp"].create_index( + deeplake.types.NumericIndex(deeplake.types.Inverted) +) + +# Now you can use efficient comparison operations in queries: +# - Greater than: WHERE timestamp > 1609459200 +# - Less than: WHERE timestamp < 1640995200 +# - Between: WHERE timestamp BETWEEN 1609459200 AND 1640995200 +# - Value list: WHERE timestamp IN (1609459200, 1640995200) +``` + +::: deeplake.types.Audio + options: + heading_level: 2 + +```python +# Basic audio storage +ds.add_column("audio", deeplake.types.Audio()) + +# WAV format +ds.add_column("audio", deeplake.types.Audio( + sample_compression="wav" +)) + +# MP3 compression (default) +ds.add_column("audio", deeplake.types.Audio( + sample_compression="mp3" +)) + +# With specific dtype +ds.add_column("audio", deeplake.types.Audio( + dtype="uint8", + sample_compression="wav" +)) + +# Audio with Link for external references +ds.add_column("audio_links", deeplake.types.Link( + deeplake.types.Audio(sample_compression="mp3") +)) +``` + +::: deeplake.types.Image + options: + heading_level: 2 + +```python +# Basic image storage +ds.add_column("images", deeplake.types.Image()) + +# JPEG compression +ds.add_column("images", deeplake.types.Image( + sample_compression="jpeg" +)) + +# With specific dtype +ds.add_column("images", deeplake.types.Image( + dtype="uint8" # 8-bit RGB +)) +``` + +::: deeplake.types.Embedding + options: + heading_level: 2 + +```python +# Basic embeddings +ds.add_column("embeddings", deeplake.types.Embedding(768)) + +# With binary quantization for faster search +ds.add_column("embeddings", deeplake.types.Embedding( + size=768, + index_type=deeplake.types.EmbeddingIndex(deeplake.types.ClusteredQuantized) +)) + +# Custom dtype +ds.add_column("embeddings", deeplake.types.Embedding( + size=768, + dtype="float32" +)) +``` + +::: deeplake.types.Text + options: + heading_level: 2 + +```python +# Basic text +ds.add_column("text", deeplake.types.Text()) + +# Text with BM25 index for semantic search +ds.add_column("text2", deeplake.types.Text( + index_type=deeplake.types.BM25 +)) + +# Text with inverted index for keyword search +ds.add_column("text3", deeplake.types.Text( + index_type=deeplake.types.Inverted +)) + +# Text with exact index for whole text matching +ds.add_column("text4", deeplake.types.Text( + index_type=deeplake.types.Exact +)) +``` + +::: deeplake.types.Dict + options: + heading_level: 2 + +```python +# Store arbitrary key/value pairs +ds.add_column("metadata", deeplake.types.Dict()) + +# Add data +ds.append([{ + "metadata": { + "timestamp": "2024-01-01", + "source": "camera_1", + "settings": {"exposure": 1.5} + } +}]) +``` + +::: deeplake.types.Array + options: + heading_level: 2 + +```python +# Fixed-size array +ds.add_column("features", deeplake.types.Array( + "float32", + shape=[512] # Enforces size +)) + +# Variable-size array +ds.add_column("sequences", deeplake.types.Array( + "int32", + dimensions=1 # Allows any size +)) +``` + +## Numeric Indexes + +Deep Lake supports indexing numeric columns for faster lookup operations: + +```python +from deeplake.types import NumericIndex, Inverted +# Add numeric column and create an inverted index +ds.add_column("scores", "float32") +ds["scores"].create_index(NumericIndex(Inverted)) + +# Use with TQL for efficient filtering +results = ds.query("SELECT * WHERE CONTAINS(scores, 0.95)") +``` + +::: deeplake.types.Bytes + options: + heading_level: 2 + +::: deeplake.types.BinaryMask + options: + heading_level: 2 + +```python +# Basic binary mask +ds.add_column("masks", deeplake.types.BinaryMask()) + +# With compression +ds.add_column("masks", deeplake.types.BinaryMask( + sample_compression="lz4" +)) +``` + +::: deeplake.types.SegmentMask + options: + heading_level: 2 + +```python +# Basic segmentation mask +ds.add_column("segmentation", deeplake.types.SegmentMask()) + +# With compression +ds.add_column("segmentation", deeplake.types.SegmentMask( + dtype="uint8", + sample_compression="lz4" +)) +``` + +::: deeplake.types.BoundingBox + options: + heading_level: 2 + +```python +# Basic bounding boxes +ds.add_column("boxes", deeplake.types.BoundingBox()) + +# With specific format +ds.add_column("boxes", deeplake.types.BoundingBox( + format="ltwh" # left, top, width, height +)) +``` + +::: deeplake.types.Point + options: + heading_level: 2 + +::: deeplake.types.Polygon + options: + heading_level: 2 + + +::: deeplake.types.Video + options: + heading_level: 2 + +::: deeplake.types.Medical + options: + heading_level: 2 + +::: deeplake.types.Mesh + options: + heading_level: 2 + +::: deeplake.types.Struct + options: + heading_level: 2 + +```python +# Define fixed structure with specific types +ds.add_column("info", deeplake.types.Struct({ + "id": deeplake.types.Int64(), + "name": "text", + "score": deeplake.types.Float32() +})) + +# Add data +ds.append([{ + "info": { + "id": 1, + "name": "sample", + "score": 0.95 + } +}]) +``` + +::: deeplake.types.Sequence + options: + heading_level: 2 + +```python +# Sequence of images (e.g., video frames) +ds.add_column("frames", deeplake.types.Sequence( + deeplake.types.Image(sample_compression="jpeg") +)) + +# Sequence of embeddings +ds.add_column("token_embeddings", deeplake.types.Sequence( + deeplake.types.Embedding(768) +)) + +# Add data +ds.append([{ + "frames": [frame1, frame2, frame3], # List of images + "token_embeddings": [emb1, emb2, emb3] # List of embeddings +}]) +``` + +::: deeplake.types.Link + options: + heading_level: 2 + +## Index Types + +Deep Lake supports several index types for optimizing queries on different data types. + +### IndexType Enum + +::: deeplake.types.IndexType + options: + heading_level: 3 + +### Text Index Types + +::: deeplake.types.TextIndex + options: + heading_level: 3 + +::: deeplake.types.Inverted + options: + heading_level: 3 + +::: deeplake.types.BM25 + options: + heading_level: 3 + +::: deeplake.types.Exact + options: + heading_level: 3 + +### Numeric Index Types + +::: deeplake.types.NumericIndex + options: + heading_level: 2 + +### JSON Index Types + +::: deeplake.types.JsonIndex + options: + heading_level: 2 + +### Embedding Index Types + +::: deeplake.types.EmbeddingIndexType + options: + heading_level: 2 + +::: deeplake.types.EmbeddingIndex + options: + heading_level: 2 + +::: deeplake.types.EmbeddingsMatrixIndexType + options: + heading_level: 2 + +::: deeplake.types.EmbeddingsMatrixIndex + options: + heading_level: 2 + +### Generic Index Wrapper + +::: deeplake.types.Index + options: + heading_level: 2 + +```python +# Create numeric index for efficient range queries +ds.add_column("age", deeplake.types.Int32()) +ds["age"].create_index( + deeplake.types.NumericIndex(deeplake.types.Inverted) +) + +# Use in queries with comparison operators +results = ds.query("SELECT * WHERE age > 25") +results = ds.query("SELECT * WHERE age BETWEEN 18 AND 65") +results = ds.query("SELECT * WHERE age IN (25, 30, 35)") +``` diff --git a/docs/docs/api/version_control.md b/docs/docs/api/version_control.md new file mode 100644 index 0000000000..4c95c67cae --- /dev/null +++ b/docs/docs/api/version_control.md @@ -0,0 +1,327 @@ +--- +seo_title: "Activeloop Deep Lake Docs Version Control" +description: "Access Deep Lake Documentation For Version Control." +toc_depth: 2 +--- +# Version Control + + + + +## Version + +::: deeplake.Version + options: + heading_level: 4 + members: + - client_timestamp + - id + - message + - open + - open_async + - timestamp + +```python +# Get current version +version_id = ds.version + +# Access specific version +version = ds.history[version_id] +print(f"Version: {version.id}") +print(f"Message: {version.message}") +print(f"Timestamp: {version.timestamp}") + +# Open dataset at specific version +old_ds = version.open() + +# Open dataset at specific version asynchronously +old_ds_future = version.open_async() +``` + +## History + +::: deeplake.History + options: + heading_level: 4 + members: + - __getitem__ + - __iter__ + +```python +# View all versions +for version in ds.history: + print(f"Version {version.id}: {version.message}") + print(f"Created: {version.timestamp}") + +# Get specific version +version = ds.history[version_id] + +# Get version by index +first_version = ds.history[0] +latest_version = ds.history[-1] +``` + +## Branching + +::: deeplake.Branch + options: + heading_level: 4 + members: + - base + - delete + - id + - name + - open + - open_async + - rename + - timestamp + +```python +# Create branch +ds.branch("Branch1") + +# Access branch +branch = ds.branches["Branch1"] +print(f"Branch: {branch.name}") +print(f"Created: {branch.timestamp}") +print(f"Base: {branch.base}") + +# Open dataset at branch +branch_ds = branch.open() + +# Open dataset at branch asynchronously +branch_ds_future = branch.open_async() + +# Rename branch +branch.rename("Other Branch") + +# Delete branch +branch.delete() +``` + +::: deeplake.Branches + options: + heading_level: 4 + members: + - __str__ + - __getitem__ + - __len__ + - names + +```python +# Create branch +ds.branch("B1") + +# List all branches +for name in ds.branches.names(): + br = ds.branches[name] + print(f"Branch: {br.name} based on {br.base}") + +# Check number of branches +num_branches = len(ds.branches) + +# Access specific branch +branch = ds.branches["main"] + +# Common operations with branches +branch_ds = ds.branches["B1"].open() # Open branch +branch_future = ds.branches["B1"].open_async() # Async open + +# Error handling +try: + branch = ds.branches["non_existent"] +except deeplake.BranchNotFoundError: + print("Branch not found") +``` + +::: deeplake.BranchView + options: + heading_level: 4 + members: + - base + - id + - name + - open + - open_async + - timestamp + +```python +# Open read-only dataset +ds = deeplake.open_read_only("s3://bucket/dataset") + +# Access branch view +branch_view = ds.branches["B1"] +print(f"Branch: {branch_view.name}") +print(f"Created: {branch_view.timestamp}") + +# Open branch view +branch_ds = branch_view.open() + +# Open branch view asynchronously +branch_future = branch_view.open_async() +``` + +::: deeplake.BranchesView + options: + heading_level: 4 + members: + - __getitem__ + - __len__ + - names + +```python +# Access read-only branches +branches_view = ds.branches + +# List branch names +for name in branches_view.names(): + print(f"Found branch: {name}") + +# Get specific branch +branch_view = branches_view["B1"] +``` + +## Tagging + +::: deeplake.Tag + options: + heading_level: 4 + members: + - delete + - id + - message + - name + - open + - open_async + - rename + - timestamp + - version + +```python +# Create tag +ds.tag("v1.0") + +# Access tagged version +tag = ds.tags["v1.0"] +print(f"Tag: {tag.name}") +print(f"Version: {tag.version}") + +# Open dataset at tag +tagged_ds = tag.open() +# Open dataset at tag asynchronously +tagged_ds_future = tag.open_async() + +# Rename tag +tag.rename("v1.0.0") + +# Delete tag +tag.delete() +``` + +::: deeplake.Tags + options: + heading_level: 4 + members: + - __getitem__ + - __len__ + - names + +```python +# Create tag +ds.tag("v1.0") # Tag current version +specific_version = ds.version +ds.tag("v2.0", version=specific_version) # Tag specific version + +# List all tags +for name in ds.tags.names(): + tag = ds.tags[name] + print(f"Tag {tag.name} points to version {tag.version}") + +# Check number of tags +num_tags = len(ds.tags) + +# Access specific tag +tag = ds.tags["v1.0"] + +# Common operations with tags +latest_ds = ds.tags["v2.0"].open() # Open dataset at tag +stable_ds = ds.tags["v1.0"].open_async() # Async open + +# Error handling +try: + tag = ds.tags["non_existent"] +except deeplake.TagNotFoundError: + print("Tag not found") +``` + +::: deeplake.TagView + options: + heading_level: 4 + members: + - id + - message + - name + - open + - open_async + - timestamp + - version + +```python +# Open read-only dataset +ds = deeplake.open_read_only("s3://bucket/dataset") + +# Access tag view +tag_view = ds.tags["v1.0"] +print(f"Tag: {tag_view.name}") +print(f"Version: {tag_view.version}") + +# Open dataset at tag +tagged_ds = tag_view.open() + +# Open dataset at tag asynchronously +tagged_future = tag_view.open_async() +``` + +::: deeplake.TagsView + options: + heading_level: 4 + members: + - __getitem__ + - __len__ + - names + +```python +# Access read-only tags +tags_view = ds.tags + +# List tag names +for name in tags_view.names(): + print(f"Found tag: {name}") + +# Get specific tag +tag_view = tags_view["v1.0"] +``` diff --git a/docs/docs/concepts/.pages b/docs/docs/concepts/.pages new file mode 100644 index 0000000000..c764a8509a --- /dev/null +++ b/docs/docs/concepts/.pages @@ -0,0 +1,4 @@ +nav: + - Architecture: architecture.md + - Data Model: data-model.md + - Performance: performance.md \ No newline at end of file diff --git a/docs/docs/css/custom.css b/docs/docs/css/custom.css new file mode 100644 index 0000000000..4696b1ac93 --- /dev/null +++ b/docs/docs/css/custom.css @@ -0,0 +1,158 @@ + [data-md-color-scheme="slate"] { + --md-primary-fg-color: #1f2129; + --md-primary-fg-color--light: #ECB7B7; + --md-primary-fg-color--dark: #90030C; + } + + +.md-search { + margin: 0 6px 0 0; +} + +.md-content { + margin-bottom: 100px; +} + +.doc-function, .doc-attribute { + border-bottom: 2px lightgrey dashed; + margin-bottom: 40px; +} +.md-nav__link { + padding: 5px ; + margin: 0 !important +} +.md-nav__link--active { + font-weight: 700; + font-size: 16px; + color: #0b61ff !important; +} +#next-steps { + font-size: 30px; + font-weight: bold; +} +.md-header__source { + margin: 0px !important; + position: relative; + z-index: 2; + width: auto; + display: block !important; +} +.md-header--shadow { + box-shadow: none; + border-bottom: .05rem solid #00000012; +} +[data-md-color-scheme="slate"] .md-header--shadow { + border-bottom: .05rem solid #484848; /* Dark mode color */ +} +.md-header__source .md-source { + width: 200px; +} +.md-header-nav__button { + position: relative; + z-index: 1; +} + +/*Remove the site title from the header while keeping the version selector*/ +.md-header__topic .md-ellipsis { + display: none; +} +.jupiter_download .md-content__button { + border-top: 1px dashed lightgray; + padding-top: 8px; + float: none; + display: block !important; + color: darkgrey +} +.md-content__button:nth-child(1){ + display: none; +} +.md-footer { + display: none; +} + +[data-md-color-scheme="slate"] .md-nav__link:hover { + color: #fff !important; + background: rgba(255, 255, 255, 0.2); +} +[data-md-color-scheme="default"] .md-nav__link:hover { + color: #000 !important; + background: rgba(233, 233, 233, 0.6); +} +[data-md-color-scheme="slate"] .md-nav__link:focus { + color: #fff !important; + font-weight: 700; +} +[data-md-color-scheme="default"] .md-nav__link:focus { + color: #000 !important; + font-weight: 700; +} +[data-md-color-scheme="slate"] .md-nav__link--active { + color: #fff !important; +} +[data-md-color-scheme="default"] .md-nav__link--active { + color: #000 !important; +} + +[data-md-color-scheme="default"] .md-header__ellipsis a { + color: #000 !important; +} +[data-md-color-scheme="default"] .v3-docs-btn { + color: #000 !important; + border: 1px solid rgb(237 237 237) !important; +} + +[data-md-color-scheme="default"] .notFound p { + color: #666666 +} + +[data-md-color-scheme="default"] .v3-info { + color: #666666 +} + +@media screen and (max-width: 500px) { + .md-header__source { + width: 60px + } + .md-source__repository { + display: none; + } +} + + .screenshot { + border: 1px solid lightgrey; + } + + + .md-nav__link:before { + content: ""; + } + + [data-md-color-scheme="default"] .md-version__link:hover, .md-version__link:focus { + color: #000 !important; + background: #ffffff !important; + } + + [data-md-color-scheme="slate"] .md-version__link:hover, .md-version__link:focus { + color: #fff !important; + background: #1f2129 !important; + } + + #__nav_1_label::before { + content: '🏗️'; /* Emoji for Quick Start */ + padding-left: 7px; + } + + #__nav_2_label::before { + content: '📚'; /* Emoji for Setup */ + padding-left: 7px; + } + + #__nav_3_label::before { + content: '🔬'; /* Emoji for User Guides */ + padding-left: 7px; + } + + #__nav_4_label::before { + content: '🐍'; /* Emoji for User Guides */ + padding-left: 7px; + } \ No newline at end of file diff --git a/docs/docs/getting-started/.pages b/docs/docs/getting-started/.pages new file mode 100644 index 0000000000..7104bc686e --- /dev/null +++ b/docs/docs/getting-started/.pages @@ -0,0 +1,4 @@ +nav: + - Quickstart: quickstart.md + - Authentication: authentication.md + - Storage and Credentials: storage-and-creds \ No newline at end of file diff --git a/docs/docs/getting-started/authentication.md b/docs/docs/getting-started/authentication.md new file mode 100644 index 0000000000..c92c804c5b --- /dev/null +++ b/docs/docs/getting-started/authentication.md @@ -0,0 +1,34 @@ +--- +seo_title: "Deep Lake Authentication | Secure Access For AI Workflows" +description: "Set Up Authentication Protocols For Secure Access To Deep Lake Datasets, Ensuring Data Integrity And Privacy In Collaborative Machine Learning Environments." +--- + +# User Authentication + +## How to Register and Authenticate in Deep Lake + +### Registration and Login + +In order to use Deep Lake features that require authentication (Activeloop storage, connecting your cloud dataset to the Deep Lake UI, etc.) you should register and login in the [Deep Lake App](https://app.activeloop.ai/). + +### Authentication in Programmatic Interfaces + +You can create an API token in the [Deep Lake App](https://app.activeloop.ai/) and authenticate in programatic interfaces using 2 options: + +#### Environmental Variable + +Set the environmental variable `ACTIVELOOP_TOKEN` to your API token. In Python, this can be done using: + +`os.environ['ACTIVELOOP_TOKEN'] = ` + +#### Pass the Token to Individual Methods + +You can pass your API token to individual methods that require authentication such as: + +`ds = deeplake.open('al://org_name/dataset_name', token = )` + +## Next Steps + +Now that you have a local dataset, you can learn more about: + +- [Activeloop Managed Credentials](storage-and-creds/managed-credentials/index.md) diff --git a/docs/docs/getting-started/quickstart.md b/docs/docs/getting-started/quickstart.md new file mode 100644 index 0000000000..c50f79a377 --- /dev/null +++ b/docs/docs/getting-started/quickstart.md @@ -0,0 +1,190 @@ +--- +seo_title: "Activeloop Deep Lake Docs | Multi-Modal AI Search | API Reference & Guides" +description: "Access Deep Lake Documentation For Complete Setup, API Reference, Guides On Efficient Multi-Modal AI Search, Dataset Management, Cost-Efficient Training, And Retrieval-Augmented Generation." +hide: + - toc +--- + +# Quickstart Guide + +Get started with Deep Lake by following these examples. + +## Installation + +Deep Lake can be installed using pip: + +```bash +pip install deeplake +``` + +## Creating a Dataset + + + +```python +import deeplake + +# Create a local dataset +ds = deeplake.create("path/to/dataset") + +# Or create in cloud storage +ds = deeplake.create("s3://my-bucket/dataset") +ds = deeplake.create("gcs://my-bucket/dataset") +ds = deeplake.create("azure://container/dataset") +``` + +## Adding Data + +Add columns to store different types of data: + +```python +# Add basic data types +ds.add_column("ids", "int32") +ds.add_column("labels", "text") + +# Add specialized data types +ds.add_column("images", deeplake.types.Image()) +ds.add_column("videos", deeplake.types.Video()) +ds.add_column("embeddings", deeplake.types.Embedding(768)) +ds.add_column("masks", deeplake.types.BinaryMask()) +``` + +Insert data into the dataset: + +```python +# Add single samples +ds.append([{ + "ids": 1, + "labels": "cat", + "images": image_array, + "videos": video_bytes, + "embeddings": embedding_vector, + "masks": mask_array +}]) + +# Add batches of data +ds.append({ + "ids": [1, 2, 3], + "labels": ["cat", "dog", "bird"], + "images": batch_of_images, + "videos": batch_of_videos, + "embeddings": batch_of_embeddings, + "masks": batch_of_masks +}) + +ds.commit() # Commit changes to the storage +``` + +## Accessing Data + +Access individual samples: + +```python +# Get single items +image = ds["images"][0] +label = ds["labels"][0] +embedding = ds["embeddings"][0] + +# Get ranges +images = ds["images"][0:100] +labels = ds["labels"][0:100] + +# Get specific indices +selected_images = ds["images"][[0, 2, 3]] +``` + +## Vector Search + +Search by embedding similarity: + +```python +# Find similar items +text_vector = ','.join(str(x) for x in search_vector) +results = ds.query(f""" + SELECT * + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[{text_vector}]) DESC + LIMIT 100 +""") + +# Process results - Method 1: iterate through items +for item in results: + image = item["images"] + label = item["labels"] + +# Process results - Method 2: direct column access +images = results["images"][:] +labels = results["labels"][:] # Recommended for better performance +``` + +## Data Versioning + +```python +# Commit changes +ds.commit("Added initial data") + +# Create version tag +ds.tag("v1.0") + +# View history +for version in ds.history: + print(version.id, version.message) + +# Create a new branch +ds.branch("new-branch") +### Add new data to the branch ... +main_ds = ds.branches['main'].open() +main_ds.merge("new-branch") +``` + +## Async Operations + +Use async operations for better performance: + +```python +# Async data loading +future = ds["images"].get_async(slice(0, 1000)) +images = future.result() + +# Async query +future = ds.query_async( + "SELECT * WHERE labels = 'cat'" +) +cats = future.result() +``` + +## Next Steps + +- Explore [RAG applications](../../guide/rag) +- Check out [Deep Learning integration](../../guide/deep-learning/deep-learning) + +## Support + +If you encounter any issues: + +1. Check our [GitHub Issues](https://github.com/activeloopai/deeplake/issues) +2. Join our [Slack Community](https://slack.activeloop.ai) diff --git a/docs/docs/getting-started/storage-and-creds/index.md b/docs/docs/getting-started/storage-and-creds/index.md new file mode 100644 index 0000000000..7f811697d3 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/index.md @@ -0,0 +1,27 @@ +--- +seo_title: "Storage And Credentials | Set Up Scalable Data Access" +description: "Comprehensive Guide To Configuring Storage And Credentials In Deep Lake 4.0, Including Cloud, Local, And Hybrid Storage Options For Efficient AI Model Data Handling." +--- + +# Storage and Credentials + +How to access datasets in other clouds and manage their credentials. + +## Storing Datasets in Your Own Cloud + +Deep Lake can be used as a pure OSS package without any registration or relationship with Activeloop. However, registering with Activeloop offers several benefits: + +- Storage provided by Activeloop +- Access to [Deep Lake App](https://app.activeloop.ai), which provides dataset visualization, querying, version control UI, dataset analytics, and other powerful features +- [Managed credentials](managed-credentials/index.md) for Deep Lake datasets stored outside of Activeloop + +!!! tip + + When connecting data from your cloud using Managed Credentials, the data is never stored or cached in Deep Lake. All Deep Lake user interfaces (browser, python, etc.) fetch data directly from long-term storage. + +![Authentication_With_Managed_Creds.png](managed-credentials/images/Authentication_With_Managed_Creds.png) + +## Next Steps + +- [Storage Options](storage-options.md) +- [Configuring Managed Credentials](managed-credentials/index.md) \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/.pages b/docs/docs/getting-started/storage-and-creds/managed-credentials/.pages new file mode 100644 index 0000000000..61dbdd83cc --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/.pages @@ -0,0 +1,6 @@ +nav: + - Overview: index.md + - AWS: aws + - Azure: azure + - GCP: gcp + - ... \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/cors.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/cors.md new file mode 100644 index 0000000000..0d8f1640c9 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/cors.md @@ -0,0 +1,30 @@ +--- +seo_title: "AWS CORS Setup | Configure Secure Cross-Origin Access" +description: "Follow This Guide To Configure CORS For AWS With Deep Lake 4.0, Ensuring Secure Cross-Origin Resource Sharing And Compliance For Machine Learning Training Data." +--- + +# Enabling CORS in S3 + +In order to visualize Deep Lake datasets stored in your own S3 buckets in the [Deep Lake app](https://app.activeloop.ai/), please enable [Cross-Origin Resource Sharing (CORS)](https://en.wikipedia.org/wiki/Cross-origin\_resource\_sharing) in the buckets containing the Deep Lake dataset and any linked data, by inserting the snippet below in the CORS section of the Permissions tab for the bucket: + +``` +[ + { + "AllowedHeaders": [ + "*" + ], + "AllowedMethods": [ + "GET", + "HEAD" + ], + "AllowedOrigins": [ + "*.activeloop.ai" + ], + "ExposeHeaders": [] + } +] +``` + +## Next Steps + +- [Provisioning Role-Based Access](./provisioning.md) diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.001.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.001.jpeg new file mode 100644 index 0000000000..c617816cfd Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.001.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.002.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.002.jpeg new file mode 100644 index 0000000000..a76dc87d5a Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.002.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.003.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.003.jpeg new file mode 100644 index 0000000000..d5afb6b3a4 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.003.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.004.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.004.jpeg new file mode 100644 index 0000000000..6000e1e8b4 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.004.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.005.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.005.jpeg new file mode 100644 index 0000000000..e2d0835d3c Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.005.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.006.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.006.jpeg new file mode 100644 index 0000000000..ee6cd35d80 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.006.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.007.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.007.jpeg new file mode 100644 index 0000000000..98449eb2d7 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.007.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.008.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.008.jpeg new file mode 100644 index 0000000000..b325f502ec Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.008.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.009.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.009.jpeg new file mode 100644 index 0000000000..37ca6bc7b4 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.009.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.010.jpeg b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.010.jpeg new file mode 100644 index 0000000000..dd985325db Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/images/IAM_Provisioning_Screenshots.010.jpeg differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/provisioning.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/provisioning.md new file mode 100644 index 0000000000..b0fd251e08 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/aws/provisioning.md @@ -0,0 +1,146 @@ +--- +seo_title: "AWS Provisioning | Resource Setup For ML Data Storage" +description: "Provision AWS Resources To Store And Access Deep Lake Datasets, Supporting Scalable Machine Learning Training With Optimized Cloud Storage Setup." +--- + +# Provisioning Role-Based Access + +## Setting up Role-Based Access for AWS S3 + +The most secure method for connecting data from your AWS account to Deep Lake is using Federated Credentials and Role-Based Access, which are set up using the steps below: + +#### Step 1: Create the AWS IAM Policy + +1\. Login to the AWS account where the IAM Role will be created and where the data is stored. + +2\. Go to the IAM page in the AWS UI, which can be done by searching "IAM" in the console and locating the IAM page under Services. + +3\. In the left nav, open the `Policies` under `Access management` and on `Create policy` on the right. + +
+ +5\. Select the `JSON` tab instead of `Visual editor`. + +
+ +6\. Replace the code in the editor with the code below. Replace `BUCKET_NAME` with the bucket names for which you want to grant role-based access: + +``` +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:ListBucket", + "s3:GetBucketLocation", + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject" + ], + "Resource": [ + "arn:aws:s3:::BUCKET_NAME", + "arn:aws:s3:::BUCKET_NAME/*" + ] + } + ] +} + +``` + +7\. On the bottom right, click `Next: Tags` (create tags if needed) and `Next: Preview`, enter the policy `name` and `description`, and click `Create policy` + +
+ +#### Step 2: Create the AWS IAM Role + +1\. On the `IAM` page, in the left nav, open the `Roles` under `Access management`, and click `Create role` on the right. + +
+ +3\. Select `Custom trust policy` from the list of options. + +
+ +4\. Replace the policy definition with the code below and click `Next` + +``` +{ + "Version": "2012-10-17", + "Statement": + [ + { + "Sid": "AllowAssumeRoleFromActiveloopSaaS", + "Effect": "Allow", + "Principal": { + "AWS": "arn:aws:iam::597713067985:role/activeloop_backend" + }, + "Action": "sts:AssumeRole" + } + ] +} +``` + +5\. From the provided policy list, select the previously created policy from Step 1 and click `Next` + +
+ +6\. Set the `name` and `description` for the role and click `Create role` at the bottom. + +
+ +#### Step 3: Grant Access to AWS KMS Key (**only for buckets that are encrypted with customer managed KMS keys**) + +1\. Navigate to the bucket in the AWS S3 UI + +2\. Open the bucket Properties + +
+ +3\. Scroll down to Default encryption and copy the `AWS KMS key ARN` + +
+ +4\. In the Policy creation step (Step 1, Sub-step 6), use the JSON below in the policy statement, and replace `YOUR_KMS_KEY_ARN` with the copied Key ARN for the encrypted bucket. + +``` +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:ListBucket", + "s3:GetBucketLocation", + "s3:*Object*" + ], + "Resource": [ + "arn:aws:s3:::BUCKET_NAME", + "arn:aws:s3:::BUCKET_NAME/*" + ] + }, + { + "Effect": "Allow", + "Action": [ + "kms:Encrypt", + "kms:Decrypt", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:DescribeKey" + ], + "Resource": [ + "YOUR_KMS_KEY_ARN” + ] + } + ] +} + +``` + +#### Step 4: Enter the created AWS Role ARN (Step 2) into the Activeloop UI + +See the first video in the [managed credentials overview](../index.md) + +## Next Steps + +- [Enabling CORS in AWS S3](cors.md) \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/cors.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/cors.md new file mode 100644 index 0000000000..cc67bcf7e5 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/cors.md @@ -0,0 +1,30 @@ +--- +seo_title: "Azure CORS Setup | Enable Secure Data Access" +description: "Set Up Azure CORS With Deep Lake To Enable Secure Cross-Origin Requests, Providing Efficient Access To Training Data For Machine Learning Projects." +--- + +# Enabling CORS in Azure + +Cross-Origin Resource Sharing (CORS) is typically enabled by default in Azure. If that's not the case in your Azure account, please enable [CORS](https://en.wikipedia.org/wiki/Cross-origin\_resource\_sharing) in order to use the [Deep Lake app](https://app.activeloop.ai/) to visualize Deep Lake datasets stored in your own Azure storage. [CORS](https://en.wikipedia.org/wiki/Cross-origin\_resource\_sharing) should be enabled in the storage account containing the Deep Lake dataset and any linked data. + +## Steps for enabling CORS in Azure + +1\. Login to the Azure. + +2\. Navigate to the `Storage account` with the relevant data. + +3\. Open the `Resource sharing (CORS)` section on the left nav. + +
+ +4\. Add the following items to the permissions. + +
+ +| Allowed origins | Allowed methods | Allowed headers | +| ------------------------- | --------------- | --------------- | +| https://app.activeloop.ai | GET, HEAD | \* | + +## Next Steps + +- [Provisioning Federated Credentials](provisioning.md) \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_CORS_1.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_CORS_1.png new file mode 100644 index 0000000000..a354c82544 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_CORS_1.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_CORS_2.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_CORS_2.png new file mode 100644 index 0000000000..6600fb17a6 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_CORS_2.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_3.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_3.png new file mode 100644 index 0000000000..d8f6970510 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_3.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_4.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_4.png new file mode 100644 index 0000000000..11bf57e333 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_4.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_5.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_5.png new file mode 100644 index 0000000000..a94514c3ac Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_5.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_6.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_6.png new file mode 100644 index 0000000000..bc64fbd4c4 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_6.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_7.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_7.png new file mode 100644 index 0000000000..da0ac69bad Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_7.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_8.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_8.png new file mode 100644 index 0000000000..4dbdf00c3b Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_1_8.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_2.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_2.png new file mode 100644 index 0000000000..9e062d4fee Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_2.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_3.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_3.png new file mode 100644 index 0000000000..7201103114 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_3.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_4.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_4.png new file mode 100644 index 0000000000..329c4fc28c Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_4.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_5.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_5.png new file mode 100644 index 0000000000..939058bd53 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_5.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_6.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_6.png new file mode 100644 index 0000000000..a2be65af3d Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2a_6.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_2.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_2.png new file mode 100644 index 0000000000..9e062d4fee Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_2.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_3.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_3.png new file mode 100644 index 0000000000..91bec1aa26 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_3.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_4.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_4.png new file mode 100644 index 0000000000..35246efb5a Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/Azure_Federated_Step_2b_4.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_1.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_1.png new file mode 100644 index 0000000000..f4c351c5d8 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_1.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_2.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_2.png new file mode 100644 index 0000000000..771ab88f62 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_2.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_3.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_3.png new file mode 100644 index 0000000000..97f36e3e0d Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_1_3.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_2.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_2.png new file mode 100644 index 0000000000..63fd7e6bfc Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_2.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_3_1.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_3_1.png new file mode 100644 index 0000000000..679435b8a1 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_3_1.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_3_2.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_3_2.png new file mode 100644 index 0000000000..5d2649ec8f Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_3_2.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_4.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_4.png new file mode 100644 index 0000000000..efe59fa3c2 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/images/workload_step_4.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/provisioning.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/provisioning.md new file mode 100644 index 0000000000..86d2cf64c9 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/provisioning.md @@ -0,0 +1,99 @@ +--- +seo_title: "Azure Provisioning | Set Up Storage For AI Data" +description: "Step-By-Step Guide For Provisioning Azure Storage With Deep Lake, Ensuring Secure And Scalable Data Access To Support Machine Learning Workflows." +--- + +# Provisioning Federated Credentials + +## Setting up Federated Credentials in Microsoft Azure + +The most secure method for connecting data from your Azure storage to Deep Lake is using Federated Credentials, which are set up using the steps below: + +#### Step 1: Register Application Credentials with the Microsoft Identity Platform + +1\. Login to the Azure account where the App will be registered and where the data is stored. + +2\. Go to the `App Registrations` page in the Azure UI, which can be done by searching "App registrations" in the console. + +3\. Click on `Register an application` or `New registration`. + +
+ +4\. Enter the `Name` and `Supported account type` (all are supported in Deep Lake) and click `Register` + +
+ +5\. In the application console, click `Certificates & secrets`. + +
+ +6\. Click on `Federated credentials` and `Add credential`. + +
+ +7\. Click on `Select scenario` and select `Other issuer`. + +
+ +8\. Enter the following information in the form, and click `Add`. + +* **Issuer:** https://cognito-identity.amazonaws.com + * This is for trusting Activeloop's Cognito issuer. There's no need to create AWS Cognito by the user. +* **Subject identifier:** us-east-1:7bc30eb1-bac6-494b-bf53-5747849d45aa +* **Name:** enter a name with your choice +* **Description (optional):** enter description a with your choice +* **Audience:** us-east-1:57e5de2f-e2ec-4514-b9b0-f3bb8c4283c3 + +
+ +#### Step 2a: Apply the Application Credentials to your Azure storage account + +**NOTE: Skip to 2b if you want to assign Application Credentials to a specific Azure container** + +1\. Go to the `Storage accounts` page in the Azure UI, which can be done by searching "Storage accounts" in the console. + +2\. Select the `Storage account` to which you want to add Application Credentials. + +
+ +3\. Select `Access Control (IAM)` and click `Add`, and `select Add role assignment`. + +
+ +4\. Search and select `Storage Blob Data Contributor` under the role names and click `Next`. + +
+ +5\. Click on the `Select members` link, and in the tab that opens up on the right, search by name and select the application you created in Step 1. Click `Select` at the bottom of the page. + +
+ +6\. The application should appear in the list of Members, at which point you can click `Review + assign`. + +
+ +#### Step 2b: Apply the Application Credentials to a specific Azure contained in your Azure storage account + +1\. Go to the `Storage accounts` page in the Azure UI, which can be done by searching "Storage accounts" in the console. + +2\. Select the `Storage account` to which you want to add Application Credentials. + +
+ +3\. Select the `Container` to which you add the Application Credentials. + +
+ +4\. Select `Access Control (IAM)` and click `Add`, and `select Add role assignment`. + +
+ +#### IMPORTANT TO PERFORM STEPS BELOW TO COMPLETE 2b - PLEASE DO NOT SKIP + +5\. **Perform substeps 5-7 from Step 2a above, in order to add the Application Credentials to the Container** + +6\. **Execute the steps in Step 2a above on your Storage Account, except set the Storage Account Role Assignment to `Storage Blob Delegator` in substep 5.** + +## Next Steps + +- [Enabling CORS in Azure](cors.md) \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/workload-identities.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/workload-identities.md new file mode 100644 index 0000000000..2bb86c2006 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/azure/workload-identities.md @@ -0,0 +1,79 @@ +--- +seo_title: "Azure Workload Identities | Secure Role Management" +description: "Manage Azure Workload Identities For Role-Based Access To Deep Lake, Providing Secure Data Access And Ensuring Safety Across Machine Learning Workflows." +--- + +# Azure Workload Identities + +How to authenticate using workload identities instead of user credentials. + +## Authenticating Using Workload Identities Instead of User Credentials + +Workload identities enable you to define a cloud workload that will have access to your Deep Lake organization without authenticating using Deep Lake user tokens. This enables users to manage and define Deep Lake permissions for jobs that many not be attributed to a specific user. + +Set up a Workload Identity using the following steps: + +1. Define an Azure Managed Identity in your cloud +1. Attached the Azure Managed Identity to your workload +1. Create a Deep Lake Workload Identity using the Azure Managed Identity + +1. Run the workload in Azure + +## Step 1: Define the workload identity in Azure + +1. Navigate to Managed Identities in Azure + + + +1. Click `Create` a Managed Identity + + + +1. Select the `Subscription` and `Resource Group` containing the workload, and give the Managed Identity a `Name`. Click `Review + Create`. + + + + +## Step 2: Attached the Azure Managed Identity to your workload + +When creating or updating a resource that will serve as the Client running Deep Lake, assign the Managed Identity from Step 1 to this resource. + +For example, in Azure Machine Learning Studio, when creating a compute instance, toggle `Assign Identity` and select the `Managed Identity` from Step 1. + + + +## Step 3: Create a Deep Lake Workload Identity using the Azure Managed Identity + +Navigate to the `Permissions` tab for your organization in the [Deep Lake App](https://app.activeloop.ai/), locate the `Workload Identities`, and select `Add`. + + + +Specify a `Display Name`, `Client ID` (for the Managed Identity), and `Tenant ID`. The `Client ID` can be found in the main page for the Managed Identity, and the `Tenant ID` can be found in `Tenant Properties` in Azure. Click `Add`. + + + +## Step 4: Run the workload + +Specify the environmental variables below in the Deep Lake client and run other Deep APIs as normal. + + + +```python +#### THIS IS THE CLIENT_ID FOR THE COMPUTE INSTANCE +#### NOT THE MANAGED IDENTITY +os.environ["AZURE_CLIENT_ID"] = azure_client_id + +os.environ["ACTIVELOOP_AUTH_PROVIDER"] = "azure" +``` + +Specifying the `AZURE_CLIENT_ID` is not necessary in some environments because the correct value may automatically be set. + +For a compute instance in the Azure Machine Learning Studio, the Client ID can be found in instance settings below: + + diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/cors.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/cors.md new file mode 100644 index 0000000000..22dc89df3c --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/cors.md @@ -0,0 +1,23 @@ +--- +seo_title: "GCP CORS Setup | Cross-Origin Access For AI Data" +description: "Set Up CORS For Google Cloud Platform With Deep Lake To Securely Handle Cross-Origin Requests, Ensuring Compliance And Easy Access To Machine Learning Data." +--- + +# Enabling CORS in GCP + +In order to visualize Deep Lake datasets stored in your own GCP buckets in the [Deep Lake app](https://app.activeloop.ai/), please enable [Cross-Origin Resource Sharing (CORS)](https://en.wikipedia.org/wiki/Cross-origin\_resource\_sharing) in the buckets containing the Deep Lake dataset and any linked data, by inserting the snippet below in the CORS section of the Permissions tab for the bucket: + +``` +[ + { + "origin": ["https://app.activeloop.ai"], + "method": ["GET", "HEAD"], + "responseHeader": ["*"], + "maxAgeSeconds": 3600 + } +] +``` + +## Next Steps + +- [Provisioning Federated Credentials](provisioning.md) \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_add_principal.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_add_principal.png new file mode 100644 index 0000000000..0fe29376a9 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_add_principal.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_edit_access.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_edit_access.png new file mode 100644 index 0000000000..5b1c84df33 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_edit_access.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_select_bucket.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_select_bucket.png new file mode 100644 index 0000000000..b9accade4b Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_select_bucket.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_create.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_create.png new file mode 100644 index 0000000000..ce07d987c3 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_create.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_details.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_details.png new file mode 100644 index 0000000000..4f194a2558 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_details.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_done.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_done.png new file mode 100644 index 0000000000..31bcce5496 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_done.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_grant.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_grant.png new file mode 100644 index 0000000000..2d9bd3ffa5 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_account_grant.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_accounts.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_accounts.png new file mode 100644 index 0000000000..4ef9ce2668 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_service_accounts.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_set_principal.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_set_principal.png new file mode 100644 index 0000000000..2dfa6e7a48 Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/images/gcs_set_principal.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/provisioning.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/provisioning.md new file mode 100644 index 0000000000..ca96b7ed8f --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/gcp/provisioning.md @@ -0,0 +1,56 @@ +--- +seo_title: "GCP Provisioning | Configure Storage For AI Workflows" +description: "Provision Google Cloud Resources For Use With Deep Lake, Enabling Secure, Scalable Storage Solutions For Managing Large Multi-Modal AI Datasets." +--- + +# Provisioning Federated Credentials + +## Setting up Federated Credentials in Google Cloud Platform + +The most secure method for connecting data from your Azure storage to Deep Lake is using Federated Credentials, which are set up using the steps below: + +#### Step 1: Create Google Cloud Service Account + +1\. **If you already have a service account, skip to Step 2** + +2\. Navigate to `IAM & Admin` -> `Service Accounts` -> `CREATE SERVICE ACCOUNT` + +
+
+ +3\. Enter the `service account id`, and optional `name` and `description`. Make sure to copy the email address and and click on `CREATE AND CONTINUE`. + +
+ +4\. Click `CONTINUE` without entering any information. + +
+ +5\. Enter `activeloop-platform@activeloop-saas-iam.iam.gserviceaccount.com` in the `Service account users role` and click `DONE`. + +
+ +#### Step 2: Grant Access to the bucket using a Service Account Principal + +1\. Navigate to `Cloud Storage` and `Buckets`. +
+ +2\. Select `Edit Access` for the bucket you want to connect to Activeloop. + +
+ +3\. Select `Add Principal`. + +
+ +4\. Enter the `Service Account Email`, select the role as `Storage Object Admin`, and click `Save`. If the bucket is encrypted with customer managed KMS key, then `Cloud KMS CryptoKey Encrypter/Decrypter` should be added in the `Role` field as well. + +
+ +#### Step 3: Enter the Service Account Email (Step 2) into the Activeloop App + +See the first video in the [managed credentials overview](../index.md) + +## Next Steps + +- [Enabling CORS in GCP](cors.md) \ No newline at end of file diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/images/Authentication_With_Managed_Creds.png b/docs/docs/getting-started/storage-and-creds/managed-credentials/images/Authentication_With_Managed_Creds.png new file mode 100644 index 0000000000..998f4bb31d Binary files /dev/null and b/docs/docs/getting-started/storage-and-creds/managed-credentials/images/Authentication_With_Managed_Creds.png differ diff --git a/docs/docs/getting-started/storage-and-creds/managed-credentials/index.md b/docs/docs/getting-started/storage-and-creds/managed-credentials/index.md new file mode 100644 index 0000000000..a2715723c0 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/managed-credentials/index.md @@ -0,0 +1,103 @@ +--- +seo_title: "Managed Credentials | Secure Cloud Storage Access" +description: "Set Up Managed Credentials To Enable Secure Access To Cloud Storage Resources In Deep Lake, Essential For Managing Large-Scale AI And Machine Learning Data." +--- + +# Setting up Deep Lake in Your Cloud + +## Connecting Data From Your Cloud Using Deep Lake Managed Credentials + +You can use Deep Lake while storing data in your own cloud (AWS, Azure, GCS) without data passing through Activeloop Servers. Most Deep Lake services run on the client, and our browser App or Python API directly read/write data from object storage. + +Deep Lake Managed credentials should be set up for granting the client access to cloud storage. Managed Credentials use IAM Policies or Federated Credentials on Activeloop's backend to generate temporary credentials that are used by the client do access the data. + +![Authentication_With_Managed_Creds.png](images/Authentication_With_Managed_Creds.png) + +## Default Storage + +Default storage enables you to map the Deep Lake path `al://org_id/dataset_name` to a cloud path of your choice. Subsequently, all datasets created using the Deep Lake path will be stored at the user-specified, and they can be accessed using API tokens and managed credentials from Deep Lake. By default, the default storage is set as Activeloop Storage, and you may change it using the UI below. + +
+ +!!! note + In order to visualize data in the Deep Lake browser application, it is necessary to enable CORS in the bucket containing any source data. + +## Connecting Deep Lake Dataset in your Cloud to the Deep Lake to App + +If you do not set the Default Storage as your own cloud, Datasets in user's clouds can be connected to the [Deep Lake App](https://app.activeloop.ai) using the Python API below. Once a dataset is connected to Deep Lake, it is assigned a Deep Lake path `al://org_id/dataset_name`, and it can be accessed using API tokens and managed credentials from Deep Lake, without continuously having to specify cloud credentials. + +#### Connecting Datasets in the Python API + + + +```python +# Use deeplake.connect to connect a dataset in your cloud to the Deep Lake App +# Managed Credentials (creds_key) for accessing the data +# (See Managed Credentials above) +ds = deeplake.create('s3://my_bucket/dataset_name', +creds={'creds_key': 'managed_creds_key'}) # or deeplake.open + +# Specify your own path and dataset name for +# future access to the dataset. +# You can also specify different managed credentials, if desired +deeplake.connect(src='s3://my_bucket/dataset_name', + dest='al://org_id/dataset_name', + creds_key='managed_creds_key') +ds = deeplake.open_read_only('al://org_id/dataset_name', token='my_token') +``` + +## Using Manage Credentials with Linked Tensors + +Managed credentials can be used for accessing data stored in linked tensors. Simply add the managed credentials to the dataset's creds_keys and assign them to each sample. + +```python +ds.add_column('images', deeplake.types.Link(deeplake.types.Image())) +ds.set_creds_key('my_creds_key') +ds.append([{"images": link_to_sample}]) +``` + +## Next Steps + +- [Provisioning AWS](aws/provisioning.md) +- [Provisioning Azure](azure/provisioning.md) +- [Provisioning GCP](gcp/provisioning.md) diff --git a/docs/docs/getting-started/storage-and-creds/storage-options.md b/docs/docs/getting-started/storage-and-creds/storage-options.md new file mode 100644 index 0000000000..889afd77c1 --- /dev/null +++ b/docs/docs/getting-started/storage-and-creds/storage-options.md @@ -0,0 +1,143 @@ +--- +seo_title: "Storage Options | Use Deep Lake With Preferred Storage Option" +description: "Explore Various Storage Options In Deep Lake, To Store and Search Across Data Efficiently" +--- + +# Storage Options + +How to authenticate using Activeloop storage, AWS S3, and Google Cloud Storage. + +## Overview + +**Deep Lake datasets can be stored locally, or on several cloud storage providers including Deep Lake Storage, AWS S3, +Microsoft Azure, and Google Cloud Storage.** + +Datasets are accessed by choosing the correct prefix for the dataset `path` that is passed to methods such +as [deeplake.open(path)](../../api/dataset.md#deeplake.open), +and [deeplake.create(path)](../../api/dataset.md#deeplake.create). + +The path prefixes are: + +| Storage Location | Path | Notes | +|--------------------------------------|--------------------------------------------------|-----------------------------------------------------------------------------------------------| +| In Memory | mem://dataset_id | | +| Local | file://local_path | | +| Deep Lake Storage | al://org_id/dataset_name | | +| AWS S3 | s3://bucket_name/dataset_name | Dataset can be connected to Deep Lake via [Managed Credentials](managed-credentials/index.md) | +| Microsoft Azure (Gen2 DataLake Only) | az://account_name/container_name/dataset_name | Dataset can be connected to Deep Lake via [Managed Credentials](managed-credentials/index.md) | +| Google Cloud | gcs://bucket_name/dataset_name | Dataset can be connected to Deep Lake via [Managed Credentials](managed-credentials/index.md) | + +!!! tip + + Connecting Deep Lake datasets stored in your own cloud via Deep Lake [Managed Credentials](managed-credentials/index.md) is required for accessing enterprise features, and it significantly simplifies dataset access. + +## Authentication for each cloud storage provider + +### Activeloop Storage and Managed Datasets + +In order to access datasets stored in Deep Lake, or datasets in other clouds that +are [managed by Activeloop](managed-credentials/index.md), users must register and authenticate using +the steps in the link below in [User Authentication](../authentication.md) + +### AWS S3 + +Authentication with AWS S3 has 4 options: + +1. Use Deep Lake on a machine in the AWS ecosystem that has access to the relevant S3 bucket + via [AWS IAM](https://aws.amazon.com/iam/), in which case there is no need to pass credentials in order to access + datasets in that bucket. + +1. Configure AWS through the cli using `aws configure`. This creates a credentials file on your machine that is + automatically access by Deep Lake during authentication. + +1. Save the `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_SESSION_TOKEN` (optional) in environmental variables + of the same name, which are loaded as default credentials if no other credentials are specified. + +1. Create a dictionary with the `aws_access_key_id`, `aws_secret_access_key`, and `aws_session_token` (optional), and + pass it to Deep Lake using: + + + +```python +# Low Level API +ds = deeplake.open("s3://my-bucket/dataset", + creds = { + "aws_access_key_id": "my_access_key_id", + "aws_secret_access_key": "my_aws_secret_access_key", + "aws_session_token": "my_aws_session_token", # Optional +}) +``` + +#### Custom Storage with S3 API + +In order to connect to other object storages supporting S3-like API such as [MinIO](https://github.com/minio/minio), +[StorageGrid](https://www.netapp.com/data-storage/storagegrid/) and others, simply add +endpoint_url the creds dictionary. + +```python +ds = deeplake.open('s3://...', + creds = { + 'aws_access_key_id': "my_access_key_id", + 'aws_secret_access_key': "my_aws_secret_access_key", + 'aws_session_token': "my_aws_session_token", # Optional + 'endpoint_url': 'http://localhost:8888' + }) +``` + +### Microsoft Azure + +Authentication with Microsoft Azure has 4 options: + +1. Log in from your machine's CLI using az login. + +1. Save the `AZURE_STORAGE_ACCOUNT`, `AZURE_STORAGE_KEY`, or other credentials in environmental variables of the same name, +which are loaded as default credentials if no other credentials are specified. + +1. Create a dictionary with the `ACCOUNT_KEY` or `SAS_TOKEN` and pass it to Deep Lake using: + + ```python + ds = deeplake.open('azure:////', + creds = { + 'account_key': "my_account_key", + #OR + 'sas_token': "your_sas_token", + }) + ``` + +### Google Cloud Storage + +Authentication with Google Cloud Storage has 2 options: + +1. Create a service account, download the JSON file containing the keys, and then pass that file to the `creds` parameter in `deeplake.open('gcs://.....', creds = 'path_to_keys.json')`. It is also possible to manually pass the information from the JSON file into the creds parameter using: + + ```python + ds = deeplake.open('gcs://.....', + creds = content_of_json_file + ) + ``` + +1. Authenticate through the browser using the steps below. This requires that the project credentials are stored on your +machine, which happens after gcloud is initialized and logged in through the CLI. Afterwards, creds can be switched to +creds = 'cache'. + + ```python + ds = deeplake.open('gcs://.....', + creds = 'browser' # Switch to 'cache' after doing this once + ) + ``` diff --git a/docs/docs/guide/.pages b/docs/docs/guide/.pages new file mode 100644 index 0000000000..72a339aafa --- /dev/null +++ b/docs/docs/guide/.pages @@ -0,0 +1,6 @@ +nav: + - RAG: rag.md + - VectorStore: vectorstore.md + - Deep Learning: deep-learning + - v3-conversion.md + - Annotations: annotations diff --git a/docs/docs/guide/annotations/.pages b/docs/docs/guide/annotations/.pages new file mode 100644 index 0000000000..82173f225a --- /dev/null +++ b/docs/docs/guide/annotations/.pages @@ -0,0 +1,2 @@ +nav: + - Labelbox: labelbox.md \ No newline at end of file diff --git a/docs/docs/guide/annotations/labelbox.md b/docs/docs/guide/annotations/labelbox.md new file mode 100644 index 0000000000..91c5df4eb0 --- /dev/null +++ b/docs/docs/guide/annotations/labelbox.md @@ -0,0 +1,297 @@ +--- +seo_title: "Labelbox Integration | Annotations" +description: "Labelbox integration for annotations." +--- +# Labelbox Integration +This document describes how to create Deep Lake datasets from [Labelbox](https://labelbox.com/) annotations. The API also allows you to update the dataset with new annotations. + +## Prerequisites + +```bash +python -m pip install labelbox +``` + +## Supported Labelbox Ontologies + +- [Video Ontology](#video-ontology) +- [Image Ontology](#image-ontology) + +### Video Ontology + +For video ontolgy, python `av` library is used to extract frames from videos. + +```bash +python -m pip install av +``` + +### Uploading videos for annotation to Labelbox + +Deeplake supports uploading videos to Labelbox using the [Labelbox API](https://docs.labelbox.com/reference/getting-started). + + + +```python +from deeplake.integrations import create_labelbox_annotation_project + +client = labelbox.Client(api_key=LABELBOX_API_KEY) + +files = [] # list of video urls, can be all local or all remote. + +# connect the ontology to the project +ontology = client.get_ontology('ontology_id_from_labelbox') + +# create annotation project in labelbox +create_labelbox_annotation_project(files, 'dataset-for-deeplake-tests', 'project-for-deeplake-tests', LABELBOX_API_KEY, lb_ontology=ontology) +``` + + +### Creating a dataset from an annotated Labelbox project + +To create a dataset from an annotated Labelbox project, you can use the following code: + + + +```python +from deeplake.integrations import ( + create_dataset_from_video_annotation_project, + converter_for_video_project_with_id +) + +# the path where we want to create the dataset +ds_path = "mem://labelbox_connect_test" + +# the project id of the labelbox project that we want to create the dataset from +project_id = get_project_id() + +# we pass the url presigner in cases when the videos are in cloud storage ( +# for this case azure blob storage) and the videos were added to labelbox with their integrations functionality. +# the default one tries to use labelbox api to get the non public remote urls. +def url_presigner(url): + sas_token = "" + # the second value is the headers that will be added to the request + return url.partition("?")[0] + "?" + sas_token, {} + +# create the dataset, this will extract the frames from the videos and create the dataset. +# the project_json is a json file that contains the project information from labelbox which we can reuse during the labels fetching. +ds, project_json = create_dataset_from_video_annotation_project( + ds_path, + project_id, + LABELBOX_API_KEY, + url_presigner=url_presigner, +) + +# commit the dataset +ds.commit("create dataset") + +# define the dataset provider +# the dataset provider can be used to update do some other operations on the dataset, before the annotations are applied. +def ds_provider(p): + # we need to keep p (labelbox project name) with the ds path in case we need to refetch labeles. + # this step is completely optional, we just need to be able to load the correct dataset for refetching labels. + # our refetching example will be using the same mapping to retrieve the ds_path from the project name. + with open(f'{project_id}_mapping.json', 'w') as f: + import json + json.dump({p: ds_path}, f) + try: + # create a new branch , where all the annotations will be stored. + # the main branch will be intact. + ds.branch("labelbox") + except: + pass + return ds.branches["labelbox"].open() + +# create the converter +converter = converter_for_video_project_with_id( + project_id, + ds_provider, + LABELBOX_API_KEY, + group_mapping={"raster-segmentation": "mask"}, + project_json=project_json, +) + +# generate the annotations +ds = converter.dataset_with_applied_annotations() + +# commit the annotations to the dataset +ds.commit("add labelbox annotations") +``` + +### Re-fetching the annotations from Labelbox to the existing dataset + +At the moment, the for re-fetching the annotations from Labelbox to the existing dataset is not supported. However it will be supported in the future. In the meantime, you can keep the annotations in a separate dataset. There are only 2 requirements: + +- The dataset should have the same length as the dataset that you have created from Labelbox. +- The dataset should have the same `labelbox_meta` metadata as the dataset that you have created from Labelbox. + +Then you can UNION the two datasets. + + +### Image ontology + +### Uploading images for annotation to Labelbox + +Deeplake supports uploading images to Labelbox using the [Labelbox API](https://docs.labelbox.com/reference/getting-started). + + + +```python +from deeplake.integrations import create_labelbox_annotation_project + +client = labelbox.Client(api_key=LABELBOX_API_KEY) + +files = [] # list of image urls, can be all local or all remote. + +# connect the ontology to the project +ontology = client.get_ontology('ontology_id_from_labelbox') + +# create annotation project in labelbox +create_labelbox_annotation_project(files, 'dataset-for-deeplake-tests', 'project-for-deeplake-tests', LABELBOX_API_KEY, lb_ontology=ontology, media_type = "IMAGE") +``` + + +### Creating a dataset from an annotated Labelbox project +To create a dataset from an annotated Labelbox image project, the logic is similar to the video ontology, but you will use the `create_dataset_from_image_annotation_project` function instead. Here is an example: + + + +```python +import deeplake +from deeplake.integrations import ( + create_dataset_from_image_annotation_project, + converter_for_image_project_with_id, +) +# the path where we want to create the dataset +ds_path = "mem://labelbox_connect_test" +# the project id of the labelbox project that we want to create the dataset from +project_id = get_project_id() + +# create the dataset, this will extract the frames from the videos and create the dataset. +ds, project_json = create_dataset_from_image_annotation_project( + ds_path, + project_id, + LABELBOX_API_KEY, +) + +# commit the dataset +ds.commit("create dataset") +# define the dataset provider +# the dataset provider can be used to update do some other operations on the dataset, before the +# annotations are applied. +def ds_provider(p): + # we need to keep p (labelbox project name) with the ds path in case + # we need to refetch labeles. + # this step is completely optional, we just need to be able to load the correct dataset + # for refetching labels. + # our refetching example will be using the same mapping to retrieve the ds_path from the project name. + with open(f'{project_id}_mapping.json', 'w') as f: + import json + json.dump({p: ds_path}, f) + try: + # create a new branch , where all the annotations will be stored. + # the main branch will be intact. + ds.branch("labelbox") + except: + pass + return ds.branches["labelbox"].open() + +# create the converter +converter = converter_for_image_project_with_id( + project_id, + ds_provider, + LABELBOX_API_KEY, + group_mapping={"raster-segmentation": "mask"}, + project_json=project_json, +) +# generate the annotations +ds = converter.dataset_with_applied_annotations() +# commit the annotations to the dataset +ds.commit("add labelbox annotations") +``` \ No newline at end of file diff --git a/docs/docs/guide/deep-learning/.pages b/docs/docs/guide/deep-learning/.pages new file mode 100644 index 0000000000..5ff92335bf --- /dev/null +++ b/docs/docs/guide/deep-learning/.pages @@ -0,0 +1,5 @@ +nav: + - Quickstart: deep-learning.ipynb + - DataLoader: async-data-loader.md + - Object Detection with MM: mmdet.md + - Segmentation with MM: mmseg.md \ No newline at end of file diff --git a/docs/docs/guide/deep-learning/async-data-loader.md b/docs/docs/guide/deep-learning/async-data-loader.md new file mode 100644 index 0000000000..0d109c79b1 --- /dev/null +++ b/docs/docs/guide/deep-learning/async-data-loader.md @@ -0,0 +1,133 @@ +--- +seo_title: "Async Data Loader | Speed Up Multi-Modal Data Ingestion" +description: "Use The Async Data Loader To Parallelize Data Ingestion, Enhancing Speed And Efficiency When Loading Multi-Modal Data For ML Model Training or Fast AI Search." +--- + +# Async Data Loader + +## Overview + +This document describes the implementation of a custom DataLoader for handling data retrieval using `deeplake.Dataset` with `PyTorch`. The DataLoader supports both sequential and asynchronous data fetching, with the asynchronous approach being optimized for performance and speed. + +## Dataset Structure + +The dataset comprises pairs of images and their respective masks. Each image is a high-resolution file, while each mask is a binary image indicating the regions of interest within the corresponding image. + +## Sequential data fetching + +This ImageDataset class is a custom implementation of a PyTorch dataset that uses `deeplake.Dataset` as a datasource. + + + +```python +class ImageDataset(torch.utils.data.Dataset): + def __init__(self, deeplake_ds: deeplake.Dataset, transform: Callable = None): + self.ds = deeplake_ds + self.transform = transform + + def __len__(self): + return len(self.ds) + + def __getitem__(self, item): + image = self.ds[item]["images"] + mask = self.ds[item]["masks"] + + if self.transform is not None: + image, mask = self.transform((image, mask)) + + return image, mask + +``` + +In the sequential fetching approach, data is loaded one item at a time in a synchronous manner. While this method is straightforward, it can become a bottleneck when working with large datasets with multiple tensors. + +## Asynchronous Data Fetching + +The asynchronous fetching method utilizes asyncio and threading to load data in parallel. This significantly improves loading times, especially for large datasets with multiple tensors. + +```python +import deeplake + +import asyncio +from threading import Thread, Lock +from multiprocessing import Queue + +lock = Lock() +index = -1 +def generate_data(ds: deeplake.Dataset): + total_count = len(ds) + global index + while True: + idx = 0 + with lock: + index = (index + 1) % (total_count - 1) + idx = index + yield ds[idx] + +class AsyncImageDataset(torch.utils.data.IterableDataset): + def __init__(self, deeplake_ds: deeplake.Dataset, transform: Callable = None, max_queue_size: int = 1024): + self.ds = deeplake_ds + self.transform = transform + self.worked_started = False + self.data_generator = generate_data(self.ds) + self.q = Queue(maxsize=max_queue_size) + + async def run_async(self): + for item in self.data_generator: + data = await asyncio.gather( + item.get_async("images"), + item.get_async("masks") + ) + self.q.put(data) + + def start_worker(self): + loop = asyncio.new_event_loop() + + for _ in range(128): + loop.create_task(self.run_async()) + + def loop_in_thread(loop): + asyncio.set_event_loop(loop) + loop.run_forever() + + self.loop_thread = Thread(target=loop_in_thread, args=(loop,), daemon=True) + self.loop_thread.start() + + self.worked_started = True + + def __iter__(self): + while True: + if not self.worked_started: + self.start_worker() + + # wait until some data is filled + while self.q.empty(): + pass + + image, mask = self.q.get() + if self.transform is not None: + image, mask = self.transform((image, mask)) + + yield image, mask +``` + +The `AsyncImageDataset` utilizes Python’s `asyncio` library to fetch images and masks concurrently from `deeplake.Dataset`, minimizing data loading times. The class implements a separate thread to run an event loop, allowing multiple data retrieval tasks to operate simultaneously. A multiprocessing `Queue` is used to store retrieved items, enabling thread-safe communication between data loading threads and the main processing loop. + +## Benchmark results + +| Method | Average Loading Time (seconds per batch) | +| -------- | -------: | +| Sequential | 6.2 | +| Asynchronous | 2.15 | + +While the sequential method is simpler to implement, the asynchronous approach offers substantial performance benefits, making it the preferred choice for handling larger datasets in machine learning workflows. This flexibility allows users to choose the best method suited to their specific use case, ensuring efficient data handling and model training. diff --git a/docs/docs/guide/deep-learning/deep-learning.ipynb b/docs/docs/guide/deep-learning/deep-learning.ipynb new file mode 100644 index 0000000000..332ccca3c3 --- /dev/null +++ b/docs/docs/guide/deep-learning/deep-learning.ipynb @@ -0,0 +1,338 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Deep Learning QuickStart" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Installing Deep Lake" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Deep Lake can be installed using PyPi." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!pip3 install deeplake" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Opening Your First Deep Lake Dataset" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's load the Visdrone dataset, a rich dataset with many object detections per image. Datasets hosted by Activeloop are identified by the host organization id followed by the dataset name: /." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import deeplake\n", + "import getpass\n", + "import os\n", + "from deeplake import types\n", + "\n", + "os.environ['ACTIVELOOP_TOKEN'] = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dataset_path = 'al://activeloop/visdrone-det-train-v4'\n", + "ds = deeplake.open(dataset_path)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The dataset has 3 columns, the images, labels, and bounding boxes:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dataset(columns=(images,labels,boxes), length=6471)\n", + "\u001B[1m+------+--------------------------------------------+\n", + "|column| type |\n", + "+------+--------------------------------------------+\u001B[0m\n", + "|images| array(dtype=uint8, shape=[None,None,None]) |\n", + "+------+--------------------------------------------+\n", + "|labels| array(dtype=uint32, shape=[None]) |\n", + "+------+--------------------------------------------+\n", + "|boxes |array(dtype=float32, shape=[None,None,None])|\n", + "+------+--------------------------------------------+\n", + "\n" + ] + } + ], + "source": [ + "ds.summary()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reading Data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Deep Lake does not download any data in advance. Data is fetched lazily from long-term storage based on row numbers in the dataset:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "image = ds[\"images\"][0] # Fetch the first image and return a numpy array\n", + "labels = ds[\"labels\"][0] # Fetch the labels in the first image\n", + "boxes = ds[\"boxes\"][0] # Fetch the bounding boxes for the first image\n", + "\n", + "img_list = ds[\"labels\"][0:100] # Fetch 100 labels and store them as a list of numpy arrays" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Visualizing Datasets" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The dataset above can be visualized in the [Deep Lake App](https://app.activeloop.ai/activeloop/visdrone-det-train-v4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Creating Your Own Datasets" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's follow along with the example below to create our first dataset. First, download and unzip the small classification dataset below called the *animals dataset*." + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " % Total % Received % Xferd Average Speed Time Time Time Current\n", + " Dload Upload Total Spent Left Speed\n", + "100 167k 0 167k 0 0 426k 0 --:--:-- --:--:-- --:--:-- 425k\n" + ] + } + ], + "source": [ + "# Download dataset\n", + "from IPython.display import clear_output\n", + "# !wget https://github.com/activeloopai/examples/blob/main/colabs/starting_data/animals.tar\n", + "!curl -L -o animals.tar https://github.com/activeloopai/examples/blob/main/colabs/starting_data/animals.tar" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tar: Error opening archive: Unrecognized archive format\n" + ] + } + ], + "source": [ + "# Unzip to './animals' folder\n", + "!tar -xvf ./animals.tar" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "animals\n", + "- cats\n", + " - image_1.jpg\n", + " - image_2.jpg\n", + "- dogs\n", + " - image_3.jpg\n", + " - image_4.jpg" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that you have the data, you can **create a Deep Lake `Dataset`** and initialize its tensors. Running the following code will create a Deep Lake dataset inside of the `./animals_dl` folder." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [], + "source": [ + "import deeplake\n", + "import numpy as np\n", + "import os\n", + "\n", + "ds = deeplake.create('./animals_dl') # Creates the dataset" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, let's inspect the folder structure for the source dataset './animals' to find the class names and the files that need to be uploaded to the Deep Lake dataset." + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "# Find the class_names and list of files that need to be uploaded\n", + "dataset_folder = '/Users/istranic/ActiveloopCode/Datasets/animals'\n", + "\n", + "# Find the subfolders, but filter additional files like DS_Store that are added on Mac machines.\n", + "class_names = [item for item in os.listdir(dataset_folder) if os.path.isdir(os.path.join(dataset_folder, item))]\n", + "\n", + "files_list = []\n", + "for dirpath, dirnames, filenames in os.walk(dataset_folder):\n", + " for filename in filenames:\n", + " files_list.append(os.path.join(dirpath, filename))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, let's **create the dataset columns and upload data**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds.add_column('images', dtype = types.Image(sample_compression = \"jpg\"))\n", + "ds.add_column('labels', dtype = types.Array( dtype = types.UInt32(), dimensions=1))\n", + "\n", + "# Iterate through the files and append to Deep Lake dataset\n", + "for file in files_list:\n", + " label_text = os.path.basename(os.path.dirname(file))\n", + " label_num = class_names.index(label_text)\n", + "\n", + " #Append data to the tensors\n", + " ds.append({'images': [open(file, \"rb\").read()], 'labels': [label_num]})" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dataset(columns=(images,labels), length=0)\n", + "\u001B[1m+------+------------------------------------------+\n", + "|column| type |\n", + "+------+------------------------------------------+\u001B[0m\n", + "|images|array(dtype=uint8, shape=[None,None,None])|\n", + "+------+------------------------------------------+\n", + "|labels| array(dtype=uint32, shape=[None]) |\n", + "+------+------------------------------------------+\n", + "\n" + ] + } + ], + "source": [ + "ds.summary()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "c18a7e5b6ff84ec8af70f0053f08fa65dad397e6ed6e820ac7403aeb2095bb6e" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/docs/guide/deep-learning/deep-learning.meta.yaml b/docs/docs/guide/deep-learning/deep-learning.meta.yaml new file mode 100644 index 0000000000..7d1a134e14 --- /dev/null +++ b/docs/docs/guide/deep-learning/deep-learning.meta.yaml @@ -0,0 +1,2 @@ +seo_title: Deep Learning Integration | Train ML Models Faster And 5x Cheaper +description: Learn How To Integrate Deep Lake With PyTorch And TensorFlow For Efficient ML Model Training. Stream Multi-Modal Data Real-Time as Training Happens, At Scale. \ No newline at end of file diff --git a/docs/docs/guide/deep-learning/mmdet.md b/docs/docs/guide/deep-learning/mmdet.md new file mode 100644 index 0000000000..464ea4be10 --- /dev/null +++ b/docs/docs/guide/deep-learning/mmdet.md @@ -0,0 +1,220 @@ +--- +seo_title: "MMDetection Integration | Object Detection Pipeline" +description: "Deeplake Dataset training for Object detection using MMDetection framework." +--- + +# Training Object Detection Models with Deep Lake and MMDetection + +This tutorial shows how to train an object detection model using MMDetection with data stored in Deep Lake. We'll use a YOLOv3 model trained on ImageNet data to demonstrate the workflow. + +## Prerequisites + +First, let's install the required packages: + +```bash +python -m pip install torch==1.12.0+cu116 torchvision==0.13.0+cu116 -f https://download.pytorch.org/whl/torch_stable.html +python -m pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.12.0/index.html + +git clone -b dev-2.x https://github.com/open-mmlab/mmdetection.git +cd mmdetection +python3 -m pip install -e . +``` + +Note: We use MMDetection 2.x versions as they're currently supported by the Deep Lake integration. + +## Setup + +Let's set up our imports and authentication: + + + +```python +import deeplake +from mmcv import Config +from mmdet.models import build_detector +import os +import mmcv + +# Set your Deep Lake token +token = os.environ["ACTIVELOOP_TOKEN"] +``` + +## Configuration + +MMDetection uses config files to define models and training parameters. Here's our YOLOv3 config with Deep Lake integration: + +```python + +_base_ = "/configs/yolo/yolov3_d53_mstrain-416_273e_coco.py" + +# use caffe img_norm +img_norm_cfg = dict(mean=[0, 0, 0], std=[255., 255., 255.], to_rgb=True) + +train_pipeline = [ + dict(type='LoadImageFromFile'), + dict(type='LoadAnnotations', with_bbox=True), + dict( + type='Expand', + mean=img_norm_cfg['mean'], + to_rgb=img_norm_cfg['to_rgb'], + ratio_range=(1, 2)), + dict(type='Resize', img_scale=[(320, 320), (416, 416)], keep_ratio=True), + dict(type='RandomFlip', flip_ratio=0.0), + dict(type='PhotoMetricDistortion'), + dict(type='Normalize', **img_norm_cfg), + dict(type='Pad', size_divisor=32), + dict(type='DefaultFormatBundle'), + dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) +] +test_pipeline = [ + dict(type='LoadImageFromFile'), + dict( + type='MultiScaleFlipAug', + img_scale=(416, 416), + flip=False, + transforms=[ + dict(type='Resize', keep_ratio=True), + dict(type='RandomFlip', flip_ratio=0.0), + dict(type='Normalize', **img_norm_cfg), + dict(type='Pad', size_divisor=32), + dict(type='ImageToTensor', keys=['img']), + dict(type='Collect', keys=['img']) + ]) +] + + +data = dict( + train=dict( + pipeline=train_pipeline, + deeplake_path="hub://activeloop/coco-train", + # If not specified, Deep Lake will auto-infer the mapping, but it might make mistakes if datasets have many tensors + deeplake_tensors = {"img": "images", "gt_bboxes": "boxes", "gt_labels": "categories"}, + + # the parameters in other parts of the cfg file such as samples_per_gpu, and others. + deeplake_dataloader = {"shuffle": True, "batch_size": 4, 'num_workers': 8} + ), + + # Parameters as the same as for train + val=dict( + pipeline=test_pipeline, + deeplake_path="hub://activeloop/coco-val", + deeplake_tensors = {"img": "images", "gt_bboxes": "boxes", "gt_labels": "categories"}, + deeplake_dataloader = {"shuffle": False, "batch_size": 1, 'num_workers': 8} + ), +) + + +deeplake_metrics_format = "COCO" + +evaluation = dict(metric=["bbox"], interval=1) + +load_from = "checkpoints/yolov3_d53_mstrain-416_273e_coco-2b60fcd9.pth" + +work_dir = "./mmdet_outputs" + +log_config = dict(interval=10) + +checkpoint_config = dict(interval=5000) + +seed = None + +device = "cuda" + +runner = dict(type='EpochBasedRunner', max_epochs=10) + +``` + +## Training + +Now we can start the training: + +```python +# Load config +cfg = Config.fromfile(config_path) + +# Build the detector +model = build_detector(cfg.model) + +# Create work directory +mmcv.mkdir_or_exist(os.path.abspath(cfg.work_dir)) + +# Start training +from deeplake.integrations import mmdet as mmdet_deeplake +mmdet_deeplake.train_detector( + model, + cfg, + distributed=False, # Set to True for multi-GPU training + validate=False # Set to True if you have validation data +) +``` + +## Key Benefits of Using Deep Lake + +1. **Simple Data Loading**: Deep Lake automatically handles data streaming and batching, so you don't need to write custom data loaders. + +2. **Efficient Storage**: Data is stored in an optimized format and loaded on-demand, saving disk space and memory. + +3. **Easy Tensor Mapping**: The `deeplake_tensors` config maps your dataset's tensor names to what MMDetection expects, making it easy to use any dataset. + +4. **Built-in Authentication**: Deep Lake handles authentication and access control for your datasets securely. + +5. **Distributed Training Support**: The integration works seamlessly with MMDetection's distributed training capabilities. + +## Monitoring Training + +You can monitor the training progress in the work directory: + +```python +# Check latest log file +log_file = os.path.join(cfg.work_dir, 'latest.log') +if os.path.exists(log_file): + with open(log_file, 'r') as f: + print(f.read()) +``` + +## Inference + +After training, you can use the model for inference: + +```python +from mmdet.apis import inference_detector, init_detector + +# Load trained model +checkpoint = os.path.join(cfg.work_dir, 'latest.pth') +model = init_detector(config_path, checkpoint) + +# Load an image +img = 'path/to/test/image.jpg' + +# Run inference +result = inference_detector(model, img) +``` + +## Common Issues and Solutions + +1. If you get CUDA out of memory errors: + - Reduce `samples_per_gpu` in the config + - Use smaller image sizes in the pipeline + +2. If training is slow: + - Increase `num_workers` in `deeplake_dataloader` + - Use distributed training with multiple GPUs + +3. If you see authentication errors: + - Make sure your Deep Lake token is correct + - Check if you have access to the dataset + +## Next Steps + +- Try different MMDetection models by changing the base config +- Add validation data to monitor model performance +- Experiment with different data augmentations in the pipeline +- Enable distributed training for faster processing diff --git a/docs/docs/guide/deep-learning/mmseg.md b/docs/docs/guide/deep-learning/mmseg.md new file mode 100644 index 0000000000..a8452ba963 --- /dev/null +++ b/docs/docs/guide/deep-learning/mmseg.md @@ -0,0 +1,264 @@ +--- +seo_title: "MMSegmentation Integration | Semantic Segmentation Pipeline" +description: "Deeplake Dataset training for semantic segmentation using MMSegmentation framework." +--- +# Semantic Segmentation with Deep Lake and MMSegmentation + +## Integration Interface +MMSegmentation works with configs. Deeplake adopted this strategy, and in order to train MMSeg models, you need to create/specify your model and training/validation config. Deep Lake integration's logic is almost the same as MMSegmentation's with some minor modifications. The integrations with MMSeg occurs in the deeplake.integrations.mmseg module. At a high-level, Deep Lake is responsible for the pytorch dataloader that streams data to the training framework, while MMSeg is used for the training, transformation, and evaluation logic. Let us take a look at the config with deeplake changes: + +Learn more about MMSegmentation [here](https://mmsegmentation.readthedocs.io/en/latest/). + +### Example Configuration with Deep Lake +This tutorial shows how to train a semantic segmentation model using MMSegmentation with data stored in Deep Lake. We'll use a PSPNet model with ResNet-101 backbone trained on COCO data. + +## Prerequisites + +Install the required packages: + +```bash +python -m pip install torch==1.12.0+cu116 torchvision==0.13.0+cu116 -f https://download.pytorch.org/whl/torch_stable.html +python -m pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.12.0/index.html + +git clone https://github.com/open-mmlab/mmsegmentation.git +cd mmsegmentation +git checkout v0.30.0 +python -m pip install -e . +# Old pytorch version does not work with the new numpy versions +python -m pip install numpy==1.24.4 --force-reinstall +``` + +Note: We use MMSegmentation versions compatible with Deep Lake's integration. + +## Setup + + + +```python +import os +import deeplake +from mmcv import Config +import mmcv +from deeplake.integrations import mmseg as mmseg_deeplake + +# Set your Deep Lake token +token = os.environ["ACTIVELOOP_TOKEN"] +``` + +## Configuration + +Here's our PSPNet configuration with Deep Lake integration: + +```python +from mmdet.apis import set_random_seed + +_base_ = '/configs/pspnet/pspnet_r101-d8_512x512_4x4_160k_coco-stuff164k.py' + +# Normalize configuration +img_norm_cfg = dict( + mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) +crop_size = (256, 256) + +reduce_zero_label=False + +train_pipeline = [ + dict(type='LoadImageFromFile'), + dict(type='LoadAnnotations'), + dict(type='Resize', img_scale=(320, 240), ratio_range=(0.5, 2.0)), + dict(type='RandomCrop', crop_size=crop_size), + dict(type='RandomFlip', flip_ratio=0.5), + dict(type='PhotoMetricDistortion'), + dict(type='Normalize', **img_norm_cfg), + dict(type='Pad', size=crop_size, pad_val=0), + dict(type='DefaultFormatBundle'), + dict(type='Collect', keys=['img', 'gt_semantic_seg']), + ] + +test_pipeline = [ + dict(type='LoadImageFromFile'), + dict( + type='MultiScaleFlipAug', + img_scale=(320, 240), + # img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75], + flip=False, + transforms=[ + dict(type='Resize', img_scale=(320, 240), keep_ratio=True), + dict(type='RandomFlip'), + dict(type='Normalize', **img_norm_cfg), + dict(type='ImageToTensor', keys=['img']), + dict(type='Collect', keys=['img']), + ]) +] + +evaluation = dict(metric=["mIoU"], interval=10000) + +data = dict( + samples_per_gpu=4, + workers_per_gpu=8, + train=dict( + pipeline=train_pipeline, + deeplake_path="hub://activeloop/coco-train-seg-mask", + deeplake_tensors = {"img": "images", "gt_semantic_seg": "seg_masks"}, + deeplake_dataloader={"shuffle": False, "num_workers": 0, "drop_last": True} + ), + val=dict( + pipeline=test_pipeline, + deeplake_path="hub://activeloop/coco-val-seg-mask"", + deeplake_tensors = {"img": "images", "gt_semantic_seg": "seg_masks"}, + deeplake_dataloader={"shuffle": False, "batch_size": 1, "num_workers": 0, "drop_last": True} + ) +) + +work_dir = "./deeplake_logs" + +optimizer = dict(lr=0.02 / 8) +lr_config = dict(warmup=None) +log_config = dict(interval=50) +checkpoint_config = dict(interval=5000) + +runner = dict(type="IterBasedRunner", max_iters=100000, max_epochs=None) +device = "cuda" + +``` + +## Training + +Now we can start the training: + +```python + +if __name__ == "__main__": + current_loc = os.getcwd() + cfg_file = f"{current_loc}/seg_mask_config.py" + + # Read the config file + cfg = Config.fromfile(cfg_file) + cfg.model.decode_head.num_classes = 81 + cfg.model.auxiliary_head.num_classes = 81 + + # build segmentor + model = mmseg_deeplake.build_segmentor( + cfg.model + ) + + # Create work directory + mmcv.mkdir_or_exist(os.path.abspath(cfg.work_dir)) + + # train_segmentor + mmseg_deeplake.train_segmentor( + model, + cfg, + distributed=True, # Set to True for multi-GPU training + validate=True, # Set to True if you have validation data + ) +``` + +## Deep Lake Integration Benefits + +1. **Efficient Mask Handling**: Deep Lake efficiently stores and loads segmentation masks, which can be large and memory-intensive. + +2. **Automatic Format Conversion**: Deep Lake handles conversion between different mask formats (binary, RLE, polygon) automatically. + +3. **Smart Batching**: Deep Lake's dataloader handles variable-sized images and masks efficiently. + +4. **Memory Management**: Data is loaded on-demand, preventing out-of-memory issues with large datasets. + +5. **Distributed Training Support**: Seamless integration with MMSegmentation's distributed training. + +## Monitoring Training + +Monitor training progress: + +```python +# Check latest log file +log_file = os.path.join(cfg.work_dir, 'latest.log') +if os.path.exists(log_file): + with open(log_file, 'r') as f: + print(f.read()) +``` + +## Inference + +After training, use the model for inference: + +```python +from mmseg.apis import inference_segmentor, init_segmentor + +# Load trained model +checkpoint = os.path.join(cfg.work_dir, 'latest.pth') +model = init_segmentor(config_path, checkpoint) + +# Load an image +img = 'path/to/test/image.jpg' + +# Run inference +result = inference_segmentor(model, img) +``` + +### Key Integration Parameters + +- **`data`**: Central to the MMSegmentation configuration file, it specifies the training and validation datasets, transformations, and paths. + - **`train`**: Contains dataset path, credentials, and transformations for training data. + - **`val`**: Contains dataset path, credentials, and transformations for validation data. + - **`pipeline`**: A list of transformations applied to the dataset. + - **`deeplake_path`**: Path to the Deep Lake dataset for training and validation. + - **`deeplake_credentials`**: (Optional) Required for private, nonlocal datasets. + - **`deeplake_tag_id`**: (Optional) Specifies a dataset commit for reproducibility. + - **`deeplake_query`**: (Optional) Used to load datasets based on a query. + - **`deeplake_tensors`**: Maps MMSegmentation tensors to Deep Lake tensors: + - `"img"`: Image tensor. + - `"gt_semantic_seg"`: Semantic segmentation tensor. + +## Common Issues and Solutions + +1. Memory Issues: + - Reduce `samples_per_gpu` in config + - Decrease image size in pipeline + - Use smaller batch sizes + +2. Performance Issues: + - Increase `num_workers` in `deeplake_dataloader` + - Enable distributed training + - Use proper GPU settings + +3. Mask Format Issues: + - Verify mask format in dataset + - Check normalization settings + - Ensure proper padding configuration + +### Custom Loss Functions + +```python +# Add custom loss function to decode head +config['model']['decode_head']['loss_decode'] = dict( + type='CrossEntropyLoss', + use_sigmoid=False, + loss_weight=1.0, + class_weight=[1.0] * 171 # Class weights for imbalanced datasets +) +``` + +### Multiple Optimization Strategies + +```python +# Different learning rates for backbone and heads +config['optimizer'] = dict( + type='AdamW', + lr=0.0001, + paramwise_cfg=dict( + custom_keys={ + 'backbone': dict(lr_mult=0.1), + 'decode_head': dict(lr_mult=1.0), + 'auxiliary_head': dict(lr_mult=1.0) + } + ) +) +``` diff --git a/docs/docs/guide/images/rag-img-1.png b/docs/docs/guide/images/rag-img-1.png new file mode 100644 index 0000000000..61c3f66e9d Binary files /dev/null and b/docs/docs/guide/images/rag-img-1.png differ diff --git a/docs/docs/guide/images/rag-img-2.png b/docs/docs/guide/images/rag-img-2.png new file mode 100644 index 0000000000..6ef902d3d1 Binary files /dev/null and b/docs/docs/guide/images/rag-img-2.png differ diff --git a/docs/docs/guide/images/rag-img-3.png b/docs/docs/guide/images/rag-img-3.png new file mode 100644 index 0000000000..1f70c91f78 Binary files /dev/null and b/docs/docs/guide/images/rag-img-3.png differ diff --git a/docs/docs/guide/images/rag-img-4.png b/docs/docs/guide/images/rag-img-4.png new file mode 100644 index 0000000000..08d9828932 Binary files /dev/null and b/docs/docs/guide/images/rag-img-4.png differ diff --git a/docs/docs/guide/images/rag-img-5.png b/docs/docs/guide/images/rag-img-5.png new file mode 100644 index 0000000000..69618d1aee Binary files /dev/null and b/docs/docs/guide/images/rag-img-5.png differ diff --git a/docs/docs/guide/images/rag-img-6.png b/docs/docs/guide/images/rag-img-6.png new file mode 100644 index 0000000000..8268349eb5 Binary files /dev/null and b/docs/docs/guide/images/rag-img-6.png differ diff --git a/docs/docs/guide/images/rag-img-7.png b/docs/docs/guide/images/rag-img-7.png new file mode 100644 index 0000000000..cda9e98db2 Binary files /dev/null and b/docs/docs/guide/images/rag-img-7.png differ diff --git a/docs/docs/guide/images/rag-img-8.png b/docs/docs/guide/images/rag-img-8.png new file mode 100644 index 0000000000..082a3785fd Binary files /dev/null and b/docs/docs/guide/images/rag-img-8.png differ diff --git a/docs/docs/guide/images/rag-img-9.png b/docs/docs/guide/images/rag-img-9.png new file mode 100644 index 0000000000..5480245bbc Binary files /dev/null and b/docs/docs/guide/images/rag-img-9.png differ diff --git a/docs/docs/guide/rag.md b/docs/docs/guide/rag.md new file mode 100644 index 0000000000..7106ef92d6 --- /dev/null +++ b/docs/docs/guide/rag.md @@ -0,0 +1,1643 @@ +--- +seo_title: "Deep Lake RAG | RAG Application using Deeplake Multimodal vector search" +description: "Up to 10x More Efficient Data Retrieval with TQL, learn how to build RAG applications with deeplake." +--- + +# Advancing Search Capabilities: From Lexical to Multi-Modal with Deep Lake + +Install the main libraries: + +```bash +pip install deeplake +``` + +## Load the Data from Deep Lake + +The following code opens the dataset in read-only mode from Deep Lake at the specified path `al://activeloop/restaurant_reviews_complete`. The `scraped_data` object now contains the complete restaurant dataset, featuring 160 restaurants and over 24,000 images, ready for data extraction and processing. + + + +```python +import deeplake +scraped_data = deeplake.open_read_only(f"al://activeloop/restaurant_reviews_complete") +``` + +```python +print(f"Scraped {len(scraped_data)} reviews") +``` + +Output: + +``` +Scraped 18625 reviews +``` + +## 1) Create the Dataset and Use an Inverted Index for Filtering + +In the first stage of this course, we'll cover Lexical Search, a traditional and foundational approach to information retrieval. + +![Architecture](images/rag-img-1.png) + +An inverted index is a data structure commonly used in search engines and databases to facilitate fast full-text searches. Unlike a row-wise search, which scans each row of a document or dataset for a search term, an inverted index maps each unique word or term to the locations (such as document IDs or row numbers) where it appears. This setup allows for very efficient retrieval of information, especially in large datasets. + +For small datasets with up to 1,000 documents, row-wise search can provide efficient performance without needing an inverted index. For medium-sized datasets (10,000+ documents), inverted indexes become useful, particularly if search queries are frequent. For large datasets of 100,000+ documents, using an inverted index is essential to ensure efficient query processing and meet performance expectations. + +```python +import deeplake +from deeplake import types + +# Create a dataset +inverted_index_dataset = "local_inverted_index" +ds = deeplake.create(f"file://{inverted_index_dataset}") +``` + +### Extract the data + +This code extracts restaurant details from `scraped_data` into separate lists: + +1. **Initialize Lists**: `restaurant_name`, `restaurant_review` and `owner_answer` are initialized to store respective data for each restaurant. + +2. **Populate Lists**: For each entry (`el`) in `scraped_data`, the code appends: + - `el['restaurant_name']` to `restaurant_name` + - `el['restaurant_review']` to `restaurant_review` + - `el['owner_answer']` to `owner_answer` + +After running, each list holds a specific field from all restaurants, ready for further processing. + +```python +restaurant_name = [] +restaurant_review = [] +owner_answer = [] +images = [] +for el in scraped_data: + restaurant_name.append(el['restaurant_name']) + restaurant_review.append(el['restaurant_review']) + owner_answer.append(el['owner_answer']) +``` + +### Add the data to the dataset + +We add the collected restaurant names and reviews to the dataset `ds`. Using `ds.append()`, we insert two columns: `"restaurant_name"` and `"restaurant_review"`, populated with the values from our lists `restaurant_name` and `restaurant_review`. After appending the data, `ds.commit()` saves the changes permanently to the dataset, ensuring all new entries are stored and ready for further processing. + +```python +ds.append({ + "restaurant_name": restaurant_name, + "restaurant_review": restaurant_review, + "owner_answer": owner_answer +}) +ds.commit() +print(ds) +``` + +Output: +``` +Dataset(columns=(restaurant_name,restaurant_review,owner_answer), length=18625) +``` + +### Search for the restaurant using a specific word + +We define a search query to find any entries in the dataset `ds` where the word `"tapas"` appears in the `restaurant_review` column. The command `ds.query()` runs a TQL query with `SELECT *`, which retrieves all entries that match the condition `CONTAINS(restaurant_review, '{word}')`. This search filters the dataset to show only records containing the specified word (`tapas`) in their reviews. The results are saved in the variable `view`. + +Deep Lake offers a high-performance SQL-based query engine for data analysis called `TQL` (Tensor Query Language). You can find the official documentation [here](https://docs.deeplake.ai/latest/guide/tql/). + +```python +word = 'burritos' +view = ds.query(f""" + SELECT * + WHERE CONTAINS(restaurant_review, '{word}') + LIMIT 4 +""") +print(view) +``` + +Output: +``` +Dataset(columns=(restaurant_name,restaurant_review,owner_answer), length=4) +``` + +### Show the results + +```python +for row in view: + print(f"Restaurant name: {row['restaurant_name']} \nReview: {row['restaurant_review']}") +``` + +Output: +``` +Restaurant name: Los Amigos +Review: Best Burritos i have ever tried!!!!! Wolderful!!! +Restaurant name: Los Amigos +Review: Really good breakfast burrito, and just burritos in general +Restaurant name: Los Amigos +Review: Ordered two of their veggie burritos, nothing crazy just added extra cheese and sour cream. They even repeated the order back to me and everything was fine, then when I picked the burritos up and got home they put zucchini and squash in it.. like what?? +Restaurant name: Los Amigos +Review: Don't make my mistake and over order. The portions are monstrous. The wet burritos are as big as a football. +``` + +AI data retrieval systems today face 3 challenges: `limited modalities`, `lack of accuracy`, and `high costs at scale`. Deep Lake 4.0 fixes this by enabling true multi-modality, enhancing accuracy, and reducing query costs by 2x with index-on-the-lake technology. + +Consider a scenario where we store all our data locally on a computer. Initially, this may be adequate, but as the volume of data grows, managing it becomes increasingly challenging. The computer's storage becomes limited, data access slows, and sharing information with others is less efficient. + +To address these challenges, we can transition our data storage to the cloud using Deep Lake. Designed specifically for handling large-scale datasets and AI workloads, Deep Lake enables up to 10 times faster data access. With cloud storage, hardware limitations are no longer a concern: Deep Lake offers ample storage capacity, secure access from any location, and streamlined data sharing. + +This approach provides a robust and scalable infrastructure that can grow alongside our projects, minimizing the need for frequent hardware upgrades and ensuring efficient data management. + +## 2) Create the Dataset and use BM25 to Retrieve the Data + +Our advanced `"Index-On-The-Lake"` technology enables sub-second query performance directly from object storage, such as `S3`, using minimal compute power and memory resources. Achieve up to `10x greater cost efficiency` compared to in-memory databases and `2x faster performance` than other object storage solutions, all without requiring additional disk-based caching. + +With Deep Lake, you benefit from rapid streaming columnar access to train deep learning models directly, while also executing sub-second indexed queries for retrieval-augmented generation. + +![on-the-lake-env.png](images/rag-img-2.png) + +In this stage, the system uses BM25 for a straightforward lexical search. This approach is efficient for retrieving documents based on exact or partial keyword matches. + +We start by importing deeplake and setting up an organization ID `org_id` and dataset name `dataset_name_bm25`. Next, we create a new dataset with the specified name and location in Deep Lake storage. + +We then add two columns to the dataset: `restaurant_name` and `restaurant_review`. Both columns use a BM25 index, which optimizes them for relevance-based searches, enhancing the ability to rank results based on how well they match search terms. + +Finally, we use `ds_bm25.commit()` to save these changes to the dataset and `ds_bm25.summary()` to display an overview of the dataset's structure and contents. + +If you don't have a token yet, you can sign up and then log in on the official [Activeloop website](https://app.activeloop.ai/), then click the `Create API token` button to obtain a new API token. Here, under `Select organization`, you can also find your organization ID(s). + +```python +import os, getpass +os.environ["ACTIVELOOP_TOKEN"] = getpass.getpass("Activeloop API token: ") +``` + +```python +org_id = "" +dataset_name_bm25 = "bm25_test" + +ds_bm25 = deeplake.create(f"al://{org_id}/{dataset_name_bm25}") +``` + +```python +# Add columns to the dataset +ds_bm25.add_column("restaurant_name", types.Text(index_type=types.BM25)) +ds_bm25.add_column("restaurant_review", types.Text(index_type=types.BM25)) +ds_bm25.add_column("owner_answer", types.Text(index_type=types.BM25)) +ds_bm25.commit() +ds_bm25.summary() +``` + +Output: +``` +Dataset(columns=(restaurant_name,restaurant_review,owner_answer), length=0) ++-----------------+-----------------+ +| column | type | ++-----------------+-----------------+ +| restaurant_name |text (bm25 Index)| ++-----------------+-----------------+ +|restaurant_review|text (bm25 Index)| ++-----------------+-----------------+ +| owner_answer |text (bm25 Index)| ++-----------------+-----------------+ +``` + +### Add data to the dataset + +We add data to the `ds_bm25` dataset by appending the two columns, filled with values from the lists we previously created. + +After appending, `ds_bm25.commit()` saves the changes, ensuring the new data is permanently stored in the dataset. Finally, `ds_bm25.summary()` provides a summary of the dataset's updated structure and contents, allowing us to verify that the data was added successfully. + +```python +ds_bm25.append({ + "restaurant_name": restaurant_name, + "restaurant_review": restaurant_review, + "owner_answer": owner_answer +}) +ds_bm25.commit() +ds_bm25.summary() +``` + +Output: +``` +Dataset(columns=(restaurant_name,restaurant_review,owner_answer), length=18625) ++-----------------+-----------------+ +| column | type | ++-----------------+-----------------+ +| restaurant_name |text (bm25 Index)| ++-----------------+-----------------+ +|restaurant_review|text (bm25 Index)| ++-----------------+-----------------+ +| owner_answer |text (bm25 Index)| ++-----------------+-----------------+ +``` + +### Search for the restaurant using a specific sentence + +We define a query, `"I want burritos"`, to find relevant restaurant reviews in the dataset. Using `ds_bm25.query()`, we search and rank entries in `restaurant_review` based on **BM25 similarity** to the query. The code orders results by how well they match the query (`BM25_SIMILARITY`), from highest to lowest relevance, and limits the output to the top 10 results. The final list of results is stored in `view_bm25`. + +```python +query = "I want burritos" +view_bm25 = ds_bm25.query(f""" + SELECT *, BM25_SIMILARITY(restaurant_review, '{query}') AS score + ORDER BY BM25_SIMILARITY(restaurant_review, '{query}') DESC + LIMIT 6 +""") +print(view_bm25) +``` + +Output: +``` +Dataset(columns=(restaurant_name,restaurant_review,owner_answer), length=6) +``` + +### Show the results + +```python +for row in view_bm25: + print(f"Restaurant name: {row['restaurant_name']} \nReview: {row['restaurant_review']}") +``` + +Output: +``` +Restaurant name: Los Amigos +Review: Best Burritos i have ever tried!!!!! Wolderful!!! +Restaurant name: Los Amigos +Review: Fantastic burritos! +Restaurant name: Cheztakos!!! +Review: Great burritos +Restaurant name: La Costeña +Review: Awesome burritos! +Restaurant name: La Costeña +Review: Awesome burritos +Restaurant name: La Costeña +Review: Bomb burritos +``` + +## 3) Create the Dataset and use Vector Similarity Search + +If you want to generate text embeddings for similarity search, you can choose a proprietary model like `text-embedding-3-large` from `OpenAI`, or you can opt for an `open-source` model. The [MTEB leaderboard](https://huggingface.co/spaces/mteb/leaderboard) on Hugging Face provides a selection of open-source models that have been tested for their effectiveness at converting text into embeddings, which are numerical representations that capture the meaning and nuances of words and sentences. Using these embeddings, you can perform similarity search, grouping similar pieces of text (like sentences or documents) based on their meaning. + +Selecting a model from the MTEB leaderboard offers several benefits: these models are ranked based on performance across a variety of tasks and languages, ensuring that you're choosing a model that's both accurate and versatile. If you prefer not to use a proprietary model, a high-performing model from this list is an excellent alternative. + +We start by installing and importing the `openai` library to access OpenAI's API for generating embeddings. Next, we define the function `embedding_function`, which takes `texts` as input (either a single string or a list of strings) and a model name, defaulting to `"text-embedding-3-large"`. Then, for each text, we replace newline characters with spaces to maintain clean, uniform text. Finally, we use `openai.embeddings.create()` to generate embeddings for each text and return a list of these embeddings, which can be used for cosine similarity comparisons. + +```python +!pip install openai +``` + +Sets the OpenAI API key in the environment using `getpass`. + +```python +os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key: ") +``` + +```python +import openai + +def embedding_function(texts, model="text-embedding-3-large"): + if isinstance(texts, str): + texts = [texts] + + texts = [t.replace("\n", " ") for t in texts] + return [data.embedding for data in openai.embeddings.create(input = texts, model=model).data] +``` + +### Create the dataset and add the columns + +Next, we add three columns to `vector_search`: + +1. `embedding`: Stores vector embeddings with a dimension size of 3072, which will enable vector-based similarity searches. + +2. `restaurant_name`: A text column with a **BM25 index**, optimizing it for relevance-based text search. + +3. `restaurant_review`: Another text column with a **BM25 index**, also optimized for efficient and ranked search results. + +4. `owner_answer`: A text column with an **inverted index**, allowing fast and efficient filtering based on specific owner answer. + +Finally, we use `vector_search.commit()` to save these new columns, ensuring the dataset structure is ready for further data additions and queries. + +```python +dataset_name_vs = "vector_indexes" +vector_search = deeplake.create(f"al://{org_id}/{dataset_name_vs}") + +# Add columns to the dataset +vector_search.add_column(name="embedding", dtype=types.Embedding(3072)) +vector_search.add_column(name="restaurant_name", dtype=types.Text(index_type=types.BM25)) +vector_search.add_column(name="restaurant_review", dtype=types.Text(index_type=types.BM25)) +vector_search.add_column(name="owner_answer", dtype=types.Text(index_type=types.Inverted)) + +vector_search.commit() +``` + +This function processes each review in `restaurant_review` and converts it into a numerical embedding. These embeddings, stored in `embeddings_restaurant_review`, represent each review as a vector, enabling us to perform cosine similarity searches and comparisons within the dataset. + +Deep Lake will handle the search computations, providing us with the final results. + +```python +# Create embeddings +batch_size = 500 +embeddings_restaurant_review = [] +for i in range(0, len(restaurant_review), batch_size): + embeddings_restaurant_review += embedding_function(restaurant_review[i : i + batch_size]) +``` + +```python +# Add data to the dataset +vector_search.append({ + "restaurant_name": restaurant_name, + "restaurant_review": restaurant_review, + "embedding": embeddings_restaurant_review, + "owner_answer": owner_answer +}) +vector_search.commit() +vector_search.summary() +``` + +Output: +``` +Dataset(columns=(embedding,restaurant_name,restaurant_review,owner_answer), length=18625) ++-----------------+---------------------+ +| column | type | ++-----------------+---------------------+ +| embedding | embedding(3072) | ++-----------------+---------------------+ +| restaurant_name | text (bm25 Index) | ++-----------------+---------------------+ +|restaurant_review| text (bm25 Index) | ++-----------------+---------------------+ +| owner_answer |text (Inverted Index)| ++-----------------+---------------------+ +``` + +### Search for the restaurant using a specific sentence + +We start by defining a search query, `"A restaurant that serves good burritos."`. + +1. **Generate Embedding for Query**: + - We call `embedding_function(query)` to generate an embedding for this query. Since `embedding_function` returns a list, we access the first (and only) item with `[0]`, storing the result in `embed_query`. + +2. **Convert Embedding to String**: + - We convert `embed_query` (a list of numbers) into a single comma-separated string using `",".join(str(c) for c in embed_query)`. This step stores the embedding as a formatted string in `str_query`, preparing it for further processing or use in queries. + +```python +query = "A restaurant that serves good burritos." +embed_query = embedding_function(query)[0] +str_query = ",".join(str(c) for c in embed_query) +``` + +1. **Define Query with Cosine Similarity**: + - We construct a TQL query (`query_vs`) to search within the `vector_search` dataset. + + - The query calculates the **cosine similarity** between the `embedding` column and `str_query`, which is the embedding of our query, `"A restaurant that serves good burritos."`. This similarity score `score` measures how closely each entry matches our query. + +2. **Order by Score and Limit Results**: + - The query orders results by `score` in descending order, showing the most relevant matches first. We limit the results to the top 3 matches to focus on the best results. + +3. **Execute Query**: + - `vector_search.query(query_vs)` runs the query on the dataset, storing the output in `view_vs`, which contains the top 3 most similar entries based on cosine similarity. +This approach helps us retrieve the most relevant records matching our query in `vector_search`. + +```python +query_vs = f""" + SELECT *, cosine_similarity(embedding, ARRAY[{str_query}]) as score + FROM ( + SELECT *, ROW_NUMBER() AS row_id + ) + ORDER BY cosine_similarity(embedding, ARRAY[{str_query}]) DESC + + LIMIT 3 +""" +view_vs = vector_search.query(query_vs) +print(view_vs) +``` + +Output: +``` +Dataset(columns=(embedding,restaurant_name,restaurant_review,owner_answer,row_id,score), length=3) +``` + +```python +for row in view_vs: + print(f"Restaurant name: {row['restaurant_name']} \nReview: {row['restaurant_review']}") +``` + +Output: +``` +Restaurant name: Cheztakos!!! +Review: Great burritos +Restaurant name: Los Amigos +Review: Nice place real good burritos. +Restaurant name: La Costeña +Review: Awesome burritos +``` + +If we want to filter for a specific owner answer, such as **Thank you**, we set `word = "Thank you"` to define the desired owner answer. Here, we're using an **inverted index** on the `owner_answer` column to efficiently filter results based on this owner answer. + +```python +word = "Thank you" +query_vs = f""" + SELECT *, cosine_similarity(embedding, ARRAY[{str_query}]) as score + FROM ( + SELECT *, ROW_NUMBER() AS row_id + ) + + WHERE CONTAINS(owner_answer, '{word}') + ORDER BY cosine_similarity(embedding, ARRAY[{str_query}]) DESC + + LIMIT 3 +""" +view_vs = vector_search.query(query_vs) +for row in view_vs: + print(f"Restaurant name: {row['restaurant_name']} \nReview: {row['restaurant_review']} \nOwner Answer: {row['owner_answer']}") +``` + +Output: +``` +Restaurant name: Taqueria La Espuela +Review: My favorite place for super burrito and horchata +Owner Answer: Thank you for your continued support! +Restaurant name: Chaat Bhavan Mountain View +Review: Great place with good food +Owner Answer: Thank you for your positive feedback! We're thrilled to hear that you had a great experience at our restaurant and enjoyed our delicious food. Your satisfaction is our priority, and we can't wait to welcome you back for another wonderful dining experience. + +Thanks, +Team Chaat Bhavan +Restaurant name: Chaat Bhavan Mountain View +Review: Good food. +Owner Answer: Thank you for your 4-star rating! We're glad to hear that you had a positive experience at our restaurant. Your feedback is valuable to us, and we appreciate your support. If there's anything specific we can improve upon to earn that extra star next time, please let us know. We look forward to serving you again soon. + +Thanks, +Team Chaat Bhavan +``` + +## 4) Explore Results with Hybrid Search + +![image.png](images/rag-img-3.png) + +In the stage, the system enhances its search capabilities by combining BM25 with Approximate Nearest Neighbors (ANN) for a hybrid search. This approach blends lexical search with semantic search, improving relevance by considering both keywords and semantic meaning. The introduction of a Large Language Model (LLM) allows the system to generate text-based answers, delivering direct responses instead of simply listing relevant documents. + +We open the `vector_search` dataset to perform a hybrid search. First, we define a query `"Let's grab a drink"` and generate its embedding using `embedding_function(query)[0]`. We then convert this embedding into a comma-separated string `embedding_string`, preparing it for use in combined text and vector-based searches. + +```python +vector_search = deeplake.open(f"al://{org_id}/{dataset_name_vs}") +``` + +### Search for the correct restaurant using a specific sentence + +```python +query = "I feel like a drink" +embed_query = embedding_function(query)[0] +embedding_string = ",".join(str(c) for c in embed_query) +``` + +We create two queries: + +1. **Vector Search** (`tql_vs`): Calculates cosine similarity with `embedding_string` and returns the top 5 matches by score. + +2. **BM25 Search** (`tql_bm25`): Ranks `restaurant_review` by BM25 similarity to `query`, also limited to the top 5. + +We then execute both queries, storing vector results in `vs_results` and BM25 results in `bm25_results`. This allows us to compare results from both search methods. + +```python +tql_vs = f""" + SELECT *, cosine_similarity(embedding, ARRAY[{embedding_string}]) as score + FROM ( + SELECT *, ROW_NUMBER() AS row_id + ) + ORDER BY cosine_similarity(embedding, ARRAY[{embedding_string}]) DESC + LIMIT 5 +""" + +tql_bm25 = f""" + SELECT *, BM25_SIMILARITY(restaurant_review, '{query}') as score + FROM ( + SELECT *, ROW_NUMBER() AS row_id + ) + ORDER BY BM25_SIMILARITY(restaurant_review, '{query}') DESC + LIMIT 5 +""" + +vs_results = vector_search.query(tql_vs) +bm25_results = vector_search.query(tql_bm25) +print(vs_results) +print(bm25_results) +``` + +Output: +``` +Dataset(columns=(embedding,restaurant_name,restaurant_review,owner_answer,row_id,score), length=5) +Dataset(columns=(embedding,restaurant_name,restaurant_review,owner_answer,row_id,score), length=5) +``` + +### Show the scores + +```python +for el_vs in vs_results: + print(f"vector search score: {el_vs['score']}") + +for el_bm25 in bm25_results: + print(f"bm25 score: {el_bm25['score']}") +``` + +Output: +``` +vector search score: 0.5322654247283936 +vector search score: 0.46281781792640686 +vector search score: 0.4580579102039337 +vector search score: 0.45585304498672485 +vector search score: 0.4528498649597168 +bm25 score: 13.076177597045898 +bm25 score: 11.206666946411133 +bm25 score: 11.023599624633789 +bm25 score: 10.277934074401855 +bm25 score: 10.238584518432617 +``` + +First, we import the required libraries and define a Document class, where each document has an id, a data dictionary, and an optional score for ranking. + +1. **Setup and Classes**: + - We import necessary libraries and define a `Document` class using `pydantic.BaseModel`. Each `Document` has an `id`, a `data` dictionary, and an optional `score` for ranking. + +2. **Softmax Function**: + - The `softmax` function normalizes a list of scores (`retrieved_score`) using the softmax formula. Scores are exponentiated, limited by `max_weight`, and then normalized to sum up to 1. This returns `new_weights`, a list of normalized scores. + +Install the required libraries: + +```bash +pip install numpy pydantic +``` + +```python +import math +import numpy as np +from typing import Any, Dict, List, Optional +from pydantic import BaseModel + +class Document(BaseModel): + id: str + data: Dict[str, Any] + score: Optional[float] = None + +def softmax(retrieved_score: list[float], max_weight: int = 700) -> Dict[str, Document]: + # Compute the exponentials + exp_scores = [math.exp(min(score, max_weight)) for score in retrieved_score] + + # Compute the sum of the exponentials + sum_exp_scores = sum(exp_scores) + + # Update the scores of the documents using softmax + new_weights = [] + for score in exp_scores: + new_weights.append(score / sum_exp_scores) + + return new_weights +``` + +### Normalize the score + +1. **Apply Softmax to Scores**: + - We extract `score` values from `vs_results` and `bm25_results` and apply `softmax` to them, storing the results in `vss` and `bm25s`. This step scales both sets of scores for easy comparison. + +2. **Create Document Dictionaries**: + - We create dictionaries `docs_vs` and `docs_bm25` to store documents from `vs_results` and `bm25_results`, respectively. For each result, we add the `restaurant_name` and `restaurant_review` along with the normalized score. Each document is identified by `row_id`. + +This code standardizes scores and organizes results, allowing comparison across both vector and BM25 search methods. + +```python +vs_score = vs_results["score"] +bm_score = bm25_results["score"] + +vss = softmax(vs_score) +bm25s = softmax(bm_score) +print(vss) +print(bm25s) +print(vs_results) +``` + +Output: +``` +[0.21224761685297047, 0.19800771415362647, 0.1970674552539808, 0.19663342673946818, 0.19604378699995426] +[0.7132230191866898, 0.10997834807700335, 0.09158030054295993, 0.04344738382536802, 0.04177094836797888] +Dataset(columns=(embedding,restaurant_name,restaurant_review,owner_answer,row_id,score), length=5) +``` + +```python +docs_vs = {} +docs_bm25 = {} +for el, score in zip(vs_results, vss): + docs_vs[str(el["row_id"])] = Document(id=str(el["row_id"]), data={"restaurant_name": el["restaurant_name"], "restaurant_review": el["restaurant_review"]}, score=score) + +for el, score in zip(bm25_results, bm25s): + docs_bm25[str(el["row_id"])] = Document(id=str(el["row_id"]), data={"restaurant_name": el["restaurant_name"], "restaurant_review": el["restaurant_review"]}, score=score) +print(docs_vs) +print(docs_bm25) +``` + +Output: +``` +{'17502': Document(id='17502', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Nice place for a drink'}, score=0.21224761685297047), + '17444': Document(id='17444', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Good drinks, good food'}, score=0.19800771415362647), + '4022': Document(id='4022', data={'restaurant_name': 'Eureka! Mountain View', 'restaurant_review': 'Good drinks and burgers'}, score=0.1970674552539808), + '17426': Document(id='17426', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Good drinks an easy going bartenders'}, score=0.19663342673946818), + '5136': Document(id='5136', data={'restaurant_name': 'Scratch', 'restaurant_review': 'Just had drinks. They were good!'}, score=0.19604378699995426)} + +{'3518': Document(id='3518', data={'restaurant_name': 'Olympus Caffe & Bakery', 'restaurant_review': 'I like the garden to sit down with friends and have a drink.'}, score=0.7132230191866898), + '2637': Document(id='2637', data={'restaurant_name': 'Mifen101 花溪米粉王', 'restaurant_review': 'Feel like I’m back in China.'}, score=0.10997834807700335), + '11383': Document(id='11383', data={'restaurant_name': 'Ludwigs Biergarten Mountain View', 'restaurant_review': 'Beer is fresh tables are big feel like a proper beer garden'}, score=0.09158030054295993), + '2496': Document(id='2496', data={'restaurant_name': 'Seasons Noodles & Dumplings Garden', 'restaurant_review': 'Comfort food, excellent service! Feel like back to home.'}, score=0.04344738382536802), + '10788': Document(id='10788', data={'restaurant_name': 'Casa Lupe', 'restaurant_review': 'Run by a family that makes you feel like part of the family. Awesome food. I love their wet Chili Verde burritos'}, score=0.04177094836797888)} +``` + +### Fusion method + +We define weights for our hybrid search: `VECTOR_WEIGHT` and `LEXICAL_WEIGHT` are both set to `0.5`, giving equal importance to vector-based and BM25 scores. + +1. **Initialize Results Dictionary**: + - We create an empty dictionary, `results`, to store documents with their combined scores from both search methods. + +2. **Combine Scores**: + - We iterate over the unique document IDs from `docs_vs` and `docs_bm25`. + + - For each document: + - We add it to `results`, defaulting to the version available (vector or BM25). + - We calculate a weighted score: `vs_score` from vector results (if present in `docs_vs`) and `bm_score` from BM25 results (if present in `docs_bm25`). + - The final `results[k].score` is set by adding `vs_score` and `bm_score`. + +This produces a fused score for each document in `results`, ready to rank in the hybrid search. + +```python +def fusion(docs_vs: Dict[str, Document], docs_bm25: Dict[str, Document]) -> Dict[str, Document]: + VECTOR_WEIGHT = 0.5 + LEXICAL_WEIGHT = 0.5 + + results: Dict[str, Dict[str, Document]] = {} + + + for k in set(docs_vs) | set(docs_bm25): + results[k] = docs_vs.get(k, None) or docs_bm25.get(k, None) + vs_score = VECTOR_WEIGHT * docs_vs[k].score if k in docs_vs else 0 + bm_score = LEXICAL_WEIGHT * docs_bm25[k].score if k in docs_bm25 else 0 + results[k].score = vs_score + bm_score + + return results +``` + +```python +results = fusion(docs_vs, docs_bm25) +print(results) +``` + +Output: +``` +{'2637': Document(id='2637', data={'restaurant_name': 'Mifen101 花溪米粉王', 'restaurant_review': 'Feel like I’m back in China.'}, score=0.013747293509625419), + '5136': Document(id='5136', data={'restaurant_name': 'Scratch', 'restaurant_review': 'Just had drinks. They were good!'}, score=0.024505473374994282), + '17426': Document(id='17426', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Good drinks an easy going bartenders'}, score=0.024579178342433523), + '17444': Document(id='17444', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Good drinks, good food'}, score=0.02475096426920331), + '2496': Document(id='2496', data={'restaurant_name': 'Seasons Noodles & Dumplings Garden', 'restaurant_review': 'Comfort food, excellent service! Feel like back to home.'}, score=0.005430922978171003), + '4022': Document(id='4022', data={'restaurant_name': 'Eureka! Mountain View', 'restaurant_review': 'Good drinks and burgers'}, score=0.0246334319067476), + '3518': Document(id='3518', data={'restaurant_name': 'Olympus Caffe & Bakery', 'restaurant_review': 'I like the garden to sit down with friends and have a drink.'}, score=0.08915287739833623), + '17502': Document(id='17502', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Nice place for a drink'}, score=0.02653095210662131), + '11383': Document(id='11383', data={'restaurant_name': 'Ludwigs Biergarten Mountain View', 'restaurant_review': 'Beer is fresh tables are big feel like a proper beer garden'}, score=0.011447537567869991), + '10788': Document(id='10788', data={'restaurant_name': 'Casa Lupe', 'restaurant_review': 'Run by a family that makes you feel like part of the family. Awesome food. I love their wet Chili Verde burritos'}, score=0.00522136854599736)} +``` + +We sort the results dictionary by each document's combined score in descending order, ensuring that the highest-ranking documents appear first. + +```python +sorted_documents = dict(sorted(results.items(), key=lambda item: item[1].score, reverse=True)) +print(sorted_documents) +``` + +Output: +``` +{'3518': Document(id='3518', data={'restaurant_name': 'Olympus Caffe & Bakery', 'restaurant_review': 'I like the garden to sit down with friends and have a drink.'}, score=0.3566115095933449), + '17502': Document(id='17502', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Nice place for a drink'}, score=0.10612380842648524), + '17444': Document(id='17444', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Good drinks, good food'}, score=0.09900385707681324), + '4022': Document(id='4022', data={'restaurant_name': 'Eureka! Mountain View', 'restaurant_review': 'Good drinks and burgers'}, score=0.0985337276269904), + '17426': Document(id='17426', data={'restaurant_name': "St. Stephen's Green", 'restaurant_review': 'Good drinks an easy going bartenders'}, score=0.09831671336973409), + '5136': Document(id='5136', data={'restaurant_name': 'Scratch', 'restaurant_review': 'Just had drinks. They were good!'}, score=0.09802189349997713), + '2637': Document(id='2637', data={'restaurant_name': 'Mifen101 花溪米粉王', 'restaurant_review': 'Feel like I’m back in China.'}, score=0.054989174038501676), + '11383': Document(id='11383', data={'restaurant_name': 'Ludwigs Biergarten Mountain View', 'restaurant_review': 'Beer is fresh tables are big feel like a proper beer garden'}, score=0.045790150271479965), + '2496': Document(id='2496', data={'restaurant_name': 'Seasons Noodles & Dumplings Garden', 'restaurant_review': 'Comfort food, excellent service! Feel like back to home.'}, score=0.02172369191268401), + '10788': Document(id='10788', data={'restaurant_name': 'Casa Lupe', 'restaurant_review': 'Run by a family that makes you feel like part of the family. Awesome food. I love their wet Chili Verde burritos'}, score=0.02088547418398944)} +``` + +### Show the results + +We will output a list of restaurants in order of relevance, showing each name and review based on the hybrid search results. + +```python +for v in sorted_documents.values(): + print(f"Restaurant name: {v.data['restaurant_name']} \nReview: {v.data['restaurant_review']}") +``` + +Output: +``` +Restaurant name: Olympus Caffe & Bakery +Review: I like the garden to sit down with friends and have a drink. +Restaurant name: St. Stephen's Green +Review: Nice place for a drink +Restaurant name: St. Stephen's Green +Review: Good drinks, good food +Restaurant name: Eureka! Mountain View +Review: Good drinks and burgers +Restaurant name: St. Stephen's Green +Review: Good drinks an easy going bartenders +Restaurant name: Scratch +Review: Just had drinks. They were good! +Restaurant name: Mifen101 花溪米粉王 +Review: Feel like I’m back in China. +Restaurant name: Ludwigs Biergarten Mountain View +Review: Beer is fresh tables are big feel like a proper beer garden +Restaurant name: Seasons Noodles & Dumplings Garden +Review: Comfort food, excellent service! Feel like back to home. +Restaurant name: Casa Lupe +Review: Run by a family that makes you feel like part of the family. Awesome food. I love their wet Chili Verde burritos +``` + +This code completes the RAG (Retrieval-Augmented Generation) approach by generating an LLM-based answer to a user's question, using results retrieved in the previous step. Here's how it works: + +1. **Setup and Initialization**: + - We import `json` for handling JSON responses and initialize the `OpenAI` client to interact with the language model. + +2. **Define `generate_question` Function**: + - This function accepts: + - `question`: The user's question. + - `information`: A list relevant chunks retrieved previously, providing context. + +3. **System and User Prompts**: + - The `system_prompt` instructs the model to act as a restaurant assistant, using the provided chunks to answer clearly and without repetition. + + - The model is directed to format its response in JSON. + + - The `user_prompt` combines the user's question and the information chunks. + +4. **Generate and Parse the Response**: + - Using `client.chat.completions.create()`, the system and user prompts are sent to the LLM (specified as `gpt-4o-mini`). + + - The response is parsed as JSON, extracting the `answer` field. If parsing fails, `False` is returned. + +```python +import json +from openai import OpenAI + +client = OpenAI() + +def generate_question(question:str, information:list): + system_prompt = f"""You are a helpful assistant specialized in providing answers to questions about restaurants. Below is a question from a user, along with the top four relevant information chunks about restaurants from a Deep Lake database. Using these chunks, construct a clear and informative answer that addresses the question, incorporating key details without repeating information. + The output must be in JSON format with the following structure: + {{ + "answer": "The answer to the question." + }} + + """ + + user_prompt = f"Here is a question from a user: {question}\n\nHere are the top relevant information about restaurants {information}" + response = client.chat.completions.create( + model="gpt-4o-mini", + messages=[ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt}, + ], + response_format={"type": "json_object"}, + ) + + try: + response = response.choices[0].message.content + response = json.loads(response) + questions = response["answer"] + return questions + except: + return False +``` + +This function takes a restaurant-related question and retrieves the best response based on the given context. It completes the RAG process by combining relevant information and LLM-generated content into a concise answer. + +```python +information = [f'Review: {el["restaurant_review"]}, Restaurant name: {el["restaurant_name"]}' for el in view_vs] +result = generate_question(query, information) +print(result) +``` + +Output: +``` +"If you're feeling like a drink, consider visiting Taqueria La Espuela, which is known for its refreshing horchata. Alternatively, you might enjoy Chaat Bhavan Mountain View, a great place with good food and a lively atmosphere." +``` + +### Let's run a search on a multiple dataset + +In this approach, we perform the hybrid search across two separate datasets: `vector_search` for vector-based search results and `ds_bm25` for BM25-based text search results. This allows us to independently query and retrieve scores from each dataset, then combine them using the same fusion method as before. + +```python +ds_bm25 = deeplake.open(f"al://{org_id}/{dataset_name_bm25}") +vs_results = vector_search.query(tql_vs) +bm25_results = ds_bm25.query(tql_bm25) +``` + +```python +vs_score = vs_results["score"] +bm_score = bm25_results["score"] + +vss = softmax(vs_score) +bm25s = softmax(bm_score) +``` + +```python +docs_vs = {} +docs_bm25 = {} +for el, score in zip(vs_results, vss): + docs_vs[str(el["row_id"])] = Document(id=str(el["row_id"]), data={"restaurant_name": el["restaurant_name"], "restaurant_review": el["restaurant_review"]}, score=score) + +for el, score in zip(bm25_results, bm25s): + docs_bm25[str(el["row_id"])] = Document(id=str(el["row_id"]), data={"restaurant_name": el["restaurant_name"], "restaurant_review": el["restaurant_review"]}, score=score) +``` + +```python +results = fusion(docs_vs, docs_bm25) +``` + +```python +for v in sorted_documents.values(): + print(f"Restaurant name: {v.data['restaurant_name']} \nReview: {v.data['restaurant_review']}") +``` + +Output: +``` +Restaurant name: Olympus Caffe & Bakery +Review: I like the garden to sit down with friends and have a drink. +Restaurant name: St. Stephen's Green +Review: Nice place for a drink +Restaurant name: St. Stephen's Green +Review: Good drinks, good food +Restaurant name: Eureka! Mountain View +Review: Good drinks and burgers +Restaurant name: St. Stephen's Green +Review: Good drinks an easy going bartenders +Restaurant name: Scratch +Review: Just had drinks. They were good! +Restaurant name: Mifen101 花溪米粉王 +Review: Feel like I’m back in China. +Restaurant name: Ludwigs Biergarten Mountain View +Review: Beer is fresh tables are big feel like a proper beer garden +Restaurant name: Seasons Noodles & Dumplings Garden +Review: Comfort food, excellent service! Feel like back to home. +Restaurant name: Casa Lupe +Review: Run by a family that makes you feel like part of the family. Awesome food. I love their wet Chili Verde burritos +``` + +### Comparison of Sync vs Async Query Performance + +This code performs an asynchronous query on a Deep Lake dataset. It begins by opening the dataset asynchronously using `await deeplake.open_async()`, specifying `org_id` and `dataset_name_vs`. + +```python +ds_async = await deeplake.open_async(f"al://{org_id}/{dataset_name_vs}") +ds_async_results = ds_async.query_async(tql_vs).result() +``` + +This following code compares the execution times of synchronous and asynchronous queries on a Deep Lake dataset: + +- First, it records the start time `start_sync` for the synchronous query, executes the query with `vector_search.query(tql_vs)`, and then records the end time `end_sync`. It calculates and prints the total time taken for the synchronous query by subtracting `start_sync` from `end_sync`. +- Next, it measures the asynchronous query execution by recording `start_async`, running `vector_search.query_async(tql_vs).result()` to execute and retrieve the query result asynchronously, and then recording `end_async`. The asynchronous query time is calculated as the difference between `end_async` and `start_async`, and is printed. + +The code executes two queries both synchronously and asynchronously, measuring the execution time for each method. In the synchronous part, the queries are executed one after the other, and the execution time is recorded. In the asynchronous part, the queries are run concurrently using `asyncio.gather()` to parallelize the asynchronous calls, and the execution time is also measured. The "speed factor" is then calculated by comparing the execution times, showing how much faster the asynchronous execution is compared to the synchronous one. Using `asyncio.gather()` allows the asynchronous queries to run in parallel, reducing the overall execution time. + +```python +import time +import asyncio +import nest_asyncio + +nest_asyncio.apply() + +async def run_async_queries(): + # Use asyncio.gather to run queries concurrently + ds_async_results, ds_bm25_async_results = await asyncio.gather( + vector_search.query_async(tql_vs), + ds_bm25.query_async(tql_bm25) + ) + return ds_async_results, ds_bm25_async_results + +# Measure synchronous execution time +start_sync = time.time() +ds_sync_results = vector_search.query(tql_vs) +ds_bm25_sync_results = ds_bm25.query(tql_bm25) +end_sync = time.time() +print(f"Sync query time: {end_sync - start_sync}") + +# Measure asynchronous execution time +start_async = time.time() +# Run the async queries concurrently using asyncio.gather +ds_async_results, ds_bm25_async_results = asyncio.run(run_async_queries()) +end_async = time.time() +print(f"Async query time: {end_async - start_async}") + +sync_time = end_sync - start_sync +async_time = end_async - start_async + +# Calculate speed factor +speed_factor = sync_time / async_time + +# Print the result +print(f"The async query is {speed_factor:.2f} times faster than the sync query.") +``` + +We can execute asynchronous queries even after loading the dataset synchronously. In the following example, we perform a BM25 query asynchronously on a dataset `ds_bm25` that was loaded synchronously. + +```python +result_async_with_bm25 = ds_bm25.query_async(tql_bm25).result() +print(result_async_with_bm25) +``` + +Output: +``` +Dataset(columns=(restaurant_name,restaurant_review,owner_answer,row_id,score), length=5) +``` + +## 5) Integrating Image Embeddings for Multi-Modal Search + +![image.png](images/rag-img-4.png) + +In the third stage, the system gains multi-modal retrieval capabilities, handling both papers and images. This setup allows for the retrieval of images alongside text, making it suitable for fields that require visual data, such as medicine and science. The use of cosine similarity on image embeddings enables it to rank images based on similarity to the query, while the Vision Language Model (VLM) allows the system to provide visual answers as well as text. + +Install required libraries + +```python +!pip install -U torchvision +!pip install git+https://github.com/openai/CLIP.git +``` + +To set up for image embedding generation, we start by importing necessary libraries. + +1. **Set Device**: + - We define `device` to use GPU if available, otherwise defaulting to CPU, ensuring compatibility across hardware. + +2. **Load CLIP Model**: + - We load the CLIP model (`ViT-B/32`) with its associated preprocessing steps using `clip.load()`. This model is optimized for multi-modal tasks and is set to run on the specified `device`. + +This setup allows us to efficiently process images for embedding, supporting multi-modal applications like image-text similarity. + +The following image illustrates the `CLIP` (Contrastive Language-Image Pretraining) model's structure, which aligns text and images in a shared embedding space, enabling cross-modal understanding. + +![](https://github.com/openai/CLIP/raw/main/CLIP.png) + +```python +import torch +import clip + +device = "cuda" if torch.cuda.is_available() else "cpu" +model, preprocess = clip.load("ViT-B/32", device=device) +``` + +### Create the embedding function for images + +To prepare images for embedding generation, we define a transformation pipeline and a function to process images in batches. + +1. **Define Transformations (`tform`)**: + - The transformation pipeline includes: + - **Resize**: Scales images to 224x224 pixels. + - **ToTensor**: Converts images to tensor format. + - **Lambda**: Ensures grayscale images are replicated across three channels to match the RGB format. + - **Normalize**: Standardizes pixel values based on common RGB means and standard deviations. + +2. **Define `embedding_function_images`**: + - This function generates embeddings for a list of image. + - If `images` is a single filename, it's converted to a list. + - **Batch Processing**: Images are processed in batches (default size 4), with transformations applied to each image. The batch is then loaded to the device. + - **Embedding Creation**: The model encodes each batch into embeddings, stored in the `embeddings` list, which is returned as a single list. + +This function supports efficient, batched embedding generation, useful for multi-modal tasks like image-based search. + +```python +from torchvision import transforms + +tform = transforms.Compose([ + transforms.Resize((224,224)), + transforms.ToTensor(), + transforms.Lambda(lambda x: torch.cat([x, x, x], dim=0) if x.shape[0] == 1 else x), + transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), +]) + +def embedding_function_images(images, model = model, transform = tform, batch_size = 4): + """Creates a list of embeddings based on a list of image. Images are processed in batches.""" + + if isinstance(images, str): + images = [images] + + # Proceess the embeddings in batches, but return everything as a single list + embeddings = [] + for i in range(0, len(images), batch_size): + batch = torch.stack([transform(item) for item in images[i:i+batch_size]]) + batch = batch.to(device) + with torch.no_grad(): + embeddings+= model.encode_image(batch).cpu().numpy().tolist() + + return embeddings +``` + +### Create a new dataset to save the images + +We set up a dataset for restaurant images and embeddings. The dataset includes an `embedding` column for 512-dimensional image embeddings, a `restaurant_name` column for names, and an `image` column for storing images in UInt8 format. After defining the structure, `vector_search_images.commit()` saves it, making the dataset ready for storing data for multi-modal search tasks with images and metadata. + +```python +import deeplake +scraped_data = deeplake.open_read_only("al://activeloop/restaurant_dataset_complete") +``` + +This code extracts restaurant details from `scraped_data` into separate lists: + +1. **Initialize Lists** : `restaurant_name` and `images` are initialized to store respective data for each restaurant. + +2. **Populate Lists** : For each entry (`el`) in `scraped_data`, the code appends: + - `el['restaurant_name']` to `restaurant_name`, + - `el['images']['urls']` to `images`. + +After running, each list holds a specific field from all restaurants, ready for further processing. + +```python +restaurant_name = [] +images = [] +for el in scraped_data: + restaurant_name.append(el['restaurant_name']) + images.append(el['images']['urls']) +``` + +```python +image_dataset_name = "restaurant_dataset_with_images" +vector_search_images = deeplake.create(f"al://{org_id}/{image_dataset_name}") + +vector_search_images.add_column(name="embedding", dtype=types.Embedding(512)) +vector_search_images.add_column(name="restaurant_name", dtype=types.Text()) +vector_search_images.add_column(name="image", dtype=types.Image(dtype=types.UInt8())) + +vector_search_images.commit() +``` + +### Convert the URLs into images + +We retrieve images for each restaurant from URLs in scraped_data and store them in restaurants_images. For each restaurant, we extract image URLs, request each URL, and filter for successful responses (status code 200). These responses are then converted to PIL images and added to restaurants_images as lists of images, with each sublist containing the images for one restaurant. + +```bash +!pip install requests +``` + +```python +import requests +from PIL import Image +from io import BytesIO + +restaurants_images = [] +for urls in images: + pil_images = [] + for url in urls: + response = requests.get(url) + if response.status_code == 200: + image = Image.open(BytesIO(response.content)) + if image.mode == "RGB": + pil_images.append(image) + if len(pil_images) == 0: + pil_images.append(Image.new("RGB", (224, 224), (255, 255, 255))) + restaurants_images.append(pil_images) +``` + +We populate `vector_search_images` with restaurant image data and embeddings. For each restaurant in `scraped_data`, we retrieve its name and images, create embeddings for the images, and convert them to `UInt8` arrays. Then, we append the restaurant names, images, and embeddings to the dataset and save with `vector_search_images.commit()`. + +```python +import numpy as np + +for sd, rest_images in zip(scraped_data, restaurants_images): + restaurant_name = [sd["restaurant_name"]] * len(rest_images) + embeddings = embedding_function_images(rest_images, model=model, transform=tform, batch_size=4) + vector_search_images.append({"restaurant_name": restaurant_name, "image": [np.array(fn).astype(np.uint8) for fn in rest_images], "embedding": embeddings}) + +vector_search_images.commit() +``` + +### Search similar images + +If you want direct access to the images and the embeddings, you can copy the Activeloop dataset. + +```python +deeplake.copy("al://activeloop/restaurant_dataset_images_v4", f"al://{org_id}/{image_dataset_name}") +vector_search_images = deeplake.open(f"al://{org_id}/{image_dataset_name}") +``` + +Alternatively, you can load the dataset you just created. + +```python +vector_search_images = deeplake.open(f"al://{org_id}/{image_dataset_name}") +vector_search_images +``` + +```python +query = "https://www.moltofood.it/wp-content/uploads/2024/09/Hamburger.jpg" + +image_query = requests.get(query) +image_query_pil = Image.open(BytesIO(image_query.content)) +``` + +### Performing a similar image search based on a specific image + +```python +print(image_query_pil) +``` + +Output: +![image.png](images/rag-img-7.png) + +We generate an embedding for the query image, `image_query_pil`, by calling `embedding_function_images([image_query_pil])[0]`. This embedding is then converted into a comma-separated string, `query_embedding_string`, for compatibility in the query.The query, `tql`, retrieves entries from the dataset by calculating cosine similarity between `embedding` and `query_embedding_string`. It ranks results by similarity score in descending order, limiting the output to the top 6 most similar images. + +```python +query_embedding = embedding_function_images([image_query_pil])[0] +query_embedding_string = ",".join([str(item) for item in query_embedding]) + +tql = f""" + SELECT *, cosine_similarity(embedding, ARRAY[{query_embedding_string}]) as score + FROM ( + SELECT *, ROW_NUMBER() AS row_id + ) + ORDER BY cosine_similarity(embedding, ARRAY[{query_embedding_string}]) DESC + LIMIT 6 +""" +``` + +```python +similar_images_result = vector_search_images.query(tql) +print(similar_images_result) +``` + +Output: +``` +Dataset(columns=(embedding,restaurant_name,restaurant_review,owner_answer,row_id,score), length=6) +``` + +### Show similar images and the their respective restaurants + +```bash +!pip install matplotlib +``` + +The `show_images` function displays a grid of similar images, along with restaurant names and similarity scores. It defines a grid with 3 columns and calculates the required number of rows based on the number of images. A figure with subplots is created, where each image is displayed in a cell with its restaurant name and similarity score shown as the title, and axes turned off for a cleaner look. Any extra cells, if present, are hidden to avoid empty spaces. Finally, `plt.tight_layout()` arranges the grid, and `plt.show()` displays the images in a well-organized layout, highlighting the most similar images along with their metadata. + +```python +import matplotlib.pyplot as plt +from PIL import Image +import numpy as np + +def show_images(similar_images: list[dict]): + # Define the number of rows and columns for the grid + num_columns = 3 + num_rows = (len(similar_images) + num_columns - 1) // num_columns # Calculate the required number of rows + + # Create the grid + fig, axes = plt.subplots(num_rows, num_columns, figsize=(15, 5 * num_rows)) + axes = axes.flatten() # Flatten for easier access to cells + + for idx, el in enumerate(similar_images): + img = Image.fromarray(el["image"]) + axes[idx].imshow(img) + axes[idx].set_title(f"Restaurant: {el['restaurant_name']}, Similarity: {el['score']:.4f}") + axes[idx].axis('off') # Turn off axes for a cleaner look + + # Remove empty axes if the number of images doesn't fill the grid + for ax in axes[len(similar_images):]: + ax.axis('off') + + plt.tight_layout() + plt.show() + +show_images(similar_images_result) +``` + +Output: +![image.png](images/rag-img-8.png) + +## 6) ColBERT: Efficient and Effective Passage Search via Contextualized Late Interaction over BERT + +ColBERT is a model designed to efficiently retrieve and rank passages by leveraging the power of deep language models like BERT, but with a unique approach called **late interaction**. In traditional information retrieval, a model often computes detailed interactions between the query and every document at an early stage, which is computationally expensive, especially with large datasets. **Late interaction** , however, postpones this detailed interaction until a later stage. + +At the final stage of retrieval, **late interaction** occurs: each query token embedding interacts with the most relevant document token embeddings, using a simplified comparison (e.g., cosine similarity or max similarity). + +This targeted, late-stage interaction allows the model to capture fine-grained relationships between query and document content without requiring full-scale interactions upfront. + +![late-interaction.png](images/rag-img-6.png) + +To use ColBERT, we can leverage the `colbert-ai` library. We'll start by installing it: + +```bash +!pip install -U colbert-ai torch +``` + +In this snippet, we are loading a pretrained ColBERT model checkpoint for use in information retrieval tasks. Here's what each part does: + +1. **Importing Modules** : + - `Checkpoint` is a utility from ColBERT that allows loading and managing pretrained model checkpoints. + + - `ColBERTConfig` provides configuration options for the ColBERT model, such as directory paths and other settings. + +2. **Initializing the Checkpoint** : + - `"colbert-ir/colbertv2.0"` specifies the name of the pretrained checkpoint to load. This could be a path to a local model file or a remote model identifier, depending on your setup. + + - `ColBERTConfig(root="experiments")` sets the root directory where model-related experiments will be saved or accessed. This is useful for organizing logs, results, and intermediate files. + +3. **Purpose** : + - The `ckpt` object now contains the pretrained ColBERT model and its configuration, ready to be used for tasks like ranking or embedding documents in information retrieval pipelines. + +This step sets up the foundation for using ColBERT's capabilities in semantic search and ranking tasks efficiently. + +```python +from colbert.modeling.checkpoint import Checkpoint +from colbert.infra import ColBERTConfig + +ckpt = Checkpoint( + "colbert-ir/colbertv2.0", colbert_config=ColBERTConfig(root="experiments") +) +``` + +In this example, we copy, structure, and process a **medical dataset** to generate embeddings for text documents using a pretrained ColBERT model. + +1. **Dataset Copy and Setup** : + - The `deeplake.copy()` function duplicates the `medical_dataset` from the Activeloop repository into your organization's workspace. + + - `deeplake.open()` then opens the dataset for modifications, allowing us to add or manipulate columns. + +2. **Adding an Embedding Column** : + - A new column named `embedding` is added to the dataset with the data type `types.Array(types.Float32(), dimensions=2)`, preparing it to store 2D embeddings generated from the medical text. + + +```python +deeplake.copy(f"al://activeloop/medical_dataset", f"al://{org_id}/medical_dataset") +``` + +```python +medical_dataset = deeplake.open(f"al://{org_id}/medical_dataset") +medical_dataset.summary() +``` + +Output: +``` +Dataset(columns=(text,embedding), length=19719) ++---------+---------------------------------------+ +| column | type | ++---------+---------------------------------------+ +| text | text | ++---------+---------------------------------------+ +|embedding|array(dtype=float32, shape=[None,None])| ++---------+---------------------------------------+ +``` + +```python +medical_dataset.add_column(name="embedding", dtype=types.Array(types.Float32(),dimensions=2)) +medical_dataset.commit() +``` + +3. **Text Extraction** : + - The text data from the medical dataset is extracted into a list (`medical_text`) by iterating over the dataset and pulling the `text` field for each entry. + +4. **Batch Embedding Generation** : + - The text data is processed in batches of 1,000 entries using the ColBERT model (`ckpt.docFromText`), which generates embeddings for each batch. + + - The embeddings are appended to a list (`all_vectors`) for later use. + +5. **Efficient Processing** : + - Batching ensures efficient processing, especially when dealing with large datasets, as it prevents memory overload and speeds up embedding generation. + +```python +all_vectors = [] +medical_text = [el["text"] for el in medical_dataset] + +for i in range(0, len(medical_text), 1000): + chunk = medical_text[i:i+1000] + vectors_chunk = ckpt.docFromText(chunk) + all_vectors.extend(vectors_chunk) +``` + +```python +list_of_embeddings = [vector.tolist() for vector in all_vectors] +len(list_of_embeddings) +``` + +We convert the embeddings into Python lists for compatibility with Deep Lake storage and checks the total number of embeddings. Each embedding from all_vectors is transformed using `.tolist()`, creating list_of_embeddings, and `len(list_of_embeddings)` confirms the total count matches the processed text entries. + +```python +medical_dataset["embedding"][0:len(list_of_embeddings)] = list_of_embeddings +medical_dataset.commit() +``` + +This code performs a semantic search using ColBERT embeddings, leveraging the MaxSim operator, executed directly in the cloud (as described in the `index-on-the-lake` section), for efficient similarity computations. + +1. **Query Embedding** : The query is embedded with `ckpt.queryFromText` and converted into a format compatible with TQL queries. + +```python +query_vectors = ckpt.queryFromText(["What were the key risk factors for the development of posthemorrhagic/postoperative epilepsy in the study?"])[0] +query_vectors = query_vectors.tolist() +``` + +2. **TQL Query Construction** : The `maxsim` function compares the query embedding to dataset embeddings, ranking results by similarity and limiting them to the top `n_res` matches. + +3. **Query Execution** : `medical_dataset.query` retrieves the most relevant entries based on semantic similarity. + +```python +n_res = 3 +q_substrs = [f"ARRAY[{','.join(str(x) for x in sq)}]" for sq in query_vectors] +q_str = f"ARRAY[{','.join(q_substrs)}]" + +# Construct a formatted TQL query +tql_colbert = f""" + SELECT *, maxsim(embedding, {q_str}) as score + ORDER BY maxsim(embedding, {q_str}) DESC + LIMIT {n_res} +""" + +# Execute the query and append the results +results = medical_dataset.query(tql_colbert) +``` + +Here are the results: + +```python +for res in results: + print(f"Text: {res['text']}") +``` + +Output: +``` +Text: In resistant dogs, myocardial infarction did not affect any measure of heart rate variability: 1) mean RR interval, 2) standard deviation of the mean RR interval, and 3) the coefficient of variance (standard deviation/RR interval). By contrast, after myocardial infarction, susceptible dogs showed significant decrease in all measures of heart rate variability. Before myocardial infarction, no differences were seen between susceptible and resistant dogs. However, 30 days after infarction, epidemiologic analysis of the coefficient of variance showed high sensitivity and specificity (88% and 80%, respectively), predicting susceptibility. Therefore, results of analysis of 30 min of beat to beat heart period at rest 30 days after myocardial infarction are highly predictive for increased risk of sudden death. \n5\tMultiple organ failure: inflammatory priming and activation sequences promote autologous tissue injury. Systemic inflammation promotes multiple organ failure through the induction of diffuse microvascular leak. Inflammatory cells such as neutrophil +Text: Risk for sudden death was assessed 1 month after myocardial infarction by a protocol in which exercise and myocardial ischemia were combined; dogs that developed ventricular fibrillation were classified at high risk for sudden death (susceptible) and the survivors were considered low risk (resistant). In resistant dogs, myocardial infarction did not affect any measure of heart rate variability: 1) mean RR interval, 2) standard deviation of the mean RR interval, and 3) the coefficient of variance (standard deviation/RR interval). By contrast, after myocardial infarction, susceptible dogs showed significant decrease in all measures of heart rate variability. Before myocardial infarction, no differences were seen between susceptible and resistant dogs. However, 30 days after infarction, epidemiologic analysis of the coefficient of variance showed high sensitivity and specificity (88% and 80%, respectively), predicting susceptibility. Therefore, results of analysis of 30 min of beat to beat heart period at rest 30 days after myocardial infarction are highly predictive for increased risk of sudden death. \n5\tMultiple organ failure: inflammatory priming and activation sequences promote autologous tissue injury. +Text: However, no paired studies have been reported to examine heart rate variability before and after myocardial infarction. The hypothesis was tested that low values of heart rate variability provided risk assessment both before and after myocardial infarction with use of an established canine model of sudden cardiac death. Risk for sudden death was assessed 1 month after myocardial infarction by a protocol in which exercise and myocardial ischemia were combined; dogs that developed ventricular fibrillation were classified at high risk for sudden death (susceptible) and the survivors were considered low risk (resistant). In resistant dogs, myocardial infarction did not affect any measure of heart rate variability: 1) mean RR interval, 2) standard deviation of the mean RR interval, and 3) the coefficient of variance (standard deviation/RR interval). By contrast, after myocardial infarction, susceptible dogs showed significant decrease in all measures of heart rate variability. Before myocardial infarction, no differences were seen between susceptible and resistant dogs. However, 30 days after infarction, epidemiologic analysis of the coefficient of variance showed high sensitivity and specificity (88% and 80%, respectively), predicting susceptibility. + +``` + +## 7) Discover Restaurants Using ColPali and the Late Interaction Mechanism + +![image.png](images/rag-img-5.png) + +In this final stage, the system uses an **end-to-end neural search** approach with a focus on the **MaxSim** operator, as implemented in ColPali, to improve multi-modal retrieval. MaxSim allows the system to compare different types of data, like text and images, and find the most relevant matches. This helps retrieve results that are contextually accurate and meaningful, making it especially useful for complex applications, like scientific and medical research, where a deep understanding of the content is essential. + +Recent advancements in Visual Language Models (VLMs), as highlighted in the ColPali paper, demonstrate that VLMs can achieve recall rates on document retrieval benchmarks comparable to those of traditional OCR pipelines. End-to-end learning approaches are positioned to surpass OCR-based methods significantly. However, representing documents as a `bag of embeddings` demands 30 times more storage than single embeddings. Deep Lake's format, which inherently supports n-dimensional arrays, enables this storage-intensive approach, and the 4.0 query engine introduces MaxSim operations. + +With Deep Lake 4.0's 10x increase in storage efficiency, we can allocate some of these savings to store PDFs as 'bags of embeddings' processed at high speeds. While this approach requires 30 times more storage than single embeddings, it allows us to capture richer document representations, bypassing OCR-based, manual feature engineering pipelines. This trade-off facilitates seamless integration within VLM/LLM frameworks, leading to more accurate and genuinely multimodal responses. + +Unlike CLIP, which primarily focuses on aligning visual and text representations, ColPali leverages advanced Vision Language Model (VLM) capabilities to deeply understand both textual and visual content. This allows ColPali to capture rich document structures—like tables, figures, and layouts—directly from images without needing extensive preprocessing steps like OCR or document segmentation. ColPali also utilizes a late interaction mechanism, which significantly improves retrieval accuracy by enabling more detailed matching between query elements and document content. These features make ColPali faster, more accurate, and especially effective for visually rich document retrieval, surpassing CLIP's capabilities in these areas​. + +For more details, see the [ColPali paper](https://arxiv.org/pdf/2407.01449). + +```python +!pip install colpali-engine accelerate +``` + +### Download the ColPali model + +We initialize the **ColPali** model and its processor to handle images efficiently. The model version is set to `"vidore/colpali-v1.2"`, specifying the desired ColPali release. The model is loaded using `ColPali.from_pretrained()`, with `torch_dtype=torch.bfloat16` for optimized memory use and `"cuda:0"` as the device, or `"mps"` for Apple Silicon devices. After loading, we set the model to evaluation mode with `.eval()` to prepare it for inference tasks. The `ColPaliProcessor` is also initialized to handle preprocessing of images and texts, enabling seamless input preparation for the model. This setup readies ColPali for high-performance image and document processing. + +![](https://raw.githubusercontent.com/illuin-tech/colpali/refs/heads/main/assets/colpali_architecture.webp) + +The provided image illustrates the architecture of **ColPali** , a vision-language model designed specifically for efficient document retrieval using both visual and textual cues. Here's an overview of its workings and how it's designed to perform this task efficiently: + +1. **Offline Document Encoding** : + - On the left side, we see the **offline** processing pipeline, where a document is fed into ColPali's **Vision Language Model (VLM)** . + + - Each document undergoes encoding through a **vision encoder** (to handle images and visual content) and a **language model** (for textual understanding). These two modules generate multi-dimensional embeddings representing both visual and textual aspects of the document. + + - The embeddings are stored in a pre-indexed format, making them ready for fast retrieval during the online phase. + +2. **Online Query Processing** : + - On the right side, in the **online** section, user queries (such as "What are ViTs?") are processed through the **language model** to create a query embedding. + + - ColPali uses a **late interaction mechanism** , where each part of the query embedding is compared with document embeddings through a **MaxSim** operation to find the most similar regions in the document's visual and textual content. + +3. **Similarity Scoring** : + - ColPali calculates a **similarity score** based on the MaxSim results, which identifies the most relevant documents or document sections matching the query. + + - This approach allows ColPali to capture fine-grained matches, even within complex document structures. +The ColPali model improves on traditional document retrieval methods by incorporating both vision and language models, making it effective for **visually rich documents** (such as those with tables, images, or infographics). Additionally, its **late interaction** mechanism enables fast and accurate retrieval, optimizing the model for low-latency performance even in large-scale applications​. + +```python +import torch +from PIL import Image + +from colpali_engine.models import ColPali, ColPaliProcessor + +model_name = "vidore/colpali-v1.2" + +model = ColPali.from_pretrained( + model_name, + torch_dtype=torch.bfloat16, + device_map="cuda:0", # or "mps" if on Apple Silicon +).eval() + +processor = ColPaliProcessor.from_pretrained(model_name) +``` + +We load the **FigQA** dataset using `deeplake`, specifically retrieving the `"train"` split of the `"FigQA"` subset within the `"futurehouse/lab-bench"` dataset. This dataset, contains figure data tailored for question-answering tasks, making it an ideal input format for the ColPali model. ColPali's advanced capabilities in handling structured and tabular data enable effective extraction of answers and insights from these figures, enhancing overall performance on complex, figure-based queries. + +```python +figQA_dataset = "figQA_dataset" +fig_qa = deeplake.open_read_only(f"al://activeloop/{figQA_dataset}") +figure_images = [Image.fromarray(el["image"]) for el in fig_qa] +questions = [el["question"] for el in fig_qa] +``` + +### Create a new dataset to store the ColPali embeddings + +We create a Deep Lake dataset named `"tabqa_colpali"` for ColPali's table-based question answering. Stored in `vector_search_images`, it includes an `embedding`** column for 2D float arrays, a `question` column for text, and an `image` column for table images. After defining the structure, `vector_search_images.commit()` saves the setup, optimizing it for ColPali's multi-modal retrieval in table QA tasks. + +```python +late_interaction_dataset_name = "figQA_colpali" +vector_search_images = deeplake.create(f"al://{org_id}/{late_interaction_dataset_name}") + +vector_search_images.add_column(name="embedding", dtype=types.Array(types.Float32(),dimensions=2)) +vector_search_images.add_column(name="question", dtype=types.Text()) +vector_search_images.add_column(name="image", dtype=types.Image(dtype=types.UInt8())) + +vector_search_images.commit() +``` + +### Save the data in the dataset + +We batch-process and store ColPali embeddings for table-based question answering. +Using a `batch_size` of 2, we take the first 10 tables and questions from `table_qa`. For each pair, if `question` is a single string, it's converted to a list. The `table_image` is processed in batches, passed through `processor` and ColPali, and embeddings are generated without gradients. These embeddings are stored as lists and appended with each question and image to `vector_search_images`.Finally, `vector_search_images.commit()` saves everything for efficient retrieval. + +```python +batch_size = 8 + +matrix_embeddings: list[torch.Tensor] = [] + +for i in range(0, len(figure_images), batch_size): + batch = figure_images[i:i + batch_size] # Take batch_size images at a time + batch_images = processor.process_images(batch).to(model.device) + with torch.no_grad(): + embeddings = model(**batch_images) + matrix_embeddings.extend(list(torch.unbind(embeddings.to("cpu")))) + +# Convert embeddings to list format +matrix_embeddings_list = [embedding.tolist() for embedding in matrix_embeddings] + +# Append question, images, and embeddings to the dataset +vector_search_images.append({ + "question": questions, + "image": [np.array(img).astype(np.uint8) for img in figure_images], + "embedding": matrix_embeddings_list +}) + +# Commit the additions to the dataset +vector_search_images.commit() +``` + +### Chat with images + +We randomly select three questions from `questions` and process them with `processor`, sending the batch to the model's device. Embeddings are generated without gradients and converted to a list format, stored in `query_embeddings`. + +```python +queries = [ + "At Time (ms) = 0, the membrane potential modeled by n^6 is at -70 ms. If the axis of this graph was extended to t = infinity, what Membrane Voltage would the line modeled by n^6 eventually reach?", + "Percent frequency distribution of fiber lengths in cortex and spinal cord by diameter" +] + +batch_queries = processor.process_queries(queries).to(model.device) +with torch.no_grad(): + query_embeddings = model(**batch_queries) +query_embeddings = query_embeddings.tolist() +``` + +### Retrieve the most similar images + +For each embedding in `query_embeddings`, we format it as a nested array string for querying. The innermost lists (`q_substrs`) are converted to `ARRAY[]` format, and then combined into a single string, `q_str`. This formatted string is used in a query on `vector_search_images`, calculating the `maxsim` similarity between `q_str` and `embedding`. The query returns the top 2 results, ordered by similarity score (`score`). This loop performs similarity searches for each query embedding. + +```python +colpali_results = [] +n_res = 1 + +for el in query_embeddings: + # Convert each sublist of embeddings into a formatted TQL array string + q_substrs = [f"ARRAY[{','.join(str(x) for x in sq)}]" for sq in el] + q_str = f"ARRAY[{','.join(q_substrs)}]" + + # Construct a formatted TQL query + tql_colpali = f""" + SELECT *, maxsim(embedding, {q_str}) as score + ORDER BY maxsim(embedding, {q_str}) DESC + LIMIT {n_res} + """ + + # Execute the query and append the results + colpali_results.append(vector_search_images.query(tql_colpali)) +``` + +For each result in `view`, this code prints the `question` text and its similarity `score`. It then converts the `image` data back to an image format with `Image.fromarray(el["image"])` and displays it using `el_img.show()`. This loop visually presents each query's closest matches alongside their similarity scores. + +```python +import matplotlib.pyplot as plt + +num_columns = n_res +num_rows = len(colpali_results) + +fig, axes = plt.subplots(num_rows, num_columns, figsize=(15, 5 * num_rows)) +axes = axes.flatten() # Flatten for easier access to cells + +idx_plot = 0 +for res, query in zip(colpali_results, queries): + for el in res: + img = Image.fromarray(el["image"]) + axes[idx_plot].imshow(img) + axes[idx_plot].set_title(f"Query: {query}, Similarity: {el['score']:.4f}") + axes[idx_plot].axis('off') # Turn off axes for a cleaner look + idx_plot += 1 +for ax in axes[len(colpali_results):]: + ax.axis('off') + +plt.tight_layout() +plt.show() +``` + +Output: +![image.png](images/rag-img-9.png) + +### VQA: Visual Question Answering + +The following function, `generate_VQA`, creates a visual question-answering (VQA) system that takes an image and a question, then analyzes the image to provide an answer based on visual cues. + +1. **Convert Image to Base64** : The image (`img`) is encoded to a base64 string, allowing it to be embedded in the API request. + +2. **System Prompt** : A structured prompt instructs the model to analyze the image, focusing on visual details that can answer the question. + +3. **Payload and Headers** : The request payload includes the model (`gpt-4o-mini`), the system prompt, and the base64-encoded image. The model is expected to respond in JSON format, specifically returning an `answer` field with insights based on the image. + +4. **Send API Request** : Using `requests.post`, the function sends the payload to the OpenAI API. If successful, it parses and returns the answer; otherwise, it returns `False`. + +This approach enables an AI-powered visual analysis of images to generate contextually relevant answers. + +```python +import json + +def generate_VQA(base64_image: str, question:str): + + system_prompt = f"""You are a visual language model specialized in analyzing images. Below is an image provided by the user along with a question. Analyze the image carefully, paying attention to details relevant to the question. Construct a clear and informative answer that directly addresses the user's question, based on visual cues. + + The output must be in JSON format with the following structure: + {{ + "answer": "The answer to the question based on visual analysis." + }} + + Here is the question: {question} + """ + + response = client.chat.completions.create( + model="gpt-4o-mini", + messages = [ + { + "role": "user", + "content": [ + {"type": "text", "text": system_prompt}, + { + "type": "image_url", + "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}, + }, + ], + } + ], + response_format={"type": "json_object"}, + ) + + try: + + response = response.choices[0].message.content + response = json.loads(response) + answer = response["answer"] + return answer + except Exception as e: + print(f"Error: {e}") + return False +``` + +This code sets `question` to the first item in `queries`, converts the first image in `colpali_results` to an image format, and saves it as `"image.jpg"`. + +```python +question = queries[0] +output_image = "image.jpg" +img = Image.fromarray(colpali_results[0]["image"][0]) +img.save(output_image) +``` + +The following code opens `"image.jpg"` in binary mode, encodes it to a base64 string, and passes it with `question` to the `generate_VQA` function, which returns an answer based on the image. + +```python +import base64 + +with open(output_image, "rb") as image_file: + base64_image = base64.b64encode(image_file.read()).decode('utf-8') + +answer = generate_VQA(base64_image, question) +print(answer) +``` + +Output: +``` +'As time approaches infinity, the voltage modeled by n^6 will eventually stabilize at the equilibrium potential for potassium (EK), which is represented at approximately -90 mV on the graph.' +``` + +We've now gained a solid understanding of multi-modal data processing, advanced retrieval techniques, and hybrid search methods using state-of-the-art models like ColPali. With these skills, you're equipped to tackle complex, real-world applications that require deep insights from both text and image data. + +Keep experimenting, stay curious, and continue building innovative solutions—this is just the beginning of what's possible in the field of AI-driven search and information retrieval. + +**To learn more about Deep Lake v4, visit the [official blog post](https://www.activeloop.ai/resources/deep-lake-4-0-the-fastest-multi-modal-ai-search-on-data-lakes/) and [documentation](https://docs.deeplake.ai/latest/).** diff --git a/docs/docs/guide/v3-conversion.md b/docs/docs/guide/v3-conversion.md new file mode 100644 index 0000000000..eb92c10aa0 --- /dev/null +++ b/docs/docs/guide/v3-conversion.md @@ -0,0 +1,160 @@ +--- +seo_title: "Deep Lake 3.xx To 4.xx Migration Guide | Optimize Your ML Datasets" +description: "Step-By-Step Guide For Migrating Deep Lake 3.xx Datasets To 4.xx, Enabling Advanced Vector Search, RAG Applications, And Efficient ML Training." +--- + +# Migrating to Deep Lake v4 + +Deep Lake 4.0 introduces major improvements for ML and AI applications: + +- Enhanced vector similarity search performance +- Optimized embedding storage and retrieval +- Improved support for large-scale RAG applications +- Better handling of multi-modal ML data +- Advanced data versioning for ML experiments + +This guide walks through migrating your v3 datasets to take advantage of these capabilities. + +## Working with v3 Datasets + +### Read-Only Access + +While v3 datasets cannot be modified in v4, you can still access them in read-only mode using `deeplake.query()`: + +```python +# Query v3 dataset directly +results = deeplake.query('SELECT * FROM "al://org_name/v3_dataset"') + +# Use in ML training pipeline +train_data = results.pytorch() +val_data = results.tensorflow() + +# Vector similarity search still works +similar = deeplake.query(""" + SELECT * FROM "al://org_name/v3_dataset" + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[...]) DESC + LIMIT 100 +""") +``` + +This allows you to continue using existing v3 datasets while gradually migrating to v4. + +## Migration Options + +### Option 1: Automatic Migration (Recommended) +Use the built-in conversion tool to automatically migrate your dataset: + + + +```python +deeplake.convert( + src='al://org_name/v3_dataset', + dst='al://org_name/v4_dataset' +) +``` + +### Option 2: Manual Migration +For custom schemas or complex ML datasets: + + + +```python +# 1. Create v4 dataset with desired schema +ds = deeplake.create("s3://new/dataset") +ds.add_column("embeddings", deeplake.types.Embedding(768)) +ds.add_column("images", deeplake.types.Image()) +ds.commit() + +# 2. Load v3 data through query +source = deeplake.query(f'SELECT * FROM "{old_ds_path}"') + +# 3. Migrate in batches with progress tracking +for i in range(0, len(source), batch_size): + batch = source[i:i+batch_size] + ds.append(batch) + if i % 10000 == 0: + ds.commit() +``` + +## Validating Your Migration + +After migration, verify your ML workflows: + +1. Check vector search functionality: +```python +# Verify similarity search +array_str = ','.join(str(x) for x in search_vector) +results = ds.query(f""" + SELECT * + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[{array_str}]) + LIMIT 10 +""") +``` + +2. Validate ML training pipelines: +```python +# Test PyTorch/TensorFlow integration +train_loader = ds.pytorch(transform=transforms) +``` + +3. Verify data integrity: +```python +# Compare dataset statistics +assert len(old_ds) == len(new_ds) +``` + +## Troubleshooting + +Common issues during migration: + +- **Memory Issues**: For large datasets, use smaller batch sizes during migration +- **Schema Mismatches**: Verify column types match between v3 and v4 datasets +- **Missing Embeddings**: Ensure embedding dimensions are correctly specified +- **Training Issues**: Update data loading code to use new v4 API + +Need help? Join our [Slack Community](https://slack.activeloop.ai) diff --git a/docs/docs/guide/vectorstore.md b/docs/docs/guide/vectorstore.md new file mode 100644 index 0000000000..027614f015 --- /dev/null +++ b/docs/docs/guide/vectorstore.md @@ -0,0 +1,283 @@ +--- +seo_title: "Deep Lake RAG | RAG Application using Deeplake Multimodal vector search" +description: "Up to 10x More Efficient Data Retrieval with TQL, learn how to build RAG applications with deeplake." +--- + +# Using Deep Lake as a Vector Store in LangChain + +## How to Use Deep Lake as a Vector Store in LangChain +Deep Lake can be used as a VectorStore in [LangChain](https://github.com/langchain-ai/langchain) for building Apps that require filtering and vector search. In this tutorial we will show how to create a Deep Lake Vector Store in LangChain and use it to build a Q&A App about the [Twitter OSS recommendation algorithm](https://github.com/twitter/the-algorithm). This tutorial requires installation of: + +Install the main libraries: + +```bash +pip install --upgrade --quiet langchain-openai langchain-deeplake tiktoken +``` +## Downloading and Preprocessing the Data +First, let's import necessary packages and make sure the Activeloop and OpenAI keys are in the environmental variables `ACTIVELOOP_TOKEN`, `OPENAI_API_KEY`. + + + + +```python +import os +import getpass +from langchain_openai import OpenAIEmbeddings +from langchain_deeplake.vectorstores import DeeplakeVectorStore +from langchain_community.document_loaders import TextLoader +from langchain_text_splitters import CharacterTextSplitter +from langchain.chains import RetrievalQA +from langchain_openai import ChatOpenAI +``` + +Next, we set up environmental variables +```python +if "OPENAI_API_KEY" not in os.environ: + os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") + +if "ACTIVELOOP_TOKEN" not in os.environ: + os.environ["ACTIVELOOP_TOKEN"] = getpass.getpass("activeloop token:") +``` + +Next, let's clone the Twitter OSS recommendation algorithm: + +```bash +!git clone https://github.com/twitter/the-algorithm +``` + +Next, let's load all the files from the repo into a list: + + +```python +repo_path = '/the-algorithm' + +docs = [] +for dirpath, dirnames, filenames in os.walk(repo_path): + for file in filenames: + try: + loader = TextLoader(os.path.join(dirpath, file), encoding='utf-8') + docs.extend(loader.load_and_split()) + except Exception as e: + print(e) + pass +``` + +## A note on chunking text files + +Text files are typically split into chunks before creating embeddings. In general, more chunks increases the relevancy of data that is fed into the language model, since granular data can be selected with higher precision. However, since an embedding will be created for each chunk, more chunks increase the computational complexity. + +```python +text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) +texts = text_splitter.split_documents(docs) +``` + +## Creating the Deep Lake Vector Store + +First, we specify a path for storing the Deep Lake dataset containing the embeddings and their metadata. + +```python +dataset_path = 'al:///twitter_algorithm' +``` + +Next, we specify an OpenAI algorithm for creating the embeddings, and create the VectorStore. This process creates an embedding for each element in the texts lists and stores it in Deep Lake format at the specified path. + +```python +embeddings = OpenAIEmbeddings() +``` + + +```python +db = DeeplakeVectorStore.from_documents(dataset_path=dataset_path, embedding=embeddings, documents=texts, overwrite=True) +``` + +The Deep Lake Vector Store has 4 columns including the `texts`, `embeddings`, `ids`, and `metadata`. + +```python +ds.dataset.summary() +``` + +```bash +Dataset length: 31305 +Columns: + documents : text + embeddings: embedding(1536, clustered) + ids : text + metadata : dict +``` + +## Use the Vector Store in a Q&A App + +We can now use the VectorStore in Q&A app, where the embeddings will be used to filter relevant documents (`texts`) that are fed into an LLM in order to answer a question. + +If we were on another machine, we would load the existing Vector Store without recalculating the embeddings: + +```python +db = DeeplakeVectorStore(dataset_path=dataset_path, read_only=True, embedding_function=embeddings) + +``` + +We have to create a `retriever` object and specify the search parameters. + +```python +retriever = db.as_retriever() +retriever.search_kwargs['distance_metric'] = 'cos' +retriever.search_kwargs['k'] = 20 +``` + +Finally, let's create an `RetrievalQA` chain in LangChain and run it: + +```python +model = ChatOpenAI(model='gpt-3.5-turbo') +qa = RetrievalQA.from_llm(model, retriever=retriever) +``` + +```python +qa.run('What programming language is most of the SimClusters written in?') +``` + +This returns: +``` +Most of the SimClusters code is written in Scala as indicated by the packages such as `com.twitter.simclustersann.modules`, `com.twitter.simclusters_v2.scio.common`, `com.twitter.simclusters_v2.summingbird.storm`, and references to Scala-based GCP jobs. +``` + + +## Accessing the Low Level Deep Lake API (Advanced) +When using a Deep Lake Vector Store in LangChain, the underlying Vector Store and its low-level Deep Lake dataset can be accessed via: + +```python +# LangChain Vector Store +db = DeeplakeVectorStore(dataset_path=dataset_path) + +# Deep Lake Dataset object +ds = db.dataset +``` + +## SelfQueryRetriever with Deep Lake + +Deep Lake supports the [SelfQueryRetriever](https://api.python.langchain.com/en/latest/retrievers/langchain.retrievers.self_query.base.SelfQueryRetriever.html) implementation in LangChain, which translates a user prompt into a metadata filters. + + +>This section of the tutorial requires installation of additional packages: +> `pip install deeplake lark` + +First let's create a Deep Lake Vector Store with relevant data using the documents below. + +```python +from langchain_core.documents import Document + +docs = [ + Document( + page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose", + metadata={"year": 1993, "rating": 7.7, "genre": "science fiction"}, + ), + Document( + page_content="Leo DiCaprio gets lost in a dream within a dream within a dream within a ...", + metadata={"year": 2010, "director": "Christopher Nolan", "rating": 8.2}, + ), + Document( + page_content="A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea", + metadata={"year": 2006, "director": "Satoshi Kon", "rating": 8.6}, + ), + Document( + page_content="A bunch of normal-sized women are supremely wholesome and some men pine after them", + metadata={"year": 2019, "director": "Greta Gerwig", "rating": 8.3}, + ), + Document( + page_content="Toys come alive and have a blast doing so", + metadata={"year": 1995, "genre": "animated"}, + ), + Document( + page_content="Three men walk into the Zone, three men walk out of the Zone", + metadata={ + "year": 1979, + "rating": 9.9, + "director": "Andrei Tarkovsky", + "genre": "science fiction", + "rating": 9.9, + }, + ), +] +``` + +Since this feature uses Deep Lake's [Tensor Query Language](https://docs.deeplake.ai/latest/advanced/tql/) under the hood, the Vector Store must be stored in or connected to Deep Lake, which requires [registration with Activeloop](https://app.activeloop.ai/levongh/home): + +```python +org_id = +dataset_path = f"al://{org_id}/self_query" + +vectorstore = DeeplakeVectorStore.from_documents( + docs, embeddings, dataset_path = dataset_path, overwrite = True, +) +``` + +Next, let's instantiate our retriever by providing information about the metadata fields that our documents support and a short description of the document contents. + +```python +from langchain.llms import OpenAI +from langchain.retrievers.self_query.base import SelfQueryRetriever +from langchain.chains.query_constructor.base import AttributeInfo + +metadata_field_info = [ + AttributeInfo( + name="genre", + description="The genre of the movie", + type="string or list[string]", + ), + AttributeInfo( + name="year", + description="The year the movie was released", + type="integer", + ), + AttributeInfo( + name="director", + description="The name of the movie director", + type="string", + ), + AttributeInfo( + name="rating", description="A 1-10 rating for the movie", type="float" + ), +] + +document_content_description = "Brief summary of a movie" +llm = OpenAI(temperature=0) + +retriever = SelfQueryRetriever.from_llm( + llm, vectorstore, document_content_description, metadata_field_info, verbose=True +) +``` + +And now we can try actually using our retriever! + +```python +# This example only specifies a relevant query +retriever.get_relevant_documents("What are some movies about dinosaurs") +``` + +Output: +Output: + +``` +[Document(metadata={'genre': 'science fiction', 'rating': array(7.7), 'year': array(1993)}, page_content='A bunch of scientists bring back dinosaurs and mayhem breaks loose'), + Document(metadata={'genre': 'science fiction', 'rating': array(7.7), 'year': array(1993)}, page_content='A bunch of scientists bring back dinosaurs and mayhem breaks loose'), + Document(metadata={'genre': 'animated', 'year': array(1995)}, page_content='Toys come alive and have a blast doing so'), + Document(metadata={'genre': 'animated', 'year': array(1995)}, page_content='Toys come alive and have a blast doing so')] +``` + +Now we can run a query to find movies that are above a certain ranking: + +```python +# This example only specifies a filter +retriever.get_relevant_documents("I want to watch a movie rated higher than 8.5") +``` + +Output: +``` +[Document(metadata={'director': 'Satoshi Kon', 'rating': array(8.6), 'year': array(2006)}, page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea'), + Document(metadata={'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'rating': array(9.9), 'year': array(1979)}, page_content='Three men walk into the Zone, three men walk out of the Zone'), + Document(metadata={'director': 'Satoshi Kon', 'rating': array(8.6), 'year': array(2006)}, page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea'), + Document(metadata={'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'rating': array(9.9), 'year': array(1979)}, page_content='Three men walk into the Zone, three men walk out of the Zone'), Document(metadata={'director': 'Satoshi Kon', 'rating': array(8.6), 'year': array(2006)}, page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea'), Document(metadata={'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'rating': array(9.9), 'year': array(1979)}, page_content='Three men walk into the Zone, three men walk out of the Zone')] +``` + + +Congrats! You just used the Deep Lake Vector Store in LangChain to create a Q&A App! 🎉 + diff --git a/docs/docs/images/Two_Way_Utility.png b/docs/docs/images/Two_Way_Utility.png new file mode 100644 index 0000000000..f989f59519 Binary files /dev/null and b/docs/docs/images/Two_Way_Utility.png differ diff --git a/docs/docs/images/deeplake.favicon.png b/docs/docs/images/deeplake.favicon.png new file mode 100644 index 0000000000..cbef6be801 Binary files /dev/null and b/docs/docs/images/deeplake.favicon.png differ diff --git a/docs/docs/images/deeplake.logo.svg b/docs/docs/images/deeplake.logo.svg new file mode 100644 index 0000000000..3f0b79323c --- /dev/null +++ b/docs/docs/images/deeplake.logo.svg @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/docs/images/forward.svg b/docs/docs/images/forward.svg new file mode 100644 index 0000000000..232444dfcf --- /dev/null +++ b/docs/docs/images/forward.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/docs/docs/images/github.svg b/docs/docs/images/github.svg new file mode 100644 index 0000000000..7fa5d69a9e --- /dev/null +++ b/docs/docs/images/github.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/docs/docs/images/slack.svg b/docs/docs/images/slack.svg new file mode 100644 index 0000000000..69a4eb6a21 --- /dev/null +++ b/docs/docs/images/slack.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/docs/docs/index.md b/docs/docs/index.md new file mode 100644 index 0000000000..9d782922ff --- /dev/null +++ b/docs/docs/index.md @@ -0,0 +1,153 @@ +--- +seo_title: "Activeloop Deep Lake Docs | Multi-Modal AI Search | API Reference & Guides" +description: "Access Deep Lake Documentation For Complete Setup, API Reference, Guides On Efficient Multi-Modal AI Search, Dataset Management, Cost-Efficient Training, And Retrieval-Augmented Generation." +hide: + - toc +--- + +# 🌊 Deep Lake: Multi-Modal AI Database + +Deep Lake is a database specifically designed for machine learning and AI applications, offering efficient data management, vector search capabilities, and seamless integration with popular ML frameworks. + +## Key Features + +### 🔍 Vector Search & Semantic Operations +- High-performance similarity search for embeddings +- BM25-based semantic text search +- Support for building RAG applications +- Efficient indexing strategies for large-scale search + +### 🚀 Optimized for Machine Learning +- Native integration with PyTorch and TensorFlow +- Efficient batch processing for training +- Built-in support for common ML data types (images, embeddings, tensors) +- Automatic data streaming with smart caching + +### ☁️ Cloud-Native Architecture +- Native support for major cloud providers: + - Amazon S3 + - Google Cloud Storage + - Azure Blob Storage +- Cost-efficient data management +- Data versioning and lineage tracking + +## Quick Installation + +```bash +pip install deeplake +``` + +## Basic Usage + + + +```python +import deeplake + +# Create a dataset +ds = deeplake.create("s3://my-bucket/dataset") # or local path + +# Add data columns +ds.add_column("images", deeplake.types.Image()) +ds.add_column("embeddings", deeplake.types.Embedding(768)) +ds.add_column("labels", deeplake.types.Text()) + +# Add data +ds.append([{ + "images": image_array, + "embeddings": embedding_vector, + "labels": "cat" +}]) + +# Vector similarity search +text_vector = ','.join(str(x) for x in search_vector) +results = ds.query(f""" + SELECT * + ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[{text_vector}]) DESC + LIMIT 100 +""") +``` + +## Common Use Cases + +### Deep Learning Training +```python +# PyTorch integration +from torch.utils.data import DataLoader + +loader = DataLoader(ds.pytorch(), batch_size=32, shuffle=True) +for batch in loader: + images = batch["images"] + labels = batch["labels"] + # training code... +``` + +### RAG Applications +```python +ds = deeplake.create("s3://my-bucket/dataset") # or local path +# Store text and embeddings +ds.add_column("text", deeplake.types.Text(index_type=deeplake.types.BM25)) +ds.add_column("embeddings", deeplake.types.Embedding(1536)) + +# Semantic search +results = ds.query(""" + SELECT text + ORDER BY BM25_SIMILARITY(text, 'machine learning') DESC + LIMIT 10 +""") +``` + +### Computer Vision +```python +# Store images and annotations +ds = deeplake.create("s3://my-bucket/dataset") # or local path +ds.add_column("images", deeplake.types.Image(sample_compression="jpeg")) +ds.add_column("boxes", deeplake.types.BoundingBox()) +ds.add_column("masks", deeplake.types.SegmentMask(sample_compression='lz4')) + +# Add data +ds.append({ + "images": imgs, + "boxes": bboxes, + "masks": smasks +}) +``` + +## Next Steps + +- Check out our [Quickstart Guide](getting-started/quickstart) for detailed setup +- Explore [RAG Applications](guide/rag) +- See [Deep Learning Integration](guide/deep-learning/deep-learning) + +## Resources + +- [GitHub Repository](https://github.com/activeloopai/deeplake) +- [API Reference](api/) +- [Community Support](https://slack.activeloop.ai) + +## Why Deep Lake? + +- **Performance**: Optimized for ML workloads with efficient data streaming +- **Scalability**: Handle billions of samples directly from the cloud +- **Flexibility**: Support for all major ML frameworks and cloud providers +- **Cost-Efficiency**: Smart storage management and compression +- **Developer Experience**: Simple, intuitive API with comprehensive features diff --git a/docs/docs/js/custom.js b/docs/docs/js/custom.js new file mode 100644 index 0000000000..c5668f14dd --- /dev/null +++ b/docs/docs/js/custom.js @@ -0,0 +1,42 @@ +document.addEventListener("DOMContentLoaded", function () { + let slackButton = document.createElement("nav"); + slackButton.classList.add("md-header-nav__button", "md-button"); + + // Create the element + let slackLink = document.createElement("a"); + slackLink.href = + "https://slack.activeloop.ai"; + slackLink.target = "_blank"; // Open in a new tab + slackLink.title = "Join us on Slack"; + + let slackIcon = document.createElement("img"); + slackIcon.src = "/images/slack.svg"; + slackIcon.alt = "Slack"; + slackButton.setAttribute("target", "_blank"); + slackIcon.style.height = "20px"; + slackIcon.style.height = "20px"; + slackIcon.style.width = "20px"; + slackIcon.style.marginRight = "8px"; + + slackLink.appendChild(slackIcon); + + slackButton.appendChild(slackLink); + + let navBar = document.querySelector(".md-header__source"); + if (navBar) { + navBar.parentNode.insertBefore(slackButton, navBar.nextSibling); + } + + let nextSteps = document.getElementById("next-steps"); + if (nextSteps) { + let nextStepsIcon = document.createElement("img"); + nextStepsIcon.src = "/images/forward.svg"; + nextStepsIcon.alt = "Next Steps Icon"; + nextStepsIcon.style.height = "20px"; + nextStepsIcon.style.width = "20px"; + nextStepsIcon.style.marginLeft = "8px"; + + // Append the icon to the #next-steps div + nextSteps.appendChild(nextStepsIcon); + } +}); diff --git a/docs/docs/llms.txt b/docs/docs/llms.txt new file mode 100644 index 0000000000..885ff5e876 --- /dev/null +++ b/docs/docs/llms.txt @@ -0,0 +1,197 @@ +# Deep Lake + +> Deep Lake is a multi-modal AI database with TQL (Tensor Query Language) for vector similarity search, text search, and complex data operations across cloud storage. It provides native support for embeddings, images, text, and other AI data types with efficient indexing and cross-cloud querying capabilities. + +## Dataset API + +**Creation & Access:** +```python +ds = deeplake.create("s3://bucket/path") # Create new dataset +ds = deeplake.open("s3://bucket/path") # Read-write access +ds = deeplake.open_read_only("path") # Read-only access +ds = deeplake.like(source_ds, "new/path") # Copy schema +ds = deeplake.from_parquet("file.parquet", "path") # Import from Parquet +``` + +**Dataset Operations:** +```python +ds.add_column(name, type) # Add new column +ds.append(data) # Add data rows +ds.extend(other_dataset) # Merge datasets +ds.delete() # Delete dataset +ds.summary() # Dataset info +ds.pytorch() # PyTorch integration +ds.tensorflow() # TensorFlow integration +``` + +## Column API + +**Column Access:** +```python +column = ds["column_name"] # Get column +column[0:100] # Slice data +column.metadata # Column metadata +column.name # Column name +``` + +**Indexing:** +```python +column.create_index("embedding") # Vector index +column.create_index("inverted") # Text search index +column.create_index("btree") # Numeric index +``` + +## Data Types + +**Basic Types:** `"int32"`, `"float32"`, `"float64"`, `"bool"`, `"text"` + +**AI-Optimized Types:** +```python +types.Image(sample_compression="jpeg") # Images with compression +types.Embedding(dim, index_type="embedding") # Vector embeddings +types.Text(index_type="inverted") # Text with search index +types.Audio(sample_compression="mp3") # Audio files +types.Video(sample_compression="mp4") # Video files +types.Medical(compression="dcm") # Medical imaging (DICOM, NIfTI) +types.Mesh() # 3D meshes (STL, PLY formats) +types.BoundingBox() # Object detection boxes +types.SegmentMask(sample_compression="lz4") # Segmentation masks +types.ClassLabel(names=["cat", "dog"]) # Classification labels +types.Array(dtype, shape) # Custom arrays +``` + +**Index Types:** +- `"embedding"`: Vector similarity search (cosine, L2, etc.) +- `"inverted"`: Text keyword search +- `"btree"`: Numeric range queries +- `"hash"`: Exact value lookups + +## TQL (Tensor Query Language) + +**Basic Syntax:** +```sql +-- Single dataset query (no FROM needed) +SELECT * WHERE id > 10 + +-- Cross-dataset query (FROM required) +SELECT * FROM "s3://bucket/dataset" WHERE condition +``` + +**Vector Similarity Search:** +```sql +-- Cosine similarity (higher = more similar) +SELECT * ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[0.1,0.2,0.3]) DESC LIMIT 100 + +-- L2/Euclidean distance (lower = more similar) +SELECT * ORDER BY L2_NORM(embeddings - ARRAY[0.1,0.2,0.3]) ASC LIMIT 100 + +-- L1/Manhattan distance +SELECT * ORDER BY L1_NORM(embeddings - ARRAY[0.1,0.2,0.3]) ASC LIMIT 100 + +-- Inner product similarity +SELECT * ORDER BY INNER_PRODUCT(embeddings, ARRAY[0.1,0.2,0.3]) DESC LIMIT 100 +``` + +**Text Search:** +```sql +-- BM25 semantic search +SELECT * ORDER BY BM25_SIMILARITY(text, 'search query') DESC LIMIT 10 + +-- Keyword search (requires inverted index) +SELECT * WHERE CONTAINS(text, 'keyword') + +-- Full text search +SELECT * WHERE text LIKE '%pattern%' +``` + +**Array Operations:** +```sql +-- Array slicing +SELECT features[:, 0:10] FROM dataset + +-- Array filtering +SELECT * WHERE features[0] > 0.5 + +-- Array aggregation +SELECT AVG(features, axis=0) FROM dataset +``` + +**Cross-Cloud Joins:** +```sql +-- Join datasets across cloud providers +SELECT i.image, e.embedding, m.metadata +FROM "s3://bucket1/images" AS i +JOIN "gcs://bucket2/embeddings" AS e ON i.id = e.image_id +JOIN "azure://container/meta" AS m ON i.id = m.image_id +WHERE m.verified = true +ORDER BY COSINE_SIMILARITY(e.embedding, ARRAY[0.1,0.2,0.3]) DESC +``` + +**Complex Filtering:** +```sql +-- Combine filters with vector search +SELECT * FROM dataset +WHERE label IN ('cat', 'dog') AND confidence > 0.9 +ORDER BY COSINE_SIMILARITY(embeddings, ARRAY[0.1,0.2,0.3]) DESC +LIMIT 100 +``` + +**Aggregations:** +```sql +-- Statistical functions +SELECT COUNT(*), AVG(confidence), MAX(score) FROM dataset +GROUP BY label + +-- Array statistics +SELECT AVG(embeddings, axis=0), STD(embeddings, axis=1) FROM dataset +``` + +## Query Execution + +**Synchronous:** +```python +results = deeplake.query("SELECT * FROM dataset WHERE condition") +results = ds.query("SELECT * WHERE condition") # On dataset instance +``` + +**Asynchronous:** +```python +future = deeplake.query_async("SELECT * FROM dataset WHERE condition") +results = future.result() # Get results when ready +is_done = future.is_completed() # Check completion status +``` + +**Query Results:** +```python +# Iterate through results +for item in results: + image = item["images"] + label = item["labels"] + +# Direct column access (faster) +images = results["images"][0:100] +labels = results["labels"][:] + +# Chain queries on views +view = ds.query("SELECT * WHERE category = 'animals'") +cats = view.query("SELECT * WHERE species = 'cat'") +``` + +## Key Features + +- **Multi-cloud Storage**: Seamless querying across S3, GCS, Azure +- **Vector Database**: Built-in similarity search with multiple distance metrics +- **Text Search**: BM25 semantic search and keyword matching +- **ML Integration**: Native PyTorch/TensorFlow data loaders +- **Compression**: Automatic compression for images, video, audio +- **Versioning**: Dataset branching, tagging, and version control +- **Streaming**: Efficient data streaming for large datasets +- **Visualization**: 3D data visualization capabilities + +## Documentation + +- [Dataset API](https://docs.deeplake.ai/api/dataset/): Complete dataset operations +- [Column API](https://docs.deeplake.ai/api/column/): Column management and indexing +- [Query API](https://docs.deeplake.ai/api/query/): TQL syntax and examples +- [Data Types](https://docs.deeplake.ai/api/types/): All supported data types +- [TQL Reference](https://docs.deeplake.ai/advanced/tql/): Complete TQL syntax guide \ No newline at end of file diff --git a/docs/docs/resources/.pages b/docs/docs/resources/.pages new file mode 100644 index 0000000000..e0e14118c0 --- /dev/null +++ b/docs/docs/resources/.pages @@ -0,0 +1,3 @@ +nav: + - Academic Paper: academic-paper.md + - White Paper: whitepaper.md diff --git a/docs/docs/resources/academic-paper.md b/docs/docs/resources/academic-paper.md new file mode 100644 index 0000000000..fb202bf51a --- /dev/null +++ b/docs/docs/resources/academic-paper.md @@ -0,0 +1,34 @@ +--- +seo_title: "Deep Lake Academic Paper - Lakehouse for Deep Learning Research" +description: "Read the Deep Lake academic paper published on arXiv covering lakehouse architecture for deep learning, tensor storage, streaming for PyTorch/TensorFlow/JAX, and MLOps integration for NLP, computer vision, and audio processing." +--- +# Academic Paper + +## Deep Lake: a Lakehouse for Deep Learning + +**Authors:** Activeloop Research Team + +**Published:** arXiv:2209.10785 (2022) + +### Abstract + +Traditional data lakes provide critical data infrastructure for analytical workloads by enabling time travel, running SQL queries, ingesting data with ACID transactions, and visualizing petabyte-scale datasets on cloud storage. They allow organizations to break down data silos, unlock data-driven decision-making, improve operational efficiency, and reduce costs. However, as deep learning usage increases, traditional data lakes are not well-designed for applications such as natural language processing (NLP), audio processing, computer vision, and applications involving non-tabular datasets. + +This paper presents Deep Lake, an open-source lakehouse for deep learning applications developed at Activeloop. Deep Lake maintains the benefits of a vanilla data lake with one key difference: it stores complex data, such as images, videos, annotations, as well as tabular data, in the form of tensors and rapidly streams the data over the network to (a) Tensor Query Language, (b) in-browser visualization engine, or (c) deep learning frameworks without sacrificing GPU utilization. Datasets stored in Deep Lake can be accessed from PyTorch, TensorFlow, JAX, and integrate with numerous MLOps tools. + +### Access the Paper + +[View on arXiv](https://arxiv.org/pdf/2209.10785){ .md-button .md-button--primary } + +### Citation + +If you use Deep Lake in your research, please cite: + +```bibtex +@article{deeplake2022, + title={Deep Lake: a Lakehouse for Deep Learning}, + author={Activeloop}, + journal={arXiv preprint arXiv:2209.10785}, + year={2022} +} +``` diff --git a/docs/docs/resources/whitepaper.md b/docs/docs/resources/whitepaper.md new file mode 100644 index 0000000000..1698ad523f --- /dev/null +++ b/docs/docs/resources/whitepaper.md @@ -0,0 +1,25 @@ +--- +seo_title: "Deep Lake White Paper - Database for AI Architecture & Design" +description: "Download the Deep Lake white paper covering database architecture, storage strategies, query optimization for ML workloads, ML framework integration, and performance benchmarks for AI applications." +--- +# White Paper + +## Deep Lake: Database for AI + +**Published by:** Activeloop + +### Overview + +The Deep Lake whitepaper provides a comprehensive overview of the Deep Lake database architecture, its core features, and use cases for AI and machine learning workflows. + +### Key Topics + +- Deep Lake architecture and design principles +- Storage and data management strategies +- Query optimization for ML workloads +- Integration with popular ML frameworks +- Performance benchmarks and case studies + +### Access the White Paper + +[Download White Paper (PDF)](https://www.deeplake.ai/whitepaper){ .md-button .md-button--primary } diff --git a/docs/hooks/custom_hooks.py b/docs/hooks/custom_hooks.py new file mode 100644 index 0000000000..e43de5d5b6 --- /dev/null +++ b/docs/hooks/custom_hooks.py @@ -0,0 +1,67 @@ +import logging +import shutil +import subprocess +import mkdocs.plugins as plugins +import yaml +import os + +log = logging.getLogger("mkdocs") + + +def on_startup(command, dirty, **kwargs): + if True: # JS is disabled for now + return + log.info("Generating JS API...") + shutil.rmtree("docs/api-js", ignore_errors=True) + res = subprocess.run( + [ + "typedoc", + "--tsconfig", + "/docs/deeplake.tsconfig.json", + "--treatWarningsAsErrors", + ] + ) + if res.returncode != 0: + log.error("Failed to generate JS API") + exit(1) + + log.info("Generating JS API...DONE") + +def extract_first_title(markdown): + # Find first # heading + lines = markdown.split('\n') + for line in lines: + if line.strip().startswith('# '): + return line.strip('# ').strip() + return None + +@plugins.event_priority(100) # This hook should run before the social plugin +def on_page_markdown(markdown, page, config, files): + meta_file = page.file.src_path.rsplit('.', 1)[0] + '.meta.yaml' + + # For files like notebooks which don't support metadata in markdown, look for a .meta.yaml file to define it + if meta_file in files: + log.info(f"Using metadata from {meta_file} for {page.file.src_path}") + with open(page.file.src_dir + "/" + meta_file, 'r') as f: + meta_data = yaml.safe_load(f) + if not meta_data: + raise Exception(f"Invalid metadata file {meta_file}") + + if "description" in meta_data: + page.meta['description'] = meta_data.get('description') + if "title" in meta_data: + page.meta['title'] = meta_data.get('title') + elif 'title' not in page.meta or page.meta['title'] == None: + page.meta['title'] = extract_first_title(markdown) + return markdown + +def on_post_build(config): + """Copy llms.txt to the root of the site after build""" + src_path = os.path.join(config['docs_dir'], 'llms.txt') + dest_path = os.path.join(config['site_dir'], 'llms.txt') + + if os.path.exists(src_path): + log.info(f"Copying llms.txt from {src_path} to {dest_path}") + shutil.copy2(src_path, dest_path) + else: + log.warning(f"llms.txt not found at {src_path}") diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml new file mode 100644 index 0000000000..106bbb1f33 --- /dev/null +++ b/docs/mkdocs.yml @@ -0,0 +1,136 @@ +site_name: "" +site_url: https://docs.deeplake.ai/ +repo_url: https://github.com/activeloopai/deeplake +repo_name: "" +theme: + icon: + repo: fontawesome/brands/github + name: material + custom_dir: overrides + logo: images/deeplake.logo.svg + favicon: /images/deeplake.favicon.png + static_templates: + - 404.html + features: + - navigation.top + # - navigation.tabs + # - navigation.tabs.sticky + - navigation.sections + - navigation.path + # - navigation.expand + # - content.action.edit + # - content.action.view + - content.code.copy + - palette.toggle + # - header.autohide # Uncomment to enable autohiding header + palette: + - media: "(prefers-color-scheme: light)" + scheme: default + primary: "white" + background: "white" + toggle: + icon: material/weather-sunny + name: Switch to dark mode + + - media: "(prefers-color-scheme: dark)" + scheme: slate + toggle: + icon: material/weather-night + name: Switch to light mode + +hooks: + - hooks/custom_hooks.py +extra: + version: + provider: mike + default: latest + alias: true + generator: false + analytics: + provider: google + property: G-S41VYMMQEB + +markdown_extensions: + - admonition + - attr_list + - pymdownx.details + - pymdownx.emoji: + emoji_index: !!python/name:material.extensions.emoji.twemoji + emoji_generator: !!python/name:material.extensions.emoji.to_svg + - pymdownx.highlight: + anchor_linenums: true + line_spans: __span + pygments_lang_class: true + - pymdownx.inlinehilite + - pymdownx.snippets + - pymdownx.superfences: + custom_fences: + - name: mermaid + class: mermaid + format: !!python/name:pymdownx.superfences.fence_code_format + - pymdownx.tabbed: + alternate_style: true + - tables + - toc: + permalink: true + toc_depth: 4 + +plugins: + - awesome-pages + - glightbox + - al-social: + cards_layout: default + cards_layout_options: + background_color: "#30323aef" + color: white + font_family: Noto Sans TC + + - mkdocs-jupyter: + include_source: True + ignore_h1_titles: True + - mkdocstrings: + default_handler: python + # custom_templates: templates + handlers: + python: + paths: [/source/python] + options: + allow_inspection: True + show_if_no_docstring: True + line_length: 60 + show_symbol_type_heading: True + show_source: False + show_if_no_docstring: True + show_signature_annotations: True + show_root_heading: True + show_root_full_path: True + group_by_category: False + show_category_heading: False + inherited_members: True + separate_signature: True + signature_crossrefs: True + filters: + - "!^_" + - "^__" + - "!^__repr__" + - "!^__str__" + - "!^__eq__" + - "!^__ne__" + - "!^__all__" + - "!^__getstate__" + - "!^__setstate__" + - redirects: + redirect_maps: + - search + +extra_css: + - css/custom.css + - https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css + - https://fonts.googleapis.com/icon?family=Material+Icons + +extra_javascript: + - js/custom.js + + +watch: + - /source diff --git a/docs/overrides/404.html b/docs/overrides/404.html new file mode 100644 index 0000000000..b71134ce09 --- /dev/null +++ b/docs/overrides/404.html @@ -0,0 +1,84 @@ +{% extends "main.html" %} + + + +{% block container %} + + + +
+

Oops! Page Not Found

+

+ It looks like the page you're looking for doesn't exist.
+ Let's head back to the home page and start fresh. +

+ + If you are looking for the Deep Lake 3.x docs, they have moved to
https://docs.deeplake.ai/3.9/ + + +
+ +{% endblock %} diff --git a/docs/overrides/main.html b/docs/overrides/main.html new file mode 100644 index 0000000000..74c4247260 --- /dev/null +++ b/docs/overrides/main.html @@ -0,0 +1,64 @@ +{% extends "base.html" %} +{% block htmltitle %} +{% if page.meta and page.meta.title %} +{{ page.meta.title }} - Deep Lake +{% elif page.title and not page.is_homepage %} +{{ page.title | striptags }} - Deep Lake +{% else %} +Deep Lake +{% endif %} + +{% if page.meta and page.meta.seo_title %} + +{% endif %} + +{% endblock %} + +{% block extrahead %} +{% if page.meta and page.meta.seo_title %} + + + + + + + + + + + +{% endif %} +{% endblock %} + +{% block outdated %} +You're not viewing the latest +version. + + Click here to go to latest. + +{% endblock %} + +{% block content %} + +{{ super() }} + + +{% if page.nb_url %} + + +{% endif %} + +{% endblock content %} diff --git a/docs/overrides/partials/toc.html b/docs/overrides/partials/toc.html new file mode 100644 index 0000000000..492f8de5fc --- /dev/null +++ b/docs/overrides/partials/toc.html @@ -0,0 +1,40 @@ + +{% set title = lang.t("toc") %} {% if config.mdx_configs.toc and +config.mdx_configs.toc.title %} {% set title = config.mdx_configs.toc.title %} +{% endif %} + + + diff --git a/docs/plugins/setup.py b/docs/plugins/setup.py new file mode 100644 index 0000000000..468d94c1a3 --- /dev/null +++ b/docs/plugins/setup.py @@ -0,0 +1,12 @@ +from setuptools import find_packages, setup + +setup( + name='al-social', + packages=find_packages(), + include_package_data=True, + entry_points={ + 'mkdocs.plugins': [ + 'al-social = social.plugin:SocialPlugin' + ] + } +) diff --git a/docs/plugins/social/__init__.py b/docs/plugins/social/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docs/plugins/social/config.py b/docs/plugins/social/config.py new file mode 100644 index 0000000000..fcdc64090b --- /dev/null +++ b/docs/plugins/social/config.py @@ -0,0 +1,5 @@ +from mkdocs.config.base import Config +from mkdocs.config.config_options import Deprecated, Type + +class SocConfig(Config): + enabled = Type(bool, default = True) \ No newline at end of file diff --git a/docs/plugins/social/plugin.py b/docs/plugins/social/plugin.py new file mode 100644 index 0000000000..43527be941 --- /dev/null +++ b/docs/plugins/social/plugin.py @@ -0,0 +1,47 @@ +from material.plugins.social.plugin import SocialPlugin as BasePlugin +import emoji +from io import BytesIO +from PIL import Image + +try: + from cairosvg import svg2png +except ImportError as e: + import_errors.add(repr(e)) +except OSError as e: + cairosvg_error = str(e) + + +# Original source: +# https://github.com/squidfunk/mkdocs-material/blob/master/src/plugins/social/plugin.py +class SocialPlugin(BasePlugin): + + ## Overriding to control the description size + def _render_card(self, site_name, title, description): + # Render background and logo + image = self._render_card_background((1200, 630), self.color["fill"]) + logo = self._resized_logo_promise.result() + logo = logo.resize((logo.width * 2, logo.height * 2), Image.Resampling.LANCZOS) + image.alpha_composite( + logo, + (1200 - 324, 64) + ) + + # Render page title + font = self._get_font("Bold", 92) + image.alpha_composite( + self._render_text((800, 328), font, title, 3, 30), + (64, 64) + ) + + # Render page description + font = self._get_font("Regular", 42) + image.alpha_composite( + self._render_text((1200-228, 120), font, description, 3, 21), + (64, 488) + ) + + # Return social card image + return image + + def _render_text(self, size, font, text, lmax, spacing = 0): + return super()._render_text(size, font, emoji.replace_emoji(text, "").strip(), lmax, spacing)