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

1.1       paf         1: /** @file
                      2:        Parser: @b image parser class.
                      3: 
1.16      paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.46      parser      5:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         6: 
1.55    ! parser      7:        $Id: image.C,v 1.54 2001/10/16 08:18:09 parser Exp $
1.31      parser      8: */
                      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"
                     27: 
1.22      paf        28: // defines
1.1       paf        29: 
1.22      paf        30: #define IMAGE_CLASS_NAME "image"
                     31: 
                     32: // class
                     33: 
                     34: class MImage : public Methoded {
                     35: public: // VStateless_class
                     36:        Value *create_new_value(Pool& pool) { return new(pool) VImage(pool); }
                     37: 
                     38: public:
                     39:        MImage(Pool& pool);
1.24      paf        40: 
                     41: public: // Methoded
1.22      paf        42:        bool used_directly() { return true; }
                     43: 
                     44: };
1.1       paf        45: 
                     46: // helpers
                     47: 
1.43      parser     48: #ifndef DOXYGEN
1.1       paf        49: class Measure_reader {
                     50: public:
1.45      parser     51:        enum { READ_CHUNK_SIZE=0x400*20 };// 20K
1.16      paf        52:        typedef size_t(*Func)(void *& buf, size_t limit, void *info);
1.1       paf        53: 
                     54:        Measure_reader(Func afunc, void *ainfo) : 
                     55:                func(afunc), info(ainfo), 
                     56:                chunk(0), offset(0), size(0) {
                     57:        }
                     58: 
1.31      parser     59:        size_t read(void *&buf, size_t limit) {
1.3       paf        60:                if(offset+limit>size) // nothing left
                     61:                        if(offset==0 || limit==1) { // only one-byte continuations allowed
                     62:                                size=(*func)(chunk, READ_CHUNK_SIZE, info);
                     63:                                offset=0;
                     64:                        } else
1.16      paf        65:                                return 0;// as if EOF
1.1       paf        66:                if(!size) // EOF
                     67:                        return 0;
                     68:                        
                     69:                // something left
                     70:                size_t read_size=min(offset+limit, size)-offset;
                     71:                buf=((unsigned char *)chunk)+offset;
                     72:                offset+=read_size;
                     73:                return read_size;
                     74:        }
                     75: 
                     76: private:
                     77:        Func func;
                     78:        void *info;
                     79: 
                     80:        void *chunk;
                     81:        size_t offset;
                     82:        size_t size;
                     83: };
1.43      parser     84: #endif
1.1       paf        85: 
1.21      paf        86: /// GIF file header
1.1       paf        87: struct GIF_Header {
1.16      paf        88:        char       type[3];         // 'GIF'
1.1       paf        89:        char       version[3];
                     90:        unsigned char       width[2];
                     91:        unsigned char       height[2];
                     92:        char       dif;
                     93:        char       fonColor;
                     94:        char       nulls;
                     95: };
                     96: 
1.31      parser     97: /// JPEG record head
                     98: struct JPG_Segment_head {
                     99:        unsigned char marker;
                    100:        unsigned char code;
                    101:        unsigned char length[2];
1.1       paf       102: };
1.21      paf       103: /// JPEG frame header
1.31      parser    104: struct JPG_Size_segment_body {
1.27      parser    105:        char data;                    //< data precision of bits/sample
1.31      parser    106:        unsigned char height[2];               //< image height
                    107:        unsigned char width[2];                //< image width
1.27      parser    108:        char numComponents;           //< number of color components
1.1       paf       109: };
                    110: 
                    111: //
                    112: 
