Annotation of parser3/src/classes/image.C, revision 1.90.2.5

1.1       paf         1: /** @file
                      2:        Parser: @b image parser class.
                      3: 
1.90.2.3  paf         4:        Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.66      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.73      paf         6: */
1.1       paf         7: 
1.90.2.5! paf         8: static const char* IDENT_IMAGE_C="$Date: 2003/02/04 14:04:45 $";
1.31      parser      9: 
                     10: /*
                     11:        jpegsize: gets the width and height (in pixels) of a jpeg file
                     12:        Andrew Tong, werdna@ugcs.caltech.edu           February 14, 1995
                     13:        modified slightly by alex@ed.ac.uk
                     14:        and further still by rjray@uswest.com
                     15:        optimization and general re-write from tmetro@vl.com
                     16:        from perl by paf@design.ru
1.1       paf        17: */
                     18: 
                     19: #include "pa_config_includes.h"
1.90.2.5! paf        20: 
        !            21: #include "pa_vmethod_frame.h"
1.1       paf        22: 
1.8       paf        23: #include "gif.h"
1.6       paf        24: 
1.1       paf        25: #include "pa_common.h"
                     26: #include "pa_request.h"
                     27: #include "pa_vfile.h"
                     28: #include "pa_vimage.h"
1.89      paf        29: #include "pa_vdate.h"
1.1       paf        30: 
1.22      paf        31: // class
                     32: 
                     33: class MImage : public Methoded {
                     34: public: // VStateless_class
1.90.2.2  paf        35:        ValuePtr create_new_value() { return ValuePtr(new VImage()); }
1.22      paf        36: 
                     37: public:
                     38:        MImage(Pool& pool);
1.24      paf        39: 
                     40: public: // Methoded
1.22      paf        41:        bool used_directly() { return true; }
                     42: 
                     43: };
1.1       paf        44: 
                     45: // helpers
                     46: 
1.90.2.1  paf        47: /// value of exif tag -> it's value
1.90.2.3  paf        48: Hash<int, const char* > exif_tag_value2name;
1.90.2.1  paf        49: 
                     50: 
1.43      parser     51: #ifndef DOXYGEN
1.1       paf        52: class Measure_reader {
                     53: public:
1.77      paf        54:        virtual size_t read(const void *&buf, size_t limit)=0;
1.79      paf        55:        virtual void seek(long value, int whence)=0;
                     56:        virtual long tell()=0;
1.77      paf        57: };
1.1       paf        58: 
1.77      paf        59: class Measure_file_reader: public Measure_reader {
                     60: public:
1.90.2.3  paf        61:        Measure_file_reader(Pool& apool, int af, const String& afile_name, const char* afname): 
1.77      paf        62:                pool(apool), file_name(afile_name), fname(afname), f(af) {
1.1       paf        63:        }
                     64: 
1.77      paf        65:        /*override*/size_t read(const void *&abuf, size_t limit) {
                     66:                if(limit==0)
1.1       paf        67:                        return 0;
1.77      paf        68: 
                     69:                void *lbuf=pool.malloc(limit);
                     70:                size_t read_size=(size_t)::read(f, lbuf, limit);  abuf=lbuf;
                     71:                if(ssize_t(read_size)<0 || read_size>limit)
                     72:                        throw Exception(0,
                     73:                                &file_name, 
                     74:                                "measure failed: actually read %lu bytes count not in [0..%lu] valid range", 
                     75:                        read_size, limit);
                     76: 
1.1       paf        77:                return read_size;
                     78:        }
                     79: 
1.79      paf        80:        /*override*/void seek(long value, int whence) {
                     81:                if(lseek(f, value, whence)<0)
1.88      paf        82:                        throw Exception("image.format",
1.77      paf        83:                                &file_name, 
1.79      paf        84:                                "seek(value=%ld, whence=%d) failed: %s (%d), actual filename '%s'", 
                     85:                                        value, whence, strerror(errno), errno, fname);
1.77      paf        86:        }
                     87: 
1.81      paf        88:        /*override*/long tell() { return lseek(f, 0, SEEK_CUR); }
1.79      paf        89: 
1.1       paf        90: private:
1.77      paf        91:        Pool& pool;
1.90.2.3  paf        92:        const String& file_name; const char* fname;
1.77      paf        93:        int f;
                     94: };
                     95: 
                     96: class Measure_buf_reader: public Measure_reader {
                     97: public:
                     98:        Measure_buf_reader(const void *abuf, size_t asize, const String& afile_name): 
                     99:                buf(abuf), size(asize), file_name(afile_name), offset(0) {
                    100:        }
                    101:        
                    102:        /*override*/size_t read(const void *&abuf, size_t limit) {
                    103:                size_t to_read=min(limit, size-offset);
                    104:                abuf=(const char*)buf+offset;
                    105:                offset+=to_read;
                    106:                return to_read;
                    107:        }
                    108: 
1.79      paf       109:        /*override*/void seek(long value, int whence) {
                    110:                size_t new_offset;
                    111:                switch(whence) {
                    112:                case SEEK_CUR: new_offset=offset+value; break;
                    113:                case SEEK_SET: new_offset=(size_t)value; break;
1.88      paf       114:                default: throw Exception(0, 0, "whence #%d not supported", 0, whence); break; // never
1.79      paf       115:                }
                    116:                
1.77      paf       117:                if((ssize_t)new_offset<0 || new_offset>size)
1.88      paf       118:                        throw Exception("image.format",
1.77      paf       119:                                &file_name, 
1.79      paf       120:                                "seek(value=%l, whence=%d) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0", 
                    121:                                        value, whence, new_offset, size);
1.77      paf       122:                offset=new_offset;
                    123:        }
                    124: 
1.79      paf       125:        /*override*/long tell() { return offset; }
                    126: 
1.77      paf       127: private:
                    128: 
                    129:        const void *buf; size_t size;
                    130:        const String& file_name; 
1.1       paf       131: 
                    132:        size_t offset;
                    133: };
1.77      paf       134: 
1.43      parser    135: #endif
1.1       paf       136: 
1.72      paf       137: /// PNG file header
                    138: struct PNG_Header {
                    139:        char dummy[12];
                    140:        char signature[4]; //< must be "IHDR"
1.80      paf       141:        uchar high_width[2]; //< image width high bytes [we ignore for now]
                    142:        uchar width[2]; //< image width low bytes
                    143:        uchar high_height[2]; //< image height high bytes [we ignore for now]
                    144:        uchar height[4]; //< image height
1.72      paf       145: };
                    146: 
1.21      paf       147: /// GIF file header
1.1       paf       148: struct GIF_Header {
1.72      paf       149:        char       signature[3];         // 'GIF'
1.1       paf       150:        char       version[3];
1.80      paf       151:        uchar       width[2];
                    152:        uchar       height[2];
1.1       paf       153:        char       dif;
                    154:        char       fonColor;
                    155:        char       nulls;
                    156: };
                    157: 
1.31      parser    158: /// JPEG record head
                    159: struct JPG_Segment_head {
1.80      paf       160:        uchar marker;
                    161:        uchar code;
                    162:        uchar length[2];
1.1       paf       163: };
1.21      paf       164: /// JPEG frame header
1.31      parser    165: struct JPG_Size_segment_body {
1.27      parser    166:        char data;                    //< data precision of bits/sample
1.80      paf       167:        uchar height[2];               //< image height
                    168:        uchar width[2];                //< image width
1.27      parser    169:        char numComponents;           //< number of color components
1.1       paf       170: };
                    171: 
1.79      paf       172: /// JPEG frame header
                    173: struct JPG_Exif_segment_start {
                    174:        char signature[6]; // Exif\0\0
                    175: };
                    176: 
