---
name: "jpcpack.c"
section: "3"
platform: "linux"
package: "libg2c-dev"
version: "2.1.0-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/jpcpack.c"
---

# jpcpack.c(3)

Pack and unpack an array of float/double using JPEG2000.

## NAME

jpcpack.c - Pack and unpack an array of float/double using JPEG2000.

## SYNOPSIS

`#include 'grib2_int.h'`\
 `#include <math.h>`\
 `#include <stdlib.h>`\

### Functions

int **g2c\_jpcpackd** (double \*fld, size\_t width, size\_t height, int \*idrstmpl, unsigned char \*cpack, size\_t \*lcpack)\
 This function packs up a double array into a JPEG2000 code stream.  int **g2c\_jpcpackf** (float \*fld, size\_t width, size\_t height, int \*idrstmpl, unsigned char \*cpack, size\_t \*lcpack)\
 This function packs up a float array into a JPEG2000 code stream.  void **jpcpack** (float \*fld, **g2int** width, **g2int** height, **g2int** \*idrstmpl, unsigned char \*cpack, **g2int** \*lcpack)\
 This function packs up a float array into a JPEG2000 code stream.  static int **jpcpack\_int** (void \*fld, int fld\_is\_double, **g2int** width, **g2int** height, **g2int** \*idrstmpl, unsigned char \*cpack, **g2int** \*lcpack, int verbose)\
 Packs a float or double array into a JPEG2000 code stream.

## Detailed Description

Pack and unpack an array of float/double using JPEG2000.

**Author**

Stephen Gilbert

**Date**

2003-08-17

### Program History Log

Date   Programmer   Comments    2003-08-17   Gilbert   Initial.    2004-11-92   Gilbert   Fixed bug packing a near constant field.    2004-07-19   Gilbert   If jpeg2000 encoding fails, try again with different encoder options.    2005-05-10   Gilbert   Imposed minimum size on cpack.    2022-08-12   Hartnett   Now handle doubles too.

Definition in file **jpcpack.c**.

## Function Documentation

### int g2c\_jpcpackd (double \* fld, size\_t width, size\_t height, int \* idrstmpl, unsigned char \* cpack, size\_t \* lcpack)

This function packs up a double array into a JPEG2000 code stream. After the data are scaled, and the reference value is subtracted out, the data are treated as a grayscale image and passed to a JPEG2000 encoder.

This function also fills in GRIB2 Data Representation Template 5.40 or 5.40000 with the appropriate values.

This function is the V2 API version of **jpcpack()** for doubles.

**Parameters**

*fld* Pointer to the double data values to pack. \
 *width* The number of points in the x direction. \
 *height* The number of points in the y direction. \
 *idrstmpl* Contains the array of values for Data Representation Template `Table 5.40` or 5.40000.

- •

  0 Reference value - ignored on input, set by jpcpack routine.

- •

  1 Binary Scale Factor - used on input, unchanged by jpcpack routine.

- •

  2 Decimal Scale Factor - used on input, unchanged by jpcpack routine.

- •

  3 number of bits for each data value - ignored on input

- •

  4 Original field type - currently ignored on input Data values assumed to be reals. Set to 0 on output.

- •

  5 if 0 use lossless compression, if 1 use lossy compression.

- •

  6 Desired compression ratio, if idrstmpl\[5\]=1. Set to 255, if idrstmpl\[5\]=0. May be modified in this function.

*cpack* A pointer that will get the packed data field. Must be allocated before this function is called. Pass the allocated size in the lcpack parameter. \
 *lcpack* Pointer that gets the length of packed field in cpack. This must be set by the calling function to the size available in cpack.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EJPEG** Error encoding/decoding JPEG data.

**Author**

Ed Hartnett

Definition at line **386** of file **jpcpack.c**.

References **G2C\_JPEG\_DRS\_TEMPLATE\_LEN**, and **jpcpack\_int()**.

### int g2c\_jpcpackf (float \* fld, size\_t width, size\_t height, int \* idrstmpl, unsigned char \* cpack, size\_t \* lcpack)

This function packs up a float array into a JPEG2000 code stream. After the data are scaled, and the reference value is subtracted out, the data are treated as a grayscale image and passed to a JPEG2000 encoder.

This function also fills in GRIB2 Data Representation Template 5.40 or 5.40000 with the appropriate values.

This function is the V2 API version of **jpcpack()** for floats.

**Parameters**

*fld* Pointer to the float data values to pack. \
 *width* The number of points in the x direction. \
 *height* The number of points in the y direction. \
 *idrstmpl* Contains the array of values for Data Representation Template `Table 5.40` or 5.40000.

- •

  0 Reference value - ignored on input, set by jpcpack routine.

- •

  1 Binary Scale Factor - used on input, unchanged by jpcpack routine.

- •

  2 Decimal Scale Factor - used on input, unchanged by jpcpack routine.

- •

  3 number of bits for each data value - ignored on input

- •

  4 Original field type - currently ignored on input Data values assumed to be reals. Set to 0 on output.

- •

  5 if 0 use lossless compression, if 1 use lossy compression.

- •

  6 Desired compression ratio, if idrstmpl\[5\]=1. Set to 255, if idrstmpl\[5\]=0. May be modified in this function.

*cpack* A pointer that will get the packed data field. Must be allocated before this function is called. Pass the allocated size in the lcpack parameter. \
 *lcpack* Pointer that gets the length of packed field in cpack. This must be set by the calling function to the size available in cpack.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EJPEG** Error encoding/decoding JPEG data.

**Author**

Ed Hartnett

Definition at line **320** of file **jpcpack.c**.

References **G2C\_JPEG\_DRS\_TEMPLATE\_LEN**, and **jpcpack\_int()**.

### void jpcpack (float \* fld, g2int width, g2int height, g2int \* idrstmpl, unsigned char \* cpack, g2int \* lcpack)

This function packs up a float array into a JPEG2000 code stream. After the data are scaled, and the reference value is subtracted out, the data are treated as a grayscale image and passed to a JPEG2000 encoder.

This function also fills in GRIB2 Data Representation Template 5.40 or 5.40000 with the appropriate values.

**Parameters**

*fld* Pointer to the float data values to pack. \
 *width* The number of points in the x direction. \
 *height* The number of points in the y direction. \
 *idrstmpl* Contains the array of values for Data Representation Template `Table 5.40` or 5.40000.

- •

  0 Reference value - ignored on input, set by jpcpack routine.

- •

  1 Binary Scale Factor - used on input, unchanged by jpcpack routine.

- •

  2 Decimal Scale Factor - used on input, unchanged by jpcpack routine.

- •

  3 number of bits for each data value - ignored on input

- •

  4 Original field type - currently ignored on input Data values assumed to be reals. Set to 0 on output.

- •

  5 if 0 use lossless compression, if 1 use lossy compression.

- •

  6 Desired compression ratio, if idrstmpl\[5\]=1. Set to 255, if idrstmpl\[5\]=0. May be modified in this function.

*cpack* A pointer that will get the packed data field. Must be allocated before this function is called. Pass the allocated size in the lcpack parameter. \
 *lcpack* Pointer that gets the length of packed field in cpack. This must be set by the calling function to the size available in cpack.

**Author**

Stephen Gilbert, Ed Hartnett

Definition at line **269** of file **jpcpack.c**.

References **jpcpack\_int()**.

Referenced by **g2\_addfield()**.

### static int jpcpack\_int (void \* fld, int fld\_is\_double, g2int width, g2int height, g2int \* idrstmpl, unsigned char \* cpack, g2int \* lcpack, int verbose)` [static]`

Packs a float or double array into a JPEG2000 code stream. This function is used by **jpcpack()**, **g2c\_jpcpackf()**, and **g2c\_jpcpackd()**.

**Parameters**

*fld* Pointer to the float or double data values to pack. \
 *fld\_is\_double* If non-zero, then fld points to array of doubles, otherwise an array of floats. \
 *width* The number of points in the x direction. \
 *height* The number of points in the y direction. \
 *idrstmpl* Contains the array of values for Data Representation Template `Table 5.40` or 5.40000. May be modified in this function. \
 *cpack* A pointer that will get the packed data field. Must be allocated before this function is called. Pass the allocated size in the lcpack parameter. \
 *lcpack* Pointer that gets the length of packed field in cpack. This must also be set by the calling function to the size available in cpack. \
 *verbose* If non-zero, error messages will be printed in case of error. Otherwise, error codes will be return but no error messages printed. Calls to the original g2c API may cause error messages to be printed in case of error. For the new g2c\_ API, no error messages will be printed - instead an error code will be returned. Call **g2c\_strerror()** to get the error message for any error code.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EJPEG** Error encoding/decoding JPEG data.

**Author**

Stephen Gilbert, Ed Hartnett

Definition at line **54** of file **jpcpack.c**.

References **ALOG2**, **enc\_jpeg2000()**, **G2C\_EJPEG**, **G2C\_NOERROR**, **int\_power()**, **LOG**, **mkieee()**, and **sbits()**.

Referenced by **g2c\_jpcpackd()**, **g2c\_jpcpackf()**, and **jpcpack()**.

## Author

Generated automatically by Doxygen for NCEPLIBS-g2c from the source code.