1.33      parser    113: inline short x_endian_to_int(unsigned char L, unsigned char H) {
                    114:        return(short)((H<<8) + L);
                    115: }
                    116: 
                    117: inline short big_endian_to_int(unsigned char b[2]) {
                    118:        return x_endian_to_int(b[1], b[0]);
                    119: }
                    120: 
                    121: inline short little_endian_to_int(unsigned char b[2]) {
                    122:        return x_endian_to_int(b[0], b[1]);
1.1       paf       123: }
                    124: 
                    125: void measure_gif(Pool& pool, const String *origin_string, 
                    126:                         Measure_reader& reader, int& width, int& height) {
                    127: 
1.31      parser    128:        void *buf;
1.1       paf       129:        const int head_size=sizeof(GIF_Header);
                    130:        if(reader.read(buf, head_size)<head_size)
                    131:                PTHROW(0, 0, 
                    132:                        origin_string, 
1.34      parser    133:                        "not GIF file - too small");
1.31      parser    134:        GIF_Header *head=(GIF_Header *)buf;
1.1       paf       135: 
1.31      parser    136:        if(strncmp(head->type, "GIF", 3)!=0)
1.1       paf       137:                PTHROW(0, 0, 
                    138:                        origin_string, 
1.44      parser    139:                        "not GIF file - wrong signature");      
1.1       paf       140: 
1.33      parser    141:        width=little_endian_to_int(head->width);
                    142:        height=little_endian_to_int(head->height);
1.1       paf       143: }
                    144: 
                    145: void measure_jpeg(Pool& pool, const String *origin_string, 
                    146:                         Measure_reader& reader, int& width, int& height) {
1.2       paf       147:        // JFIF format markers
1.31      parser    148:        const unsigned char MARKER=0xFF;
1.34      parser    149:        const unsigned char CODE_SIZE_FIRST=0xC0;
                    150:        const unsigned char CODE_SIZE_LAST=0xC3;
1.2       paf       151: 
1.31      parser    152:        void *buf;
1.18      paf       153:        const size_t prefix_size=2;
1.31      parser    154:        if(reader.read(buf, prefix_size)<prefix_size)
1.1       paf       155:                PTHROW(0, 0, 
                    156:                        origin_string, 
1.34      parser    157:                        "not JPEG file - too small");
1.31      parser    158:        unsigned char *signature=(unsigned char *)buf;
1.1       paf       159:        
1.31      parser    160:        if(!(signature[0]==0xFF && signature[1]==0xD8)) 
1.1       paf       161:                PTHROW(0, 0, 
                    162:                        origin_string, 
1.44      parser    163:                        "not JPEG file - wrong signature");
1.31      parser    164: 
                    165:        bool found=false;
                    166:        while(true) {
                    167:                void *buf;
                    168:         // Extract the segment header.
                    169:                if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
                    170:                        break;          
                    171:                JPG_Segment_head *head=(JPG_Segment_head *)buf;
                    172: 
                    173:         // Verify that it's a valid segment.
                    174:                if(head->marker!=MARKER)
1.1       paf       175:                        break;
1.31      parser    176: 
1.34      parser    177:                if(head->code >= CODE_SIZE_FIRST && head->code  <= CODE_SIZE_LAST) {
1.31      parser    178:             // Segments that contain size info
                    179:                        if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
                    180:                                break;
                    181:                        JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
                    182:                        
1.32      parser    183:                        width=big_endian_to_int(body->width);
                    184:                        height=big_endian_to_int(body->height);
1.31      parser    185:                        found=true;
1.1       paf       186:                        break;
1.31      parser    187:                } else {
                    188:             // Dummy read to skip over data
1.32      parser    189:             size_t limit=big_endian_to_int(head->length) - 2;
1.31      parser    190:                        if(reader.read(buf, limit)<limit)
                    191:                                break;
                    192:         }
                    193:        }
                    194: 
                    195:        if(!found)
1.16      paf       196:                PTHROW(0, 0, 
                    197:                        origin_string, 
1.31      parser    198:                        "broken JPEG file - size frame not found");
1.1       paf       199: }
                    200: 
                    201: // measure center
                    202: 
                    203: void measure(Pool& pool, const String& file_name, 
                    204:                         Measure_reader& reader, int& width, int& height) {
                    205:        if(const char *cext=strrchr(file_name.cstr(), '.')) {
                    206:                cext++;
                    207:                if(strcasecmp(cext, "GIF")==0)
                    208:                        measure_gif(pool, &file_name, reader, width, height);
                    209:                else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) 
                    210:                        measure_jpeg(pool, &file_name, reader, width, height);
                    211:                else
                    212:                        PTHROW(0, 0, 
                    213:                                &file_name, 
                    214:                                "unhandled image file name extension '%s'", cext);
                    215:        } else
                    216:                PTHROW(0, 0, 
                    217:                        &file_name, 
                    218:                        "can not determine image type - no file name extension");
                    219: }
                    220: 
1.40      parser    221: #ifndef DOXYGEN
1.1       paf       222: struct Read_mem_info {
                    223:        unsigned char *ptr;
                    224:        unsigned char *eof;
                    225: };
1.40      parser    226: #endif
1.1       paf       227: static size_t read_mem(void*& buf, size_t limit, void *info) {
                    228:        Read_mem_info& rmi=*static_cast<Read_mem_info *>(info);
                    229:        buf=rmi.ptr;
                    230:        size_t read_size=min(limit, (size_t)(rmi.eof-rmi.ptr));
                    231:        rmi.ptr+=read_size;
                    232:        return read_size;
                    233: }
                    234: 
1.40      parser    235: #ifndef DOXYGEN
1.1       paf       236: struct Read_disk_info {
                    237:        const String *file_spec;
                    238:        size_t offset;
                    239: };
