/
pxiImg.go
531 lines (420 loc) · 15.3 KB
/
pxiImg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package st33
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"github.com/paulmatencio/s3c/gLog"
"github.com/paulmatencio/s3c/utils"
"strconv"
)
type PxiImg struct {
PxiId []byte
PageNum []byte
RefNum []byte
Img *bytes.Buffer
DataType []byte
NumPages []byte
}
// return the address of aPxiImg structure
func NewPxiImg() (*PxiImg) {
return &PxiImg{}
}
// Return a TIFF image
func (image *PxiImg) GetImage() *bytes.Buffer {
return image.Img
}
func (image *PxiImg) GetPageNum() []byte {
return image.PageNum
}
func (image *PxiImg) GetPxiId() []byte{
return image.PxiId
}
func (image *PxiImg) SetPxiId(p []byte) {
image.PxiId = p
}
func (image *PxiImg) GetRefNum() []byte {
return image.RefNum
}
func (image *PxiImg) GetDataType() []byte {
return image.DataType
}
func (image *PxiImg) BuildTiffImage(r *St33Reader, v Conval,cp int) (int,error,error) {
var (
totalRec uint16
totalLength uint32
recs uint16
imgl uint16
err, err1 error
nrec int = 0
st33,buf []byte
id string
)
gLog.Trace.Printf("PxiId: %s - Building image - Page number %d of %d pages",v.PxiId,cp,v.Pages)
Little := binary.LittleEndian
Big := binary.BigEndian
enc := Big
buf,err = r.Read() // Read the First record
if err != nil {
return nrec,err,err1
}
nrec++
/* check if record length = length of the ST33 record */
err1 = CheckST33Length(&v,r,buf)
if err1 != nil {
gLog.Error.Println(err)
gLog.Info.Printf("%s", hex.Dump(buf[0:255]))
return nrec,err,err1
}
if len(buf) <= 256 {
if buf,err = r.Read(); err != nil {
return nrec,err,err1
} else {
nrec++
}
}
st33 = utils.Ebc2asci(buf[0: 214])
id = string(st33[9:17]) + string(st33[7:9])
//k:= strings.Replace(v.PxiId,"_","",-1)[2:]
l := len(v.PxiId)
k := v.PxiId[9:l-2]
if ( k != id) {
// RewindST33(v,r,1)
gLog.Warning.Printf("Reading a wrong record which does not belong to the current page %d - PxiID: %s - IageId: %s k: %s",cp,v.PxiId,id,k)
}
err = binary.Read(bytes.NewReader(buf[25 : 27]), Big, &recs)
err = binary.Read(bytes.NewReader(buf[84 : 86]), Big, &totalRec)
err = binary.Read(bytes.NewReader(buf[214 : 218]), Big, &totalLength)
err = binary.Read(bytes.NewReader(buf[250 : 252]), Big, &imgl)
/*
convert St33 encoded Big Endian input data ( EBCDIC) to Little Endian (ASCII)
*/
// long, _ := strconv.Atoi(string(st33[0:5]))
// image.PxiId = st33[5:17] // PXI ID
image.PxiId = []byte(id)
image.PageNum = st33[17:21] // page nunber
image.RefNum = st33[34:45] // was 41
image.NumPages = st33[76:80] // Total number of pages
image.DataType = st33[180:181] // data type
/*
comp_meth := st33[181:183]
k_fac := st33[183:185]
Resolution := st33[185:187]
*/
Resolution := st33[185:187]
sFrH := st33[187:190]
sFrW := st33[190:193]
nlFrH := st33[193:197]
nlFrW := st33[197:201]
rotationCode := st33[201:202]
// fr_x := st33[202:206]
// fr_y := st33[206:210]
// fr_stat := st33[210:211]
version := st33[211:214]
buf1 := bytes.NewReader(buf[214 : 218]) // get the ST33 version number
if string(version) == "V30" {
// buf1 = bytes.NewReader(buf[k+214 : k+218])
// some V30 total_length are encoded with big Endian byte order
_ = binary.Read(buf1, Little, &totalLength)
if int(totalLength) > 16777215 {
buf1 = bytes.NewReader(buf[214 : 218])
_ = binary.Read(buf1, Big, &totalLength)
}
} else {
_ = binary.Read(buf1, Big, &totalLength) // get total length of the image
}
// Build the tiff image header
var img = new(bytes.Buffer)
_ = SetTiffMagicNumber(img, enc) // Magic number 6 bytes
_ = SetTiffImageWidth(img, enc, nlFrW) // image WIDTH 12 bytes
_ = SetTiffImageLength(img, enc, nlFrH) // image HEIGHT 12 bytes
_ = SetTiffImageCompression(img, enc) // image compression cG4 12 bytes
_ = SetTiffImagePhotometric(img, enc) // image Photometric 12 bytes
_ = SetTiffImageStripOffset(img, enc) // image Stripoffsets 12 bytes
_ = SetTiffImageOrientation(img, enc, rotationCode) // image Orientation 12 bytes
// computing the image length is done after every records containing the image are read
// continue to create the TIFF header before the image length attribute
var img2 = new(bytes.Buffer)
_ = SetTiffImageXresolution(img2,enc) // image X resolution
_ = SetTiffImageYresolution(img2, enc) // image Y resolution
_ = SetTiffImageResolutionUnit(img2, Resolution, enc) // image resolution Unit
_ = binary.Write(img2, enc, uint32(0)) // next IFD = 0
_ = binary.Write(img2, enc, Getuint32(nlFrW)*25) // Xresolution value
_ = binary.Write(img2, enc, Getuint32(sFrW))
_ = binary.Write(img2, enc, Getuint32(nlFrH)*25) // Yresoluton value
_ = binary.Write(img2, enc, Getuint32(sFrH))
imageL := 0 // Total length of the image
// build the image with the St33 first record
_ = binary.Read(bytes.NewReader(buf[250:252]), Big, &imgl)
rec := 1
imgL,err1 := writeImg(img2,buf)
if err1 == nil {
imageL += imgL
} else {
gLog.Error.Printf("Record:%d of %d - Error: %v ",rec,int(totalRec),err1)
return nrec,err,err1
}
// read all the records for this image.
// the number of records are extracted from the image header
// if the total number of the records is > the control records the take the total number of records from the
// control file
for rec := 2; rec <= int(totalRec); rec++ {
if buf,err = r.Read();err == nil {
nrec++ // increment the number of read records
imgL, err1 = writeImg(img2, buf)
if err1 == nil {
imageL += imgL
} else {
gLog.Error.Printf("Record:%d of %d - Error:%v ", rec, int(totalRec), err1)
return nrec, err, err1
}
} else {
break
}
}
gLog.Trace.Printf("PixId: %s - Total number of records from ST33 header: %d - Total number of read records: %d ",v.PxiId,totalRec,nrec)
// Check if the input header of the first record we read is an ST33 record
// totalLength is the length of the TIFF image extracted from the first record of the image
// imageL is the sum of the image length of all records = true length of the image
var img3 = new(bytes.Buffer)
if int(totalLength) < imageL {
totalLength = uint32(imageL)
}
// set the TIFF image length */
_ = SetTiffImageStripByteCount(img3, enc, uint32(imageL)) // image Strip Byte Counts
// Append img3 and img2 into img to form the final TIFF image
// img2 and img3 bytes buffer willbe reset when this function is exited
img.Write(img3.Bytes())
img.Write(img2.Bytes())
defer img2.Reset()
defer img3.Reset()
// return the final image in the image struct
// It is recommended to reset the the buffer of the image when it is consummed by the client */
image.Img = img
// The caller must check if number of records from the control file match the number of records in the data file
// If not the caller must skip all the remaining records in the data file
return nrec,err,err1
}
func writeImg(img2 *bytes.Buffer, buf []byte) (int, error){
var (
imgl uint16
err error
Big = binary.BigEndian
)
_ = binary.Read(bytes.NewReader(buf[250:252]), Big, &imgl)
err = nil
if int64(imgl) <= int64(len(buf)-252) {
img2.Write(buf[252 : 252+int64(imgl)]) // append the image length found in this record to the image
// Compute the total image length
} else {
gLog.Error.Printf("Dumping ST33 header of the record: %s", hex.Dump(buf[0:252]))
error := fmt.Sprintf("Invalid ST33 image length: %d - Buffer length: %d",int64(imgl), int64(len(buf)-252))
err = errors.New(error)
/* gLog.Error.Printf("%v",err)*/
}
return int(imgl),err
}
func (image *PxiImg) BuildTiffImageV2(r *St33Reader, v Conval,cp int) (int,error,error) {
var (
totalRec uint16
totalLength uint32
recs uint16
imgl uint16
err, err1 error
nrec int = 0
st33,buf []byte
id string
)
gLog.Trace.Printf("PxiId: %s - Building image - Page number %d of %d pages",v.PxiId,cp,v.Pages)
Little := binary.LittleEndian
Big := binary.BigEndian
enc := Big
// Skip records if the page number of st33 header < the current page
for {
buf, err = r.Read()
if err != nil {
return nrec, err, err1
}
nrec++ // increment the number of total record read
err1 = CheckST33Length(&v, r, buf)
if err1 != nil {
gLog.Error.Println(err)
gLog.Info.Printf("Dump of the ST33 header of the current record: %s", hex.Dump(buf[0:255]))
/*
buf, err = r.Read()
m:= "Dump of the ST33 header of the next record"
if len(buf) > 252 {
gLog.Info.Printf("%s: %s", m,hex.Dump(buf[0:255]))
} else {
gLog.Info.Printf("%s: %s", m,hex.Dump(buf[0:len(buf)-1]))
}
*/
return nrec, err, err1
}
st33 = utils.Ebc2asci(buf[0:214])
// get the id of the image
id = string(st33[9:17]) + string(st33[7:9])
pn, _ := strconv.Atoi(string(st33[17:21])) /* get the page number of the image */
k := v.PxiId[9:len(v.PxiId)-2]
/*
Compare the page number of the image with the current page
if cp > pn then skip the record
if less or equal just
*/
if cp > pn { // I am reading a record of the previous image- Just reas the next record
gLog.Warning.Printf("PxiId: %s - Skipping a record ( %d bytes) - current page number:%d > page number of the image: %d - image id/pxiId %s/%s ",v.PxiId,len(buf),cp,pn,id ,k)
if id != k {
/* overflow */
RewindST33(v,r,-1)
nrec--
return nrec,nil,nil
}
} else {
break
}
}
/* check if record length = length of the ST33 record */
err = binary.Read(bytes.NewReader(buf[25 : 27]), Big, &recs)
err = binary.Read(bytes.NewReader(buf[84 : 86]), Big, &totalRec)
err = binary.Read(bytes.NewReader(buf[214 : 218]), Big, &totalLength)
err = binary.Read(bytes.NewReader(buf[250 : 252]), Big, &imgl)
/*
convert St33 encoded Big Endian input data ( EBCDIC) to Little Endian (ASCII)
*/
// st33 := utils.Ebc2asci(buf[0: 214])
// long, _ := strconv.Atoi(string(st33[0:5]))
// image.PxiId = st33[5:17] // PXI ID
image.PxiId = []byte(id)
image.PageNum = st33[17:21] // page nunber
image.RefNum = st33[34:45] // was 41
image.NumPages = st33[76:80] // Total number of pages
image.DataType = st33[180:181] // data type
/*
comp_meth := st33[181:183]
k_fac := st33[183:185]
Resolution := st33[185:187]
*/
Resolution := st33[185:187]
sFrH := st33[187:190]
sFrW := st33[190:193]
nlFrH := st33[193:197]
nlFrW := st33[197:201]
rotationCode := st33[201:202]
// fr_x := st33[202:206]
// fr_y := st33[206:210]
// fr_stat := st33[210:211]
version := st33[211:214]
buf1 := bytes.NewReader(buf[214 : 218]) // get the ST33 version number
if string(version) == "V30" {
// buf1 = bytes.NewReader(buf[k+214 : k+218])
// some V30 total_length are encoded with big Endian byte order
_ = binary.Read(buf1, Little, &totalLength)
if int(totalLength) > 16777215 {
buf1 = bytes.NewReader(buf[214 : 218])
_ = binary.Read(buf1, Big, &totalLength)
}
} else {
_ = binary.Read(buf1, Big, &totalLength) // get total length of the image
}
// Build the tiff image header
var img = new(bytes.Buffer)
_ = SetTiffMagicNumber(img, enc) // Magic number 6 bytes
_ = SetTiffImageWidth(img, enc, nlFrW) // image WIDTH 12 bytes
_ = SetTiffImageLength(img, enc, nlFrH) // image HEIGHT 12 bytes
_ = SetTiffImageCompression(img, enc) // image compression cG4 12 bytes
_ = SetTiffImagePhotometric(img, enc) // image Photometric 12 bytes
_ = SetTiffImageStripOffset(img, enc) // image Stripoffsets 12 bytes
_ = SetTiffImageOrientation(img, enc, rotationCode) // image Orientation 12 bytes
// computing the image length is done after every records containing the image are read
// continue to create the TIFF header before the image length attribute
var img2 = new(bytes.Buffer)
_ = SetTiffImageXresolution(img2,enc) // image X resolution
_ = SetTiffImageYresolution(img2, enc) // image Y resolution
_ = SetTiffImageResolutionUnit(img2, Resolution, enc) // image resolution Unit
_ = binary.Write(img2, enc, uint32(0)) // next IFD = 0
_ = binary.Write(img2, enc, Getuint32(nlFrW)*25) // Xresolution value
_ = binary.Write(img2, enc, Getuint32(sFrW))
_ = binary.Write(img2, enc, Getuint32(nlFrH)*25) // Yresoluton value
_ = binary.Write(img2, enc, Getuint32(sFrH))
imageL := 0 // Total length of the image
// build the image with the St33 first record
_ = binary.Read(bytes.NewReader(buf[250:252]), Big, &imgl)
/*
img2.Write(buf[252 : 252+int64(imgl)]) // append the image length found in this record to the image
imageL += int(imgl)
*/
rec := 1
imgL,err1 := writeImg(img2,buf)
if err1 == nil {
imageL += imgL
} else {
gLog.Error.Printf("Record:%d of %d - Error: %v ",rec,int(totalRec),err1)
return nrec,err,err1
}
// read all the records for this image.
// the number of records are extracted from the image header
// if the total number of the records is > the control records the take the total number of records from the
// control file
/*
for rec := 2; rec <= int(totalRec); rec++ {
if buf,err = r.Read();err == nil {
nrec++ // increment the number of read records
imgL, err1 = writeImg(img2, buf)
if err1 == nil {
imageL += imgL
} else {
gLog.Error.Printf("Record:%d of %d - Error:%v ", rec, int(totalRec), err1)
return nrec, err, err1
}
} else {
break
}
}
*/
tRecord := int(totalRec)
for {
if rec >= tRecord {
break
}
if buf,err = r.Read();err == nil {
rec++ //increment the number of records for this page
nrec++ // increment the total number of read records for this image
imgL, err1 = writeImg(img2, buf)
if err1 == nil {
imageL += imgL
} else {
// dump the next record for trouble shooting and exit
if buf,err = r.Read();err == nil && len(buf) > 255 {
gLog.Info.Printf("Dump of the next record : %s", hex.Dump(buf[0:255]))
}
gLog.Error.Printf("Record:%d of %d - Error:%v ", rec, int(totalRec), err1)
return nrec, err, err1
}
} else {
break
}
}
gLog.Trace.Printf("PixId: %s - Total number of records from ST33 header: %d - Total number of read records: %d ",v.PxiId,totalRec,nrec)
// Check if the input header of the first record we read is an ST33 record
// totalLength is the length of the TIFF image extracted from the first record of the image
// imageL is the sum of the image length of all records = true length of the image
var img3 = new(bytes.Buffer)
if int(totalLength) < imageL {
totalLength = uint32(imageL)
}
// set the TIFF image length */
_ = SetTiffImageStripByteCount(img3, enc, uint32(imageL)) // image Strip Byte Counts
// Append img3 and img2 into img to form the final TIFF image
// img2 and img3 bytes buffer willbe reset when this function is exited
img.Write(img3.Bytes())
img.Write(img2.Bytes())
defer img2.Reset()
defer img3.Reset()
// return the final image in the image struct
// It is recommended to reset the the buffer of the image when it is consummed by the client */
image.Img = img
// The caller must check if number of records from the control file match the number of records in the data file
// If not the caller must skip all the remaining records in the data file
return nrec,err,err1
}