openexr-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Openexr-devel] UNICODE support in openexr file I/O


From: Daniel Léonard
Subject: Re: [Openexr-devel] UNICODE support in openexr file I/O
Date: Fri, 20 Jan 2006 22:19:57 -0500

Hi,

To encode any strings, we could use the same encoding used with email
(smtp, pop and imap), that is the protocol that is specified in RFC
822.

   http://www.faqs.org/rfcs/rfc822.html

In short, any substring that needs characters that cannot be encoded
in 7-bit ASCII is sorrounded by a special quote characters. The basic
grammar is (but refer to the code after my signature) :

=?charset?[B|Q]?substring?=

where :

- =?, ?, ? and ?= are string litterals

- charset is the string representation of the charset (I have seen
utf8 and big5)
- either B or Q; B for hexadecimal, each character is on two octets, Q
is for base64.
- substring is the encoded substring

I suppose that any OpenEXR code that expects a string should expect a
unicode string and be able to transform it (writeRFC822) and any
OpenEXR code that returns a string should return a decoded unicode
string.

Then again my experiment was with Java (whose String type has always
been Unicode).

Daniel

--
Entre Ce que je pense, Ce que je veux dire, Ce que je crois dire, Ce
que je dis, Ce que vous avez envie d'entendre, Ce que vous entendez,
Ce que vous comprenez... il y a dix possibilités qu'on ait des
difficultés à communiquer. Mais essayons quand même...

P.S. : The Java code to decode such a string

   /**
    * Decodes a string encoding according to
    * <a href="http://www.faqs.org/rfcs/rfc822.html";>RFC 822</a>.
    * @param   raw   the raw string.
    * @return   the decoded string if the character encoding is supported, the
    *           raw string otherwise.
    */
   public static String rfc822Decode(String raw) {
      StringBuffer sb = new StringBuffer();

      int from = 0;
      while (raw != null) {
         int begin = raw.indexOf("=?", from);
         if (-1 == begin) {
            String what_is_left = raw.substring(from);
            sb.append(what_is_left);
            break;
            }

         int first = raw.indexOf("?", begin + 2);
         int second = raw.indexOf("?", first + 1);
         int end = raw.indexOf("?=", second + 1);

         String charset = raw.substring(begin + 2, first);
         String format = raw.substring(first + 1, second);
         String code = raw.substring(second + 1, end);

         String prefix = raw.substring(from, begin);
         sb.append(prefix);

         if ("Q".equalsIgnoreCase(format)) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len = code.length();
            for (int i = 0; i < len; i++) {
               int c = code.charAt(i);
               if ('=' == c) {
                  String hex = code.substring(i + 1, i + 3);
                  c = Integer.parseInt(hex, 16);
                  i += 2;
                  }
               baos.write(c);
               }

            byte[] bytes = baos.toByteArray();
            String value = raw;
            try {
               value = new String(bytes, charset);
               }
            catch (UnsupportedEncodingException uee) {
            // do nothing
               }
            sb.append(value);
            }
         else if ("B".equalsIgnoreCase(format)) {
            BASE64Decoder base64 = new BASE64Decoder();
            String value = raw;
            try {
               byte[] bytes = base64.decodeBuffer(code);
               value = new String(bytes, charset);
               }
            catch (IOException ioe) {
            // do nothing
               }

            sb.append(value);
            }
         else {
            sb.append(raw.substring(begin, end + 2));
            }

         from = end + 2;
         }

      raw = sb.toString();
      return raw;
      }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]