1.40      parser    240: #endif
1.1       paf       241: static size_t read_disk(void*& buf, size_t limit, void *info) {
                    242:        Read_disk_info& rdi=*static_cast<Read_disk_info *>(info);
                    243:        Pool& pool=rdi.file_spec->pool();
                    244: 
                    245:        size_t read_size;
                    246:        file_read(pool, *rdi.file_spec, 
                    247:                           buf, read_size, 
                    248:                           false/*as_text*/, 
                    249:                           true/*fail_on_read_problem*/, 
                    250:                           rdi.offset, limit);
                    251: 
                    252:        rdi.offset+=read_size;
                    253:        return read_size;
                    254: }
                    255: 
                    256: // methods
                    257: 
1.17      paf       258: static void _measure(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       259:        Pool& pool=r.pool();
                    260: 
1.30      parser    261:        Value& data=params->as_no_junction(0, "data must not be code");
1.1       paf       262: 
1.16      paf       263:        void *info;Measure_reader::Func read_func;
1.1       paf       264:        Read_mem_info read_mem_info;
                    265:        Read_disk_info read_disk_info;
                    266:        const String *file_name;
                    267:        if(data.is_string()) {
                    268:                file_name=data.get_string();
                    269:                read_disk_info.file_spec=&r.absolute(*file_name);
                    270:                read_disk_info.offset=0;
1.16      paf       271:                info=&read_disk_info;read_func=read_disk;
1.1       paf       272:        } else {
                    273:                const VFile& vfile=*data.as_vfile();
                    274:                file_name=&static_cast<Value *>(vfile.fields().get(*name_name))->as_string();
                    275:                read_mem_info.ptr=(unsigned char *)vfile.value_ptr();
                    276:                read_mem_info.eof=read_mem_info.ptr+vfile.value_size();
1.16      paf       277:                info=&read_mem_info;read_func=read_mem;
1.1       paf       278:        }
                    279: 
                    280:        Measure_reader reader(read_func, info);
                    281:        int width, height;
                    282:        measure(pool, *file_name, reader, width, height);
                    283: 
1.6       paf       284:        static_cast<VImage *>(r.self)->set(file_name, width, height);
1.1       paf       285: }
                    286: 
1.40      parser    287: #ifndef DOXYGEN
1.4       paf       288: struct Attrib_info {
1.21      paf       289:        String *tag; ///< html tag being constructed
                    290:        Hash *skip; ///< tag attributes not to append to tag string [to skip]
1.4       paf       291: };
1.40      parser    292: #endif
1.3       paf       293: static void append_attrib_pair(const Hash::Key& key, Hash::Val *val, void *info) {
1.4       paf       294:        Attrib_info& ai=*static_cast<Attrib_info *>(info);
                    295: 
1.49      parser    296:        // skip user-specified and internal(starting with "line-") attributes 
                    297:        if(ai.skip && ai.skip->get(key) || key.pos("line-")==0)
1.4       paf       298:                return;
                    299: 
1.3       paf       300:        Value& value=*static_cast<Value *>(val);
                    301:        // src="a.gif" width=123 ismap[=-1]
1.4       paf       302:        *ai.tag << " " << key;
1.26      parser    303:        if(value.is_string() || value.as_int()>=0)
1.6       paf       304:                *ai.tag << "=\"" << value.as_string() << "\"";
1.3       paf       305: }
1.17      paf       306: static void _html(Request& r, const String& method_name, MethodParams *params) {
1.3       paf       307:        Pool& pool=r.pool();
                    308: 
                    309:        String tag(pool);
                    310:        tag << "<img";
1.4       paf       311: 
1.18      paf       312:        const Hash& fields=static_cast<VImage *>(r.self)->fields();
1.5       paf       313:        Hash *attribs=0;
1.4       paf       314: 
1.39      parser    315:        if(params->size()) {
1.52      parser    316:                Value &vattribs=r.process(params->get(0),
                    317:                        0/*no name*/,
                    318:                        false/*don't intercept string*/);
1.39      parser    319:                if(vattribs.is_defined()) // allow 'void'
1.49      parser    320:                        if(attribs=vattribs.get_hash()) {
1.39      parser    321:                                Attrib_info attrib_info={&tag, 0};
                    322:                                attribs->for_each(append_attrib_pair, &attrib_info);
                    323:                        } else
                    324:                                PTHROW(0, 0, 
                    325:                                        &method_name, 
                    326:                                        "attributes must be hash");
                    327:        }
1.4       paf       328: 
1.5       paf       329:        Attrib_info attrib_info={&tag, attribs};
1.4       paf       330:        fields.for_each(append_attrib_pair, &attrib_info);
1.6       paf       331:        tag << " />";
1.3       paf       332:        r.write_pass_lang(tag);
                    333: }