1.80      paf       177: /// JPEG Exif TIFF Header
                    178: struct JPG_Exif_TIFF_header {
                    179:        uchar byte_align_identifier[2];
                    180:        char dummy[2]; // always 000A [or 0A00]
                    181:        uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
                    182: };
                    183: 
                    184: // JPEG Exif IFD start
                    185: struct JPG_Exif_IFD_start {
                    186:        uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
                    187: };
                    188: 
                    189: // TTTT ffff NNNNNNNN DDDDDDDD 
                    190: struct JPG_Exif_IFD_entry {
                    191:        uchar tag[2]; // Tag number, this shows a kind of data
                    192:        uchar format[2]; // data format
                    193:        uchar components_count[4]; // number of components
                    194:        uchar value_or_offset_to_it[4]; // data value or offset to data value
                    195: };
                    196: 
                    197: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
                    198: 
1.89      paf       199: #define JPEG_EXIF_DATE_CHARS 20
                    200: 
1.1       paf       201: //
                    202: 
1.80      paf       203: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
                    204:        return (ushort)((b1<<8) + b0);
1.33      parser    205: }
                    206: 
1.80      paf       207: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
                    208:        return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
1.33      parser    209: }
                    210: 
1.80      paf       211: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
                    212:        return is_big?x_endian_to_ushort(b[1], b[0]):
                    213:                x_endian_to_ushort(b[0], b[1]);
1.1       paf       214: }
                    215: 
1.80      paf       216: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
                    217:        return is_big?x_endian_to_uint(b[3], b[2], b[1], b[0]):
                    218:                x_endian_to_uint(b[0], b[1], b[2], b[3]);
                    219: }
                    220: 
                    221: static void measure_gif(Pool& pool, const String *origin_string, 
1.78      paf       222:                         Measure_reader& reader, ushort& width, ushort& height) {
1.1       paf       223: 
1.83      paf       224:        const void *buf;
1.1       paf       225:        const int head_size=sizeof(GIF_Header);
                    226:        if(reader.read(buf, head_size)<head_size)
1.68      paf       227:                throw Exception("image.format", 
1.1       paf       228:                        origin_string, 
1.34      parser    229:                        "not GIF file - too small");
1.31      parser    230:        GIF_Header *head=(GIF_Header *)buf;
1.1       paf       231: 
1.72      paf       232:        if(strncmp(head->signature, "GIF", 3)!=0)
1.68      paf       233:                throw Exception("image.format", 
1.1       paf       234:                        origin_string, 
1.44      parser    235:                        "not GIF file - wrong signature");      
1.1       paf       236: 
1.80      paf       237:        width=endian_to_ushort(false, head->width);
                    238:        height=endian_to_ushort(false, head->height);
                    239: }
                    240: 
                    241: static Value *parse_IFD_entry_formatted_one_value(Pool& pool,
                    242:                                                                                                  bool is_big,
                    243:                                                                                                  ushort format, 
                    244:                                                                                                  size_t component_size, 
                    245:                                                                                                  const uchar *value) {
                    246:        switch(format) {
                    247:        case 1: // unsigned byte
                    248:                return new(pool) VInt(pool, (uchar)value[0]);
                    249:        case 3: // unsigned short
                    250:                return new(pool) VInt(pool, endian_to_ushort(is_big, value));
                    251:        case 4: // unsigned long
                    252:                 // 'double' because parser's Int is signed
                    253:                return new(pool) VDouble(pool, endian_to_uint(is_big, value));
                    254:        case 5: // unsigned rational
                    255:                {
                    256:                        uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
                    257:                        uint denominator=endian_to_uint(is_big, value);
                    258:                        if(!denominator)
                    259:                                return 0;
                    260:                        return new(pool) VDouble(pool, ((double)numerator)/denominator);
                    261:                }
                    262:        case 6: // signed byte
                    263:                return new(pool) VInt(pool, (signed char)value[0]);
                    264:        case 8: // signed short
                    265:                return new(pool) VInt(pool, (signed short)endian_to_ushort(is_big, value));
                    266:        case 9: // signed long
                    267:                return new(pool) VInt(pool, (signed int)endian_to_uint(is_big, value));
                    268:        case 10: // signed rational
                    269:                {
                    270:                        signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
                    271:                        uint denominator=endian_to_uint(is_big, value);
                    272:                        if(!denominator)
                    273:                                return 0;
                    274:                        return new(pool) VDouble(pool, numerator/denominator);
                    275:                }
                    276:                /*
                    277:        case 11: // single float
                    278:                todo
                    279:        case 12: // double float
                    280:                todo
                    281:                */
                    282:        };      
                    283:        
                    284:        return 0;
                    285: }
                    286: 
1.89      paf       287: // date.C
                    288: time_t cstr_to_time_t(char *cstr, const String *report_error_origin);
                    289: 
1.80      paf       290: static Value *parse_IFD_entry_formatted_value(Pool& pool,
                    291:                                                                                          bool is_big, ushort format, 
                    292:                                                                                          size_t component_size, uint components_count, 
                    293:                                                                                          const uchar *value) {
                    294:        if(format==2) { // ascii string, exception: the only type with varying size
1.90.2.3  paf       295:                const char* cstr=(const char* )value;
1.80      paf       296:                size_t size=components_count;
1.89      paf       297:                // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
                    298:                if(size==JPEG_EXIF_DATE_CHARS 
                    299:                        && isdigit(cstr[0])
                    300:                        && cstr[JPEG_EXIF_DATE_CHARS-1]==0) {
                    301:                        char cstr_writable[JPEG_EXIF_DATE_CHARS]; 
                    302:                        strcpy(cstr_writable, cstr);
                    303: 
                    304:                        time_t t=cstr_to_time_t(cstr_writable, 0/* do not throw exception, just return bad result */);
                    305:                        if(t>=0)
                    306:                                return new(pool) VDate(pool, t);
                    307:                }
                    308: 
1.90.2.3  paf       309:                if(const char* premature_zero_pos=(const char* )memchr(cstr, 0, size))
1.80      paf       310:                        size=premature_zero_pos-cstr;
                    311:                return new(pool) VString(*new(pool) String(pool, cstr, size, true/*tainted*/));
                    312:        }
                    313: 
                    314:        if(components_count==1)
                    315:                return parse_IFD_entry_formatted_one_value(pool, is_big, format, component_size, value);
                    316: 
                    317:        VHash& result=*new(pool) VHash(pool);
                    318:        Hash& hash=result.hash(0);
                    319:        for(uint i=0; i<components_count; i++, value+=component_size) {
                    320:                String& skey=*new(pool) String(pool);
                    321:                {
                    322:                        char *buf=(char *)pool.malloc(MAX_NUMBER);
                    323:                        snprintf(buf, MAX_NUMBER, "%d", i);
                    324:                        skey << buf;
                    325:                }
                    326:                hash.put(skey, parse_IFD_entry_formatted_one_value(pool, is_big, format, component_size, value));
                    327:        }
                    328: 
                    329:        return &result;
                    330: }
                    331: 
                    332: static Value *parse_IFD_entry_value(Pool& pool,
                    333:                                                                        bool is_big, Measure_reader& reader, long tiff_base,
                    334:                                                                        JPG_Exif_IFD_entry& entry) {
                    335:        size_t format2component_size[]={
                    336:                0, // undefined
                    337:                1, // unsigned byte
                    338:                1, // ascii string
                    339:                2, // unsigned short
                    340:                4, // unsigned long
                    341:                8, // unsigned rational
                    342:                1, // signed byte
                    343:                0, // undefined
                    344:                2, // signed short
                    345:                4, // signed long
                    346:                8, // signed rational
                    347:                /*
                    348:                4, // single float
                    349:                8, // double float
                    350:                */
                    351:        };
                    352: 
                    353:        ushort format=endian_to_ushort(is_big, entry.format);
                    354:        if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
                    355:                return 0; // format out of range, ignoring
                    356: 
                    357:        size_t component_size=format2component_size[format];
                    358:        if(component_size==0)
                    359:                return 0; // undefined format
                    360: 
                    361:        // You can get the total data byte length by multiplies 
                    362:        // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
                    363:        uint components_count=endian_to_uint(is_big, entry.components_count);
                    364:        size_t value_size=component_size*components_count;
                    365:        // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
                    366:        Value *result;
                    367: 
                    368:        if(value_size<=4)
                    369:                result=parse_IFD_entry_formatted_value(pool,
                    370:                        is_big, format, 
                    371:                        component_size, components_count, 
                    372:                        entry.value_or_offset_to_it);
                    373:        else {
                    374:                long remembered=reader.tell();
                    375:                {
                    376:                        reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
                    377:                        const void *value;
                    378:                        if(reader.read(value, value_size)<sizeof(value_size))
                    379:                                return 0;
                    380:                        result=parse_IFD_entry_formatted_value(pool,
                    381:                                is_big, format, 
                    382:                                component_size, components_count, 
                    383:                                (const uchar*)value);
                    384:                }
                    385:                reader.seek(remembered, SEEK_SET);
                    386:        }
                    387: 
                    388:        return result;
                    389: }
                    390: 
                    391: static void parse_IFD(Pool& pool,
                    392:                                          Hash& hash,
                    393:                                          bool is_big, Measure_reader& reader, long tiff_base);
                    394: 
                    395: static void parse_IFD_entry(Pool& pool, Hash& hash,
                    396:                                                        bool is_big, Measure_reader& reader, long tiff_base,
                    397:                                                        JPG_Exif_IFD_entry& entry) {
                    398:        ushort tag=endian_to_ushort(is_big, entry.tag);
                    399:        if(tag==JPG_IFD_TAG_EXIF_OFFSET) {
                    400:                long remembered=reader.tell();
                    401:                {
                    402:                        reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
                    403:                        parse_IFD(pool, hash, is_big, reader, tiff_base);
                    404:                }
                    405:                reader.seek(remembered, SEEK_SET);
                    406:                return;
                    407:        }
                    408:        
                    409:        if(Value *value=parse_IFD_entry_value(pool, is_big, reader, tiff_base, entry)) {
                    410:                String& skey=*new(pool) String(pool);
                    411:                {
                    412:                        char *buf=(char *)pool.malloc(MAX_NUMBER);
                    413:                        snprintf(buf, MAX_NUMBER, "%u", tag);
                    414:                        skey << buf;
                    415:                }
                    416: 
1.90.2.3  paf       417:                if(const char* name=(const char* )exif_tag_value2name->get(skey))
1.80      paf       418:                        hash.put(*new(pool) String(pool, name), value);
                    419:                else
                    420:                        hash.put(skey, value);
                    421:        }
1.1       paf       422: }
                    423: 
