; $Id: make_imageArray.pro,v 1.2 2002/01/14 20:59:25 amunter Exp $

; make_imageArray.pro
;
;
; This is a simple program to embed the data from a PNG file
; inside an IDL function which can be compiled and saved in an
; IDL savefile.
;
; There are probably a number of issues with this.  I have
; used it to embed a 185 x 138 24 bit PNG file successfully
; but suspect that there is a limit on .pro file size which
; would limit the maximum image size.  I have only tested
; this with a single image on a single machine so YMMV.
;
; In general, you select the file to convert and select a name
; to save the new .pro file as.
;
; image file: myimage.png
; pro file:   show_myimage.pro
;
; In your program, you call the function like any other:
;
; myimage = show_myimage()
; tv, myimage, /true
;
;
; There are color table issues that I haven't dealt with.
;
; Rick Towler
; School of Acquatic and Fishery Sciences
; University of Washington
; Box 355020
; Seattle, WA 98195-5020
; rtowler@u.washington.edu


infile = dialog_pickfile(/read, Title='Select PNG file to convert...', FILTER='*.png')
image = read_png(infile)

;select the output filename
outfile = dialog_pickfile(/write, Title='Save function as...', FILTER='*.pro')
;parse the path **will need to change depending on platform**
outtemp = strsplit(outfile, '\', /extract)
outfunc = (strsplit(outtemp[n_elements(outtemp) - 1], '.', /extract))[0]


image_length = n_elements(image)
image_size = strtrim(string(size(image, /dimensions)),2)
nloops = ceil(image_length / 3000) + 1
print,image_length
print,image_size
print,nloops

print, 'processing ' + strtrim(string(image_length),2) + ' pixel image'
print, 'image size: '+ image_size[0] + ',' + image_size[1] + ',' + image_size[2]

openw, lun, outfile, /get_lun

printf, lun, ';' + outfunc + '.pro'
printf, lun, ';image array generated by make_imageArray.pro'
printf, lun, ';image function created on: ' + systime()
printf, lun, ''
printf, lun, 'function ' + outfunc
printf, lun, ''
printf, lun, '    data = bytarr('+strtrim(string(image_size[0]),2) + $
             ',' + strtrim(string(image_size[1]),2) + ',' + $
             strtrim(string(image_size[2]),2) + ',/nozero)'
printf, lun, ''
printf, lun, ''


;main conversion loop
; write 20 x 150 element chunks.  Very close to IDL's limit
; on line length.
s=0L
for n = 0, nloops-2 do begin
    printf, lun, '    temp = [ $'

    printf, lun, image[s:s+2979L], format='(20(I3.3,","), " $")'
    printf, lun, image[s+2980L:s+2999L], format='(19(I3.3,","), I3.3, "]")'
    printf, lun, ''
    printf, lun, '    data['+strtrim(string(s),2)+':'+strtrim(string(s+2999L),2)+'] = temp'
    printf, lun, ''

    s=s+3000L
endfor


;clean up the tidbits
if (image_length gt s) then begin

    printf, lun, '    temp = [ $'

    whole_lines = floor((image_length - s) / 20.)
    last_line = (image_length - s) - (whole_lines * 20)

    if (last_line gt 0) then begin
        printf, lun, image[s:s+(whole_lines * 20)-1], format='(20(I3.3,","), " $")'
        printf, lun, image[(whole_lines * 20):(whole_lines * 20)+last_line-1], $
                format='('+strtrim(string(last_line-1))+'(I3.3,","), I3.3, "]")'
    endif else begin
        printf, lun, image[s:s+(whole_lines * 20)-21], format='(20(I3.3,","), " $")'
        printf, lun, image[s+(whole_lines * 20)-20:(image_length-1)], format='(19(I3.3,","), I3.3, "]")'
    end

    printf, lun, ''
    printf, lun, '    data['+strtrim(string(s),2)+':'+strtrim(string(image_length-1),2)+'] = temp'

end


printf, lun, ''
printf, lun, '    data = reform(data,'+image_size[0]+','+image_size[1]+','+image_size[2]+', /overwrite)'
printf, lun, ''
printf, lun, '    return, data'
printf, lun, ''
printf, lun, 'end'

free_lun, lun


end