NCEPLIBS-g2  3.4.5
pngpack.f
Go to the documentation of this file.
1 
5 
30 
31  subroutine pngpack(fld,width,height,idrstmpl,cpack,lcpack)
32 
33  integer,intent(in) :: width,height
34  real,intent(in) :: fld(width*height)
35  character(len=1),intent(out) :: cpack(*)
36  integer,intent(inout) :: idrstmpl(*)
37  integer,intent(out) :: lcpack
38 
39  real(4) :: ref,rmin4
40  real(8) :: rmin,rmax
41  integer(4) :: iref
42  integer :: ifld(width*height), nbits
43  integer,parameter :: zero=0
44  integer :: enc_png
45  character(len=1),allocatable :: ctemp(:)
46 
47  ndpts=width*height
48  bscale=2.0**real(-idrstmpl(2))
49  dscale=10.0**real(idrstmpl(3))
50 !
51 ! Find max and min values in the data
52 !
53  if(ndpts>0) then
54  rmax=fld(1)
55  rmin=fld(1)
56  else
57  rmax=1.0
58  rmin=1.0
59  endif
60  do j=2,ndpts
61  if (fld(j).gt.rmax) rmax=fld(j)
62  if (fld(j).lt.rmin) rmin=fld(j)
63  enddo
64  maxdif=nint((rmax-rmin)*dscale*bscale)
65 !
66 ! If max and min values are not equal, pack up field.
67 ! If they are equal, we have a constant field, and the reference
68 ! value (rmin) is the value for each point in the field and
69 ! set nbits to 0.
70 !
71  if (rmin.ne.rmax .AND. maxdif.ne.0) then
72  !
73  ! Determine which algorithm to use based on user-supplied
74  ! binary scale factor and number of bits.
75  !
76  if (idrstmpl(2).eq.0) then
77  !
78  ! No binary scaling and calculate minimum number of
79  ! bits in which the data will fit.
80  !
81  imin=nint(rmin*dscale)
82  imax=nint(rmax*dscale)
83  maxdif=imax-imin
84  temp=alog(real(maxdif+1))/alog(2.0)
85  nbits=ceiling(temp)
86  rmin=real(imin)
87  ! scale data
88  do j=1,ndpts
89  ifld(j)=nint(fld(j)*dscale)-imin
90  enddo
91  else
92  !
93  ! Use binary scaling factor and calculate minimum number of
94  ! bits in which the data will fit.
95  !
96  rmin=rmin*dscale
97  rmax=rmax*dscale
98  maxdif=nint((rmax-rmin)*bscale)
99  temp=alog(real(maxdif+1))/alog(2.0)
100  nbits=ceiling(temp)
101  ! scale data
102  do j=1,ndpts
103  ifld(j)=max(0,nint(((fld(j)*dscale)-rmin)*bscale))
104  enddo
105  endif
106  !
107  ! Pack data into full octets, then do PNG encode.
108  ! and calculate the length of the packed data in bytes
109  !
110  if (nbits.le.8) then
111  nbits=8
112  elseif (nbits.le.16) then
113  nbits=16
114  elseif (nbits.le.24) then
115  nbits=24
116  else
117  nbits=32
118  endif
119  nbytes=(nbits/8)*ndpts
120  allocate(ctemp(nbytes))
121  call g2_sbytesc(ctemp,ifld,0,nbits,0,ndpts)
122  !
123  ! Encode data into PNG Format.
124  !
125  lcpack=enc_png(ctemp,width,height,nbits,cpack)
126  if (lcpack.le.0) then
127  print *,'pngpack: ERROR Encoding PNG = ',lcpack
128  endif
129  deallocate(ctemp)
130 
131  else
132  nbits=0
133  lcpack=0
134  endif
135 
136 !
137 ! Fill in ref value and number of bits in Template 5.0
138 !
139  rmin4=rmin
140  call mkieee(rmin4,ref,1) ! ensure reference value is IEEE format
141 ! call g2_gbytec(ref,idrstmpl(1),0,32)
142  iref=transfer(ref,iref)
143  idrstmpl(1)=iref
144  idrstmpl(4)=nbits
145  idrstmpl(5)=0 ! original data were reals
146 
147  return
148  end
g2_sbytesc
subroutine g2_sbytesc(OUT, IN, ISKIP, NBYTE, NSKIP, N)
This subrountine is to put arbitrary size values into a packed bit string, taking the low order bits ...
Definition: g2_gbytesc.f:115
pngpack
subroutine pngpack(fld, width, height, idrstmpl, cpack, lcpack)
This subroutine packs up a data field into PNG image format.
Definition: pngpack.f:32
mkieee
subroutine mkieee(a, rieee, num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
Definition: mkieee.f:17