1.80      paf       424: static void parse_IFD(Pool& pool,
                    425:                                          Hash& hash,
                    426:                                          bool is_big, Measure_reader& reader, long tiff_base) {
                    427:        const void *buf;
                    428:        if(reader.read(buf, sizeof(JPG_Exif_IFD_start))<sizeof(JPG_Exif_IFD_start))
                    429:                return;
                    430:        JPG_Exif_IFD_start *start=(JPG_Exif_IFD_start *)buf;
                    431: 
                    432:        ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
                    433:        for(int i=0; i<directory_entry_count; i++) {
                    434:                if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
                    435:                        return;
                    436: 
                    437:                parse_IFD_entry(pool, hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf);
                    438:        }
                    439:        // then goes: LLLLLLLL Offset to next IFD [not going there]
                    440: }
                    441: 
                    442: static Value *parse_exif(Pool& pool,
                    443:                                           Measure_reader& reader,
                    444:                                           const String *origin_string) {
                    445:        const void *buf;
                    446:        if(reader.read(buf, sizeof(JPG_Exif_segment_start))<sizeof(JPG_Exif_segment_start))
                    447:                throw Exception("image.format", 
                    448:                        origin_string, 
                    449:                        "not JPEG file - can not fully read Exif segment start");
                    450: 
                    451:        JPG_Exif_segment_start *start=(JPG_Exif_segment_start *)buf;
                    452:        if(memcmp(start->signature, "Exif\0\0", 4+2)!=0) //signature invalid?
                    453:                return 0; // ignore invalid block
                    454: 
                    455:        uint tiff_base=reader.tell();
                    456:        if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
                    457:                return 0;
                    458: 
                    459:        JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
                    460:        bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
                    461: 
                    462:        uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
                    463:        reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
                    464: 
                    465:        VHash& vhash=*new(pool) VHash(pool);
                    466: 
                    467:        // IFD
                    468:        parse_IFD(pool, vhash.hash(0), is_big, reader, tiff_base);
                    469: 
                    470:        return &vhash;
                    471: }
                    472: 
                    473: static void measure_jpeg(Pool& pool, const String *origin_string, 
                    474:                         Measure_reader& reader, ushort& width, ushort& height, Value ** exif) {
1.2       paf       475:        // JFIF format markers
1.80      paf       476:        const uchar MARKER=0xFF;
                    477:        const uchar CODE_SIZE_A=0xC0;
                    478:        const uchar CODE_SIZE_B=0xC1;
                    479:        const uchar CODE_SIZE_C=0xC2;
                    480:        const uchar CODE_SIZE_D=0xC3;
                    481:        const uchar CODE_EXIF=0xE1;
1.2       paf       482: 
1.83      paf       483:        const void *buf;
1.18      paf       484:        const size_t prefix_size=2;
1.31      parser    485:        if(reader.read(buf, prefix_size)<prefix_size)
1.68      paf       486:                throw Exception("image.format", 
1.1       paf       487:                        origin_string, 
1.34      parser    488:                        "not JPEG file - too small");
1.80      paf       489:        uchar *signature=(uchar *)buf;
1.1       paf       490:        
1.31      parser    491:        if(!(signature[0]==0xFF && signature[1]==0xD8)) 
1.68      paf       492:                throw Exception("image.format", 
1.1       paf       493:                        origin_string, 
1.44      parser    494:                        "not JPEG file - wrong signature");
1.31      parser    495: 
                    496:        while(true) {
1.80      paf       497:                uint segment_base=reader.tell()+2/*marker,code*/;
1.31      parser    498:                if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79      paf       499:                        break;
1.31      parser    500:                JPG_Segment_head *head=(JPG_Segment_head *)buf;
                    501: 
                    502:         // Verify that it's a valid segment.
                    503:                if(head->marker!=MARKER)
1.79      paf       504:                        throw Exception("image.format", 
                    505:                                origin_string, 
                    506:                                "not JPEG file - marker not found");
                    507: 
                    508:                switch(head->code) {
                    509:                // http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
                    510:                case CODE_EXIF:
1.80      paf       511:                        if(exif && !*exif) // seen .jpg with some xml under EXIF tag, after real exif block :)
                    512:                                *exif=parse_exif(pool, reader, origin_string);
1.1       paf       513:                        break;
1.31      parser    514: 
1.79      paf       515:                case CODE_SIZE_A:
                    516:                case CODE_SIZE_B:
                    517:                case CODE_SIZE_C:
                    518:                case CODE_SIZE_D:
                    519:                        {
                    520:                                // Segments that contain size info
                    521:                                if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
                    522:                                        throw Exception("image.format", 
                    523:                                                origin_string, 
                    524:                                                "not JPEG file - can not fully read Size segment");
                    525:                                JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
                    526:                                
1.80      paf       527:                                width=endian_to_ushort(true, body->width);
                    528:                                height=endian_to_ushort(true, body->height);
1.79      paf       529:                        }                       
1.80      paf       530:                        return;
1.79      paf       531:                };
                    532: 
1.80      paf       533:                reader.seek(segment_base+endian_to_ushort(true, head->length), SEEK_SET);
1.31      parser    534:        }
                    535: 
1.79      paf       536:        throw Exception("image.format", 
                    537:                origin_string, 
                    538:                "broken JPEG file - size frame not found");
1.1       paf       539: }
                    540: 
1.80      paf       541: static void measure_png(Pool& pool, const String *origin_string, 
1.78      paf       542:                         Measure_reader& reader, ushort& width, ushort& height) {
1.72      paf       543: 
1.83      paf       544:        const void *buf;
1.72      paf       545:        const int head_size=sizeof(PNG_Header);
                    546:        if(reader.read(buf, head_size)<head_size)
                    547:                throw Exception("image.format", 
                    548:                        origin_string, 
                    549:                        "not PNG file - too small");
                    550:        PNG_Header *head=(PNG_Header *)buf;
                    551: 
                    552:        if(strncmp(head->signature, "IHDR", 4)!=0)
                    553:                throw Exception("image.format", 
                    554:                        origin_string, 
                    555:                        "not PNG file - wrong signature");      
                    556: 
1.80      paf       557:        width=endian_to_ushort(true, head->width);
                    558:        height=endian_to_ushort(true, head->height);
1.72      paf       559: }
                    560: 
