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

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

E-mail: