image processing - Extract IPTC information from JPEG using Javascript -


i'm trying extract iptc photo caption information jpeg file using javascript. (i know can server-side, i'm looking javascript solution.)

i found this script, extracts exif information ... i'm not sure how adapt grab iptc data.

are there existing scripts offer such functionality? if not, how modify exif script parse iptc data?

update

i've modified exif script linked above. sorta want, it's not grabbing right data 100 percent of time.

after line 401, added:

else if (imarker == 237) {         // 0xed = application-specific 13 (photoshop iptc)                         if (bdebug) log("found 0xffed marker");            return readiptcdata(ofile, ioffset + 4, getshortat(ofile, ioffset+2, true)-2);                        } 

and elsewhere in script, added function:

function readiptcdata(ofile, istart, ilength) {     exif = new array();  if (getstringat(ofile, istart, 9) != "photoshop") {     if (bdebug) log("not valid photoshop data! " + getstringat(ofile, istart, 9));     return false; }  var output = ''; var count = 0; 2 = new array(); (i=0; i<ilength; i++) {    if (getbyteat(ofile, istart + i) == 2 && getbyteat(ofile, istart + + 1) == 120) {       var caption = getstring2at(ofile, istart + + 2, 800);    }    if (getbyteat(ofile, istart + i) == 2 && getbyteat(ofile, istart + + 1) == 80) {       var credit = getstring2at(ofile, istart + + 2, 300);    }        }  exif['imagedescription'] = caption; exif['artist'] = credit;  return exif;  } 

so let me modify question slightly. how can function above improved?

for it's worth, extrapolated on bit... haven't done whole lot of testing, few test images have seem work.

    var bdebug = false;      var fieldmap = {         120 : 'caption',         110 : 'credit',         25 : 'keywords',         85 : 'byline',         122 : 'captionwriter',         105 : 'headline',         116 : 'copyright',         15 : 'category'     };      function readiptcdata(ofile, istart, ilength) {         var data = {};          if (ofile.getstringat(istart, 9) != "photoshop") {             if (bdebug) log("not valid photoshop data! " + ofile.getstringat(istart, 9));             return false;         }          var filelength = ofile.getlength();          var length, offset, fieldstart, title, value;         var file_separator_char = 28,             start_of_text_char = 2;          (var = 0; < ilength; i++) {              fieldstart = istart + i;             if(ofile.getbyteat(fieldstart) == start_of_text_char && ofile.getbyteat(fieldstart + 1) in fieldmap) {                 length = 0;                 offset = 2;                  while(                     fieldstart + offset < filelength &&                     ofile.getbyteat(fieldstart + offset) != file_separator_char &&                     ofile.getbyteat(fieldstart + offset + 1) != start_of_text_char) { offset++; length++; }                  if(!length) { continue; }                  title = fieldmap[ofile.getbyteat(fieldstart + 1)];                 value = ofile.getstringat(istart + + 2, length) || '';                 value = value.replace('\000','').trim();                  data[title] = value;                 i+=length-1;             }         }          return data;      }      function findiptcinjpeg(ofile) {         var amarkers = [];          if (ofile.getbyteat(0) != 0xff || ofile.getbyteat(1) != 0xd8) {             return false; // not valid jpeg         }          var ioffset = 2;         var ilength = ofile.getlength();         while (ioffset < ilength) {             if (ofile.getbyteat(ioffset) != 0xff) {                 if (bdebug) console.log("not valid marker @ offset " + ioffset + ", found: " + ofile.getbyteat(ioffset));                 return false; // not valid marker, wrong             }              var imarker = ofile.getbyteat(ioffset+1);              if (imarker == 237) {                 if (bdebug) console.log("found 0xffed marker");                 return readiptcdata(ofile, ioffset + 4, ofile.getshortat(ioffset+2, true)-2);              } else {                 ioffset += 2 + ofile.getshortat(ioffset+2, true);             }          }      }      iptc.readfrombinaryfile = function(ofile) {         return findiptcinjpeg(ofile);     } 

Comments