1.1       paf       561: // measure center
                    562: 
1.80      paf       563: static void measure(Pool& pool, const String& file_name, 
                    564:                         Measure_reader& reader, ushort& width, ushort& height, Value ** exif) {
1.90.2.3  paf       565:        if(const char* cext=strrchr(file_name.cstr(String::UL_FILE_SPEC), '.')) {
1.1       paf       566:                cext++;
                    567:                if(strcasecmp(cext, "GIF")==0)
                    568:                        measure_gif(pool, &file_name, reader, width, height);
                    569:                else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) 
1.80      paf       570:                        measure_jpeg(pool, &file_name, reader, width, height, exif);
1.72      paf       571:                else if(strcasecmp(cext, "PNG")==0)
                    572:                        measure_png(pool, &file_name, reader, width, height);
1.1       paf       573:                else
1.68      paf       574:                        throw Exception("image.format", 
1.1       paf       575:                                &file_name, 
                    576:                                "unhandled image file name extension '%s'", cext);
                    577:        } else
1.68      paf       578:                throw Exception("image.format", 
1.1       paf       579:                        &file_name, 
                    580:                        "can not determine image type - no file name extension");
                    581: }
                    582: 
1.77      paf       583: // methods
1.1       paf       584: 
1.40      parser    585: #ifndef DOXYGEN
1.77      paf       586: struct File_measure_action_info {
1.78      paf       587:        ushort *width;
                    588:        ushort *height;
1.80      paf       589:        Value ** exif;
1.77      paf       590:        const String *file_name;
1.1       paf       591: };
1.40      parser    592: #endif
1.77      paf       593: static void file_measure_action(Pool& pool,
1.80      paf       594:                                                                struct stat& finfo, int f, 
1.90.2.3  paf       595:                                                                const String& file_spec, const char* fname, bool as_text,
1.77      paf       596:                                                                void *context) {
                    597:        File_measure_action_info& info=*static_cast<File_measure_action_info *>(context);
1.1       paf       598: 
1.77      paf       599:        Measure_file_reader reader(pool, f, *info.file_name, fname);
1.80      paf       600:        measure(pool, *info.file_name, reader, *info.width, *info.height, info.exif);
1.1       paf       601: }
                    602: 
1.90.2.4  paf       603: static void _measure(Request& r, StringPtr method_name, MethodParams& params) {
1.1       paf       604:        Pool& pool=r.pool();
                    605: 
1.90.2.4  paf       606:        Value& data=params.as_no_junction(0, "data must not be code");
1.1       paf       607: 
1.78      paf       608:        ushort width=0;
                    609:        ushort height=0;
1.80      paf       610:        Value *exif=0;
1.1       paf       611:        const String *file_name;
1.77      paf       612:        if(file_name=data.get_string()) {
1.80      paf       613:                File_measure_action_info info={&width, &height, &exif, file_name};
1.77      paf       614:                file_read_action_under_lock(pool, r.absolute(*file_name), 
                    615:                        "measure", file_measure_action, &info);
1.1       paf       616:        } else {
                    617:                const VFile& vfile=*data.as_vfile();
                    618:                file_name=&static_cast<Value *>(vfile.fields().get(*name_name))->as_string();
1.77      paf       619:                Measure_buf_reader reader(
                    620:                        vfile.value_ptr(),
                    621:                        vfile.value_size(),
                    622:                        *file_name
                    623:                );
1.80      paf       624:                measure(pool, *file_name, reader, width, height, &exif);
1.1       paf       625:        }
                    626: 
1.80      paf       627:        VImage &vimage=*static_cast<VImage *>(r.get_self());
                    628:        vimage.set(file_name, width, height, 0, exif);
1.1       paf       629: }
                    630: 
1.40      parser    631: #ifndef DOXYGEN
1.4       paf       632: struct Attrib_info {
1.21      paf       633:        String *tag; ///< html tag being constructed
                    634:        Hash *skip; ///< tag attributes not to append to tag string [to skip]
1.4       paf       635: };
1.40      parser    636: #endif
1.3       paf       637: static void append_attrib_pair(const Hash::Key& key, Hash::Val *val, void *info) {
1.4       paf       638:        Attrib_info& ai=*static_cast<Attrib_info *>(info);
                    639: 
1.49      parser    640:        // skip user-specified and internal(starting with "line-") attributes 
                    641:        if(ai.skip && ai.skip->get(key) || key.pos("line-")==0)
1.4       paf       642:                return;
                    643: 
1.3       paf       644:        Value& value=*static_cast<Value *>(val);
                    645:        // src="a.gif" width=123 ismap[=-1]
1.4       paf       646:        *ai.tag << " " << key;
1.26      parser    647:        if(value.is_string() || value.as_int()>=0)
1.6       paf       648:                *ai.tag << "=\"" << value.as_string() << "\"";
1.3       paf       649: }
1.90.2.4  paf       650: static void _html(Request& r, StringPtr method_name, MethodParams& params) {
1.3       paf       651:        Pool& pool=r.pool();
                    652: 
                    653:        String tag(pool);
                    654:        tag << "<img";
1.4       paf       655: 
1.76      paf       656:        const Hash& fields=static_cast<VImage *>(r.get_self())->fields();
1.5       paf       657:        Hash *attribs=0;
1.4       paf       658: 
1.90.2.4  paf       659:        if(params.count()) {
1.69      paf       660:                // for backward compatibility: someday was ^html{}
1.90.2.4  paf       661:                Value &vattribs=r.process_to_value(params.get(0),
1.70      paf       662:                        /*0/*no name* /,*/
1.52      parser    663:                        false/*don't intercept string*/);
1.82      paf       664:                if(!vattribs.is_string()) // allow empty
1.59      parser    665:                        if(attribs=vattribs.get_hash(&method_name)) {
1.39      parser    666:                                Attrib_info attrib_info={&tag, 0};
                    667:                                attribs->for_each(append_attrib_pair, &attrib_info);
                    668:                        } else
1.68      paf       669:                                throw Exception("parser.runtime", 
1.39      parser    670:                                        &method_name, 
                    671:                                        "attributes must be hash");
                    672:        }
1.4       paf       673: 
1.5       paf       674:        Attrib_info attrib_info={&tag, attribs};
1.4       paf       675:        fields.for_each(append_attrib_pair, &attrib_info);
1.6       paf       676:        tag << " />";
1.3       paf       677:        r.write_pass_lang(tag);
                    678: }
1.8       paf       679: 
1.68      paf       680: /// @test wrap FILE to auto-object
1.16      paf       681: static gdImage *load(Request& r, const String& method_name, 
                    682:                                         const String& file_name){
                    683:        Pool& pool=r.pool();
                    684: 
1.90.2.3  paf       685:        const char* file_name_cstr=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
1.16      paf       686:        if(FILE *f=fopen(file_name_cstr, "rb")) {
                    687:                gdImage& image=*new(pool) gdImage(pool);
1.23      paf       688:                bool ok=image.CreateFromGif(f);
1.16      paf       689:                fclose(f);
1.23      paf       690:                if(!ok)
1.68      paf       691:                        throw Exception("image.format", 
1.23      paf       692:                                &file_name,
                    693:                                "is not in GIF format");
1.16      paf       694:                return &image;
                    695:        } else {
1.68      paf       696:                throw Exception("file.missing", 
1.16      paf       697:                        &method_name, 
                    698:                        "can not open '%s'", file_name_cstr);
                    699:                return 0;
                    700:        }
                    701: }
                    702: 
                    703: 
1.90.2.4  paf       704: static void _load(Request& r, StringPtr method_name, MethodParams& params) {
1.6       paf       705:        Pool& pool=r.pool();
                    706: 
1.90.2.4  paf       707:        const String& file_name=params.as_string(0, "file name must not be code");
1.6       paf       708: 
1.16      paf       709:        gdImage& image=*load(r, method_name, file_name);
                    710:        int width=image.SX();
                    711:        int height=image.SY();
1.76      paf       712:        static_cast<VImage *>(r.get_self())->set(&file_name, width, height, &image);
1.6       paf       713: }
                    714: 
1.90.2.4  paf       715: static void _create(Request& r, StringPtr method_name, MethodParams& params) {
1.6       paf       716:        Pool& pool=r.pool();
                    717: 
1.90.2.4  paf       718:        int width=params.as_int(0, "width must be int", r);
                    719:        int height=params.as_int(1, "height must be int", r);
1.8       paf       720:        int bgcolor_value=0xffFFff;
1.90.2.4  paf       721:        if(params.count()>2)
                    722:                bgcolor_value=params.as_int(2, "color must be int", r);
1.15      paf       723:        gdImage& image=*new(pool) gdImage(pool);
                    724:        image.Create(width, height);
                    725:        image.FilledRectangle(0, 0, width-1, height-1, image.Color(bgcolor_value));
1.76      paf       726:        static_cast<VImage *>(r.get_self())->set(0, width, height, &image);
1.6       paf       727: }
                    728: 
1.90.2.4  paf       729: static void _gif(Request& r, StringPtr method_name, MethodParams& params) {
1.6       paf       730:        Pool& pool=r.pool();
                    731: 
1.76      paf       732:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.8       paf       733:        if(!image)
1.68      paf       734:                throw Exception(0, 
1.16      paf       735:                        &method_name, 
1.12      paf       736:                        "does not contain an image");
1.6       paf       737: 
                    738:        // could _ but don't thing it's wise to use $image.src for vfile.name
1.10      paf       739: 
1.16      paf       740:        String out(pool); image->Gif(out);
1.6       paf       741:        
                    742:        VFile& vfile=*new(pool) VFile(pool);
1.41      parser    743:        Value *content_type=new(pool) VString(*new(pool) String(pool, "image/gif"));
1.20      paf       744:        vfile.set(false/*not tainted*/, 
1.61      paf       745:                out.cstr(), out.size(), 0, content_type);
1.6       paf       746: 
                    747:        r.write_no_lang(vfile);
                    748: }
                    749: 
1.90.2.4  paf       750: static void _line(Request& r, StringPtr method_name, MethodParams& params) {
1.12      paf       751:        Pool& pool=r.pool();
                    752: 
1.76      paf       753:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.12      paf       754:        if(!image)
1.68      paf       755:                throw Exception(0, 
1.16      paf       756:                        &method_name, 
1.12      paf       757:                        "does not contain an image");
                    758: 
                    759:        image->Line(
1.90.2.4  paf       760:                params.as_int(0, "x0 must be int", r), 
                    761:                params.as_int(1, "y0 must be int", r), 
                    762:                params.as_int(2, "x1 must be int", r), 
                    763:                params.as_int(3, "y1 must be int", r), 
                    764:                image->Color(params.as_int(4, "color must be int", r)));
1.13      paf       765: }
                    766: 
1.90.2.4  paf       767: static void _fill(Request& r, StringPtr method_name, MethodParams& params) {
1.13      paf       768:        Pool& pool=r.pool();
                    769: 
1.76      paf       770:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.13      paf       771:        if(!image)
1.68      paf       772:                throw Exception(0, 
1.16      paf       773:                        &method_name, 
1.13      paf       774:                        "does not contain an image");
1.12      paf       775: 
1.13      paf       776:        image->Fill(
1.90.2.4  paf       777:                params.as_int(0, "x must be int", r), 
                    778:                params.as_int(1, "y must be int", r), 
                    779:                image->Color(params.as_int(2, "color must be int", r)));
1.13      paf       780: }
                    781: 
1.90.2.4  paf       782: static void _rectangle(Request& r, StringPtr method_name, MethodParams& params) {
1.13      paf       783:        Pool& pool=r.pool();
                    784: 
1.76      paf       785:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.13      paf       786:        if(!image)
1.68      paf       787:                throw Exception(0, 
1.16      paf       788:                        &method_name, 
1.13      paf       789:                        "does not contain an image");
                    790: 
                    791:        image->Rectangle(
1.90.2.4  paf       792:                params.as_int(0, "x0 must be int", r), 
                    793:                params.as_int(1, "y0 must be int", r), 
                    794:                params.as_int(2, "x1 must be int", r), 
                    795:                params.as_int(3, "y1 must be int", r), 
                    796:                image->Color(params.as_int(4, "color must be int", r)));
1.13      paf       797: }
                    798: 
1.90.2.4  paf       799: static void _bar(Request& r, StringPtr method_name, MethodParams& params) {
1.13      paf       800:        Pool& pool=r.pool();
                    801: 
1.76      paf       802:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.13      paf       803:        if(!image)
1.68      paf       804:                throw Exception(0, 
1.16      paf       805:                        &method_name, 
1.13      paf       806:                        "does not contain an image");
                    807: 
                    808:        image->FilledRectangle(
1.90.2.4  paf       809:                params.as_int(0, "x0 must be int", r), 
                    810:                params.as_int(1, "y0 must be int", r), 
                    811:                params.as_int(2, "x1 must be int", r), 
                    812:                params.as_int(3, "y1 must be int", r), 
                    813:                image->Color(params.as_int(4, "color must be int", r)));
1.13      paf       814: }
                    815: 
1.44      parser    816: #ifndef DOXYGEN
                    817: static void add_point(Array::Item *value, void *info) {
                    818:        Array& row=*static_cast<Array *>(value);
                    819:        gdImage::Point **p=static_cast<gdImage::Point **>(info);
                    820:        
                    821:        (**p).x=row.get_string(0)->as_int();
                    822:        (**p).y=row.get_string(1)->as_int();
                    823:        (*p)++;
                    824: }
                    825: #endif
1.90.2.4  paf       826: static void _replace(Request& r, StringPtr method_name, MethodParams& params) {
1.13      paf       827:        Pool& pool=r.pool();
                    828: 
1.76      paf       829:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.13      paf       830:        if(!image)
1.68      paf       831:                throw Exception(0, 
1.16      paf       832:                        &method_name, 
1.13      paf       833:                        "does not contain an image");
                    834: 
1.90.2.4  paf       835:        Table *table=params.as_no_junction(2, "coordinates must not be code").get_table();
1.44      parser    836:        if(!table) 
1.68      paf       837:                throw Exception(0,
1.44      parser    838:                        &method_name,
                    839:                        "coordinates must be table");
1.13      paf       840: 
1.44      parser    841:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    842:        gdImage::Point *add_p=all_p;    
                    843:        table->for_each(add_point, &add_p);
                    844:        image->FilledPolygonReplaceColor(all_p, table->size(), 
1.90.2.4  paf       845:                image->Color(params.as_int(0, "src color must be int", r)),
                    846:                image->Color(params.as_int(1, "dest color must be int", r)));
1.13      paf       847: }
                    848: 
1.90.2.4  paf       849: static void _polyline(Request& r, StringPtr method_name, MethodParams& params) {
1.13      paf       850:        Pool& pool=r.pool();
                    851: 
1.76      paf       852:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.13      paf       853:        if(!image)
1.68      paf       854:                throw Exception(0, 
1.16      paf       855:                        &method_name, 
1.13      paf       856:                        "does not contain an image");
                    857: 
1.90.2.4  paf       858:        Table *table=params.as_no_junction(1, "coordinates must not be code").get_table();
1.44      parser    859:        if(!table) 
1.68      paf       860:                throw Exception(0,
1.44      parser    861:                        &method_name,
                    862:                        "coordinates must be table");
                    863: 
                    864:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    865:        gdImage::Point *add_p=all_p;    
                    866:        table->for_each(add_point, &add_p);
                    867:        image->Polygon(all_p, table->size(), 
1.90.2.4  paf       868:                image->Color(params.as_int(0, "color must be int", r)),
1.44      parser    869:                false/*not closed*/);
                    870: }
                    871: 
1.90.2.4  paf       872: static void _polygon(Request& r, StringPtr method_name, MethodParams& params) {
1.44      parser    873:        Pool& pool=r.pool();
                    874: 
1.76      paf       875:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.44      parser    876:        if(!image)
1.68      paf       877:                throw Exception(0, 
1.16      paf       878:                        &method_name, 
1.44      parser    879:                        "does not contain an image");
1.13      paf       880: 
1.90.2.4  paf       881:        Table *table=params.as_no_junction(1, "coordinates must not be code").get_table();
1.44      parser    882:        if(!table) 
1.68      paf       883:                throw Exception(0,
1.44      parser    884:                        &method_name,
                    885:                        "coordinates must be table");
                    886: 
                    887:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    888:        gdImage::Point *add_p=all_p;    
                    889:        table->for_each(add_point, &add_p);
                    890:        image->Polygon(all_p, table->size(), 
1.90.2.4  paf       891:                image->Color(params.as_int(0, "color must be int", r)));
1.13      paf       892: }
                    893: 
1.90.2.4  paf       894: static void _polybar(Request& r, StringPtr method_name, MethodParams& params) {
1.13      paf       895:        Pool& pool=r.pool();
                    896: 
1.76      paf       897:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.13      paf       898:        if(!image)
1.68      paf       899:                throw Exception(0, 
1.16      paf       900:                        &method_name, 
1.13      paf       901:                        "does not contain an image");
                    902: 
1.90.2.4  paf       903:        Table *table=params.as_no_junction(1, "coordinates must not be code").get_table();
1.44      parser    904:        if(!table) 
1.68      paf       905:                throw Exception("parser.runtime",
1.44      parser    906:                        &method_name,
                    907:                        "coordinates must be table");
1.13      paf       908: 
1.44      parser    909:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    910:        gdImage::Point *add_p=all_p;    
                    911:        table->for_each(add_point, &add_p);
                    912:        image->FilledPolygon(all_p, table->size(), 
1.90.2.4  paf       913:                image->Color(params.as_int(0, "color must be int", r)));
1.12      paf       914: }
                    915: 
1.16      paf       916: // font
                    917: 
1.85      paf       918: #define Y(y)(y+index*height)
1.21      paf       919: 
                    920: /// simple gdImage-based font storage & text output 
1.85      paf       921: class Font: public Pooled {
1.16      paf       922: public:
                    923:        
1.38      parser    924:        const static int letter_spacing;
1.35      parser    925:        int height;         ///< Font heigth
                    926:        int monospace;      ///< Default char width
                    927:        int spacebarspace; ///< spacebar width
1.16      paf       928:        gdImage& ifont;
                    929:        const String& alphabet;
                    930:        
                    931:        Font(Pool& pool, 
                    932:                const String& aalphabet, 
1.85      paf       933:                gdImage& aifont, int aheight, int amonospace, int aspacebarspace): Pooled(pool), 
1.16      paf       934:                alphabet(aalphabet), 
1.35      parser    935:                height(aheight), monospace(amonospace),  spacebarspace(aspacebarspace),
1.16      paf       936:                ifont(aifont) {
                    937:        }
                    938:        
                    939:        /* ******************************** char ********************************** */
                    940:        
                    941:        int index_of(char ch) {
                    942:                if(ch==' ') return -1;
                    943:                return alphabet.pos(&ch, 1);
                    944:        }
                    945:        
                    946:        int index_width(int index) {
                    947:                if(index<0)
1.35      parser    948:                        return spacebarspace;
1.16      paf       949:                int tr=ifont.GetTransparent();
1.35      parser    950:                for(int x=ifont.SX()-1; x>=0; x--) {
1.86      paf       951:                        for(int y=0; y<height; y++)
1.16      paf       952:                                if(ifont.GetPixel(x, Y(y))!=tr) 
1.35      parser    953:                                        return x+1;
1.16      paf       954:                }
                    955:                return 0;
                    956:        }
                    957:        
                    958:        void index_display(gdImage& image, int x, int y, int index){
                    959:                if(index>=0) 
1.85      paf       960:                        ifont.Copy(image, x, y, 0, Y(0), index_width(index), height);
1.16      paf       961:        }
                    962:        
                    963:        /* ******************************** string ********************************** */
1.47      parser    964:        
1.87      paf       965:        int step_width(int index) {
                    966:                return letter_spacing + (monospace ? monospace : index_width(index));
                    967:        }
                    968: 
                    969:        // counts trailing letter_spacing, consider this OK. useful for contiuations
1.47      parser    970:        int string_width(const String& s){
1.90.2.3  paf       971:                const char* cstr=s.cstr();
1.16      paf       972:                int result=0;
                    973:                for(; *cstr; cstr++)
1.87      paf       974:                        result+=step_width(index_of(*cstr));
1.16      paf       975:                return result;
                    976:        }
                    977:        
                    978:        void string_display(gdImage& image, int x, int y, const String& s){
1.90.2.3  paf       979:                const char* cstr=s.cstr();
1.16      paf       980:                if(cstr) for(; *cstr; cstr++) {
                    981:                        int index=index_of(*cstr);
                    982:                        index_display(image, x, y, index);
1.87      paf       983:                        x+=step_width(index);
1.16      paf       984:                }
                    985:        }
                    986:        
                    987: };
1.38      parser    988: const int Font::letter_spacing=1;
1.35      parser    989: 
1.90.2.4  paf       990: static void _font(Request& r, StringPtr method_name, MethodParams& params) {
1.16      paf       991:        Pool& pool=r.pool();
                    992: 
1.90.2.4  paf       993:        const String& alphabet=params.as_string(0, "alphabet must not be code");
                    994:        gdImage& image=*load(r, method_name, params.as_string(1, "file_name must not be code"));
                    995:        int spacebar_width=params.as_int(2, "spacebar_width must be int", r);
1.37      parser    996:        int monospace_width;
1.90.2.4  paf       997:        if(params.count()>3) {
                    998:                monospace_width=params.as_int(3, "monospace_width must be int", r);
1.37      parser    999:                if(!monospace_width)
                   1000:                        monospace_width=image.SX();
                   1001:        } else
                   1002:                monospace_width=0;
1.16      paf      1003: 
1.37      parser   1004:        if(!alphabet.size())
1.68      paf      1005:                throw Exception("parser.runtime",
1.37      parser   1006:                        &method_name,
                   1007:                        "alphabet must not be empty");
1.84      paf      1008: 
                   1009:        if(int remainder=image.SY() % alphabet.size())
                   1010:                throw Exception("parser.runtime",
                   1011:                        &method_name,
                   1012:                        "font-file height(%d) not divisable by alphabet size(%d), remainder=%d",
                   1013:                                image.SY(), alphabet.size(), remainder);
1.37      parser   1014:        
1.76      paf      1015:        static_cast<VImage *>(r.get_self())->font=new(pool) Font(pool, 
1.37      parser   1016:                alphabet, 
1.36      parser   1017:                image, 
1.37      parser   1018:                image.SY() / alphabet.size(), monospace_width, spacebar_width);
1.16      paf      1019: }
                   1020: 
1.90.2.4  paf      1021: static void _text(Request& r, StringPtr method_name, MethodParams& params) {
1.16      paf      1022:        Pool& pool=r.pool();
                   1023: 
1.90.2.4  paf      1024:        int x=params.as_int(0, "x must be int", r);
                   1025:        int y=params.as_int(1, "y must be int", r);
                   1026:        const String& s=params.as_string(2, "text must not be code");
1.16      paf      1027: 
1.76      paf      1028:        VImage& vimage=*static_cast<VImage *>(r.get_self());
1.16      paf      1029:        if(vimage.image)
                   1030:                if(vimage.font)
                   1031:                        vimage.font->string_display(*vimage.image, x, y, s);
                   1032:                else
1.68      paf      1033:                        throw Exception("parser.runtime",
1.16      paf      1034:                                &method_name,
                   1035:                                "set the font first");
                   1036:        else
1.68      paf      1037:                throw Exception(0, 
1.16      paf      1038:                        &method_name, 
                   1039:                        "does not contain an image");
                   1040: }
                   1041: 
1.90.2.4  paf      1042: static void _length(Request& r, StringPtr method_name, MethodParams& params) {
1.47      parser   1043:        Pool& pool=r.pool();
                   1044: 
1.90.2.4  paf      1045:        const String& s=params.as_string(0, "text must not be code");
1.47      parser   1046: 
1.76      paf      1047:        VImage& vimage=*static_cast<VImage *>(r.get_self());
1.47      parser   1048:        if(vimage.image)
                   1049:                if(vimage.font) {
1.71      paf      1050:                        r.write_no_lang(*new(pool) VInt(pool, vimage.font->string_width(s)));
1.47      parser   1051:                } else
1.68      paf      1052:                        throw Exception("parser.runtime",
1.47      parser   1053:                                &method_name,
                   1054:                                "set the font first");
                   1055:        else
1.68      paf      1056:                throw Exception(0, 
1.47      parser   1057:                        &method_name, 
                   1058:                        "does not contain an image");
                   1059: }
                   1060: 
1.90.2.4  paf      1061: static void _arc(Request& r, StringPtr method_name, MethodParams& params) {
1.48      parser   1062:        Pool& pool=r.pool();
                   1063: 
1.76      paf      1064:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.48      parser   1065:        if(!image)
1.68      paf      1066:                throw Exception(0, 
1.48      parser   1067:                        &method_name, 
                   1068:                        "does not contain an image");
                   1069: 
                   1070:        image->Arc(
1.90.2.4  paf      1071:                params.as_int(0, "center_x must be int", r), 
                   1072:                params.as_int(1, "center_y must be int", r), 
                   1073:                params.as_int(2, "width must be int", r), 
                   1074:                params.as_int(3, "height must be int", r), 
                   1075:                params.as_int(4, "start degrees must be int", r), 
                   1076:                params.as_int(5, "end degrees must be int", r), 
                   1077:                image->Color(params.as_int(6, "cx must be int", r)));
1.48      parser   1078: }
                   1079: 
1.90.2.4  paf      1080: static void _sector(Request& r, StringPtr method_name, MethodParams& params) {
1.49      parser   1081:        Pool& pool=r.pool();
                   1082: 
1.76      paf      1083:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.49      parser   1084:        if(!image)
1.68      paf      1085:                throw Exception(0, 
1.49      parser   1086:                        &method_name, 
                   1087:                        "does not contain an image");
                   1088: 
                   1089:        image->Sector(
1.90.2.4  paf      1090:                params.as_int(0, "center_x must be int", r), 
                   1091:                params.as_int(1, "center_y must be int", r), 
                   1092:                params.as_int(2, "width must be int", r), 
                   1093:                params.as_int(3, "height must be int", r), 
                   1094:                params.as_int(4, "start degrees must be int", r), 
                   1095:                params.as_int(5, "end degrees must be int", r), 
                   1096:                image->Color(params.as_int(6, "color must be int", r)));
1.49      parser   1097: }
                   1098: 
1.90.2.4  paf      1099: static void _circle(Request& r, StringPtr method_name, MethodParams& params) {
1.48      parser   1100:        Pool& pool=r.pool();
                   1101: 
1.76      paf      1102:        gdImage *image=static_cast<VImage *>(r.get_self())->image;
1.48      parser   1103:        if(!image)
1.68      paf      1104:                throw Exception(0, 
1.48      parser   1105:                        &method_name, 
                   1106:                        "does not contain an image");
                   1107: 
1.90.2.4  paf      1108:        int size=params.as_int(2, "radius must be int", r)*2;
1.48      parser   1109:        image->Arc(
1.90.2.4  paf      1110:                params.as_int(0, "center_x must be int", r), 
                   1111:                params.as_int(1, "center_y must be int", r), 
1.50      parser   1112:                size, //w
                   1113:                size, //h
1.48      parser   1114:                0, //s
                   1115:                360, //e
1.90.2.4  paf      1116:                image->Color(params.as_int(3, "color must be int", r)));
1.48      parser   1117: }
                   1118: 
1.90.2.4  paf      1119: gdImage& as_image(Pool& pool, StringPtr method_name, MethodParams& params, 
1.90.2.3  paf      1120:                                                int index, const char* msg) {
1.75      paf      1121:        gdImage *src=0;
                   1122: 
1.90.2.4  paf      1123:        Value& value=params.as_no_junction(index, msg);
1.53      parser   1124: 
1.75      paf      1125:        if(Value *vimage=value.as(VIMAGE_TYPE, false)) {
                   1126:                src=static_cast<VImage *>(vimage)->image;
                   1127:                if(!src)
                   1128:                        throw Exception("parser.runtime", 
                   1129:                                &method_name, 
                   1130:                                msg);
                   1131:        } else
1.68      paf      1132:                throw Exception("parser.runtime", 
1.53      parser   1133:                        &method_name, 
                   1134:                        msg);
                   1135: 
                   1136:        return *src;
                   1137: }
                   1138: 
1.90.2.4  paf      1139: static void _copy(Request& r, StringPtr method_name, MethodParams& params) {
1.53      parser   1140:        Pool& pool=r.pool();
                   1141: 
1.76      paf      1142:        gdImage *dest=static_cast<VImage *>(r.get_self())->image;
1.53      parser   1143:        if(!dest)
1.68      paf      1144:                throw Exception(0, 
1.53      parser   1145:                        &method_name, 
                   1146:                        "self does not contain an image");
                   1147: 
                   1148:        gdImage& src=as_image(pool, method_name, params, 0, "src must be image");
                   1149: 
1.90.2.4  paf      1150:        int sx=params.as_int(1, "src_x must be int", r);
                   1151:        int sy=params.as_int(2, "src_y must be int", r);
                   1152:        int sw=params.as_int(3, "src_w must be int", r);
                   1153:        int sh=params.as_int(4, "src_h must be int", r);
                   1154:        int dx=params.as_int(5, "dest_x must be int", r);
                   1155:        int dy=params.as_int(6, "dest_y must be int", r);
                   1156:        if(params.count()>1+2+2+2) {
                   1157:                int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
                   1158:                int dh=(int)(params.count()>1+2+2+2+1?
                   1159:                        params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
                   1160:                int tolerance=params.count()>1+2+2+2+2?
                   1161:                        params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53      parser   1162: 
1.56      parser   1163:                src.CopyResampled(*dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53      parser   1164:        } else
1.54      parser   1165:                src.Copy(*dest, dx, dy, sx, sy, sw, sh);
1.53      parser   1166: }
                   1167: 
                   1168: 
1.22      paf      1169: // constructor
                   1170: 
1.71      paf      1171: MImage::MImage(Pool& apool) : Methoded(apool, "image") {
1.1       paf      1172:        // ^image:measure[DATA]
1.22      paf      1173:        add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3       paf      1174: 
1.25      paf      1175:        // ^image.html[]
                   1176:        // ^image.html[hash]
1.22      paf      1177:        add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf      1178: 
1.25      paf      1179:        // ^image.load[background.gif]
1.22      paf      1180:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf      1181: 
1.25      paf      1182:        // ^image.create[width;height] bgcolor=white
                   1183:        // ^image.create[width;height;bgcolor]
1.22      paf      1184:        add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6       paf      1185: 
1.25      paf      1186:        // ^image.gif[]
1.22      paf      1187:        add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12      paf      1188: 
1.25      paf      1189:        // ^image.line(x0;y0;x1;y1;color)
1.22      paf      1190:        add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf      1191: 
1.25      paf      1192:        // ^image.fill(x;y;color)
1.22      paf      1193:        add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13      paf      1194: 
1.25      paf      1195:        // ^image.rectangle(x0;y0;x1;y1;color)
1.22      paf      1196:        add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13      paf      1197: 
1.25      paf      1198:        // ^image.bar(x0;y0;x1;y1;color)
1.22      paf      1199:        add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13      paf      1200: 
1.44      parser   1201:        // ^image.replace(color-source;color-dest)[table x:y]
                   1202:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 3, 3);
                   1203: 
                   1204:        // ^image.polyline(color)[table x:y]
                   1205:        add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13      paf      1206: 
1.44      parser   1207:        // ^image.polygon(color)[table x:y]
                   1208:        add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13      paf      1209: 
1.44      parser   1210:        // ^image.polybar(color)[table x:y]
                   1211:        add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13      paf      1212: 
1.36      parser   1213:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
                   1214:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;width)
                   1215:        add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16      paf      1216: 
1.25      paf      1217:     // ^image.text(x;y)[text]
1.22      paf      1218:        add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47      parser   1219:        
                   1220:     // ^image.ngth[text]
                   1221:        add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16      paf      1222:        
1.48      parser   1223:        // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
                   1224:        add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49      parser   1225: 
                   1226:        // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
                   1227:        add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48      parser   1228: 
                   1229:        // ^image.circle(center x;center y;r;color)
                   1230:        add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
                   1231: 
1.56      parser   1232:        // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
                   1233:        add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.22      paf      1234: }
                   1235: 
                   1236: // global variable
                   1237: 
                   1238: Methoded *image_class;
                   1239: 
                   1240: // creator
                   1241: 
                   1242: Methoded *MImage_create(Pool& pool) {
1.90.2.1  paf      1243:        // image JPEG Exif
                   1244:        #define EXIF_TAG(tag, name) \
                   1245:                exif_tag_value2name.put(tag, #name);
                   1246:        // Tags used by IFD0 (main image)
                   1247:        EXIF_TAG(0x010e,        ImageDescription);
                   1248:        EXIF_TAG(0x010f,        Make);
                   1249:        EXIF_TAG(0x0110,        Model);
                   1250:        EXIF_TAG(0x0112,        Orientation);
                   1251:        EXIF_TAG(0x011a,        XResolution);
                   1252:        EXIF_TAG(0x011b,        YResolution);
                   1253:        EXIF_TAG(0x0128,        ResolutionUnit);
                   1254:        EXIF_TAG(0x0131,        Software);
                   1255:        EXIF_TAG(0x0132,        DateTime);
                   1256:        EXIF_TAG(0x013e,        WhitePoint);
                   1257:        EXIF_TAG(0x013f,        PrimaryChromaticities);
                   1258:        EXIF_TAG(0x0211,        YCbCrCoefficients);
                   1259:        EXIF_TAG(0x0213,        YCbCrPositioning);
                   1260:        EXIF_TAG(0x0214,        ReferenceBlackWhite);
                   1261:        EXIF_TAG(0x8298,        Copyright);
                   1262:        EXIF_TAG(0x8769,        ExifOffset);
                   1263:        // Tags used by Exif SubIFD
                   1264:        EXIF_TAG(0x829a,        ExposureTime);
                   1265:        EXIF_TAG(0x829d,        FNumber);
                   1266:        EXIF_TAG(0x8822,        ExposureProgram);
                   1267:        EXIF_TAG(0x8827,        ISOSpeedRatings);
                   1268:        EXIF_TAG(0x9000,        ExifVersion);
                   1269:        EXIF_TAG(0x9003,        DateTimeOriginal);
                   1270:        EXIF_TAG(0x9004,        DateTimeDigitized);
                   1271:        EXIF_TAG(0x9101,        ComponentsConfiguration);
                   1272:        EXIF_TAG(0x9102,        CompressedBitsPerPixel);
                   1273:        EXIF_TAG(0x9201,        ShutterSpeedValue);
                   1274:        EXIF_TAG(0x9202,        ApertureValue);
                   1275:        EXIF_TAG(0x9203,        BrightnessValue);
                   1276:        EXIF_TAG(0x9204,        ExposureBiasValue);
                   1277:        EXIF_TAG(0x9205,        MaxApertureValue);
                   1278:        EXIF_TAG(0x9206,        SubjectDistance);
                   1279:        EXIF_TAG(0x9207,        MeteringMode);
                   1280:        EXIF_TAG(0x9208,        LightSource);
                   1281:        EXIF_TAG(0x9209,        Flash);
                   1282:        EXIF_TAG(0x920a,        FocalLength);
                   1283:        EXIF_TAG(0x927c,        MakerNote);
                   1284:        EXIF_TAG(0x9286,        UserComment);
                   1285:        EXIF_TAG(0x9290,        SubsecTime);
                   1286:        EXIF_TAG(0x9291,        SubsecTimeOriginal);
                   1287:        EXIF_TAG(0x9292,        SubsecTimeDigitized);
                   1288:        EXIF_TAG(0xa000,        FlashPixVersion);
                   1289:        EXIF_TAG(0xa001,        ColorSpace);
                   1290:        EXIF_TAG(0xa002,        ExifImageWidth);
                   1291:        EXIF_TAG(0xa003,        ExifImageHeight);
                   1292:        EXIF_TAG(0xa004,        RelatedSoundFile);
                   1293:        EXIF_TAG(0xa005,        ExifInteroperabilityOffset);
                   1294:        EXIF_TAG(0xa20e,        FocalPlaneXResolution);
                   1295:        EXIF_TAG(0xa20f,        FocalPlaneYResolution);
                   1296:        EXIF_TAG(0xa210,        FocalPlaneResolutionUnit);
                   1297:        EXIF_TAG(0xa215,        ExposureIndex);
                   1298:        EXIF_TAG(0xa217,        SensingMethod);
                   1299:        EXIF_TAG(0xa300,        FileSource);
                   1300:        EXIF_TAG(0xa301,        SceneType);
                   1301:        EXIF_TAG(0xa302,        CFAPattern);
                   1302:        // Misc Tags
                   1303:        EXIF_TAG(0x00fe,        NewSubfileType);
                   1304:        EXIF_TAG(0x00ff,        SubfileType);
                   1305:        EXIF_TAG(0x012d,        TransferFunction);
                   1306:        EXIF_TAG(0x013b,        Artist);
                   1307:        EXIF_TAG(0x013d,        Predictor);
                   1308:        EXIF_TAG(0x0142,        TileWidth);
                   1309:        EXIF_TAG(0x0143,        TileLength);
                   1310:        EXIF_TAG(0x0144,        TileOffsets);
                   1311:        EXIF_TAG(0x0145,        TileByteCounts);
                   1312:        EXIF_TAG(0x014a,        SubIFDs);
                   1313:        EXIF_TAG(0x015b,        JPEGTables);
                   1314:        EXIF_TAG(0x828d,        CFARepeatPatternDim);
                   1315:        EXIF_TAG(0x828e,        CFAPattern);
                   1316:        EXIF_TAG(0x828f,        BatteryLevel);
                   1317:        EXIF_TAG(0x83bb,        IPTC/NAA);
                   1318:        EXIF_TAG(0x8773,        InterColorProfile);
                   1319:        EXIF_TAG(0x8824,        SpectralSensitivity);
                   1320:        EXIF_TAG(0x8825,        GPSInfo);
                   1321:        EXIF_TAG(0x8828,        OECF);
                   1322:        EXIF_TAG(0x8829,        Interlace);
                   1323:        EXIF_TAG(0x882a,        TimeZoneOffset);
                   1324:        EXIF_TAG(0x882b,        SelfTimerMode);
                   1325:        EXIF_TAG(0x920b,        FlashEnergy);
                   1326:        EXIF_TAG(0x920c,        SpatialFrequencyResponse);
                   1327:        EXIF_TAG(0x920d,        Noise);
                   1328:        EXIF_TAG(0x9211,        ImageNumber);
                   1329:        EXIF_TAG(0x9212,        SecurityClassification);
                   1330:        EXIF_TAG(0x9213,        ImageHistory);
                   1331:        EXIF_TAG(0x9214,        SubjectLocation);
                   1332:        EXIF_TAG(0x9215,        ExposureIndex);
                   1333:        EXIF_TAG(0x9216,        TIFF/EPStandardID);
                   1334:        EXIF_TAG(0xa20b,        FlashEnergy);
                   1335:        EXIF_TAG(0xa20c,        SpatialFrequencyResponse);
                   1336:        EXIF_TAG(0xa214,        SubjectLocation);
                   1337:        #undef EXIF_TAG
                   1338: 
1.22      paf      1339:        return image_class=new(pool) MImage(pool);
1.1       paf      1340: }

E-mail: