Reading the (text) contents of a GIF

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Athor
Posts: 11
Joined: July 7th, 2009, 5:18 pm

Hi guys,

I am working on a script to basically insert a gif as metadata into a fx preset file. If you open Adobe bridge and browse to the After effects text presets you will see that you can click on a preset and in the preview there is an animated GIF. So I am attempting to do this for custom presets.

Basically I think I need to read the contents of a GIF file as data, or text, then encode that as base64 and insert it into the metadata of the preset file.

The problem is when I read in the GIF as data I can't seem to get all the data.

The code I'm using is

Code: Select all

file = new File("C:\\test.gif");
file.open("r");
test = file.read();
alert(test);
And what gets returned is

Code: Select all

GIF89aØ
If I open the gif in notepad I get:

Code: Select all

GIF89aØ   ô     ÿÿÿ–••øøøòòòìììæææàààÛÛÛÕÕÕÐÐÐÊÊÊÅÅÅ¿¿¿¸¸¸°°°¨¨¨ŸŸŸŒŒŒƒƒƒ{{{rrriii___VVVMMMCCC999///###!ÿNETSCAPE2.0   !ù ÿ ,    Ø    ÿ  Ždižhª®lë¾p,Ïtmßx®ï|ïÿÀ pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ 
H° Áƒ*\Ȱ¡Ã‡#JœH±¢Å‹3jÜȱ£Ç CŠI²¤ÉE!  !ù ÿ ,! C    VàdYJ›Tlë¶HF½4œe¥KM+è:@,JZ›¢Ré9²†Ë¥gÆJF—Î ôJl¶¶\@|õVÃÆ/`Ö®ÅjtÛŠ¯§ñp›|¥è\{ovK! !ù ÿ ,0 I    ˆ TŒ`š[3Y‹w—ôÛ,í Å&9q¯jñBœ2ˆEìzÂWK‹Šk Bx0ÀÆk™	]¬L’kâx¿[ù$.0n&-;
‚&L;E‰ ;AŽ;dŽ yŽ5/
˜CMt'^%‰6_˜L›˜fœ˜! !ù ÿ ,@ H      Ž¢·qd	dM ’7z,…ì‹MI0›-¿†ÍCô	l KOv0P‘Åñ\e™Ô&(X›!‰Óp¾é '{£Õ#¶ÏÛ,„I2|>.$R2g?#H`Œ?‚X“‘?(“‰ƒYpjY7wr)§®$°±Cƒx±p´%«´! !ù ÿ ,C H    ë  ŠÞƍh
p¦ZP@Öt!y*àQØ'Q@ èùh‹M*³8ú’Bca86p¢Mó'¸4Œ“O€µ’ ©]z€FFY|Ç(GAè4)3?"wP84am#k4'N›I’”#˜ ¡›Gž)–>—D±±ƒ(Z4"º·9s0vFu>ÃÀ>} U‚$†6*X¦Ï€#gŠ
MbØGá)äGP’f>Xªéb“"òÀ$-P¤òG0ÇÀ‚…äK˜ð C‚?>=ô×o"° !ù ÿ ,a I    ªàUŒ`R ×	d*"qE@®U‚KÙ…`akpN[ á9AlÓ/€¢Êª)eë8@‹	 J/lð´óJl ©!\ÉÛñà.6 |scPDO,DV[5"%6‡6.&|yG]u7 ˜K
KM««-’5h?#MŠJ€„J
§(SeG&޲Ç/;.yË&! !ù ÿ ,y H 
   =  Ž€·™MQœRPÛâf[Àu ¹.ÅÉ<%ãáä
Òæè!m$À3°ˆNÖ\Õ™Ã*»Y×vt
¼£ !ù ÿ ,„ B    0  x¢(ÁdH,×¾âÖHd©ïÜR
€ÌŠ3$Q˜d.Ê£S
F¯V@ !ù ÿ ,ˆ E    bàŒd&q$à¥R[*KÁˤPÃi£¶µ`¡ ðø<º âZ®¢ 1¦’<’/çÊŽ¶#ìí{ݍ`€¸wížÓk-kwa“(Qˆ}tA(h2
Ji?JU8$+ ! !ù ÿ ,– H    k  ŽÞƍèH!A'ʱn½‘K[@T葤…ÈŒ2¬‚àI.Q"À’42™”bC©y]EÉï[+d†-ˆ`Í^K<–Ÿ9•B¦é©†KDõ4c_7$+^0x%& ! !ùF ÿ ,£ H    S  ޤÇq$…PE •MX‚-{žèA¶šN ˜~º@ArA-zHÛ“„Ø…	€È¶- àÄ(ø,öaÛ¾5ü«÷èá­õZߢðl$! ;


So am I doing something wrong in reading in the gif? Do I need to specify some sort of encoding?
Athor
Posts: 11
Joined: July 7th, 2009, 5:18 pm

If i copy the data to a txt file, and remove the "GIF89aØ" at the beginning, the rest of the data reads in fine.

So I guess javascript is getting stuck on the "GIF89aØ". Any ideas?
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Might be a PC thing. Just tested your test script on mac and I get the full text.

-Lloyd
Attachments
Screen shot 2010-02-01 at 10.09.08 AM.png
Screen shot 2010-02-01 at 10.09.08 AM.png (193.59 KiB) Viewed 16220 times
Athor
Posts: 11
Joined: July 7th, 2009, 5:18 pm

Thanks for testing it :D Atleast it narrows down the issue.

I've been testing it a little more and if i 'seek' to a specific byte number before reading the data, I can read parts of it in.

E.g. If I seek to byte 14 I get a portion of text up until it encounters white space. It seems whenever there is some whitespace (like directly after the GIF89a part), it stops reading it.

Maybe its a encoding issue, and I don't have the correct encoding on my system.

Would you be able to run it again with a

Code: Select all

alert(file.encoding);
after the read command?
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Here you go. Have you read over the File encoding chapter in the Javascript Tools Guide CS4?
Attachments
Screen shot 2010-02-02 at 9.52.51 AM.png
Screen shot 2010-02-02 at 9.52.51 AM.png (23.7 KiB) Viewed 16211 times
Athor
Posts: 11
Joined: July 7th, 2009, 5:18 pm

Yea I have read over it. I have tried to read it with a bunch of different encodings but get the same result each time.
Post Reply