1.8       paf       334: 
1.16      paf       335: static gdImage *load(Request& r, const String& method_name, 
                    336:                                         const String& file_name){
                    337:        Pool& pool=r.pool();
                    338: 
1.42      parser    339:        const char *file_name_cstr=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
1.16      paf       340:        if(FILE *f=fopen(file_name_cstr, "rb")) {
                    341:                gdImage& image=*new(pool) gdImage(pool);
1.23      paf       342:                bool ok=image.CreateFromGif(f);
1.16      paf       343:                fclose(f);
1.23      paf       344:                if(!ok)
                    345:                        PTHROW(0, 0, 
                    346:                                &file_name,
                    347:                                "is not in GIF format");
1.16      paf       348:                return &image;
                    349:        } else {
                    350:                PTHROW(0, 0, 
                    351:                        &method_name, 
                    352:                        "can not open '%s'", file_name_cstr);
                    353:                return 0;
                    354:        }
                    355: }
                    356: 
                    357: 
1.17      paf       358: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       359:        Pool& pool=r.pool();
                    360: 
1.44      parser    361:        const String& file_name=params->as_string(0, "file name must not be code");
1.6       paf       362: 
1.16      paf       363:        gdImage& image=*load(r, method_name, file_name);
                    364:        int width=image.SX();
                    365:        int height=image.SY();
                    366:        static_cast<VImage *>(r.self)->set(&file_name, width, height, &image);
1.6       paf       367: }
                    368: 
1.17      paf       369: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       370:        Pool& pool=r.pool();
                    371: 
1.51      parser    372:        int width=params->as_int(0, "width must be int", r);
                    373:        int height=params->as_int(1, "height must be int", r);
1.8       paf       374:        int bgcolor_value=0xffFFff;
1.6       paf       375:        if(params->size()>2)
1.51      parser    376:                bgcolor_value=params->as_int(2, "color must be int", r);
1.15      paf       377:        gdImage& image=*new(pool) gdImage(pool);
                    378:        image.Create(width, height);
                    379:        image.FilledRectangle(0, 0, width-1, height-1, image.Color(bgcolor_value));
                    380:        static_cast<VImage *>(r.self)->set(0, width, height, &image);
1.6       paf       381: }
                    382: 
1.17      paf       383: static void _gif(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       384:        Pool& pool=r.pool();
                    385: 
1.9       paf       386:        gdImage *image=static_cast<VImage *>(r.self)->image;
1.8       paf       387:        if(!image)
1.16      paf       388:                PTHROW(0, 0, 
                    389:                        &method_name, 
1.12      paf       390:                        "does not contain an image");
1.6       paf       391: 
                    392:        // could _ but don't thing it's wise to use $image.src for vfile.name
1.10      paf       393: 
1.16      paf       394:        String out(pool); image->Gif(out);
1.6       paf       395:        
                    396:        VFile& vfile=*new(pool) VFile(pool);
1.41      parser    397:        Value *content_type=new(pool) VString(*new(pool) String(pool, "image/gif"));
1.20      paf       398:        vfile.set(false/*not tainted*/, 
1.41      parser    399:                out.cstr(String::UL_AS_IS), out.size(), 0, content_type);
1.6       paf       400: 
                    401:        r.write_no_lang(vfile);
                    402: }
                    403: 
1.17      paf       404: static void _line(Request& r, const String& method_name, MethodParams *params) {
1.12      paf       405:        Pool& pool=r.pool();
                    406: 
                    407:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    408:        if(!image)
1.16      paf       409:                PTHROW(0, 0, 
                    410:                        &method_name, 
1.12      paf       411:                        "does not contain an image");
                    412: 
                    413:        image->Line(
1.51      parser    414:                params->as_int(0, "x0 must be int", r), 
                    415:                params->as_int(1, "y0 must be int", r), 
                    416:                params->as_int(2, "x1 must be int", r), 
                    417:                params->as_int(3, "y1 must be int", r), 
                    418:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       419: }
                    420: 
1.17      paf       421: static void _fill(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       422:        Pool& pool=r.pool();
                    423: 
                    424:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    425:        if(!image)
1.16      paf       426:                PTHROW(0, 0, 
                    427:                        &method_name, 
1.13      paf       428:                        "does not contain an image");
1.12      paf       429: 
1.13      paf       430:        image->Fill(
1.51      parser    431:                params->as_int(0, "x must be int", r), 
                    432:                params->as_int(1, "y must be int", r), 
                    433:                image->Color(params->as_int(2, "color must be int", r)));
1.13      paf       434: }
                    435: 
1.17      paf       436: static void _rectangle(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       437:        Pool& pool=r.pool();
                    438: 
                    439:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    440:        if(!image)
1.16      paf       441:                PTHROW(0, 0, 
                    442:                        &method_name, 
1.13      paf       443:                        "does not contain an image");
                    444: 
                    445:        image->Rectangle(
1.51      parser    446:                params->as_int(0, "x0 must be int", r), 
                    447:                params->as_int(1, "y0 must be int", r), 
                    448:                params->as_int(2, "x1 must be int", r), 
                    449:                params->as_int(3, "y1 must be int", r), 
                    450:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       451: }
                    452: 
1.17      paf       453: static void _bar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       454:        Pool& pool=r.pool();
                    455: 
                    456:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    457:        if(!image)
1.16      paf       458:                PTHROW(0, 0, 
                    459:                        &method_name, 
1.13      paf       460:                        "does not contain an image");
                    461: 
                    462:        image->FilledRectangle(
1.51      parser    463:                params->as_int(0, "x0 must be int", r), 
                    464:                params->as_int(1, "y0 must be int", r), 
                    465:                params->as_int(2, "x1 must be int", r), 
                    466:                params->as_int(3, "y1 must be int", r), 
                    467:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       468: }
                    469: 
1.44      parser    470: #ifndef DOXYGEN
                    471: static void add_point(Array::Item *value, void *info) {
                    472:        Array& row=*static_cast<Array *>(value);
                    473:        gdImage::Point **p=static_cast<gdImage::Point **>(info);
                    474:        
                    475:        (**p).x=row.get_string(0)->as_int();
                    476:        (**p).y=row.get_string(1)->as_int();
                    477:        (*p)++;
                    478: }
                    479: #endif
1.17      paf       480: static void _replace(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       481:        Pool& pool=r.pool();
                    482: 
                    483:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    484:        if(!image)
1.16      paf       485:                PTHROW(0, 0, 
                    486:                        &method_name, 
1.13      paf       487:                        "does not contain an image");
                    488: 
1.44      parser    489:        Table *table=params->as_no_junction(2, "coordinates must not be code").get_table();
                    490:        if(!table) 
                    491:                PTHROW(0, 0,
                    492:                        &method_name,
                    493:                        "coordinates must be table");
1.13      paf       494: 
1.44      parser    495:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    496:        gdImage::Point *add_p=all_p;    
                    497:        table->for_each(add_point, &add_p);
                    498:        image->FilledPolygonReplaceColor(all_p, table->size(), 
1.51      parser    499:                image->Color(params->as_int(0, "src color must be int", r)),
                    500:                image->Color(params->as_int(1, "dest color must be int", r)));
1.13      paf       501: }
                    502: 
1.44      parser    503: static void _polyline(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       504:        Pool& pool=r.pool();
                    505: 
                    506:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    507:        if(!image)
1.16      paf       508:                PTHROW(0, 0, 
                    509:                        &method_name, 
1.13      paf       510:                        "does not contain an image");
                    511: 
1.44      parser    512:        Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
                    513:        if(!table) 
                    514:                PTHROW(0, 0,
                    515:                        &method_name,
                    516:                        "coordinates must be table");
                    517: 
                    518:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    519:        gdImage::Point *add_p=all_p;    
                    520:        table->for_each(add_point, &add_p);
                    521:        image->Polygon(all_p, table->size(), 
1.51      parser    522:                image->Color(params->as_int(0, "color must be int", r)),
1.44      parser    523:                false/*not closed*/);
                    524: }
                    525: 
                    526: static void _polygon(Request& r, const String& method_name, MethodParams *params) {
                    527:        Pool& pool=r.pool();
                    528: 
                    529:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    530:        if(!image)
1.16      paf       531:                PTHROW(0, 0, 
                    532:                        &method_name, 
1.44      parser    533:                        "does not contain an image");
1.13      paf       534: 
1.44      parser    535:        Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
                    536:        if(!table) 
                    537:                PTHROW(0, 0,
                    538:                        &method_name,
                    539:                        "coordinates must be table");
                    540: 
                    541:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    542:        gdImage::Point *add_p=all_p;    
                    543:        table->for_each(add_point, &add_p);
                    544:        image->Polygon(all_p, table->size(), 
1.51      parser    545:                image->Color(params->as_int(0, "color must be int", r)));
1.13      paf       546: }
                    547: 
1.17      paf       548: static void _polybar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       549:        Pool& pool=r.pool();
                    550: 
                    551:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    552:        if(!image)
1.16      paf       553:                PTHROW(0, 0, 
                    554:                        &method_name, 
1.13      paf       555:                        "does not contain an image");
                    556: 
1.44      parser    557:        Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
                    558:        if(!table) 
                    559:                PTHROW(0, 0,
                    560:                        &method_name,
                    561:                        "coordinates must be table");
1.13      paf       562: 
1.44      parser    563:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    564:        gdImage::Point *add_p=all_p;    
                    565:        table->for_each(add_point, &add_p);
                    566:        image->FilledPolygon(all_p, table->size(), 
1.51      parser    567:                image->Color(params->as_int(0, "color must be int", r)));
1.12      paf       568: }
                    569: 
1.16      paf       570: // font
                    571: 
                    572: #define Y(y)(y+index*height+1)
1.21      paf       573: 
                    574: /// simple gdImage-based font storage & text output 
1.16      paf       575: class Font : public Pooled {
                    576: public:
                    577:        
1.38      parser    578:        const static int letter_spacing;
1.35      parser    579:        int height;         ///< Font heigth
                    580:        int monospace;      ///< Default char width
                    581:        int spacebarspace; ///< spacebar width
1.16      paf       582:        gdImage& ifont;
                    583:        const String& alphabet;
                    584:        
                    585:        Font(Pool& pool, 
                    586:                const String& aalphabet, 
1.35      parser    587:                gdImage& aifont, int aheight, int amonospace, int aspacebarspace) : Pooled(pool), 
1.16      paf       588:                alphabet(aalphabet), 
1.35      parser    589:                height(aheight), monospace(amonospace),  spacebarspace(aspacebarspace),
1.16      paf       590:                ifont(aifont) {
                    591:        }
                    592:        
                    593:        /* ******************************** char ********************************** */
                    594:        
                    595:        int index_of(char ch) {
                    596:                if(ch==' ') return -1;
                    597:                return alphabet.pos(&ch, 1);
                    598:        }
                    599:        
                    600:        int index_width(int index) {
                    601:                if(index<0)
1.35      parser    602:                        return spacebarspace;
1.16      paf       603:                int tr=ifont.GetTransparent();
1.35      parser    604:                for(int x=ifont.SX()-1; x>=0; x--) {
1.16      paf       605:                        for(int y=0; y<height-1; y++)
                    606:                                if(ifont.GetPixel(x, Y(y))!=tr) 
1.35      parser    607:                                        return x+1;
1.16      paf       608:                }
                    609:                return 0;
                    610:        }
                    611:        
                    612:        void index_display(gdImage& image, int x, int y, int index){
                    613:                if(index>=0) 
                    614:                        ifont.Copy(image, x, y, 0, Y(0), index_width(index), height-1);
                    615:        }
                    616:        
                    617:        /* ******************************** string ********************************** */
1.47      parser    618:        
                    619:        int string_width(const String& s){
                    620:                const char *cstr=s.cstr(String::UL_AS_IS);
1.16      paf       621:                int result=0;
                    622:                for(; *cstr; cstr++)
                    623:                        result+=index_width(index_of(*cstr));
                    624:                return result;
                    625:        }
                    626:        
                    627:        void string_display(gdImage& image, int x, int y, const String& s){
                    628:                const char *cstr=s.cstr(String::UL_AS_IS);
                    629:                if(cstr) for(; *cstr; cstr++) {
                    630:                        int index=index_of(*cstr);
                    631:                        index_display(image, x, y, index);
1.38      parser    632:                        x+=letter_spacing + (monospace ? monospace : index_width(index));
1.16      paf       633:                }
                    634:        }
                    635:        
                    636: };
1.38      parser    637: const int Font::letter_spacing=1;
1.35      parser    638: 
1.17      paf       639: static void _font(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       640:        Pool& pool=r.pool();
                    641: 
1.37      parser    642:        const String& alphabet=params->as_string(0, "alphabet must not be code");
                    643:        gdImage& image=*load(r, method_name, params->as_string(1, "file_name must not be code"));
1.51      parser    644:        int spacebar_width=params->as_int(2, "spacebar_width must be int", r);
1.37      parser    645:        int monospace_width;
                    646:        if(params->size()>3) {
1.51      parser    647:                monospace_width=params->as_int(3, "monospace_width must be int", r);
1.37      parser    648:                if(!monospace_width)
                    649:                        monospace_width=image.SX();
                    650:        } else
                    651:                monospace_width=0;
1.16      paf       652: 
1.37      parser    653:        if(!alphabet.size())
                    654:                PTHROW(0, 0,
                    655:                        &method_name,
                    656:                        "alphabet must not be empty");
                    657:        
1.16      paf       658:        static_cast<VImage *>(r.self)->font=new(pool) Font(pool, 
1.37      parser    659:                alphabet, 
1.36      parser    660:                image, 
1.37      parser    661:                image.SY() / alphabet.size(), monospace_width, spacebar_width);
1.16      paf       662: }
                    663: 
1.17      paf       664: static void _text(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       665:        Pool& pool=r.pool();
                    666: 
1.51      parser    667:        int x=params->as_int(0, "x must be int", r);
                    668:        int y=params->as_int(1, "y must be int", r);
1.36      parser    669:        const String& s=params->as_string(2, "text must not be code");
1.16      paf       670: 
                    671:        VImage& vimage=*static_cast<VImage *>(r.self);
                    672:        if(vimage.image)
                    673:                if(vimage.font)
                    674:                        vimage.font->string_display(*vimage.image, x, y, s);
                    675:                else
                    676:                        PTHROW(0, 0,
                    677:                                &method_name,
                    678:                                "set the font first");
                    679:        else
                    680:                PTHROW(0, 0, 
                    681:                        &method_name, 
                    682:                        "does not contain an image");
                    683: }
                    684: 
1.47      parser    685: static void _length(Request& r, const String& method_name, MethodParams *params) {
                    686:        Pool& pool=r.pool();
                    687: 
                    688:        const String& s=params->as_string(0, "text must not be code");
                    689: 
                    690:        VImage& vimage=*static_cast<VImage *>(r.self);
                    691:        if(vimage.image)
                    692:                if(vimage.font) {
                    693:                        VInt& result=*new(pool) VInt(pool, vimage.font->string_width(s));
                    694:                        result.set_name(method_name);
                    695:                        r.write_assign_lang(result);
                    696:                } else
                    697:                        PTHROW(0, 0,
                    698:                                &method_name,
                    699:                                "set the font first");
                    700:        else
                    701:                PTHROW(0, 0, 
                    702:                        &method_name, 
                    703:                        "does not contain an image");
                    704: }
                    705: 
1.48      parser    706: static void _arc(Request& r, const String& method_name, MethodParams *params) {
                    707:        Pool& pool=r.pool();
                    708: 
                    709:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    710:        if(!image)
                    711:                PTHROW(0, 0, 
                    712:                        &method_name, 
                    713:                        "does not contain an image");
                    714: 
                    715:        image->Arc(
1.51      parser    716:                params->as_int(0, "center_x must be int", r), 
                    717:                params->as_int(1, "center_y must be int", r), 
                    718:                params->as_int(2, "width must be int", r), 
                    719:                params->as_int(3, "height must be int", r), 
                    720:                params->as_int(4, "start degrees must be int", r), 
                    721:                params->as_int(5, "end degrees must be int", r), 
                    722:                image->Color(params->as_int(6, "cx must be int", r)));
1.48      parser    723: }
                    724: 
1.49      parser    725: static void _sector(Request& r, const String& method_name, MethodParams *params) {
                    726:        Pool& pool=r.pool();
                    727: 
                    728:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    729:        if(!image)
                    730:                PTHROW(0, 0, 
                    731:                        &method_name, 
                    732:                        "does not contain an image");
                    733: 
                    734:        image->Sector(
1.51      parser    735:                params->as_int(0, "center_x must be int", r), 
                    736:                params->as_int(1, "center_y must be int", r), 
                    737:                params->as_int(2, "width must be int", r), 
                    738:                params->as_int(3, "height must be int", r), 
                    739:                params->as_int(4, "start degrees must be int", r), 
                    740:                params->as_int(5, "end degrees must be int", r), 
                    741:                image->Color(params->as_int(6, "color must be int", r)));
1.49      parser    742: }
                    743: 
1.48      parser    744: static void _circle(Request& r, const String& method_name, MethodParams *params) {
                    745:        Pool& pool=r.pool();
                    746: 
                    747:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    748:        if(!image)
                    749:                PTHROW(0, 0, 
                    750:                        &method_name, 
                    751:                        "does not contain an image");
                    752: 
1.51      parser    753:        int size=params->as_int(2, "radius must be int", r)*2;
1.48      parser    754:        image->Arc(
1.51      parser    755:                params->as_int(0, "center_x must be int", r), 
                    756:                params->as_int(1, "center_y must be int", r), 
1.50      parser    757:                size, //w
                    758:                size, //h
1.48      parser    759:                0, //s
                    760:                360, //e
1.51      parser    761:                image->Color(params->as_int(3, "color must be int", r)));
1.48      parser    762: }
                    763: 
1.53      parser    764: gdImage& as_image(Pool& pool, const String& method_name, MethodParams *params, 
                    765:                                                int index, const char *msg) {
                    766:        Value& value=params->as_no_junction(index, msg);
                    767: 
                    768:        if(strcmp(value.type(), VIMAGE_TYPE)!=0)
                    769:                PTHROW(0, 0, 
                    770:                        &method_name, 
                    771:                        msg);
                    772: 
                    773:        gdImage *src=static_cast<VImage *>(&value)->image;
                    774:        if(!src)
                    775:                PTHROW(0, 0, 
                    776:                        &method_name, 
                    777:                        msg);
                    778: 
                    779:        return *src;
                    780: }
                    781: 
                    782: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h]])
                    783: static void _copy(Request& r, const String& method_name, MethodParams *params) {
                    784:        Pool& pool=r.pool();
                    785: 
                    786:        gdImage *dest=static_cast<VImage *>(r.self)->image;
                    787:        if(!dest)
                    788:                PTHROW(0, 0, 
                    789:                        &method_name, 
                    790:                        "self does not contain an image");
                    791: 
                    792:        gdImage& src=as_image(pool, method_name, params, 0, "src must be image");
                    793: 
                    794:        int sx=params->as_int(1, "src_x must be int", r);
                    795:        int sy=params->as_int(2, "src_y must be int", r);
                    796:        int sw=params->as_int(3, "src_w must be int", r);
                    797:        int sh=params->as_int(4, "src_h must be int", r);
                    798:        int dx=params->as_int(5, "dest_x must be int", r);
                    799:        int dy=params->as_int(6, "dest_y must be int", r);
                    800:        if(params->size()>1+2+2+2) {
                    801:                int dw=params->as_int(1+2+2+2, "dest_w must be int", r);
                    802:                int dh=params->size()>1+2+2+2+1?params->as_int(1+2+2+2+1, "dest_h must be int", r):dw;
                    803: 
1.55    ! parser    804:                src.CopyResampled(*dest, dx, dy, sx, sy, dw, dh, sw, sh);
1.53      parser    805:        } else
1.54      parser    806:                src.Copy(*dest, dx, dy, sx, sy, sw, sh);
1.53      parser    807: }
                    808: 
                    809: 
1.22      paf       810: // constructor
                    811: 
                    812: MImage::MImage(Pool& apool) : Methoded(apool) {
                    813:        set_name(*NEW String(pool(), IMAGE_CLASS_NAME));
                    814: 
                    815: 
1.1       paf       816:        // ^image:measure[DATA]
1.22      paf       817:        add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3       paf       818: 
1.25      paf       819:        // ^image.html[]
                    820:        // ^image.html[hash]
1.22      paf       821:        add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf       822: 
1.25      paf       823:        // ^image.load[background.gif]
1.22      paf       824:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf       825: 
1.25      paf       826:        // ^image.create[width;height] bgcolor=white
                    827:        // ^image.create[width;height;bgcolor]
1.22      paf       828:        add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6       paf       829: 
1.25      paf       830:        // ^image.gif[]
1.22      paf       831:        add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12      paf       832: 
1.25      paf       833:        // ^image.line(x0;y0;x1;y1;color)
1.22      paf       834:        add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf       835: 
1.25      paf       836:        // ^image.fill(x;y;color)
1.22      paf       837:        add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13      paf       838: 
1.25      paf       839:        // ^image.rectangle(x0;y0;x1;y1;color)
1.22      paf       840:        add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13      paf       841: 
1.25      paf       842:        // ^image.bar(x0;y0;x1;y1;color)
1.22      paf       843:        add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13      paf       844: 
1.44      parser    845:        // ^image.replace(color-source;color-dest)[table x:y]
                    846:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 3, 3);
                    847: 
                    848:        // ^image.polyline(color)[table x:y]
                    849:        add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13      paf       850: 
1.44      parser    851:        // ^image.polygon(color)[table x:y]
                    852:        add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13      paf       853: 
1.44      parser    854:        // ^image.polybar(color)[table x:y]
                    855:        add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13      paf       856: 
1.36      parser    857:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
                    858:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;width)
                    859:        add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16      paf       860: 
1.25      paf       861:     // ^image.text(x;y)[text]
1.22      paf       862:        add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47      parser    863:        
                    864:     // ^image.ngth[text]
                    865:        add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16      paf       866:        
1.48      parser    867:        // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
                    868:        add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49      parser    869: 
                    870:        // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
                    871:        add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48      parser    872: 
                    873:        // ^image.circle(center x;center y;r;color)
                    874:        add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
                    875: 
1.53      parser    876:        // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h]])
                    877:        add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2);
1.22      paf       878: }
                    879: 
                    880: // global variable
                    881: 
                    882: Methoded *image_class;
                    883: 
                    884: // creator
                    885: 
                    886: Methoded *MImage_create(Pool& pool) {
                    887:        return image_class=new(pool) MImage(pool);
1.1       paf       888: }

E-mail: