NCEPLIBS-g2  3.4.7
jpcpack.F90
Go to the documentation of this file.
1 
4 
31 subroutine jpcpack(fld,width,height,idrstmpl,cpack,lcpack)
32  implicit none
33 
34  integer,intent(in) :: width,height
35  real,intent(in) :: fld(width*height)
36  character(len=1),intent(out) :: cpack(*)
37  integer,intent(inout) :: idrstmpl(*)
38  integer,intent(inout) :: lcpack
39  integer :: ndpts, nbits, maxdif, imin, imax, j, nbytes
40  real :: dscale, bscale, temp
41 
42  interface
43  function enc_jpeg2000(cin, width, height, nbits, ltype, ratio, retry, outjpc, jpclen) &
44  bind(c, name="g2c_enc_jpeg2000")
45  use iso_c_binding
46  character(kind = c_char), intent(in) :: cin(*)
47  integer(c_int), value, intent(in) :: width, height, nbits, ltype, ratio, retry
48  character(kind = c_char), intent(inout) :: outjpc(*)
49  integer(c_size_t), value, intent(in) :: jpclen
50  integer(c_int) :: enc_jpeg2000
51  end function enc_jpeg2000
52  end interface
53 
54  real(4) :: ref, rmin4
55  real(8) :: rmin, rmax
56  integer(4) :: iref
57  integer(8) :: nsize
58  integer :: ifld(width * height), retry
59  integer, parameter :: zero = 0
60  character(len = 1), allocatable :: ctemp(:)
61 
62  ndpts = width * height
63  bscale = 2.0**real(-idrstmpl(2))
64  dscale = 10.0**real(idrstmpl(3))
65 
66  ! Find max and min values in the data.
67  if (ndpts > 0) then
68  rmax = fld(1)
69  rmin = fld(1)
70  else
71  rmax = 1.0
72  rmin = 1.0
73  endif
74  do j = 2, ndpts
75  if (fld(j) .gt. rmax) rmax = fld(j)
76  if (fld(j) .lt. rmin) rmin = fld(j)
77  enddo
78  if (idrstmpl(2) .eq. 0) then
79  maxdif = nint(rmax * dscale) - nint(rmin * dscale)
80  else
81  maxdif = nint((rmax - rmin) * dscale * bscale)
82  endif
83 
84  ! If max and min values are not equal, pack up field. If they are
85  ! equal, we have a constant field, and the reference value (rmin) is
86  ! the value for each point in the field and set nbits to 0.
87  if (rmin .ne. rmax .AND. maxdif .ne. 0) then
88  ! Determine which algorithm to use based on user-supplied binary
89  ! scale factor and number of bits.
90  if (idrstmpl(2) .eq. 0) then
91  ! No binary scaling and calculate minimum number of bits in
92  ! which the data will fit.
93  imin = nint(rmin * dscale)
94  imax = nint(rmax * dscale)
95  maxdif = imax - imin
96  temp = alog(real(maxdif + 1)) / alog(2.0)
97  nbits = ceiling(temp)
98  rmin = real(imin)
99  ! Scale data.
100  do j = 1, ndpts
101  ifld(j) = nint(fld(j) * dscale) - imin
102  enddo
103  else
104  ! Use binary scaling factor and calculate minimum number of
105  ! bits in which the data will fit.
106  rmin = rmin * dscale
107  rmax = rmax * dscale
108  maxdif = nint((rmax - rmin) * bscale)
109  temp = alog(real(maxdif + 1)) / alog(2.0)
110  nbits = ceiling(temp)
111  ! scale data
112  do j = 1, ndpts
113  ifld(j) = max(0, nint(((fld(j) * dscale) - rmin) * bscale))
114  enddo
115  endif
116 
117  ! Pack data into full octets, then do JPEG2000 encode. and
118  ! calculate the length of the packed data in bytes
119  retry = 0
120  nbytes = (nbits + 7) / 8
121  nsize = lcpack ! needed for input to enc_jpeg2000
122  allocate(ctemp(nbytes * ndpts))
123  call g2_sbytesc(ctemp, ifld, 0, nbytes * 8, 0, ndpts)
124  lcpack = enc_jpeg2000(ctemp, width, height, nbits, idrstmpl(6), &
125  idrstmpl(7), retry, cpack, nsize)
126  if (lcpack .le. 0) then
127  print *,'jpcpack: ERROR Packing JPC = ',lcpack
128  if (lcpack .eq. -3) then
129  retry = 1
130  print *,'jpcpack: Retrying....'
131  lcpack = enc_jpeg2000(ctemp, width, height, nbits, idrstmpl(6), &
132  idrstmpl(7), retry, cpack, nsize)
133  if (lcpack .le. 0) then
134  print *,'jpcpack: Retry Failed.'
135  else
136  print *,'jpcpack: Retry Successful.'
137  endif
138  endif
139  endif
140  deallocate(ctemp)
141  else
142  nbits = 0
143  lcpack = 0
144  endif
145 
146  ! Fill in ref value and number of bits in Template 5.0.
147  rmin4 = real(rmin, 4)
148  call mkieee(rmin4, ref, 1) ! ensure reference value is IEEE format.
149  iref = transfer(ref, iref)
150  idrstmpl(1) = iref
151  idrstmpl(4) = nbits
152  idrstmpl(5) = 0 ! original data were reals.
153  if (idrstmpl(6) .eq. 0) idrstmpl(7) = 255 ! lossy not used
154 
155 end subroutine
int enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, g2int ltype, g2int ratio, g2int retry, char *outjpc, g2int jpclen)
This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 ...
Definition: enc_jpeg2000.c:91
subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
Put arbitrary size values into a packed bit string, taking the low order bits from each value in the ...
Definition: g2_gbytesc.F90:120
subroutine jpcpack(fld, width, height, idrstmpl, cpack, lcpack)
Pack a data field into a JPEG2000 code stream.
Definition: jpcpack.F90:32
subroutine mkieee(a, rieee, num)
Copy an array of real to an array of 32-bit IEEE floating points.
Definition: mkieee.F90:14